Created
December 18, 2019 18:08
-
-
Save me2beats/190b69da4429a1ce8d38b780a963296f to your computer and use it in GitHub Desktop.
my cycle function
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
def cycle(*args, **kw): | |
d = my_getattr(cycle, 'dict', {}) | |
key = my_get(d, args, 0) | |
try: | |
res = args[key] | |
except IndexError: | |
res = args[0] | |
d[args]=0 | |
d[args]+=1 | |
return res | |
def my_getattr(obj, attr, default): | |
#sets default attr, if not found | |
value = getattr(obj, attr, default) | |
if value is default: | |
setattr(obj, attr, value) | |
return value | |
def my_get(d, key, default): | |
''' | |
get dict val. | |
if not found, set (and return) default | |
''' | |
try: | |
return d[key] | |
except KeyError: | |
d[key] = default | |
return default | |
if __name__ == '__main__': | |
pass | |
# soon |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment