test.py 883 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import logging
  2. from scriptshifter.exceptions import CONT
  3. __doc__ = """ Test hook functions.
  4. Do not remove. Used in unit tests.
  5. """
  6. logger = logging.getLogger(__name__)
  7. def rotate(ctx, n):
  8. """
  9. Simple character rotation.
  10. Implements the Caesar's Cypher algorithm by shifting a single
  11. [A-Za-z] character by `n` places, and wrapping around
  12. the edges.
  13. Characters not in range are not shifted.
  14. """
  15. uc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  16. lc = uc.lower()
  17. logger.debug(f"cursor before ROT: {ctx.cur}")
  18. ch = ctx.src[ctx.cur]
  19. if ch in uc:
  20. idx = uc.index(ch)
  21. dest_ch = uc[(idx + n) % len(uc)]
  22. elif ch in lc:
  23. idx = lc.index(ch)
  24. dest_ch = lc[(idx + n) % len(lc)]
  25. else:
  26. dest_ch = ch
  27. logger.debug(f"ROT{n}: {ch} -> {dest_ch}")
  28. ctx.dest_ls.append(dest_ch)
  29. ctx.cur += 1
  30. return CONT