Skip to content

Instantly share code, notes, and snippets.

Avatar
🎶

E. datageekette

🎶
  • Berlin, Germany
View GitHub Profile
@datageekette
datageekette / database.py
Created Oct 8, 2019
database setup + query runner
View database.py
import psycopg2
import psycopg2.extras
from configparser import ConfigParser
import logging
logger = logging.getLogger()
logging.basicConfig(format='%(asctime)s %(message)s')
logger.setLevel(logging.DEBUG)
View collections_tricks.py
from collections import defaultdict, Counter
from typing import Mapping, List
""" Most commom element with Counter.""
stuff: List = ['book', 'book', 'phone', 'book', 'phone']
cnt = Counter(stuff)
# first most common
# first element of the list and a tuple
cnt.most_common(1)[0][0]
# 'book'
@datageekette
datageekette / mask_train.py
Created Mar 29, 2019
Mask for filters on train feature set
View mask_train.py
filters = {
'field_1': <threshold value for the field1>,
'field_2': <threshold value for the field2>,
}
# creates a matrix of True's
mask = np.ones(features.shape[0], dtype=bool)
for field, threshold_value in filters.items():
# updates the matrix with
View precision_recall_plot.py
from sklearn.metrics import precision_score, recall_score, precision_recall_curve
import matplotlib.pyplot as plt
def plt_prc(recall, precision, label=None):
plt.step(recall, precision, label=label)
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
@datageekette
datageekette / get_url_link.py
Created Mar 29, 2019
Creates a clickable URL from two fields of the pandas DataFrame
View get_url_link.py
View recursive_file_search.py
import globt
# if you need a list, just use glob.glob instead of glob.iglob
for filename in glob.iglob('src/**/*.c', recursive=True):
pprint(filename)
# ----- OR -----
from pathlib import Path
@datageekette
datageekette / test_endpoints.py
Created Mar 12, 2019
Test example with client
View test_endpoints.py
headers = {
'Content-Type': 'application/json',
'Authorization': ('...'),
'accept': 'application/json'
}
def test_post(client):
response = client.post('/endpoint',
data=json.dumps(payload),
@datageekette
datageekette / conftest.py
Created Mar 12, 2019
Setups the client fixture
View conftest.py
import pytest
from <service> import app
# ex: app = flask.Flask(__name__, static_url_path='/static')
@pytest.fixture
def client():
app.app.config['TESTING'] = True
client = app.app.test_client()
yield client
View test_examples_pytest.py
def test_format_to_dict():
""" Tests formatting to a dictionary. """
data_to_transform = defaultdict(lambda: defaultdict(lambda: {}))
assert isinstance(format_to_dict(data_to_transform), dict)
def test_valid():
valid_data = {'valid_key': 1}
assert function_to_test(valid_data) == 1
@datageekette
datageekette / get_obj_key.py
Created Mar 12, 2019
Creates a hash for an object
View get_obj_key.py
import hashlib
def get_obj_key(obj: Type) -> str:
""" Creates and returns a unique object id."""
obj_key = '+'.join(
[
obj.attr1, str(obj.num_attr1), obj.attr2, ...
])
return hashlib.md5(obj_key.encode('utf8')).hexdigest()