Skip to content

Instantly share code, notes, and snippets.

@snahor
Created October 16, 2017 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snahor/db8e2c2914dc9a108541eedca56c57b3 to your computer and use it in GitHub Desktop.
Save snahor/db8e2c2914dc9a108541eedca56c57b3 to your computer and use it in GitHub Desktop.
Caesar Cipher
from functools import wraps
a_ord = ord('a')
class NonAlphaException(Exception):
pass
def check(func):
@wraps(func)
def wrapper(alpha, offset):
if not alpha.isalpha():
raise NonAlphaException
return func(alpha, offset)
return wrapper
@check
def encrypt(alpha, offset):
return chr(a_ord + ((ord(alpha) - a_ord + offset) % 26))
@check
def decrypt(alpha, offset):
return chr(a_ord + ((ord(alpha) - a_ord - offset) % 26))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment