code:
import pandas
testData=pandas.DataFrame([["Cats","Parrots","Sandstone"],["Dogs","Cockatiels","Marble"]],columns=["Mammals","Birds","Rocks"])
swapVals={("Cats","Parrots"):("Rats","Canaries")}
target=["Mammals","Birds"]

def swapper(in_row):
    temp =tuple(in_row.values)
    if temp in swapVals:
        return list(swapVals[temp])
    else:
        return in_row

testData[target] = testData[target].apply(swapper, axis=1)

testData

rawDescription: How do I search for a tuple of values in pandas?

description: Search for a tuple of values in pandas


code: form = SearchProjectForm(project, request.POST)

rawDescription: Expanding my search function

description: Expanding my search function


code:
print("How many teams would you like?")
numberteam = int(input("Number: "))
teams = {}
for i in range(numberteam):
    teams['Team{}'.format(i)] = []

rawDescription: Alternative to Using Expression as Variable?


code:
class FooForm(models.ModelForm):
    def __init__(self, *args, **kwargs):
        # you take the user out of kwargs and store it as a class attribute
        self.user = kwargs.pop('user', None)
        super(FooForm, self).__init__(*args, **kwargs)

    def clean_avatar(self):
        current_user = self.user
        # use current_user

rawDescription: How to access self.request.user in clean_avatar() in forms.py (django application)

description: Access self.request.user in clean_avatar() in forms.py (django application)


code:
# in yourapp/__init__.py
default_app_config = 'yourapp.apps.YourAppConfig'

rawDescription: Can you give a Django app a verbose name for use throughout the admin?

description: Give a Django app a verbose name for use throughout the admin


code:
@app.route('/charge', methods=['POST'])
def charge():
    access_token = session.get("stripe_token")
    if not access_token:
        raise MyMissingAccessTokenException()
        # Or whatever you want to do here

    # User has an access token - try to use it here

rawDescription: pass access_token to charge function from callback function

description: Pass access_token to charge function from callback function


code:
pat_dict = {
            'a/b/*/f.txt': ['a/b/c/f.txt', 'a/b/q/f.txt', 'a/b/c/d/f.txt','a/b/c/d/e/f.txt'],
            '/foo/bar/*': ['/foo/bar/baz', '/spam/eggs/baz', '/foo/bar/bar'],
            '/*/bar/b*': ['/foo/bar/baz', '/foo/bar/bar'],
            '/*/[be]*/b*': ['/foo/bar/baz', '/foo/bar/bar'],
            '/foo*/bar': ['/foolicious/spamfantastic/bar', '/foolicious/bar']

        }
for pat in pat_dict:
    print('pattern :\t{}\nstrings :\t{}'.format(pat,pat_dict[pat]))
    print('matched :\t{}\n'.format(list(glob_filter(pat_dict[pat],pat))))

rawDescription: Python glob but against a list of strings rather than the filesystem


code:
import random
inds = [i for i,_ in enumerate(s) if not s.isspace()]

print(random.sample(inds,3))

rawDescription: Python replace 3 random characters in a string with no duplicates


code:
def check_output(*popenargs, **kwargs):
    if 'stdout' in kwargs:
        raise ValueError('stdout argument not allowed, it will be overridden.')
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
    output, unused_err = process.communicate()
    retcode = process.poll()
    if retcode:
        cmd = kwargs.get("args")
        if cmd is None:
            cmd = popenargs[0]
        raise CalledProcessError(retcode, cmd, output=output)
    return output

rawDescription: In a Python 2.4 script, I would like to execute a os system call `ls -l` or `curl` for example and capture the output in a variable. How to do this?


code: any(pred(x) for x in lst)

rawDescription: Is there a Python equivalent of Ruby's 'any?' function?


rawDescription: Encode non-ascii char into UTF-16

description: Encode non-ascii char into UTF-16


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

y, x = np.mgrid[-10:10:20j, -10:10:20j]
land = np.hypot(x, y) < 7

fig, ax = plt.subplots()
ax.pcolormesh(land, cmap='gray', edgecolor='gray', antialiased=True)
plt.show()

rawDescription: Isolating coastal grids using convolving window (python)


code:
def str2bits(astr):
    for char in astr:
        n=ord(char)
        for bit in '{0:0=#10b}'.format(n)[2:]:
            yield int(bit)

rawDescription: Is there a better way to convert from decimal to binary in python?


code:
class Example(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('spam', univ.Integer()),
        namedtype.NamedType('eggs', univ.OctetString())
    )

rawDescription: Decode using ASN.1 where substrate contains some opaque data


rawDescription: Python String to Bool conversion yields True for all cases


code:
if ns0 not in safezone:
    safezone.append(ns0)

rawDescription: For Loop efficiency when using enumerate or other functions


code:
df = pandas.DataFrame(...)
for file in files:
    ...
    for line in file:
        ...
        df = df.append(...)

rawDescription: Why is my Python script so much slower than its R equivalent?


rawDescription: Format number number with specific mask regex python


code:
class A(object):
    def rec(self):
        return 1

class B(A):
    def rec(self):
        # __class__ is a cell that is only created if super() is in the method
        super()
        return self.rec_gen(__class__)

    def rec_gen(self, rec_class):
        return super(rec_class, self).rec() + 1

class C(B):
    def rec(self):
        # __class__ is a cell that is only created if super() is in the method
        super()
        return self.rec_gen(__class__)

rawDescription: Making a class method recognize which class context it's running in


code: arr = np.delete(arr, rows//2, axis=0)

rawDescription: How to make a large numpy array efficiently

description: Make a large numpy array efficiently


code:
import os
import pprint
P= ["E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin" , "E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BCAS_BD_Infrastructure"]
Cld1 = ['BGD_4_new_district', 'BGD_3_old_district', 'BGD_2_division', 'BGD_1_all', 'BGD_5_Upazilla', 'BGD_4_old_district', 'BGD_6_Union_court', 'BGD_6_Union', 'BD_exposed_coastal_area','BD_drought', 'BGD_1_River', 'BGD_1_River_detail', 'BD_international_bnd', 'BGD_1_River_1', 'BGD_7_Mauza', 'test', 'BGD_5_UpazillaAnno', 'BGD_4_new_districtAnno', 'BGD_4_new_districtAnno2']
Cld2 = ['BD_Health_Infrastructures_1', 'BD_Railway_Establishments_1', 'BGD_roads_1']

lis1 = [os.path.join(x, y) for x in P for y in Cld1]
lis2 = [os.path.join(x, y) for x in P for y in Cld2]

My_Full_Path = lis1 + lis2

pprint.pprint(My_Full_Path)

rawDescription: How can i make a list of directories from two list nested (parent and child)

description: Make a list of directories from two list nested (parent and child)


code: self.size_hint_x, self.size_hint_y = 0.3, 0.3

rawDescription: Issues with Scatter size and position

description: Issues with Scatter size and position


code:
result = result.reshape(a.shape)

[[1, 4]
 [4, 5]
 [2, 1]]

rawDescription: how to replace an array containing indices with values from another array?

description: Replace an array containing indices with values from another array


code:
#Get only unique indexes
unique_trans = list(set(df.index.get_level_values('Trans')))

rawDescription: Changing the columns in DataFrame with respect to values in other columns


code:
import locale
from functools import cmp_to_key

@cmp_to_key
def strcoll(a, b):
    if isinstance(a, str):
        a = a.decode('utf-8')
    if isinstance(b, str):
        b = b.decode('utf-8')
    return locale.strcoll(a, b)

rawDescription: Python sort order unicode/ansi differs

description: Python sort order unicode/ansi differs


rawDescription: Finding the collatz conjecture that uses n steps to get to one using python


code:
my_list = ["A", "X", "B", "X"]
list(mergefill(my_list, 'X', product('LE', repeat=2)))

rawDescription: Replacing List values sequentially with values from another list


code:
{'Content-Type': 'multipart/mixed;',
 'DKIM-Signature': 'v=1; a=rsa-sha256; c=relaxed/simple; d=mydomain.com;',
 'Date': 'Tue, 15 Mar 2016 04',
 'Feedback-ID': '19',
 'From': 'Kitemailer Newsletter <info@kitemailer.com>',
 'List-Unsubscribe': '<http',
 'MIME-Version': '1.0',
 'Message-ID': '<15106466-1b13-4d64-b220-15f05f4815b7-1458031171312@smtp.mydomain.com>',
 'Received': 'from 128.199.202.14 (unknown [128.199.202.14])',
 'Subject': 'KiteMailer | New Features this Week',
 'To': 'vipul4.j@tcs.com',
 'X-Amp-File-Uploaded': 'False',
 'X-Amp-Result': 'Clean',
 'X-IPAS-Result': 'A0CGBgCEyedW/3zwO4tdHgEBAg4BgklMUm2nXoJekBMBDYFmBxUFAQ2HGwI4FAEBAQEBAQFkJ4RLIAoTAQEECCwGSQMBCQICMTsFHASHJ10FCatgZ4RBAQSLKQaBD4REgkIBhlERAWqCNBOBJ5MJhEuBLwKEPogSAoIuhnOFYo1YgUUBAUKBNgwBgj5OB4kqgTIBAQE',
 'X-IronPort-AV': 'E=Sophos;i="5.24,338,1454956200";',
 'X-IronPort-Anti-Spam-Filtered': 'true',
 'X-IronPort-Anti-Spam-Result': 'A0CGBgCEyedW/3zwO4tdHgEBAg4BgklMUm2nXoJekBMBDYFmBxUFAQ2HGwI4FAEBAQEBAQFkJ4RLIAoTAQEECCwGSQMBCQICMTsFHASHJ10FCatgZ4RBAQSLKQaBD4REgkIBhlERAWqCNBOBJ5MJhEuBLwKEPogSAoIuhnOFYo1YgUUBAUKBNgwBgj5OB4kqgTIBAQE',
 'h=Date': 'From'}

rawDescription: Python email parse non multipart

description: Python email parse non multipart


code:
import random
from tkinter import *

store_items = random.sample(['sword','pickaxe','toothpick','hammer','torch','saw'], 5)

root = Tk()
canvas = Canvas(root)
canvas.pack()
XBASE, YBASE, DISTANCE = 10, 20, 20
for i, word in enumerate(store_items):  # <-- iterate words using `for` loop.
    canvas.create_text(
        (XBASE, YBASE + i * DISTANCE),
        text=word, anchor=W, fill='blue')
root.mainloop()

rawDescription: How to add text to a canvas from a list

description: Add text to a canvas from a list


code:
fruit [{'type': 'fruit', 'name': 'apple'}, {'type': 'fruit', 'name': 'pear'}]
vegetable [{'type': 'vegetable', 'name': 'parrot'}]

rawDescription: Create list with one key of list of dictionaries


code: [ www.domain.com ]

rawDescription: django-allauth logging in with Facebook token from iOS Device

description: Django-allauth logging in with Facebook token from iOS Device


code:
def is_solution(A, B, idx):
    return np.allclose(A[np.ix_(idx, idx)], B)

def find_idx_orig(A, B):
    index = []
    for j in range(len(B)):
        k = 0
        while k<len(A) and set(np.intersect1d(B[j],A[k])) != set(B[j]):
            k+=1
        index.append(k)
    return index

def find_idx_diag(A, B):
    index = []
    a = np.diag(A)
    b = np.diag(B)
    for j in range(len(b)):
        k = 0
        while a[j+k] != b[j] and k<len(A):
            k+=1
        index.append(k+j)
    return index

def counterexample():
    """
    Show find_idx_diag, find_idx_orig may not return the correct result
    """
    A = np.array([[1,2,0],
                  [2,1,0],
                  [0,0,1]])
    ans = [0,1]
    B = A[np.ix_(ans, ans)]
    assert not is_solution(A, B, find_idx_orig(A, B))
    assert is_solution(A, B, find_idx(A, B))

    A = np.array([[1,2,0],
                  [2,1,0],
                  [0,0,1]])
    ans = [1,2]
    B = A[np.ix_(ans, ans)]

    assert not is_solution(A, B, find_idx_diag(A, B))
    assert is_solution(A, B, find_idx(A, B))

counterexample()

rawDescription: Extract indices of intersecting array from numpy 2D array in subarray


code:
import weakref

class MyDescriptor(object):

    def __init__(self):
        self.instances = {} #instance -> CountingAttribute

    def __get__(self, inst, cls):
        if inst is None: return self.instances # operating on the class, we get the dictionary.
        i = id(inst)
        if i in self.instances:
            ca, wr = self.instances[i]
            if wr() is None: del self.instances[i]
        if i not in self.instances:
            ca = CountingAttribute()
            self.instances[i] = (ca, weakref.ref(inst))
        ca.count += 1
        return ca

rawDescription: Using descriptors in unhashable classes - python


code: p = numpy.vstack((x, y, z)).T

rawDescription: reshaping an arbitrary collection numpy arrays

description: Reshaping an arbitrary collection numpy arrays


code: np.allclose(f2, np.array(f1))

rawDescription: Numba : cell vars are not supported

description: Numba : cell vars are not supported


code: client = ndb.Key(YourUserModel, userId, Client, key_name).get()

rawDescription: NDB MyModel.get_by_id() cannot find entity by key_name


code:
print(re.findall('(.*?)', fileString, re.DOTALL))
# ['foo\nbaby foo\nbaby foo\nbaby foo\n', 'bar']

rawDescription: Python RegEx skipping the first few characters?


code:
depth = [0.0,1.1,2.0] * pq.m
depth2 = [0,1,1.1,1.5,2] * pq.m

s1 = pd.DataFrame(
        {'depth' : [x for x in depth]},
        index = depth)

s2 = extendAndInterpolate(s1, depth2)

rawDescription: Pandas interpolate data with units

description: Pandas interpolate data with units


code:
# Import futures library
from concurrent import futures

# executors.py will create our threadpools, and this can be shared around different python files
# which will not re-create 10 threadpools when we call it.
# we can a handful of executors for running synchronous tasks

# Create a 10 thread threadpool that we can use to call any synchronous/blocking functions
executor = futures.ThreadPoolExecutor(10)

rawDescription: Python Tornado - How to create background process?


code: button = ttk.Button(container, text='Button', command=layOut.updizzle)

rawDescription: Python 3.4/Tkinter cross class variable accessing


code:
def add_property(klass, name, key):
    def getter(self):
        return self.query_dict.get(key)
    def setter(self, value):
        self.query_dict[key] = value
    setattr(klass, name, property(getter, setter))

def set_properties(**mapping):
    def decorator(klass):
        for name, key in mapping.iteritems():
            add_property(klass, name, key)
        return klass
    return decorator

@set_properties(name=NAME_KEY, content_type=CONTENT_TYPE_KEY,
                hash=HASH_KEY, node_id=NODE_ID_KEY)
class SignedFileRequest(SignedRequest):
    def __init__(self, host, path, node_id = None, name=None, \
                 content_type=None, hash=None, ssl=False, expires=0):
        super(SignedFileRequest, self).__init__(host, path, ssl, expires)
        self.name = name
        self.content_type = content_type
        self.hash = hash
        self.node_id = node_id

rawDescription: How do I abbreviate this Python code using metaprogramming?

description: Abbreviate this Python code using metaprogramming


List of strings in ints ['123 121','42 23','23 23']


code:
def data_to_idxlists(data):
    u, ixs = np.unique(data, return_inverse=True)
    return {u: np.nonzero(ixs==i) for i, u in enumerate(u)}

rawDescription: Python: faster operation for indexing

description: Python: faster operation for indexing


code: max_value = max(stats.itervalues())

rawDescription: Generate a new dictionary from the maximum values in a dictionary with ties


rawDescription: Django Models: aggregate filter

description: Django Models: aggregate filter


code: static = static.split(' ', 4)[-1]

rawDescription: very specific substring retrieval and split

description: Very specific substring retrieval and split


code:
from django import template

register = template.Library()

@register.daterange
def daterange(start_date, end_date):
    start_date = datetime.date.today()
    end_date = start_date + datetime.timedelta(days=5)
    for n in range(int((end_date - start_date).days)):
        yield start_date + timedelta(n)

rawDescription: How to loop over datetime range in django templates

description: Loop over datetime range in django templates


code:
import itertools as it           #1

def cycle_first_n(lst, n):
    """ cycles through first n elements of the list """
    c = it.cycle(lst[:n])        #2
    for idx in xrange(len(lst)): #3
        sfx = idx / n
        yield c.next() + ('.' + str(sfx) if sfx > 0 else '')  #4

rawDescription: python how to efficiently cycle through few elements in a list

description: Python how to efficiently cycle through few elements in a list


code:
ret = "[{'itemkey1': 'item1', 'itemkey2': 'item2'}, {'itemkey1': 'item1', 'itemkey2': 'item2'}]\n"
ast.literal_eval(ret)

rawDescription: Get list of dicts from another python script?


rawDescription: how to move identical elements in numpy array into subarrays

description: Move identical elements in numpy array into subarrays


rawDescription: Pythonic way to break out of iteration over tuples when one value changes


code:
l =['Number', 1,2,3,4]
from shutil import move
from tempfile import NamedTemporaryFile
with open('in.csv') as csvfile, NamedTemporaryFile("w",dir=".", delete=False) as temp:
    r = csv.reader(csvfile)
    wr = csv.writer(temp,delimiter=" ")
    for row,new in zip(r,l):
        wr.writerow(row+[new])

move(temp.name,"in.csv")

rawDescription: Adding a new column on CSV with Python

description: Adding a new column on CSV with Python


code:
from PIL import Image
image_file = Image.open("cat-tied-icon.png") # open colour image
image_file = image_file.convert('1') # convert image to black and white
image_file.save('/tmp/result.png')

rawDescription: Convert RGB to black OR white

description: Convert RGB to black OR white


code:
After
from babel.numbers import format_decimal
x = format_decimal(123, format='#0.00', locale='de')

rawDescription: Python: Access Posix' locale database without setlocale()


rawDescription: To zip a list with more than 2 lists

description: To zip a list with more than 2 lists


code:
colLabels=("Structure", "Energy", "Density")
nrows, ncols = len(clust_data)+1, len(colLables)
hcell, wcell = 0.3, 1.
hpad, wpad = 0, 0
fig=plt.figure(figsize=(ncols*wcell+wpad, nrows*hcell+hpad))
ax = fig.add_subplot(111)
ax.axis('off')
#do the table
the_table = ax.table(cellText=clust_data,
          colLabels=colLabels,
          loc='center')
plt.savefig("table.png")

rawDescription: Creating tables in matplotlib

description: Creating tables in matplotlib


code:
bar_df = pd.DataFrame(bar, columns=['PLACE','Xcords','Ycords'])
bar_df
pd.merge(foo[['PLACE','TIME']], bar_df, on='PLACE', how='left')

rawDescription: Conditionally populate dataframe row from tuple values when dataframe and tuple values match


code:
def writerows(self, rowdicts):
    rows = []
    for rowdict in rowdicts:
        rows.append(self._dict_to_list(rowdict))
    return self.writer.writerows(rows)

rawDescription: csv.writer.writerows takes iterator?

description: Csv.writer.writerows takes iterator


code:
@app.context_processor
def utility_processor():
    def subdivide_list(list_to_group, group_size):
        return [list_to_group[i:i+group_size] for i  in range(0, len(list_to_group), group_size)]
return dict(subdivide_list=subdivide_list)

rawDescription: Loop with counter

description: Loop with counter


code:
from module1 import cool_func as cool
from module2 import cool_func as cooler
cool()  #module1
cooler() #module2

rawDescription: Some confusion regarding imports in Python


rawDescription: Python Regex not returning phone numbers


code:
import random
candidates = []
n = 0
with open(textFile) as inFile:
    for line in inFile:
        if valid_candidate(line): # use regex here
            n += 1
            if n <= x:
                candidates.append(line)
            else:
                i = random.randrange(n)
                if i < x:
                    candidates[i] = line
results = []
for line in candidates:
    analysisResult = analyze_the_match(line)
    results.append(analysisResult)

rawDescription: How to efficiently read lines of large text file in Python in different orders: a) random line each time, b) starting in middle...?

description: Efficiently read lines of large text file in Python in different orders: a) random line each time, b) starting in middle...


code: fp=fopen(ptr,p);

rawDescription: How to open ssl socket using certificate stored in string variables in python

description: Open ssl socket using certificate stored in string variables in python


code:
@event.listens_for(User, "after_insert")
def update_hash(mapper, connection, target):
    user_table = mapper.local_table
    connection.execute(
          user_table.update().
              values(hash=hash_function(target.id)).
              where(user_table.c.id==target.id)
    )

rawDescription: Automatically hash primary key and make it persistent upon flush/commit with sqlalchemy


code:
@jit(nopython=True)
def roots2(a,b,c):
    bp=b/2
    delta=bp*bp-a*c
    u1=(-bp-delta**.5)/a
    u2=-u1-b/a
    return u1,u2

rawDescription: fastest way to find the smallest positive real root of quartic polynomial 4 degree in python

description: Fastest way to find the smallest positive real root of quartic polynomial 4 degree in python


code:
def odd_even_sieve(x):
    output = x[:]
    even_index, odd_index = 0, 1
    for value in x:
        if value % 2 == 0:
            output[even_index] = value
            even_index += 2
        else:
            output[odd_index] = value
            odd_index += 2
    return output

odd_even_sieve([3, 2, 5, 6, 4, 7, 8, 9, 10, 11])
odd_even_sieve([19, 11, 23, 16, 18, 20])

rawDescription: Move odd and even numbers in list to odd and even positions


code: documents = [" ".join(sentence) for sentence in documents]

rawDescription: How to stem words in python list?

description: Stem words in python list


code:
class NewRequest(BaseRequest):
    name = 'N-1'

    def response(self):
        return "new"

manager = RequestManager()
manager.register_request(NewRequest)
request = manager.generate_request('N-1')
if request is not None:
    print(request.response()) # "new"

rawDescription: Creating a set of Python classes

description: Creating a set of Python classes


code:
list = ['deviceA;portZ,portchanC', ... , 'deviceE;portX,portchan2']
output = [ele.split(';')[0] for ele in list]
uniques = set(output)

rawDescription: Python: Cutting column from list and storing as list


code:
panelOne = wx.Panel(self)
panelTwo = wx.Panel(self)
panelThree = MyPanels(parent, id)

rawDescription: Destroying and Recreating a Panel wx.Python


code:
import copy
obj = object()
original = {x: [obj] * 10 for x in xrange(10)}
copy1 = {x:v[:] for x,v in original.iteritems()}
copy2 = copy.deepcopy(original)
copy1[0][0] is original[0][0]
copy2[0][0] is original[0][0]

rawDescription: Is python deepcopy more efficient than custom code, or less efficient/predictable?


code: schema = {u'fields': [{u'type': u'STRING', u'name': u'date', u'mode': u'NULLABLE'}, {u'type': u'INTEGER', u'name': u'unix_date', u'mode': u'NULLABLE'}, {u'type': u'STRING', u'name': u'init_cv_date', u'mode': u'NULLABLE'}, {u'type': u'STRING', u'name': u'end_cv_date', u'mode': u'NULLABLE'}, {u'type': u'INTEGER', u'name': u'days_trained', u'mode': u'NULLABLE'}, {u'type': u'INTEGER', u'name': u'days_validated', u'mode': u'NULLABLE'}, {u'type': u'INTEGER', u'name': u'navigated_score', u'mode': u'NULLABLE'}, {u'type': u'INTEGER', u'name': u'carted_score', u'mode': u'NULLABLE'}, {u'type': u'INTEGER', u'name': u'purchased_score', u'mode': u'NULLABLE'}, {u'type': u'STRING', u'name': u'description', u'mode': u'NULLABLE'}, {u'type': u'STRING', u'name': u'metric', u'mode': u'NULLABLE'}, {u'type': u'FLOAT', u'name': u'result', u'mode': u'NULLABLE'}]}

rawDescription: Bigquery - Insert new data row into table by python


rawDescription: Python Pandas: Counting the Frequency of unique values over all Columns


code:
from distutils.core import setup
from Cython.Distutils import build_ext
setup(
      cmdclass = {'build_ext': build_ext},
      name = 'My app',
      ext_modules = cythonize("yourext.pyx"),
)

rawDescription: Create a DLL exposing python code

description: Create a DLL exposing python code


rawDescription: python print to file prints correctly in console but not in file

description: Python print to file prints correctly in console but not in file


rawDescription: Finding a set of indices that maps the rows of one NumPy ndarray to another


code:
ctr = np.arange(N)
list1 = n1 - n1[0] - ctr * c1
list2 = n2 - n2[0] - ctr * c2
list3 = c3 - ctr
list4 = c4 - ctr

rawDescription: Optimizing Python for loop

description: Optimizing Python for loop


code: newlist = [dict for dict in mylist if dict["category"] == "insect"]

rawDescription: Select items with specific field values from a list of dictionaries (similar to SQL SELECT command)


code:
with open(filename) as f:
    urls = f.readlines()

rawDescription: live link checking with Python can't get it to run from file

description: Live link checking with Python can't get it to run from file


code: c.groupby(level='time').apply(lambda x: x - m).mean(axis=1)

rawDescription: Applying transformations to dataframes with multi-level indices in Python's pandas


code:
from scipy.sparse import csr_matrix
from itertools import chain

def sparse_solution(X, nbrs):
    items = (([i]*len(col), col, [1]*len(col)) for i, col in nbrs.items())
    rows, cols, data = (np.array(list(chain(*a))) for a in zip(*items))
    M = csr_matrix((data, (rows, cols)))
    return M.dot(X)

np.allclose(original_solution(X, nbrs),
            sparse_solution(X, nbrs))
# True

rawDescription: Code optimization - number of function calls in Python


code:
SOCIAL_AUTH_PIPELINE = (

    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.user.get_username',
    'social.pipeline.social_auth.associate_by_email',
    'social.pipeline.user.create_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'myapp.auth_pipeline.populate_info',
)

rawDescription: Python Social Auth: Not change info for existant user when associating account


code:
def our_permutations(n):
    x = list(range(1, n+1))
    perm_list = [x]
    final_perm = x[::-1]

    while x != final_perm:
        x = x[:]
        istar = n-2
        while x[istar] > x[istar+1]:
            istar -= 1
        jstar = n-1
        while x[jstar] < x[istar]:
            jstar -= 1
        x[istar],x[jstar] = x[jstar],x[istar]
        if istar+1 != n-1:
            a = x[istar+1:]
            a = a[::-1]
            x = x[:istar+1] + a
        perm_list += [x]

    return perm_list

rawDescription: Generate all permutations of a list in lexicagraphical order


code:
def make_adder(increment):
    def adder(number):
        return number + increment
    adder.__name__ = "adder(%s)" % increment
    return adder

rawDescription: In python, when you pass internally defined functions into other functions, how does it keep the variables?


code:
def getSum(tab,size):
    # Define kernel and perform convolution to get such sliding windowed summations
    kernel = np.ones((size,size),dtype=tab.dtype)
    return convolve2d(tab, kernel, mode='valid').sum()

rawDescription: How can I vectorize and speed up this large array calculation?

description: Vectorize and speed up this large array calculation


code:
import csv
import re
import time
import urllib2
from urlparse import urljoin
from bs4 import BeautifulSoup

BASE_URL = 'http://omaha.craigslist.org/sys/'
URL = 'http://omaha.craigslist.org/sya/'
FILENAME = 'C:/Python27/Folder/Folder/Folder/craigstvs.txt'

opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
soup = BeautifulSoup(opener.open(URL))

with open(FILENAME, 'a') as f:
    writer = csv.writer(f, delimiter=';')
    for link in soup.find_all('a', class_=re.compile("hdrlnk")):
        timeset = time.strftime("%m-%d %H:%M")

        item_url = urljoin(BASE_URL, link['href'])
        item_soup = BeautifulSoup(opener.open(item_url))

        # do smth with the item_soup? or why did you need to follow this link?

        writer.writerow([timeset, link.text, item_url])

rawDescription: I'm attempting to extract some links from craigslist using beautifulsoup but it's pulling the links 100 times rather than once


code:
ITEM_PIPELINES = {
    # ...
    # Last pipeline, because further changes won't be saved.
    'apps.scrapy.pipelines.ItemPersistencePipeline': 999
}

rawDescription: How to update DjangoItem in Scrapy

description: Update DjangoItem in Scrapy


code:
import collections

class NADict(object):
    def __getitem__(self, k):
        return 'NA'

    @staticmethod
    def instance():
        return NADict._instance

NADict._instance = NADict()

nadict = collections.defaultdict(NADict.instance, {
                'a': {'j':1,'k':2},
                'b': {'j':2,'k':3},
                'd': {'j':1,'k':3}
            })

rawDescription: Python dict.get() with multidimensional dict


code:
def dict_intersection(d1, d2):
    return dict((key, d2[key] or d1[key]) for key in frozenset(d1) & frozenset(d2))

rawDescription: intersection_update for dicts?

description: Intersection_update for dicts


code: Word(printables, excludeChars=',.')

rawDescription: python pyparsing word excludeChars

description: Python pyparsing word excludeChars


code:
cursor.execute('INSERT INTO foo (username,password) VALUES (?,?)',
               ('blah','blah'))

cursor2=connection.cursor()
cursor2.execute('INSERT INTO foo (username,password) VALUES (?,?)',
               ('blah','blah'))

print(cursor2.lastrowid)
# 3
print(cursor.lastrowid)
# 2

cursor.execute('INSERT INTO foo (id,username,password) VALUES (?,?,?)',
               (100,'blah','blah'))
print(cursor.lastrowid)
# 100

rawDescription: How to retrieve inserted id after inserting row in SQLite using Python?

description: Retrieve inserted id after inserting row in SQLite using Python


code:
class StateMachine(object):

    dispatcher = StateDispatcher()
    state = None

    @dispatcher.register('test')
    def test(self):
        print('Hello, World!', self.state)

    @dispatcher.register('working')
    def do_work(self):
        print('Working hard, or hardly working?', self.state)

rawDescription: Singledispatch based on value instead of type


code:
import itertools as it

def exp_for_factor_in_factorial(factor, n):
    total = 0
    for e in it.count(1):
        if factor ** e > n:
            break
        total += n // factor**e
    return total

rawDescription: Find the number of divisors of (N!)^2

description: Find the number of divisors of (N!)^2


code:
a = A()
b = B()
type(a.fp1)
type(b.fp1)

rawDescription: Python function pointer in class __init__


code:
res = function()
print(*res)

rawDescription: Print to stdout with space's between result?


code:
from django.utils import timezone

from myproject.profiles.models import Profile

class UpdateLastActivityMiddleware(object):
    def process_view(self, request, view_func, view_args, view_kwargs):
        assert hasattr(request, 'user'), 'The UpdateLastActivityMiddleware requires authentication middleware to be installed.'
        if request.user.is_authenticated():
            Profile.objects.filter(user__id=request.user.id) \
                           .update(last_activity=timezone.now())

rawDescription: Django: How can I check the last activity time of user if user didn't log out?


code:
params = {'a': 1, 'b': 2, 'c': 3}
result = {k:get_value(v) for k,v in params.iteritems()}
# result == {'a': get_value(1), 'b': get_value(2), 'c': get_value(3)}

rawDescription: Working with tuples in python?

description: Working with tuples in python


rawDescription: Remove unicode in Python from databse?

description: Remove unicode in Python from databse


code:
def normalize_version(padded_version):
    return '.'.join(bit.lstrip('0') for bit in padded_version.split('.'))

normalize_version('00010')
normalize_version('00002.00001.00010')

rawDescription: How do I store version number strings in a django database so they are correctly comparable/sortable

description: Store version number strings in a django database so they are correctly comparable/sortable


code: a

rawDescription: Find if value exists in multiple lists

description: Find if value exists in multiple lists


rawDescription: How to delete [NaN] from a list of lists?

description: Delete [NaN] from a list of lists


code: x = ttkcal.selection

rawDescription: python calendar widget - return the user-selected date

description: Python calendar widget - return the user-selected date


rawDescription: Can I sort text by its numeric value in Python?

description: Sort text by its numeric value in Python


rawDescription: How to convert list into set in pandas?

description: Convert list into set in pandas


rawDescription: How to get all substrings in a list of characters (python)

description: Get all substrings in a list of characters (python)


code:
resource['contents'][media_type] = []
for index in (0, 2):
    item = row[i]
    if item is not None:
        resource['contents'][media_type].append(item.toPython())

rawDescription: Is this a valid use of a conditional expression?


code:
# -*- coding: utf-8 -*-

class MyDict(dict):
    def __str__(self):
        return "{"+", ".join(["%s: %s" % (key, self[key]) for key in self])+"}"

a = {0:"Velmi žluťoučký kůň"}
b = MyDict({0:"Velmi žluťoučký kůň"})
c = "Velmi žluťoučký kůň"
print(a)
print(b)
print(c)

rawDescription: how to print a dict which has japanese word using python

description: Print a dict which has japanese word using python


rawDescription: Why isn't this even function's FFT real?

description: Why isn't this even function's FFT real


rawDescription: Python dict how to create key or append an element to key?


code:
import multiprocessing
pool = multiprocessing.Pool()
pool._processes

rawDescription: Get number of workers from process Pool in python multiprocessing module


rawDescription: scatter plot python with several y values

description: Scatter plot python with several y values


code:
# Get linear indices
n = array.shape[1]
lidx = np.hstack(([id*n+np.array(item) for id,item in enumerate(indices)]))

# Get successive differentiations
diffs = array.cumsum(1).ravel()[lidx] - array.ravel()[lidx]

# Get previous group's offsetted summations for each row at all
# indices positions across the entire 2D array
_,idx = np.unique(lidx/n,return_index=True)
offsetted_diffs = np.diff(np.append(0,diffs))
offsetted_diffs[idx] = diffs[idx]

# Get a copy of input array and place previous group's offsetted summations
# at indices. Then, do cumulative sum which will create a boundary like
# effect with those offsets at indices positions.
arrayc = array.copy()
arrayc.ravel()[lidx] -= offsetted_diffs
out = arrayc.cumsum(1)

rawDescription: Find cumsum of subarrays split by indices for numpy array efficiently


code: subprocess.check_output(['scp', '-r', '-i', id_file, src, dest])

rawDescription: Additional processing of arugments for a function in Python


code:
related_fieldnames = [f.get_accessor_name() for f in Server._meta.get_all_related_objects()]
res = Server.objects.prefetch_related(*related_fieldnames).first()
res.serverinfo_set # Won't need another query
res.serverdnsrecord_set # Won't need another query

rawDescription: Django select all models with foreign key


code:
from django.utils.decorators import method_decorator
from django.views.decorators.cache import never_cache

class NeverCacheMixin(object):
    @method_decorator(never_cache)
    def dispatch(self, *args, **kwargs):
        return super(NeverCacheMixin, self).dispatch(*args, **kwargs)

rawDescription: Ensuring django template is never cached by any browser


code: sprite1 = MySprite('test sprite')

rawDescription: Pygame get name of colided sprite

description: Pygame get name of colided sprite


code: (min_post_date <= begin <= max_post_date) or (min_post_date <= end <= max_post_date)

rawDescription: check date range with only month and year in Python

description: Check date range with only month and year in Python


code:
import os
os.sys.path

rawDescription: importing from venv folder with same file name

description: Importing from venv folder with same file name


code:
import peewee as pw
from BaseModel import BaseModel

class Taco(BaseModel):
    has_cheese = pw.BooleanField()

rawDescription: Modularizing peewee

description: Modularizing peewee


rawDescription: loading tab separated text files with multipled data into arrays in python

description: Loading tab separated text files with multipled data into arrays in python


code:
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.support import expected_conditions as EC

class wait_for_visible_element_text_to_contain(object):
    def __init__(self, locator, text):
        self.locator = locator
        self.text = text

    def __call__(self, driver):
        try:
            elements = EC._find_elements(driver, self.locator)
            for element in elements:
                if self.text in element.text and element.is_displayed():
                    return element
        except StaleElementReferenceException:
            return False

rawDescription: Why does selenium return an empty text field?


code:
args, varargs, varkw, defaults = inspect.getargspec(func)
result = []
if defaults:
    firstdefault = len(args) - len(defaults)
for i, arg in enumerate(args):
    if defaults and i >= firstdefault:
        result.append((arg, defaults[i - firstdefault]))
    else:
        result.append((arg,))

rawDescription: Getting respective *args, **kwargs from collection of functions, then filling in the irregular 2d structure with provided 1d arguments


rawDescription: Is there a bad interaction, in numpy 1.7.1, between datetime64 and vectorize?


code:
for i in FUNNEL_STEP_ARRAY:
    y = db.external_funnel.find({
        "step_id": i,
        "client_timestamp": {"$regex": "2013-10-25T"},
        "application_id":"6083502215600857441"
    }).distinct("user_id")
    print(len(y))

rawDescription: For Loop returning blank values for query values


code:
l
lrounded = [ round(i, 8) for i in l ]

rawDescription: Relative frequencies of a list of numbers


rawDescription: Split when character changes in Python

description: Split when character changes in Python


rawDescription: How to store lists over criteria in Python?

description: Store lists over criteria in Python


rawDescription: How to write to a specific line in a text file

description: Write to a specific line in a text file


code:
# main.py
import os

# get the current directory dynamically
base_dir = os.getcwd()
# create the file path
file_path = os.path.join(base_dir, 'bin',  'file.exe')
# run the file
os.system(file_path)

rawDescription: Python Packaging: Requiring non-python binaries or installing them if they are not there?


code:
def get(self, block=True, timeout=None):
    self.not_empty.acquire()
    try:
        if not block:
            if not self._qsize():
                raise Empty
        elif timeout is None:
            while not self._qsize():
                self.not_empty.wait()  # This is where the code blocks
        elif timeout < 0:
            raise ValueError("'timeout' must be a non-negative number")
        else:
            endtime = _time() + timeout
            while not self._qsize():
                remaining = endtime - _time()
                if remaining <= 0.0:
                    raise Empty
                self.not_empty.wait(remaining)
        item = self._get()
        self.not_full.notify()
        return item
    finally:
        self.not_empty.release()

def put(self, item, block=True, timeout=None):
    self.not_full.acquire()
    try:
        if self.maxsize > 0:
            if not block:
                if self._qsize() == self.maxsize:
                    raise Full
            elif timeout is None:
                while self._qsize() == self.maxsize:
                    self.not_full.wait()
            elif timeout < 0:
                raise ValueError("'timeout' must be a non-negative number")
            else:
                endtime = _time() + timeout
                while self._qsize() == self.maxsize:
                    remaining = endtime - _time()
                    if remaining <= 0.0:
                        raise Full
                    self.not_full.wait(remaining)
        self._put(item)
        self.unfinished_tasks += 1
        self.not_empty.notify()  # This is what wakes up `get`
    finally:
        self.not_full.release()

rawDescription: efficient python raw_input and serial port polling

description: Efficient python raw_input and serial port polling


code:
('foo', 'foo text')
('foo', '\n  data text\n    ')
('bar', '\n      bar text\n      ')
('baz', 'text')
('baz', 'text')
('baz', '\n      bar text\n    ')
('bar', '\n   data text\n')

rawDescription: How to get owner element of a text node?

description: Get owner element of a text node


rawDescription: Slicing array in regions - Python

description: Slicing array in regions - Python


rawDescription: How to extract classes out of Python modules?

description: Extract classes out of Python modules


code: df["Score"] = df["Score"].astype(float)

rawDescription: Pandas summing up different data types using counts and conditions


code:
ff = [functools.partial(lambda i: i, x) for x in range(100)]
ff[12]()
ff[54]()

rawDescription: Python: lambda function behavior with and without keyword arguments?


code:
import math

def round_up_to_even(f):
    return math.ceil(f / 2.) * 2

rawDescription: python 2.7: round a float up to next even number

description: Python 2.7: round a float up to next even number


code:
assert id_for_value('10') == '*'
assert id_for_value('499') == 'a'
assert id_for_value('703') == 'b'
assert id_for_value('7007') == 'c'
assert id_for_value('7017') == 'd'
assert id_for_value('76') == id_for_value('83') == '*'
assert id_for_value('857') == 'e'
assert id_for_value('8716') == 'a'

rawDescription: Building a "complete" number range w/out overlaps


code:
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'public'),
)

WEBPACK_LOADER = {
    'DEFAULT': {
        'BUNDLE_DIR_NAME': 'js/',
        'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json')
    }
}

rawDescription: decoupled frontend and backend with Django, webpack, reactjs, react-router

description: Decoupled frontend and backend with Django, webpack, reactjs, react-router


rawDescription: How to find all kinds of website tags by python and beautifulsoup?

description: Find all kinds of website tags by python and beautifulsoup


code:
from __future__ import print_function
from treq import get
from twisted.internet.task import react

def handleResponse(response):
    """ Callback Function

    Once the response is recived, display the information.
    This is the part where I suspect people will have the most
    trouble wrapping their heads around since it's heavily
    dependent on deferreds (ie. futures or promises).
    """
    print('Code: %s\n' % response.code)

    print('Simple print:')
    response.content().addCallback(print)       # simple way to print on py2 & py3

    text = response.text()                      # returns a deferred
    text.addCallback(displayText)               # the way you should be handling responses, ie. via callbacks

def displayText(text):
    """ Callback Function

    Simply display the text. You would usually do more useful
    things in this call back, such as maniuplating the response
    text or setting the text to some global or otherwise accessible
    variable(s).
    """
    print('Deferred print:')
    print(text)

def main(reactor):
    """
    This is the main function which will execute a request using the
    GET method. After getting the response, the response code and content
    will be displayed. Finally, the twisted reactor will stop (since
    the react function is being used).
    """
    url = 'http://baidu.com/'
    header={
        'User-Agent': ['Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0'],
        'Accept': ['text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'],
        'Accept-Language': ['en-US,en;q=0.5'],
        'Accept-Encoding': ['gzip, deflate'],
        'Connection': ['keep-alive']}

    d = get(url, headers=header)
    d.addCallback(handleResponse)
    return d

react(main)         # run the main function and display results

rawDescription: how to send raw http header

description: Send raw http header


code:
- Numpydoc
"""
Parameters
----------
param1 : int
    Description of parameter `param1`.
param2 : {'value1', 'value2'}
    Description of a parameter with two possible values.

Returns
-------
int
    Description of the returned value
"""

rawDescription: Python: Standard way to denote return and parameter types


code:
f
id(f)
w
id(w)

rawDescription: What is the second number in a python weak reference's repr?


code:
from PySide import QtGui, QtCore
import sys

class CombinedModel(object):

    def __init__(self):
        self.items = [['Pet', 'Dog'],['Pet', 'Cat'],['Bird','Eagle'],['Bird','Jay'],['Bird','Falcon']]
        self.selectors = list({ k[0] for k in self.items})
        self.table_if = TableModel(self)
        self.combo_if = ComboModel(self)
        self.currentSelection = None

    def currentItems(self):
        return [k[1] for k in self.items if k[0] == self.currentSelection]

    def setSelection(self,combo_row):
        self.currentSelection = self.selectors[combo_row]
        self.table_if.layoutChanged.emit()

class TableModel(QtCore.QAbstractTableModel):
    def __init__(self, model, parent=None):
        QtCore.QAbstractTableModel.__init__(self, parent)
        self.model = model

    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self.model.currentItems())
    def columnCount(self, parent=QtCore.QModelIndex()):
        return 1
    def data(self, index, role):
        if not index.isValid() or role != QtCore.Qt.DisplayRole: return
        row=index.row()
        return self.model.currentItems()[row]

class ComboModel(QtCore.QAbstractListModel):
    def __init__(self, model, parent=None):
        QtCore.QAbstractListModel.__init__(self, parent)
        self.model = model

    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self.model.selectors)
    def data(self, index, role):
        if not index.isValid() or role != QtCore.Qt.DisplayRole: return
        row=index.row()
        return self.model.selectors[row]

class MyWindow(QtGui.QWidget):
    def __init__(self, *args):
        QtGui.QWidget.__init__(self, *args)
        vLayout=QtGui.QVBoxLayout(self)
        self.setLayout(vLayout)

        self.model=CombinedModel()

        self.combo=QtGui.QComboBox()
        self.combo.setModel(self.model.combo_if)
        self.combo.activated.connect(self.model.setSelection)
        vLayout.addWidget(self.combo)

        self.ViewA=QtGui.QTableView(self)
        self.ViewA.setModel(self.model.table_if)
        self.ViewA.clicked.connect(self.viewClicked)
        vLayout.addWidget(self.ViewA)

        self.model.setSelection(0)

    def viewClicked(self, indexClicked):
        print('indexClicked() row: %s  column: %s'%(indexClicked.row(), indexClicked.column() ))

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())

rawDescription: How to share same model with QTableView and QComboBox

description: Share same model with QTableView and QComboBox


rawDescription: Issue with merging time series variables to create new DataFrame with arbitrary index


rawDescription: In Python how can I change the values in a list to meet certain criteria


code:
moduleName = input('Enter module name:')
import_module(moduleName)

rawDescription: Python: How to import other Python files


code:
def col_var(input_file, delimiter):
    # get each line into a variable
    rows = open(input_file).read().splitlines()

    # split each row into entries
    split_rows = [row.split(delimiter) for row in rows]

    # Re-orient your list
    columns = zip(*split_rows)

rawDescription: How to assign columns of data to variables

description: Assign columns of data to variables


code:
import csv
import operator as op
import requests

symbol = "mtgoxUSD"
url = 'http://api.bitcoincharts.com/v1/trades.csv?symbol={}'.format(symbol)
csv_file = "trades_{}.csv".format(symbol)

data = requests.get(url)
with open(csv_file, "w") as f:
    f.write(data.text)

with open(csv_file) as f:
    next(f) # discard first row from file -- see notes
    max_value = max(row[0] for row in csv.reader(f))

with open(csv_file) as f:
    next(f) # discard first row from file -- see notes
    max_row = max(csv.reader(f), key=op.itemgetter(0))

rawDescription: CSV find max in column and append new data


code:
Turtle.__init__(self, shape = "circle")
solar_element.__init__(self, Name, Radius, Mass, Colour)

rawDescription: How to use a turtle superclass

description: Use a turtle superclass


rawDescription: Python pandas: get elements (index,col) below diagonal in dataframe


code:
list1 = map(str.split,open("text1"))
list2 = map(str.split,open("text2"))
with open("combined.txt","wb") as f:
     f.write("\n".join(map(" ".join,row) for row in zip(*(zip(*list1)+zip(*list2)))))

rawDescription: Properly combining 2 different files

description: Properly combining 2 different files


code:
convertedAges = {
    1: "9 to 10 months",
    2: "9 to 10 months",
    3: "2 to 3 years", # and so on
}

rawDescription: Any way I could make this more efficient?


rawDescription: Python: Avoiding overwriting/unreadability of columns while writing a csv file


code:
xml = x.toxml()
with open('foo.xml', 'w') as f:
    f.write(xml.partition('>')[2])

rawDescription: Create xml file without prologue with python dom minidom


code:
df = pd.DataFrame(np.random.randint(5, 15, (10, 3)), columns=list('abc'))
df

rawDescription: How do I select and store columns greater than a number in pandas?

description: Select and store columns greater than a number in pandas


code:
friday_night = datetime.datetime(2012, 8, 10, 23, 59, 50)
add_weekday_seconds(friday_night, 5)
add_weekday_seconds(friday_night, 10)
add_weekday_seconds(friday_night, 15)

rawDescription: Add seconds to python datetime excluding weekends


code:
def log_call(f):
    def logged(*args, **kwargs):
        print("Called {}".format(f.__name__))
        return f(*args, **kwargs)
    return logged

class A:
    @log_call
    def add_two_numbers(self):
        return 1 + 2

print(A().add_two_numbers())

rawDescription: Is there any benefits for using * or ** in python?


code:
class Int(object):
    def __init__(self):
        self.value = ...

    @classmethod
    def static_init(cls):
        setattr(cls, "__add__", lambda self, other: self.value + other)

Int.static_init()

rawDescription: Why aren't my Python custom math-overload class methods working?


code:
from decimal import Decimal
d = 10**23 * Decimal('1.1')
d
int(d)

rawDescription: Preventing auto scientific form for numbers in python3


code:
from xml.etree import cElementTree as ET

for event, elem in ET.iterparse("books.xml", events=("end",)):
    if elem.tag == "book":
        for d in elem.iterfind("description"):
            elem.remove(d)

rawDescription: Python removing elements from large xml file with xml iterparse


code:
import os
import select
import sys
import time
from threading import Thread

def threadOne():
    while True:
        print("Thread 1")
        time.sleep(1)

def threadTwo():
    while True:
        # python 2 print does not support file argument like python 3,
        # so writing to sys.stderr directly to simulate error message.
        sys.stderr.write("Thread 2\n")
        time.sleep(1)

# Run the threads in the background
threadOne = Thread(target = threadOne)
threadOne.daemon = True

threadTwo = Thread(target = threadTwo)
threadTwo.daemon = True

def main():
    threadOne.start()
    threadTwo.start()

    tty_in = open('/dev/tty', 'r')
    tty_out = open('/dev/tty', 'w')
    fn = tty_in.fileno()
    poll = select.poll()
    poll.register(fn, select.POLLIN)

    userInput = ""
    chars = []
    prompt = True

    while True:
        if os.getpgrp() == os.tcgetpgrp(fn) and poll.poll(10):  # 10 ms
            # poll should only return if the input buffer is filled,
            # which is triggered when a user enters a complete line,
            # which lets the following readline call to not block on
            # a lack of input.
            userInput = tty_in.readline()
            # This should allow us to exit out
            if userInput.strip() == "quit":
                sys.exit()
            # alternatively an empty string from Ctrl-D could be the
            # other exit method.
            else:
                tty_out.write("user input: %s\n" % userInput)
                prompt = True
        elif not os.getpgrp() == os.tcgetpgrp(fn):
            time.sleep(0.1)
            if os.getpgrp() == os.tcgetpgrp(fn):
                # back to foreground, print a prompt:
                prompt = True

        if prompt:
            tty_out.write('> ')
            tty_out.flush()
            prompt = False

if __name__ == '__main__':
    try:
        # Uncomment if you are expecting stdin
        # print('input stream length: %d ' % len(sys.stdin.read()))
        main()
    except KeyboardInterrupt:
        print("Forcibly interrupted.  Quitting")
        sys.exit()  # maybe with an error code

rawDescription: Python script hanging when running in the background


code:
import logging
import random
import os
class myFileHandler(logging.FileHandler):
    def __init__(self,path,fileName,mode):
        r = random.randint(1,100000)
        path = path+"/log_"+str(r)
        os.mkdir(path)
        super(myFileHandler,self).__init__(path+"/"+fileName,mode)

rawDescription: Dynamic filepath & filename for FileHandler in logger config file in python


rawDescription: Is there anyway to convert a list into a variable for use as a dictionary key?


code:
import shutil
def download_file_locally(url, dest):
    req, filename, content_type = download_file(url)
    if dest.endswith('/'):
        dest = os.path.join(dest, filename)
    with open(dest, 'wb') as f:
        shutil.copyfileobj(req, f)
    req.close()

rawDescription: How to download file from ftp?

description: Download file from ftp


code:
import unittest
import doctest
from project import views

class FooTestCase(unittest.TestCase):
    def testFoo(self):
        self.assertEquals('foo', 'bar')

def suite():
    suite = unittest.TestSuite()
    suite.addTest(doctest.DocTestSuite(views))
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(FooTestCase))
    return suite

rawDescription: Django doctests in views.py

description: Django doctests in views.py


rawDescription: Create arg string from ArgumentParser parsed args in Python


rawDescription: Why is it not possible to access other variables from inside the apply function in Python?


code: timeit.timeit('foobar()', number=1000)

rawDescription: Python: Start and stop timer

description: Python: Start and stop timer


code:
df['mydate'] = pd.to_datetime(df['mydate'])
df = df.set_index('mydate')
df['day'] = df.index.date
counts = df.groupby(['day', 'col_name']).agg(len)

rawDescription: Plot number of occurrences from Pandas DataFrame


code: packet = [chr(int(a, 16)) for a in bytes.split()]

rawDescription: Python help understanding sample code

description: Python help understanding sample code


code:
# Notice epoch lowercase, panda column names are case sensitive
g_follow = teach_freq['epoch'].groupby(teach_freq['observation'])
start_follow = g_follow.min()
# Important - start_follow is a Series where observation is an index
blocks = start_follow # to have the same notation
# main part - using Series instead of DataFrame makes the indexing simpler
min_time = lambda row: row['epoch'] - blocks[row['observation']]
teach_freq['std_epoch'] = teach_freq.apply(min_time, axis=1)

rawDescription: Create five minute time blocks (Pandas/Python)


code:
keywords = {"conditions","BC"}
words = s.split(" ")
inds = (i for i, w in enumerate(words) if w in keywords)

a,b = next(inds), next(inds)
print(a, b)

rawDescription: Find a word after a keyword, then, paste it after a different keyword.


rawDescription: Getting index of two repeated numbers on a list python


code:
xx
zz

rawDescription: Typecasting a numpy matrix into a string in python


code:
for item in x:
    x1, x2 = item.split()
    t1, r1 = dataDictionary[x1]
    t2, r2 = dataDictionary[x2]
    y = r1 - r2 + t1 * t2
    print(y)

rawDescription: Splitting two lists and assigning data from one list to values in the other


code:
from datetime import datetime, timedelta
t = 734869.00138889
datetime.min + timedelta(days=t - 1)

rawDescription: Create a datetime.time object just from seconds


code:
for dir, dirs, files in os.walk(src):
    if any(f.endswith('.txt') for f in files):
        dirs[:] = []  # do not recurse into subdirectories
        continue      # ignore this directory

    # do something with the files here, there are no .txt files.
    files = [os.path.join(dir, f) for f in files]

rawDescription: How to exclude files extension from os.walk

description: Exclude files extension from os.walk


rawDescription: numpy - pretty printing

description: Numpy - pretty printing


code: func_dict['lt'](5, 7)

rawDescription: Python: Pass inequality as string in dict for evaluation


rawDescription: How to combine the closest non_overlapping ranges?

description: Combine the closest non_overlapping ranges


code: patch -p1 < patch_file

rawDescription: Applying patch to standard python file locally


code:
from datetime import datetime
fmt = '%a, %d %b %Y %H:%M:%S %z'
datetime.strptime('Wed, 30 Sep 2015 18:59:39 +0000', fmt)

rawDescription: Parse date from string and insert with Sqlalchemy tosqlite


code:
try:
    unicode = unicode
except NameError: # Python 3 (or no unicode support)
    unicode = str # str type is a Unicode string in Python 3

rawDescription: check python version(s) from within a python script and if/else based on that

description: Check python version(s) from within a python script and if/else based on that


code:
class Category(models.Model):
    name = models.CharField(max_length=50, blank=False)

class Profile(models.Model):
    name = models.CharField(max_length=255, blank=False)
    user = models.ForeignKey(User, blank=True, null=True)
    categories = models.ManyToManyField(Category, blank=True)

rawDescription: Form, Create and Update View for Many-To-Many Relationship


code:
class defaultPoint(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

p = defaultPoint(0, 3)
p

rawDescription: Python object names and the interactive interpreter


code:
import string
allowed = string.lowercase + string.digits
''.join(c for c in a.lower() if c in allowed)

rawDescription: How to remove all symbols and spaces from the String?

description: Remove all symbols and spaces from the String


rawDescription: binning a dataframe in pandas in Python

description: Binning a dataframe in pandas in Python


code:
def make_requests_from_url(self, url):
    return Request(url, method='HEAD', dont_filter=True)

rawDescription: Scrapy crawl http header data only

description: Scrapy crawl http header data only


code:
script_path = os.path.abspath(os.path.dirname(__file__))
f, path, desc = imp.find_module('oauth2', [script_path])
oauth2 = imp.load_module('oauth2', f, path, desc)

rawDescription: How to import 3rd party module to python if its name is taken?

description: Import 3rd party module to python if its name is taken


code: GdkPixbuf.Pixbuf.scale_simple ()

rawDescription: Scaling up an animated GIF in python to be used with PyGTK


rawDescription: How to set first column to a constant value of an empty np.zeros numPy matrix?

description: Set first column to a constant value of an empty np.zeros numPy matrix


code: print_chord([0, 4, 5])

rawDescription: Name those music notes - python console

description: Name those music notes - python console


code:
# test_foo.py
import logging

def logAssert(test,msg):
    if not test:
        logging.error(msg)
        assert test,msg

def test_foo():
    logging.info("testing foo")
    logAssert( 'foo' == 'foo', "foo is not foo")

def test_foobar():
    logging.info("testing foobar")
    logAssert( 'foobar' == 'foo', "foobar is not foo")

rawDescription: py.test logging messages and test results/assertions into a single file

description: Py.test logging messages and test results/assertions into a single file


code:
namedtuple
from collections import namedtuple
headers = ["one", "two", "three"]

CustomRow = namedtuple("CustomRow", headers)

a_row = CustomRow(1, 2, 3)
a_row.one == 1 # True
a_row.two == 2 # True
a_row.three == 3 # True

rawDescription: make a class __init__ from a list

description: Make a class __init__ from a list


code: datetime.combine(date.today(), time()) + timedelta(hours=1)

rawDescription: python time + timedelta equivalent

description: Python time + timedelta equivalent


code:
class ChatsSerializer(serializers.ModelSerializer):
    # some code...

    sender_name = serializers.Field(source = 'sender_name') # source = 'name of property or method' you dont have to pass it in this example becouse model property has the same name as this serializer attribute

    class Meta:
         fields = ('sender_name', 'id', 'sender', 'recipient','thread','subject','moderation_reason','body')

rawDescription: How to include in queryset details fields of a foreign key (django and rest_api)

description: Include in queryset details fields of a foreign key (django and rest_api)


rawDescription: Ask for a regx for re.sub in python

description: Ask for a regx for re.sub in python


code: re.split(r'(\s+)', line)

rawDescription: Preserve whitespaces when using split() and join() in python


code:
list(x.flat)
list(mask.flat)

rawDescription: Replacing column values in an array using putmask and preserving order


code:
d = [(key, list(group)) for key, group in groupby(dt, lambda x: x[0])]
prnt(d)

rawDescription: groupby() giving an empty list

description: Groupby() giving an empty list


code:
# i and j are the indices for the node whose neighbors you want to find
def find_neighbors(m, i, j, dist=1):
    return [row[max(0, j-dist):j+dist+1] for row in m[max(0, i-1):i+dist+1]]

rawDescription: python find all neighbours of a given node in a list of lists

description: Python find all neighbours of a given node in a list of lists


code:
for i in items:
    for j in items:
        if i != x or j != y:
            cp.append([i, j])

rawDescription: Creating a list with two different numbers


rawDescription: How do i append a list with new data?

description: Append a list with new data


code:
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)

ax1.plot(x1, y1)
ax2.plot(x2, y2)

plt.setp(ax1.get_xticklabels(), visible=False)

rawDescription: How can I remove xtics from an axis in matplotlib?

description: Remove xtics from an axis in matplotlib


rawDescription: Selecting a new dataframe via a multi-indexed frame in Pandas using index names


code:
data=pack('!BBBBHHII', P2P_h.version, P2P_h.ttl, P2P_h.msgType, P2P_h.reserved, P2P_h.sendPort, P2P_h.payloadLength, P2P_h.ipSrc, P2P_h.messageId) #your current header
data+= pack("!I",len(body)) #assumes len(body) fits into a integer
data+= body #assumes body is a string
socket.send(data)

rawDescription: struct.pack() Python unknown fields length to send through a socket

description: Struct.pack() Python unknown fields length to send through a socket


code:
A
B
C

rawDescription: Extracting items in a list that are not in order


code:
from matplotlib import pyplot
import numpy

a = numpy.array([[ 3.57,  1.76,  7.42,  6.52],
                 [ 1.57,  1.2 ,  3.02,  6.88],
                 [ 2.23,  4.86,  5.12,  2.81],
                 [ 4.48,  1.38,  2.14,  0.86],
                 [ 6.68,  1.72,  8.56,  3.23]])

def plotCollection(ax, xs, ys, *args, **kwargs):

  ax.plot(xs,ys, *args, **kwargs)

  if "label" in kwargs.keys():

    #remove duplicates
    handles, labels = pyplot.gca().get_legend_handles_labels()
    newLabels, newHandles = [], []
    for handle, label in zip(handles, labels):
      if label not in newLabels:
        newLabels.append(label)
        newHandles.append(handle)

    pyplot.legend(newHandles, newLabels)

ax = pyplot.subplot(1,1,1)
plotCollection(ax, a[:,::2].T, a[:, 1::2].T, 'r', label='data_a')
plotCollection(ax, a[:,1::2].T, a[:, ::2].T, 'b', label='data_b')
pyplot.show()

rawDescription: pyplot combine multiple line labels in legend

description: Pyplot combine multiple line labels in legend


code:
def match_either(tag):
    if tag.name == 'div':
        # at *least* these three classes must be present
        return {'DetailInternetFirstContent', 'empty', 'openPostIt'}.issubset(tag.get('class', []))
    if tag.name == 'p':
        # at *least* this one class must be present
        return 'connection' in tag.get('class', [])

rawDescription: Issue with designing function for data scraping using BS4


code: re.search(r'[|]([^|]*)[|]Ab Initio Software', name, re.IGNORECASE).group(1)

rawDescription: Anyone offer a better solution? Right to left regex. using python


rawDescription: Nicer way to compute means of a set with numpy


code:
# in UserProfile model (adding related_name)
user = models.OneToOneField(User, related_name="profile")

rawDescription: Combine Related Resources With TastyPie

description: Combine Related Resources With TastyPie


code:
def how():
     tangerine = 1
     return tangerine

rawDescription: How to access a variable from a function in an imported file

description: Access a variable from a function in an imported file


code:
from tkinter import ttk
from tkinter import *
from tkinter.ttk import *

class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

    def createWidgets(self):
        self.can = Canvas(self.master, width=500, height=250)
        self.can.pack()
        self.can.create_line(0,0,500,200)

root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()

rawDescription: just a canvas in a tkinter window

description: Just a canvas in a tkinter window


code:
if __name__ == '__main__':
    # code here
    pass

rawDescription: Attempting to have a trivia game by randomly taking certain choices


code:
dir("re")  # The output is the same as doing dir(str)
import re
dir(re) # The output is for the re module, not str

rawDescription: Regarding dir()

description: Regarding dir()


code:
np_a
np_a[np_a > 252]  = 0
np_a

rawDescription: Filtering a list of lists in a smart way


code:
wts = [.25, .25, .5]    # in order: R, G, B
tot = (arr*wts).sum(-1) # now blue has twice the weight of red and green

rawDescription: Sum of colorvalues of an image

description: Sum of colorvalues of an image


code:
cfd = nltk.ConditionalFreqDist(
           (fileid[:4], target)
               for fileid in inaugural.fileids()
               for w in inaugural.words(fileid)
               for target in ['america', 'citizen']
               if w.lower().startswith(target))

A = ['1789', '1793']
B = ['america', 'citizen']
cfd.tabulate(conditions=A, samples=B)

rawDescription: Counting the number of specified words

description: Counting the number of specified words


code:
from timeit import default_timer as timer

N = 10000000

def f():
    for i in range(N):
        pass

start = timer()
for i in range(N):
    pass
print("global %.2f" % (timer() - start,))

start = timer()
f()
print("function %.2f" % (timer() - start,))

rawDescription: python struct pack speed up inside function

description: Python struct pack speed up inside function


code:
melted = pd.melt(df, id_vars=["imageName", "shape"], value_name="color")
melted.groupby(["shape","color"]).size()

rawDescription: Counting occurrences based on criterion

description: Counting occurrences based on criterion


code: img = np.array(width*height)

rawDescription: Assigning random colour from RGB into a image


code:
data = {'foo': [1, 2, 3]}
next(iter(data))

rawDescription: Extract key name from single element dictionary in Python


code:
import os.path
import qllib  # this is the module that comes with QLCLI

def update_tags(mp3_fn, flac_fn):
    mp3 = qllib.AudioFile(mp3_fn)
    flac = qllib.AudioFile(flac_fn)
    # you can iterate over the tag names
    # they will be the same for all file types
    for tag_name in mp3:
        flac[tag_name] = mp3[tag_name]
    flac.write()

mp3_filenames = ['foo.mp3', 'bar.mp3', 'baz.mp3']

for mp3_fn in mp3_filenames:
    flac_fn = os.path.splitext(mp3_fn)[0] + '.flac'
    if os.path.getmtime(mp3_fn) != os.path.getmtime(flac_fn):
        update_tags(mp3_fn, flac_fn)

rawDescription: abstracting the conversion between id3 tags, m4a tags, flac tags

description: Abstracting the conversion between id3 tags, m4a tags, flac tags


code:
filtered_list = []
for name in names:
    for padded_name in padded_names:
        if padded_name.find(name) >= 0:
            filtered_list.append(padded_name)

rawDescription: Creating a list by searching for substrings in python


code:
import pandas as pd

s = pd.Series([
        "1 1 2 2 3 3 4 4 5 5 6 6 6",
        "3 4 2 1 2 3 4 4 5 5 4 34 2"
    ])

todel = set(["1", "3", "5"])
s.apply(lambda x:" ".join(v for v in x.strip().split() if v not in todel))

rawDescription: How to replace multiple character in string of data frame in pandas?

description: Replace multiple character in string of data frame in pandas


code:
#setup.py

from distutils.core import setup, Extension

module1 = Extension('pariparse',
                    include_dirs = ['/usr/include', '/usr/local/include'],
                    libraries = ['pari'],
                    library_dirs = ['/usr/lib', '/usr/local/lib'],
                    sources = ['pariparse.c'])

setup (name = 'pariparse',
       version = '0.01a',
       description = 'A super tiny python-pari interface',
       ext_modules = [module1])

rawDescription: Calling PARI/GP from Python

description: Calling PARI/GP from Python


code: min(f.keys() & o, key=f.get)

rawDescription: Most efficient way to find the key of the smallest value in a dictionary from a subset of the dictionary


rawDescription: How to parse the header files of the pcap file?

description: Parse the header files of the pcap file


code: key_value_mapping = dict(tuple=(), string='', list=[], int=0, set=set())

rawDescription: Writing and reading variables in a file in Python


code:
N = 6 # Set number of gridlines you want to have in each graph

ax1.xaxis.set_major_locator(LinearLocator(N))
ax2.xaxis.set_major_locator(LinearLocator(N))

rawDescription: align grid lines on two plots

description: Align grid lines on two plots


code: func("arg", "arg2", **({"arg3": "some value"} if condition == True else {}))

rawDescription: Conditionally passing arbitrary number of default named arguments to a function


rawDescription: Applying cell wise function in a dataframe


code: test = float("inf")

rawDescription: How can I represent an infinite number in Python?

description: Represent an infinite number in Python


code:
def predict(time, rdd):
    ...

    model = Custom_ModelContainer.getmodel()
    pred = (df.rdd.map(lambda lp: lp.features.toArray())
        .mapPartitions(lambda iter: model.predict(np.array(list(iter)))))
    ...

rawDescription: How to avoid one Spark Streaming window blocking another window with both running some native Python code

description: Avoid one Spark Streaming window blocking another window with both running some native Python code


code:
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image

class ImageButton(ButtonBehavior, Image):
    pass

rawDescription: Kivy clickable image that replaced a button


rawDescription: Quickly compute eigenvectors for each element of an array in python


code:
@app.route("/somewhere")
@extract_args("gender", age=int)
def somewhere(gender, age):
    return jsonify(gender=gender, age=age)

rawDescription: Get query string as function parameters on flask


code:
ultrias = "am 17"
response = "Hi i am 17, did you know I am 17"

count = 0
data = response[:]
while True:
    start = data.find(ultrias)
    if start < 0:
        break
    data = data[start + len(ultrias):]
    count += 1
print(count)

rawDescription: Python count everytime a string is found in another string


code: construct_list = csv.reader(constructs, delimiter=',')

rawDescription: Processing a sub-list of variable size within a larger list


rawDescription: Converting IP address into bytes in python


code:
def enclosing_box(x, y, radius):
    """Given the coordinates of the circle center and its radius, return the top-left and bottom-right coordinates of the enclosing box."""
    return (x-radius, y-radius, x+radius, y+radius)

rawDescription: Python - basics - how to make a 'class('?) function for a simple set of maths based variables


code:
import numpy as np

# Having an array:
a = [1,2,2,3,4,4,3]
print ("Original vector:")
print(a)

# Printting unique values of a:
print ("Unique values of a:")
print (np.unique(a))

# Other option:
print  ("Another way:")
print (set(a))

rawDescription: Is there Python equivalent to MATLAB unique?


code:
import requests
params = {'path[]': ['/empty', '/Burns.docx', '/Byron.doc']}
url = 'http://httpbin.org/get'
response = requests.get(url, params=params)
response.url
from pprint import pprint
pprint(response.json())

rawDescription: sending html input array with requests lib

description: Sending html input array with requests lib


code:
d = {}
d['__metaclass__'] = FooMeta
d['FOO'] = 123
def a(self):
    pass
d['a'] = a
Foo = d.get('__metaclass__', type)('Foo', (object,), d)

rawDescription: Python metaclasses: Why isn't __setattr__ called for attributes set during class definition?


code: variable = (condition and something) or something_else

rawDescription: python ? (conditional/ternary) operator for assignments

description: Python ? (conditional/ternary) operator for assignments


code:
import datetime
time_posted = datetime.datetime(2013, 3, 31, 12, 55, 10)
timediff = datetime.datetime.now() - time_posted
timediff.total_seconds()

rawDescription: Django: How to get a time difference from the time post?


rawDescription: How do I change the scale of imshow in matplotlib without stretching the image?

description: Change the scale of imshow in matplotlib without stretching the image


code:
class ListField(Field):
    def process_formdata(self, valuelist):
        self.data = valuelist

rawDescription: Validating an unknown set of items with WTForms


rawDescription: convert list elements to dictionary python

description: Convert list elements to dictionary python


code:
#!/usr/bin/env python
import re

commands = "copy move".split()
input_string  = "something move 11 something move 12 something 13 copy 14 15"
tokens = iter(re.split("(%s)" % "|".join(map(re.escape, commands)), input_string))
result = []
for tok in tokens:
     if tok in commands:
        args = re.findall(r"\d+", next(tokens, ""))
        result.append((tok,) + tuple(args) + ("",)*(2 - len(args)))
print(result)

rawDescription: parse commands with regex and python

description: Parse commands with regex and python


code:
import os
path = os.environ.get('path')
pathlist = path.split(';')
matlabpath = [s for s in pathlist if all(x in s for x in ['MATLAB','R','bin'])]
print(matlabpath)

rawDescription: What is the Matlab install directory on 64bit or how to get it in Python?


code:
def halftone_reshape(image, mask):
    # you can make up a nicer reshape code maybe, it is a bit ugly. The
    # rolling window code can do this too (but much more general then reshape).
    new_shape = np.array(zip(image.shape, mask.shape))
    new_shape[:,0] /= new_shape[:,1]
    reshaped_image = image.reshape(new_shape.ravel())

    reshaped_mask = mask[None,:,None,:,None,:]

    # and now they just broadcast:
    result_funny_shaped = reshaped_image >= reshaped_mask

    # And you can just reshape it back:
    return result_funny_shaped.reshape(image.shape)

rawDescription: Alternative to `numpy.tile` for periodic mask


rawDescription: How to force sympy to extract specific subexpressions?

description: Force sympy to extract specific subexpressions


code:
import asyncio

@asyncio.coroutine
def factorial(name, number):
    f = 1
    for i in range(2, number+1):
        print("Task %s: Compute factorial(%s)..." % (name, i))
        yield from asyncio.sleep(1)
        f *= i
    print("Task %s: factorial(%s) = %s" % (name, number, f))

loop = asyncio.get_event_loop()
tasks = [
    asyncio.ensure_future(factorial("A", 2)),
    asyncio.ensure_future(factorial("B", 3)),
    asyncio.ensure_future(factorial("C", 4))]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()

rawDescription: Python thread pools - tasks that create subtasks and wait on them


code:
def __init__(self, state, parent=None, action=None, path_cost=0):
    "Create a search tree Node, derived from a parent by an action."
    self.state = state
    self.parent = parent
    self.action = action
    self.path_cost = path_cost
    self.depth = 0
    if parent:
        self.depth = parent.depth + 1

rawDescription: update fct in python constructors

description: Update fct in python constructors


rawDescription: is there a sage equivalent to php 'implode' for a list?

description: Is there a sage equivalent to php 'implode' for a list


code: [s for s in lst if all(x[0].isupper() for x in s.split())]

rawDescription: Remove any phrase in a list based on capitalization (Python 3)


code:
from pprint import pprint
pprint(result)

rawDescription: how to normalize list of lists of strings in python?

description: Normalize list of lists of strings in python


code: df.loc[df.sample == 'sampleD',['count','psi5']] = np.nan

rawDescription: Pandas groupby add rows with cross references


rawDescription: isolate data from a string

description: Isolate data from a string


code:
# shell execute PHP
def php(code):
  # open process
  p = Popen(['php'], stdout=PIPE, stdin=PIPE, stderr=STDOUT, close_fds=True)

  # read output
  o = p.communicate(code)[0]

  # kill process
  try:
    os.kill(p.pid, signal.SIGTERM)
  except:
    pass

  # return
  return o

rawDescription: call php function from python

description: Call php function from python


code:
import matplotlib.pyplot as plt
from matplotlib import gridspec

fig = plt.figure()
gs = gridspec.GridSpec(1,2)
ax1 = fig.add_subplot(gs[0])
ax2 = fig.add_subplot(gs[1], sharey=ax1)
plt.setp(ax2.get_yticklabels(), visible=False)

plt.setp([ax1, ax2], title='Test')
fig.suptitle('An overall title', size=20)
gs.tight_layout(fig, rect=[0, 0, 1, 0.97])

plt.show()

rawDescription: GridSpec with shared axes in Python

description: GridSpec with shared axes in Python


code:
df_2 = df_1.stack(level=['Marker', 'mrk'])
df_2

rawDescription: Pivoting duplicate columns into rows

description: Pivoting duplicate columns into rows


code:
from bs4 import BeautifulSoup

with open('file.html', 'r') as f:
    html_file_as_string = f.read()
soup = BeautifulSoup(html_file_as_string, "lxml")
for div in soup.find_all('div', {'class': 'my_class'}):
    for p in div.findAll('p'):
        new = BeautifulSoup(u'\n'.join(u'<p>{}</p>'.format(line.strip()) for line in p.text.splitlines() if line), 'html.parser')
        p.replace_with(new)
with open('file', 'w') as f:
    f.write(soup.renderContents())

rawDescription: How to perform re substitutions on <p> tags within a specific class?

description: Perform re substitutions on <p> tags within a specific class


code:
import matplotlib.pyplot as plt

def get_ax_size(ax):
    bbox = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
    width, height = bbox.width, bbox.height
    width *= fig.dpi
    height *= fig.dpi
    return width, height

fig, ax = plt.subplots()
print(get_ax_size(ax))
#(496.0, 384.00000000000006)

ax2 = plt.axes([0.3, 0.3, 0.7, 0.7])
print(get_ax_size(ax2))
# (448.0, 336.0)

rawDescription: determine matplotlib axis size in pixels

description: Determine matplotlib axis size in pixels


code: Board = range(1, 10) # python 2

rawDescription: (Python) adding a list to another without the brackets


rawDescription: Add dictionary values where keys match

description: Add dictionary values where keys match


code:
def kill(self):
    del self

rawDescription: Delete all objects in a list

description: Delete all objects in a list


code: df.groupby(['FROM', 'CONT']).sum()

rawDescription: Pandas- Removing duplicate rows based on the columns


code: sorted(re.findall("g+", "fggfggggfggfg"), key=len, reverse=True)

rawDescription: Python - finding the longest sequence with findall


code:
from collections import OrderedDict
items = ([4,1,2],[1,2,3],[4,1,2])
OrderedDict((tuple(x), x) for x in items).values()

rawDescription: How do I remove duplicate arrays in a list in Python

description: Remove duplicate arrays in a list in Python


code:
cust = lambda g: g[df.ix[g.index]['C'] < 0.5].sum()
f = {'A':['sum','mean'], 'B':['prod'], 'D': {'my name': cust}}
df.groupby('GRP').agg(f)

rawDescription: Apply multiple functions to multiple groupby columns


code: mystring = unicode(mystring, errors='replace')

rawDescription: How to print out ascii characters from 0 to 255 in C, displayed in a webpage through Django?

description: Print out ascii characters from 0 to 255 in C, displayed in a webpage through Django


code:
def foo(data, x_min, x_max):
    from bisect import bisect_left, bisect
    from operator import itemgetter
    data = sorted(data)
    x_data = [x for x,y in data]
    index_min = bisect_left(x_data, x_min)
    index_max = bisect(x_data, x_max)
    return max(data[index_min:index_max],key=itemgetter(1))[-1]

rawDescription: Given a float, find if in list or closest number to it


code:
where_filter = 'e4da3b7fbbce2345d7772b0674a318d5'

Students.objects.get(pk__md5=where_filter)

rawDescription: Django queryset model apply function in field


code:
import oursql
import config
import numpy as np

def create_random_data():
    connection = oursql.connect(
        host=config.HOST, user=config.USER, passwd=config.PASS,
        db='test')
    with connection.cursor() as cursor:
        sql = 'DROP TABLE IF EXISTS `mytable`'
        cursor.execute(sql)
        sql = '''CREATE TABLE `mytable` (
            `id` INT(11) NOT NULL AUTO_INCREMENT,
            `ipsvalue` INT(11) DEFAULT NULL,
            `ipstimestamp` INT(11) DEFAULT NULL,
            `varid` INT(11) DEFAULT NULL,
            PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=latin1'''
        cursor.execute(sql)
        sql = '''
            INSERT INTO mytable (ipsvalue, ipstimestamp, varid)
            VALUES (?, ?, ?)'''
        N = 10**5
        args = np.empty((N, 3))
        args[:, :-1] = np.random.randint(100, size=(N, 2))
        args[:, -1] = np.tile(np.array([17884, 55437, 34637, 17333]), N//4)
        cursor.executemany(sql, args)

def one_query():
    connection = oursql.connect(
        host=config.HOST, user=config.USER, passwd=config.PASS,
        db='test')
    with connection.cursor() as cursor:
        varids = sorted((17884, 55437, 34637, 17333))
        sql = '''SELECT varid, ipsvalue, ipstimestamp FROM mytable
                 WHERE varid IN {}
                 ORDER BY varid, ipstimestamp, ipsvalue'''.format(tuple(varids))
        cursor.execute(sql)
        data = np.array(cursor.fetchall())
        data = data.reshape(4, -1, 3)
        arr = dict()
        for i, varid in enumerate(varids):
            arr[varid] = data[i]
        return arr

def four_queries():
    connection = oursql.connect(
        host=config.HOST, user=config.USER, passwd=config.PASS,
        db='test')
    with connection.cursor() as cursor:
        arr = dict()
        varids = (17884, 55437, 34637, 17333)
        for varid in varids:
            sql = '''SELECT varid, ipsvalue, ipstimestamp FROM mytable
                     WHERE varid = ?
                     ORDER BY ipstimestamp, ipsvalue'''
            cursor.execute(sql, [varid])
            arr[varid] = np.array(cursor.fetchall())
        return arr

arr = one_query()
arr2 = four_queries()
assert all([np.all(arr[key]==arr2[key]) for key in arr])

rawDescription: denormalized numpy arrays from postgres

description: Denormalized numpy arrays from postgres


code:
import pandas as pd

df = pd.DataFrame([([u'Avocado', u' Havarti', u' Turkey'], 'Barry Z', 8.98),
                   ([u'Bacon', u' Swiss', u'Turkey'], 'Barry B', 8.98)],
                  columns=['ingredients', 'name', 'price'])

ingredients = df['ingredients'].apply(
    lambda lst: pd.Series(True, index=[item.strip().lower() for item in lst]))
ingredients.fillna(False, inplace=True)
del df['ingredients']
df = df.join(ingredients)
print(df)

rawDescription: How to normalize text in a list that is itself in a column of Pandas dataframe?

description: Normalize text in a list that is itself in a column of Pandas dataframe


code: sub_c = np.asarray(categories)[i]

rawDescription: How should I go about subsampling from a scipy.sparse.csr.csr_matrix and a list


code:
my_string = re.sub(r'^(.*?) \(The\)$', r'The \1', my_string)
my_string = re.sub(r'\bL\.? ?L\.? ?C\.?\b', '', my_string)
my_string = re.sub(r'[(),]', '', my_string)

rawDescription: Convert this perl replacement to python

description: Convert this perl replacement to python


code:
import time
import data as shared

shared.list_vars.append("001")
time.sleep(5.0)
shared.print_vars()

# It prints:
# list_vars = ['001']
# var =

rawDescription: Static variables in Python that are shared across files


code:
class Handle:
    # Either pid or procname, not both, may be specified.
    def __init__(self, pid=-1, procname=None):
        if pid >= 0 and procname is not None:
            raise ValueError('you can specify either pid or procname, not both')
        self.procname = procname
        self.pid = pid

rawDescription: Python overload __init__

description: Python overload __init__


code:
class RecurseTreeNode(template.Node):
    def __init__(self, template_nodes, queryset_var, order_var=None):
        self.template_nodes = template_nodes
        self.queryset_var = queryset_var
        self.order_var = order_var

    def _render_node(self, context, node):
        bits = []
        context.push()

        children = node.get_children()

        if children and self.order_var is not None:
            children = children.order_by(self.order_var)

        for child in children:
            bits.append(self._render_node(context, child))
        context['node'] = node
        context['children'] = mark_safe(''.join(bits))
        rendered = self.template_nodes.render(context)
        context.pop()
        return rendered

    def render(self, context):
        queryset = self.queryset_var.resolve(context)
        roots = cache_tree_children(queryset)
        bits = [self._render_node(context, node) for node in roots]
        return ''.join(bits)

@register.tag
def recursetree(parser, token):
    bits = token.contents.split()
    if len(bits) < 2:
        raise template.TemplateSyntaxError(_('%s tag requires a queryset') % bits[0])

    queryset_var = template.Variable(bits[1])

    if len(bits) == 3:
        order_var = bits[2]
    else:
        order_var = None

    template_nodes = parser.parse(('endrecursetree',))
    parser.delete_first_token()

    return RecurseTreeNode(template_nodes, queryset_var, order_var)

rawDescription: Dynamic order in django-mptt

description: Dynamic order in django-mptt


code:
s = '[download] /tmp/743979_file.mp4 has already been downloaded'
re.search(r'(?<=\s)\S*_file\S*', s).group()

rawDescription: Get everything between two spaces including a word stem


code:
if sys.version_info[0] == 3:
    source_dir = '.'
else:
    source_dir = 'python2_source'

rawDescription: What is a good way to support Python 2 in a Python 3 codebase when using PyPi?


code: nt = NegTuple((slice(None),))

rawDescription: python/numpy: negate or complement a slice

description: Python/numpy: negate or complement a slice


rawDescription: Sorting dictionary list values

description: Sorting dictionary list values


code:
import numpy as np
arr = np.array([1, 2, 3, 4])
arr.dtype.name
import pandas as pd
cat = pd.Categorical(['a', 'b', 'c'])
cat.dtype.name

rawDescription: Check if dataframe column is Categorical


code:
class MyFactory(OKFactory):

    object = factory.SubFactory(MyOtherFactory)
    related = factory.RelatedFactory(YetAnotherFactory)  # We want to pass object in here

rawDescription: Passing an object created with SubFactory and LazyAttribute to a RelatedFactory in factory_boy


rawDescription: Get data points from Seaborn distplot

description: Get data points from Seaborn distplot


rawDescription: Finding most recent datetime in list before a given datetime


code:
1
Two,Three
4.0

rawDescription: Python save arbitrarily nested list to CSV


code:
last_msg = get_object_or_404(Messages, pk=form.cleaned_data['last_msg'])
messages = Message.objects.filter(timestamp__gt=last_msg.timestamp)

rawDescription: Ajax returning only new message

description: Ajax returning only new message


code:
new_dict = {}
for road_id in road_dict:
    homes_on_road = [home for home in homes if home['road_id'] == road_id]
    ordered_homes = sorted(homes_on_road, key=lambda h: h['distance'])
    for position, home in enumerate(ordered_homes):
        home['home_number'] = position + 1

    new_dict[road_id] = ordered_homes

pprint(new_dict)

rawDescription: Sorting and re-numbering values in Python dictionary


rawDescription: Dictionary comprehension with nested list, prevent overwrite


code:
def neighbouring_minimum(iterable, key=None):
    if key is None:
        # default to the element itself
        key = lambda x: x

    paired = pairwise(iterable)

    # find minimum pair by key summed
    minpair = min(paired, key=lambda pair: sum(map(key, pair)))
    return min(minpair, key=key)

neighbouring_minimum(ifilter(lambda x: x[2] == 1, aList), key=itemgetter(1))

rawDescription: generate min combining multiple lists, return lowest uncombined list?

description: Generate min combining multiple lists, return lowest uncombined list


code:
import itertools
import random

def make_lists(input_list):
    categories = [input_list[i:i+56] for i in range(0, 448, 56)] # 8 lists of 56
    for c in categories:
        random.shuffle(c)
    selections = zip(*categories) # 56 tuples of 8
    it = iter(selections)
    by_four = zip(it, it, it, it) # 14 tuples of 4 tuples of 8
    results = [list(itertools.chain.from_iterable(lst)) for lst in by_four]
    return results

rawDescription: Python 2.7: Building Sub Lists from Specific Ranges within a Master List Without Replacement


code:
from pygame import gfxdraw
gfxdraw.pixel(surface, x, y, color)

rawDescription: Pygame: Draw single pixel

description: Pygame: Draw single pixel


rawDescription: Numpy array, fill empty values for a single column


rawDescription: How can I get pyyaml.safe_load() to handle python/unicode tag?

description: Get pyyaml.safe_load() to handle python/unicode tag


code:
# Generation of random numbers using `random` function
res3 = np.random.randint(1e15, 1e16, 100000)
# Plot
n, bins, patches = plt.hist(res3, 100, normed=1, histtype='stepfilled', label='randint')

rawDescription: Generating random 16 digit number in Python


code:
pix1, pix2 = PIX(), PIX()
mylib.testfunc1(ctypes.byref(pix1))
mylib.testfunc2(ctypes.byref(pix2))

rawDescription: Does Ctypes Structures and POINTERS automatically free the memory when the Python object is deleted?


code:
sorted(student_tuples, key=lambda x: (x[2],x[4],-x[0],x[1]))
                                                #^ This field will be
                                                #  in descending order

rawDescription: Sorting tuples in python using different orders for different fields


code:
class Bar:
    def spam(): pass

class_for_method(ClassA, Bar.spam) is None

rawDescription: In Python, class from which a given instance is inheriting a particular method


code:
def cube(a):
    final = []
    for x in a:
        final.append(x**3)
    return final

rawDescription: For loops clarity

description: For loops clarity


code:
from operator import  itemgetter

for name, scores in sorted(data.items(), key=itemgetter(1), reverse=True):
    avg = sum(scores[-3:]) / min(3, len(scores))
    print('{} your average was {}'.format(name, avg))

rawDescription: Finding the average of a list

description: Finding the average of a list


code:
y='y'
y.isalpha
y.isalpha()

rawDescription: Finding letters in a string

description: Finding letters in a string


code:
def sub_lists(sequence):
    all_but_first = set(sequence[1:])
    for item in itertools.combinations(sequence[1:], len(sequence)//2 - 1):
        yield [[sequence[0]] + list(item), list(all_but_first.difference(item))]

rawDescription: N choose N/2 sublists of a list

description: N choose N/2 sublists of a list


rawDescription: Vowel counter in a text file

description: Vowel counter in a text file


code:
class Weather(object):

    temperature = ValidRange('_temp', 0, 100)

    ...

rawDescription: Implementing a decorator to limit setters


code: channel_views.filter(lambda x: "XYZ" in x[1])  # XYZ in the value

rawDescription: Subsetting data in Spark

description: Subsetting data in Spark


code: from pylab import *

rawDescription: matplotlib.pyplot vs matplotlib.pylab

description: Matplotlib.pyplot vs matplotlib.pylab


code:
class classproperty(object):
    def __init__(self, f):
        self.f = classmethod(f)
    def __get__(self, *a):
        return self.f.__get__(*a)()

rawDescription: Property method without class

description: Property method without class


code:
self.btn1.clicked.connect(lambda: self.btn1.setText("Wow"))
self.btn2.clicked.connect(lambda: self.btn2.setText("Yay !"))

rawDescription: Writing modular codes in python

description: Writing modular codes in python


rawDescription: Exclude one or more items from pandas Series


code:
d = {}
for key, value in l:
    if key in d:
        d[key] += value
    else:
        d[key] = value

rawDescription: List of list increment if exists otherwise extend


code: d["gymnasium"] = {}

rawDescription: Python: Dictionary within dictionary?

description: Python: Dictionary within dictionary


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

X = np.array([0., 0.5, 1., 1.5, 2., 2.5, 3., 3.5, 4.])
X2 = np.array([122, 85, 63, 50, 23, 12, 7, 5, 2])
Y = np.cos(X*20)
Z = np.sin(X*20)

fig = plt.figure()
gs = gridspec.GridSpec(1, 2)
right_gs = gridspec.GridSpecFromSubplotSpec(1, 2, width_ratios=[30, 1], subplot_spec=gs[1], wspace=0.05)

ax1 = fig.add_subplot(right_gs[0])
color_axis = fig.add_subplot(right_gs[1])

ax2 = ax1.twiny()

ax1.set_xlim(-0.2, max(X)+0.2)
plt.tick_params(axis='both', which='major', labelsize=10)
ax1.minorticks_on()
ax1.grid(b=True, which='major', color='gray', linestyle='--', lw=0.3)

SC = ax1.scatter(X, Y, c=Z, cmap='viridis')
ax1.set_xlabel("Original x-axis")

ax2.set_xlim(ax1.get_xlim())
ax2.set_xticks(X)
ax2.set_xticklabels(X2)
ax2.set_xlabel("Second x-axis")

cbar = fig.colorbar(SC, cax=color_axis)
cbar.set_label('B', fontsize=10, labelpad=4, y=0.5)
cbar.ax.tick_params(labelsize=10)

fig.tight_layout()
plt.show()

rawDescription: Matplotlib colorbar moves second x axis

description: Matplotlib colorbar moves second x axis


code:
from ctypes import *

class NodeStatInfo(Structure):
    _fields_ = [
        ('status', c_char * 10),
        ('name', c_char * 64)]

P_NodeStatInfo = POINTER(NodeStatInfo)
PP_NodeStatInfo = POINTER(P_NodeStatInfo)

lib = CDLL('./lib.so')
lib.cluster_info.argtypes = [c_char_p,
                             POINTER(PP_NodeStatInfo),
                             POINTER(c_int)]

rawDescription: Python Ctypes : pointer to array of pointers to a structure


code:
import cherrypy
from cherrypy.process.plugins import SimplePlugin

PROC_NAME = 'sample'

def set_proc_name(newname):
    """
    Set the process name.
    Source: http://stackoverflow.com/a/923034/298371
    """
    from ctypes import cdll, byref, create_string_buffer
    libc = cdll.LoadLibrary('libc.so.6')
    buff = create_string_buffer(len(newname)+1)
    buff.value = newname
    libc.prctl(15, byref(buff), 0, 0, 0)

class NamedProcess(SimplePlugin):
    """
    Set the name of the process everytime that the
    engine starts.
    """

    def start(self):
        self.bus.log("Setting the name as '{}'".format(PROC_NAME))
        set_proc_name(PROC_NAME)

class HelloWorld(object):

    @cherrypy.expose
    def index(self):
        return "Hello world!"

def run_without_autoreload():
    set_proc_name(PROC_NAME)
    cherrypy.quickstart(HelloWorld(), config={
        'global': {
            'engine.autoreload.on': False
        }
    })

def run_with_autoreload():
    # Work on any configuration but for the sake of the
    # question this works with the autoreload.
    NamedProcess(cherrypy.engine).subscribe()
    cherrypy.quickstart(HelloWorld())

if __name__ == '__main__':
    run_with_autoreload()
    # run_without_autoreload()

rawDescription: How to stop cherrypy auto-reload from changing the process name on Debian?

description: Stop cherrypy auto-reload from changing the process name on Debian


code:
from wand.image import Image

# Prototype local variables
pixels = []
width, height = 0, 0
blob = None

# Load image
with Image(filename='2x2.png') as image:
    # Enforce pixels quantum between 0 & 255
    image.depth = 8
    # Save width & height for later use
    width, height = image.width, image.height
    # Copy raw image data into blob string
    blob = image.make_blob(format='RGB')

# Iterate over blob and collect pixels
for cursor in range(0, width * height * 3, 3):
    # Save tuple of color values
    pixels.append((blob[cursor],      # Red
                   blob[cursor + 1],  # Green
                   blob[cursor + 2])) # Blue
print(pixels)
#=> [(255, 0, 0), (0, 0, 255), (0, 128, 0), (255, 255, 255)]

rawDescription: Get all pixels of an Image [Wand]

description: Get all pixels of an Image [Wand]


code:
import itertools

def chunked(it, size):
    it = iter(it)
    while True:
        p = tuple(itertools.islice(it, size))
        if not p:
            break
        yield p

rawDescription: How to iterate over a dictionary - n key-value pairs at a time

description: Iterate over a dictionary - n key-value pairs at a time


code:
# assuming sensible bases:
class DB(object):
    ...

class Transformation(object):
    def transform(self, obj):
        ...

    def get(self, id):
        return self.transform(super(Transformation, self).get(id))

class Cache(object):
    def __init__(self, *args, **kwargs):
        self.cache = Cache()
        super(Cache, self).__init__(*args, **kwargs)
    def get(self, id):
        if id in self.cache:
            return self.cache.get(id)
        else:
            self.cache.set(id, super(Cache, self).get(id))
            return self.cache.get(id)

class DBwithTransformation(Transformation, DB):
    # empty body
    pass

rawDescription: Is it possible to emulate Scala's traits in Python?