code:
names = ['rock', 'paper', 'scissors']
print("Computer chooses {}, you loose".format(names[cpu_choice]))

rawDescription: rock, paper, scissor-python-code. help simplify

description: Rock, paper, scissor-python-code. help simplify


code:
from collections import Mapping

class FrozenDict(Mapping):
    """Immutable dictionary.

    Abstract methods required by Mapping are
    1. `__getitem__`
    2. `__iter__`
    3. `__len__`
    """

    def __init__(self, *args, **kwargs):
        self._data = dict(*args, **kwargs)

    def __getitem__(self, key):
        return self._data[key]

    def __iter__(self):
        return iter(self._data)

    def __len__(self):
        return len(self._data)

rawDescription: How to define self-made object that can be unpacked by `**`?

description: Define self-made object that can be unpacked by `**`


rawDescription: Python Pandas: Inserting new rows for date gaps in data


code: myfunc(whatever)

rawDescription: How to understand python decorator arguments pass

description: Understand python decorator arguments pass


code:
for a in list_a:
   if a not in list_b:
       writeFile(...)

rawDescription: Comparing large list of hashes to other large list of hashes


code:
import fileinput
for line in fileinput.input (filenameToProcess, inplace=1):
    process (line)

rawDescription: Open file, read it, process, and write back - shortest method in Python


code:
engine: TEST
result: 42

rawDescription: Boost Python , python functions and ptrs


rawDescription: How can I calculate the average of a list of tuples in python?

description: Calculate the average of a list of tuples in python


code:
idx = pd.IndexSlice
df.loc[:,idx[:,:,[2,3]]]

rawDescription: Advanced cross-section with multi-index in pandas


code:
myvar = [1, 2, '3']
",".join(map(str, myvar))

rawDescription: Remove leading and trailing spaces from a Python list


rawDescription: Python : How do i get a new column for every file I read?


rawDescription: Numpy: get 1D array as 2D array without reshape


code: return permNums(newInp, N)

rawDescription: How to write a lot of nested 'for' loops (Recursively)

description: Write a lot of nested 'for' loops (Recursively)


code:
from run1 import *
from run2 import *

rawDescription: Execute python files through code

description: Execute python files through code


code: result = myfunction(myarray)

rawDescription: Concatenate numpy arrays inside a function and return it?


rawDescription: Apostrophe within Python lookbehind assertion


rawDescription: Python: include a third party library in a personal Python package


code:
import numpy as np

a = np.random.rand(6,4)
b = ['test']*6

c = np.column_stack([a,b])
np.savetxt('/tmp/out', c, delimiter=',', fmt='%s')

rawDescription: How to combine a numpy array and a text column and export to csv

description: Combine a numpy array and a text column and export to csv


rawDescription: Git pre-commit hook not working on windows


code:
s = "asdf"
hash(s)
s = "xxx"
hash(s)
hash("xxx")

rawDescription: Is there a significant overhead by using different versions of sha hashing (hashlib module)


code:
replacements = {ord(k): ord(v) for k, v in replacements.iteritems()}
replacements
u'\u2019Hello world! \u2014 You rock!\u2018'.translate(replacements)

rawDescription: How to use str.replace() with a dictionary of replacements? Python

description: Use str.replace() with a dictionary of replacements? Python


code:
def do_regression(df_group, ret='outcome'):
    """Apply the function to each group in the data and return one result."""
    y,X = dmatrices('value1 ~ value2 + value3',
                    data=df_group,
                    return_type='dataframe')
    if ret == 'outcome':
        return y
    else:
        return X

outcome = df.groupby(pd.TimeGrouper("M")).apply(do_regression, ret='outcome')

rawDescription: Pandas - Split dataframe into multiple dataframes based on dates?


rawDescription: subset-by-subset list comparison in python

description: Subset-by-subset list comparison in python


code: lambda x, a: x.dot(a) - 10

rawDescription: Scipy, optimize a function with argument dependent constraints


rawDescription: Using a pattern for making a list

description: Using a pattern for making a list


code:
from sqlalchemy.orm.interfaces import AttributeExtension, InstrumentationManager
from sqlalchemy.orm import ColumnProperty

class InstallValidatorListeners(InstrumentationManager):
    def post_configure_attribute(self, class_, key, inst):
        """Add validators for any attributes that can be validated."""
        prop = inst.prop
        # Only interested in simple columns, not relations
        if isinstance(prop, ColumnProperty) and len(prop.columns) == 1:
            col = prop.columns[0]
            # if we have string column with a length, install a length validator
            if isinstance(col.type, String) and col.type.length:
                inst.impl.extensions.insert(0, LengthValidator(col.type.length))

class ValidationError(Exception):
    pass

class LengthValidator(AttributeExtension):
    def __init__(self, max_length):
        self.max_length = max_length

    def set(self, state, value, oldvalue, initiator):
        if len(value) > self.max_length:
            raise ValidationError("Length %d exceeds allowed %d" %
                                (len(value), self.max_length))
        return value

rawDescription: SQLAlchemy - maximum column length

description: SQLAlchemy - maximum column length


rawDescription: Subtracting the Nearest Prime Given Two Lists using for loop and max


rawDescription: django-post_office - Using file-based instead of database-based email templates

description: Django-post_office - Using file-based instead of database-based email templates


code:
REGION_DATA = { "USA": {'Bordering':['MEXICO','CANADA'],'MILITARYGROWTHRATE':1.03},
                "MEXICO": {'Bordering':['USA'],'MILITARYGROWTHRATE':1.01},
                "CANADA": {'Bordering':['USA'],'MILITARYGROWTHRATE':1.01}
              }

rawDescription: How do I loop this?

description: Loop this


rawDescription: Setup dictionary lazily

description: Setup dictionary lazily


code:
f = open("diaryfile", "r")
body = []
for line in f:
    if your_regexp.match(line):
        if body:
            write_one(body)
        body = []
    body.append(line)
if body:
    write_one(body)
f.close()

rawDescription: Split diary file into multiple files using Python


code:
from multiprocessing import Pool

pool = Pool()
pool.apply_async(func1, (10**9,))
pool.apply_async(func1, (10**9,))
pool.apply_async(func1, (10**9,))

rawDescription: Run function with positional and optional arguments in parallel in python (follow up)


code:
top_points, smallest = choose(lines, ntop)
decremented = top_points

rawDescription: Sorting points on multiple lines

description: Sorting points on multiple lines


code:
cutoff = '2014-10-30'
df[df['event_date'] <= cutoff].groupby(['id'])['event_date'].last()

rawDescription: pandas index.asof with multiindex

description: Pandas index.asof with multiindex


code:
def filter_result_items(bundle):
    res = ResultResource()
    new_bundle = Bundle(request=bundle.request)
    objs = res.obj_get_list(new_bundle)
    return objs.filter(parent_id=bundle.obj.pk)

rawDescription: Tastypie: Filtering by ToManyField

description: Tastypie: Filtering by ToManyField


code:
s = " \t foo \n bar "
"".join(s.split())

rawDescription: How to strip all whitespace from string

description: Strip all whitespace from string


code: quadrants = [[Quadrant() for i in range(8)] for j in range(8)]

rawDescription: Creating 2d array of classes in Python

description: Creating 2d array of classes in Python


code:
def tick():
  for box in randomBoxes:
    box.relocate()

  specialBlock1.relocate()

  specialBlock2.relocate()

  specialBlock3.relocate()

rawDescription: Python - Threading, Timing, or function use?


rawDescription: how can i make a sum in a line of numbers in python?

description: Make a sum in a line of numbers in python


code:
# subquery to get number of topics per IP
subq_ip_topics = (session.query(
        MCastSession.ip.label("mcast_session_ip"),
        func.count(Topic.id).label("num_topics")
    )
    .join(Topic)
    .group_by(MCastSession.ip)
    ).subquery().alias("ip_topics")

# subquery to get number of topics per host per ip
subq_host_ip_topics = (session.query(
        Host.id.label("host_id"),
        MCastSession.ip.label("mcast_session_ip"),
        func.count(Topic.id).label("num_topics")
    )
    .join(Subscriber)
    .join(Topic)
    .join(MCastSession)
    .group_by(Host.id, MCastSession.ip)
    ).subquery().alias("host_ip_topics")

# final query: get those Hosts where results on both sub-queries do not match
query = (session.query(Host)
        .join(subq_host_ip_topics, Host.id == subq_host_ip_topics.c.host_id)
        .join(subq_ip_topics, and_(
            subq_host_ip_topics.c.mcast_session_ip == subq_ip_topics.c.mcast_session_ip,
            subq_host_ip_topics.c.num_topics != subq_ip_topics.c.num_topics
            ))
        )

rawDescription: Writing SQL query refering same table twice


code:
'hello'.islower()
[m for m in dir(str) if m.startswith('is')]

rawDescription: Check if string is upper, lower, or mixed case in Python


code:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import Screen
from kivy.properties import StringProperty, ObjectProperty
from random import choice

class ImageButton(GridLayout):

    images = [
        'http://kivy.org/logos/kivy-logo-black-256.png',
        'http://img.interia.pl/rozrywka/nimg/2/7/roz4286600.jpg',
        'http://www.i-programmer.info/images/stories/News/2014/Apr/A/kivycont2.jpg',
        'http://static.giantbomb.com/uploads/scale_small/9/90155/2472244-abathur.png',
        'https://www.gravatar.com/avatar/a07de5a89d18964a22775deae84d9ba6?s=328&d=identicon&r=PG',
        'https://www.gravatar.com/avatar/578e323e1c4dda99b24bf047e0cb2e3e?s=328&d=identicon&r=PG&f=1'
    ]

    source = StringProperty(images[0])

    def change_image(self):
        while True:
            new_img = choice(self.images)
            if new_img != self.source:
                self.source = new_img
                break

class MainScreen(Screen):

    grid = ObjectProperty()

    def populate(self):
        for i in xrange(10):
            self.grid.add_widget(ImageButton())

class Test(App):

    def on_start(self):
        self.root.populate()

Test().run()

rawDescription: ScrollView and CustomButton

description: ScrollView and CustomButton


code:
import os
os.path.isfile("/1.txt")
os.path.isfile("/home/vivek/Desktop/file3.csv")

rawDescription: How to exit for loop once valid file name is provided and give 3 chances for correct input

description: Exit for loop once valid file name is provided and give 3 chances for correct input


rawDescription: Get all methods of a class, but not those of superclasses


code:
with open('highscores.txt', 'rb') as f:
    reader = csv.reader(f)
    scores = (int(row[-1]) for row in reader)
    topscores = sorted(scores, reverse=True)
    top10 = topscores[:10]

rawDescription: How to display top 10 highscores from a comma separated value (.txt) file in Python (Pygame)

description: Display top 10 highscores from a comma separated value (.txt) file in Python (Pygame)


code:
from xml.etree import ElementTree

inputs = dict()
tree = ElementTree.parse( 'inputs.xml' )
for row in tree.getiterator('inputs'):
    for elem in row.getchildren():
        inputs[elem.tag] = elem.get('value')

rawDescription: Listing inputs on Python by using XML and array


code:
from collections import Iterable

my_strip("5,6!7,8")
my_strip(["hel?llo","intro"])

rawDescription: How to remove the punctuation in the middle of a word or numbers?

description: Remove the punctuation in the middle of a word or numbers


code:
from operator import not_
true_values = list(compress(sequence, bits))
false_values = list(compress(sequence, map(not_, bits)))

rawDescription: How to efficiently extract specific subsets of list elements in Python

description: Efficiently extract specific subsets of list elements in Python


code: head, *rest = my_func()

rawDescription: Pattern matching of lists in Python

description: Pattern matching of lists in Python


rawDescription: What is Python's coerce() used for?

description: What is Python's coerce() used for


code: json.dumps(_your_dict, cls=DateEncoder)

rawDescription: psycopg2 execute returns datetime instead of a string

description: Psycopg2 execute returns datetime instead of a string


code:
def shape_preserving_mult(x, y):
    return (x.reshape((-1,)) * y.reshape((-1))).reshape(x.shape)

rawDescription: Elementwise multiply 1D-numpy arrays (shapes (k,1) or (k,)) and have result have the shape of the first


code: owner_cals = [j for i,j in enumerate(cal_items) if j["access_role"]=="owner"]

rawDescription: An easier way of referring to the index of a desired dictionary (Python)?


code:
length = None
while not length:
    try:
        length = float(input('Enter your preferred turtle line length: '))
    except ValueError:
        print('You need to enter a number')

width = None
while not width:
    try:
        width = int(input('Enter your preferred turtle line width: '))
    except ValueError:
        print('You need to enter a positive integer')
    else:
        if width < 1:
            print('You need to enter a positive integer')
            width = None

rawDescription: Python: Write a program that asks the user for a color, a line width, a line length and a shape


code: a = [sorted(i, key=lambda i: (i[0], -i[1])) for i in a]

rawDescription: Python 3.3 Sorting a tuple list with multiple keys


rawDescription: Apply non-linear regression for multi dimension data samples in Python


code:
# top of the program logic
root = tkinter.Tk()
...
def some_function(): ...
...
some_widget.bind("<1>", some_function)
...
# bottom of the program logic
root.mainloop()

rawDescription: How is tkinter code executed?

description: How is tkinter code executed


rawDescription: sort Python dictionary with two keys

description: Sort Python dictionary with two keys


code:
import subprocess

subprocess.Popen('export ATEST="Hello";echo "1 $ATEST"', shell=True)
subprocess.Popen('echo "2 $ATEST"', shell=True)

rawDescription: Calling alias Command from python script


code:
import re

s = '''Ik heb nog nooit een kat gezien zo lélijk!
Het is een minder lelijk dan uw hond.'''

p = re.compile(r'^.*\b(?<!-)(\w+(?:-\w+)*)', re.M | re.U)

words = p.findall(s)

print('\n'.join(words))

rawDescription: Unicode Regex with regex not working in Python


code:
def reverse_list(letters, first=0, last=None):
    "reverses the elements of a list in-place"
    if last is None:
        last = len(letters)
    last -= 1
    while first < last:
        letters[first], letters[last] = letters[last], letters[first]
        first += 1
        last -= 1

def reverse_words(string):
    """reverses the words in a string using a list, with each character
    as a list element"""
    characters = list(string)
    reverse_list(characters)
    first = last = 0
    while first < len(characters) and last < len(characters):
        if characters[last] != ' ':
            last += 1
            continue
        reverse_list(characters, first, last)
        last += 1
        first = last
    if first < last:
        reverse_list(characters, first, last=len(characters))
    return ''.join(characters)

rawDescription: How do you reverse the words in a string using python (manually)?

description: Reverse the words in a string using python (manually)


rawDescription: How to alter a part of an array

description: Alter a part of an array


rawDescription: How to fetch timestamp in Pandas DataFrame

description: Fetch timestamp in Pandas DataFrame


code:
import itertools as IT

header_map = {'Foo':str,
              'Bar':str,
              'FooBar':float}

N = 20000
csv_data = [('Foo', 'Bar', 'FooBar')] + [('Foo', 'Bar', 1123.451)]*N

def original(csv_data):
    index_map = {}
    #enumerate the rows and create a dictionary of index:function
    for i, header in enumerate(csv_data[0]):
        index_map[i] = header_map[header]

    #retrieve the appropriate function for each index and call it on the value
    new_csv = [[index_map[i](value) for i, value in enumerate(row)]
               for row in csv_data[1:]]
    return new_csv

def using_converter(csv_data):
    converters = IT.cycle([header_map[header] for header in csv_data[0]])
    conv = converters.next
    new_csv = [[conv()(item) for item in row] for row in csv_data[1:]]
    return new_csv

def using_header_map(csv_data):
    heads = csv_data[0]
    new_csv = [
        tuple(header_map[head](item) for head, item in zip(heads, row))
        for row in csv_data[1:]]
    return new_csv

# print(original(csv_data))
# print(using_converter(csv_data))
# print(using_header_map(csv_data))

rawDescription: Map multiple functions to CSV rows

description: Map multiple functions to CSV rows


rawDescription: Decorator for overloading in Python

description: Decorator for overloading in Python


rawDescription: Validate a name in Python

description: Validate a name in Python


code:
upper_bound = 1000

sqr = [i**2 for i in range(upper_bound+1)]

def solution(p):
    count = 0
    for a in range(1, p - 1):
        for b in range(a, p - a):
            c = p - (a + b)
            d = sqr[a]
            e = sqr[b]
            f = sqr[c]

            if (d + e == f):
                count += 1
    return count

c, p = max((solution(p), p) for p in range(3, upper_bound+1))
print("%d has %d solutions" % (p, c))

rawDescription: How to optimize this Python script?

description: Optimize this Python script


rawDescription: How to plot data with Python from text file

description: Plot data with Python from text file


code:
for values in List:
    print (" ".join([chars for chars in values]), "\n")

rawDescription: Printing a list into grid

description: Printing a list into grid


code:
flierprops = dict(markerfacecolor='0.75', markersize=5,
              linestyle='none')
seaborn.boxplot(x="centrality", y="score", hue="model", data=data,
                flierprops=flierprops)

rawDescription: Tweaking seaborn.boxplot

description: Tweaking seaborn.boxplot


code: ['{0}: {1}'.format(', '.join(v), k) for k, v in d.items()]

rawDescription: grouping elements into new list from two lists using python

description: Grouping elements into new list from two lists using python


code: tags = map(int, tags)

rawDescription: Unable to match tuples items to list items


code:
if re.search(r'мерзавец|подлец', agit_corpus.raw(fileid)):
    dict_features[fileid] = {'oskorblenie':'1'}
else:
    dict_features[fileid] = {'oskorblenie':'0'}

if re.search(r'честны*|труд*', agit_corpus.raw(fileid)):
    dict_features[fileid] = {'samoprezentacia':'1'}
else:
    dict_features[fileid] = {'samoprezentacia':'0'}

rawDescription: Dictionary of dictionaries in loop in Python


code:
from pyparsing import Suppress,QuotedString,Word,alphas,nums,alphanums,Keyword,Optional
import datetime

# define UTC timezone for sake of eval
if hasattr(datetime,"timezone"):
    UTC = datetime.timezone(datetime.timedelta(0),"UTC")
else:
    UTC = None

_ = Suppress
evaltokens = lambda s,l,t: eval(''.join(t))

timevalue = 'datetime.datetime' + QuotedString('(', endQuoteChar=')', unquoteResults=False)
timevalue.setParseAction(evaltokens)

strvalue = 'u' + QuotedString("'", unquoteResults=False)
strvalue.setParseAction(evaltokens)

nonevalue = Keyword("None").setParseAction(lambda s,l,t: [None])
intvalue = Word(nums).setParseAction(lambda s,l,t: int(t[0]))

COMMA = Optional(_(","))

valuedexpr = lambda expr: (Word(alphas) + "(" + "value" + "=" + expr + ")").setParseAction(lambda t: t[4])

lineexpr = (_("Aggregate(aggregate_dimension_value_list=[") +
            valuedexpr(timevalue)("timestamp") + COMMA +
            (nonevalue | valuedexpr(strvalue))("s1") + COMMA +
            (nonevalue | valuedexpr(strvalue))("s2") + COMMA +
        "]" + COMMA +
        "quantity=" + intvalue("qty"))

rawDescription: How to parse the following multi-dimensional xml-like data into a dataframe

description: Parse the following multi-dimensional xml-like data into a dataframe


code: sparse.vstack([sparse.kron(a,b) for a,b in zip(As,Bs)]).A

rawDescription: Special kind of row-by-row multiplication of 2 sparse matrices in Python


code:
def levenshteinDistance (s, t):
    if len(s) == 0:
        return len(t)
    if len(t) == 0:
        return len(s)
    return min(levenshteinDistance(s[:-1], t) + 1, levenshteinDistance(s, t[:-1]) + 1, levenshteinDistance(s[:-1], t[:-1]) + (0 if s[-1] == t[-1] else 1))

def isSimilar (s, t):
    return levenshteinDistance(s, t) < 2

rawDescription: How to remove duplicates from a list in python

description: Remove duplicates from a list in python


rawDescription: numpy.equal with nested lists

description: Numpy.equal with nested lists


code:
import numpy as np

H1 = np.array([[11, 11], [11, 11]])
H2 = np.array([[22, 22], [22, 22]])
H3 = np.array([[33, 33], [33, 33]])

inputs = (H1, H2, H3)

# This assumes all the arrays in `inputs` have the same shape,
# and that the data type of all the arrays is the same as H1.dtype.
nh = len(inputs)
nrows = 2*nh - 1
m, n = H1.shape
# T is a 4D array.  For a given i and j, T[i, :, j, :] is a 2D array
# with shape (m, n).  T can be intepreted as a 2D array of 2D arrays.
T = np.zeros((nrows, m, nh, n), dtype=H1.dtype)
for i, H in enumerate(inputs):
    for j in range(nh):
        T[i + j, :, j, :] = H

# Partially flatten the 4D array to a 2D array that has the desired
# block structure.
T.shape = (nrows*m, nh*n)

print(T)

rawDescription: Toeplitz matrix of toeplitz matrix

description: Toeplitz matrix of toeplitz matrix


code: lst = []

rawDescription: Read specific column from .dat-file in Python


code: res = {k[0]: [k[1], len(k[1])] for k in [(date, [x[2] for x in gr]) for date, gr in groupby(l, itemgetter(1))]}

rawDescription: Extracting data from a list of tuples

description: Extracting data from a list of tuples


code:
from collections import MutableMapping

class StrictWriteOnceDict(MutableMapping, dict):
    # dict implementations to override the MutableMapping versions
    __getitem__ = dict.__getitem__
    __iter__ = dict.__iter__

    def __delitem__(self, key):
        raise KeyError('Read-only dictionary')

    def __setitem__(self, key, value):
        if key in self:
            raise KeyError('{} has already been set'.format(key))
        dict.__setitem__(self, key, value)

rawDescription: Write-once dictionary?

description: Write-once dictionary


code:
[...]
width = 500 #the desired minimum width
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.SetMinSize((width,1)) #ensures sizer will be minimum width
"""NOTE: the minimum size gets recalculated when new items are added.
i.e. if height exceeds 1 pixel or if width exceeds wdith, the sizer
will recalculate the minimum size"""

for i in range(5):
    description = wx.StaticText(self, -1, "Short Description") #description is less than 500 pixels, Wrap() will have no effect.
    description.Wrap(width)
[...]

rawDescription: Filling available space with a column in GridBagSizer


code:
df1 = df.reset_index('status')
df1

rawDescription: Pandas: Aggregate by month for every subgroup


code:
def reduceDups(lst):
  lst = sorted(lst)
  i = 0
  while(i < len(lst)):
    if(i < len(lst) - 1):
      # Get reference elements
      (refFirst, refLast) = (lst[i][:-1], lst[i][-1])

      # Get first, last elements of possible duplicate
      (dupFirst, dupLast) = (lst[i + 1][:-1], lst[i + 1][-1])

      if(refFirst == dupFirst):
        # Next element is identical (by our comparison). Get rid of it
        lst.pop(i + 1)
        lst[i].append(dupLast)
      else:
        i += 1
    else:
      i += 1

  return lst

rawDescription: python how can I find out the duplicate elements in a three dimensional list

description: Python how can I find out the duplicate elements in a three dimensional list


code:
person = db.relationship(
    "Person",
    backref=db.backref("gift", uselist=False),
)

rawDescription: SQLAlchemy Join two tables in (1 to 0..1 ) relationship


rawDescription: python 3.x xml parsing similar to plistlib?

description: Python 3.x xml parsing similar to plistlib


code: array2 = [int(x == 4) for x in array1]

rawDescription: Python list comprehensions: set all elements in an array to 0 or 1


code:
import dis
def f(): return 1 < input("Value:") < 10

dis.dis(f)

rawDescription: How do chained comparisons in Python actually work?


rawDescription: Convert an SQL into pymongo query

description: Convert an SQL into pymongo query


code:
x = df.applymap(encoding.get)
(x.cool+x.not_cool).values  # gives you matrix without the headers
# should be elementary to get labels you need in there

rawDescription: Expanding a Pandas.dataframe after encoding


code:
json_obj = {
    'list': {
        # this is the array that is being iterated
        'story': [
            {'title': {
                '$text': 'some title1'
                }
            },
            {'title': {
                '$text': 'some title2'
                }
            },
            {'title': {
                '$text': 'some title3'
                }
            },
        ],
    },
  }

rawDescription: Python for loop syntax

description: Python for loop syntax


code:
from twisted.internet import reactor
from scrapy.crawler import Crawler
# scrapy.conf.settings singlton was deprecated last year
from scrapy.utils.project import get_project_settings
from scrapy import log

def setup_crawler(spider_name):
    crawler = Crawler(settings)
    crawler.configure()
    spider = crawler.spiders.create(spider_name)
    crawler.crawl(spider)
    crawler.start()

log.start()
settings = get_project_settings()
crawler = Crawler(settings)
crawler.configure()

for spider_name in crawler.spiders.list():
    setup_crawler(spider_name)

reactor.run()

rawDescription: Locally run all of the spiders in Scrapy


code:
@app.route("/oauth/callback")
def callback():
    # retrieve access_token
    session["stripe_token"] = access_token
    return redirect(to_somewhere_that_makes_sense)

rawDescription: pass access_token to charge function from callback function

description: Pass access_token to charge function from callback function


code:
def glob_filter(names,pat):
    return (name for name in names if re.match(glob2re(pat),name))

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


code:
import os

def load_modules(pkg):
    for filename in os.listdir(os.path.dirname(pkg.__file__)):
        if filename.endswith(".py"):
            m = __import__("{}.{}".format(pkg.__name__, os.path.splitext(filename)[0]))

rawDescription: Importing all modules in a package?

description: Importing all modules in a package


rawDescription: print found characters inside a string

description: Print found characters inside a string


code: wx.Font(pointSize, family, style, weight, underline=False, faceName="", encoding=wx.FONTENCODING_DEFAULT)

rawDescription: wxPython StaticText Widget Font

description: WxPython StaticText Widget Font


code:
__all__ = ['my_wrapper_maker']

# Add the entire API of someapi to our program.
for func in someapi.__all__:
    # Only add in bindings for functions.
    if callable(getattr(someapi, func)):
        globals()[func] = my_wrapper_maker(getattr(someapi, func))
        __all__.append(func)

rawDescription: Handling flexible function arguments in Python


rawDescription: Python BeautifulSoup - how to extract text between <a> tags


code:
s = "Adriano Celentano"
import random
inds = [i for i,_ in enumerate(s) if not s.isspace()]
sam = random.sample(inds, 3)
from string import ascii_letters

lst = list(s)
for ind in sam:
    lst[ind] = random.choice(ascii_letters)

print("".join(lst))

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


code:
msg = MIMEText("""The nightly build status was a SUCCESS

Build File: http://www.python.org

Build Results File: http://10.51.54.57/sandboxes/""", project, "\n")

rawDescription: String manipulation in Python

description: String manipulation in Python


rawDescription: How to test custom Django forms clean/save methods?

description: Test custom Django forms clean/save methods


code:
L = ['John', 'House', '23 4500', 'grass', '542 3256']
[y for x in L for y in x.split()]

rawDescription: explode list to another list at space characters

description: Explode list to another list at space characters


rawDescription: Merge Python List

description: Merge Python List


code:
import scipy.ndimage
erosion = scipy.ndimage.binary_erosion(land)

rawDescription: Isolating coastal grids using convolving window (python)


code:
from scipy.spatial import distance
D = distance.squareform(distance.pdist(X))

rawDescription: How to do this operation in numPy?

description: Do this operation in numPy


code:
example = Example()
example['spam'] = 42
example['eggs'] = b'\x01\x00abcde'
print(example.prettyPrint())
substrate = encoder.encode(example)
data, tail = decoder.decode(substrate, asn1Spec=Example())
print(data.prettyPrint())

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


code:
validate_names = NameValidator(excludelist=excludelist,
                               deletechars=deletechars,
                               case_sensitive=case_sensitive,
                               replace_space=replace_space)

rawDescription: How to preserve column names while importing data using numpy?

description: Preserve column names while importing data using numpy


code:
data = get_json()
next(iter(data[0].items())) # python 2/python 3
data[0].items()[0] # python 2
list(data[0].items())[0] # python 2/python 3

rawDescription: refrencing certain values in a list

description: Refrencing certain values in a list


rawDescription: Beautiflsoup Create Soup With A Snippet of Page Source


rawDescription: Print contents of two lists so corresponding records from each print sequentially


code: n = records.next()

rawDescription: pymongo: possible to iterate result use next() and prev()?

description: Pymongo: possible to iterate result use next() and prev()


code: self.scale = 0.3

rawDescription: Issues with Scatter size and position

description: Issues with Scatter size and position


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

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


code:
for match in re.finditer(r"(?im)^\D*(\d+(?:[- ][a-z ]*[a-z])?)", subject):
    yournumber = match.group(1)

rawDescription: regex to extract first series of numbers in a string and all words after

description: Regex to extract first series of numbers in a string and all words after


code:
#Change how we index the frame
df.set_index(["Trans", "Num"], inplace=True)

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


rawDescription: two question about python regular expressions

description: Two question about python regular expressions


code: print(line.rstrip('\n') + additional)

rawDescription: Printing output in one line from standard input in python


rawDescription: Removing elements from pandas series in python


code:
import re
re.findall( '[^@]+@[^@]+\.[^@]+ ', text) # remember change `text` variable

rawDescription: Python web Automation to get Email from Webpage


code:
import random
from tkinter import *

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

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

def onclick(e):
    found = canvas.find_closest(e.x, e.y)
    if found:
        canvas.itemconfig(found[0], fill='blue')
canvas.bind('<1>', onclick)

root.mainloop()

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

description: Add text to a canvas from a list


code:
# See if the ball hits the player paddle
if pygame.sprite.spritecollide(player, balls, False):
    # The 'diff' lets you try to bounce the ball left or right
    # depending where on the paddle you hit it
    diff = (player.rect.x + player.width/2) - (ball.rect.x+ball.width/2)

    # Set the ball's y position in case
    # we hit the ball on the edge of the paddle
    ball.rect.y = screen.get_height() - player.rect.height - ball.rect.height - 1
    ball.bounce(diff)

rawDescription: Understand object orientated python - initialization of objects in pygame


code:
line_width = 10
sys.stdout.write('\r{0}\r'.format(' ' * line_width))

rawDescription: Stdout looping halt after certain amount of characters


rawDescription: Python lxml - find parent of comment

description: Python lxml - find parent of comment


code:
{'label': u'noun',
 'meaning': u'the joining together of businesses which deal with different stages in the production or sale of the same product, as when a restaurant chain takes over a wine importer',
 'related_to': [u'sale', u'chain', u'wine'],
 'utag': [u'product']}

rawDescription: How to extract and ignore span in markup? - python

description: Extract and ignore span in markup? - python


code: p = numpy.concatenate((x, y, z)).reshape((3, len(x))).T

rawDescription: reshaping an arbitrary collection numpy arrays

description: Reshaping an arbitrary collection numpy arrays


code:
{'pmid': '19587274', 'abstract': None, 'title': None}
{'pmid': None, 'abstract': None, 'title': 'Domain general mechanisms of perceptual decision making in human cortex.\n'}
{'pmid': None, 'abstract': 'To successfully interact with objects in the environment, sensory evidence must\n      be continuously acquired, interpreted, and used to guide appropriate motor\n      responses. For example, when driving, a red \n', 'title': None}
{'pmid': '19583148', 'abstract': None, 'title': None}
{'pmid': None, 'abstract': None, 'title': 'Ursodeoxycholic acid for treatment of cholestasis in patients with hepatic\n      amyloidosis.\n'}
{'pmid': None, 'abstract': 'BACKGROUND: Amyloidosis represents a group of different diseases characterized by\n      extracellular accumulation of pathologic fibrillar proteins in various tissues\n', 'title': None}

rawDescription: Match multiple patterns in a multiline string


code:
a()
b()
c()
d()
e()
False

rawDescription: In Python can one implement mixin behavior without using inheritance?


code:
from catalog.views import catalog_products_view

urlpatterns = patterns(
    '',
    url(r'^(?P<category>[\w-]+)/$', catalog_products_view, name="catalog_products_view"),
    url(r'^(?P<category>[\w-]+)/(?P<filter1>[\w-]+)/$', catalog_products_view, name="catalog_products_view"),
    url(r'^(?P<category>[\w-]+)/(?P<filter1>[\w-]+)/(?P<filter2>[\w-]+)/$', catalog_products_view, name="catalog_products_view"),
    url(r'^(?P<category>[\w-]+)/(?P<filter1>[\w-]+)/(?P<filter2>[\w-]+)/(?P<filter3>[\w-]+)/$', catalog_products_view, name="catalog_products_view"),
)

rawDescription: Django: variable parameters in URLconf

description: Django: variable parameters in URLconf


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

# Here we have a string with a multiline ...
fileString='''bazfoo
baby foo
baby foo
baby foo
bar'''

# re.DOTALL tells re that '.' should match any character, including newlines.
x = re.search('(.*?)', fileString, re.DOTALL)
for match in x.groups():
    print(match)
# foo
# baby foo
# baby foo
# baby foo
#

rawDescription: Python RegEx skipping the first few characters?


code: buffer = leyendo.read()

rawDescription: How to append a list to a file on Python using JSON

description: Append a list to a file on Python using JSON


code:
class SignedFileRequest(SignedRequest):
    __attr_map = dict(name=NAME_KEY, content_type=CONTENT_TYPE_KEY,
                      hash=HASH_KEY, node_id=NODE_ID_KEY)

    def __init__(self, host, path, node_id = None, name=None, \
                 content_type=None, hash=None, ssl=False, expires=0):
        super(SignedFileRequest, self).__init__(host, path, ssl, expires)
        self.name = name
        self.content_type = content_type
        self.hash = hash
        self.node_id = node_id

    def __getattr__(self, attr):
        if attr in self.__attr_map:
            return self.query_dict.get(self.__attr_map[attr])
        return super(SignedFileRequest, self).__getattr__(attr)

    def __setattr__(self, attr, value):
        if attr in self.__attr_map:
            self.query_dict[self.__attr_map[attr]] = value
        super(SignedFileRequest, self).__setattr__(attr, value)

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

description: Abbreviate this Python code using metaprogramming


code:
_breakpoints = {}

def reset_breakpoints(disabled=[]):
    global _breakpoints
    _breakpoints = dict((x, False) for x in disabled)

def set_breakpoint(tag, condition=True):
    if tag not in _breakpoints:
        _breakpoints[tag] = True
        if condition:
            pdb.set_trace()
    else:
        if _breakpoints[tag] and condition:
            pdb.set_trace()

def mycode():
    some_command()
    set_breakpoint('mycode0')
    another_command()
    set_breakpoint('mycode1', x == 4)

rawDescription: How to set pdb break condition from within source code?

description: Set pdb break condition from within source code


code:
from django.contrib import admin
from lottery.models import Genre, Actor, Title
from lottery.admin.actor import ActorAdmin

admin.site.register(Actor, ActorAdmin)
...

rawDescription: When to split sub-apps into new apps

description: When to split sub-apps into new apps


rawDescription: How to get data from this "list" looking thing in python 2.7

description: Get data from this "list" looking thing in python 2.7


code:
import mock
import time

class Foo(object):

   def __init__(self):
       self._is_running = False

   @property
   def is_running(self):
       return self._is_running

   @is_running.setter
   def is_running(self, val):
       self._is_running = val

   def run(self):
       self._is_running = True  # Don't go through the property here.
       while self.is_running:
           print("in here")
           time.sleep(.5)

with mock.patch('__main__.Foo.is_running', new_callable=mock.PropertyMock,
                side_effect=[True, True, False]) as m:
    f = Foo()
    f.run()

rawDescription: Testing a while loop in python

description: Testing a while loop in python


code:
def data_to_idxlists(data):
    sorting_ixs = np.argsort(data)
    uniques, unique_indices = np.unique(data[sorting_ixs], return_index = True)
    return {u: sorting_ixs[start:stop] for u, start, stop in zip(uniques, unique_indices, list(unique_indices[1:])+[None])}

rawDescription: Python: faster operation for indexing

description: Python: faster operation for indexing


code: max_only = {k: v for k, v in stats.iteritems() if v == max_value}

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


code: col = A.getcol(colindex)

rawDescription: (Python) How do you get the mean and std of a column in a csr_matrix?


code:
import re
s = 'Data'
re.search(r'manual-data-link" href="(.*?)"', s).group(1)

rawDescription: Regex matching specific HTML string with Python


code:
# untested
backgroundImage = root.PhotoImage(io.StringIO(base64.decode(background_image)))
...

rawDescription: How to convert a string to an image to make a single executable

description: Convert a string to an image to make a single executable


rawDescription: Add a number to the front of a number row python


code:
import ast
import subprocess
ret = subprocess.check_output(['python','some_script.py'])
dic = ast.literal_eval(ret)

rawDescription: Get list of dicts from another python script?


code:
# file: bar.py
import singleton

def bar():
    # ... do more complex things ...
    if 'baz' in singleton.element_list:
        print('baz')

rawDescription: Define a constructor in full classmethod class


code:
for xx in bigbadlistofcontours:
    cv2.drawContours(contourimage,[xx[0]],-1,(0,0,0),-1)
    cv2.drawContours(contourimage,[xx[1]],-1,(0,0,0),-1)

rawDescription: why are my contours not being totally filled in opencv?

description: Why are my contours not being totally filled in opencv


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


code: soup1 = BeautifulSoup(''.join(str(t) for t in tags))

rawDescription: Cannot prettify scraped html in BeautifulSoup


code:
def return_template(item):
    template = 'null'

    context = {
            #Dictionary of things to pass back

    }
    #if statements that choose what 'template' should be
    if item == 5:
        template = 'item5.html'
    else:
        template = 'default.html'

    #render the template
    return render_to_string(template, context)

rawDescription: Django: How to include a view from within a template


code: np.vectorize(f, otypes=[np.int32])(*np.rollaxis(arr, 2, 0))

rawDescription: Reducing an axis with numpy

description: Reducing an axis with numpy


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

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


code:
import re
re.search(r"\b\+359\b","Is your phone number +359 887438?")
re.search(r"\B\+359\b","Is your phone number +359 887438?")
re.search(r"(?<!\w)\+359\b","Is your phone number +359 887438?")

rawDescription: How can you make \b accept words that start with '+' using regex in python?

description: Make \b accept words that start with '+' using regex in python


code: next(b)

rawDescription: Python itertools tee, clones and caching


code:
_success = False
def atsuccess(func, *args, **kwds):
    def wrapper():
        if _success:
            func(*args,**kwds)
    atexit(wrapper)

def set_success():
    global _success
    _success = True

# then call atsuccess() to attach your callbacks,
# and call set_success() before your script returns

rawDescription: How can I register a function to be called only on *successful* exit of my Python program?

description: Register a function to be called only on *successful* exit of my Python program


code: documents = [stem(word) for sentence in documents for word in sentence.split(" ")]

rawDescription: How to stem words in python list?

description: Stem words in python list


code:
def func3(xy, s):
    size = xy.shape[0]-2*s
    tmp1 = xy[0:size] - xy[s:size+s]
    tmp2 = xy[2*s:size+2*s] - xy[s:size+s]
    tmp2 = tmp2[:, ::-1]
    tmp2[:, 0] *= -1
    return np.einsum('ij,ij->i', tmp1, tmp2)

rawDescription: Can numpy einsum() perform a cross-product between segments of a trajectory


code: vids = ['vid_' + c for c in 'abcdefghijkl']

rawDescription: How to choose a random but non-recent asset from a list?

description: Choose a random but non-recent asset from a list


code:
maxRadius < 3*minRadius
maxRadius - minRadius < 100

rawDescription: Circular Hough Transform misses circles

description: Circular Hough Transform misses circles


code:
def create_table_from_query(self,
                            query,
                            dest_table,
                            how):
    body = {
        'configuration': {
            'query': {
                'destinationTable': {
                    'projectId': your_project_id,
                    'tableId': dest_table,
                    'datasetId': your_dataset_id
                },
                'writeDisposition': how,
                'query': query,
            },
        }
    }

    response = self.connector.jobs().insert(projectId=self._project_id,
                                            body=body).execute()
    self.wait_job_completion(response['jobReference']['jobId'])

def wait_job_completion(self, job_id):
    while True:
        response = self.connector.jobs().get(projectId=self._project_id,
                                             jobId=job_id).execute()
        if response['status']['state'] == 'DONE':
            return

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


code:
from itertools import groupby

tuples = [('Jem', 10), ('Sam', 10), ('Sam', 2), ('Jem', 9), ('Jem', 10)]
tuples.sort(key=lambda tup: tup[0])
# tuples is now [('Jem', 10), ('Jem', 9), ('Jem', 10), ('Sam', 10), ('Sam', 2)]

groups = groupby(tuples, lambda tup: tup[0])

rawDescription: How can I find the average of each similar entry in a list of tuples?

description: Find the average of each similar entry in a list of tuples


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


code: lst1, lst2, lst3 = [lst[i:i+4] for i in xrange(0, len(lst), 4)]

rawDescription: How to split lists into smaller lists at certain points in python?

description: Split lists into smaller lists at certain points in python


code: lines = [tuple(row) for row in csv.reader(yourfile)]

rawDescription: Access elements of a comma separated list with tuples in Python


rawDescription: pythonic way to maximize the number of items that fit in a list of available spots

description: Pythonic way to maximize the number of items that fit in a list of available spots


code:
a_to_b = a_to_as.take(as_to_b)
print(np.all(A[a_to_b] == B))
# True

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


code:
mylist=[{'word':'bear','category':'animal'},
        {'word':'lion','category':'animal'},
        {'word':'ant','category':'insect'},
        {'word':'ladybug','category':'insect'}]

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


code:
In [11]: c.index.levels[0].values
Out[11]: array(['t1', 't2'], dtype=object)

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


code:
import pandas as pd

def groupby_solution(X, nbrs):
    i, j = np.transpose([[k, vi] for k, v in nbrs.items() for vi in v])
    return pd.groupby(pd.DataFrame(X[j]), i).sum().values

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

rawDescription: Code optimization - number of function calls in Python


code:
def test_mockCheckOutput(self):
    m = mock.MagicMock(return_value='True')
    with mock.patch("subprocess.check_output", m):
        self.assertTrue(subprocess.check_output(args=[])=='True')

rawDescription: How does @mock.patch know which parameter to use for each mock object?


code:
def getAllSums(tab):
    finalSum = 0
    for i in range(tab.shape[0]):
        finalSum += getSum(tab,i+1)
    return finalSum

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

description: Vectorize and speed up this large array calculation


code:
from collections import OrderedDict
[value for _, value in OrderedDict.fromkeys((type(x), x) for x in l)]

rawDescription: Python removing duplicates in list and 1==1.0 True


code:
from pprint import pprint
pprint([cities[i] for i in [37810, 15755, 16276]])

rawDescription: Python - how to speed up calculation of distances between cities


code:
slot = []
for row in range(9):
    slot.append([None] * 9)

def print_slot(slot, empty='[ ]'):
    for row in slot:
        print(' '.join(empty if element is None else '{{: ^{}}}'.format(len(empty)).format(element) for element in row))

rawDescription: ultimate fixed width Python board array

description: Ultimate fixed width Python board array


code:
def dict_intersection(d1, d2):
    return {key: d2[key] or d1[key] for key in d1.viewkeys() & d2.viewkeys()}

rawDescription: intersection_update for dicts?

description: Intersection_update for dicts


code:
def consumer(func):
    def wrapper(*args,**kw):
        gen = func(*args, **kw)
        gen.next()
        return gen
    wrapper.__name__ = func.__name__
    wrapper.__dict__ = func.__dict__
    wrapper.__doc__  = func.__doc__
    return wrapper

rawDescription: Circular Programming in Python (corecursion) - is it possible?


code: DEB_MAKE_CHECK_TARGET = check

rawDescription: Run Python integration tests inside pBuilder environment


code: integer = Word(nums+'_').setParseAction(lambda t: t[0].replace('_',''))

rawDescription: python pyparsing word excludeChars

description: Python pyparsing word excludeChars


code:
cursor.executemany('INSERT INTO foo (username,password) VALUES (?,?)',
               (('baz','bar'),('bing','bop')))
print(cursor.lastrowid)
# None

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

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


code:
class StateDispatcher(object):

    def __init__(self, state_attr='state'):
        self.registry = {}
        self._state_attr = state_attr

    def __get__(self, instance, owner):
        if instance is None:
            return self

        method = self.registry[getattr(instance, self._state_attr)]
        return method.__get__(instance, owner)

    def register(self, state):
        def decorator(method):
            self.registry[state] = method
            return method

        return decorator

rawDescription: Singledispatch based on value instead of type


code:
def minutes(s, e):
    secs = (e - s).seconds
    return (s + datetime.timedelta(minutes = x) for x in xrange(secs / 60 + 1))

rawDescription: Python generator to return series of times


code:
f1=open('test.txt', 'r')
f2=open('/tmp/test.txt', 'r')

rawDescription: Java file permissions for threads

description: Java file permissions for threads


rawDescription: Python: parsing texts between keywords

description: Python: parsing texts between keywords


code:
MIDDLEWARE_CLASSES = (
    # other middlewares
    'myproject.profiles.middleware.UpdateLastActivityMiddleware',
)

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


code:
import re
p = re.compile(r'_ab(?!\w)') #consider underscore also as a word character.

rawDescription: Python regular expressions match end of word


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

x = np.linspace(0, 1, 100)
y = (np.random.random(100) - 0.5).cumsum()

fig, ax = plt.subplots()
ax.plot(x, y)

def imperial(x, pos):
    ft, inches = divmod(round(x*12), 12)
    ft, inches = map(int, [ft, inches])
    return ('{} ft'.format(ft) if not inches
            else '{} {} in'.format(ft, inches) if ft
            else '{} in'.format(inches))

ax.yaxis.set_major_formatter(ticker.FuncFormatter(imperial))

plt.show()

rawDescription: Python Matplotlib: plotting feet and inches


rawDescription: How to associate multiple things using dictionary?

description: Associate multiple things using dictionary


code:
def foo1():
  a = {}
  a['test1'] = 12345
  a['test2'] = 12456

import dis

dis.dis(foo1)

def foo2():
  a = {}
  for i in range(1,10):
       a['test + str(i)'] = i

dis.dis(foo2)

rawDescription: python2.7.6 Initialize a big dictionary but Memory would be used up

description: Python2.7.6 Initialize a big dictionary but Memory would be used up


rawDescription: Extract values from a list based on a condition


rawDescription: Remove unicode in Python from databse?

description: Remove unicode in Python from databse


code:
class IPA(Base):
    __tablename__ = 'ip_address'

    id = Column(Integer, primary_key=True)
    v4address = Column(INET)

    network = relationship("Network",
                        primaryjoin="IPA.v4address.op('<<', is_comparison=True)"
                            "(foreign(Network.v4representation))",
                        viewonly=True
                    )

rawDescription: SQLAlchemy, eager load postgresql INET/CIDR relationships


code:
#!/usr/bin/env python
"""
CS 65 Lab #3 -- 5 Oct 2008
Dougal Sutherland

Implements a hidden Markov model, based on Jurafsky + Martin's presentation,
which is in turn based off work by Jason Eisner. We test our program with
data from Eisner's spreadsheets.
"""

identity = lambda x: x

class HiddenMarkovModel(object):
    """A hidden Markov model."""

    def __init__(self, states, transitions, emissions, vocab):
        """
        states - a list/tuple of states, e.g. ('start', 'hot', 'cold', 'end')
                 start state needs to be first, end state last
                 states are numbered by their order here
        transitions - the probabilities to go from one state to another
                      transitions[from_state][to_state] = prob
        emissions - the probabilities of an observation for a given state
                    emissions[state][observation] = prob
        vocab: a list/tuple of the names of observable values, in order
        """
        self.states = states
        self.real_states = states[1:-1]
        self.start_state = 0
        self.end_state = len(states) - 1
        self.transitions = transitions
        self.emissions = emissions
        self.vocab = vocab

    # functions to get stuff one-indexed
    state_num = lambda self, n: self.states[n]
    state_nums = lambda self: xrange(1, len(self.real_states) + 1)

    vocab_num = lambda self, n: self.vocab[n - 1]
    vocab_nums = lambda self: xrange(1, len(self.vocab) + 1)
    num_for_vocab = lambda self, s: self.vocab.index(s) + 1

    def transition(self, from_state, to_state):
        return self.transitions[from_state][to_state]

    def emission(self, state, observed):
        return self.emissions[state][observed - 1]

    # helper stuff
    def _normalize_observations(self, observations):
        return [None] + [self.num_for_vocab(o) if o.__class__ == str else o
                                               for o in observations]

    def _init_trellis(self, observed, forward=True, init_func=identity):
        trellis = [ [None for j in range(len(observed))]
                          for i in range(len(self.real_states) + 1) ]

        if forward:
            v = lambda s: self.transition(0, s) * self.emission(s, observed[1])
        else:
            v = lambda s: self.transition(s, self.end_state)
        init_pos = 1 if forward else -1

        for state in self.state_nums():
            trellis[state][init_pos] = init_func( v(state) )
        return trellis

    def _follow_backpointers(self, trellis, start):
        # don't bother branching
        pointer = start[0]
        seq = [pointer, self.end_state]
        for t in reversed(xrange(1, len(trellis[1]))):
            val, backs = trellis[pointer][t]
            pointer = backs[0]
            seq.insert(0, pointer)
        return seq

    # actual algorithms

    def forward_prob(self, observations, return_trellis=False):
        """
        Returns the probability of seeing the given `observations` sequence,
        using the Forward algorithm.
        """
        observed = self._normalize_observations(observations)
        trellis = self._init_trellis(observed)

        for t in range(2, len(observed)):
            for state in self.state_nums():
                trellis[state][t] = sum(
                    self.transition(old_state, state)
                        * self.emission(state, observed[t])
                        * trellis[old_state][t-1]
                    for old_state in self.state_nums()
                )
        final = sum(trellis[state][-1] * self.transition(state, -1)
                    for state in self.state_nums())
        return (final, trellis) if return_trellis else final

    def backward_prob(self, observations, return_trellis=False):
        """
        Returns the probability of seeing the given `observations` sequence,
        using the Backward algorithm.
        """
        observed = self._normalize_observations(observations)
        trellis = self._init_trellis(observed, forward=False)

        for t in reversed(range(1, len(observed) - 1)):
            for state in self.state_nums():
                trellis[state][t] = sum(
                    self.transition(state, next_state)
                        * self.emission(next_state, observed[t+1])
                        * trellis[next_state][t+1]
                    for next_state in self.state_nums()
                )
        final = sum(self.transition(0, state)
                        * self.emission(state, observed[1])
                        * trellis[state][1]
                    for state in self.state_nums())
        return (final, trellis) if return_trellis else final

    def viterbi_sequence(self, observations, return_trellis=False):
        """
        Returns the most likely sequence of hidden states, for a given
        sequence of observations. Uses the Viterbi algorithm.
        """
        observed = self._normalize_observations(observations)
        trellis = self._init_trellis(observed, init_func=lambda val: (val, [0]))

        for t in range(2, len(observed)):
            for state in self.state_nums():
                emission_prob = self.emission(state, observed[t])
                last = [(old_state, trellis[old_state][t-1][0] * \
                                    self.transition(old_state, state) * \
                                    emission_prob)
                        for old_state in self.state_nums()]
                highest = max(last, key=lambda p: p[1])[1]
                backs = [s for s, val in last if val == highest]
                trellis[state][t] = (highest, backs)

        last = [(old_state, trellis[old_state][-1][0] * \
                            self.transition(old_state, self.end_state))
                for old_state in self.state_nums()]
        highest = max(last, key = lambda p: p[1])[1]
        backs = [s for s, val in last if val == highest]
        seq = self._follow_backpointers(trellis, backs)

        return (seq, trellis) if return_trellis else seq

    def train_on_obs(self, observations, return_probs=False):
        """
        Trains the model once, using the forward-backward algorithm. This
        function returns a new HMM instance rather than modifying this one.
        """
        observed = self._normalize_observations(observations)
        forward_prob,  forwards  = self.forward_prob( observations, True)
        backward_prob, backwards = self.backward_prob(observations, True)

        # gamma values
        prob_of_state_at_time = posat = [None] + [
            [0] + [forwards[state][t] * backwards[state][t] / forward_prob
                for t in range(1, len(observations)+1)]
            for state in self.state_nums()]
        # xi values
        prob_of_transition = pot = [None] + [
            [None] + [
                [0] + [forwards[state1][t]
                        * self.transition(state1, state2)
                        * self.emission(state2, observed[t+1])
                        * backwards[state2][t+1]
                        / forward_prob
                  for t in range(1, len(observations))]
              for state2 in self.state_nums()]
          for state1 in self.state_nums()]

        # new transition probabilities
        trans = [[0 for j in range(len(self.states))]
                    for i in range(len(self.states))]
        trans[self.end_state][self.end_state] = 1

        for state in self.state_nums():
            state_prob = sum(posat[state])
            trans[0][state] = posat[state][1]
            trans[state][-1] = posat[state][-1] / state_prob
            for oth in self.state_nums():
                trans[state][oth] = sum(pot[state][oth]) / state_prob

        # new emission probabilities
        emit = [[0 for j in range(len(self.vocab))]
                   for i in range(len(self.states))]
        for state in self.state_nums():
            for output in range(1, len(self.vocab) + 1):
                n = sum(posat[state][t] for t in range(1, len(observations)+1)
                                              if observed[t] == output)
                emit[state][output-1] = n / sum(posat[state])

        trained = HiddenMarkovModel(self.states, trans, emit, self.vocab)
        return (trained, posat, pot) if return_probs else trained

# ======================
# = reading from files =
# ======================

def normalize(string):
    if '#' in string:
        string = string[:string.index('#')]
    return string.strip()

def make_hmm_from_file(f):
    def nextline():
        line = f.readline()
        if line == '': # EOF
            return None
        else:
            return normalize(line) or nextline()

    n = int(nextline())
    states = [nextline() for i in range(n)] # <3 list comprehension abuse

    num_vocab = int(nextline())
    vocab = [nextline() for i in range(num_vocab)]

    transitions = [[float(x) for x in nextline().split()] for i in range(n)]
    emissions   = [[float(x) for x in nextline().split()] for i in range(n)]

    assert nextline() is None
    return HiddenMarkovModel(states, transitions, emissions, vocab)

def read_observations_from_file(f):
    return filter(lambda x: x, [normalize(line) for line in f.readlines()])

# =========
# = tests =
# =========

