Skip to content

Instantly share code, notes, and snippets.

View makmanalp's full-sized avatar

Mehmet Ali "Mali" Akmanalp makmanalp

View GitHub Profile
@makmanalp
makmanalp / network.py
Created September 12, 2018 19:39
Read / write d3 style network JSON files with pandas, preserving order and types
import pandas as pd
import json
def read_network(file_name, nodes_field="nodes", edges_field="edges"):
network = None
with open(file_name, "r") as f:
network = json.loads(f.read())
nodes = network[nodes_field]
edges = network[edges_field]
other_fields = {x:network[x] for x in network.keys()
@makmanalp
makmanalp / data_store.py
Created July 30, 2018 22:18
Simple filesystem organization wrapper
"""
Simple filesystem organization scheme. You have:
- Objects: A logical "thing", e.g. a document or a page, with unique IDs
- Keys: A type of data that we're storing about the object, like the
location of margins on a page, or the locations of each text box.
- Files: For a specific object under a specific key, you can have multiple
files, e.g. image files for each column in the page
Generally you might want to store data in a specific object's key:
@makmanalp
makmanalp / selfjoin.py
Created September 11, 2017 18:46
Readable double self join / tree traversal in SQLAlchemy
FourDigit = aliased(HSProduct)
TwoDigit = aliased(HSProduct)
Section = aliased(HSProduct)
product_data = db.session\
.query(
FourDigit.id.label("product_id"),
FourDigit.code.label("product_code"),
FourDigit.name_en.label("product_name"),
Section.id.label("section_id"),
@makmanalp
makmanalp / eb.md
Created April 6, 2017 18:52
Elastic beanstalk python gotchas

Config not being run / used

Don't forget to actually commit the config or run eb deploy --staged. Also eb deploy -v gives you a detailed view of which config files get pushed.

Where are my application logs?

/var/log/httpd/error.log (unless you put them elsewhere, this is where stdout goes)

I set WSGIPath right and it's still not working, I get 500 errors

You need to name the variable with the WSGI application application. Naming it app as is flask convention won't work.

@makmanalp
makmanalp / fillin.py
Created April 5, 2017 16:09
STATA fillin in pandas
def fillin(df, entities):
"""STATA style 'fillin', makes sure all combinations of entities in the
index are in the dataset."""
return df\
.set_index(entities)\
.reindex(pd.MultiIndex.from_product(df.index.levels, names=df.index.names))
@makmanalp
makmanalp / indent.json
Created March 15, 2017 16:17
Weird python json pretty printing that puts many tiny items on their own lines
{
"neighbors":{
"2":[
441
],
"3":[
29,
31
],
"4":[
@makmanalp
makmanalp / list.md
Last active March 9, 2017 03:45
30 women in tech with awesome twitters that you should follow
h = {}
h[5] = 10
k = h.get(10, None)
is_missing = (k == None)
del h[5]
for k,v in h.items():
if v is not None:
h[k]=v
@makmanalp
makmanalp / khash_example.c
Created August 25, 2016 04:10
KHash example
#include "khash.h"
KHASH_MAP_INIT_INT(32, char)
int main() {
int ret, is_missing;
khiter_t k;
khash_t(32) *h = kh_init(32);
k = kh_put(32, h, 5, &ret);
kh_value(h, k) = 10;
k = kh_get(32, h, 10);
is_missing = (k == kh_end(h));
@makmanalp
makmanalp / shift_columns.py
Created August 17, 2016 20:36
Shift a column down by n rows in pandas
def shift_columns(df, key_columns, data_columns, shift_years, year_column="year"):
"""Shift given data columns by n years in order to be able to later compute deltas."""
shifted = df[key_columns + data_columns].copy()
shifted[year_column] = shifted[year_column] - shift_years
new_column_names = {x: (x + "_plus" + str(shift_years))
for x in data_columns}
shifted = shifted.rename_axis(new_column_names, axis="columns")
return shifted
#eg