code:
class HTMLForm:

    # <...>

    set_value(value,
          name=None, type=None, kind=None, id=None, nr=None,
          by_label=False,  # by_label is deprecated
          label=None)

rawDescription: Use mechanize to submit form without control name


rawDescription: Setting axis limits on pandas scatter_matrix from editor vs console


code:
d = collections.defaultdict(list)
for sublist in lst:
    d[sublist.count(0)].append(sublist)

rawDescription: check two values in list element python

description: Check two values in list element python


code:
from xml.dom.minidom import parseString

pretty_print = lambda data: '\n'.join([line for line in parseString(data).toprettyxml(indent=' '*2).split('\n') if line.strip()])

rawDescription: Empty lines while using minidom.toprettyxml


code:
def getMethod(module, cls, method):
    return getattr(getattr(__import__(module), cls), method)

getMethod('sys', 'stdin', 'write')

rawDescription: Calling a method of a class, the classname is a variable


code:
if settings.URL_PREFIX:
    prefixed_urlpattern = []
    for pat in urlpatterns:
        pat.regex = re.compile(r"^%s/%s" % (settings.URL_PREFIX[1:], pat.regex.pattern[1:]))
        prefixed_urlpattern.append(pat)
    urlpatterns = prefixed_urlpattern

rawDescription: Default redirect url in Django

description: Default redirect url in Django


rawDescription: How to perform an operation on this tuple and convert to another tuple in Python?

description: Perform an operation on this tuple and convert to another tuple in Python


code:
from collections import Counter
inputlist = [4.1, 8.2, 2.1, 8.1, 3.1]
integer_counts = Counter(int(num) for num in inputlist)
sorted(inputlist, key=lambda n: (integer_counts[int(n)] == 1, n))
inputlist = [9.3, 4.1, 4.2, 9.4, 8.2, 7.1]
integer_counts = Counter(int(num) for num in inputlist)
sorted(inputlist, key=lambda n: (integer_counts[int(n)] == 1, n))

rawDescription: How to sort a list firstly writing same elements, and then - by ascending order

description: Sort a list firstly writing same elements, and then - by ascending order


code:
import matplotlib.pyplot as plt

fig=plt.figure(figsize=(8,4))
ax1=fig.add_subplot(121,projection='polar')
ax2=fig.add_subplot(122,projection='polar')

ax1.grid(linewidth=3)

plt.show()

rawDescription: Polar plot with Python changing the thickness of my grid


code:
integer1 = int(input('Enter the first integer between -10 and 10: '))

integer2 = int(input('Enter the second integer between -10 and 10: '))

while (integer1 >= 10 or integer1 <= -10):
    integer1 = int(input('Integer 1 is invalid, Please enter a valid number: '))

while (integer2 > 10 or integer2 < -10):
    integer2 = int(input('Integer2 is invalid, Please enter a valid number: '))

rawDescription: I can't figure out why this while loop isn't functioning properly


code:
def draw(self):
    ax = self.figure.add_subplot(111)
    scatter = ax.scatter(np.random.randn(100), np.random.randn(100))

legend = DraggableLegend(ax.legend())

rawDescription: How to create a draggable legend in matplotlib?

description: Create a draggable legend in matplotlib


code:
def keep_none(func):
    """Wraps a function which expects a value as the first argument, and
    ensures the function won't get called with *None*.  If it is, this
    will return *None*.

    >>> def f(x):
    ...     return x + 5
    >>> f(1)
    6
    >>> f(None) is None
    Traceback (most recent call last):
        ...
    TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
    >>> f = keep_none(f)
    >>> f(1)
    6
    >>> f(None) is None
    True"""

    @wraps(func)
    def _exec(value, *args, **kw):
        return None if value is None else func(value, *args, **kw)

    return _exec

rawDescription: Nice Python Decorators

description: Nice Python Decorators


code:
import os
import sys

sys.setrecursionlimit(100000)

_proc_status = '/proc/%d/status' % os.getpid()

_scale = {'kB': 1024.0, 'mB': 1024.0*1024.0,
          'KB': 1024.0, 'MB': 1024.0*1024.0}

def _VmB(VmKey):
    '''Private.
    '''
    global _proc_status, _scale
     # get pseudo file  /proc/<pid>/status
    try:
        t = open(_proc_status)
        v = t.read()
        t.close()
    except:
        return 0.0  # non-Linux?
     # get VmKey line e.g. 'VmRSS:  9999  kB\n ...'
    i = v.index(VmKey)
    v = v[i:].split(None, 3)  # whitespace
    if len(v) < 3:
        return 0.0  # invalid format?
     # convert Vm value to bytes
    return float(v[1]) * _scale[v[2]]

def memory(since=0.0):
    '''Return memory usage in bytes.
    '''
    return _VmB('VmSize:') - since

stop = False
def solve(n, used, current_sum):
    global stop

    if stop:
        return

    if current_sum == n:
        print(used)
        stop = True
        return

    start = 1 if len(used) == 0 else (used[-1] + 2)
    for i in range(start, n + 1, 2):
        if current_sum + i <= n and not stop:
            used.append(i)
            solve(n, used, current_sum + i)
            used.pop()
        else:
            return

m0 = memory()
solve(100000, [], 0)
m1 = memory(m0)
print(m1/(1024*1024))

rawDescription: Python recursive algorithm doesn't work for large values - C program works


code:
import urllib2, base64
req = urllib2.Request('http://httpbin.org/basic-auth/foouser/barpw')
base64string = base64.encodestring("%s:%s" % ("username", "password")).replace("\n", "")
req.add_header("Authorization", "Basic %s" % base64string)
urllib2.urlopen(req)

rawDescription: Python - urllib2 how to know if our authorization is successful


code:
a = a.reshape((4, 2))
a

rawDescription: Numpy: efficient way to generate combinations from given ranges


code:
lets=("", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz")

def trans(pnum, res = ""):           # res is the current result (initially the empty string)
    if not pnum:
        yield res                    # phone number is empty string, so we're done, return res
    else:
        digit = pnum[0]              # get first digit
        try:
            repls = lets[int(digit)] # if it is an integer, get replacements
            if not repls:            # if replacements is an empty string,
                repls = digit        #    fall back to initial digit
        except ValueError:
            repls = digit            # not an integer  - fall back to initial character
        for i in repls:              # for every replacement character
            yield from trans(pnum[1:], res+i) # recursively process the rest of the phone number

for pnum in ["borla63", "h3llo"]:
    print(list(trans(pnum)))

rawDescription: solution to this recursive function questions?

description: Solution to this recursive function questions


code: from scipy.ndimage import label

rawDescription: How to group elements of a numpy array with the same value in separate numpy arrays

description: Group elements of a numpy array with the same value in separate numpy arrays


code:
from bs4 import BeautifulSoup
import requests

def web_crawl(url):
    source_code = requests.get(url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text, 'lxml')
    for link in soup.findAll('a', {'class': 'pu-image fk-product-thumb '}):
        href = "http://www.flipkart.com" + link.get('href')
        print(href)
        title = link.string
        print(title)

web_crawl(
    'http://www.flipkart.com/laptops/pr?p%5B%5D=facets.availability%255B%255D%3DExclude%2BOut%2Bof%2BStock&p%5B%5D=sort%3Dpopularity&sid=6bo%2Cb5g&otracker=clp_computers-laptops_CategoryLinksModule_0-2_catergorylinks_0_AllBrands')

rawDescription: Basic Web crawler no output

description: Basic Web crawler no output


code:
if hasattr(1, 'bit_length'):
    bitlen = lambda x: x.bit_length()
else:
    bitlen = lambda x: len(bit(x)) - 2

rawDescription: Cross-version portability in Python

description: Cross-version portability in Python


code:
def get_version():
    logger.info('Started')
    version = None
    for file in os.listdir(running_config_dir):
        if file.endswith('.config'):
            with open(file) as fileobj:
                for line in fileobj:
                    if 'string1' in line:
                        print(line)
                        print('Found string1')
                        version = line.strip()
        else:
            logger.critical('Running Configuration not found')

    logger.info('Finished')
    return version

rawDescription: Python: Parse file for a string and return value as variable


code:
x1 = cityOne   #
y1 = cityOne    # wrong!
x2 = cityTwo    #
y2 = cityTwo   #
addLine(map, cityXvalue[x1], cityYvalue[y1], cityXvalue[x2], cityYvalue[y2])

rawDescription: combining distance between pixels during while loop

description: Combining distance between pixels during while loop


code:
from itertools import groupby
from collections import Counter
strs = 'TTHHTHHTHHHHTTHHHTTT'
Counter(k for k, g in groupby(strs))

rawDescription: Counting runs in a string

description: Counting runs in a string


code:
parser.add_argument('--feature', dest='feature', action='store_true')
parser.add_argument('--no-feature', dest='feature', action='store_false')
parser.set_defaults(feature=True)

rawDescription: parsing boolean values with argparse

description: Parsing boolean values with argparse


code:
class C(object, metaclass=AKA):
    aliases = 'C1', 'C2'

rawDescription: Defining a python class "alias" within the class definition


code:
def read_config_file():
    php_bsc_mdls = None
    php_adv_mdls = None
    config = ConfigParser.ConfigParser()
    cnf_path = 'config_files/php.sr'
    config.read(cnf_path)
    if config.has_section('basic'):
        if config.has_option('basic', 'basic'):
            php_bsc_mdls = config.get('basic', 'basic').split(',')
    if config.has_section('advance'):
        if config.has_option('advance','advance'):
            php_adv_mdls = config.get('advance', 'advance').split(',')

    return php_bsc_mdls, php_adv_mdls

rawDescription: How to access the variables declared inside functions in python

description: Access the variables declared inside functions in python


code:
def storescores():
    with open("hst.txt") as hs:
        lines = list(hs)
    lines.append(name + " " + score + "\n")
    lines.sort(key=lambda line: int(line.split()[-1]))
    lines = lines[-10:]
    with open("hst.txt", "w") as hs:
        for line in lines:
            hs.write(line)

rawDescription: Maximum pieces of data appending to a file


code:
#!/usr/bin/env python

import socket
import sys

host = ''
port = 50000
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
s.listen(backlog)

# build dict of addresses first, no need to do this for each message
with open('list.txt', 'r') as my_file:
    address_map = dict(line.split() for line in my_file)

while True:
    client, address = s.accept()
    req_address = client.recv(size)
    ip = address_map.get(req_address, 'Not found')
    client.send(req_address + '  ' + ip)
    client.close()

rawDescription: python search string from socket

description: Python search string from socket


code:
raclose = []
ramid = []
rafar = []
decdlose = []
decmid = []
decfar = []

for ii in range(len(dist)):
  if dist[ii] < 10.:
    raclose.append(ra[ii])
    decclose.append(dec[ii])
  elif dist[ii] > 20.:
    rafar.append(ra[ii])
    decfar.append(dec[ii])
  else:
    ramid.append(ra[ii])
    decmid.append(dec[ii])

plt.clf
ax1 = scatter(raclose, decclose, marker='o', s=20, color="darkgreen", alpha=0.6)
ax2 = scatter(ramid, decmid, marker='o', s=20, color="goldenrod", alpha=0.6)
ax3 = scatter(rafar, decfar, marker='o', s=20, color="firebrick", alpha=0.6)
line1 = Line2D(range(10), range(10), marker='o', color="darkgreen")
line2 = Line2D(range(10), range(10), marker='o',color="goldenrod")
line3 = Line2D(range(10), range(10), marker='o',color="firebrick")
plt.legend((line1,line2,line3),('d < 10pc','20pc > d > 10pc', 'd > 20pc'),numpoints=1, loc=3)
show()

rawDescription: matplotlib: how can I convert a XYZ scatter to a pixel image?

description: Matplotlib: how can I convert a XYZ scatter to a pixel image


code:
class DoublyLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None

    def __repr__(self):
        return '<DoublyLinkedList {}>'.format(self.head)

    def add_head(self, data=None):
        if self.head is None:
            self.head = self.tail = Node(data)  # the very fist node
        else:
            new_head = Node(data=data, next=self.head)  # prev is None
            self.head.prev = self.head = new_head

    def add_tail(self, data=None):
        if self.tail is None:
            self.head = self.tail = Node(data)  # the very first node
        else:
            new_tail = Node(data=data, prev=self.tail)  # next is None
            self.tail.next = self.tail = new_tail

    # implements iteration from head to tail
    def __iter__(self):
        current = self.head
        while current is not None:
            yield current
            current= current.next

    # implements iteration from tail to head
    def __reversed__(self):
        current = self.tail
        while current  is not None:
            yield current
            current = current.prev

rawDescription: How could I make this singly linked list into a doubly linked list?


code: x = map(func, x)

rawDescription: What is the best way to appy changes in a list in Python


code: crave_made = None

rawDescription: Not able to implemant the relationship between crave and replye django


rawDescription: numpy reverse multidimensional array

description: Numpy reverse multidimensional array


code:
book = get_object_or_404(Book, pk=1)
book_form = BookForm(instance=book) # this will create a form with the data filled of book with id 1

rawDescription: django form dropdown list of stored models

description: Django form dropdown list of stored models


code:
nx = 1000
ny = 1000
xi = np.linspace(min(X), max(X), nx)
yi = np.linspace(min(Y), max(Y), ny)

rawDescription: smoothing outer edges in matplotlib

description: Smoothing outer edges in matplotlib


code:
class Foo:
   def __init__(self, num):
      self.num = num
      def _fn(other):
         return self.num + other.num
      self.__add__ = _fn

rawDescription: Dynamic Operator Overloading on dict classes in Python


code:
myfoo = u'àà'
myfoo
unicodedata.normalize('NFD', myfoo).encode('ascii', 'ignore')

rawDescription: How to convert unicode accented characters to pure ascii without accents?

description: Convert unicode accented characters to pure ascii without accents


rawDescription: Combine two columns under one header in Numpy array


code:
if isinstance(obj, basestring):
    i_am_string(obj)

rawDescription: check if variable is dataframe

description: Check if variable is dataframe


code: thread = threading.Thread(target = server.serve_forever())

rawDescription: SimpleHTTPServer launched as a thread: does not daemonize


code:
X2
Y2
Z2
np.column_stack((X2.ravel(), Y2.ravel(), Z2.ravel()))

rawDescription: Maintaining point correlation while flattening 2D meshgrid


code:
base = [['a', 5001, 1, 4, 8],
        ['b', 5002, 2, 5],
        ['c', 5002, 2, 5],
        ['d', 5003, 2, 6, 7],
        ['e', 5004, 3, 6, 9]]
uniques = [1,2,3,4,5,6,7,8,9]
uniques_dict = {u: [] for u in uniques}
for record in base:
    key, values = record[1], record[2:]
    for unique in uniques_dict.keys() & values:  # the intersection
        uniques_dict[unique].append(key)

uniques_dict

rawDescription: Dictionary from associations found in list of lists (performance issue)


code:
from datetime import datetime
import time
d = datetime.strptime(time.ctime(),"%a %b %d %H:%M:%S %Y")

d.strftime('%m/%d/%Y %H:%M:%S')
Out[9]: '11/06/2013 19:01:11'

rawDescription: Python - how to convert ctime to '%m/%d/%Y %H:%M:%S'


rawDescription: Getting last (newest) element with lxml,python


code:
import pandas as pd

fn = 'D:/Technical_Data/final.csv'
sep=','
out_path = 'D:/Technical_Data_by_Date'
chunk_size = 20000
key_col_idx = 0

reader = pd.read_csv(fn, sep=sep, chunksize=chunk_size)

for chunk in reader:
    # loop through keys
    for key in chunk.iloc[:, key_col_idx].unique():
        # writing (appending: mode='a') data to CSV files (by key)
        chunk[chunk.iloc[:, key_col_idx] == key] \
             .to_csv('{0}/{1}.csv'.format(out_path, key),
                     mode='a', header=None, index=False)

rawDescription: how to speed up the code for splitting file on first column

description: Speed up the code for splitting file on first column


code:
class Incident(models.Model):
    STATUS_CHOICES = (
        ('open',   'Open'),
        ('closed', 'Closed'),
        # some other statuses go here
    )
    incident_date_reported = models.DateField('Date Reported',
                                              default=timezone.now)
    status = models.CharField(max_length=20,
                              choices=STATUS_CHOICES,
                              default='open')

rawDescription: Django: Filtering query with foreign key not working


code:
abort
clone
config
console
create
deploy
events
health
init
list
local
logs
open
platform
printenv
scale
setenv
ssh
status
swap
terminate
upgrade
use

rawDescription: AWS EC2: "Invalid command: terminate"

description: AWS EC2: "Invalid command: terminate"


code: L=list()

rawDescription: How can I create a list in Python?

description: Create a list in Python


code:
class OpenDir(QtGui.QMainWindow):
    def __init__(self):
         super(OpenDir, self).__init__()
         self.button = QtGui.QPushButton('Open', self)
         self.button.clicked.connect(self.openDirectory)
         self.setCentralWidget(self.button)

rawDescription: Open a directory in a new window in Python QT


code: crypt = ''.join(i for i in message if i.isalnum())

rawDescription: How to keep "spaces" although dict contains it?

description: Keep "spaces" although dict contains it


code:
data = [ ('john', 23), ('arquimedes', 42) ]
pd.DataFrame(data, columns=['name', 'age'], index=['id0', 'id1'])

rawDescription: Pandas: how to reuse the index information from the data?


code:
child = int(input("How many children do you have?"))
count = 1
total = 0
if child > 100:
    print("Invalid!")
else:
    while count <= child:
        n = int(input("Enter child #%s" %str(count)+ " age:"))
        count += 1
        total += n

print("Average age:", total/child)

rawDescription: calculating average in python?

description: Calculating average in python


rawDescription: How to treat a hex as string?

description: Treat a hex as string


code:
some_dictionary = {"a": 1, "b": 5, "c": 0, "e": 2}
sorted(some_dictionary.items())
sorted(some_dictionary.items(), key=lambda i:i[1])

rawDescription: Container to Use for Sorting in Python

description: Container to Use for Sorting in Python


rawDescription: a function that takes a list of integers as a parameter and returns a list of running totals

description: A function that takes a list of integers as a parameter and returns a list of running totals


rawDescription: Cycle through list starting at a certain element


code:
def sum_until_found_3(lst, num, res=0):
    if num in lst:
        if lst[0] == num:
            return res
        else:
            return sum_until_found_3(lst[1:], num, res + lst[0])
    else:
        return "The number is not in the list!"

rawDescription: Sum a list of numbers until a number X is found


code:
print(b - a)
7

print(a - b)
-7

rawDescription: Overwrite __sub__ for a-b when 'b' is my class


code:
g = df.groupby(['PROT', 'PEP'])
g.filter(lambda x: len(x) >= 2)

rawDescription: Filtering, grouping data

description: Filtering, grouping data


code:
import sys, types
isinstance(sys, types.ModuleType)

rawDescription: Python: How can I check whether a object is a module?


code:
def runningMean(x, N):
    return np.convolve(x, np.ones((N,))/N)[(N-1):]

pl.plot(x, runningMean(f(x), 10))

rawDescription: Produce random wavefunction

description: Produce random wavefunction


code:
for line in lines:
    print(line)

rawDescription: How to store each line of a text file into a python list excluding comments

description: Store each line of a text file into a python list excluding comments


code:
def bucket_floats(data_row):
    float_data = data_row[3]
    if float_data > 0 and float_data <= 2:
        return 1
    elif float_data > 2 and float_data <= 5:
        return 2
    elif float_data > 5 and float_data <= 10:
        return 3
    ...

rawDescription: Elegant data processing in Python

description: Elegant data processing in Python


rawDescription: How to pass a function within another function when using Python

description: Pass a function within another function when using Python


code:
dt = datetime.timedelta(1)
s[d - 2*dt:d + 2*dt]

rawDescription: slice pandas timeseries on date +/- 2 business days

description: Slice pandas timeseries on date +/- 2 business days


rawDescription: Is there a way to check a function's signature in Python?


code:
def f(x):
    return 2*x*(1-x)

iterate(seed, num, f)
#                  ^

rawDescription: How to pass a function as function parameter in Python

description: Pass a function as function parameter in Python


code:
from collections import namedtuple

Point = namedtuple('Point', 'x, y, z')

class Atom(object):
    def __init__(self, symbol, x, y, z):
        self.symbol = symbol
        self.position = Point(x, y, z)

    def __repr__(self):
        return '{name}({sym!r}, {pos.x}, {pos.y}, {pos.z})'.format(
            name=self.__class__.__name__, sym=self.symbol, pos=self.position)

class Selection(object):
    def __init__(self, a_system, atom_list=None):
        if atom_list is None:
            atom_list = []
        for atom in a_system:
            atom_list.append(atom)
        self.atom_list = atom_list

    def __repr__(self):
        return '{name}({atoms})'.format(
            name=self.__class__.__name__, atoms=self.atom_list)

    def _filter(self, func):
        return filter(func, self.atom_list)

    def by_symbol(self, symbol):
        self.atom_list = self._filter(lambda a: a.symbol == symbol)
        return self

    def by_zrange(self, zmin, zmax):
        def zrange(a):
            return zmin <= a.position.z <= zmax
        self.atom_list = self._filter(zrange)
        return self

rawDescription: Selecting objects

description: Selecting objects


rawDescription: Python str.format() --- passing in list, outputting list


rawDescription: Selecting DataFrame slices by date string


code:
import heapq

class HeapQueue(list):
    def __init__(self,xs):
        super(HeapQueue,self).__init__(xs)
        self._index_table = {x:i for i,x in enumerate(self)}
    def __setitem__(self,i,v):
        super(HeapQueue,self).__setitem__(i,v)
        self._index_table[v] = i
    def append(self,x):
        super(HeapQueue,self).append(x)
        self._index_table[x] = len(self)-1
    def push(self, x):
        self.append(x)
        heapq._siftdown(self, 0, len(self)-1)
    def heapify(self):
        n = len(self)
        for i in reversed(range(n//2)):
            heapq._siftup(self, i)

rawDescription: Intercepting heapq

description: Intercepting heapq


code:
import gdb

gdb.execute('set python print-stack full')
gdb.execute('set confirm off')
gdb.execute('file name1')
gdb.execute('run')

name_in_program = gdb.parse_and_eval('p->name').string()
gdb.write('Please input name: ')
name = raw_input()
while name != name_in_program:
    gdb.write('Please try another name: ')
    name = raw_input()

gdb.execute('quit')

rawDescription: gdb python : How to redirect the output of a gdb command to a variable?

description: Gdb python : How to redirect the output of a gdb command to a variable


code: df["rno_defined"]= map(lambda x: D[x] if x!="Nan" else "Unknown",df['rno_cd'].values)

rawDescription: How can I create a new column and assign values to it based off of critieria?

description: Create a new column and assign values to it based off of critieria


code:
import winsound
winsound.Beep(300,2000)

rawDescription: Python: Sound alarm when code finishes

description: Python: Sound alarm when code finishes


code: human = Human(world=world, range=range, speed=speed)

rawDescription: One-Many-One Inheritance in Python

description: One-Many-One Inheritance in Python


code:
def finder(attr):
    for cls in object.__subclasses__():
         if hasattr(cls, attr):
              return cls
    raise ValueError

rawDescription: Inverse of hasattr in Python

description: Inverse of hasattr in Python


code:
import Tkinter as tk
import subprocess
import os
import io
from PIL import Image

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.line_start = None
        self.canvas = tk.Canvas(self, width=300, height=300, bg="white")
        self.canvas.bind("<Button-1>", lambda e: self.draw(e.x, e.y))
        self.button = tk.Button(self, text="save",
                                command=self.save)
        self.canvas.pack()
        self.button.pack(pady=10)

    def draw(self, x, y):
        if self.line_start:
            x_origin, y_origin = self.line_start
            self.canvas.create_line(x_origin, y_origin, x, y)
        self.line_start = x, y

    def save(self):
        ps = self.canvas.postscript(colormode='color')
        img = Image.open(io.BytesIO(ps.encode('utf-8')))
        img.save('/tmp/test.jpg')

app = App()
app.mainloop()

rawDescription: How to convert a Python tkinter canvas postscript file to an image file readable by the PIL?

description: Convert a Python tkinter canvas postscript file to an image file readable by the PIL


code:
gen = (s.__getitem__(i) for i in xrange(len(s)))
# <generator object <genexpr> at 0x1004e8780>
gen.next()
# 's'
gen.next()
# 't'
gen.next()
# 'r'

rawDescription: find the docs page for function "in"

description: Find the docs page for function "in"


rawDescription: Python equivalent for HashMap

description: Python equivalent for HashMap


code:
bufsize = wintypes._COORD(100, 80) # rows, columns
windll.kernel32.SetConsoleScreenBufferSize(h, bufsize)

rawDescription: How to control the size of the Windows shell window from within a python script?

description: Control the size of the Windows shell window from within a python script


code:
import numpy as np
from PIL import Image, ImageDraw

def _spatial_contour_frequency_pil(paths,lon,lat,regular_grid=False,
        method_ind=None):

    def get_indices(points,lon,lat,tree=None,regular=False):

        def get_indices_regular(points,lon,lat):
            lon,lat = lon.T,lat.T
            def _get_ij(lon,lat,x,y):
                lon0 = lon[0,0]
                lat0 = lat[0,0]
                lon1 = lon[-1,-1]
                lat1 = lat[-1,-1]
                nx,ny = lon.shape
                dx = (lon1-lon0)/nx
                dy = (lat1-lat0)/ny
                i = int((x-lon0)/dx)
                j = int((y-lat0)/dy)
                return i,j
            return [_get_ij(lon,lat,x,y) for x,y in points]

        def get_indices_irregular(points,tree,shape):

            dist,idx = tree.query(points,k=1)
            ind = np.column_stack(np.unravel_index(idx,lon.shape))
            return [(i,j) for i,j in ind]

        if regular:
            return get_indices_regular(points,lon,lat)
        return get_indices_irregular(points,tree,lon.T.shape)

    tree = None
    if not regular_grid:
        lonlat = np.column_stack((lon.T.ravel(),lat.T.ravel()))
        tree = sp.spatial.cKDTree(lonlat)

    frequency = np.zeros(lon.shape)
    for path in paths:
        path_ij = get_indices(path,lon,lat,tree=tree,regular=regular_grid)
        raster_poly = Image.new("L",lon.shape,0)
        rasterize = ImageDraw.Draw(raster_poly)
        rasterize.polygon(path_ij,1)
        mask = sp.fromstring(raster_poly.tobytes(),'b')
        mask.shape = raster_poly.im.size[1],raster_poly.im.size[0]
        frequency += mask

    return frequency

rawDescription: count how often each field point is inside a contour

description: Count how often each field point is inside a contour


code:
words = {}

# read file and add to 'words'

rawDescription: Best way to store and use a large text-file in python

description: Store and use a large text-file in python


code:
class GraduateManager(models.Manager):
    def get_queryset(self):
        return super(GraduateManager, self).get_queryset().filter(graduated=True)

class UndergraduateManager(models.Manager):
    def get_queryset(self):
        return super(Undergraduate, self).get_queryset().filter(graduated=False)

class Student(models.Model):
    graduated = BooleanField()

    graduates = GraduateManager()
    undergraduates = UndergraduateManager()

rawDescription: Reuse filter() queryset in Django

description: Reuse filter() queryset in Django


code:
np.random.seed(seed)
np.random.normal(0, 1, (100, 3))

rawDescription: Regenerate vector of randoms in Python

description: Regenerate vector of randoms in Python


rawDescription: How to get last part of line with Python

description: Get last part of line with Python


code:
out = dict(getattr(self, self.LOOKUPDICT[k])({k: query[k]})
           for k in query.keys() - self.exclusions)

rawDescription: Python dictionary comprehension

description: Python dictionary comprehension


rawDescription: How convert list of dictionaries to a dictionary of dictionaries in Python


code: regex = re.compile('[,\.!?]') #etc.

rawDescription: Python, remove all non-alphabet chars from string


code:
class Input(models.Model):
    # This date has the month and the year. You can set the default day
    # to the 1st of every month for consistency.
    startDate=models.DateField()

    # This also has the end month and end year.
    endDate=models.DateField()

rawDescription: Django:how to concatenate the seperated integer field (Year, month) as date range to filter the database


code:
setattr(MyClass, "blah", property(self.get_blah, self.set_blah,
        self.del_blah, "bleh blih"))

rawDescription: How to create a property with its name in a string?

description: Create a property with its name in a string


rawDescription: YUV420p to other formats, color shift problems


code:
df.meter_read_date = pd.to_datetime(df.meter_read_date)
df['days_since'] = df.groupby('meternumber').meter_read_date.diff()
df = df.set_index('meter_read_date')
df['daily_consumption'] = df.consumption / df.days_since.dt.days

rawDescription: Impute daily values based on erratic monthly data measures in Python / Pandas


code:
def foo(**kwargs):
    print(kwargs)
foo(a = 'happy', g = 'birthday')

rawDescription: Python - Database query function with non-consistent arguments


code:
import time
from unittest import mock

@mock.patch('time.time', mock.MagicMock(return_value=12345))
def test_something():
    assert time.time() == 12345

rawDescription: Faking a time stamp from time.time() in Nose


code: Foo.blah.__get__(f, type(f))

rawDescription: Python method lookup rules

description: Python method lookup rules


code:
from datetime import datetime as dt
while True:
    if dt.now().hour in range(start, stop):  #start, stop are integers (eg: 6, 9)
        # call to your scheduled task goes here
        time.sleep(60)  # Minimum interval between task executions
    else:
        time.sleep(10)  # The else clause is not necessary but would prevent the program to keep the CPU busy.

rawDescription: Use sched module to run at a given time

description: Use sched module to run at a given time


rawDescription: Python: I have a dual key list, which is the most effective storage object for retreival


code: any(keyword in s for s in json_data["thing1"]["thing2"])

rawDescription: Finding text in json with python

description: Finding text in json with python


code:
import pickle
import multiprocessing

class Tricky:
    def __init__(self,x):
        self.data=x

    def __setstate__(self, d):
        print('setstate happening')
        self.data = 10

    def __getstate__(self):
        return self.data
        print('getstate happening')

def report(ar,q):
    q.put(ar.data)

if __name__ == '__main__':
    ar = Tricky(5)
    q = multiprocessing.Queue()
    p = multiprocessing.Process(target=report, args=(ar, q))
    print('now starting process')
    p.start()
    print('now joining process')
    p.join()
    print('now getting results from queue')
    print(q.get())
    print('now getting pickle dumps')
    print(pickle.loads(pickle.dumps(ar)).data)

rawDescription: multiprocessing ignores "__setstate__"

description: Multiprocessing ignores "__setstate__"


code: from module import *

rawDescription: Hide imported modules to interpreter

description: Hide imported modules to interpreter


code:
l=[]
for i in range (5):
 l.append(int(raw_input('enter %dth number :'%i)))

l

rawDescription: Python parsing, convert string to list of integers


rawDescription: Reading a CSV file, calculating averages and printing said averages


code:
for index in range(7):
    lotto[index] = random.randrange(1, 9)
    print("Lotto number", index, "is", lotto[index], "!")

rawDescription: Sequence elements will not modify (Python 3.4)


rawDescription: Multiple login fields in django user

description: Multiple login fields in django user


code:
# custom_filters.py
@register.filter(name='string_or_list')
def string_or_list(value, delimiter='\n'):
    """Renders string or each instance of a list with given delimiter."""
    if isinstance(value, list):
        return delimiter.join(value)
    return value

rawDescription: Generating complex tables with Django

description: Generating complex tables with Django


code:
import csv
with open('file_name.csv', 'wb') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ')
    #do stuff

rawDescription: Python array printing in unicode and csv issue


rawDescription: Select elements row-wise based on single array


code:
mask2 = (r>=17) & (r<=22)
res2 = r[mask2]*-0.4
I,J = np.where(mask2)
M2=sparse.coo_matrix((res2,(I,J)), r.shape)
M2.A
(M1+M2).A

rawDescription: How can I create a sparse matrix instead of a dense one in this program?

description: Create a sparse matrix instead of a dense one in this program


rawDescription: Fast process each line of one file for each line in another file


code:
freq = pd.Series(s.str.split(',').sum()).value_counts()
s.str.split(',').apply(lambda x: ','.join(sorted(x, key=freq.get, reverse=True)))

rawDescription: Sort str series by frequency

description: Sort str series by frequency


code:
import shapely.geometry as sg

def polygon_intersect_x(poly, x_val):
    """
    Find the intersection points of a vertical line at
    x=`x_val` with the Polygon `poly`.
    """
    if x_val < poly.bounds[0] or x_val > poly.bounds[2]:
        raise ValueError('`x_val` is outside the limits of the Polygon.')
    if isinstance(poly, sg.Polygon):
        poly = poly.boundary
    vert_line = sg.LineString([[x_val, poly.bounds[1]],
                               [x_val, poly.bounds[3]]])
    pts = [pt.xy[1][0] for pt in poly.intersection(vert_line)]
    pts.sort()
    return pts

rawDescription: Python - Return y coordinates of polygon path given x


code:
def autolabel(rects, values):
    # Attach some text labels.
    for (rect, value) in zip(rects, values):
        ax.text(rect.get_x() + rect.get_width() / 2.,
                rect.get_y() + rect.get_height() / 2.,
                '%d'%value,
                ha = 'center',
                va = 'center')

rawDescription: Y values on each stacked bar using matplotlib


code: '//' in currentLine

rawDescription: Inspecting source code files properly (with Python)


code:
def sum_to_n(n, size, limit=None):
    """Produce all lists of `size` positive integers in decreasing order
    that add up to `n`."""
    if size == 1:
        yield [n]
        return
    if limit is None:
        limit = n
    start = (n + size - 1) // size
    stop = min(limit, n - size + 1) + 1
    for i in range(start, stop):
        for tail in sum_to_n(n - i, size - 1, i):
            yield [i] + tail

rawDescription: Get all numbers that add up to a number

description: Get all numbers that add up to a number


code:
A = np.random.randint(2, size=(3992, 3992))
B = np.random.randint(50, size=(3992, 3992))

rawDescription: Plotting Histogram using data from 2 numpy matrices


code:
def gene(a):
    for b in range(a):
        if b==0 or b==1:
            continue
        if a%b==0:
            yield ("This is Composite number "+str(b))
    yield ("Out!")

gio = gene(12)
next(gio)
next(gio)
next(gio)
next(gio)
next(gio)

rawDescription: Can't understand generator

description: Can't understand generator


code:
def average( what ):
    if not isinstance( what, list ):
        return what
    if len(what) == 0:
        return None
    if not all( isinstance(item, (int, long, float, complex)) for item in what ):
        return what
    else:
        return sum( what ) / len( what )

rawDescription: Correcting the code in python

description: Correcting the code in python


code:
def repl(m):
    g = list(m.groups())
    g[1] = repl1
    g[3] = repl2
    return "".join(g)

rawDescription: How to replace only elements found using a python re.findall rather than entire string?

description: Replace only elements found using a python re.findall rather than entire string


code: user == "paper" or "Paper"

rawDescription: weird behaviour in python if clause

description: Weird behaviour in python if clause


rawDescription: numerical operation on two numpy ndarrays

description: Numerical operation on two numpy ndarrays


code:
import csv
in_file = open("d:/in.csv", "rb")
reader = csv.reader(in_file)
out_file = open("d:/out.csv", "wb")
writer = csv.writer(out_file)
for row in reader:
    row[3] = 4
    writer.writerow(row)
in_file.close()
out_file.close()

rawDescription: Writing to a particular cell using csv module in python


code:
def getter(self):
    try:
        return self._x
    except AttributeError:
        # access the attribute on the instance directly.
        try:
            return self.__dict__['x']
        except KeyError:
            raise AttributeError('x')

def setter(self, value):
    self._x = value

foo._x = foo.x
type(foo).x = property(getter, setter)

rawDescription: Define properties for object instances

description: Define properties for object instances


code:
colors = np.array(list('bgrcmykw'))

...

ax = df.plot(subplots=True,
             style=colors[np.random.randint(0, len(colors), df.shape[1])],
             sharex=True)

rawDescription: Change subplot color in DataFrame?

description: Change subplot color in DataFrame


code:
line = '0386      ; Greek # L&   GREEK CAPITAL LETTER ALPHA WITH TONOS'
point, script = map(str.strip, line.split('#')[0].split(';'))
point
script

rawDescription: Pythonic way of parsing this string?

description: Pythonic way of parsing this string


code:
import datetime
dt = str(datetime.datetime.now())

rawDescription: Python - How To Rename A Text File With DateTime


code:
regex = re.compile(...)
new_list = [s for s in old_list if regex.match(s)]

rawDescription: Delete all items in a list that don't contain a regular expression string


code:
example.cpp
#include "example.h"

rawDescription: Python Properties & Swig

description: Python Properties & Swig


code:
import time

def parse_info_line(line):
    # implement to your own liking
    logical_channel, physical_probe, hardware_id, crc = [line[index:index+2] for index in (1, 3, 5, 19)]
    serialno = line[7:19]
    return physical_probe

def parse_value_line(line):
    channel, crc = [line[ind:ind+2] for ind in (1,7)]
    encoded_temp = line[3:7]
    return twos_comp(int(encoded_temp, 16), 16)/100.

def twos_comp(val, bits):
    """compute the 2's compliment of int value `val`"""
    if (val & (1 << (bits - 1))) != 0: # if sign bit is set e.g., 8bit: 128-255
        val = val - (1 << bits)        # compute negative value
    return val                         # return positive value as is

def listen_on_serial(ser):
    ser.readline() # do nothing with the first line: you have no idea when you start listening to the data broadcast from the sensor
    while True:
        line = ser.readline()
        try:
            first_char = line[0]
        except IndexError:  # got no data from sensor
            break
        else:
            if first_char == '@':  # begins a new sensor record
                in_record = True
            elif first_char == '$':
                in_record = False
            elif first_char == 'I':
                parse_info_line(line)
            elif first_char == 'V':
                print(parse_value_line(line))
            else:
                print("Unexpected character at the start of the line:\n{}".format(line))
            time.sleep(2)

rawDescription: read and stock various data from various usb devices in python

description: Read and stock various data from various usb devices in python


code:
def writeToCsv(lst, limit):
    # use a context manager to close the file for you when you're done
    with open('file.csv', 'wb') as f:
        writer = csv.writer(f, delimiter=' ')
        # takes the first `max` dictionaries from the list
        for item in lst[:limit]:
            writer.writerow([item['year'], item['name']])

rawDescription: How to write to consecutive CSV columns in Python

description: Write to consecutive CSV columns in Python


code: objectList.append(DataObject(foo.c, lambda val: setattr(foo, 'c', val)))

rawDescription: Set a Python property using a function call


code:
class BlogPost(models.Model):
    #...
    def get_last_like(self):
        if hasattr(self, 'likes_by_request_user') and len(self.likes_by_request_user) > 0:
            return self.likes_by_request_user[0]
        return None

rawDescription: Django (django-rest-framework) looking for a best practice to find out wether request.user exists in blog.likes


code:
orderedData[1]
orderedData[11] = 'str5'
orderedData

rawDescription: Sort a Dictionary in Python Based on its Keys (when all keys are ints)


code:
fixed = [c for c in dataset.columns if c.startswith("fixed")]
fixed_dataset = dataset.ix[:, fixed]

rawDescription: Filter out columns containing specific word


code: outcome = true_expression if test_expression else false_expression

rawDescription: Is there a way to use an if statement as an argument?


code:
def __eq__(self, other):
    self.__dict__ == other.__dict__

rawDescription: Sort both elements of a tuple, ignoring case, within a list, so that the keys of a dictionary line up correctly


code:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.utils import platform

Builder.load_string('''
#:import lv kivy.uix.listview
#:import la kivy.adapters.listadapter

<MyWidget>:
    drives_list: drives_list
    file_chooser: file_chooser
    ListView:
        id: drives_list
        adapter:
            la.ListAdapter(data=root.get_win_drives(), selection_mode='single', allow_empty_selection=False, cls=lv.ListItemButton)
    FileChooserListView:
        id: file_chooser
''')

class MyWidget(BoxLayout):
    def __init__(self, **kwargs):
        super(MyWidget, self).__init__(**kwargs)
        self.drives_list.adapter.bind(on_selection_change=self.drive_selection_changed)

    def get_win_drives(self):
        if platform == 'win':
            import win32api

            drives = win32api.GetLogicalDriveStrings()
            drives = drives.split('\000')[:-1]

            return drives
        else:
            return []

    def drive_selection_changed(self, *args):
        selected_item = args[0].selection[0].text
        self.file_chooser.path = selected_item

class MyApp(App):
    def build(self):
        return MyWidget()

if __name__ == '__main__':
    MyApp().run()

rawDescription: Python kivy: how to use filechooser access files outside C drive


rawDescription: How does Python handle equality checking?


code:
three_mon_rel = relativedelta(months=3)
TODAY + three_mon_rel

rawDescription: How do you add "3 months" to a datetime.date object in python?

description: Add "3 months" to a datetime.date object in python


code:
# can't use .iteritems() as you can't/shouldn't modify something while iterating it
to_remove = d.keys()[500:] # slice off first 500 keys
for key in to_remove:
    del d[key]

rawDescription: Truncate the length of a Python dictionary


code:
tk.Button(self,text=b[0],width=5,height=2, bg="grey",
command=lambda text=b:self.call_both(text)).grid(row=r,column=c)

def call_both(self, line):
    self.name(line[1])
    self.info(line[2])

rawDescription: Using Lambda and tuples to send to Multiple Functions: Python, Tkinter


code:
import Tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root)
canvas.pack()
item = canvas.create_rectangle(50, 25, 150, 75, fill="blue")

def callback():
    canvas.itemconfig(item,fill='red')

button = tk.Button(root,text='Push me!',command=callback)
button.pack()

root.mainloop()

rawDescription: How to reconfigure tkinter canvas items?

description: Reconfigure tkinter canvas items


rawDescription: counting occurrence of consecutive elements in a list (python)

description: Counting occurrence of consecutive elements in a list (python)


code:
user1 = Info_model(username = 'Bill',
                   phone = 1231231234,
                   active = False)
user1.put()

rawDescription: Retrieving Datastore Object With Key

description: Retrieving Datastore Object With Key


code:
def variations_with_repetition(alphabetlen):
    """Return a list of all possible sets of len=3 with elements
    chosen from range(alphabetlen)."""

    a = np.arange(alphabetlen)
    z = np.vstack((
        np.repeat(a,alphabetlen**2),
        np.tile(np.repeat(a,alphabetlen),alphabetlen),
        np.tile(a,alphabetlen**2))).T
    return z

rawDescription: How do I optimize this python code using cython?

description: Optimize this python code using cython


rawDescription: Web Scraping Python Access Table data

description: Web Scraping Python Access Table data


rawDescription: Separating Words in a List Tkinter Listbox Python


code:
import scipy.sparse as sps

# number of nonzero entries per row of X
nnz_per_row = np.diff(X.indptr)
# multiply every row by the corresponding entry of v
# You could do this in-place as:
# X.data *= np.repeat(v, nnz_per_row)
Y = sps.csr_matrix((X.data * np.repeat(v, nnz_per_row), X.indices, X.indptr),
                   shape=X.shape)

# subtract from the non-zero entries the corresponding column value in c...
Y.data -= np.take(c, Y.indices)
# ...and multiply by -1 to get the value you are after
Y.data *= -1

rawDescription: Scipy Sparse Matrix special substraction


code:
def get_value(some_thing, some_key):
    if type(some_thing) in ('dict','tuple','list'):
        return some_thing[some_key]
    else:
        return getattr(some_thing, some_key)

rawDescription: python dictionary key Vs object attribute

description: Python dictionary key Vs object attribute


code:
from bs4 import BeautifulSoup
html_doc="""

<title>The practice html document</title>

I like pizza

I really like pizza

pizza is good

pancake

 This is a paragraph

"""
soup = BeautifulSoup(html_doc)
[elem.get_text() for elem in soup.select('div.dinner p.first')]

rawDescription: Adding elements to BeautifulSoup's find_all list as a string


code: re.findall('[^.!?]+', 'Bob is at the beginning. Bob after period!')

rawDescription: Possible to match at the start of a textfile without using regex?


code: myObjects.objects.filter(Q(a = 1) | Q( a = 2) | Q( a = 3))

rawDescription: How to use Q objects to test list membership?

description: Use Q objects to test list membership


code:
df =     df.unstack(level=1)
df =     df.reset_index()

rawDescription: How do I update dataframe column values with Pandas?

description: Update dataframe column values with Pandas


code:
#define IF    if(
#define THEN  ){
#define ELSE  } else {
#define ELIF  } else if (
#define FI    ;}

rawDescription: What are the downsides to defining a C macro that works like the Python "with" statement?


code:
s2 = 'WinneR'
[ord(i)-ord('a') for i in s2]

rawDescription: working with lists and strings

description: Working with lists and strings


code:
...
(r'/products/(\d+)/(\d+)', ProductHandler)
...

rawDescription: Parsing url with webapp2 handler Python GAE


code: trendingGrps = [Group.objects.filter(id=grp['which_group']).extra(select={"views":grp['total']})[0] for grp in trendingGrp_ids]

rawDescription: Highly challenging queryset filtering, sorting and annotation (in Django-based app)


code:
src_path = string.Template("/home/data/$pkg_name/file.zip").substitute(locals())
# Or maybe globals() instead of locals(), depending on where pkg_name is defined.

rawDescription: Is there any way to access variable like $var (similar to shell scripting) in python?


code:
['foobar',
 'fooba r',
 'foob ar',
 'foob a r',
 'foo bar',
 'foo ba r',
 'foo b ar',
 'foo b a r',
 'fo obar',
 'fo oba r',
 'fo ob ar',
 'fo ob a r',
 'fo o bar',
 'fo o ba r',
 'fo o b ar',
 'fo o b a r',
 'f oobar',
 'f ooba r',
 'f oob ar',
 'f oob a r',
 'f oo bar',
 'f oo ba r',
 'f oo b ar',
 'f oo b a r',
 'f o obar',
 'f o oba r',
 'f o ob ar',
 'f o ob a r',
 'f o o bar',
 'f o o ba r',
 'f o o b ar',
 'f o o b a r']

rawDescription: How do I generate all possible combinations of a string with spaces between the characters? Python

description: Generate all possible combinations of a string with spaces between the characters? Python


code:
with open('result_file', 'w') as f:
    f.write('\n'.join(result))

rawDescription: Python: optimal search for substring in list of strings


code:
from itertools import product
df

rawDescription: ensuring the Cartesian product of keys appears in a Pandas table

description: Ensuring the Cartesian product of keys appears in a Pandas table


rawDescription: How to do nested Django SELECT?

description: Do nested Django SELECT


code: search_result.sort(key=lambda x: (x.last_name, x.first_name))

rawDescription: python sorting objects in a list using to criteria

description: Python sorting objects in a list using to criteria


code:
def relations(**kwargs):
    if len(kwargs) != 1 or not set('abcd').intersection(kwargs):
        raise ValueError('Invalid parameters')
    vtype, v = kwargs.popitem()
    functions = {
        'a': lambda x: x, 'b': lambda x: x-1,
        'c': lambda x: x * 2, 'd': lambda x: x**2
    }

    a = functions[vtype](v)
    b = 1 + a
    c = 0.5 * a
    d = a ** 0.5
    return a,b,c,d

rawDescription: What’s the most Pythonic way of handling different, mutually-exclusive function inputs?


code:
class MyClass:
    def say_hello(self):
       print('Hello!')

class MyException(MyClass, Exception):
    def __init__(self, *args, **kwargs):
        self.say_hello()
        super(MyException, self).__init__(*args, **kwargs)

rawDescription: Python3 - call method from parent class

description: Python3 - call method from parent class


code:
with open( "input.txt", "w" ) as outFile:
    for item in oplist:
        outFile.write(item + "\n")

rawDescription: Printing to file works on Linux but not on Windows?


code: hats_list = list(get_hats())

rawDescription: Optimise filtering lists in Python 2.7

description: Optimise filtering lists in Python 2.7


code: lst = [None] * 100

rawDescription: Initialize a list of objects in Python

description: Initialize a list of objects in Python


code:
class GenericToolMeta(type):
    """Meta classs that is used by Tools to register in a central dictionary."""
    tools = {}

    def __new__(meta, classname, bases, classDict):
        new_class = type.__new__(meta, classname, bases, classDict)

        try:
            meta.tools[new_class.id] = new_class.title
        except AttributeError:
            pass
        return new_class

class GenericTool(object):
    __metaclass__ = GenericToolMeta

rawDescription: Registering object members in Python at import time


rawDescription: Use python (or R) to cleanup tables missing data


rawDescription: How do you pack three bytes into a python struct?

description: Pack three bytes into a python struct


code: output_dict[front['a']] = front

rawDescription: How to separate nested JSON

description: Separate nested JSON


code:
data = [x.split('') for x in a]
data

rawDescription: Python list to pandas dataframe

description: Python list to pandas dataframe


rawDescription: 1000 digits of pi in python

description: 1000 digits of pi in python


rawDescription: Extract specific characters from the string


code:
oG = Graph()
for p in pathDict.values():
    oG.add_path(p)

rawDescription: How can i optimize my graph using nx.single_source_shortest_path?

description: Optimize my graph using nx.single_source_shortest_path


code:
@contextlib.contextmanager
def dumping():
    for i in range(4):
        f = sys._getframe(i)
        print(f.f_code.co_filename, f.f_code.co_firstlineno, f.f_lineno)

rawDescription: Getting name of local variable at runtime in Python


code: admin_objects = UserAdminManager()

rawDescription: Filter a User list using a UserProfile field in Django Admin


code:
#! /usr/bin/python

import gtk
import gtk.glade
import gconf

class GConfExample:
    def __init__(self):
        self.client = gconf.client_get_default()

    def get_key(self, key):
        return client.get_string(key)

    def set_key(self, key, val):
        client.set_string(key, str(val))

rawDescription: access to gnome configuration information using python

description: Access to gnome configuration information using python


code:
collection_brochures = defaultdict(list)
for b in self.brochures.all():
    collection_brochures[b.collection.slug].append(b)
result = []
for collection, brochures in collection_brochures.iteritems():
    result.append({'collection': collection, 'brochures': brochures})

rawDescription: Django: how to get a dictionary with a related object as key from a querydict


code:
df = pd.DataFrame(np.random.randn(5, 2), pd.date_range('2014-01-01', periods=5, freq='H'), columns=list('AB'))
df
df.resample('D', how={'A': 'sum', 'B': 'last'})

rawDescription: Convert tick data to daily

description: Convert tick data to daily


code:
from PySide import QtCore, QtGui
import time

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 133)
        self.progressBar = QtGui.QProgressBar(Dialog)
        self.progressBar.setGeometry(QtCore.QRect(20, 10, 361, 23))
        self.progressBar.setProperty("value", 24)
        self.progressBar.setObjectName("progressBar")
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(20, 40, 361, 61))
        self.pushButton.setObjectName("pushButton")

        self.worker = Worker()
        self.worker.updateProgress.connect(self.setProgress)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

        self.progressBar.minimum = 1
        self.progressBar.maximum = 100

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("Dialog", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
        self.progressBar.setValue(0)
        self.pushButton.clicked.connect(self.worker.start)

    def setProgress(self, progress):
        self.progressBar.setValue(progress)

#Inherit from QThread
class Worker(QtCore.QThread):

    #This is the signal that will be emitted during the processing.
    #By including int as an argument, it lets the signal know to expect
    #an integer argument when emitting.
    updateProgress = QtCore.Signal(int)

    #You can do any extra things in this init you need, but for this example
    #nothing else needs to be done expect call the super's init
    def __init__(self):
        QtCore.QThread.__init__(self)

    #A QThread is run by calling it's start() function, which calls this run()
    #function in it's own "thread".
    def run(self):
        #Notice this is the same thing you were doing in your progress() function
        for i in range(1, 101):
            #Emit the signal so it can be received on the UI side.
            self.updateProgress.emit(i)
            time.sleep(0.1)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

rawDescription: Python PySide and Progress Bar Threading


code:
from BeautifulSoup import MinimalSoup
import urllib

fp = urllib.urlopen("http://www.ucanews.com/diocesan-directory/html/ordinary-of-philippine-cagayandeoro-parishes.html")
html = fp.read()
fp.close()

soup = MinimalSoup(html);

root = soup.table.tr.td

items = []
currentdistrict = None
# this loops through each "item"
for li in root.findAll(lambda tag: tag.name=='li' and len(tag.attrs)==0):
    attributes = []
    parishordistrict = li.next.strip()
     # look for string "district" to determine if district; otherwise it's something else under the district
    if parishordistrict.endswith(' District'):
        currentdistrict = parishordistrict
        attributes.append(('_isDistrict',True))
    else:
        attributes.append(('_isDistrict',False))

    attributes.append(('_name',parishordistrict))
    attributes.append(('_district',currentdistrict))

    # now loop through all attributes of this thing
    attributekeys = li.findAll('i')

    for i in attributekeys:
        key = i.string # normalize as needed. Will be 'Address:', 'Parochial Victor:', etc
        # now continue among the siblings until we reach an <i> again.
        # these are "values" of this key
        # if you want a nested key:[values] structure, you can use a dict,
        # but beware of multiple <i> with the same name in your logic
        next = i.nextSibling
        while next is not None and getattr(next, 'name', None) != 'i':
            if not hasattr(next, 'name') and getattr(next, 'string', None):
                value = next.string.strip()
                if value:
                    attributes.append((key, value))
            next = next.nextSibling
    items.append(attributes)

from pprint import pprint
pprint(items)

rawDescription: beautifulSoup screenscraping list of improperly nested <ul>s

description: BeautifulSoup screenscraping list of improperly nested <ul>s


code:
for fieldchild in field.findall('field'):
    result = findXXX(fieldchild)
    if result is not None:
        return result

rawDescription: recursion None python

description: Recursion None python


code: df

rawDescription: Parsing strings of date ranges in Pandas


rawDescription: pandas dataframe : creating pivot_table on a data frame without giving values

description: Pandas dataframe : creating pivot_table on a data frame without giving values


code:
import youtube_dl
# ... reddit stuff here ...

for item in submissions:
   # see options at https://github.com/rg3/youtube-dl/blob/master/youtube_dl/YoutubeDL.py#L89
   ydl_opts = {'outtmpl': item.title + '.%(ext)s'}
   with youtube_dl.YoutubeDL(ydl_opts) as ydl:
       ydl.download([item.url, ])

rawDescription: YouTube-dl to download and name videos from Reddit


rawDescription: Is fortran-like print in python possible?


code:
import itertools

frequencies = {}
for line in open_file: # You don't need .splitlines() to iterate, and you shouldn't use file as a name
    line = line.strip().split()
    list1_used = (x for x in list1 if x in line)
    list2_used = (x for x in list2 if x in line)
    for combination in itertools.product(list1_used, list2_used):
        frequencies[combination] = frequencies.get(combination, 0) + 1

rawDescription: Find combination of items in the same line in one file text and thei frequency


code:
# urls.py
urlpatterns = [
    url(r'^xxxx/(?P<id>[0-9]+)$', views.BaseView.as_view(Type=X), name="view_x"),
    url(r'^yyyy/(?P<id>[0-9]+)$', views.BaseView.as_view(Type=Y), name="view_x"),
]

rawDescription: Python reusing views and forms with model inheritance


code:
import numpy
numpy.choose([0, 1, 0, 1], [off, on])
numpy.product(numpy.choose([0, 1, 0, 1], [off, on]))

rawDescription: Python combinations of 2 lists

description: Python combinations of 2 lists


code: data["foo"] = "bar"

rawDescription: Basic savegames for a text based game

description: Basic savegames for a text based game


code:
now_ts = datetime.now(pytz.timezone('Europe/Istanbul'))
now_ts > make_aware(campaingObject.publish_end, pytz.timezone('Europe/Istanbul'))

rawDescription: How to make a time object TZ aware without changing the value?

description: Make a time object TZ aware without changing the value


code:
group = argparse.mutually_exclusive_group()
group.add_argument('--foo', action=StoreOption, subaction='store_true')
group.add_argument('--bar', nargs='?', action=StoreOption, subaction='store')

rawDescription: python argparse store --foo=bar as args.key='foo', args.value='bar'

description: Python argparse store --foo=bar as args.key='foo', args.value='bar'


code:
namespace = metaclass.__prepare__(name, bases, **kargs)
#..."cls" object gets created somewhere in here...
MyClass = metaclass.__new__(cls, name, bases, namespace, **kargs)
metaclass.__init__(MyClass, name, bases, namespace, **kargs)

rawDescription: Metaclass Arguments for Python 3.x

description: Metaclass Arguments for Python 3.x


code:
exit_code = subprocess.Popen(["py.test", "smoke_test_suite.py"]).wait()
test_failures = bool(exit_code)

rawDescription: How to make a py.test failure trigger outside functions?

description: Make a py.test failure trigger outside functions


code:
for i in range(5):
    print('test')
    print('this is a test')
print("Have any test funcs been called")

rawDescription: Will a for loop that calls a function finish before any of the functions are called?


code:
import os
from google.appengine.ext.remote_api import remote_api_stub
from google.appengine.datastore.entity_pb import Reference

remote_api_stub.ConfigureRemoteApi(app_id=None, path='/_ah/remote_api',
                                   auth_func=lambda: ('email', 'password'),
                                   servername='appid.appspot.com')

if os.environ['SERVER_SOFTWARE'].startswith('Development'):
  Reference.app = lambda *args: os.environ['APPLICATION_ID'].replace('dev~', 's~')

rawDescription: Is it possible to run dev_appserver.py with the remote datastore?


code:
def update_votes(song_id):
    song = Song.objects.get(id=song_id)
    song.votes += 1
    song.save()

rawDescription: Getting object value for AJAX call using Dajaxice


rawDescription: Does linux disk buffer cache make python cPickle more efficient than shelve?


code: singleitem = next(iter(mylist))

rawDescription: Getting only element from a single-element list in Python?


code:
@cell_magic
def sql(self, line, cell):
    """store the cell in the store"""
    self._store[line.strip()] = cell

rawDescription: Read cell content in an ipython notebook


code:
'{:02d}'.format(d.month)
'%02d' % d.month
d.strftime('%m')

rawDescription: python datetime extract double digit month and day values

description: Python datetime extract double digit month and day values


code:
person = Person.objects.get(pk=1)
form = PersonForm(instance=person)

rawDescription: Django how to set initial object to ModelList?


code:
type(deltaDF)
np.subtract.outer(deltaDF.iloc[0],deltaDF.iloc[0])
deltaDF.iloc[0][:,None] - deltaDF.iloc[0][None,:]

rawDescription: np.subtract.outer() returning more dimensions than I expect

description: Np.subtract.outer() returning more dimensions than I expect


code:
[{'level': 1,
  'page_number': 1,
  'sub': [{'level': 2, 'page_number': 2, 'sub': [], 'title': 'Section 1.1'},
          {'level': 2, 'page_number': 3, 'sub': [], 'title': 'Section 1.2'}],
  'title': 'Section 1'},
 {'level': 1,
  'page_number': 4,
  'sub': [{'level': 2,
           'page_number': 42,
           'sub': [{'level': 3,
                    'page_number': 42,
                    'sub': [],
                    'title': 'Section 2.1.1'}],
           'title': 'Section 2.1'}],
  'title': 'Section 2'},
 {'level': 1, 'page_number': 42, 'sub': [], 'title': 'Section 3'}]

rawDescription: Generate a nested list from flatten data in Python


code:
from itertools import chain
[v for z in ('a', 'b\nc', 'd') for v in z.splitlines()]
y = []
y.extend(v for z in ('a', 'b\nc', 'd') for v in z.splitlines())
y
y = []
y.extend(chain.from_iterable(z.splitlines() for z in ('a', 'b\nc', 'd')))
y

rawDescription: Can this generator expression be modified to match for loop result?


code:
import itertools
rows = (''.join(row) for row in board)
columns = (''.join(column) for column in zip(*board))
words = [word for line in itertools.chain(rows,columns) for word in line.split() if len(word) > 1]

rawDescription: Find all words placed on a Scrabble board


rawDescription: Best way to replace \x00 in python lists?

description: Replace \x00 in python lists


code:
import operator
sorted(DISKIMAGES, key=operator.attrgetter('epoch', 'size'), reverse=True)

rawDescription: Can I sort a list of objects by 2 keys?

description: Sort a list of objects by 2 keys


code:
class DataUI(object):
  def __init__(self, list_of_ones):
    for one in list_of_ones:
      assert type(one) == One
    self.list_of_ones = list_of_ones
    self.list_of_twos = ListView(
        self.list_of_ones,
        Two.newByDTO,
        Two.getDTO
        )

rawDescription: How can I change in Python the return/input type of a list that is implemented as an class attribute?

description: Change in Python the return/input type of a list that is implemented as an class attribute


code:
import csv
from itertools import product

def main():
    with open('file1.csv', 'rb') as f1, open('file2.csv', 'rb') as f2:
        reader1 = csv.reader(f1, dialect=csv.excel_tab)
        reader2 = csv.reader(f2, dialect=csv.excel_tab)

        # Step 1: Read and write the headers separately.
        header1, header2 = next(reader1), next(reader2)
        with open('output.csv', 'wb') as out:
            writer = csv.writer(out, dialect=csv.excel_tab)
            writer.writerow(header1 + header2)
            # Step 2: Write the product of the rest of the rows.
            writer.writerows(
                row1 + row2 for row1, row2 in product(reader1, reader2))

main()

rawDescription: Create the cartesian product (cross join) of two csv files in python


code:
[f for (subdir, dirs, files) in os.walk(rootdir) for f in files]

sum([files for (subdir, dirs, files) in os.walk(rootdir)], [])

rawDescription: Simple list comprehension

description: Simple list comprehension


code: rdiffArgs = ' '.join(rdiffArgs)

rawDescription: Scripting Python for Linux commands

description: Scripting Python for Linux commands


code:
from datetime import date
import struct

somedate = date(*struct.unpack('>HBB', "\x07\xdb\x0c\x01"))

rawDescription: How to unpack binary formatted dates in python?

description: Unpack binary formatted dates in python


rawDescription: Store new JSON data to existing file in python


code:
chisq = np.random.chisquare(4, 8)
chisq -= chisq.min()
chisq /= chisq.max()
errorbar_colors = cm.winter(chisq)

rawDescription: cmap to rgba in Matplotlib

description: Cmap to rgba in Matplotlib


code:
scraper = get_scraper('google')
scraper.run()

rawDescription: Pattern and design for functions that differ greatly from each other but are processed similarly


code:
def hexor(hex1, hex2):
    """XOR two hex strings."""
    xor = hex(int(hex1, 16) ^ int(hex2, 16))
    return xor[2:].rstrip("L")  # get rid of "0x" and maybe "L"

import random
a = "".join(random.choice("0123456789abcdef") for i in range(200))
b = "".join(random.choice("0123456789abcdef") for i in range(200))
a
b
hexor(a, b)

rawDescription: Xor Hex/ASCII/Conversion Issue

description: Xor Hex/ASCII/Conversion Issue


code:
for animal, letter in data:
    if animal in animals:
        animals[animal][letter] = 0
    else:
        animals[animal] = {letter: 0}

rawDescription: more on creating dictionaries of dictionaries

description: More on creating dictionaries of dictionaries


code: df['Time'] = pd.to_datetime(df['Time'], format='%d/%m/%Y %H:%M', errors='coerce')

rawDescription: Convert dataframe column to datetime for re-mapping


code:
a = ['hello ', 'goodbye ']
b = ['tom', 'dick', 'harry']
[['{} {}'.format(greeting, name) for name in b] for greeting in a]

rawDescription: creating new lists by adding elements of lists

description: Creating new lists by adding elements of lists


code:
import json
rs = json.dumps(dict(lst))

rawDescription: more pythonic way to format a JSON string from a list of tuples

description: More pythonic way to format a JSON string from a list of tuples


code:
with open('tem2.py', 'w') as f:
    f.write('list2 = ' + repr(mylist))
from tem2 import list2
print(list2 == mylist)
# True

rawDescription: How to write a list of lists to a txt file in python without using pickle or other alternatives?

description: Write a list of lists to a txt file in python without using pickle or other alternatives


code: selected = dict(zip(selected, selected))

rawDescription: Traversing and modifying a tree-like list of dict structure


code:
def start_app():
    app = QtGui.QApplication(sys.argv)

    main_window = QtGui.QWidget()
    xpathInput = QtGui.QLineEdit(main_window)

    web_view = MyWebView(main_window, xpathInput)  #<===HERE
    web_view.load(QUrl("http://www.indiatimes.com/"))

    main_window.show()
    sys.exit(app.exec_())

rawDescription: Return value from PYQt slot of QwebView to main window widget (PYQt python)


code:
import sqlite3
conn = sqlite3.connect(':memory:')

c = conn.cursor()
c.execute('''CREATE TABLE datum (floorid int, robotid int, partnum int)''')
conn.commit()

c = conn.cursor()
new_data = [(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1)]
c.executemany('INSERT INTO datum VALUES (?,?,?)', new_data)
conn.commit()

# exercise for the reader - add indexes

c = conn.cursor()
for row in c.execute('SELECT * FROM datum WHERE floorid=? and robotid=?', (1, 1)):
    print(row)

rawDescription: How to manage multiple indexes to same data?

description: Manage multiple indexes to same data


code: l = ttk.Checkbutton(root, text ="",variable=checkData[i],command=lambda i=i: onCheck(conditionID[i],checkData[i]), onvalue=True, offvalue=False)

rawDescription: Issue with generating ttk checkboxes in loops and passing arguments


code:
with open(filename) as f:
     for line in f:
         PyparsingGrammar.parseString(line)

rawDescription: how to feed string to pyparsing line by line?

description: Feed string to pyparsing line by line


code:
class More(object):
  def squared(self, x):
    return x*x

import pathos

p = pathos.pp.ParallelPythonPool(servers=('localhost,1234',))

m = More()
p.map(m.squared, range(5))

rawDescription: Pickle python class instance plus definition


code:
if "HTTP_AUTHORIZATION" in os.environ:
  (tokentype, token)  = os.environ["HTTP_AUTHORIZATION"].split(" ")

rawDescription: Make calls to Drive API inside of a Google App Engine Cloud Endpoints


code:
def im2col(A,BLKSZ):

    # Parameters
    M,N = A.shape
    col_extent = N - BLKSZ[1] + 1
    row_extent = M - BLKSZ[0] + 1

    # Get Starting block indices
    start_idx = np.arange(BLKSZ[0])[:,None]*N + np.arange(BLKSZ[1])

    # Get offsetted indices across the height and width of input array
    offset_idx = np.arange(row_extent)[:,None]*N + np.arange(col_extent)

    # Get all actual indices & index into input array for final output
    return np.take (A,start_idx.ravel()[:,None] + offset_idx.ravel())

rawDescription: How can I check if one two-dimensional NumPy array contains a specific pattern of values inside it?

description: Check if one two-dimensional NumPy array contains a specific pattern of values inside it


code:
import datetime as DT
import pytz

def convert_mills_GMT(milliseconds,
                      utc=pytz.utc,
                      eastern=pytz.timezone('US/Eastern')
):
    converted_raw = DT.datetime.fromtimestamp(milliseconds/1000.0)
    date_eastern = eastern.localize(converted_raw, is_dst=True)
    date_utc = date_eastern.astimezone(utc)
    timestamp = ((date_utc.replace(tzinfo=None) - DT.datetime(1970, 1, 1))
                 .total_seconds())
    return int(timestamp) * 1000

print(DT.datetime.utcfromtimestamp(convert_mills_GMT(1432202088224)/1000.0))

rawDescription: Convert EST timestamp to GMT taking into account Daylight Savings


code: a_list = map(functions[use_function], range(0, 100))

rawDescription: Is there a way to avoid useless if-else clauses inside a loop when selectign from multiple functions?


code:
import os
import re

def sorter(filename):
    match = re.search(r'\d+', filename)
    if match:
        return int(match.group())
    return 0

files = sorted(os.listdir('mydir/'), key = lambda fname: sorter(fname))

rawDescription: sort files on the filesystem using a cutom sort

description: Sort files on the filesystem using a cutom sort


code:
def tearDown(self):
    self.browser.quit()
    self.tb.deactivate()
    self.server.stop()

rawDescription: Why can't I import a model from a file containing import of any of the Google App Engine components?


code:
raw_data  = [(lhs, rhs)
            for lhs, rhs
            in [s.split(',')[:2] for s in all_lines]
            if rhs != '"NaN"']

rawDescription: Temporary variable within list comprehension


code:
f = open('array.dat', 'rb')
f.seek(7 * a.itemsize)
raw = f.read(a.itemsize)
import struct
struct.unpack(a.typecode, raw)

rawDescription: Storage size of array.array in a file

description: Storage size of array.array in a file


code:
class AttributeBlock():
    objects = {}
    dummy = {}
    def __init__(self, key, label, isClosed, isRequired,
             attributes, glob = None):
        if glob is None:
            glob = self.objects
        if key in glob:
            raise ValueError(str(key) + " already exists")
        self.key = key
        self.label = label
        self.isClosed = isClosed
        self.isRequired = isRequired
        self.attributes = attributes if attributes is not None else {}
        if glob is not self.dummy:
            glob[key] = self
    def copy(self):
        return AttributeBlock(self.key, self.label, self.isClosed,
                      self.isRequired, self.attributes[:],
                      self.dummy)

rawDescription: creating a new copy of a python class each time it's called

description: Creating a new copy of a python class each time it's called


code:
from itertools import groupby
from tempfile import NamedTemporaryFile
from shutil import move

with open("in.txt") as f, NamedTemporaryFile("w",dir=".",delete=False) as out:
    grps = groupby(f,key=lambda x: x.startswith("$"))
    for k, v in grps:
        if k:
            val = next(v)
            out.write(val)
            if val.startswith("$ yum"):
                out.writelines(next(grps,[[], [""]])[1])
                out.write("I am a new line\n")
        else:
            out.writelines(v)
move(out.name,"in.txt")

rawDescription: inserting a line in a file using python

description: Inserting a line in a file using python


code:
def addDateTimeResults(tokens):
    tokens['date'] = ('20'+tokens.year, tokens.month, tokens.day)
    tokens['time'] = (tokens.hour, tokens.min, tokens.sec)
    tokens['datetime'] = ParseResults([tokens.date, tokens.time])
    for name in ('date', 'time'):
        tokens['datetime'][name] = tokens[name]
grammar.setParseAction(addDateTimeResults)

rawDescription: How to group tokens placed in different positions using pyparsing?

description: Group tokens placed in different positions using pyparsing


rawDescription: python how to change element in nested list

description: Python how to change element in nested list


code: df[cat_cols].apply(lambda row: ', '.join(row.index[row == 1]), axis=1)

rawDescription: Pandas Dataframe Filtering Columns and return column name


code:
import re
from collections import Counter

#get the length of the guess - an int number  between 4(included) to 6(included)
secretL = input()
#get the base of the guess - an int number  between 6(included) to 10(included)
secretB = input()
#get the guess
secret = raw_input()

# Check if the secret input uses a digit greater than the base value
# You can use the check on the next line as well. It can be made faster
# too by using search in place of findall.
# if len(re.findall(r"[^0-" + str(secretB) + r" ]+", secret)):
if sum((i!=" " and int(i) > secretB) for i in secret) > 0:
  print("Error")
  quit()

secretNumbers = map(int, secret.split())
guess = raw_input()
guessNumbers = map(int, guess.split())

# Count the bulls by iterating over the two lists simultaneously
bulls = sum(i==j for i, j in zip(secretNumbers, guessNumbers))

# Remove the bulls before counting the cows
secretNumbers, guessNumbers = zip(*((x,y) for x,y in zip(secretNumbers, guessNumbers) if x!=y))

# Cows are defined as the elements present in both the guess and the secret
# but not in the right position.
# If we ignore duplicates, this is the way to go about it.
cows = len(set(secretNumbers) & set(guessNumbers))

## If we count [1,1,2,4] and [5,3,1,1] to have 2 cows, this is how it should be:
#counter = Counter(secretNumbers) & Counter(guessNumbers)
#cows = sum(counter.itervalues())

print(str(bulls) + " BULLS " + str(cows) + " COWS")

rawDescription: Bulls & cows (mastermind) in Python. Should be relatively easy


code:
def triangle(n):
    for i in range(1, n +1):
        print ('x' * i).rjust(n, ' ')

rawDescription: How to make a triangle of x's in python?

description: Make a triangle of x's in python


rawDescription: efficient algorithm instead of looping

description: Efficient algorithm instead of looping


code:
{'category': 'A', 'fruit': 'apple', 'type': 'new', 'order': '1'}
{'category': 'B', 'fruit': 'banana', 'type': 'old', 'order': '2'}

rawDescription: How to merge multiple dictionaries from separate lists if they share any key-value pairs?

description: Merge multiple dictionaries from separate lists if they share any key-value pairs


code:
z = np.array([F(x,y) for x,y in zip(np.ravel(X), np.ravel(Y))])
Z = z.reshape(X.shape)

rawDescription: Python show a two-dimensional PDF function


code:
import itertools

def new_phrases(phrase, syns):
    """Generate new phrases from a base phrase and synonyms."""
    words = [syns.get(word, [word]) for word in phrase.split(' ')]
    for t in itertools.product(*words):
        yield ' '.join(t)

def get_matches(phrase, syns, phrases):
    """Generate acceptable new phrases based on a whitelist."""
    phrases = set(phrases)
    for new_phrase in new_phrases(phrase, syns):
        if new_phrase in phrases:
            yield new_phrase

rawDescription: Return a list of matches by given phrase


code:
order = Order.objects.get(pk=123)
order_items = list(order.order_items.all())

rawDescription: From queryset to list, is it expensive operation?


code: helloFunc = hello_func

rawDescription: Renaming of functions with preservation of backward compatibility


code:
def pascal_row(levels):
    if levels in pascal_cache.keys():
        return pascal_cache[levels]
    else:
        row = []
        row.append(1)
        prev = pascal_row(levels-1)
        for i in range(len(prev)):
            try:
                row.append(prev[i] + prev[i+1])
            except:
                pass
        row.append(1)
        pascal_cache[levels]=row
        return row

rawDescription: Optimization of Recursive Pascal Triangle Function


code: init_b = min(y)

rawDescription: How to force zero interception in linear regression?

description: Force zero interception in linear regression


rawDescription: Vectorizing for loops in python with numpy multidimensional arrays


code:
for i in range(2,n):
    if n%i==0:
        a=0
        break

rawDescription: removing "none" result from the function which prints the prime number

description: Removing "none" result from the function which prints the prime number


code:
@myblueprint.route('/YourFlaskRoute', methods = ['POST'])
def refresh_tags():
    if request.method == 'POST':
        data = request.json

        #access your data
        for key, value in data.items():
            key = id
            value = id

        # run your query
        tags = ...

        #send back your list items template
        return render_template('list_items.html', tags = tags)

rawDescription: Sending a list of values in Jquery to flask template


code:
class a:
    @HTMLwrapper
    def return_stuff_as_html(self, input):
        return self.return_stuff(input)
    def return_stuff(self, input):
        return awesome_dict

rawDescription: How to skip or ignore python decorators

description: Skip or ignore python decorators


rawDescription: How to view Boto3 HTTPS request string

description: View Boto3 HTTPS request string


code:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproj.settings")
from myapp.models import Stuff
# ...

rawDescription: Django Python Shell

description: Django Python Shell


code:
def ask_exe(prompt='Executable file name? '):
    name = input(prompt)
    if name[-4:] in {'.exe', '.bat'}: return name
    return ask_exe(prompt='The name has to end in ".exe" or ".bat", please retry: ')

rawDescription: python: check file extension and prompt again input if invalid file extension

description: Python: check file extension and prompt again input if invalid file extension


code:
def switchFiles(args):
    pairs = []
    for root1, dirs1, files1 in os.walk(args.folder):
        for f1 in files1:
            for root2, dirs2, files2 in os.walk(args.database):
                for f2 in files2:
                    if f1 == f2:
                        pairs.append(os.path.join(root1, f1), os.path.join(root2, f2))
    for src, dst in pairs:
        shutil.copyfile(src, dst)

rawDescription: Optimizing simple Python file matching & replacement script


code:
x1
y1

rawDescription: Extend different shapes/dimension matrices in python


rawDescription: When django model access DB

description: When django model access DB


code:
with open('file') as f:
    a = f.read().splitlines()

l = [a[i][i:] for i in range(len(a))]
final_list = [''.join(i) for i in zip(*l)]

print(final_list)

rawDescription: Reading from a file diagonally and having the diagonal characters create a list


code:
hashlib.sha256('Ronald').hexdigest() == data['user']
hashlib.sha256('Roland').hexdigest() == data['user']

rawDescription: How do I store account information in python so that it can be accessed later?

description: Store account information in python so that it can be accessed later


rawDescription: Scipy: Argrelmax Find Maxmima along Dimension in 3d Array


code:
df_iter = pd.read_csv("basic.csv", usecols=["item1", "item2", "item3", "item4"], chunksize=10)
sum(c.apply(pd.value_counts).fillna(0) for c in df_iter)

rawDescription: extract basic count per values using the chunksize parameter in pandas

description: Extract basic count per values using the chunksize parameter in pandas


rawDescription: Transform slow pandas iterrows into apply


code:
record = [12, 'Tom']
cursor.execute("insert into test (age,name) values(%s,%s)", record)

rawDescription: Mysql with Python can't insert record with autoincrement id, why?


code: d = map(lambda x: 1 if x else 0, d)

rawDescription: Only nonzero values Python list

description: Only nonzero values Python list


code:
re.search('((?!.*thumb))(.*)', '/tmp/somewhere/thumb').groups()
re.search('^((?!.*thumb))(.*)', '/tmp/somewhere/thumb').groups()
re.match('((?!.*thumb))(.*)', '/tmp/somewhere/thumb').groups()

rawDescription: negative lookahead assertion not working in python

description: Negative lookahead assertion not working in python


code: items = list(zip(*time_list))

rawDescription: List index out of range when using more than one at the same time


code:
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(123456)
import pandas as pd

df = pd.DataFrame(3 * np.random.rand(4, 4), index=['a', 'b', 'c', 'd'],
                  columns=['x', 'y','z','w'])

plt.style.use('ggplot')
colors = plt.rcParams['axes.color_cycle']

fig, axes = plt.subplots(nrows=2, ncols=3)
for ax in axes.flat:
    ax.axis('off')

for ax, col in zip(axes.flat, df.columns):
    ax.pie(df[col], labels=df.index, autopct='%.2f', colors=colors)
    ax.set(ylabel='', title=col, aspect='equal')

axes[0, 0].legend(bbox_to_anchor=(0, 0.5))

fig.savefig('your_file.png') # Or whichever format you'd like
plt.show()

rawDescription: How to make MxN piechart plots with one legend and removed y-axis titles in Matplotlib

description: Make MxN piechart plots with one legend and removed y-axis titles in Matplotlib


code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# generate some data with noise
xData = np.linspace(0.01, 100., 50)
aOrg = 0.08
Norg = 10.5
yData = Norg * xData ** (-aOrg) + np.random.normal(0, 0.5, len(xData))

# get logarithmic data
lx = np.log(xData)
ly = np.log(yData)

def f(x, N, a):
    return N * x ** (-a)

def f_log(x, lN, a):
    return a * x + lN

# optimize using the appropriate bounds
popt, pcov = curve_fit(f, xData, yData, bounds=(0, [30., 20.]))
popt_log, pcov_log = curve_fit(f_log, lx, ly, bounds=([0, -10], [30., 20.]))

xnew = np.linspace(0.01, 100., 5000)

# plot the data
plt.plot(xData, yData, 'bo')
plt.plot(xnew, f(xnew, *popt), 'r')
plt.plot(xnew, f(xnew, np.exp(popt_log[0]), -popt_log[1]), 'k')
plt.show()

rawDescription: SciPy Curve Fit Fails Power Law

description: SciPy Curve Fit Fails Power Law


code:
class Hierarchy(models.Model):
    parent = models.ForeignKey('self',null=True,blank=True)

    def get_children(self):
        return self._default_manager.filter(parent=self)

    def get_descendants(self):
        descs = set(self.get_children())
        for node in list(descs):
            descs.update(node.get_descendants())
        return descs

    class Meta:
        abstract = True

rawDescription: hierarchal permission on same model

description: Hierarchal permission on same model


code:
class BackupForm(ModelForm):
    """Form for adding and editing backups."""

    def __init__(self, *args, **kwargs):
        systemid = kwargs.pop('systemid')
        super(BackupForm, self).__init__(*args, **kwargs)
        self.fields['units'] = forms.ModelMultipleChoiceField(
                required=False,
                queryset=Unit.objects.filter(system__id=systemid),
                widget=forms.SelectMultiple(attrs={'title': _("Add unit")}))

    class Meta:
        model = Backup
        exclude = ('system',)

rawDescription: How to pass initial parameter to django's ModelForm instance?

description: Pass initial parameter to django's ModelForm instance


code:
import lxml.html.clean as clean

code = '<tr id="ctl00_Content_AdManagementPreview_DetailView_divNova" class="Extended" style="display: none;">'

safe_attrs = clean.defs.safe_attrs
cleaner = clean.Cleaner(safe_attrs_only=True, safe_attrs=frozenset())
cleansed = cleaner.clean_html(code)

print(cleansed)

rawDescription: remove certain attributes from HTML tags

description: Remove certain attributes from HTML tags


code:
def assign_coo_to_dense(sparse, dense):
    dense[sparse.row, sparse.col] = sparse.data

rawDescription: Load sparse scipy matrix into existing numpy dense matrix


code:
class GetSetDesc(object):
    def __init__(self, value):
        self.value=value

    def __get__(self, obj, objtype):
        print("get_set_desc: Get")
        return self.value

    def __set__(self, obj, value):
        print("get_set_desc: Set")
        self.value=value

class SetDesc(object):
    def __init__(self, value):
        self.value=value

    def __set__(self, obj, value):
        print("set_desc: Set")
        self.value=value

class GetDesc(object):
    def __init__(self, value):
        self.value=value

    def __get__(self, obj, objtype):
        print("get_desc: Get")
        return self.value

class Test1(object):
    attr=10
    get_set_attr=10
    get_set_attr=GetSetDesc(5)
    set_attr=10
    set_attr=SetDesc(5)
    get_attr=10
    get_attr=GetDesc(5)

class Test2(object):
    def __init__(self):
        self.attr=10
        self.get_set_attr=10
        self.get_set_attr=GetSetDesc(5)
        self.set_attr=10
        self.set_attr=SetDesc(5)
        self.get_attr=10
        self.get_attr=GetDesc(5)

class Test3(Test1):
    def __init__(self):
        #changing values to see differce with superclass
        self.attr=100
        self.get_set_attr=100
        self.get_set_attr=GetSetDesc(50)
        self.set_attr=100
        self.set_attr=SetDesc(50)
        self.get_attr=100
        self.get_attr=GetDesc(50)

class Test4(Test1):
    pass

print("++Test 1 Start++")
t=Test1()

print("t.attr:", t.attr)
print("t.get_set_desc:", t.get_set_attr)
print("t.set_attr:", t.set_attr)
print("t.get_attr:", t.get_attr)

print("Class dict attr:", t.__class__.__dict__['attr'])
print("Class dict get_set_attr:", t.__class__.__dict__['get_set_attr'])
print("Class dict set_attr:", t.__class__.__dict__['set_attr'])
print("Class dict get_attr:", t.__class__.__dict__['get_attr'])

#These will obviously fail as instance dict is empty here
#print("Instance dict attr:", t.__dict__['attr'])
#print("Instance dict get_set_attr:", t.__dict__['get_set_attr'])
#print("Instance dict set_attr:", t.__dict__['set_attr'])
#print("Instance dict get_attr:", t.__dict__['get_attr'])

t.attr=20
t.get_set_attr=20
t.set_attr=20
t.get_attr=20

print("t.attr:", t.attr)
print("t.get_set_desc:", t.get_set_attr)
print("t.set_attr:", t.set_attr)
print("t.get_attr:", t.get_attr)

print("Class dict attr:", t.__class__.__dict__['attr'])
print("Class dict get_set_attr:", t.__class__.__dict__['get_set_attr'])
print("Class dict set_attr:", t.__class__.__dict__['set_attr'])
print("Class dict get_attr:", t.__class__.__dict__['get_attr'])

print("Instance dict attr:", t.__dict__['attr'])
#Next two will fail,
#because the descriptor for those variables has __set__
#on the class itself which was called with value 20,
#so the instance is not affected
#print("Instance dict get_set_attr:", t.__dict__['get_set_attr'])
#print("Instance dict set_attr:", t.__dict__['set_attr'])
print("Instance dict get_attr:", t.__dict__['get_attr'])

print("++Test 1 End++")

print("++Test 2 Start++")
t2=Test2()

print("t.attr:", t2.attr)
print("t.get_set_desc:", t2.get_set_attr)
print("t.set_attr:", t2.set_attr)
print("t.get_attr:", t2.get_attr)

#In this test the class is not affected, so these will fail
#print("Class dict attr:", t2.__class__.__dict__['attr'])
#print("Class dict get_set_attr:", t2.__class__.__dict__['get_set_attr'])
#print("Class dict set_attr:", t2.__class__.__dict__['set_attr'])
#print("Class dict get_attr:", t2.__class__.__dict__['get_attr'])

print("Instance dict attr:", t2.__dict__['attr'])
print("Instance dict get_set_attr:", t2.__dict__['get_set_attr'])
print("Instance dict set_attr:", t2.__dict__['set_attr'])
print("Instance dict get_attr:", t2.__dict__['get_attr'])

t2.attr=20
t2.get_set_attr=20
t2.set_attr=20
t2.get_attr=20

print("t.attr:", t2.attr)
print("t.get_set_desc:", t2.get_set_attr)
print("t.set_attr:", t2.set_attr)
print("t.get_attr:", t2.get_attr)

#In this test the class is not affected, so these will fail
#print("Class dict attr:", t2.__class__.__dict__['attr'])
#print("Class dict get_set_attr:", t2.__class__.__dict__['get_set_attr'])
#print("Class dict set_attr:", t2.__class__.__dict__['set_attr'])
#print("Class dict get_attr:", t2.__class__.__dict__['get_attr'])

print("Instance dict attr:", t2.__dict__['attr'])
print("Instance dict get_set_attr:", t2.__dict__['get_set_attr'])
print("Instance dict set_attr:", t2.__dict__['set_attr'])
print("Instance dict get_attr:", t2.__dict__['get_attr'])

print("++Test 2 End++")

print("++Test 3 Start++")
t3=Test3()

print("t.attr:", t3.attr)
print("t.get_set_desc:", t3.get_set_attr)
print("t.set_attr:", t3.set_attr)
print("t.get_attr:", t3.get_attr)

#These fail, because nothing is defined on Test3 class itself, but let's see its super below
#print("Class dict attr:", t3.__class__.__dict__['attr'])
#print("Class dict get_set_attr:", t3.__class__.__dict__['get_set_attr'])
#print("Class dict set_attr:", t3.__class__.__dict__['set_attr'])
#print("Class dict get_attr:", t3.__class__.__dict__['get_attr'])

#Checking superclass
print("Superclass dict attr:", t3.__class__.__bases__[0].__dict__['attr'])
print("Superclass dict get_set_attr:", t3.__class__.__bases__[0].__dict__['get_set_attr'])
print("Superclass dict set_attr:", t3.__class__.__bases__[0].__dict__['set_attr'])
print("Superclass dict get_attr:", t3.__class__.__bases__[0].__dict__['get_attr'])

print("Instance dict attr:", t3.__dict__['attr'])
#Next two with __set__ inside descriptor fail, because
#when the instance was created, the value inside the descriptor in superclass
#was redefined via __set__
#print("Instance dict get_set_attr:", t3.__dict__['get_set_attr'])
#print("Instance dict set_attr:", t3.__dict__['set_attr'])
print("Instance dict get_attr:", t3.__dict__['get_attr'])
#The one above does not fail, because it doesn't have __set__ in
#descriptor in superclass and therefore was redefined on instance

t3.attr=200
t3.get_set_attr=200
t3.set_attr=200
t3.get_attr=200

print("t.attr:", t3.attr)
print("t.get_set_desc:", t3.get_set_attr)
print("t.set_attr:", t3.set_attr)
print("t.get_attr:", t3.get_attr)

#print("Class dict attr:", t3.__class__.__dict__['attr'])
#print("Class dict get_set_attr:", t3.__class__.__dict__['get_set_attr'])
#print("Class dict set_attr:", t3.__class__.__dict__['set_attr'])
#print("Class dict get_attr:", t3.__class__.__dict__['get_attr'])

#Checking superclass
print("Superclass dict attr:", t3.__class__.__bases__[0].__dict__['attr'])
print("Superclass dict get_set_attr:", t3.__class__.__bases__[0].__dict__['get_set_attr'])
print("Superclass dict set_attr:", t3.__class__.__bases__[0].__dict__['set_attr'])
print("Superclass dict get_attr:", t3.__class__.__bases__[0].__dict__['get_attr'])

print("Instance dict attr:", t3.__dict__['attr'])
#Next two fail, they are in superclass, not in instance
#print("Instance dict get_set_attr:", t3.__dict__['get_set_attr'])
#print("Instance dict set_attr:", t3.__dict__['set_attr'])
print("Instance dict get_attr:", t3.__dict__['get_attr'])
#The one above succeds as it was redefined as stated in prior check

print("++Test 3 End++")

print("++Test 4 Start++")
t4=Test4()

print("t.attr:", t4.attr)
print("t.get_set_desc:", t4.get_set_attr)
print("t.set_attr:", t4.set_attr)
print("t.get_attr:", t4.get_attr)

#These again fail, as everything defined in superclass, not the class itself
#print("Class dict attr:", t4.__class__.__dict__['attr'])
#print("Class dict get_set_attr:", t4.__class__.__dict__['get_set_attr'])
#print("Class dict set_attr:", t4.__class__.__dict__['set_attr'])
#print("Class dict get_attr:", t4.__class__.__dict__['get_attr'])

#Checking superclass
print("Superclass dict attr:", t4.__class__.__bases__[0].__dict__['attr'])
print("Superclass dict get_set_attr:", t4.__class__.__bases__[0].__dict__['get_set_attr'])
print("Superclass dict set_attr:", t4.__class__.__bases__[0].__dict__['set_attr'])
print("Superclass dict get_attr:", t4.__class__.__bases__[0].__dict__['get_attr'])

#Again, everything is on superclass, not the instance
#print("Instance dict attr:", t4.__dict__['attr'])
#print("Instance dict get_set_attr:", t4.__dict__['get_set_attr'])
#print("Instance dict set_attr:", t4.__dict__['set_attr'])
#print("Instance dict get_attr:", t4.__dict__['get_attr'])

t4.attr=200
t4.get_set_attr=200
t4.set_attr=200
t4.get_attr=200

print("t.attr:", t4.attr)
print("t.get_set_desc:", t4.get_set_attr)
print("t.set_attr:", t4.set_attr)
print("t.get_attr:", t4.get_attr)

#Class is not affected by those assignments, next four fail
#print("Class dict attr:", t4.__class__.__dict__['attr'])
#print("Class dict get_set_attr:", t4.__class__.__dict__['get_set_attr'])
#print("Class dict set_attr:", t4.__class__.__dict__['set_attr'])
#print("Class dict get_attr:", t4.__class__.__dict__['get_attr'])

#Checking superclass
print("Superclass dict attr:", t4.__class__.__bases__[0].__dict__['attr'])
print("Superclass dict get_set_attr:", t4.__class__.__bases__[0].__dict__['get_set_attr'])
print("Superclass dict set_attr:", t4.__class__.__bases__[0].__dict__['set_attr'])
print("Superclass dict get_attr:", t4.__class__.__bases__[0].__dict__['get_attr'])

#Now, this one we redefined it succeeds
print("Instance dict attr:", t4.__dict__['attr'])
#This one fails it's still on superclass
#print("Instance dict get_set_attr:", t4.__dict__['get_set_attr'])
#Same here - fails, it's on superclass, because it has __set__
#print("Instance dict set_attr:", t4.__dict__['set_attr'])
#This one succeeds, no __set__ to call, so it was redefined on instance
print("Instance dict get_attr:", t4.__dict__['get_attr'])

print("++Test 4 End++")

rawDescription: Why attribute lookup in Python is designed this way (precedence chain)?


code: {v: old_dic[k] for k,v in mapping_dic.items()}

rawDescription: How to create a new dictionary from old one using a given mapping?

description: Create a new dictionary from old one using a given mapping


code: [[(data[i,j], data[i+56,j]) for i in range(56)] for j in range(40)]

rawDescription: Numpy: Reshaping this array in the least number of operations


code:
ESC = Literal('\x1b')
integer = Word(nums)
escapeSeq = Combine(ESC + '[' + Optional(delimitedList(integer,';')) +
                oneOf(list(alphas)))

rawDescription: Getting correct string length in Python for strings with ANSI color codes


rawDescription: extracting data from multiple text files using python

description: Extracting data from multiple text files using python


rawDescription: Sorting a list based on another list with DSU


code:
import os
list1 = ['myfile.v', 'myfile2.sv', 'myfile3.vhd', 'etcfile.v', 'randfile.sv']
list2 = ['myfile', 'myfile2', 'myfile3']
set2 = set(list2)  # Use a set for O(1) lookups
list1 = [x for x in list1 if os.path.splitext(x)[0] not in set2]

rawDescription: Python list.remove(x) 2.7.5

description: Python list.remove(x) 2.7.5


code:
def mycreate():
    return NamedTemporaryFile()
def mywrite(f, i):
    f.write(i)
def myread(f):
    f.seek(0)
    return f.read()

f = mycreate()        # 'f' is now a reference to the file created in the function,
                      # which will keep it from being garbage collected
mywrite(f, b'Hi')
myread(f)

rawDescription: How to used NamedTemporaryFile (and when is it closed?)

description: Used NamedTemporaryFile (and when is it closed?)


code:
#my.html
{{ error|safe }}

rawDescription: How to escape partially a template variable in Django

description: Escape partially a template variable in Django


code:
def tolist(commaseparated):
    return map(str.strip, commaseparated.split(',')) if commaseparated else []

rawDescription: DRY version of similar string processing


code:
[something]
[something]

rawDescription: Python 2.7 string

description: Python 2.7 string