Skip to content

Instantly share code, notes, and snippets.

from itertools import chain, starmap
def remove_spans(s, spans):
to_drop = set(chain(*starmap(range, spans)))
return ''.join(c for (i, c) in enumerate(s) if i not in to_drop)
def remove_spans_gen(s, spans):
checker = make_range_checker(spans); next(checker)
return ''.join(c for (i, c) in enumerate(s) if not checker.send(i))
def make_range_checker(spans):
idx = yield
for (i, j) in spans:
while idx < j:
idx = (yield i <= idx < j)
while True:
def remove_spans_neg(s, spans):
return ''.join(s[i:j] for (_, i), (j, _) in zip([(None,0)]+spans, spans+[(len(s),None)]))
dept_prov_apt_types = DepartmentProviderAppointmentType.where("department_provider_id in (?)", @department_providers.map(&:id))
@apt_types = AppointmentType.where("id in (?)", dept_prov_apt_types.map(&:appointment_type_id)).distinct.order(:name)
@spitz-dan-l
spitz-dan-l / gist:c8f69e4a045f6f57f2f0
Last active August 29, 2015 14:04
Python List Monad with Coroutine Copying
# how to get python to look like this haskell?..
# listOfTuples :: [(Int,Char)]
# listOfTuples = do
# n <- [1,2]
# ch <- ['a','b']
# return (n,ch)
# -> [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]
# ..such that we can write a coroutine and apply a decorator that will make
@list_monad
@spitz-dan-l
spitz-dan-l / gist:01a8e7788ffaeec87fd7
Created September 3, 2014 17:19
Pandas panel not adding column
import numpy as np
import pandas as ps
np.random.seed(0)
index=range(3)
columns = list('abc')
panel = ps.Panel.from_dict(
{'A' : ps.DataFrame(np.random.randn(3, 3), index=index, columns=columns),
@spitz-dan-l
spitz-dan-l / gist:f494674b855eeea9f998
Created November 18, 2014 22:58
One-hot encoding demo
from sklearn.feature_extraction import DictVectorizer
import pandas
def demo1(input_csv, new_csv):
"""Demonstration of one-hot encoding through Scikit-Learn's DictVectorizer object."""
df = pandas.read_csv(input_csv) #read csv into a DataFrame object
#create the one hot encoder
one_hot_encoder = DictVectorizer(sparse=False)
@spitz-dan-l
spitz-dan-l / gist:77968e6177ad483741ad
Created December 9, 2014 21:07
Loading a mysql table into pandas
# If you are using anaconda, then you need to install an extra package which is used behind the scenes.
# Run the following command on the command line:
#
# $ conda install pymysql
#
# say yes when it prompts you.
import pandas
from sqlalchemy import create_engine
public void updateWordCount(HashMap<String, Integer> counts, String wordToUpdate){
if (counts.containsKey(wordToUpdate)) {
int current_count = counts.get(wordToUpdate);
counts.put(wordToUpdate, current_count++);
} else {
counts.put(wordToUpdate, 1);
}
}
public class SynchronizedCounter {
private int c = 0;
public synchronized void increment() {
c++;
}
public synchronized void decrement() {
c--;
}