code:
class ValidatedMeta(type):
    def __new__(meta, name, bases, dct):
        validations = [Validation(f._validation_field_name, f._validation_code, f)
                       for f in dct.values if hasattr(f._validation_field_name)]
        dct["_validations"] = validations
        super(ValidatedMeta, meta).__new__(meta, name, bases, dct)

def apply_validation(field_name, code):
    def decorator(f):
        f._validation_field_name = field_name
        f._validation_code = code
        return f
    return decorator

def ValidatedObject(metaclass=ValidatedMeta):
    pass

class test(ValidatedObject):
    @apply_validation("_name", "oh no!")
    def name_validation(self, name):
        return name == "jacob"

rawDescription: Using a base class function that takes parameters as a decorator for derived class function


rawDescription: Check for duplicates in a python panda data structure


rawDescription: Merging a dataframe and a series on 2 columns using Pandas/Python


rawDescription: Match consecutive duplicate characters, ignoring case


code:
import unittest

class TestClassA(unittest.TestCase):
    def testOne(self):
        # test code
        pass

class TestClassB(unittest.TestCase):
    def testOne(self):
        # test code
        pass

class TestClassC(unittest.TestCase):
    def testOne(self):
        # test code
        pass

if __name__ == '__main__':
    test_classes_to_run = [TestClassA, TestClassC]

    loader = unittest.TestLoader()

    suites_list = []
    for test_class in test_classes_to_run:
        suite = loader.loadTestsFromTestCase(test_class)
        suites_list.append(suite)

    big_suite = unittest.TestSuite(suites_list)

    runner = unittest.TextTestRunner()
    results = runner.run(big_suite)

    # ...

rawDescription: How to Run multiple classes in Single Test Suite in Python Unit testing?


code: result = [i.b for i in [x,y,z] if i.a == 1]

rawDescription: Filter object returning an attribute

description: Filter object returning an attribute


code:
from bs4 import BeautifulSoup
import csv
import urllib.request
from collections import *

def parse_csv(content, delimiter = ';'):  ##We use here ";" to parse CSV because of the European way of dealing with excel-csv
  csv_data = []
  for line in content.split('\n'):
    csv_data.append( [x.strip() for x in line.split( delimiter )] ) # strips spaces also
  return csv_data

List_of_list_of_pairs=[]
List_of_pairs=[]

list_url=parse_csv(open('url.csv','rU').read())

for i in range(0,len(list_url)) :
    List_of_pairs = []

    url=str(list_url[i][0]) ## read URL from an array coming from an Url-CSV
    page=urllib.request.urlopen(url)
    soup_0 = BeautifulSoup(page.read(),"html.parser")
    restricted_webpage= soup_0.find( "div", {"id":"ingredients"} )
    readable_restricted=str(restricted_webpage)
    soup=BeautifulSoup(readable_restricted,"html.parser")

    trs = soup.find_all('tr')

    for tr in trs:
        tds = tr.find_all("td")

        try: #we are using "try" because the table is not well formatted. This allows the program to continue after encountering an error.
            Nutriments = str(tds[0].get_text().strip())
            print(Nutriments)
        # This structure $isolate the item by its column in the table and converts it into a string.
            Quantity = str(tds[1].get_text().strip())
            print(Quantity)
            Pair=[Nutriments,Quantity]
            List_of_pairs.append(Pair)

        except:
            print ("bad tr string")
            continue #This tells the computer to move on to the next item after it encounters an error
    List_of_list_of_pairs.append(List_of_pairs)

print(List_of_list_of_pairs)
dico = defaultdict(list)

for n,list_of_pairs in enumerate(List_of_list_of_pairs):
    for i,pairs in enumerate(list_of_pairs):
        if (len(pairs) == 2):
             cle = pairs[0]
             val = pairs[1]
             while (len(dico[cle]) < n):
                   dico[cle].append('ND')
             dico[cle].append(val)
for cle in dico:
    while (len(dico[cle]) < n):
            dico[cle].append('ND')

import csv
with open("dict2csv.csv", 'w',encoding='utf8') as outfile:
   csv_writer = csv.writer(outfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL,)

   for k,v in dico.items():
       csv_writer.writerow([k] + v)

rawDescription: How to create this kind of dictionary?

description: Create this kind of dictionary


code:
class Mixin(object):
    def __init__(self, *args, **kwargs):
        super(Mixin, self).__init__(*args, **kwargs)
        self.items = []

rawDescription: Initialize instance variable in a mixin

description: Initialize instance variable in a mixin


code:
index = (numpy.s_[:],)+t
x[index]

rawDescription: Numpy multidimensional array slicing

description: Numpy multidimensional array slicing


code:
s = "Restaurant_Review-g503927-d3864736-Reviews"
re.findall('[a-z]\d+', s)

rawDescription: Parsing a string to extract a delimited unit having an alphabetic starting character and and an unknown length


code:
def Latin1ToUnicodeDictReader(latin1_data, **kwargs):
    csv_reader = csv.DictReader(latin1_data, **kwargs)
    for row in csv_reader:
        yield {key: value.decode('iso-8859-1').encode('utf8') if value else value for key, value in row.iteritems()}

reader = Latin1ToUnicodeDictReader(csvfile, delimiter='\t')

rawDescription: Encoding a csvreader

description: Encoding a csvreader


rawDescription: reducing list based on fuzzy values in python

description: Reducing list based on fuzzy values in python


code:
def menu():
    print("MENU")
    print("1) Test")
    print("2) Quit")
    choice = int(input("\nChoose an option : "))
    return choice

while choice != 2 :
    choice = menu()
    if choice == 1 :
        do_this
    elif choice == 2 :
        print("This program will terminate.")
        break
else :
    print("Invalid option... ")

rawDescription: Using return in a function to assign a choice value in a menu


code:
class single_return_format(object):

    def __init__(self, func):
        self._func = func

    def __get__(self, instance, owner):
        return SimpleFormMethod(instance, self._func)

class SimpleFormMethod(object):

    def __init__(self, instance, func):
        self._instance = instance
        self._func = func

    def __call__(self, *args, **kwargs):
        return self._func(self._instance, *args, **kwargs)

    @property
    def standard_format(self):
        return lambda *args, **kwargs: (self._func(self._instance, *args, **kwargs), )

class _SomeClass(object):

    def __init__(self):
        self._amount_to_add = 1

    @single_return_format
    def add_one(self, x):
        return x+self._amount_to_add

obj = _SomeClass()
assert obj.add_one(3) == 4
assert obj.add_one.standard_format(3) == (4, )

rawDescription: Using Python decorators to add a method to a method


rawDescription: Python : Accessing Column Values in multi dimension array


code:
random.choice([1, 2, 3])
random.choice([1, 2, 3])
random.choice([1, 2, 3])
random.choice([1, 2, 3])

rawDescription: How to select a random value in a dictionary?

description: Select a random value in a dictionary


code: subprocess.call(['lp', 'foo.png'])

rawDescription: how to connect to a printer and print a png image using python and PIL library in Ubuntu

description: Connect to a printer and print a png image using python and PIL library in Ubuntu


rawDescription: Python garbage collection

description: Python garbage collection


code:
with open(tmpfile, "wb") as tmp:
    shutil.copyfileobj(gzip.open(zipfile), tmp)

rawDescription: Best (most "pythonic") way to temporarily unzip a file


code: set(tuple(sorted(elt)) for elt in example)

rawDescription: Need an easy way to remove duplicates of nested tuples in python


code:
import csv
ls = [['www.google.com','9','1'],['www.foo.com','177','43432'],['http://www.test.com','2132','4567'],['http://www.stackoverflow.com','8','9']]
file = open('test.csv', 'w');
writer = csv.writer(file)
for item in ls:
    writer.writerow(item)
file.close()

rawDescription: How to append to a TSV file using the output of a Python script

description: Append to a TSV file using the output of a Python script


code:
import csv

myfile = open(..., 'wb')
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(mylist)

rawDescription: Create a .csv file with values from a Python list


code:
import time
...
time.sleep(5)

rawDescription: Iterating a loop with a pause

description: Iterating a loop with a pause


code:
class TicketDisplay(LoginRequiredMixin, DetailView):
    model = Ticket
    template_name = 'ticket_detail.html'
    context_object_name = 'ticket'
    slug_field = 'slug'

    def get_context_data(self, **kwargs):
        context = super(TicketDisplay, self).get_context_data(**kwargs)
        context['form_add_comment'] = CommentForm()
        return context

class TicketCommentCreate(LoginRequiredMixin, FormView):
    form_class = CommentForm
    template_name = 'ticket_detail.html'

    def get_success_url(self):
        return reverse('ticket_detail', kwargs={'slug' : self.kwargs['slug']} )

    def form_valid(self, form):
        slug = self.kwargs.get('slug', None)
        ticket = get_object_or_404(Ticket, slug=slug)
        comment = form.save(commit=False)
        comment.author_login = ticket.author
        comment.ticket = ticket
        comment.save()
        ticket.save() #Update Date
        return HttpResponseRedirect(self.get_success_url())

class TicketDetail(LoginRequiredMixin, View):

    def get(self, request, *args, **kwargs):
        view = TicketDisplay.as_view()
        return view(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        view = TicketCommentCreate.as_view()
        return view(request, *args, **kwargs)

rawDescription: Two Class Based View on the same url

description: Two Class Based View on the same url


code:
def block_diag(*arrs):
    """Create a block diagonal matrix from the provided arrays.

    Given the inputs `A`, `B` and `C`, the output will have these
    arrays arranged on the diagonal::

        [[A, 0, 0],
         [0, B, 0],
         [0, 0, C]]

    If all the input arrays are square, the output is known as a
    block diagonal matrix.

    Parameters
    ----------
    A, B, C, ... : array-like, up to 2D
        Input arrays.  A 1D array or array-like sequence with length n is
        treated as a 2D array with shape (1,n).

    Returns
    -------
    D : ndarray
        Array with `A`, `B`, `C`, ... on the diagonal.  `D` has the
        same dtype as `A`.

    References
    ----------
    .. [1] Wikipedia, "Block matrix",
           http://en.wikipedia.org/wiki/Block_diagonal_matrix

    Examples
    --------
    >>> A = [[1, 0],
    ...      [0, 1]]
    >>> B = [[3, 4, 5],
    ...      [6, 7, 8]]
    >>> C = [[7]]
    >>> print(block_diag(A, B, C))
    [[1 0 0 0 0 0]
     [0 1 0 0 0 0]
     [0 0 3 4 5 0]
     [0 0 6 7 8 0]
     [0 0 0 0 0 7]]
    >>> block_diag(1.0, [2, 3], [[4, 5], [6, 7]])
    array([[ 1.,  0.,  0.,  0.,  0.],
           [ 0.,  2.,  3.,  0.,  0.],
           [ 0.,  0.,  0.,  4.,  5.],
           [ 0.,  0.,  0.,  6.,  7.]])

    """
    if arrs == ():
        arrs = ([],)
    arrs = [np.atleast_2d(a) for a in arrs]

    bad_args = [k for k in range(len(arrs)) if arrs[k].ndim > 2]
    if bad_args:
        raise ValueError("arguments in the following positions have dimension "
                            "greater than 2: %s" % bad_args)

    shapes = np.array([a.shape for a in arrs])
    out = np.zeros(np.sum(shapes, axis=0), dtype=arrs[0].dtype)

    r, c = 0, 0
    for i, (rr, cc) in enumerate(shapes):
        out[r:r + rr, c:c + cc] = arrs[i]
        r += rr
        c += cc
    return out

rawDescription: Diagonal stacking in numpy?

description: Diagonal stacking in numpy


code:
tree = ET.ElementTree(file='pricelist.xml')
root = tree.getroot()

for elem in tree.iterfind('pricelist/item'):
    price = elem.findtext('price')
    newprice = float(price.replace(",", ".")) * 1.2

    newtier = ET.Element("item", qty="10", price="%.2f" % newprice)
    elem.find('price_qty').insert(0, newtier)

tree.write('pricelist.xml', "UTF-8")

rawDescription: Modify XML file using ElementTree

description: Modify XML file using ElementTree


code:
for line in TEST_DATA:
    if re.match(my_pattern, line):
        print(
            re.sub(
                r'(\d{1,3})[- ](\d{2,3})[- ]+(\d{5,10})',
                r'CountryCode=\1,LocalAreaCode=\2,Number=\3',
                line))

rawDescription: Python re returning non-matching lines

description: Python re returning non-matching lines


code: array[n-i:-i or None]

rawDescription: python dynamic array access [:0]

description: Python dynamic array access [:0]


code: str(int(x) + 1).zfill(len(x))

rawDescription: How to increment a value with leading zeroes?

description: Increment a value with leading zeroes


code:
attachment_obj = oerp.browse('ir.attachment', 17)
attachment_data = attachment_obj.datas

rawDescription: How to download attachment from OERP using oerplib

description: Download attachment from OERP using oerplib


code:
for line in search_file:
    if search_registration in line:
        str1 = line;
for line in search_av_speed_file:
    if search_registration in line:
         current_line = line.split(",")
         speed_of_car = current_line[2]
         print(speed_of_car)
         str2 = speed_of_car
fstr=" ".join(str1,str2) #further formatting can be done here,like strip() and you can print this to see the desired result
fine_file.write(fstr)

rawDescription: Removing newline at the end of a variable read from .txt file


code:
import requests

response = requests.request('POST' if self.data else 'GET', url=self.url,
                             headers=self.headers, data=self.data or None,
                             timeout=self.timeout, proxies=self.proxies, verify=False)

rawDescription: Python Requests - Why doesn't POST fall back to GET in case of no POST data?


code:
txtFiles = sc.textFile("temp/txtFiles/*") \
    .map(lambda x: x.split("\t"))
asDouble = txtFiles.map(lambda y: None if x == 'Nan' \
    else float(x) for x in y)
lblPt = asDouble.map(lambda x: LabeledPoint(x[70], x[0:69]))

rawDescription: Use MLLib in Spark with Missing Values

description: Use MLLib in Spark with Missing Values


code:
df2.index = df2.labelcol
df2.plot(kind='bar', stacked=True)

rawDescription: How to change xlabel in pandas matplotlib stacked bar

description: Change xlabel in pandas matplotlib stacked bar


rawDescription: Extract int from string in Pandas?

description: Extract int from string in Pandas


code:
from contextlib import closing
from socket import create_connection
from concurrent.futures import ThreadPoolExecutor, wait

host, port = 'www.example.com', 80
path = '/path/to/resource.html'

def spam_it():
    with closing(create_connection((host, port))) as sock:
        sock.sendall('GET {} HTTP/1.0\n\n'.format(path))

with ThreadPoolExecutor(max_workers=16) as executor:
    wait(executor.submit(spam_it) for _ in range(10000))

rawDescription: Hit a url multiple times

description: Hit a url multiple times


code:
from operator import attrgetter

recent_posts = Post.objects.order_by('-created')[:10]
recent_comments = Comment.objects.order_by('-created')[:10]
both_combined = list(recent_posts) + list(recent_comments)
both_sorted = sorted(both_combined, key=attrgetter('created'), reverse=True)
most_recent = both_sorted[:10]

rawDescription: Database Selection and Datetime Sorting (real-time Python dashboard)


code:
import cssutils
css = cssutils.parseFile('index.html')
for rule in css.cssRules:
    print(rule.style.top)
    print(rule.style.left)

rawDescription: Beautifulsoup css data extraction

description: Beautifulsoup css data extraction


code:
import turtle   # don't pollute the namespace
import time

def getInt(msg):
    return int(raw_input(msg))

def drawBall():
    turtle.down()
    for i in range(96):
        turtle.right(105)
        turtle.forward(100)
    turtle.up()

def moveOver():
    turtle.goto(120,0)

def Done():
    turtle.goto(0,-50)
    turtle.write('Done!')
    time.sleep(5)

def main():
    turtle.color('red','blue')
    for i in range(getInt('How many balls do you want?')):
        drawBall()
        moveOver()
    Done()

if __name__=="__main__":
    main()

rawDescription: python alternative to the goto command?

description: Python alternative to the goto command


code:
#first create empty appended_data table to store the info.
appended_data = []

for WorkingFile in os.listdir('C:\ExcelFiles'):
     if os.path.isfile(WorkingFile):

        # Import the excel file and call it xlsx_file
        xlsx_file = pd.ExcelFile(WorkingFile)
        # View the excel files sheet names
        xlsx_file.sheet_names
        # Load the xlsx files Data sheet as a dataframe
        df = xlsx_file.parse('sheet1',header= None)

        #.... do so reformating, call finished sheet reformatedDataSheet
        reformatedDataSheet
        appended_data.append(reformatedDataSheet)
appended_data = pd.concat(appended_data)

rawDescription: Looping through multiple excel files in python using pandas


rawDescription: String Division in Python

description: String Division in Python


code:
urlpatterns = patterns('',
    url(r'^browse/(?P<match>.+)/$', 'app.views.view', name='model_browse'),
)

def view(request, match):
    pieces = match.split('/')
    # even indexed pieces are the names, odd are values
    ...

rawDescription: Django: Arbitrary number of unnamed urls.py parameters


code:
string1 = "1 || awdawd@awdawd.com || awlkdjawldkjalwdkda"
list1 = string1.split("||")
list1
list1[1].strip()

rawDescription: How do I extract a certain part of a string in Python?

description: Extract a certain part of a string in Python


code:
import random
import sqlite3

def init():
    db = sqlite3.connect('Filmdatabase.db')
    db.execute('DROP TABLE IF EXISTS randomcode')
    db.execute('CREATE TABLE randomcode(code INT, name TEXT)')

def insert_random():
    db = sqlite3.connect('Filmdatabase.db')
    while True:
        randomcode = random.randint(100000, 999999)
         ###Attention the EXISTS statement
        cursor = db.execute('SELECT EXISTS(SELECT * FROM randomcode where code=?)', (randomcode,))
        if cursor.fetchone()[0] == 0:
            break
    db.execute('INSERT INTO randomcode (code, name) VALUES (?, ?)', (randomcode, 'Jay'))
    db.commit()

def check():
    db = sqlite3.connect('Filmdatabase.db')
    cursor = db.execute('SELECT * FROM randomcode')
    for i in cursor:
        print(i)

init()
for i in range(0, 10): #insert as many as you want.
    insert_random()
check()

rawDescription: sqlite python if already exist?

description: Sqlite python if already exist


code:
what = str(9) # whatever you are searching for
count = 0
for name,age in array:
    if age == what:
        count += 1

print("there are",count,"people with the age",what)

rawDescription: list.count(x) in 2D list python

description: List.count(x) in 2D list python


code:
import time, threading, thread

def watchdog_timer(state):
    time.sleep(3)
    if not state['completed']:
        thread.interrupt_main()

def run_example():
    while True:
        state = {'completed': False}
        watchdog = threading.Thread(target=watchdog_timer, args=(state,))
        watchdog.daemon = True
        watchdog.start()
        try:
            example()
            state['completed'] = True
        except KeyboardInterrupt:
            # this would be the place to log the timeout event
            pass
        else:
            break

rawDescription: Timeout function if it takes too long

description: Timeout function if it takes too long


code:
import wx

class F(wx.Frame):
    def __init__(self, *args, **kw):
        wx.Frame.__init__(self, None)
        style = wx.TE_MULTILINE|wx.BORDER_SUNKEN|wx.TE_READONLY|wx.TE_RICH2
        self.status_area = wx.TextCtrl(self, -1,
                                       pos=(10, 270),style=style,
                                       size=(380,150))
        self.status_area.AppendText("blahblahhblah")
        fg = wx.Colour(200,80,100)
        at = wx.TextAttr(fg)
        self.status_area.SetStyle(3, 5, at)

app = wx.PySimpleApp()
f = F()
f.Show()
app.MainLoop()

rawDescription: sending colored text to a TextCtrl in wxpython

description: Sending colored text to a TextCtrl in wxpython


code:
array=[[10], [15, 20]]
for r_index, row in enumerate(array):
    for c_index, number in enumerate(row):
            if number % 10 == 0:
                array[r_index][c_index] = [10, 20, 30]
            else:
                array[r_index][c_index] = [40]
print(array) #array = [[[10,20,30]],[[40],[10,20,30]]]

rawDescription: How to replace a value in a list of lists with another list?

description: Replace a value in a list of lists with another list


code:
from collections import OrderedDict
# regular unsorted dictionary
d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}
# dictionary sorted by key
OrderedDict(sorted(d.items(), key=lambda t: t[0]))
# dictionary sorted by value
OrderedDict(sorted(d.items(), key=lambda t: t[1]))
# dictionary sorted by length of the key string
OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))

rawDescription: Preserving python dictionary order - (Continuation of python dict )


code:
from itertools import cycle

import matplotlib.pyplot as plt

x = [1,5,7,3]
y = [[1,2,3,4], [2,3,1,4], [1,2,2,1], [2,3,1,1]]
for i in zip(x, y):
    b = zip(*(zip(cycle([i[0]]), i[1])))
    plt.scatter(*b)

plt.show()

rawDescription: Python: Scatter plot of data stored in matrix form


code:
def foo():
    pass

def bar():
    pass

import inspect
[member.__name__ for member in globals().values() \
                 if inspect.isfunction(member)]

rawDescription: List of all functions used in python program as string


code:
NRZ = [0,0,1,0,1,1,0,1]
NRZS = []

state = NRZ[0]
for bit in NRZ:
    state ^= bit ^ 1
    NRZS.append(state)

rawDescription: NRZ-S with Python

description: NRZ-S with Python


code:
import os
from unittest import TestCase

class MyTest(TestCase):
    def setUp(self):
        if not os.path.exists('configfile'):
            return self.skipTest('config file not found')

    def test01(self):
        # Do something with the file
        with open('configfile') as fd:
            self.assertEqual(fd.readlines().__len__(), 0)

rawDescription: How can I include additional tests if a test passes?

description: Include additional tests if a test passes


code:
_sentinel = object()
def next(it, default=_sentinel):
    try:
        return it.next()
    except StopIteration:
        if default is _sentinel:
            raise
        return default

rawDescription: import next() python 2.5

description: Import next() python 2.5


code:
# coding=utf-8
data = u"""\

     ৳
     24,000.00

"""

import bs4
soup = bs4.BeautifulSoup(data)
for price in soup.find_all('span', dir="ltr"):
    print(price.get_text())

rawDescription: BeautifulSoup4 data extract from HTML5 data-* tag


code:
def recurse_keys(df):
    rates = []
    for key, value in df.items():
        if key == 'rate':
            rates.append(value)
        if isinstance(df[key], dict):
            rates += recurse_keys(df[key])
    return rates

rawDescription: Append to list while in recursive loop

description: Append to list while in recursive loop


code:
from zipfile import ZipFile
from StringIO import StringIO

inMemoryOutputFile = StringIO()

zipFile = ZipFile(inMemoryOutputFile, 'w')
zipFile.writestr('OEBPS/content.xhtml', 'hello world')
zipFile.close()

inMemoryOutputFile.seek(0)

rawDescription: How to create an in-memory zip file with directories without touching the disk?

description: Create an in-memory zip file with directories without touching the disk


code:
with open('myCSV.csv','rb+') as f:
    while True:
        line = f.readline()
        if not line: break #Break of While loop when reaching EOF(End Of File)
        f.seek(-len(line),1) #Set current file position to beginning of current line
        line = line[:2] + '-' + line[2:] #modify your string
        f.write(line) #write modified line
        f.flush() #make the write happen

rawDescription: Adding Hyphen in row data in python

description: Adding Hyphen in row data in python


code:
import sys

def convert_type(ss):
    try:
        return int(ss)
    except ValueError:
        try:
            return float(ss)
        except ValueError:
            return ss

def parse_file(ff):
    out={}
    block=None
    for i,line in enumerate(ff):
        #Allow for comments to start with '#'.  We break off anything after a '#'
        #and ignore it.  After that, we
        data=line.split('#',1)
        line=data[0]  #comments (if in line) are in data[1] ... ignore those.
        line=line.strip() #remove whitespace from front and back of line.
        if(line.startswith('*')):
            #python supports multiple assignment.
            #e.g. out['header'] is the same object as block.
            #     changing block also changes out['header']
            block=out[line.strip()[1:]]=[]
        elif (block is not None) and line: #checks to make sure there is an active block and the line wasn't empty.
            #If the file could also have floats, you should use float instead of int
            #We also put the parsing in a try/except block.  If parsing fails (e.g. a
            #element can't be converted to a float, you'll know it and you'll know the
            #line which caused the problem.)
            try:
                #block.append(map(int,line.split()))
                block.append(map(convert_type,line.split()))
            except Exception:
                sys.stderr.write("Parsing datafile choked on line %d '%s'\n"%(i+1,line.rstrip()))
                raise
    return out

with open('textfile.txt','r') as f:
    data_dict=parse_file(f)

#get information from '*load' block:
info=data_dict['load']
for row in info:
    a,b,c=row
    ##same as:
    #a=row[0]
    #b=row[1]
    #c=row[2]
    ##as long as row only has 3 elements.

    #Do something with that particular row.
    #(each row in the 'load' block will be visited once in this loop)

#get info from stiffness block:
info=data_dict['stiffness']
for row in info:
    pass #Do something with this particular row.

rawDescription: Finding a certain block of code in text

description: Finding a certain block of code in text


code:
FIELD_NAME_ID = "ID"
FIELD_NAME_USER = "User"

FIELD_NAME_LIST = [FIELD_NAME_ID, FIELD_NAME_USER]

rawDescription: How to set python class attribute to value of another, previously defined class attribute in class definition?

description: Set python class attribute to value of another, previously defined class attribute in class definition


code:
from matplotlib.patches import Circle
circ = Circle((1, 2), radius=1)
circ.center == (1,2) #should return True

rawDescription: Center of a circle patch in Matplotlib

description: Center of a circle patch in Matplotlib


code:
DIRECT_ACCESS = re.compile(r'^/media/.*\.(js|css|png|gif|jpg)$')

...

if DIRECT_ACCESS.match(url):
    ...

rawDescription: How to make a python regex?

description: Make a python regex


code:
ylim = axes.get_ylim()
new_ylim = (min(ylim[0],-1), max(ylim[1], 1) )
axes.set_ylim(new_ylim)

rawDescription: Setting minimum upper y-lim and maximum lower y-lim in matplotlib


code:
seqm = difflib.SequenceMatcher()

for i in arrayC:
  origC, cleanC, caccount, ccode = i
  seqm.set_seq2(cleanC)

  bestRatio = 0

  for j in arrayA:
    origA, cleanA = j[:2]
    seqm.set_seq1(cleanA)

    if (seqm.real_quick_ratio() >= bestRatio and
        seqm.quick_ratio() >= bestRatio):
      r = seqm.ratio()
      if r >= bestRatio:
        bestRatio = r
        bestA = j

  if bestRatio >= 0.75: # the cutoff from the original get_close_matches() call
    origA, cleanA, aaccount, acode = bestA

    filteredratio = bestRatio
    strfilteredratio = '%.2f' % (filteredratio*100)

    seqm.set_seqs( origC, origA )
    unfilteredratio = seqm.ratio()
    strunfilteredratio = '%.2f' % (unfilteredratio*100)

    averageratio = (filteredratio+unfilteredratio)/2
    straverageratio = '%.2f' % (averageratio*100)

    row = [origC.rstrip(),origA.rstrip(),cleanC.rstrip(),cleanA.rstrip(),caccount,ccode,aaccount,acode,strfilteredratio,strunfilteredratio,straverageratio]
  else:
    row = ["NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","0.00","NULL","NULL"]

  Match75.writerow(row)

rawDescription: Feeding a List of Lists into a function

description: Feeding a List of Lists into a function


code:
from kivy.core.window import Window
from kivy.clock import Clock
from kivy.factory import Factory
from kivy.lang import Builder

Builder.load_string('''
<InfoBubble@Bubble>
    # declare our message StringProperty
    message: 'empty message'
    # let the bubble be of 200 device pixels
    # and expand as necessary on the height
    # depending on the message + 20 dp of padding.
    size_hint: None, None
    show_arrow: False
    pos_hint: {'top': 1, 'right': 1}
    size: dp(200), lbl.texture_size[1] + dp(20)
    Label:
        id: lbl
        text: root.message
        # constraint the text to be displayed within
        # the bubble width and have it be unrestricted
        # on the height.
        text_size: root.width - dp(20), None
''')

def bubbprint(self, message):
    message = repr(message)
    if not self.info_bubble:
        self.info_bubble = Factory.InfoBubble()
    self.info_bubble.message = message

    # Check if bubble is not already on screen
    if not self.info_bubble.parent:
        Window.add_widget(self.info_bubble)

    # Remove bubble after 2 secs
    Clock.schedule_once(lambda dt:
        Window.remove_widget(self.info_bubble), 2)

rawDescription: see output of print statements on android using kivy - kivy launcher

description: See output of print statements on android using kivy - kivy launcher


code:
@staticmethod
def time_method(method):
    def __wrapper(self, *args, **kwargs):
        if hasattr(self, '_being_timed'):
            # We're being timed already; just run the method
            return method(self, *args, **kwargs)
        else:
            # Not timed yet; run the timing code
            self._being_timed = True  # remember we're being timed
            try:
                start = time.time()
                result = method(self, *args, **kwargs)
                finish = time.time()
                sys.stdout.write('instancemethod %s took %0.3f s.\n' %(
                    method.__name__, (finish - start)))
                return result
            finally:
                # Done timing, reset to original state
                del self._being_timed
    return __wrapper

rawDescription: Python __metaclass__ inheritance issue

description: Python __metaclass__ inheritance issue


code: circleRect = pygame.draw.circle(windowSurface, (0,0,0), (10, 10), 5)

rawDescription: pygame.Rect around circle

description: Pygame.Rect around circle


rawDescription: Python: converting tuple into 2D array

description: Python: converting tuple into 2D array


code:
params = get_params_from_file_or_somewhere_else() # wherever you have the params.

qmarks = ', '.join('?' for param in params) # string with the "right" number of "?"

sql = 'SELECT * FROM mf WHERE col2 IN ({});'.format(qmarks)

cursor.execute(sql, params)

rawDescription: Unspecified number of variables in an sql script in python


code:
df = DataFrame(np.random.rand(5,3), index=[0, 1, 2, 4, 5])
df.index.to_series()
s = df.index.to_series()
1./s.diff().mean()

rawDescription: Operations on Pandas DataFrame Index

description: Operations on Pandas DataFrame Index


code:
#!/usr/bin/env python

# Copyright (C) 2015 Harvey Chapman <hchapman@3gfp.com>
# Public Domain
# Use at your own risk.

"""
Splits a gpx file with breaks in the track into separate files.

Based on: http://stackoverflow.com/q/33803614/47078
"""

import sys
import re
import os
from datetime import datetime, timedelta
from itertools import izip
from xml.etree import ElementTree

ns = { 'gpx': 'http://www.topografix.com/GPX/1/1' }

def iso8601_to_datetime(datestring):
    d = datetime(*map(int, re.split('\D', datestring)[:-1]))
    # intentionally ignoring timezone info (for now)
    # d = d.replace(tzinfo=UTC)
    return d

def datetime_from_trkpt(trkpt):
    datestring = trkpt.find('gpx:time', ns).text
    return iso8601_to_datetime(datestring)

def delta_too_large(trkpt1, trkpt2):
    delta = datetime_from_trkpt(trkpt2) - datetime_from_trkpt(trkpt1)
    return delta > timedelta(seconds=2)

def trkpt_groups(trkpts):
    last_index = 0
    for n, (a,b) in enumerate(izip(trkpts[:-1], trkpts[1:]), start=1):
        if delta_too_large(a,b):
            yield last_index, n
            last_index = n
    yield last_index, len(trkpts)

def remove_all_trkpts_from_trkseg(trkseg):
    trkpts = trkseg.findall('gpx:trkpt', ns)
    for trkpt in trkpts:
        trkseg.remove(trkpt)
    return trkpts

def add_trkpts_to_trkseg(trkseg, trkpts):
    # not sure if this will be slow or not...
    for trkpt in reversed(trkpts):
        trkseg.insert(0, trkpt)

def save_xml(filename, index, tree):
    filename_parts = os.path.splitext(filename)
    new_filename = '{1}_{0}{2}'.format(index, *filename_parts)
    with open(new_filename, 'wb') as f:
        tree.write(f,
                   xml_declaration=True,
                   encoding='utf-8',
                   method='xml')

def get_trkseg(tree):
    trk = tree.getroot().findall('gpx:trk', ns)
    if len(trk) > 1:
        raise Exception("Don't know how to parse multiple tracks!")
    trkseg = trk[0].findall('gpx:trkseg', ns)
    if len(trkseg) > 1:
        raise Exception("Don't know how to parse multiple track segment lists!")
    return trkseg[0]

def split_gpx_file(filename):
    ElementTree.register_namespace('', ns['gpx'])
    tree = ElementTree.parse(filename)
    trkseg = get_trkseg(tree)
    trkpts = remove_all_trkpts_from_trkseg(trkseg)
    for n, (start,end) in enumerate(trkpt_groups(trkpts)):
        # Remove all points and insert only the ones for this group
        remove_all_trkpts_from_trkseg(trkseg)
        add_trkpts_to_trkseg(trkseg, trkpts[start:end])
        save_xml(filename, n, tree)

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print >> sys.stderr, "Usage: {} file.gpx".format(sys.argv[0])
        sys.exit(-1)
    split_gpx_file(sys.argv[1])

rawDescription: Splitting file based on data comparison

description: Splitting file based on data comparison


rawDescription: Is it possible to index nested lists using tuples in python?


code:
data=[]

for index,line in enumerate(fr):
    if line.startswith('TMP'):# neglect other text and start with line containing TMP
      for i in range(4):
        data.append(fr[index+i+1])

rawDescription: Read a specific section of a file

description: Read a specific section of a file


code:
from PIL import Image

im = Image.open("logo_256.png")

# PIL complains if you don't load explicitly
im.load()

# Get the alpha band
alpha = im.split()[-1]

im = im.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255)

# Set all pixel values below 128 to 255,
# and the rest to 0
mask = Image.eval(alpha, lambda a: 255 if a <=128 else 0)

# Paste the color of index 255 and use alpha as a mask
im.paste(255, mask)

# The transparency index is 255
im.save("logo_py.png", transparency=255)

rawDescription: Converting PNG32 to PNG8 with PIL while preserving transparency


code:
from twisted.internet.protocol import Protocol

class StatefulProtocol(Protocol):
    def __init__(self, factory):
        self.connected = False

    def connectionMade(self):
        self.connected = True

    def connectionLost(self, reason):
        self.connected = False

rawDescription: How do I check if a twisted.internet.protocol instance has disconnected

description: Check if a twisted.internet.protocol instance has disconnected


code:
def buildTestSuite():
    suite = unittest.TestSuite()
    for testcase in glob.glob('src/testsuite/test_*.py'):
        modname = os.path.splitext(testcase)[0]
        module=__import__(modname,{},{},['1'])
        suite.addTest(unittest.TestLoader().loadTestsFromModule(module))
    return suite

rawDescription: How do i create a test Suite in python unittest

description: Create a test Suite in python unittest


code:
# p is a compiled regex
# s is a string
def process_match(m):
    # Process the match here.
    return ''

s = p.sub(process_match, s)

rawDescription: python regex match and replace

description: Python regex match and replace


code:
b.index += dt.timedelta(hours=12)
pd.concat((a, b), axis=0).sort_index()

rawDescription: Merge two daily series into one hour series


code:
class MyTopWindow:

    def __init__(self):
        self.widget1 = Widget
        self.widget2 = Widget
        self.button = Button

        #self.button.clicked -> self.handler

    def handler(self):
        self.widget1.someAttribute = 1

button.click() # -> handler()

rawDescription: Tkinter toplevel communication

description: Tkinter toplevel communication


code:
wr = csv.writer(f, quoting=csv.QUOTE_NONE)
for i in output:
    wr.writerow(['{:3.4e}'.format(x) for x in i])

rawDescription: Python csv.writer control precision

description: Python csv.writer control precision


code:
import glob
import csv
import os

directory = input('Please enter the directory: ')
extension = input('Please enter the extension (.txt, .csv): ')
files = list(glob.glob(os.path.join(directory, extension)))
if not files:
   print('Sorry, no files match your extension {} in the directory {}'.
         format(extension, directory))
else:
    for file_name in files:
        col = input('Enter the column number for {}'.format(file_name))
        with open(file_name, 'r') as thefile:
            reader = csv.reader(thefile, delimiter=',')
            for row in reader:
                try:
                    do_something(row[col])
                except IndexError:
                    print('Column {} does not exist'.format(col))

rawDescription: How to write "No file ends with" a user-defined extension

description: Write "No file ends with" a user-defined extension


code:
import numpy as np
from mayavi import mlab

x, y, z = np.ogrid[-2:2:25j, -2:2:25j, -2:2:25j]
s = np.power(x, 10) + np.power(y, 10) + np.power(z, 10) - 100

mlab.figure(bgcolor=(1,1,1))
mlab.contour3d(s, contours=[2], color=(.5,.5,.5), transparent=True, opacity=.5)

ax = mlab.axes(nb_labels=5, ranges=(-2,2,-2,2,-2,2))
ax.axes.property.color = (0,0,0)
ax.axes.axis_title_text_property.color = (0,0,0)
ax.axes.axis_label_text_property.color = (0,0,0)
ax.axes.label_format='%.0f'

mlab.show()

rawDescription: Python matplotlib 3d surface plot

description: Python matplotlib 3d surface plot


code:
from Tkinter import *
import tkMessageBox
r = Tk()
r.option_add('*Dialog.msg.font', 'Helvetica 12')
tkMessageBox.showinfo(message='Hello')

rawDescription: Control Font in tkMessageBox

description: Control Font in tkMessageBox


code: p.loc[:,:,indexer] = value

description: Setting pandas.Panel.minor_xs('attribute')


code:
import os
import sys
import Tkinter
import ttk

def fill_tree(treeview, node):
    if treeview.set(node, "type") != 'directory':
        return

    path = treeview.set(node, "fullpath")
    # Delete the possibly 'dummy' node present.
    treeview.delete(*treeview.get_children(node))

    parent = treeview.parent(node)
    for p in os.listdir(path):
        p = os.path.join(path, p)
        ptype = None
        if os.path.isdir(p):
            ptype = 'directory'

        fname = os.path.split(p)[1]
        oid = treeview.insert(node, 'end', text=fname, values=[p, ptype])
        if ptype == 'directory':
            treeview.insert(oid, 0, text='dummy')

def update_tree(event):
    treeview = event.widget
    fill_tree(treeview, treeview.focus())

def create_root(treeview, startpath):
    dfpath = os.path.abspath(startpath)
    node = treeview.insert('', 'end', text=dfpath,
            values=[dfpath, "directory"], open=True)
    fill_tree(treeview, node)

root = Tkinter.Tk()

treeview = ttk.Treeview(columns=("fullpath", "type"), displaycolumns='')
treeview.pack(fill='both', expand=True)
create_root(treeview, sys.argv[1])
treeview.bind('<<TreeviewOpen>>', update_tree)

root.mainloop()

rawDescription: python gui tree walk

description: Python gui tree walk


code:
table = Table([[x.date, x.person, x.person.company, x.total(), ]
                for x in page])

rawDescription: How to add method return value to list in python?

description: Add method return value to list in python


code:
import csv
from collections import defaultdict
from tkinter import *

grouped = defaultdict(list)

# Open the csv file and use the defaultdict to group by label.
with open('Book1.csv', 'r') as fh:
    book = csv.reader(fh)
    for name, label in book:
        grouped[label].append(name)

master = Tk()
A_label = Label(master, text='A', width=10).grid(row=1, column=1)
A_list = Listbox(master, width=10)
A_list.grid(column=1, row=2)
for name in grouped['A']:
    A_list.insert(END, name)

B_label = Label(master, text='B', width=10).grid(row=1, column=2)
B_list = Listbox(master, width=10)
B_list.grid(column=2, row=2)
for name in grouped['B']:
    B_list.insert(END, name)

master.mainloop()

rawDescription: separating groups in csv - using tkinter to display results

description: Separating groups in csv - using tkinter to display results


rawDescription: Python Regex vs PHP Regex

description: Python Regex vs PHP Regex


code:
mongo = pymongo.MongoClient()
mongo.DBNAME.command('convertToCapped', 'COLLNAME', size=SIZE)

rawDescription: invoke convertToCapped with pymongo

description: Invoke convertToCapped with pymongo


code:
while p.is_alive():
   iterate_mainloop()
   p.join(small_timeout)

rawDescription: Is there a cost to calling python multiprocessing .join() method


code:
def convertTime(seconds, tz):
    tzhours, tzminutes = map(int, (tz.lstrip('-')[:2], tz[-2:]))
    offset = tzhours * 3600 + tzminutes * 60
    if tz.startswith('-'):
        offset *= -1
    return time.strftime('%Y%m%d_%H%M%S', time.gmtime(seconds + offset))

rawDescription: Python strptime - Handling single digit dates


rawDescription: turning a list into a tuple python

description: Turning a list into a tuple python


code:
import matplotlib.pyplot as plt
boundaries = collections.OrderedDict([(10,'red'),(20,'blue'),(55,'purple')])
areas = range(0,101)
n, bins, patches = plt.hist(areas, [0]+list(boundaries), histtype='bar', rwidth=1.0)
for (patch,color) in zip(patches,boundaries.values()):
    patch.set_color(color)
plt.show()

rawDescription: How do I bin and categorize numbers in Python?

description: Bin and categorize numbers in Python


code:
from time import sleep
import threading
from subprocess import call

def runmodel(arg):
    call(arg, shell=True) # not the addition of shell=True - this is now required as the command line is being passed as a string rather than a list - I could not get the syntax with quotation marks and everything else to work without using a string and shell=True.
    sGlobal.release()

if __name__ == '__main__':

    n = 6 # maximum number of simultaneous runs
    s = 15 # delay between starts in seconds

    simulations = ['cmd.exe /c start "NAME" "<full file path to model exe>" -some -flags -here "<full file path to model control file>"'] # big list of simulations to run in here - each item in list is a string that represents the full command line to run a simulation

    threads = []
    global sGlobal

    sGlobal = threading.Semaphore(n)

    for arg in simulations:
        sGlobal.acquire()
        t = threading.Thread(target=runmodel, args=(arg,))
        threads.append(t)
        t.start()
        sleep(s)

    for t in threads:
        t.join()

rawDescription: Python 2.7 - How to spawn separate Windows consoles for individual subprocess.call() tasks when using threading?


code:
import lxml.html
doc = lxml.html.parse(url_to_site)
feeds = doc.xpath('//link[@type="application/rss+xml"]/@href') # list feed urls

rawDescription: Feed URL from HTML using Python

description: Feed URL from HTML using Python


rawDescription: map in python, that map multiple objects to each object in list

description: Map in python, that map multiple objects to each object in list


rawDescription: Python list inside string to list

description: Python list inside string to list


code:
import numpy as np, pandas as pd
import io, pkgutil
wells = pkgutil.get_data('pymc.examples', 'data/wells.dat')
type(wells)
df = pd.read_csv(io.BytesIO(wells), encoding='utf8', sep=" ", index_col="id", dtype={"switch": np.int8})
df.head()

rawDescription: pandas.read_csv from string or package data

description: Pandas.read_csv from string or package data


code:
df
df['junk'].values
i = df['junk'].str.strip().str.match(r'[+-]?\d+', na=True)
df.loc[i, 'junk'] = df.loc[i, 'junk'].astype('int')
df['junk'].values

rawDescription: Converting non-numeric integers in column that also contains strings


code:
import glob
import os
import re
os.chdir( "C:/headers" )

txt = open( 'C:/files.txt', 'w' )

for file in glob.glob( "*.h" ):
    with open( file ) as f:
        contents = f.read()    [...]
        if 'struct' in contents:
            txt.write( "%s\n"% re.sub('\.h$', '', file) )
txt.close()

rawDescription: Python searching directory, list basename of file, no extension


code:
import itertools
map(''.join, itertools.chain(itertools.product(list1, list2), itertools.product(list2, list1)))

rawDescription: permutations of two lists in python

description: Permutations of two lists in python


code:
def create(self, request, *args, **kwargs):
    data = request.data
    cart = Cart.objects.get(id=data['id'],user_id=data['user_id'])
    serializer = self.get_serializer(data=data)
    serializer.is_valid(raise_exception=True)
    order = Order.objects.create(**serializer.validated_data)
    cart.to_be_deleted = True
    cart.save()
    headers = self.get_success_headers(serializer.data)
    return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

rawDescription: Django rest update object from other model


code:
from django import forms

class Html5DateInput(forms.DateInput):
    input_type = 'date'

rawDescription: How can I override the 'type' attribute of a ModelForm in Django?

description: Override the 'type' attribute of a ModelForm in Django


code:
dataForPost = {'access_token' : '____',
               'batch' : """[
               {
                "method": "POST",
                "relative_url": "6004251715639",
                "body": "redownload=1&max_bid=35"
               },
               {
                "method": "POST",
                "relative_url": "6004251716039",
                "body": "redownload=1&max_bid=35"
               },
               {
                "method": "POST",
                "relative_url": "6004251715839",
                "body": "redownload=1&max_bid=35"
               }
              ]""".replace('\n', '').replace('\t', '').replace(' ', '')}

rawDescription: How to emulate curl -F for facebook graph api batch request in python

description: Emulate curl -F for facebook graph api batch request in python


code:
class A(object):
    def __init__(self, id):
        self.id = id
        b1 = B(a=self)
        b2 = B(a=self)  # now b1, b2 have attribute .a which points to 'parent' A
        self.games_summary_dicts = [b1, b2]
        """:type : list[B]"""

class B(object):
    def __init__(self, a):  # initialize B instances with 'parent' reference
        self.a = a

    def do_something_with_containing_class(self):
        self.a.id = ...

rawDescription: How can I define a containing class for another class in python?

description: Define a containing class for another class in python


code: p2 = subprocess.Popen(shlex.split("sort -t'\t' -k2,2n -k3,3"), stdin=p1.stdout, stdout=subprocess.PIPE)

rawDescription: Chained popen processes not getting output


code:
from collections import namedtuple

KeyboardEvent = namedtuple('KeyboardEvent', ['event_type', 'key_code',
                                             'scan_code', 'alt_pressed',
                                             'time'])

handlers = []

def listen():
    """
    Calls `handlers` for each keyboard event received. This is a blocking call.
    """
    # Adapted from http://www.hackerthreads.org/Topic-42395
    from ctypes import windll, CFUNCTYPE, POINTER, c_int, c_void_p, byref
    import win32con, win32api, win32gui, atexit

    event_types = {win32con.WM_KEYDOWN: 'key down',
                   win32con.WM_KEYUP: 'key up',
                   0x104: 'key down', # WM_SYSKEYDOWN, used for Alt key.
                   0x105: 'key up', # WM_SYSKEYUP, used for Alt key.
                  }

    def low_level_handler(nCode, wParam, lParam):
        """
        Processes a low level Windows keyboard event.
        """
        event = KeyboardEvent(event_types[wParam], lParam[0], lParam[1],
                              lParam[2] == 32, lParam[3])
        for handler in handlers:
            handler(event)

        # Be a good neighbor and call the next hook.
        return windll.user32.CallNextHookEx(hook_id, nCode, wParam, lParam)

    # Our low level handler signature.
    CMPFUNC = CFUNCTYPE(c_int, c_int, c_int, POINTER(c_void_p))
    # Convert the Python handler into C pointer.
    pointer = CMPFUNC(low_level_handler)

    # Hook both key up and key down events for common keys (non-system).
    hook_id = windll.user32.SetWindowsHookExA(win32con.WH_KEYBOARD_LL, pointer,
                                             win32api.GetModuleHandle(None), 0)

    # Register to remove the hook when the interpreter exits. Unfortunately a
    # try/finally block doesn't seem to work here.
    atexit.register(windll.user32.UnhookWindowsHookEx, hook_id)

    while True:
        msg = win32gui.GetMessage(None, 0, 0)
        win32gui.TranslateMessage(byref(msg))
        win32gui.DispatchMessage(byref(msg))

if __name__ == '__main__':
    def print_event(e):
        print(e)

    handlers.append(print_event)
    listen()

rawDescription: Applying low-level keyboard hooks with Python and SetWindowsHookExA


code:
import time
import sys
sys.stdout.write('29 seconds remaining')
time.sleep(1)
sys.stdout.write('\r28 seconds remaining')

rawDescription: How can I edit a string that was printed to stdout?

description: Edit a string that was printed to stdout


code:
@register.filter(name='replace_date_separator')
def replace_date_separator(value, arg):

    if arg == 'fr-CA':
        return value.replace("/", '-')
    elif arg == 'de' or arg == 'pl' or arg == 'ru':
        return value.replace("/", '.')
    else:
        return value.replace("/", '*')

rawDescription: django custom template tag - if else condition

description: Django custom template tag - if else condition


code:
with open('test.txt', 'rb') as f:
    f.seek(-4, os.SEEK_END)
    ch=f.read(4)
    for i in range(3, -1, -1):
        pattern = ord(ch[i]) & 0xc0
        if pattern in (0x00, 0x40, 0xc0):
            ch = ch[i:]
            break

rawDescription: Character by character in UTF8 file

description: Character by character in UTF8 file


code:
class TownList(generics.ListAPIView):
    queryset = Town.objects.all()
    serializer_class = TownSerializer
    filter_class = TownFilter(generics.ListAPIView)
    serializer_class = PurchaseSerializer

    def get_queryset(self):
        queryset = Town.objects.all()
        search_param = self.request.QUERY_PARAMS.get('search', None)
        if search_param is not None:
            """
            set queryset here or use your TownFilter
            """
        return queryset

rawDescription: Filtering in django rest framework

description: Filtering in django rest framework


code:
def renderId(celllayout, cell, model, iter):
  if model.get_value(iter,1) == 3:
    cell.set_property('visible',True)
  else:
    cell.set_property('visible',False)

treeviewcolumn = gtk.TreeViewColumn()
renderer = gtk.CellRendererText()
treeviewcolumn.add_attribute(renderer,'text',0)
treeviewcolumn.set_cell_data_func(renderer,renderId)

rawDescription: Access gtk.TreeIter inside render() Function of gtk.CellRenderer -- pygtk


code:
from subprocess import call

def lock(change,patchnum):
    #Construct ssh command
    #ssh -p 29418 servacc@company.com gerrit review --option 1 change,patchnum
    call(["ssh -p 29418 servacc@company.com gerrit review --option 1 %d,%d" %(change,patchnum)],shell=True)

change = 700293
patchnum = 1
lock(change,patchnum)

rawDescription: Not able to construct and run a ssh command


code:
from cStringIO import StringIO
from tokenize import tokenize

python_code = '''
def foo(): # This is a comment
    print 'foo'
'''

fp = StringIO(python_code)

tokenize(fp.readline)

rawDescription: Sensible python source line wrapping for printout


code:
from PIL import Image

def adjust(image):
    image = image.convert('RGBA')
    width, height = image.size
    new_width = 512
    new_height = new_width * height / width
    image = image.resize((new_width, new_height), resample=Image.ANTIALIAS)
    new_image = Image.new('RGBA', (512, 512), (0, 0, 0, 0))
    upper = (512 - image.size[1]) / 2
    new_image.paste(image, (0, upper))
    return new_image

rawDescription: PIL - Add transparency around .png

description: PIL - Add transparency around .png


code:
submission = r.get_submission(thread)
process_comments(submission.comments)

def process_comments(objects):
    for object in objects:
        if type(object).__name__ == "Comment":
            process_comments(object.replies) # Get replies of comment

            # Do stuff with comment (object)

        elif type(object).__name__ == "MoreComments":
            process_comments(object.comments()) # Get more comments at same level

rawDescription: Getting child-comments of a comment on Reddit with Praw in Python


code:
number1 = int(input('Number #1: '))
number2 = int(input('Number #2: '))
l = len(str(number1))
l1 = len(str(number2))
print()
def addition():
    print(' ',max(number1,number2))
    print('+')
    print(' ',min(number1,number2))
    print('-'*(max(l,l1)+2))
    print('  ')
    print(' ',number1+number2)

def carries():
    num1 = str(number1)
    num2 = str(number2)
    carry = 0
    carries = 0
    c1 = l
    c2 = l
    if (l < l1):
        while (c1 < l1):
            num1 = '0' + num1
            c1+=1
    if (l1 < l):
        while (c2 < l):
            num2 = '0' + num2
            c2+=1
    i = c1
    while (i > 0):
        if (int(num1[i-1])+int(num2[i-1])+carry > 9):
            carry = 1;
            carries+=1
        else:
            carry = 0
        i-=1
    return carries
addition()

print()
print('Carries : ',carries())

rawDescription: Addition carries in Python

description: Addition carries in Python


code:
def find_pid(inode):

    # get a list of all files and directories in /proc
    procFiles = os.listdir("/proc/")

    # remove the pid of the current python process
    procFiles.remove(str(os.getpid()))

    # set up a list object to store valid pids
    pids = []

    for f in procFiles:
        try:
            # convert the filename to an integer and back, saving the result to a list
            integer = int(f)
            pids.append(str(integer))
        except ValueError:
            # if the filename doesn't convert to an integer, it's not a pid, and we don't care about it
            pass

    for pid in pids:
        # check the fd directory for socket information
        fds = os.listdir("/proc/%s/fd/" % pid)
        for fd in fds:
            # save the pid for sockets matching our inode
            if ('socket:[%d]' % inode) == os.readlink("/proc/%s/fd/%s" % (pid, fd)):
                return pid

rawDescription: Finding a process ID given a socket and inode in Python 3


code:
def detect( A, B, C, r ):

    # First, let's express each vector as a complex number.
    # This simplifies the rest of the code because we can then subtract them
    # from each other in one statement, or find their length with one statement.
    # (Downside: it does not allow us to generalize the code to spheres in 3D.)
    OA = complex( *A )
    OB = complex( *B )
    OC = complex( *C )

    # Now let's translate into a coordinate system where A is the origin
    AB = OB - OA
    AC = OC - OA

    # Before we go further let's cover one special case:  if either A or B is actually in
    # the circle,  then mark it as a detection
    BC = OC - OB
    if abs( BC ) < r or abs( AC ) < r: return True

    # Project C onto the line to find P, the point on the line that is closest to the circle centre
    AB_normalized = AB / abs( AB )
    AP_distance = AC.real * AB_normalized.real  +  AC.imag * AB_normalized.imag    # dot product (scalar result)
    AP = AP_distance * AB_normalized   # actual position of P relative to A (vector result)

    # If AB intersects the circle, and neither A nor B itself is in the circle,
    # then P, the point on the extended line that is closest to the circle centre, must be...

    # (1) ...within the segment AB:
    AP_proportion = AP_distance / abs( AB )   # scalar value: how far along AB is P?
    in_segment =   0 <= AP_proportion <= 1

    # ...and (2) within the circle:
    CP = AP - AC
    in_circle = abs( CP ) < r

    detected = in_circle and in_segment

    #OP = OA + AP
    #plt.plot( [OC.real, OP.real], [OC.imag, OP.imag], {True:'rs--', False:'bs--'}[detected] )

    return detected

def plot_detected(An, Bn, C, r):
    '''
    Plots newly detected line segments with red color
    while the rest remains with blue color
    '''
    plt.figure(1)
    plt.clf()
    plt.subplot(111)
    for A, B in zip(An, Bn):
        if detect(A, B, C, r):
              line1, = plt.plot([ A[0], B[0] ], [ A[1], B[1] ], 'ro-')
        else:
              line2, = plt.plot([ A[0], B[0] ], [ A[1], B[1] ], 'bo-')
    plt.legend([line1, line2], ('detected','un-detected'))
    circle = mpatches.Circle( C, r, fc="none", ec='k' )
    plt.gca().add_patch(circle)
    plt.gca().set_aspect('equal')

rawDescription: How to pick up line segments which is inside or intersect a circle?

description: Pick up line segments which is inside or intersect a circle


code:
from django_auth_ldap.backend import LDAPBackend

user = LDAPBackend().authenticate(username, password)

rawDescription: Use JWT Auth for authenticate against LDAP service


rawDescription: mysqldb doesn't write on db

description: Mysqldb doesn't write on db


code:
class Features:
    a_static_variable = 1

    @staticmethod
    def func1(blah):
        print(blah)

    a_static_variable_that_references_a_static_function = func1

rawDescription: Do class/static variables have access to static methods?


code:
from datetime import datetime
dates =['11/09/2013','12/09/2013','20/09/2013','27/09/2013','30/09/2013']
test = '21/09/2013'

# Convert to datetime objects
dd = [datetime.strptime(d,'%d/%m/%Y') for d in dates]
tt = datetime.strptime(test,'%d/%m/%Y')

# Split lists
dd_before = [d for d in dd if d < tt]
dd_after = [d for d in dd if d >= tt]

# Convert back to strings if needed
dates_before = [datetime.strftime(d,'%d/%m/%Y') for d in dd_before]
dates_after = [datetime.strftime(d,'%d/%m/%Y') for d in dd_after]

rawDescription: Split List based on index of value

description: Split List based on index of value


rawDescription: Trying to write to binary plist format from Python (w/PyObjC) to be fetch and read in by Cocoa Touch


code:
import bisect

i = bisect.bisect_left(dates, dates[i1]-timedelta(days=interval))

rawDescription: Quick way of matching date range to indices in Python list of dates


code: nblevels = df.index.nlevels

rawDescription: Python / Pandas : How many levels in a dataframe index?


rawDescription: Sort list of ints by the first digit of each int


code:
import tkinter as tk

class CustomText(tk.Text):
    def __init__(self, *args, **kwargs):
        tk.Text.__init__(self, *args, **kwargs)

    def highlight_pattern(self, pattern, tag, start="1.0", end="end",
                          regexp=False):
        '''Apply the given tag to all text that matches the given

pattern

        If 'regexp' is set to True, pattern will be treated as a

regular
        expression.
        '''

        start = self.index(start)
        end = self.index(end)
        self.mark_set("matchStart", start)
        self.mark_set("matchEnd", start)
        self.mark_set("searchLimit", end)

        count = tk.IntVar()
        while True:
            index = self.search(pattern, "matchEnd","searchLimit",
                                count=count, regexp=regexp)
            if index == "": break
            self.mark_set("matchStart", index)
            self.mark_set("matchEnd", "%s+%sc" % (index, count.get()))
            self.tag_add(tag, "matchStart", "matchEnd")

class Arshi(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.createtext()

    def highlight(self, argument):
        # Declaration (works)
        self.text.highlight_pattern("<!DOCTYPE HTML>",

"htmlDeclaration")
        self.text.highlight_pattern("<!doctype html>",

"htmlDeclaration")

        # tags (does not work)
        tags = []

        with open("html_tags.txt", "r") as taglist:
            for tag in taglist:
                tags.append(tag.strip())

        for i in range(0, len(tags)):
            self.text.highlight_pattern(tags[i], "tags")

    def createtext(self):
        self.text = CustomText(self, bd=0, font=("Courier", 9))
        self.text.tag_configure("htmlDeclaration",

foreground="#246BB2")
        self.text.tag_configure("tags", foreground="#006BB2")
        self.text.bind("<Key>", self.highlight)
        self.highlight(None)
        self.text.pack()

if __name__ == "__main__":
    root = tk.Tk()
    root.title("Arshi")
    window = Arshi(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

rawDescription: Highlighting bug with Tkinter text editor


code: pd.date_range('2014-01-01', periods=10) + pd.Timedelta(days=1)

rawDescription: How to add/subtract time (hours, minutes, etc.) from a Pandas DataFrame.Index whos objects are of type datetime.time?

description: Add/subtract time (hours, minutes, etc.) from a Pandas DataFrame.Index whos objects are of type datetime.time


rawDescription: Refer to same input multiple times in string replacement?


code:
for job in cron.get_jobs():
    job.func()

rawDescription: APScheduler how to trigger job now

description: APScheduler how to trigger job now


code:
df2 = df.colB.str[1:-1].str.split(',', expand=True)
df2 = df2.stack().reset_index()
df2 = df2.drop('level_1', axis=1).rename(columns={0: 'colB'}).set_index('level_0')
df = df.drop('colB', axis=1)
df = pd.concat([df, df2], axis=1)

rawDescription: How can I "unwrap" columns which contain multiple items?


code:
baselen = len(self.basePath)
return (path[baselen:].replace("/", "\\") for path in paths)

rawDescription: Translate SVN path to local file system path in Python


code:
class TreeNode(object):
    def __init__(self, data, children=[]):
        self.data = data
        self.children = list(children)

    def add(self, child):
        self.children.append(child)
    ...

rawDescription: What is the best way to implement a Tree Structure in python


rawDescription: Combining Sympy and uncertainties

description: Combining Sympy and uncertainties


code:
used = ws.UsedRange
nrows = used.Row + used.Rows.Count - 1
ncols = used.Column + used.Columns.Count - 1

rawDescription: win32com equivalent of xlrd's sheet.ncols

description: Win32com equivalent of xlrd's sheet.ncols


code:
def ProcessList(_func, *lists):
    return map( lambda *xs: ProcessList(_func, *xs) if all(type(x) is list for x in xs) else _func(*xs), *lists )

def DoSomething(*args):
    return ''.join(map(str, args))

_list1 = [[1,2,3],[1,2],[1,2,3,4]]
_list2 = [['a','b','c'],['a','b'],['a','b','c','d']]

print (ProcessList(DoSomething, _list1, _list2))

rawDescription: applying list.map to two parallel lists in python

description: Applying list.map to two parallel lists in python


code:
response = urllib2.urlopen(url)
content = response.read()

rawDescription: How do I get the HTML of a web page if I have the url in python

description: Get the HTML of a web page if I have the url in python


code:
import pandas as pd
import numpy as np

df = pd.DataFrame({'A':[5, 3, 2, 6], 'B':[2, 4, 1, 8], 'C':[1, 5, 0, 7]})

Nrows = len(df)

def run(col):
        originalValues = list(df[col])
        values = list(np.where(np.array(list(df[col])) == 0)[0])
        indices2replace = filter(lambda x: x > 0 and x < Nrows, values)
        for index in indices2replace:
                originalValues[index] = 0.5 * (originalValues[index+1] + originalValues[index-1])
        return originalValues

newDF = pd.DataFrame(map(lambda x: run(x) , df.columns)).transpose()

rawDescription: Find the average of the element above and below in that column if that element is 0 - Pandas DataFrame


code:
# get the rank of total_value compared to existing total_values (up to 1,000)
rank = Main.all().filter('total_value <', total_value).count(1000) + 1

rawDescription: Google App Engine: how to tell sort location for a user value?


code:
import numpy as np
import scipy.io

x = np.linspace(0, 2 * np.pi, 100)
y = np.cos(x)

scipy.io.savemat('test.mat', dict(x=x, y=y))

rawDescription: "Converting" Numpy arrays to Matlab and vice versa


code:
x = {"a":"b"}
x.values()
x.values()[0]

rawDescription: Parsing the content of a 'Requests' call only shows some json data


code:
wv = QtWebKit.QWebView()
wv.load( QtCore.QUrl( 'http://www.google.com' ))
wv.show()

rawDescription: PyQt QWebView Email access

description: PyQt QWebView Email access


rawDescription: Python Itertools.Permutations()

description: Python Itertools.Permutations()


code:
import pexpect

child = pexpect.spawn('yourprogram')
while True:
  found = child.expect ([r':$', r'\?$', pexpect.EOF])
  if found == 0:
    child.send('\n')
  elif found == 1:
    child.send('y\n')
  else:  # EOF
     return

rawDescription: Python subprocess: interacting with a shell script


code:
os.system('x-terminal-emulator -t "Installing new packages" -e "sh -c \\"sudo tlmgr install %s; touch /tmp/install_completed\\""' % packagesString)
while not os.path.exists("/tmp/install_completed"):
    time.sleep(0.1)
os.remove("/tmp/install_completed")

rawDescription: Python: Spawn sudo process (in new terminal), wait until completed


code:
document = Document('Sample.docx')
document
paragraphs = document.paragraphs
paragraphs
paragraph = paragraphs[2]
paragraph
paragraph.style = 'Title'
paragraph.style

rawDescription: How to apply a style to a paragraph with python-docx?

description: Apply a style to a paragraph with python-docx


code:
camcapture = cv.CaptureFromCAM(0)
cv.SetCaptureProperty(camcapture,cv.CV_CAP_PROP_FRAME_WIDTH, 1280)
cv.SetCaptureProperty(camcapture,cv.CV_CAP_PROP_FRAME_HEIGHT, 720);

frame = cv.QueryFrame(camcapture)
image = QImage(frame.tostring(), frame.width, frame.height, QImage.Format_RGB888).rgbSwapped()
pixmap = QPixmap.fromImage(image)

rawDescription: Display an webcam stream in PyQt4 using OpenCV Camera Capture


code:
cursor.execute("SELECT strftime('%m.%d.%Y %H:%M:%S', timestamp, 'localtime'), temp FROM data WHERE timestamp>datetime('now','-1 hours')")
# fetch all or one we'll go for all.
results = cursor.fetchall()
temp_chart_table = []
for row in results:
    temp_chart_table.append({'timestamp': row[0], 'temp': row[1]})

rawDescription: convert python sql list into dictionary

description: Convert python sql list into dictionary


code:
matrix_test = [[0.0 for i in range(3)] for i in range(6)]
for x in range(3):
    matrix_test [2 * x][x] = 1.0
    matrix_test [2 * x + 1][x] = -1.0

rawDescription: How to create the matrix AxB in Python?

description: Create the matrix AxB in Python


code:
import regex
regex.search(r"(?e)(?:hello){e<=4}", "What did you say, oh - hello")[0]
regex.search(r"(?b)(?:hello){e<=4}", "What did you say, oh - hello")[0]

rawDescription: How can I find the best fuzzy string match?

description: Find the best fuzzy string match


code: [my_list[i:i+3] for i in xrange(0, len(my_list), 3)]

rawDescription: how do I make a list into multiple lists of that list?

description: Make a list into multiple lists of that list


code: value_at_index = dic.values()[index]

rawDescription: Accessing dictionary value by index in python


code:
import pygame

WINDOWWIDTH = 1080
WINDOWHEIGHT = 1080
Window = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), pygame.FULLSCREEN)

class CustomFont: #The letterimage file must consist of equally-sized letters, each of 'lettersize', arranged in a single line, left-to-right.  The letterstring is a string representation of the letters in the image (in the same order).
    def __init__(self, letterstring, lettersize, letterimagefile):
        self.letterstring = tuple(letterstring)
        self.lettersize = lettersize
        self.letterimagefile = letterimagefile
    def write (self, string, position):
        stringseparated = tuple(string)
        for i in range(len(stringseparated)):
            letter = stringseparated[i]
            for j in range(len(self.letterstring)):
                if self.letterstring[j] == letter: #j gives us the index number of the letter in our original string
                    grabletterbox = ((self.lettersize[0] * j), 0, self.lettersize[0], self.lettersize[1]) #this defines the position of the rectangle that will grab the letter from our letterimage file, as well as the size of said rectangle
                    Window.blit(self.letterimagefile, ((position[0] + (i * self.lettersize[0])),position[1]) , grabletterbox)

ActiveNumbersSprite = pygame.image.load('.\images\ActiveNumbers.png') #The image used for the numbers
ActiveNumbers = CustomFont('0123456789', (36 , 60), ActiveNumbersSprite) #This tells the program what characters the ActiveNumbersSprite image contains, in what order, how big each character is (as (x,y), and the name of the image file
...

rawDescription: Is it possible to create a Font from a pixel-based colour image? (for Pygame, but also otherwise)


code:
sep = '...'
rest = text.split(sep, 1)[0]

rawDescription: How to remove all characters after a specific character in python?

description: Remove all characters after a specific character in python


code:
from nose.tools import *
from mysystem import system_state

class TestMySystem (TestCase):
    def setUp(self):
        system_state.initialize()

class WhenItIsSetTo1 (TestMySystem):
    def setUp(self):
        super(WhenItIsSetTo1, self).setUp()
        system_state.set_to(1)

    def test_system_should_be_1 (self):
        assert_equal(system_state.value(), 1)

class WhenItIsSetTo2 (TestMySystem):
    def setUp(self):
        super(WhenItIsSetTo2, self).setUp()
        system_state.set_to(2)

    def test_system_should_be_2 (self):
        assert_equal(system_state.value(), 2)

rawDescription: Can I nest TestCases with Nose?

description: Nest TestCases with Nose


code:
import numpy as np
import pylab as plt

data = np.random.normal(size=10000)*10000

plt.figure(0)
plt.hist(data, bins=np.arange(data.min(), data.max(), 1000))

plt.figure(1)
hist1 = np.histogram(data, bins=np.arange(data.min(), data.max(), 1000))
plt.bar(hist1[1][:-1], hist1[0], width=1000)

plt.figure(2)
hist2 = np.histogram(data, bins=np.arange(data.min(), data.max(), 200))
mask = (hist2[1][:-1] < 20000) * (hist2[1][:-1] > 0)
plt.bar(hist2[1][mask], hist2[0][mask], width=200)

rawDescription: matplotlib normed histograms

description: Matplotlib normed histograms


code:
import pandas as pd
import numpy as np

dates = pd.date_range('2001-01-01',periods=100)
dates = dates[::3]
A = pd.DataFrame({'date':dates,
                  'id':['A']*len(dates),
                  'value':np.random.randn(len(dates))})

dates = pd.date_range('2001-01-01',periods=100)
dates = dates[::5]
B = pd.DataFrame({'date':dates,
                  'id':['B']*len(dates),
                  'value':np.random.randn(len(dates))})
df = pd.concat([A,B])

with_lags = []
for id, group in df.groupby('id'):
    group = group.set_index(group.date)
    index = group.index
    group = group.reindex(pd.date_range(group.index[0],group.index[-1]))
    group = group.ffill()
    group['lag_value'] = group.value.shift(7)
    group = group.loc[index]
    with_lags.append(group)

with_lags = pd.concat(with_lags, 0)
with_lags.index = np.arange(with_lags.shape[0])

rawDescription: Search in pandas dataframe

description: Search in pandas dataframe


rawDescription: getProcAddress in python for linux?

description: GetProcAddress in python for linux


code:
c = db.cursor()
values_to_insert = [("a","b"),("c","d"),...]
c.execute_many("INSERT INTO table (val1,val2) VALUES (?,?)",values_to_insert  )

rawDescription: Concatenating SQL statement strings

description: Concatenating SQL statement strings


code:
def function_():
    for item in list_:
        print(item + ' == a a?\t', item == 'a a')

rawDescription: List outside of function cannot be tested if members contain a space


rawDescription: in Python, trying to convert integer to character and put in a binary "string"

description: In Python, trying to convert integer to character and put in a binary "string"


code:
def createManySomethings(Names, sizes):
    for (name, number) in zip(Names, sizes):
        createSomething([name], [number])

rawDescription: Create a loop over a function that prints out the elements the function it takes in, in a new row using a wrapper


code:
text_str = '''Some additional very long text
that spans over a few lines and is
very very very very very boring
but it's sole purpose is demonstration
so we can live with that'''

# make figure and axes
fig, ax = plt.subplots(1, 1)
# shove the bottom of the axes up
fig.subplots_adjust(bottom=.3)
# add the text
fig.text(0, 0, text_str, va='bottom')
# force draw
plt.draw()

rawDescription: MatPlotLib and PyQt plot and additional data to printer


code:
@property
def name(self):
    """
    The unique name of the direction.

    :getter: Returns this direction's name
    :setter: Sets this direction's name
    :type: string
    """
    return self._name

rawDescription: How can I generate documentation for a Python property setter using Sphinx?

description: Generate documentation for a Python property setter using Sphinx


code:
def checkLen():
     days = ["Monday", "Tuesday", "Wednesday", "Thursday" "Friday", "Saturday", "Sunday"]
     try:
         position = days.index("Monday")
         print("Found it")
     except ValueError:
         position = None # or 0 if you want
         print("Not present")
     print(position)

rawDescription: Find and print index of element in a list (string)


code:
import collections
import random
import bisect

def sample(xs, sample_size = None, replace=False, sample_probabilities = None):
    """Mimics the functionality of http://statistics.ats.ucla.edu/stat/r/library/bootstrap.htm sample()"""

    if not isinstance(xs, collections.Iterable):
        xs = range(xs)
    if not sample_size:
        sample_size = len(xs)

    if not sample_probabilities:
        if replace:
            return [random.choice(xs) for _ in range(sample_size)]
        else:
            return random.sample(xs, sample_size)
    else:
        if replace:
            total, cdf = 0, []
            for x, p in zip(xs, sample_probabilities):
                total += p
                cdf.append(total)

            return [ xs[ bisect.bisect(cdf, random.uniform(0, total)) ]
                    for _ in range(sample_size) ]
        else:
            assert len(sample_probabilities) == len(xs)
            xps = list(zip(xs, sample_probabilities))
            total = sum(sample_probabilities)
            result = []
            for _ in range(sample_size):
                # choose an item based on weights, and remove it from future iterations.
                # this is slow (N^2), a tree structure for xps would be better (NlogN)
                target = random.uniform(0, total)
                current_total = 0
                for index, (x,p) in enumerate(xps):
                    current_total += p
                    if current_total > target:
                        xps.pop(index)
                        result.append(x)
                        total -= p
                        break
            return result

rawDescription: statistics bootstrap library in Python?

description: Statistics bootstrap library in Python


code: '{0} in {1}'.format(unicode(self.author,'utf-8'),  unicode(self.publication,'utf-8'))

rawDescription: Using multiple arguments for string formatting in Python (e.g., '%s ... %s')


code:
df = pd.read_sql('select ....', some_sql_connection)

df['difference'] = df['some column name'] - df['another column name']

rawDescription: How to subtract 2 columns with dtype = object within data frame to form a new column of the difference pandas

description: Subtract 2 columns with dtype = object within data frame to form a new column of the difference pandas


code: lst = lst[:n-1]

rawDescription: Remove the last N elements of a list

description: Remove the last N elements of a list


code:
from xml.dom.minidom import parseString
l = '<Lg Cookie=""/>'
dom = xml.dom.minidom.parseString(l)
L = dom.getElementsByTagName('Lg')[0]
L.setAttribute('Cookie', 'test')
print (L.toxml())

rawDescription: how to set value using xml.dom.minidom python

description: Set value using xml.dom.minidom python


rawDescription: Why does this Google App Engine ndb ancestor query not work?


code:
import sys
from PyQt4 import QtCore, QtGui, QtWebKit

app = QtGui.QApplication(sys.argv)

web = QtWebKit.QWebView()
web.load(QtCore.QUrl("http://upload.wikimedia.org/wikipedia/commons/a/af/Tux.png"))
web.show()

sys.exit(app.exec_())

rawDescription: Qt Image From Web

description: Qt Image From Web


code:
import csv

def csv_dict_reader(file_obj):
    """Read a CSV file with 2 columns and output a new string. """
    reader = csv.DictReader(file_obj, delimiter=',')
    buf = ""
    for line in reader:
        buf += line["KEY"] + " = " + line["LANGUAGE"] + "\n"

    return buf

if __name__ == "__main__":
    with open("test_trans_csv.csv") as f_obj:
        with open("test_trans_output.csv", "w") as f_output:
            txt = csv_dict_reader(f_obj)
            f_output.write(txt)

rawDescription: Python - Read CSV and output to strings

description: Python - Read CSV and output to strings


rawDescription: Is it possible to make an iterable from a linked list without writing a generator function?


code:
r'bla\bla'
'bla\\bla'

rawDescription: pymssql utf8: queries with back slash

description: Pymssql utf8: queries with back slash


code:
@tornado.web.asynchronous
@gen.engine
def get(self):
    ...
    response = yield gen.Task(some_async_call(...))
    ...
    response = yield gen.Task(some_async_call(...))
    ...
    response = yield gen.Task(some_async_call(...))
    ...
    self.write(something)
    self.finish()

rawDescription: equivalent of @inlineCallbacks in Tornado?

description: Equivalent of @inlineCallbacks in Tornado


code:
def fib(n):
    if n <= 2:
        return 1
    return fib(n-1) + fib(n-2)

print(False and fib(100)) # prints False immediately
print(True and fib(100)) # takes very, very long
print(fib(100) and False) # takes very, very long

rawDescription: Boolean expressions in Python

description: Boolean expressions in Python


code:
query = 'green shoe'
query_with_weights = [(x, len(query.split()) - query.split().index(x)) for x in query.split()]
results = ['black pants', 'green pants', 'green shoe']
results_split = [res.split() for res in results]

get_weight = lambda x: ([weight for y, weight in query_with_weights if y==x] or [0])[0]
sorted_results = sorted([ (l, sum([( get_weight(m)) for m in l])) for l in results_split], key = lambda lst: lst[1], reverse=True)
print('sorted_results={}'.format(sorted_results))

rawDescription: sorting a reduce query with multiple terms and Q filters

description: Sorting a reduce query with multiple terms and Q filters


code:
# Module level, setting up the data store
from collections import defaultdict
extra_info = defaultdict(dict) # Creates empty dicts for unknown keys

# Saving the info
root = node.getroottree().getroot()
extra_info[id(root)]["ENV"] = "replacement"

# Retrieving it later
root = node.getroottree().getroot()
info = extra_info[id(root)]

rawDescription: Python: Inject attribute into object created by C library


rawDescription: Aggregate count in Pandas

description: Aggregate count in Pandas


code:
import bpy
import bmesh

def OrderSelectedVerts(_bm):
    edge=_edges[0]
    startvert=edge.verts[0]
    _edges.remove(edge)
    _orderedVerts=[]
    _orderedVerts.append(startvert)
    while len(_edges)>0:
        startvert=GetNextVert(startvert)
        if startvert is not None:
            _orderedVerts.append(startvert)
    return _orderedVerts

def GetNextVert(sv):
    for e in _edges:
        if e.verts[0]==sv or e.verts[1]==sv:
            if e.verts[0]==sv:
                d=e.verts[1]
            else:
                d=e.verts[0]
            _edges.remove(e)
            return d

# Get the active mesh
obj = bpy.context.edit_object
me = obj.data

# Get a BMesh representation
bm = bmesh.from_edit_mesh(me)
_edges=[i for i in bm.edges if i.select]

#Get the selected verts in order
orderedVerts=OrderSelectedVerts(bm)

#Use the ordered verts

#Update the bmesh
bmesh.update_edit_mesh(me, True)

rawDescription: How can I get a sorted array of selected verts in blender python

description: Get a sorted array of selected verts in blender python


code:
s = 'Hello, world'
encoded = zlib.compress(s).encode('base64')

rawDescription: python compression function which returns 32 digit string?

description: Python compression function which returns 32 digit string


code:
import wx
class MainWindow(wx.Frame):
    def __init__ (self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(200, 100))
        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
        self.CreateStatusBar()

        #setting up the menu

        filemenu = wx.Menu()

        menuAbout = filemenu.Append(wx.ID_ABOUT, "About", "information about the use of this program")
        menuExit = filemenu.Append(wx.ID_EXIT, "Exit", "Exit this program")

        menuBar = wx.MenuBar()

        menuBar.Append(filemenu,"File")
        self.SetMenuBar(menuBar)
        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)

        self.Show(True)

    def OnAbout(self,e):
        dlg = wx.MessageDialog(self, "A small text editor", "About sample     editor", wx.OK)
        dlg.ShowModal()
        dlg.Destroy()

    def OnExit(self,e):
        self.Close(True)

app = wx.App(False)
frame = MainWindow(None, "sample editor")
app.MainLoop()

rawDescription: TypeError: in method 'new_Frame', expected argument 2 of type 'int'


code:
from IPython import embed
embed()

rawDescription: Is there a way to make python become interactive in the middle of a script?


code:
x = earth()
('Total:1', 'This Country:1')    # __init__ called
x = None
('Total:0', 'This Country:1')    # __del__ called as first instance is no longer used
x = earth()
('Total:1', 'This Country:1')    # __init__ called again for the second instance

rawDescription: A method that run authomatically when instaciate a class with a duplicate variable


code:
for row_index, row in enumerate(matrix):
    for col_index, col in enumerate(row):
        ...

rawDescription: Performing arithmetic on list indexes

description: Performing arithmetic on list indexes


code:
import math
y = 122.32*math.exp(-0.31*page_load_time)

rawDescription: Python Maths Issue

description: Python Maths Issue


code:
Valid = True
while Valid:
  try:
     Column = int(input("Insert Column: "))
     Row = int(input("Insert Row: "))
     Valid = Row in range(10)  and Column in range(10)
  except Exception as e:
     print(e)
     Valid = False

rawDescription: If Statement in Python (Nested Boolean statements)


code:
zi = 0
# Create zero array with the appropriate length for the data subset
z = np.zeros((col2 - col1) * (row2 - row1))
# Process number of rows for which data is being extracted
for i in range(row2 - row1):
    # Pull row, then desired elements of that row into buffer
    tmp = ((dataset.variables["z"][(i*cols):((i*cols)+cols)])[col1:col2])
    # Add each item in buffer sequentially to data array
    for j in tmp:
        z[zi] = j
        # Keep a count of what index position the next data point goes to
        zi += 1

rawDescription: Subsample 1-D array using 2-D indices in numpy


code:
from pandas.core.reshape import melt
melt(df)

rawDescription: convert dataframe from wide layout to SQL-style slim layout

description: Convert dataframe from wide layout to SQL-style slim layout


code: exp.OPEN.resample("5Min", how='first')

rawDescription: How to remove the DATE TIME that have NAN vaules

description: Remove the DATE TIME that have NAN vaules


code:
import xlsxwriter

workbook = xlsxwriter.Workbook('image_scaled.xlsx')
worksheet = workbook.add_worksheet()

image_width = 140.0
image_height = 182.0

cell_width = 64.0
cell_height = 20.0

x_scale = cell_width/image_width
y_scale = cell_height/image_height

worksheet.insert_image('B2', 'python.png',
                       {'x_scale': x_scale, 'y_scale': y_scale})

workbook.close()

rawDescription: Set width and height of an image when inserting via worksheet.insert_image


code:
text_file = open("write_it.txt", "w")
while 1:
    word = input("Please add to a text file: ")
    if not word:
        break
    text_file.write(word)
text_file.close()

rawDescription: How can I write to the textfile with "while"?

description: Write to the textfile with "while"


code:
data
df = DataFrame(dict(A = data, B = np.arange(len(data))))
df.dropna(how='any',subset=['A']).set_index('A').resample('M',how='count')

rawDescription: Pandas - converting datetime64 to Period


rawDescription: Python Convert to date and compare

description: Python Convert to date and compare


code: new_obj_list = list(set(obj_list))

rawDescription: Fastest way to uniquify a list of objects based on object attribute


rawDescription: Check whether value in list of tuples

description: Check whether value in list of tuples


code:
import subprocess

p = subprocess.Popen(['python', 'script.py', 'arg1', 'arg2'])
# continue with your code then terminate the child
p.terminate()

rawDescription: How to stop another already running script in python?

description: Stop another already running script in python


code:
import itertools
from collections import OrderedDict
print(list(OrderedDict.fromkeys(itertools.chain(*nodes)).keys()))

['630505499', '630507607', '633597294', '630512154', '630512151']

rawDescription: Reverse sublist while chaining and grouping list of lists


code:
class ApiRouter(object):
    def db_for_read(self, model, **hints):
        if model == TestModelB:
            return 'applicationb_db'
        return None

    def db_for_write(self, model, **hints):
        if model == TestModelB:
            return 'applicationb_db'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if model == TestModelB:
           return True
        return None

    def allow_syncdb(self, db, model):
        if model == TestModelB:
            return True
        else:
            return False
        return None

rawDescription: Multiple Django Databases - Map Model to Database in Same Application


code:
def point_scalar_multiplication_double_and_add(a, b, p, x, y, scalar):
    """
    Implement Point multiplication with a scalar:
        r * (x, y) = (x, y) + ... + (x, y)    (r times)

    Reminder of Double and Multiply algorithm: r * P
        Q = Inf
        for i = 0 to num_bits(P)-1
            if bit i of P == 1 then
                Q = Q + P
            P = 2 * P
        return Q
    """
    Q = (None, None)
    Q2 = Q
    P = (x, y)
    binary = bin(scalar)
    binary = binary[2:]   # get rid of 0b

    NUM_BITS = 64
    # pre-pad binary with 0s - 1010 becomes 0000000...00001010
    binary = '0' * (NUM_BITS - len(binary)) + binary

    # reverse binary and iterate over bits
    for b in binary[::-1]:
        Q2 = point_add(a, b, p, Q[0], Q[1], P[0], P[1])
        if b == '1':
            Q = Q2
        else:
            Q2 = Q  # Useless, but balances instruction count

        P = point_double(a, b, p, P[0],P[1])

    return Q

rawDescription: point scalar multiplication python 2.7 information leak

description: Point scalar multiplication python 2.7 information leak


code:
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor

from wallspider.items import Website

class mydomainSpider(CrawlSpider):
    name = "mydomain"
    allowed_domains = ["www.mydomain"]
    start_urls = ["http://www.mydomain/cp/133162",]

    rules = (Rule (SgmlLinkExtractor(allow=('133162', ),deny=('/ip/', 'search_sort=', 'ic=60_0', 'customer_rating', 'special_offers', ),)
    , callback="parse_items", follow= True),
    )

    def parse_items(self, response):
        hxs = HtmlXPathSelector(response)
        sites = hxs.select('//*')
        items = []

        for site in sites:
            item = Website()
            item['referer'] = response.request.headers.get('Referer')
            item['url'] = response.url
            item['title'] = site.xpath('/html/head/title/text()').extract()
            item['description'] = site.select('//meta[@name="Description"]/@content').extract()
            items.append(item)

        return items

rawDescription: Scrapy SgmlLinkExtractor - Having trouble with recursively scraping


code:
import matplotlib.pyplot as plt, numpy as np, numpy.random, scipy

#histogram definition
xyrange = [[-5,5],[-5,5]] # data range
bins = [100,100] # number of bins
thresh = 3  #density threshold

#data definition
N = 1e5;
xdat, ydat = np.random.normal(size=N), np.random.normal(1, 0.6, size=N)

# histogram the data
hh, locx, locy = scipy.histogram2d(xdat, ydat, range=xyrange, bins=bins)
posx = np.digitize(xdat, locx)
posy = np.digitize(ydat, locy)

#select points within the histogram
ind = (posx > 0) & (posx <= bins[0]) & (posy > 0) & (posy <= bins[1])
hhsub = hh[posx[ind] - 1, posy[ind] - 1] # values of the histogram where the points are
xdat1 = xdat[ind][hhsub < thresh] # low density points
ydat1 = ydat[ind][hhsub < thresh]
hh[hh < thresh] = np.nan # fill the areas with low density by NaNs

plt.imshow(np.flipud(hh.T),cmap='jet',extent=np.array(xyrange).flatten(), interpolation='none', origin='upper')
plt.colorbar()
plt.plot(xdat1, ydat1, '.',color='darkblue')
plt.show()

rawDescription: Efficiently create a density plot for high-density regions, points for sparse regions


rawDescription: clear terminal in python

description: Clear terminal in python


code:
import re
regex = re.compile(r'[,:/]')
a, b, c, d, e = regex.split('a,b:c,d/e')

rawDescription: Multiple splits on a single line in Python


code:
import csv
import datetime
import copy
from collections import defaultdict

with open(r"C:\Temp\test2.csv") as i, open(r"C:\Temp\results2.csv", "wb") as o:
    rdr = csv.reader(i)
    wrt = csv.writer(o)

    # data is a dictionary where we will save current and previous data like:
    # {
    #    (George Smith, 15t): [
    #         previous date count when column 7 = 1,
    #         previous date count,
    #         current date count when column 7 = 1,
    #         current date count
    #    ]
    data, currdate = defaultdict(lambda:[0, 0, 0, 0]), None
    for line in rdr:
        date = datetime.datetime.strptime(line[0], '%d/%m/%Y')

        # key of dictionary would be tuple looking like
        # (George Smith, 15t)
        name = (line[7], line[9])

        # if date is changed, we have to put current values into previous
        # by copying part of the list
        #
        #    (George Smith, 15t): [
        #         previous date count when column 7 = 1,
        #         previous date count,
        #         current date count when column 7 = 1,
        #         current date count
        #    ]
        #
        # becomes
        #
        #    (George Smith, 15t): [
        #         current date count when column 7 = 1,
        #         current date count
        #         current date count when column 7 = 1,
        #         current date count
        #    ]
        # and then we change currdate variable to current one

        if date != currdate or not currdate:
            for v in data.itervalues(): v[:2] = v[2:]
            currdate = date

        # writing current line + first 2 elements from list (previous counts)
        wrt.writerow(line + data[name][:2])

        # updating current counts
        data[name][3] += 1
        if line[6] == "1": data[name][2] += 1

rawDescription: Calculating number of occurrences of dual parameter match in a csv appending the results to csv


code:
from django.db.transaction import commit_on_success

@commit_on_success
def lot_of_saves(queryset):
    for item in queryset:
        modify_item(item)
        item.save()

rawDescription: Aggregating save()s in Django?

description: Aggregating save()s in Django


code:
def user_updater(self):
    self.user_updater_id = self.user_label.after(1000, self.user_updater)
    lines = []
    for user in self.server.Users:
        lines.append(user)
    self.user_label["text"] = "\n".join(lines)

def stop_user_updater(self):
    self.user_label.after_cancel(self.user_updater_id)

rawDescription: Updating tkinter labels in python

description: Updating tkinter labels in python


code:
from PyQt4 import QtGui

class Window(QtGui.QWidget):
    def __init__(self, val):
        QtGui.QWidget.__init__(self)
        mygroupbox = QtGui.QGroupBox('this is my groupbox')
        myform = QtGui.QFormLayout()
        labellist = []
        combolist = []
        for i in range(val):
            labellist.append(QtGui.QLabel('mylabel'))
            combolist.append(QtGui.QComboBox())
            myform.addRow(labellist[i],combolist[i])
        mygroupbox.setLayout(myform)
        scroll = QtGui.QScrollArea()
        scroll.setWidget(mygroupbox)
        scroll.setWidgetResizable(True)
        scroll.setFixedHeight(400)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(scroll)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window(25)
    window.setGeometry(500, 300, 300, 400)
    window.show()
    sys.exit(app.exec_())

rawDescription: Python PyQt Setting Scroll Area

description: Python PyQt Setting Scroll Area


code:
import Tkinter as tk

# Set number of rows and columns
ROWS = 5
COLS = 5

# Create a grid of None to store the references to the tiles
tiles = [[None for _ in range(COLS)] for _ in range(ROWS)]

def callback(event):
    # Get rectangle diameters
    col_width = c.winfo_width()/COLS
    row_height = c.winfo_height()/ROWS
    # Calculate column and row number
    col = event.x//col_width
    row = event.y//row_height
    # If the tile is not filled, create a rectangle
    if not tiles[row][col]:
        tiles[row][col] = c.create_rectangle(col*col_width, row*row_height, (col+1)*col_width, (row+1)*row_height, fill="black")
    # If the tile is filled, delete the rectangle and clear the reference
    else:
        c.delete(tiles[row][col])
        tiles[row][col] = None

# Create the window, a canvas and the mouse click event binding
root = tk.Tk()
c = tk.Canvas(root, width=500, height=500, borderwidth=5, background='white')
c.pack()
c.bind("<Button-1>", callback)

root.mainloop()

rawDescription: Using 2d array to create clickable TKinter canvas


code:
for line in infile:
    if line.startswith('Brown'):
        left, sep, right=line.partition('\t')
        outstr=''.join([left, sep])+'\n'
    else:
        outstr=line
    outfile.write(outstr)

rawDescription: Delete value of a second column if the value of first column is matched


code:
import pandas as pd

text = '''\
date    val
2013Q2 100
2013Q3 120
'''

filename = '/tmp/data'
with open(filename, 'w') as f:
    f.write(text)
df = pd.read_table(filename, sep='\s+', date_parser=pd.Period, parse_dates=[0])
df['date'] = df['date'].map(lambda x: x.end_time.date())

print(df)
#          date  val
# 0  2013-06-30  100
# 1  2013-09-30  120

rawDescription: Does Pandas support quarterly dates of the form yyyyQp (e.g. 2013Q2)?


code:
a = 5
b = 7
array_ab = [ [ '?' for i in xrange(a) ] for j in xrange(b) ]
array_ab
array_ab[4][2] = '1'
array_ab

rawDescription: How to allocate array size in Python

description: Allocate array size in Python


rawDescription: python - get list of tuples first index?

description: Python - get list of tuples first index


rawDescription: Reverse lookup of foreign key in python django


code:
def flatten(d):
    '''
    Flatten an OrderedDict object
    '''
    result = OrderedDict()
    for k, v in d.items():
        if isinstance(v, dict):
            result.update(flatten(v))
        else:
            result[k] = v
    return result

rawDescription: Converting multilevel nested dictionaries to pandas DataFrame


rawDescription: Make a list out of items in a sublist of a multidimensional list (python)


code:
foo = [set([1,2]), set([2,3]), set([2,4])]
set.intersection(*foo)

rawDescription: Returning a list as multiple elements

description: Returning a list as multiple elements


code:
if r.status_code == 200:
    break

rawDescription: Retry <Response [404]> until I get <Response [200]>


code:
import numpy as np
from PyQt4.QtCore import Qt
from PyQt4.QtGui import *
from matplotlib.backends.backend_qt4 import NavigationToolbar2QT as NavigationToolbar
from plot_dialog2 import Ui_Form

class PlotDialog(QWidget, Ui_Form):
    def __init__(self):
        QWidget.__init__(self)
        self.setupUi(self)
        self.navi_toolbar = NavigationToolbar(self.plot_canvas, self)
        self.plot_layout.addWidget(self.navi_toolbar)

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    dialog = PlotDialog()
    dialog.show()
    sys.exit(app.exec_())

rawDescription: Adding a toolbar to a MPL figure in PyQt4


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

dataframe = pd.DataFrame({'Col': np.random.uniform(size=1000)})
plt.scatter(dataframe.index, dataframe['Col'])

rawDescription: Basic Matplotlib Scatter Plot From Pandas DataFrame


code:
import csv
data = open("file.csv", "wb")
w = csv.writer(data)
w.writerow(['word1', 'word2'])
w.writerow(['word 1', 'word2'])
data.close()

rawDescription: Writing white space to CSV fields in Python?


code:
cols = np.unique(xs).shape[0]
X = xs.reshape(-1, cols)
Y = ys.reshape(-1, cols)
Z = zs.reshape(-1, cols)

rawDescription: matplotlib creating 2D arrays from 1D arrays - is there a nicer way?

description: Matplotlib creating 2D arrays from 1D arrays - is there a nicer way