code:
import re
from itertools import izip_longest
from xml.sax.saxutils import escape  # '<' -> '<'

# assumptions:
#   doc = *( start_tag / end_tag / text )
#   start_tag = '<' name *attr [ '/' ] '>'
#   end_tag = '<' '/' name '>'
ws = r'[ \t\r\n]*'  # allow ws between any token
name = '[a-zA-Z]+'  # note: expand if necessary but the stricter the better
attr = '{name} {ws} = {ws} "[^"]*"'  # note: fragile against missing '"'; no "'"
start_tag = '< {ws} {name} {ws} (?:{attr} {ws})* /? {ws} >'
end_tag = '{ws}'.join(['<', '/', '{name}', '>'])
tag = '{start_tag} | {end_tag}'

assert '{{' not in tag
while '{' in tag: # unwrap definitions
    tag = tag.format(**vars())

tag_regex = re.compile('(%s)' % tag, flags=re.VERBOSE)

# escape &, <, > in the text
iters = [iter(tag_regex.split(invalid_xml))] * 2
pairs = izip_longest(*iters, fillvalue='')  # iterate 2 items at a time
print(''.join(escape(text) + tag for text, tag in pairs))

rawDescription: Matching patterns in Python

description: Matching patterns in Python


code: json.loads(t, object_hook=load_object)

rawDescription: Creating a list from lists inside a namedtuple


rawDescription: Python - Unicode to ASCII conversion

description: Python - Unicode to ASCII conversion


code:
from bs4 import BeautifulSoup

doc = BeautifulSoup("", "html.parser")
body = BeautifulSoup("

1

2

", "html.parser")

doc.html.body.append(body.ol)

print(doc.html.body.ol)

rawDescription: Accessing Appended item with Beautiful Soup


code:
def methodB(self, flag=False, execute_anyways=False):
    if not flag and not execute_anyways:
        return #Note that while calling, you would be sending True, or False. If flag=None, it would execute.
    self.method1()
    self.method2()

def methodA(self):
    self.methodB(flag=doc['flag'])

rawDescription: Reusing code from a method which performs a flag check


code:
import sys
from bs4 import BeautifulSoup

sys.setrecursionlimit(2000)

soup = None
def soupSetup(file_name, encode_type):
    global soup
    soup = BeautifulSoup(open("C:\\Python32\\html\\"+file_name+".html", encoding=encode_type))

rawDescription: Using a script to set variables in IDLE

description: Using a script to set variables in IDLE


code:
results = [twoeachside(s, 'not') for s in review_list]
results = [[s] if s else [] for s in results]

rawDescription: Python: finding the two words following a key word


code:
a = numpy.array(['apples', 'foobar', 'cowboy'])
a[2] = 'bananas'
a

rawDescription: python numpy array of arbitrary length strings

description: Python numpy array of arbitrary length strings


code:
word_set = set("NEW Fujifilm 16MP 5x".split())
character_set = set("NEW Fujifilm 16MP 5x")
word_set
character_set

rawDescription: Python: Jaccard Distance using word intersection but not character intersection


code:
class SomeObject:
    def __init__(self, reference):
         self.reference_=reference
    #code to access parents MyVar
    self.reference_.MyVar=5

class MyClass:
    MyVar = 3

    MyObject = SomeObject(self)

rawDescription: Access parents variables in Python

description: Access parents variables in Python


rawDescription: Reducing pandas series with multiple nan values to a set gives multiple nan values


code:
with open(the_filename, 'wb') as f:
    pickle.dump(my_list, f)

rawDescription: Python 2.7 - Write and read a list from file


code:
from collections import defaultdict
from itertools import zip_longest

with open('file1', 'r') as f1, open('file2', 'r') as f2, \
                               open('common', 'w') as fout:
    remainder = {}
    markers = defaultdict(list)
    for line in f1:
        fields = line.split()
        markers[fields[1]].append(fields[0])
        remainder[fields[1]] = fields[2:]

    contigs = defaultdict(list)
    for line in f2:
        fields = line.split()
        contigs[fields[1]].append(fields[0])
        remainder[fields[1]] = fields[2:]

    print(remainder)
    transcripts = sorted(set(markers.keys()) | set(contigs.keys()))
    for transcript in transcripts:
        rest = remainder[transcript]
        zipped = zip_longest(markers[transcript], contigs[transcript],
                             fillvalue='0')
        for marker, contig in zipped:
            print(transcript, marker, contig, *rest, sep='\t')

rawDescription: Find common elements in two files and unite them in one file


rawDescription: How can i grab the matched regex pattern in python

description: Grab the matched regex pattern in python


code:
lst = [1, 3, 4, 5, 6, 7, 8]
for i, val in enumerate(lst):
    if i < 2:
        continue
    print(val)

rawDescription: For count from 2

description: For count from 2


code:
d = '''

    Command get executed successfully. server.jvm.memory.maxheapsize-count-count = 518979584
'''
d
import re
line = re.compile(r'\d+\n')
rline = "".join(line.findall(d))
rline
line.findall(d)
line.findall(d)[0].strip() # strip() used to remove newline character - may not be needed

rawDescription: Python Regex to find last occurence of digit


code:
import ping, time

def found():
    print("FOUND INTERNET! :)")
def lost():
    print("LOST INTERNET! :(")

def wait_and_notify_connection(found, lost, already_found=False):
    while True:
        # Ping Google DNS Server (99.999% Uptime)
        if ping.do_one('8.8.8.8', timeout=2, psize=64) is not None:
            if not already_found:
                found()
                already_found = True
        else:
            if already_found:
                lost()
                already_found = False
        time.sleep(1)

wait_and_notify_connection(found, lost)

rawDescription: python call function when internet is found

description: Python call function when internet is found


code:
l = range(5)
l.__iter__()
# <listiterator object at 0x1004e4fd0>
it = l.__iter__()
it.next()
# 0
it.next()
# 1
it.next()
# 2

rawDescription: find the docs page for function "in"

description: Find the docs page for function "in"


code:
import argparse
import os
import subprocess

content = r'''\documentclass{article}
\begin{document}
... P \& B
\textbf{\huge %(school)s \\}
\vspace{1cm}
\textbf{\Large %(title)s \\}
...
\end{document}
'''

parser = argparse.ArgumentParser()
parser.add_argument('-c', '--course')
parser.add_argument('-t', '--title')
parser.add_argument('-n', '--name',)
parser.add_argument('-s', '--school', default='My U')

args = parser.parse_args()

with open('cover.tex','w') as f:
    f.write(content%args.__dict__)

cmd = ['pdflatex', '-interaction', 'nonstopmode', 'cover.tex']
proc = subprocess.Popen(cmd)
proc.communicate()

retcode = proc.returncode
if not retcode == 0:
    os.unlink('cover.pdf')
    raise ValueError('Error {} executing command: {}'.format(retcode, ' '.join(cmd)))

os.unlink('cover.tex')
os.unlink('cover.log')

rawDescription: Generating pdf-latex with python script

description: Generating pdf-latex with python script


rawDescription: Searching logs, outputting lines before and after match


code:
s = create_session(autocommit=False, autoflush=False)
u = User()
u.name = u'Cheezo'
s.add(u)
s.flush()

rawDescription: In Sqlalchemy, if i add an object using session.add() and flush it, session.query() does not give that object, why?


code:
a_foo1 = Foo()
a_foo2 = Foo()
a_foo1.spam
a_foo2.spam
Foo.spam = 3
a_foo1.spam
a_foo2.spam
a_foo1.spam = 5
a_foo1.spam
a_foo2.spam
Foo.spam = 2
a_foo1.spam
a_foo2.spam

rawDescription: which to use: class variables versus classmethod?

description: Which to use: class variables versus classmethod


code:
from django import forms

class DocumentForm(forms.Form):
    docfile = forms.FileField(label='Select a file')

rawDescription: Django Upload Image can not find uploaded image


code:
import itertools
import operator

def make_spans(data, key):
    groups = itertools.groupby(data, operator.itemgetter(key))
    return [{'colspan': len(list(items)), key: value} for value, items in groups]

rawDescription: How to strip a list of tuple with python?

description: Strip a list of tuple with python


code:
from scipy.interpolate import griddata

gridpoints = data.reshape(width*height, 2)
d = {(x, y): data_to_color(x, y) for x, y in gridpoints if not random.randint(0, 1000)}

len(d)
#>>> 142

rawDescription: Plotting sort of a heatmap whose colors are a result of a function x,y -> r,g,b


code: (r"^$", redirect_to, {"url": "myapp/title/"}),

rawDescription: Relative redirect in Django

description: Relative redirect in Django


code:
import re
text = '{text1} {stringA} {text2} {stringB} {text3}'
re.sub(r'(stringA)(.*)(stringB)', r'\3\2\1', text)

rawDescription: Using Python to substitute or swap substrings in a file


code: [range(i, i + 10) for i in range(1, 102, 10)]

rawDescription: How to generate groups of 10 consecutive numbers in a list?

description: Generate groups of 10 consecutive numbers in a list


code:
image_counts = [16, 2, 14]
total_images = sum(image_counts)

proportions = [1.0 * i / total_images for i in image_counts]
counts = [int(15 * i) for i in proportions]
if sum(counts) < 15:
    counts[counts.index(min(counts))] += 1

rawDescription: Taking proportional samples, no bad sampling


code:
class ScrapySpider(Spider):
    name = 'live_function'

    def start_requests(self):
        yield Request('some url', callback=self.non_stop_function)

    def non_stop_function(self, response):

        parse_and_pause = Deferred()  # changed
        parse_and_pause.addCallback(self.second_parse_function) # changed
        parse_and_pause.addCallback(pause, seconds=10)  # changed

        for url in ['url1', 'url2', 'url3', 'more urls']:
            yield Request(url, callback=parse_and_pause)  # changed

        yield Request('some url', callback=self.non_stop_function)  # Call itself

    def second_parse_function(self, response):
        pass

rawDescription: Scrapy: non-blocking pause

description: Scrapy: non-blocking pause


code:
def f(x, gs_wdth = 1., mean=0.):
    return 1./numpy.sqrt(2*numpy.pi)*gs_wdth * numpy.exp(-(x-mean)**2/(2*gs_wdth**2))

rawDescription: Calculating new entries in array based on entries from another array in python


code:
nrows, ncols = M.shape
C = M.T.reshape(1, ncols, 1, nrows) * M.T.conj().reshape(ncols, 1, nrows, 1)

rawDescription: Efficient way to fill up a 4d array from entries of a product of two matrices


rawDescription: python functions, math between csv files

description: Python functions, math between csv files


code: filter(lambda x: x > 5, iterable)

rawDescription: Regarding the use of filter()

description: Regarding the use of filter()


rawDescription: How to get error estimates for fit parameters in scipy.stats.gamma.fit?

description: Get error estimates for fit parameters in scipy.stats.gamma.fit


code:
def addClientButtonClicked(self, edit, validator):
    state, pos = validator.validate(edit.text(), 0)
    print(state == QtGui.QValidator.Acceptable)

rawDescription: Checking QValidator's state

description: Checking QValidator's state


code:
from collections import OrderedDict
d= OrderedDict()

import fileinput
with open("joined.txt","w") as f:
    for line in fileinput.input(["file1.txt","file2.txt","file3.txt","file4.txt"]):
        spl = line.strip().split(",")
        d.setdefault(spl[0],[])
        d[spl[0]] += spl[1:]
    for k,v in d.items():
        f.write("{} {}\n".format(k,",".join(v)))

rawDescription: Python side-by-side merge of comma-delimited text files


code:
total_days = df.groupby(['JOB', 'NAME', 'YEAR'])[['DAYS']].sum()
total_days.columns = ['SUM(DAYS)']

#                   SUM(DAYS)
# JOB    NAME YEAR
# Employ Mary 2013          5
#             2014         20
# Worker Bob  2013          3
#             2014         29

result = result.join(total_days)
del result['DAYS']
#                   MONTH  SALARY  SUM(DAYS)
# JOB    NAME YEAR
# Employ Mary 2013     12      23          5
#             2014      1     200         20
#             2014      2     190         20
# Worker Bob  2013     12      17          3
#             2014      1     210         29
#             2014      2      80         29

rawDescription: Pandas multiple index with multiple aggregate functions


code: [my_list[i:i + chunk_size] for i in range(0, len(my_list), chunk_size)]

rawDescription: Iterate an iterator by chunks (of n) in Python?


rawDescription: How to parse this string of data structure with pyparsing

description: Parse this string of data structure with pyparsing


code:
# ... create iterator
rows = list(iterator)

# ... use rows - contains all values

rawDescription: Python Iterators from CSV module...help me understand how to use this, please?


code: #!/bin/sh -c '`dirname $0`/python2.7 $0'

rawDescription: Relative shebang: How to write an executable script running portable interpreter which comes with it


field: BBB

field: aaa

field: AAA

rawDescription: Serializing MongoDB find() return into non-anonymous JSON array, using PyMongo


code:
import datetime

def value_to_string(input):

    # String
    if isinstance(input, basestring):
        return 'string'

    # BigDecimal
    if isinstance(input, int):
        return 'int'

    # Array
    if isinstance(input, list):
        return 'list'

    # Map
    if isinstance(input, dict):
        return 'dictionary'

    # Date
    if isinstance(input, datetime.date):
        return 'date'

    # ...

rawDescription: How to convert the following scala code to python

description: Convert the following scala code to python


code:
for ip in tt:
    child = pexpect.spawn("telnet %s" % ip)
    ...

rawDescription: Python ingest of text file with IP's to do LAN network management


code:
from sklearn import datasets
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC
iris = datasets.load_iris()
X, y = iris.data, iris.target
clf = OneVsRestClassifier(SVC(kernel='linear', probability=True, class_weight='auto'))
clf.fit(X, y)
proba = clf.predict_proba(X)

rawDescription: Multiclass linear SVM in python that return probability


rawDescription: Is there a way in Python to index a list of containers (tuples, lists, dictionaries) by an element of a container?


code:
class MyEnum(Enum):
    apple = 1
    banana = 2
    walrus = 3

rawDescription: What's the cleanest way to set up an enumeration in Python?


rawDescription: Python string, find specific word, then copy the word after it


code: return b

rawDescription: Which Python Conditional Return Statement Is The Most Pythonic?


code: summarywrapper(monthNumber, data, np.sum, np.var)

rawDescription: Python: using variable as function name

description: Python: using variable as function name


code:
from heapq import merge
from itertools import groupby
from operator import itemgetter

def sorted_merge(*iterables):
    for key, group in groupby(merge(*iterables), itemgetter(0)):
        yield key, [pair[1] for pair in group]

rawDescription: Generator to merge sorted dictionary-like iterables


code: my_list = map(int, my_string.split(','))

rawDescription: python: unpacking a string to a list

description: Python: unpacking a string to a list


code:
class Test_SectionListModel(TestCase):
    """Tests the class SectionListModel."""

    def test_model_works_as_expected(self):
        """Tests if the expected rows are generated from a sample pws file
        content.
        """
        model = SectionListModel(SAMPLE_PASSWORDS_DICT)
        l = len(SAMPLE_PASSWORDS_DICT)
        self.assertEqual(model.rowCount(None), l)
        i = 0
        for section in SAMPLE_PASSWORDS_DICT.iterkeys():
            self.assertEqual(model.data(model.index(i)), section)
            i += 1

rawDescription: Unit and functional testing a PySide-based application?


code: lib = args.lib

rawDescription: python argparse, how to refer args by their name

description: Python argparse, how to refer args by their name


rawDescription: python incorrect rounding with decimals

description: Python incorrect rounding with decimals


code:
number_of_bins = 10
revbin = np.linspace(np.min(rev_count), np.max(rev_count)+1, number_of_bins+1)

rawDescription: Python: Binning one coordinate and averaging another based on these bins


code:
from itertools import product

all_combos = (
    ''.join([digit if i == pos else c for i, c in enumerate(letters)])
    for digit, pos in product('1234', range(len(letters))))

rawDescription: Change one character at a time in a string, don't change the string?


code:
def qcharToUnicode(s, p=re.compile(r"QChar\((0x[a-fA-F0-9]*)\)")):
    return p.sub(lambda m: '"' + chr(int(m.group(1),16)) + '"', s)

rawDescription: In Python, are function-level assignments evaluated at each function call?


code:
from proj.celery import app
from celery import Task

class Task1(Task):
    def __init__(self):
        self._x = 1.0

class Task2(Task):
    def __init__(self):
        self._x = 2.0

@app.task(base=Task1)
def add1(y):
    return add1._x + y

@app.task(base=Task2)
def add2(y):
    return add2._x + y

rawDescription: How can I set up Celery to call a custom worker initialization?

description: Set up Celery to call a custom worker initialization


code:
class RootFactory(object):

    __name__ = ''
    __parent__ = None
    __acl__ = [
        (Allow, '__ADMIN__', ALL_PERMISSIONS),
        (Allow, 'creator',  ('VIEWER', 'ANALYST', 'MANAGER', 'CREATOR')),
        (Allow, 'manager',  ('VIEWER', 'ANALYST', 'MANAGER')),
        (Allow, 'analyst',  ('VIEWER', 'ANALYST')),
        (Allow, 'viewer',   ('VIEWER')),
        # (Allow, Everyone, NO_PERMISSION_REQUIRED)
        # It seems, here you want to deny access for all other users,
        # so you should use this:
        # (Deny, Everyone, ALL_PERMISSIONS)
        # or simply:
        DENY_ALL
    ]

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

    def __getitem__(self, name):
        return ChildObject(name, self)

rawDescription: Having trouble with "fine-grained" Pyramid ACL


code:
import json
json_txt = """
{
"ROLE_NAME": {
    "FOO": {
        "download_url": "http: //something.staging/12345/buzz.zip"
    },
    "BAR": {
        "download_url": "http: //something.staging/12345/fizz.zip"
    },
    "download_url": "http: //something.staging/12345/fizzbuzz.zip",
    "db_name": "somedb",
    "db_server": "dbserver.staging.dmz",
    "plugin": {
        "server_url": "http: //lab.staging.corp/server/"
    }
}
}
"""
data = json.loads(json_txt)

def fixup(adict, k, v):
    for key in adict.keys():
        if key == k:
            adict[key] = v
        elif type(adict[key]) is dict:
            fixup(adict[key], k, v)

import pprint
pprint.pprint( data )

fixup(data, 'download_url', 'XXX')

pprint.pprint( data )

rawDescription: Replace value in JSON file for key which can be nested by n levels


code:
x = "Jun 30 13:07:51 xxxx kernel: 15702368.296557 UFW BLOCK IN=eth1 OUT= MAC=so:me:mac SRC=xx.xx.xx.xx DST=xx.xx.xx.xx LEN=40 TOS=0x00 PREC=0x00 TTL=55 ID=47632 PROTO=TCP SPT=58875 DPT=80 WINDOW=65535 RES=0x00 CK FIN URGP=0"
k = "=TCP"
keep = [ele for ele in x.split() if ele.endswith(k)]
['PROTO=TCP']

rawDescription: Pythonic way of getting a value from a list


code: (item['path'], prev_item['path'])

rawDescription: Double assignment in python

description: Double assignment in python


code:
from pymongo import MongoClient

mongoServer = "mongodb://localhost:27017"
mongoDb = "moviesDat"
mongoCol = "userDat"

client = MongoClient(mongoServer)
db = client.get_database(mongoDb)
col = db.get_collection(mongoCol)
rattings = []

# Data generation for testing purpose.
# for i in range(1, 4):
#     rattings.append([0, i*1, i*2, i*3])

# assuming rattings is a valid collection with valid data
for row in rattings:
    col.update_one({"_id": row[0]}, {"$push": {"rattings": {"ratting": row[1], "movie_id": row[2], "timestamp": row[3]}}})

rawDescription: How to add documents to an array in a collection using PyMongo

description: Add documents to an array in a collection using PyMongo


code:
from pattern.text.en import wordnet
s = wordnet.synsets('dog')[0] # a word can belong to many synsets, let's just use one for the sake of argument
print(s.synonyms)

rawDescription: Replacement by synsets in Python pattern packatge


rawDescription: Give/get arguments in popup from "on_press"


code:
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
encryption_code = 'LFWOAYUISVKMNXPBDCRJTQEGHZ'
letters += letters.lower()
encryption_code += encryption_code.lower()
enc = dict(zip(letters,encryption_code))

dec = dict(zip(encryption_code, letters))

s = "HELLO world"

encr = "".join([enc.get(ch, ch) for ch in s])
decr = "".join([dec.get(ch, ch) for ch in encr])

print(encr)
print(decr)

rawDescription: How would I make a simple encryption/decryption program?


code:
def append(self, item):
    self.list.append(item)
def extend(self, item):
    self.list.extend(item)

rawDescription: Python - Neaten this append/extend conditional


code: mask = diff>1

rawDescription: Add two 3D numpy arrays with a 2D mask

description: Add two 3D numpy arrays with a 2D mask


code:
class BlobKey(db.Model) :
    blob_key = db.StringProperty()

rawDescription: GAE Python - Task to save CSV to same Blobstore element daily


code: dr_area.window.draw_drawable(self.gc, self.image, 0, 0, x, y, *self.image.get_size())

rawDescription: What is the fastest way to draw an image in Gtk+?


rawDescription: Merge python Dictionaries

description: Merge python Dictionaries


rawDescription: How to remove parenthesis from elements in a list (Python)

description: Remove parenthesis from elements in a list (Python)


code:
for i in range(len(col_one)):
    holder = list(number)
    for j in range(4):
        holder.remove(col_one[i][j])
    col_two.append(holder)

rawDescription: Randomizing an array into two arrays

description: Randomizing an array into two arrays


code:
[x+y for x in 'cat' for y in 'potty']
[x+y for x in 'catty' for y in 'pot']

rawDescription: Python list comprehension for loops

description: Python list comprehension for loops


rawDescription: RPy2, subset data frame with multiple conditions


code:
[('Itali', 'Italy'),
 ('am', 'Fem:Sg:Acc'),
 ('fat', 'fate'),
 ('o', 'Neut:Sg:Abl'),
 ('profug', 'fleeing'),
 ('us', 'Masc:Sg:Nom'),
 ('Lavini', 'Lavinian'),
 ('a', 'Neut:Pl:Acc'),
 ('que', 'come:Perf'),
 ('ven', '3'),
 ('it', 'Sg:Indic:Act')]

rawDescription: Using Python to count morphemes in a file & list them in columns


code:
with open('python_dictionary.json','r+') as f:
    dic = json.load(f)
    dic.update(new_dictionary)
    json.dump(dic, f)

rawDescription: Append to dictionary file in json, python


code:
import re
re.findall('([a-zA-Z0-9]+|[^a-zA-Z0-9]+)', u"abc你好def啊")

rawDescription: python split a unicode string by 3-bytes utf8 character

description: Python split a unicode string by 3-bytes utf8 character


code:
driver.maximize_window()
driver.get("http://store.nike.com/us/en_us/pw/mens-tops-t-shirts/7puZobp?ipp=120")
# ...

rawDescription: selenium fails when adding to cart, how to click on a javascript button?

description: Selenium fails when adding to cart, how to click on a javascript button


code:
subject = "by the way I know 54 how to take praise for 65 excellent questions 34 thank you for asking appreciated."
for match in re.finditer(r"(?=((?:\b\w+\b ){4}\d+(?: \b\w+\b){4}))", subject):
    print(match.group(1))

rawDescription: python regex find substrings centered with numbers

description: Python regex find substrings centered with numbers


code:
import pandas as pd
import numpy as np
np.random.seed(10)

remove_n = 1
df = pd.DataFrame({"a":[1,2,3,4], "b":[5,6,7,8]})
drop_indices = np.random.choice(df.index, remove_n, replace=False)
df_subset = df.drop(drop_indices)

rawDescription: Pandas: Remove rows at random without shuffling dataset


code:
from matplotlib import pyplot as pl

import threading

def anomaly_selection(selected, indexes, fig, ax):
    for i in range(0, len(indexes)):
        index = indexes[i]
        ax.set_xlim(index-100, index+100)
        ax.autoscale_view()
        #fig.canvas.draw_idle() # Do not need because of pause
        print("[%d/%d] Index %d " % (i, len(indexes), index), end="")
        while True:
            response = input("Particle? ")
            if response == "y":
                selected.append(index)
                break
            elif response == "x":
                selected[0] = True
                return selected
            elif response == "n":
                break

    selected[0] = True
    return selected

fig, ax = pl.subplots(2, sharex=True)
ax[0].plot([1, 2, 3, 4, 5]) # just pretend data
pl.show(block=False)

sel = [False]
th = threading.Thread(target=anomaly_selection, args=(sel, [100, 1000, 53000, 4300], fig, ax[0]))
th.start()
#sel = anomaly_selection([100, 1000, 53000, 4300], fig, ax[0])

while not sel[0]:
    pl.pause(1)
th.join()

rawDescription: Matplotlib Freezes When input() used in Spyder


code: [i for i, x in enumerate(text.split('\n')) if x and re.search(pattern, x)]

rawDescription: Given a list of strings, how to locate position of first string matching substring using RegEx?


code: next_scene_name = current_scene.enter()

rawDescription: Python: Understanding how 'return' works within a class (Learn Python Hard Way, exercise 43)


code:
# First, point Spark to the file
log = sc.textFile('file:///tmp/demo.txt')
# Second, replace each line with array of the values, thus string
# '1987|-5|SJC|500' is replaced with ['1987', '-5', 'SJC', '500']
log = log.map(lambda line: line.split('|'))
# Now filter leaving only the lists with 3rd element equal to 'SJC'
log = log.filter(lambda x: x[2]=='SJC')
# Now leave only the second column, 'Delay'
log = log.map(lambda x: x[1])
# And here's the result
log.collect()

rawDescription: Return data from same row as a given query in Pyspark


code:
def on_pick(event):
    #some code here
    art = event.artist
    if isinstance(art, matplotlib.axes.Axes):
        # the artist is as axes
        if art.get_label() == 'x':
            # do one thing
            pass
        elif art.get_label() == 'y':
            # do another thing
            pass

rawDescription: 2 pick events 2 colorbars

description: 2 pick events 2 colorbars


code:
def get_pagination_serializer(self, page):
    """
    Return a serializer instance to use with paginated data.
    """
    class SerializerClass(self.pagination_serializer_class):
        class Meta:
            object_serializer_class = self.get_serializer_class()

    pagination_serializer_class = SerializerClass
    context = self.get_serializer_context()
    return pagination_serializer_class(instance=page, context=context)

rawDescription: "get_paginated_response" in django-rest-framework 3.0


rawDescription: How can I find specific values inside a multidimensional array?

description: Find specific values inside a multidimensional array


code:
def __new__(self, x, y):
    return super(Point, self).__new__(self, (x, y))

rawDescription: python: representing square grid that wraps on itself (cylinder)

description: Python: representing square grid that wraps on itself (cylinder)


code: list1.replace(', ',',').replace(',',' ouf ')

rawDescription: Python3 how to seperate items in truple

description: Python3 how to seperate items in truple


code:
tag = Tag.objects.get(slug=slug)
return tag.post_set.all()

rawDescription: Using get_queryset() method in ListView, Django 1.9


code:
class WorkersApi(remote.Service):
  @remote.method(TravelRequest, message_types.VoidMessage)
  def save_to_db(self, request):
    # Instead of write each parameter, I am using this "cheat"
    params = {}
    for field in request.all_fields():
      params[field.name] = getattr(request, field.name)

    # Save data in the datastore
    form_model = FormModel(**params)
    form_model.put()

    return message_types.VoidMessage()

rawDescription: Google App Engine Python - Protorpc && Taskqueue


code: items = [line.split('|')]

rawDescription: Aggregate text keys-values python defaultdict


code:
COMPANY = ''.join(list(lines[30])).replace(",", "").replace("\n", "").split(" : ")
COMPANY[1] = COMPANY[1].lower()

rawDescription: How can I set a variable to a value multiple times in one line?

description: Set a variable to a value multiple times in one line


rawDescription: Sort inter-dependant elements of a dictionary


code:
with open("out1.csv", "wb") as fp:
   writer = csv.writer(fp, delimiter=",")
   for entry in large:
       writer.writerow([entry[0]])
       for line in entry[1]:
           writer.writerow(line)
       writer.writerow([])
       writer.writerow(['Sorted absolute maxes'])
       maxima = map(max, zip(map(abs, entry[1][0]), map(abs, entry[1][1])) )
       writer.writerow(sorted(maxima, reverse=True))
       writer.writerow([])

rawDescription: Exporting a modified list to CSV

description: Exporting a modified list to CSV


code: df1 = df1.set_index('ID')

rawDescription: How to extract values of one dataframe with values of other dataframe in pandas?

description: Extract values of one dataframe with values of other dataframe in pandas


code:
for item in vect:
    finished, results = do_stuff_with(item)
    if finished:
        break

rawDescription: Single Vs. Multiple For Loops In Python?

description: Single Vs. Multiple For Loops In Python


rawDescription: Python re.sub - replacing character when context does not match


rawDescription: python: unpacking an array of dictionaries in a matrix

description: Python: unpacking an array of dictionaries in a matrix


code:
gsrc2.props.start          = 2 * gst.SECOND
gsrc2.props.duration       = 6 * gst.SECOND
gsrc2.props.media_start    = 0
gsrc2.props.media_duration = 2 * gst.SECOND
gsrc2.props.priority       = 2

rawDescription: Video Transitions with GStreamer & GNonLin not working


code:
last_day_data['avg_temperature'][index_max] = "{\"y\": %s, \"marker\":" \
                                               " {\"symbol\": \"url(%ssun.png)\"}" \
                                                  "}" % (value_max, settings.STATIC_URL)

rawDescription: Remove quotes from an item in list in django for highcharts


code:
class Damage:

    WEAPONS = {'shortsword': (2, 7)}

    @classmethod
    def damage(cls, weapon):
        return randint(*cls.WEAPONS[weapon])

damage = Damage.damage('shortsword')

rawDescription: Storing the value of a randomly generated integer


code:
child = list()
for head in heads:
    child.append(etree.SubElement(root, head))

rawDescription: Creating XML-child's from a list

description: Creating XML-child's from a list


code:
user_name = ""
while not user_name:  # Empty strings evaluate to False
    user_name = raw_input('Enter a user name: ')

rawDescription: How to stop a user from hitting enter on a Python Command Line program?

description: Stop a user from hitting enter on a Python Command Line program


code:
for i, value in enumerate(lst):
    if i >= 4:
        break
    do_something_with(lst, i, value)

rawDescription: Most pythonic way to truncate a list to N indices when you can't guarantee the list is at least N length?


code:
one,two,three = [u'retw', u'folivi_jochan', u':', u'rt', u'newsycombinator', u':', u'uber',   u'is', u'taking', u'millions', u'of', u'manhattan', u'rides', u'away', u'from', u'taxis', u'http', u':', u'//t.co/zluyq3f6cc'],[u'retw', u'chr1sa', u':', u'rt', u'newsycombinator', u':', u'uber', u'is', u'taking', u'millions', u'of', u'manhattan', u'rides', u'away', u'from', u'taxis', u'http', u':', u'//t.co/zluyq3f6cc'], [u'retw', u'olutosinfashusi', u':', u'rt', u'newsycombinator', u':', u'uber', u'is', u'taking', u'millions', u'of']
from itertools import chain
from collections import Counter
c=Counter(chain(one,two,three))

rawDescription: Regex: creating a dictionary from a list of strings


code:
[match.group(0) for match in
       regex.finditer("1 1.1 .1 1. 1e1 1.04E-1 -.1 -1. e1 .1e1")]

rawDescription: Regex for Matching a Floating Point Number which is not an Integer


rawDescription: Why does string equality not working in this Python code as expected?


code:
df
df['a']
df.iloc[:, 0]

rawDescription: Call column in dataframe by column index instead of column name - pandas


code:
my_list = []
my_list
isinstance(my_list, object)

rawDescription: Are class names objects in python?

description: Are class names objects in python


rawDescription: Check if value exists in nested lists

description: Check if value exists in nested lists


rawDescription: Method that applies to instance of a class in python // Apply a method to an instance of a class in python


code: coords = zip(X, Y)

rawDescription: How to create matrix of points by X, Y coordinate in Python?

description: Create matrix of points by X, Y coordinate in Python


code:
m = MyHashTable(5)

m.insert("foo")
m.insert(73)
m.insert(93)
m.insert(1)
print(m.search(73))
print(m.search(93))
print(m.search(1))
print(m.search("foo"))
m.insert(73)
print(m.slots)
print(len(m))
print("foo" in m)
print(5 in m)

rawDescription: Python MyHashTable class: search method with linear probing


rawDescription: Blurring images containing text using Python Imaging Library


code:
a = A()
a.login_message
a.logout
a.version = '2.0'
a.logout

rawDescription: python global decorator for class attributes

description: Python global decorator for class attributes


rawDescription: implementing a spinner type object in a class as a subprocess

description: Implementing a spinner type object in a class as a subprocess


code:
# dt_tz is a datetime.datetime object
dt = dt_tz.replace(tzinfo=None)

rawDescription: remove pytz timezone

description: Remove pytz timezone


rawDescription: Accessing and performing operations on queryset data in Django


code:
def for_current_user():
    user_id = request.view_args.get('param_userid')
    return db.session.query([MODEL]).filter_by(...)

# And then in your form definition
QuerySelectField(query_factory=for_current_user)

rawDescription: How to access Form field data passed at init in QuerySelectField of another field

description: Access Form field data passed at init in QuerySelectField of another field


code:
import os
os.system("cd path/to/repo && git pull")

rawDescription: setup.py that pulls in github repos and puts them in the correct directory?

description: Setup.py that pulls in github repos and puts them in the correct directory


rawDescription: Python - How to change the name of a section use configparser?


code:
def myview(request, id=None):
    if id:
        obj_to_edit = MyModel.objects.get(id=1)
        form = MyForm(instance=obj_to_edit)
    else:
        obj_to_edit = None
        form = MyForm()

    if request.method == 'POST':
        if id: #update
            form = MyForm(request.POST, instance=obj_to_edit)
        else: #create
            form = MyForm(request.POST)

    #rest of the code

rawDescription: How to use Django AutoField to edit a modelForm?

description: Use Django AutoField to edit a modelForm


code:
import os

filelist = [ f for f in os.listdir(".") if f.endswith(".bak") ]
for f in filelist:
    os.remove(f)

rawDescription: Deleting all files in a directory with Python


code: sorted(nice.items(), key=lambda itm: list(map(int,itm[::-1])),reverse=True)

rawDescription: Python : Displaying a sorted dictionary by more than one condition


code:
burstsize = 10
for i in range(burstsize):
   sock.write(data)

rawDescription: Configuring client throughput in simple TCP server


rawDescription: How to convert a string to "04x" hex in little endian format?

description: Convert a string to "04x" hex in little endian format


code:
def playerMoveX(board):

    validMove = False
    while not validMove:
        col = input("You are the X player. What column would you like to play? :")
        for row in range (6,0,-1):
            if (1 <= int(row) <= 6) and (1 <= int(col) <= 7) and (board[int(row)-1][int(col)-1] == " "):
                board[int(row)-1][int(col)-1] = "X"
                validMove = True
                break
        else:
            print ("Invalid input. Please try again!\n")

rawDescription: Edit: Python - Going from 1 Player to Two Player Game Using Functions


code:
import csv

files = ['file_1', 'file_2']

def running_number_in_csv(filename_list):
    """ running_number resets every time the function is called, but is
    remembered within the function itself"""
    running_number = 1

    for individual_file in filename_list:
        new_rows = [] # Make something to hold row + extra column

        # Read contents of each row and append the running number to the list
        with open(individual_file + '.csv', 'r') as infile:
             reader = csv.reader(infile)
             for row in reader:
                 row.append(running_number)
                 new_rows.append(row)
                 running_number += 1 # Increments every row, regardless of file name number

        # Write the list containing the extra column for running number
        with open(individual_file + '.csv', 'w') as outfile: # Might need 'wb' in Windows
            writer = csv.writer(outfile)
            writer.writerows(new_rows)

get_running_number = running_number_in_csv(files) # CALL THE FUNCTION :)

rawDescription: counting number with 3 csvs python

description: Counting number with 3 csvs python


code:
import pandas

x = pandas.DatetimeIndex(start='2014-01-01', end='2014-01-06', freq='30T')
df = pandas.DataFrame(index=x, columns=['time since'])

basedate = pandas.Timestamp('2011-11-25')
df['time since'] = df.apply(lambda x: (x.name.to_datetime() - basedate).days, axis=1)

rawDescription: Pandas: number of days elapsed since a certain date


code:
for char in reversed(sequence):
    if char == end:
        endCount += 1
    else:
        break

rawDescription: Function does not work properly in for loop - Python


code:
def subfolders(path_to_parent):
     try:
        return next(os.walk(path_to_parent))[1]
     except StopIteration:
        return []

rawDescription: Check if a given directory contains any directory in python


code: omniboxHwnd = win32gui.FindWindowEx(hwnd, 0, 'Chrome_OmniboxView', None)

rawDescription: How do I get the URL of the active Google Chrome tab in Windows?

description: Get the URL of the active Google Chrome tab in Windows


rawDescription: pandas - percentage of matr

description: Pandas - percentage of matr


code:
import numpy as np
import scipy.sparse as sparse
N = 5
c = 1.1
A = sparse.lil_matrix((N, N))
A.setdiag(c**np.arange(1, N+1))
print(A.todense())

rawDescription: Python, lil_matrix : set the diagonal to be a power of a number


code:
i=100
a = 1 if i<100 else 2 if i>100 else 0
a
i=101
a = 1 if i<100 else 2 if i>100 else 0
a
i=99
a = 1 if i<100 else 2 if i>100 else 0
a

rawDescription: Python putting an if-elif-else statement on one line


code: lvls = np.logspace(0,4,20)

rawDescription: matplotlib contour plot: proportional colorbar levels in logarithmic scale

description: Matplotlib contour plot: proportional colorbar levels in logarithmic scale


code:
path =  os.path.dirname(os.path.realpath(__file__))
import zipfile
z = zipfile.ZipFile(file(path.rsplit("/", 3)[0]))
accessfiles  = filter(lambda zipList: zipList.startswith("<pattern>"), z.namelist())

rawDescription: Create a Python binary using compile all


code:
import library
getattr(library, 'oakalleyit')()

rawDescription: Pythonic way to import module whose name is only known at runtime?


rawDescription: How to find a specific file in Python

description: Find a specific file in Python


code:
class MyRequestHandlerFactory(object):

  def __init__(self, param1, param2):
            self.param1 = param1
            self.param2 = param2

  def __call__(self):
            handler = MCRequestHandler()
            handler.param1 = param1
            handler.param2 = param2

rawDescription: Python threads passing parameters

description: Python threads passing parameters


code:
# main definitions
import time
class Blah():
    update=0
    def testthings(self, function):
        return function(9)

# rpyc servic definition
import rpyc

class MyService(rpyc.Service):
    def exposed_testthings(self, function = lambda x: x):
        return main.testthings(function = function)
    def exposed_get_main_update(self):
        return main.update

# start the rpyc server
from rpyc.utils.server import ThreadedServer
from threading import Thread
server = ThreadedServer(MyService, port = 12345)
t = Thread(target = server.start)
t.daemon = True
t.start()

# the main logic
main = Blah()
while True:
    main.update+=1
    time.sleep(1)

rawDescription: How to interact with Running Python Script

description: Interact with Running Python Script


code:
from whoosh.query import Every
results = ix.searcher().search(Every('content'))

rawDescription: Whoosh index viewer

description: Whoosh index viewer


code:
import datetime

self.response.set_cookie('name', 'value', expires=datetime.datetime.now(), path='/', domain='example.com')

rawDescription: How to set cookies with GAE/Python for 1 month?

description: Set cookies with GAE/Python for 1 month


code:
idx = pd.IndexSlice
df.loc[idx[:,('a','b')], :] = df_.values

rawDescription: pandas multiindex assignment from another dataframe

description: Pandas multiindex assignment from another dataframe


code: Eg_dict = {row[1]: row[2] for row in iterable}  # python 2.7 and up

rawDescription: Add new key value pair in a Python Dictionary


rawDescription: How can change the logo of Tkinter GUI screen


code:
class baseSprite(pyglet.sprite.Sprite):
    def __init__(self, texture, x, y, batch, subgroup):
        self.texture = texture

        super(baseSprite, self).__init__(self.texture, batch=batch, group=subgroup)
        self.x = x
        self.y = y

    def move(self, x, y):
        """ This function is just to show you
            how you could improve your base class
            even further """
        self.x += x
        self.y += y

    def _draw(self):
        """
        Normally we call _draw() instead of .draw() on sprites
        because _draw() will contains so much more than simply
        drawing the object, it might check for interactions or
        update inline data (and most likely positioning objects).
        """
        self.draw()

rawDescription: Pyglet Image Rendering

description: Pyglet Image Rendering


code:
from collections import Iterable
def check(ele):
   return isinstance(ele,Iterable) and not isinstance(ele,basestring)

rawDescription: python: automatically differentiate between list / tuple / array and int / float

description: Python: automatically differentiate between list / tuple / array and int / float


code:
import struct

def u2s(v, width=32):
    return struct.unpack("q",struct.pack("Q",v<<(64-width)))[0]>>(64-width)

def s2u(v, width=32):
    return struct.unpack("Q",struct.pack("q",v<<(64-width)))[0]>>(64-width)

rawDescription: Sneaking unsigned values from Jython, through Java, to C and back again


code:
import numpy as np
dat = np.loadtxt( 'data.txt' )

rawDescription: 2D Array from a columned text file in python


code:
# create this manually
OBJ_TYPE_MAP = {
    # @note: using both None and 'parent', but should settle on one
    None: Parent, 'parent': Parent,
    'child': Child,
}
# ... or even automatically from the mappings:
OBJ_TYPE_MAP = {
    x.polymorphic_identity: x.class_
    for x in Parent.__mapper__.self_and_descendants
}
print(OBJ_TYPE_MAP)

def createNewObject(type_name, **kwargs):
    typ = OBJ_TYPE_MAP.get(type_name)
    assert typ, "Unknown type: {}".format(type_name)
    return typ(**kwargs)

a_parent = createNewObject(None, p_field1='parent_name1')
a_child = createNewObject(
    'child', p_field1='child_name1', c_field2='child_desc')

session.add_all([a_child, a_parent])

rawDescription: Add child classes in SQLAlchemy session using parent constructor


code: sdb = Button(..., command=lambda script="shutdown.sh": runscript(script))

rawDescription: python interpreter running shutdown code when I don't want it to

description: Python interpreter running shutdown code when I don't want it to


code:
# Module imports
import Tkinter as tk
import tkMessageBox
import urllib2
import re

# Function Definitions
class Application(tk.Frame):

    def __init__(self, parent = None):
        tk.Frame.__init__(self, parent)
        self.parent = parent
        self.setupUI()
        self.createWidgets()

    def setupUI(self):
        self.parent.title("User Input")
        self.grid()
        self.centerWindow()

    def centerWindow(self):
        app_width = 307
        app_height = 350

        sw = self.parent.winfo_screenwidth()
        sh = self.parent.winfo_screenheight()

        x = (sw - app_width)/2
        y = (sh - app_height)/2
        self.parent.geometry('%dx%d+%d+%d' % (app_width, app_height, x, y))

    def createWidgets(self):
        self.emptyFrame = tk.Frame(self.parent,bg="white")
        self.emptyFrame.grid(row=0,column=0,sticky="news")
        self.parent.grid_columnconfigure(0,weight=1)
        self.parent.grid_rowconfigure(0,weight=1)

        self.label1 = tk.Label(self.emptyFrame,text="Currency Converter",bg="white",font="Arial 26")
        self.label1.grid(row=0,column=0, sticky="news")
        self.label2 = tk.Label(self.emptyFrame,text="Visit http://www.xe.com/iso4217.php\nFor Currency Code Information",bg="white",font="Arial 8")
        self.label2.grid(row=0,column=0, sticky="news")
        self.label2.place(x = 55, y = 40)

        self.currAmount1 = tk.StringVar()                                    # Adding user imput for what value they want to convert
        self.currEntry1 = tk.Entry(self.parent, textvariable=self.currAmount1,bd=2,width=28,background="white")
        self.currEntry1.grid()
        self.currEntry1.place(x = 27, y = 125)

        self.currAmount2 = tk.StringVar()
        self.currEntry2 = tk.Entry(self.parent,textvariable=self.currAmount2,bd=2,width=28,background="white")
        self.currEntry2.grid()
        self.currEntry2.place(x = 27, y = 195)

        self.currency_list1 = ['USD','GBP'] #Creating the Currency Options menu and adding a list for all the currencies
        self.curr_start1 = tk.StringVar()
        self.curr_start1.set(self.currency_list1[0])
        self.currencyMenu1 = tk.OptionMenu(self.parent, self.curr_start1, *self.currency_list1)
        self.currencyMenu1.grid(row=0,column=0)
        self.currencyMenu1.place(x = 230, y = 120)

        self.currency_list2 = ['USD','GBP'] #Creating the Currency Options menu and adding a list for all the currencies
        self.curr_start2 = tk.StringVar()
        self.curr_start2.set(self.currency_list2[0])
        self.currencyMenu2 = tk.OptionMenu(self.parent, self.curr_start2, *self.currency_list2)
        self.currencyMenu2.grid(row=0,column=0)
        self.currencyMenu2.place(x = 230, y = 190)

        self.convertBtn = tk.Button(self.parent, text="Convert", font="Times 12", command=self.curr_search)
        self.convertBtn.grid()
        self.convertBtn.place(x = 120,y = 225)

    def curr_search(self):
        cur1 = self.curr_start1.get()
        cur2 = self.curr_start2.get()
        amount = self.currAmount1.get()
        url = 'https://www.google.com/finance/converter?'
        final_url = url + 'a=' + str(amount) + '&from=' + str(cur1) + '&to=' + str(cur2) # Adds the paramters the user inputs to the general url for converting to currencies
        web_obj = urllib2.urlopen(final_url)

        #Read each line , check if reg expression is in

        for line in web_obj:
            line = line.replace('\n', '')
            line = re.search('class=bld>(.+?)</span>', line)
            if line is not None:
                final_str = line

        final_str = final_str.group(1) # Extracts the string between the class and </span> which is the final conversion
        final_num = map(int, re.findall(r'\d+', final_str)) # Extracts the integers from the final_str so that it wouldn't have the currency code in the final. Puts in a list

        #If number is a decimal number, group, else take integer
        if len(final_num) > 1:
            final = str(final_num[0]) + '.' + str(final_num[1]) # Adds the decimal back from
        else:
            final = str(final_num[0])

        web_obj.close()
        self.currEntry2.delete(0, tk.END)
        self.currEntry2.insert(tk.END, final)
        return final

# Main body
root = tk.Tk()
root.resizable(width=False, height=False)
app = Application(root)
root.mainloop()

rawDescription: Problems with tk entry and optionmenu widget in currency converter


code:
from urlparse import urljoin

base_url = 'https://www.website.com/'

div0 = soup.find('a')
resource0 = urljoin(base_url, div0['href'])

rawDescription: Beautifulsoup, add attribute information (resource id) to url


code:
class GalleryForm(ModelForm):

    photos = ThumbnailChoiceField(queryset=Photo.objects.all(), label='Photos', widget=MyWidget(), required=False)

    def __init__(self, *args, **kwargs):
        super(GalleryForm, self).__init__(*args, **kwargs)
        rel = ManyToOneRel(self.instance.photos.model, 'id')
        self.fields['photos'].widget = RelatedFieldWidgetWrapper(self.fields['photos'].widget, rel, self.admin_site)

rawDescription: Django admin - How can I add the green plus sign for Many-to-many Field in custom admin form


code:
class SomeClass(object):
    # __init__ and other stuff
    #...

    def helper(self, func, *args, **kwargs):
        if self._current_view == self.WEAPON:
            items = self._weapons
        elif self._current_view == self.OTHER:
            items = self._others

        # The abstract part
        for item_id, item in items.iteritems():
            getattr(item, func)(*args, **kwargs)

        items_to_remove = []
        for item_id, each_item in items.iteritems:
            if each_item.out_dated():
                item_to_remove.append(item_id)
        for item_id in items_to_remove:
            del items[item_id]

    def update_status(self, status):
        self.helper('update_status', status)

    def refresh(self):
        self.helper('refresh')

rawDescription: Python: is using decorator to change method arguments a bad thing?


code:
import pandas as pd
from scipy.spatial import distance_matrix

data = [[5, 7], [7, 3], [8, 1]]
ctys = ['Boston', 'Phoenix', 'New York']
df = pd.DataFrame(data, columns=['xcord', 'ycord'], index=ctys)

rawDescription: Creating a Distance Matrix?

description: Creating a Distance Matrix


code:
input = ['canter creant cretan nectar', 'opt top pot', 'preprosessing',
         'subessential suitableness']
sorted((p for p in input if ' ' in p), key=lambda l: len(l.split()[0]),
       reverse=True)

rawDescription: List ordering in Python. Very simple

description: List ordering in Python. Very simple


code:
class point():
   def __init__(self,x = None,y =None,k =None,f =None):
      self.x = 0 if x is None else x  #assign default value only if the value was not passed
      self.y = 0 if y is None else y
      self.k = 0 if k is None else k
      self.f = -1 if f is None else f

rawDescription: PYTHON - reading single row from a file

description: PYTHON - reading single row from a file


rawDescription: Python counting lines in files using exact locations


code:
s = pd.Series(pd.DatetimeIndex(start='2012-01-01 00:05:00', periods=50,
                   freq=pd.offsets.Minute(n=5)), name='dttm_utc')
s.reset_index().set_index('dttm_utc').resample(pd.offsets.Hour()).agg([np.sum, np.mean])

rawDescription: Pandas: Timeseries data: How to select rows of an hour or a day or a minute?


code:
right = False
def func():
    right = True
func()
print (right)  #right is still False in the outer scope.

rawDescription: Arrow key input code not working in tkinter


code: os.kill(os.getpid(), signal.CTRL_C_EVENT) # send Ctrl+C to itself

rawDescription: Timeout function in Python

description: Timeout function in Python


code:
df = pd.DataFrame(randn(6, 3), index=arrays, columns=['A', 'B', 'C'])
df
df = df.sortlevel(0)
df
df.loc[('bar','two'),'A'] = 9999
df

rawDescription: Assign new values to slice from MultiIndex DataFrame


code:
class SetQueue(Queue):

    def _init(self, maxsize):
        self.maxsize = maxsize
        self.queue = set()

    def _put(self, item):
        self.queue.add(item)

    def _get(self):
        return self.queue.pop()

rawDescription: How check if a task is already in python Queue?


code: string=  input("Enter string:").slice(',')

rawDescription: string slicing python in lists

description: String slicing python in lists


code:
...
N = 10 #Number of Steps
d_avg = 0
nw = 2  # number of walkers
# Takes one step per second (Per N)
time = np.arange(N)
position = np.zeros(N)
walkers = np.zeros((nw, N))
for w in range(nw):
    for i in range (N-1):
        rn = np.random.uniform(0,1) # This generates a random number between (x,y)
        if rn < fwd:   # If the random number picked is >0.5
            step = 1 # move forward 1 step
        else:
            step = -1 # If the random number picked is not >0.5, then move backwards
        position[i+1]=position[i]+step
        d_avg+=position[i]
    walkers[w, :] = position
...

rawDescription: General Random Walk generator questions

description: General Random Walk generator questions


code:
mask = (d.weekday == 1) & (7 < d.day) & (d.day < 15)
d[mask]

rawDescription: Resample a pandas timeseries by "1st Monday of month" etc


code:
import itertools
x = {'a': 'foo', 'b': 'foobar'}
y = {'c': 'barfoo', 'd': 'bar'}
z = [key for key in itertools.chain(x, y)]
z
dict(enumerate(z))
dict(enumerate(z, 1))

rawDescription: Merging dictionaries using a counter

description: Merging dictionaries using a counter


code:
page = render_template('email/activate.html', confirmURL=confirmURL)
msg = Message('Please confirm your email', sender='pavsidhu98@gmail.com', recipients=[email])
msg.body(page)
mail.send(msg)

rawDescription: HTML email appears as plain text?

description: HTML email appears as plain text


code:
prefix, sep, rest = rev_data.partition("{{")
if sep: # found the first "{{"
    rest = sep + rest # put it back
    while rest.startswith("{{"):
        # remove nested "{{expr}}" one by one until there is none
        rest, n = re.subn(r"{{(?:[^{]|(?<!{){)*?}}", "", rest, 1)
        if n == 0:
            break # the first "{{" is unmatched; can't remove it
    else: # deletion is successful
        rev_data = prefix + rest
print(rev_data)

rawDescription: get first paragraph from wikipedia article

description: Get first paragraph from wikipedia article


code:
form = MyForm()
if form.validate_on_submit():
    ...

rawDescription: Proper way to create a RadioField with a dynamic default value?


code:
Irek: [(None, 'Insufficient funds for loan, current balance is 35000'), (None, 'Insufficient funds for loan, current balance is 70000'), (None, 'Unable to get balance due to technical issue for Wells Fargo: 3')]
John: [(None, 'Insufficient funds for loan, current balance is 140000')]
Alex: [(245000, None), (280000, None)]
Fred: [(None, 'No bank associated with name Fred')]

rawDescription: combining maybe and seq monads: confused at the output

description: Combining maybe and seq monads: confused at the output


code: idx = dict((k, i) for i, k in enumerate(fields))

rawDescription: Fastest way to build a list from a sparse dictionary and a list of keys


rawDescription: convert flat dict to nested list and dict and intersect this

description: Convert flat dict to nested list and dict and intersect this


code:
class ConnectionHandler(SockJSConnection):
    def __init__(self, *args, **kwargs):
        super(ConnectionHandler, self).__init__(*args, **kwargs)
        self.client = brukva.Client()
        self.client.connect()
        self.client.subscribe('some_channel')

    def on_open(self, info):
        self.client.listen(self.on_chan_message)

    def on_message(self, msg):
        # this is a message broadcast from the client
        # handle it as necessary (this implementation ignores them)
        pass

    def on_chan_message(self, msg):
        # this is a message received from redis
        # send it to the client
        self.send(msg.body)

    def on_close(self):
        self.client.unsubscribe('text_stream')
        self.client.disconnect()

rawDescription: python Socket.IO client for sending broadcast messages to TornadIO2 server

description: Python Socket.IO client for sending broadcast messages to TornadIO2 server


code:
symbols = {
    '*': lambda x, y: x*y,
    '+': lambda x, y: x+y,
    '-': lambda x, y: x-y,
    '/': lambda x, y: x/y,
}

rawDescription: How to convert '*' to *

description: Convert '*' to *


code:
# assuming your db is named "db"
db.create_index(Products, [Products.inserted])

rawDescription: Python orm datetime issue

description: Python orm datetime issue


code:
def my_function(*args):
    print("Arguments: {0}".format(args))

class MyProcess(mp.Process):
    def __init__(self, target, args):
        mp.Process.__init__(self, target=target, args=args)

def main():
    a1 = MyProcess(target=my_function, args=("1st Process...",))
    a2 = MyProcess(target=my_function, args=("2nd Process...",))
    a3 = MyProcess(target=my_function, args=("3rd Process...",))
    a4 = MyProcess(target=my_function, args=("4th Process...",))

    proclist = [a1, a2, a3, a4]
    for proc in proclist:
        proc.start()

    for proc in proclist:
        proc.join()

if __name__ == '__main__':
    main()

rawDescription: Need of running the same python file in parallel


code:
def fuzzy_startswith(needle,haystack):
    n_words = len(needle.split())
    haystack_startswith = " ".join(haystack.split()[:n_words])
    return fuzz.ratio(needle,haystack_startswith)

fuzzy_startswith("my nam is","my name is joran")

rawDescription: Fuzzy Startswith

description: Fuzzy Startswith


code:
setup(
    # ...
    data_files = [('', ['Tempate.xlsx',])],
)

rawDescription: Making a standalone .exe file of a python script


rawDescription: How correctly draw boxes in pyprocessing?


rawDescription: Call python from javascript

description: Call python from javascript


code:
import your_project.lib.dbfunctions
results = dbfunctions.get_articles()
return dict(status=True, results=results)

rawDescription: SqlAlchemy query in a template page

description: SqlAlchemy query in a template page


rawDescription: How to append selected columns to pandas dataframe from df with different columns

description: Append selected columns to pandas dataframe from df with different columns


code:
job = q.enqueue(find_keyword, keyword)
while not job.result:
    time.sleep(1)
return jsonify(word=job.result)

rawDescription: Storing a calculation in a flask route cache


rawDescription: python list manipulation nesting vertically, making it look like a matrix

description: Python list manipulation nesting vertically, making it look like a matrix


code:
data = soup.find("table",{"class":"xTable" })

from collections import OrderedDict
headers = OrderedDict.fromkeys(s["title"] for s in soup.find("div", {"class":"xtRowCon"}).find_all("span"))

rows = [[ele.text.strip() for ele in tag.find_all("td")] for tag in data.find_all("tr")]

maturity = [ele.find("span",{"class":"label_MATURITY"}).text.strip() for ele in soup.find("div",{"class":"xtTblCon"}).find_all("li")]

print(headers.keys())
print(rows)
print(maturity)

rawDescription: AttributeError when web-scraping data using Python


code:
import datetime
c=datetime.datetime
methodList = [method for method in dir(c) if callable(getattr(c, method))]
methodList

rawDescription: Methods in a class -Python

description: Methods in a class -Python


rawDescription: Why is random.choice so slow?

description: Why is random.choice so slow


code: MAILTO=username

rawDescription: How do I know if jobs have been/are performing? - Crontab

description: Know if jobs have been/are performing? - Crontab


code: help(foo)

rawDescription: How to display Python file header description

description: Display Python file header description


code: MyData.groupby('Location').apply(lag_year).reset_index().drop('level_1' , axis = 1).sort_index(by = 'Year')

rawDescription: Time lag of variables in Pandas

description: Time lag of variables in Pandas


code:
import base64

with open('otherframeBorder.gif', 'rb') as f:
    encoded = base64.encodestring(f.read())
    print(encoded.decode('latin1'))

rawDescription: ttk Entry background colour

description: Ttk Entry background colour


code: my_dict[buy] = my_dict.get(buy, 0) + equal

rawDescription: Dictionary up to date

description: Dictionary up to date


code:
'Newyork, NY'.split(',', 1)[0]
'Newyork, NY'.partition(',')[0]

rawDescription: How to delete some characters from a string by matching certain character in python

description: Delete some characters from a string by matching certain character in python


rawDescription: How to un-pad a string/number with zeros

description: Un-pad a string/number with zeros


code:
import re
import json

repaired_data = re.sub(r'(?<={|,)\s*(\w+)(?=:)', r'"\1"', broken_data)
data = json.loads(repaired_data)

rawDescription: Fetch News Data from right Scrollbar using Beautifulsoup


code:
def f(coord,x,y,r):
    vec = ((coord[0]-x)**2) + ((coord[1] - y)**2) - (r**2)
    # return the sum of the squares of the vector
    return np.sum(vec**2)

rawDescription: How to perform non-linear optimization with scipy/numpy or sympy?

description: Perform non-linear optimization with scipy/numpy or sympy


code:
import thread, socket, Queue

connections = Queue.Queue()
num_threads = 10
backlog = 10

def request():
    while 1:
        conn = connections.get()
        data = ''
        while '\r\n\r\n' not in data:
            data += conn.recv(4048)
        conn.sendall('HTTP/1.1 200 OK\r\n\r\nHello World')
        conn.close()

if __name__ == '__main__':
    for _ in range(num_threads):
        thread.start_new_thread(request, ())

    acceptor = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    acceptor.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    acceptor.bind(('', 1234))
    acceptor.listen(backlog)
    while 1:
        conn, addr = acceptor.accept()
        connections.put(conn)

rawDescription: I’m stunned: weird problem with python and sockets + threads


code: array[column][row]

rawDescription: Minimum x and y coordinate of 2d array. Python


code:
from logging import log
log("A critical error occured.")

rawDescription: Globals and singletons in Python

description: Globals and singletons in Python


code: value = d.get(key)

rawDescription: Return None if Dictionary key is not available


code:
input = open('file name', 'r')
listoflists = []
for line in input:
  list = line.split(' ') # an example of splitting with space separator
  listoflists.append(list)

rawDescription: How to pick lines containg numbers in given positions on the line with python

description: Pick lines containg numbers in given positions on the line with python


code:
def pass_pointer_array():
  string_set = [
    "Hello",
    "Bye Bye",
    "How do you do"
  ]

  string_length = len(string_set)

  select_type = (c_char_p * string_length)
  select = select_type()

  for key, item in enumerate(string_set):
    select[key] = item

  library.passPointerArray.argtypes = [c_int, select_type]
  library.passPointerArray(string_length, select)

rawDescription: Calling C methods with Char** arguments from Python with ctypes


code:
for i,j in np.ndindex(x.shape[:2]):
    beta[i,j] = foo(x[i,j,:], y[j,:])

rawDescription: apply a function on two ndarrays's corresponding rows at once(without for loop)

description: Apply a function on two ndarrays's corresponding rows at once(without for loop)


code:
from multiprocessing.pool import ThreadPool
pool = ThreadPool()

ar = ...
states = np.empty_like(array)

def f(i):
    states[i] = function(ar[i], *args)

pool.map(f, range(len(ar)))

rawDescription: Parallelize loop over numpy rows

description: Parallelize loop over numpy rows


code: [grid[x][y] for x, y in indices]

rawDescription: Indexing a list using the values in a tuple in python


rawDescription: Python regex fractions

description: Python regex fractions


code:
def counter(my_list, key):
    return sum(item == key for item in my_list)

assert counter([2, 1, 3, 1, 4], 1) == 2
assert counter([2, 1, 3, 1, 4], 5) == 0
assert counter([2, 1, 3, 1, 4], 2) == 1

rawDescription: How to print the number of times an element appears in a list in Python?

description: Print the number of times an element appears in a list in Python


code:
@route("/plain-text")
def plain_text():
    response.content_type = "text/plain"
    return """This
              multi-line string
              will show up
              just fine"""

rawDescription: Output Multi-line strings with Python Flask or other framework


code:
def foo2():
    result=[]
    for i in range(1,101):
        if i%2:
            i=i*i
            for j in [1,2,3,4,5]:
                if i+j > 40:
                    result+=[(i,j)]
    return result

rawDescription: How fast are nested python generators?

description: How fast are nested python generators


code:
output = data.groupby(["A", "B"]).size()#dont have to call the constructor again
output = output.unstack('B').fillna(0)

rawDescription: Pandas: what is a simple way to group by two columns and build a new flat dataframe


rawDescription: when logging from facebook using django allauth request user is admin

description: When logging from facebook using django allauth request user is admin


code:
def remove_nulls(d):
    return {k: v for k, v in d.iteritems() if v is not None}

res = json.loads(json_value, object_hook=remove_nulls)

rawDescription: is there a way to config python json library to ignore fields that has null values when calling json.loads?

description: Is there a way to config python json library to ignore fields that has null values when calling json.loads


code:
def code_char(char, key):
    ichar = int(char)
    ikey = int(key)
    return chr(ord('0') + ichar+ikey)

rawDescription: Python encoding blocks of text using strings


code:
import numpy as np

def sector_mask(shape,centre,radius,angle_range):
    """
    Return a boolean mask for a circular sector. The start/stop angles in
    `angle_range` should be given in clockwise order.
    """

    x,y = np.ogrid[:shape[0],:shape[1]]
    cx,cy = centre
    tmin,tmax = np.deg2rad(angle_range)

    # ensure stop angle > start angle
    if tmax < tmin:
            tmax += 2*np.pi

    # convert cartesian --> polar coordinates
    r2 = (x-cx)*(x-cx) + (y-cy)*(y-cy)
    theta = np.arctan2(x-cx,y-cy) - tmin

    # wrap angles between 0 and 2*pi
    theta %= (2*np.pi)

    # circular mask
    circmask = r2 <= radius*radius

    # angular mask
    anglemask = theta <= (tmax-tmin)

    return circmask*anglemask

rawDescription: Mask a circular sector in a numpy array

description: Mask a circular sector in a numpy array


code:
def require_api_token(func):
    @wraps(func)
    def check_token(*args, **kwargs):
        # Check to see if it's in their session
        if 'api_session_token' not in session:
            # If it isn't return our access denied message (you can also return a redirect or render_template)
            return Response("Access denied")

        # Otherwise just send them where they wanted to go
        return func(*args, **kwargs)

    return check_token

rawDescription: How do you implement token authentication in Flask?

description: Implement token authentication in Flask


rawDescription: Find out indices of an array elements

description: Find out indices of an array elements


rawDescription: list of ints into a list of tuples python

description: List of ints into a list of tuples python


code:
from query import new_query, _and, _or

def process(table_cls, bp_cls, conn, args):
    query = new_query('active', '=', 'true')
    table = table_cls(conn)
    res = table.get_records(query=query)
    for record in res['records']:
        bp = bp_cls(record)
        for check in args:
            fcheck = getattr(bp, check, None)
            if fcheck:
                fcheck()

rawDescription: Generic python module

description: Generic python module


code:
valid_test_ = valid_test.map(lambda xs: tuple(int(x) for x in xs))
valid_test_
## PythonRDD[497] at RDD at PythonRDD.scala:43

model.predictAll(valid_test_).count()
## 3

rawDescription: Spark ALS predictAll returns empty

description: Spark ALS predictAll returns empty


code:
def set_test(mylist):
    for short_tuple in mylist:
        short_tuple_set = set(short_tuple)
        for combo in itertools.combinations((range(1, 50)), 5):
            if short_tuple_set.issubset(combo):
                yield combo

def create_combos(mylist):
    for short_tuple in mylist:
        short_tuple_set = set(short_tuple)
        remainder = (i for i in range(1, 50) if i not in short_tuple_set)
        for combo in itertools.combinations(remainder, 5 - len(short_tuple)):
            combo = sorted(combo + short_tuple)
            yield combo

all(a == tuple(b) for a, b in itertools.izip_longest(set_test(mylist), create_combos(mylist)))

rawDescription: How do I find out if a combination of numbers are in another combination of numbers?

description: Find out if a combination of numbers are in another combination of numbers


code: output, = fun(other_func())

rawDescription: How to return a single value instead of a tuple?

description: Return a single value instead of a tuple


rawDescription: Convert 3d Numpy array to 2d

description: Convert 3d Numpy array to 2d


code:
your_values = { 'x' : 'x value', 'y' : 'y value', 'z' : 'value'}
arguments = {}
for k, v in your_values.items():
    if v:
        arguments[k] = v

Model.objects.filter(**arguments)

rawDescription: Query when parameter is none django

description: Query when parameter is none django


code: p('beginning','the')

rawDescription: What is the Probability of ‘begining’ given ‘the’?


code:
a = np.array([1,2,3,4])
a
a.dtype
a.dtype = complex
a

rawDescription: Python: unpacking string of floats to complex numbers


code:
M = np.random.rand((4, 4))
df = pd.DataFrame(M)
df

rawDescription: Numpy Construct a new matrix without rowi,columnj keeping row,column indices


code:
import timeit

def function(stuff):
    NotImplemented

def setup():
    print("the setup_data is:",setup_data)

setup_code = """
from __main__ import function, setup
data = setup()
"""
func_code = "function(data)"

for i in range(4):
    setup_data = i
    result = timeit.timeit(func_code,setup_code)
    print("result is",result)

rawDescription: How to use timeit for multiple values

description: Use timeit for multiple values


code:
hit = random.uniform(0, 10)
if hit >= 8:
    damage = 0.00
else:
    damage = hit / 8

rawDescription: Random Python module and creating chances


rawDescription: Add Integer into existing list

description: Add Integer into existing list


code:
from copy import copy

d3 = copy(dict(((x, y), z) for x, y, z in list1))
d2 = dict(((x, y), z) for x, y, z in list2)

for key, value in d2.iteritems():
    d3[key] = (d3[key] if key in d3 else 0.0) + value

rawDescription: Check whether an entry present in python list and add the elements


code: np.rec.fromarrays(t.T, dt)

rawDescription: ValueError while trying to convert an array of strings to an array with mixed given dtypes


code:
from old import Sassy

new = Sassy()
print(new.bbq)

rawDescription: Pass class variables onto a class on another script in Python


rawDescription: Python, Pandas: average every 2 rows together


code:
import matplotlib.pyplot as plt
import networkx as nx
socialNetworl = nx.Graph()
socialNetworl.add_nodes_from([1,2,3,4,5,6])
socialNetworl.add_edges_from([(1,2),(1,3),(2,3),(2,5),(2,6)])
labels = {1:'King Arthur', 2:'Lancelot', 3:'shopkeeper', 4:'dead parrot', 5:'Brian', 6:'Sir Robin'}
nx.draw(socialNetworl, node_size = 800, node_color="cyan", labels=labels, with_labels = True)
plt.show()

rawDescription: I can't see labels in NetworkX diagram

description: I can't see labels in NetworkX diagram


code:
class MyRecover(Recover):
    search_fields = ['email']

myrecover = MyRecover.as_view()

rawDescription: django-password-reset - how to set attributes

description: Django-password-reset - how to set attributes


code: [(i,sum(j)/len(j)) for i,j in d.items()]

rawDescription: Averaging the values in a dictionary based on the key


code:
X = HashingVectorizer(n_features=8, binary=True).transform([text])
X.toarray()
scipy.linalg.norm(X.toarray())
X = HashingVectorizer(n_features=8, binary=True, norm=None).transform([text])
X.toarray()

rawDescription: Why is my scikit learn HashingVectorizor giving me floats with binary = True set?


code:
for year in range(1, years + 1):
    print('Looks like you will be making', new_salary,' in ', years,'years.')

rawDescription: Should I do anything to make my code more pythonic?


code: nbsp;g

rawDescription: Filling in select field form with mechanize


code:
from protorpc import messages

class DetailMessage(messages.Message):
    """
    General-format Json detail response
    """
    data = GeneralField(1)

rawDescription: how to return dynamic json as response from google cloud endpoint python

description: Return dynamic json as response from google cloud endpoint python


rawDescription: Decoding JSON and printing some specific part?


code: n = int(my_num)

rawDescription: Why is this python while loop missing the logical operator?


code:
sched_cmd = ['at', starttime]
command = 'python foo.py %s %s' % (arg1, arg2)
p = subprocess.Popen(sched_cmd, stdin=subprocess.PIPE)
p.communicate(command)

rawDescription: Creating A Linux at Job (scheduling) in Python


code:
lst = []
for line in new_listes(poem_lines):
    i = 0
    for word in line.split():
            for sylables in word_to_phonemes[word]:
                for char in sylables:
                    i += number_count(char)
    lst.append(i)

return lst

rawDescription: Counting Syllables In String

description: Counting Syllables In String


rawDescription: Grouping items in two lists

description: Grouping items in two lists


code: brutedata = dict(('postdata[{0}]'.format(i), v) for i, v in enumerate(itemlist))

rawDescription: How to call all list items in a list based on item count

description: Call all list items in a list based on item count


rawDescription: Select row prior as.freq() occurance

description: Select row prior as.freq() occurance


code:
count, bin_edges = np.histogram(lat, bins=grid)
temp_sum, bin_edges = np.histogram(lat, bins=grid, weights=temp)

rawDescription: Recursively select elements in numpy array


code:
class Generic(object):
    pass

rawDescription: Python create list of objects from list of 2 elements lists


rawDescription: Grouping model objects in Django

description: Grouping model objects in Django


code: sys.modules['mymodule'] = mymodule

rawDescription: Load module from string in python

description: Load module from string in python


code:
import collections

def build(file):
    pairs = collections.defaultdict(list)
    for line in file:  # N.B. file assumed to be already sorted
        a, b, c, freq = line.split()
        key = ' '.join((a, b))
        pairs[key].append(c + ':' + freq if freq != '1' else c)
    out = open('squeezedsinglearrayfile', 'w')
    for key in sorted(pairs.keys()):
        out.write('%s|%s\n' % (key, ' '.join(pairs[key])))

def load():
    return open('squeezedsinglearrayfile').readlines()

if __name__ == '__main__':
    build(open('freqs'))

rawDescription: Memory Efficient Alternatives to Python Dictionaries


code: [set(item) for item in set(frozenset(item) for item in L)]

rawDescription: removing duplicates of a list of sets

description: Removing duplicates of a list of sets


code: first5 = random.sample(range(1, 59+1), 5)

rawDescription: Generating Combinations NOT Permutations. How?


code: (hist_data, bin_edges) = histogram(my_array[:,0], weights=my_array[:,1])

rawDescription: making binned boxplot in matplotlib with numpy and scipy in Python

description: Making binned boxplot in matplotlib with numpy and scipy in Python


code:
bytesread = 0
for line in fh:
  bytesread += len(line)

rawDescription: Keep Track of Number of Bytes Read

description: Keep Track of Number of Bytes Read


code:
chunks = [
    {
        'offset': struct.unpack('<I', stream_file.read(int_length))[0],
        'length': struct.unpack('<I', stream_file.read(int_length))[0]
    }
    for _ in range(chunks_count)
]

rawDescription: Python Out of memory in range

description: Python Out of memory in range


rawDescription: How can I copy a Python string?

description: Copy a Python string


code:
import glob
from heapq import heappush, heappushpop
from pprint import pprint as pp

def my_iterable():
    for fname in glob.glob("in_s*.txt"):
        f = open(fname, "r")
        for line in f:
            items = line.split()
            yield -float(items[6]), fname, items[0]
        f.close()

def homegrown_nlargest(n, iterable):
    """Ensures heap never has more than n entries"""
    heap = []
    for item in iterable:
        if len(heap) < n:
            heappush(heap, item)
        else:
            heappushpop(heap, item)
    return heap

result =  homegrown_nlargest(10, my_iterable())
result = sorted(result, reverse=True)
result = [(fname, fld0, -negfld6) for negfld6, fname, fld0 in result]
pp(result)

rawDescription: Search and sort data from several files

description: Search and sort data from several files


code:
import sys
import random

def random_nonascii_unicode(length):
    return u''.join(unichr(random.randint(0x80, sys.maxunicode)) for _ in range(length))

rawDescription: how to generate unicode strings only with multi-byte characters

description: Generate unicode strings only with multi-byte characters


rawDescription: Consuming from queues based upon external event (event queues)


rawDescription: How do you tally the amount of times a item in appears from a randomly list?

description: Tally the amount of times a item in appears from a randomly list


code:
import numpy as np
def blockshaped(arr, nrows, ncols):
    """
    Return an array of shape (n, nrows, ncols) where
    n * nrows * ncols = arr.size

    If arr is a 2D array, the returned array looks like n subblocks with
    each subblock preserving the "physical" layout of arr.
    """
    h, w = arr.shape
    return (arr.reshape(h//nrows, nrows, -1, ncols)
               .swapaxes(1,2)
               .reshape(-1, nrows, ncols))

def unblockshaped(arr, h, w):
    """
    Return an array of shape (h, w) where
    h * w = arr.size

    If arr is of shape (n, nrows, ncols), n sublocks of shape (nrows, ncols),
    then the returned array preserves the "physical" layout of the sublocks.
    """
    n, nrows, ncols = arr.shape
    return (arr.reshape(h//nrows, -1, nrows, ncols)
               .swapaxes(1,2)
               .reshape(h, w))

rawDescription: Form a big 2d array from multiple smaller 2d arrays


rawDescription: xtick max value setup with panda and matplotlib

description: Xtick max value setup with panda and matplotlib


rawDescription: How do I add tags to certain strings in python using re.sub?

description: Add tags to certain strings in python using re.sub


code:
icon = wx.EmptyIcon()
icon.CopyFromBitmap(wx.Bitmap("icon.ico", wx.BITMAP_TYPE_ANY))
self.SetIcon(icon)

rawDescription: How to set icon on wxFrame?

description: Set icon on wxFrame


code: return next(account for account in self.accounts if account.type == 'equity')

rawDescription: Get specific object from a list with a certain parameter


code:
while True:
    fd = file("batch_control", "r")
    for cmd in fd:
        print("New command [%s]" % cmd[:-1])
    fd.close()

rawDescription: Re-read environment of parent process in python


code:
p1 = {'pair': ([2, 3, 4], [5, 6, 7])}
p2 = {'pair': ([9, 10, 11], [12, 13, 14])}
p3 = {'pair': ([12, 20, 30], [17, 8, 30])}

def find_and_set_mean(a_dictionary):
    a_dictionary['mean']=np.mean(a_dictionary['pair'])

find_and_set_mean(p1)
find_and_set_mean(p2)
find_and_set_mean(p3)

rawDescription: Automatization issue

description: Automatization issue


code:
df
df[(df.values > 1.5).any(1)]

rawDescription: selecting across multiple columns with python pandas?

description: Selecting across multiple columns with python pandas


code:
rect_array[2,:] += rect_array[0,:]
rect_array[4,:] += rect_array[1,:]

rawDescription: How to form a numpy array of elements in a loop?

description: Form a numpy array of elements in a loop


code:
class Callable(object):
    def __init__(self, name):
        self.name = name

    def __call__(self, greeting):
        return '{}, {}!'.format(greeting, self.name)

rawDescription: Python 'Functions are objects'

description: Python 'Functions are objects'


rawDescription: How to create a number of variables from a number entered by user

description: Create a number of variables from a number entered by user


code:
parser = argparse.ArgumentParser()
parser.add_argument('infile', type=argparse.FileType('r'))
parser.add_argument('outfile', type=argparse.FileType('w'))

required, extra = parser.parse_known_args()
infile, outfile = required.infile, required.outfile

args = re.findall(pattern, infile.read())
infile.seek(0)

parser = argparse.ArgumentParser()
for arg in args:
    parser.add_argument('--' + arg.lower())

replacements = vars(parser.parse_args(extra))

rawDescription: How to extend this search-and-replace python script to accept variables from command line?

description: Extend this search-and-replace python script to accept variables from command line


rawDescription: convert numpy array to string with delimiter and save to txt file

description: Convert numpy array to string with delimiter and save to txt file


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

x = np.arange(-2*np.pi, 2*np.pi, 0.1)
fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.plot(x, np.sin(x), label='Sine')
ax.plot(x, np.cos(x), label='Cosine')
ax.plot(x, np.arctan(x), label='Inverse tan')
handles, labels = ax.get_legend_handles_labels()
lgd = ax.legend(handles, labels, loc='upper center', bbox_to_anchor=(0.5,-0.1))
ax.grid('on')
fig.savefig('samplefigure', bbox_extra_artists=(lgd,), bbox_inches='tight')

rawDescription: Moving matplotlib legend outside of the axis makes it cutoff by the figure box


code: oldStack = input_stack

rawDescription: Python stack contents are overwritten when passed to a function


code:
df.sort_index(inplace=True)
idx = pd.IndexSlice
df.loc[idx[:, ('foo','bar'), 'can'], :]

rawDescription: How to slice one MultiIndex DataFrame with the MultiIndex of another

description: Slice one MultiIndex DataFrame with the MultiIndex of another


code:
x = np.linspace(0, 2 * np.pi, 128)
y = 1 - np.sin(x)
fft = np.fft.fft(y)
np.mean((fft * fft.conj()).real)
np.mean(y**2)
fft = fft / np.sqrt(len(fft))
np.mean((fft * fft.conj()).real)

rawDescription: (numpy) Wrong amplitude(?) of FFT'd array?


rawDescription: Is there a way to use PhantomJS in Python?


code: df.drop('a', level=1, axis=1)

rawDescription: Dropping a single (sub-) column from a MultiIndex


code: padding = random.choice(string.letters)

rawDescription: How to make sure the input is a multiple of 16, and if not add padding to it

description: Make sure the input is a multiple of 16, and if not add padding to it


code: a[~b] = np.nan

rawDescription: Using bool array mask, replace False values with NaN


rawDescription: Calculate the euclidean distance in scipy csr matrix


code:
b = B()
str(b)

rawDescription: Python : Revert to base __str__ behavior


code:
class Base:
    def foo(self, *args, **kwargs):
        print("foo stuff from Base")
        return "return something here"

class SomeClass(Base):
    def foo(self, *args, **kwargs):
        # python 2
        #x = super(SomeClass, self).foo(*args, **kwargs)
        # python 3
        x = super().foo(*args, **kwargs)
        print(x)
        print("SomeClass extension of foo")

s = SomeClass()
s.foo()

rawDescription: What is a good way to implement several very similar functions?


code: self.text_area = tk.Text(self)

rawDescription: Tkinter Class method

description: Tkinter Class method


code:
with open('out.dat', 'wb') as pkl_file:
    pickle.dump([x,y], pkl_file, -1)

rawDescription: processing a binary file containing an array using python

description: Processing a binary file containing an array using python


code:
import functools
boughtColClipper = functools.partial(removeOutlier, 5, 105)

rawDescription: Refactoring a bad 'code smell' in multiplication of columns in a dataframe


code:
return HttpResponse(loader.render_to_string(*args, **kwargs),
                        **httpresponse_kwargs)

rawDescription: Can you return a render() without a context?

description: Return a render() without a context


code:
current/models.py
class CurrentPostModel(CommonPostModel):
    pass

rawDescription: Reusing django models

description: Reusing django models


code: matrix = [[0 for x in range(len(content2))] for x in range(len(content1))]

rawDescription: What is the easier way to initialize a matrix in Python?


rawDescription: Manipulating a non-evenly spaced data series in python


code:
import os
os.chdir("/home/udi/foo")

rawDescription: python: Change the scripts working directory to the script's own directory

description: Python: Change the scripts working directory to the script's own directory


code:
allG = [np.linspace(-0.5,0.5, G) for G in GridSize]
out = np.meshgrid(*allG)

rawDescription: numpy linspace and mesh grid for multiple dimensions

description: Numpy linspace and mesh grid for multiple dimensions


code: from xpcom import components

rawDescription: How does miro embed vlc

description: How does miro embed vlc


code:
sol = bisection(0, 0.9, tstr, 0.01)
sol

rawDescription: Function not returning value (Python 2.7)


code:
import pygraphviz as pgv
import networkx as nx
import matplotlib.pyplot as plt
Gtmp = pgv.AGraph('file.dot')
G = nx.Graph(Gtmp)
nx.draw(G)
plt.show()

rawDescription: open .dot formatted graph from python

description: Open .dot formatted graph from python


code:
for name, pos in names.items():
    doSomethingWith(pos.x)
    doSomethingElseWith(pos.x, pos.y, pos.z)

rawDescription: Multiple values for key in dictionary in Python


code: [(name, getattr(obj, name)) for name in dir(obj) if not name.startswith('__')]

rawDescription: "Public" attributes of an object with their values


code:
from __future__ import division
counter2 = Counter({k:v/2 for k,v in counter.items()})
counter2

rawDescription: How to concatenate String count and Int

description: Concatenate String count and Int


code:
time = 20

def update_time():
    global time
    time = time-0.1
    timeLabel.config(text=str(time))
    if time<0.01:
        timeLabel.config(text='0')
        button.pack_forget()
        Label(root, text='You scored {}'.format(clicks), font=('Helvetica', 20, 'bold')).pack()
    else:
        timeLabel.after(100, update_time)

rawDescription: Python tkinter - timer

description: Python tkinter - timer


code:
re_pattern = r'\b(?:total|staff)\b'
re.findall(re_pattern, question)

rawDescription: Python regular expression to search for words in a sentence


code:
for item in lst:
    func(*item)

rawDescription: python: using nested lists as arguments

description: Python: using nested lists as arguments


code:
ctx = {
       'user' : request.user
      }

if request.user.is_authenticated():
    ctx.update({
         'first_name': request.user.first_name,
         'last_name': request.user.last_name,
         'email' : request.user.email
    })

rawDescription: How to get user data with python social auth..?

description: Get user data with python social auth..


rawDescription: Groupby in pandas timeseries dataframe choosing most recent event


rawDescription: How to get a list of tags and create new tags with python and dulwich in git?

description: Get a list of tags and create new tags with python and dulwich in git


code:
known_states = [5, 10, 15]
DF_columns = ["time", "id", "intensity"]

rawDescription: Inexpensive way to add time series intensity in python pandas dataframe


code: x = int("deadbeef", 16)

rawDescription: Convert hex string to int in Python

description: Convert hex string to int in Python


code:
np.average(np.abs(my_target - df['column1']), weights=df['weight_column'])
Out[8]: 1.9429828309434098

rawDescription: Apply a weighted average function to a dataframe without grouping it, as if it was a single group


code:
labels = Label.objects.filter(
    pk__in=LabelWords.objects.filter(word='car').values_list('label_id', flat=True)
)

rawDescription: How to annotate/aggregate each item in a list without for loops (Django)

description: Annotate/aggregate each item in a list without for loops (Django)


code:
df.set_index(['col1', 'col2'], append=True, inplace=True)
df.unstack(['col1', 'col2']).xs('value', axis=1).plot(colormap='jet')

rawDescription: Pandas colormap with groupby

description: Pandas colormap with groupby


code:
#loop through dicts using keys and rename column names in place
for k in frames.keys():
    frames[k].rename(columns=({None:'Something'}), inplace=True)

rawDescription: Rename Pandas column inside Python dictionary


code:
grouped = df.groupby("Symbol")["Close"]
for i in range(1,5):
    df["Perf{}".format(i)] = grouped.pct_change(i)

rawDescription: Can I apply a function that uses 'shift' on a grouped data frame, and return a simple data frame from pandas?

description: Apply a function that uses 'shift' on a grouped data frame, and return a simple data frame from pandas


code:
# echo 80 > /proc/sys/vm/dirty_ratio
# echo 60 > /proc/sys/vm/dirty_background_ratio

rawDescription: Bad Linux Memory Mapped File Performance with Random Access C++ & Python


code:
from mmtimer import mmtimer
import time

def tick():
    print("{0:.2f}".format(time.clock() * 1000))

t1 = mmtimer(10, tick)
time.clock()
t1.start(True)
time.sleep(0.1)
t1.stop()

rawDescription: How to implement high speed, consistent sampling?

description: Implement high speed, consistent sampling


code:
source = [[1], [2], [3]]
tpl = tuple(source)
tpl
tpl[0].append(4)
tpl
source

rawDescription: Does tuple() copy the elements of the argument?


code:
# nested.html - your second html snippet
# $scrapy shell "nested.html"

In [1]: row = hxs.select('//tr')

In [2]: row.select('td[2]').select('string()').extract()
Out[2]: [u'   some other text  ']

In [3]: row.select('td[2]').select('string()').extract()[0]
Out[3]: u'   some other text  '

In [4]: row.select('td[3]').select('string()').extract()[0]
Out[4]: u'  \r\n   yet some text  \r\n     '

rawDescription: scrapy with newline characters and nested tags

description: Scrapy with newline characters and nested tags


code:
from django.conf import settings

class Media:
    js = (settings.STATIC_URL + "js/myfile.js",
          settings.STATIC_URL + "js/myfile2.js")

rawDescription: How load Static Files in Models Admin File from Django?


code:
import dis
def f(a, b, c):
    return a < b < c
dis.dis(f)
def f(a, b, c):
    return a < b and b < c
dis.dis(f)

rawDescription: What is the operator precedence when writing a double inequality in Python (explicitly in the code, and how can this be overridden for arrays?)


code: max(z, key=lambda x:x[1])[1]

rawDescription: find maximum length of consecutive repeated numbers in a list (python)

description: Find maximum length of consecutive repeated numbers in a list (python)


code:
coords = {}
for element in elements:
    if element['type'] == 'node':
        coords[element['id']] = (element['lat'], element['lon'])

rawDescription: How to access way coordinates from a large (.json-based) list structure

description: Access way coordinates from a large (.json-based) list structure


rawDescription: Merge 2 columns in pandas into one columns that have data in python


code:
def update_widget(app, messages):
    message = next(messages, None)
    if message is None: # end of the loop
        return
    delay = int(message.time * 1000) # milliseconds
    app.after(delay, change, message) # execute body
    app.after(delay, update_widget, app, messages) # next iteration

update_widget(app, iter(listOfMessages))

rawDescription: time.sleep() Equivalent on Tkinter

description: Time.sleep() Equivalent on Tkinter


code: a_unq, a_inv, a_cnt = np.unique(a, return_inverse=True, return_counts=True)

rawDescription: Conditional numpy.cumsum?

description: Conditional numpy.cumsum


code: columns = [list(col) for col in zip(*L)]

rawDescription: Appending nested list values into a new list


rawDescription: Numpy masked operations

description: Numpy masked operations


code:
def guess(num):
    while(True):
        user_guess = int(input("Pick a number between 1 and 100: "))
        if user_guess > num:
            print("Lower\n")
        elif user_guess < num:
            print("Higher\n")
        else:
            print("Correct")
            break;

rawDescription: Using random library to guess number (Python)


code:
def overnover(fn, val):
    while True:
        val = fn(val)
        val = (yield val) or val

rawDescription: Are there already python generators that do these basic things?


code:
index = pd.date_range('2014-07-01', '2014-07-03', freq='H')
ts = ts.reindex(index, fill_value=0)

rawDescription: Making missing time slots and filling in 0 values in a Pandas series?


rawDescription: Sorting a list of list of tuples based on the sum of first field in the tuple in Python


code:
def load_npy_to_memmap(filename, dtype, shape):
    # npy format is documented here
    # https://github.com/numpy/numpy/blob/master/doc/neps/npy-format.txt
    with open(filename, 'r') as f:
        # skip magic string \x93NUMPY + 2 bytes major/minor version number
        # + 2 bytes little-endian unsigned short int
        junk, header_len = struct.unpack('<8sh', f.read(10))

    data= np.memmap(filename, dtype=dtype, shape=shape, offset=6+2+2+header_len)
    return data

rawDescription: numpy.memmap map to save file

description: Numpy.memmap map to save file


code:
def convert(file):
    import csv
    lst = []
    with open('<filename>','r') as f:
        reader = csv.reader(f)
        for row in reader:
                row.pop(0)
                if row[0] == 'B':
                        row[0] = 0
                else:
                        row[0] = 1
                lst.append(list(map(float, row)))
    return lst

rawDescription: changing given text to list of lists

description: Changing given text to list of lists


code: df1.index.get_level_values(0).values + df1.index.get_level_values(1).values * 1e-3 + df1.index.get_level_values(2).values * 1e-6

rawDescription: Convenient way to slice from multiindexed rows based on range of keys at different levels with pandas dataframe


code:
df1 = pd.read_csv("file1.csv")
df2 = pd.read_csv("file2.csv")
merged = df1.merge(df2, on="id", how="outer").fillna("")
merged.to_csv("merged.csv", index=False)

rawDescription: How do I merge two CSV files based on field and keep same number of attributes on each record?

description: Merge two CSV files based on field and keep same number of attributes on each record


code:
os.chdir('/etc')
p = pathlib.Path('../relative/path')
str(p.parent)
str(p.parent.absolute())

rawDescription: How to get folder name, in which given file resides, from pathlib.path?

description: Get folder name, in which given file resides, from pathlib.path


rawDescription: Python: Why is Xpath seemingly only processing the first element in this tree?


code:
# python-script.py
import xyz
if __name__ == '__main__':
    xyz.showdate()

rawDescription: Calling python shell apps from ruby (rails)


code: text = text.replace("jnnorm", "jnnorm MYTAGHERE")

rawDescription: How to use regular expressions on a .html file in python?

description: Use regular expressions on a .html file in python


code: text = soup.find('a').gettext()

rawDescription: pls help: how to use BeautifulSoup picking up texts wanted

description: Pls help: how to use BeautifulSoup picking up texts wanted


code:
class PastableEntry(gtksheet.ItemEntry):
    def __init__(self):
        gtksheet.ItemEntry.__init__(self)
        self.connect('paste-clipboard', self.__on_paste)

rawDescription: Handling spreadsheet data through the clipboard in GTK


rawDescription: Learn Python the Hard Way, ex60 Extra Credit 3


code: print(line.decode("utf8")) ## or some encoding; that one should print anything though

rawDescription: subprocess popen stdout

description: Subprocess popen stdout


code:
class A(object):
    def __new__(cls, *args, **kw):
        instance = super(A, cls).__new__(cls, *args, **kw)
        return [instance]

A()

rawDescription: (Python) __new__ method returning something other than class instance


rawDescription: Pandas - restructure longitudinal data in a multilevel df to a single-level column


code:
def load_or_make(filename):
    def decorator(func):
        def wraps(*args, **kwargs):
            try:
                with open(filename, 'r') as f:
                    return json.load(input_handle)
            except Exception:
                data = func(*args, **kwargs)
                with open(filename, 'w') as out:
                    json.dump(data, out)
                return data
        return wraps
    return decorator

@load_or_make(filename)
def your_method_with_arg(arg):
    # do stuff
    return data

@load_or_make(other_filename)
def your_method():
    # do stuff
    return data

rawDescription: Use decorators to retrieve jsondata if file exists, otherwise run method and then store output as json?


code:
# query = obj.template_query - dont need this
query = {}
query['name'] = 'sam'
query['age'] = '23'
obj.queries.append(query)

query2 = {} #obj.template_query-dont need this
query2['name'] = 'dj'
query2['age'] = '19'
obj.queries.append(query2)

rawDescription: issue in list of dict

description: Issue in list of dict


code:
c1=pyfits.Column(name='ID', format='E', array=M[:,0])
c2=pyfits.Column(name='RA', format='E', array=M[:,1])
c3=pyfits.Column(name='DEC', format='E', array=M[:,2])
c4=pyfits.Column(name='X', format='E', array=M[:,3])
c5=pyfits.Column(name='Y', format='E', array=M[:,4])
c6=pyfits.Column(name='Umag', format='E', array=M[:,5])
c7=pyfits.Column(name='Bmag', format='E', array=M[:,6])
c8=pyfits.Column(name='Vmag', format='E', array=M[:,7])
c9=pyfits.Column(name='Rmag', format='E', array=M[:,8])
c10=pyfits.Column(name='Imag', format='E', array=M[:,9])
cols = pyfits.ColDefs([c1, c2, c3, c4, c5, c6, c7, c8, c9, c10])

rawDescription: How is it possible in python to convert an ascii table to a fits table?


rawDescription: Extracting href ID from block of code using Python