Created
December 16, 2017 17:36
-
-
Save tommelo/f6ffbac25b99835a273327e58968e6a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Alphanumeric(object): | |
ALPHABET_LENGTH = 26 | |
def __init__(self, nrange=None): | |
self.current_letter = 'z' | |
self.current_number = 0 | |
self.nrange = nrange | |
def forward_letter(self, letter, positions): | |
if letter.islower(): | |
unicode_point = ord('a') | |
else: | |
unicode_point = ord('A') | |
start = ord(letter) - unicode_point | |
offset = ((start + positions) % self.ALPHABET_LENGTH) + unicode_point | |
self.current_letter = chr(offset) | |
return self.current_letter | |
def backward_letter(self, letter, positions): | |
if letter.islower(): | |
unicode_point = ord('a') | |
else: | |
unicode_point = ord('A') | |
start = ord(letter) - unicode_point | |
offset = ((start - positions) % self.ALPHABET_LENGTH) + unicode_point | |
self.current_letter = chr(offset) | |
return self.current_letter | |
def next_letter(self): | |
return self.forward_letter(self.current_letter, 1) | |
def previous_letter(self): | |
return self.backward_letter(self.current_letter, 1) | |
def forward_number(self, number, positions): | |
if not self.nrange: | |
self.current_number = number + positions | |
return self.current_number | |
index = self.nrange.index(number) | |
start = index + positions | |
offset = (start % len(self.nrange)) | |
self.current_number = self.nrange[offset] | |
return self.current_number | |
def backward_number(self, number, positions): | |
if not self.nrange: | |
return number - positions | |
index = self.nrange.index(number) | |
start = index - positions | |
offset = (start % len(self.nrange)) | |
self.current_number = self.nrange[offset] | |
return self.current_number | |
def next_number(self): | |
return self.forward_number(self.current_number, 1) | |
def previous_number(self): | |
return self.backward_number(self.current_number, 1) | |
def forward_alphanumeric(self, alpha, positions, ignore_numbers=False, ignore_letters=False): | |
result = "" | |
for char in alpha: | |
if char.isdigit() and not ignore_numbers: | |
char = str(self.forward_number(int(char), positions)) | |
if char.isalpha() and not ignore_letters: | |
char = self.forward_letter(char, positions) | |
result += char | |
return result | |
def backward_alphanumeric(self, alpha, positions, ignore_numbers=False, ignore_letters=False): | |
result = "" | |
for char in alpha: | |
if char.isdigit() and not ignore_numbers: | |
char = str(self.backward_number(int(char), positions)) | |
if char.isalpha() and not ignore_letters: | |
char = self.backward_letter(char, positions) | |
result += char | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment