Skip to content

Instantly share code, notes, and snippets.

View hellpanderrr's full-sized avatar
🎯
Focusing

hellpanderrr

🎯
Focusing
View GitHub Profile
@hellpanderrr
hellpanderrr / python list split.py
Last active February 21, 2019 08:15
Python split list into list of sublists. A list of break words is used as the separator, the list is split between each break word.
def break_list_by_code_words(l, words):
'''Breaks 1-d list in groups by code words.
['Code_word_1,'abc','123','sd','Code_word_2','34253','123','Code_word_3','1231','4545'] -->
[[Code_word_1,'abc','123','sd'], ['Code_word_2','34253','123'] ,['Code_word_3','1231','4545'] ]
'''
flag = False
ret = []
block = []
for i in l:
@hellpanderrr
hellpanderrr / cling.bash
Created April 7, 2016 22:48
installing cling on ubuntu 14
# you might need to install cmake
#!/bin/bash
#
# [email protected], 2014-02-07
# which is not ideal, see http://stackoverflow.com/a/677212/1392758
python=`which python`
if type python2 > /dev/null 2>&1; then
@hellpanderrr
hellpanderrr / django server setup.md
Last active March 25, 2016 07:00
Ubuntu 14.04 Django1.9 Nginx Gunicorn Setup without virtualenv

sudo apt-get install nginx

sudo pip install gunicorn

/home/ubuntu/my_project -- path to the folder containing manage.py

/home/ubuntu/my_project/my_project -- path to the folder containing settings.py

$USER -- your username

@hellpanderrr
hellpanderrr / drop_duplicates.py
Last active February 21, 2019 08:17
python drop duplicates from an iterable while preserving the order
### http://www.peterbe.com/plog/uniqifiers-benchmark
def uniqify(seq):
seen = set()
seen_add = seen.add
return [x for x in seq if not (x in seen or seen_add(x))]
@hellpanderrr
hellpanderrr / isiterable.py
Last active April 5, 2016 20:16
python object is iterable
#'Iterable' means object has an __iter__() method.
isiterable = lambda obj: isinstance(obj, basestring) or bool(getattr(obj, '__iter__', False))
from collections import Iterable
isiterable = lambda obj: isinstance(obj, Iterable)
#https://docs.python.org/3/library/collections.abc.html#collections.abc.Iterable
@hellpanderrr
hellpanderrr / .py
Created February 11, 2016 06:36
Read Cyrillic from PyPDF2
def fix_string(string):
ret = ''
for char in string:
try:
ret += char.encode('cp1252').decode('cp1251')
except:
ret += char
return ret
@hellpanderrr
hellpanderrr / string_agg.py
Last active January 26, 2016 13:54
Django PostgreSQL string_agg
#models.py
from django.db.models import Aggregate
class Concat(Aggregate):
def add_to_query(self, query, alias, col, source, is_summary):
#we send source=CharField to prevent Django from casting string to int
aggregate = SQLConcat(col, source=models.CharField(), is_summary=is_summary, **self.extra)
query.aggregates[alias] = aggregate
def __init__(self, col, distinct=False, **extra):
@hellpanderrr
hellpanderrr / OHE.py
Last active February 21, 2019 08:15
One hot encode a dataframe in pandas and sklearn
from sklearn.feature_extraction import DictVectorizer as DV
vectorizer = DV(sparse = False)
v = vectorizer.fit_transform(df.T.to_dict().values())
new_df = pd.DataFrame(v, columns=vectorizer.feature_names_)
@hellpanderrr
hellpanderrr / sort_df.py
Created July 27, 2015 12:26
Pandas sort dataframe using custom function
def sort_df(df, column_idx, key):
'''Takes dataframe, column index and custom function for sorting,
returns dataframe sorted by this column using this function'''
col = df.ix[:,column_idx]
temp = pd.DataFrame([])
temp[0] = col
temp[1] = df.index
temp = temp.values.tolist()
df = df.ix[[i[1] for i in sorted(temp, key=key)]]
@hellpanderrr
hellpanderrr / get_filelist.py
Created July 15, 2015 13:54
python get files in a folder
def get_filelist(path, extension=None,only_folders=False):
'''Returns list of files in a given folder, without going further
Parameters
---------
extension: Collect only files with this extension
only_folders: Collect only folders names
'''
filenames = []
if not only_folders: