rawDescription: Is it possible to scale data by group in Spark?


code:
import matplotlib.pyplot as plt

plt.plot(range(10))
plt.title('This is some random font', family='GroovyGhosties', size=32)

plt.show()

rawDescription: How to use a (random) *.otf or *.ttf font in matplotlib?

description: Use a (random) *.otf or *.ttf font in matplotlib


code:
last = ran #previous number
#generate new until it becomes different
while ran == last:
    ran = random.randrange(0,11)

rawDescription: block repeat of random only for certain step through the random value

description: Block repeat of random only for certain step through the random value


code:
while (looperCPU != 0):
    start_time = time.time()
    # Do some stuff
    while printerLooper == True :
        print("Sleeping for ", secondsPause, " seconds")
        print(random_number)
        printerLooper = False
    end_time = time.time()

    print("total time taken this loop: ", end_time - start_time)

rawDescription: Time a while loop python

description: Time a while loop python


code:
l=[{'Name':i, 'Age':j} for i,j in dic.items()]
dict((i.values()[::-1] for i in l))

rawDescription: A dict and a list of dicts: conversion and when to use which


rawDescription: What is the most pythonic way to sort dates sequences?


code:
def M(n, k):
    if n == 1: return [1]*k
    m = M(n-1, k)
    return [sum(m[:i]) for i in xrange(k)][::-1]

def C(n, k):
    if n < 1: return 0
    if n == 1: return k
    return 2*sum(M(n,k))

rawDescription: counting zigzag sequences

description: Counting zigzag sequences


rawDescription: Remove matched tags in html files?

description: Remove matched tags in html files


code:
import os
s = "E:/John/2012/practice/question11"
os.path.split(s)

rawDescription: Python Split path recursively

description: Python Split path recursively


code:
for lis in (a_list, b_list, c_list):
    a, b, c = lis  #assign the items of list fetched from the `tuple` to a, b ,c

rawDescription: python multiple 'for' statement in one row

description: Python multiple 'for' statement in one row


code:
def __repr__(self):
    return 'threed' + repr(self.lwh)

rawDescription: Naming coordinates in python

description: Naming coordinates in python


rawDescription: Converting a List within a List from String to Integers


code:
import csv

with open('somefile.txt', 'r') as f:
   reader = csv.reader(f, delimiter=' ')
   for row in reader:
       print(row[3])

rawDescription: get the content of what is in between the second set of quotes

description: Get the content of what is in between the second set of quotes


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

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

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

timevalue = 'datetime.datetime' + QuotedString('(', endQuoteChar=')', unquoteResults=False)
replUTC = lambda s,l,t: ''.join(t).replace("< UTC>","UTC").replace("<UTC >","UTC").replace("<UTC>","UTC")
timevalue.setParseAction(replUTC, evaltokens)

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

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

COMMA = Optional(_(","))
LT,GT,LPAR,RPAR,LBRACK,RBRACK = map(Suppress,"<>()[]")

#~ valuedexpr = lambda expr: (Word(alphas) + "(" + "value" + "=" + expr + ")").setParseAction(lambda t: t[4])
valuedexpr = lambda expr: ungroup(LT + (Word(alphas) + "(" + "value" + "=" + expr("value") + ")" + GT).setParseAction(lambda t: t.value))

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

lineexpr = (LT + "Aggregate" + LPAR + "aggregate_dimension_value_list" + "=" + LBRACK +
            valuedexpr(timevalue)("timestamp") + COMMA +
            (nonevalue | valuedexpr(strvalue))("s1") + COMMA +
            (nonevalue | valuedexpr(strvalue))("s2") +
        RBRACK + COMMA +
        "quantity=" + intvalue("qty") + RPAR + GT)

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

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


rawDescription: Mark checkboxes in rendered HTML

description: Mark checkboxes in rendered HTML


code:
from django.conf import settings
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'my_django_project.settings'

a_dict = {}

for attr in dir(settings):
    value = getattr(settings, attr)
    a_dict[attr] = value

for key, value in a_dict.items():
            print('%s = %r' % (key, value))

rawDescription: List quickly all settings of your Django project


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

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


code:
#!/usr/bin/env python2.7
import sys
from lib2to3.main import main

sys.path.append('path/to/my_own_package')
sys.exit(main('my_own_package.contained_fixers'))

rawDescription: How to launch own 2to3 fixer?

description: Launch own 2to3 fixer


code:
merged = merge(string1.lower(), string2.lower())
''.join(merged)

rawDescription: How to find union of two strings and maintain the order

description: Find union of two strings and maintain the order


code:
from __future__ import print_function, division

import mdtraj as md
import numpy as np
import numba as nb

@nb.njit
def abs_contact_order(xyz, atoms_residue, cutoff_nm=.6):
    """Return the absolute contact order."""
    contact_count = 0
    seq_distance_sum = 0
    cutoff_2 = cutoff_nm*cutoff_nm
    N = len(atoms_residue)
    for i in xrange(N):
        for j in xrange(N):
            seq_dist = atoms_residue[j] - atoms_residue[i]
            if seq_dist > 0:
                d = xyz[j] - xyz[i]
                if np.dot(d, d) < cutoff_2:
                    seq_distance_sum += seq_dist
                    contact_count += 1

    if contact_count==0.:
        return 0.

    return seq_distance_sum/float(contact_count)

@nb.njit
def abs_contact_order2(xyz, atoms_residue, cutoff_nm=.6):
    """Return the absolute contact order."""
    contact_count = 0
    seq_distance_sum = 0
    cutoff_2 = cutoff_nm*cutoff_nm
    N = len(atoms_residue)
    for i in xrange(N):
        for j in xrange(N):
            seq_dist = atoms_residue[j] - atoms_residue[i]
            if seq_dist > 0:
                d = 0.0
                for k in xrange(3):
                    d += (xyz[j,k] - xyz[i,k])**2
                if d < cutoff_2:
                    seq_distance_sum += seq_dist
                    contact_count += 1

    if contact_count==0.:
        return 0.

    return seq_distance_sum/float(contact_count)

rawDescription: Calculate protein contact order in Python


code:
pd.isnull(df2.iloc[-1])
to_shift = pd.isnull(df2.iloc[-1])

rawDescription: For any rows with a NaN in a Pandas DataFrame, shift by 1


code:
settings = {}
execfile('the_config.py', settings )
settings['CONFIG'] == "some string"

rawDescription: Using Python as the config language for a Python program


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

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


code:
class ValidationDisabledEntry(ttk.Entry):
    def __init__(self, *args, **kwargs):
        super().__init__(
            *args,
            validate='focus',
            validatecommand=lambda *a: 'invalid' not in self.state(),
            **kwargs)

rawDescription: How can I ensure my ttk.Entry's invalid state isn't cleared when it loses focus?

description: Ensure my ttk.Entry's invalid state isn't cleared when it loses focus


code:
total_count, dir_count = fcount(path)
print('Total:', total_count)
print('Directories:', dir_count)

rawDescription: Using the recursive code, I want to return a set of 2 values (total files, folders)


rawDescription: Python script counting lines in multiple text files in one directory and producing simple report


rawDescription: Get global mouse position and use in python program?


code:
mylist = ['first', 'second', 'other']
s = ""
for item in mylist:
   s += item
s

rawDescription: Python - How to concatenate to a string in a for loop?


code:
# Examples:
    # url(r'^$', 'Toolkit.views.home', name='home'), #this will match url of the type localhost:8000
    # url(r'^blog/', include('blog.urls')), # this will match url of the type localhost:8000/blog/

rawDescription: how to set the index page in django like http://127.0.0.1:8000/

description: Set the index page in django like http://127.0.0.1:8000/


code:
with open("output.csv", "wb") as f:
    writer = csv.DictWriter(f, fieldnames=list(events[0]), delimiter = ';')
    writer.writeheader()
    for event in events:
        outevent = event.copy()
        outevent["random_nums"] = ", ".join(map(str, outevent["random_nums"]))
        outevent["guests"] = ", ".join(outevent["guests"])
        writer.writerow(outevent)

rawDescription: Write a list of common dictionaries to CSV with common key values in the same column


code:
class SomeModel(db.Expando):
    pass

your_model = SomeModel()
for k, v in your_dict.iteritems():
    setattr(your_model, k, v)

rawDescription: How to make persistent a python dictionary on google appengine

description: Make persistent a python dictionary on google appengine


code:
for line in fileinput.readlines():
    a, b, c = line.split('\t')      # Variable unpacking; assumes each line has three columns
    if a == '?':
        function_a()
    if b == '?':
        function_b()
    if c == '?':
        function_c()

rawDescription: Parsing through a file

description: Parsing through a file


code:
a = x;
b = y;

rawDescription: Running statements in 'parallel'

description: Running statements in 'parallel'


code:
def f(values, x):
    seen = set()
    add = seen.add
    return [res for res in (i % x for i in values) if res not in seen and not add(res)]

rawDescription: Most idiomatic way of applying modulo to list in python


rawDescription: Python - How to launch a subprocess before exiting the program (which is running on a detached screen)?


rawDescription: numpy equivalent of matlab dummyvar

description: Numpy equivalent of matlab dummyvar


code: cell_list.append({'num':1, 'state':0})

rawDescription: problem with a 2 dimensional array(list of list of dict)

description: Problem with a 2 dimensional array(list of list of dict)


code:
import string

inputs = [dict([(s, i) for i, s in enumerate(string.letters[:14])])] * 12

def offline_optimize(*args, **kwargs):
    return [("args", args),
            ("args_count", len(args)),
            ("kwargs", kwargs),
            ("kwargs_count", len(kwargs))]

rawDescription: Apply a function to a list of dictionaries in python


code:
import subprocess
subprocess.call(["python", "userscript.py"])

rawDescription: Open the terminal in python and run a python script


code: stock.sort(key=lambda x: (int(x['date']), float(x['amount'])))

rawDescription: Numeric sort of list of dictionary objects


code:
class AutoVivification(dict):
    """Implementation of perl's autovivification feature."""
    def __getitem__(self, item):
        try:
            return dict.__getitem__(self, item)
        except KeyError:
            value = self[item] = type(self)()
            return value

rawDescription: What's the best way to initialize a dict of dicts in Python?


code:
def before_all(context):
    context.browser = webdriver.Firefox()
    context.url = context.config.url

rawDescription: passing command line argument to python-behave

description: Passing command line argument to python-behave


code:
from django.db import models

class ArticleBase(models.Model):
    title = models.CharField()
    author = models.CharField()
    # Store the actual class name.
    class_name = models.CharField()

    # Define save to make sure class_name is set.
    def save(self, *args, **kwargs):
        self.class_name = self.__class__.__name__
        super(ArticleBase, self).save(*args, **kwargs)

    # Multi-table inheritance defines an attribute to fetch the child
    # from a parent instance given the lower case subclass name.
    def get_child(self):
        return getattr(self, self.class_name.lower())

    # If indeed you really need the class.
    def get_child_class(self):
        return self.get_child().__class__

    # Check the type against a subclass name or a subclass.
    # For instance, 'if article.child_is(News):'
    # or 'if article.child_is("News"):'.
    def child_is(self, cls):
        if isinstance(cls, basestring):
            return cls.lower() == self.class_name.lower()
        else:
            return self.get_child_class()  == cls

class Review(ArticleBase):
    rating = models.IntegerField()

class News(ArticleBase):
    source = models.CharField()

rawDescription: Can you access a subbed-classed model from within the super-class model in the Django ORM?

description: Access a subbed-classed model from within the super-class model in the Django ORM


code:
class A:
    def __init__(self, a):
        self.attr1 = a

    def __hash__(self):
        return hash(self.attr1)

    def __eq__(self, other):
        return self.attr1 == other.attr1

    def __repr__(self):
        return str(self.attr1)

rawDescription: efficient way to remove duplicates from list of custom objects in python

description: Efficient way to remove duplicates from list of custom objects in python


rawDescription: Format string - spaces between every three digit


code: totals = [sum(probabilities[:i+1]) for i in range(len(probabilities))]

rawDescription: Generating weighted random numbers

description: Generating weighted random numbers


code:
tmp = pickle.dumps(dict1) # no exception
new = pickle.loads(tmp)

rawDescription: Can't pickle defaultdict

description: Can't pickle defaultdict


code:
import Tkinter as tk

def cinterp(x1,x2,x,c1,c2):
    s1=float(x-x1)/(x2-x1)
    s2=1.-s1
    return [int(s1*i+s2*j) for i,j in zip(c1,c2)]

root=tk.Tk()
cvs=tk.Label(root,text="Hello")
c2=(255,0,0)
c1=(255,255,255)
def updateCVS(dt,timeleft,deltat):
    timeleft=timeleft-dt
    fill=cinterp(0,deltat,deltat-timeleft,c1,c2)
    fs="#%02x%02x%02x"%(fill[0],fill[1],fill[2])
    cvs.configure(bg=fs)
    if(timeleft>0):
        timeleft=timeleft-dt
        cvs.after(int(dt*1000),updateCVS,dt,timeleft,deltat)

cvs.grid(row=0,column=0)
b=tk.Button(root,text="push me",command=lambda : updateCVS(.2,5,5))
b.grid(row=1,column=0)

root.mainloop()

rawDescription: Crossfading between label colors over time in tkinter?


code:
def choose(first, second, value):
    if hasattr(first, '__iter__'):
        return [choose(i,j,k) for i,j,k in zip(first, second, value)]
    else:
        return first if value else second

rawDescription: pick and choose from two nested lists by a pattern using Python

description: Pick and choose from two nested lists by a pattern using Python


code:
for d in data:
    if(d['MAG'] > 0):
        ar = patches.FancyArrow(d['X0'],d['Y0'],d['X1']-d['X0'], d['Y1']-d['Y0'], width=d['MAG']/4.,
         head_width=3., head_length=1.54, length_includes_head=True, zorder = 0)
        plt.gca().add_patch(ar)

rawDescription: Arrows on Scatter

description: Arrows on Scatter


rawDescription: Template Python list with default element values


code: [sum(r) for r in it.izip_longest(it.islice(l,0,None,2), it.islice(l,1,None,2), fillvalue=0)]

rawDescription: adding two consecutive numbers in a list

description: Adding two consecutive numbers in a list


code:
for code, name in zip(df['Codes'], df['Company Name']):
    get_prices = Quandl.get(code, returns='pandas')
    # Data to SQL
    get_prices.to_sql(name=name, con=engine)

rawDescription: How to structure a For loop with pandas dataframe

description: Structure a For loop with pandas dataframe


rawDescription: Calculate permutations

description: Calculate permutations


code:
import urlparse, re
from scrapy import Spider, log
from bs4 import BeautifulSoup
from scrapy.http import Request
from govcrawl.items import DomainItem

class DomainSimpleSpider(Spider):
    name = "govcrawl_simple"

    def parse(self, response):
        pages_done = self.crawler.stats.get_value('downloader/response_count')
        pages_todo = self.crawler.stats.get_value('scheduler/enqueued') - self.crawler.stats.get_value('downloader/response_count')
        log.msg("URL: %s (%s) Crawled %d pages. To Crawl: %d" % (self.start_urls[0], self.url_id, pages_done, pages_todo), spider = self)
        #import ipdb
        #ipdb.set_trace()
        soup = BeautifulSoup(response._body, "html5lib")
        links = []
        for tag in self.tags:
           for a in soup.find_all(tag):
              for attr in self.attrs:
                 if attr in a.attrs:
                    href = a.attrs[attr]
                    if href.startswith("http"):
                       links.append(href)
                    href = urlparse.urljoin(response.url, href)
                    href_parts = urlparse.urlparse(href.replace('\t', '').replace('\r', '').replace('\n', '').replace(' ', '+'))
                    if re.match(self.allow, href_parts.path) and not self.forbidden_extension(href_parts.path):
                       yield Request(href)
        for script in soup(["script", "style"]):
           script.extract()
        item = DomainItem()
        item["url"] = response.url
        #item["text"] = re.sub(r'\s{2,}', ' ', remove_tags(' '.join(response.xpath('//body//text()').extract()))).strip()
        item["text"] = soup.get_text()
        item["links"] = links
        self.crawler.stats.inc_value('pages_crawled')
        yield item

    def forbidden_extension(self, url):
       url = url.lower()
       return url.endswith("pdf") or url.endswith("jpg") or url.endswith("wmv") or url.endswith("avi") or url.endswith("pptx") or url.endswith("gif") or url.endswith("mp3") or url.endswith("mp4") or url.endswith("wav") or url.endswith("mov") or url.endswith("ppt") or url.endswith("xls") or url.endswith("doc") or url.endswith("docx") or url.endswith("xlsx") or url.endswith("flv") or url.endswith("wma") or url.endswith("jpeg") or url.endswith("png") or url.endswith("odf") or url.endswith("ods") or url.endswith("zip") or url.endswith("gz") or url.endswith("tar") or url.endswith("7z") or url.endswith("rar") or url.endswith("vob")

rawDescription: CrawlSpider fails to follow rule for some websites


code: n = next(records)

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

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


code: user = self.request.get('username')

rawDescription: GAE - Issue with GET

description: GAE - Issue with GET


code:
x * x             # n = 2

x * (x * x)       # n = 3
                  # here, we initially calculate x * x,
                  # which we pass as y in the next stack for x * y

rawDescription: Recursive function to calculate matrix multiplication


code:
user_input = raw_input('Please enter your search item: ')
elements = {'2': 'Hydrogen', '8': 'Oxygen'}
result = search_dictionary(user_input, elements)
if result:
    print("The result of your search is {0}".format(result))
else:
    print("Your search for {0} returned no results".format(user_input))

rawDescription: How to access part of a dictionary in python?

description: Access part of a dictionary in python


code:
new_seen = [item for item in seen
            if sum(1 for k in maxx if item==k.f) >= 3]

rawDescription: PYTHON - 'for' index changing because of list.pop() call


rawDescription: Concatenating column vectors using numpy arrays


code:
def get_cells(size):
    result = []
    for x_axis in range(size):
        for y_axis in range(size):
            result.append((x_axis, y_axis))
    return result

rawDescription: Python: returning the cell coordinates

description: Python: returning the cell coordinates


code:
from tkinter import *

root = Tk()
var = StringVar()
var.set('hello')

l = Label(root, textvariable = var)
l.pack()

t = Entry(root, textvariable = var)
t.pack()

root.mainloop() # the window is now displayed

rawDescription: Update Tkinter Label from variable

description: Update Tkinter Label from variable


code:
def gen_oriented_cycles(xs):
    from itertools import combinations, permutations
    for length in range(2, len(xs) + 1):
        for pieces in combinations(xs, length):
            first = pieces[0],  # 1-tuple
            for rest in permutations(pieces[1:]):
                yield first + rest

rawDescription: Most efficient way to list all oriented cycles given n elements in Python


code:
from matplotlib.patches import Rectangle
ax = g.ax_heatmap

ax.add_patch(Rectangle((3, 4), 1, 1, fill=False, edgecolor='blue', lw=3))
plt.show()

rawDescription: Add custom border to certain cells in a matplotlib / seaborn plot


code:
[music21.interval.Interval(music21.note.Note(s1[i]),\
                           music21.note.Note(s1[i + 1])).name\
 for i in range(len(s1)-1)]

rawDescription: Make a pandas series by running a function on all adjacent values


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


code:
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get('http://stackoverflow.com/questions/27813251')
driver.title

rawDescription: Web scraping: Automating button click

description: Web scraping: Automating button click


code:
@staticmethod
def deep_list(x):
    """fully copies trees of tuples to a tree of lists.
       deep_list( (1,2,(3,4)) ) returns [1,2,[3,4]]"""
    if type(x)!=type( () ):
        return x
    return map(deep_list,x)

rawDescription: Python calling map() with 'self'

description: Python calling map() with 'self'


code:
@as_logname("things")
def do_things():
    logging.info("Starting to do things")
    do_more_stuff()

rawDescription: How can you make logging module functions use a different logger?

description: Make logging module functions use a different logger


code: 'J O I N   : # S t a f f'[::2]

rawDescription: Python remove separating spaces

description: Python remove separating spaces


code: map(formatter, data)

rawDescription: add leading zeros to a list of numbers in Python

description: Add leading zeros to a list of numbers in Python


code:
original_time # datetime.datetime(2015, 5, 11, 12, 32, 46, 246004)
original_time.hour # 12
original_time.year # 2015
original_time.month # 5
original_time.day # 11
original_time.hour # 12
original_time.minute # 32
original_time.second # 46

rawDescription: getting the hour from current time

description: Getting the hour from current time


rawDescription: How to map func_closure entries to variable names?

description: Map func_closure entries to variable names


code:
cv2.waitKey(33)
if k==27:    # Esc key to stop
    break

rawDescription: How do can I use OpenCV imshow to display the most recent image in a loop without keyboard feedback


code:
from twisted.internet import reactor
from twisted.internet.protocol import Factory
port = reactor.listenTCP(0, Factory())
port.getHost().port

rawDescription: twisted - get OS-chosen listen port

description: Twisted - get OS-chosen listen port


rawDescription: Append a dict in a nested dict python

description: Append a dict in a nested dict python


rawDescription: Plot two vectors for elements which exist - i.e. skip rows without entries


code:
s = "$word1$, $word2$, $word3$, $word4$"
authors = re.findall(r"\$(\w+)\$", s)
authors

rawDescription: python match only captures first and last group - am I misunderstanding something?

description: Python match only captures first and last group - am I misunderstanding something


code: p + coord_polar()

rawDescription: How to make a sunburst plot in R or Python?

description: Make a sunburst plot in R or Python


code:
def on_pushbutton_clicked(self):
    if not self.dlg:
        self.dlg = DialogWindow(self)
        self.dlg.mySignal.connect(self.on_mySignal)
        self.dlg.show()

def on_mySignal(value):
    self.dlg.mySignal.disconnect()
    self.dlg.close()
    self.dlg.deleteLater()
    self.dlg = None
    self.updateLabelAnswer(value)

rawDescription: Communicate between two windows without violating class encapsulation


rawDescription: write matrix to image by PIL

description: Write matrix to image by PIL


code:
def lru_cache_class(maxsize):
    def wrap(klass):
        @lru_cache(maxsize=maxsize)
        def new(cls, *args, **kwargs):
            self = object.__new__(cls)
            return self
        klass.__new__ = new
        return klass
    return wrap

rawDescription: Unique representations of Python objects


code:
from functools import partial
set2 = filter(partial(filtCheck, filt="Hi"), set1)

rawDescription: Passing a parameter through a python filter function


code:
from struct import *
a='\x00\x00\x00\x01'*9
unpack('>ddddi', a)
unpack('<ddddi', a)
unpack('!ddddi', a)

rawDescription: Python buffer decode

description: Python buffer decode


rawDescription: How to check if a dictionary is in another dictionary in python

description: Check if a dictionary is in another dictionary in python


code:
cursor.execute("INSERT INTO vendor_id VALUES (%s, %s, %s)", (id, provider_id, title))
cursor.execute("INSERT INTO vendor_id VALUES (?, ?, ?)", (id, provider_id, title))

rawDescription: Add SQL variables in through MySQLdb

description: Add SQL variables in through MySQLdb


code: c = {k: a[k]/v for k,v in b.iteritems()}

rawDescription: pythonic way to match dictionaries

description: Pythonic way to match dictionaries


code: np.kron(a, np.ones((blockSize, blockSize)))[:rows, :cols]

rawDescription: Fill larger matrix from smaller matrix

description: Fill larger matrix from smaller matrix


rawDescription: python: converting list into string, adding ',' only when '|' is not around

description: Python: converting list into string, adding ',' only when '|' is not around


code: ' '.join(''.join(map(str, sublist)) for sublist in my_list)

rawDescription: Printing a list of list without brackets in python


code:
from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.set_preference('network.http.phishy-userpass-length', 255)
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("https://appuser:apppass@somewebsite.com/")

rawDescription: Login, Navigate and Retrieve data behind a proxy with Python


rawDescription: Python: three different delimiters between columns, column to list, quickest way


code:
from tkinter import *

class MyButton(Label):

    def __init__(self, *args, **kwargs):
        Label.__init__(self, *args, **kwargs)
        self.bind("<Enter>", self.changeBGEnter)
        self.bind("<Leave>", self.changeBGLeave)

    def changeBGEnter(self, event):
        self.config(background="gray")

    def changeBGLeave(self, event):
        self.config(background="black")

FONT_SIZE = 14  # Change this to change all font sizes in the program

root = Tk()

root.resizable(width=TRUE, height=TRUE)
root.geometry("800x600")
root.config(background="black")

# Labels
newGameButton = MyButton(root)
loadGameButton = MyButton(root)
optionsButton = MyButton(root)
exitButton = MyButton(root)
Labels = [newGameButton, loadGameButton, optionsButton, exitButton]

# New Game Button
newGameButton.config(
    anchor=CENTER,  # Where to anchor the text in the widget
    background="black",
    foreground="white",
    font=("default", FONT_SIZE),
    text="NewGame",
)

newGameButton.place(
    relwidth=0.6,
    relheight=0.1,
    relx=0.2,
    rely=0.3
)

# Load Game Button
loadGameButton.config(
    anchor=CENTER,
    background="black",
    foreground="white",
    font=("default", FONT_SIZE),
    text="Stats",
)

loadGameButton.place(
    relwidth=0.6,
    relheight=0.1,
    relx=0.2,
    rely=0.4
)

# Options Button
optionsButton.config(
    anchor=CENTER,
    background="black",
    foreground="white",
    font=("default", FONT_SIZE),
    text="Output",
)

optionsButton.place(
    relwidth=0.6,
    relheight=0.1,
    relx=0.2,
    rely=0.5
)

# Exit Button
exitButton.config(
    anchor=CENTER,
    background="black",
    foreground="white",
    font=("default", FONT_SIZE),
    text="Exit",
)

exitButton.place(
    relwidth=0.6,
    relheight=0.1,
    relx=0.2,
    rely=0.6
)

root.mainloop()

rawDescription: Python Tkinter loop to change button color on Enter and on Leave (python 3.4, Windows 10)


rawDescription: Finding BitVec weight efficently (aka access to bits of a bitvector) in z3


code:
import pandas as pd

# remember the length of each 'NYC' ... 'CIN' has to be the same as you said, 162
WinLoss = {'NYY': [[u'L', u'2-6'], [u'L', u'1-3'], [u'W', u'2-1']],
'MIN': [[u'L', u'3-5'], [u'L', u'6-7'], [u'W', u'10-9']],
'CIN': [[u'L', u'0-1'], [u'W', u'1-0'], [u'L', u'6-7']]}

# construct an empty DataFrame here
df = pd.DataFrame()

# just loop through the dictionary you have, and write into DataFrame
# another update to shorten the syntax
df = pd.DataFrame()
for k,v in WinLoss.items():
    # name the columns to whatever you want
    df['{} {}'.format(k, 'Win/Loss')] = [r[0] for r in v]
    df['{} {}'.format(k, 'Scores')] = [r[1] for r in v]

rawDescription: Split items in a series

description: Split items in a series


code:
def __init__(self, starting=0, birthrate=100, imrate=10, emrate=10, deathrate=100):
     self.update(n=starting, b=birthrate, i=imrate, e=emrate, d=deathrate)

rawDescription: Object vs. Dictionary: how to organise a data tree?


code: QtGui.QToolTip.showText(QtGui.QCursor.pos(),"Your long format text...",None)

rawDescription: How to make custom overlay Text window (similar to Tool Tip )

description: Make custom overlay Text window (similar to Tool Tip )


rawDescription: Why my regex replaces match pattern with function object rather than the replacement value?


code:
import maya.cmds as cmds
curSel = cmds.ls(sl=True)

rawDescription: Maya + Python array collecting

description: Maya + Python array collecting


code: pd.DataFrame( np.where( np.abs(df) > df.mean(), 2, df ), columns=df.columns )

rawDescription: Transforming outliers in Pandas DataFrame using .apply, .applymap, .groupby


code:
# First time
with open('current.csv', 'r') as f:
    data = f.readlines()
    last_pos = f.tell()

# Second time
with open('current.csv', 'r') as f:
    f.seek(last_pos)
    new_data = f.readlines()
    last_pos = f.tell()

rawDescription: Continuously parse CSV files which are updated by another process


code:
A = np.arange(5) + 0.5
C = np.ones_like(B)  # They are all ones, but could be anything
A += np.bincount(B, weights=C)
A

rawDescription: indexing/slicing in numpy array to modify it

description: Indexing/slicing in numpy array to modify it


rawDescription: Python date format?

description: Python date format


code:
@pytest.mark.tryfirst
def pytest_runtest_makereport(item, call, __multicall__):
    report = __multicall__.execute()
    # then I was able to manipulate report and get the same results as below

rawDescription: Customizing pytest junitxml failure reports


code:
self._sslobj = _ssl2.sslwrap(self._sock, server_side,
                                     keyfile, certfile,
                                     cert_reqs, ssl_version, ca_certs)

rawDescription: How to open ssl socket using certificate stored in string variables in python

description: Open ssl socket using certificate stored in string variables in python


code:
import enum
class EnumWithAttrs(enum.Enum):

    def __new__(cls, *args, **kwds):
        value = len(cls.__members__) + 1
        obj = object.__new__(cls)
        obj._value_ = value
        return obj

    def __init__(self, a, b):
        self.a = a
        self.b = b

    GREEN = 'a', 'b'
    BLUE = 'c', 'd'

rawDescription: python enums with attributes

description: Python enums with attributes


code:
import multiprocessing as mp
import copy_reg
import types

def _pickle_method(method):
    # Author: Steven Bethard
    # http://bytes.com/topic/python/answers/552476-why-cant-you-pickle-instancemethods
    func_name = method.im_func.__name__
    obj = method.im_self
    cls = method.im_class
    cls_name = ''
    if func_name.startswith('__') and not func_name.endswith('__'):
        cls_name = cls.__name__.lstrip('_')
    if cls_name:
        func_name = '_' + cls_name + func_name
    return _unpickle_method, (func_name, obj, cls)

def _unpickle_method(func_name, obj, cls):
    # Author: Steven Bethard
    # http://bytes.com/topic/python/answers/552476-why-cant-you-pickle-instancemethods
    for cls in cls.mro():
        try:
            func = cls.__dict__[func_name]
        except KeyError:
            pass
        else:
            break
    return func.__get__(obj, cls)

# This call to copy_reg.pickle allows you to pass methods as the first arg to
# mp.Pool methods. If you comment out this line, `pool.map(self.foo, ...)` results in
# PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup
# __builtin__.instancemethod failed

copy_reg.pickle(types.MethodType, _pickle_method, _unpickle_method)

class MyClass(object):

    def __init__(self):
        self.my_args = [1,2,3,4]
        self.output  = {}

    def my_single_function(self, arg):
        return arg**2

    def my_parallelized_function(self):
        # Use map or map_async to map my_single_function onto the
        # list of self.my_args, and append the return values into
        # self.output, using each arg in my_args as the key.

        # The result should make self.output become
        # {1:1, 2:4, 3:9, 4:16}
        self.output = dict(zip(self.my_args,
                               pool.map(self.my_single_function, self.my_args)))

rawDescription: Python: Efficient workaround for multiprocessing a function that is a data member of a class, from within that class


code:
[{item.tag: item.text for item in ch} for ch in tree.findall('file')]
[ {'Bitrate': '131',
   'Name': 'some filename.mp3',
   'Encoder': 'Gogo (after 3.0)'},
  {'Bitrate': '128',
   'Name': 'another filename.mp3',
   'Encoder': 'iTunes'}]

rawDescription: Parse XML file into Python object

description: Parse XML file into Python object


rawDescription: looping back to specific point in code

description: Looping back to specific point in code


rawDescription: selecting rows in numpy ndarray based on the value of two columns

description: Selecting rows in numpy ndarray based on the value of two columns


rawDescription: Labels for scatterplot-matrices

description: Labels for scatterplot-matrices


code: update_text("Hello, world")

rawDescription: How to make a text button which speeds up text animation

description: Make a text button which speeds up text animation


rawDescription: Pandas - finding the row with least value of one of the levels of a multiindex


code:
import numpy as np

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

a = np.array([0,1,2,3])
b = np.array([1,2,3,4])

In [430]: f(a,b)
Out[430]: array([1, 3, 5, 7])

rawDescription: Vectorize a 2-dimensional function in python


code:
class Post(models.Model):
       title= models.CharField(max_length=150)
       body = models.TextField()
       created = models.DateField()

rawDescription: How do you use django-datatable-view

description: Use django-datatable-view


code:
document = "hello there my name is dominic and my name is very special"
searchTerms = { 'my', 'dominic' } # set has faster lookups

doc_words = document.split()

from sys import maxint
def search(words, terms):
    found_terms = [[i,x] for i,x in enumerate(words) if x in terms]
    lookup = {}
    cnt = maxint
    k = len(terms)
    start,end=-1,-1
    for i,w in found_terms:
        lookup[w] = i
        if k == len(lookup):
            min_idx = min(lookup.values())
            curr = i - min_idx
            if curr < cnt:
                cnt,start,end = curr,min_idx,i
    return words[start:end + 1]

rawDescription: Implementing Basic Efficient "Search" Algorithm: Python


code:
from Pyside.QtGui import QDialog

class Window(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.menu_bar()

        self.help_open = False # Tracks if the help dialog is already open

    def help_closed(self):
        self.help_open = False

    ...

    def add_helpWindow(self):
        if not self.help_open:
            self.help_open = True
            help_dialog = QDialog(self)
            # Any other setup code here
            help_dialog.setModal(False)
            help_dialog.accepted.connect(self.help_closed)
            help_dialog.rejected.connect(self.help_closed)
            help_dialog.show()

rawDescription: Query regarding Pyside

description: Query regarding Pyside


code:
# Already mounted ?
if g_file.query_exists():
    self._folder = g_file
else:
    mount_operation = MountOperation()
    mount_operation.set_anonymous(True)
    g_file.mount_enclosing_volume(mount_operation, self._mount_end)

rawDescription: gio: check if volume is mounted

description: Gio: check if volume is mounted


code:
import pandas as pd
import io

temp=u"""213.87.137.33 - - [14/Apr/2016:17:23:36],"CONNECT api-glb-ams.smoot.apple.com:443",200 0,"SafariShared/601.1.46.42 (iPhone4,1; iPhone OS 13C75) Safari/601.1",9443 api-glb-ams.smoot.apple.com 443 1856
213.87.137.33 - - [14/Apr/2016:17:23:36],"CONNECT init.itunes.apple.com:443",200 0,"MobileSafari/601.1 CFNetwork/758.2.8 Darwin/15.0.0",9443 init.itunes.apple.com 443 50073
213.87.137.33 - - [14/Apr/2016:17:23:54],"GET http://www.rbc.ru/ajax/getnewsfeed/?",304 292,"Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13C75 Safari/601.1",9443 www.rbc.ru 80 9547
213.87.137.33 - - [14/Apr/2016:17:23:56],"GET http://www.rbc.ru/ajax/mainjson/?",200 99535,"Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13C75 Safari/601.1",9443 www.rbc.ru 80 0
213.87.137.33 - - [14/Apr/2016:17:23:58],"CONNECT api-glb-ams.smoot.apple.com:443",200 0,"SafariShared/601.1.46.42 (iPhone4,1; iPhone OS 13C75) Safari/601.1",9443 api-glb-ams.smoot.apple.com 443 40633"""

rawDescription: Use columns from table using pandas

description: Use columns from table using pandas


code:
import subprocess
import sys

file = sys.argv[1]
p1 = subprocess.Popen(['hg', 'cat', file], stdout=subprocess.PIPE)
p2 = subprocess.Popen([
    'gvimdiff',
    '/proc/self/fd/%s' % p1.stdout.fileno(),
    file])
p2.wait()

rawDescription: Redirect command to input of another in Python


code:
from Tkinter import *
from PIL import ImageGrab
import cStringIO, base64

window = Tk()

box = (100,100,500,500)
MyImage = ImageGrab.grab(box)

fp = cStringIO.StringIO()
MyImage.save(fp,'GIF')

MyPhotoImage = PhotoImage(data=base64.encodestring(fp.getvalue()))
PictureLabel = Label(image=MyPhotoImage)
PictureLabel.pack()
PictureLabel.image = MyPhotoImage

window.mainloop()

rawDescription: Tkinter using a non-saved picture as an image


code:
with open('test.csv', 'wb') as f:
    writer = csv.writer(f)
    for row in rows:
        writer.writerow(row)

rawDescription: Python - Outputting two data sets (lists?) to data file as two columns


code:
import collections

g = iter(range(2))
my_input = [1, [2, 3], ((4), 5), [6, g]]

def unfold(iterable):
    ret = []
    for element in iterable:
        if isinstance(element, collections.Iterable):
            ret.append(unfold(element))
        else:
            ret.append(element)
    return ret

n = unfold(my_input)
print(n)
print(n)

rawDescription: Convert nested iterables to list

description: Convert nested iterables to list


code:
import string
from itertools import product
#http://www.puzzlers.org/pub/wordlists/engwords.txt

with open('/Users/kyle/Documents/english words.txt', 'r') as word_file:
    english_words = set(tuple(word.strip().lower()) for word in word_file if len(word.strip()) == 4)

words = english_words.intersection(product(string.ascii_lowercase, repeat=4))

rawDescription: words being left out

description: Words being left out


code: from myfolder import *

rawDescription: Python project architecture

description: Python project architecture


rawDescription: How to convert a unicode character representation from string to unicode in python?

description: Convert a unicode character representation from string to unicode in python


rawDescription: python get all ids startingwith a pattern

description: Python get all ids startingwith a pattern


code:
import pandas as pd
import numpy as np

t= [1,1,1,2,0,1,1,0,0,2,1,1,2,0,0,0,0,1,1,1]
df = pd.DataFrame(np.random.randn(20, 2), columns=list('AC'))
df['B']=t  #put a random column with only 0,1,2 values, then insert it to the dataframe

rawDescription: Conditional probability density estimation in python


code:
class MyClass(object):
    def __init__(self):
            self.a = 2
            self.b = 3

obj = MyClass()
obj.__dict__
eval("a + b", obj.__dict__)

rawDescription: how to create local dynamic variables

description: Create local dynamic variables


code: df

rawDescription: Pandas: decompress date range to individual dates


rawDescription: why reversed() list iteration with step doesn't work properly?

description: Why reversed() list iteration with step doesn't work properly


code:
"""
one line summary

longer explanation

Parameters
----------
a : int
    parameter description

Returns
-------
servers : list
    servers to use
msg : str
    logging message string
"""

rawDescription: Can sphinx napoleon document function returning multiple arguments?


code: ax.scatter(coordData[:, 0], coordData[:, 1], c=coordLabels, marker="o")

rawDescription: How to change marker and color in matplotlib for a specific column value?

description: Change marker and color in matplotlib for a specific column value


code: Word(nums+'_', excludeChars='_')

rawDescription: python pyparsing word excludeChars

description: Python pyparsing word excludeChars


code:
for i in range(1,10):
    parser.add_option('-l', '--location', value="{0}".format(i))

rawDescription: python 2.7 loop's index in quotation marks

description: Python 2.7 loop's index in quotation marks


code:
while True:
    player_input = raw_input("Enter a character: ")
    if player_input in ('n', 'r', 'e'):
        break

rawDescription: Multiple 'or' on while loop

description: Multiple 'or' on while loop


code:
from functools import wraps
import inspect

def initializer(fun):
   names, varargs, keywords, defaults = inspect.getargspec(fun)
   @wraps(fun)
   def wrapper(self, *args, **kargs):
       for name, arg in zip(names[1:], args) + kargs.items():
           setattr(self, name, arg)
       fun(self, *args, **kargs)
   return wrapper

rawDescription: Python: Automatically initialize instance variables?


code:
x = low > high   # CompareList([False])
y = high > low   # CompareList([True])
x and y

rawDescription: Custom chained comparisons

description: Custom chained comparisons


rawDescription: numpy gradient function and numerical derivatives

description: Numpy gradient function and numerical derivatives


rawDescription: Django Tinymce file manager

description: Django Tinymce file manager


code:
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.w3schools.com/")

driver.execute_script("""
window.getPathTo = function (element) {
    if (element.id!=='')
        return 'id("'+element.id+'")';
    if (element===document.body)
        return element.tagName;

    var ix= 0;
    var siblings= element.parentNode.childNodes;
    for (var i= 0; i<siblings.length; i++) {
        var sibling= siblings[i];
        if (sibling===element)
            return window.getPathTo(element.parentNode)+'/'+element.tagName+'['+(ix+1)+']';
        if (sibling.nodeType===1 && sibling.tagName===element.tagName)
            ix++;
    }
}
""")

element = driver.find_element_by_class_name("w3-fluid")

generated_xpath = driver.execute_script("return window.getPathTo(arguments[0]);", element)
print(generated_xpath)

rawDescription: Selenium Python Finding actual xPath of current node within loop


code:
# helper class: returning full_name instead of username
class UserModelChoiceField(forms.ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.get_full_name()

class ProjectForm(forms.ModelForm):
    manager = UserModelChoiceField(queryset=User.objects.all(), label='Manager', required=True)

    class Meta:
        model = Project
        fields = ['name']

rawDescription: Replacing ManyToMany-Relation with ModelChoiceField


code:
from tornado import template
t = template.Template('''
<div id="statistics-table">
    {% for key, value in statistics.items() %}
    {{key}} : {{value['number']}}
    {% end %}
</div>
''')
statistics = { 1 : {'number' : 2},  2 : {'number' : 8}}
print(t.generate(statistics=statistics))

rawDescription: How to iterate through dictionary passed from Python/Tornado handler to Tornado's template?

description: Iterate through dictionary passed from Python/Tornado handler to Tornado's template


code:
import decimal
D = decimal.Decimal
q = D(10) ** -precision
print(D(somefloat).quantize(q))
# 0.01235

rawDescription: Limiting floats to a varying number (decided by the end-user) of decimal points in Python


code: cmds.ls(long=True, type='mesh')

rawDescription: Python (Maya) pass flags as variables

description: Python (Maya) pass flags as variables


code:
def logging_debug(what):
    ''' Function to support cp866 encoding in developers admin console
    '''
    if os.environ.get('SERVER_SOFTWARE','').startswith('Devel'):
        logging.debug(what.encode('cp866'))
    else:
        logging.debug(what)

rawDescription: How to log e-mail details in AppEngine Admin console?

description: Log e-mail details in AppEngine Admin console


rawDescription: Importing matlab structure to python

description: Importing matlab structure to python


code: keyarray

rawDescription: Replace values of a numpy array by values from another numpy array


code:
{ line['Name']: line for line in csv.DictReader(open('t.tsv')) }
rows_by_name = { line['Name']: line for line in csv.DictReader(open('t.tsv')) }
rows_by_name['Ann']

rawDescription: How to read a specific row of a csv file in python?

description: Read a specific row of a csv file in python


code:
s = ['6','5','7','7','6']
item_length = len(s)

with open('test.csv', 'wb') as test_file:
    file_writer = csv.writer(test_file)
    for item in s:
        file_writer.writerow(item)

rawDescription: Writing output in first column of csv

description: Writing output in first column of csv


code:
def simplify(tree, threshold):
    # simplify tree bottom-up
    for key, child in tree.items():
        if isinstance(child, dict):
            tree[key] = simplify(child, threshold)
    # all child-nodes are leafs and sum smaller than threshold -> return sum
    if all(isinstance(child, str) for child in tree.values()) \
           and sum(map(float, tree.values())) <= threshold:
        return str(sum(map(float, tree.values())))
    # else return tree itself
    return tree

rawDescription: Python Dictionary - Consolidating Leaf Nodes below a threshold


code: fn = lambda x: (x if True else lambda x: x*x)

rawDescription: anonymous function assignment using a one line if statement

description: Anonymous function assignment using a one line if statement


code: links = re.findall(r"(url=:'http://.+?=tag)", source)

rawDescription: python regular expression on first occurance

description: Python regular expression on first occurance


code:
A
B

rawDescription: Efficient pairwise correlation for two matrices of features


rawDescription: How do I compute the intersection point of two lines in Python?

description: Compute the intersection point of two lines in Python


code:
import pandas as pd

def select_blue_cars(df):
    """Returns a new dataframe of blue cars"""
    return df.loc[df['color'] == 'blue', :].copy()

cars = pd.DataFrame({'color': ['blue', 'blue', 'red'], 'make': ['Ford', 'BMW', 'Ford']})
blue_cars = select_blue_cars(cars)
blue_cars['price'] = 10000

rawDescription: Avoiding pandas chained selection

description: Avoiding pandas chained selection


rawDescription: twisted client transport.write not sending data, even after close

description: Twisted client transport.write not sending data, even after close


code:
output = ImageOps.fit(tmp, self.mask.size, centering=(0.5, 0.5))
output.putalpha(self.mask)
self.zimg = ImageTk.PhotoImage(output)

rawDescription: Create circular image PIL Tkinter

description: Create circular image PIL Tkinter


rawDescription: Reshape wide to long in PANDAS

description: Reshape wide to long in PANDAS


code:
#!/usr/bin/env python3

import re

def is_consecutive(n):
    x = n | (n - 1)
    return x & (x + 1) == 0

for n in range(64):
    print('*' if re.fullmatch('0b1*0*', bin(n)) else ' ',
          '*' if is_consecutive(n) else ' ',
          n, bin(n))

rawDescription: How to check if there is only consecutive ones and zeros in bytestream

description: Check if there is only consecutive ones and zeros in bytestream


code:
import code
def host_func():
    print("Hello old chap!")
c = code.compile_command("print(\"Script says hello!\"); host_func()")
exec(c)

rawDescription: Creating a simple scripting language in Python


rawDescription: new file name each time python run

description: New file name each time python run


code:
p = ggplot(dat, aes(x = x, y = y, fill = z)) + geom_tile()
print(p)

rawDescription: How to make a sunburst plot in R or Python?

description: Make a sunburst plot in R or Python


code:
for i, file in enumerate([htmlFile, jsFile, sassFile]):
    self.window.focus_group(i)
    self.window.open_file(file)

rawDescription: How to use the sublime plugin api to create a new layout and open a file in each cell

description: Use the sublime plugin api to create a new layout and open a file in each cell


code:
class MyDecorator(object):
    def __init__(self, func, param1, param2):
        self.func = func
        self.param1, self.param2 = param1, param2

    def __call__(self, *args, **kwargs):
        ...
        #use self.param1
        result = self.func(*args, **kwargs)
        #use self.param2
        return result

def my_dec_factory(param1, param2):
    def decorator(func):
         return MyDecorator(func, param1, param2)
    return decorator

rawDescription: Python decorator best practice, using a class vs a function


rawDescription: Python: How much space does each element of a list take?


code: fig = plt.gcf()

rawDescription: Hierarchy in matplotlib

description: Hierarchy in matplotlib


code: dict(results)

rawDescription: How to use Python re to parse this string in to list of multiple key value pairs?

description: Use Python re to parse this string in to list of multiple key value pairs


code:
import sys

if __name__ != "__main__":
    sys.modules[__name__] = OpenGLRenderer()

rawDescription: Intercepting module calls, python

description: Intercepting module calls, python


rawDescription: how to turn a string of letters embedded in squared brackets into embedded lists

description: Turn a string of letters embedded in squared brackets into embedded lists


rawDescription: How can I create a bytes object in python?

description: Create a bytes object in python


code: np.ndarray((x,), dt1, mat1)

rawDescription: How to initialize NumPy structured array with different default value for each column?

description: Initialize NumPy structured array with different default value for each column


code:
import pygal.util
import pygal.graph
import pygal.graph.radar

pygal.util.is_major = my_function
reload(pygal.graph)
reload(pygal.graph.radar)

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

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


code:
# create a few gates
g1 = AndGate("G1")
g2 = OrGate("G2")

# at this point, no connectors have been hooked up, so all pinX attrs are None:
print("G1 pins:", g1.pinA, g1.pinB) # "None, None"
print("G2 pins:", g2.pinA, g2.pinB) # "None, None"

# test that the gates work independently of each other:
print("G1 output:", g1.getOutput()) # will prompt for two inputs, then print their AND
print("G2 output:", g2.getOutput()) # will prompt for two inputs, then print their OR

# create a connection between the gates
c1 = Connector(g1, g2) # connects output of g1 to first available pin (pinA) of g2

# we can see that g2.pinA has changed
print("G2 pins after connection:", g2.pinA, g2.pinB)
   # "<__main__.Connector object at SomeHexAddress>, None"

# now, if we get g2's output, it will automatically request g1's output via the Connector
print("G2 output:", g2.getOutput())
   # will prompt for 2 G1 inputs, and one G2 input and print (G1_A AND G1_B) OR G2_B

rawDescription: Python class inheritance/Logic gate & circuit example


rawDescription: Combine results from regex to make x,y pairs


rawDescription: Split array to approximately equal chunks


code:
elevation_change = np.zeros_like(sediment_transport)
nrows, ncols = flow_direction.shape
lookup = {32: (-1, -1),
          16:  (0, -1),
          8:   (1, -1),
          4:   (1,  0),
          64: (-1,  0),
          128:(-1,  1),
          1:   (0,  1),
          2:   (1,  1)}

padded_transport = np.zeros((nrows + 2, ncols + 2), float)
padded_transport[1:-1, 1:-1] = sediment_transport

for [i, j], flow in np.ndenumerate(flow_direction):
    # Need to take into account the offset in the "padded_transport"
    r, c = i + 1, j + 1
    # This also allows for flow_direction values not listed above...
    dr, dc = lookup.get(flow, (0,0))
    elevation_change[i,j] = padded_transport[r + dr, c + dc]

rawDescription: Iterating through a numpy array and then indexing a value in another array


code:
class test(object):
    def __sub__(self,  other):
        return other

    def __rsub__(self,  other):
        return other if other != 1 else 666

In [11]: t = test()

In [12]: t - np.arange(4)
Out[12]: array([0, 1, 2, 3])

In [13]: np.arange(4) - t
Out[13]: array([0, 666, 2, 3], dtype=object)

rawDescription: Argument types differ in normal and reflected operator overload (__sub__ / __rsub__)


code: [(i,j.__name__) for i,j in l]

rawDescription: Extract characters from string after last period


code:
Output
sUEBtw

rawDescription: How do I use Python to pack a big integer treating four characters as an unsigned long in network byte order as Ruby's .pack("N") does?

description: Use Python to pack a big integer treating four characters as an unsigned long in network byte order as Ruby's .pack("N") does


code:
for handler in a_handlers + b_handlers + c_handlers:
    # do something....
    pass

rawDescription: Keeping a list of websocket connections in Tornado


code:
import re
import types
import inspect

class DummyInject:

    def __call__(self, **kwargs):
        return lambda func: func

    def __getattr__(self, name):
        return self

class Inject:

    function_end = re.compile(r"\)\s*:\s*\n")
    indent = re.compile("\s+")
    decorator = re.compile("@([a-zA-Z0-9_]+)[.a-zA-Z0-9_]*")
    exec_source = """
def create_new_func({closure_names}):
{func_source}
{indent}return {func_name}"""
    nonlocal_declaration = "{indent}nonlocal {closure_names};"

    def __init__(self, **closure_vars):
        self.closure_vars = closure_vars

    def __call__(self, func):

        lines, line_number = inspect.getsourcelines(func)
        self.inject_nonlocal_declaration(lines)
        new_func = self.create_new_function(lines, func)
        return new_func

    def inject_nonlocal_declaration(self, lines):
        """hides nonlocal declaration in first line of function."""
        function_body_start = self.get_function_body_start(lines)
        nonlocals = self.nonlocal_declaration.format(
            indent=self.indent.match(lines[function_body_start]).group(),
            closure_names=", ".join(self.closure_vars)
        )
        lines[function_body_start] = nonlocals + lines[function_body_start]
        return lines

    def get_function_body_start(self, lines):
        line_iter = enumerate(lines)

        found_function_header = False
        for i, line in line_iter:
            if self.function_end.search(line):
                found_function_header = True
                break
        assert found_function_header

        for i, line in line_iter:
            if not line.strip().startswith("#"):
                break

        return i

    def create_new_function(self, lines, func):
        # prepares source -- eg. making sure indenting is correct
        declaration_indent, body_indent = self.get_indent(lines)
        if not declaration_indent:
            lines = [body_indent + line for line in lines]
        exec_code = self.exec_source.format(
            closure_names=", ".join(self.closure_vars),
            func_source="".join(lines),
            indent=declaration_indent if declaration_indent else body_indent,
            func_name=func.__name__
        )

        # create new func -- mainly only want code object contained by new func
        lvars = {"closure_vars": self.closure_vars}
        gvars = self.get_decorators(exec_code, func.__globals__)
        exec(exec_code, gvars, lvars)
        new_func = eval("create_new_func(**closure_vars)", gvars, lvars)

        # add back bits that enable function to work well
        # includes original global references and
        new_func = self.readd_old_references(new_func, func)
        return new_func

    def readd_old_references(self, new_func, old_func):
        """Adds back globals, function name and source reference."""
        func = types.FunctionType(
            code=self.add_src_ref(new_func.__code__, old_func.__code__),
            globals=old_func.__globals__,
            name=old_func.__name__,
            argdefs=old_func.__defaults__,
            closure=new_func.__closure__
        )
        func.__doc__ = old_func.__doc__
        return func

    def add_src_ref(self, new_code, old_code):
        return types.CodeType(
            new_code.co_argcount,
            new_code.co_kwonlyargcount,
            new_code.co_nlocals,
            new_code.co_stacksize,
            new_code.co_flags,
            new_code.co_code,
            new_code.co_consts,
            new_code.co_names,
            new_code.co_varnames,
            old_code.co_filename, # reuse filename
            new_code.co_name,
            old_code.co_firstlineno, # reuse line number
            new_code.co_lnotab,
            new_code.co_freevars,
            new_code.co_cellvars
        )

    def get_decorators(self, source, global_vars):
        """Creates a namespace for exec function creation in. Must remove
        any reference to Inject decorator to prevent infinite recursion."""
        namespace = {}
        for match in self.decorator.finditer(source):
            decorator = eval(match.group()[1:], global_vars)
            basename = match.group(1)
            if decorator is Inject:
                namespace[basename] = DummyInject()
            else:
                namespace[basename] = global_vars[basename]
        return namespace

    def get_indent(self, lines):
        """Takes a set of lines used to create a function and returns the
        outer indentation that the function is declared in and the inner
        indentation of the body of the function."""
        body_indent = None
        function_body_start = self.get_function_body_start(lines)
        for line in lines[function_body_start:]:
            match = self.indent.match(line)
            if match:
                body_indent = match.group()
                break
        assert body_indent

        match = self.indent.match(lines[0])
        if not match:
            declaration_indent = ""
        else:
            declaration_indent = match.group()

        return declaration_indent, body_indent

if __name__ == "__main__":

    a = 1

    @Inject(b=10)
    def f(c, d=1000):
        "f uses injected variables"
        return a + b + c + d

    @Inject(var=None)
    def g():
        """Purposefully generate exception to show stacktraces are still
        meaningful."""
        create_name_error # line number 164

    print(f(100)) # prints 1111
    assert f(100) == 1111
    assert f.__doc__ == "f uses injected variables" # show doc is retained

    try:
        g()
    except NameError:
        raise
    else:
        assert False
    # stack trace shows NameError on line 164

rawDescription: decorator that add variable to closure

description: Decorator that add variable to closure


code:
import FunctionLibrary
FunctionLibrary.foo()
FunctionLibrary.bar()

rawDescription: Difference in ways of importing modules

description: Difference in ways of importing modules


code:
from ast import literal_eval
literal_eval(x)

rawDescription: Converting String to List

description: Converting String to List


code: idx = np.argmin(arr, axis=1)

rawDescription: keep only lowest value per row in a Python Pandas dataset

description: Keep only lowest value per row in a Python Pandas dataset


code:
def compare_tokens(t1, t2):
    getter = operator.attrgetter('word', 'lemma', 'pos')
    t1_values = getter(t1)
    if any(x is None for x in t1_values):
        return False
    return t1_values == getter(t2)

rawDescription: How to compare two objects, but just not none attributes

description: Compare two objects, but just not none attributes


code:
class Variables(object):
    numbers = {
        "num1": "Text 1",
        "num2": "Text 2",
        "num3": "Text 3",
    }

rawDescription: Python: Assigning Random Integer to Variables in a Class


code:
#!/usr/bin/env python

def startServer():
    print("Hello World")
    f= open("testfile.txt", "w")
    f.write("Hello world\n")
    f.close()

rawDescription: multiprocessing not spawning new processes

description: Multiprocessing not spawning new processes


code: contacts = dict((item["id"], item) for item in original_array)

rawDescription: Set-like alternative for yaml files

description: Set-like alternative for yaml files


code:
intersection = list_of_sets[0]
for s in list_of_sets[1:]:
    intersection = list_of_sets[0].intersection(intersection, s)

rawDescription: Why are methods so slow?

description: Why are methods so slow


code: min_index, min_value = min(enumerate(list1), key=lambda ie: ie[1][1])

rawDescription: What is faster? minimum of list then index of minimum, vs iterating through list


code:
def my_function(user_id):
    row =  database.db_session.query(User.name, ApiKey.api_key)\
        .join(ApiKey, User.vioozer_api_key==ApiKey.api_key)\
        .filter(User.user_id==user_id).first()
    return row._asdict()

my_data = my_function('user_00000000000000000000000000000000')

rawDescription: SQLAlchemy: Getting a single object from joining multiple tables


code:
my_data = ((3, 4), (None, 4), (5, 8), (None, 1), (0, None), (0, 1))
[x or y for x,y in my_data]
[x if x is not None else y for x,y in my_data]

rawDescription: Flatten Sequence With Boolean Expression Generator


code:
def letters(input):
    valids = []
    for character in input:
        if character.isalpha():
            valids.append(character)
    return ''.join(valids)

rawDescription: How do you filter a string to only contain letters?

description: Filter a string to only contain letters


code:
def test_func(arg1, *args, **kwargs):
    optional_arg = kwargs.get('optional_arg')
    ...

rawDescription: How to define method with optional argument and argument unpacking?

description: Define method with optional argument and argument unpacking


code:
bool("foo")
bool("")

rawDescription: Converting from a string to boolean in Python?


code:
def foo(a, b, c):
    return ''.join(v for v in locals().values() if v is not None)

foo('Cleese', 'Palin', None)

rawDescription: How to iterate over function arguments

description: Iterate over function arguments


code:
zip_values(list_1, list_2)
find_minimum(list_1)
find_minimum(list_2)
find_maximum(list_1)
find_maximum(list_2)

rawDescription: Getting specific values from different lists and how to


code:
names = ['Alice', 'Bob', 'Charlie']
scores = [8, 12, 9]
results = dict(zip(names, scores))

rawDescription: Is there a real-life advantage of mixed lists


code:
first_name = 'Arnold'
last_name = 'Pettibone'
addresses = [['1234 fake street', 'Anytown', 'OH', '12345'],]
addresses = [['1234 fake street', 'Anytown', 'OH', '12345'],
    ['742 Evergreen Terrace', 'Springfield', 'OH', '12345']]

rawDescription: Dynamically add lists as embedded document in Python pymongo mongodb


code:
class UploadBlobsHandler(blobstore_handlers.BlobstoreUploadHandler):

    def post(self):
        try:
            upload = self.get_uploads()[0]
            logging.info(upload)
            url = images.get_serving_url(upload)
            # Do something with it.
        except:
            self.redirect('/uploadform/?error', abort=True)

        self.redirect('/uploadform/?success&image_url=' + url)

rawDescription: Python GAE Problems with combining requestHandler and BlobstoreUploadHandler


code: fields = [item for item in fields if item in ccy_fields]

rawDescription: Items not being removed from list when list is larger


code:
import twitter

class Api(twitter.Api):
    pass
    # override/add any functions here.

rawDescription: How do I extend a python module? (python-twitter)

description: Extend a python module? (python-twitter)


code:
a = ['product','shampoo','vivel','spark']
b = ['random','product','shampoo','nothing']

c = list(set(a) & set(b))

d = list(set(b) - set(a))

print(c)

print(d)

rawDescription: Comparing two list and getting result a list with some checks


code:
with RenamedTemporaryFile('whatever') as f:
    f.write('stuff')

rawDescription: Threadsafe and fault-tolerant file writes


code:
class mystr(str):
    def __init__(self, value):
        self.value = value
        self._decoded = None
    @property
    def decoded(self):
        if self._decoded == None:
            self._decoded = self.value.decode("hex")
            return self._decoded
    def __repr__(self):
        return self.decoded
    def __len__(self):
        return len(self.decoded)
    def __getitem__(self, i):
        return self.decoded.__getitem__(i)
    def __getslice__(self, i, j):
        return self.decoded.__getslice__(i, j)

rawDescription: Python: Lazy String Decoding

description: Python: Lazy String Decoding


rawDescription: Parsing fasta file with biopython to count number sequence reads belonging to each ID


code:
driver = webdriver.Firefox()
get_status(driver)
driver.quit()
get_status(driver)

rawDescription: Python Selenium: How to check whether the WebDriver did quit()?


code: a[ind, :] = b[ind][:, None]

rawDescription: Search for value from one array in another array and modify array if not found - numpy


code:
#!/usr/bin/env python
import sys
##Initials,Last,First,ShiftStart,ShiftEnd
s='''BAB,Smith,Bob,10:00a,1:00p
JCJ,Jones,Jill,11:00a,3:00p
JIH,Hernandez,Jose,1:00p,4:00p
BAB,Smith,Bob,1:00p,3:00p
JIH,Hernandez,Jose,5:00p,9:00p
JCJ,Jones,Jill,3:00p,3:30p
JCJ,Jones,Jill,3:30p,5:00p
DJM,Martin,Dominique,8:00a,11:00a'''

db = {}
for line in s.split('\n'):
     Initials,Last,First,ShiftStart,ShiftEnd = line.split(',')
     if Initials in db:
         db[Initials][2].append((ShiftStart,ShiftEnd))
     else:
         db[Initials] = (Last,First,[(ShiftStart,ShiftEnd)])
for Initials,v in db.iteritems():
    Last,First,shifts = v
    sys.stdout.write(Initials + ',')
    sys.stdout.write(Last + ',' + First)
    for shift in shifts:
        ShiftStart,ShiftEnd = shift
        sys.stdout.write(',' + ShiftStart + ',' + ShiftEnd)
    sys.stdout.write('\n')

rawDescription: Consolidate .csv File to One Row per Unique Key (Example, Person) in Python


code:
from collections import Counter
from functools import reduce
d = ...
list(reduce(lambda a,b:a|b, d.values()))

rawDescription: counterdict unique values Python

description: Counterdict unique values Python


code:
class MyClass(object):
    ...
    def __getattr__(self, key):
        return self.config.get('constants', key)

rawDescription: python initialization of constants in separate file

description: Python initialization of constants in separate file


code:
datestr = "2013-12-12"
expireDate = datetime.strptime(datestr, "%Y-%m-%d").date()

rawDescription: Datetime string doesn't match after strftime() and strptime()


code: image.shape[1::-1] # gives you (width, height) tuple

rawDescription: OpenCV cv2 image to PyGame image?

description: OpenCV cv2 image to PyGame image


code:
@app.before_request
def f():
    g.db = connect_db()

rawDescription: '@' python decorator used to do something similar to method overriding in java?


code: timeseries['timeblock'] = timeseries['epoch'] - timeseries['epoch'][0]

rawDescription: Create five minute time blocks (Pandas/Python)


code: Counter({'apple': 3, 'cat': 2, 'boat': 1})

rawDescription: Printing items in a list that occur a certain number of times?


code: {{ adminform.form.non_field_errors }}

rawDescription: Permission to view, but not to change! - Django


code:
valid_ages = [12, 14, 18, 88, 33]

r.table("users").filter(lambda user:
    r.expr(valid_ages).contains(user["age"])
).run(connection)

rawDescription: RethinkDB: multiple comparisons filtering


code: product(range(2), repeat=k)

rawDescription: python all possible combinations of 0,1 of length k

description: Python all possible combinations of 0,1 of length k


code: [item for item in thingToSplit if not item.isdigit()]

rawDescription: Python: split a string delimited by given object type


code: df['filtered_exit'] = np.nan

rawDescription: Using Pandas DataFrame to Generate Trading Signals


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


code: ['foo', 'foo bar', 'foo bar baz', 'foo bar baz qux', 'bar', 'bar baz', 'bar baz qux', 'baz', 'baz qux', 'qux']

rawDescription: Break string into words and phrases

description: Break string into words and phrases


code:
with open('Sample.csv') as f:
    wordslist = f.read().split()

rawDescription: Counter in python not showing all words

description: Counter in python not showing all words


code:
d
dict((v, tuple([key for key in d if d[key]==v])) for k,v in d.iteritems())

rawDescription: Improvement for swapping keys with values in a dictionay?


code:
from basefile import *

class A(A):
    var = 10

rawDescription: Access subclass variables from parent file


code: A.reshape(len(A), -1, 1)*C

rawDescription: How to do element-wise multiplication of a matrix of scalars with a matrix of vectors?

description: Do element-wise multiplication of a matrix of scalars with a matrix of vectors


code:
def emit_sound(self, cr, uid, ids, context=None):
    for this in self.browse(cr, uid, ids, context):
       animal_registry = self.pool['animal.%s' % this.type]
       animal_ids = animal_registry.search(cr, uid,
           [('base_id','=',this.id)], context)
       assert len(animal_ids) == 1, 'Chimera alert! ;-)'
       animal_registry.emit_sound(cr, uid, animal_ids, context)

rawDescription: How can I treat an inheriting object as a subclass in OpenERP?

description: Treat an inheriting object as a subclass in OpenERP


rawDescription: Python string manipulation. Adding a character to the fifth place


code:
buffer_from_memory = ctypes.pythonapi.PyBuffer_FromMemory
buffer_from_memory.restype = ctypes.py_object
buffer = buffer_from_memory(y, 8*array_length)

rawDescription: Getting data from ctypes array into numpy


code:
import string #doesn't exist on python3.x, use the builtin `str` type instead.
from itertools import takewhile

printable = set(string.printable)
''.join(takewhile(lambda x: x in printable, s))

rawDescription: Howto Remove Garbage Data from String

description: Howto Remove Garbage Data from String


code:
foo = Foo([1,2,3])
print(foo.list)
print(foo.list[1])
foo.list[1] = 12
print(foo.list)

rawDescription: Create a list property in Python

description: Create a list property in Python


code:
import csv
...

input = open(in_fnam, 'rb')
output = open(out_fnam, 'wb')
writer = csv.writer(output)
for row in csv.reader(input):
    if row:
        writer.writerow(row)
input.close()
output.close()

rawDescription: Delete blank rows from CSV?

description: Delete blank rows from CSV


code:
class O(object):
    __metaclass__ = Meta

    def __init__(self):
        next_in_line(super(O, self))
        super(O, self).__init__()

class A(O):
    def __init__(self):
        next_in_line(super(A, self))
        super(A, self).__init__()

class B(O):
    def __init__(self):
        next_in_line(super(B, self))
        super(B, self).__init__()

class C(O):
    def __init__(self):
        next_in_line(super(C, self))
        super(C, self).__init__()

class D(O):
    def __init__(self):
        next_in_line(super(D, self))
        super(D, self).__init__()

class E(O):
    def __init__(self):
        next_in_line(super(E, self))
        super(E, self).__init__()

class K1(A, B, C):
    def __init__(self):
        next_in_line(super(K1, self))
        super(K1, self).__init__()

class K2(D, B, E):
    def __init__(self):
        next_in_line(super(K2, self))
        super(K2, self).__init__()

class K3(D, A):
    def __init__(self):
        next_in_line(super(K3, self))
        super(K3, self).__init__()

class Z(K1, K2, K3):
    def __init__(self):
        next_in_line(super(Z, self))
        super(Z, self).__init__()

rawDescription: How do I get the instance method's next-in-line parent class from `super()` in Python

description: Get the instance method's next-in-line parent class from `super()` in Python


code: c.NotebookApp.port = 7777

rawDescription: Make IPython Notebook accept connections only from given address/subnet


code: from gui import Gui

rawDescription: Python paths/import in standalone programs


code:
import glob, os, time
directory = os.path.join('inbox','*') #Uses wildcard so ANY files in the 'inbox' directory are used.
numFiles = len(glob.glob(directory)) #Gets initial number of files
while True:
    numFilesNew = len(glob.glob(directory)) #Gets current number of files
    if numFilesNew > numFiles: #Compare current amount of files to initial
        break #Exit loop if there's an increase
    else: time.sleep(1) #Wait if no files were added
files = glob.glob(directory) #Make a list of filenames in the directory
print(files)

rawDescription: Know when directory copy is complete

description: Know when directory copy is complete


rawDescription: python: unicode in Windows terminal, encoding used?

description: Python: unicode in Windows terminal, encoding used


code:
import multiprocessing as mp

def do_stuff(text):
    return text

if __name__ == '__main__':
    p = mp.Pool(4)
    tasks = ['str{}'.format(i) for i in range(2000)]
    results = p.map(do_stuff, tasks)
    p.close()

    print(results)

rawDescription: Python multiprocessing.Pool.map dying silently


code:
dfOut = dfIn.apply(expand, axis=1).stack()
dfOut = dfOut.to_frame().reset_index(level=1, drop=False)
dfOut.columns = ['location', 'name']
dfOut.reset_index(drop=True, inplace=True)
print(dfOut)

rawDescription: Iterate over rows and expand pandas dataframe


code:
from collections import OrderedDict

arrays = OrderedDict()
for file in os.listdir(directoryPath):
    if file.endswith(".csv"):
       arrays[file] = numpy.genfromtxt(file, delimiter=',')[:,2]

rawDescription: On using a string as an integer counter (aka index) in a for loop


rawDescription: How to check that pylab backend of matplotlib runs inline?

description: Check that pylab backend of matplotlib runs inline


rawDescription: Generate XML file from model data

description: Generate XML file from model data


code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.button import Button

class GetLinksButton(Button):

    link_number = 1

    def get_links(self):
        for i in xrange(3):
            link_button = LinkButton(
                text='link number ' + str(self.link_number)
            )
            self.link_number += 1
            self.links_grid.add_widget(link_button)

class LinkButton(Button):
    pass

class Test(App):
    pass

Test().run()

rawDescription: Usig Kivy how can I generate scaled buttons in a scrollview


rawDescription: how to get the common items count of lists

description: Get the common items count of lists


code:
# Parse the file which was already split into split_list
split_list = ['>103L', 'Sequence:', 'MNIFEMLRIDEGLRLKIYKDTEGYYTIGIGHLLTKSPSLNSLDAAKSELDKAIGRNTNGVITKDEAEKLFNQDVDAAVRGILRNAKLKPVYDSLDAVRRAALINMVFQMGETGVAGFTNSLRMLQQKRWDEAAVNLAKSRWYNQTPNRAKRVITTFRTGTWDAYKNL', 'Disorder:', '----------------------------------XXXXXX-----------------------------------------------------------------------------------------------------------------------------XX']
header   = split_list[0] + " " + split_list[1]
seq      = split_list[2]
disorder = split_list[4]

# Create the new disorder string
new_disorder = ["Disorder: Posi R"]
for i, x in enumerate(disorder):
    if x == "X":
        # Appends of the form: "AminoAcid Position"
        new_disorder.append( "{} {}".format(i, seq[i]) )

new_disorder = " ".join(new_disorder)

# Output the modified file
open("seq2.txt", "w").write( "\n".join([header, seq, new_disorder]))

rawDescription: How to align and compare two elements (sequence) in a list using python

description: Align and compare two elements (sequence) in a list using python


code:
df.columns = ['first'] + list(range(1,11))*3
pieces = [df.iloc[:, i:i+10] for i in range(1,31, 10)]
df2 = pd.concat(pieces, keys = ['first', 'second', 'third'])
df2 = df2.swaplevel(1,0).sortlevel(0)
df2.insert(0, 'first', df['first'].repeat(3).values)

rawDescription: Moving columns down and replicating keys in pandas


code:
source = urllib2.urlopen(link)
html = source.read()
source.close();
soup = BeautifulSoup(html, "html.parser")

rawDescription: Getting names from HTML file via python

description: Getting names from HTML file via python


rawDescription: How to sort the results of Counter.mostCommon(n) lexicographically (by counter, then by value)?

description: Sort the results of Counter.mostCommon(n) lexicographically (by counter, then by value)


code:
with tempfile.TemporaryFile('w') as ftemp:
    shutil.copyfileobj(ftemp, f)
    ftemp.seek(0, 0)
    f.seek(start, 0)
    shutil.copyfileobj(f, ftemp)
f.truncate()

rawDescription: How to make a buffered writer?

description: Make a buffered writer


code: df['day'] = pd.factorize(df.day)[0]

rawDescription: Pandas: How to convert column of string objects representing categories to integers?


code:
employed = models.NullBooleanField(choices=LOCATOR_YES_NO_CHOICES,
                                max_length=3,
                                blank=True, null=True, default=None,)

rawDescription: store null as default value againt Null boolean field

description: Store null as default value againt Null boolean field


code:
output = ''
for element in MyArray:
    C = chr(int(element,2))
    output = output + C

print(output)

rawDescription: Convert a while loop into a for loop in Python 3.3


code:
def find_neighbor_indices(m, i, j, dist=1):
    irange = range(max(0, i-dist), min(len(m), i+dist+1))
    if len(m) > 0:
        jrange = range(max(0, j-dist), min(len(m[0]), j+dist+1))
    else:
        jrange = []
    for icheck in irange:
        for jcheck in jrange:
            # Skip when i==icheck and j==jcheck
            if icheck != i or jcheck != j:
                neighbors.append((icheck, jcheck))
    return neighbors

rawDescription: python find all neighbours of a given node in a list of lists

description: Python find all neighbours of a given node in a list of lists


code:
f = obj.method
f(args)
# is equivalent to
obj.method(args)

rawDescription: Can I specify another class's instance-method as a variable for my method?

description: Specify another class's instance-method as a variable for my method


code:
import matplotlib.pyplot as plt

plt.axvline(x=0.22058956)
plt.axvline(x=0.33088437)
plt.axvline(x=2.20589566)

rawDescription: How to draw vertical lines on a given plot in matplotlib?

description: Draw vertical lines on a given plot in matplotlib


code:
import re
inputtext = '''\
Lorem Ipsum is simply a dummy ? text | of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it d to make * type specimen book. It has survived not only five centuries, but also the leap into [ electronic typesetting, remaining essentially unchanged.
'''
re.sub(r'((?:^|(?<=\s))\S\s|\s\S(?:$|(?=\s)))', '', inputtext)

rawDescription: Python single character clean

description: Python single character clean


code:
def __init__(self, start_scene):
    self.start_scene = start_scene

rawDescription: Learn Python the Hard Way Exercise 43

description: Learn Python the Hard Way Exercise 43


code:
timeit('line.partition("|")[0] == name', 'line="John|22|Nice";name="John"')
timeit('line.split("|", 1)[0] == name', 'line="John|22|Nice";name="John"')

rawDescription: Deleting a line in file containing exact string (Python)


rawDescription: Python regex negating metacharacters

description: Python regex negating metacharacters


rawDescription: Python finite boundary Voronoi cells

description: Python finite boundary Voronoi cells


rawDescription: Iterating through a list - Python 3

description: Iterating through a list - Python 3


code:
from datetime import date
import calendar

d = date(2000,1,1)
d = d.replace(day=calendar.monthrange(d.year, d.month)[1])

rawDescription: Handling monthly-binned data in pandas

description: Handling monthly-binned data in pandas


code:
a['Atom'] == 'C'
np.logical_and(a['Atom'] == 'C', a['AA2'] == 'GLN')
a[np.logical_and(a['Atom'] == 'C', a['AA2'] == 'GLN')]

rawDescription: Nicer way to compute means of a set with numpy


code:
import numpy as np

c = a[np.arange(len(b)), b]

rawDescription: Is there a faster method to do this sort of indexing in numpy?


code:
import csv
import re
import string

output_header = ['a_id', 'b_id', 'CC', 'DD', 'EE', 'FF', 'GG']

sanitise_table = string.maketrans("","")
nodigits_table = sanitise_table.translate(sanitise_table, string.digits)

def sanitise_cell(cell):
    return cell.translate(sanitise_table, nodigits_table)       # Keep digits

with open('fileOne.csv') as f_input, open('resultFile.csv', 'wb') as f_output:
    csv_input = csv.reader(f_input)
    csv_output = csv.writer(f_output)

    input_header = next(f_input)
    csv_output.writerow(output_header)

    for row in csv_input:
        bb = re.match(r'(\d+)_(\d+)\.csv', row[1])

        if bb and row[2] not in ['No Bi', 'less']:
            # Remove all columns after 'Mi' if present
            try:
                mi = row.index('Mi')
                row[:] = row[:mi] + [''] * (len(row) - mi)
            except ValueError:
                pass

            row[:] = [sanitise_cell(col) for col in row]
            row[0] = bb.group(1)
            row[1] = bb.group(2)
            csv_output.writerow(row)

rawDescription: Delete or remove unexpected records and strings based on multiple criteria by python or R script


code:
# -*- coding: utf-8 -*-
print(repr(u"서울특별시"))
print(repr(u"서울특별시"[0]))

rawDescription: Segment a korean word into individual syllables - C++/Python


code:
import os, glob
next(glob.iglob(os.path.expanduser('~/.mozilla/firefox/*.default/bookmarkbackups')))

rawDescription: How to find the path of a changing directory name?

description: Find the path of a changing directory name


code: user = session.query(User).get(user_id)

rawDescription: Application Layout with Groups of single items in Python


code:
class MyForm(forms.Form):
# ...

    def clean(self):
        cleaned_data = super(MyForm, self).clean()

        # get "some info from the form"
        part1 = cleaned_data.get('part1_field', '')
        part2 = cleaned_data.get('part2_field', '')

        # "manipulate it to fill another filed"
        cleaned_data['another_field'] = part1 + part2

        return cleaned_data

rawDescription: Django Assigning BoundField Value

description: Django Assigning BoundField Value


code:
BLAJHKDTRGALQUXHGVDR
MAQZTBARFZTYVVCNSSIR
NULVQTZFFLXEBHBLOJRR
PFEDLOSDSRHAYYXLAYBV
KSXPMGHDFJDLHGYEEWKD

rawDescription: How to fragment a large python object into smaller chunks

description: Fragment a large python object into smaller chunks


code:
from Tkinter import *
root = Tk()
cv = Canvas(root)
cv.create_rectangle(10,10,50,50)
cv.pack()
root.mainloop()

cv.update()
cv.postscript(file="file_name.ps", colormode='color')

root.mainloop()

rawDescription: How can I convert canvas content to an image?

description: Convert canvas content to an image


code:
a = [[1, None, 2], [3, 4, 5], [6, None, 7]]
[[0 if x is None else x for x in sublist] for sublist in a]

rawDescription: Iteration and replace trough double array


rawDescription: Get a 3D or 4D array as input and reshape it to 2D array in a function from external script


code: repo.head.set_commit(new_commit, logmsg="commit: %s" % message)

rawDescription: How can I specify the committed_date of a Commit using GitPython?

description: Specify the committed_date of a Commit using GitPython


rawDescription: sparse versions of Pandas DataFrame

description: Sparse versions of Pandas DataFrame


rawDescription: Get a date based on week number (python)


code:
# Assuming you have flower object and quantity=number of qty.
cart_item = CartDetail(flower=flower, cart=request.user.cart, quantity=quantity)

cart_item.save()

rawDescription: Python/Django: RelatedObjectDoesNotExist: Cart has no user


code:
def fs2_Unclear_upper(*args):
    import socket, time
    class FontSlant():
        from com.sun.star.awt.FontSlant import (NONE, ITALIC,)
#get the doc from the scripting context which is made available to all scripts
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
    text = model.Text
    tRange = text.End
    cursor = desktop.getCurrentComponent().getCurrentController().getViewCursor()
# your cannot insert simple text and text into a table with the same method
# so we have to know if we are in a table or not.
# oTable and oCurCell will be null if we are not in a table
    oTable = cursor.TextTable
    oCurCell = cursor.Cell
    import os
    from configobj import ConfigObj
    configuration_dir = os.environ["HOME"]
    config_filename = configuration_dir + "/fs2.cfg"
    if  os.access(config_filename, os.R_OK):
        pass
    else:
        return None
    cfg = ConfigObj(config_filename)
    #define values to use from the configuration file
    tcp_port = int(cfg["control"]["TCP_PORT"])
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(0.5)
    try:
        sock.connect(("localhost", tcp_port))
    except:
        return None
    sock.settimeout(0.5)
    try:
        sock.send(bytes('get_time\n', 'UTF-8'))
    except:
        return None
    try:
        time.sleep(0.2)
        s_list = sock.recv(1024).decode('UTF-8')
        s_list = s_list.split("\n")
    except:
        return None
    lines_in_response = len(s_list)
    if lines_in_response is None:
        return None
    time_stamp = s_list[0]
    insert_text = "[Unclear " + time_stamp + "]"
    Text_Italic = FontSlant.ITALIC
    Text_None = FontSlant.NONE
    cursor.CharPosture=Text_Italic
    if oCurCell == None: # Are we inserting into a table or not?
        text.insertString(cursor, insert_text, 0)
    else:
        cell = oTable.getCellByName(oCurCell.CellName)
        cell.insertString(cursor, insert_text, False)
    cursor.CharPosture=Text_None
    sock.close()
    return None

rawDescription: How do I insert text as Italic or Bold etc with a python macro in libreoffice writer

description: Insert text as Italic or Bold etc with a python macro in libreoffice writer


code:
from PyQt4 import QtGui
import sys

from guiLoop import guiLoop # https://gist.github.com/niccokunzmann/8673951

@guiLoop
def led_blink(argument):
    while 1:
        print("LED on " + argument)
        yield 0.5 # time to wait
        print("LED off " + argument)
        yield 0.5

app = QtGui.QApplication(sys.argv)

w = QtGui.QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()

led_blink(w, 'shiny!')

sys.exit(app.exec_())

rawDescription: How to quit the program in while loop using push-button in PyQt

description: Quit the program in while loop using push-button in PyQt


code:
import re
html = re.sub(r'(<img src=").+/(.+">)', r'\1id:\2', html)

rawDescription: Python regex: search for HTML tags and modify them


code:
try:
    # Python 2
    from future_builtins import map, filter
except ImportError:
    # Python 3
    pass

conditions = (check_size, check_color, check_tone, check_flavor)
return next(filter(None, map(lambda f: f(), conditions)), None)

rawDescription: Pythonic way to avoid "if x: return x" statements


code:
obj = json.load(...)
[dct for dct in obj['posts'] if 'no' in dct]

rawDescription: How do I parse JSON with multiple keys the same?

description: Parse JSON with multiple keys the same


code:
class ConcertInline(admin.TabularInline):
    model = Concert

class VenueAdmin(admin.ModelAdmin):
    inlines = [ConcertInline]
    # that was quick!

rawDescription: why is models.ForeignKey advantageous?

description: Why is models.ForeignKey advantageous


rawDescription: Getting digits in an order (left to right) after OCR from x,y coordinates


code:
def square(x):
    y = x**2
    return y
square(x) # where x is your data running from the prior cells

rawDescription: Run parts of a ipython notebook in a loop / with different input parameter


code:
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)

rawDescription: Register a "Hello World" DBus service, object and method using Python


rawDescription: Tab-delimited file using csv.reader not delimiting where I expect it to


code:
import os
import tkinter as tk
from tkinter import Frame, Button
from PIL import Image, ImageTk

tk_root = tk.Tk()
tk_root.title("Picture Viewer - Do I want to keep this picture?")
file_count = 0

p = path = 'C:\\Users\\MyUserName\\Desktop\\Test\\'

def search(directory):
    global file_count
    for root, subdirs, files in os.walk(directory):
        for file in files:
            if os.path.splitext(file)[1].lower() in ('.jpg', '.jpeg'):
                img = os.path.join(root, file)
                file_count += 1
                yield img

def next_image():
    try:
        global photo_path
        photo_path = next(path_generator)
        photo = ImageTk.PhotoImage(Image.open(photo_path))
        picture.configure(image=photo)
        picture.image = photo
    except StopIteration:
        picture.configure(image='', text='All done!')

def move_file(directory):
    if not os.path.exists(directory):
        os.makedirs(directory)
    new_file = directory + 'Picture_{0}.jpg'.format(file_count)
    os.rename(photo_path, new_file)

def yes():
    move_file(path + 'Yes\\')
    next_image()

def maybe():
    move_file(path + 'Maybe\\')
    next_image()

def skip():
    move_file(path + 'Skipped\\')
    next_image()

def delete():
    # Code for deleting file here
    next_image()

top_frame = Frame(tk_root)
bottom_frame = Frame(tk_root)
top_frame.pack(side='top')
bottom_frame.pack(side='bottom')

path_generator = search(p)
photo_path = next(path_generator)

photo = ImageTk.PhotoImage(Image.open(photo_path))
picture = tk.Label(tk_root, image=photo)
picture.image = photo
picture.pack(side='top')

button_yes = Button(top_frame, text="Yes", command=yes)
button_maybe = Button(top_frame, text="Maybe", command=maybe)
button_skip = Button(top_frame, text="skip", command=skip)
button_delete = Button(bottom_frame, text="Delete", command=delete)

button_yes.pack(side='left')
button_maybe.pack(side='left')
button_skip.pack(side='left')
button_delete.pack(side='bottom')

tk_root.mainloop()

rawDescription: How do I get my tkinter picture viewer working?

description: Get my tkinter picture viewer working


code:
import os

name = "in_file%d"
for i in range(1, 21):
    os.system("python code.py in_file{} in_file2 outfile{}".format(i, i))

rawDescription: how to direct a sh script to run a python script?

description: Direct a sh script to run a python script


code:
d = {}
d['__metaclass__'] = FooMeta
d['FOO'] = 123
def a(self):
    pass
d['a'] = a
Foo = d.get('__metaclass__', type)('Foo', (object,), d)

rawDescription: Python metaclasses: Why isn't __setattr__ called for attributes set during class definition?


code:
import requests
url = 'https://...'
payload = {'key1': 'value1', 'key2': 'value2'}

# GET
r = requests.get(url)

# GET with params in URL
r = requests.get(url, params=payload)

# POST with form-encoded data
r = requests.post(url, data=payload)

# POST with JSON
import json
r = requests.post(url, data=json.dumps(payload))

# Response, status etc
r.text
r.status_code

rawDescription: Simple URL GET/POST function in Python

description: Simple URL GET/POST function in Python


code:
for Xtmp,Ytmp in zip(X,Y):
    ...

rawDescription: SciPy interp2D for pairs of coordinates

description: SciPy interp2D for pairs of coordinates


code:
# x in lst
for item in lst:
    if x == item:
        return True
return False

rawDescription: `object in list` behaves different from `object in dict`?


rawDescription: Python 2D array manipulation

description: Python 2D array manipulation


code: [[float(x) for x in term.as_coeff_exponent(z)] for term in G_F.as_ordered_terms()]

rawDescription: Getting a list of coefficient/exponent pairs in sympy


code:
data = rb'''\xF0\x9F\x98\x81
\xF0\x9F\x98\x82
\xF0\x9F\x98\x83
\xF0\x9F\x98\x84
\xF0\x9F\x98\x85'''

data = data.decode('unicode-escape').encode('latin-1').decode('utf8')
print(ascii(data))
print(data)

rawDescription: python3 interpret ascii string as unicode string

description: Python3 interpret ascii string as unicode string


code: ['abcd']

rawDescription: How to compute the minimum number of maximal strings?

description: Compute the minimum number of maximal strings


code:
from sqlalchemy import tuple_

session.query(Foo).filter(tuple_(Foo.a, Foo.b, Foo.c).in_(items))

rawDescription: How to get rows which match a list of 3-tuples conditions with SQLAlchemy

description: Get rows which match a list of 3-tuples conditions with SQLAlchemy


code:
mydict = dict([('&' + chr(i), str(i)) for i in range(39,1024)])

# random inserts between keys
from random import randint
rawstr = ''.join(mydict.keys())
mystr = ''
for i in range(0, len(rawstr), 2):
    mystr += chr(randint(65,91)) * randint(0,20) # insert between 0 and 20 chars

from time import time

# How many times to run each solution
rep = 10000

print('Running %d times with string length %d and ' \
      'random inserts of lengths 0-20' % (rep, len(mystr)))

# Tor Valamo - too long
#t = time()
#for x in range(rep):
#    for k, v in mydict.items():
#        mystr.replace(k, v)
#print('%-30s' % 'Tor fixed & variable dict', time()-t)

from re import sub, compile, escape

# Peter Hansen
t = time()
for x in range(rep):
    sub(r'(&[a-zA-Z])', r'%(\1)s', mystr) % mydict
print('%-30s' % 'Peter fixed & variable dict', time()-t)

# Peter 2
def dictsub(m):
    return mydict[m.group()]

t = time()
for x in range(rep):
    sub(r'(&[a-zA-Z])', dictsub, mystr)
print('%-30s' % 'Peter fixed dict', time()-t)

# Claudiu - too long
#def multiple_replace(dict, text):
#    # Create a regular expression  from the dictionary keys
#    regex = compile("(%s)" % "|".join(map(escape, dict.keys())))
#
#    # For each match, look-up corresponding value in dictionary
#    return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
#
#t = time()
#for x in range(rep):
#    multiple_replace(mydict, mystr)
#print('%-30s' % 'Claudio variable dict', time()-t)

# Claudiu - Precompiled
regex = compile("(%s)" % "|".join(map(escape, mydict.keys())))

t = time()
for x in range(rep):
    regex.sub(lambda mo: mydict[mo.string[mo.start():mo.end()]], mystr)
print('%-30s' % 'Claudio fixed dict', time()-t)

# Separate setup for Andrew and gnibbler optimized dict
mydict = dict((k[1], v) for k, v in mydict.items())

# Andrew Y - variable dict
def mysubst(somestr, somedict):
  subs = somestr.split("&")
  return subs[0] + "".join(map(lambda arg: somedict[arg[0:1]] + arg[1:], subs[1:]))

def mysubst2(somestr, somedict):
  subs = somestr.split("&")
  return subs[0].join(map(lambda arg: somedict[arg[0:1]] + arg[1:], subs[1:]))

t = time()
for x in range(rep):
    mysubst(mystr, mydict)
print('%-30s' % 'Andrew Y variable dict', time()-t)
t = time()
for x in range(rep):
    mysubst2(mystr, mydict)
print('%-30s' % 'Andrew Y variable dict 2', time()-t)

# Andrew Y - fixed
def repl(s):
  return mydict[s[0:1]] + s[1:]

t = time()
for x in range(rep):
    subs = mystr.split("&")
    res = subs[0] + "".join(map(repl, subs[1:]))
print('%-30s' % 'Andrew Y fixed dict', time()-t)

# gnibbler
t = time()
for x in range(rep):
    myparts = mystr.split("&")
    myparts[1:]=[mydict[x[0]]+x[1:] for x in myparts[1:]]
    "".join(myparts)
print('%-30s' % 'gnibbler fixed & variable dict', time()-t)

rawDescription: Mass string replace in python?

description: Mass string replace in python


code:
import itertools
from collections import Counter
a = ((0,1,2,3) + c for c in itertools.combinations_with_replacement(range(4), 6) if max(Counter(c).values()) < 5)

rawDescription: itertools cominbations with replacement with limitations

description: Itertools cominbations with replacement with limitations


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

# The key here is the figsize (it needs to be square). The position and size of
# axes in matplotlib are defined relative to the size of the figure.
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(8,8))

for i, ax in enumerate(axes.flat, start=1):
    ax.plot(np.arange(0, i * 4, i))

# By default, subplots leave a bit of room for tick labels on the left.
# We'll remove it so that the axes are perfectly square.
fig.subplots_adjust(left=0.1)

plt.show()

rawDescription: matplotlib: aspect ratio in subplots with various y-axes

description: Matplotlib: aspect ratio in subplots with various y-axes


code:
import bottle
from threading import Thread

on_data = lambda alpha, beta, gamma: None

@bottle.route('/')
def handler():
    return bottle.static_file('index.html', '.')

@bottle.post('/data')
def handler():
    on_data(**bottle.request.json)

def data_handler(f):
    global on_data
    on_data = f
    return f

def go():
    t = Thread(target=lambda: bottle.run(quiet=True))
    t.start()

rawDescription: How can I read the accelerometer in my windows tablet with python?

description: Read the accelerometer in my windows tablet with python


rawDescription: Understanding numpy "void" dtype and generic arrays