Last active
April 29, 2022 10:37
-
-
Save fxadecimal/a79e8575e064d95dbe382d7cccb97580 to your computer and use it in GitHub Desktop.
Python3 Snippets
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
# Quick Virtualenv | |
sudo apt-get install python3-pip python3-virtualenv | |
virtualenv .venv && source .venv/bin/activate | |
which pip # .venv/bin/pip | |
pip install -r requirements | |
# CSV | |
## To and from list of dictionaries | |
import csv | |
with open('df_big.csv') as f: | |
reader = csv.DictReader(f) | |
list_of_dicts = list(reader) | |
with open('output.csv', 'w') as f: | |
writer = csv.DictWriter(f, list_of_dicts[0].keys()) | |
writer.writeheader() | |
writer.writerows(list_of_dicts) | |
# Logging | |
## Shorthand | |
import logging, logging.handlers | |
logging.basicConfig( | |
level=logging.DEBUG, | |
format="%(asctime)s [%(levelname)s] %(message)s", | |
handlers=[ | |
logging.handlers.RotatingFileHandler('debug.log', mode='a', maxBytes=5*1024*1024, backupCount=2), | |
logging.StreamHandler() # echo to screen | |
] | |
) | |
# Glob files | |
import glob | |
def glob_files(dir_path, match='*.html'): | |
return glob.glob(f'{dir_path}/{match}') | |
# Requests | |
import requests | |
## prep a request | |
request = requests.Request('POST', url, headers=headers, json=payload).prepare() | |
## inspect request | |
print( json.dumps( request.__dict__, indent=1, default=str)) ) | |
## send request | |
response = session.send( request ) | |
# Simple print in colour | |
# Source: https://github.com/coleifer/huey/blob/master/examples/django_ex/djangoex/test_app/tasks.py | |
def tprint(s, c=32): | |
# Helper to print messages from within tasks using color, to make them | |
# stand out in examples. | |
# c = 31 -> 36 | |
print('\x1b[1;%sm%s\x1b[0m' % (c, s)) | |
# Date range generator function | |
import datetime | |
def today_and_yesterday(today=None): | |
if today is None: | |
today = datetime.datetime.utcnow().replace( | |
hour=0, minute=0, second=0, microsecond=0 | |
) | |
yesterday = today - datetime.timedelta(days=1) | |
return today, yesterday | |
def date_range_generator(starts, ends = None, step=None): | |
if step is None: | |
step = datetime.timedelta(days=1) | |
if ends is None: | |
_, ends = today_and_yesterday() | |
current = starts | |
while current <= ends: | |
yield current | |
current = current + step | |
# decorator that makes functions fail silently - use at your own risk! | |
def fail_silently(func): | |
def wrapper(*args, **kwargs): | |
try: | |
return func(*args, **kwargs) | |
except: | |
return None | |
return wrapper | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment