code:
import logging
import argparse
import subprocess

def initLogging( args ):
    formatString = '[%(levelname)s][%(asctime)s] : %(message)s' # specify a format string
    logLevel = logging.INFO # specify standard log level
    logging.basicConfig( format=formatString , level=logLevel, datefmt='%Y-%m-%d %I:%M:%S')
    log_file = args.output
    fileHandler = logging.FileHandler( log_file )
    logging.root.addHandler( fileHandler ) # add file handler to logging

parser = argparse.ArgumentParser()
parser.add_argument('-u', '--url', help='       Add here the url you want     to use. Example: www.google.com')
parser.add_argument('-o', '--output', help='    Add here the output file for logging')
args = parser.parse_args()
initLogging( args )

cmd = [ "ping" , "-c" ,"4", args.url ]

p = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
stdout_string , stderr_string = p.communicate() # receive stdout, stderr, take care, this is a blocking call,
# stdout_string or stderr_string could be of type None

logging.info( stderr_string )
logging.info( stdout_string )

rawDescription: Python script, log to screen and to file


code:
from __future__ import print_function
import ast

def transform(eq,functions):
    class EqVisitor(ast.NodeVisitor):
        def visit_BinOp(self,node):
            #generate("=>BinOp")
            generate("(")
            self.visit(node.left)
            self.visit(node.op)
            #generate("ici",str(node.op),node._fields,node._attributes)
            #generate(dir(node.op))
            self.visit(node.right)
            generate(")")
            #ast.NodeVisitor.generic_visit(self,node)
        def visit_USub(self,node):
            generate("-")
        def visit_UAdd(self,node):
            generate("+")

        def visit_Sub(self,node):
            generate("-")
        def visit_Add(self,node):
            generate("+")
        def visit_Pow(self,node):
            generate("**")
        def visit_Mult(self,node):
            generate("*")
        def visit_Div(self,node):
            generate("/")
        def visit_Name(self,node):
            generate(node.id)
        def visit_Call(self,node):
            debug("function",node.func.id)
            if node.func.id in functions:
                debug("defined function")
                func_visit(functions[node.func.id],node.args)
                return
            debug("not defined function",node.func.id)
            #generate(node._fields)
            #generate("args")
            generate(node.func.id)
            generate("(")
            sep = ""
            for arg in node.args:
                generate (sep)
                self.visit(arg)
                sep=","
            generate(")")
        def visit_Num(self,node):
            generate(node.n)
        def generic_visit(self, node):

            debug ("\n",type(node).__name__)
            debug (node._fields)
            ast.NodeVisitor.generic_visit(self, node)

    def func_visit(definition,concrete_args):
        class FuncVisitor(EqVisitor):
            def visit_arguments(self,node):
                #generate("visit arguments")
                #generate(node._fields)
                self.arguments={}
                for concrete_arg,formal_arg in zip(concrete_args,node.args):
                    #generate(formal_arg._fields)
                    self.arguments[formal_arg.id]=concrete_arg
                debug(self.arguments)
            def visit_Name(self,node):
                debug("visit Name",node.id)
                if node.id in self.arguments:
                    eqV.visit(self.arguments[node.id])
                else:
                    generate(node.id)

        funcV=FuncVisitor()
        funcV.visit(ast.parse(definition))

    eqV=EqVisitor()
    result = []
    def generate(s):
        #following line maybe usefull for debug
        debug(str(s))
        result.append(str(s))
    eqV.visit(ast.parse(eq,mode="eval"))
    return "".join(result)
def debug(*args,**kwargs):
    #print(*args,**kwargs)
    pass
functions= {
    "f1":"def f1(x,y):return x+y**2",
    "f2":"def f2(x,y):return sin(x+y)",
}
eq="-(a+b)+f1(f2(+x,y),z)*4/365.12-h"
print(transform(eq,functions))

rawDescription: Parsing an equation with custom functions in python


rawDescription: Using URL in Django Views? Possible?

description: Using URL in Django Views? Possible


code:
from functools import partial
heatmap_rgba = partial(cmap.to_rgba, alpha=0.5, bytes=True)
heatmap_color = [map(heatmap_rgba, c) for c in heatmap]

rawDescription: Python: Optimise For loops using list-comprehension


code:
decd=open('jalf_decode_err','r').read()
chardet.detect(decd)
unicode(decd, 'cp1252')
unicode(decd, chardet.detect(decd)['encoding'])

rawDescription: Python mailbox encoding errors

description: Python mailbox encoding errors


code:
nums_and_words = [(1, 'one'), (2, 'two'), (3, 'three')]
nums, words = zip(*nums_and_words)

rawDescription: Python Tuple Unpacking

description: Python Tuple Unpacking


code:
class Blog(models.Model):
    name = models.CharField(max_length=100)
    def save(self, force_insert=False, force_update=False):
        self.name = self.name.upper()
        super(Blog, self).save(force_insert, force_update)

rawDescription: Changing case (upper/lower) on adding data through Django admin site


code: names = [description[0] for description in cursor.description]

rawDescription: Is there a way to get a list of column names in sqlite?


rawDescription: python: make the elements of a list within a certain range

description: Python: make the elements of a list within a certain range


code:
class Positive(object):
    def __init__(self, item):
        self._validate_item(item)
        self.item = item

    @staticmethod
    def _validate_item(item):
        if item <= 0:
            raise ValueError("item should be positive.")

rawDescription: Unit testing a method called during initialization?


rawDescription: convert list to string?

description: Convert list to string


code:
# Convert a large PSD to small PNG thumbnail
myImage = Image(filename="test.psd[0]")
myImage.format = "png"
myImage.sample(100, 100)
myImage.save(filename="test.png")

rawDescription: How to Convert PSD into PNG, using Python Wand / ImageMagick?


code: [s for s in List1 if s.isdigit()]

rawDescription: Hi I was wondering if there was a data type in Python that only allows letters to be inputed and not numbers


rawDescription: Tensorflow Race conditions when chaining multiple queues


code:
for f in files:
    print >> SPSSList, f

rawDescription: User selects file names and writes the file names to a text file [Python]


code:
urlpatterns = patterns('',
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
)

rawDescription: Url conf for query params in django rest framework


code:
apostrophes = dict.fromkeys(
    (0x2013, 0x2018, 0x2019, 0x201b, 0x2035, 0x275b, 0x275c),
    "'")
phrase.translate(apostrophes)

rawDescription: Problems with encoding while parsing website (html) and getting strings


code:
numbers_buf = numbers[:] #  make a copy not a reference

def purify(numbers):
    numbers_buf = numbers[:]
    for item in numbers:
        if item % 2:
            numbers_buf.remove(item)
    return numbers_buf

rawDescription: Python remove element in for loop

description: Python remove element in for loop


code:
from pyparsing import *
import re

data = ... # data goes here

date_regex = re.compile(r'\d\d-\d\d-\d\d')
time_regex = re.compile(r'\d\d:\d\d:\d\d')
pairs = [{'category': 'General Information',
          'kv': Group(Word(alphanums) + Word(alphanums))},
         {'category': 'Last Reset:',
          'kv': Group(Word(alphas, max=1) + Word(alphas)) + Literal(':').suppress()
                + Group(Regex(date_regex) + Regex(time_regex)
                + Optional(SkipTo(LineEnd())))
          }
         ]
# build list of categories with associated parsing rules
categories = [Word("# ").suppress() + x['category']
              + OneOrMore(Group(x['kv']))
              for x in pairs]
# account for thing you don't have specific rules for
categories.append(Word("#").suppress() + Optional(SkipTo(LineEnd())) +
                  Group(OneOrMore(Combine(Word(alphanums) + SkipTo(LineEnd()))))
                  )
# OR all the categories together
categories_ored = categories[0]
for c in categories[1:]:
    categories_ored |= c
configDef = OneOrMore(categories_ored)
suppress_tokens = ["show all", "SSH>", "Active System Configuration"]
suppresses = [Literal(x).suppress() for x in suppress_tokens]
for s in suppresses:
    configDef.ignore(s)

result = configDef.parseString(data)
for e in result:
    print(e)

rawDescription: Parsing config settings in python

description: Parsing config settings in python


code:
for t, b in zip(T, B):
    t.extend(b)

rawDescription: Appending first value of a list into the first value of another list; python


rawDescription: Find the dimensions of a multidimensional Python array


code: lambda x, i=i: x%i==0

rawDescription: lambda function acessing outside variable

description: Lambda function acessing outside variable


code:
class AutoEnum(enum.Enum):
    """
    Automatically numbers enum members starting from 1.

    Includes support for a custom docstring per member.

    """
    __last_number__ = 0

    def __new__(cls, *args):
        """Ignores arguments (will be handled in __init__."""
        value = cls.__last_number__ + 1
        cls.__last_number__ = value
        obj = object.__new__(cls)
        obj._value_ = value
        return obj

    def __init__(self, *args):
        """Can handle 0 or 1 argument; more requires a custom __init__.

        0  = auto-number w/o docstring
        1  = auto-number w/ docstring
        2+ = needs custom __init__

        """
        if len(args) == 1 and isinstance(args[0], (str, unicode)):
            self.__doc__ = args[0]
        elif args:
            raise TypeError('%s not dealt with -- need custom __init__' % (args,))

rawDescription: How do I put docstrings on Enums?

description: Put docstrings on Enums


code:
from pyspark.sql import DataFrame
from pyspark.rdd import RDD

def foo(x):
    if isinstance(x, RDD):
        return "RDD"
    if isinstance(x, DataFrame):
        return "DataFrame"

foo(sc.parallelize([]))
## 'RDD'
foo(sc.parallelize([("foo", 1)]).toDF())
## 'DataFrame'

rawDescription: Check Type: How to check if something is a RDD or a dataframe?


rawDescription: Scale an image in GTK

description: Scale an image in GTK


code:
import Image
import ImageFont
import ImageDraw

img = Image.open("in.jpg")

draw = ImageDraw.Draw(img)
font = ImageFont.truetype(
    "/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-C.ttf", 66)
x, y = (width - 510, height-100)
# x, y = 10, 10
text = "copyright"
w, h = font.getsize(text)
draw.rectangle((x, y, x + w, y + h), fill='black')
draw.text((x, y), text, fill=(209, 239, 8), font=font)
img.save('out.jpg')

rawDescription: A watermark inside a rectangle which filled the certain color


code: rects = plt.bar(range(N), x, align='center')

rawDescription: Dynamically updating a bar plot in matplotlib


code:
isinstance(UiFlags.Checked, IntEnum)
isinstance(UiFlags.Checked, int)

rawDescription: How do I serialize IntEnum from enum34 to json in python?

description: Serialize IntEnum from enum34 to json in python


code:
txtarea = self.documentElement.findFirst('textarea[name="someTextArea"]')
txtarea.evaluateJavaScript('this.value="' + yourText + '";')

rawDescription: How to click text area in Qwebview and python

description: Click text area in Qwebview and python


code:
result = Counter()
for v in docDict.itervalues():
    result.update(set(v))
return result

rawDescription: How to count the documents which contain a specific word?

description: Count the documents which contain a specific word


code:
def index(request):
    return render(request,
                  'shared/layout.html',
                  {'form': LoginForm()})

rawDescription: Django Tastypie Captcha

description: Django Tastypie Captcha


code:
image_rgb = np.zeros([n_pixel_x, n_pixel_y, 3])
# normalize the image to values in [0, 1]
normalized_image = (image - image.min()) / (image.max() - image.min())
image_rgb[:, :, 0] = normalized_image

plt.imshow(image_rgb)

rawDescription: Issues with isolating color channels in Pylab


rawDescription: Using Deque.popleft as args for function


rawDescription: How to get all items from a list in Python 2

description: Get all items from a list in Python 2


code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d

def centeredDistanceMatrix(n):
    # make sure n is odd
    x,y = np.meshgrid(range(n),range(n))
    return np.sqrt((x-(n/2)+1)**2+(y-(n/2)+1)**2)

def function(d):
    return np.log(d) # or any funciton you might have

def arbitraryfunction(d,y,n):
    x = np.arange(n)
    f = interp1d(x, y)
    return f(d.flat).reshape(d.shape)

n = 101
d = centeredDistanceMatrix(n)
y = np.random.randint(0,100,n) # this can be your vector
f = arbitraryfunction(d,y,n)
plt.plot(np.arange(101),arbitraryfunction(np.arange(n),y,n))
plt.show()
plt.imshow(f.T,origin='lower',interpolation='nearest')
plt.show()

rawDescription: Converting 1D radial profil to 2D image

description: Converting 1D radial profil to 2D image


code:
from matplotlib import pyplot as plt

fig, ax = plt.subplots(2, 2, figsize=(8, 8))
ax[0, 0].imshow(img)
ax[0, 0].set_title('Original')
ax[0, 1].imshow(boxcar)
ax[0, 1].set_title('Boxcar filter (width = 50)')
ax[1, 0].imshow(gauss)
ax[1, 0].set_title(r'Gaussian filter ($\sigma$ = 10)')
ax[1, 1].imshow(hanning)
ax[1, 1].set_title(r'Hanning window (width = 50)')

for aa in ax.flat:
    aa.set_axis_off()

fig.tight_layout()
plt.show()

rawDescription: Smoothing a 2D array along only one axis


code:
import peewee as pw
db = pw.SqliteDatabase('/abs/path/to/food/db/food.db')

class BaseModel(pw.Model):
    class Meta:
        database = db

rawDescription: Modularizing peewee

description: Modularizing peewee


code: qCombo.addItem('Sin', 'np.sin')

rawDescription: Simple PyQt Function Evaluator

description: Simple PyQt Function Evaluator


code: from sys import ps1

rawDescription: Python import and module scoping (specifically sys.ps1)


code:
players=int(input("How many players?\n"))
player_names=list()
while len(player_names)<players:
  playname=input("What is player {}'s name?\n".format(len(player_names)+1))
  if playname not in player_names:
    player_names.append(playname)

rawDescription: Doesn't accept the list index?

description: Doesn't accept the list index


code:
class A():
    pass

def newMethod(self):
    self.value = 1

a = A()
newMethod
newMethod.__get__(a, A)
a.newMethod = newMethod.__get__(a, A)
a.newMethod()
a.value

rawDescription: Adding a method to a class after its creation in Python


rawDescription: Nesting lists in for loop in python

description: Nesting lists in for loop in python


code:
import Image
try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO
from django.core.files.base import ContentFile

# other imports and models

class City(models.Model):
    # your fields

    def get_thumbnail(self, thumb_size=None):
        # find a way to choose one of the uploaded images and
        # assign it to `chosen_image`.
        base = Image.open(StringIO(chosen_image.image.read()))  # get the image

        size = thumb_size
        if not thumb_size:
            # set a default thumbnail size if no `thumb_size` is given
            rate = 0.2  # 20% of the original size
            size = base.size
            size = (int(size[0] * rate), int(size[1] * rate))

        base.thumbnail(size)  # make the thumbnail
        thumbnail = StringIO()
        base.save(thumbnail, 'PNG')
        thumbnail = ContentFile(thumbnail.getvalue())  # turn the tumbnail to a "savable" object
        return thumbnail

rawDescription: Uploading Images in Django Admin

description: Uploading Images in Django Admin


code:
from ast import literal_eval
data = "((145541L, u'/.stats/'), (175706L, u'///'))"
result = dict(literal_eval(data))

rawDescription: turn string to array python

description: Turn string to array python


code:
import pandas as pd
d = {'a': [1,2,3,4,3,3,3], 'b': [5,6,7,8,4,4,4], 'c': [9,10,11,12,3,3,3],
     'd': pd.Series(['red', 'blue', 'green', 'red', 'orange', 'blue', 'red'],
                    dtype='category')}
df = pd.DataFrame(d)
dummies = pd.get_dummies(df['d'])
df = pd.concat([df, dummies], axis=1)
df = df.drop(['d', 'green'], axis=1)
print(df)

rawDescription: R's relevel() and factor variables in linear regression in pandas


code:
tt = np.empty(n)
tt[0] = 0.
for i in range(1,n):
    tt[i] = Tm[i] + (tt[i-1] - Tm[i])**(-tau[i])

rawDescription: Is a "for" loop necessary if elements of the a numpy vector are dependant upon the previous element?


code:
import pandas as pd

df = pd.DataFrame({'A':[1,2,3],
                   'B1':[4,5,6],
                   'B2':[7,8,9],
                   'B3':[1,3,5],
                   'B4':[5,3,6],
                   'B5':[7,4,3],
                   'B6':[1,3,7]})

print (df)

df = pd.concat([df]*10000).reset_index(drop=True)

print (pd.concat([df.A, df.filter(like='B').mul(df.A, axis=0)], axis=1))

def loop(df):
    for i in range(0,len(df),1):
         col_B = ["B1","B2","B3","B4","B5","B6",]
         for j in range(len(col_B)):
             df[col_B[j]].iloc[i] = df[col_B[j]].iloc[i]*df.A.iloc[i]
    return df

print (loop(df))

rawDescription: Speed up multi-loop data computation in Pandas


rawDescription: Concatenating Arbitrary number of items of a string in Python


code:
with prefix_stdout('Prefixed: '):
    print('Hello world!')

rawDescription: Change python's print behaviour in a context


code: self.isDisarmed = False

rawDescription: Canceling events after the .after() method has been called on them?


code:
import datetime

# write a parser for the timepoints
def dateparser(string):
    # guessed the dateformat
    return datetime.datetime.strptime(string, '%Y:%m:%d:%H:%M:%S.%f')

# From your code `if line[0] == 'K'` I assume that 'K' is a key for the
# datatypes in the corresponding row.

# For every rowtype you define the datatypes here, where datatype
# is equivalent to a parser. Just make sure it accepts a string and returns the
# type you need.
# I guessed the types here so it works with your example.
parsers = {'K': [str,int,int,int,int,dateparser,float,int,float]}

# the example content
contents = """K; 0; 710; 85; 2; 2013:12:04:13:11:36.291; 0.0000; 1;1009.3000; 0;
K; 0; 710; 85; 3; 2013:12:04:13:11:36.291; 0.0000; 1;1009.3000; 0;
K; 17; 718; 86; 1; 2013:12:04:13:11:36.198; 995.6880; 4; 0.0000; 0; 0.0000; 280; 0.0000; 576; 0.0000; 904;
K; 17; 718; 86; 2; 2013:12:04:13:11:36.198; 0.0000; 4;1484.0000; 0;1484.0000; 280;1484.0000; 576;1481.6000; 904; """

data = []
# the right way for doing this with a file would be:
# with open(filepath, 'r') as f:
#     for line in f:
for line in contents.split('\n'):
    # skip empty lines
    if not line.strip():
        continue

    # first split then strip, feels safer this way...
    fields = [f.strip() for f in line.split(';')]

    # select the parserlist from our dict
    parser_list = parsers[fields[0]]

    # Now match the fields with their parsers, it will automatically stop
    # when there is no parser left. This means if you have 8 parsers only 8
    # fields will be evaluated and the rest is ignored.
    # Comes in handy when the lengths of your row types differ.
    # However it this also goes the other way around. If there
    # are less fields than parsers, the last parsers will be
    # ignored. If you don't want this to happen you have to
    # make sure that len(fields) >= len(parser_list)
    data.append([parser(field) for parser, field in zip(parser_list, fields)])

for row in data:
    print(row)

rawDescription: Assign Different Types to Python List in Advance


code:
from collections import defaultdict
d = defaultdict(list)
for item in names:
    d[item.split()[1][0]].append(item)

df['Names'] = [d[x] for x in df['Letters']]
df

rawDescription: python list comprehension return names based on letter

description: Python list comprehension return names based on letter


code:
try:
    # Python 3
    from urllib.parse import unquote
except ImportError:
    # Python 2
    from urllib import unquote

percent_encoded = '''
%D9%85%D8%A7%D9%81%D8%B8%D8%A9_%D8%A7%D9%84%D8%A8%D8%AF%D8%A7%D8%A6%D8%B9
%D8%A3%D9%88%D8%B1%D9%8A%D9%88%D9%8A%D9%84%D8%A7
Battle_of_Nicopolis
Qingdao
'''
print(unquote(percent_encoded))

rawDescription: python unicode issues in both python2 and python3

description: Python unicode issues in both python2 and python3


code: {{locals()['mylist' + str(i)][0]}}

rawDescription: Passing variable in Python Bottle in Iteration


code:
# equivalent
heads = map(attrgetter('head'), objs)
heads = map(lambda o: o.head, objs)

rawDescription: Why doesn't python have a attrsetter (and what would it take to make one)?


code: color = cmap(float(i)/N)

rawDescription: Pyplot / matplotlib line plot - same color


code:
# Copying your data and reading from the clipboard:
df = pd.read_clipboard()

unique_counts = df.groupby(['col_1', 'col_3'])['col_2'].unique().map(len)
unstacked = unique_counts.unstack(level='col_3').fillna(0)

rawDescription: Pandas: pivot_table unique count returns count


code:
def sse(x0, x, y):
    f = inverter(x, x0)
    diff = y - f
    s = (diff**2).sum()
    return s

rawDescription: python curve_fit doesn't work with stiff model

description: Python curve_fit doesn't work with stiff model


code: {{ url_for('static', filename='[filenameofstaticfile]') }}

rawDescription: Creating link to an url of Flask app in jinja2 template


code: np.array([-0.], dtype='float64').view('V8') == np.array([0.], dtype='float64').view('V8')

rawDescription: Matrix row difference, output a boolean vector


code:
map_init  #[Basemap Instance]
extra_shapes  #[Basemap.readshapefile object]

#plot the weather data for current timestep to current plot
img = map_init.imshow(data[0])

# extra_shapes are county boundaries.  Plot those as polygons
plygn = pyplot.Polygon(map_init.extra_shapes[0])

# Plot the state boundaries (in basemap)
map_init.drawstates()

# add a colorbar
pyplot.colorbar()

for i in range(timestamps):
    img.set_data(data[i])
    plygn.set_xy(map_init.extra_shapes[i])
    pyplot.draw()
    pyplot.savefig(filepath)

rawDescription: Plotting in a loop (with basemap and pyplot)....problems with pyplot.clf()


code:
for substr, freq in counts.most_common():
    if substr.count('-') < 2:
        # skip substrings shorter than 3 elements
        continue

rawDescription: Top of the most frequent substrings in file of sequences


code:
d = {}

for nm, ip, tm, dte in data:
    key = nm, dte
    if key in d:
        v = d[key]
        v[1] += ", {}".format(ip)
        v[2] += ", {}".format(dte)
    else:
        d[key] = [nm, ip, tm, dte]

print(list(d.values()))

rawDescription: Combine multidimensional array by group python


code: client = ssl.wrap_socket(client)

rawDescription: Python HTTPS Proxy Tunnelling

description: Python HTTPS Proxy Tunnelling


code:
import datetime as DT
import parsedatetime as pdt

p = pdt.Calendar()
for text in ('today', 'tomorrow', 'this Sunday', 'Wednesday next week', 'next week Wednesday', ):
    timetuple, flag = p.parse(text)
    date = DT.datetime(*timetuple[:6])
    print('{:20} --> {}'.format(text, date))

rawDescription: python to understand human friendly (plain English) date specification

description: Python to understand human friendly (plain English) date specification


rawDescription: Most efficient way of making an if-elif-elif-else statement when the else is done the most?


rawDescription: Python: Building a dictionary list based on a key and sub keys and totalling up the results based on name, region, region total and grand total


rawDescription: Python idiom to chain (flatten) an infinite iterable of finite iterables?


code:
a.foo is a.foo
b = A('Two')
b.foo is a.foo
b.foo.__func__ is a.foo.__func__

rawDescription: Does Python really create all bound method for every new instance?


code:
boomBangs = lambda xs: ["BOOM!" if x < 10 else "BANG!" for x in xs if odd(x)]

def boomBang(xs):
  return ["BOOM!" if x < 10 else "BANG!" for x in xs if odd(x)]

rawDescription: Partially applied List Comprehension

description: Partially applied List Comprehension


code: #set global $HTTP_PORT="34120"

rawDescription: Python Cheetah - Specify name/value pairs for templating


code:
import os
import csv

open('some_file_name', 'w').write("""user_id,user_name,code
0001,user_a,e-5
0001,user_a,s-N
0002,user_b,e-N
0002,user_b,t-5
""")

list_with_all_codes = ['e', 's', 'n', 't', 'a']

def set_unused_codes(writer, row, tracker):
    for l_code in list_with_all_codes:
        if l_code not in tracker:
            writer.writerow([row['user_id'], row['user_name'], l_code])

with open(os.path.expanduser('out_put_csv_file.csv'), 'w+') as data_file:
    writer = csv.writer(data_file)
    writer.writerow(('user_id', 'user_name', 'code'))
    l_file = csv.DictReader(open('some_file_name'))
    previous_row = None
    tracker = []
    for row in l_file:
        if not previous_row:
            previous_row = row
        if row['user_id'] != previous_row.get('user_id'):
            set_unused_codes(writer, previous_row, tracker)
            previous_row = row
            tracker = []
        tracker.append(row['code'].split('-')[0])
        writer.writerow([row['user_id'], row['user_name'], row['code']])
    set_unused_codes(writer, row, tracker)

print(open('out_put_csv_file.csv').read())

rawDescription: Python iterate over csv row by row while keeping track of content of next row


code:
DATABASES = {
    'app_data': {
        'NAME': 'app_data',
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'USER': 'postgres_user',
        'PASSWORD': 's3krit'
    },
    'DBNAME_1': {
        'NAME': 'user_data',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'mysql_user',
        'PASSWORD': 'priv4te'
    }
}

rawDescription: How to change database in django in runtime

description: Change database in django in runtime


code:
years = mdates.YearLocator()
months = mdates.MonthLocator()
days = mdates.DayLocator()
hours = mdates.HourLocator(12) #if you want ticks every 12 hrs, you can pass 12 to this function
minutes = mdates.MinuteLocator()
daysFmt = mdates.DateFormatter('%m/%d') #or whatever format you want

rawDescription: pandas, matplotlib, use dataframe index as axis tick labels

description: Pandas, matplotlib, use dataframe index as axis tick labels


code:
import subprocess

subprocess.call(['C:\Program Files\Mozilla Firefox\\firefox.exe'])

rawDescription: How to open external programs in Python

description: Open external programs in Python


code: u'some 012 of 345 it 67 is 89 normal text'

rawDescription: Converting Eastern Arabic numbers to Western Arabic numbers


code:
import re
csvfile = open('abc.csv','r')
csv = [line.split(',') for line in csvfile.readlines()]

rawDescription: Python- Check in the CSV file the Header value and data value is equal


rawDescription: Python Networkx detecting loops/circles

description: Python Networkx detecting loops/circles


code:
function_object and function_object(some, arguments)
some_value or produce_new_value(expensive, call)

rawDescription: Python function for boolean "and" operator?


code:
words = {}
for line in f:
    word,year,freq = [i.strip() for i in line.split(',')]
    #create a new list if one does not already exist for this word
    if not words.get(word):
        words[word] = []
    #add this CountByYear object to corresponding list in the dictionary
    words[word].append(CountByYear(word,year,freq))
print(words)

rawDescription: Putting objects into a list into a dictionary


code: l = [[d[key] for key in dicts[0].keys()] for d in dicts]

rawDescription: list of dicts to memory for csv reader

description: List of dicts to memory for csv reader


rawDescription: Remove items from list by using python list comprehensions


code:
from collections import deque
d = deque(a)
[d[0] if d[0] < x and [d.popleft()] else d[0] for x in b ]

rawDescription: Linking two related lists of different lengths in python


rawDescription: Syntax in Python (.T)

description: Syntax in Python (.T)


code:
# get a iterator over the lines in the file:
with open("input.txt", 'rt') as lines:
    # while the line is not empty drop it
    for line in lines:
        if not line.strip():
            break

    # now lines is at the point after the first paragraph
    # so write out everything from here
    with open("output.txt", 'wt') as out:
        out.writelines(lines)

rawDescription: How to delete parts of a file in python?

description: Delete parts of a file in python


code:
def findObject(self, attr, value):
    if getattr(self, attr) == value:
        return self
    else:
        for child in self.children:
            match = child.findObject(attr, value)
            if match:
                return match

rawDescription: Looping through tree hierarchy in python?


code:
a = Foo()
a.bar()
Foo.bar()
Foo.bar(a)

rawDescription: Python Suite, Package, Module, TestCase and TestSuite differences


rawDescription: Numpy: split array randomly

description: Numpy: split array randomly


code:
import unittest

def my_adding_function(x,y):
    return x+y

class TestMyFunction(unittest.TestCase):
    def setUp(self):
        pass

    def test_my_function(self):
        self.assertEqual(my_adding_function(3,4), 7)

if __name__ == '__main__':
    unittest.main()

rawDescription: write unit test case in python for below algorithm

description: Write unit test case in python for below algorithm


rawDescription: Is avoiding expensive __init__ a good reason to use __new__?


code:
app = QApplication(sys.argv)
app.aboutToQuit.connect(myExitHandler) # myExitHandler is a callable

rawDescription: PySide / PyQt detect if user trying to close window


rawDescription: Convert/Reshape 3D Matrix to a 2D Matrix


code:
maX,_ =  max(filtered_lst)
maX

rawDescription: Python find the indexes of a list given a condition


code: shell/open/command

rawDescription: python configuration on windows

description: Python configuration on windows


code:
start = randint(0, len(numbers))
start

rawDescription: Iterate through each value of list in order, starting at random value


code:
def group(xs, idx_start=1):
    xs = np.add.accumulate(xs)
    idxs = np.searchsorted(xs, np.arange(xs[-1]) + 1, side='left').tolist()
    return [list(range(i+idx_start, j+idx_start)) for i, j in zip([0] + idxs, idxs)]

rawDescription: Grouping tuple columns so their sum is less than 1


code:
from collections import OrderedDict

d = OrderedDict()
' '.join([d.fromkeys([set(re.split('\s{2,}',i)).pop() for i in column]).keys()[0] for column in zip(*splitted_lines)])

rawDescription: Python - Merging two strings that overlap


code: baseSet = ThreadedComment.objects.filter(tree_path__contains = ('%010i' % int(baseT.comment_ptr_id)))

rawDescription: How to filter with "contains"?

description: Filter with "contains"


rawDescription: Sorting all rows in numpy matrix by target-column


code:
def one(cls, x):
    return int(x)

def two(cls, y):
    return str(y)

rawDescription: Progammatically turn module/set of functions into a Python class


code:
process = subprocess.Popen(['/bin/bash'], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
process.stdin.write('echo it works!\n')
process.stdout.readline()
process.stdin.write('date\n')
process.stdout.readline()

rawDescription: Interacting with bash from python

description: Interacting with bash from python


code:
def unescapematch(matchobj):
    escapesequence = matchobj.group(0)
    digits = escapesequence[2:]
    ordinal = int(escapesequence, 16)
    char = chr(ordinal)
    return char
re.sub(r'(\\u[0-9A-Fa-f]+)', unescapematch, t)

rawDescription: Easiest way to remove unicode representations from a string in python 3?


code:
for elem in data:
    print('{artist}\n{title}\n{album}\n'.format(**elem['metadata']))

rawDescription: Extract/scrape specific data from JSON file


code: dict((j,i+1) for i,j in enumerate(lowercase))

rawDescription: Efficient way to write this expression: English alphabets dictionary


code:
def filter_item(item):
    item.Bar = item.Bar.title()

rawDescription: Using IronRuby or IronPython to modify a list of C# objects


rawDescription: Python regex look ahead look behind

description: Python regex look ahead look behind


code:
import psycopg2
connection=psycopg2.connect(
    database=config.PGDB,
    host=config.HOST,
    password=config.PASS)
cursor=connection.cursor()
sql='CREATE TABLE foo (location varchar(40))'
cursor.execute(sql)
sql='INSERT INTO foo (location) VALUES (%s)'
cursor.execute(sql,['(goat) 172.10.x.x -> /var/log/messages'])
sql='SELECT * FROM foo'
cursor.execute(sql)
data=cursor.fetchall()
print(data)
# [('(goat) 172.10.x.x -> /var/log/messages',)]

sql='SELECT regexp_replace(location, %s, %s, %s) FROM foo'
cursor.execute(sql,[r'\(|\)','','g'])
data=cursor.fetchall()
print(data)
# [('goat 172.10.x.x -> /var/log/messages',)]

rawDescription: Literal parentheses in postgres query, pgadmin vs. psycopg2


code:
def toggle1(evt):
    if self.boxQuantity.GetValue() == "Enter quantity":
        self.boxQuantity.SetValue("")
    evt.Skip()
def toggle2(evt):
    if self.boxQuantity.GetValue() == "":
        self.boxQuantity.SetValue("Enter quantity")
    evt.Skip()

self.boxQuantity.Bind(wx.EVT_FOCUS,toggle1)
self.boxQuantity.Bind(wx.EVT_KILL_FOCUS,toggle2)

rawDescription: wxPython: How to clear default text in TextCtrl with one click

description: WxPython: How to clear default text in TextCtrl with one click


code:
import re
# this will match 01:23, 11:34, 12:00, etc.
timex = re.compile('.*(\d{2}:\d{2})[ ]?pm NST')
match = timex.match(text, re.M|re.S)
variable = match.groups(0)

rawDescription: Python: getting finding a variable in a string


code:
def handle(self, *args, **options):
    if os.environ.get('RUN_MAIN') and settings.DEBUG:
        call_command('initialize_some_stuff', *args, **options)
    super(Command, self).handle(*args, **options)

rawDescription: Django app initialization process

description: Django app initialization process


rawDescription: Iteration Through tuple of dictionaries in Python


rawDescription: Pandas cumulative sum on column with condition


code:
series_with_latest_occurences = Series.objects.order_by('-occurences__datetime')
series_with_earliest_occurences = Series.objects.order_by('occurences__datetime')

rawDescription: Override save method force me to save twice in the admin thing


code:
import numpy as np

new_matrix = np.fliplr(np.flipud(matrix)) # flip left-right and flip up-down
new_matrix = new_matrix.tolist() # If you want to use a list
                                 # instead a numpy array after the operation

rawDescription: How can I flip around the coordinates inside a multidimensional array in Python?

description: Flip around the coordinates inside a multidimensional array in Python


code: datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + offstr

rawDescription: Time issues in Python

description: Time issues in Python


code:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime as DT
import numpy as np

x = np.array([DT.datetime(2013, 9, i).strftime("%Y-%m-%d") for i in range(1,5)],
            dtype='datetime64')
x = x.astype(DT.datetime)
y = np.array([1,-1,7,-3])
fig, ax = plt.subplots()
ax.plot(x, y)
ax.axhline(linewidth=4, color='r')
xfmt = mdates.DateFormatter('%b %d')
ax.xaxis.set_major_formatter(xfmt)
plt.show()

rawDescription: How do I display dates when plotting in matplotlib.pyplot?

description: Display dates when plotting in matplotlib.pyplot


code: df = df.replace('(Blank)', '_')

rawDescription: Pandas: transform a DataFrame by splitting each value into multiple values


code:
d1 = {u'Orange': [u'OR', u'LI', u'LE'], u'Red': [u'AP', u'ST']}
d2 = {u'Orange': [u'ORANGE', u'LIME', u'LEMON'], u'Red': [u'APPLE', u'STRAWBERRY']}

for k in d1:
    print("Category:", k)
    for abbr, val in zip(d1[k], d2[k]):
        print(abbr, "-", val)
    print()

rawDescription: How to match up two dictionaries in Python

description: Match up two dictionaries in Python


code:
nsmap={'x': 'http://www.mediawiki.org/xml/export-0.10/'}
for title in tree.xpath('//x:title', namespaces=nsmap):
  print (title.text)

rawDescription: Retrieving All Articles Titles from an XML Wiki Dump - Python


code:
h = {'User-Agent':'Mozilla'}
requests.get('http://www.taringa.net/envivo/ajax.php', headers=h)

rawDescription: 403 status code while trying to access webpage from Python


code:
def for_all_methods_in_float(decorator,*exceptions):
    def decorate(cls):
        for attr in float.__dict__:
            if callable(getattr(float, attr)) and not attr in exceptions:
                setattr(cls, attr, decorator(getattr(float, attr),cls))
        return cls
    return decorate

rawDescription: How to set up a class with all the methods of and functions like a built in such as float, but holds onto extra data?

description: Set up a class with all the methods of and functions like a built in such as float, but holds onto extra data


code: sorted(result)

rawDescription: How to pull all the unique combinations from a list of lists

description: Pull all the unique combinations from a list of lists


code:
a.__array_interface__['data']   # (22356720, False)
b.__array_interface__['data']   # (22356720, False)
c.__array_interface__['data']   # (22356720, False)

rawDescription: Numpy Vector (N,1) dimension -> (N,) dimension conversion


code:
from webtest import TestApp
from myapp import my_wsgi_app
app = TestApp(my_wsgi_app)
res = app.get('/')
res.status
res.status_int
params = {'email': 'foo@example.com', 'password': 'foo', }
res = app.post('/login/', params)
res.status_int

rawDescription: Best Way to Run Functional Tests of a WSGI Application?


code:
def cmp_to_symbol(val, other_val):
    '''returns the symbol representing the relationship between two values'''
    return '=><'[(val > other_val) - (val < other_val)]

rawDescription: Python - Why cmp( ) is useful?

description: Python - Why cmp( ) is useful


code:
Builder.load_string('''
<SweetWidget>:
    canvas:
        Color:
            rgba: 1,0,0,1
        Rectangle:
            size: self.size
            pos: self.pos
    size_hint: 1,None
    height: 100
    pos_hint: {'top': 0.8}
    BoxLayout:
        canvas:
            Color:
                rgba: 0,0,1,1
            Rectangle:
                size: self.size
                pos: self.pos
        #pos_hint:
        #    {'top': 1
        #    }
        orientation: 'vertical'
        pos: self.parent.pos
        Button:
            text: "2"
        Button:
            text: "6"
''')

rawDescription: Why adding height to floatLayout will make it start adding following blocks from the bottom of the screen?


code:
from bs4 import BeautifulSoup

soup = BeautifulSoup(open('myNotes.xml','r'))
print(soup.prettify())

rawDescription: How can I Extract Specific xml tags from a local xml file using python?


code:
user_flags = {'is_registered':True, 'has_paid':False, 'has_phone':True}
all(user_flags.values())
user_flags = {'is_registered':True, 'has_paid':True, 'has_phone':True, 'one_more_flag':True}
all(user_flags.values())

rawDescription: Verify state of boolean variables in a dict


code:
def save_heap(heap, toll):
    name = 'heap-{0}.txt'.format(toll)
    with open(name, 'w') as temp:
        for val in heap:
            temp.write(str(val))
            gevent.sleep(0)

rawDescription: How could a distributed queue-like-thing be implemented on top of a RBDMS or NOSQL datastore or other messaging system (e.g., rabbitmq)?


rawDescription: What is the most efficient way to create a dictionary of two pandas Dataframe columns?


code:
def load_save_game(file_name):
    with open(file_name) as f:
        return [json.loads(l) for l in f if l.strip()]

rawDescription: Python json.loads not able to parse json string


code:
import inspect
import functools
def autoargs(*include,**kwargs):
    def _autoargs(func):
        attrs,varargs,varkw,defaults=inspect.getargspec(func)
        def sieve(attr):
            if kwargs and attr in kwargs['exclude']: return False
            if not include or attr in include: return True
            else: return False
        @functools.wraps(func)
        def wrapper(self,*args,**kwargs):
            # handle default values
            for attr,val in zip(reversed(attrs),reversed(defaults)):
                if sieve(attr): setattr(self, attr, val)
            # handle positional arguments
            positional_attrs=attrs[1:]
            for attr,val in zip(positional_attrs,args):
                if sieve(attr): setattr(self, attr, val)
            # handle varargs
            if varargs:
                remaining_args=args[len(positional_attrs):]
                if sieve(varargs): setattr(self, varargs, remaining_args)
            # handle varkw
            if kwargs:
                for attr,val in kwargs.iteritems():
                    if sieve(attr): setattr(self,attr,val)
            return func(self,*args,**kwargs)
        return wrapper
    return _autoargs

rawDescription: What is the best way to do automatic attribute assignment in Python, and is it a good idea?


code:
from pprint import pprint
a = [{'title': 'Learning How to Program', 'views': 1,'url': '/4XvR', 'slug': 'learning-how-to-program'},
     {'title': 'Mastering Programming', 'views': 3,'url': '/7XqR', 'slug': 'mastering-programming'}]
b = [{'title': 'Learning How to Program', 'views': 7,'url': '/4XvR', 'slug': 'learning-how-to-program'},
     {'title': 'Mastering Programming', 'views': 2,'url': '/7XqR', 'slug': 'mastering-programming'},
     {'title': 'Programming Fundamentals', 'views': 1,'url': '/93hB', 'slug': 'programming-fundamentals'}]
per_url = {}
for entry in a + b:
    key = entry['url']
    if key not in per_url:
        per_url[key] = entry.copy()
    else:
        per_url[key]['views'] += entry['views']

per_url
pprint(per_url.values())

rawDescription: Combine two lists of dicts, adding the values together


code: name, score_list = [t[0] for t in tuples], [t[1] for t in tuples]

rawDescription: How to sort a list according to another list? Python

description: Sort a list according to another list? Python


code:
c.most_common(1)
c.most_common(1)[0][0]

rawDescription: Python Caesar cypher using the most common letter to decode a message


code:
def function(l, test, n=0, calls=0):
    if not l and calls == 0: # changed for jython
        return False
    if not l:
        return n == test
    else:
        n += l[0]
        calls += 1
        return function(l[1:],test,n, calls)

rawDescription: How to compare a return value of a recursive function with an integer (actual parameter of function)

description: Compare a return value of a recursive function with an integer (actual parameter of function)


code:
import ROOT

strings = ROOT.vector('string')()

tree = ROOT.TTree('tree', 'tree')
tree.Branch('strings', strings)
strings.push_back('Hello')
strings.push_back('world!')
tree.Fill()
tree.Scan()

rawDescription: Python array of strings in a ROOT TTree

description: Python array of strings in a ROOT TTree


code: foo(*args, **kwargs)

rawDescription: Lisp's apply and funcall vs Python's apply


code:
"""This is the module docstring."""

def f(x):
    """This is the function docstring."""
    return 2 * x

rawDescription: print(__doc__) in Python 3 script

description: Print(__doc__) in Python 3 script


code:
anchors = response.selector.xpath("//table[@class='style1']//ul//li//a")

links = anchors.xpath('@href').extract()
names = anchors.xpath('../text()').extract()

paired_links_with_names = zip(links, names)
...
# you may do your thing here or still do a for / loop

rawDescription: XPath - How to access anchor text and href from the current node in a loop


rawDescription: Pause Python For Loop for a day

description: Pause Python For Loop for a day


code:
data = 'hello'
array = bytearray(data)
array[0]
hex(array[0])
array[0] == 0x68

rawDescription: capturing data from clients as hex using twisted Python

description: Capturing data from clients as hex using twisted Python


code:
def page_detail (request, slug):
    return render_to_response('page/' + slug + '.html', {},
                              context_instance=RequestContext(request))

rawDescription: Extending Django Flatpages to accept template tags


code:
d = collections.defaultdict(list)
for line in stream:
    if '/' in line:
        #.....
        key =  str(hostname)+":"+r.groups()[0][0:2]+r.groups()[2]
        value = str(line[3])
        d[key].append(value)

rawDescription: Python - uniquifying(!) dictionary keys

description: Python - uniquifying(!) dictionary keys


rawDescription: How to make a query on pymongo cursor object [mongo db]

description: Make a query on pymongo cursor object [mongo db]


code: next((i for i, (x,y) in enumerate(zip(G, G[1:])) if x == y), -1)

rawDescription: Python: Index in a list of lists

description: Python: Index in a list of lists


code: [np.where(uind==x)[0] for x in range(u.shape[0])]

rawDescription: Getting the indexes to the duplicate columns of a numpy array


code:
self.groupBox.setStyleSheet("QGroupBox { background-color: rgb(255, 255,\
255); border:1px solid rgb(255, 170, 255); }")

rawDescription: Applying style sheets in pyqt

description: Applying style sheets in pyqt


code:
for idx in range(len(li)):
    li[idx] = 'foo'

rawDescription: Can't modify list elements in a loop Python


code: df.loc[df.A > 0.5,'LAST_TIME_A_ABOVE_X'] = df.loc[df.A > 0.5].index.tolist()

rawDescription: Pandas: Set Value Of Column To Value of Index Based On Condtion


code:
import itertools as it

L = ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc', '10', '%a', ' b']

sorter = lambda x: x[0].lower() if x and x[0].isalpha() else '}'
GL = [list(v) for k, v in it.groupby(sorted(L, key=sorter), key=sorter)]

rawDescription: How to group items in an iterable object based on the first character of the item?

description: Group items in an iterable object based on the first character of the item


code:
from win32com.client import Dispatch
xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Add()
ws = wb.Worksheets.Add()
cell = ws.Cells(1)
cell.Value = 'Some text'
wb.Close(True, r'C:\Path\to\folder\Test.xlsx')

rawDescription: python win32 COM closing excel workbook

description: Python win32 COM closing excel workbook


code:
people = {}
for id, data in persons_database.items():
    people[id] = Person(data[0], data[1], data[2])

rawDescription: Dynamically create instances of a class python


code:
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import numpy as np

fig,ax = plt.subplots(1)

minflux = 0.
maxflux = 10.

for i in range(10):

    flux = float(i)

    normflux = (flux - minflux) / (maxflux - minflux)
    fluxcolor = (1.-normflux, 1.-normflux, normflux)

    ell = Ellipse(xy=(i+1,0.5), width=0.5, height=0.3, angle=90., edgecolor=fluxcolor, lw=3, fc='None')

    ax.add_patch(ell)

ax.set_xlim(0,11)
plt.show()

rawDescription: Edgecolor of Ellipse as a function of a third variable


rawDescription: Rounding numbers - What's wrong here?

description: Rounding numbers - What's wrong here


rawDescription: Python:16 bit one's complement addition implementation


code:
import numpy as np
X1[:, [3,4]] # last two columns

rawDescription: How to normalize only certain columns in scikit-learn?

description: Normalize only certain columns in scikit-learn


code: User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

rawDescription: Writing user.get_profile() custom function which should create auth profile if there is not any


code: ''.join(random.choice(E) for i in xrange(N))

rawDescription: Most Pythonic Way to Generate Random Strings of Fixed Length From Given Characters


rawDescription: Combine rows (under a condition) in a Pandas Dataframe


code:
from pyparsing import *

LBRACE,RBRACE,SEMI,EQ,DASH = map(Suppress,"{};=-")

ident = Word(alphas + '_', alphanums+'_').setName("ident")
guid = Group('GUID' + Combine(Word(hexnums)+('-'+Word(hexnums))*4))
qs = QuotedString('"', escChar="\\", multiline=True)
character_literal = Combine("'" + oneOf(list(printables+' ')) + "'")
value_word = ~DASH + ~LBRACE + ~RBRACE + Word(printables, excludeChars=';').setName("value_word")

value_atom = guid | qs | character_literal | value_word

object_ = Forward()

attr_value = OneOrMore(object_) | Optional(delimitedList(Group(value_atom+OneOrMore(value_atom))|value_atom, ';')) + SEMI
attr_value.setName("attr_value")
attr_defn = Group(DASH + ident("name") + EQ + Group(attr_value)("value"))
attr_defn.setName("attr_defn")

object_ <<= Group(
    LBRACE + ident("type") +
    Group(ZeroOrMore(attr_defn | object_))("attributes") +
    RBRACE
    )

object_.parseString(TEST).pprint()

rawDescription: pyparsing recursion of values list (ibm rhapsody)

description: Pyparsing recursion of values list (ibm rhapsody)


rawDescription: Merge and sum of two dictionaries

description: Merge and sum of two dictionaries


code:
[[n, p1, p2] + parents.get(p1, [0, 0]) + parents.get(p2, [0, 0])
 for n, p1, p2 in group]

rawDescription: interating through a list of lists to create a new list

description: Interating through a list of lists to create a new list


rawDescription: Double loops for "concatenation of subarrays in bigger array"


rawDescription: Filter Pyspark dataframe column with None value


code:
like_with_tokenizer.explain()

## TungstenProject [document_id#2L,keyword#4]
##  SortMergeJoin [token#29], [keyword#4]
##   TungstenSort [token#29 ASC], false, 0
##    TungstenExchange hashpartitioning(token#29)
##     TungstenProject [document_id#2L,token#29]
##      !Generate explode(words#27), true, false, [document_id#2L, ...
##       ConvertToSafe
##        TungstenProject [document_id#2L,UDF(document_text#3) AS words#27]
##         Scan PhysicalRDD[document_id#2L,document_text#3]
##   TungstenSort [keyword#4 ASC], false, 0
##    TungstenExchange hashpartitioning(keyword#4)
##     ConvertToUnsafe
##      Scan PhysicalRDD[keyword#4]

rawDescription: How can we JOIN two Spark SQL dataframes using a SQL-esque "LIKE" criterion?


code: buf = (ctypes.c_char * ctypes.sizeof(header)).from_buffer_copy(header)

rawDescription: python concatenate ctype struct and array

description: Python concatenate ctype struct and array


code: ['njm-kwt@hotmail.com', 'fnan-ksa@hotmail.com', 'k-w-t7@hotmail.com', 'coool-uae@hotmail.com', 'qsd@hotmail.de', 'o1ooo@hotmail.de', 'm-p-3@hotmail.de', 'ya7oo@hotmail.de', 'g5x@hotmail.de', 'f7t@hotmail.de', 'm2y@hotmail.de', 's2udi@hotmail.de', 'q2tar@hotmail.de', 'kuw2it@hotmail.de', 's2udi@hotmail.fr', 'qxx@hotmail.de', 'y-e-s@hotmail.de', 'y-a@hotmail.de', 'qqj@hotmail.de', 'qjj@hotmail.de', 'admin_vb@hotmail.de', 'eng-vb@hotmail.com', 'a3lantk@hotmail.com', 'a3lnkm@hotmail.com', 't7t@hotmail.de', 'mohamed_fathy41@hotmail.com', 'ox-9@hotmail.com', 'ox-9@hotmail.com']

rawDescription: how to Parse emails from mailto urls in Python

description: Parse emails from mailto urls in Python


code:
[int(x) if x else None for x in "1\t2\t3\t\t4".split("\t")]
#>>> [1, 2, 3, None, 4]

rawDescription: Could python split return None while the string has two continuous tab('\t')?


code: sorted(bdict.items(), key=lambda i: float(i[1]), reverse=True)

rawDescription: Casting a dictionary value to decimal or float before sorting


code: waterimg = watertiles[tick % 10]

rawDescription: Update one surface in Pygame

description: Update one surface in Pygame


rawDescription: Readable display of the filename using python


code:
from __future__ import print_function

#iterate through each team
for team, df2 in player_total.groupby(level = 0):
    print(team, end = '\t')
    #drop the 0th level (team) and move the first level (player) as the index
    team_df = df2.reset_index(level = 0, drop = True).reset_index()
    #iterate through each player on the team and print player, trip, and time
    for player in team_df.values.tolist():
        print(': '.join(map(str, player)), end = '; ')
    #After printing all players insert a new line
    print()

rawDescription: Printing all rows in each level of MultiIndex pd.DataFrame in one row


code: "nde*"

rawDescription: Django-Haystack with Solr contains search


code:
class MyMainWindow(QtGui.QMainWindow):

    def __init__(self, owner):
        QtGui.QMainWindow.__init__(self)
        # 'Ui_MyMainWindow' is the class, that was generated by uic,
        # naming convention is: 'Ui_' plus the name of root widget in designer
        self.ui = Ui_MyMainWindow()
        self.ui.setupUi(self)

rawDescription: Using QTDesigner with PyQT and Python 2.6


code: list_of_lists = [list2, [0]*len(list2)]

rawDescription: Swap two lists in python

description: Swap two lists in python


code: Av = kv

rawDescription: Alternative to numpy's linalg.eig?

description: Alternative to numpy's linalg.eig


rawDescription: Exponential not working in Python

description: Exponential not working in Python


rawDescription: Python Argparser show parser from instancemethod


code:
repr(A)
repr(B)

rawDescription: Can I define a __repr__ for a class rather than an instance?

description: Define a __repr__ for a class rather than an instance


code:
#!/usr/bin/env python
from distutils.core import setup

setup(name='fake_package',
      version='1.0.5',
      description='Python modules used by fake-package',
      py_modules=['fake_package.fake_module_1', 'fake_package.fake_module_2'],
      scripts=['fake_package/fake_python_app']
     )

rawDescription: How to build debian package with CPack to execute setup.py?

description: Build debian package with CPack to execute setup.py


code:
def collapseTrajectories(sx, sy, st, trialList, kind='median'):
    # find the longest trial to use as template
    l = 0
    tr = []
    for t in trialList:
        if len(st[st==t]) > l:
            l = len(st[st==t])
            tr = t

    # Make all vectors the same length by interpolating the values
    xnew = np.linspace(0, 640, l)
    ynew = np.linspace(0, 480, l)
    sx_new = []
    sy_new = []

    for t in trialList:
        if len(st[st==t]) > 3:
            X = sx[st==t]
            Y = sy[st==t]
            x = np.linspace(0,640, len(X))
            y = np.linspace(0,480,len(Y))
            fx = interp1d(x, X, kind='cubic')
            fy = interp1d(y, Y, kind='cubic')
            sx_new.append(fx(xnew))
            sy_new.append(fy(ynew))

    # Collapse using the appropriate kind
    if kind == 'median':
        out_x = np.median(sx_new, axis=0)
        out_y = np.median(sy_new, axis=0)
    elif kind=='mean':
        out_x = np.mean(sx_new, axis=0)
        out_y = np.mean(sy_new, axis=0)

    return out_x, out_y

rawDescription: Average multiple vectors of points of different lengths in python


code:
r = requests.get(url, headers=headers, timeout=3, allow_redirects=False)
r.status_code
r.history

rawDescription: Detect URLs changes in python

description: Detect URLs changes in python


code:
from lxml.etree import parse
tree = parse('nodes.xml')
tree.xpath('//Node[@name="node3"]')

rawDescription: How can I get element from XML file with attribute having specific value

description: Get element from XML file with attribute having specific value


code: D = np.sqrt(np.einsum('ij,ji->i', A, B))

rawDescription: Calculate Mahalanobis distance using NumPy only


code:
#!/usr/bin/env python
# read input
rainfall_data = [] # city, rainfall pairs
with open('rainfalls.txt') as file:
    for line in file:
        if line.strip(): # non-blank
            city, rainfall = line.split() # no comments in the input
            rainfall_data.append((city, float(rainfall)))

def mm_to_inches(mm):
    """Convert *mm* to inches."""
    return mm * 0.039370

# write output
with open('rainfallInInches.txt', 'w') as file:
    for city, rainfall_mm in rainfall_data:
        file.write("{city} {rainfall:.2f}\n".format(city=city,
            rainfall=mm_to_inches(rainfall_mm)))

rawDescription: Python File Writing Format Issue

description: Python File Writing Format Issue


code:
import argparse
args = argparse.Namespace()
args.foo = 1
args.bar = [1,2,3]
d = vars(args)
d

rawDescription: what is the right way to treat Python argparse.Namespace() as a dictionary?

description: What is the right way to treat Python argparse.Namespace() as a dictionary


code:
# only once:
risk_free = ql.SimpleQuote()
jibar_curve = ql.FlatForward(0,calendar,ql.QuoteHandle(risk_free),ql.Actual365Fixed(),compounding,compoundingFrequency)
jibarTermStructure.linkTo(jibar_curve)

# pricing:
def price_floater(myqlvalDate,risk_free_rate,credit_spread):
    ql.Settings.instance().evaluationDate = myqlvalDate
    risk_free.linkTo(risk_free_rate)
    spread.setValue(credit_spread)
    return [bond.NPV() ,bond.cleanPrice()]

rawDescription: Daily Pricing of a Bond with QuantLib using Python


code:
import itertools

def mismatch(word, letters, num_mismatches):
    for locs in itertools.combinations(range(len(word)), num_mismatches):
        this_word = [[char] for char in word]
        for loc in locs:
            orig_char = word[loc]
            this_word[loc] = [l for l in letters if l != orig_char]
        for poss in itertools.product(*this_word):
            yield ''.join(poss)

rawDescription: introducing mutations in a DNA string in python

description: Introducing mutations in a DNA string in python


code:
a = np.random.randint(0, 10, (3,3))
b = np.random.randint(0, 10, (3,3))
a
b
a.dot(b)
np.diagonal(a.dot(b))
np.einsum('ij,ji->i', a,b)

rawDescription: (Python) How to get diagonal(A*B) without having to perform A*B?


code:
def allNumAvg(values):
    # get average of all numbers
    return sum(values) / len(values)

def posNumAvg(values):
    # get only positive numbers
    values = [v for v in values if v > 0]
    return sum(values) / len(values)

def nonPosAvg(values):
    # get all negative numbers
    values = [v for v in values if v < 0]
    return sum(values) / len(values)

print("The list of all numbers entered is:")

# pass list of values to each function
dictionary = {
    "AvgPositive": posNumAvg(values),
    "AvgNonPos": nonPosAvg(values),
    "AvgAllNum": allNumAvg(values)
}

rawDescription: Implementing a dictionary function to calculate the average of a list


code:
def sum_prices(orderList, productPrices):
    sum = 0
    for (item, pounds) in orderList:
        if item not in productPrices:
            return 0
        sum += pounds * productPrices[item]
    return sum

rawDescription: How to add a list of tuples (Python)

description: Add a list of tuples (Python)


code:
out == ['universitario de deportes', 'universitario de',
      'lancaster', 'juan aurich', 'juan', 'muni']

rawDescription: Unexpected output in for loop - Python

description: Unexpected output in for loop - Python


code:
dt = iso8601.parse_date("2012-08-25T02:00:00Z")
format_date(dt, "MMMM dd, yyyy", locale='en') + ' at ' + format_time(dt, "HH:mm V")

rawDescription: Convert date to natural language in python?


code:
dic={'0002': ['Frank', 'Castle', 'franstle', 'franny.casty'], '0001': ['Juan', 'Botero', 'jbotero', 'snf23jn4'], '0003': ['Logan', 'James', 'jagan', '87654321']}
login=input('Login: ')
lista=[]
key=''.join([k for k, v in dic.items() if login in v])
if key in dic:
  lista=dic[key]
  print(lista)
else:
  print(None)

rawDescription: How to compare a value from a dictionary

description: Compare a value from a dictionary


code:
import ast
config= ast.parse( file('config.py').read() )

rawDescription: Python text processing and parsing

description: Python text processing and parsing


code:
def __init__(self) :
    self.seq = 0

def getNextSeqNo(self) :
    self.seq += 1
    return '%010d' % self.seq

rawDescription: How to increment a numeric string '0000000001' in python?

description: Increment a numeric string '0000000001' in python


rawDescription: Python Regular Expression to Edit First Comment


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

xdata, ydata, zdata = np.random.random((3, 10))

fig, ax = plt.subplots()
scat = ax.scatter(xdata, ydata, c=zdata, s=200, marker='o')
fig.colorbar(scat)

plt.show()

rawDescription: Colormap in Matplotlib without using the "scatter" function


code:
def func(x): #the function to find a root of
  return x[0] + 1 + x[1]**2

def dfunc(x): #the gradient of that function
  return array([1, 2*x[1]])

def newtRoot(x0, func, dfunc):
  x = array(x0)
  for n in xrange(100): # do at most 100 iterations
    f  = func(x)
    df = dfunc(x)

    if abs(f) < 1e-6: # exit function if we're close enough
      break

    x = x - df*f/norm(df)**2 # update guess
  return x

rawDescription: Best way to find roots of a multidimensional, scalar function with SciPy

description: Find roots of a multidimensional, scalar function with SciPy


code: list_Species_IC = Species_IC.keys()

rawDescription: Filtering a Pythons Pandas DataFrame based on values from dictionary


code:
class LoginForm(ModelForm):
    """ Form used to login a user that does not use social login. """
    class Meta:
        model = User
        only = [u'username', u'password']
        field_args = {
            u'password': {
            'widget': PasswordInput(),
            'label': 'PASSWORD'
            },
            u'username': {'label': 'USERNAME'},
        }
    remember_me = BooleanField(u'Remember me')

rawDescription: How to set form element name?

description: Set form element name


code:
class Customer:
    ...
    region = models.CharField(choices=(('NA', 'NA'), ('Asia', 'Asia'), ))

rawDescription: Django: Get Model Name by a dynamic condition


code:
st = "foo akss saks aks"
t = tuple(arr_invalid)
filt = list(filter(lambda x: not x.startswith(t),st.lower().split()))

rawDescription: Filter using lambda function python

description: Filter using lambda function python


code:
from collections import defaultdict

id_to_scores = defaultdict(list)

for entry in l:
    id_to_scores[entry['id']].append(entry['s'])

output = [{'id': k, 's': max(v)} for k, v in id_to_scores.iteritems()]

rawDescription: python: remove duplicate elements based on score

description: Python: remove duplicate elements based on score


code:
import csv, itertools

with open('names.csv') as csvfile:
    for row in itertools.islice(csv.DictReader(csvfile), 100):
        print(row['first_name'], row['last_name'])

rawDescription: Reading first n lines of a CSV into a dictionary


rawDescription: Count occurences of a row with pandas in python


code: G = price_network(n, gamma=0, directed=False)

rawDescription: Graph-tool surprisingly slow compared to Networkx


code: # BEGIN DEBUG

rawDescription: How to prevent a part of arbitrary code to be compiled in Python?

description: Prevent a part of arbitrary code to be compiled in Python


code: table = 1 - (experiment**2).sum(axis=1).astype(int)

rawDescription: python(numpy) -- create array and how to implement an expression

description: Python(numpy) -- create array and how to implement an expression


code: myarr[(myarr != 2).nonzero()[0]]

rawDescription: Concise way to get NumPy to return array of proper shape after logical indexing, without doing a reshape?


code:
import threading
class YourThreadSubclass(threading.Thread):
    def __init__(self,your_args):
        threading.Thread.__init__(self)
            #do whatever setup you want
    def run(self):
            process_data(self.some_property)

threads = [YourThreadSubclass(args) for args in Iterable]
for t in threads:
    t.start()
for t in threads:
    t.join()
return reduce(combiner, (t.result_field for t in threads))

rawDescription: How do I parallelize I/O-bound operations in Python?

description: Parallelize I/O-bound operations in Python


code:
from elixir import *
class MyTable(Entity):
    using_options(tablename='my_table')
    foo = Field(String, primary_key=True)
    content = Field(JsonType())
    active = Field(Boolean, default=True)

rawDescription: SQLAlchemy JSON as blob/text

description: SQLAlchemy JSON as blob/text


code:
dx = 2. * X / (N - 1)
x = r_[-X:X+dx:dx]
x
x.shape

rawDescription: Getting a N-element vector of values evenly spaced from -X to X


rawDescription: ordering by a field in a foreign key alphabetically

description: Ordering by a field in a foreign key alphabetically


code:
play_again = True
while play_again:
    score = 0
    counter = 0

    #intro and instruction
    print ("""Welcome to the translation quiz""")
    print("""You will be ask a series of questions and have 2 tries per questions
    If you get the correct answer you will get one point, half a point for getting it
    your second try and zero points if you get it wrong twice.
    GOOD LUCK!!! """)

    age = int(input("Please enter your age: "))
    for question, options, correct in question_list:
        if counter == 5 and age <= 5:
            break
        else:
            print(question)
            print(options)
            ans = input("Hit 'a', 'b', 'c' or 'd' for your answer\n")
            if ans.lower() == correct:
                    print ("Correct\n")
                    score += 1
            else:
                    print("Wrong. Try again.\n")
                    ans = input("Hit 'a', 'b', 'c' or 'd' for your answer\n")
                    if ans.lower() == correct:
                        print ("Correct\n")
                        score += 0.5
                    else:
                        print("Wrong. You ran out of attempts\n")

        counter += 1

    print ("Your score was", str(score) + '/' + str(counter))

    response = input("Do you want to play again (y/n)?").strip().lower()
    if response not in ('', 'y', 'yes'):
        play_again = False

rawDescription: wanting to add an age gate to my quiz

description: Wanting to add an age gate to my quiz


code:
@method_decorator(auth_required)
def dispatch(self, *args, **kwargs):
    return super(ICalDownload, self).dispatch(*args, **kwargs)

rawDescription: django-cbv serve password protected static content

description: Django-cbv serve password protected static content


code: result = [x+[y] for x in result for y in pool]

rawDescription: How do I convert Python's itertools.product library from a list comprehension to normal for loops?

description: Convert Python's itertools.product library from a list comprehension to normal for loops


code:
import csv
with open("results.txt", "a") as outfile:
    outcsv = csv.Writer(outfile)
    outcsv.writerow(match.groups())

rawDescription: Regex to find search term and place result in another data file?


code:
class Sophisticated(Individualist):
  def __setattr__(self, n, v):
    if n[:2]=='__' and n[-2:]=='__' and n!='__class__':
      setattr(self.__class__, n, v)
    else:
      object.__setattr__(self, n, v)

c = Sophisticated()
d = Sophisticated()
c.__int__ = lambda self: 54
d.__int__ = lambda self: 88
int(c)
int(d)

rawDescription: Dynamically assign special methods to objects but not classes in Python


code: gtxt = [pat[b[0]:b[1] + 1] for b in bloc]

rawDescription: How to return the regex that matches some text?

description: Return the regex that matches some text


code:
def public_endpoint(function):
    function.is_public = True
    return function

rawDescription: Best way to make Flask-Login's login_required the default

description: Make Flask-Login's login_required the default


code:
doc = """Usage:
   test.py balance= <b1> <b2>
   test.py
"""

from docopt import docopt

options, arguments = docopt(__doc__)  # parse arguments based on docstring above

rawDescription: Multiple command line arguments

description: Multiple command line arguments


code:
np.math.log(1.1, 2)
import math
math.log(1.1, 2) #This will return a float object not Numpy's scalar value

rawDescription: TypeError: return arrays must be of ArrayType for a function that uses only floats


code:
import pandas as pd
data = pd.DataFrame(pd.np.random.rand(100, 50))
data[data > 0.2] = 1
data[data <= 0.2] = pd.np.nan
data.head()

rawDescription: Distance matrix for rows in pandas dataframe


code:
p = Popen(...)
pp = psutil.Process(p.pid)
for child in pp.get_children():
    child.send_signal(signal.SIGINT)

rawDescription: Terminating subprocess in python

description: Terminating subprocess in python


code:
def fast(iterators, sum=sum):
    for i in iterator:
        yield sum(i)

rawDescription: Constructing objects in __init__

description: Constructing objects in __init__


code:
import os

if os.name == 'nt':
    loop = asyncio.ProactorEventLoop() # for subprocess' pipes on Windows
    asyncio.set_event_loop(loop)
else:
    loop = asyncio.get_event_loop()
try:
    rc, *output = loop.run_until_complete(read_and_display("/path/to/script"))
    if rc:
        sys.exit("child failed with '{}' exit code".format(rc))
finally:
    loop.close()

rawDescription: Displaying subprocess output to stdout and redirecting it


code:
from werkzeug.utils import import_string

DEFAULT_SIGNALS = ('MYSIGNAL', 'MYOTHERSIGNAL')

def register_blueprint_and_signals(app, bp_path, signals=DEFAULT_SIGNALS):
    bp = import_string(bp_path)
    app.register_blueprint(bp)

    signal_path = bp_path.rsplit(".", 1)[0] + "."
    for signal in signals:
        try:
            listeners = import_string(signal_path + signal + '_LISTENERS')
        except ImportError:
            continue

        for listener in listeners:
            signal.connect_via(app)(listener)

rawDescription: Catch signals in Flask Blueprint

description: Catch signals in Flask Blueprint


code:
#!/usr/bin/python3

from gi.repository import Poppler, Gtk

def draw(widget, surface):
    page.render(surface)

document = Poppler.Document.new_from_file("file:///home/me/some.pdf", None)
page = document.get_page(0)

window = Gtk.Window(title="Hello World")
window.connect("delete-event", Gtk.main_quit)
window.connect("draw", draw)
window.set_app_paintable(True)

window.show_all()
Gtk.main()

rawDescription: Displaying PDF files with python3

description: Displaying PDF files with python3


code:
all_of_unicode = set(range(0x10ffff))
iso_8859_2 = {ord(chr(i).decode('iso-8859-2')) for i in xrange(0xff)}
iso_8859_5 = {ord(chr(i).decode('iso-8859-5')) for i in xrange(0xff)}
# map the difference to None values
to_remove = dict.fromkeys(all_of_unicode - iso_8859_2 - iso_8859_5)
text_using_only_iso_8859_2_or_5 = unicodetext.translate(to_remove)

rawDescription: In python replacing characters from multiple char maps


code:
import ast
import types

with open("submission1.py") as f:
   p = ast.parse(f.read())

for node in p.body[:]:
    if not isinstance(node, ast.FunctionDef):
        p.body.remove(node)

module = types.ModuleType("mod")
code = compile(p, "mod.py", 'exec')
sys.modules["mod"] = module
exec(code,  module.__dict__)

import mod

mod.fun("calling fun")
mod.fun2("calling fun2")

rawDescription: Import only functions from a python file


code:
import re
s = "tokenA1234tokenB34567"
print(re.findall(r'(?:tokenA|tokenB)([0-9]{4})', s))

rawDescription: python regex: capturing group within OR

description: Python regex: capturing group within OR


code:
from geopy.distance import vincenty

def get_dist(row):
    lat1 = row['EQP_GPS_SPEC_LAT_CORD']
    long1 = row['EQP_GPS_SPEC_LONG_CORD']
    lat2 = row['EQP_GPS_SPEC_LAT_CORD_2']
    long2 = row['EQP_GPS_SPEC_LONG_CORD_2']
    return vincenty((lat1, long1), (lat2, long2)).km

rawDescription: Python: simplifying code by writing it in a more Pandas specific way


code:
from collections import defaultdict

words = defaultdict(list)
for entry in raw:
    key, word = entry.split()
    words[key].append(word)

raw = [' '.join(v) for v in words.values()]

rawDescription: Merge some part of the list together using python


code:
import urlparse
from scrapy.http import Request
from scrapy.spider import BaseSpider
from scrapy.selector import Selector
#from NT.items import NowTorontoItem
from scrapy.item import Item, Field

class NowTorontoItem(Item):
    eventArtist = Field()
    eventTitle = Field()
    eventHolder = Field()
    eventDetails = Field()

    eventLocation = Field()
    eventOrganization = Field()
    eventName = Field()
    eventAddress = Field()
    eventLocality = Field()
    eventPostalCode = Field()
    eventPhone = Field()
    eventURL = Field()

class MySpider(BaseSpider):
    name = "NTSpider"
    allowed_domains = ["nowtoronto.com"]
    start_urls = ["http://www.nowtoronto.com/music/listings/"]

    def parse(self, response):
        selector = Selector(response)
        listings = selector.css("div.listing-item0, div.listing-item1")

        for listing in listings:
            item = NowTorontoItem()
            for body in listing.css('span.listing-body > div.List-Body'):
                item ["eventArtist"] = body.css("span.List-Name::text").extract()
                item ["eventTitle"] = body.css("span.List-Body-Emphasis::text").extract()
                item ["eventHolder"] = body.css("span.List-Body-Strong::text").extract()
                item ["eventDetails"] = body.css("::text").extract()
                #for body in listing.css('div.listing-readmore'):
                #    item ["eventURL"] = body.css("a::attr(href)").extract()

            # instead of returning all items at the end,
            # you can use yield with an item in each iteration
            #yield item

            # and you can also yield a Request()
            # so that scrapy enqueues a new page to fetch
            detail_url = listing.css("div.listing-readmore > a::attr(href)")

            if detail_url:
                yield Request(urlparse.urljoin(response.url,
                                               detail_url.extract()[0]),
                              callback=self.parse_details)

    def parse_details(self, response):
        self.log("parse_details: %r" % response.url)
        selector = Selector(response)
        listings = selector.css("div.whenwhereContent")

        for listing in listings:
            for body in listing.css('td.small-txt.dkgrey-txt.rightInfoTD'):
                item = NowTorontoItem()
                item ["eventLocation"] = body.css("span[property='v:location']::text").extract()
                item ["eventOrganization"] = body.css("span[property='v:organization'] span[property='v:name']::text").extract()
                item ["eventName"] = body.css("span[property='v:name']::text").extract()
                item ["eventAddress"] = body.css("span[property='v:street-address']::text").extract()
                item ["eventLocality"] = body.css("span[property='v:locality']::text").extract()
                item ["eventPostalCode"] = body.css("span[property='v:postal-code']::text").extract()
                item ["eventPhone"] = body.css("span[property='v:tel']::text").extract()
                item ["eventURL"] = body.css("span[property='v:url']::text").extract()
                yield item

rawDescription: Increasing the functionality of my Scrapy spider - Following internal links


code:
ncols = 5
dt = np.dtype([('pre', np.int32), ('data', np.float64, ncols), ('post', np.int32)])

rawDescription: improve speed when reading a binary file

description: Improve speed when reading a binary file


code: xgettext -c --no-location [...]

rawDescription: Python line numbers with xgettext

description: Python line numbers with xgettext


code:
from django.contrib.auth.models import User

class Middleware(object):
    def process_request(self, request):
        request.user = User.objects.filter()[0]

rawDescription: Django Admin without Authentication

description: Django Admin without Authentication


code:
theDate = str(myDatetime.date())
theTime = str(myDatetime.time())

rawDescription: how to get the date from datetime

description: Get the date from datetime


code:
ndarray(shape, dtype=float, buffer=None, offset=0,
        strides=None, order=None)

rawDescription: Compare (assert equality of) two complex data structures containing numpy arrays in unittest


code:
messagePayload = dict(item.split(": ") for item in messagePayload.split("; ")
    if len(item.split(': '))>1
)

rawDescription: Conditionally Convert a String to a Dictionary in Python


code: bool(set(a_nums) & set(b_nums))

rawDescription: comparing numbers in 2 string A and B

description: Comparing numbers in 2 string A and B


code:
from lxml import etree

def myFunc(context, parm):
  em = etree.Element('em')
  em.text = 'text'
  return ['some sample ', em]

rawDescription: How can I return a text fragment from an XPath function?

description: Return a text fragment from an XPath function


code: df

rawDescription: How reading data with pandas.read_csv containing time AM/PM format?


rawDescription: Python socket data returns <byte> object. How to regexp it?


code:
import subprocess

def find_vga():
    vga = subprocess.Popen("lspci -v -s `lspci | awk '/VGA/{print $1}'`", shell=True)
    return vga

print(find_vga())

rawDescription: Python, In linux obtain VGA specifications via lspci or HAL?


code:
def custom_predicate(rel):
    assert is_functional(rel)
    return rel('col1') == Atom('foo') \
           and rel('col5') < Atom(date(2015, 4, 1)) < rel('col6') \
           and (rel('col3') == Atom('baz1') or rel('col3') == Atom('baz2')) \
           and (rel('col4') == Atom('nurf1') or rel('col4') == Atom('nurf2'))
answer = sets.restrict(data_clan, custom_predicate)

rawDescription: Writing complex query using algebraixlib


code: var, var_entry = NewEntry()

rawDescription: creating a new variable using the def command in python

description: Creating a new variable using the def command in python


rawDescription: Python Recursive Search of Dict with Nested Keys


code: [(name, gender) for (id0, gender) in genders for (id1, name) in names if id0==id1]

rawDescription: In python how can I match two lists of tuples by a tuple element?


rawDescription: Autobiographical number in python?

description: Autobiographical number in python


rawDescription: More plotting options for pandas.DataFrame.plot(kind="bar")


code:
import sys
artist = sys.argv[1]
song = sys.argv[2]

rawDescription: How to input command into python script

description: Input command into python script


code:
foos = [imp.load_source(fname.split('.py')[0], FOOS_DIR)
            for fname in os.listdir(FOOS_DIR)
               if re.match(MOD_REGEX, fname)]

rawDescription: Trouble with imp.load_source: 'module' has no attribute 'foo'


code:
remove = dict.fromkeys(map(ord, '\n ' + string.punctuation))
f.translate(remove)

rawDescription: How to strip punctuation from a text file

description: Strip punctuation from a text file


code: some_region = SCREEN # for whole screen

rawDescription: Using Sikuli to take automated screenshots of a window?


code:
# mm.py
registry = {}

class MultiMethod(object):
    def __init__(self, name):
        self.name = name
        self.typemap = {}
    def __call__(self, *args):
        types = tuple(arg.__class__ for arg in args) # a generator expression!
        function = self.typemap.get(types)
        if function is None:
            raise TypeError("no match")
        return function(*args)
    def register(self, types, function):
        if types in self.typemap:
            raise TypeError("duplicate registration")
        self.typemap[types] = function

def multimethod(*types):
    def register(function):
        function = getattr(function, "__lastreg__", function)
        name = function.__name__
        mm = registry.get(name)
        if mm is None:
            mm = registry[name] = MultiMethod(name)
        mm.register(types, function)
        mm.__lastreg__ = function
        return mm
    return register

    if hasattr(function, "__lastreg__"):
        function = function.__lastreg__

rawDescription: Force a function parameter type in Python?


code:
import Queue
import time

work_q = Queue.Queue()     # first create your "work object"
q_lock = threading.Lock()
count = 1

dev = Dev(work_q, q_lock, count)  # after instantiate like this your Thread
dev.setDaemon(True)
dev.start()

time.sleep(1)
with q_lock:
    work_q.put('word')
# word
# 1

time.sleep(1)
count = 10
with q_lock:
    work_q.put('dog')
# dog
# 1

count = 'foo'
with q_lock:
    work_q.put('quit')
# quit
# 1

dev.join()   # This will prevent the main to exit
             # while the dev thread is still running

rawDescription: Threading and information passing -- how to


code:
import gc

mobile_suits = [o for o in gc.get_objects() if isinstance(o, Mobilesuits)]

rawDescription: How do I reference all class instances at the same time?

description: Reference all class instances at the same time


code:
parser.add_option("--file-name", dest="file_name")
parser.add_option("--file-action", dest="file_action")

rawDescription: Python optparse, having one option depends on another


code:
# Set up each connection, must have a way to access each file's name
connect_string = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\access\\month_{}.mdb;"
# Assuming that you'll get the same data from each database
sql = "SELECT column_1, column_2 FROM table"
# Connect to each file
connections = [pyodbc.connect(connect_string.format(n)) for n in range(1, 12 + 1)]
# Create a cursor for each file
cursors = [conn.cursor() for conn in connections]
# Query each file and save the data
data = []
for cur in cursors:
  cur.execute(sql)
  data.extend(cur.fetchall())

rawDescription: Creation pyodbc cursor to multiple databases


code:
a = 'hi'
b = 'ji'
c = 'ki'
d = 'li'
e = 'hi'
letters = [a, b, c, d, e]
len(letters) != len(set(letters))
letters = [a,b,c,d]
len(letters) != len(set(letters))

rawDescription: Python Check if Variable is Duplicated in list


code: uniquevalues = {row.value for row in rows}

rawDescription: Printing the different values in a column not the amount of their repeats


code: x = 'test',

rawDescription: Why do my nested python class instances become tuples?


rawDescription: File name matching - middle of the string


code:
from django.core.exceptions import ValidationError, NON_FIELD_ERRORS

class Order_options(models.Model):
    ...
    def validate_unique(self, exclude = None):
        super(Order_options, self).validate_unique(exclude)

        options = { 'order__id' : self.order.id, 'option__type' : 'self.option.type' }
        objs = Order_options.objects.exclude(id=self.id) if self.id else Order_options.objects
        if objs.filter(**options).exists():
            raise ValidationError({NON_FIELD_ERRORS: ['Error: {0} option type already exists'.format(self.option.type)]})
    ...

rawDescription: Django ManytoMany field duplicates, attribute error: 'ManyRelatedManager'


code:
import sympy as sp

# define a vector of variables
vm = sp.var('m0:3', real = True)

y = 4*sp.log(vm[0]) + 4*sp.log(vm[1]) + 4*sp.log(vm[2]) - 2*sp.log(vm[1] + vm[2]) \
    - 2*sp.log(vm[0] + vm[2]) - 2*sp.log(vm[0] + vm[1]) - 6*sp.log(vm[0] + vm[1] + vm[2])

rawDescription: Set derivatives of a function to 0 in Python


code:
hexlist = ['0x23051fb2', '0xAB', '0xAC']
binlist = [bin(int(x, 16)) for x in hexlist]

rawDescription: Python 3: Programmatically converting list of hex numbers to binary


rawDescription: How to find Term frequency of a particular sets of tags in a document

description: Find Term frequency of a particular sets of tags in a document


code:
def find_majority(k):
    myMap = {}
    maximum = ( '', 0 ) # (occurring element, occurrences)
    for n in k:
        if n in myMap: myMap[n] += 1
        else: myMap[n] = 1

        # Keep track of maximum on the go
        if myMap[n] > maximum[1]: maximum = (n,myMap[n])

    return maximum

rawDescription: Trying to find majority element in a list


code: exec("php script.php  $variable_here > /dev/null &");

rawDescription: Execute PHP Script in background from another php script


rawDescription: Sorting a complex datastructure

description: Sorting a complex datastructure


code:
# This uses functions (and imports) in the previous code example!!
def paw_regions(infile):
    # Read in and stack all data together into a 3D array
    data, time = [], []
    for t, frame in paw_file(infile):
        time.append(t)
        data.append(frame)
    data = np.dstack(data)
    time = np.asarray(time)

    # Find and label the paw impacts
    data_slices, coded_paws = find_paws(data, smooth_radius=4)

    # Sort by time of initial paw impact... This way we can determine which
    # paws are which relative to the first paw with a simple modulo 4.
    # (Assuming a 4-legged dog, where all 4 paws contacted the sensor)
    data_slices.sort(key=lambda dat_slice: dat_slice[2].start)

    # Plot up a simple analysis
    fig = plt.figure()
    ax1 = fig.add_subplot(2,1,1)
    annotate_paw_prints(time, data, data_slices, ax=ax1)
    ax2 = fig.add_subplot(2,1,2)
    plot_paw_impacts(time, data_slices, ax=ax2)
    fig.suptitle(infile)

def plot_paw_impacts(time, data_slices, ax=None):
    if ax is None:
        ax = plt.gca()

    # Group impacts by paw...
    for i, dat_slice in enumerate(data_slices):
        dx, dy, dt = dat_slice
        paw = i%4 + 1
        # Draw a bar over the time interval where each paw is in contact
        ax.barh(bottom=paw, width=time[dt].ptp(), height=0.2,
                left=time[dt].min(), align='center', color='red')
    ax.set_yticks(range(1, 5))
    ax.set_yticklabels(['Paw 1', 'Paw 2', 'Paw 3', 'Paw 4'])
    ax.set_xlabel('Time (ms) Since Beginning of Experiment')
    ax.yaxis.grid(True)
    ax.set_title('Periods of Paw Contact')

def annotate_paw_prints(time, data, data_slices, ax=None):
    if ax is None:
        ax = plt.gca()

    # Display all paw impacts (sum over time)
    ax.imshow(data.sum(axis=2).T)

    # Annotate each impact with which paw it is
    # (Relative to the first paw to hit the sensor)
    x, y = [], []
    for i, region in enumerate(data_slices):
        dx, dy, dz = region
        # Get x,y center of slice...
        x0 = 0.5 * (dx.start + dx.stop)
        y0 = 0.5 * (dy.start + dy.stop)
        x.append(x0); y.append(y0)

        # Annotate the paw impacts
        ax.annotate('Paw %i' % (i%4 +1), (x0, y0),
            color='red', ha='center', va='bottom')

    # Plot line connecting paw impacts
    ax.plot(x,y, '-wo')
    ax.axis('image')
    ax.set_title('Order of Steps')

rawDescription: How can I improve my paw detection?

description: Improve my paw detection


code:
movedmsg = "object moved from = {0} to = {1}"
print(movedmsg.format(object.location, new_location))

rawDescription: python 3: log function signature problem

description: Python 3: log function signature problem


code:
import profile
import dis

def all_in_one():
    def passer():
        pass
    passer()
    for x in range(10000):
        x += x
    print("let's have some tea now")

rawDescription: Performance of inline Python function definitions


code:
import pylab
txt_files = glob.iglob("*.txt")
for txt_filename in txt_files:
    data = pylab.loadtxt(txt_filename)

    t, x, y = data[:,0], data[:,1], data[:,2]

    line, = pylab.plot(x, y, label=time.ctime(t[0]))
    line.track_id = filename[:-4]

rawDescription: limiting the data to the desired values

description: Limiting the data to the desired values


code: g.get_all_shortest_paths(0, 15)

rawDescription: Path between two nodes

description: Path between two nodes


code:
elem = driver.find_element_by_id("downloadBtn")
elem.click()

rawDescription: Download File from website without filename? Python 3.4


code:
from sys import argv

script, first, second, third = argv

rawDescription: Printing individual script arguments repeatedly prints all of them


code:
class MyClass(object):
    def __init__(self):
        self.a = {}
        ...
...

rawDescription: Is embedded python Py_Finalize() blocking?


rawDescription: pandas split string into columns

description: Pandas split string into columns


rawDescription: OpenCV-Python: What is correct shape of points for finding epipolar lines?


rawDescription: PRAW: serializing Comment and Submission objects as JSON


code:
self.images = IT.cycle([filename for filename in os.listdir(IMAGE_DIR) if filename.endswith('.jpg')])
self.image_file = None

rawDescription: wxPython Button to load multiple images

description: WxPython Button to load multiple images


code:
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[::-1], labels[::-1], title='Line', loc='upper left')

