code:
import sys

try:
    import tty, termios
except ImportError:
    # Probably Windows.
    try:
        import msvcrt
    except ImportError:
        # FIXME what to do on other platforms?
        # Just give up here.
        raise ImportError('getch not available')
    else:
        getch = msvcrt.getch
else:
    def getch():
        """getch() -> key character

        Read a single keypress from stdin and return the resulting character.
        Nothing is echoed to the console. This call will block if a keypress
        is not already available, but will not wait for Enter to be pressed.

        If the pressed key was a modifier key, nothing will be detected; if
        it were a special function key, it may return the first character of
        of an escape sequence, leaving additional characters in the buffer.
        """
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(fd)
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch

rawDescription: How can I read keyboard input in Python

description: Read keyboard input in Python


code:
buttons_to_make = [Button("First Name", self.__create_QLineEdit),
                   Button("Last Name", self.__create_QLineEdit),
                   Button("Date of Birth", self.__create_QPushButton),
                   ...]

rawDescription: Pyqt4 accessing objects when creating a dynamic form


code:
import argparse as ap

p = ap.ArgumentParser()
p.add_argument('-v', action='store_true')
p.add_argument('-l', action='store_true')
p.add_argument('files', nargs='*', action='append')

args, unknown = p.parse_known_args()
while unknown:
    args, unknown = p.parse_known_args(unknown, args)

print(args)

rawDescription: argparse optional parameter within positional

description: Argparse optional parameter within positional


rawDescription: Expanding a numpy array of matrices with zeros


code:
dest=(np.random.choice(rows,len(v),True),np.random.choice(cols,len(v),True))
dest
arr[dest]
arr[I]=0
arr[dest]=v
arr

rawDescription: How to randomly change positions of non-zero entries of an array where certain rows are excluded

description: Randomly change positions of non-zero entries of an array where certain rows are excluded


code:
s2 = 'this is an apple pie'
s2.partition('delicious')

rawDescription: How do I return a substring from the right of a specified word/phrase in Python?

description: Return a substring from the right of a specified word/phrase in Python


code:
from contextlib import contextmanager

@contextmanager
def open_many_files(filenames):
    files = [open(filename) for filename in filenames]
    try:
        yield files
    finally:
        for f in files:
            f.close()

rawDescription: Nested with blocks in Python, level of nesting variable


code:
import matplotlib.pyplot as plt

plt.clf()

for i, row in enumerate(data_with_minimum):
    plt.plot(x - mi[i], row)

plt.xlabel('offset from minimum')
plt.show()

rawDescription: Shift several graphs under each other

description: Shift several graphs under each other


code:
import pandas as pd
df1 = pd.DataFrame({'item' :  [1, 2, 3, 4],
                           'fruit' : ['mango', 'apple', 'banana', 'mango']})

df2 = pd.DataFrame({'item' : [1, 2]})

print(df1[(df1.item).isin(df2.item)])

rawDescription: Python Subset dataframe with another dataframe


code:
def num(s):
    try:
        return int(s)
    except ValueError:
        return float(s)

rawDescription: How to create lists from csv files and cast columns to ints and floats

description: Create lists from csv files and cast columns to ints and floats


code: test = [[i * j for j in range(15)] for i in range(10)]

rawDescription: Error in writing Python dictionary to a file


code:
path2 = list(set(path)) ## gets the unique values in the list

histo = []

for i in path2:
    histo.append(path.count(i)) ## add the number of occurances to the histo list

plt.bar(path2, histo)
plt.show()

rawDescription: Make histogram in python with list

description: Make histogram in python with list


code:
import Tkinter as tk

class ExampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.label = tk.Label(self, text="", width=10, font= ("Helvetica",72), background='yellow', fg = 'red')
        self.label2 = tk.Label(self, text="", width=10, font=("Helvetica",72), background='yellow', fg = 'blue')
        self.label.pack()
        self.label2.pack()
        self.intervals = [[30, "Slow"], [60, "Exercise 1"],
                          [30, "Slow"], [60, "Exercise 2"],
                          [30, "Slow"], [60, "Exercise 3"],
                          [30, "Slow"], [60, "Exercise 4"],
                          ]
        self.countdown()

    def countdown(self):
        (remaining, label) = self.intervals[0]
        self.label.configure(text=remaining)
        self.label2.configure(text=label)
        remaining -= 1
        if remaining < 0:
            self.intervals.pop(0)
        else:
            self.intervals[0][0] = remaining
        if len(self.intervals) > 0:
            self.after(1000, self.countdown)
        else:
            self.label.configure(text="Done!")

if __name__ == "__main__":
    app = ExampleApp()
    app.title("Intense Workout")
    app.geometry('550x550+200+200')
    app.configure(background='yellow')
    app.mainloop()

rawDescription: Clock Based Methods in Python TKinter Window - Please Critique


code:
{'CC DD EE', 'EE FF GG HH', 'GG', 'DD EE FF',
'FF', 'DD EE FF GG', 'BB CC DD', 'DD EE', 'FF GG',
'CC', 'FF GG HH', 'HH', 'EE FF GG', 'AA BB', 'CC DD',
'AA BB CC', 'DD', 'GG HH', 'AA', 'BB CC DD EE',
'EE FF', 'EE', 'AA BB CC DD', 'BB', 'BB CC',
'CC DD EE FF'}

rawDescription: Use python regex to match all possible groups of 1, 2, 3, and 4 words


code:
a = 1
b = 1
a is b
id(a),id(b)

rawDescription: Checking if for-loop variable resides at two indexes of a list


code:
counts = np.bincount(inv)
counts

rawDescription: Finding the difference between two numpy structured arrays


code:
def unique_values(iterable):
    it = iter(iterable)
    previous = next(it)
    yield previous
    for item in it:
        if item != previous:
            previous = item
            yield item
tuple(unique_values((1, 2, 3, 3, 4, 4, 5)))

rawDescription: Iterator Object for Removing Duplicates in Python


code:
import functools
db = functools.partial(pg_driver.connect,
                       user=DB_NAME,
                       password=DB_WORD,
                       host=DB_HOST,
                       port=DB_PORT)

rawDescription: Storing a unopened database connection in a Python variable


code:
from itertools import imap
a = [[1,2,3], [2,3,4], [3,4,5]]
reduce(set.intersection,imap(set,a))

rawDescription: Find intersection of large number of lists in python


code:
mylist = []
myset = set()
for item in ...:
    if item not in myset:
        mylist.append(item)
        myset.add(item)

rawDescription: check if a number already exist in a list in python

description: Check if a number already exist in a list in python


code:
from collections import Counter

def only_uniques(seq):
    return [k for k,n in Counter(seq).iteritems() if n == 1]

rawDescription: Fastest way to remove all multiple occurrence items from a list?


code:
insertIntoDataStruct("CS 2316", "1505", "1555", courses)
insertIntoDataStruct("CS 2316", "1405", "1455", courses)
insertIntoDataStruct("CS 2316", "1305", "1355", courses)
insertIntoDataStruct("CS 4400", "1405", "1455", courses)
insertIntoDataStruct("CS 4400", "1605", "1655", courses)

rawDescription: How to add values to existing dictionary key Python

description: Add values to existing dictionary key Python


code:
tree.xpath('//*[local-name() = "html"]')
# [<Element {http://www.w3.org/1999/xhtml}html at 0x103b8d848>]

rawDescription: XPath Child Traversal Methods and Performance


rawDescription: arrange values in a python 2D array

description: Arrange values in a python 2D array


code: ul

rawDescription: How do I sort an unordered list using BeautifulSoup 4 by a child element

description: Sort an unordered list using BeautifulSoup 4 by a child element


code:
my_vals = np.random.rand(1000)
res1 = pd.rolling_apply(my_vals, 10000, countDaysSinceHigherHigh, min_periods = 0 )
res2 = nearest_hi_value(my_vals)
np.allclose(res1, res2)

rawDescription: Pandas Series : faster way of computing periods back to preceding high


code:
class Stack(object):
    def __init__(self):
        self._stack = [] # Allocate an empty list

    def push(self, ele):
        self._stack += [ele] # Use list concatenation to emulate the `push` operation

    def pop(self):
        last = self._stack[-1:] # Return the last element of the list (also works for empty lists)
        self._stack = self.stack[0:-1] # Copy the elements from the beginning of the list to the last element of the list
        return last # return the last element

    # All stacks should have the `size` function to determine how many elements are
    # in the stack:
    def size(self):
        return len(self._stack)

    # Occasionally you'll want to see the contents of the stack, so we have to
    # implement the `__str__` magic method:
    def __str__(self):
        return '[%s]' % ','.join(map(str, self._stack))

rawDescription: Python stack simulation using list structure but not list methods


code:
class LoadingWorker(QtCore.QObject):
    [...]
    def process(self):
        self.ready.emit()
        [...]
        self.finished.emit()

loadingWorker.ready.connect(loading_progress_dialog.exec)
loadingWorker.finished.connect(loading_progress_dialog.close)

rawDescription: PyQt Dialog not responsible while thread running


code:
run = par.add_run()
run.add_picture('x.png')
run.font.subscript = True

rawDescription: How to vertically align picture in line using python-docx

description: Vertically align picture in line using python-docx


code: [[k for k in x if x[k] != y[k]] for x, y in pairs if x != y]

rawDescription: Comparing 2 lists consisting of dictionaries with unique keys in python


code:
with open("corpus.txt", encoding="utf-8") as fp:
    for line in fp:
        for word in line.split():
            process(word)

rawDescription: using unicode in python for Farsi

description: Using unicode in python for Farsi


rawDescription: What is the best way to perform an operation on several DataFrames?


code:
from django import forms
from django.db import models
from django.contrib import admin

class Foo(models.Model):
    name = models.CharField(max_length=30)

class FooAdminForm(forms.ModelForm):
    # custom field not backed by database
    calculated = forms.IntegerField()

    class Meta:
        model = Foo

class FooAdmin(admin.ModelAdmin):
    # use the custom form instead of a generic modelform
    form = FooAdminForm

    # your own processing
    def save_model(self, request, obj, form, change):
        # for example:
        obj.name = 'Foo #%d' % form.cleaned_data['calculated']
        obj.save()

admin.site.register(Foo, FooAdmin)

rawDescription: Django: Faking a field in the admin interface?


code:
test.py
import myext
myext.printx('hello world')

rawDescription: Can C programs have Python GUI?

description: Can C programs have Python GUI


code:
def queryUID(UID):
    if '-' in UID:
        return (UID.replace('-', ''), UID)
    return (UID, '-'.join([
        UID[:8], UID[8:12], UID[12:16], UID[16:20], UID[20:]]))

rawDescription: Querying portal_catalog using typed uuids instead of string uuids


rawDescription: How to convert pandas timezone aware timestamps to UNIX epoche?

description: Convert pandas timezone aware timestamps to UNIX epoche


code:
element_counts = collections.Counter(itertools.chain.from_iterable(allsets))
all_uniques = {elem for elem, count in element_counts.items() if count == 1}
#                                                     ^ viewitems() in Python 2.7
nondupes = [original & all_uniques for original in allsets]

rawDescription: Subtraction over a list of sets

description: Subtraction over a list of sets


code:
import itertools
map(''.join, itertools.product(*((c.upper(), c.lower()) for c in 'Fox')))

rawDescription: Find all upper, lower and mixed case combinations of a string


code: a == b

rawDescription: Most pythonic way of ensuring a list of objects contains only unique items


code: x not in seen and not seen.add(x)

rawDescription: Remove following duplicates in a tuple

description: Remove following duplicates in a tuple


code: pd.DataFrame({'A': A_concat_B_notin_A, 'B': df['B']})

rawDescription: Merging two columns in a DataFrame while preserving first column values


code:
c = ConfigParser.RawConfigParser()
c.readfp(open(os.path.join('/var/lib/schroot/session', chroot_session), 'r'))
chroot_basedir = c.get(chroot_session, 'mount-location')
with open(os.path.join(chroot_basedir, '/tmp/file.txt'), 'w') as fp:
    fp.write(text)

rawDescription: Writing files into chroot environment

description: Writing files into chroot environment


code:
import multiprocessing

class Test(object) :

    def __init__(self):
       self.manager = multiprocessing.Manager()
       self.some_list = self.manager.list()  # Shared Proxy to a list

    def method(self):
        self.some_list.append(123) # This change won't be lost

if __name__ == "__main__":
    t1 = Test()
    t2 = Test()
    pr1 = multiprocessing.Process(target=t1.method)
    pr2 = multiprocessing.Process(target=t2.method)
    pr1.start()
    pr2.start()
    pr1.join()
    pr2.join()
    print(t1.some_list)
    print(t2.some_list)

rawDescription: Parallel execution of class methods

description: Parallel execution of class methods


rawDescription: How to stop a Python program after some arbitrary timeout

description: Stop a Python program after some arbitrary timeout


code: TableA.end_date == None

rawDescription: Python Sqlalchemy: Filter by datediff of months


rawDescription: how extract real time form time.gov in python?

description: How extract real time form time.gov in python


code:
class Parent(object):
    boolean_attribute = True
    list_attribute = ['1', '2', '3']

class Child(Parent):
    pass

vars(Child)
vars(Parent)
Child.boolean_attribute = False
vars(Child)

rawDescription: Changing immutable type class attributes by way of an inherited static method


code:
class DeviceSettingManager(models.Manager):
    use_for_related_fields = True

    def manager_status(self):
        print("Device Setting Manager is Active")

rawDescription: How to create a custom manager for related objects?

description: Create a custom manager for related objects


code: plt.plot(x_list, y_list)

rawDescription: Pyplot - automatically setting x axis range to min, max x values passed to plotting function


code:
try:
    from setuptools import setup, Extension, Feature
except ImportError:
    from distutils.core import setup, Extension
    Feature = None

rawDescription: setup.py adding options (aka setup.py --enable-feature )

description: Setup.py adding options (aka setup.py --enable-feature )


code: s.apply(lambda x : any((i for i in x if i.find('you') >= 0)))

rawDescription: Use Pandas string method 'contains' on a Series containing lists of strings


code:
class model(object):
    @staticmethod
    def foobar(a_model, mu):
        y = a_model.y
        def _foobar(x):
            return x - mu.value + y
        return _foobar

    def __init__(self, sc):
        self.sc = sc
        self.y = -1
        self.rdd = self.sc.parallelize([1, 2, 3])

    def get_mean(self):
        return self.rdd.mean()

    def run_foobar(self):
        mu = self.sc.broadcast(self.get_mean())
        self.data = self.rdd.map(model.foobar(self, mu))

rawDescription: Proper handling of spark broadcast variables in a Python class


code:
from x import foo

def bar():
    foo.cache_clear()

rawDescription: How do I use cache_clear() on python @functools.lru_cache

description: Use cache_clear() on python @functools.lru_cache


code:
s = df.stack()
s

rawDescription: Pandas : how do I merge minute data rows in a timeseries


code: json.dumps([{"name": name} for name in names])

rawDescription: mapping dictonary a key with multiple values to json in python

description: Mapping dictonary a key with multiple values to json in python


code:
first_list = [(1,'a'),(2,'b'),(3,'c')]
second_list = [1,2,3,4,4,5]
set(second_list) - set(x[0] for x in first_list)
set(second_list) - set(map(f, first_list))

rawDescription: Referring to the first element of all tuples in a list of tuples


code:
p = re.compile(r'<img.*?/>')
data = '<img alt="" border="0" class="inlineimg" src="images/smilies/smile.png" title="Smile"/>' * 5
p.sub(lambda match: match.group().split('title=')[1].split('/')[0], data)

rawDescription: Relace emotion html tags with emotion names using Python


code:
link_name = []
fr = open('links.txt', 'r')
for link in fr:
    link_name.append(link)

fr.close()
print(link_name)

rawDescription: How do I store all the links into the variable while allowing them to be read separately? (Python)

description: Store all the links into the variable while allowing them to be read separately? (Python)


code:
import numpy as np
df = pd.DataFrame({"A": [10,20,30], "B": [20, 30, 10]})
df['new_column'] = np.multiply(df['A'], df['B'])
df

rawDescription: Applying function with multiple arguments to create a new pandas column


code:
for line in (line for line in x if not line.startswith('?')):
    DO_STUFF

rawDescription: What is the easiest way to get all strings that do not start with a character?


code: [m.group(1) for m in re.finditer(r"@(\w+)", "@dasweo where you at?")]

rawDescription: Simple regex to get a twitter username from a string


code:
from itertools import cycle, izip

l = ['a','b','c']
it = iter(l)
import csv
with open('in.csv', 'rU') as c:
    rows = csv.reader(c)
    for a, row in izip(cycle(l), rows):
        print([a]+ row)

rawDescription: Use repeating iterator in python to insert item into list


code:
if row[0][0:].find('OPSURVEYLEVEL') == 0:
    break

rawDescription: How do I cycle through a csv in python, writing lines to a new file that meet new criteria

description: Cycle through a csv in python, writing lines to a new file that meet new criteria


code:
# e.g. in my_app/templatetags/fraction_tags.py

@register.filter
def pretty_fraction(value):
    if value.denominator == 1:
        return unicode(value.numerator)
    else:
        return unicode(value)

rawDescription: is it possible to override django's {{ }} for specific object?

description: Is it possible to override django's {{ }} for specific object


code:
def nested_sum(L):
    total = 0
    for x in L:
        total += nested_sum(x) if isinstance(x, list) else x
    return total

rawDescription: What would be the non-comprehension list way of doing this sum


code: bin_means = np.array([ x[inds==bin_idx].mean() for bin_idx in inds ])

rawDescription: can numpy's digitize function output the mean or median?

description: Can numpy's digitize function output the mean or median


rawDescription: How do I make the paddle move with the arrow keys?

description: Make the paddle move with the arrow keys


code:
checkpoint = tools.Checkpoint()
checkpoint.load("my_checkpoint.ems")
population = checkpoint["population"]

# Continue the evolution has in before

rawDescription: Resuming an optimization with pyevolve

description: Resuming an optimization with pyevolve


code:
import decorator

@decorator.decorator
def log_method(func, *args, **kwargs):
    # allows me to put the wrapper in helper methods that are sometimes major steps
    if check_for_True_in_last_arg(*args):
        log.debug('''Last arg is "True" which could indicate the caller
intends the log to work on this method. For that behaviour, use the keyword arg:  log_this=True''')

    if 'log_this' in kwargs and kwargs['log_this'] is not False:
        args_name = inspect.getargspec(func)[0][1:] #drop the 'self' arg
        args_dict = dict(list(itertools_izip(args_name, args)) + list(kwargs.iteritems()))
        method_name = wrapper.__name__
        args_dict_string = '\nMETHOD: ' + method_name + '\n'

        for k,v in args_dict.iteritems():
            if type(v) is not np.ndarray:
                args_dict_string += "%s: %s\n" %(k,v)

            elif type(v) is np.ndarray:
                args_dict_string += "NUMPY ARRAY:%s \nSHAPE:%s\n" %(k, str(v.shape))

        args_dict_string += '\n'
        log.info(args_dict_string)

    return func(self, *args, **kwargs)

class Tools(object):
    @log_method
    def save_new_image(self, data, header, outfile, coordmap):
        """
        Jazz the snaz output baz
        """

rawDescription: Python: functools, wrapper decorator for methods: make ipython return proper definition when using '?'


rawDescription: Python regular expressions (extracting monetary value from a string)


code:
with open("CountryCodes.csv",'r') as CountryCodes:
    genc = (line.split(',') for line in CountryCodes)
    lic = [map(str.strip,row) for row in genc]

def name_of(x,lic=lic):
  for code,name,continent in lic:
    if x==code:
      return name

with open("CountryData.csv",'r') as CountryData:
    gend = (line.split(',') for line in CountryData)

    the_data = [ (name_of(d_fields[0].strip()), # data_country_code
                  int(d_fields[1]),         # country_pop
                  d_fields[2].strip(),      # country_area
                  d_fields[3].strip(),      # country_gdp
                  d_fields[4].strip())      # country_lit_rate
                 for d_fields in gend ]

rawDescription: Comparing in lists in Python

description: Comparing in lists in Python


rawDescription: How to write data to a binary file using Python

description: Write data to a binary file using Python


code:
def update_object(ticks):
    object.x += float(x_speed) * ticks / 1000  # converting to float is needed

# this is your main game loop
time = pygame.time.Clock()
ticks = 0
while running:
    ...
    update_object(ticks)
    ticks = time.tick(30)  # this 30 means time.tick() waits enough to make your max framerate 30

rawDescription: Pygame move object position from one point to another at constant speed


rawDescription: Convert this python dictionary into JSON format?


code:
with open('filename', 'r') as f: data = f.read()

keys = {k: v for k, v in [i.split('=') for i in data.split('&')]}

result = {}
for key, value in keys.items():
    if key == 'kv1':
        result[key] = int(value)

rawDescription: Aggregating key value pair in python

description: Aggregating key value pair in python


code:
f = file("in.txt")
d = {}
while True:
    line = f.readline()
    if not line:
        break
    c0,c1 = line.split()
    d[c0] = c1

rawDescription: Can You Save Data To A Dictionary From A Text Document?


rawDescription: Python regex partial extract

description: Python regex partial extract


code:
from random import randint

class RandomSparseList(object):
    def __init__(self, size, min_bits, max_bits):
        self.size = int(size)
        self.bits = {}
        self.bits_set = randint(min_bits, max_bits)
        while self.bits_set > len(self.bits):
            self.bits[randint(0, self.size)] = 1

    def __len__(self):
        return self.size

    def __getitem__(self, index):
        if index < 0 or index >= self.size:
            raise IndexError
        return self.bits.get(int(index), 0)

    def __iter__(self):
        for i in xrange(self.size):
            yield self.__getitem__(i)

    def __contains__(self, index):
        return index in self.bits

    def __repr__(self):
        return '[{}]'.format(', '.join(str(x) for x in self))

    def set_bits(self):
        return self.bits.keys()

rawDescription: Populating a sparse list with random 1's


code:
from importlib import import_module

def load_plugin(name):
    m = import_module('Settings.'+name)
    return getattr(m, name)

rawDescription: Accessing classes from arbitrary modules inside of package directly


rawDescription: Concise vector adding in Python?

description: Concise vector adding in Python


code:
foo = 'foo'
bar = 'bar'
if funcname == 'a':
  func = a  # a takes no args
elif funcname == 'b':
  func = lambda: bb(bar=bar)  # bb takes bar
elif funcname == 'c':
  func = lambda: ccc(foo=foo)  # ccc takes foo
# call it, uniformly
func()

rawDescription: Python: Passing conditional parameters by name


code:
class freezablelist(list):
    def __init__(self,*args,**kwargs):
        list.__init__(self, *args)
        self.frozen = kwargs.get('frozen', False)

    def __setitem__(self, i, y):
        if self.frozen:
            raise TypeError("can't modify frozen list")
        return list.__setitem__(self, i, y)

    def __setslice__(self, i, j, y):
        if self.frozen:
            raise TypeError("can't modify frozen list")
        return list.__setslice__(self, i, j, y)

    def freeze(self):
        self.frozen = True

    def thaw(self):
        self.frozen = False

