Some things and techniques I like python for
Table of Contents
- TODO [C] hacked seaborn to display vs on pairplots python
- TODO [C] hacking logging.Logger to do basicConfig python
- TODO [C] why generators are good: fbchat export example python
- TODO [C] another example of cool generator use in fbchat python
- TODO example of hacky setuplogger python
- [C] hacky 'autonaming' python
- TODO [C] why mypy is good: injector DI library pythontoblogmypy
- TODO [D] Why python is good pythontoblog
- TODO [C] Literal types and demonstrate mypy? Perhaps timezones as example? pythontoblogmypyconfigs
- TODO By highlighting this i will know what patterns to look for in other languages pythontoblog
¶TODO [C] hacked seaborn to display vs on pairplots python
used inspect to extract directly from local variables, lol
https://github.com/mwaskom/seaborn/blob/master/seaborn/axisgrid.py#L1340-L1341
¶TODO [D] why python is good: patching seaborn bootstrap python
I've had nondeterministic behaviour due to random seed
was not propagated, but with a patch it's possible to achieve
# TODO careful.. need to do this ONCE bootstrap_orig = sns.algorithms.bootstrap def bootstrap_hacked(*args, **kwargs): return bootstrap_orig(*args, random_seed=0, **kwargs) sns.algorithms.bootstrap = bootstrap_hacked
¶TODO [C] hacking logging.Logger to do basicConfig python
logging.basicConfig( format='%(asctime)s %(levelname)-8s %(message)s', level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S')
However if you are a well-mannered person, you don't want to mess with other loggers and you're probably providing your own logger method
This is a bit horrible
https://stackoverflow.com/a/28330410/706389
For my personal projects I'm using logzero which has got a single line setup method. However, for code I'm sharing with other people I prefer to have as little external dependencies as you can
e.g. libraries should never configure logging
https://www.reddit.com/r/learnpython/comments/69grzn/logging_confuses_me/dh6ngvo/
¶ could publish in r/python? python
¶ sometimes it makes sense for your library to configure other loggers (e.g. requests) python
¶TODO [C] why generators are good: fbchat export example python
separating network retrieval and storing in database. with generators can do it simultaneously
¶TODO [C] another example of cool generator use in fbchat python
mts = int(r.timestamp) if newest is not None and oldest is not None and newest > mts > oldest: logger.info('Fetched everything for %s', thread.name) break # interrupt, thus interrupting fetching data db.insert_message(thread, r)
¶TODO example of hacky setuplogger python
¶[C] hacky 'autonaming' python
import ast import traceback import inspect src = ast.parse(open(__file__).read()) def find_name(lno: int): # TODO end_lineno is not set?? node = None for x in src.body: if x.lineno >= lno: break node = x [tg] = node.targets return tg.id class Namer: @property def auto(self) -> str: stack = inspect.stack() fr = stack[1] lno = fr.lineno return find_name(lno) N = Namer() aaaa = ( 1, N.auto, ) bbbb = ( 2, N.auto, ) print(aaaa) print(bbbb)
¶TODO [C] why mypy is good: injector DI library pythontoblogmypy
¶TODO [D] Why python is good pythontoblog
generators, decorators and random dynamic things with examples