Skip to content

Instantly share code, notes, and snippets.

@FBosler
Last active August 19, 2019 05:11
Show Gist options
  • Save FBosler/b48bc10efdf4a1396d373520f26e248e to your computer and use it in GitHub Desktop.
Save FBosler/b48bc10efdf4a1396d373520f26e248e to your computer and use it in GitHub Desktop.
helper functions for the cohorts analysis
def generate_dummy_names(adj,sub,number_names=10):
"""
function generates random name combinations of the provided adjectives and subjects
>>> name_generator(adj=['cool','strong'],sub=['harry','kate'],number_names=3)
['cool_harry', 'strong_kate', 'strong_harry', 'cool_kate']
"""
if number_names > len(adj)*len(sub):
raise ValueError(f"""
Can at most genereate {len(adj)*len(sub) -1} names, increase adj or sub to allow for more names
""")
res = set()
while len(res) < number_names:
new_name = f'{np.random.choice(adj)}_{np.random.choice(sub)}'
res = res | set([new_name])
return list(res)
def generate_dummy_order_id(size=16, chars=list(string.ascii_uppercase + string.digits)):
"""
function generates random order ids
>>> generate_order_id()
'0BHSIX003CJKMH2A'
"""
return ''.join(np.random.choice(chars) for _ in range(size))
def fortmat_quarter(x):
"""
function turns a datetime into a string representation of the corresponding quarter:
>>> fortmat_quarter(datetime.datetime(2018,1,3))
'2018-Q1'
>>> fortmat_quarter(datetime.datetime(2019,5,3))
'2019-Q2'
"""
quarter = (x.month-1)//3 + 1
return str(x.year)+'-Q'+str(quarter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment