test.py 847 B

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