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
# can also include requirements | |
# create _init_.py in the package folder | |
# to install: `pip install -e .` | |
# or with the github/lab url `pip install git+ssh://[email protected]/...` | |
import os | |
import setuptools | |
here = os.path.abspath(os.path.dirname(__file__)) |
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
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) | |
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
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' |
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
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 |
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
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]) |
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
def create_link(file_name, file_id): | |
return f'http://localhost:port/{file_name}/{file_id}' | |
def make_clickable(val): | |
# target _blank to open new window | |
return '<a target="_blank" href="{}">{}</a>'.format(val, val) | |
data['link'] = data.apply(lambda x: create_link(str(x['file_path']), x['file_id']), axis=1) | |
clickable_table = data.style.format({'link': make_clickable}) |
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
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 |
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
headers = { | |
'Content-Type': 'application/json', | |
'Authorization': ('...'), | |
'accept': 'application/json' | |
} | |
def test_post(client): | |
response = client.post('/endpoint', | |
data=json.dumps(payload), |
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
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 |
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
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 |
NewerOlder