import sys import time import datetime file = sys.stderr def log(logfile, tag): now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M EST') with open(logfile, 'a') as fd: fd.write(f'{now} {tag}\n') def do(minutes, logfile, tag): log(logfile, f'{tag} start') seconds = minutes*60 for i in range(seconds): file.flush() #s = str(i%60).zfill(2) file.write(f'\r{i//60}:{str(i%60).zfill(2)}') time.sleep(1) log(logfile, f'{tag} end') And choose any logfile location and any tag ..
Handies
line profiler Big fan of the line_profiler ( formerly here ) pip install line_profiler expensive.py import time @profile def foo(): for x in range(10): bar() flarg() @profile def bar(): time.sleep(.1) @profile def flarg(): time.sleep(.1) foo() profile (pandars38) ツ kernprof -lv expensive.py Wrote profile results to expensive.py.lprof Timer unit: 1e-06 s Total time: 2.06251 s File: expensive.py Function: foo at line 3 Line # Hits Time Per Hit % Time Line Contents ============================================================== 3 @profile 4 def foo(): 5 11 54....
What the what Notes from after converting a project using the 2to3, of additional gotchas TOC StringIO Pickling Uuid xrange wow the silent division bug! func.func_name calling lambdas w/ boto3 and using BytesIO Bytes and json lambda , [ERROR] Runtime.MarshalError: Unable to marshal response: b'gAN9cQAoWA4 dict merging Meat StringIO Doing this fixes things typically.. Change import StringIO to try: from StringIO import StringIO except: from io import StringIO And update any StringIO....
Generate a CTE from a local csv file import pandas as pd replace_nan = lambda x: x.replace('nan', 'null') def df_to_values(df, columns=None, replace_nans=True): if columns is None: columns = df.columns.tolist() newdata = str(list(df[columns].to_records( index=False)) )[1:-1] if replace_nans: newdata = replace_nan(newdata) return newdata def cte_from_csv(localfile, colgroups, cte_names, head=False): df = pd.read_csv(localfile) if head: df = df.head() return 'with ' + ', '.join([ f''' {cte_names[i]}({', '.join(colgroups[i])}) as ( VALUES {df_to_values(df, columns=colgroups[i], replace_nans=True)} ) ''' for i, _ in enumerate(colgroups) ]) temp....
Search and return json paths def substring_exists_lower(substring, string): # f = lambda key, term: term in key.lower() return substring.lower() in string.lower() def path_join(path, key): return f'{path}{"." if path else ""}{key}' def find_term(path, term, node, found, only_leaves=False): # must be dict or list if not ((isinstance(node, dict)) or (isinstance(node, list))): return # look in this node if isinstance(node, dict): for key in node.keys(): if substring_exists_lower(term, key): if only_leaves: if not ((isinstance(node[key], dict)) or (isinstance(node[key], list))): found....