rawDescription: Reverse the order of legend

description: Reverse the order of legend


code:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt

data = np.random.normal(size=1000)
hist, bins = np.histogram(data, bins=50)

bin_midpoints = bins[:-1] + np.diff(bins)/2
cdf = np.cumsum(hist)
cdf = cdf / cdf[-1]
values = np.random.rand(10000)
value_bins = np.searchsorted(cdf, values)
random_from_cdf = bin_midpoints[value_bins]

plt.subplot(121)
plt.hist(data, 50)
plt.subplot(122)
plt.hist(random_from_cdf, 50)
plt.show()

rawDescription: Random Number from Histogram

description: Random Number from Histogram


code:
import csv

with open('path/to/input') as infile, open('path/to/input', 'w') as fout:
    outfile = csv.writer(fout)
    for row in csv.reader(infile):
        row = [cell if cell else 'NoData' for cell in row]
        outfile.writerow(row)

rawDescription: Write NoData in every line that has no data (python 3.x)


code:
from string import punctuation,digits

def delUnknownChar(s):
    trans = """!"#$%&()*+,./:;<=>?@[\]^_`{|}~0123456789"""
    return s.translate(None,trans).lstrip("-'").rstrip("-")

rawDescription: Python: Suggestion needed to optimise runtime of filter character method


code: url = dict([(d['scheme'],d['url']) for d in serv])['urn:x-esri:specification:ServiceType:WMS']

rawDescription: Find value matching value in a list of dicts


rawDescription: Get attribute of all items in container python


code: a = a.ravel().view((np.str, a.itemsize*a.shape[1]))

rawDescription: test for membership in a 2d numpy array

description: Test for membership in a 2d numpy array


rawDescription: Decode poweshell output possibly containing non-ascii unicode characters into a python string


code:
def main():
    p=eval(input("Enter in the present value of the account: "))
    i=eval(input("Enter in the monthly interest rate(%): "))
    I=i/100 #simply put this
    t=eval(input("Enter the number of months that that the money will be in the account: "))

    print(futureValue(p, I, t),"Is the future value of your account!")

def futureValue(p, I, t):
    return p*((1 + I) ** t)

main()

rawDescription: Computing future value of an account, when the user enters in the present value, interest rate, and number of years


code:
bins = {}
for event in Event.all().fetch(1000):
    bins.setdefault(event.doe.date(), []).append( event )

rawDescription: Counts of events grouped by date in python?


code:
a = [0, 4, 10, 100]
[x - a[i - 1] for i, x in enumerate(a)][1:]
[x - a[i - 1] for i, x in enumerate(a) if i > 0]
[t - s for s, t in zip(a, a[1:])]

rawDescription: Difference between consecutive elements in list


code:
class BandwidthAction(argparse.Action):

    def __call__(self, parser, namespace, values, option_string=None):
        if values < 12:
            parser.error("Minimum bandwidth for {0} is 12".format(option_string))
            #raise argparse.ArgumentError("Minimum bandwidth is 12")

        setattr(namespace, self.dest, values)

parser.add_argument("-b", "--bandwidth", action=BandwidthAction, type=int,
                     help="target bandwidth >= 12")

rawDescription: python argparse integer condition (>=12)

description: Python argparse integer condition (>=12)


code:
D = (  np.einsum('il,ljk->ijk', MX, A)
     + np.einsum('jl,ilk->ijk', MY, A)
     + np.einsum('kl,ijl->ijk', MZ, A))

rawDescription: triple tensor product in numpy

description: Triple tensor product in numpy


rawDescription: numpy random numpers in specified shape of any complexity

description: Numpy random numpers in specified shape of any complexity


code: sentence = []

rawDescription: How to create paragraphs from markov chain output?

description: Create paragraphs from markov chain output


code: cur.execute("SELECT "+column+" FROM Data where "+goal+"=?", (constrain,))

rawDescription: Python sqlite3 string variable in execute


code:
for mod in map(import_by_name, imported):
  mod.run()

