Last active
January 13, 2018 21:56
-
-
Save stevenvo/50e1d716fb205cc2fd07dff1e5ef5d85 to your computer and use it in GitHub Desktop.
Useful python snippets
This file contains 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
############################################################################# | |
# Generate Dict from a List of Dict | |
ticker = [ | |
{ | |
'symbol': 'ABC', | |
'price': 100.0 | |
}, | |
{ | |
'symbol': 'CDE', | |
'price': 105.0 | |
} | |
] | |
prices = {t['symbol']: t for t in ticker} | |
# prices: | |
# { | |
# 'ABC': { | |
# 'symbol': 'ABC', | |
# 'price': 100.0 | |
# }, | |
# 'CDE': { | |
... | |
# } | |
############################################################################# | |
# Subclass UserDict to create object with dict | |
# - able to acccess property by key, for example obj1['prop1'] | |
# - able to access property by obj 'dot' reference, for example obj1.prop1 | |
from collections import UserDict # using python 3 | |
class MyUserDict(UserDict): | |
def __init__(self, data={}, **kw): | |
UserDict.__init__(self) | |
self.update(data) | |
self.update(kw) | |
def __getattr__(self, attr): | |
return self.get(attr) | |
def __setattr__(self, key, value): | |
self.__dict__.update({key: value}) | |
def test(): | |
sample_data = {'b': 2, 'a': 1} | |
t = MyUserDict(sample_data, c=1) | |
assert t['a'] == 1 | |
assert t.a == 1 | |
t.b=10 | |
assert t.b == 10 | |
print("ALL tests passed!") | |
MyUserDict.test() | |
############################################## | |
# Array of hashmap to String and vice versa # | |
import json | |
t = [ | |
{'a': 1, 'b': '2'} | |
] | |
s = json.dumps(t) | |
print(s) | |
t = json.loads(s) | |
print(t) | |
############################################## | |
# LOGGING TO FILE AND CONSOLE # | |
import logging | |
import sys | |
import datetime | |
from logging.handlers import RotatingFileHandler | |
fmt = logging.Formatter("%(asctime)s [%(levelname)-5.5s] L%(lineno)s: %(message)s", datefmt = '%Y/%m/%d %I:%M:%S %p') | |
logger = logging.getLogger() | |
logger.setLevel(logging.DEBUG) | |
fileHandler = RotatingFileHandler( | |
f"{sys.argv[0]}.log", maxBytes=(1024*1024*10), backupCount=7) | |
fileHandler.setFormatter(fmt) | |
logger.addHandler(fileHandler) | |
consoleHandler = logging.StreamHandler() | |
consoleHandler.setFormatter(fmt) | |
logger.addHandler(consoleHandler) | |
logger.info(datetime.datetime.now()) | |
############################################## | |
# Compare 2 dicts except some keys | |
def compare_dict(d1, d2, ignore): | |
for k in d1: | |
if k in ignore: | |
continue | |
try: | |
if d1[k] != d2[k]: | |
return False | |
except KeyError: | |
return False | |
return True | |
d1 = {'price': 1000.0, 'position': 'long', 'action': 'hold', 'warning': ''} | |
d2 = {'price': 1001.0, 'position': '', 'action': 'hold', 'warning': ''} | |
d3 = {'price': 1002.0, 'position': 'long', 'action': 'hold', 'warning': ''} | |
print(compare_dict(d1, d2, ['price'])) | |
print(compare_dict(d2, d3, ['price'])) | |
print(compare_dict(d1, d3, ['price'])) | |
############################################## | |
# Convert a dictionary containing unicode string, non string into a json loadable object | |
def convert_dict_to_json(data, convert_unicode_to_string=True): | |
if isinstance(data, basestring): | |
return str(data) | |
elif isinstance(data, unicode): | |
if convert_unicode_to_string: | |
return data.encode('utf-8') | |
else: | |
return data | |
elif isinstance(data, collections.Mapping): | |
return dict(map(convert_dict_to_json, data.iteritems())) | |
elif isinstance(data, collections.Iterable): | |
return type(data)(map(convert_dict_to_json, data)) | |
else: | |
return str(data) | |
############################################## | |
# Check if a key is in a dict | |
if myKey in dict: | |
do_some_work | |
else: | |
pass | |
# Option 2 - try exception | |
try: | |
do_some_work(dict[myKey]) | |
except KeyError: | |
pass | |
############################################## | |
# String format | |
>>> "{:0>2}".format("1") # Works for both numbers and strings. | |
'01' | |
>>> "{:02}".format(1) # Only works for numbers. | |
'01' | |
############################################## | |
# Connect to HSQLDB | |
import jaydebeapi | |
conn = jaydebeapi.connect("org.hsqldb.jdbcDriver", | |
"jdbc:hsqldb:hsql://localhost:9000/db", | |
["SA", ""], | |
"/home/user_id/hsqldb-2.4.0/hsqldb/lib/hsqldb.jar",) | |
curs = conn.cursor() | |
query = """ | |
SELECT * | |
FROM test | |
WHERE id = ? | |
""" | |
curs.execute(query, (some_id)) | |
print curs.fetchall() | |
curs.close() | |
conn.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment