code: return x + float(y)/2**len(t)

rawDescription: Converting binary to float

description: Converting binary to float


code:
from django.db import connection
from time import time
from operator import add
import re

class StatsMiddleware(object):

    def process_view(self, request, view_func, view_args, view_kwargs):
        '''
        In your base template, put this:
        <div id="stats">
        <!-- STATS: Total: %(total_time).2fs Python: %(python_time).2fs DB: %(db_time).2fs Queries: %(db_queries)d ENDSTATS -->
        </div>
        '''

        # Uncomment the following if you want to get stats on DEBUG=True only
        #if not settings.DEBUG:
        #    return None

        # get number of db queries before we do anything
        n = len(connection.queries)

        # time the view
        start = time()
        response = view_func(request, *view_args, **view_kwargs)
        total_time = time() - start

        # compute the db time for the queries just run
        db_queries = len(connection.queries) - n
        if db_queries:
            db_time = reduce(add, [float(q['time'])
                                   for q in connection.queries[n:]])
        else:
            db_time = 0.0

        # and backout python time
        python_time = total_time - db_time

        stats = {
            'total_time': total_time,
            'python_time': python_time,
            'db_time': db_time,
            'db_queries': db_queries,
        }

        # replace the comment if found
        if response and response.content:
            s = response.content
            regexp = re.compile(r'(?P<cmt><!--\s*STATS:(?P<fmt>.*?)ENDSTATS\s*-->)')
            match = regexp.search(s)
            if match:
                s = (s[:match.start('cmt')] +
                     match.group('fmt') % stats +
                     s[match.end('cmt'):])
                response.content = s

        return response

rawDescription: Django: display time it took to load a page on every page


rawDescription: Plotting pandas groupby

description: Plotting pandas groupby


code: seq

rawDescription: How to parse ambiguous characters with BioPython

description: Parse ambiguous characters with BioPython


code:
request_with_cookies = Request(url="http://www.example.com",
                               cookies={'currency': 'USD', 'country': 'UY'})

rawDescription: How to clear cookies in scrapy?

description: Clear cookies in scrapy


code:
def list_section_reverse(list1, reverse_index):
    print("In function reverse()")

    n_list = [ element for element in list1 ]

    for item in range( reverse_index + 1 ):
        n_list[ item ] = list1[ reverse_index - item ]
        item += 1

    return n_list

mylist = ['a', 'b', 'c', 'd', 'e', 'f']
print(list_section_reverse(mylist, 3))

rawDescription: How can I reverse a section of a list using a loop in Python?

description: Reverse a section of a list using a loop in Python


code:
class UserForm(ModelForm):
    #your user form attributes and stuff

    def clean_number(self, value):
        user_number = value
        number_occurrences = User.objects.filter(number=user_number).count()
        if  number_occurrences > 0:
            raise forms.ValidationError("You number is already taken by other user")

        return self.cleaned_data

rawDescription: Django Integrity Error Handling

description: Django Integrity Error Handling


rawDescription: Why does pandas series.map method work for column concatenation?


code:
class SelObj():
    def __init__(self, DataObj, selA, selB):
        self.selA = selA
        self.selB = selB
        self.DataObj = DataObj

        for n in DataObj.arrays:
            Darr = Darray( getattr( self.DataObj, n), self )
            setattr( self, n, Darr )

    def __setattr__(self, name, value):
        setattr(self.DataObj, name, value) # updates the source of the data
        super(SelObj, self).__setattr__(name, value) # updates the representation

rawDescription: ndarray that updates its data on demand

description: Ndarray that updates its data on demand


code: print([x for x in s.split() if any(set(pr).issubset(vowels) for pr in (x[i:i+2] for i in range(len(x[:-1]))))])

rawDescription: Delete words which have 2 consecutive vowels in it


rawDescription: Python stack containing objects changing automatically


code: #python2.6 setup.py

rawDescription: In Linux, how do I compile and install code for different versions of Python?


code:
for x in sub_combinations(('A', 'B', 'C', 'D')):
    print(x)

rawDescription: subtuples for a tuple

description: Subtuples for a tuple


code:
def DisplayIt(request):
    stringparts = []
    for a in A.objects.filter(b__name__isnull=False):
        stringparts.append(a.b.name)
    for a in A.objects.filter(b__name__isnull=True):
        stringparts.append("dummy value for missing b.name")
    ''.join(stringparts)

rawDescription: Django 1.4: Get a field in a foreign key object (null=True), get None or field value


rawDescription: Pulling date and time from Excel to Pandas and combining it to a timestamp


code:
magic_number = base64.b64decode("GIhTSuBask6y60iLI2VwIg==")
offset = magic_number_offset = data.find(magic_number)
offset -= 72

rawDescription: Find previous byte that isn't equal to unwanted byte in Python


code: data = np.array(zip(xy[:, 0], xy[:, 1], z))

rawDescription: Using scipy's kmeans2 function in python


code:
foo = lambda: 0
foo.bar = 'bar'
foo.baz = 'baz'

rawDescription: coding style: lightweight/simplest way to create instances supporting attribute assignment?

description: Coding style: lightweight/simplest way to create instances supporting attribute assignment


code:
def empty_board(n):
    return tuple(
        (i, j, 0) for i in range(n) for j in range(n))

rawDescription: How to create a tuple of tuples and pass it to the superclass's constructor

description: Create a tuple of tuples and pass it to the superclass's constructor


code:
import dis
dis.dis(compile("s = []; s.append('spam')", '', 'exec'))
dis.dis(compile("s = []; s += ['spam']", '', 'exec'))

rawDescription: In Python, what is the difference between ".append()" and "+= []"?


code:
latbounds = [ 40 , 43 ]
lonbounds = [ -96 , -89 ] # degrees east ?
lats = f.variables['latitude'][:]
lons = f.variables['longitude'][:]

# latitude lower and upper index
latli = np.argmin( np.abs( lats - latbounds[0] ) )
latui = np.argmin( np.abs( lats - latbounds[1] ) )

# longitude lower and upper index
lonli = np.argmin( np.abs( lons - lonbounds[0] ) )
lonui = np.argmin( np.abs( lons - lonbounds[1] ) )

rawDescription: netcdf4 extract for subset of lat lon

description: Netcdf4 extract for subset of lat lon


code:
from csv import  reader
from collections import defaultdict
with open("in.txt") as f:
    d = defaultdict(list)
    for k, v in reader(f,delimiter=" "):
        d[k].append(v)

print(d.values())

rawDescription: splitting a list into two lists based on a unique value python

description: Splitting a list into two lists based on a unique value python


code:
def part_1(my_training_data):
   new_training_data=[]
   for key in sorted(my_training_data):
       new_training_data.append(list(key) + [1])
   return new_training_data

rawDescription: My Python for loop is giving the result None


code:
def funct():
    """
     This function doesn't expect arguments
     and returns zero.
    """
    return 0

rawDescription: How do I show my argspec/docstring in bpython?

description: Show my argspec/docstring in bpython


code:
img = np.asarray(Image.open('your_image').convert('L'))
plt.hist(img.flatten(), facecolor='green', alpha=0.75)

rawDescription: Change visual of histogram from image using matplotlib in Python


code:
class TestC():
    def __init__(self, attr=None):
        if attr is None:
            attr = []
        self.attr = attr

a = TestC()
b = TestC()
a.attr.append(1)
a.attr
b.attr

rawDescription: How should I declare default values for instance variables in Python?


code:
letter_positions = {}
for (y, row) in enumerate(board):
    for (x, letter) in enumerate(row):
         letter_positions.setdefault(letter, []).append((x, y))

rawDescription: Python: Find a word in a matrix of characters


code: input("Would you like to find the factorial of another non-negative integer? Y/N: ")

rawDescription: Recursive Function inside While loop (Python)


code:
class configContainer():
    def __init__(self):
        self.instances = {}
...

rawDescription: Python nested objects unable to retrieve value in function


rawDescription: List of dictionaries - how to format print output


code:
HELLO
FOUR

rawDescription: Iteration over python dictionary sorted by an element


code: x[:,None]*y   # y[None,:] is automatic.

rawDescription: Fast outer tensor product in numpy

description: Fast outer tensor product in numpy


code:
for d in data.values():
    myspecialtool(*d)

rawDescription: Extracting variable names and data from csv file using Python


code:
while state:
    time.sleep(.1)
    if i % 10 == 1:
        self.browser.append(str(i))
        QApplication.processEvents()
    i += 1
else:
    self.browser.append('Stop loop')
    QApplication.processEvents()
    time.sleep(3)
    sys.exit()

rawDescription: PyQt4 Gui that prints loop

description: PyQt4 Gui that prints loop


code: str.join = lambda x: None

rawDescription: How to patch method ''.join using the mock library

description: Patch method ''.join using the mock library


code:
method_to_use = order[i]
method_to_use(my_pop)

rawDescription: Using a dictionary to control the flow of methods


code:
xeqy = Piecewise((1,Eq(x,y)),(0,True)) # {1 for x = y, 0 otherwise}
f = z * xeqy                           # {z for x = y, 0 otherwise}
simplify(f)

rawDescription: Simplify removes relational term in Sympy?


code:
r = requests.get("http://www.december.com/html/demo/hello.html", prefetch=False)
mime = magic.from_buffer(r.iter_content(256).next(), mime=True)

if mime == "text/html":
     print(r.requests.get("http://www.december.com/html/demo/hello.html").text)

rawDescription: python-requests: fetching the head of the response content without consuming it all

description: Python-requests: fetching the head of the response content without consuming it all


code: item.insert(1, item[-1])

rawDescription: Insert xml node in specific location

description: Insert xml node in specific location


code:
for item in my_list:
    if item['id'] == my_unique_id:
        my_item = item
        break
else:
    my_item = None

rawDescription: Python: get a dict from a list based on something inside the dict


code:
def my_sum(a, b):
    return a + b

rawDescription: Keep getting syntax error. What do I need to do?


code: today = pd.to_datetime('2015-01-30')

rawDescription: return pandas DF column with the number of days elapsed between index and today's date

description: Return pandas DF column with the number of days elapsed between index and today's date


code:
def my_own_range(n):
    i = 1
    while(i<=n):
        yield(i)
        i*=2

for i in my_own_range(64):
    print(i)

rawDescription: Modifying increment value in for loop

description: Modifying increment value in for loop


code:
d = dict.fromkeys(l, [])
d["a"].append("foo")
d

rawDescription: How to make a dictionary from list

description: Make a dictionary from list


code:
# Plot inputs
plt.figure()
plt.imshow(a, cmap="spectral", interpolation='nearest')
plt.figure()
plt.imshow(b, cmap="spectral", interpolation='nearest')

# Plot output
plt.figure()
plt.imshow(out, cmap="spectral", interpolation='nearest')

rawDescription: Label regions with unique combinations of values in two numpy arrays?


code:
class MyString(str):
    def __iter__(self):
        return (type(self)(i) for i in super().__iter__())

rawDescription: Inheriting from "str" class

description: Inheriting from "str" class


code:
theta1 = [el.copy() for el in theta1]  # or list(el) for el in ...
theta2 = [el.copy() for el in theta2]  # or el[:] for el in ...

rawDescription: Why doesn't python overwite non-zero arrays?


code:
distance = 50; "{:d}m".format(100+(distance//100))
distance = 360; "{:d}m".format(100+(distance//100))
distance = 3605; "{:d}m".format(100+(distance//100))

rawDescription: Calculating the distance and print it out


code: [[y.strip() for y in x.xpath('.//text()') if y.strip()] for x in tree.xpath('//p')]

rawDescription: How to prevent infinite looping in Python while parsing with lxml?

description: Prevent infinite looping in Python while parsing with lxml


rawDescription: Python3 how is mro defined in object?

description: Python3 how is mro defined in object


rawDescription: Catch multiple string occurrences in multiline text


code:
passes = {}

def new_pass(player1, player2):
    # if p1 has no passes, create a new entry in the dict, else append to existing
    if player1 not in passes:
        passes[player1] = [player2]
    else:
        passes[player1].append(player2)

def total_passes(player1):
    # if p1 has any passes, return the total number; otherewise return 0
    total = len(passes[player1]) if player1 in passes else 0
    return total

def total_passes_from_p1_to_p2(player1, player2):
    # if p1 has any passes, count number of passes to player 2; otherwise return 0
    total = passes[player1].count(player2) if player1 in passes else 0
    return total

rawDescription: Python Data Structure Selection

description: Python Data Structure Selection


rawDescription: Dynamically return a class implementation in Python


code:
{'Filan Fisteku': ['Said something about this , blla blla blla then',
                   'the Filan Fisteku speech goes on on the next line,  plus some other text.'],
 'Fisteku Filan': ['This is another text from another guy which i am trying to put in a json.']}

rawDescription: Read a file line by line,and get the line that starts with some word?


code: a = [map(float, suba) for suba in a]  # Only works on Py2

rawDescription: "Map" a nested list in Python

description: "Map" a nested list in Python


code:
qry = (session
        .query(Page).filter(Page.slug=='currentlevel')
        .join(Page.parent, aliased=True).filter(Page.slug=="secondlevel")
        .join(Page.parent, aliased=True, from_joinpoint=True).filter(Page.slug=="firstlevel")
        )

rawDescription: Foreign key querines in SQLAlchemy

description: Foreign key querines in SQLAlchemy


code:
result = np.zeros_like(b)
x_offset = 1  # 0 would be what you wanted
y_offset = 1  # 0 in your case
result[x_offset:a.shape[0]+x_offset,y_offset:a.shape[1]+y_offset] = a
result

array([[ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.,  1.,  1.]])

rawDescription: python how to pad numpy array with zeros

description: Python how to pad numpy array with zeros


rawDescription: find binary permutations in python

description: Find binary permutations in python


rawDescription: Generate a multi-indexed dataframe from a dictionary with different length of values


code:
import os
import numpy as np
arr = np.memmap('mm', dtype='float32', mode='w+', shape=(1,10000000))
arr[:] = np.zeros((1,10000000))
os.system("md5sum mm")
arr += 1.1
os.system("md5sum mm")
arr = np.argsort(arr)
os.system("md5sum mm")
arr += 1.3
os.system("md5sum mm")
type(arr)

rawDescription: Correct way to do operations on Memmapped arrays


code:
def f(day_count, year_fraction):
    day_count(a, b)
    # ...
    year_fraction(x, y)

rawDescription: Static method-only class and subclasses in Python - is there a better design pattern?


code:
def divider(lst,n):
    lst = [float(x) for x in reversed(lst)]
    yield lst
    for i in range(2,n+1):
        lst = [x/i for x in lst]
        yield lst

rawDescription: loop through a list and use previous elements

description: Loop through a list and use previous elements


rawDescription: Unable to upload a json file in same syntex to amazon SQS using Python BOTO


code:
def cantor(n):
    return [0.] + cant(0., 1., n) + [1.]

def cant(x, y, n):
    if n == 0:
        return []

    new_pts = [2.*x/3. + y/3., x/3. + 2.*y/3.]
    return cant(x, new_pts[0], n-1) + new_pts + cant(new_pts[1], y, n-1)

for i in range(4):
    print(i, cantor(i))

rawDescription: Cantor ternary set in Python or C

description: Cantor ternary set in Python or C


code:
re.sub(">\s*(/?)(\!--|\!DOCTYPE|\a|abbr|acronym|address|applet|area|\
                 b|base|basefont|bdo|big|blockquote|body|br|button|\
                 caption|center|cite|code|col|colgroup|\
                 dd|del|dfn|dir|div|dl|dt|\
                 em|\
                 fieldset|font|form|frame|frameset|\
                 head|h1|h2|h3|h4|h5|h6|hr|html|\
                 i|iframe|img|input|ins|\
                 kbd|\
                 label|legend|li|link|\
                 map|menu|meta|\
                 noframes|noscript|\
                 object|ol|optgroup|option|\
                 p|param|pre|\
                 q|\
                 s|samp|script|select|small|span|strike|strong|style|sub|sup|\
                 table|tbody|td|textarea|tfoot|th|thead|title|tr|tt|\
                 u|ul|\
                 var)>", "><\g<1>\g<2>>", s)

rawDescription: Beautiful Soup - how to fix broken tags

description: Beautiful Soup - how to fix broken tags


code:
import itertools as IT
readers = [(1,2,3), ('a','b','c','d'), (10,20,30,40)]
list(IT.izip_longest(readers[0], readers[1], readers[2]))

rawDescription: Combine columns from several CSV files into a single file


code:
target_leafpos = np.treeposition() + np.leaf_treeposition(0) # Step 1
all_leaf_treepositions = t.treepositions("leaves")           # Step 2
x = all_leaf_treepositions.index(target_leafpos)
print(x)

rawDescription: How many leaves are there before a subtree?


code:
df
df.iloc[1:] + df.iloc[0]

rawDescription: How do I add one row of a PANDAS dataframe to the rest of the rows?

description: Add one row of a PANDAS dataframe to the rest of the rows


code:
from tkinter import *

root = Tk()
entry = Entry(root, state='disabled')
entry.pack()
root.mainloop()

rawDescription: In Tkinter, How I disable Entry?

description: In Tkinter, How I disable Entry


code:
char_dict = {'A': 1, 'B': 2, 'C': 3}

def values(char):
    return char_dict[char]

rawDescription: How does one talk about comparing positions of characters in a string?

description: Talk about comparing positions of characters in a string


code: headers = {'Content-type': 'application/json'}

rawDescription: Python http.client json request and response. How?


code: client = Client(url, plugins=[plugin])

rawDescription: Is it possible to replace schema locations in Suds/python?


code:
from bisect import bisect_left
def contains(a, x):
    """returns true if sorted sequence `a` contains `x`"""
    i = bisect_left(a, x)
    return i != len(a) and a[i] == x

rawDescription: Efficiency of Python "in" keyword for sorted list


code: first.rsplit('.', 1)

rawDescription: Python Beginner split String in two strings


code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.mlab import griddata
import csv

data = np.genfromtxt('example.csv', dtype=[('x',float),('y',float),('z',float)],
                     comments='"', delimiter=',')

sample_pts = 500
con_levels = 20

x = data['x']
xmin = x.min()
xmax = x.max()

y = data['y']
ymin = y.min()
ymax = y.max()

z = data['z']

xi = np.linspace(xmin,xmax,sample_pts)
yi = np.linspace(ymin,ymax,sample_pts)

zi = griddata(x,y,z,xi,yi)

plt.contour(xi,yi,zi,con_levels,linewidths=1)
plt.scatter(x,y,c=z,s=20)
plt.xlim(xmin,xmax)
plt.ylim(ymin,ymax)
plt.show()

rawDescription: Matplotlib contour plot with intersecting contour lines


code:
d1 = {'a': 1, 'b':2}
d2 = {'b': 2, 'c':3}
diff = set(d1).symmetric_difference(d2)
diff

rawDescription: script to find the difference between two dictionaries in python

description: Script to find the difference between two dictionaries in python


code:
def sql_decorator(func):
    def decorated(table, user, limit):
        ... # do stuff with table
        return func(table, user, limit)
    return decorated

@sql_decorator
def user_records(table, user, limit):
    ...

rawDescription: Python Decorator with extraneous arguments


code:
from kivy.uix.carousel import Carousel
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
from kivy.lang import Builder

Builder.load_string('''
<Page>:
    Label:
        text: str(id(root))

<Carousel>
    on_index: print("slide #{}".format(args[1]))
''')

class Page(BoxLayout):
    pass

class TestApp(App):
    def build(self):
        root = Carousel()
        for x in range(10):
            root.add_widget(Page())
        return root

if __name__ == '__main__':
    TestApp().run()

rawDescription: Kivy Event on carousel when slide change


code:
import wx
class MyForm(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Toggle")
        self.panel = wx.Panel(self, wx.ID_ANY)
        self.dummy = wx.StaticText(self.panel, -1, "")
        self.cb1 = wx.CheckBox(self.panel, -1,"Choice 1", pos=(10,10))
        self.cb2 = wx.CheckBox(self.panel, -1,"Choice 2", pos=(10,40))
        self.cb1.Bind(wx.EVT_CHECKBOX, self.onToggle1)
        self.cb2.Bind(wx.EVT_CHECKBOX, self.onToggle2)
        self.cb1.SetToolTipString(wx.EmptyString)
        self.cb2.SetToolTipString(wx.EmptyString)
    #----------------------------------------------------------------------
    def onToggle1(self, event):
        if self.cb1.GetValue() == True:
            self.cb1.SetToolTipString("Check Box 1 is Checked")
        else:
            self.cb1.SetToolTip(None)

    def onToggle2(self, event):
        if self.cb2.GetValue() == True:
            self.cb2.SetToolTipString("Check Box 2 Is Checked")
        else:
            self.cb2.SetToolTip(None)

# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

rawDescription: How to remove a tooltip in wxpython?

description: Remove a tooltip in wxpython


code: "Newton's method".replace(r"'", r"\'")

rawDescription: Replace single quote with backslash single quote in python


code:
lhs = nltk.grammar.Nonterminal('singapore')
rhs = [u'singapore']
singapore_production = nltk.grammar.Production(lhs, rhs)
new_grammar._productions.append(singapore_production)

rawDescription: NLTK: can I add terminal to grammar that is already generated


rawDescription: How do I find the next 7am in a timezone

description: Find the next 7am in a timezone


code:
def normalize_whitespace(str):
    import re
    str = str.strip()
    str = re.sub(r'\s+', ' ', str)
    return str

rawDescription: normalize-space just works with xpath not css selector

description: Normalize-space just works with xpath not css selector


code: a += np.array(a.T)

rawDescription: Numpy: Why doesn't 'a += a.T' work?

description: Numpy: Why doesn't 'a += a.T' work


rawDescription: ffill not retaining all columns

description: Ffill not retaining all columns


code:
import urlparse
import cgi
s = 'http://domain.com/xxxxx/xxxxxxxx?tags=%7C105651%7C102496&Asidebar=1&dnr=y'
cgi.parse_qs(urlparse.urlparse(s).query)
cgi.parse_qs(urlparse.urlparse(s).query)['tags'][0]

rawDescription: get substring from the main string Python

description: Get substring from the main string Python


code: mydict = {item[0] : item for item in reader}

rawDescription: Python csvreader separate lines

description: Python csvreader separate lines


rawDescription: How to add dictionaries to tuple

description: Add dictionaries to tuple


code: l = ttk.Checkbutton(root, text ="",variable=checkData[i],command=lambda index=i: onCheck(conditionID[index],checkData[index]), onvalue=True, offvalue=False)

rawDescription: Issue with generating ttk checkboxes in loops and passing arguments


rawDescription: product of arrays of different sizes in numpy

description: Product of arrays of different sizes in numpy


code:
it = d.iteritems()
min_key, min_value = next(it)
num_mins = 1
for k, v in it:
    if v < min_value:
        num_mins = 1
        min_key, min_value = k, v
    elif v == min_value:
        num_mins += 1
        if random.randrange(num_mins) == 0:
            min_key = k

rawDescription: Python: get key with the least value from a dictionary BUT multiple minimum values


code: utc_dt = datetime.utcnow() # PC

rawDescription: Python - how do calculate the time difference between local and server


rawDescription: Create new numpy array-scalar of flexible dtype


code:
class PageListView(ListView):
    model = FbPage
    paginate_by = 50

    all_pages = False

    def dispatch(self, *args, **kwargs):
        self.request.session['all_pages'] = self.all_pages
        return super(PageListView, self).dispatch(*args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(PageListView, self).get_context_data(**kwargs)

        context['list_url'] = 'all_show_page_list' if self.all_pages else 'show_page_list'

        return context

rawDescription: Django Pagination based on Boolean value from view


code:
def find_prefix(string_list):
    prefix = []
    for chars in zip(*string_list):
        if len(set(chars)) == 1:
            prefix.append(chars[0])
        else:
            break
    return ''.join(prefix)

rawDescription: Find common sub string from the list of strings


code:
print("hey...", end=' ')
time.sleep(0.5)

rawDescription: Printing at timed intervals

description: Printing at timed intervals


code:
from database import DataBase
DataBase.execute_some_query(query)

rawDescription: Method that gets called on module deletion in Python


code: {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'}

rawDescription: How can I create dictionary in python3?

description: Create dictionary in python3


rawDescription: Flattening a list with a nested dictionary


rawDescription: Split the python list of list

description: Split the python list of list


code:
def secs():
    soup = BeautifulSoup(html, "lxml")
    sections = soup.find_all("span", {"class": "someclass"})
    for sec in sections:
        fr = sec.find("a", {"class": "frame"})
        url, img = fr["href"], fr.find("img")["src"]
        name, size =  sec.select("h3.title a")[0].text, sec.select("span.details")[0].text.split(None,1)[-1]
        yield url, name, img,size

rawDescription: xbmc/kodi python scrap data using BeautifulSoup

description: Xbmc/kodi python scrap data using BeautifulSoup


rawDescription: Python find n-sized window around phrase within string


code:
import numpy as np
np.fromstring(a, dtype=float, sep=' ')

rawDescription: Python - Parse a String into Float Array?


code:
def run_tests():
    import os
    import importlib
    import re
    dTests = {}
    lFolders = [s for s in os.listdir('all_tests') if re.match('.*_tests$',s)]
    for sFolder in lFolders:
        sFolderPath = os.path.join('all_tests',sFolder)
        lTestFileNames = [s for s in os.listdir(sFolderPath) if re.match('^test.*py$',s)]
        for sFileName in lTestFileNames:
            sSubPath = '{0}.{1}'.format(sFolder,sFileName.split('.')[0])
            dTests[sSubPath] = importlib.import_module('all_tests.{0}'.format(sSubPath))
    #now you have all the tests...
    for k in dTests:
        stuff = initialisation_stuff()
        test_result = dTests[k].run(stuff)
        do_whatever_you_want(test_result)

if __name__ == "__main__":
    run_tests()

rawDescription: boilerplateless tests

description: Boilerplateless tests


code:
data = open('breaks.log', 'rb').read()
data[:2]
udata = data.decode('utf16')
all(d < u'\x80' for d in udata)

rawDescription: Python kludge to read UCS-2 (UTF-16?) as ASCII


code: (returned, referenced) = GetValue(15.1)

rawDescription: Calling a .Net function in Python which has a reference parameter


code:
class AutoServer(object):
    def __init__(self):
        self.server = Server()
        self.server.start_listening()

rawDescription: Call method just after object creation

description: Call method just after object creation


code: SSLOptions +StdEnvVars

rawDescription: Mutual authentication with python as a CGI web app?


rawDescription: Format APNS-style JSON message in Python for use with Amazon SNS


code:
mask2 = masked_c.mask.any(axis=0)
all_masked_c = np.ma.array(c, mask=np.vstack((mask2, mask2)))
all_masked_c
np.ma.cov(all_masked_c) # same function call as the first approach, but with a different mask!

rawDescription: Covariance Matrix Python - Omit -9999 Value


code:
def parse(x):
    res = []
    match = re.finditer("\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\n(.+)\n", x)
    for each in match:
        res.append(each.group(6))
    return res

rawDescription: Parsing message parameters received by a GSM modem in python


code:
import nltk
text = "Hello, this is my sentence. It is a very basic sentence with not much information in it"
tokenized_text = nltk.word_tokenize(text)
stopwords = nltk.corpus.stopwords.words('english')
word_freq = nltk.FreqDist(tokenized_text)
dict_filter = lambda word_freq, stopwords: dict( (word,word_freq[word]) for word in word_freq if word not in stopwords )
filtered_word_freq = dict_filter(word_freq, stopwords)
len(word_freq)
len(filtered_word_freq)
word_freq
filtered_word_freq

rawDescription: Dropping specific words out of an NLTK distribution beyond stopwords


code:
c1 = collections.Counter("hello world")
c2 = collections.Counter("hey worlds")
c1 - c2
(c1 - c2).elements()

rawDescription: Remove common letters in strings

description: Remove common letters in strings


code:
import re

f = open( "code.py", "r" )
newsrc = re.sub( "print \"(.*?)\"", "scribble( \"\\1\" )", f.read() )
f.close()

f = open( "newcode.py", "w" )
f.write( newsrc )
f.close()

rawDescription: python replace print with function

description: Python replace print with function


rawDescription: What's the unit of RSS in psutil.Process.get_memory_info?


code:
d = {"chuck" : 1 , "annie" : 42, "jan" : 100};
s = '\n'.join(['%s %s' % (key, value) for (key, value) in sorted(d.items(), key=lambda x: x[1])])
print(s) # 'chuck 1\nannie 42\njan 100'

rawDescription: Generating string from a dictionary

description: Generating string from a dictionary


rawDescription: How can I use pyparsing to parse nested expressions that have mutiple opener/closer types?

description: Use pyparsing to parse nested expressions that have mutiple opener/closer types


rawDescription: Having user input variables to be used in a later equation


rawDescription: scikit-learn how to know documents in the cluster?

description: Scikit-learn how to know documents in the cluster


code:
#!/usr/bin/env python

import boto
import boto.emr
from boto.emr.instance_group import InstanceGroup

conn = boto.emr.connect_to_region('us-east-1')

rawDescription: How to launch and configure an EMR cluster using boto

description: Launch and configure an EMR cluster using boto


rawDescription: multiplying numpy arrays of different size

description: Multiplying numpy arrays of different size


rawDescription: python matplotlib save plot

description: Python matplotlib save plot


code:
from operator import itemgetter
list_of_tuples.sort(key = itemgetter(1, 3, 5))

rawDescription: sort list of tuples with multiple criteria

description: Sort list of tuples with multiple criteria


rawDescription: Python - 'tuple' object does not support item assignment


code:
class Match(models.Model):
    player = models.ForeignKey('player.Player')

rawDescription: Model Definitions: Ordering Importo Avoid ImportError


code: # Inputs

rawDescription: sort multible numpy arrays into one like: [1,2,..n, 1,2..n]

description: Sort multible numpy arrays into one like: [1,2,..n, 1,2..n]


code:
kwargs = {'name': name}

if self.obj_one is not None:
    kwargs['obj_one'] = self.obj_one
if self.obj_two is not None:
    kwargs['obj_two'] = self.obj_two

return MyObj(**kwargs)

rawDescription: Create object with variable attributes in a cleaner way in python


rawDescription: Using Xpath in Scrapy to select any text below paragraph


code:
name = input("What's your name? ")
print("Hi " + name)

rawDescription: Is this a proper way to test stdout with Python 3 unittest?


code:
P = Logic_Value('P')
-P in listA  # True if not P is in listA

rawDescription: Python: Check if list item has a complement


code:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

...
wait = WebDriverWait(driver, 10)
table = wait.until(EC.presence_of_element_located(By.CSS_SELECTOR, 'div.datatable'))

rawDescription: Waiting for a table to load completely using selenium with python


code:
x = {"a": "stuff"}
print(build_instructions(x, "f[0].a"))

rawDescription: Python recursive setattr()-like function for working with nested dictionaries


code:
def get_even_lists(lst):
    """ (list of list of int) -> list of list of int

    Return a list of the lists in lst that contain only even integers.

    >>> only_evens([[1, 2, 4], [4, 0, 6], [22, 4, 3], [2]])
    [[4, 0, 6], [2]]
    """
    even_lists = []
    for sublist in lst:
        for i in sublist:
            if i % 2 == 1:
                break
        else:
            even_lists.append(sublist)
    return even_lists

rawDescription: How to get only even numbers from list

description: Get only even numbers from list


code: (t.text + ''.join(map(etree.tostring, t))).strip()

rawDescription: How do I get the whole content between two xml tags in Python?

description: Get the whole content between two xml tags in Python


code: argcount = f.func_code.co_argcount

rawDescription: Find what is in a PYC file

description: Find what is in a PYC file


code: map(lambda row: (float(row[0]), int(row[1]), parse(row[2])), res)

rawDescription: what is this saying in python

description: What is this saying in python


code:
from statistics import  mean

l = [1,2,3,4,5,6,7,8,9,10]
print([x for x in l if x > mean(l)])
[6, 7, 8, 9, 10]

rawDescription: How to display numbers greater than the mean in a list?

description: Display numbers greater than the mean in a list


code:
# Upload files
@app.route('/uploads/', methods=['POST'])
def results():

    files = request.files

    # assuming only one file is passed in the request
    key = files.keys()[0]
    value = files[key] # this is a Werkzeug FileStorage object
    filename = value.filename

    if 'Content-Range' in request.headers:
        # extract starting byte from Content-Range header string
        range_str = request.headers['Content-Range']
        start_bytes = int(range_str.split(' ')[1].split('-')[0])

        # append chunk to the file on disk, or create new
        with open(filename, 'a') as f:
            f.seek(start_bytes)
            f.write(value.stream.read())

    else:
        # this is not a chunked request, so just save the whole file
        value.save(filename)

    # send response with appropriate mime type header
    return jsonify({"name": value.filename,
                    "size": os.path.getsize(filename),
                    "url": 'uploads/' + value.filename,
                    "thumbnail_url": None,
                    "delete_url": None,
                    "delete_type": None,})

rawDescription: Streaming file upload using bottle (or flask or similar)


code:
import io
import pandas as pd

def load_dataframe(filename):
    # First you read the data as a regular csv file and extract the _raw column values
    values = pd.read_csv(filename)['_raw'].values
    # Cleanup the values: remove newline character
    values = map(lambda x: x.replace('\n', ' '), values)
    # Add them to a stream
    s = io.StringIO(u'\n'.join(values))
    # And now everithing is the same just read it from the stream
    df = pd.read_table(s, sep='\s+', header=None, parse_dates=[[0,1]])[['0_1',4, 6]]
    df[4] = df[4].str.split(':').str.get(0)
    df[6] = df[6].str.split(':').str.get(0)
    return df

def get_diffs(df, source, destination):
    timestamps = df[(df[4] == source) & (df[6] == destination)]['0_1']
    return (timestamps.shift() - timestamps).dropna()

def main():
    filename = raw_input('Enter filename: ')
    df = load_dataframe(filename)
    while True:
       source = raw_input('Enter source IP: ').strip()
       destination = raw_input('Enter destination IP: ').strip()
       diffs = get_diffs(df, source, destination)
       for i, row in enumerate(diffs):
           print('row %d - row %d = %s' % (i+2, i+1, row.astype('timedelta64[ms]')))
       print('Mean: %s' % diffs.mean())
       yn = raw_input('Again? [y/n]: ').lower().strip()
       if yn != 'y':
            return

if __name__ == '__main__':
    main()

rawDescription: Calcuate Duration and Average of CSV file data using python


code: b = map(lambda x: x**2, start_list)  # wrap with list() in Python3

rawDescription: Square a list (or array?) of numbers in Python


rawDescription: Python: Export a matrix in csv

description: Python: Export a matrix in csv


rawDescription: How to save tokenization result for each file in a new separate text file?

description: Save tokenization result for each file in a new separate text file


code:
def decode(text, shift):
    d = {}
    for c in (65, 97):
        for i in range(26):
            d[chr(i+c)] = chr((i+shift) % 26 + c)
    return "".join([d.get(c, c) for c in text])

rawDescription: Efficient Translation Function without using maketrans()


code:
def upside_down_asterix_triangle(i, t=0):
    if i == 0:
        return 0
    else:
        print(' ' * ( t + 1 ) + '*' * ( i * 2 - 1 ))
        return upside_down_asterix_triangle( i - 1, t + 1 )

upside_down_asterix_triangle(5)

rawDescription: Asterisk triangle in python

description: Asterisk triangle in python


rawDescription: python dataframe convert week number to month

description: Python dataframe convert week number to month


code:
import datetime
date = 'Wed Jun  4 19:01:58 CDT 2014'
datetime.datetime.strptime(date.replace("CDT",""),  '%a %b %d %H:%M:%S %Y')

rawDescription: How to ssh with paramiko when prompted for password?

description: Ssh with paramiko when prompted for password


rawDescription: Python script freezes for no apparent reason (afaik)


code:
def new_alchemy_encoder(revisit_self = False, fields_to_expand = []):
    _visited_objs = []
    class AlchemyEncoder(json.JSONEncoder):
        def default(self, obj):
            if isinstance(obj.__class__, DeclarativeMeta):
                # don't re-visit self
                if revisit_self:
                    if obj in _visited_objs:
                        return None
                    _visited_objs.append(obj)

                # go through each field in this SQLalchemy class
                fields = {}
                for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
                    val = obj.__getattribute__(field)

                    # is this field another SQLalchemy object, or a list of SQLalchemy objects?
                    if isinstance(val.__class__, DeclarativeMeta) or (isinstance(val, list) and len(val) > 0 and isinstance(val[0].__class__, DeclarativeMeta)):
                        # unless we're expanding this field, stop here
                        if field not in fields_to_expand:
                            # not expanding this field: set it to None and continue
                            fields[field] = None
                            continue

                    fields[field] = val
                # a json-encodable dict
                return fields

            return json.JSONEncoder.default(self, obj)
    return AlchemyEncoder

rawDescription: How to serialize SqlAlchemy result to JSON?

description: Serialize SqlAlchemy result to JSON


code:
myFunction(data, min, avg, max)
# returns (min(data), avg(data), max(data)), and you can get them by
minValue, averageValue, maxValue = myFunction(data, min, avg, max)

rawDescription: Refactor a couple of functions into one by accepting sets and returning sets


code:
from Tkinter import *
import random

class Ask:
    def __init__(self, parent,question,right,wrong):
        self.answer=right
        self.top=top=Toplevel(parent)
        Label(self.top, text=question,width=25).grid()
        a1=random.choice([right,wrong])
        self.option_1 = Button(self.top, text=a1, width=25,command = lambda: self.check(1))
        self.option_1.grid()
        a2=right
        if a1==right:
            a2=wrong
        self.option_2 = Button(self.top, text=a2, width=25, command = lambda: self.check(2))
        self.option_2.grid()

    def check(self,pressed):
        if pressed==1:
            ans=self.option_1['text']
        else:
            ans=self.option_2['text']
        if ans==self.answer:
            self.correct()
        else:
            self.incorrect()

    def correct(self):
        self.option_1.config(state=DISABLED)
        self.option_2.config(state=DISABLED)
        Label(self.top, text='Correct').grid()
        Button(self.top, text='Next Question', command = self.top.destroy).grid()

    def incorrect(self):
        self.option_1.config(state=DISABLED)
        self.option_2.config(state=DISABLED)
        Label(self.top, text='Incorrect').grid()
        Button(self.top, text='Next Question', command = self.top.destroy).grid()

class QUIZ:
    def __init__(self, questionsdict):
        self.root=Tk()

        self.root.withdraw()
        self.questions(questionsdict)
        self.root.deiconify()
        c=Button(self.root, text='Click me to exit', command=self.root.destroy, width=30).grid(row=2, sticky='ew')
        Label(self.root, text='Quiz is Complete', width=30, background='red').grid(row=0, sticky='ew')
        mainloop()

    def questions(self,questionsdict):
        for k in questionsdict.keys():
            right=questionsdict[k][0]
            wrong=questionsdict[k][1]
            b=Ask(self.root, k, right,wrong )
            self.root.wait_window(b.top)

rawDescription: Python Tkinter: How do you create a toplevel window and destroy the previous window?


code:
from inspect import getargspec
getargspec(foo)

rawDescription: Retrieve the names of (and default values of) the arguments accepted by a specified function


code:
for line in file:
    lst.append([ float(x) for x in line.split()])

rawDescription: How to convert column data from text file into list in Python?

description: Convert column data from text file into list in Python


code:
'''//idx:namespace[
       starts-with(@idx:resourceLocation,
       "http://resource.belframework.org/belframework/1.0/namespace")]
           /@idx:resourceLocation'''

rawDescription: XPath - Return ALL nodes with certain string pattern


code:
class Contact:
    def __init__(self, name, phone, mail):
        self.name = name
        self.phone = phone
        self.mail = mail
    def __repr__(self):
        return "Contact(%r, %r, %r)" % (self.name, self.phone, self.mail)

rawDescription: How to use Dictionary in While Loop?

description: Use Dictionary in While Loop


code: [Павел,ходит,домой]

rawDescription: How to make Python Interactive Shell print cyrillic symbols?

description: Make Python Interactive Shell print cyrillic symbols


code:
a = comp('2 * (3 + x)', '', 'eval')
eval(a, dict(x=3))
decomp(a)

rawDescription: Reversible version of compile() in Python


code:
# get all Rooms (no duplicates)
s = session.query(cte.c.dst.distinct().label("room_id")).subquery(name="rooms")
q = session.query(Room).join(s, Room.id == s.c.room_id)
for r in q:
    print(r)

rawDescription: Recursively select (with limited depth) relationships in SQLAlchemy using ORM, declarative style, and Association objects


code:
def sqrt_cf_expansion(S):
    """Generalized generator to compute continued
       fraction representation of sqrt(S)"""
    m = 0
    d = 1
    a = int(math.sqrt(S))
    a0 = a
    while True:
        yield a
        m = d*a-m
        d = (S-m**2)//d
        a = (a0+m)//d

rawDescription: Code to Generate e one Digit at a Time

description: Code to Generate e one Digit at a Time


rawDescription: python while loop and for loop

description: Python while loop and for loop


code:
for sect in parser.sections():
    if sect == 'root':
        continue
    config_dict[sect] = {}
    for name, value in parser.items(sect):
        config_dict[sect][name] = value

rawDescription: using ConfigParser and dictionary in Python

description: Using ConfigParser and dictionary in Python


code:
for nhat, c in IT.izip(points, colors):
    p = patches.Rectangle((-2.5, -2.5), 5, 5, color=cmap(c), alpha=0.5)
    ax.add_patch(p)
    pathpatch_2d_to_3d(p, z=0, normal=nhat)

ax.set_xlim3d([-5,5])
ax.set_ylim3d([-5,5])
ax.set_zlim3d([-5,5])
plt.show()

rawDescription: How to draw random planes

description: Draw random planes


rawDescription: I want to make a script that recognises even numbers


code:
import re
re.sub("([A-Z])(?=[A-Z])", r"\1 ", "ABC thIs iS XYZ a Test")

rawDescription: regexp for nvda to put spaces between all capital letters?

description: Regexp for nvda to put spaces between all capital letters


code:
def createBoard(rows, cols, mines):
    board = []
    for n_row in range(rows):
        board.append([])

    for n_row in range(rows):
        for n_col in range(cols):
            board[n_row].append("C")
    return board #add this line if you wish the function to actually return the board

rawDescription: list assignment index out of range (creating a minesweeper board)

description: List assignment index out of range (creating a minesweeper board)


rawDescription: In Python Create A Numeric Date/Time Representation


code:
def register(request):
    form = up_form(request.POST or None)
    if form.is_valid():
        sign_up = form.save(commit=False)
        sign_up.password = make_password(form.cleaned_data['password'])
        sign_up.status = 1
        sign_up.save()
        return redirect('/thank-you/')
    return render(request, 'sign_up_form.html', {'form': form})

rawDescription: Saving Hashed Version of User Password in Django Form Not Working


rawDescription: izip_longest on a list of lists

description: Izip_longest on a list of lists


code:
loop = asyncio.get_event_loop()
loop.set_default_executor(ThreadPoolExecutor(1000))
coroutines = asyncio.wait([getaddr(loop, i+site) for i in create_host(char)])
loop.run_until_complete(coroutines)

rawDescription: Why is the asyncio library slower than threads for this I/O-bound operation?


code:
soup = BeautifulSoup(requests.get("http://www.hltv.org/?pageid=188&offset=1").content)
cont = soup.select("div.covMainBoxContent a[href*=matchid=]")

print([a["href"] for a in cont])

rawDescription: Getting TypeError when trying to concatenate 'str' and 'NoneType' objects


code:
somelist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
somelist.__delitem__(slice(4, len(somelist)))
somelist

rawDescription: list.__delslice__ python 3

description: List.__delslice__ python 3


rawDescription: Getting file path from command line arguments in python


code:
pep_seq
ANSPV
ANSPVL
SSISGAGGGGLA
NYDNSAGKW

rawDescription: How to slice a single CSV file into several smaller ones grouped by a field and deleting columns in the final csv's?

description: Slice a single CSV file into several smaller ones grouped by a field and deleting columns in the final csv's


code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get('http://www.doi2bib.org/#/doi/10.1007/s00425-007-0544-9')

element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//pre[@ng-show="bib"]')))
print(element.text)

driver.close()

rawDescription: Python web scraping for javascript generated content


rawDescription: Dynamically creating lists in Python

description: Dynamically creating lists in Python


code:
class MyApi(remote.Service):
  # ...

  @GreetingCollection.method(path='hellogretting', http_method='POST',
                             name='greetings.postGreeting')
  def greetings_post(self, my_collection):
      ndb.put_multi(my_collection.items)
      return my_collection

rawDescription: POST List of Objects w/ endpoints-proto-datastore


code: out = [sum(i) for i in zip(x1, x2, x3)]

rawDescription: Reduce list based on index

description: Reduce list based on index


code:
def collide(self, xvel, yvel, blocks):
    # all blocks that we collide with
    for block in [blocks[i] for i in self.rect.collidelistall(blocks)]:

        # if xvel is > 0, we know our right side bumped
        # into the left side of a block etc.
        if xvel > 0: self.rect.right = block.rect.left
        if xvel < 0: self.rect.left = block.rect.right

        # if yvel > 0, we are falling, so if a collision happpens
        # we know we hit the ground (remember, we seperated checking for
        # horizontal and vertical collision, so if yvel != 0, xvel is 0)
        if yvel > 0:
            self.rect.bottom = block.rect.top
            self.on_ground = True
            self.yvel = 0
        # if yvel < 0 and a collision occurs, we bumped our head
        # on a block above us
        if yvel < 0: self.rect.top = block.rect.bottom

rawDescription: Add collision detection to a plattformer in pygame


rawDescription: Not so simple recursive descent in PyParsing


code:
def custom_sample(population, count):
    names = []
    already_used = []
    for x in range(count):
        temp_index = random.randint(0, len(population)-1)
        while temp_index in already_used:
            temp_index = random.randint(0, len(population)-1)
        names.append(population[temp_index])
        already_used.append(temp_index)
    return names

rawDescription: randomly choose from list using random.randint in python

description: Randomly choose from list using random.randint in python


code:
def processed_record_key(record):
    int_val, value_category, unknown_boolean, unknown_string = record
    return value_category, int_val, unknown_boolean, unknown_string

rawDescription: Combine multiple variable results into a single list in Python


code: {0: 'The S Family', 1: 'The M Family', 2: 'Mr Rich', -1: 'Mr J', -5: 'Stoker', -4: 'Laundryboy', -3: 'Receptionist', -2: 'Mr Z'}

rawDescription: Python - I need a collection with negative indices


code:
import re
regex = re.compile(r'(\d+):(\d+)')

def optionA(line):
    _id, info = line.split("@")
    data = {}
    for g_info in info.split(";"):
        k, v = g_info.split(":")
        data[k] = v
    return data

def optionAA(line):
    data = {}
    for g_info in line[2:].split(";"):
        k, v = g_info.split(":")
        data[k] = v
    return data

def optionB(line):
    _id, info = line.split("@")
    return dict(map(lambda i: i.split(":"), info.split(";")))

def optionC(line):
    return dict(regex.findall(line))

line = 'A@1:100;2:240;3:250;4:260;5:100;6:100;7:100;8:100;9:100;10:100'

rawDescription: What is the best way to create a python dictionary from a string?


code:
C = A.copy()
C[1] += B[1]    # (or appropriate indexes)

rawDescription: add only specific colums in array - python

description: Add only specific colums in array - python


code: elem = next((s for s in selectionsFromTable if data['elements'] in s), None)

rawDescription: How to retreive particular values from a list: python

description: Retreive particular values from a list: python


code:
import requests
from multiprocessing.pool import ThreadPool

NUM_OF_WORKERS=32

def check(username):
    try:
       req = requests.get("http://kik.me/"+username, allow_redirects=False).status_code

       if req == 302:
           print(username, " does not exist.")
       if req == 200:
           print(username, "exists.")
    except Exception as error:
        print(error)

usernames = _loadList(filename)

pool = ThreadPool(processes=NUM_OF_WORKERS)
pool.map_async(check, usernames)
pool.close()
pool.join()

rawDescription: Any possible way to speed up the processing of this?


code:
import random
import itertools
import bisect

def seq_prob(fprob_table, K=6, N=10):
    choices, weights = fprob_table.items()
    cumdist = list(itertools.accumulate(weights))

    results = []
    for _ in range(N):
        s = ""
        while len(s) < K:
            x = random.random() * cumdist[-1]
            s += choices[bisect.bisect(cumdist, x)]
        results.append(s)

    return results

rawDescription: Generate N "random" string of length K using probability table


code: encrypt = 'd' if raw_input("Encrypt or decrypt a file (E/d):") in ('d', 'D') else 'e'

rawDescription: Python Input validation

description: Python Input validation


code:
@property
def _cache(self):
    """
    Implements transparent thread-safe access to a memcached client.
    """
    if getattr(self, '_client', None) is None:
        self._client = self._lib.Client(self._servers)

    return self._client

rawDescription: Django Memcache : Compare and Set

description: Django Memcache : Compare and Set


code:
new_name = re.sub('[^A-Za-z0-9_]+', '', new_name)  # This will strip every character NOT ( NOT is denoted by the leading ^) enclosed in the []
new_name = re.sub('_+', '_', new_name)  # This will replace one or more occurrences of _ with a single _

rawDescription: Python String Templating with Case Sensitivity


code: df.div(df.sum(axis=1), axis=0)

rawDescription: Normalize rows of pandas data frame by their sums


code:
self.bot = servo.Robot(dict(
        waist = servo.Servo(3, 90, .02, 0),
        shoulder = servo.Servo(4, 130, .03, 15),
        elbow = servo.Servo(5, 110, .02, 19),
        wrist = servo.Servo(6, 20, .01, 9),
        claw = servo.Servo(7, 40, .01, 0)
    ), [5, 15, 25])

rawDescription: Preferred way to format a dictionary being passed to a function?


code: return fp, resides, rank, eval, rcondnum

rawDescription: Python Multiple Variable Declaration

description: Python Multiple Variable Declaration


code:
all_forms_are_valid = all(lambda fm: fm.is_val(),
                          map(ArticleForm, some_list_of_dictionaries))

rawDescription: Django: validating forms declared on client side with angularjs


code:
recordedPackage = Package.query.filter_by(name=package.name,
                                        distributionId=package.distributionId,
                                        componentId=package.componentId).first()
app.logger.debug("Does it already Exists ?")
if recordedPackage is None:
    package.depends = packagesNeeded
    db.session.add(package)
elif isinstance(recordedPackage,Package) :
    app.logger.info("updating %r"%(recordedPackage))
    recordedPackage.update(package)

db.session.commit()

rawDescription: Flask-SQLAlchemy updating whole object

description: Flask-SQLAlchemy updating whole object


code: CELERYD_LOG_FORMAT = '[%(levelname)s/%(processName)s] %(message)s'

rawDescription: How to remove timestamps from celery pprint output?

description: Remove timestamps from celery pprint output


rawDescription: How to get rightmost characters plus text between double quotes using regular expressions in Python

description: Get rightmost characters plus text between double quotes using regular expressions in Python


code: bincounts

description: Output in scipy.stats.binned_statistic_dd()


code:
points = np.array([.5, .5]).reshape(1, 2)
points
p.contains_points(points)

rawDescription: inpolygon for Python - Examples of matplotlib.path.Path contains_points() method?

description: Inpolygon for Python - Examples of matplotlib.path.Path contains_points() method


code:
import re
matches = ['$money', 'x^y']
map(re.escape, matches)

rawDescription: pandas test if str contains one of a list

description: Pandas test if str contains one of a list


code:
def m(abacus, units):
    starting_index = abacus.index(min(abacus))
    for raw_index in range(units):
        index = (raw_index + starting_index) % len(abacus)
        abacus[index] = abacus[index] + 1

rawDescription: python list controlled increment

description: Python list controlled increment


code:
def bin(x):
    if x == 0:
        return '0' #Special case: Don't strip leading zero if no other digits
    elif x < 0:
        sign='-'
        x*=-1
    else:
        sign = ''
    l=[]
    while x:
        l.append(_conv_table[x & 0xff])
        x >>= 8
    return sign + ''.join(reversed(l)).lstrip("0")

rawDescription: Base-2 (Binary) Representation Using Python


code:
with open('genes.csv') as f:
    cread_1 = list(csv.reader(f, delimiter = '\t'))
    sample_1_dict = {i: float(j) for i, j in cread_1}
    sample_1_list = [x for x in sample_1_dict.items()]
    sample_1_values_sorted = sorted(sample_1_list, key=lambda expvalues: expvalues[1])
    sample_1_genes = [i for i, j in sample_1_values_sorted]
    sample_1_values = [j for i, j in sample_1_values_sorted]
    sample_1_graph_raw = [float(j) for i, j in cread_1]

rawDescription: Create multiple dictionaries from a single iterator in nested for loops


code:
categories.pickle
cat.txt

rawDescription: How to prepare my own dataset to NLTK ConditionalFreqDist function?

description: Prepare my own dataset to NLTK ConditionalFreqDist function


code:
def main():
    make_video(*get_sound(), find_key())

rawDescription: How to assign values returned from a function to another function

description: Assign values returned from a function to another function


code:
import re

javascript_text = '''
    <script language="javascript" type="text/javascript">
    lpAddVars('page','Section','womens');
    lpAddVars('page','CartTotal','0.00');

        lpAddVars('page','ProductID','43577');
        lpAddVars('page','ProductValue','128.00');

    </script>
'''

product_value = re.findall(r"ProductValue.*,['|\"](.*)['|\"]", javascript_text)

# at this point, product_value = ['128.00']

rawDescription: How do I Extract a Javascript Value using Regular Expressions?


code:
loop = asyncio.get_event_loop()

#UDP Server
coro = loop.create_datagram_endpoint(UdpServer, local_addr=('localhost', 9000))
server_udp = loop.run_until_complete(coro)

#TCP client
loop.run_until_complete(do_connect())

loop.run_forever()

rawDescription: How to reconnect a socket on asyncio?

description: Reconnect a socket on asyncio


code:
def x():
   def _y():
      pass
   x.y = _y

rawDescription: How to autodoc a function inside a method in Sphinx

description: Autodoc a function inside a method in Sphinx


code:
# Vector of features per example.
x = tf.placeholder(tf.float32, shape=[batch_size, num_features])

# Scalar weight per example.
x_weights = tf.placeholder(tf.float32, shape=[batch_size])

# Vector of outputs per example.
y = tf.placeholder(tf.float32, shape=[batch_size, num_outputs])

# ...
logits = ...

# Insert appropriate cost function here.
cost = tf.nn.softmax_cross_entropy_with_logits(logits, y)

rawDescription: How to use event-by-event weights in Tensorflow?

description: Use event-by-event weights in Tensorflow


rawDescription: Best Way to Resize Numpy Dtype for Structured data


code:
has_integer_cube_root(40000**3)
has_integer_cube_root(40000**3 + 1)

rawDescription: How do I get a whole number as a result for a cube root?

description: Get a whole number as a result for a cube root


code:
#Simple player object
class Player(object):
    def __init__(self,x,y,image):
        self.x = x
        self.y = y
        self.image = image

    #Method to draw object
    def draw(self):
        window.blit(self.image,(self.x,self.y))

    #Method to move object (special input of speedx and speedy)
    def move(self,speedx,speedy):
        self.x += speedx
        self.y += speedy

rawDescription: Using classes in Pygame

description: Using classes in Pygame


code:
from numbapro import jit, cuda
from numba import float32
import numbapro.cudalib.cublas as cublas
import numpy as np
from timeit import default_timer as timer

def generate_input(n):
    A = np.array(np.random.sample((n,n)), dtype=np.float32)
    B = np.array(np.random.sample(n), dtype=A.dtype)
    return A, B

@cuda.jit(argtypes=[float32[:,:], float32[:,:], float32[:]])
def diagproduct(c, a, b):
  startX, startY = cuda.grid(2)
  gridX = cuda.gridDim.x * cuda.blockDim.x;
  gridY = cuda.gridDim.y * cuda.blockDim.y;
  height, width = c.shape

  for y in range(startY, height, gridY):
    for x in range(startX, width, gridX):
      c[y, x] = a[y, x] * b[x]

def main():

    N = 1000

    A, B = generate_input(N)
    D = np.empty(A.shape, dtype=A.dtype)
    E = np.zeros(A.shape, dtype=A.dtype)
    F = np.empty(A.shape, dtype=A.dtype)

    start = timer()
    E = np.dot(A, np.diag(B))
    numpy_time = timer() - start

    blas = cublas.api.Blas()

    start = timer()
    blas.gemm('N', 'N', N, N, N, 1.0, np.diag(B), A, 0.0, D)
    cublas_time = timer() - start

    diff = np.abs(D-E)
    print("Maximum CUBLAS error %f" % np.max(diff))

    blockdim = (32, 8)
    griddim  = (16, 16)

    start = timer()
    dA = cuda.to_device(A)
    dB = cuda.to_device(B)
    dF = cuda.to_device(F, copy=False)
    diagproduct[griddim, blockdim](dF, dA, dB)
    dF.to_host()
    cuda_time = timer() - start

    diff = np.abs(F-E)
    print("Maximum CUDA error %f" % np.max(diff))

    print("Numpy took    %f seconds" % numpy_time)
    print("CUBLAS took   %f seconds, %0.2fx speedup" % (cublas_time, numpy_time / cublas_time))
    print("CUDA JIT took %f seconds, %0.2fx speedup" % (cuda_time, numpy_time / cuda_time))

if __name__ == '__main__':
    main()

rawDescription: Anaconda's NumbaPro CUDA Assertion Error


code:
import numpy as np
def gradientX(img):
    return np.gradient(img)[::-1]

rawDescription: Implementing gradient operator in Python


code:
I=np.nonzero(arr>15)
I
arr[I]

rawDescription: Python 2D NumPy array comprehension

description: Python 2D NumPy array comprehension


code: df.shift() != df

rawDescription: How does pandas.shift really work?

description: How does pandas.shift really work


rawDescription: Display data in (%) for pie chart using the PyChart library in Python


code:
ax1.plot(...)
ax2.boxplot(...)
ax3.hist(...)
# etc

rawDescription: How to aggregate plots into figure with subplots

description: Aggregate plots into figure with subplots


code:
df1
df2

rawDescription: Summing over a DataFrame with two conditions and multiple values


code:
import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

rawDescription: Multiple functions to write into a single log file using Python


code:
from django.contrib.auth.decorators import login_required as django_l_r

# Here you're defining your own decorator called `login_required`
# it uses Django's built in `login_required` decorator
def login_required(func):
    decorated_func = django_l_r(func)
    decorated_func.func_dict['login_is_required'] = True
    return decorated_func

@login_required # Your decorator
def authenticatedd_view(request):
    pass

def unauthenticated_view(request):
    pass

rawDescription: Check if a function has a decorator

description: Check if a function has a decorator


rawDescription: With the Python Imaging Library (PIL), how does one compose an image with an alpha channel over another image?


rawDescription: "Index Match" functionality in Excel not captured in pandas Merge (or is it?)


code:
def permu(arr, start = 0):
    if start == len(arr):
        yield arr[:]
    for i in range(start, len(arr)):
        arr[start], arr[i] = arr[i], arr[start]
        for x in permu(arr, start + 1):
            yield x
        arr[start], arr[i] = arr[i], arr[start]

rawDescription: Python generator cannot be converted to list


code:
import itertools as IT

def connected_components(neighbors):
    """
    http://stackoverflow.com/a/13837045/190597 (pillmuncher)
    """
    seen = set()
    def component(node):
        nodes = set([node])
        while nodes:
            node = nodes.pop()
            seen.add(node)
            nodes |= neighbors[node] - seen
            yield node
    for node in neighbors:
        if node not in seen:
            yield component(node)

l1 = [{'fruit':'apple','category':'A'},{'fruit':'banana','category':'B'}]
l2 = [{'type':'new','category':'A'},{'type':'old','category':'B'}]
l3 = [{'order':'1','type':'new'},{'order':'2','type':'old'}]

data = [l1, l2, l3]
G = {}
for dct in IT.chain.from_iterable(data):
    items = dct.items()
    node1 = items[0]
    for node2 in items[1:]:
        G.setdefault(node1, set()).add(node2)
        G.setdefault(node2, set()).add(node1)

for cc in connected_components(G):
    print(dict(cc))

rawDescription: How to merge multiple dictionaries from separate lists if they share any key-value pairs?

description: Merge multiple dictionaries from separate lists if they share any key-value pairs


rawDescription: Invert position of sublists

description: Invert position of sublists


code:
df = pd.read_csv('foo.csv', sep='(?<!,)\s', header=None)
df

rawDescription: Create pandas dataframe from json objects


code:
name = tkinter.filedialog.asksaveasfilename()
with open(name,'w') as f:
    NotImplemented #do stuff with the file

rawDescription: How to save as a file but get its name at the same time?

description: Save as a file but get its name at the same time


rawDescription: Get links form wikipedia

description: Get links form wikipedia


rawDescription: Show interception values, matplotlib

description: Show interception values, matplotlib


code:
import os, errno

def silentremove(filename):
    try:
        os.remove(filename)
    except OSError as e: # this would be "except OSError, e:" before Python 2.6
        if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
            raise # re-raise exception if a different error occured

rawDescription: Most pythonic way to delete a file which may not exist


code:
#include <netinet/in.h>
#include <arpa/inet.h>
#import "Base64.h"

rawDescription: Socket code from python to Objective C

description: Socket code from python to Objective C


rawDescription: What is the Pythonic way to create informative comments in Python 2.x?


code: np.vstack([np.arange(x[0], x[-1]), np.repeat(yp, np.diff(x))]).T

rawDescription: Python - quick interpolate by left value


code:
class Foo(object):
    @classmethod
    def spam(cls):
        print(cls.__name__)

class Bar(Foo):
    @classmethod
    def spam(cls):
        super(Bar, cls).spam()

Bar.spam()
Foo.spam()

rawDescription: When running a method from a Python superclass, how can I know the name of the child class that invoked it?


rawDescription: How to return more than one value over multiple lines

description: Return more than one value over multiple lines


code:
name = input("What is your name? ")

while not name.isalpha():
    print("Make sure to enter your actual name.")
    name = input("What is your name? ")

rawDescription: How would I disallow numbers as input?

description: How would I disallow numbers as input


code:
for x in self.candidates:
    if x is not None:
        db.delete(x.path())

rawDescription: To execute a generator

description: To execute a generator


code:
#using same imports as previous code with one more
import calendar as cal

def make_occupancy_array(start_year, end_year):
    """
    Represents the time between the start and end years, inclusively, as a 1-D array
    of 'pixels', where each pixel corresponds to a day. Consecutive days are thus
    mapped to consecutive pixels. We can perform morphology on this 1D array to
    close small gaps between date ranges.
    """
    years_days = [(yr, 366 if cal.isleap(yr) else 365) for yr in range(start_year, end_year+1)]
    YD = np.array(years_days)  # like [ (2011, 365), (2012, 366), ... ] in ndarray form
    total_num_days = YD[:, 1].sum()
    occupancy = np.zeros((total_num_days,), dtype='int')
    return YD, occupancy

rawDescription: How to combine records based on date using python connected components?

description: Combine records based on date using python connected components


code:
class Card:
    SUITS = ("HEARTS", "SPADES", "DIAMONDS", "CLUBS")
    def __init__(self, suit):
        if suit not in self.SUITS:
             raise ValueError('invalid argument suit. must be: {}'.format(
                  ', '.join(self.SUITS)
             ))
        self.suit = suit

rawDescription: How to get a retrieve a string from a tuple directly?

description: Get a retrieve a string from a tuple directly


code:
results2=[]
for item in results[1]:
    results2.append(item -1)
sub = df.iloc[results2]
sub['three'].mean()

rawDescription: Get statistics from subgroups in pandas

description: Get statistics from subgroups in pandas


code:
from itertools import izip
import csv
with open('A','rb') as f1, open('B','rb') as f2, open('out.csv','wb') as w:
    writer = csv.writer(w)
    for r1,r2 in izip(csv.reader(f1),csv.reader(f2)):
        writer.writerow(r1+r2)

rawDescription: Combining CSV files column-wise

description: Combining CSV files column-wise


code:
def isPrime2(n):
    if n==2 or n==3: return True
    if n%2==0 or n<2: return False
    for i in range(3,int(n**0.5)+1,2):   # only odd numbers
        if n%i==0:
            return False

    return True

rawDescription: isPrime Function for Python Language

description: IsPrime Function for Python Language


code:
def encode_traffic(traffic):
    result = {}
    for id, data in enumerate(traffic):
        result['html_body%s' % id] = data['html_body%s' % id]
    return result
...
features = [encode_traffic(traffic) for traffic in train]

h = FeatureHasher(n_features=10)
features = h.fit_transform(features)
features.toarray()

clf = RandomForestClassifier(n_estimators=100)
clf.train(features)

rawDescription: scikit-learn multi dimensional features

description: Scikit-learn multi dimensional features


rawDescription: Is there a better way to re-use plots Matplotlib?


code:
with open("data.csv") as f,open("new.csv" ,"w") as out:
    r = csv.reader(f)
    wr= csv.writer(out)
    header = next(r)
    for row in r:
        od.setdefault((row[0], row[-2], row[-1]), []).append(" ".join(row[1:-2]))
    wr.writerow(header)
    for loc,vals in od.items():
        wr.writerow([loc[0]] + vals+list(loc[1:]))

rawDescription: python ordered dict issue

description: Python ordered dict issue


code: whoami

rawDescription: How can get current function name inside that function in python


code:
def wonderful_spam(x):
    try:
            return x['infos']['spam'] == 'eggs'
    except (KeyError, TypeError):
            return False
filter(wonderful_spam, my_json)
[x for x in my_json if wonderful_spam(x)]

rawDescription: Filtering json response with python list comprehension


code:
import scrapy
selector = scrapy.Selector(text="""

                        $622
.00

""")
selector.xpath('.//*[@class="price"]/text()').extract()
selector.xpath('string(.//*[@class="price"])').extract()
selector.xpath('normalize-space(.//*[@class="price"])').extract()
selector.xpath('.//*[@class="price"]/text()[1]').extract()
selector.xpath('normalize-space(.//*[@class="price"]/text()[1])').extract()

rawDescription: Selecting the text contents in a particular <div> that has another <div> within it using Scrapy and Xpath


code:
def hours(self):
    retval = ""
    if self.totalTime:
        hoursfloat = self.totalTime.seconds / 3600
        retval = round(hoursfloat)
    return retval

def minutes(self):
    retval = ""
    if self.totalTime:
        minutesfloat = self.totalTime.seconds / 60
        hoursAsMinutes = self.hours() * 60
        retval = round(minutesfloat - hoursAsMinutes)
    return retval

rawDescription: Python format timedelta to string

description: Python format timedelta to string


code: DFList = [group[1] for group in df.groupby(df.index.day)]

rawDescription: How to split a pandas dataframe or series by day (possibly using an iterator)

description: Split a pandas dataframe or series by day (possibly using an iterator)


code:
def elements_and_texts(t):
    for el in t.iter():
        yield 'tag', el.tag
        if el.text is not None:
            yield 'text', el.text
        if el.tail is not None:
            yield 'tail', el.tail

rawDescription: Using lxml to find order of text and sub-elements


code:
where = '%(year)s >= YEAR(date_from) AND IF(%(year)s > YEAR(date_from), \
     IF(%(month)s > MONTH(date_from), %(month)s >= MONTH(date_from), %(month)s < MONTH(date_from)), \
     IF(%(month)s < MONTH(date_from), %(month)s < MONTH(date_from), %(month)s >= MONTH(date_from))) \
     AND %(year)s <= YEAR(date_to) \
     AND %(month)s <= MONTH(date_to)' % \
     {'year': year, 'month': month}
Departure_Date.objects.extra(where=[where])

rawDescription: Django, filter by specified month and year in date range


code:
from PIL import Image
from random import seed, randint

width, height = 200, 200
background = 0

seed(42)
d = dict(((x, y), randint(0, 255)) for x in range(width) for y in range(height))

algorithm = 2
print('Algorithm', algorithm)

if algorithm == 0:
    im = Image.new('L', (width, height))
    for i in d:
        im.putpixel(i, d[i])
elif algorithm == 1:
    buff = bytearray((background for _ in xrange(width * height)))
    for (x,y), v in d.items():
        buff[y*width + x] = v
    im = Image.frombytes('L', (width,height), str(buff))
elif algorithm == 2:
    data = [background] * width * height
    for i in d:
        x, y = i
        data[x + y * width] = d[i]
    im = Image.new('L', (width, height))
    im.putdata(data)

#im.show()

fname = 'qrand%d.png' % algorithm
im.save(fname)
print(fname, 'saved')

rawDescription: Creating an image from a dictionary using PIL


code: erlangTaggedTuple = Dict(Group(Suppress('{') + erlangAtom + Suppress(',') + erlangValue + Suppress('}') ))

rawDescription: pyparsing: grammar for list of Dictionaries (erlang)

description: Pyparsing: grammar for list of Dictionaries (erlang)


code:
np.random.seed(2016)

date_times = pd.date_range(DT.datetime(2012, 4, 5, 8, 0),
                           DT.datetime(2012, 4, 6, 12, 0),
                           freq='1min')
tamb = np.random.sample(date_times.size) * 10.0
radiation = np.random.sample(date_times.size) * 10.0
df = pd.DataFrame(data={'tamb': tamb, 'radiation': radiation},
                  index=date_times)

rawDescription: insert new non-existing column in how= of resample

description: Insert new non-existing column in how= of resample


code:
for line in Table:
    print(*line)

rawDescription: how to print array as a table in python

description: Print array as a table in python


rawDescription: Numpy first occurence of value greater than existing value


code:
pandas.txt
content.bmp
cats.txt

rawDescription: Use pickAFile delete file path but keep file name Python


code:
str1 = ".X."
change_string(str1, 'Y')
change_string("This is a larger 'example' with (more than one) replacement, including a 'false positive.", 'Y')

rawDescription: Replace generic string with something else in python


code:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import scrapy
from scrapy.shell import inspect_response
from scrapy.utils.response import open_in_browser

class Cars(scrapy.Item):
    Make = scrapy.Field()
    Model = scrapy.Field()
    Year = scrapy.Field()
    Entered_Yard = scrapy.Field()
    Section = scrapy.Field()
    Color = scrapy.Field()

class LkqSpider(scrapy.Spider):
    name = "lkq"
    allowed_domains = ["lkqpickyourpart.com"]
    start_urls = (
        'http://www.lkqpickyourpart.com/DesktopModules/pyp_vehicleInventory/getVehicleInventory.aspx?store=224&page=0&filter=toyota&sp=&cl=&carbuyYardCode=1224&pageSize=1000&language=en-US',
    )

    def parse(self, response):
        section_color = response.xpath(
            '//div[@class="pypvi_notes"]/p/text()').extract()
        info = response.xpath('//td["pypvi_make"]/text()').extract()
        for element in range(0, len(info), 4):
            item = Cars()
            item["Make"] = info[element]
            item["Model"] = info[element + 1]
            item["Year"] = info[element + 2]
            item["Entered_Yard"] = info[element + 3]
            item["Section"] = section_color.pop(
                0).replace("Section:", "").strip()
            item["Color"] = section_color.pop(0).replace("Color:", "").strip()
            yield item

        # open_in_browser(response)
        # inspect_response(response, self)

rawDescription: POST request in search query with Scrapy


rawDescription: Python: Read hex from file into list?

description: Python: Read hex from file into list


code: a = x or y

rawDescription: What is the most Pythonic way to provide a fall-back value in an assignment?


code: issubclass(bool, int)

rawDescription: number of values in a list greater than a certain number

description: Number of values in a list greater than a certain number


code:
import logging.handlers
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

syslog_address = '/var/run/syslog'

handler = logging.handlers.SysLogHandler(syslog_address)
# create the formatter
formatter = logging.Formatter('%(name)s: [%(levelname)s] %(message)s')
# plug this formatter into your handler(s)
handler.setFormatter(formatter)

logger.addHandler(handler)
logger.error("What the crap?")

rawDescription: Syslog messages show up as "Unknown" when I use Python's logging.handlers.SysLogHandler


code: [u'AND', u'DOCUMENT', u'FIRST', u'IS', u'ONE', u'SECOND', u'THE', u'THIRD', u'THIS']

rawDescription: How can I pass a preprocessor to TfidfVectorizer? - sklearn - python

description: Pass a preprocessor to TfidfVectorizer? - sklearn - python


code:
issuer = dict()
website = dict()
for dct in [sg, vontobel]:
    for key, val in dct.items():
        if val.startswith('www') or val.endswith('.com'):
            website[key] = val
        else:
            issuer[key] = val

rawDescription: Key to Value to Key in Python Dictionary


code:
class EstablecimientoViewSet(viewsets.ModelViewSet):
    filter_backends = (filters.DjangoFilterBackend,)
    filter_fields = ('categoria', 'categoria__titulo',)

rawDescription: django rest framework filter

description: Django rest framework filter


code: from utils import time_encode

rawDescription: How to write Unit Test in Django

description: Write Unit Test in Django


code:
# Reshape a to a 2D as required in few places later on
ar = a.reshape(a.shape[0],-1)

# Get lex-sorted indices
sortidx = np.lexsort(ar.T)

# Lex-sort reshaped array to bring duplicate rows next to each other.
# Perform differentiation to check for rows that have at least one non-zero
# as those represent unique rows and as such those are unique blocks
# in axes(1,2) for the original 3D array
out = a[sortidx][np.append(True,(np.diff(ar[sortidx],axis=0)!=0).any(1))]

rawDescription: Finding duplicate matrices in Python?

description: Finding duplicate matrices in Python


code:
def naive_sort(ls, key):
    ls = [(key(i), i) for i in ls]
    while ls:
       e = min(ls)
       ls.remove(e)
       yield e[1]

rawDescription: What Can I do for sorting algorithm with my number language?


rawDescription: how to fetch a column in browse_record_list in orm browse method in openERP

description: Fetch a column in browse_record_list in orm browse method in openERP


code:
import pandas as pd
pdf = pd.DataFrame.from_dict({'APP': [0.013, 0.42, -0.23, 0.06],
                              'BOOKS': [-1.3, 0.04, 0.54, 0.01],
                              'CN': [0.012, -0.03, 0.003, 0.5]})
abs_pdf = pdf.abs()
value = 0.01
sel = abs_pdf[abs_pdf > value]

results = list()
by_col = sel.T
for col in by_col:
    results.append(by_col[col].dropna().to_dict())
print(results)

rawDescription: Iterating through a Pandas Dataframe

description: Iterating through a Pandas Dataframe


rawDescription: Sorting in Python 3.3

description: Sorting in Python 3.3


code:
import collections
def top5_words(text):
    counts = collections.Counter(text.split())
    return [elem for elem, _ in counts.most_common(5)]

rawDescription: Python - Return top 5 words with highest frequency


code:
s = "a descriptor 23 fd"
s[:s.find("fd")]
s[:s.find("23")]
s[:s.find("gggfdf")] # <-- look out! last character got cut off

rawDescription: Slice a string after a certain phrase?

description: Slice a string after a certain phrase


code:
trusted_proxies = ('42.42.42.42', '82.42.82.42', '127.0.0.1')

def limit_remote_addr():
    remote = request.remote_addr
    route = list(request.access_route)
    while remote in trusted_proxies:
        remote = route.pop()

    if remote != '10.20.30.40':
        abort(403)  # Forbidden

rawDescription: How to limit Flask dev server to only one visiting ip address

description: Limit Flask dev server to only one visiting ip address


code:
okays_append = okays.append
errors_append = errors.append

for r in results:
    (okays_append if success_condition(r) else errors_append)(r)

rawDescription: List splitting by predicate

description: List splitting by predicate


code:
from module import Home

h = Home(3, 3000, 2)
h.show_info()

h2 = Home(2, 4000, 5)
h2.message = "Second Human habitation: {} bedrooms".format(h2.bedrooms)

h2.show_info()

rawDescription: Inheritance and classes

description: Inheritance and classes


rawDescription: python regex inserting a space between punctuation and letters

description: Python regex inserting a space between punctuation and letters


rawDescription: Finding the n largest primes under m

description: Finding the n largest primes under m


code:
def counter(s):
    d = {}
    for char in s:
        d[char] = d.get(char, 0) + 1
    return d

def isPerm(s1, s2):
    return counter(s1) == counter(s2)

assert isPerm("aab", "aba") == True
assert isPerm("aab", "abb") == False

rawDescription: Why does this Python Dict code work?

description: Why does this Python Dict code work


code:
import sys
sys.exit(main())

rawDescription: Python argparse conditional requirements


code:
def test_decorator(message, arg_name):
    def _test_decorator(f):
        def wrapped_f(*args, **kwargs):
            if arg_name in kwargs:
                print(message)
            return f(*args, **kwargs)
        return wrapped_f
    return _test_decorator

rawDescription: How to allow parameters for a decorator while keep the function's signature at the same time?

description: Allow parameters for a decorator while keep the function's signature at the same time


code:
cartes([False, True], repeat=3)
list(cartes([False, True], repeat=3))

rawDescription: Truth tables in python using sympy

description: Truth tables in python using sympy


rawDescription: Replace escape characters like \n \t etc with \\t , \\n


code: [x + y for x, y in zip(list1, list2)]

rawDescription: Add two lists of integers together into a 3rd list?


rawDescription: How to create channel with socket in Python

description: Create channel with socket in Python


rawDescription: python find all file names in folder that follows a pattern

description: Python find all file names in folder that follows a pattern


code:
x=np.array([(1,2),(2,2),(1,4),(1,1),(3,2)])
x
def activity(t,tickets):
    tmp = tickets[tickets[:,0] < t]
    return tmp[tmp[:,0]+tmp[:,1] > t]
activity(2,x)
activity(3,x)

rawDescription: Numpy : find number of active tickets at time t given a list of start and duration


code:
from threading import *

submits = [[1,2,3], [3,4,5], [6,7,8]]

class worker(Thread):
    def __init__(self, site, postdata, data):
        Thread.__init__(self)
        self.data = data
        self.site = site
        self.postdata = postdata
        self.start()
    def run(self):
        for i in self.data:
            browser.open("http://example.com/jobs.php?actions="+str(i))
            browser.open(self.site, self.postdata)
for obj in submits:
    worker('http://example.com/managament.php?', {'UserID' : '', 'Action':'Delete'}, submits)

rawDescription: Python multi-threading missing jobs

description: Python multi-threading missing jobs


code:
def lookup(x):
    return something, whatever

a, b = lookup(request)

rawDescription: Looking up content and exporting results


code:
import numpy as np
import pylab as pl
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
pl.plot(x, y)
pl.title('Plot of y vs. x')
pl.xlabel('x axis')
pl.ylabel('y axis')
newYlabel = ['1','10','100','1000','10000']
pl.gca().set_yticklabels(newYlabel)
pl.show()

rawDescription: override y axis tick labels without affecting the graph shape in pyplot

description: Override y axis tick labels without affecting the graph shape in pyplot


code:
to_remove = set()
# with enumerate, we walk over index, element pairs
for index,f1 in enumerate(lst):
    if f1 in to_remove:
        continue
    # parens in slicing aren't required, but add clarity
    for f2 in lst[(index+1):]:
        if f2 in to_remove:
            continue
        # no need to check for f1 == f2, since that's now impossible
        # unless elements are duplicated in your list, which I assume
        # is not the case
        if f1.intersect(f2):
            if f1.score >= f2.score:
                to_remove.add(f2)
            else:
                to_remove.add(f1)
# still probably the clearest/easiest way of trimming lst
lst = [x for x in lst if x not in to_remove]

rawDescription: Python 2.7 delete items form list while looping


code:
import myfunctions # for this demo, this module only contains the function myfunction

d = {'somestring': 'myfunction'} # in the real code this came from json.load
d = {k:getattr(myfunctions,v) for k,v in d.items()}
print(d) # {'somestring': <function myfunction at 0x7f36e69d8c20>}
print(d['somestring'](42)) # 84

rawDescription: Clean way to manage parse-dictionaries that contain function names


code: np.insert(F, insert_indices, values)

rawDescription: Efficent insertion of not aligned elements in a numpy array


code: response.xpath('//div[contains(@class, "abc")]/div[contains(@class, "xyz")]/*[local-name() != "div" and not(contains(@class, "needremove"))]').extract()

rawDescription: Remove first tag html using python & scrapy


code:
import numpy

# create a vector for distances from the nearest leftmost minimum
# img_len is the length of the image row

# first we create an integer vector with 1 at each minimum
b = numpy.zeros(img_len, dtype='int')

# then we create an integer vector for the distances
d = numpy.zeros(img_len, dtype='int')

# we mark leftmost distances up to the first minimum to be largest possible
d[:minima[0]] = minima[0] - numpy.arange(minima[0])

# then we iterate through the vector and calculate the distances
for i in range(len(minima) - 1):
    prev = minima[i]
    next = minima[i+1]
    # now we have a gap between the two minima
    # let's fill it with a triangle 0,1,2,...,2,1,0
    k = (next-prev + 1) // 2
    d[prev:prev+k+1] = numpy.arange(k+1)
    d[next-k+1:next] = k -1 - numpy.arange(k-1)

# fill in the space after the last minimum:
d[minima[-1]:] = numpy.arange(img_len - minima[-1])

# all maxima whose distance is less than D from the closest minimum
results = [ m for m in maxima if d[m] < D ]

rawDescription: Find sequence in two Numpy arrays

description: Find sequence in two Numpy arrays


rawDescription: Python - Remove "tagged" item from list only if another item with greater "tag" exists


code:
class Days(Enum):

    Sunday = 'S'
    Monday = 'M'
    Tuesday = 'T'
    Wednesday = 'W'
    Thursday = 'Th'
    Friday = 'F'
    Saturday = 'Sa'

    def next(self):
        cls = self.__class__
        members = list(cls)
        index = members.index(self) + 1
        if index >= len(members):
            index = 0
        return members[index]

rawDescription: Get next enumerator constant/property

description: Get next enumerator constant/property


code:
def sections(fle):
    with open(fle) as f:
        stp = imap(str.strip, f)
        for line in imap(str.strip, stp):
            if line.startswith(":50:"):
                yield next(stp) + next(stp)
                break

rawDescription: Python, optimal way of looping through a file, match two lines after a pattern and then join them


code:
import numpy
import matplotlib
import matplotlib.pyplot as plt

matplotlib.rc('text', usetex = True)
matplotlib.rc('font', **{'family' : "sans-serif"})
params = {'text.latex.preamble' : [r'\usepackage{siunitx}', r'\usepackage{amsmath}']}
plt.rcParams.update(params)

fig = plt.figure(figsize=(8,6))
ax1 = fig.add_subplot(1, 1, 1)
ax1.set_xlabel(r"$a$", fontsize = 14)

fontProperties = {'family':'sans-serif', 'weight': 'normal', 'size': 12}
ax1.set_xticklabels(ax1.get_xticks(), fontProperties)
ax1.set_yticklabels(ax1.get_yticks(), fontProperties)

rawDescription: Latex font style issues using amsmath and sfmath for plot labeling


rawDescription: Extracting selected feature names from scikit pipeline


code:
import json
json_data = json.load(open("myfile.json"))

def walk_keys(obj, path=""):
    if isinstance(obj, dict):
        for k, v in obj.items():
            yield from walk_keys(v, path + "." + k if path else k)
    elif isinstance(obj, list):
        for i, v in enumerate(obj):
            s = "[" + str(i) + "]"
            yield from walk_keys(v, path + s if path else s)
    else:
        yield path

for s in sorted(walk_keys(json_data)):
    print(s)

rawDescription: Print unique JSON keys in dot notation using Python


code:
import re
fixed = re.sub(r"(\[name:.*?)\n", r"\1,\t", originalString, re.M)

rawDescription: Regex matching certain character in string pattern


code:
ids = df.groupby(['FName', 'LName']).id.apply(lambda x: list(x)[-1])
df.Usedid = df.apply(lambda x: int(ids[x.UsedFName, x.UsedLName]), axis=1)

rawDescription: matching between two columns and taking value from another in pandas

description: Matching between two columns and taking value from another in pandas


code:
f.seek(0, os.SEEK_END)
f.write('some stuff')

rawDescription: Python: TemporaryFile returns empty string when read


code:
def emptyline(self):
        """Called when an empty line is entered in response to the prompt.

        If this method is not overridden, it repeats the last nonempty
        command entered.

        """
        if self.lastcmd:
            self.lastcmd = ""
            return self.onecmd('\n')

rawDescription: Python: cmd execute last command while prompt and empty line


rawDescription: python configparser writes configuration file to one line

description: Python configparser writes configuration file to one line


code:
f = urllib2.urlopen("http://tinyurl.com/oex2e")
f.geturl()

rawDescription: How to get the true URL of a file on the web. (Python)

description: Get the true URL of a file on the web. (Python)


rawDescription: How do I parse some of the data from a large xml file?

description: Parse some of the data from a large xml file


code: df['agebin'] = pd.cut(df['agepreg'],breaks, labels=labels, right=False)

rawDescription: Python: How to find properties of items in a series


code: return redirect(url_for('upload.html'))

rawDescription: Bad Request(400) when upload file, Flask


code:
import pkg_resources
pkg_resources.require("Twisted==8.2.0")
import twisted

rawDescription: Force python to use an older version of module (than what I have installed now)


code:
def get_names(input_dictionary):
    list_of_keys = []
    for key in input_dictionary.iterkeys():
        list_of_keys.append(key)
    return list_of_keys

rawDescription: Get a list of a dictionary's key names

description: Get a list of a dictionary's key names


rawDescription: How to keep track of how many foreign keys are connected to an object?

description: Keep track of how many foreign keys are connected to an object


code:
from tkinter import * #If you get an error here, try Tkinter not tkinter

def Dialog1Display():
    Dialog1 = Toplevel(height=100, width=100) #Here

def Dialog2Display():
    Dialog2 = Toplevel(height=1000, width=1000) #Here

master=Tk()

Button1 = Button(master, text="Small", command=Dialog1Display)
Button2 = Button(master, text="Big", command=Dialog2Display)

Button1.pack()
Button2.pack()
master.mainloop()

rawDescription: Can I adjust the size of message box created by tkMessagebox?

description: Adjust the size of message box created by tkMessagebox


code:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base= declarative_base()

class A(Base):
    __tablename__ = "a"

    id = Column(Integer, primary_key=True)
    data = Column(String)

e = create_engine('sqlite://', echo=True)
Base.metadata.create_all(e)
s = Session(e)

a1 = A()
a1.data = func.lower("SomeData")
s.add(a1)
s.commit()

assert a1.data == "somedata"

rawDescription: mysql Compress() with sqlalchemy

description: Mysql Compress() with sqlalchemy


rawDescription: representing large number in python

description: Representing large number in python


code:
class Info(object):
    def __init__(self, data):
        self.data = data

rawDescription: Passing unique dictionary addresses to class instances


code:
import tempfile

new_file, _ = tempfile.mkstemp()

rawDescription: creating a tmp file in python

description: Creating a tmp file in python


code:
#XXX: WRONG (unless your local timezone is UTC), DON'T DO IT
pacific.localize(datetime(2011,2,11,20,0,0,0)).astimezone(utc).strftime("%s")

rawDescription: Python - Setting a datetime in a specific timezone (without UTC conversions)


code:
# Initialize placeholders for comparison
temp_max_string = ''
temp_max_length = 0

#Loop over each string in the list
for i in array:
    # Check if the current substring is longer than the longest found so far
    if len(i) > temp_max_length:
        # Replace the placeholders if it is longer
        temp_max_length = len(i)
        temp_max_string = i

print(temp_max_string) # or 'print temp_max_string' if you are using python2.

rawDescription: DNA sequencing using python

description: DNA sequencing using python


rawDescription: How to find the column index of the value of a given row in numpy?

description: Find the column index of the value of a given row in numpy


code:
def traverse_tree(t):
    yield t.label, t.val
    for child in t.children:
        for label, val in traverse_tree(child):
            yield label, val

rawDescription: Python generator with recursive call

description: Python generator with recursive call


code:
class IncomeVerification(object):
        data = {'IncomeYears': []}
        def __init__(self, income_years):
            for year in income_years:
                new_year = IncomeYear(year).data
                self.data['IncomeYears'].append(new_year)

class IncomeYear(object):
    def __init__(self, year, reported_income=None):
        # new dict/object for each instance
        self.data = {'IncomeYear': {'Year': None}}
        self.data['IncomeYear']['Year'] = year

income_years = ['2014', '2013', '2012', '2011']

foo = IncomeVerification(income_years)
print(foo.data)

rawDescription: Nested dictionaries in classes

description: Nested dictionaries in classes


rawDescription: Numpy array slicing vs. for-loop results


code: graph.es[10:20]["weight"] = range(10)

rawDescription: igraph: how to use add_edges when there are attributes?

description: Igraph: how to use add_edges when there are attributes


code:
class A:
    x = None
    y = None

    def calculate_z(s):
        return s.x ** s.y + s.x

foo = A()
foo.x = 4
foo.y = 2
foo.calculate_z()  # 20

rawDescription: rename class variables in python without the self

description: Rename class variables in python without the self


code:
def create_mean_template(signals, offsets, radius=20):
    w = numpy.hanning(2 * radius)
    return numpy.mean(
        [s[o-radius:o+radius] * w for s, o in zip(signals, offsets)],
        axis=0)

rawDescription: Detecting regions in (x, y) data

description: Detecting regions in (x, y) data


code:
new_cols = df.columns.values
new_cols[:2] = '',''  # since my index columns are the two left-most on the table
df.columns = new_cols

rawDescription: How to save a pandas DataFrame table as a png

description: Save a pandas DataFrame table as a png


code: plt.spy(a, aspect='auto')

rawDescription: python plot large dimension data

description: Python plot large dimension data


rawDescription: Python: Getting all the items out of a `threading.local`


code: ''.join("[{}]".format(e) for e in s.split('.'))

rawDescription: Dot notation string manipulation

description: Dot notation string manipulation


code:
deleted_rows= numpy.where(Data_v_co > 0)
Data_deleted_row=Data_v_co[numpy.all(Data_v_co < 0, axis=0)]

rawDescription: Python row index of deleted row

description: Python row index of deleted row


code:
nodes = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
edges = [('a','b'), ('a','e'), ('f', 'a'), ('k','l')]
nodesRDD = sc.parallelize(nodes).map(lambda n: (n, 0))
edgesRDD = sc.parallelize(edges).map(lambda tup: (tup[0], 1))
joinedRDD = nodesRDD.join(edgesRDD).map(lambda tup: (tup[0], tup[1][1]))
reducedRDD = joinedRDD.reduceByKey(lambda a, b: a + b)

reducedRDD.collect()

rawDescription: join two rdds to make an adjacency list

description: Join two rdds to make an adjacency list


code:
N = 100 # sample count
P = 10  # period
D = 5   # width of pulse
sig = np.arange(N) % P < D

rawDescription: rectangular pulse train in python

description: Rectangular pulse train in python


code:
def check(S):
    if len(S) != 19 or S[4] != ' ' or S[9] != ' ' or S[14] != ' ':
        return False
    S = S.replace(" ", '')
    if len(S) != 16 or not S.isdigit():
        return False
    L = map(int, S)  # makes S into a sequence of integers in one step
    if sum(L) % 10 != 0:  # % calculates the remainder, which should be 0
        return False
    return True

check('9384 3495 3297 0123')
check('9384 3495 3297 0121')

rawDescription: Credit card check exercise python

description: Credit card check exercise python


rawDescription: Find the k smallest values of a numpy array


code:
from subprocess import call

call('color a', shell=True) #this sets the color to light green
print('The quick brown fox jumps over the lazy dog.')

rawDescription: Change shell print color in python 3.3.2


code:
from flask import Blueprint

bp = Blueprint('myext', __name__, template_folder='templates')

class MyExt(object):
    ...
    def init_app(self, app):
        ...
        app.register_blueprint(bp)
    ...

rawDescription: How to load templates from Flask extension

description: Load templates from Flask extension


rawDescription: How To Get Values of Variable For All Instances of A Class


code:
with open(trainFile) as f:
    for line in f:
        if line.startswith('#'):
            continue
        # deal with the rest.

rawDescription: using a for loop but ignoring the first item?

description: Using a for loop but ignoring the first item