rawDescription: Freeze in Python?

description: Freeze in Python


code:
table = soup.find("table", id = "history")
rows = table.findAll('tr')
data = [[td.findChildren(text=True) for td in tr.findAll("td")] for tr in rows]
# data now contains:
[[u'Google', u'07/11/2001'],
 [u'Apple', u'27/08/2001'],
 [u'Microsoft', u'01/11/1991']]

# If the data may contain extraneous whitespace you can clean it up
# Additional processing could also be done - but once you hit much more
# complex than this later maintainers, yourself included, will thank you
# for using a series of for loops that call clearly named functions to perform
# the work.
data = [[u"".join(d).strip() for d in l] for l in data]

# If you want to store it joined as name | company
# then simply follow that up with:
data = [u"|".join(d) for d in data]

rawDescription: How do I extract table data in pairs using BeautifulSoup?

description: Extract table data in pairs using BeautifulSoup


rawDescription: Python graphics module

description: Python graphics module


rawDescription: How to make pyes search method to return the keys of the found documents?

description: Make pyes search method to return the keys of the found documents


rawDescription: Creating a palindrome list with reverse()


rawDescription: Populating dictionary from text file PYTHON


code:
bla = 'hannahannahskdjhannahannah'
cnt = 0
idx = 0
while True:
    idx = bla.find('hannah', idx)
    if idx >= 0:
        cnt += 1
        idx += 1
    else:
        break
print(cnt)

rawDescription: Python: Count overlapping substring in a string


code:
@property
def json_as_json(self):
  return json.dumps(self.json)

rawDescription: App Engine return JSON from JsonProperty


rawDescription: Python dictionary with list keyword

description: Python dictionary with list keyword


rawDescription: numpy type conversions for arrays vs. scalars

description: Numpy type conversions for arrays vs. scalars


code:
items = ['platypus duck','platypus duck','platypus duck','platypus duck','cat','dog','platypus duck','elephant','cat','cat','dog','bird','','','cat','dog','bird','cat','cat','cat','cat','cat','cat','cat']
values, counts = np.unique(items, return_counts=True)
values
counts

rawDescription: Sorting a scipy.stats.itemfreq result containing strings


code:
import requests

req = requests.Request('POST','http://stackoverflow.com',headers={'X-Custom':'Test'},data='a=1&b=2')
prepared = req.prepare()

def pretty_print_POST(req):
    """
    At this point it is completely built and ready
    to be fired; it is "prepared".

    However pay attention at the formatting used in
    this function because it is programmed to be pretty
    printed and may differ from the actual request.
    """
    print('{}\n{}\n{}\n\n{}'.format(
        '-----------START-----------',
        req.method + ' ' + req.url,
        '\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),
        req.body,
    ))

pretty_print_POST(prepared)

rawDescription: Python requests - print entire http request (raw)?


code:
s = 'aabcc'
for x,y in (('ab','b'), ('aa','a')):
    s = s.replace(x,y)

s

rawDescription: translate 'aa' to 'a' and 'ab' to 'b' in python

description: Translate 'aa' to 'a' and 'ab' to 'b' in python


rawDescription: Finding Combinations to the provided Sum value


code: x = np.maximum(x, y)

rawDescription: Find the maximum x,y value fromn a series of images


rawDescription: Iterating over expanding list / nested for loops


rawDescription: Python dict.get() with multidimensional dict


code:
def _flatten_items(items, sep, prefix):
  _items = []
  for key, value in items:
    _prefix = "{}{}".format(prefix, key)
    if isinstance(value, list):
      _items.extend(_flatten_items(list(enumerate(value)), sep=sep,
                    prefix=_prefix+sep))
    elif isinstance(value, dict):
      _items.extend(_flatten_items(value.items(), sep=sep,
                    prefix=_prefix+sep))
    else:
      _items.append((_prefix, value))
  return _items

def flatten_dict(d, sep='_'):
  return dict(_flatten_items(d.items(), sep=sep, prefix=""))

rawDescription: Iterate through Nested, mixed dictionary


code:
s = '901 R 902 M 903 Picture_message 904 NA 905 F 906 Local_Relay 907 46 908 51705 909 306910001112/TYPE=PLMN@mms.cosmote.gr'
pieces = s.split()
' '.join('_'.join(pieces[i:i+2]) for i in xrange(0, len(pieces), 2))

rawDescription: Python replace every odd occurrence of space with "_"


rawDescription: Python Tkinter puzzling result

description: Python Tkinter puzzling result


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

N = 1000
n = N/10
s = np.random.normal(size=N)   # generate your data sample with N elements
p, x = np.histogram(s, bins=n) # bin it into n = N/10 bins
x = x[:-1] + (x[1] - x[0])/2   # convert bin edges to centers
f = UnivariateSpline(x, p, s=n)
plt.plot(x, f(x))
plt.show()

rawDescription: Plotting probability density function by sample with matplotlib


code:
import lxml.etree as etree

ns = {
    'sbml': 'http://www.sbml.org/sbml/level2/version4',
    'rdf':'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
    'body':'http://www.w3.org/1999/xhtml',
    're': 'http://exslt.org/regular-expressions',
    }

def extract_name_and_chebi(node):
    chebies = node.xpath("""
        .//rdf:li[
        starts-with(@rdf:resource, 'urn:miriam:obo.chebi')
        ]/@rdf:resource
        """, namespaces=ns)
    return node.attrib['name'], chebies[0]

with open('yeast_4.02.xml') as xml:
    tree = etree.parse(xml)

    maybemeta = tree.xpath("""
        //sbml:speciesType[descendant::rdf:li[
        starts-with(@rdf:resource, 'urn:miriam:obo.chebi')]]
        """, namespaces = ns)

    with open('metabolites.txt', 'w') as output:
        for node in maybemeta:
            output.write('%s\t%s\n' % extract_name_and_chebi(node))

rawDescription: Xpath select attribute of current node?

description: Xpath select attribute of current node


rawDescription: Slick way to reverse the (binary) digits of a number in Python?


code:
def link_exception(self, callback, SpawnedLink=FailureSpawnedLink):
        """Like :meth:`link` but *callback* is only notified when the greenlet dies because of unhandled exception"""
        self.link(callback, SpawnedLink=SpawnedLink)

rawDescription: Progress Bar and gevent

description: Progress Bar and gevent


code:
from Tkinter import *

#------------------------------------

def my_tracer(a, b, c): # trace send 3 arguments to my_tracer
    #print a, b, c

    # using StringVar to get and set text
    new_text = e1_var.get() + e2_var.get()
    t_var.set(new_text)

#------------------------------------

top = Tk()

#---

t_var = StringVar() # or StringVar(top)

t = Label(top, textvariable=t_var)
t.pack()

#---

e1_var = StringVar() # or StringVar(top)
e1_var.trace('w', my_tracer) # run my_tracer if value was changed (w = write)

e1 = Entry(top, textvariable=e1_var)
e1.pack()

#---

e2_var = StringVar() # or StringVar(top)
e2_var.trace('w', my_tracer) # run my_tracer if value was changed (w = write)

e2 = Entry(top, textvariable=e2_var)
e2.pack()

#---

top.mainloop()

#------------------------------------

rawDescription: Constantly Update Label Widgets From Entry Widgets TKinter


code:
['Mozilla/* (X*; Linux i*) AppleWebKit/* (KHTML, like Gecko) Ubuntu/* Chromium/* Chrome/* Safari/*',
'Mozilla/*(iPad; U; CPU iPhone OS * like Mac OS X; en-us) AppleWebKit/* (KHTML, like Gecko) Version/* Mobile/* Safari/*',
'Mozilla/* (Windows; U; Windows NT *; pt-PT; rv:*) Gecko/* Firefox/* (.NET CLR *)']

rawDescription: Regex for identifying version numbers - Python


code:
hov = ActionChains(driver).move_to_element(data)
hov.perform()

rawDescription: How can I locate a onmouseover element using Selenium in Python?

description: Locate a onmouseover element using Selenium in Python


code:
import re
s = r'{"data1": {"data1_inside": "bla{bl\"a"}}{"data1": {"data1_inside": "blabla["}}{"data1": {"data1_inside": "bla{bla"}}{"data1": {"data1_inside": "bla["}}'
r = re.split('(\{.*?\})(?= *\{)', s)

rawDescription: Parse multiple json objects that are in one line


code:
a_set = {t[:-1] for t in a}
result = [t for t in b if t[:-1] not in a_set]

rawDescription: compare few elements in list of tuples

description: Compare few elements in list of tuples


rawDescription: pandas dataframe aggregate - why does it return column names?

description: Pandas dataframe aggregate - why does it return column names


code:
def sum_digits3(n):
   r = 0
   while n:
       r, n = r + n % 10, n // 10
   return r

rawDescription: Sum the digits of a number - python

description: Sum the digits of a number - python


rawDescription: Randomly sample rows (first dimension) of an array with unknown dimension


code:
sorted_lists = [d[k] for k in sorted(d)]
tuples = izip_longest(*sorted_lists)
result = [i for t in tuples for i in t if i is not None]
print(result)

rawDescription: iterating through dictionary with list as values in python

description: Iterating through dictionary with list as values in python


code:
a = []
nums = raw_input("Enter input- ")
for n in nums.split():
    n = int(n)
    if n < 0:
        break
    a.append(n)

rawDescription: How to terminate input as a list when a negative integer is entered? (python)

description: Terminate input as a list when a negative integer is entered? (python)


code:
your_list = ['bread', 'milk', 'sugar', 'tea', 'eggs', 'shampoo', 'clothes', 'tiger', 'beads', 'washing machine', 'juice', 'mixed herbs']

print("{0:20}    {1:20}    {2:20}    {3:20}\n".format('Column 1', 'Column 2', 'Column 3', 'Column 4'))
i = 0
for x in range(0, 3):
    print("{0:20}    {1:20}    {2:20}    {3:20}\n".format(your_list[i], your_list[i + 1], your_list[i + 2], your_list[i + 3]))
    i += 4

rawDescription: Creating a table out of data in python

description: Creating a table out of data in python


code: full_list = [x.lstrip() for x in test.split(',')]

rawDescription: Add characters (',') every time a certain character ( , )is encountered ? Python 2.7.3


code:
ignored_files = ['baz.a',
                 'foo.a',
                 'xyzzy.a',
                 ]
projfiles['buildasm'] = ['#build/'+os.path.splitext(x)[0]+'.asm' for x in projfiles['a'] if x not in ignored_files]

rawDescription: python: how to get all members of an array except for ones that match a condition

description: Python: how to get all members of an array except for ones that match a condition


code:
class InlineButtonWidget(object):
    html = """
    <button type="submit" title="%s"><span>%s</span></button>
    """

    def __init__(self, input_type='submit'):
        self.input_type = input_type

    def __call__(self, field, **kwargs):
        kwargs.setdefault('id', field.id)
        kwargs.setdefault('type', self.input_type)
        if 'value' not in kwargs:
            kwargs['value'] = field._value()
        return HTMLString(self.html % (field.name, field.lable ))

rawDescription: Flask-WTF uses input=submit instead of button type=submit


code:
try:
    decompressor = zlib.decompressobj()
    data = decompressor.decompress(response_body)
except zlib.error:
    decompressor = zlib.decompressobj(-zlib.MAX_WBITS)
    data = decompressor.decompress(response_body)

rawDescription: HTTP response body is obfuscated

description: HTTP response body is obfuscated


code:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST='smtp.gmail.com'
EMAIL_PORT=587
EMAIL_HOST_USER='your gmail user name'
EMAIL_HOST_PASSWORD='your gmail password'
EMAIL_USE_TLS = True
EMAIL_USE_SSL = True

rawDescription: How to send email in Django to register account?

description: Send email in Django to register account


code:
def rendered_games_list(self, sort_by=None):
    games_query = Game.all()
    if sort_by:
        # I winged this bit, you didn't specify the ORM used
        games_query = games_query.order_by(sort_by)
    template = JINJA_ENVIRONMENT.get_template(games_list_template)
    return template.render(gl=games_query.run())

rawDescription: Update Jinja2 template variables

description: Update Jinja2 template variables


code:
import scrapy
from scrapy.contrib.spiders import CrawlSpider
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

class CompItem(scrapy.Item):
    model_name = scrapy.Field()
    model_link = scrapy.Field()
    url = scrapy.Field()

class criticspider(CrawlSpider):
    name = "extract"
    allowed_domains = ["mysmartprice.com"]
    start_urls = ["http://www.mysmartprice.com/computer/lenovo-g50-70-laptop-msf201821"]

    def __init__(self, *args, **kwargs):
        super(criticspider, self).__init__(*args, **kwargs)
        self.download_delay = 0.25
        self.browser = webdriver.Firefox()
        self.browser.maximize_window()

        self.browser.implicitly_wait(20)

    def parse_start_url(self, response):
        self.browser.get(response.url)

        # waiting for "Go to store" to become visible
        wait = WebDriverWait(self.browser, 10)
        wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.store_pricetable")))

        main_window = self.browser.window_handles[0]

        # iterate over featured stores and visit them
        for store in self.browser.find_elements_by_css_selector("div.store_pricetable"):

            item = CompItem()

            # shift+click on the "Go to Store" link
            link = store.find_element_by_css_selector("div.store_gostore > div.storebutton")
            ActionChains(self.browser).key_down(Keys.SHIFT).move_to_element(link).click(link).key_up(Keys.SHIFT).perform()

            # there is a popup preventing us to navigate to the store URL - close it
            try:
                popup_close = self.browser.find_element_by_css_selector(".popup-closebutton")
                popup_close.click()

                # repeat the click
                ActionChains(self.browser).key_down(Keys.SHIFT).move_to_element(link).click(link).key_up(Keys.SHIFT).perform()
            except NoSuchElementException:
                pass

            # switch to the newly opened window, read the current url and close the window
            self.browser.switch_to.window(self.browser.window_handles[-1])

            # wait until "On your way to the store" would not be in title
            wait.until(lambda browser: "On your way to the Store" not in browser.title)

            item['url'] = self.browser.current_url
            self.browser.close()

            # switch back to the main window
            self.browser.switch_to.window(main_window)

            yield item

rawDescription: Selenium not working to switch tabs and extracting url


code:
import requests
url = 'http://urlshortener.com/applebanana'
t = requests.get(url)
print(t.url)

rawDescription: Find tiny url final redirected url

description: Find tiny url final redirected url


code:
line = 'a+b+c+d'
line.split('+')

rawDescription: In line.split('+')[-1] what does the -1 in the square brackets indicate in Python


code:
import collections
import flask

class MyRequest(flask.Request):
    """A subclass of Request that maintains function call counts."""
    def __init__(self, *args, **kwargs):
        super(MyRequest, self).__init__(*args, **kwargs)
        self.call_counts = collections.defaultdict(int)

rawDescription: Python decorator to limit number of calls


code:
specificuser = "fred"
src = os.path.join("G:\\users", specificuser, "Documents")
dst = os.path.join("D:\\users", specificuser, "Documents")
print(src)
print(dst)

rawDescription: Python: recursively copy batch of directories


code:
import requests
from urllib2 import urljoin
filename = "%s_%s_.exe" % (software.capitalize(),version)
if not os.path.exists(os.path.join(OUTPUT_FOLDER, filename)):
    with open(filename, "wb") as fp:
        fp.write(requests.get(urljoin(LINK, version + "_multi.exe")).content)

rawDescription: Python skip a function if the file already exists


code:
def get_group_pairs(grp):
    pairs = combinations(grp.index, 2)
    data = [grp.loc[p, ('Name', 'Z')].values.flatten('F') for p in pairs]
    return pd.DataFrame(data, columns=['Name1', 'Name2', 'Z1', 'Z2'])

def merge_zpairs(grp):
    data = {'Z1': grp['Z1'].tolist(), 'Z2': grp['Z2'].tolist()}
    return pd.Series(data)

namepairs = df.groupby('Frame').apply(get_group_pairs).reset_index(level=1, drop=True)
zpairs = namepairs.groupby(['Name1', 'Name2']).apply(merge_zpairs).reset_index()

rawDescription: How to Process Row Pairs Preserving an ID Column without Dictionaries using Pandas?


code:
def create_dict(keys):
    d = {}
    for key in keys:
        d[key] = 0
    return d

rawDescription: save and update a dictionary

description: Save and update a dictionary


rawDescription: Get HTML Source of WebElement in Selenium WebDriver using Python


code: open('./testing.json','w').write("[%s]" % ",\n ".join(map(json.dumps, d)))

rawDescription: Python JSON dump a list of object.

description: Python JSON dump a list of object.


code:
weights_list = [weights[f] for f in self.__fields]
as_lists = [[d[f] for f in self.__fields] for d in _list]
aux1, aux2 = min(combinations(as_lists, 2),
                 key=lambda pair: sum(w * (x - y) ** 2
                                      for w, x, y in zip(weights_list, *pair)))

aux1, aux2 = (dict(zip(self.__fields, x)) for x in (aux1, aux2))

rawDescription: Compress dict sum statement with Python

description: Compress dict sum statement with Python


code:
no_background=[]
for i in range(num_frames):
    no_background.append(orig[:,i]-np.median(orig,1))
no_background=np.array(no_background).T

rawDescription: How can I extract this obvious event from this image?

description: Extract this obvious event from this image


code:
import pandas as pd
import sqlalchemy
import psycopg2

engine = sqlalchemy.create_engine("postgresql+psycopg2://...")

the_frame = pd.read_sql_query("SELECT * FROM %s;" % name_of_table, engine)
the_frame = pd.read_sql_table(name_of_table, engine)

rawDescription: Getting data from table in database

description: Getting data from table in database


rawDescription: Pandas multiIndex DataFrame sort

description: Pandas multiIndex DataFrame sort


code:
# Try to import the C implementation of StringIO; if that doesn't work
# (e.g. in IronPython or Jython), import the pure Python version.
# Make sure the imported module is called StringIO locally.
try:
    import cStringIO as StringIO
except ImportError:
    import StringIO

rawDescription: Conventions for 'import ... as'

description: Conventions for 'import ... as'


code:
class bidict(dict):
    def __setitem__(self, key, value):
        super(bidict, self).__setitem__(key, value)
        super(bidict, self).__setitem__(value, key)

    def __delitem__(self, key):
        super(bidict, self).__delitem__(self[key])
        super(bidict, self).__delitem__(key)

rawDescription: Finding key from value in Python dictionary:


code:
def distribute_function(tokens):
    # unpack function name and arguments
    fname, args = tokens[0]

    # if args contains an expression, expand it
    if len(args) > 1:
        ret = ParseResults([])
        for i,a in enumerate(args):
            if i % 2 == 0:
                # even args are operands to be wrapped in the function
                ret += ParseResults([ParseResults([fname,ParseResults([a])])])
            else:
                # odd args are operators, just add them to the results
                ret += ParseResults([a])
        return ParseResults([ret])
function_call.setParseAction(distribute_function)

rawDescription: How to get pyparser to work in a particular form

description: Get pyparser to work in a particular form


code:
from functools import partial

def foo(self):
    print(self.toString())

class Node:
    def __init__(self, myFoo):
        self.myFoo = partial(myFoo, self)

    def run(self):
        self.myFoo()

    def toString(self):
        return "Hello World"

n = Node(foo)
n.run()

rawDescription: python pass self to referenced function

description: Python pass self to referenced function


rawDescription: how to convert two-dimensional list to another list with dict as elements

description: Convert two-dimensional list to another list with dict as elements


code:
dicts.collectAsMap()
## {'bar': {'x': None, 'y': None, 'z': 4},
##     'foo': {'x': None, 'y': 3, 'z': None}}

rawDescription: How to return a dictionary in parallel processing in spark?

description: Return a dictionary in parallel processing in spark


code:
def gen():
    B1 = [1,2,3]
    B2 = [7,8]
    C = [x for x in range(10) if x not in B1 + B2 ]
    def gen1():
        for x in C:
            c = C[:]
            b1 = B1[:]
            b1.append(x)
            c.remove(x)
            for y in c:
                c1 = c[:]
                b2 = B2[:]
                b2.append(y)
                c1.remove(y)
                yield(b1, b2, c1)
    def gen2():
        for i in range(len(C)-1):
            for j in range(i+1, len(C)):
                b2 = B2 + [C[i], C[j]]
                c = [C[k] for k in range(len(C)) if k not in (i,j)]
                yield (B1, b2, c)
    def gen3():
        for x in C:
            b2 = B2[:]
            c = C[:]
            c.remove(x)
            b2.append(x)
            yield(B1, b2, c)
    for g in (gen1, gen2, gen3):
        for t in g():
            yield t

rawDescription: Set partition with constraints in Python


code:
if math.isnan(b):
  #case of overflow
  b = -999999

J =  (a-b)/m

return J/m

rawDescription: fmin with really poor performance. fmin_bfgs with precision loss. Minimization does not fit well

description: Fmin with really poor performance. fmin_bfgs with precision loss. Minimization does not fit well


code:
print("walrus", end=" ")
sys.stdout.flush()

rawDescription: Python waits to print incomplete lines?

description: Python waits to print incomplete lines


code: print('[' + ', '.join(your_list) + ']')

rawDescription: Remove single quotes from python list item


code:
def merge(A, B, f):
    # Start with symmetric difference; keys either in A or B, but not both
    merged = {k: A.get(k, B.get(k)) for k in A.viewkeys() ^ B.viewkeys()}
    # Update with `f()` applied to the intersection
    merged.update({k: f(A[k], B[k]) for k in A.viewkeys() & B.viewkeys()})
    return merged

rawDescription: Python merge dictionaries with custom merge function


code:
import json
import sys
from pkgutil import simplegeneric

@simplegeneric
def get_items(obj):
    while False: # no items, a scalar object
        yield None

@get_items.register(dict)
def _(obj):
    return obj.iteritems() # json object

@get_items.register(list)
def _(obj):
    return enumerate(obj) # json array

def strip_whitespace(json_data):
    for key, value in get_items(json_data):
        if hasattr(value, 'strip'): # json string
            json_data[key] = value.strip()
        else:
            strip_whitespace(value) # recursive call

data = json.load(sys.stdin) # read json data from standard input
strip_whitespace(data)
json.dump(data, sys.stdout, indent=2)

rawDescription: Remove whitespaces and newlines from JSON file


code:
from fabric.api import *
from fabric.tasks import Task

class BaseConfig(Task):
    """BaseConfig task docstring"""
    def run(self):
        """BaseConfig task docstring for `run` method"""
        env.env = self.name
        env.user = 'serafeim'
        env.role = self.name
        puts("Using {0} configuration with user {1} at {2}".format(env.env, env.user, env.roledefs[env.env]))

class UatConfig(BaseConfig):
    """
    UAT settings
    """
    name = "uat"
uat_cfg_instance = UatConfig()

class ProdConfig(BaseConfig):
    """
    Prod settings

    talking a bit more about our classy concept, not yet diving into `run` details
    """
    name = "prod"
    def run(self):
        """ProdConfig run docstring

        on multiple lines
        we are now in prod"""
        super(ProdConfig, self).run()

prod_cfg_instance = ProdConfig()

rawDescription: Fabric docstring for classy tasks

description: Fabric docstring for classy tasks


code:
from itertools import product
[x for x in product(range(10),range(10)) if sum(x)==10]

rawDescription: Python: Generate matrix of Nx4 integers who sum is a constant


code: Bit("key","secret", "net").run()

rawDescription: Creating a list in python using the same string but with different values attached to it


code: from google.appengine import api as gaeapi

rawDescription: Making python imports more structured?

description: Making python imports more structured


code:
name_list = [['John', 'Smith'], ['Sarah', 'Nichols'], ['Alex', 'Johnson'], ['Alex', 'Johnson']]

color_list = ['Red', 'Green', 'Blue']

i = 2
for ind, sub in enumerate(name_list):
    sub.append(color_list[ind % i])

print(name_list)

rawDescription: Python append value at array increment to a list


code:
for i, line in enumerate(data_list):
    if i > 0:
        between_items()
    item()

rawDescription: What is the pythonic way to detect the last element in a python 'for' loop?


code:
def all(self):
    return self.get_queryset()

rawDescription: Django ORM - objects.filter() vs. objects.all().filter() - which one is preferred?


rawDescription: Python inner parenthetic string parsing

description: Python inner parenthetic string parsing


code: a, b, c = np.zeros((3,2), object)

rawDescription: assigning an object to multiple variables - python

description: Assigning an object to multiple variables - python


code: np.sum(f**2, axis=1)

rawDescription: Parallel array manipulations in numpy

description: Parallel array manipulations in numpy


code:
images = Image.objects.all().prefetch_related('favorited')
for image in images:
    image.favorited = image.favorited.filter(user=current_user).exists()

rawDescription: Displaying images, and next to every image display if it has been favorited by the user


code:
@class_or_instance
def method(self, verbose=False, _async_method_name=k, *args, **kwargs):
    response = getattr(self, _async_method_name)(*args,**kwargs)
    result = self._parse_result(response, verbose=verbose)
    return result

cls.__dict__[methodname] = method

rawDescription: Add methods to a class generated from other methods


code:
arr = StringInside('abcdefg')
arr[2]
arr[1:3]

rawDescription: subclass str, and make new method with same effect as +=

description: Subclass str, and make new method with same effect as +=


code: socketIO.emit('gaze', ...)

rawDescription: Code in Python, communicate in Node.js and Socket.IO, present in HTML


code:
try:
    store.get('credentials')['username']
except KeyError:
    username = ""
else:
    username = store.get('credentials')['username']

try:
    store.get('credentials')['password']
except KeyError:
    password = ""
else:
    password = store.get('credentials')['password']

rawDescription: Saving login screen username and password for Kivy app


code: ts = pd.Series(df['Value'].values, index=df['Date'])

rawDescription: pandas.Series() Creation using DataFrame Columns returns NaN Data entries

description: Pandas.Series() Creation using DataFrame Columns returns NaN Data entries


code:
#make some data
N = 22
data = np.random.randint(1,10,N)
bin = np.arange(N)
width = 1

#plot it
ax = plt.subplot(111)
ax.bar(bin, data, width, color='r')
plt.show()

rawDescription: Annoying white space in bar chart (matplotlib, Python)


code:
from Frame2 import fr
def OnButton4Button(self, event):
    fr.Show()
    event.Skip()  # you need to spell event correctly

rawDescription: call a Frame2 from a button in Frame1

description: Call a Frame2 from a button in Frame1


code: len(net.layers[0].blobs)    # >> 0

rawDescription: Finding gradient of a Caffe conv-filter with regards to input


code: Contact.PhoneNumber_ref_contact_to_Contact == (PhoneNumber, PhoneNumber.contact)

rawDescription: Following backreferences of unknown kinds in NDB


code: reindex = np.argsort(matrix.flatten())

rawDescription: Sorting cells of big numpy matrices

description: Sorting cells of big numpy matrices


rawDescription: What is the Time Complexity of finding the max k integers?


code:
import numpy as np
x = np.arange(100).reshape(10, 10)
x
sum(x)
x.sum()
np.sum(x)

rawDescription: Python script returns an array instead of a single value


code:
# DF TO EXCEL
from pandas import ExcelWriter

writer = ExcelWriter('PythonExport.xlsx')
yourdf.to_excel(writer,'Sheet5')
writer.save()

# DF TO CSV
yourdf.to_csv('PythonExport.csv', sep=',')

rawDescription: Pandas Dataframe to excel sheet

description: Pandas Dataframe to excel sheet


code:
def foo4(annot, simi_matrix_word):
    N = annot.shape[0]
    A = annot.astype(bool)
    simi_matrix_img = np.eye(N,dtype=float)
    for i in range(1,N):
        x = simi_matrix_word[A[i],:]
        for j in range(i):
            score = x[:,A[j]]
            simi_matrix_img[i, j] = score.mean()
    return simi_matrix_img

rawDescription: How can I accelerate the array assignment in python?

description: Accelerate the array assignment in python


rawDescription: Classes in python, get attribute

description: Classes in python, get attribute


code:
label_counts = np.bincount(label)
label_sums = np.histogram2d(np.repeat(label, data_length),
                            np.tile(np.arange(data_length), k),
                            bins=(k, data_length),
                            weights=data.ravel())[0]
c = label_sums / label_count[:, None]

rawDescription: A quick way to compute the average of multiple timeseries?


rawDescription: Extract attributes and certain tag values from xml using python script


code: (r'^admin/', include(admin.site.urls)),

rawDescription: Get the version of Django for application


rawDescription: how to format list and assign them seperately?

description: Format list and assign them seperately


code:
A = [1, 2, 3, 4]
A = [a for a in A if a < 4]       # creates new list and evaluates each element of old
A

rawDescription: Removing elements from a List in Python

description: Removing elements from a List in Python


code:
def createWall():
    global boundry
    boundry=[]
    for i in range(0,Window_Width):
        boundry.append((i,0))
        boundry.append((i,Window_Height-18))
    for i in range(0,Window_Height):
        boundry.append((0,i))
        boundry.append((Window_Width-18,i))

def drawWall():
    for each in boundry:
        wallRect = pygame.Rect(each[0],each[1], Cell_Size-2, Cell_Size-2)
        pygame.draw.rect(DISPLAYSURF, DARKGreen, wallRect)

rawDescription: Pygame : Snake game How to make making snake go throught walls or creating a fram around the edge?


rawDescription: Rounding of variables

description: Rounding of variables


code:
def convert_to_decimal(degrees, arcminutes, arcseconds):
    '''
       Convert degrees, minutes, seconds into the
       same latitude (or longitude) as a single in
       decimal degrees (a single value of type float)
    '''
    return float(degrees + arcminutes/60. + arcseconds/3600.)

rawDescription: python code for "minute of arc" calculation, completing a latitude and longitude conversion function

description: Python code for "minute of arc" calculation, completing a latitude and longitude conversion function


code:
row,col = np.nonzero(A <= A[:,None])
C = np.bincount(row,np.take(B,col))

rawDescription: Efficient 1D array comparison, scaling and summation


code:
class Category(db.Model):

    resource_fields = {
        'id': fields.Integer(attribute='catid'),
        'name': fields.String(attribute='catname')
    }

    __tablename__ = 'CATEGORY'
    catid = db.Column(db.Integer, primary_key=True)
    clientid = db.Column(db.Integer, db.ForeignKey('CLIENT.clientid'))
    catname = db.Column(db.String(50))

    def __repr__(self):
        return '<Category %r>' % (self.catname)

class ContactGrouping(db.Model):

    resource_fields = {
        'id': fields.Integer(attribute='catid'),
        'name': fields.String(attribute='category.catname'),
    }

    __tablename__ = 'CONTACT_GROUPING'
    __table_args__ = (
        PrimaryKeyConstraint('catid', 'contactid', 'clientid'),
    )

    catid = db.Column(db.Integer, db.ForeignKey('CATEGORY.catid'))
    contactid = db.Column(db.Integer, db.ForeignKey('CONTACT.contactid'))
    clientid= db.Column(db.Integer, db.ForeignKey('CONTACT.clientid'))
    category = db.relationship('Category')

    def __repr__(self):
        return '<ContactGrouping %r %r>' % (self.catid, self.contactid)

class Contact(db.Model):

    resource_fields = {
        'id': fields.Integer(attribute='contactid'),
        'firstname': fields.String,
        'lastname': fields.String,
        'categories': fields.List(fields.Nested(ContactGrouping.resource_fields))
    }

    __tablename__ = 'CONTACT'
    contactid = db.Column(db.Integer, primary_key=True)
    clientid = db.Column(db.Integer, db.ForeignKey('CLIENT.clientid'))
    firstname = db.Column(db.String(50))
    lastname = db.Column(db.String(50))
    categories = db.relationship('ContactGrouping', backref='contact', lazy='dynamic', foreign_keys='ContactGrouping.contactid')

    def __repr__(self):
        return '<Contact %r %r>' % (self.firstname, self.lastname)

rawDescription: Update many to many association table with derived field


rawDescription: Python function to extract multiple segments of a file path


code:
foo = 9
foo == (8 or 9)
foo == 8 or foo == 9

rawDescription: In python, is 'foo == (8 or 9)' or 'foo == 8 or foo == 9' more correct?


code: isinstance(n, int)

rawDescription: How can I type-check variables in Python?

description: Type-check variables in Python


code:
import othermodule
import types
for k,v in vars(othermodule).items():
    if isinstance(v, types.FunctionType):
        vars(othermodule)[k] = my_decorator(v)

rawDescription: Defining Python decorators for a complete module


rawDescription: Limit the number of sentences in a string


code:
"""
Django settings for Test project.

Generated by 'django-admin startproject' using Django 1.9.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'yp0at6d_bpr5a^j$6$#)(17tj8m5-^$p773lc6*jy%la!wu5!i'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'modeltest',
]

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'Test.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'Test.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]
# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'

rawDescription: What minimal files i need to use django ORM


code:
class INoStorage(Products.Archetypes.interfaces.storage.IStorage):
    pass

class NoStorage(object):
    zope.interface.implements(INoStorage)

nostorage = NoStorage()
Products.Archetypes.interfaces.storage.IStorage.providedBy(nostorage)

rawDescription: Plone/Zope/ZODB: how to override storage method on a File field


code:
import re
p = re.compile(r'Temp:([^,]+)')
test_str = "Temp:C5E501374D0343090957F7E5929E765C931F7D3EC7A96189FDA88549D54D9E4E5DB3FC1C2, adfsafd1242412,"
print (re.search(p, test_str).group(1))

rawDescription: Using Regex to catch text until first occurrence of certain character


rawDescription: finding a record with a max value in python

description: Finding a record with a max value in python


code:
valid=df.tap>0
df.tafp[valid] /= df.tap[valid]

rawDescription: Python Pandas Pivot Table - Adding calculated column to multi-index column


code:
from django.db.models import Sum

items = Item.objects.filter(status='A').annotate(total_amount=Sum('offer__amount'))

rawDescription: Django "extra" query

description: Django "extra" query


code:
id = int(vehicle.get('id'))
origin = int(vehicle.get('fromTaz'))
destination = int(vehicle.get('toTaz'))

rawDescription: python for loop output as array

description: Python for loop output as array


code:
inverse_mapping = dict(zip(alphaShift, alpha))
unencrypted = ''.join(inverse_mapping.get(char, char) for char in encrypted)

rawDescription: Better way to test for a value in a list than to stack try's and except's


rawDescription: pcolormesh with masked invalid values

description: Pcolormesh with masked invalid values


code:
def generate():
    """
    CSV generator.
    """
    yielded_headers = False

    for row in database.get_all_rows():
        if not yielded_headers:
            yield CSV_HEADERS
            yielded_headers = True

        csv_res = ','.join(row) + '\n'
        yield csv_res

rawDescription: python flask generate csv without losing first row

description: Python flask generate csv without losing first row


code:
REPUTATION_MAX_GAIN_PER_DAY = 100
current_rep = self.model.objects.calculate_points_for_today(user)
expected_rep = min(reputation_value + current_rep, REPUTATION_MAX_GAIN_PER_DAY )
delta = expected_rep - current_rep

rawDescription: Python work out difference to 0

description: Python work out difference to 0


code:
from itertools import combinations

x = ['a','b','c','d','e']
c = []
n = 2

for i in range(n):
    c.extend(combinations(x, i + 1))

print(c)

rawDescription: Listing all combinations of a list up to length n (Python)


code:
df = pd.read_csv('ni.csv', sep='\s+', index_col=0)
df
df.set_index('ni', drop=False)

rawDescription: Reindex without changing order

description: Reindex without changing order


rawDescription: Difference between methods and functions


rawDescription: Validate a filename in python

description: Validate a filename in python


code:
delta_Array = np.array([0.01,0.02,0.03, 0.04, 0.05, 0.06,0.07, 0.08, 0.09, 0.10])
theta_Matrix = []

for i in range(N):
     t = Ridge(Xtrain, ytrain, .3)
     theta_Matrix.append(t)
theta_Matrix = np.array(theta_Matrix)

rawDescription: creating a numpy array of arrays while looping (Python)

description: Creating a numpy array of arrays while looping (Python)


code:
a=['SqNr', 'Xpos', 'Ypos', 'ALPHA_J2000', 'DELTA_J2000', \
 'UMAG', 'BMAG', 'VMAG', 'RMAG', 'IMAG', 'MB420MAG', \
 'MB464MAG', 'MB485MAG', 'MB518MAG', 'MB571MAG', 'MB604MAG',\
 'MB646MAG', 'MB696MAG', 'MB753MAG', 'MB815MAG', 'MB855MAG',\
 'MB915MAG', 'UMAG_ERR', 'BMAG_ERR', 'VMAG_ERR', 'RMAG_ERR',\
 'IMAG_ERR', 'MB420MAG_ERR', 'MB464MAG_ERR', 'MB485MAG_ERR',\
 'MB518MAG_ERR', 'MB571MAG_ERR', 'MB604MAG_ERR', 'MB646MAG_ERR',\
 'MB696MAG_ERR', 'MB753MAG_ERR', 'MB815MAG_ERR', 'MB855MAG_ERR',\
 'MB915MAG_ERR', 'PHOTOZ', 'PHOTOZ_ERR', 'PHOTOZ2', 'PHOTOZ2_ERR',\
 'Z_B', 'Z_B_MIN', 'Z_B_MAX', 'T_B', 'ODDS', 'CHISQUARED', 'Z_M',\
 'Z_fp', 'Z_sp', 'Z_s']

mags     = filter(lambda x: x[-3:]=='MAG', a)
mag_errs = filter(lambda x: x[-7:]=='MAG_ERR', a)

rawDescription: Finding a pattern in a list of strings

description: Finding a pattern in a list of strings


code:
# Name of nodes to start
# here we have a single node
CELERYD_NODES="w1"
# or we could have three nodes:
#CELERYD_NODES="w1 w2 w3"

ENV_MY="/home/stoyanov/wsapp"
CELERYD="$ENV_MY/bin/celeryd"
CELERYD_MULTI="$ENV_MY/bin/celeryd-multi"
CELERYCTL="$ENV_MY/bin/celeryctl"

# Where to chdir at start.
CELERYD_CHDIR="$ENV_MY/wslib/"
ENV_PYTHON="$ENV_MY/bin/python"
# Extra arguments to celeryd
CELERYD_OPTS="--app=PriceProviders --loglevel=WARNING --events --autoreload"
#"--time-limit=300 --concurrency=8"

# %n will be replaced with the nodename.
CELERYD_LOG_FILE="/var/log/celery/%n.log"
CELERYD_PID_FILE="/var/run/celery/%n.pid"
CELERY_CREATE_RUNDIR=0
CELERY_CREATE_LOGDIR=0

# Workers should run as an unprivileged user.
CELERYD_USER="stoyanov"
#CELERYD_GROUP="stoyanov"

rawDescription: Celery in daemon mode

description: Celery in daemon mode


code:
#pythran export np_cos_norm(float[], float[])
import numpy as np
def np_cos_norm(a, b):
    val = np.sum(1. - np.cos(a-b))
    return np.sqrt(val / 2. / a.shape[0])

rawDescription: Optimization and speedup of a mathematical function in python


code:
args = ("test",)
kwargs = {"stdout" : content}

call_command(*args,**kwargs)

rawDescription: Python C API - Creating keywords

description: Python C API - Creating keywords


code:
def ret_this():
    global result
    result = "I'm called!"

rawDescription: Python return value from scheduled event


rawDescription: How to write small DSL parser with operator module in python

description: Write small DSL parser with operator module in python


rawDescription: Slicing pandas dataframe as per expression value


code:
df["label"] = (df["change"] > 1.03).astype(int)
df

rawDescription: pandas dataframe: new column depending on other

description: Pandas dataframe: new column depending on other


code:
def to_str(item):
    if type(item) in {np.int, np.float64}:
        return '{:g}'.format(item)
    else:
        return item

pd.DataFrame({'int': [1, 2], 'float': [1.03, 1.0], 'str': ['a', 'b']}).applymap(to_str)

rawDescription: Minimize float format in Pandas df.to_csv()


rawDescription: getting GtkTressStore parent from child in python

description: Getting GtkTressStore parent from child in python


code:
df['DATA'] = df['DATA'].astype(str)
df['DATA'] = df['DATA'].str.rjust(6,'0')

rawDescription: Add 0 to number with 5 characters (pandas)


code:
class ABC:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def copy_init(other):
        self.a = other.a
        self.b = other.b
        self.c = other.c

class ABCD(ABC):
    def __init__(self, abc, d):
        self.copy_init(abc)
        self.d = d

rawDescription: Copy constructing a base class from an instance in Python


code:
import mru as _mru

SomeClass = _mru.SomeClass
# etc...

rawDescription: What is the right way to organize a project with simple dependencies?


code: pd.read_hdf('/tmp/out.h5', 'results_table', where='A in [1,3,4]')

rawDescription: read HDF5 file to pandas DataFrame with conditions

description: Read HDF5 file to pandas DataFrame with conditions


code: from mpl_toolkits.axisartist.grid_finder import MaxNLocator, DictFormatter

rawDescription: How to change ticklabels within figure created with axisartist

description: Change ticklabels within figure created with axisartist


code: [ result for variables in iterable if condition ]

rawDescription: Python - Add boolean condition to generator


code:
from functools import wraps

def convertparameters(*types):
    def wrapper(func):
        @wraps(func)
        def new_func(self, *args, **kwargs):
            pass # Your stuff

rawDescription: Pythonic way of converting parameters to the same standard within all methods of a class


code:
def mySubplotFunction(fig,gs,x,y,c,ax=None):

    if not ax:
        ax = fig.add_subplot(gs)
    ax.plot(x, y, c)

    return fig, ax

rawDescription: Set height and width of figure created with plt.subplots in matplotlib?


code:
args = []
for row in cur_msql:
    # additional operations eg.:
    if row['customer_name'] == NULL:
        row['customer_name'] = row['customer_name']
    args.append(cur_psql.mogrify("(%(customer_id)s, %(customer_name)s, %(contact_name)s, %(address)s, %(city)s, %(postal_code)s, %(country)s)", row))

args_str = ','.join(args)

rawDescription: Python - Concat the output of join() method


code:
y.sum(axis=1,keepdims=True)
_.shape

rawDescription: Python/numpy issue with array/vector with empty second dimension


code: (v for v in g.vs if v[atr] < c)

rawDescription: igraph select function for generic attributes

description: Igraph select function for generic attributes


code:
def match_line(ID, f):
    with open(f) as file:
        for line in file:
            if str(ID) in line:
               next(file)
               return next(file)

rawDescription: Searching a txt file for a character, and returning a character from a line below it


code:
import os
import subprocess

current_dir = os.path.dirname(os.path.realpath(__file__))
target_script = os.path.abspath(os.path.join(current_dir, '..', '..', 'file1.py'))
arg1 = 'test_value'
call_args = ['python', target_script, arg1]
subprocess.Popen(call_args)

rawDescription: python invoke script with arguments two folders above

description: Python invoke script with arguments two folders above


code:
from PyQt4 import QtGui

class Widget(QtGui.QWidget):
    def __init__(self, parent):
        QtGui.QWidget.__init__(self, parent)
        self.destroyed.connect(self.handleDestroyed)

    def __del__(self):
        print ('__del__:', self)

    def handleDestroyed(self, source):
        print ('destroyed:', source)

class Foo(Widget):
    def __init__(self, parent):
        Widget.__init__(self, parent)

class Bar(Widget):
    def __init__(self, parent):
        Widget.__init__(self, parent)

class Window(Widget):
    def __init__(self, parent=None):
        Widget.__init__(self, parent)
        self.foo = Foo(self)
        self.bar = Bar(None)

if __name__ == "__main__":

    app = QtGui.QApplication([__file__, '-widgetcount'])
    window = Window()
    window.show()
    app.exec_()

rawDescription: QWidget not deleted with parent window

description: QWidget not deleted with parent window


code:
def cmdloop():
    (sread, swrite, sexc) = select.select( [sock], [], [], 0)
    for s in sread:
        ...
        ...
        ...
    app.after(500,cmdloop)

rawDescription: GUI Input with select.select() in Python


code: isinstance(item, str)

rawDescription: TypeError list object is not callable when trying to convert an object to a string


code:
class EvaluatingName(object):
    def __init__(self, callable):
        self._callable = callable
    def __call__(self):
        return self._callable()
    def __repr__(self):
        return repr(self())

ls = EvaluatingName(os.getcwd)

rawDescription: call function through variable or without parentheses in python

description: Call function through variable or without parentheses in python


code:
a = input("Enter the first input:")
b = input("Enter the second input:")
result = a + delimiter + b
# or
# result = delimiter.join([a, b])

rawDescription: Python 3.3. Concatenating strings from input. How?


code:
" ", "achieve", "achievement", "achievable", "", "", ""
"agree", "agreement", "", " ", "agreeable", "agreeably"
"", "apartment", "", ""

rawDescription: How to split Strings in Java including blanks (like in Python)

description: Split Strings in Java including blanks (like in Python)


code:
def mustNotBeEmpty(tokens):
    if not tokens[0]:
        raise ParseException("match must not be empty")

parser = "A" + SkipTo("B").setParseAction(mustNotBeEmpty) + "B"

rawDescription: Right way to SkipTo not empty tokens with ZeroOrMore in pyparsing


code:
# from http://stackoverflow.com/questions/15390807/integer-square-root-in-python
def isqrt(n):
    x = n
    y = (x + 1) // 2
    while y < x:
        x = y
        y = (x + n // x) // 2
    return x

rawDescription: Limit of digit-by-digit calculation of square roots


code:
def on_data(self, raw_data):
    data = json.loads(raw_data)
    print(json.dumps(data, ensure_ascii=False))

rawDescription: How can I replace Unicode characters with Turkish characters in a text file with Python

description: Replace Unicode characters with Turkish characters in a text file with Python


code:
import re

reexpr = r"""
    (                           # Capture code
        "(?:\\.|[^"\\])*"       # String literal
        |
        '(?:\\.|[^'\\])*'       # String literal
        |
        (?:[^/\n"']|/[^/*\n"'])+ # Any code besides newlines or string literals
        |
        \n                      # Newline
    )|
    (/\*  (?:[^*]|\*[^/])*   \*/)        # Multi-line comment
    |
    (?://(.*)$)                 # Comment
    $"""
rx = re.compile(reexpr, re.VERBOSE + re.MULTILINE)

rawDescription: Matching one-line JavaScript comments (//) with re


code:
# -*- coding: utf-8 -*-
word = "文本"
print(word)
for each in unicode(word,"utf-8"):
    print(each)

rawDescription: Iterating through a unicode string in Python


code:
from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring

root = Element('root')

l = ['a', 'b', 'c']
for ch in l:
    parent = SubElement(root,'parent')
    child = SubElement(parent, 'child')
    child.text = ch

document = ElementTree(root)
document.write('file.xml', encoding='utf-8', xml_declaration=True)

rawDescription: Writing just the xml declaration to file using Python


code:
from operator import itemgetter
sorted(a, key=itemgetter(1))
[['xyz', 2], ['abc', 5]]

rawDescription: Python: sorting a list by "column"

description: Python: sorting a list by "column"


rawDescription: Creating a pipe from the inside of the python interpreter


code: n.phone = "%c%c%c-%c%c%c-%c%c%c%c" % tuple(map(ord, n.phone))

rawDescription: Format an un-decorated phone number in django?


code:
import pandas as pd
df = pd.read_table('data', sep='\s{2,}')
# Make sure the index is a DatetimeIndex
df.index = pd.DatetimeIndex(df.index)

# discard rows whose time falls between 16:00 and 18:00
df = df.between_time('18:00', '16:00', include_start=True, include_end=True)

proxy = df.index + pd.DateOffset(hours=6)
result = df.groupby(proxy.date).agg(
    {'Open': 'first', 'High': 'max', 'Low': 'min', 'Close': 'last'})
result = result.reindex(columns=['Open','High','Low','Close'])
print(result)

rawDescription: Custom time series resampling in Pandas

description: Custom time series resampling in Pandas


code:
def do_all(self):
    seen = set()
    for c in reversed(self.__class__.__mro__[:-1]):
        if c.do_it not in seen:
            seen.add(c.do_it)
            c.do_it(self)

rawDescription: How can I ensure that a method is called (once, if present) for every class in a hierarchy?

description: Ensure that a method is called (once, if present) for every class in a hierarchy


code: def foo(*args): ...

rawDescription: Python: Unpack individual strings from tuple using Unpacking asterisk argument


rawDescription: How do you perform basic joins of two RDD tables in Spark using Python?

description: Perform basic joins of two RDD tables in Spark using Python


code:
#put into np.array
im = np.asarray(im_data, dtype=np.uint8) #this is the most Time consuming part
newshape = (3, sx, sy)
im = np.reshape(im, newshape, order='F')

#write out
f = h5py.File('zenimage_bgr24.h5', 'w')
f.create_dataset('/im', data = im, dtype='uint8')
f.close()

rawDescription: passing Array[Byte] from ironpython to python using pickle

description: Passing Array[Byte] from ironpython to python using pickle


code:
with open('words.py', 'r') as myself:
    for word in words(myself):
        print(word)

rawDescription: How to split a text file to its words in python?

description: Split a text file to its words in python


code:
def shooting():
    c = canvas.coords("player")
    canvas.create_line(c[0],c[1] + 20,c[2],c[3],width=5,fill="yellow",tag="shot")
    window.after(100, shooting_after)

def shooting_after():
    canvas.move("shot",0,20)
    canvas.update()
    window.after(100, shooting_after)

rawDescription: Tkinter...space invaders...shooting

description: Tkinter...space invaders...shooting


code:
def toolbox_page2(self):
    self.page2 = QtGui.QWidget()
    self.page2.setGeometry(QtCore.QRect(0, 0, 471, 145))

    label = QtGui.QLabel(self.page2)
    label.setText("Title - Build a Report : page 2")
    label.setStyleSheet("font: 10pt 'Helvetica'; color: rgb(168, 34, 3)")
    label.setGeometry(QtCore.QRect(QtCore.QPoint(50, 50), label.sizeHint()))

rawDescription: Qt Python - report in toolbox: QTextDocument and QPainter


code: df['code'].str.split('_', expand=True)

rawDescription: how to break a vector into two in python

description: Break a vector into two in python


rawDescription: Creating a dictionary from a list

description: Creating a dictionary from a list


code:
count, rating = 0, 0
for review in reviews:
    count += 1
    rating += review.bun
if count:
   av_rating = rating/count

rawDescription: Django: Having trouble setting up Many-To-One model


code:
x1, res, rnk, s = np.linalg.lstsq(A, b)
Z = qr_null(A)

rawDescription: How can I obtain the same 'special' solutions to underdetermined linear systems that Matlab's `A \ b` (mldivide) operator returns using numpy/scipy?

description: Obtain the same 'special' solutions to underdetermined linear systems that Matlab's `A \ b` (mldivide) operator returns using numpy/scipy


code: data = fun1()

rawDescription: How to use python output generated by several print statements?

description: Use python output generated by several print statements


code:
lst = [["cat","dog","mouse",1],["cat","dog","mouse",2],["cat","dog","mouse",3]]
item = 'cat'
sum(sub.count(item) for sub in lst)

rawDescription: How to find the number of instances of an item in a list of lists

description: Find the number of instances of an item in a list of lists


code:
ngrams = ["sedan", "sail sedan", "sail", "price of", "price", "of chevrolet", "of", "chevrolet sail", "chevrolet"]
sorted(ngrams, key=lambda s: len(s.split()), reverse=True)

rawDescription: How to sort list of string by n-grams in python

description: Sort list of string by n-grams in python


code:
def _quicksort(context, A, left=None, right=None):
    ...
    _quicksort(context, ...)

rawDescription: Counting comparisons quicksort without global Python


code:
pos = pixel_array_offset + row_size * y + pixel_size * x
data[pos:pos+3] = 255, 255, 255

rawDescription: working with .bmp files in python 3

description: Working with .bmp files in python 3


code: [tuple(getLength(i) for i in tup) for tup in a]

rawDescription: access elements of the list of tuples with variable length and save the output in the same format

description: Access elements of the list of tuples with variable length and save the output in the same format


code:
store.select('df',where='second="b"')
store.select('df',where='second="b" & first=2')

rawDescription: Pandas multiindex and pytables... separate indexes or one concatenated index?


code: ftp.storbinary(command="stor someFileNameOnServer", file=open("localFile",'rb'), callback=handle,blocksize=1024)

rawDescription: Python ftplib and storbinary

description: Python ftplib and storbinary


rawDescription: How to split an array according to a condition in numpy?

description: Split an array according to a condition in numpy


code:
ip = '127.0.0.1'

new = ip.split('.')

new = reversed(new)

print('.'.join(new))

print(ip)

rawDescription: read an ip address conversely using python

description: Read an ip address conversely using python


code:
l = []
ln_m = len(myList)
for ind, sub in enumerate(myListOfList):
    k = sub[0][-1]
    if len(sub) == ln_m and all(ch[0] in st for ch in sub) and all(ch[-1] == k for ch in sub):
        l.append(ind)

rawDescription: Compare lists of strings regarding the suffixes of their respective elements


code: json_string = dumps(attrdict, default=as_attrdict)

rawDescription: How do you enable python's json package to encode attrdict.AttrDict objects?

description: Enable python's json package to encode attrdict.AttrDict objects


rawDescription: How do i find the iloc of a row in pandas dataframe?

description: Find the iloc of a row in pandas dataframe


code:
L = ['AB', 'AB', 'CD', 'EF', 'JK', 'LM']
stats = {key: 0 for key in L}
for index, value in enumerate(L):
    lookforward = L[index:index+2]
    lookforward = [item for item in lookforward if item == value]
    value_counter = len(lookforward)
    stats[value] += value_counter

rawDescription: comparing a list item within certain interval in python

description: Comparing a list item within certain interval in python


code:
import unicodecsv
w = unicodecsv.writer(open("test.csv", "w"))
w.writerow((u"花花公子昊天鞋类专营店", 78.10))
del w

rawDescription: write chinese words in csv file using python2.7

description: Write chinese words in csv file using python2.7


code:
with num_threads(8):
    np.dot(x, y)
    # etc

rawDescription: Set max number of threads at runtime on numpy/openblas


code:
def install_auth_opener():
    '''install the authentication handler.

    This handles non-standard behavior where the server responds with
    403 forbidden, instead of 401 retry. Which means it does not give you the
    chance to provide your credentials.'''
    auth_handler = HTTPBasic403AuthHandler()
    auth_handler.add_password(
        realm=None,
        uri=jenkins_url,
        user=username,
        passwd=api_token)
    opener = urllib2.build_opener(auth_handler)
    # install it for all urllib2.urlopen calls
    urllib2.install_opener(opener)

rawDescription: Fetching a URL from a basic-auth protected Jenkins server with urllib2


code:
df
pd.concat((pd.Series((v, row['var2'], row['var3']), df.columns) for _, row in df.iterrows() for v in row['var1'].split(',')), axis=1).T

rawDescription: Converting Column with string separated values into rows


code:
from dis import dis

def case1():
    bob = input("Are you Bob? Yes or no? ").lower()
    if bob == "yes":
        print("Hi, Bob.")
    else:
        print("Sorry, wrong person.")

def case2():
    bob = input("Are you Bob? Yes or no? ")
    if bob.lower() == "yes":
        print("Hi, Bob.")
    else:
        print("Sorry, wrong person.")

rawDescription: Is there a difference between putting .lower() in one place or another?


code: re.findall('[A-Za-z]+', s)

rawDescription: Regex only searching for words

description: Regex only searching for words


code:
n = Symbol('n')
refine(Abs(n-1), Q.positive(n-1))
refine(Abs(n-1))

rawDescription: SymPy -- define domain of variable

description: SymPy -- define domain of variable


code:
from salt.modules import environ

def env_vars():
    return {'T17_SECRET_KEY': environ.get('T17_SECRET_KEY','')}

rawDescription: Access Environmental Variables with SaltStack Grains


code:
# Because we want this to look like a continuous plot, we need to hide the
# boundaries (a.k.a. "spines") and yticks on most of the subplots
for ax in axes[1:]:
    ax.spines['left'].set_color('none')
    ax.spines['right'].set_color('none')
    ax.yaxis.set_ticks([])
axes[0].spines['right'].set_color('none')

# To reduce clutter, let's leave off the first and last x-ticks.
for ax in axes:
    xticks = ax.get_xticks()
    ax.set_xticks(xticks[1:-1])

# Now, we'll "scrunch" all of the subplots together, so that they look like one
fig.subplots_adjust(wspace=0)

rawDescription: Python: Matplotlib - probability plot for several data set


code: linklist = [href for href in (tag.get('href') for tag in links) if href is not None]

rawDescription: Saving results to list from a for loop?

description: Saving results to list from a for loop


code:
from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", 'PATH TO DESKTOP')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")

driver = webdriver.Firefox(firefox_profile=profile)
driver.get("Name of web site I'm grabbing from")
driver.find_element_by_xpath("//a[contains(text(), 'DEV.tgz')]").click()

rawDescription: Downloading file to specified location with Selenium and python


code:
for i in e:
    found, ind = binsearch(l,i)
    if found:
        index.append(ind)

rawDescription: Python bisect: pass value instead of returning insertion index


code: [[run.text for run in p.runs if run.style.name=='Emphasis'] for p in document.paragraphs]

rawDescription: Reading docx files, recognizing and storing italicized text


code:
lxml.html.fragment_fromstring('
a
', create_parent='body')

rawDescription: parse html body fragment in lxml

description: Parse html body fragment in lxml


code:
import pickle
pickled_repr = pickle.dumps(my_large_num())
restored_object = pickle.loads(pickled_repr)
sum(restored_object)

rawDescription: Can I store an iterator in a file which I can read from later? Will this reduce space consumption?

description: Store an iterator in a file which I can read from later? Will this reduce space consumption


rawDescription: How to iterate an array in Python pointer-likely and efficiently?

description: Iterate an array in Python pointer-likely and efficiently


code:
sent = "This is a foo, bar sentence."
sent.split()
from nltk import word_tokenize
word_tokenize(sent)

rawDescription: Python re.split() vs nltk word_tokenize and sent_tokenize


rawDescription: Not able to sort the unicode numbers in python


code: from __future__ import print_function, division

rawDescription: Within a nested for loop, how can I print tab and carriage return delimited groups of data?


code:
c = store.select_column('df','index')
c

rawDescription: Pandas get specific rows from HDF5 by index


code:
count = some_queryset \
    .filter( Q(related__value__gte=1) & Q(related__value__lt=4) ).count()  # This should work also without using Q()

rawDescription: Is there a way to improve the queries in a loop on one in django?


code:
import MySQLdb

# connect
con = MySQLdb.connect(host="localhost", user="appuser", passwd="",db="onco")

rawDescription: Keeping a connection open for the purposes of testing


code:
for k in myDict.keys():
    myDict[k](PARAM1, PARAM2)

rawDescription: Preventing a class instance from being evaluated until it's parameters have been set


code:
...
def new_sys_excepthook(type, value, traceback):
    if type == NameError:
        jump_to(traceback.tb_lineno, traceback.tb_frame)
        traceback.tb_frame
        return

    sys.__excepthook__(type, value, traceback)

def install():
    sys.excepthook = new_sys_excepthook

    sys.settrace(_trace)
    ...

rawDescription: Python: resuming program at line number in the context before an exception using a custom sys.excepthook


code:
class MyForm(forms.Form):
    myinput = forms.forms.CharField(max_length=100)

rawDescription: how to manipulate user submitted text and display it with django?

description: Manipulate user submitted text and display it with django


code: df['income'] = df['income'].apply(lambda L, rep={c:i for i,c in enumerate('abcdefh', 1)}: rep[L[0]])

rawDescription: Dataframe Replace Regex

description: Dataframe Replace Regex


code:
url='https://10.10.20.161/IHUD/'
parameters = ["master.passwd",
    "etc/passwd",
    "etc/shadow%00",
    "/etc/passwd",
    "/etc/passwd%00",
    "../etc/passwd",
    "../etc/passwd%00",
    "../../etc/passwd",
    "../../etc/passwd%00"]

for p in parameters:
    tree = url + p
    print(tree)

rawDescription: defining multiple parameters in python as one variable

description: Defining multiple parameters in python as one variable


code:
class my_config_wizard(osv.osv_memory):
        _name = 'my.settings'
        _inherit = 'res.config.settings'
        _columns = {
            'default_foo': fields.type(..., default_model='my.model'),
            'group_bar': fields.boolean(..., group='base.group_user', implied_group='my.group'),
            'module_baz': fields.boolean(...),
            'other_field': fields.type(...),
        }

rawDescription: Value of custom field in sale.config.settings is not displayed in sales settings view


code: 'Fred'.get('firstname', '')

rawDescription: Overriding decorated subclass methods

description: Overriding decorated subclass methods


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

N = 128
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(range(N+1))
plt.xlim([0,N])
plt.ylim([0,N])
ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: ('%g') % (x * 5.0)))
plt.show()

rawDescription: Changing axis without changing data (Python)


code:
import pydoop.hdfs
hdfs = pydoop.hdfs.hdfs()
hdfs.list_directory('/user/sample')
# This lists all files under /user/sample

rawDescription: How do I install Hadoop and Pydoop on a fresh Ubuntu instance

description: Install Hadoop and Pydoop on a fresh Ubuntu instance


code:
from pyspark.sql.window import Window
import sys

def z_score_w(col, w):
    avg_ = func.avg(col).over(w)
    avg_sq = func.avg(col * col).over(w)
    sd_ = func.sqrt(avg_sq - avg_ * avg_)
    return (col - avg_) / sd_

w = Window().partitionBy("dsc").rowsBetween(-sys.maxsize, sys.maxsize)
df.withColumn("zscore", z_score_w(df.TranAmount, w))

rawDescription: How to create z score in spark sql for each group

description: Create z score in spark sql for each group


rawDescription: BeautifulSoup: How to remove empty tables, while preserving tables that are partially empty or not empty


code:
class Foo(object):
    __metaclass__ = LogMethodCalls

rawDescription: Python - Print method name whenever the methods of a class is called


code: frame.bind('<Return>', lambda e: self.ret())

rawDescription: Problems with Tkinter Binding to enter/return


code: key=sum(map(itemgetter(play)))

rawDescription: Sorting a list of lists of dictionaries in python


code: y = pd.DataFrame(y, columns=list('y'))

rawDescription: simple/efficient way to expand a pandas dataframe

description: Simple/efficient way to expand a pandas dataframe


code:
df1
df2

rawDescription: Averaging values between Pandas dataframes with keys existing in one and data in the other


code:
keep = ~numpy.isnan(w)
numpy.bincount(x[keep],  weights=w[keep])
#>>> array([ 0.3,  0.2,  1.1])

rawDescription: Ignore NaN in numpy bincount in python

description: Ignore NaN in numpy bincount in python


code: (x + 3 for x in mylist)

rawDescription: Map list by partial function vs lambda

description: Map list by partial function vs lambda


rawDescription: Generating a list of unique characters in a MySQL table


code:
import lxml.etree as ET

XML = """ your string here """
tree = ET.fromstring(XML) # you may get errors here because of encoding
                          # if so, re.sub(r"\bencoding="[^"]+?", '', XML) works

info_you_need = {entity: {field.get("Name"): field.find("Value").text for field in entity.findall("Fields/Field")} for entity in tree.findall("Entity")}

rawDescription: How to read the following XML and get values for "host","status","owner","user-template-01" and "test-id"?

description: Read the following XML and get values for "host","status","owner","user-template-01" and "test-id"


code:
a = Rectangle()
a.set_width(3)
a.set_length(5)
a.get_width()
a.get_length()
a.get_area()
print(a)

rawDescription: Creating a Rectangle class

description: Creating a Rectangle class


rawDescription: Efficient way to find sum of digits of an entire sequence


rawDescription: Pandas: How to plot a barchar with dataframes with labels?


code:
lis = range(10)
[x+y for x, y in zip(lis, lis[1:])]

rawDescription: Python for loop get index and index+1

description: Python for loop get index and index+1


code:
from lxml import etree

parser = etree.XMLParser()
tree = etree.parse('example.xml', parser)
ns = {'ns': 'http://www.cisco.com/nxos:1.0:if_manager'}

interface = tree.find('//ns:ROW_interface', namespaces=ns)
print [interface.find('.//ns:interface', namespaces=ns).text,
       interface.find('.//ns:state', namespaces=ns).text]

rawDescription: Parsing XML file with lxml in Python

description: Parsing XML file with lxml in Python


code:
c = C()
help(c)

rawDescription: What is the right way to put a docstring on Python property?


code: ####I want whatever is located here ###

rawDescription: Python web scraping involving HTML tags with attributes


rawDescription: Python 3.4 tkinter checkbutton variable handling not working / responding


code:
out = open("outfile.csv", "w")
for fname in ["file1.csv","file2.csv"]:
    with open(fname) as f:
        for line in f:
             out.write(line.rstrip() + ',' + fname + '\n')
out.close()

rawDescription: 3 Dimensional Data in Python

description: 3 Dimensional Data in Python


code: result = a[None, :, :] & b[:, None, :] & c[:, :, None]

rawDescription: Create 3D Object out of 3 views

description: Create 3D Object out of 3 views


code:
d = attrdict(zip(("American","bbbb"),(2,2.5)))
d['American']
d['bbbb']

rawDescription: class __dict__ from output **kwargs, *args

description: Class __dict__ from output **kwargs, *args


code:
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline
import matplotlib.pyplot as plt

newsgroups_train = fetch_20newsgroups(subset='train',
                                      categories=['alt.atheism', 'sci.space'])
pipeline = Pipeline([
    ('vect', CountVectorizer()),
    ('tfidf', TfidfTransformer()),
])
X = pipeline.fit_transform(newsgroups_train.data).todense()

pca = PCA(n_components=2).fit(X)
data2D = pca.transform(X)
plt.scatter(data2D[:,0], data2D[:,1], c=data.target)
plt.show()              #not required if using ipython notebook

rawDescription: plot a document tfidf 2D graph

description: Plot a document tfidf 2D graph


code:
import array
import sys

BLOCKSIZE = 1024 * 1024

def reader(f):
  blockstart = 0
  while True:
    block = f.read(BLOCKSIZE)
    if not block: break
    inblock = 0
    while True:
      nextnl = block.find(b'\n', inblock)
      if nextnl < 0:
        blockstart += len(block)
        break
      yield nextnl + blockstart
      inblock = nextnl + 1

def doindex(fn):
  with open(fn, 'rb') as f:
    # result format: x[0] is tot # of lines,
    # x[N] is byte offset of END of line N (1+)
    result = array.array('L', [0])
    result.extend(reader(f))
    result[0] = len(result) - 1
    return result

def main():
  for fn in sys.argv[1:]:
    index = doindex(fn)
    with open(fn + '.indx', 'wb') as p:
      print('File', fn, 'has', index[0], 'lines')
      index.tofile(p)

main()

rawDescription: Moving to an arbitrary position in a file in Python


code:
import sys
from subprocess import Popen, PIPE; STDOUT

pyversion = sys.version_info.major

class interact():
    def __init__(self, c):
        self.handle = Popen(c, stdin=PIPE, stdout=PIPE, stderr=STDOUT, shell=True)
        sleep(1)
    def send(self, what):
        for c in what:
            sys.stdout.write(c)
            if pyversion == 3:
                self.handle.stdin.write(bytes(c, 'UTF-8'))
            else:
                self.handle.stdin.write(c)
            sys.stdout.flush()
            sys.handle.stdin.flush()
            sleep(0.05)
    def getrow(self):
        return self.handle.stdout.readline()
    def poll(self):
        return self.handle.poll()
    def done(self):
        if self.poll() == 1:
            return 'Return Y'
        return 'Return X'
    def close(self):
        self.handle.stdout.close()
        self.handle.stdin.close()

rawDescription: run a shell script in a python function and get data from the script

description: Run a shell script in a python function and get data from the script


code:
class class_name:
    def __init__(self,list_name,other_list):
        self.list_name = list_name
        self.other_list = other_list

list1 = [1.0,2.0,3.0,4.0]
list2 = [4.0,5.0,6.0,7.0]

def add_five(x): return x+5
list1 = map(add_five, list1)
#or instead you can use a lambda
list1 = map(lambda x: x+5 , list1)

rawDescription: add number to all elements of a list within a class?

description: Add number to all elements of a list within a class


code:
def thank_you(request):
    if not 'docrequest_id' in request.session:
        return HttpResponseForbidden
    docrequest = DocumentRequest.objects.get(request.session['docrequest_id'])

rawDescription: Passing form data from my view to thank you page