rawDescription: How to import from command line and then iterate over imported modules?

description: Import from command line and then iterate over imported modules


code:
import itertools as it

c = it.cycle(range(6))

for x in range(10):
    print(next(c))

rawDescription: return constantly changing variable python

description: Return constantly changing variable python


code:
def add_divide(a, b):
    a = float(a)
    b = float(b)
    operation = (a+b)/b
    return operation

rawDescription: Django: Use external function with django variables


code:
class Publisher:
    def __init__(self):
        self.sub_list = []

    def subscribe(self, subscriber):
        if subscriber in self.sub_list:
            raise ValueError("Multiple subscriptions are not allowed")
        self.sub_list.append(subscriber)

    def unsubscribe(self, subscriber):
        if subscriber not in self.sub_list:
            raise ValueError("Can only unsubscribe subscribers")
        self.sub_list.remove(subscriber)

    def publish(self, s):
        for subscriber in self.sub_list:
            subscriber(s)

class Subscriber:
    def __init__(self, name, publisher):
        self.publisher = publisher
        publisher.subscribe(self.process)
        self.name = name
        self.calls = 0

    def process(self, s):
        print("  call {}".format(self.name))   # debug on call
        self.calls += 1
        if self.calls <= 3:
            print("{} call #{}: {}".format(self.name, self.calls, s.upper()))
        else:
            print("  remove {}".format(self.name))    # debug on remove
            self.publisher.unsubscribe(self.process)

if __name__ == '__main__':
    pub = Publisher()
    for i in range(6):
        Subscriber("Sub"+str(i), pub)
        pub.publish("test" + str(i))
        print()

rawDescription: Python Pub/Sub -- Unsubscribe Issue

description: Python Pub/Sub -- Unsubscribe Issue


code: sorted([4, 2, 5, 3, 8], cmp=lambda a, b: a > b)

rawDescription: Why is my list not sorted as expected?

description: Why is my list not sorted as expected


code:
url = 'http://httpbin.org/post'
files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=files)

rawDescription: How to post an image in Python just like byte array in Java?

description: Post an image in Python just like byte array in Java


code:
df = pd.DataFrame(np.random.randn(8,3),columns=['A','B','C'])
df2 = df.copy()
df2.ix[0:3,['B','C']] = np.nan
df2
df

rawDescription: day is out of range for month python

description: Day is out of range for month python


rawDescription: Streamflow plot not matching mgrid

description: Streamflow plot not matching mgrid


code:
characters = ['A', 'B', 'C', 'D', 'E']
relations = list(itertools.combinations(characters, 2))

rawDescription: Python fastest method to group pairs from a list of items


code:
import classdef1, classdef2, classdef3

class Bigclass(classdef1.Bigclass, classdef2.Bigclass, classdef3.Bigclass):
    num = 42   # add more members if desired

rawDescription: Is there any Python equivalent to partial classes?


rawDescription: Generate list of numbers in specific format


code:
def generate_response(serialize):
    file = StreamWriter()
    def serialize_task():
        serialize(file)
        file.close()
    threading.Thread(target=serialize_task).start()
    while True:
        chunk = file.read()
        if chunk is None:
            break
        yield chunk

rawDescription: Flask: Streaming data by writing to client?


code:
class RGB(object):
    def __init__(self, red, blue, green):
        super(RGB, self).__init__()
        self._red = red
        self._blue = blue
        self._green = green

    def _color(self, type):
        return getattr(self, type)

    red = functools.partialmethod(_color, type='_red')
    blue = functools.partialmethod(_color, type='_blue')
    green = functools.partialmethod(_color, type='_green')

rawDescription: functools.partial on class method

description: Functools.partial on class method


code:
def load_big_file(fname):
    '''only works for well-formed text file of space-separated doubles'''

    rows = []  # unknown number of lines, so use list
    with open(fname) as f:
        for line in f:
            line = [float(s) for s in line.split()]
            rows.append(np.array(line, dtype = np.double))
    return np.vstack(rows)  # convert list of vectors to array

rawDescription: Fastest way to load huge .dat into array


code: alltests = unittest.TestSuite([fast, slow])

rawDescription: Python unittest: how to run only part of a test file?


code:
expected_rows = []
for row in rows:
    expected_rows.append(get_expected_rows(**row))

rawDescription: Python unpacking list value in kwargs

description: Python unpacking list value in kwargs


rawDescription: How can a class that inherits from list and uses keyword arguments be made to work in both Python 2 and Python 3?


code: ax.plot(np.linspace(0, 2*np.pi, 100), np.ones(100)*5, color='r', linestyle='-')

rawDescription: matplotlib: how to plot concentric circles at a given set of radii

description: Matplotlib: how to plot concentric circles at a given set of radii


code:
filepath = r"path:\To\Excel\Workbook"
excelInstance.Workbooks.Open(filepath)

rawDescription: How Do I Use Excel's Format Painter Across a Whole Workbook


code:
import sys
this_module = sys.modules[__name__]

rawDescription: Go through a number of functions in Python


code: myindex(array, "a")

rawDescription: Find index of nested item in python

description: Find index of nested item in python


rawDescription: What is the quickest way to HTTP GET in Python?


rawDescription: Mask an array with conditions on multiple columns


code:
from itertools import izip

for x, y in izip(lis_A, vals):
    x.append(y)

rawDescription: Append to several lists inside list

description: Append to several lists inside list


code:
import re
s="6d) We took no [pains] to hide it ."
matches = re.findall('\[(.*?)\]', s)

rawDescription: Python - remove parts of a string

description: Python - remove parts of a string


code:
queue = []
queue.append((1,2))  # FIFO in
queue.append((2,3))  # FIFO in
queue.pop(0)  # Oldest element out
queue.append((3,5))
queue.pop(0)

rawDescription: Python FIFO 2 Dimensional

description: Python FIFO 2 Dimensional


code:
from itertools import cycle

i = 0
n = 10
l = []
step = cycle([1, -2, 3])

for _ in range(n):
    i += next(step)
    l.append(i)

print(l)

rawDescription: Create a list that follows a certain creation pattern