code:
pieces = ['a', 'b', 'c']
text = '\n'.join(pieces)

rawDescription: How to get mutable string?

description: Get mutable string


code:
import csv
headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']
rows = [{'Symbol':'AA', 'Price':39.48, 'Date':'6/11/2007',
         'Time':'9:36am', 'Change':-0.18, 'Volume':181800},
        {'Symbol':'AIG', 'Price': 71.38, 'Date':'6/11/2007',
         'Time':'9:36am', 'Change':-0.15, 'Volume': 195500},
        {'Symbol':'AXP', 'Price': 62.58, 'Date':'6/11/2007',
         'Time':'9:36am', 'Change':-0.46, 'Volume': 935000}]

with open('stocks.csv','w') as f:
    f_csv = csv.DictWriter(f, headers)
    f_csv.writeheader()
    f_csv.writerows(rows)

with open('stocks.csv', 'r') as file:
    csvread = csv.DictReader(file, delimiter=',')

    with open('out.csv', 'w') as out:
        headertowrite = ['Time', 'Symbol', 'NewColumn']
        writer = csv.DictWriter(out, headertowrite, extrasaction='ignore')
        writer.writeheader()
        for row in csvread:
            row['NewColumn'] = '1'
            writer.writerow(row)

rawDescription: "Doing work" on csv DictReader fails

description: "Doing work" on csv DictReader fails


code:
Text1
Text2
Text3

rawDescription: Extract based on instances of nodes

description: Extract based on instances of nodes


code:
class BaseContextMixin(ContextMixin):

    def get_context_data(self, **kwargs):
        context_data = super(BaseContextMixin, self).get_context_data(**kwargs)
        common_data_1 = ...
        context_data["common_key_1"] = common_data_1
        common_data_2 = ...
        context_data["common_key_2"] = common_data_2
        ...
        return context_data

rawDescription: Serving multiple templates from a single view (or should I use multiple views?)


code:
a = np.ones((3, 3, 2))
np.einsum('i, jki', v, a)

rawDescription: Numpy Array Broadcasting with different dimensions


code:
import numpy as np
isprime = lambda x: np.all(np.mod(x, range(2, 1 + int(np.sqrt(x)))))
primes = np.array([ x for x in range(2, 255) if isprime(x) ])

rawDescription: Python script to collect all hostnames of ip addresses with only prime entities


code:
urlpatterns = patterns('myapp.views',
    (r'^manager$', 'manager_view'),
)

urlpatterns += patterns('django.views.generic',
    (r'^items/(?P<page>[0-9]+)$', 'list_detail.object_list',
        items_list),
)

rawDescription: How to set-up generic views in an application's urls.py?

description: Set-up generic views in an application's urls.py


code:
seenIds = [ 6 ];
lastScore = 3;

rawDescription: Efficiently page query results

description: Efficiently page query results


code: df.index = df.index.get_level_values(0)

rawDescription: Select only one index of multiindex frame


code:
lis=[1,2,3,4,5]
[(x, i) for i, x in enumerate(lis)]

rawDescription: creating a list of tuples using for loop

description: Creating a list of tuples using for loop


code: sum_l1 = [ x/num_rows for x in sum_l]

rawDescription: Add only the first value in my list of arrays


code:
for level in range(current+1, desti):
    print('Level {}'.format(level))

rawDescription: How to find the numbers in between the numbers that the user has input?

description: Find the numbers in between the numbers that the user has input


code:
import re
seq = 'cacacgtccgccggccctcagagcccgagcgacttccgccccaggccaccgtccacccgcagaaacgcgccgacccacact'

rawDescription: Finding a string pattern and printing the following characters for multiple occurrences in Python 3


code:
def to_file(d):
    return '\n'.join("%s %s" % (key, ' '.join(map(str, values))) for key, values in d.items())

rawDescription: Printing dictionary with lists into file


rawDescription: Extracting data with Headless Selenium browser with Python


rawDescription: Pandas boolean indexing vs comparision of two lists: Same (?) methods, different results


code:
colorarray = np.empty((num, 4), dtype=float)
colorarray[:, :3] = np.random.random_sample((num, 3))
colorarray[:, -1] = 0.6
colorarray = np.repeat(colorarray, 2, axis=0)

rawDescription: Python - Fastest way to generate list of random colours with fixed alpha


code:
class grid_object(object): # inherit from object for new-style classes
    def __init__(self): # when you instantiate the class, this function runs
        self.x, self.y = 1, 1
        self.player_here = '0'
        self.text = " "
        self.been_here_text = " "
        self.been_here_before_bool = False
    def text_to_type(self):
        return self.text
        # this whole function is silly, just reference
        # self.text any time you would do grid_object().text_to_type()

rawDescription: Global variable. I'm doing something wrong I just don't know what


code:
import magic
magic.from_file("path/to/file/filename.extension")

# usage example
magic.from_file("test/test.avi", mime=True)

rawDescription: Detecting if a file is a video in Python?


code:
def parse_url_contents(self, response):
    item = response.request.meta['item']
    item['url_contents'] = response.body
    yield item

rawDescription: Scrapy - parse a page to extract items - then follow and store item url contents


code:
def parse_start_url(response):
    page_number = int(re.search('page=(\d+)', response.url).group(1))
    if page_number > 10:
        raise CloseSpider('page number limit exceeded')
    # scrape the data

rawDescription: Scrapy SgmlLinkExtractor Stop after 10 Pages


code:
import os
qualifier = os.environ['QUALIFIER']

rawDescription: How to use Jenkins Environment variables in python script

description: Use Jenkins Environment variables in python script


code:
class SplitpointGeneralizedTest(unittest.TestCase):
    def testNegatives(self):
        self.assertEquals(2, splitpoint([-1, 5, 2, 4]))
    def testComplex(self):
        self.assertEquals(2, splitpoint([1+1j, -5+2j, 43, -4+3j]))
    def testObjectDtype(self):
        from fractions import Fraction
        from decimal import Decimal
        self.assertEquals(2, splitpoint(map(Fraction, [1.5, 2.5, 3.5, 4])))
        self.assertEquals(2, splitpoint(map(Decimal, [1.5, 2.5, 3.5, 4])))

rawDescription: Find the equilibrium point in a numpy array


rawDescription: Convert redundant array to dict (or JSON)?


code:
class ClusterAssignment(object):

    def _str(self):
        return "[string]"

    def __str__(self):
        return self._str()

    def __repr__(self):
        return self._str()

rawDescription: Specifics of converting a custom __repr__, __str__ on a custom python class


code:
def get_cleaned_or_initial(self, fieldname):
        if hasattr(self, 'cleaned_data'):
            return self.cleaned_data.get(fieldname)
        else:
            return self[fieldname].field.initial

rawDescription: How to use form values from an unbound form

description: Use form values from an unbound form


code:
import string
from itertools import groupby

special = set(string.punctuation + string.whitespace)
s = "One two  three    tab\ttabandspace\t end"

split_combined = [''.join(g) for k, g in groupby(s, lambda c: c in special)]
split_combined
split_separated = [''.join(g) for k, g in groupby(s, lambda c: c if c in special else False)]
split_separated

rawDescription: Efficiently split a string using multiple separators and retaining each separator?


code: df3 = df1.merge(df2, left_on="SubID", right_on="Subject Number", how="left")

rawDescription: Add a new column to Pandas DataFrame with coding data from a separate DataFrame without using a Loop?


code: [i for i, z in enumerate(x) if z == 1]

rawDescription: Python: Searching for an int in a list

description: Python: Searching for an int in a list


code:
import Tkinter
import Canvas
c = Tkinter.Canvas()
line = Canvas.Line(c, 1,1,2,2)

rawDescription: Python 2.7 - Draw on Tkinter Canvas from Different Functions


rawDescription: How to filter by sub-level index in Pandas

description: Filter by sub-level index in Pandas


code: image_bgr = image[..., ::-1]

rawDescription: Reverse a 3d array in python

description: Reverse a 3d array in python


code:
inst1 = Test(2)  # create 2 and attach it to inst1.i
inst2 = Test(5)  # create 5 and attach it to inst2.i
MyList = [inst1.i,inst2.i]  # list of objects: [2 ,5]

rawDescription: Python : How to create a dynamic list of class values


code:
with open('results.csv','w') as f:
    writer = csv.writer(f, delimiter=",")
    writer.writerow(['File Name','Count'])
    writer.writerows(file_counts)

rawDescription: How do I search a file, count the number of hits and write the number to column B in a .csv?

description: Search a file, count the number of hits and write the number to column B in a .csv


code:
def read_contact(db, contact_name):
    formatting = """\
CONTACT: {contact}
    First Name: {First Name}
    Last Name: {Last Name}
    Number: {Number}
    Address: {Address}
"""
    contact_info = db.get(contact_name)
    if contact_info is None:
        raise KeyError("No such contact: {}".format(contact_name))
    contact_info['contact'] = contact_name
    return formatting.format(**contact_info)

def write_db_to_file(db, out_path):
    with open(out_path, 'w') as outf:
        for contact_name in db:
            outf.write(read_contact(db, contact_name))

def remove_user_from_db(db, contact_name):
    try:
        del db[contact_name]
    except KeyError:
        raise KeyError("No such contact: {}".format(contact_name))

rawDescription: Python Not finding phrase on any line other than first (new)


code:
import webbrowser
webbrowser.get('/usr/bin/google-chrome %s').open('http://google.com')

rawDescription: python html pop open

description: Python html pop open


code:
y = {
    k:v
    for element in list_of_elements
    for k,v in _myfunction(element).items()
}

rawDescription: Python List to Dictionary conversion via Comprehension


code: arr = as_strided(a, (slice_length, len(a)), (a.strides[0], a.strides[0])).cumsum(axis=0)

rawDescription: Efficient 2d cumsum

description: Efficient 2d cumsum


code:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder

Builder.load_string('''
# this is the rule for MyWidget that defines
# what MyWidget looks like i.e.drawing
# instructions and widgets etc
<MyWidget>:
    canvas.before:
        Color:
            rgba: 1, 0, 0, 1
        Rectangle:
            # this implicitly binds the size of the
            # rect to the size of the widget
            size: self.size
            # self here still refers to the widget as Rectangle is only a
            # graphics instruction and not a widget
            pos: self.pos
    GridLayout:
        cols: 3
        # root here refers to the `MyWidget`, bind the size of the
        # GridLayout to the size of your root widget
        size: root.size
        Button:
            text: 'A'
        Button:
            text: 'B'
        Label:
            text: 'text'
        Label:
            text: 'other'
        Button:
            text: 'text'
''')

class MyWidget(Widget):
    pass

class MyApp(App):
    def build(self):
        return MyWidget()

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

rawDescription: Why does the root widget not have the same size of the window?


rawDescription: Floating point conversion

description: Floating point conversion


rawDescription: Calling array of functions

description: Calling array of functions


code:
img = None

def openimage():
    global img

    picfile = tkFileDialog.askopenfilename()
    img = ImageTk.PhotoImage(file=picfile)
    canvas.create_image(0,0, anchor=NW, image=img)

rawDescription: Simple Image viewer

description: Simple Image viewer


code:
def elements_replaced(lst, new_element, indices):
    for i, e in enumerate(lst):
        if i in indices:
            yield new_element
        else:
            yield e

rawDescription: Replacing selected elements in a list in Python


code:
responce = {
    'knight': lambda:print('Neee!'),
    'bunny': lambda:print('Rawwww!'),
    'pesant': lambda:print('Witch!'),
    'dingo': lambda:print('Bad, wicked, naughty Zoot!'),
}

rawDescription: Long elif chains vs dictionary with exec()


code:
action = {
    'newChannel': lambda ok: newChannel(ok[12][1]),
    'newNetwork': lambda ok: newNetwork(ok[11][1]),
    'loginError': lambda ok: loginError(ok[10][1])
     }

rawDescription: Dispatch dictionary but pass different parameters to functions


code:
a = [1,2,3]
a.__call__ = lambda n: a(n)
a(1) # AttributeError: 'list' object has no attribute '__call__'

rawDescription: Same name regular and property functions under the same class in python


rawDescription: removing duplicates in a string using set and get it back without order

description: Removing duplicates in a string using set and get it back without order


code:
ratios = []
for i in range(-2, len(players) - 2):
    if i < 0:
       ratios.append(1 - (i * 0.33)) # ratio > 1
    else:
        ratios.append(1 / (1 + (i * 0.33))) # ratio <= 1
winnings = [pot * r for r in ratios]

rawDescription: Distributing points for lottery.py

description: Distributing points for lottery.py


code:
lst = [1, 2, 3, 6, 5]
len(lst)
lst.append(7)
lst.append(4)
len(lst)
lst

rawDescription: Resizing part of a list in python

description: Resizing part of a list in python


code:
abc
abcdefghij
abc

abc

rawDescription: seek to regex in a large file using python

description: Seek to regex in a large file using python


code: pt_ids = Profile_Tag.objects.values_list('profile', flat=True)

rawDescription: Django - Access ForeignKey value without hitting database


code:
parser._actions[-1]
foo2=parser._actions[-1]
foo2.container._remove_action(foo2)
parser.print_help()

rawDescription: Disable/Remove argument in argparse

description: Disable/Remove argument in argparse


code:
class Album(ndb.Model):
    singer_name = ndb.StringProperty()
    album_name = ndb.StringProperty()

rawDescription: How to create a JSON file from a NDB database using Python

description: Create a JSON file from a NDB database using Python


code:
file2.py
import signal
import sys
...
kolejka = nfqueue.queue()
...
def sigterm_handler(signum, frame):
    # close kolejka and do any other cleanup needed, then do:
    sys.exit()

# Make sure sigterm_handler() is run when file1.py tells us
# to quit using child_process.terminate()
signal.signal(signal.SIGTERM, sigterm_handler)

rawDescription: Read data from specific memory address / reference to object


code:
import numpy as np

x = np.linspace(0, 360, 360)
y = 5 * np.sin(x / 90. * 3.14) + np.random.randn(360)

plot(x, y, '+');

rawDescription: Smooth circular data

description: Smooth circular data


code:
import os.path

def getnewfilename(filename):
    testfile = filename
    i = 0
    while os.path.exists(testfile):
        i += 1
        testfile = "%s_%s" % (testfile, i)

    return testfile

rawDescription: creating a output file when the output file exists in the path

description: Creating a output file when the output file exists in the path


code:
from collection import defaultdict

perms = defaultdict(dict)
for category, name in listOfModels:
    perms[category][name] = True

rawDescription: Python syntax. Beautiful, right, short code


code: third_part = inputstring.split('.', 3)[2]

rawDescription: Extracting part of a string based on its naming convention


code: self[:] = self[:5]

rawDescription: Python Custom List Class - Slicing Self

description: Python Custom List Class - Slicing Self


code:
fractions = xml.xpath("//ix:nonFraction[contains(@name, 'AuditFeesExpenses')]", namespaces=nsmap)

for fraction in fractions:
    print(fraction.get("name"))
    print(fraction.text)

rawDescription: Code transformation from BS4 to lxml parser


code: all(len(i) == len(myList[0]) for i in myList)

rawDescription: Checking to see if a list of lists has equal sized lists


rawDescription: pandas selects a set of columns from a dataframe

description: Pandas selects a set of columns from a dataframe


code: ROOT_URLCONF = 'qlimp.urls'

rawDescription: django - ImportError

description: Django - ImportError


code:
def timeout(timeout, raise_exc=True):
    """
    raise_exc - if exception should be raised on timeout
                or exception inside decorated func.
                Otherwise None will be returned.
    """
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            res = None
            exc = None
            def _run():
                nonlocal res
                nonlocal exc
                try:
                    res = func(*args, **kwargs)
                except Exception as e:
                    exc = e
            t = threading.Thread(target=_run)
            t.daemon = True
            t.start()
            t.join(timeout=timeout)
            if raise_exc and t.is_alive():
                raise TimeoutError()
            elif raise_exc and (exc is not None):
                raise exc
            else:
                return res
        return wrapper
    return decorator

rawDescription: Nestable timeout decorator? (i.e. timeout decorated func calling timeout decorated func)


code:
import random
...
random.choice(mylist)

rawDescription: Python: How to make a list of n numbers and randomly select any number?


code:
# as d was opened WITHOUT writeback=True, beware:
d['xx'] = [0, 1, 2]    # this works as expected, but...
d['xx'].append(3)      # *this doesn't!* -- d['xx'] is STILL [0, 1, 2]!

# having opened d without writeback=True, you need to code carefully:
temp = d['xx']      # extracts the copy
temp.append(5)      # mutates the copy
d['xx'] = temp      # stores the copy right back, to persist it

# or, d=shelve.open(filename,writeback=True) would let you just code
# d['xx'].append(5) and have it work as expected, BUT it would also
# consume more memory and make the d.close() operation slower.

d.close()       # close it

rawDescription: Appending a value to a key in Python?

description: Appending a value to a key in Python


code:
import wx

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='BoxSizer demo (resize the frame)')
        self.SetBackgroundColour(wx.WHITE)
        layout = wx.StaticBoxSizer(wx.StaticBox(self,label="wx.VERTICAL"),wx.VERTICAL)
        # row 1
        row = wx.wx.StaticBoxSizer(wx.StaticBox(self,label="wx.HORIZONTAL - proportion=0"),wx.HORIZONTAL)
        row.Add(wx.StaticText(self,label="proportion=0",style=wx.BORDER_DOUBLE),0, wx.ALL,10)
        row.Add(wx.StaticText(self,label="proportion=1",style=wx.BORDER_DOUBLE),1, wx.ALL,10)
        layout.Add(row) # add row 1 without arguments
        # row 2
        row = wx.StaticBoxSizer(wx.StaticBox(self,label="wx.HORIZONTAL - proportion=1"),wx.HORIZONTAL)
        row.Add(wx.StaticText(self,label="proportion=1",style=wx.BORDER_DOUBLE),1, wx.ALL,10)
        row.Add(wx.StaticText(self,label="proportion=2, wx.EXPAND",style=wx.BORDER_DOUBLE),2, wx.EXPAND|wx.ALL,10)
        layout.Add(row,1) # add row 2 with proportion = 1
        # row 3
        row = wx.StaticBoxSizer(wx.StaticBox(self,label="wx.HORIZONTAL - proportion=0, wx.EXPAND"),wx.HORIZONTAL)
        row.Add(wx.StaticText(self,label="proportion=0",style=wx.BORDER_DOUBLE),0, wx.ALL,10)
        row.Add(wx.StaticText(self,label="proportion=1",style=wx.BORDER_DOUBLE),1, wx.ALL,10)
        layout.Add(row,0,wx.EXPAND) # add row 3 with proportion = 0 and wx.EXPAND
        self.SetSizerAndFit(layout)

app = wx.App(False)
Frame().Show()
app.MainLoop()

rawDescription: wxpython flag=wx.EXPAND vs propotion=number

description: Wxpython flag=wx.EXPAND vs propotion=number


code:
estimated_mu = np.log(scale)
estimated_sigma = s

rawDescription: A lognormal distribution in python

description: A lognormal distribution in python


code:
#! /usr/bin/python3
print((lambda a,_: '\n'.join
('{} eats {}'.format(b, ', '
.join(c)[::-1].replace(',',
'dna ,',1)[::-1])for b,c in
a.items()))(*(lambda a,b:(a,
[a[c].append(d)if c in a else
a.update({c:[d]})for c,_,d in
b]))({},[a.strip().split(
maxsplit=2)for a in open
('predators.txt')])))

rawDescription: Python read from txt file (analyze food webs)


code:
Example
import re
from pprint import pprint

def explode(s, keywords):
    for k in keywords:
        m = re.search(r'(%s~[^~]*)(?:~|$)' % (re.escape(k),), s)
        yield m and m.group(1)

lst = [
 ['171000', 'Thing..Mega~Corporate~Thing..Mid~Dairy~Thing..Micro~Cheese', 'Cheese', '0.012174'],
 ['171000', 'Thing..Mega~Corporate', 'Cheese', '0.012174'],
]

print("Before:")
pprint(lst)

for row in lst:
    row[1:2] = explode(row[1], "Mega Mid Micro".split())

print("\nAfter:")
pprint(lst)

rawDescription: Split String In List While Preserving Original List Order


code:
any(somefunction(x) for x in sequence)
all(somefunction(x) for x in sequence)

rawDescription: Does Python have andmap / ormap?

description: Does Python have andmap / ormap


code:
def format(value):
    return "%.3f" % value

formatted = [[format(v) for v in r] for r in m]
file.write(str(formatted))

rawDescription: How do I write a float list of lists to file in Python

description: Write a float list of lists to file in Python


code:
xs = csr_matrix(x)
y = a.multiply(xs)
y
y.A

rawDescription: Element-wise multiplication for sparse matrices in python


code:
df_weekdays = df.loc[~df.weekend, :]
df_weekends = df.loc[df.weekend, :]

rawDescription: Pandas group by several values in a single column


code:
def simple_paths(start, length, visited=[]):
    if length==0:
        yield(visited + [start])
    else:
        for child in children(start):
            if child not in visited:
                for path in simple_paths(child, length-1, visited + [start]):
                    yield(path)

rawDescription: Sub graph isomorphism

description: Sub graph isomorphism


code: brandcolors = [brand[i].replace('HP','g').replace('Lenovo','r').replace('Dell','b') for i in range(len(brand))]

rawDescription: How do I assign different dots different colors in a scatter plot made with MatPlotLib?

description: Assign different dots different colors in a scatter plot made with MatPlotLib


code:
class DeleteHandler(webapp2.RequestHandler):
    def post(self):
        key = self.request.get('k')
        item = db.get(key)
        blobstore.delete([item.blob_key])
        db.delete(item)
        self.response.out.write(key)

rawDescription: How to handle delete in Google App Engine (Python)

description: Handle delete in Google App Engine (Python)


code:
import numpy as np
import pandas as pd

df = pd.DataFrame(
    {'keyword': ["foo","foo","foo","foo","foo","foo","foo","foo","bar","bar","bar"],
     "page": [1,2,3,3,4,5,6,7,7,9,10]})

# df['page'] = df['page'].astype(int)
result = df.groupby(['keyword'])['page'].agg(lambda x: ','.join(np.unique(x).astype(str)))

print(result)

rawDescription: Creating a sorted list of unique numbers from a DataFrame


code: grouped = itertools.groupby(st.splitlines(), lambda x: bool(x.strip()))

rawDescription: Python itertools groupby not grouping as I expect


code:
for x in yield_first(get_first_list()):
    x # process x
for y in yield_first(get_second_list()):
    y # process y

rawDescription: Python idiom to return first item or None


code: pl.xlabel("...", labelpad=20)

rawDescription: Matplotlib - Move X-Axis label downwards, but not X-Axis Ticks


code:
food_groups = {}
for food in foods:
    food_groups.setdefault(food[0], []).append(food[1])

rawDescription: Using counter on array for one value while keeping index of other values


code:
import mod

mod.opt = "changed"

rawDescription: how do imports work for a module used as a singleton?

description: How do imports work for a module used as a singleton


code:
x = defaultdict(list)
for word,pos in brown.tagged_words(tagset='universal')[1:100]:
    x[pos].append(word)

x
len(x)

rawDescription: NLTK - Get and Simplify List of Tags

description: NLTK - Get and Simplify List of Tags


code: 'length ' + str(length) + ' slope ' + str(slope)

rawDescription: python3.4.2 how to assign a string with texts and variables

description: Python3.4.2 how to assign a string with texts and variables


rawDescription: What is the best way to write a multiple nested for loops in Python


rawDescription: use variable to specify format in python

description: Use variable to specify format in python


code: Ximp = Imputer().fit_transform(X)

rawDescription: How to create an SVM for my data?

description: Create an SVM for my data


code:
for i in range(0, len(Array)+1):
  current_sum = 0

rawDescription: Understanding/implementing this enumeration solution for maximum subarray


code:
def receiveSome(self, bytes):
    fit = min(len(bytes), ctypes.sizeof(self))
    ctypes.memmove(ctypes.addressof(self), bytes, fit)

rawDescription: How to pack and unpack using ctypes (Structure <-> str)

description: Pack and unpack using ctypes (Structure <-> str)


code: df

rawDescription: How to calculate difference on previous within set of grouped rows in a dataframe

description: Calculate difference on previous within set of grouped rows in a dataframe


rawDescription: can collections.deque be extended to build a "file buffer"?

description: Can collections.deque be extended to build a "file buffer"


code: position["x"] += ( velocity["x"]["mag"] * velocity["x"]["dir"] )

rawDescription: Accessing dictionary values within a dictionary


code:
with open('/tmp/spam.txt') as f:
  data = [line.strip().split() for line in f.readlines()]

{d[0]: d[1:] for d in data}
{d[0]: ' '.join(d[1:]) if d[1:] else 0 for d in data}

rawDescription: Creating a dictionary with list of lists in Python


code:
df3 = df1.combine_first(df2)

print(df3)

rawDescription: Not able to concatenate two dataframes

description: Not able to concatenate two dataframes


code: subprocess.call("yum install ruby rubygems -y  2>&1 1>/dev/null", shell=True)

rawDescription: Suppress verbose output of shell command in python


code:
import numpy as np
state = np.random.randint(1, 10, (9,9))

rawDescription: Apply function to 2D numpy array elements


code:
def _next(self):
    if self.sequence_id.implementation == 'standard':
        number_next = _select_nextval(self.env.cr, 'ir_sequence_%03d_%03d' % (self.sequence_id.id, self.id))
    else:
        number_next = _update_nogap(self, self.sequence_id.number_increment)
    return self.sequence_id.get_next_char(number_next)

rawDescription: Why the sequence of a field changes when saved in Odoo?


code:
import datetime
strptime = datetime.datetime.strptime
s = "2013-02-20 17:57:25+00:00"
# Using s[:-6] to trim off the timezone
strptime(s[:-6], "%Y-%m-%d %H:%M:%S").strftime("%b %d, %Y %I:%M %p")

rawDescription: Format a timestring in python

description: Format a timestring in python


code:
def highway_factory(roads):
    class HighWay(object):
        max_speed = 55
        min_speed = 35
        road_type = roads.types[2]
    return HighWay

class RoadNetwork(object):
    def __init__(self):
        self.types = ['Dirt', 'Gravel', 'Pavement']
        self.HighWay = highway_factory(self)

rawDescription: Python Class with sub properties (Beginner)


code: total = total + (q*1.3)

rawDescription: Using the if "x" in function to do a calculator but total isn't being produced


code:
fixed_df_output = re.sub('\n\s+', ' ', raw_df_output.read())
df_output_lines = [s.split() for s in fixed_df_output.splitlines()]

rawDescription: Selecting specific columns from df -h output in python


code:
ixs = s1.index.intersection(s2.index)
s1[ixs]

rawDescription: Finding the intersection between two series in Pandas using index


code: diff = [abs(left-right) for left, right in zip(angles, angles[1:]+angles[:1])]

rawDescription: How to compare the last index to the first in a list whilst iterating, or looping

description: Compare the last index to the first in a list whilst iterating, or looping


code:
def using_merge(s):
    m = s.groupby(level=0).agg(np.max)
    df = s.reset_index(['id2'])
    df['m'] = m
    result = df.loc[df['s']==df['m']]
    del result['m']
    result = result.set_index(['id2'], append=True)
    return result['s']

rawDescription: Multiindex pandas groupby + aggregate, keep full index


code:
import datetime

datetime_str = '2016-05-18T15:37:36.993048Z'
old_format = '%Y-%m-%dT%H:%M:%S.%fZ'
new_format = '%d-%m-%Y %H:%M:%S'

new_datetime_str = datetime.datetime.strptime(datetime_str, old_format).strftime(new_format)
print(new_datetime_str)
#'18-05-2016 15:37:36'

rawDescription: Django model DateTimeField set auto_now_add format or modify the serializer


code:
import xml.etree.ElementTree as ET

data = """<?xml version="1.0" encoding="windows-1252" standalone="yes"?><!-- Generated by SMExport 4.88--><ROOT>  <RECORDS>
<METADATA><FIELDS><FIELD attrname="SALENUM" fieldtype="i4"/><FIELD attrname="TIME" fieldtype="time"/><FIELD attrname="DATE" fieldtype="date"/><FIELD attrname="AMOUNT" fieldtype="r8" SUBTYPE="Money"/></FIELDS><PARAMS DEFAULT_ORDER="1" PRIMARY_KEY="1" LCID="1033"/></METADATA>
  <RECORD>
    <ROW
      SALENUM="1"
      TIME="125108"
      DATE="20160122"
      AMOUNT="22.9"
    />
  </RECORD>
  <RECORD>
    <ROW
      SALENUM="2"
      TIME="125243"
      DATE="20160122"
      AMOUNT="22.9"
    />
  </RECORD>
</RECORDS>
</ROOT>"""

root = ET.fromstring(data)
amounts = [float(row.attrib["AMOUNT"]) for row in root.findall(".//RECORD/ROW")]
print(amounts)

rawDescription: How to import specific data from an XML file and process it using python 3.5?

description: Import specific data from an XML file and process it using python 3.5


code:
with open(filename,'r') as f:
    myobject = ET.parse(f)

rawDescription: saving an 'lxml.etree._ElementTree' object

description: Saving an 'lxml.etree._ElementTree' object


code:
X_mean3D = X_mean[:,None,:]
whos

rawDescription: Simple subtraction causes a broadcasting issue for different array shapes


code:
from itertools import chain
statement = 'INSERT INTO Names (FirstName, LastName) VALUES {values}'.format(
    values = ','.join('(?, ?)' for _ in input_seq))
input_seq = (
    ('John', 'Doe'),
    ('Mary', 'Smith'),
    # ...
    )
cursor.execute(statement, list(chain(*input_seq)))

rawDescription: SQL INSERT with multiple rows of values using Jython and zxJDBC


code:
class TestQuery(unittest.TestCase):
    def setUp(self):
        self.engine = create_engine('sqlite:///:memory:')
        self.session = Session(engine)
        Base.metadata.create_all(self.engine)
        self.panel = Panel(1, 'ion torrent', 'start')
        self.session.add(self.panel)
        self.session.commit()

    def tearDown(self):
        Base.metadata.drop_all(self.engine)

    def test_query_panel(self):
        expected = [self.panel]
        result = self.session.query(Panel).all()
        self.assertEqual(result, expected)

rawDescription: Unit tests for Query in SQLAlchemy

description: Unit tests for Query in SQLAlchemy


code:
from __future__ import print_function
import re

parse_re = r'\[([^\[\]]*)\]\s+.*?article_id=(\d+).*?user_id=(\d+)[^\"]*?\"\s+(\d+)\s+(\d+)'

with open('apache.log', 'r') as f:
    data = f.readlines()

for line in data:
    m = re.match(parse_re, line)
    if m:
        (date, article_id, user_id, response_code, content_size) =m.groups()
        print(date, article_id, user_id, response_code, content_size)

rawDescription: How to extract data from a web server log file, and also parse the request line portion?

description: Extract data from a web server log file, and also parse the request line portion


code:
for name in temp_list:
    blah = getattr(object1,name)

rawDescription: Using a variable as part of a variable name


code:
s = '123sa'
for i, c in enumerate(s):
    if not c.isdigit():
        break

s[:i] + s[i:].capitalize()

rawDescription: python capitalize first letter only

description: Python capitalize first letter only


rawDescription: An easy way of printing a n-times-character string in Python


code: os.system('"C:/t est/hello"')

rawDescription: How do I used 2 quotes in os.system? PYTHON

description: Used 2 quotes in os.system? PYTHON


code:
from timeit import timeit
timeit('x = 1 - x', 'x = 0')
timeit('x ^= 1', 'x = 0')
timeit('x = ((x + 1) % 2)', 'x = 0')
timeit('x = next(x_values)', 'from itertools import cycle; x_values = cycle((0, 1))')

rawDescription: if statement vs. expression

description: If statement vs. expression


code: new_dict[values[0]] = list(key)

rawDescription: Get a string list from a word

description: Get a string list from a word


code: names = [person['name'] for person in people]

rawDescription: Convert One Key Value from List of Dictionaries to List


rawDescription: grabbing info from a table with loops (python)

description: Grabbing info from a table with loops (python)


code:
conn = oursql.connect(...)
curs = conn.cursor(oursql.DictCursor)

rawDescription: Python, MySQL and cursors that fetch maps


code:
with open('outfile.txt', 'w') as outfile:
    print('animals', *('years_' + y for y in years), file=outfile)
    for data in zip(animals, *sales):
        print(*data, file=outfile)

rawDescription: Produce single out file from multiple lists in python


code:
from itertools import product
[int(''.join(d)) for l in range(1, 4) for d in product('89', repeat=l)]

rawDescription: How do I create a lambda function to print numbers containing x and y in Python?

description: Create a lambda function to print numbers containing x and y in Python


code:
y=xrange(5)
y
list(y)

rawDescription: Python list tuples

description: Python list tuples


code:
#!/usr/bin/python
# coding=utf-8

import sys
import codecs
import unicodedata

out = codecs.getwriter('utf-8')(sys.stdout)

def width(string):
    return sum(1+(unicodedata.east_asian_width(c) in "WF")
        for c in string)

a1=[u'する', u'します', u'trazan', u'した', u'しました']
a2=[u'dipsy', u'laa-laa', u'banarne', u'po', u'tinky winky']

for i,j in zip(a1,a2):
    out.write('%s %s: %s\n' % (i, ' '*(12-width(i)), j))

rawDescription: Python utf-8, howto align printout

description: Python utf-8, howto align printout


code:
import inspect

def get_tests(cls):
    return [k for k, v in cls.__dict__.items() if k.startswith('test') and inspect.ismethod(v)]

for name in get_tests(MyTest):
    suite.addTest(MyTest(resource, name))

rawDescription: DI with unit tests called from elsewhere in application?


code:
[s + str(suffix) if num>1 else s for s,num in Counter(mylist).items() for suffix in range(1, num+1)]
# Produces: ['zip1', 'zip2', 'city', 'state', 'name1', 'name2', 'name3']

rawDescription: Python: Rename duplicates in list with progressive numbers without sorting list


code: counter_list = [item for item in counter_list if item]

rawDescription: Removing empty Counter() objects from a list


code:
def cleaner():
    print("Cleaner from functions!")

def worker():
    print("Worker from functions!")

rawDescription: How to load my fake module every time when `import a real module` in python?

description: Load my fake module every time when `import a real module` in python


code:
import sys
from PIL import Image

images = map(Image.open, ['Test1.jpg', 'Test2.jpg', 'Test3.jpg'])
widths, heights = zip(*(i.size for i in images))

total_width = sum(widths)
max_height = max(heights)

new_im = Image.new('RGB', (total_width, max_height))

x_offset = 0
for im in images:
  new_im.paste(im, (x_offset,0))
  x_offset += im.size[0]

new_im.save('test.jpg')

rawDescription: Combine several images horizontally with Python


code:
import re
p = re.compile(r'\[{2}(?:[^|\]]*\|)?([^]]*)]{2}')
test_str = "He lives in [[Gimhae city|Gimhae]]. He lives in [[Gimhae]]. "
result = re.sub(p, r"\1", test_str)
print(result) # => He lives in Gimhae. He lives in Gimhae.

rawDescription: unstruct wikipedia synonym bracket

description: Unstruct wikipedia synonym bracket


code: locs, labels = plt.xticks()

rawDescription: Using xticks, ticks not aligned properly with figure


code:
import re
re.findall(r'\$(\d+)', "I bought a boat for $5000 and it's awesome")
re.findall(r'\$(\d+(?:\.\d+)?)', "I bought two boats for $5000  $5000.45")

rawDescription: How to return a word in a string if it starts with a certain character? (Python)

description: Return a word in a string if it starts with a certain character? (Python)


rawDescription: Python: Read text file with time in hour, minutes and seconds; and angle in degree, arc minutes and arc seconds


rawDescription: Python - Find sequence in array: start when two values below threshold, end when two values above threshold


rawDescription: Can a class variable be created inside one of the class' methods in Python?


code:
#!/usr/bin/env python
from datetime import datetime
from email.util import parsedate_tz, mktime_tz

timestamp = mktime_tz(parsedate_tz('Thu Mar 19 12:37:15 +0000 2015'))
s = str(datetime.fromtimestamp(timestamp))
# -> '2015-03-19 08:37:15'

rawDescription: Convert Time Zone + format in Python from Twitter API


code:
user_input = input('Please enter either the value 1 or 2: ')
if(user_input == 1 or user_input == 2):
    # do stuff here (remove the pass and replace with code you want to execute)
    pass
else:
    user_input = input('Please enter either the value 1 or 2: ')

rawDescription: Python Programming: How can I display a message if a certain value is entered?


code:
from fabric.api import *

env.use_ssh_config = True
env.hosts = ["myserver"]
env.user = "root"
env.key_filename = "/home/myusername/.ssh/id_rsa"
env.password = ""
env.port = 22

def testlive():
  run("ls")

rawDescription: Can't connect to remote server with Fabric and SSH using key file


code:
def func3(a,k):
    b = np.empty(a.shape)
    ind = np.indices(a.shape).reshape(6, -1)
    mask = ~np.all(ind[:3] == ind[3:6], axis=0)
    ar = a.ravel()
    br = b.ravel()
    br[:] = ar*ind[k]**2
    br[mask] -= ar[mask]*ind[3+k, mask]**2
    return b

rawDescription: Vectorized manipulation of an array, based on a function of indices


rawDescription: Equivalent for str.join in pandas

description: Equivalent for str.join in pandas


code:
any(['foo'])
from scipy import *
any(['foo'])

rawDescription: SciPy/NumPy import guideline

description: SciPy/NumPy import guideline


code:
import os
sum(os.path.getsize(f) for f in os.listdir('.') if os.path.isfile(f))

rawDescription: Calculating a directory size using Python?


rawDescription: Converting PANDAS dataframe from monthly to daily


code:
import csv

with open('psc.csv',newline='') as pscfile:
    reader = csv.reader(pscfile)
    next(reader)
    results = dict(reader)  # pull in each row as a key-value pair

rawDescription: how to read a csv into a dictionary in python?

description: Read a csv into a dictionary in python


code: pd.concat([x]*5, ignore_index=True)

rawDescription: How to repeat Pandas data frame?

description: Repeat Pandas data frame


code: [num for num in primes if any(x for x in '024685' in str(num))] # failed: 'bool' object is not iterable

rawDescription: Project Euler #35: nicer way to test if an even integer is 'inside' an integer


rawDescription: clean summation involving index of numpy arrays

description: Clean summation involving index of numpy arrays


code:
# tag rows based on the threshold
df['tag'] = df['values'] > .5

# first row is a True preceded by a False
fst = df.index[df['tag'] & ~ df['tag'].shift(1).fillna(False)]

# last row is a True followed by a False
lst = df.index[df['tag'] & ~ df['tag'].shift(-1).fillna(False)]

# filter those which are adequately apart
pr = [(i, j) for i, j in zip(fst, lst) if j > i + 4]

rawDescription: Delimiting contiguous regions with values above a certain threshold in Pandas DataFrame


code:
class MapResult(ApplyResult):

    def __init__(self, cache, chunksize, length, callback):
        ApplyResult.__init__(self, cache, callback)
        ...
        # _number_left is used to know when the MapResult is done
        self._number_left = length//chunksize + bool(length % chunksize)

rawDescription: Iterable multiprocessing Queue not exiting


code:
from bs4 import BeautifulSoup
import urllib.request as urllib2

wiki = "http://www.maditssia.com/members/list.php?p=1&id=Engineering%20Industries"
header = {'User-Agent': 'Mozilla/5.0'}
req = urllib2.Request(wiki,headers=header)
page = urllib2.urlopen(req)
soup = BeautifulSoup(page)

for table in soup.find_all('table', attrs={'class': 'tableborder'}):
    data = []
    data.append(table.select('tr:nth-of-type(1) > td:nth-of-type(2)')[0].string or '')
    data.append(table.select('tr:nth-of-type(1) > td:nth-of-type(4)')[0].string or '')
    data.append(table.select('tr:nth-of-type(2) > td:nth-of-type(2)')[0].string or '')
    data.append(table.select('tr:nth-of-type(2) > td:nth-of-type(6)')[0].string or '')
    data.append(table.select('tr:nth-of-type(3) > td:nth-of-type(6)')[0].string or '')
    data.append(table.select('tr:nth-of-type(4) > td:nth-of-type(2)')[0].string or '')
    print(' | '.join(data))

rawDescription: Scrape table without no class BS4 python


code:
import datetime

def get_stats(i, init, init2, init3, init4,
       start, end, desc, cache, part, hashp):
    return {
        "i": i,
        "init": init.getNumPartitions(),
        "init1": init2.getNumPartitions(),
        "init2": init3.getNumPartitions(),
        "init4": init4.getNumPartitions(),
        "time": str(end - start),
        "timen": (end - start).seconds + (end - start).microseconds * 10 **-6,
        "desc": desc,
        "cache": cache,
        "part": part,
        "hashp": hashp
    }

rawDescription: Spark iteration time increasing exponentially when using join


code:
string = 'Born in the USA'
' '.join([w.title() if w.islower() else w for w in string.split()])

rawDescription: Python Title Case, but leave pre-existing uppercase


code: x[:] = [datetime.strptime(dt, '%d/%m/%Y').toordinal() for dt in x]

rawDescription: Python - Altering a list that is being iterated. Out of Scope


code:
template = etree.fromstring('
 A template
')
etree.tostring(template)

rawDescription: Parsing html partial templates using lxml


code:
from sympy.abc import x

f = x**2 + 3*x + 2
g = f.subs({x:2/x})

rawDescription: Transforming polynomial variable in Numpy


rawDescription: How can I organize my data into a grid using Python Pandas?

description: Organize my data into a grid using Python Pandas


rawDescription: Save JSON outputed from a URL to a file

description: Save JSON outputed from a URL to a file


code:
import handler
import test1, test2
x = handler.handler.getInstance()

#perform operations of test1

y = handler.handler.getInstance()
#this time handler already has already instantiated so it wont print new
#performOperations of test2

rawDescription: Python:different files accessing same single instance of a class instead of creating separate single instances in each of them


code: resp, data = mail.uid('search', 'ALL', 'UID', '%d:*' % (i+1))

rawDescription: Working with IMAP using UIDs

description: Working with IMAP using UIDs


code:
class PurchaseAdmin(admin.ModelAdmin):
    list_display = ('number', 'get_modules')

admin.site.register(Service, ServiceAdmin)

rawDescription: How to display a child's many to many attribute in a parent model's list_display in django

description: Display a child's many to many attribute in a parent model's list_display in django


code:
import string
data = list(string.ascii_lowercase) * 2
df = pd.DataFrame({'txt': np.random.choice(data, 30)})

rawDescription: Equivalent of grep -C in Pandas

description: Equivalent of grep -C in Pandas


code:
def intBitsToFloat(b):
   s = struct.pack('>l', b)
   return struct.unpack('>f', s)[0]

rawDescription: convert unsigned integer to float in python

description: Convert unsigned integer to float in python


code:
source, local = sources[3], {}
exec(source, func.__globals__, local)
local.popitem()[1]

rawDescription: Is it possible to access inner functions and classes via code objects?


code:
# urls.py

urlpatterns = patterns('',

    url(r'^home/$', HomeView.as_view(), name='home'),
    url(r'^profile/$', UserProfileFormView.as_view(), name='profile'),
    url(r'^confirm/$', HomeView.as_view(template_name='checkemail.html'), name='checkemail'),
)

rawDescription: Improving Python/django view code

description: Improving Python/django view code


code:
import pandas as pd
from StringIO import StringIO

table = """suburb  state   house   yrend   median_price
CHARNWOOD   ACT H   31/01/91 0:00   91000
CHARNWOOD   ACT H   28/02/91 0:00   90000
...
BRUCE   ACT H   30/11/96 0:00   312000
"""

table = table.replace("0:00", "")
sio = StringIO(table)
df = pd.read_table(sio, sep=r"\s+", parse_dates=['yrend'])
df = df.pivot(index="yrend", columns="suburb", values="median_price")
df.plot()

rawDescription: Plot no consecutive days with even intervals


rawDescription: What is an efficient way to slice a Pandas Series with a MultiIndex along multiple dimensions?


code:
def __init__(self, *args, **kwargs):
    super(PostForm, self).__init__(*args, **kwargs)
    for field_name in self.fields:
        field = self.fields[field_name]
        field.widget.attrs.update({'class': 'form-control'})

rawDescription: how to edit {{form|crispy}} with css and move the forms around?

description: Edit {{form|crispy}} with css and move the forms around


code: df.loc[~df.name.duplicated() & df.time_diff.notnull() & (df.time_diff <= 10)]

rawDescription: How to apply time-window related data analysis via Pandas in Python

description: Apply time-window related data analysis via Pandas in Python


code:
def pyramid(h,symbol="#"):
    out = ""
    for i in range(h):
        out += (" " * (h-i - 1)) + (symbol * (2 * i + 1)) + "\n"
    return out

print(pyramid(5))

rawDescription: Writing a function that prints a pyramid - two parameters


code:
@app.route("/cameradump/<date>")
def index(date):
    print("Date:", date)
    return render_template('index.html')

rawDescription: How to set a variable with pressing a button on an html file to the flask server route

description: Set a variable with pressing a button on an html file to the flask server route


code:
s = """schemaLocation=
            "http://docs.oasis-open.org/wsn/b-2.xsd"/>"""
re.sub(r'(schemaLocation=)\s*\"(.*\/)?(.*\")(.*)', r'\1"\3\4', s, re.M)

rawDescription: Python regular expression to modify paths


code:
from collections import Counter

data = {'Paul ': [5, Counter({'i': 1, 'in': 1, 't': 1, 'fa': 1, 'cr': 1})],
        'Lisa ': [4, Counter({'f': 3, 'bo': 1})],
        'Tracy ': [6, Counter({'sl': 3, 'fi': 1, 'an': 1, 'piz': 1})],
        'Maria': [2, Counter({'t': 2})]}

for lst in data.values():
    lst[1] = {k: (lst[0], float(v)/lst[0]) for k, v in lst[1].items()}

rawDescription: Divide dictionary values by another value in the same key in Python


code:
np.random.seed(2015)
df = pd.DataFrame(np.random.randint(10, size=(3,4)))
df
df.stack()

rawDescription: How to select all data from Data Frame?

description: Select all data from Data Frame


rawDescription: Better way for concatenating two sorted list of integers


code:
background = fig.canvas.copy_from_bbox(ax.bbox)

for x, y in user_interactions:
    fig.canvas.restore_region(background)
    points.append([x, y])
    scatter.set_offsets(points)
    ax.draw_artist(scatter)
    fig.canvas.blit(ax.bbox)

rawDescription: Efficient Matplotlib Redrawing

description: Efficient Matplotlib Redrawing


code:
from bidict import BiDict
d = BiDict({'a':1,'b':2})
d['a']
d[2]
d['c']=3
d[3]
del d['a']
d['a']
d[1]

rawDescription: Python dealing with reverse lookup

description: Python dealing with reverse lookup


code:
d = AttrDict({"a":1, "b":2})
d2 = pickle.loads(pickle.dumps(d, -1))
d2
d2.b = 3
d2
d2.__dict__

rawDescription: Passing a dict-like Object through multiprocessing.Queue Makes it Unable to be Modified by Attribute


code:
n, d = abs(f).as_integer_ratio()
k = d.bit_length() - 1
result = _dec_from_triple(sign, str(n*5**k), -k)

rawDescription: Whence the 27 digits of extra precision in `decimal.Decimal(1.0/3.0)`?


code:
from selenium import webdriver

jquery_url = "http://code.jquery.com/jquery-1.11.2.min.js"

driver = webdriver.Firefox()
driver.get("http://html5demos.com/drag")
driver.set_script_timeout(30)

# load jQuery helper
with open("jquery_load_helper.js") as f:
    load_jquery_js = f.read()

# load drag and drop helper
with open("drag_and_drop_helper.js") as f:
    drag_and_drop_js = f.read()

# load jQuery
driver.execute_async_script(load_jquery_js, jquery_url)

# perform drag&drop
driver.execute_script(drag_and_drop_js + "$('#one').simulateDragDrop({ dropTarget: '#bin'});")

rawDescription: How to simulate HTML5 Drag and Drop in Selenium Webdriver?

description: Simulate HTML5 Drag and Drop in Selenium Webdriver


code: not rex_nomatch and re2_matches

rawDescription: Merge several regular expressions into one RE


rawDescription: Mpmath.meijerg: Order of arguments

description: Mpmath.meijerg: Order of arguments


code:
r = requests.get('http://finance.yahoo.com/q?s=APP')
soup = BeautifulSoup(r.content)

rawDescription: Selenium: how to get the entire html as a string?


rawDescription: Index, for loops and making things look cleaner


code: pd.options.display.multi_sparse = False

rawDescription: Flatten the results of a group by in a python dataframe after printing the grouped instance counts


code:
import os
import sys

import heroku

"""Scale heroku web processes using the heroku python API."""

# you may want to add better argument processing, use argparse, etc.
dynos = int(sys.argv[1])
cloud = heroku.from_key(os.environ.get('HEROKU_API_KEY'))
app = cloud.apps['your_app_name']

try:
    # you may want to add a maximum dyno check here to prevent costly mistakes ;)
    webproc = app.processes['web']
    webproc.scale(dynos)

except KeyError:
    # note: scaling to 0 dynos or attempting to scale up if 0 web dynos exist
    # both throw this error. Make sure you have at least one dyno.
    print >> sys.stderr, "Could not scale web processes - are there 0 web dynos running?"

rawDescription: schedule number of web dynos by time of day

description: Schedule number of web dynos by time of day


code:
def preprocess(string):
    # do what ever preprocessing that it needs to be done
    # e.g. convert to lowercase: string = string.lower()
    # return the sequence of tokens
    return string.split()

rawDescription: extracting n grams from huge text

description: Extracting n grams from huge text


code:
class  MyStruct:
    def __init__(self, first_byte, second_byte):
        self.first_byte = first_byte
        self.second_byte = second_byte
        self.combined = hex(first_byte) + " " + hex(second_byte)

test = MyStruct(36, 128)
print("MyStruct: test first=%i second=%i comb=%s" %(test.first_byte, test.second_byte, test.combined))

rawDescription: Generating a protocol string

description: Generating a protocol string


code:
with open("house.txt", "w") as fE:
    fE.writelines(','.join(str(j) for j in i) + "\n" for i in ElR + ShR)

rawDescription: Trying to write and read two matrices in text file


code:
list_of_players = []
def players():
    for i in range(0, n):
        name = raw_input("enter your name")
        running_speed = int(input("running speed:"))
        passing_acc = float(input("passing accuracy:"))
        shooting_acc = float(input("shooting accuracy:"))
        new_player = Player(name, running_speed, passing_acc, shooting_Acc)
        list_of_players.append(new_player)

rawDescription: Menu Interface Player Records

description: Menu Interface Player Records


code:
for x in thelist:
    if x==0:
        continue
    # rest of your code

rawDescription: How to use the count() method on a list excluding some items

description: Use the count() method on a list excluding some items


code:
df4 = pd.DataFrame(np.random.rand(6, 2))
pd.concat([df, pd.concat([df4], keys=["D"])])

rawDescription: Add multi-index to pandas dataframe and keep current index


code:
self.v1.set("1")
self.v2.set("0")

rawDescription: Python Check Buttons

description: Python Check Buttons


code:
if os.path.exists(df):
  with open(df, 'rb') as f:
    packet = f.read(blocksize)

    while packet != '':
      conn.send(packet)

      packet = f.read(blocksize)

rawDescription: How to transfer a file between two connected computers in python?

description: Transfer a file between two connected computers in python


code:
car_makes = soup.select("#make_id option + option + option")

for car in car_makes:
    print(car.text)
    data["parent_id"] = car["value"]
    form = requests.post(url, data=data, headers=headers)
    soup11 = BeautifulSoup(form.content.strip('"').replace('\\"','"').replace("\/", "/"), "html.parser")
    print([opt.text for opt in soup11.select("option + option + option") if not opt.text.isdigit()])

rawDescription: Scraping data which loaded when you select something


code:
with open('/tmp/output.csv', 'wb') as csvfile:
  writer = csv.writer(csvfile)
  writer.writerow(['Project Name', 'Documents'])
  writer.writerows(
    [os.path.basename(dirpath)]+filenames
    for dirpath,_,filenames in os.walk(path)
    if filenames)

rawDescription: Python script to generate CSV file with foldernames including their associated files


code:
class DmitryImageField(ImageField):

    def to_python(self, data):
        f = super(DmitryImageField, self).to_python(data)
        if f is None:
            return None

        try:
            from PIL import Image
        except ImportError:
            import Image

        # We need to get a file object for PIL. We might have a path or we might
        # have to read the data into memory.
        if hasattr(data, 'temporary_file_path'):
            file = data.temporary_file_path()
        else:
            if hasattr(data, 'read'):
                file = BytesIO(data.read())
            else:
                file = BytesIO(data['content'])

        try:
            im = Image.open(file)
            if im.format not in ('BMP', 'PNG', 'JPEG'):
                raise ValidationError("Unsupport image type. Please upload bmp, png or jpeg")
        except ImportError:
            # Under PyPy, it is possible to import PIL. However, the underlying
            # _imaging C module isn't available, so an ImportError will be
            # raised. Catch and re-raise.
            raise
        except Exception: # Python Imaging Library doesn't recognize it as an image
            raise ValidationError(self.error_messages['invalid_image'])

        if hasattr(f, 'seek') and callable(f.seek):
            f.seek(0)
        return f

rawDescription: How to validate image format in django ImageField

description: Validate image format in django ImageField


code: [reduce(lambda a, x: a * 10 + x, t) for t in tuples]

rawDescription: How to turn a tuple into an integer in Python (examples inside)?

description: Turn a tuple into an integer in Python (examples inside)


code:
def combinations(iterable, r):
    # combinations('ABCD', 2) --> AB AC AD BC BD CD
    # combinations(range(4), 3) --> 012 013 023 123
    pool = tuple(iterable)
    n = len(pool)
    if r > n:
        return
    indices = range(r)
    yield tuple(pool[i] for i in indices)
    while True:
        for i in reversed(range(r)):
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range(i+1, r):
            indices[j] = indices[j-1] + 1
        yield tuple(pool[i] for i in indices)

rawDescription: counter modified during recursion in python

description: Counter modified during recursion in python


code:
def convert(in_file, out_file):
    for line in in_file:
        line = line.rstrip(' \n\r')
        if not line:
            continue
        if line.startswith('"'):
            name = line.strip('"')
        else:
            out_file.write('{}\n'.format(','.join([name] + line.split())))

rawDescription: Python Matplotlib - Multiple series in same data file


code:
import pyqtgraph as pg
pw = pg.PlotWidget(name='My Graph')
horizontalLayout_Plot.addWidget(pw)
x,y = range(10),range(10)
pw.plot(x,y)

rawDescription: PyQt/PySide simple XY Plot widget

description: PyQt/PySide simple XY Plot widget


code:
import json

link = "https://www.coursera.org/maestro/api/topic/information?topic-id=nlp"
data = json.load(urllib2.urlopen(link))

rawDescription: Parse a webpage with BeautifulSoup4

description: Parse a webpage with BeautifulSoup4


code:
bucket1 = iter(range(0, 10))
bucket2 = iter(range(10, 20))
bucket3 = iter(itertools.count(20)) # or another range with upper bound

sub25 = itertools.chain(bucket1, bucket2, bucket3)
from25to100 = itertools.chain(bucket2, bucket3)
allOther = bucket3

rawDescription: Assigning the inputs, with respect to its len()


code:
from tkinter import *

def ftm(feet):
    return feet * 0.305

def mtf(meters):
    return meters / 0.305

def convert(amount, func, result):
    value = func(float(amount.get()))
    result.set(round((value), 3))

def choose():
    choice = lol.get()
    if choice == "ft":
        feet = StringVar()
        meters = StringVar()
        rest = Toplevel()
        f3 = Frame(rest)
        f3.grid(column=0, row=0, sticky=(N,W,E,S))
        f3.columnconfigure(0, weight=1)
        f3.rowconfigure(0, weight=1)
        feet_entry = Entry(f3, width=7, textvariable=feet)
        feet_entry.grid(column=2, row=1, sticky=(W, E))
        Label(f3, text="feet").grid(column=3, row=1, sticky=W)
        Label(f3, text="is equal to").grid(column=1, row=2, sticky=E)
        Label(f3, textvariable=meters).grid(column=2, row=2, sticky=(W, E))
        Label(f3, text="meters").grid(column=3, row=2, sticky=W)
        Button(f3,
               text="Calculate",
               command=lambda a=feet_entry, f=ftm, r=meters: convert(a, f, r)
              ).grid(column=3, row=3, sticky=W)
    elif choice == "m":
        meters = StringVar()
        feet = StringVar()
        pepp = Toplevel()
        f4 = Frame(pepp)
        f4.grid(column=0, row=0, sticky=(N,W,E,S))
        f4.columnconfigure(0, weight=1)
        f4.rowconfigure(0, weight=1)
        meter_entry = Entry(f4, width=7, textvariable=meters)
        meter_entry.grid(column=2, row=1, sticky=(W, E))
        Label(f4, text="meters").grid(column=3, row=1, sticky=W)
        Label(f4, text="is equal to").grid(column=1, row=2, sticky=E)
        Label(f4, textvariable=feet).grid(column=2, row=2, sticky=(W, E))
        Label(f4, text="feet").grid(column=3, row=2, sticky=W)
        Button(f4,
               text="Calculate",
               command=lambda a=meter_entry, f=mtf, r=feet: convert(a, f, r)
              ).grid(column=3, row=3, sticky=W)
    else:
        rino = Toplevel()
        f5 = Frame(rino)
        f5.grid(column=0, row=0, sticky=(N,W,E,S))
        Label(f5, text="ERROR!").grid(column=1, row=1)

rip = Tk()
f2 = Frame(rip)
f2.grid(column=0, row=0, sticky=(N,W,E,S))
f2.columnconfigure(0, weight=1)
f2.rowconfigure(0, weight=1)
c1=Label(f2, text="feet to meter or meter to feet?")
c1.grid(row=0, column=0)
c2=Label(f2, text='type "ft" for feet to meter, type "m" for meter to feet')
c2.grid(row=1, column=0)
lol = StringVar()
choice=Entry(f2, width=4, textvariable=lol)
choice.grid(row=2, column=0)
b1=Button(f2, text="Confirm", command=choose)
b1.grid(row=3, column=0)

rip.mainloop()

rawDescription: unit converting program using python tkinter (trouble with creating windows)

description: Unit converting program using python tkinter (trouble with creating windows)


rawDescription: Byte Array in Python

description: Byte Array in Python


code:
import classtobetested
import unittest

import contextlib

@contextlib.contextmanager
def monkey_patch(module, fn_name, patch):
    unpatch = getattr(module, fn_name)
    setattr(module, fn_name)
    try:
        yield
    finally:
        setattr(module, fn_name, unpatch)

class TestTheClassToBeTested(unittest.TestCase):
    def test_with_fs_mocks(self):
        with monkey_patch(classtobetested.os.path,
                          'isfile',
                          lambda path: path == '/path/to/file'):
            self.assertTrue(classtobetested.testable())

rawDescription: How do I mock the filesystem in python unit tests

description: Mock the filesystem in python unit tests


code:
class MinimumBalanceAccount(BankAccount):
    def __init__(self, minimum_balance):
        self.minimum_balance = minimum_balance
        super(MinimumBalanceAccount, self).__init__()

rawDescription: Why dual __init__ in python?

description: Why dual __init__ in python


code:
now = datetime.datetime.utcnow()
exp_year = now.year + 1
exp_month = now.month
exp_day = now.day

if exp_month == 2 and exp_day == 29:
    exp_month += 1
    exp_day = 1

exp_date = now.replace(year=exp_year, month=exp_month, day=exp_day)

rawDescription: Datetime expiration next year

description: Datetime expiration next year


code:
r
r = r.strip()
r
r = r.split('\t')
r
r[1] = r[1].strip()
r

rawDescription: parsing objdump output

description: Parsing objdump output


code:
od = OrderedDict.fromkeys(key_order)
od.update(d)

rawDescription: Python OrderedDict: sort by tuple order?

description: Python OrderedDict: sort by tuple order


code:
import x
v = x.BarVec()
for i in range(10):
 b = x.Bar()
 b.data = i
 v.push_back(b)
f = x.Foo(v)
v2 = f.get()
v[5].data
v2[5].data
v[5].data = 7   # Change original vector data passed in.
v2[5].data      # vector from Foo also changes.
del v           # delete the original vector
v2              # proxy is still there
v2[0]           # <CRASH>

rawDescription: SWIG python: inject pointer on construction


code:
@app.route('/categories/<string:category_type>', methods=['GET'])
def categories(category_type):
    if category_type.lower() not in ('category', 'subcagory'):
        flask.abort(404)

    if request.method == 'GET':
        results = Categories.query.all()

        json_results = []
        for result in results:
            d = {'Category': getattr(result, category_type.lower())}
            json_results.append(d)

        return jsonify(items=json_results)

rawDescription: Flask API with variable output defined by url


code:
# core module

import sqlalchemy.types as types

class DBStatus (types.TypeDecorator):

    impl = types.Enum

    # what should happen with Status objects on the way into the table
    def process_bind_param(self, value, dialect):
        if value is None:
            return value
        return str(value)  # if Status has a __str__ or __repr__ method

    # what should happen with Enum objects on the way out of the table
    def process_result_value(self, value, dialect):
        if value is None:
            return value
        return Status(value)

foo_table = Table(
    'foo',
    MetaData(),
    Column('status', DBStatus('OK', 'Error')),
    # ...
)

rawDescription: SQLAlchemy: How can I add common mapping logic to my custom base class (using declarative style mapping)?


code:
Combine(Word(nums,exact=3) + '-' + Word(nums,exact=2) + '-' + Word(nums,exact=4))

['555-66-7777']

rawDescription: pyparsing - defining keywords - compare Literal, Word, Keyword and Combine

description: Pyparsing - defining keywords - compare Literal, Word, Keyword and Combine


rawDescription: how to sum across many columns with pandas groupby?

description: Sum across many columns with pandas groupby


rawDescription: get elements in "AND" in logic string with Python

description: Get elements in "AND" in logic string with Python


code: notebook["cells"]

rawDescription: How do I get more than one worksheet in an ipython notebook?

description: Get more than one worksheet in an ipython notebook


rawDescription: A function that would give the middle letters


code:
def numbers(s):
    return [int(match) for match in re.findall(r"\d+", s)]

rawDescription: Filtering text in python for numeric values


code:
array([3,-2]) #decreasing value
array([-2,-4,-7]) #decreasing value

rawDescription: Categorizing the list of array in python


code:
def newline_callback(matchobj):
    return re.sub(re.compile(r'^(.+)$', re.M),r'\1  ',matchobj.group(0))

text = re.sub(re.compile(r'(\A|^$\n)(^\w[^\n]*\n)(^\w[^\n]*$)+', re.M), newline_callback, text)

rawDescription: Translate github flavored markdown regex from ruby to python


code: self.nb.AddPage(pnl, nm, select=True)

rawDescription: wxPython auinotebook.GetSelection() return index to the first page

description: WxPython auinotebook.GetSelection() return index to the first page


code:
@conf.commands.register
def rdpcap(filename, count=-1):
    """Read a pcap file and return a packet list
count: read only <count> packets"""
    return PcapReader(filename).read_all(count=count)

rawDescription: Close rdpcap function - Scapy

description: Close rdpcap function - Scapy


code: df.index.names = [None]

rawDescription: Pandas reindexing data frame issue

description: Pandas reindexing data frame issue


code:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

while True:
    driver = webdriver.Firefox()
    driver.get('http://example.com')

    try:
        element = driver.find_element_by_id("user_first_name")
    except NoSuchElementException:
        driver.close()
        continue
    else:
        break

# do smth with the element

rawDescription: Selenium Python- If cannot find element, close everything and restart


code:
def func(): #comment myfunc():
   pass

rawDescription: Making Python code Pythonic

description: Making Python code Pythonic


code:
numpy.ma.array(m,mask=((m<0.5) | (m > 0.8))).argmin()
m.flat[23]

rawDescription: numpy argmin elegant solution required.

description: Numpy argmin elegant solution required.


rawDescription: Begineer's sorting with tuples

description: Begineer's sorting with tuples


rawDescription: How to generate random int around specific mean?

description: Generate random int around specific mean


code:
import lxml.html as lh

content='''

'''

doc=lh.fromstring(content)
for elt in doc.xpath("//a[img[@title='Go to next page']]"):
    print(elt.attrib['href'])

# StdResults.aspx

rawDescription: lxml: get element with a particular child element?

description: Lxml: get element with a particular child element


code: r = color()[0]

rawDescription: Is there some kind of tuple wildcart in Python?


code:
import netifaces as ni
import winreg as wr
from pprint import pprint

def get_connection_name_from_guid(iface_guids):
    iface_names = ['(unknown)' for i in range(len(iface_guids))]
    reg = wr.ConnectRegistry(None, wr.HKEY_LOCAL_MACHINE)
    reg_key = wr.OpenKey(reg, r'SYSTEM\CurrentControlSet\Control\Network\{4d36e972-e325-11ce-bfc1-08002be10318}')
    for i in range(len(iface_guids)):
        try:
            reg_subkey = wr.OpenKey(reg_key, iface_guids[i] + r'\Connection')
            iface_names[i] = wr.QueryValueEx(reg_subkey, 'Name')[0]
        except FileNotFoundError:
            pass
    return iface_names

x = ni.interfaces()
pprint(get_connection_name_from_guid(x))

rawDescription: How to get meaningful network interface names instead of GUIDs with netifaces under Windows?

description: Get meaningful network interface names instead of GUIDs with netifaces under Windows


rawDescription: Understanding factorize function

description: Understanding factorize function


code: df1['date'] = [d.strftime('%Y-%m-%d') if not pd.isnull(d) else '' for d in df1['date']]

rawDescription: Remove dtype datetime NaT

description: Remove dtype datetime NaT


code:
from redis import Redis
r = Redis()
r.lpush('task_queue', 'task1')

rawDescription: Python web app with background worker thread


code:
# For convenience of representation, assume pairs to be defined as
# tuples of form ( no_1, no_2 )

def closest_pair(array):
    l = len(array)
    pivot = l/2

    if len(array) == 2:
        # Simply returns the pair
        return (array[0], array[1])

    if len(array) == 3:
        # Returns the pair which has the smallest distance amongst the 3C2 pairs
        return min(itertools.combinations(array, r = 2), key = lambda x : abs(x[1] - x[0]) )

    left_closest = closest_pair(array[:pivot])
    right_closest = closest_pair(array[pivot:])
    split_pair = (array[pivot-1], array[pivot]) # Just incase the split_pair is the closest

    return min(left_closest, right_closest, split_pair, key = lambda x : abs(x[1] - x[0]) )

rawDescription: Couple of points that are closer to each other in a list


rawDescription: How to find index of previous data that is higher than current value with pandas?

description: Find index of previous data that is higher than current value with pandas


code:
{
    0: {
        'texts': ['TEXT HERE 1..............'],
        'id': 'ctl00_ContentPlaceHolder1_CaseDetailParties1_gvParties_ctl02_gvAttyInfo_ctl02_lblAppearanceInfo1'
    },
    1: {
        'texts': ['TEXT HERE 2..............'],
        'id': 'ctl00_ContentPlaceHolder1_CaseDetailParties1_gvParties_ctl02_gvAttyInfo_ctl02_lblAppearanceInfo2'
    },
    2: {
        'texts': ['TEXT HERE 3..............'],
        'id': 'ctl00_ContentPlaceHolder1_CaseDetailParties1_gvParties_ctl02_gvAttyInfo_ctl02_lblAppSpace'
    },
    3: {
        'texts': ['TEXT HERE 4..............'],
        'id': 'ctl00_ContentPlaceHolder1_CaseDetailParties1_gvParties_ctl02_gvAttyInfo_ctl03_lblAppearanceInfo1'
    },
    4: {
        'texts': ['TEXT HERE 5..............'],
        'id': 'ctl00_ContentPlaceHolder1_CaseDetailParties1_gvParties_ctl02_gvAttyInfo_ctl03_lblAppearanceInfo2'
    },
    5: {
        'texts': ['TEXT HERE 6..............'],
        'id': 'ctl00_ContentPlaceHolder1_CaseDetailParties1_gvParties_ctl02_gvAttyInfo_ctl03_lblAppSpace'
    },
    6: {
        'texts': ['TEXT HERE 7..............'],
        'id': 'ctl00_ContentPlaceHolder1_CaseDetailParties1_gvParties_ctl02_gvAttyInfo_ctl04_lblAppearanceInfo1'
    },
    7: {
        'texts': ['TEXT HERE 8..............'],
        'id': 'ctl00_ContentPlaceHolder1_CaseDetailParties1_gvParties_ctl02_gvAttyInfo_ctl04_lblAppearanceInfo2'
    },
    8: {
        'texts': ['TEXT HERE 9..............'],
        'id': 'ctl00_ContentPlaceHolder1_CaseDetailParties1_gvParties_ctl02_gvAttyInfo_ctl04_lblAppSpace'
    },
    9: {
        'texts': ['TEXT LINE 10.............', 'TEXT LINE 11.............', 'TEXT LINE 12.............', 'TEXT LINE 13.............'],
        'id': 'ctl00_ContentPlaceHolder1_CaseDetailParties1_gvParties_ctl02_gvAttyInfo_ctl02_lblAppearanceInfo1'
    }
}

rawDescription: lxml xpath - Get all text within span tags

description: Lxml xpath - Get all text within span tags


code:
test_instance = test(filepath)
test_instance.method()

rawDescription: Call Python class methods from the command line


code:
# without itertools
import numpy as np
import random
from pprint import pprint
import colorsys
hues, saturations, values = np.arange(0.0,1,0.05), np.arange(0.3,1.01,0.345), np.arange(0.3,1.01,0.345)
rlist= [colorsys.hsv_to_rgb(hue, saturation, value)
        for hue in random.sample(hues,len(hues))
        for saturation in random.sample(saturations, len(saturations))
        for value in random.sample(values, len(values))]
pprint(rlist)

rawDescription: Suggestions for writing this small piece of code more elegantly


code:
from datetime import datetime
a = {"ID":3, "CITY":"ATLANTIS", "FOUNDATION":datetime(2014,10,12), "COLLAPSE":datetime(2010,10,12), "REGENERATE":datetime(2011,10,12)}
min(a.iteritems(),
    key=lambda i: i[1] if isinstance(i[1], datetime) else datetime.max)
min(a, key=lambda k: a[k] if isinstance(a[k], datetime) else datetime.max)
min(a.itervalues(),
    key=lambda v: v if isinstance(v, datetime) else datetime.max)

rawDescription: How to get the item with max date value in a python dictionary?

description: Get the item with max date value in a python dictionary


code:
a = BitArray(bytearray([0, 0])) # two zero bytes (16 zero bits)
a.setbit(3, 1) # set bit 3 to 1
print(list(a.array))

rawDescription: Is there a correct way of creating a bit by bit binary string?


code:
rows = [0,1]
cols = [2,2]
a = np.arange(1,10).reshape((3,3))
a[rows,cols] = 0
a

rawDescription: how to modify a 2D numpy array at specific locations without a loop?

description: Modify a 2D numpy array at specific locations without a loop


code:
def get_values_flexibly(file, keyword):
    def process(func):
        return [func(cell)] + [func(row[index]) for row in reader]

    with open(file, 'r') as f:
        reader = csv.reader(f)
        first_row = reader.next()
        if keyword in first_row:
            return [row[first_row.index(keyword)] for row in reader]
        for index, cell in enumerate(reader.next()):
            if cell.endswith(' ' + keyword):
                return process(lambda cell: cell[:-len(keyword) - 1])
            elif cell.split(':')[0].strip() == keyword:
                return process(lambda cell: cell.split(':')[1].strip())

rawDescription: Calling data from a csv file with its header Python


rawDescription: groupby common values in two columns

description: Groupby common values in two columns


code:
import csv

fileLineList = []
averageScoreDict = {}

with open('Class1scores.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        fileLineList.append(row)

for row in fileLineList:
    highest = 0
    lowest = 0
    total = 0
    average = 0
    for column in row:
        if column.isdigit():
            column = int(column)
            if column > highest:
                highest = column
            if column < lowest or lowest == 0:
                lowest = column
            total += column
    average = total / 3
    averageScoreDict[row[0]] = [highest, lowest, round(average)]

print(averageScoreDict)

rawDescription: Formatting data in a CSV file (calculating average) in python


rawDescription: What is the correct syntax for using logical or boolean indexing with Pandas DataFrames?


code:
request = urllib.request.Request(
    "http://www.altenheim-adressen.de/schnellsuche/index.cfm",
    DATA)
# adding charset parameter to the Content-Type header.
request.add_header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
request.add_header("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0")
f = urllib.request.urlopen(request)

rawDescription: Trying to send a POST-request with urllib.request returns the same page


code:
for option1 in driver.find_elements_by_xpath("//select[@id='stuff']/option"):
    if option1.text == 'Team_02':
        option1.click()
        time.sleep(100)

rawDescription: Can't select an option

description: Can't select an option


code:
import foo
import shared

shared.shared.do_stuff("bar")

rawDescription: Good practice sharing resources between modules?


code:
s = "Hello(There|World!!"
re.split(r'[(|!]+', s)

rawDescription: How can I use re's special characters when splitting a string?

description: Use re's special characters when splitting a string


code: result = [s for s in (text1,text2,text3) if not re.search(r'[/,@]{2,}', s)]

rawDescription: Python regex: not more than one special symbol in a row


code:
for row in reader:
    if row['Presedence'] == 'Some Value':
        print(row['Presedence'], row['Sno'], row['STP-testcaseno'])
    else:
        print(row['Sno'], row['STP-testcaseno'])

rawDescription: store the values of a single row and its columns into different variables in python

description: Store the values of a single row and its columns into different variables in python


code:
from core import functions
from operations import actions

functions.core_function()
actions.simple_action()

rawDescription: python module organisation

description: Python module organisation


code:
from skimage import io
io.imshow(img_gray)

rawDescription: rgb2gray not working, creating rainbow image

description: Rgb2gray not working, creating rainbow image


code:
def addMatrix(matrix):
    # use ExcelFile("fileName.xlsx") if you need to add to a specific file
    excelFile = ExcelFile()
    excelFile.addWorksheet()
    # default excel files only have 3 sheets so had to add one
    excelFile.selectWorksheet("Sheet4")
    # xrange(4,513) since end index is exclusive
    for row in xrange(4,513):
        # 1 for A, 26 for Z
        for col in xrange(1,27):
            mRow = row - 4
            mCol = col - 1
            excelFile.insertIntoCell(row, col, matrix[mRow][mCol])
    excelFile.saveAs("test.xlsx")
    excelFile.close()

matrix = list()
for row in xrange(509):
    matrix.append([])
    for col in xrange(26):
        matrix[row].append(0)
# the matrix is now filled with zeros

# use openExcel(False) to run faster, it won't open a window while running
openExcel()
addMatrix(matrix)
closeExcel()

rawDescription: python write to EXCEL cell range (NumPy?)

description: Python write to EXCEL cell range (NumPy?)


code: a == a.default

rawDescription: Create current class default instance in its base class


code:
Company = {

'NAME1': {'M_or_F': 'Male',
             'AGE': '24',
            'DEPT': 'Finance',
     'Company Car': 'No'},

'NAME2': {'M_or_F': 'Male',
             'AGE': '52',
            'DEPT': 'Marketing',
     'Company Car': 'Yes'},

'NAME3': {'M_or_F': 'Female',
             'AGE': '36',
            'DEPT': 'Finance',
     'Company Car': 'Yes'},

'NAME4': {'M_or_F': 'Male',
             'AGE': '28',
            'DEPT': 'Finance',
     'Company Car': 'No'},

'NAME5': {'M_or_F': 'Female',
             'AGE': '23',
            'DEPT': 'HR',
     'Company Car': 'Yes'}
}

rawDescription: Directly calculating conditional averages in a Python Dictionary


code:
class Player(object):
    def __init__(self, name, hp, dmg, lvl, inventory, speed):
       self.name = name
       self.hp = hp
       self.dmg = dmg
       self.lvl = lvl
       self.inventory = inventory
       self.speed = speed

    def __str__(self):
        return "Health: %s Attack Damage: %s Level: %s Speed: %s" % (self.hp, self.dmg, self.lvl, self.speed)

link = Player("Link", 10, 3, 1, {}, 3)
print(link)

rawDescription: Printing in a class python

description: Printing in a class python


code:
foo.subject = 'Fred'
bar = foo
del foo
bar('runs')

rawDescription: how can python function access its own attributes?

description: How can python function access its own attributes


code:
import numpy as np
from pandas import DataFrame
from bokeh.plotting import figure, output_notebook, show, gridplot
from bokeh.models import ColumnDataSource, widgets
from bokeh.models.glyphs import Circle

def znzt_ts():#, plot_antisym=False, **kwargs):
    t = np.arange(1000)
    Eu = np.sin(t * np.pi/10) + np.random.random(1000)
    Ez = np.cos(t * np.pi/10) + np.random.random(1000)
    ts = DataFrame({'t': t, 'Eu': Eu, 'Ez': Ez})
    tools = 'box_zoom,pan,reset,save,box_select'
    source = ColumnDataSource(data=ts)
    original_source = ColumnDataSource(data=ts)

    selection_glyph = Circle(fill_color='firebrick', line_color=None)
    nonselection_glyph = Circle(fill_color='#1f77b4', fill_alpha=0.1, line_color=None)

    p1 = figure(plot_width=300, plot_height=300, tools=tools)
    r1 = p1.circle('t', 'Eu', source=source, line_color=None)
    r1.selection_glyph = selection_glyph
    r1.nonselection_glyph = nonselection_glyph

    p2 = figure(plot_width=300, plot_height=300, tools=tools)
    r2 = p2.circle('Eu', 'Ez', source=source, line_color=None)
    r2.selection_glyph = selection_glyph
    r2.nonselection_glyph = nonselection_glyph
    return gridplot([[p1, p2]])

gp = znzt_ts()
output_notebook()
show(gp)

rawDescription: How to set properties of selected/unselected glyphs in bokeh

description: Set properties of selected/unselected glyphs in bokeh


code:
import re
import timeit
def test(charsInString, charsInRegex):
    regex = re.compile('a{'+str(charsInRegex)+'}')
    string = 'a'*charsInString;
    for i in range(1, 200000):
        regex.match(string)
print(timeit.timeit("test(1, 1)", setup="from __main__ import test", number=1))
print(timeit.timeit("test(1, 2)", setup="from __main__ import test", number=1))
print(timeit.timeit("test(1, 5000)", setup="from __main__ import test", number=1))
print(timeit.timeit("test(4999, 5000)", setup="from __main__ import test", number=1))
print(timeit.timeit("test(5000, 5000)", setup="from __main__ import test", number=1))
print(timeit.timeit("test(50000, 5000)", setup="from __main__ import test", number=1))

rawDescription: Minimum lenght of string that match a regex


code: tmatrix(None, sample)

rawDescription: Calculate transition matrix of letters

description: Calculate transition matrix of letters


code:
person = Person()

person.balance = 0  # OK
person.balance = '0'  # OK
person.balance = 'wrong'  # TypeError

person.age = 30  # OK
person.age = '30'  # OK
person.age = -1  # ValueError
person.age = 'wrong'  # TypeError

rawDescription: Reusable validating class attributes

description: Reusable validating class attributes


code:
import numpy as np
d
map(np.sum, zip(*d.values()))

rawDescription: Sum of column elements of dictionary values made of lists in Python


code: send_mail("some message", "Scraper Report")

rawDescription: Sending e-mail after scrape in scrapy

description: Sending e-mail after scrape in scrapy


code:
def foo():
    return 5

def bar(x):
    return foo() + x

scope = {'scope': {'bar': bar, 'foo':foo}}
expression = "scope['bar'](42)"
print('eval({!r}, scope) returns {}'.format(expression, eval(expression, scope)))

rawDescription: How do I execute a function inside a namespace dict?

description: Execute a function inside a namespace dict


code:
import argparse
parser = argparse.ArgumentParser(description='This is my description')
parser.add_argument('--ipv4', action='store_true', dest='ipv4')

rawDescription: Python parse options without any argument using optparse module


code:
class MyTransformedDict(TransformedDict):

    def __keytransform__(self, key):
        return key.lower()

s = MyTransformedDict([('Test', 'test')])

assert s.get('TEST') is s['test']   # free get
assert 'TeSt' in s                  # free __contains__
                                    # free setdefault, __eq__, and so on

import pickle
assert pickle.loads(pickle.dumps(s)) == s
                                    # works too since we just use a normal dict

rawDescription: Python: How to "perfectly" override a dict


rawDescription: Python - sumif from csv

description: Python - sumif from csv


code:
import sys
import re
import requests
import json

wikiApiRoot = 'https://en.wikipedia.org/w/api.php'

# returns the position past the requested token or end of string if not found
def FindToken(text, token, start=0):
    pos = text.find(token, start)
    if -1 == pos:
        nextTokenPos = len(text)
    else:
        nextTokenPos = pos
    return nextTokenPos + len(token)

# Get the contents of the template as text
def GetTemplateText(wikitext, templateName):
    templateTag = '{{' + templateName

    startPos = FindToken(wikitext, templateTag)
    if (len(wikitext) <= startPos):
        # Template not found
        return None

    openCount = 1
    curPos = startPos
    nextOpenPos = FindToken(wikitext, '{{', curPos)
    nextClosePos = FindToken(wikitext, '}}', curPos)

    # scan for template's matching close braces
    while 0 < openCount:
        if nextOpenPos < nextClosePos:
            openCount += 1
            curPos = nextOpenPos
            nextOpenPos = FindToken(wikitext, '{{', curPos)
        else:
            openCount -= 1
            curPos = nextClosePos
            nextClosePos = FindToken(wikitext, '}}', curPos)

    templateText = wikitext[startPos:curPos-2]
    return templateText

def GetTemplateDict(title, templateName='Taxobox'):
    templateDict = None

    # Get data from Wikipedia:

    resp = requests.get(wikiApiRoot + '?action=query&prop=revisions&' +
        'rvprop=content&rvsection=0&format=json&redirects&titles=' +
        title)

    # Get the response text into a JSON object:

    rjson = json.loads(resp.text)

    # Pull out the text for the revision:

    wikitext = rjson['query']['pages'].values()[0]['revisions'][0]['*']

    # Parse the text for the template

    templateText = GetTemplateText(wikitext, templateName)

    if templateText:

        # Parse templateText to get named properties

        templateItemIter = re.finditer(
            r'\|\s*(\w*)\s*=\s*([^\n]*)\n',
            templateText,
            re.M)
        templateList = [item.groups([0,1]) for item in templateItemIter]
        templateDict = dict(templateList)

    return templateDict

def main():
    import argparse
    import pprint

    parser = argparse.ArgumentParser()
    parser.add_argument('title', nargs='?', default='Okapia_johnstoni', help='title of the desired article')
    parser.add_argument('template', nargs='?', default='Taxobox', help='name of the desired template')
    args = parser.parse_args()

    templateDict = GetTemplateDict(args.title, args.template)
    pprint.pprint(templateDict)

if __name__ == "__main__":
    main()

rawDescription: Parse Wikipedia Wikitext Template Named Parameters to Extract Data from Taxobox


rawDescription: Store additional keys in dict of dicts

description: Store additional keys in dict of dicts


code:
myList = [1, 2, 3, 4, 5]
myOtherList = myList
myOtherList == myList
myOtherList is myList

rawDescription: Python: List Equal List or For each in list Append


code:
ax = plt.subplot(111)
wedges, texts = ax.pie(np.abs(np.random.randn(5)))

for w in wedges:
    w.set_linewidth(2)
    w.set_edgecolor('cyan')

rawDescription: Changing line properties in matplotlib pie chart


code:
from Tkinter import Tk, Button
root = Tk()
def click():
    button.grid_remove()
button = Button(text="click", command=click)
button.grid()
root.mainloop()

rawDescription: Where to place the code to remove a tkinter widget from a window


code: [True, False, False, True, False, False, False, True, False, False]

rawDescription: check if numpy array is subset of another array

description: Check if numpy array is subset of another array


rawDescription: Protect commas on consecutive string.join() and string.split()


code: allNA = frame[(frame.A.isnull()) & (frame.B.isnull()) & (frame.C.isnull())]

rawDescription: pandas dataframe: index is DatetimeIndex and 4 columns. Some NaN

description: Pandas dataframe: index is DatetimeIndex and 4 columns. Some NaN


code:
# file: generate.py
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Alphabet import IUPAC
from Bio import SeqIO

long_string = "A" * 8000
outfile = open('long.fasta', 'w')

record = SeqRecord(Seq(
    long_string,
    IUPAC.protein),
    id="YP_025292.1", name="HokC",
    description="toxic membrane protein, small")

SeqIO.write(record, outfile, "fasta")

rawDescription: Biopython Large Sequence splitting

description: Biopython Large Sequence splitting


rawDescription: find small occurences of a substring in a string

description: Find small occurences of a substring in a string


code:
import json
t = (1, "a")
json.dumps(t)
json.loads(_)

rawDescription: How to get a variable from the shell in python?

description: Get a variable from the shell in python


rawDescription: reindex multiindex pandas dataframe

description: Reindex multiindex pandas dataframe


code:
while not_dead_yet:
    line = cmd.stdout.readline()

for line in iter(cmd.stdout.readline, b''):
    pass

rawDescription: Displaying output of shell commands with shared environments


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

data = np.random.normal(0, 1, 1000)

fig, ax = plt.subplots()
counts, bins, patches = plt.hist(data)

bin_centers = np.mean(zip(bins[:-1], bins[1:]), axis=1)
ax.plot(bin_centers, counts.cumsum(), 'ro-')

plt.show()

rawDescription: How to plot an ogive?

description: Plot an ogive


code:
# test_fake.py
import sys
sys.modules['y'] = __import__('z')

import x

rawDescription: How can I replace a module without importing it?

description: Replace a module without importing it


code:
a={u'is_l': 0, u'importance': 1, u'notes': u'12345', u'created_by': 1 }
needs=set(['verision','importance'])
needs.issubset(a)

rawDescription: python dictionary evaluates true if key is not present

description: Python dictionary evaluates true if key is not present


code:
def _generate_django_exception(e, source=None):
    '''Generate a Django exception from a Jinja source'''
    from django.views.debug import linebreak_iter

    if source:
        exception = DjangoTemplateSyntaxError(e.message)
        exception_dict = e.__dict__
        del exception_dict['source']

        '''Fetch the entire template in a string'''
        template_string = source[0].reload()

        '''Get the line number from the error message, if available'''
        match = re.match('.* at (\d+)$', e.message)

        start_index = 0
        stop_index = 0
        if match:
            '''Convert the position found in the stacktrace to a position
            the Django template debug system can use'''
            position = int(match.group(1)) + source[1][0] + 1

            for index in linebreak_iter(template_string):
                if index >= position:
                    stop_index = min(index, position + 3)
                    start_index = min(index, position - 2)
                    break
                start_index = index

        else:
            '''So there wasn't a matching error message, in that case we
            simply have to highlight the entire line instead of the specific
            words'''
            ignore_lines = 0
            for i, index in enumerate(linebreak_iter(template_string)):
                if source[1][0] > index:
                    ignore_lines += 1

                if i - ignore_lines == e.lineno:
                    stop_index = index
                    break

                start_index = index

        '''Convert the positions to a source that is compatible with the
        Django template debugger'''
        source = source[0], (
            start_index,
            stop_index,
        )
    else:
        '''No source available so we let Django fetch it for us'''
        lineno = e.lineno - 1
        template_string, source = django_loader.find_template_source(e.name)
        exception = DjangoTemplateSyntaxError(e.message)

        '''Find the positions by the line number given in the exception'''
        start_index = 0
        for i in range(lineno):
            start_index = template_string.index('\n', start_index + 1)

        source = source, (
            start_index + 1,
            template_string.index('\n', start_index + 1) + 1,
        )

    exception.source = source
    return exception

rawDescription: Jinja2 template compilation information

description: Jinja2 template compilation information


code: os.system("\"C:/t est/hello\"")

rawDescription: How do I used 2 quotes in os.system? PYTHON

description: Used 2 quotes in os.system? PYTHON


code:
text = linecache.getline(file, 1)
question, answer = text.split(",", 1)

rawDescription: Strings won't compare properly

description: Strings won't compare properly


code:
def prog_rownum(filename, ref):
    """ Return a list with the line numbers where ref appears in filename """
    with open(filename) as f:
        return [line_num for line_num, line in enumerate(f)
                if line.startswith(ref)]    # alternatively "if ref in line" checks anywhere

rawDescription: Function returns no value

description: Function returns no value


rawDescription: Python: trying to lower a string and remove non-alphanumeric characters aside from space


code: l = [ord(a) ^ ord(b) for a,b in zip(s1,s2)]

rawDescription: how to do bitwise exclusive or of two strings in python?

description: Do bitwise exclusive or of two strings in python


code:
# processing
# ======================================
df1 = df.resample('Q', how=sum)
labels = ['{}Q{}'.format(y,q) for y,q in zip(df1.index.year, df1.index.quarter)]
ax = df1.plot(kind='bar')
ax.set_xticklabels(labels)

rawDescription: easily make labels look nice pandas plotting

description: Easily make labels look nice pandas plotting


code:
class Logger(Filterer):
    ...
    def debug(self, msg, *args, **kwargs):
        """
        Log 'msg % args' with severity 'DEBUG'.

        To pass exception information, use the keyword argument exc_info with
        a true value, e.g.

        logger.debug("Houston, we have a %s", "thorny problem", exc_info=1)
        """
        if self.isEnabledFor(DEBUG):
            self._log(DEBUG, msg, args, **kwargs)

rawDescription: Should python debug statements etc be wrapped by if debug?


code:
result = []
for d in default:
    for k, v in varying.items():
        for x in v:
            result.append(dict(d.items() + [(k, x)]))

rawDescription: How to make a list formed by changing some defaults in multiple ways

description: Make a list formed by changing some defaults in multiple ways


code:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

big_number = 10000.0
width = 0.0001

ts = np.linspace(0, 10, 2000)
def f(X, t):
    dx0 = X[1]
    dx1 = -9.8
    dx1 += big_number / (1 + np.exp(X[0]/width))
    return [dx0, dx1]

with np.errstate(over='ignore'):
    # don't print overflow warning messages from exp(),
    # and limit the step size so that the solver doesn't step too deep
    # into the forbidden region
    X = odeint(f, [2, 0], ts, hmax=0.01)
plt.plot(ts, X[:, 0])
plt.show()

rawDescription: How to simulate bouncing ball? odeint not applicable?

description: Simulate bouncing ball? odeint not applicable


code: from django.conf import settings

rawDescription: Django - use reverse url mapping in settings


rawDescription: Python - XY array from 2 lists

description: Python - XY array from 2 lists


code:
def factory(description):
    if   description == "It's flat":  return Line(description)
    elif description == "It's spiky": return Triangle(description)
    elif description == "It's big":   return Rectangle(description)

rawDescription: Python virtual classes: doing it right?

description: Python virtual classes: doing it right


code: output = subprocess.check_output(cmd, shell=True)

rawDescription: Get min, max, and total length of wav files


code: c.execute("insert into medicoes values(?,?,?,?,?,?,?)" % keys)

rawDescription: Convert JSON to SQLite in Python - How to map json keys to database columns properly?


code:
try:
    cache
except NameError:
    cache = {}

rawDescription: Why does python's imp.reload() not delete old classes and functions?


rawDescription: Parent-child relationship efficiency

description: Parent-child relationship efficiency


code:
fig, ax = subplots(1)
ax.plot(arange(10),rand(10))
fig.tight_layout()

rawDescription: Preventing xticks from overlapping yticks


code:
bs = BeautifulSoup("sometext")
reg = re.compile(r'some')
elements = [e for e in bs.find_all('a') if reg.match(e.text)]

rawDescription: Beautifulsoup find element by text using `find_all` no matter if there are elements in it


code:
def filename_cleanup(name):
    filename = name.replace('{', '').replace('}', '')
    wordlist = filename.split(' ')
    extension = '.' + wordlist.pop()
    wordlist[-1] = '{' + wordlist[-1] + '}'
    os.rename(name, ' '.join(wordlist) + extension)

rawDescription: Rename files with Python

description: Rename files with Python


code:
df['YYYY-MO-DD HH-MI-SS_SSS'] = pd.to_datetime(df['YYYY-MO-DD HH-MI-SS_SSS'])
hour = pd.to_timedelta(df['YYYY-MO-DD HH-MI-SS_SSS'].dt.hour, unit='H')

rawDescription: Pandas: Average over the same hour

description: Pandas: Average over the same hour


code:
def callback():
    try:
       C = Characters.pop()  #popping the last one is more efficient than the first.
    except IndexError:
       return None

    return C()

rawDescription: Select unique value from list

description: Select unique value from list


code: [int(i) for i in Something]

rawDescription: Receive a string with values and return a list


code:
list1 = [dict(RL = 'rl1', shaders = [dict(shader = "shader1", geom = ['a','b']),
                                     dict(shader = "shader3", geom = ['q','p'])])]
list2 = [dict(RL = 'rl1', shaders = [dict(shader = "shader1", geom = ['c','d']),
                                     dict(shader = "shader3", geom = ['x','y'])])]
list3 = [dict(RL = 'rl1', shaders = [dict(shader = "shader2", geom = ['e','f'])])]
list4 = [dict(RL = 'rl2', shaders = [dict(shader = "shader2", geom = ['g','h'])])]
list5 = [dict(RL = 'rl2', shaders = [dict(shader = "shader2", geom = ['i','j'])]),
         dict(RL = 'rl3', shaders = [dict(shader = "shader4", geom = ['ab','cd'])])]

list_o_lists = [list1, list2, list3, list4, list5]

big_list_o_dicts = []
for x in list_o_lists:
    big_list_o_dicts.extend(x)

big_list_o_dicts.sort(key=lambda x: x['RL'])

new_list_o_lists = []

last_item = None
for item in big_list_o_dicts:
    if last_item and item['RL'] == last_item['RL']:
        for this_shader in item['shaders']:
            for last_shader in last_item['shaders']:
                if last_shader['shader'] == this_shader['shader']:
                    last_shader['geom'].extend(this_shader['geom'])
                    break
    else:
        last_item = item
        new_list_o_lists.append(last_item)

rawDescription: Joining a list of dicts

description: Joining a list of dicts


code: obj, method, result, conclusion = re.split(r"\B\s(?=[^\s:]+:)", subject)

rawDescription: Splitting paragraphs using python

description: Splitting paragraphs using python


rawDescription: How to tell when more than one index matches?

description: Tell when more than one index matches


code:
import scrapy
from craiglist.items import StackItem

class CraiglistSpider(scrapy.Spider):
    name = "craiglist_spider"
    allowed_domains = ["puertorico.craigslist.org"]
    start_urls = (
        'http://puertorico.craigslist.org/search/sya',
    )

    def parse(self, response):
        items = response.selector.xpath("//p[@class='row']")
        for i in items:
            item = StackItem()
            item['prices'] = i.xpath("./a/span[@class='price']/text()").extract()
            item['url'] = i.xpath("./span[@class='txt']/span[@class='pl']/a/@href").extract()

            yield item

rawDescription: Problems using Scrapy to scrape craigslist.org


code:
def exec_module(module, globals=None, locals=None):
    frame = sys._getframe(1)

    exec_globals = nvl(globals, {})
    copy_globals = exec_globals.copy()
    exec_globals.update(frame.f_globals)
    exec_globals.update(copy_globals)

    exec_locals = nvl(locals, {})
    copy_locals = exec_locals.copy()
    exec_locals.update(frame.f_locals)
    exec_locals.update(copy_locals)

    exec(compile(module.__loader__.get_source(module.__name__),
                 module.__file__, 'exec'), exec_globals, exec_locals)

rawDescription: Start SimpleHTTPServer in python

description: Start SimpleHTTPServer in python


code:
for ii in range(3):
    for ji in range(3):
        if ii < ji:
            continue
        pprint(sij.doit().xreplace({i: ii, j: ji}).replace(lambda v: v.base == U, replace_U))

rawDescription: Expand index notation equation using sympy


code: data[ numpy.where( data > 3 ) ]

rawDescription: Count all values in a matrix greater than a value