import unittest
class TestHMM(unittest.TestCase):
    def setUp(self):
        # it's complicated to pass args to a testcase, so just use globals
        self.hmm = make_hmm_from_file(file(HMM_FILENAME))
        self.obs = read_observations_from_file(file(OBS_FILENAME))

    def test_forward(self):
        prob, trellis = self.hmm.forward_prob(self.obs, True)
        self.assertAlmostEqual(prob,           9.1276e-19, 21)
        self.assertAlmostEqual(trellis[1][1],  0.1,        4)
        self.assertAlmostEqual(trellis[1][3],  0.00135,    5)
        self.assertAlmostEqual(trellis[1][6],  8.71549e-5, 9)
        self.assertAlmostEqual(trellis[1][13], 5.70827e-9, 9)
        self.assertAlmostEqual(trellis[1][20], 1.3157e-10, 14)
        self.assertAlmostEqual(trellis[1][27], 3.1912e-14, 13)
        self.assertAlmostEqual(trellis[1][33], 2.0498e-18, 22)
        self.assertAlmostEqual(trellis[2][1],  0.1,        4)
        self.assertAlmostEqual(trellis[2][3],  0.03591,    5)
        self.assertAlmostEqual(trellis[2][6],  5.30337e-4, 8)
        self.assertAlmostEqual(trellis[2][13], 1.37864e-7, 11)
        self.assertAlmostEqual(trellis[2][20], 2.7819e-12, 15)
        self.assertAlmostEqual(trellis[2][27], 4.6599e-15, 18)
        self.assertAlmostEqual(trellis[2][33], 7.0777e-18, 22)

    def test_backward(self):
        prob, trellis = self.hmm.backward_prob(self.obs, True)
        self.assertAlmostEqual(prob,           9.1276e-19, 21)
        self.assertAlmostEqual(trellis[1][1],  1.1780e-18, 22)
        self.assertAlmostEqual(trellis[1][3],  7.2496e-18, 22)
        self.assertAlmostEqual(trellis[1][6],  3.3422e-16, 20)
        self.assertAlmostEqual(trellis[1][13], 3.5380e-11, 15)
        self.assertAlmostEqual(trellis[1][20], 6.77837e-9, 14)
        self.assertAlmostEqual(trellis[1][27], 1.44877e-5, 10)
        self.assertAlmostEqual(trellis[1][33], 0.1,        4)
        self.assertAlmostEqual(trellis[2][1],  7.9496e-18, 22)
        self.assertAlmostEqual(trellis[2][3],  2.5145e-17, 21)
        self.assertAlmostEqual(trellis[2][6],  1.6662e-15, 19)
        self.assertAlmostEqual(trellis[2][13], 5.1558e-12, 16)
        self.assertAlmostEqual(trellis[2][20], 7.52345e-9, 14)
        self.assertAlmostEqual(trellis[2][27], 9.66609e-5, 9)
        self.assertAlmostEqual(trellis[2][33], 0.1,        4)

    def test_viterbi(self):
        path, trellis = self.hmm.viterbi_sequence(self.obs, True)
        self.assertEqual(path, [0] + [2]*13 + [1]*14 + [2]*6 + [3])
        self.assertAlmostEqual(trellis[1][1] [0],  0.1,      4)
        self.assertAlmostEqual(trellis[1][6] [0],  5.62e-05, 7)
        self.assertAlmostEqual(trellis[1][7] [0],  4.50e-06, 8)
        self.assertAlmostEqual(trellis[1][16][0], 1.99e-09, 11)
        self.assertAlmostEqual(trellis[1][17][0], 3.18e-10, 12)
        self.assertAlmostEqual(trellis[1][23][0], 4.00e-13, 15)
        self.assertAlmostEqual(trellis[1][25][0], 1.26e-13, 15)
        self.assertAlmostEqual(trellis[1][29][0], 7.20e-17, 19)
        self.assertAlmostEqual(trellis[1][30][0], 1.15e-17, 19)
        self.assertAlmostEqual(trellis[1][32][0], 7.90e-19, 21)
        self.assertAlmostEqual(trellis[1][33][0], 1.26e-19, 21)
        self.assertAlmostEqual(trellis[2][ 1][0], 0.1,      4)
        self.assertAlmostEqual(trellis[2][ 4][0], 0.00502,  5)
        self.assertAlmostEqual(trellis[2][ 6][0], 0.00045,  5)
        self.assertAlmostEqual(trellis[2][12][0], 1.62e-07, 9)
        self.assertAlmostEqual(trellis[2][18][0], 3.18e-12, 14)
        self.assertAlmostEqual(trellis[2][19][0], 1.78e-12, 14)
        self.assertAlmostEqual(trellis[2][23][0], 5.00e-14, 16)
        self.assertAlmostEqual(trellis[2][28][0], 7.87e-16, 18)
        self.assertAlmostEqual(trellis[2][29][0], 4.41e-16, 18)
        self.assertAlmostEqual(trellis[2][30][0], 7.06e-17, 19)
        self.assertAlmostEqual(trellis[2][33][0], 1.01e-18, 20)

    def test_learning_probs(self):
        trained, gamma, xi = self.hmm.train_on_obs(self.obs, True)

        self.assertAlmostEqual(gamma[1][1],  0.129, 3)
        self.assertAlmostEqual(gamma[1][3],  0.011, 3)
        self.assertAlmostEqual(gamma[1][7],  0.022, 3)
        self.assertAlmostEqual(gamma[1][14], 0.887, 3)
        self.assertAlmostEqual(gamma[1][18], 0.994, 3)
        self.assertAlmostEqual(gamma[1][23], 0.961, 3)
        self.assertAlmostEqual(gamma[1][27], 0.507, 3)
        self.assertAlmostEqual(gamma[1][33], 0.225, 3)
        self.assertAlmostEqual(gamma[2][1],  0.871, 3)
        self.assertAlmostEqual(gamma[2][3],  0.989, 3)
        self.assertAlmostEqual(gamma[2][7],  0.978, 3)
        self.assertAlmostEqual(gamma[2][14], 0.113, 3)
        self.assertAlmostEqual(gamma[2][18], 0.006, 3)
        self.assertAlmostEqual(gamma[2][23], 0.039, 3)
        self.assertAlmostEqual(gamma[2][27], 0.493, 3)
        self.assertAlmostEqual(gamma[2][33], 0.775, 3)

        self.assertAlmostEqual(xi[1][1][1],  0.021, 3)
        self.assertAlmostEqual(xi[1][1][12], 0.128, 3)
        self.assertAlmostEqual(xi[1][1][32], 0.13,  3)
        self.assertAlmostEqual(xi[2][1][1],  0.003, 3)
        self.assertAlmostEqual(xi[2][1][22], 0.017, 3)
        self.assertAlmostEqual(xi[2][1][32], 0.095, 3)
        self.assertAlmostEqual(xi[1][2][4],  0.02,  3)
        self.assertAlmostEqual(xi[1][2][16], 0.018, 3)
        self.assertAlmostEqual(xi[1][2][29], 0.010, 3)
        self.assertAlmostEqual(xi[2][2][2],  0.972, 3)
        self.assertAlmostEqual(xi[2][2][12], 0.762, 3)
        self.assertAlmostEqual(xi[2][2][28], 0.907, 3)

    def test_learning_results(self):
        trained = self.hmm.train_on_obs(self.obs)

        tr = trained.transition
        self.assertAlmostEqual(tr(0, 0), 0,      5)
        self.assertAlmostEqual(tr(0, 1), 0.1291, 4)
        self.assertAlmostEqual(tr(0, 2), 0.8709, 4)
        self.assertAlmostEqual(tr(0, 3), 0,      4)
        self.assertAlmostEqual(tr(1, 0), 0,      5)
        self.assertAlmostEqual(tr(1, 1), 0.8757, 4)
        self.assertAlmostEqual(tr(1, 2), 0.1090, 4)
        self.assertAlmostEqual(tr(1, 3), 0.0153, 4)
        self.assertAlmostEqual(tr(2, 0), 0,      5)
        self.assertAlmostEqual(tr(2, 1), 0.0925, 4)
        self.assertAlmostEqual(tr(2, 2), 0.8652, 4)
        self.assertAlmostEqual(tr(2, 3), 0.0423, 4)
        self.assertAlmostEqual(tr(3, 0), 0,      5)
        self.assertAlmostEqual(tr(3, 1), 0,      4)
        self.assertAlmostEqual(tr(3, 2), 0,      4)
        self.assertAlmostEqual(tr(3, 3), 1,      4)

        em = trained.emission
        self.assertAlmostEqual(em(0, 1), 0,      4)
        self.assertAlmostEqual(em(0, 2), 0,      4)
        self.assertAlmostEqual(em(0, 3), 0,      4)
        self.assertAlmostEqual(em(1, 1), 0.6765, 4)
        self.assertAlmostEqual(em(1, 2), 0.2188, 4)
        self.assertAlmostEqual(em(1, 3), 0.1047, 4)
        self.assertAlmostEqual(em(2, 1), 0.0584, 4)
        self.assertAlmostEqual(em(2, 2), 0.4251, 4)
        self.assertAlmostEqual(em(2, 3), 0.5165, 4)
        self.assertAlmostEqual(em(3, 1), 0,      4)
        self.assertAlmostEqual(em(3, 2), 0,      4)
        self.assertAlmostEqual(em(3, 3), 0,      4)

        # train 9 more times
        for i in range(9):
            trained = trained.train_on_obs(self.obs)

        tr = trained.transition
        self.assertAlmostEqual(tr(0, 0), 0,      4)
        self.assertAlmostEqual(tr(0, 1), 0,      4)
        self.assertAlmostEqual(tr(0, 2), 1,      4)
        self.assertAlmostEqual(tr(0, 3), 0,      4)
        self.assertAlmostEqual(tr(1, 0), 0,      4)
        self.assertAlmostEqual(tr(1, 1), 0.9337, 4)
        self.assertAlmostEqual(tr(1, 2), 0.0663, 4)
        self.assertAlmostEqual(tr(1, 3), 0,      4)
        self.assertAlmostEqual(tr(2, 0), 0,      4)
        self.assertAlmostEqual(tr(2, 1), 0.0718, 4)
        self.assertAlmostEqual(tr(2, 2), 0.8650, 4)
        self.assertAlmostEqual(tr(2, 3), 0.0632, 4)
        self.assertAlmostEqual(tr(3, 0), 0,      4)
        self.assertAlmostEqual(tr(3, 1), 0,      4)
        self.assertAlmostEqual(tr(3, 2), 0,      4)
        self.assertAlmostEqual(tr(3, 3), 1,      4)

        em = trained.emission
        self.assertAlmostEqual(em(0, 1), 0,      4)
        self.assertAlmostEqual(em(0, 2), 0,      4)
        self.assertAlmostEqual(em(0, 3), 0,      4)
        self.assertAlmostEqual(em(1, 1), 0.6407, 4)
        self.assertAlmostEqual(em(1, 2), 0.1481, 4)
        self.assertAlmostEqual(em(1, 3), 0.2112, 4)
        self.assertAlmostEqual(em(2, 1), 0.00016,5)
        self.assertAlmostEqual(em(2, 2), 0.5341, 4)
        self.assertAlmostEqual(em(2, 3), 0.4657, 4)
        self.assertAlmostEqual(em(3, 1), 0,      4)
        self.assertAlmostEqual(em(3, 2), 0,      4)
        self.assertAlmostEqual(em(3, 3), 0,      4)

if __name__ == '__main__':
    import sys
    HMM_FILENAME = sys.argv[1] if len(sys.argv) >= 2 else 'example.hmm'
    OBS_FILENAME = sys.argv[2] if len(sys.argv) >= 3 else 'observations.txt'

    unittest.main()

rawDescription: Example of implementation of Baum-Welch

description: Example of implementation of Baum-Welch


code:
cursor.execute(date_query, [Code])
Dates = [datetime.strptime(r[0], "%Y-%m-%d") for r in cursor]

rawDescription: Correct way to fill a list from a SQL query using pyodbc


code:
input_ws = input_wb.sheet_by_name('StackOverflow')
output_ws = output_wb.get_sheet(0)

for rindex in range(0, input_ws.nrows):
   for cindex in range(0, input_ws.ncols):
       input_cell = input_ws.cell(rindex, cindex)
       if input_cell.value[ input_cell.value.rfind('.'): ] == 'pf':
           output_ws.write(rindex, cindex, input_cell.value, red_background)
       elif input_cell.value.find('deleted') >= 0:
           output_ws.write(rindex, cindex, input_cell.value, black_with_white_font)
       else:
           pass  # we don't need to modify it

rawDescription: Conditionally formatting text strings from Excel using python


code:
t = "".join(temp).split('#')
print(' '.join(t))

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

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


code: dview.execute("data = setup()")

rawDescription: Sharing queues with ipyparallel cluster

description: Sharing queues with ipyparallel cluster


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

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


code:
import numpy as np

def transform(A, func):
    try:
        return func(A)
    except TypeError:
        return np.vectorize(func)(A)

rawDescription: apply both vectorized and non-vectorized function on numpy array

description: Apply both vectorized and non-vectorized function on numpy array


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


code:
array
indices
out

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


rawDescription: java Runtime.exec python and thread in python not run

description: Java Runtime.exec python and thread in python not run


code:
# Safely open your file
with open('file.txt', 'r') as f:
   lines = f.readlines()

# Rearrange the lines according to the logic you are looking for
even_lines, odd_lines = lines[::2], lines[1::2]

# Safely open your files
with open('file1.txt','w') as f1:
   for odd_line in odd_lines:
       f1.write(odd_line)

with open('file2.txt','w') as f2:
   for even_line in even_lines:
       f2.write(even_line)

rawDescription: how to write alternate lines in text file to another text file in python?

description: Write alternate lines in text file to another text file in python


code:
result = [[]]
for i in range(N): result = permNums(_)

rawDescription: How to write a lot of nested 'for' loops (Recursively)

description: Write a lot of nested 'for' loops (Recursively)


code: game = Game(agents, display, self, catchExceptions=catchExceptions)

rawDescription: "self" as method attribute

description: "self" as method attribute


code:
t1.totalsecs += 10
t1.totalsecs

rawDescription: Python3/Classes/OOP/ How do you change a objects self values with a method?


code:
def EOQ(D,p,ck,ch):
    p,D = Partmaster
    Q = math.sqrt((2*D*ck)/(ch*p))
    return Q
ch=0.2
ck=5
df['Q'] = df.apply(lambda row: EOQ(row['D'], row['p'], ck, ch), axis=1)
df

rawDescription: How to apply a function on every row on a dataframe?

description: Apply a function on every row on a dataframe


rawDescription: Python-3.x SIMPLE XPath Library

description: Python-3.x SIMPLE XPath Library


code:
import numpy as np

board = np.empty((10,10), dtype = 'object')
board[:,:] = None
board[1:9, 1:9] = np.arange(64).reshape(8,8)
print(board)
# [[None None None None None None None None None None]
#  [None 0 1 2 3 4 5 6 7 None]
#  [None 8 9 10 11 12 13 14 15 None]
#  [None 16 17 18 19 20 21 22 23 None]
#  [None 24 25 26 27 28 29 30 31 None]
#  [None 32 33 34 35 36 37 38 39 None]
#  [None 40 41 42 43 44 45 46 47 None]
#  [None 48 49 50 51 52 53 54 55 None]
#  [None 56 57 58 59 60 61 62 63 None]
#  [None None None None None None None None None None]]

column = 1
row = 1
nw, n, ne, w, _, e, sw, s, se = (board[column-1:column+2, row-1:row+2]).ravel()
print(nw, n, ne, w, _, e, sw, s, se)
# (None, None, None, None, 0, 1, None, 8, 9)

rawDescription: Set none if numpy array index does not exist


code:
real = (np.bitwise_and(values, 0x0f) << 4).astype(np.int8) >> 4
imag = np.bitwise_and(values, 0xf0).astype(int) >> 4

rawDescription: how to load 4-bit data into numpy array

description: Load 4-bit data into numpy array


code:
from django.views.generic.detail import DetailView

class ArticleView(NeverCacheMixin, DetailView):
    template_name = "article_detail.html"
    queryset = Article.objects.articles()
    context_object_name = "article"

rawDescription: Ensuring django template is never cached by any browser


code: min_post_date = datetime.strptime(min_post_date.split()[0], "%Y-%m-%d").date()

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

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


code:
import pygame
import math
from itertools import cycle

# some simple vector helper functions, stolen from http://stackoverflow.com/a/4114962/142637
def magnitude(v):
    return math.sqrt(sum(v[i]*v[i] for i in range(len(v))))

def add(u, v):
    return [ u[i]+v[i] for i in range(len(u)) ]

def sub(u, v):
    return [ u[i]-v[i] for i in range(len(u)) ]

def dot(u, v):
    return sum(u[i]*v[i] for i in range(len(u)))

def normalize(v):
    vmag = magnitude(v)
    return [ v[i]/vmag  for i in range(len(v)) ]

screen = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

class Ball(object):
    def __init__(self, path):
        self.x, self.y = (0, 0)
        self.speed = 2.5
        self.color = (200, 200, 200)
        self.path = cycle(path)
        self.set_target(next(self.path))

    @property
    def pos(self):
        return self.x, self.y

    # for drawing, we need the position as tuple of ints
    # so lets create a helper property
    @property
    def int_pos(self):
        return map(int, self.pos)

    @property
    def target(self):
        return self.t_x, self.t_y

    @property
    def int_target(self):
        return map(int, self.target)

    def next_target(self):
        self.set_target(self.pos)
        self.set_target(next(self.path))

    def set_target(self, pos):
        self.t_x, self.t_y = pos

    def update(self):
        # if we won't move, don't calculate new vectors
        if self.int_pos == self.int_target:
            return self.next_target()

        target_vector = sub(self.target, self.pos)

        # a threshold to stop moving if the distance is to small.
        # it prevents a 'flickering' between two points
        if magnitude(target_vector) < 2:
            return self.next_target()

        # apply the balls's speed to the vector
        move_vector = [c * self.speed for c in normalize(target_vector)]

        # update position
        self.x, self.y = add(self.pos, move_vector)

    def draw(self):
        pygame.draw.circle(screen, self.color, self.int_pos, 4)

pygame.init()
quit = False

path = [(26, 43),
        (105, 110),
        (45, 225),
        (145, 295),
        (266, 211),
        (178, 134),
        (250, 56),
        (147, 12)]

path2 = [(26, 43),
         (105, 10),
         (45, 125),
         (150, 134),
         (150, 26),
         (107, 12)]

ball = Ball(path)
ball.speed = 1.9

ball2 = Ball(path2)
ball2.color = (200, 200, 0)

balls = [ball, ball2]

while not quit:
    quit = pygame.event.get(pygame.QUIT)
    pygame.event.poll()

    map(Ball.update, balls)

    screen.fill((0, 0, 0))

    map(Ball.draw, balls)

    pygame.display.flip()
    clock.tick(60)

rawDescription: Bouncing an image in a polygonal way with pygame


code:
def blocks(filename):
    title, block = '', None
    with open(filename) as fp:
        for line in fp:
            if '{' in line:
                block = line
            elif block is not None:
                block += line
            else:
                title = line
            if '}' in line:
                yield title, block
                title, block = '', None

rawDescription: Python - reading between parenthesis as a single 'block'


code:
from pygal.util import (
    is_major, truncate, reverse_text_len, get_texts_box, cut, rad, decorate)

rawDescription: How to monkey-patch an internal private function in a python package?

description: Monkey-patch an internal private function in a python package


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


code:
a = ['apple', 'orange']

d ={'apple': [2,4], 'carrot': [44,33], 'orange': [345,667]}

fo = open('test.csv','w')
fo.write(',\t'.join(a)+'\n')
for y in xrange(len(d[a[0]])):
    fo.write(',\t'.join([str(d[i][y]) for i in a])+'\n')

fo.close()

rawDescription: Get the dictionary values for every key in a list


code:
for row in grid:
    for item in row:
        print(item, end = " ")
    print()

rawDescription: How to traverse a 2D List in python

description: Traverse a 2D List in python


rawDescription: Trying to make a readable txt file from an np array, all printing on one line?


code:
import cgi
import hashlib
import time
import urllib
import base64
import datetime
import hmac

from django.conf import settings
from django.http import HttpResponseRedirect,HttpResponse,HttpResponseNotFound

rawDescription: How to redirect the url after logging into Facebook?

description: Redirect the url after logging into Facebook


code:
a, b = np.histogram(days,bins=5)
np.digitize(days, bins=b)

rawDescription: Assigning histogram bin to each data point


code:
answer = input("...")
if answer in ["take", "pickup"]:
    ...

rawDescription: How do i make an if statement if my answer includes something

description: Make an if statement if my answer includes something


code:
for handler in a_handlers:
    handler.write_message("message on A")

rawDescription: Keeping a list of websocket connections in Tornado


code: df

rawDescription: How to set current row item to previous if smaller, in Pandas?

description: Set current row item to previous if smaller, in Pandas


rawDescription: Multiply several matrices in numpy

description: Multiply several matrices in numpy


code:
import lxml.etree
etree = lxml.etree.fromstring('''
<data>
  <foo>foo text</foo>
  data text
    <bar>
      bar text
      <baz>text</baz>
      <baz>text</baz>
      bar text
    </bar>
   data text
</data>
''')

for text in etree.xpath("//text()[normalize-space()]"):
    parenttag = text.getparent().tag
    print(parenttag, text)

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

description: Get owner element of a text node


rawDescription: Converting String to List

description: Converting String to List


code:
itemA = random(1, 2, 3)
itemA.read('file.txt')
# now itemA.a is set to 4, itemA.b is set to 5 and item.c is set to 6

rawDescription: How do I replace variables in python class?

description: Replace variables in python class


code:
{'randlib': 'C://desktop//randlib.lib',
   'mylib': 'C://desktop//baseicmylib.lib'}

rawDescription: Python regular expression to match a path


code: not all(str(s).strip('-').replace('.','').isdigit() for s in a)

rawDescription: How to detect non-number of the list?

description: Detect non-number of the list


code:
gg = df.groupby("Location")
summ = pd.DataFrame({"Pop": gg.Location.count(),
                     "HH": gg.Address.nunique(),
                     "L4": gg.Score.apply(lambda x: (x < 4).sum())})
summ

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


code: a,b = map(list,zip(*y))

rawDescription: How to split a list of 2-tuples into two lists?

description: Split a list of 2-tuples into two lists


rawDescription: How can I update a variable to keep track of the current value minus the previous value in Python?

description: Update a variable to keep track of the current value minus the previous value in Python


rawDescription: Exclude matched string python re.findall


code:
import csv
import itertools

def is_end_of_record(line):
    return line.startswith('///')

class FieldClassifier(object):
    def __init__(self):
        self.field=''
    def __call__(self,row):
        if not row[0].isspace():
            self.field=row.split(' ',1)[0]
        return self.field

fields='NAME DESCRIPTION ORGANISM CLASS DBLINKS'.split()
with open('data','r') as f:
    for end_of_record, lines in itertools.groupby(f,is_end_of_record):
        if not end_of_record:
            classifier=FieldClassifier()
            record={}
            for fieldname, row in itertools.groupby(lines,classifier):
                record[fieldname]='; '.join(r.strip() for r in row)
            print('\t'.join(record.get(fieldname,'NA') for fieldname in fields))

rawDescription: from text file to csv using Python

description: From text file to csv using Python


code:
selItems = self.scene.selectedItems()
for item in selItems:
    obj_type = type(item)

rawDescription: pyqt. How do you know the type of object in QGraphicsScene?

description: Pyqt. How do you know the type of object in QGraphicsScene


code:
class MyClass(object):
    def __init__(self, attr1, attr2, **kwargs):
        self.attr1 = attr1
        self.attr2 = attr2

dict = {'attr1': 'value1', 'attr2': 'value2', 'extra_key': 'value3'}
new_instance = MyClass(**dict)

rawDescription: Is there any way to get class instance attributes without creating class instance?


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

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

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


code:
from twisted.internet import reactor
from twisted.web.client import Agent
from twisted.web.http_headers import Headers

agent = Agent(reactor)

d = agent.request(
    'GET',
    'http://baidu.com/',
    Headers({
            'User-Agent': ['Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0'],
            'Accept': ['text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'],
            'Accept-Language': ['en-US,en;q=0.5'],
            'Accept-Encoding': ['gzip, deflate'],
            'Connection': ['keep-alive']
        }),
    None)

def Response(null):
    print('Response received')

def Shutdown(null):
    print('Shutting down the reactor now')
    reactor.stop()

d.addCallback(Response)     # exec Response() after request is rcvd
d.addBoth(Shutdown)         # shut down after response rcvd
reactor.run()

rawDescription: how to send raw http header

description: Send raw http header


email: <email address for id 1>

phone: <phone number for id 1>

email: <email address for id 2>

phone: <phone number for id 2>

rawDescription: Set-like alternative for yaml files

description: Set-like alternative for yaml files


code:
pmName = input('Enter module name:')
pm = __import__(pmName)
print(dir(pm))

rawDescription: Python: How to import other Python files


rawDescription: How to assign columns of data to variables

description: Assign columns of data to variables


rawDescription: Python: Cumulative insertion of values in a sparse matrix (lil_matrix) due to repeated indices


code:
header
xbo

rawDescription: Converting CNC drillings from old to new system (using Python)


rawDescription: Write nested lists column wise to CSV

description: Write nested lists column wise to CSV


code:
mask = np.ones(df.shape,dtype='bool')
mask[np.triu_indices(len(df))] = False
mask

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


code:
content = '''
<root>
<Iteration>
  <Iteration_hit>Elememt1 Element1
    abc1 hit 1
  </Iteration_hit>
</Iteration>
<Iteration>
  <Iteration_hit>Elememt2 Element2
    abc2 hit 1
  </Iteration_hit>
</Iteration>
<Iteration>
  <Iteration_hit>Elememt3 Element3
    abc3 hit 1
  </Iteration_hit>
</Iteration>
<Iteration>
  <Iteration_hit>Elememt4 Element4
    abc4 hit 1
  </Iteration_hit>
</Iteration>
</root>
'''

from lxml import etree

tree = etree.XML(content)
target_elements = tree.xpath('//Iteration_hit[contains(., "Element2") or contains(., "Element4")]')

for element in target_elements:
    print(etree.tostring(element))

rawDescription: Extract from a match to next match if patten found in between


code:
import scrapy
from scrapy.exceptions import CloseSpider
from scrapy.spider import Spider
from scrapy.http import Request

class JabongItem(scrapy.Item):
    # define the fields for your item here like:
    name = scrapy.Field()
    link = scrapy.Field()
    sku = scrapy.Field()
    brand = scrapy.Field()
    img = scrapy.Field()

class JabongSpider(scrapy.Spider):
    name = "jabong"
    allowed_domains = ["jabong.com"]
    start_urls = ["http://www.jabong.com/women/clothing/kurtas-suit-sets/kurtas-kurtis/?page=1"]
    page_index = 1

    BASE_URL = 'http://www.jabong.com/'

    def parse(self, response):
        products = response.xpath("//li[@data-url]")
        if products:
            for product in products:
                link = product.xpath('@data-url').extract()
                link = self.BASE_URL + link[0] if link else ''
                sku = product.xpath('@data-sku').extract()
                sku = sku[0].strip() if sku else 'n/a'
                brand = product.xpath('.//span[contains(@class, "qa-brandName")]/text()').extract()
                brand = brand[0].strip() if brand else 'n/a'
                img = product.xpath('.//img[contains(@class, "itm-img")]/@src').extract()
                img = img[0].strip() if img else 'n/a'
                item = JabongItem()
                item['link'] = link
                item['sku'] = sku
                item['brand'] = brand
                item['img'] = img
                if link:
                    yield Request(url=link, callback=self.parse_page2, meta={'item': item})

        else:
            return

        self.page_index += 1
        yield Request(url="http://www.jabong.com/women/clothing/kurtas-suit-sets/kurtas-kurtis/?page=1%s" % (self.page_index + 1),
                          callback=self.parse, dont_filter=True)

    def parse_page2(self, response):
        item = response.meta['item']
        # add whatever extra details you want to item
        yield item

rawDescription: Scrapy Calling another Url

description: Scrapy Calling another Url


code:
for key, value in data.items():
    if key in ['foo_field', 'bar_field']:
        setattr(property_selected, key, value)

rawDescription: Iterating through a data list and edit an object's attribute based on the list


code:
class SkipFirstWriter(object):
    def __init__(self, f):
        self.f = f
        self.firstline = True
    def write(self, buf):
        if self.firstline:
            if '>' in buf:
                self.f.write(buf.partition('>')[2])
                self.firstline = False
        else:
            self.f.write(buf)
with open('foo.xml', 'w') as f:
    skipper = SkipFirstWriter(f)
    x.writexml(skipper)

rawDescription: Create xml file without prologue with python dom minidom


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

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


code: df

rawDescription: pandas - change values of second level index to display position within first level index

description: Pandas - change values of second level index to display position within first level index


code:
def add(a, b):
    return a + b

t = [5, 6]
print(add(*t))

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


code:
class Int:
    def __init__(self):
        self.value = ...
        setattr(self, "__add__", lambda other: self.value + other)

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


code:
round(number)
int(number)

rawDescription: Preventing auto scientific form for numbers in python3


code: from xml.etree import cElementTree as ET

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


code: getattr(obj, func)()

rawDescription: Python append function as dot notation

description: Python append function as dot notation


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


code:
from django import template

register = template.Library()

@register.filter(name='replace_linebr')
def replace_linebr(value):
    """Replaces all values of line break from the given string with a line space."""
    return value.replace("<br />", ' ')

rawDescription: django custom filter - replace line break with line space

description: Django custom filter - replace line break with line space


code:
import datetime
import random

def generate_data(sz_outer, sz_inner):
    res = {}
    for n in range(sz_outer):
        res[n] = []
        for m in range(sz_inner):
            date = datetime.date(
                year=random.sample(range(2010, 2015), 1)[0],
                month=random.sample(range(1, 13), 1)[0],
                day=random.sample(range(1, 29), 1)[0],
                )
            bits = random.sample(range(10), 1)[0]
            res[n].append({'date': date, 'bits': bits})
    return res

rawDescription: Max value in Dictionary of List of Dictionaries in Python


code:
data
cdf = concat(data, axis=1)
cdf

rawDescription: indexing several csv files with pandas from records?

description: Indexing several csv files with pandas from records


code:
l = [u'[190215]', u'[190216, 190217]']
l = [item for value in l for item in literal_eval(value)]
l
l = [u'[190215]', u'[190216, 190217]', [12, 12]]
l = [item for value in l for item in literal_eval(value)]

rawDescription: Remove 'u' from a python list

description: Remove 'u' from a python list


code:
# old.py - Make sure it's chmod +x
#!/usr/bin/python
import time

time.sleep(48)

rawDescription: How to execfile an independent .py and continue on to next line of code in the original .py?

description: Execfile an independent .py and continue on to next line of code in the original .py


code:
def findMatchIndexes(myList, element):
    return [i for i, x in enumerate(myList) if x == element]

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


code:
from datetime import datetime, timedelta

dt = datetime.min + timedelta(days=time - 1)

rawDescription: Create a datetime.time object just from seconds


code:
old_string = "foo bar foo"
i = old_string.rfind("foo")
new_string = old_string[:i] + "baz" + old_string[i + len("foo"):]

rawDescription: Python: replacing a repeating string once in text


rawDescription: BeautifulSoup Python is stripping my HTML comments


code:
def printarray(a):
    print(str(a).replace('None','_'))

rawDescription: numpy - pretty printing

description: Numpy - pretty printing


code: runners = fields.ToManyField("RunnersResource", "runners", full=True, null=True, blank=True)

rawDescription: tastypie related resources group by

description: Tastypie related resources group by


code: gt5 = lambda x: x > 5

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


code:
def _loadComplete(self):
    page = self.page()
    doc = page.currentFrame().documentElement()
    link = doc.findFirst('#link-signup')
    if link and not link.isNull():
        link.evaluateJavaScript("""
            var e = document.createEvent('MouseEvents');
            e.initEvent('click', true, true);
            this.dispatchEvent(e);
        """)

rawDescription: PyQt4 does not redirect me to the next page


rawDescription: How to combine the closest non_overlapping ranges?

description: Combine the closest non_overlapping ranges


code:
from django import forms
from app.models import Profile

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        widgets = {
            'categories': forms.CheckboxSelectMultiple,
        }

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


code:
a = datetime.datetime.now()
b = datetime.datetime.now()
c = b - a
c
c.days
c.seconds
c.microseconds

rawDescription: Python speed testing - Time Difference - milliseconds


code: values.items() # or list(values.items()) in Python3

rawDescription: Appending to a list of lists in Python, with conditions


code:
with RotateFileOpener('/var/log/gps', prepend='gps_data-', append='.gpx') as logger:
    while True:
        log = get_gpx_data()
        logger.write(log)

rawDescription: Create log file one every day in python

description: Create log file one every day in python


code:
import re
pat = re.compile(r'(cat|dog|cow)')
pat.findall("The cat and cow sat on the dog's catalog of doggerel")
# ['cat', 'cow', 'dog', 'cat', 'dog']

rawDescription: How to iterate with index elegantly in Python, for string-matching a list of substrings inside text?

description: Iterate with index elegantly in Python, for string-matching a list of substrings inside text


code:
for i in range(1,10):
        globals()['r'+str(i)] = "foo"

r1
r2

rawDescription: Naming a variable on the basis of index in loop


code:
import re
a = " Kingston-(USB) = 1GB, 2GB, 32GB . . . "
re.sub(r'[^a-z0-9]', '', a.lower())

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

description: Remove all symbols and spaces from the String


code: not indexTag

rawDescription: What would be a compact way to return 1 if a variable is 0 and to return 0 if it is 1?


code:
file=open(filename,'r')
for i in range(6):file.readline()   #skipping 6 lines
items=file.readline().split()

rawDescription: How to Open, Read, and Write files in Python 2.7 -- Converting code from fortran 90 to Python


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

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


code:
# runtests.py
import logging
import pytest

logging.basicConfig(filename='config_check.log', level=logging.INFO)
logging.info('start')
pytest.main()
logging.info('done')

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

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


rawDescription: Understanding a line of code

description: Understanding a line of code


code:
# Generate new underlying array
first = np.repeat(df['first'].values, 3)[:, np.newaxis]
new_vals = df.values[:, 1:].reshape(18,10)
new_vals = np.hstack((first, new_vals))
# Create new df
m = pd.MultiIndex.from_product((df.index, range(1,4)), names=['ID', 'MesNum'])
pd.DataFrame(new_vals, index=m, columns=['first'] + list(range(1,11)))

rawDescription: Moving columns down and replicating keys in pandas


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

N = 300
dates = pd.date_range('2000-1-1', periods=N, freq='D')
x = np.linspace(0, 2*np.pi, N)
data = pd.DataFrame({'A': np.sin(x), 'B': np.cos(x),
               'Date': dates})
plt.plot_date(data['Date'], data['A'], '-')
plt.plot_date(data['Date'], data['B'], '-')

d = data['Date'].values
plt.fill_between(d, data['A'], data['B'],
                where=data['A'] >= data['B'],
                facecolor='green', alpha=0.2, interpolate=True)
plt.xticks(rotation=25)
plt.show()

rawDescription: Pandas and Matplotlib - fill_between() vs datetime64


code:
from nltk import sent_tokenize, word_tokenize, pos_tag
from nltk.tree import Tree
from nltk import batch_ne_chunk as bnc
chunked_text = [[bnc(pos_tag(word_tokenize(j)) for j in sent_tokenize(i))] for i in text]

rawDescription: Ways to grab Person and Organization Names from text using python


code: df = df.drop('column_name', 1)

rawDescription: Delete column from pandas DataFrame

description: Delete column from pandas DataFrame


code:
import cffi
ffi = cffi.FFI()

data = ffi.buffer(ffi.new("double[20]")) # allocate some space to store data
         # alternatively, this could have been returned by a function wrapped
         # using ffi

# now use the Cython file to test the buffer interface
import pyximport; pyximport.install()
import buf_test

# next line DOESN'T WORK - complains about the data type of the buffer
# buf_test.test_double(obj.data)

buf_test.test_uchar(obj.data) # works fine - but interprets as unsigned char

# we can also use casts and the Python
# standard memoryview object to get it as a double array
buf_test.test_double(memoryview(obj.data).cast('d'))

rawDescription: Buffer protocol using CFFI

description: Buffer protocol using CFFI


rawDescription: groupby() giving an empty list

description: Groupby() giving an empty list


code:
if __name__ == "__main__":
    sys.exit(main())

rawDescription: Use Python's `timeit` from a program but functioning the same way as the command line?