code:
import re
from collections import namedtuple

Format = namedtuple('Format', 'name format_string regex')
class Parser(object):
    replacements = [Format('server', '%S', r'[A-Za-z0-9]+'),
                    Format('user', '%U', r'[A-Za-z0-9]+'),
                    Format('date', '%D', r'[0-9]{4}-[0-9]{2}-[0-9]{2}'),
                    Format('process_id', '%P', r'[0-9]+'),
                    ]

    def __init__(self, format):
        self.format = format
        self.re = re.compile(self._create_regex(format))

    def _create_regex(self, format):
        format = re.escape(format)
        for replacement in self.replacements:
            format = format.replace(r'\%s' % replacement.format_string,
                                    r'(?P<%s>%s)' % (replacement.name,
                                                     replacement.regex,
                                                     ),
                                    )
        return format

    def parse(self, data):
        match = self.re.match(data)
        if match:
            return match.groupdict()
        return None

rawDescription: Python Emulate strptime behavor

description: Python Emulate strptime behavor


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

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


code:
@app.task(bind=True)
def gen_crunched_numbers(self, cache_key):
    res = do_heavy_lifting()
    cache.set(cache_key, res)
    return res

rawDescription: Celery task best practices in Django/Python


code:
import sys
sys.dont_write_bytecode = True

rawDescription: Python: can 2.X be patched so as to totally ignore *.pyc files?


code:
fig = plt.figure(figsize=(8.0,6.0))
ax1 = fig.add_subplot(221)

vmax=30
vmin=0
levels = np.linspace(vmin,vmax,100)

z_contourR = ax1.contourf(xlist,ylist,z_matrixR,levels=levels,vmax=vmax,vmin=vmin)
plt.xlabel('x')
plt.ylabel('y')
plt.title('real')
cbarR = plt.colorbar(z_contourR)

ax2 = fig.add_subplot(222)
z_contourI = ax2.contourf(xlist,ylist,z_matrixI,levels=levels,vmax=vmax,vmin=vmin)
plt.xlabel('x')
plt.ylabel('y')
plt.title('imaginary')
cbarI = plt.colorbar(z_contourI)

plt.tight_layout()
plt.show()

rawDescription: Python same colorbar for subplots of real / imaginary contour plots of function


code:
def get_stones():
    colors = list('RGBW')
    forms = list('+-*_')
    random.shuffle(colors)
    random.shuffle(forms)
    return [c+f for c,f in zip(colors,forms)]

for i in range(5):
    print(get_stones())

rawDescription: compare string to list elements python

description: Compare string to list elements python


code:
# Taken from http://flask.pocoo.org/snippets/6/

from flask import url_for
from werkzeug.routing import BuildError

def permalink(function):
    def inner(*args, **kwargs):
        endpoint, values = function(*args, **kwargs)
        try:
            return url_for(endpoint, **values)
        except BuildError:
            return
    return inner

rawDescription: add slugified title to url

description: Add slugified title to url


rawDescription: python Socket send ascii command and receive response

description: Python Socket send ascii command and receive response


code:
while True:
  result = pipe.stdout.readline()
  if not result:
    break
  # do something with result

rawDescription: Call Perl script from Python constantly returning values


code: def toSec(p): return fs('h',p)*3600 + fs('mn',p)*60 + fs('sc',p)

rawDescription: Python convert time duration in words to seconds


code:
import numpy as np
import timeit
t_df.to_csv('data.tsv', sep='\t', index_label='date_time')
t = timeit.Timer("from __main__ import read_table; read_table('data.tsv', sep='\t', index_col=0, parse_dates=True)")
np.mean(t.repeat(10, number=1))

rawDescription: Python Pandas: What is the fastest way to create a datetime index?


code: print (df.pivot_table(index='a', columns='b', values='c'))

rawDescription: Sorting data in a pandas DataFrame by names in column


code: _, x, _ = sorted([lb, x, ub])

rawDescription: How to force a value to be within a range?

description: Force a value to be within a range


code:
my_list = ['one', 'two', 'three', 'four']
self.response.write(','.join(my_list))

rawDescription: Get list from server and print each element surrounded by span tag


code:
from itertools import chain, izip
import re

def nonconsumesplit(regex,content):
    outer = re.split(regex,content)
    inner = re.findall(regex,content)+['']
    return [val for pair in zip(outer,inner) for val in pair]

rawDescription: Splitting longer patterns using regex without losing characters Python 3+


rawDescription: Finding clusters of numbers in a list

description: Finding clusters of numbers in a list


code:
from collections import OrderedDict
OrderedDict(sorted(inventory.items(), key=lambda i: (cat_order[i[1][0]], -i[1][1])))

rawDescription: python dictionary sorted by list

description: Python dictionary sorted by list


code:
from xml.etree import ElementTree as ET

tree = ET.parse('yourxmlfilename.xml')
species = tree.find(".//species[@name='MyHeterotrophEPS']")
names = species.attrib['header']
array = numpy.genfromtxt((line.rstrip(';') for line in species.text.splitlines()),
    delimiter=',', names=names)

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

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


rawDescription: All paths of a tree

description: All paths of a tree


code:
import numpy as np

def func1(a, b, c, d):
    A = np.cos(a) * np.cos(b) - np.sin(a) * np.sin(b) * np.sin(c - d)
    return A

def func2(a, b, c, d):
    s = np.sin(c - d)
    A = 0.5*((1 - s)*np.cos(a - b) + (1 + s)*np.cos(a + b))
    return A

rawDescription: Improve performance of operation on numpy trigonometric functions


code: from collections import defaultdict

rawDescription: Python: How to read in file once but not exhaust iterator in nested for loops


code:
def sim(params,timerange):
    k1,k2,N = params

rawDescription: Unpacking parameters for a simulation

description: Unpacking parameters for a simulation


code:
import pandas as pd
d = {"A":[3, 3, 3, 2, 3, 3, 2, 2, 2, 3, 3, 2], "B": [3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3]}
df = pd.DataFrame.from_dict(d)

mask = (df == 2) & (df.shift() == 2)

inds_a = mask["A"][mask["A"]].index.values
inds_b = mask["B"][mask["B"]].index.values

for ind in [inds_a, inds_b]:
    for sub in np.split(ind,   np.where(np.diff(ind) != 1)[0]+1):
        print("2 appeared at {} to {}".format(sub[0]-1, sub[-1]))

rawDescription: Identify continuous occurrences of a value in a dataframe


code: weights = df2.drop_duplicates('key', keep='last').set_index('key')['value']

rawDescription: What is the fastest way to do greedy set cover with Pandas?


code: ivd = dict((v, k) for (k, v) in a.iteritems())

rawDescription: Reverse the one-to-one mapped dictionary


code:
x='aaa12333bb445bb54b5b52'
import string
all=string.maketrans('','')
nodigs=all.translate(all, string.digits)
x.translate(all, nodigs)

rawDescription: Python: removing characters except digits from string


code: link((wpick, 'value'), (counter, 'value'))

rawDescription: Linking ipython widget button and slider values


code: facts_key = db.Query(Details, keys_only=True).ancestor(john).get()

rawDescription: how to get a descendant from parent entity

description: Get a descendant from parent entity


rawDescription: Shortest code for depleting a generator in code golfing


code: re.match(r"\d*", somestring).group()

rawDescription: Regular expression to group a pattern OR group empty string as ""


code:
from collections import Counter
binsize = 100
Counter(x - x % binsize for _, l in mylist for x in l)

rawDescription: Python: how can I count the occurrences in a list of tuples?


rawDescription: Sympy - Display Full Equation in Dense Matrix Form


code:
def interpolate(ts, target):
    ts1 = ts.sort_index()
    b = (ts1.index > target).argmax() # index of first entry after target
    s = ts1.iloc[b-1:b+1]
    # Insert empty value at target time.
    s = s.reindex(pd.to_datetime(list(s.index.values) + [pd.to_datetime(target)]))
    return s.interpolate('time').loc[target]

rawDescription: Interpolate only one value of a TimeSerie using Python / Pandas


code:
import sys

def write_cols(data,n,f = sys.stdout):
    for chunk in (data[i:i+n] for i in range(0,len(data),n)):
        print('\t'.join(chunk), file = f)

rawDescription: python, zip:Discard extra elements

description: Python, zip:Discard extra elements


code: alist, blist, clist, dlist, elist = ([] for i in range(5))

rawDescription: Python - Initializing Multiple Lists/Line


code:
def future6(A):
    # list of tuples (index into A, value in A)
    # invariant: indexes and values in sorted order
    known = []
    result = []
    for idx in xrange(len(A) - 1, -1, -1):
        value = A[idx]
        # since known is sorted a binary search could be applied here
        # I haven't bothered

        # anything lower then the current value
        # cannot possibly be used again, since this value will be first index instead
        # of those
        known = [(x,y) for x,y in known if y > value]

        if known:
            # all values in known are > current value
            # they reverse sorted by index
            # the value with the lowest index is first
            result.append(known[-1][0])
        else:
            # no values exist this high, report -1
            result.append(-1)
        # add to end of the list to maintain invariant
        known.append( (idx, value) )

    # let numpy worry about reversing the array
    return np.array(result)[::-1]

rawDescription: Numpy vectorization algorithms to find first future element greater than current element


code: list(range(li[n], li[n] + length))

rawDescription: Function that returns all sub-sequences in a list that are successive


code: [b-a for a,b in zip(counters,counters[1:])]

rawDescription: Operating on pairs of value in an array

description: Operating on pairs of value in an array


code:
import sqlalchemy as sa
DBSession.query(Article).group_by( sa.func.year(Article.created), sa.func.month(Article.created)).all()

rawDescription: group by year, month, day in a sqlalchemy

description: Group by year, month, day in a sqlalchemy


code:
#You can extend python path for looking up modules
#prefs.add('python_path', '~/python/')

rawDescription: How to configure sublimerope to auto complete python code?

description: Configure sublimerope to auto complete python code


rawDescription: Fast way to find locally maximal gradient values in a numpy array?


rawDescription: random.randint not generating random values

description: Random.randint not generating random values


code: map(list, zip(cities, airports))

rawDescription: Using list comprehension in Python to do something similar to zip()?


code: subprocess.call(['scp', '%s@sew.cwe.edu:' % user_acc + ''.join(dir_[0][:-1]), '%s/%s/dicom/' % (dicom_dir, sub)])

rawDescription: Can't copy file sshing, and coping files in a directory


code:
def add_some_context(context={}):
    context['foo'] = 'bar'
    return context

def view1(self, request):
    context = {
        'something1': 'blah',
    }
    return render_to_response('template1.html', add_some_context(context), context_instance=RequestContext(request))

def view2(self, request):
    context = {
        'something2': 'blah',
    }
    return render_to_response('template2.html', add_some_context(context), context_instance=RequestContext(request))

rawDescription: Django: Views within views

description: Django: Views within views


code: label = menuitem.child

rawDescription: How can I change the display text of a MenuItem in Gtk2?

description: Change the display text of a MenuItem in Gtk2


code:
for pt in root.findall('.//plaintext'):
    if pt.text:
        print(pt.text)

rawDescription: Python Xml Parsing for Wolfram Api

description: Python Xml Parsing for Wolfram Api


code:
import platform
platform.architecture()

rawDescription: Do I have Numpy 32 bit or 64 bit?

description: Do I have Numpy 32 bit or 64 bit


rawDescription: Alphabet range python

description: Alphabet range python


code:
# plot power spectral densities
plt.figure(2)
plt.psd(data, Fs=200000, label="raw")
plt.psd(data_bp8, Fs=20000, label="order 8")
plt.psd(data_bp2, Fs=20000, label="order 2")
plt.legend()

plt.show()

rawDescription: Most efficient way to filter a long time series Python


rawDescription: setting margins in matplotlib/seaborn with subplots

description: Setting margins in matplotlib/seaborn with subplots


code:
decrypt=input("Please enter the key you used to encrypt the above text: ")
key = [ord(d) for d in decrypt]
key

rawDescription: Python: Sum of numbers

description: Python: Sum of numbers


code:
df = df.set_index(['code', 'colour']).sort_index()
df

rawDescription: Pandas `DataFrameGroupBy` and `SeriesGroupBy`


code:
with open('c:\file.txt', 'w') as file:
    file.write('\n'.join(list_a + list_b))

rawDescription: output list of special letters string with python

description: Output list of special letters string with python


code:
def parse(dic):
    for k, v in dic.items():
        if isinstance(v, dict):
            for p in parse(v):
                yield [k] + p
        else:
            yield [k, v]

lst = list(parse(dic))

rawDescription: Python 2.7 how should I parse a dict object?


code:
data = np.array([10, 20, 200, 5], dtype=np.uint8)
ii = np.iinfo(data.dtype)
ii.max
ii.min
ii.bits

rawDescription: Determine maximum allowed sample value in wave file in Python


code: cd /tmp/pip_build_falsetru/netifaces

rawDescription: Breaking 'pip install' to smaller steps, so I can edit the package before it is installed


code:
import logging

class LevelFilter(logging.Filter):
    """
    http://stackoverflow.com/a/7447596/190597 (robert)
    """
    def __init__(self, level):
        self.level = level

    def filter(self, record):
        return record.levelno >= self.level

def do_logging(turn):
    logger = logging.getLogger('extra')
    logger.info('some info turn %d' % turn)
    logger.debug('this is debug fudge turn %d' % turn)

rootLogger = logging.getLogger()
handler = logging.StreamHandler()
rootFormatter = logging.Formatter('root - %(levelname)s: %(msg)s')
handler.setFormatter(rootFormatter)
rootLogger.addHandler(handler)
rootLogger.setLevel(logging.DEBUG)
do_logging(1)

extraLogger = logging.getLogger('extra')
extraHandler = logging.StreamHandler()
extraFormatter = logging.Formatter('extra - %(levelname)s: %(msg)s')
extraHandler.setFormatter(extraFormatter)
extraLogger.addHandler(extraHandler)

# Put the Filter on the Handler so only INFO and higher is handled
extraHandler.addFilter(LevelFilter(logging.INFO))

# Handle everything (so it can propagate to root)
extraLogger.setLevel(logging.DEBUG)
do_logging(2)

rawDescription: Python logging: propagate messages of level below current logger level


code:
t = Timeout(30)
for perm in permutations(x):
    if isPrime(int(perm)):
        large = perm
        t.reset()
    t.check()

rawDescription: Check for idle condition in running python program - Euler #41


code:
nucl_id = nucleotide_accession.split()[-1]

handle = Entrez.efetch(db="nucleotide",
                       id=nucl_id,
                       rettype="gb",
                       retmode="text")
seq_record = SeqIO.read(handle, "gb")

rawDescription: Retrieve DNA sequence using a gene identifier of a protein


rawDescription: Write a loop using python

description: Write a loop using python


code: A = numpy.delete(A, 1)

rawDescription: How do I easily (and efficiently) delete a single element from array?

description: Easily (and efficiently) delete a single element from array


code:
{sql: "SELECT DISTINCT p.id_product,\n                p.price AS price,\n        \
    \        sp.reduction AS discount,\n                sp.reduction_type AS discount_type,\n\
    \                pl.description_short AS description\n    FROM ...."}

rawDescription: Save SQL to YAML as is

description: Save SQL to YAML as is


code:
def sieve(limit=1000000):
    '''Returns a generator that yields the prime numbers up to limit.'''

    sieve_limit = gmpy2.isqrt(limit) + 1
    limit += 1
    # Mark bit positions 0 and 1 as not prime.
    bitmap = gmpy2.xmpz(3)
    # Process 2 separately. This allows us to use p+p for the step size
    # when sieving the remaining primes.
    bitmap[4 : limit : 2] = -1
    # Sieve the remaining primes.
    for p in bitmap.iter_clear(3, sieve_limit):
        bitmap[p*p : limit : p+p] = -1
    return bitmap.iter_clear(2, limit)

rawDescription: Efficiently convert gmpy2.mpz to numpy boolean array


code:
df.index.name = 'date'
df

rawDescription: How to read csv file into pandas DataFrame with multiple row index level?

description: Read csv file into pandas DataFrame with multiple row index level


rawDescription: groupby multiple values, and plotting results

description: Groupby multiple values, and plotting results


code:
from itertools import groupby
from operator import itemgetter

def filter_data(data):
    for classname, group in itertools.groupby(data, itemgetter(2)):
        filtered_group = [line for line in group if some_condition(line)]
        new_count = len(filtered_group)
        for line in filtered_group:
            line[5] = new_count
            yield line

rawDescription: How to recount number in each class from a read csv

description: Recount number in each class from a read csv


code: [groups[k] for k in sorted(groups.keys())]

rawDescription: Nest a flat list based on an arbitrary criterion


code:
plt.close('all')
plt.plot([1,2,3], [4, 7, 1])
plt.gca().set(xlabel='x', ylabel='y')

rawDescription: Matplotlib: set x y label in one line

description: Matplotlib: set x y label in one line


code:
['Cheryl Mills',
 'Capricia Marshall',
 'Anne-Marie Slaughter',
 'Phillip Crowley',
 'Hillary Clinton',
 'l',
 'Linda Dewan']

rawDescription: How to add nodes and edges to a network analysis graph in Networkx?

description: Add nodes and edges to a network analysis graph in Networkx


code:
A = [[1,2,3], [2,3,4], [3,4,5], [3,2,4]]
seen = set()
smaller_A = []
for x in A:
    if frozenset(x) not in seen:
        smaller_A.append(x)
        seen.add(frozenset(x))
smaller_A

rawDescription: Python and remove duplicates in list of lists regardless of order within lists


code:
import re
text = 'Hi, my name is Will, And i am from Canada. I have 2 pets. One is a dog and the other is a Zebra. Ahoi! Thanks.'
re.split('[.!]', text)

rawDescription: Separating a string using predefined characters


code:
def mouseMoveEvent(event):
  """
    Mouse is moved inside the widget
  """
  # get mouse position
  x = event.x()
  y = event.y()
  # find coordinates of grid rectangle in your grid
  # move rectangular rubber band to this grid rectangle

rawDescription: Make certain regions of an image selectable


code:
class Action:
    pass

class Move(Action):
    pass

class Look(Action):
    pass

actions = []
global_objs = list(globals().items())

for name, obj in global_objs:
    if obj is not Action and isinstance(obj, type) and issubclass(obj, Action):
        actions.append(obj())
print(actions)

rawDescription: Python Instantiate All Classes Within a Module


code:
import os
for i in range(len(operations)):
     os.system("python myScript.py {:} {:} > out.log".format(arg1,arg2))

rawDescription: Launch a single python script as different processes differing by command line arguments


code:
query = """UPDATE animal SET name = %s
           WHERE name = %s
           """
values = ("snake", "turtle")

cursor.execute(query, values)
cursor2.execute(query, values)

rawDescription: Python MySQLdb placeholders syntax

description: Python MySQLdb placeholders syntax


rawDescription: boost python, using a namespace other than main global

description: Boost python, using a namespace other than main global


code:
import scipy.stats.mstats as mstats
import numpy as np

data = np.arange(12).reshape((3,4))
data[data % 3 == 0] = -1
mask = data <= 0
mdata = np.ma.masked_array(data, mask)
zscores = mstats.zscore(mdata, axis=1, ddof=1)
print(zscores)

rawDescription: How to apply a function on a subset of numpy.ndarray object

description: Apply a function on a subset of numpy.ndarray object


code: dict((d['name'], d['sex']) for d in input_structure)

rawDescription: turn tuple of dicts into single dict

description: Turn tuple of dicts into single dict


rawDescription: build matrix from blocks

description: Build matrix from blocks


code:
output = delete(ExampleOne).where(
    ExampleOne.value_a < 10,
).returning(
    ExampleOne.id,
    ExampleOne.value_a,
).cte('output')
query = insert(ExampleTwo, inline=True).from_select(
    ['id', 'value_a', 'value_b'],
    select(
        ['id', 'value_a', literal(3)]
    ).select_from(output)
)
query.compile(engine)

rawDescription: How can I delete rows from one table and insert into another using SQLAlchemy and Postgres RETURNING?

description: Delete rows from one table and insert into another using SQLAlchemy and Postgres RETURNING


code:
text = "Call me Ishmael. Some years ago - never mind how long precisely - having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to previous_wordent me from deliberately stepping into the street, and methodically knocking people's hats off - then, I acmatches_count it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me."
match_previous(text.split(),"the")
match_previous(text.split(),"ship.")
match_previous(text.split(),"whale")
match_previous(text.split(),"Call")

rawDescription: Preceding Word Length

description: Preceding Word Length


rawDescription: Is it possible to np.concatenate memory-mapped files?


code: 1 - a.iloc[lind]

rawDescription: Change values over specific indexes in DataFrame


code: playAgain = input('Do you want to play again? (yes or no)')

rawDescription: How do I use 2 loops, and if it is false, repeat the program indefinetely

description: Use 2 loops, and if it is false, repeat the program indefinetely


code:
def count_function(iterable, item):
    count = 0

    for item_from_iterable in iterable:
        if item_from_iterable == item:
            count += 1

    return count

rawDescription: If there any way to add this code to a function?


code:
with masking(globals(), wages < 50001):
    tinc = agi - dec
    tinc = tinc - exem

with masking(self.__dict__, self.wages < 50001):
    self.tinc = self.agi - self.dec
    self.tinc = self.tinc - self.exem

# etc.

rawDescription: Apply a mask to multiple lines (syntactic sugar?)


code:
mylist = ['student_number', 'student_age', 'student_marks', 'subject_name',
          'subject_marks', 'subject_date' , 'ass_name' , 'ass_number' ]

rawDescription: How can i convert the list into three lists based on criteria and put in dictionary

description: Convert the list into three lists based on criteria and put in dictionary


code:
B
B/a
B/b
B/c
B/x
B/y
B/z

rawDescription: Manipulation of directories java/python

description: Manipulation of directories java/python


rawDescription: Avoiding unnecessary memory consumption in Python and wxPython


code:
if row['and_so_on'] == 'peter pan':
   do_some_funny_stuff()

rawDescription: python copy to multiple variables

description: Python copy to multiple variables


rawDescription: What data structure container can be sorted by date


rawDescription: Exclude weekends in python django query set


code:
a = (((df.type == 0) & ((df.subtype == 2) | (df.subtype == 3) |
         (df.subtype == 5) | (df.subtype == 6))) |
         ((df.type == 5) & ((df.subtype == 3) | (df.subtype == 4) | (df.subtype == 7) |
         (df.subtype ==  8))))
a.sum()

rawDescription: Pandas OR statement ending in series contains


code:
s = 'abcdefghijklmnopqrstuvwxyz'
s.find('c')
s[s.find('c') - 6]

rawDescription: Subtract letters in Python

description: Subtract letters in Python


rawDescription: Python program not receiving output of printf


code: set.intersection( *[ set(tuple(x) for x in d[k]) for k in d ] )

rawDescription: Intersection of subelements of multiple list of lists in dictionary


code:
def get_entire_resultset( query, *args, **kargs ):
   ps = 100; pn = 1
   objs = query( *args, page_size=ps, page_number=pn, **kargs )
   total_downloaded = ps
   pn += 1
   total = int(objs.TotalNumResults)
   while total_downloaded<total:
      objs.extend( query( *args, page_size=ps, page_number=pn, **kargs ) )
      total_downloaded += ps
      pn += 1
   return objs

rawDescription: An elegant way of page-to-page iterating through boto.resultset.ResultSet


rawDescription: PyQt4: why do we need to pass class name in call to super()


code:
table = {}
for ccy in ccyAr:
    table[ccy] = {}
    for d in data:
        table[ccy][d] = 0

rawDescription: Create and instantiate python 2d dictionary


code:
@ensure_csrf_cookie
def dummy(request):
    return HttpResponse("done")

rawDescription: Handling CSRF tokens in Django

description: Handling CSRF tokens in Django


code:
def cintelpair3(*args):
    if any(args.count(card) == 2 for card in ['Ace', 'Two']):
        print("You have a pair")

rawDescription: Checking several values/strings/variables for a pair of phrases in python 3.3


rawDescription: Python - When duplicate items are identified, which object does a set or frozenset take?


code:
[x.id for x
 in self.db.query(schema.allPostsUuid).execute(timeout=20)
 if x.type == 'post'
    and x.deleted is not False
    and ...
    and ...]

rawDescription: How to indent Python list-comprehensions?

description: Indent Python list-comprehensions


rawDescription: Python 3 CGI: how to output raw bytes

description: Python 3 CGI: how to output raw bytes


rawDescription: Can I keep decimal precision while using integer division in Python?

description: Keep decimal precision while using integer division in Python


code: df.pivot_table(values="incoming", index="month", columns="goods", aggfunc="sum")

rawDescription: Filling gaps for cumulative sum with Pandas


rawDescription: How do I get the IP address from a http request using the requests library?

description: Get the IP address from a http request using the requests library


code: self.parent.add_line_dynamic(machine_name, sim_port, sim_script, status)

rawDescription: Wxpython Listctrl

description: Wxpython Listctrl


code:
class Milestone(models.Model):
    project = ForeignKey(Project, related_name='milestones')

rawDescription: Unable to get ToMany to work in Tastypie


code:
import json

class CustomJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if hasattr(obj, 'isoformat'): #handles both date and datetime objects
            return obj.isoformat()
        elif hasattr(obj, 'total_seconds'): #handles both timedelta objects
            return str(obj)
        if isinstance(obj, Decimal): #handles decimal timedelta objects
            return float(obj)
        else:
            return json.JSONEncoder.default(self, obj)

rawDescription: python structure JSON serializing issue

description: Python structure JSON serializing issue


code:
def on_enter(self):
    super(MyLayer, self).on_enter()
    director.window.push_handlers(self.keys)

rawDescription: Key polling with cocos2d-python and pyglet


rawDescription: Sum of dict elements

description: Sum of dict elements


code:
from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

DATA=[('imageformats',['C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qjpeg4.dll',
'C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qgif4.dll',
'C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qico4.dll',
'C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qmng4.dll',
'C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qsvg4.dll',
'C:\\Python27/Lib/site-packages/PyQt4/plugins/imageformats/qtiff4.dll'
])]

setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': True,"includes":["sip"]}},
    windows = [{'script': "main.py"}],
    zipfile = None,
    data_files = DATA,
)

rawDescription: How to build Python 3.4.3 source code with pygame to exe?

description: Build Python 3.4.3 source code with pygame to exe


code:
fields = ['val', 'other_field']
pd.DataFrame([{fn: getattr(f, fn) for fn in fields} for f in allFoo])

rawDescription: python class attributes to pandas dataframe

description: Python class attributes to pandas dataframe


code: list(reversed(range(current+1, desti)))

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

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


code:
sol[0,0]
np.linalg.lstsq(A, x[0,0])[0]
sol[1,0]
np.linalg.lstsq(A, x[1,0])[0]

rawDescription: Broadcasting issues with numpy.linalg.lstsq


rawDescription: Using a shared variable in a function

description: Using a shared variable in a function


code: filtered = [word for word in query if len(word) > 3]

rawDescription: Python list is behaving weird

description: Python list is behaving weird


code:
from setuptools import setup, find_packages

setup(name='foobar',
      version='0.9.0',
      packages=find_packages(),
) #NOTE: no install_requires

rawDescription: Require BeautifulSoup in a Python Package - What's needed in setup.py?


code:
from functools import singledispatch, update_wrapper

def methdispatch(func):
    dispatcher = singledispatch(func)
    def wrapper(*args, **kw):
        return dispatcher.dispatch(args[1].__class__)(*args, **kw)
    wrapper.register = dispatcher.register
    update_wrapper(wrapper, func)
    return wrapper

rawDescription: How can I use functools.singledispatch with instance methods?

description: Use functools.singledispatch with instance methods


code:
from collections import Counter
from nltk import FreqDist, word_tokenize

from nltk.corpus import brown

# Using Counter.
x = Counter()
with open('myfile.txt', 'r') as fin:
    for line in fin.split('\n'):
        x.update(word_tokenize(line))
uniq = x.keys()

# Using Set.
x = set()
with open('myfile.txt', 'r') as fin:
    for line in fin.split('\n'):
        x.update(word_tokenize(line))
uniq = x.keys()

rawDescription: How to quickly get the collection of words in a corpus (with nltk)?

description: Quickly get the collection of words in a corpus (with nltk)


code:
import itertools as it
{k:a.get(k,'')+'\t'+b.get(k,'') for k in set(it.chain(a.iterkeys(),b.iterkeys()))}

rawDescription: joining two dictionaries and handling its common value

description: Joining two dictionaries and handling its common value


code: getattr(re.search('(\d+)@', s), 'groups', lambda: ('0',))()

rawDescription: Sorting a list using a regex in Python

description: Sorting a list using a regex in Python


rawDescription: Python MySQL query to CSV in memory

description: Python MySQL query to CSV in memory


rawDescription: How to change the value stored in memory adress?

description: Change the value stored in memory adress


rawDescription: dividing floating points into buckets

description: Dividing floating points into buckets


rawDescription: Find all countinuous repeating characters using a simpler solution or preferably a regex


code:
n = 4 # Number of colors
new_colors = [plt.get_cmap('jet')(1. * i/n) for i in range(n)]

plt.rc('axes',
       prop_cycle=(cycler('color', new_colors) +
                   cycler('linestyle', ['-', '--', ':', '-.'])))

for i in range(4):
    plt.plot([_ + i for _ in x])

rawDescription: Use colormaps along with matplotlib cycler


rawDescription: How to change the key of a nested dictionary

description: Change the key of a nested dictionary


code:
sheet_name = 'Data'

chart.add_series({
    'categories': [sheet_name, 0, 0, 4, 0],
    'values':     [sheet_name, 0, 1, 4, 1],
    'line':       {'color': 'red'},
})

rawDescription: Pass string in add_series in xlsxwriter

description: Pass string in add_series in xlsxwriter


code:
def csvContainer(col1, col2, col3, col4=(), col5=()):
    container = [
        [x for x in items if x is not None] for items in
        itt.izip_longest(col1, col2, col3, col4, col5)
        ]

rawDescription: Better conditional lists

description: Better conditional lists


code:
def get_user_attributes(cls):
    boring = dir(type('dummy', (object,), {}))
    attrs = {}
    bases = reversed(inspect.getmro(cls))
    for base in bases:
        if hasattr(base, '__dict__'):
            attrs.update(base.__dict__)
        elif hasattr(base, '__slots__'):
            if hasattr(base, base.__slots__[0]):
                # We're dealing with a non-string sequence or one char string
                for item in base.__slots__:
                    attrs[item] = getattr(base, item)
            else:
                # We're dealing with a single identifier as a string
                attrs[base.__slots__] = getattr(base, base.__slots__)
    for key in boring:
        del attrs['key']  # we can be sure it will be present so no need to guard this
    return attrs

rawDescription: Inspect python class attributes

description: Inspect python class attributes


code:
andback = result.stack().apply(lambda t: pd.Series({'Projected': t[0],
                                              'Actual': t[1],
                                              'Previous': t[2]}))

rawDescription: Pivot a pandas dataframe and get the non-axis columns as a series


code: {{ d.content }}

rawDescription: Dynamic use of templates in Jinja2

description: Dynamic use of templates in Jinja2


code:
def resolve(root, *keys):
   for key in keys:
       try:
           root = root[key]
       except (KeyError, IndexError):
           return None
   return root

rawDescription: Concise replacement for long list of Try-Except statement in Python?


code:
from collections import Counter
counts = Counter(s)

rawDescription: Python loop improvements

description: Python loop improvements


code:
import csv
import numpy as np
import itertools as IT
my_files = ['fileA.txt', 'fileB.txt']

vals = None
for num, filename in enumerate(my_files, 1):
    arr = np.genfromtxt(filename, dtype=None, delimiter='\t', skiprows=1, usecols=(2,))
    print(arr)
    if vals is None:
        vals = arr
    else:
        vals += arr

meanvals = vals / num

with open(my_files[0], 'rb') as fin, open('/tmp/test.csv', 'wb') as fout:
    # skip first row
    next(fin)
    writer = csv.writer(fout, delimiter='\t', lineterminator='\n')
    for row, val in IT.izip(csv.reader(fin, delimiter='\t'), meanvals):
        row[2] = val
        writer.writerow(row)

rawDescription: Calculating the mean across multiple files


code:
import numpy as np
import gzip
import cPickle as pkl
import h5py

a = np.random.randn(100000, 10)
b = np.hstack( [a[cnt:a.shape[0]-10+cnt+1] for cnt in range(10)] )

f_hdf5_chunk_1 = h5py.File('noise_chunk_1.hdf5', 'w')
f_hdf5_chunk_1.create_dataset('b', data = b, compression = 'gzip', compression_opts = 9, chunks = (1,100))
f_hdf5_chunk_1.close()

f_hdf5_chunk_10 = h5py.File('noise_chunk_10.hdf5', 'w')
f_hdf5_chunk_10.create_dataset('b', data = b, compression = 'gzip', compression_opts = 9, chunks = (10,100))
f_hdf5_chunk_10.close()

f_hdf5_chunk_100 = h5py.File('noise_chunk_100.hdf5', 'w')
f_hdf5_chunk_100.create_dataset('b', data = b, compression = 'gzip', compression_opts = 9, chunks = (100,100))
f_hdf5_chunk_100.close()

f_hdf5_chunk_1000 = h5py.File('noise_chunk_1000.hdf5', 'w')
f_hdf5_chunk_1000.create_dataset('b', data = b, compression = 'gzip', compression_opts = 9, chunks = (1000,100))
f_hdf5_chunk_1000.close()

f_hdf5_chunk_10000 = h5py.File('noise_chunk_10000.hdf5', 'w')
f_hdf5_chunk_10000.create_dataset('b', data = b, compression = 'gzip', compression_opts = 9, chunks = (10000,100))
f_hdf5_chunk_10000.close()

rawDescription: Why do pickle + gzip outperform h5py on repetitive datasets?


code: tuples = [('a', 'b') for _ in xrange(4)]

rawDescription: Best way to show a list of empty tuples

description: Show a list of empty tuples


code: nans, x= np.isnan(y), lambda z: z.nonzero()[0]

rawDescription: Interpolate NaN values in a numpy array

description: Interpolate NaN values in a numpy array


code:
dates = df.index.take(np.random.randint(0,100,10))
dates

rawDescription: Pandas get specific rows from HDF5 by index


code:
import sys;sys.version
print()

rawDescription: python IDLE vs Mac Terminal print statement

description: Python IDLE vs Mac Terminal print statement


code:
oldColumns = data.schema.names
newColumns = ["name", "age"]

df = reduce(lambda data, idx: data.withColumnRenamed(oldColumns[idx], newColumns[idx]), xrange(len(oldColumns)), data)
df.printSchema()
df.show()

rawDescription: How to change dataframe column names in pyspark?

description: Change dataframe column names in pyspark


code: test()

rawDescription: Python: Trying to mock datetime.date.today() but not working


code:
for i in deposits:
    mydict[i[:2]] += float(i[2:])/100

rawDescription: adding two items in a list to make into one item

description: Adding two items in a list to make into one item


code:
import urllib2

def open_url_with_default_protocol(*args, **kwargs):
    #  Use the HTTP scheme by default if none is given
    #  pass through all other arguments to urllib2.urlopen

    default_scheme = 'http://'

    url = args[0]
    scheme, address = urllib2.splittype(url)

    if not scheme:
        #  Replace the url in the args tuple by a URL with the default scheme
        args = (default_scheme + args[0],) + args[1:]

    return urllib2.urlopen(*args, **kwargs)

rawDescription: How do I fetch url with urllib2 without declaring a url scheme?

description: Fetch url with urllib2 without declaring a url scheme


code:
import collections
dictclass = collections.OrderedDict

class Foo(collections.MutableMapping):
    def __init__(self, *args, **kwargs):
        self._data = dictclass(*args, **kwargs)
    def __setitem__(self, key, value):
        self._data[key] = value
    def __delitem__(self, key):
        del self._data[key]
    def __iter__(self):
        return iter(self._data)
    def __len__(self):
        return len(self._data)

    def __getitem__(self,key):
        try:
            return self._data[key]
        except KeyError:
            pass

        data = key*2
        self[key] = data
        return data

    def __contains__(self,whatever):
        return dictclass.__contains__(self,whatever) or 'bar' in whatever

rawDescription: Why does overriding __contains__ break OrderedDict.keys?


code:
import pandas as pd

a=[['Date', 'letters', 'data1', 'data2'], ['1/2/2014', 'a', 6, 1], ['1/2/2014', 'a', 3, 1], ['1/3/2014', 'c', 1, 3]]
df = pd.DataFrame.from_records(a[1:],columns=a[0])

from operator import add, sub, mul

d = dict(a=add, b=mul, c=sub)

df['result'] = df.apply(lambda r: d[r['letters']](r['data1'], r['data2']), axis=1)

rawDescription: pandas if then statement without looping

description: Pandas if then statement without looping


code:
for b in range(len(bullets)):
  bullets[b][0]-=10

rawDescription: How to create bullets in pygame?

description: Create bullets in pygame


code:
dct = { 'wassup' : {
            'termfreq' : 'daily',
            'docid' : 1,
            'pos' : [3,4] }}

rawDescription: positional index using python

description: Positional index using python


code: reWords = '\p{Letter}+'

rawDescription: regex to match graphic characters like smileys or word characters from multiple languages

description: Regex to match graphic characters like smileys or word characters from multiple languages


code:
def setBit(dint, offset):
    mask = 1 << offset
    return dint | mask

r = 0
r = setBit(r, 5)
# 32

rawDescription: Python Bit Operations on a list of Int's


code:
def mean(B):
        positives = [b for b in B if b >= 0]
        return sum(positives) / len(positives)

rawDescription: Screening out negatives before taking average


rawDescription: How to find all Python built-in private variables such as __file__, __name__

description: Find all Python built-in private variables such as __file__, __name__


code:
def are_coplanar(plane1, plane2):
  first = numpy.where(plane2)[0][0]  # TODO: handle all zeros
  return numpy.all(plane2 * plane1[first]/plane2[first] - plane1 == 0)

rawDescription: coplanar equations of a plane

description: Coplanar equations of a plane


code: rect.center = ("coordinates of center")

rawDescription: How to centre an image in pygame?

description: Centre an image in pygame


code:
from itertools import count
from collections import defaultdict
from functools import partial
counts = defaultdict(partial(count, 1))
next(counts['foo'])
next(counts['foo'])
next(counts['bar'])
next(counts['foo'])

rawDescription: Re Ranking data within a csv after row removal


code:
content = '''
<root>

content 1

</root>
'''

from lxml import etree

xmlDoc = etree.fromstring(content)
paragraph_element = xmlDoc.find('div/p')
print(paragraph_element)

rawDescription: Choosing a specific XML node using lxml in Python


code: np.einsum('ik,ijk,jk->k', W, Q, V)

rawDescription: How to generalize elementwise matrix operations in numpy

description: Generalize elementwise matrix operations in numpy


code:
x = pd.DataFrame([['sam',328],['ruby',3213],['jon',121]], columns = ['name', 'score'])
names = ['sam', 'ruby']
x['name'].isin(names)
x.loc[x['name'].isin(names), 'score'].sum()

rawDescription: Fast pandas filtering

description: Fast pandas filtering


code:
from itertools import count

# count(1) is "equivalent2 to [1..]
lfts = ((k, 4*k+2, 0, 2*k+1) for k in count(1))

rawDescription: Convert a Haskell code to Python or pseudocode


code:
x = [[1,2],[3,4]]
y = list(x)         #outer list is different
x is y

rawDescription: Python list([]) and []

description: Python list([]) and []


code:
left = (id1,id2,id3,id4,id5,id6,A,B,C)
right = (id1,id2,id3,id4,id5,id6,D,E,F)
middle = (id1,id2,id3,id4,id5,id6,G,H,I)

rawDescription: Union all type query with python pandas

description: Union all type query with python pandas


code:
numeric_cols = dfna._get_numeric_data().columns.tolist()
nonnumeric_cols = [c for c in dfna if c not in numeric_cols]

rawDescription: Pandas fill np.nan issue

description: Pandas fill np.nan issue


code: open(u'someUnicodeFilenameλ')

rawDescription: open file with a unicode filename?

description: Open file with a unicode filename


code: cls = getattr(sys.modules[__name__], build_options['build_type'])

rawDescription: In the context of a buildbot master.cfg file, how do I instantiate a class based on a string using native modules


code:
print(np.nan == np.nan)
# False

rawDescription: Python Numpy mask NaN not working

description: Python Numpy mask NaN not working


code:
('Tim Jones', 54)
('Anna Smith', 56)
('Barry Thomas', 88)

rawDescription: unsupported operand type(s) for +: 'int' and 'tuple'

description: Unsupported operand type(s) for +: 'int' and 'tuple'


code:
import re
import pyparsing as pp

txt='''\
nothing(blah)

options{ kA: vA, kB : vB}
   options{ kA: vA, kB : vB}

blah blah
    options{ kA: vA, kB : vB }  # tab
options{
    kA: vA,
    kB: vB,
    kC     :    vC
    }

    '''

ident = pp.Word(pp.alphas+"_", pp.alphanums+"_")
comma = pp.Suppress(',')
pair=ident+pp.Suppress(':')+ident
pair_list=pp.OneOrMore(pair)+pp.ZeroOrMore(comma+pair)
options=(pp.Suppress('{')+
         pair_list+
         pp.Suppress('}'))

for om in (m.group(1) for m in re.finditer(r'^\s*options({[^}]*})',txt, re.S | re.M)):
    res=options.parseString(om)
    data=dict(res[i:i+2] for i in range(0,len(res),2))
    print('"{}"=>{}==>{}'.format(om,res,data))

rawDescription: find a function with regex in python

description: Find a function with regex in python


code:
import libvirt
conn = libvirt.open('qemu+ssh://root@yourhost/system')
conn.defineXML(imagine_a_well_formated_domain_xml_here)

rawDescription: Python - libvirt

description: Python - libvirt


code:
class UnixHTTPServer(HTTPServer):
    address_family = socket.AF_UNIX

    def server_bind(self):
        SocketServer.TCPServer.server_bind(self)
        self.server_name = "foo"
        self.server_port = 0

rawDescription: Setting up an HTTP server that listens over a file-socket


code:
z = [2,3,4,5]
with open("output.txt", 'w') as my_file:
    my_file.write('a: %s' % ', '.join([str(i) for i in z]))

rawDescription: Elements of a list

description: Elements of a list


code:
from collections import Counter

with open("filename") as f:
    nsize = Counter(el.strip() for el in f)

rawDescription: Use text from file

description: Use text from file


code:
with open('in.txt') as f:
  lis = [x.split() for x in f]

for x in zip(*lis):
  for y in x:
    print(y+'\t', end='')
  print('\n')

rawDescription: Row-to-Column Transposition in Python

description: Row-to-Column Transposition in Python


code:
import networkx as nx
import pylab as plt

G=nx.Graph()
#Please note the use of the delimiter parameter below
G=nx.read_edgelist('test.edges', data= (('weight',int),), delimiter=";")
pos = nx.spring_layout(G)

#Here we go, essentially, build a dictionary where the key is tuple
#denoting an edge between two nodes and the value of it is the label for
#that edge. While constructing the dictionary, do some simple processing
#as well. In this case, just print the labels that have a weight less
#than or equal to 3. Variations to the way new_labels is constructed
#produce different results.
new_labels = dict(map(lambda x:((x[0],x[1]), str(x[2]['weight'] if x[2]['weight']<=3 else "") ), G.edges(data = True)))

nx.draw_networkx(G, pos=pos)

#Please note use of edge_labels below.
nx.draw_networkx_edge_labels(G, pos=pos, edge_labels = new_labels)
nx.draw_networkx_edges(G,pos,width=4, edge_color='g', arrows=False)

plt.show()

rawDescription: Problems printing weight in a NetworkX graph


code:
mypprint = MyPrettyPrinter()

mypprint.pprint(x)

rawDescription: Using pprint and skipping the types name


code:
def run(arg1,arg2):
    class_instance = MultiThreadClass(arg1, arg2)
    class_instance.run()

if __name__ == '__main__':
    parser = create_arg_parser()
    args = parser.parse_args()
    run_thread = Thread(target=run, args=(args.arg1, args.arg2))
    run_thread.start()
    time.sleep(args.arg3)

rawDescription: Python: terminate a multithreading program after some time using daemon thread


code: doStuff = lambda w, r, e: w + r + e

rawDescription: pandas appending df1 to df2 get 0s/NaNs in result

description: Pandas appending df1 to df2 get 0s/NaNs in result


rawDescription: Filling a dataframe with high speed

description: Filling a dataframe with high speed


code: res = [x for x in list(set(res)) if x]

rawDescription: find the field names from a search query

description: Find the field names from a search query


rawDescription: Algorithm to extract edge list efficiently from a vector


code: next(islice(iterator, n, n), None)

rawDescription: Pythonic solution to drop N values from an iterator


code:
my_tuple = 1,
# or, with parentheses
my_tuple = (1,)

rawDescription: Python string formatting

description: Python string formatting


rawDescription: Creating a dictionary from a string

description: Creating a dictionary from a string


code: qs_list[0] | qs_list[1] | qs_list[2] | qs_list[3]

rawDescription: Pythonic List Iteration

description: Pythonic List Iteration


code:
win = 10

avg_idx = np.arange(-win, win+1) + indices[:, None]
avg_val = np.tile(values[:, None]/(2*win+1), (1, 2*win+1))

rawDescription: Efficiently take moving average of sparse data and filter above threshold in python


code:
mask = np.random.random_integers(0, 255, (15, 12))
mask_3d = mask[:, :, None] * np.ones(3, dtype=int)[None, None, :]
mask.shape
mask_3d.shape

rawDescription: Copy channels in Numpy array

description: Copy channels in Numpy array


code:
def multiplyBy3(lst):
    newList = [] #create an empty list
    for i in range(len(lst)):
        newList.append(lst[i] * 3)
    return newList

rawDescription: Returning a list in a function

description: Returning a list in a function


code:
app.config['WTF_CSRF_SECRET_KEY'] = 'Insert_random_string_here'
db = SQLAlchemy(app)

rawDescription: Creating flask form with selects from more than one table


code:
import requests
req = requests.get("http://www.mathworks.com/moler/random.pdf")
len(req.content)
req.headers

rawDescription: Python: urlretrieve PDF downloading

description: Python: urlretrieve PDF downloading


code:
puzzle = Puzzle.objects.extra(select={
    'abs_diff': 'ABS(`rating` - %s)',
}, select_params=(rating,)).order_by('abs_diff').first()

rawDescription: Django model query for the closest integer match


code:
a = np.array([np.nan], dtype=object)
b = np.array([float('nan')], dtype=object)
a == b
id(np.nan) == id(float('nan'))

rawDescription: NaNs comparing equal in Numpy

description: NaNs comparing equal in Numpy


code: b = None

rawDescription: Unexpected behavior with list.append

description: Unexpected behavior with list.append


rawDescription: reindex pandas MultiIndex on specific level

description: Reindex pandas MultiIndex on specific level


code: df[df.columns[df.max() > 0]]

rawDescription: Python Pandas drop columns based on max value of column


code:
list(grouper(3, range(9)))
list(grouper(3, range(10)))

rawDescription: Alternative way to split a list into groups of n


code:
s2 = s1.encode('latin1')
s2

rawDescription: python 3, unicode conversion, two \u0000 as one character

description: Python 3, unicode conversion, two \u0000 as one character


code:
from itertools import groupby
from operator import itemgetter

result = [(k, {k: v for _, k, v in g}) for k, g in groupby(t, itemgetter(0))]

rawDescription: how can I convert python tuple to dict

description: Convert python tuple to dict


code:
from collections import Counter
message = "Hello, how are you today?   "
common = Counter(m for m in message if m.isalpha()) \
                .most_common(3)
common

rawDescription: Isolating and filtering results from .most_common() in python


code:
driver.find_element_by_css_selector(".search-results-item a.smallV110")
driver.find_element_by_css_selector("[id^=RECORD] a.smallV110")  # id starts with "RECORD"

rawDescription: Selenium Find Sub-Child href Element

description: Selenium Find Sub-Child href Element


rawDescription: Python: Integer to Base 32 hex (aka. Triacontakaidecimal)


rawDescription: Biopython: How to get the compound name of the pdb file of a protein?


code:
a = [1,2,3]
b = [4,a,5]
c = [6,b,7]

def add_to(input, output):
    for x in input:
        if isinstance(x, list):
            add_to(x, output)
        else:
            output.append(x)

result = []

add_to(c, result) # now result = [6, 4, 1, 2, 3, 5, 7]

rawDescription: iterate dynamically over sub lists and store its values

description: Iterate dynamically over sub lists and store its values


code:
import subprocess
import gdb

def pretty(value, field, otherwise=''):
    try:
        if str(value[field].type) == 'char []':
            data = str(gdb.selected_inferior().read_memory(value[field].address, 64))
            try:
                size = data.index("\0")
                return '\\"%s\\"' % data[0:size].encode('string_escape').replace('"', '\\"').replace("'", "\\'")
            except:
                return '\\"%s\\"..' % data.encode('string_escape').replace('"', '\\"').replace("'", "\\'")
        else:
            return str(value[field])
    except:
        return otherwise

class tee:
    def __init__(self, cmd, filename):
        self.file = open(filename, 'wb')
        gdb.write("Saving DOT to '%s'.\n" % filename)
        self.cmd = cmd
    def __del__(self):
        if self.file is not None:
            self.file.flush()
            self.file.close()
            self.file = None
    def __call__(self, arg):
        self.cmd(arg)
        if self.file is not None:
            self.file.write(arg)

def do_dot(value, output, visited, source, leg, label, left, right):
    if value.type.code != gdb.TYPE_CODE_PTR:
        return
    target = value.dereference()

    target_addr = int(str(target.address), 16)
    if target_addr == 0:
        return

    if target_addr in visited:
        if source is not None:
            path='%s.%s' % (source, target_addr)
            if path not in visited:
                visited.add(path)
                output('\t"%s" -> "%s" [ taillabel="%s" ];\n' % (source, target_addr, leg))
        return

    visited.add(target_addr)

    if source is not None:
        path='%s.%s' % (source, target_addr)
        if path not in visited:
            visited.add(path)
            output('\t"%s" -> "%s" [ taillabel="%s" ];\n' % (source, target_addr, leg))

    if label is None:
        output('\t"%s" [ label="%s" ];\n' % (target_addr, target_addr))
    elif "," in label:
        lab = ''
        for one in label.split(","):
            cur = pretty(target, one, '')
            if len(cur) > 0:
                if len(lab) > 0:
                    lab = '|'.join((lab,cur))
                else:
                    lab = cur
        output('\t"%s" [ shape=record, label="{%s}" ];\n' % (target_addr, lab))
    else:
        output('\t"%s" [ label="%s" ];\n' % (target_addr, pretty(target, label, target_addr)))

    if left is not None:
        try:
            target_left = target[left]
            do_dot(target_left, output, visited, target_addr, left, label, left, right)
        except:
            pass

    if right is not None:
        try:
            target_right = target[right]
            do_dot(target_right, output, visited, target_addr, right, label, left, right)
        except:
            pass

class Tree(gdb.Command):

    def __init__(self):
        super(Tree, self).__init__('tree', gdb.COMMAND_DATA, gdb.COMPLETE_SYMBOL, False)

    def do_invoke(self, name, filename, left, right, label, cmd, arg):
        try:
            node = gdb.selected_frame().read_var(name)
        except:
            gdb.write('No symbol "%s" in current context.\n' % str(name))
            return
        if len(arg) < 1:
            cmdlist = [ cmd ]
        else:
            cmdlist = [ cmd, arg ]
        sub = subprocess.Popen(cmdlist, bufsize=16384, stdin=subprocess.PIPE, stdout=None, stderr=None)
        if filename is None:
            output = sub.stdin.write
        else:
            output = tee(sub.stdin.write, filename)
        output('digraph {\n')
        output('\ttitle = "%s";\n' % name)
        if len(label) < 1: label = None
        if len(left)  < 1: left  = None
        if len(right) < 1: right = None
        visited = set((0,))
        do_dot(node, output, visited, None, None, label, left, right)
        output('}\n')
        sub.communicate()
        sub.wait()

    def help(self):
        gdb.write('Usage: tree [OPTIONS] variable\n')
        gdb.write('Options:\n')
        gdb.write('   left=name          Name member pointing to left child\n')
        gdb.write('   right=name         Name right child pointer\n')
        gdb.write('   label=name[,name]  Define node fields\n')
        gdb.write('   cmd=dot arg=-Tx11  Specify the command (and one option)\n')
        gdb.write('   dot=filename.dot   Save .dot to a file\n')
        gdb.write('Suggestions:\n')
        gdb.write('   tree cmd=neato variable\n')

    def invoke(self, argument, from_tty):
        args = argument.split()
        if len(args) < 1:
            self.help()
            return
        num = 0
        cfg = { 'left':'left', 'right':'right', 'label':'value', 'cmd':'dot', 'arg':'-Tx11', 'dot':None }
        for arg in args[0:]:
            if '=' in arg:
                key, val = arg.split('=', 1)
                cfg[key] = val
            else:
                num += 1
                self.do_invoke(arg, cfg['dot'], cfg['left'], cfg['right'], cfg['label'], cfg['cmd'], cfg['arg'])
        if num < 1:
            self.help()

Tree()

rawDescription: gdb pretty printing with python a recursive structure

description: Gdb pretty printing with python a recursive structure


code: [sorted(x,key=_lis1.index) for x in _list2]

rawDescription: rearranging list based on order of another list in python

description: Rearranging list based on order of another list in python


code: resp = jsonify(data=stories)

rawDescription: PyMongo and Flask's Jsonify contains escape slashes


code:
from collections import OrderedDict
dic = wd[wc][dist][True]
wd[wc][dist][True]= OrderedDict(sorted(dic.items(), key=lambda x: x[1]['Fisher'], reverse=True))

rawDescription: Python: sorting dictionary of dictionaries


code:
class Entry(object):
    __slots__ = ('t', 'a', 'c')  # save some memory
    def __init__(self, t, a, c):
        self.t = t
        self.a = a
        self.c = c
    def __eq__(self, other):
        if not isinstance(other, Entry): return NotImplemented
        return self.t == other.t
    def __hash__(self):
        return id(self.t)
    def __repr__(self):
        return '<{0}({1[0]!r}, {1[1]!r}, {1[2]!r})>'.format(type(self).__name__, self)
    def __getitem__(self, index):
        return getattr(self, 'tac'[index])

rawDescription: Python: how to work with list to have unique values


rawDescription: A better way to write this program

description: A better way to write this program


code:
for p in something:
  for r in something_else:
    print("%s\t" % r, end=" ")
  print()

rawDescription: Printing a simple table in Python

description: Printing a simple table in Python


code:
def etree_to_dict(t):
    d = {t.tag : map(etree_to_dict, t.iterchildren())}
    d.update(('@' + k, v) for k, v in t.attrib.iteritems())
    d['text'] = t.text
    return d

rawDescription: Converting xml to dictionary using ElementTree


rawDescription: Permissions Error: Within a python script, I need to find a server file in unix system and open it to run commands for the user


code:
def invnorm (x):
    return 1.0 / (x + 1)

rawDescription: Normalizing a random unending unknown series?


code:
from itertools import izip, chain
[y for x, y, z in izip(chain((None, None), li),
                       chain((None,), li),
                       li) if x != y != z]

rawDescription: Elegant way to remove contiguous repeated elements in a list?


code: setattr(self, 'clarence', SuperClass())

rawDescription: Instantiate a class to an unknown variable name in python3


code:
import glob
import numpy as np
import pandas as pd

stocks = glob.glob("stock*.csv")

total_tick = 0
for stock in stocks:
    df = pd.read_csv(stock,
                     names=["time", "open", "high", "low", "close", "volume"],
                     parse_dates=[0], index_col="time")
    tick = df["close"].diff().apply(np.sign).fillna(0.0)
    total_tick += tick

total_tick.to_csv("tick.csv")

rawDescription: Most efficient way to loop through multiple csv files and calculate NYSE tick


code:
new_sentence = sentence.format(*(random.choice(l) for l in replacements))
new_sentence
new_sentence = sentence.format(*(random.choice(l) for l in replacements))
new_sentence
new_sentence = sentence.format(*(random.choice(l) for l in replacements))
new_sentence

rawDescription: Python multiple string replacement

description: Python multiple string replacement


rawDescription: My output is exactly what is asking but is still incorrect... Why?


code:
import apt_pkg

apt_pkg.init()
cache = apt_pkg.Cache()

i = 5
for pkg in cache.Packages:
  print(pkg)
  i -= 1
  if i < 0:
    break

rawDescription: How do I programmatically get a list of packages from apt?

description: Programmatically get a list of packages from apt


code: [[None for i in range(n)] for j in range(n)]

rawDescription: 2D Array Unintended Assignment Bug

description: 2D Array Unintended Assignment Bug


code: mod = sys.__modules__[__name__]

rawDescription: Getting a Python Modules Dierctory from Inside Itself


code:
plt.plot(*zip(*testList2))
plt.show()

rawDescription: Python: plot list of tuples

description: Python: plot list of tuples


code: ['king', 'lifetime']

rawDescription: Retrieve text inside #{ }

description: Retrieve text inside #{ }


code:
import numpy as np
import pandas as pd
np.random.seed(2015)
df = pd.DataFrame(np.random.randint(10, size=(10,2))-5, columns=['col A', 'col B'])
condition = df['col A'].abs() >= df['col B'].abs()
df['new col'] = np.where(condition, df['col A'], df['col B'])

rawDescription: Python Pandas: Using an array to choose different columns for each value of a new column


code:
import csv
from itertools import izip_longest
dates = ['2014-07-31', '2013-11-22', '2014-01-01']
closes = ["what's", 'this?']
with open('data.csv', 'w') as f:
    csv.writer(f).writerows(izip_longest(dates, closes))

rawDescription: Two Lists Into One Dictionary Python

description: Two Lists Into One Dictionary Python


code:
from xml.etree import ElementTree as ET
sample = u'''\
<root>
<content type="写真">match</content>
</root>
'''.encode('utf8')
tree = ET.fromstring(sample)
tree.find("content[@type='写真']")
tree.find(u"content[@type='写真']")

rawDescription: Non-ASCII attribute values in ElementTree


code: k = soup.find(text=re.compile("My keywords")).parent.text

rawDescription: how to get the context of a search in BeautifulSoup?

description: Get the context of a search in BeautifulSoup


code:
def repeat_word_count(text, n):
    output = []
    tally = defaultdict(int)
    for i in text.split():
        tally[i] += 1
    for k in tally:
      if tally[k] > n:
          output.append(k)
    return output

text = "one two two three three three four four four four"
repeat_word_count(text, 2)
Out[141]: ['four', 'three']

rawDescription: Python - counting duplicate strings

description: Python - counting duplicate strings


code: sorted(yourdict, key=lambda k: int(k.split()[0]))

rawDescription: Sort Python Dictionary by first four characters in Key


code: {{ sc.name }}

rawDescription: Displaying dictionary value in django template


rawDescription: Verifying a PBKDF2 password hash in python-pbkdf2


code: from package2.module2 import myclass

rawDescription: python unittest import classes sub directories

description: Python unittest import classes sub directories


code:
intersect = set(a).intersection(b)
for i,e in enumerate(a):
    if e in intersect:
        c[i] = newValue

rawDescription: Comparing and editing three lists of different lenght in python?


rawDescription: python list, working with multiple elements

description: Python list, working with multiple elements


code:
class TableView(Frame):
    def __init__(self, parent, columns, rows):
        Frame.__init__(self, parent)
        self.columns = columns
        self.rows = rows
        ...
        self._setup_widgets()
        ...

    def setup_widgets(self):
        self.tree = ttk.Treeview(self, columns=self.columns, show="heading")
        self.tree.grid(column=0, row=0, sticky="nsew")
        ...

rawDescription: Making a class a widget - Tkinter

description: Making a class a widget - Tkinter


code:
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-x', action='store_true')

rawDescription: Framework argparse - check if flag is set


rawDescription: in Numpy, how to zip two 2-D arrays?

description: In Numpy, how to zip two 2-D arrays


code:
def execute_function(function_name, arg1, arg2, arg3):
    return {
        'func1': lambda: func1(arg1),
        'func2': lambda: func2(arg1, arg3),
        'func3': lambda: func3(arg2),
    }[function_name]()

rawDescription: Choose which function to execute, based on a parameter - its name - Python3.3


code:
import re

class RegexDict(dict):

    def get_matching(self, event):
        return (self[key] for key in self if re.match(key, event))

rawDescription: python accessing dictionary with wildcards

description: Python accessing dictionary with wildcards


code:
list(map(type, x)) == list(map(type, y))
[type(i) for i in x] == [type(i) for i in z]

rawDescription: check if two lists are equal by type Python

description: Check if two lists are equal by type Python


code:
rows = ~np.all(temp, axis=1)
rows

rawDescription: Python. Remove from array (presented as list of lists) rows and columns by condition


code:
start = np.random.randint(100, size=(100000,))
stop = start + np.random.randint(1, 10 ,size=start.shape)

rawDescription: Simple Numpy Vectorization

description: Simple Numpy Vectorization


rawDescription: Create different combination / patterns between the data of two columns of a csv file by python


code:
from collections import Counter
d1 = {'a':1, 'b':3}
d2 = {'a':5, 'd':5}
d3 = {'c':2, 'f':1}
Counter(d1) | Counter(d2) | Counter(d3)

rawDescription: Python Concatenate N dictionaries with highest values for each key


code:
from hackernews import HackerNews
hn = HackerNews()
top_story_ids = hn.top_stories()
# >>> top_story_ids
# [8432709, 8432616, 8433237, ...]

rawDescription: How to use Hacker News API in Python?

description: Use Hacker News API in Python


code:
orig_str = "pass"
enc_str = ""
dec_str = ""

enc_table = {'s': '44', 'a': '11', 'p': '41'}
dec_table = dict (zip(enc_table.values(),enc_table.keys()))

print ("Original: ", orig_str)
# Encode
for i in orig_str:
    enc_str += enc_table[i];

print ("Encoded: ", enc_str)

while len(enc_str) > 0:
    sym = enc_str[:2]
    dec_str += dec_table[sym]
    enc_str = enc_str[2:]

print ("Decoded: ", dec_str)

rawDescription: Decryption algorithm for a previously made encryption algorithm - python


code:
Symbol('1+x').replace(
lambda x: S(x.name).is_Symbol is False,
lambda x: S(x.name))

rawDescription: Classifying free symbols from expressions


code:
import re
re.findall('^[A-Z]{3}[a-z][A-Z]{3}$' , 'UUUlUUU')
['UUUlUUU']

rawDescription: REGEX, pythonchallenge.com

description: REGEX, pythonchallenge.com


code:
i = 0
ml = []
for el in b_List:
    if (b_List): # b_list is a list and will evaluate to True
                 # unless you have an empty list, you want if (el)
        i += 1
    ml.append(i) # even if the above line was correct you still get here
                 # on every iteration, and you don't want that
    i = 0

rawDescription: Count the number of occurrences between markers in a python list


rawDescription: little endian bits in python

description: Little endian bits in python


code:
# -*- coding: utf-8 -*-
from tornado.gen import coroutine
from tornado.httpclient import AsyncHTTPClient
from tornado.ioloop import IOLoop

@coroutine
def async_fetch_future():
    http_client = AsyncHTTPClient()
    fetch_result = yield http_client.fetch('http://www.google.com')
    return fetch_result

result = IOLoop.current().run_sync(async_fetch_future)

print(result.body)

rawDescription: Can't call result() on futures in tornado


code: editor = QtGui.QComboBox(parent)

rawDescription: PySide / PyQt QStyledItemDelegate list in table


code:
import xmlrpclib
p = xmlrpclib.Server("http://localhost:8000")
p.appendArray("test1")
p.valueOfArray()
p.appendArray("test2")
p.valueOfArray()

rawDescription: Add item to list in python script on other server


code:
from struct import *

def bitflip(x,pos):
    fs = pack('d',x)
    bval = list(unpack('BBBBBBBB',fs))
    [q,r] = divmod(pos,8)
    bval[q] ^= 1 << r
    fs = pack('BBBBBBBB', *bval)
    fnew=unpack('d',fs)
    return fnew[0]

rawDescription: Python: flip a precise bit in a double

description: Python: flip a precise bit in a double


code:
s = Series(list('aaabbaa')).astype('category',categories=list('ab'))
s.cat.codes

rawDescription: Is it possible to specify the order of levels in Pandas factorize method?


code:
class Data(object):
    __slots__ = ("beta", "calculated", "function")
    def __init__(self, beta=None, calculated=None, function=None):
        self.beta = beta; self.calculated = calculated; self.function = function

    def __repr__(self):
        return "(%s, %s, %s)"  % (self.beta, self.calculated, self.function)

rawDescription: Using namedtuples to consolidate multiple dictionaries


code:
Matplotlib / Pyplot
import numpy as np
import csv
import matplotlib.pyplot as plt

with open('mnist_test_10.csv', 'r') as csv_file:
    for data in csv.reader(csv_file):
        # The first column is the label
        label = data[0]

        # The rest of columns are pixels
        pixels = data[1:]

        # Make those columns into a array of 8-bits pixels
        # This array will be of 1D with length 784
        # The pixel intensity values are integers from 0 to 255
        pixels = np.array(pixels, dtype='uint8')

        # Reshape the array into 28 x 28 array (2-dimensional array)
        pixels = pixels.reshape((28, 28))

        # Plot
        plt.title('Label is {label}'.format(label=label))
        plt.imshow(pixels, cmap='gray')
        plt.show()

        break # This stops the loop, I just want to see one

rawDescription: Visualize MNIST dataset using OpenCV or Matplotlib/Pyplot


code:
# syntax/__init__.py
from .Block import Block   # Relative import with .

rawDescription: Conveniently import several classes from modules in a Python package


code: my_insort_left(data, ('brown', 7))

rawDescription: Python: Using bisect.insort with key

description: Python: Using bisect.insort with key


rawDescription: PYQT Qcombobox set value selected to a variable


code: text/html; charset=utf-8

rawDescription: python html parsing

description: Python html parsing


code: joined = itertools.chain(bs, jn)

rawDescription: lambda - 'QuerySet' object has no attribute 'xxx'

description: Lambda - 'QuerySet' object has no attribute 'xxx'


code:
poolA =  ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
complements = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}

def matching_codons(complements, poolA):
    answer = []
    for i in poolA:
        codon = ''     # inside of for, codon reset in each iteration
        for a in i:
            codon+= complements[a]
        answer.append(codon)  # outside of secondary for, codon have three leters to end iterations
    return answer

matching_codons(complements, poolA)

rawDescription: Converting letters using a dictionary

description: Converting letters using a dictionary


code:
# -*- coding: utf-8 -*-
import sys
from codecs import decode

if __name__ =="__main__":

    if len(sys.argv) == 2:
        my_str = decode(sys.argv[1], 'unicode_escape')
        # alternatively you transform it to a bytes obj and
        # then call decode with:
        # my_str = bytes(sys.argv[1], 'utf-8').decode('unicode_escape')
    else :
        my_str = '\r\nte st'
    print (my_str)

rawDescription: pass special character from the command line

description: Pass special character from the command line


code:
def list_gen(n):
    return range(n)

def list_gen(n):
    return xrange(n)

rawDescription: In Python, When yield cost over the return an list?


code:
# Read in the source data
with open('input.txt', 'r') as handle:
    lines = handle.readlines()
# Open the output file
with open('input.txt', 'w') as handle:
    # Examine each line of the source data
    for line in lines:
        # If it doesn't end with a '0', write it
        if line.strip()[-1] != '0':
            line.split()
            handle.write("[(%s,%s,{%s})]" % tuple(line.split()))

rawDescription: Deleting lines in external text files

description: Deleting lines in external text files


rawDescription: Numpy - automatic implicit conversion of np.arrays into lists


rawDescription: Save list of table of numbers from Python into format easily readable by Mathematica?


code:
bool_indexes = np.logical_and(a > a_70, a < a_80)
indexes = np.nonzero(bool_indexes)

rawDescription: How to get the position in a certain percentile range of 2-d array with np.NAN in it

description: Get the position in a certain percentile range of 2-d array with np.NAN in it


code:
from scipy.interpolate import interpn
import numpy as np

#make up a regular 3d grid
X=np.linspace(-5,5,11)
Y=np.linspace(-5,5,11)
Z=np.linspace(-5,5,11)
xv,yv,zv = np.meshgrid(X,Y,Z)

# make up a function
# see http://docs.scipy.org/doc/numpy/reference/ufuncs.html
A = np.add(xv,np.add(yv,zv))
#this one is easy enough for us to know what to expect at (.5,.5,.5)

# usage : interpn(points, values, xi, method='linear', bounds_error=True, fill_value=nan)
interpn((X,Y,Z),A,[0.5,0.5,0.5])

rawDescription: Interpolating 3d data at a single point in space (Python 2.7)


code:
import re
s = """Potato Prices as of 24-SEP-2014
Idaho
 BrownSpuds
  SomeSpuds 1.90-3.00 mostly 2.00-2.50
  MoreSpuds 2.50-3.50
  LotofSpuds 5.00-6.50

Washington

RedSpuds
TinyReds 1.50-2.00
BigReds 2.00-3.50
BrownSpuds
 SomeSpuds 1.50-2.50
 MoreSpuds 3.00-3.50
 LotofSpuds 5.50-6.50
 BulkSpuds 1.00-2.50

Long Island
SomeSpuds 1.50-2.50 MoreSpuds 2.70-3.75 LotofSpuds 5.00-6.50"""

rawDescription: Python multiline regex search between sections


rawDescription: Apply opencv threshold to a numpy array

description: Apply opencv threshold to a numpy array


code: " ".join(filter(lambda x:x[0]!='#', a.split()))

rawDescription: Python, Remove word start with specific character


code:
reps = rest.count("\n"+rep_line)

new_text = beginning + rest.replace("\n"+rep_line,"\n",reps - 1)
                                                  # ^ replace with a single newline

rawDescription: Python :Replace repetitive line in a file with empty space but not on first / last occurrence


code:
mask = g.groupby('Province').City.transform('count') > 1
mask

rawDescription: pandas: filtering by group size and data value

description: Pandas: filtering by group size and data value


code:
class Number:
    def __new__(cls, value):
        if cls is not __class__:
            # Subclass, create an instance (invokes object.__new__)
            return super().__new__(cls)

        if isinstance(value, complex):
            return Complex(value)
        elif isinstance(value, (int, float)):
            return Real(value)
        else:
            raise TypeError('Ну ты и мудак!!!')

    def __init__(self, value):
        self.value = value

rawDescription: In python 3, how to create child class instance using some magic in parent __new__?


rawDescription: Create an ISODate with pyMongo

description: Create an ISODate with pyMongo


code:
...
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False
        pygame.quit()
        break
if not running:
    break
event_handle(turn)
...

rawDescription: pygame - KEYDOWN event - line not updating position

description: Pygame - KEYDOWN event - line not updating position


rawDescription: Python time-lat-lon array manipulation and grouping


code:
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import RegistrationForm

def registration(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            # ... do something special here if needed
            messages.add_message(request, messages.INFO, 'Registration was successful!')
            return HttpResponseRedirect(reverse('login_page'))
    else:
        form = RegistrationForm()
        return render(request, 'registration.html', {'form': form})

rawDescription: Render without passing request

description: Render without passing request


code:
from operator import itemgetter
f2 = '''\
chr,loc,4678,1298,2295,9936,7354
chr1,849,0,0,0,0,0,
chr1,3481,1,1,0,1,1
chr1,3491,0,2,0,2,0,
'''.splitlines()
reader = csv.reader(f2)
headers = next(reader, [])
matching_indices = [i for i, header in enumerate(headers[2:], 2) if header in ids]
matching_indices
selector = itemgetter(0, 1, *matching_indices)

rawDescription: print columns matching a header value

description: Print columns matching a header value


code:
all_orders = Orders.objects.all()
resp = serializers.serialize('json', all_orders, use_natural_foreign_keys=True)

rawDescription: Retrieve related field detail instead of id


code:
with open("my_matrices.pkl", "rb") as f:
   matrix = pickle.load(f)

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


rawDescription: Make single column of numpy array another datatype


code:
f = gzip.GzipFile(filename)
f.read(10000) # fixed length ascii header
event_dtype = np.dtype([
        ('Id', '>u4'),                # simplified
        ('UnixTimeUTC', '>u4', 2),
        ('Data', '>i2', (1600,1024) )
        ])
s = f.read( event_dtype.itemsize )
event = np.frombuffer(s, dtype=event_dtype, count=1)

rawDescription: efficient numpy.fromfile on zipped files?

description: Efficient numpy.fromfile on zipped files


code:
from django import forms
from .models import Donor, Horse

class MyModelChoiceField(forms.ModelChoiceField):
    def label_from_instance(self, obj):
        return "%s %s %s" % (self.donor_first_name, self.donor_middle_name, self.donor_last_name)

class HorseForm(forms.ModelForm):
    horse_donor = forms.MyModelChoiceField(queryset=Donor.objects.all())
    # other fields (if any), class Meta, etc.

rawDescription: Instead of its id, I want to display Django values() on Foreign Key in template as object


code:
import re

def get_tokens(text, *toknums):
    toknums = set(toknums)
    maxtok = max(toknums)
    for i, m in enumerate(re.finditer(r"\S", text)):
        if i in toknums:
            yield m.group()
        elif i > maxtok:
            break

var1, var2 = get_tokens("a b c d e f g", 2, 5)

rawDescription: Prolog singleton variables in Python

description: Prolog singleton variables in Python


rawDescription: subset and transpose python arrays

description: Subset and transpose python arrays


rawDescription: Appending to lists stored in a nested dictionary


code: lambda length: filter(lambda s: s.isalpha(), b64encode(urandom(length * 2)))[:length]

rawDescription: What is the standard method for generating a nonce in Python?


rawDescription: Building a menu tree from an adjacency list in Python


rawDescription: Python3: Calling functions saved as values (strings) in a dictionary


code:
import csv
import os

receptors = ['crystal_1', 'modeller_1', 'moe_1',
             'nci5_modeller0000_1', 'nci5_modeller0001_1',
             'nci5_modeller0002_1', 'nci5_modeller0003_1',
             'nci5_modeller0004_1', 'nci5_modeller0005_1',
             'nci5_modeller0006_1', 'nci5_modeller0007_1',
             'nci5_modeller0008_1', 'nci5_modeller0009_1',
             'nci5_modeller0010_1', 'nci5_modeller0011_1',
             'nci5_moe0000_1', 'nci5_moe0001_1', 'nci5_moe0002_1',
             'nci5_moe0003_1', 'nci5_moe0004_1', 'nci5_moe0005_1',
             'nci5_moe0006_1', 'nci5_moe0007_1', 'nci5_moe0008_1',
             'nci5_moe0009_1', 'nci5_moe0010_1', 'nci5_moe0011_1',
             'nci5_moe0012_1', 'nci5_moe0013_1', 'nci5_moe0014_1']

with open('potentiation.txt', 'rt') as experiment, \
     open('output.csv', 'wb') as outfile:
    csv_writer = csv.writer(outfile, delimiter='\t')
    csv_writer.writerow(['Ligand'] + receptors)  # header row
    for ligand in (line.rstrip() for line in experiment):
        row = [ligand]
        for protein in receptors:
            with open(protein+'.txt', "rt") as file1:
                found = ['Found', 'Not Found'][file1.read().find(ligand) == -1]
                row.append(found)
        csv_writer.writerow(row)

print('output.csv file written')

rawDescription: How can I write output from a for loop in python into a csv-formatted file?

description: Write output from a for loop in python into a csv-formatted file


code:
def create(self, name, *parameters, **kwargs):
    kwargs.setdefault('opt4',False)  #You will need to do for all such parameters.
    print(parameters)
    print(kwargs['opt4'])

create('C', 'some name', 1, 2, 3, opt4 = True)

rawDescription: How to call python function with optional parameters

description: Call python function with optional parameters


code:
lines = []
f = open(filename)
for line in f:
    if line.strip() and not line.startswith('com'):
        lines.append(line.strip())

rawDescription: Spliting a file into lines in Python using re.split


code:
inputs = []
while True:
    inp = raw_input()
    if inp == "":
        break
    inputs.append(int())

rawDescription: How to take any number of inputs in python without defining any limit?

description: Take any number of inputs in python without defining any limit


code: call(*actionsList)

rawDescription: Use 2 Strings to call Function X from Module Y


code:
pidf = open("./scriptA_pid.tmp","w")
pidf.write(str(os.getpid()))
pidf.close()

rawDescription: Check if second script is running or already finished


code:
def continuation_lines(fin):
    for line in fin:
        line = line.rstrip('\n')
        while line.endswith('\\'):
            line = line[:-1] + next(fin).rstrip('\n')
        yield line

with open("long.txt") as fin:
    for line in continuation_lines(fin):
        ...

rawDescription: Read a file with line continuation characters in Python


code:
class SomeModelProxy(object):
    def __init__(self, model_instance, user):
        self.instance = model_instance
        self.user = user

    def save(self, *args, **kwargs):
        self.instance.save(*args, **kwargs)

    # define other methods that are needed...

    def __getattr__(self, name):
        if not name in get_allowed_columns_for_user_somehow(self.user):
            raise AttributeError

        return getattr(self.instance, name)

    def __setattr__(self, name, value):
        if not name in get_allowed_columns_for_user_somehow(self.user):
            raise AttributeError

        setattr(self.instance, name, value)

rawDescription: Custom Permissions in Django with Problematic Model


code:
for i in nums:
    print(i)

rawDescription: Why is direct indexing of an array significantly faster than iteration?


code:
import re

txt = '''S    7....                        <- line 1
         associated random data   <- line 2
D*EX 0....                        <- line 3
         associated random data   <- line 4
C    0....                        <- line 5
         associated random data   <- line 6
D E  6....                        <- line 7
         associated random data   <- line 8
         associated random data   <- line 9
D    3....                        <- line 10
         associated random data   <- line 11
D O  3....                        <- line 12
         associated random data   <- line 13
         associated random data   <- line 14'''

flags = re.DOTALL | re.MULTILINE |re.VERBOSE

rawDescription: Python Regex matching choice between "\*" (literal Asterisk char) OR "\s" (Space)


code:
from operator import itemgetter, eq
from functools import partial

def take_items(a):
    return takewhile(compose(partial(eq, 1), itemgetter(1)), a)

rawDescription: testing for equality using itemgetter

description: Testing for equality using itemgetter


code:
import io
import pandas as pd
import numpy as np

txt = """             State  FutureState
1947-10-01       0            0
1948-01-01       0            1
1948-04-01       1            1
1948-07-01       1            1"""

df = pd.read_csv(io.BytesIO(txt), delim_whitespace=True)
p = np.array([[ 0.7,  0.3], [ 0.4,  0.6]])
p[df.State, df.FutureState]

rawDescription: Create a vector with values from a Numpy array selected according to criteria in a Pandas DataFrame


rawDescription: How to find index of a given Fibonacci number

description: Find index of a given Fibonacci number


rawDescription: Altering elements in a python array using the for loop


code:
command = ("python(\"ctypes.cast(%s,ctypes.py_object).value.thing=False\")")%(id(self))
nameCommand = cmds.nameCommand( 'setThingOnPress', annotation='', command=command )
cmds.hotkey( keyShortcut='b', name=nameCommand)

rawDescription: How to create an object of an instance from string in Python

description: Create an object of an instance from string in Python


code:
l = [(n,n+1) for n in xrange(0,4)]
l

rawDescription: Store more than 1 value in python array?

description: Store more than 1 value in python array


rawDescription: How to get list of all EBS Snapshots "started" before a certain date?

description: Get list of all EBS Snapshots "started" before a certain date


code: total_img[:] = 255*(min - total_img)/(min - max)

rawDescription: Efficient element-wise multiplication in Python with numpy array


code: Tr1_value = Tr1(T, Tc1)

rawDescription: Saving output of a function as a variable and using it in another function


code:
from tkinter import *
import random

root = Tk()

root.geometry("350x300")
# there are 10 tiles
tiles_letter = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

tiles_make_word = []

namelbl = Label(root, text="Enter words: ")
name = Entry(root)

ok = Button(root, text="Okay")
cancel = Button(root, text="Cancel")

namelbl.place(x=35, y=200)
name.place(x=110, y=200, width=100)

ok.place(x=200, y=100)
cancel.place(x=250, y=100)

global i
i=35

def add_letter():
    global i
    if not tiles_letter:
        return
    rand = random.choice(tiles_letter)
    tiles_make_word.append(rand)
    tile_frame_column = Label(root, text=rand)
    tile_frame_column.place(x=i, y=50)
    tiles_letter.remove(rand)  # remove that tile from list of tiles
    i+=10
    root.after(2000, add_letter)

root.after(2000, add_letter)

rawDescription: tkinter labels in frame using grid

description: Tkinter labels in frame using grid


code: sum(['A' in i for i in parts_df[parts_df['Project Type']=='Type 1']['Parts'].tolist()])

rawDescription: Filtering Pandas dataframe on two criteria where one column is a list


code: ''.join(('\t', indent, '.', str(index)))

rawDescription: Printing a numbered outline of a hierarchical tree


code:
def run():
    list_serial = []
    serial_number = '1320739'
    result = init(serial_number)
    # here the items get set
    list_serial = call_api(serial_number,result)
    # here they get sorted
    serial_sorted = sort_serials(list_serial)
    # list serial is now the sorted list
    list_serial = serial_sorted['sorted_serial_list']
    get_second_element_of_that_list = serial_sorted['get_second_element_of_that_list']

    init(get_second_element_of_that_list)

rawDescription: Python: List is replaced by the new list


code:
from nltk import stem
stemmer = stem.Regexp('s$|es$|era$|erez$|ions$| <etc> ')

rawDescription: Lemmatize French text

description: Lemmatize French text


code:
from collections import Counter
c = Counter(p.nation for p in People)
c
len(c)
sum(c.values())

rawDescription: How to find number of different nations in list of Persons?

description: Find number of different nations in list of Persons


rawDescription: Python: How to find the a unique element pattern from 2 arrays?


code:
def partitions(s):
    if s:
        for i in range(1, len(s) + 1):
            for p in partitions(s[i:]):
                yield [s[:i]] + p
    else:
        yield []

rawDescription: python all combinations of subsets of a string

description: Python all combinations of subsets of a string


code:
import numpy
slice_up(numpy.arange(10))
slice_up(numpy.arange(15))
slice_up(numpy.arange(20))

rawDescription: Creating new lists from values out of lists and arrays


code:
data = u"""
Clnr   Kontonr     Konto                Valuta  Bokföringsdatum  Transaktionsdatum  Referens                            Kontohändelse                   Belopp
12345  1234567890  vardagskonto         SEK     13-09-30         13-09-30           Hyresgästför                        Autogiro                        -15,00
12345  1234567890  vardagskonto         SEK     13-09-30         13-09-30           SPOTIFY SPOTIFY                     Kortköp/uttag                   -19,00
12345  1234567890  vardagskonto         SEK     13-09-30         13-09-30           +46123456789                        Swish mottagen                   80,00
12345  1234567890  vardagskonto         SEK     13-09-30         13-09-30           PRIS NYCKELKUND                     Debiteringsavgift               -49,00
12345  1234567890  vardagskonto         SEK     13-09-27         13-09-27           12345678                            direktbetalning                -301,00
12345  1234567890  vardagskonto         SEK     13-09-27         13-09-27           Unionen                             Bg-bet. via internet           -125,00
12345  1234567890  vardagskonto         SEK     13-09-26         13-09-26           123456789012345                     Överföring                   -1 000,00
""".strip().splitlines()

rawDescription: Parsing an non-uniform but CSV-ish textfile


code:
from django import forms

class TestForm(forms.Form):
    def get_media_js(self):
        return [self.media.absolute_path(path) for path in self.media._js]

rawDescription: Django: accessing raw urls of form media in templates


code: "//a[img[@title='Go to next page']]/@href"

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

description: Lxml: get element with a particular child element


code:
import defaultdict

def Tree(): return defaultdict(tree)

rawDescription: Subclassing dict and nested dicts in it

description: Subclassing dict and nested dicts in it


code: var = 1

rawDescription: Importing same variable from multiple libraries


code: ts.resample("30Min", how=mhl)

rawDescription: Create multiple columns in pandas aggregation function


code:
def sorted_and_merged(tuplelist):
    seen = set()
    seen_add = seen.add
    return [t for t in sorted(tuplelist, reverse=True) if not (t in seen or seen_add(t))]

rawDescription: Python sort and merge with dense list of sublisted lists


code: import wx.lib.iewin as iewin

rawDescription: Wxpython cut copy paste and openfiledialog


code: print("\n".join(str(x) for x in object_list))

rawDescription: Printing all objects from a class

description: Printing all objects from a class


code:
"""
database.py: provides information to make a database connection
"""
login_info = {
    "host":"host_adress",
    "user":"user_name",
    "password":"very_secret_password",
    "database":"database_name",
    "port": 3306
    }

rawDescription: Simplest way to have my python program store/retrieve information from an online database?


code:
import arcpy
wd="Y:/" #have this as your directory where all rasters are located
arcpy.env.workspace = wd
raster_list=arcpy.ListRasters("", "tif")
arcpy.CompositeBands_management(raster_list,"stacked_img.tif") #will save output on the same folder specified above.

rawDescription: Batch processing raster images with Arcpy


rawDescription: Numpy put array in Nth dimension

description: Numpy put array in Nth dimension


code:
urlpatterns = patterns('django.views.generic.simple',
    (r'^$', 'direct_to_template', {'template': 'index.html'}),
)

rawDescription: Django index page best/most common practice


rawDescription: Same observation in multiple columns

description: Same observation in multiple columns


code:
map_dict = {'A':"A'", 'B':'BB', 'C':'CX', 'D':'KK'}
strs = 'ABCDEF'
''.join(map_dict.get(c,c) for c in strs)

rawDescription: Multiple-replace in python

description: Multiple-replace in python


code:
class CustomDict(dict):
    def __getitem__(self, key):
        if key in self:
            return dict.__getitem__(self, key)
        else:
            closest_key = min(self.keys(), key=lambda x: abs(x - key))
            return dict.__getitem__(self, closest_key)

rawDescription: Find integer nearest-neighbour in a dict


code:
import multiprocessing
import numpy as np
import pandas as pd

def tf(x):
    return np.mean(x)

def main():
    pool=multiprocessing.Pool(6)
    sds=pd.Series(np.random.normal(0,.01,1000))
    jobs=[]

    for i in xrange(10):
        jobs.append( pool.apply_async(pd.rolling_apply, (sds,2,tf)) )

    pool.close()
    pool.join()

    for i in xrange(len(jobs)):
        jobs[i].get()

if __name__=="__main__":
    main()

rawDescription: Python multiprocessing TypeError

description: Python multiprocessing TypeError


code:
all([l[i] <= l[i+1] for i in xrange(len(l)-1)])
# False

rawDescription: Why sorted list detection does not work in this situation?


code:
my_color = "blue"
if hasattr(colors, my_color):
    getattr(colors, my_color).paint(my_shape)

rawDescription: Python call a package submodule with variable module name


code:
import re

def remove_commas(mystring):
    return re.sub(r'"(\d+?),(\d+?)"', r'\1\2', mystring)

rawDescription: Cleaning up commas in numbers w/ regular expressions in Python


code:
gb = (df
      .groupby(['Condtion', 'CustID'], as_index=False)['Reading']
      .count()
      .groupby('Condtion')['Reading']
      .apply(lambda group: Counter(group))
)

rawDescription: creating histograms in pandas

description: Creating histograms in pandas


code:
data = '\x03\x2F\x00\x00\x02\x12\x01\x47'

checksum = 0
for ch in data:
    checksum += ord(ch)  # ord('\x03`) -> 3

rawDescription: calculate hex checksum in python

description: Calculate hex checksum in python


code:
# from collections import Counter
from pprint import pprint

table = [['Bacteria', 'Aquificae', 'Persephonella'],
        ['Bacteria', 'Aquificae', 'Thermovibrio'],
        ['Bacteria', 'Firmicutes', 'Bacillus']]

# count_dict = Counter( [ item for row in table for item in row   ] )

count_dict = {}
for row in table:
    for item in row:
        count_dict[item] = count_dict.get(item, 0) + 1

for index_row, row in enumerate(table):
    for index_col, element in enumerate(row):
        if element in count_dict:
            table[index_row][index_col] = '%s %s' % (element, count_dict[element])
            del count_dict[element]
        else:
            table[index_row][index_col] = '-'

pprint(table)

rawDescription: python nested list: replace repeted items in nested list while retaining first occurrence

description: Python nested list: replace repeted items in nested list while retaining first occurrence


code: all_chars = set(chr(i) for i in range(256))

rawDescription: Get all chars not in string python

description: Get all chars not in string python


code:
eatitem = input("> ")
# iterate all items
for item in inv:
    # look for item in 'inv'
    if item['name'] == eatitem:
        # gain item's 'food value'
        hunger = hunger + item["foodvalue"]
        inv.remove(item)
        print("Yum.")
        time.sleep(1)
        # stop the loop to consume a single item instead of all items
        break

rawDescription: Python: using dictionaries in conjunction with input


code:
a=1             # integer
class X: pass
x=X()           # class
y='\u5000'      # Unicode string
z=b'\xa0'       # non-ASCII byte string
' '.join(ascii(i) for i in (a,x,y,z))

rawDescription: How to convert *any* Python object into a string?

description: Convert *any* Python object into a string


code:
from urllib2 import urlopen
import json
def getplace(lat, lon):
    url = "http://maps.googleapis.com/maps/api/geocode/json?"
    url += "latlng=%s,%s&sensor=false" % (lat, lon)
    v = urlopen(url).read()
    j = json.loads(v)
    components = j['results'][0]['address_components']
    country = town = None
    for c in components:
        if "country" in c['types']:
            country = c['long_name']
        if "postal_town" in c['types']:
            town = c['long_name']
    return town, country

print(getplace(51.1, 0.1))
print(getplace(51.2, 0.1))
print(getplace(51.3, 0.1))

rawDescription: how to convert from longitude and latitude to country or city?

description: Convert from longitude and latitude to country or city


code:
dir(object)
dir(dict)

rawDescription: Where do I find Python Classes?

description: Where do I find Python Classes


code:
for ind in range(6):
    ax = subplot(6,1,ind)
    #do all your plotting code once!

rawDescription: Using matplotlib, is it possible to set properties for all subplots on a figure at once?


code:
import csv
from your_app.models import Representative
# If you're using different field names, change this list accordingly.
# The order must also match the column order in the CSV file.
fields = ['parliament', 'name', 'country', 'party_group', 'national_party', 'position']
for row in csv.reader(open('your_file.csv')):
    Representative.objects.create(**dict(zip(fields, row)))

rawDescription: Populating a SQLite3 database from a .txt file with Python


code:
def Test_method_2(self, *args, **kw):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*wrapperargs, **wrapperkw):
            fargs = args + wrapperargs
            fkw = dict(kw)
            fkw.update(wrapperkw)
            return func(*fargs, **fkw)
        return wrapper
    return decorator

rawDescription: Decorating a function with a method (with arbitrary arguments)


code:
from django.db import models
import datetime

# Create your models here.

class CustomDateTimeField(models.Field):

    __metaclass__ = models.SubfieldBase

    def db_type(self, connection):
        return 'datetime'

    def to_python(self, value):
        if not value is None:
            if isinstance(value, datetime.datetime):
                return value.strftime("%Y-%m-%d")
            else:
                return value.rstrip("0")

    def get_db_prep_value(self, value, *args, **kwargs):
        if value is not None:
            return datetime.datetime.strptime(value[0:4] + "-" + value[4:6] + "-" + value[6:], '%Y-%m-%d')
        return None

    def get_prep_lookup(self, lookup_type, value):
        return self.get_db_prep_value(value.rstrip("0"))

class Person(models.Model):
    name = models.CharField(max_length=25)
    my_date = CustomDateTimeField()

rawDescription: Django model query truncate column's value


code:
def composed(*decs):
    def deco(f):
        for dec in reversed(decs):
            f = dec(f)
        return f
    return deco

rawDescription: Can I combine two decorators into a single one in Python?

description: Combine two decorators into a single one in Python


code:
r_ab = np.array([r_a, r_b])
for theta in np.linspace(0, 2 * np.pi, lines):
    pylab.plot(np.cos(theta) * r_ab,
               np.sin(theta) * r_ab, color='red')

rawDescription: Creating a 2d mesh in python

description: Creating a 2d mesh in python


code:
l = dir(__builtins__)
d = __builtins__.__dict__

rawDescription: Is there a function in Python to print all the current properties and values of an object?


code:
def deco(func):
   def kdeco():
      print("before myfunc() called.")
      func()
      print("  after myfunc() called.")
   return kdeco

@deco
def myfunc():
   print(" myfunc() called.")

myfunc

rawDescription: the decorator in python confused me

description: The decorator in python confused me