Created
December 1, 2020 02:09
-
-
Save MicahParks/6a44e6baef9f6c75fd8ad664331c613c to your computer and use it in GitHub Desktop.
Cereal generator
This file contains hidden or 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
#!/usr/bin/env python | |
""" | |
Create successive serials based on a given character list. Optionally, provide a starting point for creating serials. | |
Use this when you need to generate unique serials for things (where variable length is acceptable). | |
""" | |
# Start standard library imports. | |
from string import ascii_lowercase, digits | |
from typing import Generator, List | |
# End standard library imports. | |
class Cereal: | |
def __init__(self, belowCerealObj, charList: List[str], customCharList: List[str] = None) -> None: | |
self.belowCerealObj = belowCerealObj | |
self.charList = charList | |
self.customCharList = customCharList | |
def cerealize(self) -> Generator[str, None, None]: | |
""" | |
Create a generator that yields all the characters of belowCerealObj and this instance in successive order. | |
""" | |
if self.customCharList is None: | |
self.customCharList = self.charList | |
for nowChar in self.customCharList: | |
if self.belowCerealObj is not None: | |
belowCerealObjCerealizeGen = self.belowCerealObj.cerealize() | |
for nowBelowCerealChars in belowCerealObjCerealizeGen: | |
yield nowChar + nowBelowCerealChars | |
else: | |
yield nowChar | |
self.customCharList = None | |
def cereal_generator(charList: List[str], beforeCerealStr: str = str()) -> Generator[Cereal, None, None]: | |
""" | |
Create a generator that yields successive serials utilizing the Cereal object. | |
""" | |
belowCerealObj = None | |
if len(beforeCerealStr) == 0: | |
cerealObj = Cereal(belowCerealObj=belowCerealObj, charList=charList) | |
cerealObjCerealizeGen = cerealObj.cerealize() | |
else: | |
for nowIndexInt in reversed(range(len(beforeCerealStr))): | |
char = beforeCerealStr[nowIndexInt] | |
charIndexInt = charList.index(char) | |
customCharList = charList[charIndexInt:] | |
cerealObj = Cereal(belowCerealObj=belowCerealObj, charList=charList, customCharList=customCharList) | |
belowCerealObj = cerealObj | |
try: | |
cerealObjCerealizeGen = cerealObj.cerealize() | |
except NameError: | |
print('"{}" is not a valid serial with the given character lists.'.format(beforeCerealStr)) | |
quit(1) | |
# next(cerealObjCerealizeGen) == beforecerealstr | |
next(cerealObjCerealizeGen) | |
while True: | |
for nowCerealObj in cerealObjCerealizeGen: | |
yield nowCerealObj | |
belowCerealObj = cerealObj | |
cerealObj = Cereal(belowCerealObj=belowCerealObj, charList=charList) | |
cerealObjCerealizeGen = cerealObj.cerealize() | |
def main() -> None: | |
""" | |
The logic of the file. | |
The feature of cereal_generator where a valid serial is continued upon is not showcased in executing main(). | |
""" | |
cerealGen = cereal_generator(charList=ascii_lowercase + digits) | |
for nowCerealStr in cerealGen: | |
print(nowCerealStr) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment