code:
# That class is necessary, it's explained why in Loader's class comments
class Cleaner(dict):
   pass

class Loader:
    def __init__(self):
        self.sprite = pyglet.resource.image(self.unzip('test.png'))
        self.sprite = pyglet.resource.image(self.unzip('test2.png'))
    def unzip(self, file):
        zip = zipfile.ZipFile('test.zip')
        file = open('.buffer', 'wb')
        # without 'b' it wont work on windows
        file.write(zip.read(file))
        file.close()
        '''now the tricky part: pyglet save every file with weakref to
           dont load save thing more than once, it wouldnt let to load
           files from buffer so we need to block it somehow after each
           file reading i do that with empty dict class (dont need to import weakref)'''
        pyglet.resource._default_loader._cached_images = Cleaner()
        return 'data/.buffer'

rawDescription: Pyglet's ZIPLocation

description: Pyglet's ZIPLocation


code:
df = pandas.DataFrame([[2, 3, 5]], columns=["A", "B", "C"])
df
df.to_csv('someCSV.csv')
df**0.5
(df**0.5).to_csv('someCSV2.csv')

rawDescription: Transformed Data Takes up 4x more space for a doubling of variables in pandas for python


code:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np

im = np.arange(100)
im.shape = 10, 10

fig = plt.figure(1, (4., 4.))
grid = ImageGrid(fig, 111, # similar to subplot(111)
                nrows_ncols = (2, 2), # creates 2x2 grid of axes
                axes_pad=0.1, # pad between axes in inch.
                aspect=False, # do not force aspect='equal'
                )

for i in range(4):
    grid[i].imshow(im) # The AxesGrid object work as a list of axes.

plt.show()

rawDescription: What can I do about the overlapping labels in these subplots?


code:
templates_dir = os.path.join(os.path.dirname(__file__), 'templates')
foo_dir = os.path.join(templates_dir, 'foo_dir')
JINJA_PAGES = jinja2.Environment(
    loader=jinja2.FileSystemLoader([templates_dir, foo_dir]),
    extensions=['jinja2.ext.autoescape'],
    autoescape=True)

rawDescription: Jinja2 Tempates Subdirectory

description: Jinja2 Tempates Subdirectory


rawDescription: python trying to remove single apostrophe

description: Python trying to remove single apostrophe


code:
from collections import defaultdict

mydict = defaultdict(lambda: defaultdict(list))

rawDescription: How to make default dictionary of default dictionaries

description: Make default dictionary of default dictionaries


code:
reobj = re.compile(r"""(?=[A-Z])([A-Za-z0-9"'. ]{50,}\.)""")
result = reobj.findall(subject)

rawDescription: How to extract a string from text that is 50 chars long and consists of A to Z and 0-9 .. starts with a capital letter

description: Extract a string from text that is 50 chars long and consists of A to Z and 0-9 .. starts with a capital letter


code:
intlist = range(1,10)
randomeven = random.choice([i for i in intlist if i % 2 == 0])

rawDescription: pythonic way of selecing a random value that satisfies a certain predicate

description: Pythonic way of selecing a random value that satisfies a certain predicate


code:
from copy import deepcopy
for iteration in range(times):
    i = 0
    particles_snapshot = deepcopy(particles)
    for particle in particles:
        results[iteration][i] = particle.move(particles_snapshot)
        i += 1

rawDescription: Floats incorrect? - Python 2.6

description: Floats incorrect? - Python 2.6


code:
USERAGENT = 'something'
HEADERS = {'User-Agent': USERAGENT}

req = urllib2.Request(URL_HERE, headers=HEADERS)
f = urllib2.urlopen(req)
s = f.read()
f.close()

rawDescription: Python's urllib2 doesn't work on some sites


rawDescription: NLTK WordNet Lemmatizer: Shouldn't it lemmatize all inflections of a word?


code:
import numpy
list_size_1 = []
list_size_2 = []
for row in data.itervalues():
    if len(row) == 1:
      list_size_1.append(row)
    elif len(row) == 2:
      list_size_2.append(row)
list_size_1 = numpy.array(list_size_1)
list_size_2 = numpy.array(list_size_2)
std_1 = numpy.std(list_size_1, axis = 1)
std_2 = numpy.std(list_size_2, axis = 1)

rawDescription: Best way to create a NumPy array from a dictionary?

description: Create a NumPy array from a dictionary


rawDescription: gevent and twisted producer performance problems

description: Gevent and twisted producer performance problems


code:
from kdtree import KDTree

data = [(1,2,3),(4,0,1),(5,3,1),(10,5,4),(9,8,9),(4,2,4)]

tree = KDTree.construct_from_data(data)
nearest = tree.query(query_point=(5,4,3), t=1)

rawDescription: Proximity of colors in a rainbow

description: Proximity of colors in a rainbow


code:
mock_funct.assert_called_once_with(mock.ANY)
call = mock_funct.call_args
call_args, call_kwargs = call  # calls are 2-tuples of (positional_args, keyword_args)
self.assertEqual(json.loads(call_args[0]), expected)

rawDescription: Assert mocked function called with json string in python


code:
from django.db import models
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _

class MyModel(models.Model):
    goal = models.DecimalField(decimal_places=2, max_digits=16,
              validators=[MinValueValidator(1)])

    def clean(self):
        if self.goal < 1:
            raise ValidationError(_('Only numbers equal to 1 or greater are accepted.'))

rawDescription: MinValueValidator not working on DecimalField in Django


code:
def testOne(self):
    """Test IsOdd(1)"""
    self.failUnless(IsOdd(1))

def testTwo(self):
    """Test IsOdd(2)"""
    self.failIf(IsOdd(2))

rawDescription: Python unittest, how to display a better group/test name?


code:
df1.set_index(['id', 'noteId'], inplace=True)
df1.update(df2)

rawDescription: Merge two DataFrames with some equal columns


code:
x

rows = np.array([[0,0],[3,3]])
columns = np.array([[0,2],[0,2]])
x[rows,columns]

rawDescription: Numpy 2d and possibly N-d indexing by array of tuples


code:
from sqlalchemy import case
from sqlalchemy.ext.hybrid import hybrid_property

@hybrid_property
def type(self):
    return 'special' if self.order_type else 'regular'

@type.expression
def type(cls):
    return case({True: 'special', False: 'regular'}, cls.order_type)

rawDescription: sqlalchemy, hybrid property case statement

description: Sqlalchemy, hybrid property case statement


code:
LETTERS = 'ZABCDEFGHIJKLMNOPQRSTUVWXY'
def main():
    myMode = input("Encrypt 'e' or Decrypt 'd': ")
    if myMode == 'encrypt' or myMode == 'e':
        translated = encryptFile()
    elif myMode == 'decrypt' or myMode == 'd':
        translated = decryptFile()

def importFile():
     importText = []
     file = input('What is the text file name: ')
     fileName = open(file,'r')
     text = fileName.read()
     fileName.close()
     fileName = text
     message = text
     #print (text)
     delete = open(file,'w')
     delete.write('')
     return input ('What key do you wnat to use: ')  # Return the key

def encryptFile():
    textFile = input("Would you like to import a text file 'Y' or 'N': ")
    if textFile.lower() == 'y' :
        key = importFile()  # Get the key returned by the function

    return translatedMessage('encrypt', key)  # Pass the key to the function

def decryptFile():
    textFile = input("Would you like to import a text file 'Y' or 'N': ")
    if textFile.lower() == 'y' :
        key = importFile()  # Get the key returned by the function

    return translatedMessage('decrypt', key)  # Pass the key to the function

def translatedMessage(mode, key):  # `key` is an argument
    translated = []
    keyIndex = 0
    key = key.upper()

    for symbol in message:
        num =LETTERS .find(symbol.upper())
        if num != -1:
            if mode == 'encrypt':
                num += LETTERS .find(key[keyIndex])
            elif mode == 'decrypt':
                num -= LETTERS .find(key[keyIndex])

            num %= len(LETTERS)

            if symbol.isupper():
                translated.append(LETTERS[num])
            elif symbol.islower():
                translated.append(LETTERS[num].lower())

            keyIndex += 1
            if keyIndex == len(key):
                keyIndex = 0
        else:
            translated.append(symbol)
    return ''.join(translated)
if __name__ == '__main__':
    main()

rawDescription: UnboundLocalError: local variable 'key' referenced before assignment


code:
from collections import defaultdict

groups = defaultdict(list)

for filename in os.listdir(directory):
    basename, extension = os.path.splitext(filename)
    project, subject, session, ftype = basename.split('-x-')

    groups[session].append(filename)

rawDescription: Python: Parsing and grouping filenames in directory


code:
from xml.etree import ElementTree as ET

# drugs is a reference to your <Drugs> element

for row in csvreader:
    drug = ET.SubElement(drugs, 'Drug')
    ET.SubElement(drug, 'DrugID').text = row[0]
    ET.SubElement(drug, 'Dose').text = row[1]
    ET.SubElement(drug, 'Unit').text = row[2]

rawDescription: Add sub-elements to newly created elements in python elementtree


code:
import asyncio

class Client(asyncio.Protocol):

    def connection_made(self, transport):
        self.connected = True
        # save the transport
        self.transport = transport

    def data_received(self, data):
        # forward data to the server
        self.server_transport.write(data)

    def connection_lost(self, *args):
        self.connected = False

class Server(asyncio.Protocol):
    clients = {}
    def connection_made(self, transport):
        # save the transport
        self.transport = transport

    @asyncio.coroutine
    def send_data(self, data):
        # get a client by its peername
        peername = self.transport.get_extra_info('peername')
        client = self.clients.get(peername)
        # create a client if peername is not known or the client disconnect
        if client is None or not client.connected:
            protocol, client = yield from loop.create_connection(
                Client, 'google.com', 80)
            client.server_transport = self.transport
            self.clients[peername] = client
        # forward data to the client
        client.transport.write(data)

    def data_received(self, data):
        # use a task so this is executed async
        asyncio.Task(self.send_data(data))

@asyncio.coroutine
def initialize(loop):
    # use a coroutine to use yield from and get the async result of
    # create_server
    server = yield from loop.create_server(Server, '127.0.0.1', 8888)

loop = asyncio.get_event_loop()

# main task to initialize everything
asyncio.Task(initialize(loop))

# run
loop.run_forever()

rawDescription: How can I create a relay server using tulip/asyncio in Python?

description: Create a relay server using tulip/asyncio in Python


code:
class CounterExample(object):

    instances_created = 0

    def __init__(self):
        CounterExample.instances_created += 1

    def __del__(self):
        """ If you want to track the current number of instances
            you can add a hook in __del__. Otherwise use
            __init__ and just count up.
        """
        CounterExample.instances_created -= 1

rawDescription: Assigning IDs to instances of a class (Pythonic)


code:
from scrapy.http import FormRequest

class myspiderSpider(Spider):
    name = "myspider"
    allowed_domains = ["www.example.com"]

    def start_requests(self):
        return [ FormRequest("http://www.example.com/login",
                     formdata={'someparam': 'foo', 'otherparam': 'bar'},
                     callback=self.parse) ]

rawDescription: How to send post data in start_urls of the scrapy spider

description: Send post data in start_urls of the scrapy spider


code:
import os
s = "/folderA/folderB/folderC/folderD"
os.path.basename(s)
os.path.dirname(s)

rawDescription: How to get names in string?

description: Get names in string


code:
message = (
    'From: me@yahoo.com \n'
    'To: %s \n'
    'Subject: Data saved successfully \n'
     '%s' %(args.r,TEXT)
)

rawDescription: Emails generated in loop not sending subject


code:
import maya.cmds as cmds
from functools import partial

#You need to add the *args at the end of your function
def superExtrude(extrScaleField, extrDistField, *args):
    """Loops through a list of selected meshes and extrudes all of the mesh faces to produce a polygon frame, based on existing mesh tesselations"""
    myObjectLt = cmds.ls(selection=True)

    #In the function, we are passing the floatFields, not their values.
    #So if we want to query the value before running the script, we need to
    #use the floatField cmds with the "query" flag

    extrScale = cmds.floatField(extrScaleField, q=1, v=1)
    extrDist = cmds.floatField(extrDistField, q=1, v=1)

    for i in range(len(myObjectLt)):
        numFaces = cmds.polyEvaluate(face=True)
        item = myObjectLt[i] + ".f[:]"
        cmds.select(clear=True)
        cmds.select(item, replace=True)

        #extrude by scale
        cmds.polyExtrudeFacet(constructionHistory=True, keepFacesTogether=False, localScaleX=extrScale, localScaleY=extrScale, localScaleZ=extrScale)
        selFaces = cmds.ls(selection=True)
        cmds.delete(selFaces)

        #extrude by height
        cmds.select(item, replace=True)
        cmds.polyExtrudeFacet(constructionHistory=True, keepFacesTogether=True, localTranslateZ=extrDist)

def extrWindow():
    """Creates the user interface UI for the user input of the extrusion scale and height"""
    windowID = "superExtrWindow"

    if cmds.window(windowID, exists=True):
        cmds.deleteUI(windowID)

    cmds.window(windowID, title="SuperExtrude", sizeable=False, resizeToFitChildren=True)
    cmds.rowColumnLayout(numberOfColumns=2, columnWidth=[(1,120),(2,120)], columnOffset=[1,"right",3])

    cmds.text(label="Extrusion Scale:")

    # There were an error here, replace 'text' with 'value'
    # to give your floatField a default value on its creation

    extrScaleVal = cmds.floatField(value=0.9)
    cmds.text(label="Extrusion Height:")
    extrDistVal = cmds.floatField(value=-0.3)
    cmds.separator(height=10, style="none")
    cmds.separator(height=10, style="none")
    cmds.separator(height=10, style="none")

    # As said above, use the partial function to pass your arguments in the function
    # Here, the arguments are the floatFields names, so we can then query their value
    # everytime we will press the button.

    cmds.button(label="Apply", command=partial(superExtrude,extrScaleVal, extrDistVal))
    cmds.showWindow(windowID)

extrWindow()

rawDescription: Maya Python: cmds.button( ) with UI passing variables and calling a function?


code:
# models.py
from django.db import models
from django.contrib.auth import models as auth_models

class MagazineUser(models.Model):
    user = models.ForeignKey(auth_models.User, unique=True)

class Magazine(models.Model):
    managers = models.ForeignKey(MagazineUser)

# admin.py

class MagazineAdmin(admin.ModelAdmin):
    def queryset(self, request):
        qs = super(admin.ModelAdmin, self).queryset(request)

        if request.user.is_superuser:
            return qs

        user_qs = MagazineUser.objects.filter(user=request.user)
        return qs.filter(managers__in=user_qs)

rawDescription: How Can I Allow Object Editing in the Django Admin For Specific Objects ONLY?


code:
import numpy as np

with open('numbers.txt', 'w') as handle:
    for n in np.arange(1, 5, 0.1):
        handle.write('{}\n'.format(n))

rawDescription: How to create a range of numbers with a given increment

description: Create a range of numbers with a given increment


code:
sys.stdout = dis_output_file
dis.dis(method1)
dis.dis(method2)
sys.stdout = sys.__stdout__

rawDescription: python: Selective redirection of stdout to a file

description: Python: Selective redirection of stdout to a file


code:
def fileList():
    matches = []
    for root, dirnames, filenames in os.walk(source):
        for filename in filenames:
            if filename.endswith(('.mov', '.MOV', '.avi', '.mpg')):
                matches.append(os.path.join(root, filename))
    return matches

rawDescription: Use Python os.walk to identify a list of files


code:
from django.contrib.auth.models import User

class Employee(models.Model):
    user = models.OneToOneField(User)
    department = models.CharField(max_length=100)

rawDescription: Permission in Django across native to custom table


rawDescription: How can I go about splicing a string to create new strings only using a part of the last string?

description: Go about splicing a string to create new strings only using a part of the last string


code:
response = self.client.get(reverse("home"))
self.assertContains(response, 'Login' % reverse("userlogin"), html=True)
...

rawDescription: Testing for links in a page content in Django


code: return redirect('new_cross_phase_2')

rawDescription: Moving Between Pages

description: Moving Between Pages


code:
from axel import Event

async def _wait_for_provider_up(self, data_provider: Provider, ms_timeout: int) -> bool:
    if provider.State == ProviderState.Connected:
        return True
    if provider.State == ProviderState.Faulted:
        return False
    taskCompletion = asyncio.Future()

    def onStateChanged(sender, e: ProviderStateChangedEventArgs):
        if e.State == ProviderState.Connected:
            taskCompletion.set_result(True)
        elif e.State == ProviderState.Faulted:
            taskCompletion.set_result(False)

    provider.StateChanged += onStateChanged
    try:
        if provider.State == ProviderState.Connected:
            return True
        elif provider.State == ProviderState.Faulted:
            return False
        try:
            result = await asyncio.wait_for(taskCompletion, timeout=ms_timeout/1000)
            if not result or provider.State != ProviderState.Connected:
                return False
        except asyncio.TimeoutError:
            return False
    finally:
        provider.StateChanged -= onStateChanged
    return True

rawDescription: What would be a Python equivalent of TaskCompletionSource<T> (.NET)?


code:
from collections import Counter
c = Counter([(0,0,1), (1,1,1), (1,1,1), (1,0,1)])
c

rawDescription: Efficient way to count unique elements in array in numpy/scipy in Python


code:
import numpy as np
a = np.eye(2, dtype='S17')
a
np.char.decode(a, 'ascii')

rawDescription: Storing string in numpy matrix

description: Storing string in numpy matrix


code:
import base64, StringIO, zipfile

# base64 string from the message
attachment = '...'
attachment = base64.b64decode(attachment)
attachment = StringIO.StringIO(attachment)

zipFile = zipfile.ZipFile(attachment, 'r')

rawDescription: Python 2.7 - Extract Zip From Email Message File


code:
x= float(input("enter a number between 0 and 1: "))
y= float(input("enter another number between 0 and 1: "))

rawDescription: writing an improved version of the Chaos Help

description: Writing an improved version of the Chaos Help


code:
by_sale = {'Spring Savings 0413' : {1 : 3000, 2: 2000, 4:1000}, 'Back to School 0812' : {1: 4000, 3:3000, 4:2000}}

from collections import defaultdict

by_day = defaultdict(dict)
for sale, sale_info in by_sale.iteritems():
    running_total = 0
    for i in range(1,5):
        daily_amount = sale_info.get(i,0)
        running_total += daily_amount
        by_day[i].update({sale:running_total})

dict(by_day)

rawDescription: How to insert key/value pairs into a nested dict without overwriting?

description: Insert key/value pairs into a nested dict without overwriting


code:
shops = dict()
shops['foodmart'] = [12.33,5.55]
shops['nike'] = [42.20,69.99]
shops['gas_station'] = [0.89,45.22]
shops['toy_store'] = [10.99,15.32]
max(shops, key = lambda k:sum(shops[k]))

rawDescription: How to get the list with the higher sum of its elements

description: Get the list with the higher sum of its elements


code:
reason = form.cleaned_data['reason']
reason = dict(form.fields['reason'].choices)[reason]

rawDescription: How to get the label of a choice in a Django forms ChoiceField?

description: Get the label of a choice in a Django forms ChoiceField


code:
from BeautifulSoup import BeautifulSoup
from urllib2 import urlopen
soup = BeautifulSoup(urlopen('http://www.reference.com/wordoftheday'))
soup.find('h2').text

rawDescription: (python) Parsing certain HTML output tags using beautiful soup


code:
text = 'Python: Cut of the last word of a sentence?'
text.rsplit(' ', 1)[0]

rawDescription: Python: Cut off the last word of a sentence?


code:
import numpy as np
from scipy.interpolate  import griddata

# resample onto a 50x50 grid
nx, ny = 50, 50

# (N, 2) arrays of input x,y coords and u,v values
pts = np.vstack((x, y)).T
vals = np.vstack((u, v)).T

# the new x and y coordinates for the grid, which will correspond to the
# columns and rows of u and v respectively
xi = np.linspace(x.min(), x.max(), nx)
yi = np.linspace(y.min(), y.max(), ny)

# an (nx * ny, 2) array of x,y coordinates to interpolate at
ipts = np.vstack(a.ravel() for a in np.meshgrid(yi, xi)[::-1]).T

# an (nx * ny, 2) array of interpolated u, v values
ivals = griddata(pts, vals, ipts, method='cubic')

# reshape interpolated u,v values into (ny, nx) arrays
ui, vi = ivals.T
ui.shape = vi.shape = (ny, nx)

# plot
fig, ax = plt.subplots(1, 1)
ax.hold(True)
ax.streamplot(xi, yi, ui, vi)
ax.quiver(x, y, u, v)

rawDescription: How to use streamplot function when 1D data of x-coordinate, y-coordinate, x-velocity and y-velocity are available?

description: Use streamplot function when 1D data of x-coordinate, y-coordinate, x-velocity and y-velocity are available


code:
line = 'name: a: b: c: d\n'
pieces = line.split(':')
name = pieces[0]
tup = tuple(x.strip() for x in pieces[1:])

rawDescription: File line to dictionary of tuples

description: File line to dictionary of tuples


code:
from twisted.internet import reactor
from twisted.web import static, server, twcgi

root = static.File("/root")
root.putChild("cgi-bin", twcgi.CGIDirectory("/var/www/cgi-bin"))
reactor.listenTCP(80, server.Site(root))
reactor.run()

rawDescription: Twisted execute python file

description: Twisted execute python file


code:
def BL2TRI(BL):
    d = {}
    tri = np.zeros((len(BL),3), dtype=np.int)
    c = 0
    for i in BL:
        if(i[0] > i[1]):
            t = i[0]
            i[0] = i[1]
            i[1] = t
        if(i[0] in d):
            d[i[0]].append(i[1])
        else:
            d[i[0]] = [i[1]]
    for key in d:
        for n in d[key]:
            for n2 in d[key]:
                if (n>n2) or n not in d:
                    continue
                if (n2 in d[n]):
                    tri[c,:] = [key,n,n2]
                    c += 1
    return tri[0:c]

rawDescription: Efficient bond list to triangulation conversion? (python)


code:
import multiprocessing.pool as mpool

def worker(task):
    # work on task
    print(task)     # substitute your migration code here.

# create a pool of 10 threads
pool = mpool.ThreadPool(10)
N = 100

for task in range(N):
    pool.apply_async(worker, args = (task, ))

pool.close()
pool.join()

rawDescription: Python queues - have at most n threads running


code:
import base64
png_data = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
with open('/tmp/red.png', 'w') as fp:
    fp.write(base64.b64decode())

rawDescription: Download a data URI using Python

description: Download a data URI using Python


code:
import urllib
import re

newsymbolslist = ["NFLX", "GOOG", "VNR", "AAPL", "SKY"]

i=0
while i<len(newsymbolslist):
    url = ("http://www.nasdaq.com/symbol/" +newsymbolslist[i]+ "/real-time")
    htmlfile = urllib.urlopen(url)
    htmltext = htmlfile.read()
    regex = '(.+?)'
    pattern = re.compile(regex)
    price = re.findall(pattern,htmltext)

    print (newsymbolslist[i] + " is: " + str (price))

    i+=1

rawDescription: Index out of range with different lists

description: Index out of range with different lists


code:
import random
import matplotlib.pyplot as pyplot

pyplot.ion()

x = range(10)
y = lambda m: [m*random.random() for i in range(10)]

pLine, = pyplot.plot(x, y(1))

for i in range(10):
    pLine.set_ydata(y(i+1))

ax = pyplot.gca()

# recompute the ax.dataLim
ax.relim()
# update ax.viewLim using the new dataLim
ax.autoscale_view()
pyplot.draw()

rawDescription: Automatically Rescale ylim and xlim in Matplotlib


code:
def warshall(a):
    assert (len(row) == len(a) for row in a)
    n = len(a)
    for k in range(n):
        for i in range(n):
            for j in range(n):
                a[i][j] = a[i][j] or (a[i][k] and a[k][j])
    return a

rawDescription: Warshall's Algorithm for Transitive Closure(Python)


code:
request = self.factory.get('/my_endpoint', HTTP_AUTHORIZATION='Token {}'.format(self.token))
force_authenticate(request, user=self.user)

rawDescription: What's the proper way to test token-based auth using APIRequestFactory?


code:
def array11(arr, index):
    if index == len(arr):
        return 0
    return (arr[index] == 11) + array11(arr, index + 1)

rawDescription: computing the occurences of a number in a list with recursion

description: Computing the occurences of a number in a list with recursion


code:
all_children = response.xpath('//*') # Mathches everything in that div
key_children = response.xpath('//h3') #Matches the h3 nodes
spec_dict = dict()
sp_dict = dict()
title_count = 0
spec_count = 0
spec_key = None
while spec_count < len(all_children):
    if title_count < len(key_children) and all_children[spec_count].extract() == key_children[title_count].extract(): # so its the same node in all_children and key_children
        spec_key = key_children[title_count].xpath('normalize-space(./text())').extract()[0]
        title_count += 1
    else:
        if spec_key:
            key = all_children[spec_count].xpath('normalize-space(./label/text())').extract()[0]
            val = all_children[spec_count].xpath('normalize-space(./span/text())').extract()[0]
            sp_dict[key] = val
            spec_dict[spec_key] = sp_dict
    spec_count += 1

rawDescription: Scrapy folllowing sibling confusion?

description: Scrapy folllowing sibling confusion


code:
import re
re.sub(r'\bn\b', 'REPL', 'sin(2 pi*n d)')

rawDescription: Python: How to replace string enclosed by certain characters


code:
def testColourMap(cmap):
    sp = SubplotParams(left=0., bottom=0., right=1., top=1.)
    fig = Figure((2.5,.2), subplotpars = sp)
    canvas = FigureCanvas(fig)
    ax = fig.add_subplot(111)
    gradient = np.linspace(0, 1, 256)
    gradient = np.vstack((gradient, gradient))
    ax.imshow(gradient, aspect=10, cmap=cmap)
    ax.set_axis_off()
    canvas.draw()
    size = canvas.size()
    width, height = size.width(), size.height()
    im = QImage(canvas.buffer_rgba(), width, height, QImage.Format_ARGB32)
    return QPixmap(im)

rawDescription: Python - matplotlib - PyQT: plot to QPixmap


code:
def insort(a, b, kind='mergesort'):
    # took mergesort as it seemed a tiny bit faster for my sorted large array try.
    c = np.concatenate((a, b)) # we still need to do this unfortunatly.
    c.sort(kind=kind)
    flag = np.ones(len(c), dtype=bool)
    np.not_equal(c[1:], c[:-1], out=flag[1:])
    return c[flag]

rawDescription: combine two arrays and sort

description: Combine two arrays and sort


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

options.FORMFIELD_FOR_DBFIELD_DEFAULTS[models.URLField] = {
                                           'widget': widgets.TextInput}

rawDescription: Changing default django admin widget site wide


code:
def tile_coordinates(i, j, tile_size):
    return slice(i, i + tile_size), slice(j, j + tile_size)

rawDescription: Indexing in numpy ndarray by dynamcaly generated indexes


code:
input_file = open('f1', 'rb')
contents = input_file.read()

#....
data = contents[offset+8:size+offset] #for example

output_file = open('f2', 'wb')
output_file.write(data)

rawDescription: Reading and writing binary data of dynamic sizes question


code:
import turtle
import random

def isInScreen(w,t):
    leftBound = w.window_width() / -2.0
    rightBound = w.window_width() / 2.0
    bottomBound = w.window_height() / -2.0
    topBound = w.window_height() / 2.0

    turtlex = t.xcor()
    turtley = t.ycor()

    if turtlex < leftBound or turtlex > rightBound or turtley < bottomBound or turtley > topBound:
        return False

    return True

def randomWalk(t,w):
    counter = 0

    while True:
        while isInScreen(w,t):
            coin = random.randrange(0,2)
            if coin == 0:
                t.left(90)
            else:
                t.right(90)
            t.forward(50)
        t.left(180)
        t.forward(50)
        counter += 1
        if counter == 4:
            break

wn = turtle.Screen()
wn.bgcolor('lightcyan')

steklovata = turtle.Turtle()
steklovata.color('darkslategray')
steklovata.shape('turtle')

randomWalk(steklovata,wn)

wn.exitonclick()

rawDescription: Randomly walking turtle function not doing what I want it to


code:
a = ('helloWorld', 20)
a[1]

rawDescription: Python utilizing split lists

description: Python utilizing split lists


code:
import pickle

s = pickle.dumps({'obj0': obj0, 'obj1': obj1, 'obj2': obj2})

obj1 = pickle.loads(s)['obj1']

rawDescription: Restore objects from pickled file by name in python


code:
for i, e in enumerate(L):
    print(e, L[i-1], L[(i+1) % len(L)])

rawDescription: How to loop over a circular list, while peeking ahead and behind current element?

description: Loop over a circular list, while peeking ahead and behind current element


rawDescription: Using a RegEx to match IP addresses in Python


code:
def __init__(self, colorList):
    for color in colorList:
        blobSpec       = getattr(blobLib, color)
        blob           = blobSpec()    # create an instance of the blob
        blob.Obj       = blob.returnObj(self.page)
        setattr(self, color, blob)

rawDescription: Instantiate an object named in a variable


code:
import marshal
import py_compile
import time
import ast

codeobject = compile(ast.parse('print "Hello World"'), '<string>', 'exec')

with open('output.pyc', 'wb') as fc:
    fc.write('\0\0\0\0')
    py_compile.wr_long(fc, long(time.time()))
    marshal.dump(codeobject, fc)
    fc.flush()
    fc.seek(0, 0)
    fc.write(py_compile.MAGIC)

rawDescription: Generate .pyc from Python AST?

description: Generate .pyc from Python AST


code:
s = ttk.Style()
s.configure('my.TButton', font=('Helvetica', 12))
b = ttk.Button(mainframe, text='Press me', style='my.TButton',
command=foo)

rawDescription: How to change font size in ttk.Button?

description: Change font size in ttk.Button


code:
data = {}
for name, age, location, phone in csv_data:
    data[name, age] = (name, age, location, phone)

rawDescription: csv unique values without considering a column

description: Csv unique values without considering a column


rawDescription: Finding the average of columns from nested lists in a dictionary's value


code:
import time
lastsave = 0
def SaveEvent(data):
    # do what you need to save it
    ...

# I'm assuming there's some sort of event loop that
# detects new events and directs them to some HandleEvent function

def HandleEvent(data):
    global lastsave
    if time.time() - lastsave > 300:
    # this is in seconds, so 5 minutes = 300 seconds
        lastsave = time.time()
        SaveEvent(data)

rawDescription: Python time counter

description: Python time counter


code:
class Serie(Base):
    __tablename__ = 'serie'
    id = Column(Integer, primary_key=True)
    record_tables = Column(String(255))

    @property
    def get_records(self):
        sess = object_session(self)
        tables = [x.strip() for x in self.record_tables.split(',')]
        raw_query = '\nUNION ALL\n'.join(
            'SELECT * FROM ' + table for table in tables)
        results = sess.execute(raw_query).fetchall()
        return results

rawDescription: Build query from list of table names with unknown length using SQLAlchemy and Postgresql


code:
import urllib2

url = 'https://www.dropbox.com/s/splz3vk9pl1tbgz/test.txt?dl=0'
user_agent = 'Mozilla 5.0 (Windows 7; Win64; x64)'
file_name = "test.txt"
u = urllib2.Request(url, headers = {'User-Agent' : user_agent})

# Actually make the request
req = urllib2.urlopen(u)

f = open(file_name, 'wb')

# Read data from the request, and write it to the file
f.write(req.read())

f.close()

rawDescription: Python Download files, but they are empty?


code:
from Tkinter import Tk, Canvas

start = None

def onclick_handler(event):
    global start
    start = (event.x, event.y)

def onrelease_handler(event):
    global start
    if start is not None:
        x = start[0]
        y = start[1]
        event.widget.create_line(x, y, event.x, event.y)
        start = None

master = Tk()
canvas = Canvas(master, width=200, height=200)
canvas.bind("<Button-1>", onclick_handler)
canvas.bind("<ButtonRelease-1>", onrelease_handler)
canvas.pack()
master.mainloop()

rawDescription: Python tkinter Canvas Dynamic Create.Line


code:
import itertools
map(''.join, itertools.product(*letters))

rawDescription: How can I find all the possible combinations of a list of lists (in Python)?

description: Find all the possible combinations of a list of lists (in Python)


code:
stack = [(0, {})]  # indentation level, top-level entry
entry = stack[-1][1]

for line in input:
    line = line.strip()
    if not line: continue

    indentation = len(input) - len(input.lstrip())
    if indentation > stack[-1][0]:  # indented further? New entry
        entry = stack[-1][1]['data'] = {}
        stack.append((indentation, entry)) # push
    else:
        while indentation < stack[-1][0]:  # indentation dropped
            del stack[-1]       # pop
            entry = stack[-1][1]

    # process line and add to entry

result = stack[0][1]

rawDescription: Parsing space indented data

description: Parsing space indented data


code:
def find_odds(numbers):
  if not numbers:
    return []
  if numbers[0] % 2 == 1:
    return [numbers[0]] + find_odds(numbers[1:])
  return find_odds(numbers[1:])

rawDescription: How can I return the odd numbers of a list, using only recursion in Python?

description: Return the odd numbers of a list, using only recursion in Python


code:
import pandas as pd
df = Series(randn(500), index=pd.date_range('2012-01-01', periods=500))
s2 = df.groupby([lambda x: x.year, lambda x: x.month]).sum()
s2

rawDescription: Summing over months with pandas

description: Summing over months with pandas


code:
driver.find_element_by_id("login_opener").click()
time.sleep(5)  # TODO: replace with an Explicit Wait

frame = driver.find_element_by_css_selector("iframe[src*=InlineLogin]")
driver.switch_to.frame(frame)

# now while you are in the iframe, search for the input

rawDescription: Logging into Zillow

description: Logging into Zillow


code:
import sys, time
from subprocess import call

from django.conf import settings
from django.utils import autoreload

from django.core.management.commands.runserver import Command

def convert_less():
    status = call([settings.CONVERT_LESS, settings.LESS_DIR, settings.CSS_DIR])
    if status != 0:
        exit(1)

def reloader_thread():
    autoreload.ensure_echo_on()
    while autoreload.RUN_RELOADER:
        convert_less()
        if autoreload.code_changed():
            sys.exit(3) # force reload
        time.sleep(1)

autoreload.reloader_thread = reloader_thread

rawDescription: Django running bash script when less file is modified


code:
for n in itertools.product(*pool):
   print(n)

rawDescription: Working with itertools.product and lists in python 3


rawDescription: How to sum up all the numeric values in Pandas Data Frame to yield one value

description: Sum up all the numeric values in Pandas Data Frame to yield one value


code:
s = "this is my really, really, really, really, really, really," \
    " really long string that I'd like to shorten."

rawDescription: PEP8 - 80 Characters - Big Strings

description: PEP8 - 80 Characters - Big Strings


code:
import pandas as pd
df = pd.read_csv('input.txt', header=None, skiprows=1, delim_whitespace=True)
df = df[df[0] != '*']  #filter out comment rows
df = df.convert_objects(convert_numeric=True)
df

rawDescription: read float values from ASCII file with variable number of columns

description: Read float values from ASCII file with variable number of columns


code:
from collections import defaultdict

class Index:
    def __init__(self, stringlist):
        self.index = defaultdict(list)
        for string in stringlist:
            for word in string.split():
                self.index[word].append(string)

    def strings_containing(self, word):
        return self.index[word]

rawDescription: Searching for strings in a list which contain a specific word


code:
@lru_cache()
def create_list():
    sublist1 = [1,2,3]
    sublist2 = [4,5,6]
    list_of_lists=[sublist1,sublist1,sublist2]
    return list_of_lists
l = create_list(); l
l[0].append(4); l
create_list()

rawDescription: In python, returning an object created in a function body makes a deep copy of it?


code: df.T.plot(kind='scatter', x=4, y=1)

rawDescription: Create scatterplot in pandas using row as index and row as data


code: subsampled = df.ix[(choice(x) for x in grouped.groups.itervalues())]

rawDescription: Using pandas, how do I subsample a large DataFrame by group in an efficient manner?


code:
#!/usr/bin/env python3

import tkinter as tk
from tkinter.font import Font

class Pad(tk.Frame):

    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)

        self.toolbar = tk.Frame(self, bg="#eee")
        self.toolbar.pack(side="top", fill="x")

        self.bold_btn = tk.Button(self.toolbar, text="Bold", command=self.make_bold)
        self.bold_btn.pack(side="left")

        self.clear_btn = tk.Button(self.toolbar, text="Clear", command=self.clear)
        self.clear_btn.pack(side="left")

        # Creates a bold font
        self.bold_font = Font(family="Helvetica", size=14, weight="bold")

        self.text = tk.Text(self)
        self.text.insert("end", "Select part of text and then click 'Bold'...")
        self.text.focus()
        self.text.pack(fill="both", expand=True)

        # configuring a tag called BOLD
        self.text.tag_configure("BOLD", font=self.bold_font)

    def make_bold(self):
        # tk.TclError exception is raised if not text is selected
        try:
            self.text.tag_add("BOLD", "sel.first", "sel.last")
        except tk.TclError:
            pass

    def clear(self):
        self.text.tag_remove("BOLD",  "1.0", 'end')

def demo():
    root = tk.Tk()
    Pad(root).pack(expand=1, fill="both")
    root.mainloop()

if __name__ == "__main__":
    demo()

rawDescription: How to change the color of certain words in the tkinter text widget?

description: Change the color of certain words in the tkinter text widget


code:
a,b = 0, 1
while b < 1000:
    print(b)
    a, b = b, a+b

rawDescription: Result of "b (None,)" on the Fibonacci sequence of Informal Introduction to Python


code:
result = MyTask.AsyncResult(task_id)
result.get()

rawDescription: Retrieve a task result object, given a `task_id` in Celery


code:
punctuation_i_care_about="?.!"
split_by_punc =  re.split("[%s]"%punctuation_i_care_about, some_big_block_of_text)
words_by_puct = [len(x.split()) for x in split_by_punc]

rawDescription: Counting number of words between punctuation characters in Python


code:
import numpy as np
test=np.asarray([[10,15,20,50],[30,40,nan,70],[nan,10,nan,25],[100,50,nan,60]])
test=np.ma.masked_invalid(test)

xi = np.arange(-66,-70,-1)
yi = np.arange(40.5,44.5,1)
xxb,yyb = np.meshgrid(xi, yi)

c=pcolor(xxb,yyb,test)
colorbar(c)

rawDescription: Plot array content in grid

description: Plot array content in grid


code:
import glob
import os.path
depth2 = glob.glob('*/*')
depth2 = filter(lambda f: os.path.isdir(f), depth2)

rawDescription: Recurse through selected level of subdirectories


code:
@detail_route(url_path='mypath')
def myapi(self, request, **kwargs):
    """
    Endpoint documentation.
    ---
        parameters_strategy: replace
        parameters:
            - name: pk
              description: "Primary Key"
              required: true
              type: string
              paramType: path
    """

rawDescription: How can I document the pk parameter (or other path parameters) with Django REST Swagger?

description: Document the pk parameter (or other path parameters) with Django REST Swagger


code:
array = [['string', '2.0', 'string'],
         ['string', '3.0', 'string']]

y = [ [float(x) if i%2 == 1 else x for i,x in enumerate(row)] for row in array]

print(y)

rawDescription: Changing strings to float variables in a 2d array


code:
def func(row):
    if row['mobile'] == 'mobile':
        return 'mobile'
    elif row['tablet'] =='tablet':
        return 'tablet'
    else:
        return 'other'

df['combo'] = df.apply(func, axis=1)

rawDescription: Create Column with ELIF in Pandas

description: Create Column with ELIF in Pandas


code:
import csv

Pt_names = []
Hz_angles = []
Vz_angles = []
Slope_Dists = []
Reference_Hts = []
Instrument_Heights = []
with open(csv_file, 'r') as fh:
    reader = csv.reader(fh)
    for row in reader:
        Pt_name = row[0]
        # list comprehension for float conversion
        Hz_angle, Vz_angle, Slope_Dist, Reference_Ht, Instrument_Height = [float(value) for value in row[1:]]
        Pt_names.append(Pt_name)
        Hz_angles.append(Hz_angle)
        Vz_angles.append(Vz_angle)
        Slope_Dists.append(Slope_Dist)
        Reference_Hts.append(Reference_Ht)
        Instrument_Heights.append(Instrument_Height)

rawDescription: Create different arrays from csv file using python


code:
@register.filter
def split_to_4_columns(queryset):
    values = list(queryset.all())
    split = int(ceil(len(values)/4.))
    columns = [values[i*split:(i+1)*split] for i in range(4)]
    return columns

rawDescription: Django - queryset split to 4, causes lots of hits to the database


code: A = P*D.sum(axis=1)-D.dot(P)

rawDescription: How do I vectorize this expression with Python numpy?

description: Vectorize this expression with Python numpy


code:
class TrackView(View):
    def get(self, request):
        try:
            track,_ = Track.objects.get_or_create(domain = request.GET['domain'])
            track.url_name = request.GET['url']
            track.title = request.GET['title']
            track.ip = request.GET['ip']
            track.referrer = request.GET['referrer']
            track.save()
            return HttpResponse(status=200)
        except KeyError:
            return HttpResponse(status=400)

rawDescription: How to store URL parameters in GET requests in django?

description: Store URL parameters in GET requests in django


rawDescription: Python string manipulation using xrange

description: Python string manipulation using xrange


code:
import numpy as np
import matplotlib.pylab as pl

pl.figure()
ax=pl.subplot(111)
for i in range(10):
    # 1. set all lines to a black color
    for l in ax.get_lines():
        l.set_color('k')

    # 2. plot the latest one in a red color
    pl.plot(np.arange(10), np.random.random(10), color='r')

rawDescription: python interactive plot newest line in color, rest in black

description: Python interactive plot newest line in color, rest in black


code:
entity_info = get_object_or_404(Entity, pk=entity_id)
for key, value in new_dict.items():
    setattr(entity_info, key, value)

rawDescription: Add extra field to result after queryset for context rendering


code: unicode("ÄÖÜ", "utf-8")

rawDescription: vobject read vcf files containing german umlauts

description: Vobject read vcf files containing german umlauts


rawDescription: How to get a win32 handle of an open file in python?

description: Get a win32 handle of an open file in python


code:
from itertools import groupby
import xlrd
import xlwt

workbook = xlrd.open_workbook(r"e:\python temp\input.xls")
sheet = workbook.sheet_by_index(0)
rows = [sheet.row_values(row, 0) for row in xrange(sheet.nrows)]

for k, g in groupby(rows, lambda x: x[0].startswith('Question')):
    if k:
        question = next(g)
    else:
        wb = xlwt.Workbook()
        ws = wb.add_sheet('Sheet 1')

        for colx, value in enumerate(question):
            ws.write(0, colx, value)

        for rowx, row in enumerate(g, start=1):
            for colx, value in enumerate(row):
                ws.write(rowx, colx, value)

        wb.save(question[0] + '.xls')

rawDescription: Split xls file with Python

description: Split xls file with Python


code:
import subprocess

p = subprocess.Popen(["commandHere", "usernameHere"], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write(b"yourPassword\n")   # What you need to input
result = p.stdout.read()           # The program's output
print(result)

rawDescription: Python answering command prompts in terminal


code: LISTC = [el for el in LISTA if el not in set(LISTB)]

rawDescription: how to maintain the order of elements in a list?

description: Maintain the order of elements in a list


code:
while True:
    wpisane = input('>')

    #dodawanie
    if wpisane == dodawanie:
        pierwsza = input('Podaj pierwsza liczbe: ')
        druga = input('Podaj druga liczbe: ')
        wynik_dodawania = (int(pierwsza) + int(druga));
        plus = '+'
        równa_się = '='
        print()
        print(int(pierwsza), plus, int(druga), równa_się, wynik_dodawania);
        print()

    #odejmowanie
    if wpisane == odejmowanie:
        pierwsza = input('Podaj pierwsza liczbe: ')
        druga = input('Podaj druga liczbe: ')
        wynik_odejmowania = (int(pierwsza) - int(druga));
        minus = '-'
        równa_się = '='
        print()
        print(int(pierwsza), minus, int(druga), równa_się, wynik_odejmowania);
        print()

rawDescription: Loop in Python script

description: Loop in Python script


rawDescription: "cross product" but raise to exponent instead of multiply


code:
def Excel(visible=True):
    '''Get running Excel instance if possible, else
    return new instance.
    '''
    try:
        excel = win32com.client.GetActiveObject("Excel.Application")
        print("Running Excel instance found, returning object")

    except:
        excel = new_Excel(visible=visible)
        print("No running Excel instances, returning new instance")

    else:
        if not excel.Workbooks.Count:
            excel.Workbooks.Add(1)
        excel.Visible = visible

    return excel

rawDescription: Python/win32com - Check if Program is Open


code:
import re

with open('testin.csv', 'r') as reader:
    lines = [re.sub(r' *#\d+', '', line.strip()) for line in reader]

with open('testout.csv', 'w') as writer:
    writer.write('\n'.join(lines))

rawDescription: Parsing all integers in a string after the character from CSV with Python


code:
import re
import urllib

import scrapy
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors import LinkExtractor

class ProductItem(scrapy.Item):
    description = scrapy.Field()
    price = scrapy.Field()

class GrupoeroskiSpider(CrawlSpider):
    name = 'grupoeroski'
    allowed_domains = ['compraonline.grupoeroski.com']
    start_urls = ['http://www.compraonline.grupoeroski.com/supermercado/home.jsp']

    rules = [
        Rule(LinkExtractor(restrict_xpaths='//div[@class="navmenu"]'), callback='parse_categories')
    ]

    def parse_categories(self, response):
        pattern = re.compile(r'/(\d+)\-\w+')
        groups = pattern.findall(response.url)
        params = {'page': 1, 'categoria': groups.pop(0)}

        if groups:
            params['grupo'] = groups.pop(0)
        if groups:
            params['familia'] = groups.pop(0)

        url = 'http://www.compraonline.grupoeroski.com/supermercado/ajax/listProducts.jsp?' + urllib.urlencode(params)
        yield scrapy.Request(url,
                             meta={'params': params},
                             callback=self.parse_products,
                             headers={'X-Requested-With': 'XMLHttpRequest'})

    def parse_products(self, response):
        for product in response.xpath('//div[@class="product_element"]'):
            item = ProductItem()
            item['description'] = product.xpath('.//span[@class="description_1"]/text()').extract()[0]
            item['price'] = product.xpath('.//div[@class="precio_line"]/p/text()').extract()[0]
            yield item

        params = response.meta['params']
        params['page'] += 1

        url = 'http://www.compraonline.grupoeroski.com/supermercado/ajax/listProducts.jsp?' + urllib.urlencode(params)
        yield scrapy.Request(url,
                             meta={'params': params},
                             callback=self.parse_products,
                             headers={'X-Requested-With': 'XMLHttpRequest'})

rawDescription: Scrapy: Spider optimization

description: Scrapy: Spider optimization


code: valx=float(input("What is/was your original purchase price?"))

rawDescription: Displaying tax total and total using purchase price? Python programming


code:
model.setData(index, QtCore.QVariant(QtCore.Qt.AlignRight),
              QtCore.Qt.TextAlignmentRole)

rawDescription: Right alignment for table cell in pyqt

description: Right alignment for table cell in pyqt


code:
import pickle

class TypeFactory(object):
    def __init__(self):
        pass
    @staticmethod
    def create_type(name='DynamicType', dict={}):
        return type(name, (object,), dict)
    @staticmethod
    def pickle(t, fh):
        dict = t.__dict__.copy()
        name = t.__name__
        for key in dict.keys():
            if key.startswith('__') and key.endswith('__'):
                del dict[key]
        pickle.dump((name, dict), fh)
    @classmethod
    def unpickle(cls, fh):
        name, dict = pickle.load(fh)
        return cls.create_type(name, dict)

rawDescription: Python: Save dynamically created object types


rawDescription: changing value of a float in python for loop

description: Changing value of a float in python for loop


code:
def gp(ltr):
    grade = {"A":4.0, "B":3.0, "C":2.0, "D":1.0, "F":0.0}
    mod   = {"": 0.0, "+":0.3, "-":-0.3}
    return grade[ltr[0]] + mod[ltr[1:]]

letters = "B A A- C F A"
grades  = [gp(letter) for letter in letters.split()]
average = sum(grades) / len(grades)

rawDescription: New to Python - Making a GPA calculator

description: New to Python - Making a GPA calculator


code: fileObject = open("countable nouns raw.txt", "rt", encoding='utf8')

rawDescription: UnicodeDecodeError when reading a text file


code:
tempfile = request.POST['file']
file_path = 'uploaded_files/' + tempfile.filename # for the original filename
permanent_file = open( file_path, 'wb')
shutil.copyfileobj(tempfile.file, permanent_file)

rawDescription: In Python + Pylons, simple way to copy uploaded file to disk


code:
# generating some test data
timestamp = [1440540000, 1450540000]
df1 = pd.DataFrame(
    {'timestamp': timestamp, 'a': ['val_a', 'val2_a'], 'b': ['val_b', 'val2_b'], 'c': ['val_c', 'val2_c']})
# building a different index
timestamp = timestamp * np.random.randn(abs(1))
df2 = pd.DataFrame(
    {'timestamp': timestamp, 'd': ['val_d', 'val2_d'], 'e': ['val_e', 'val2_e'], 'f': ['val_f', 'val2_f'],
     'g': ['val_g', 'val2_g']}, index=index)
# keeping a value in common with the first index
timestamp = [1440540000, 1450560000]
df3 = pd.DataFrame({'timestamp': timestamp, 'h': ['val_h', 'val2_h'], 'i': ['val_i', 'val2_i']}, index=index)

# Setting the timestamp as the index
df1.set_index('timestamp', inplace=True)
df2.set_index('timestamp', inplace=True)
df3.set_index('timestamp', inplace=True)

# You can convert timestamps to dates but it's not mandatory I think
df1.index = pd.to_datetime(df1.index, unit='s')
df2.index = pd.to_datetime(df2.index, unit='s')
df3.index = pd.to_datetime(df3.index, unit='s')

# Just perform a join and that's it
result = df1.join(df2, how='outer').join(df3, how='outer')
result

rawDescription: Merging time series data by timestamp using numpy/pandas


code:
if len(set([s1, s2, s3, s4])) != 4:
    pass # not all unique

rawDescription: Check 4 strings if there are duplicates

description: Check 4 strings if there are duplicates


code:
import pandas as pd
import numpy as np

# just following your previous post to simulate your data
np.random.seed(0)
dates = np.random.choice(pd.date_range('2015-01-01 00:00:00', '2015-06-30 00:00:00', freq='1h'), 10000)
company = np.random.choice(['company' + x for x in '1 2 3 4 5'.split()], 10000)
df = pd.DataFrame(dict(recvd_dttm=dates, CompanyName=company)).set_index('recvd_dttm').sort_index()
df['C'] = 1
df.columns = ['CompanyName', '']
result = df.groupby([lambda idx: idx.month, 'CompanyName']).agg({df.columns[1]: sum}).reset_index()
result.columns = ['Month', 'CompanyName', 'counts']
pivot_table = result.pivot(index='CompanyName', columns='Month', values='counts')

colors = ["#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce",
    "#ddb7b1", "#cc7878", "#933b41", "#550b1d" ]

month = []
company = []
color = []
rate = []
for y in pivot_table.index:
    for m in pivot_table.columns:
        month.append(m)
        company.append(y)
        num_calls = pivot_table.loc[y, m]
        rate.append(num_calls)
        color.append(colors[min(int(num_calls)-2, 8)])

rawDescription: KeyError in for loop of dataframe in pandas


code:
def Grrr():
    a = 10
    def nested():
        nonlocal a
        b = 5
        a -= b
    nested()
    return a

rawDescription: python - Changing a variable with a nested function

description: Python - Changing a variable with a nested function


code:
table = soup.find('div', class_='right1').table
rows = table.find_all('tr', {"class" : re.compile('list.*')}, recursive=False)

rawDescription: Python beautifulsoup level 1 only text

description: Python beautifulsoup level 1 only text


rawDescription: Grab the last 2 of a split string, python


code:
def fraud_detect_merge(first_booth_voters, second_booth_voters):
    fraud = VoterList()
    comparisons = 0

    first_booth_voters.sort()     # if not already sorted
    second_booth_voters.sort()    # if not already sorted

    first = first_booth_voters[0]
    second = second_booth_voters[0]

    while first_booth_voters and second_booth_voters:
        comparisons += 1

        if first == second:
            fraud.append(first)
            first = first_booth_voters.pop(0)
            second = second_booth_voters.pop(0)
        elif first < second:
            first = first_booth_voters.pop(0)
        else:
            second = second_booth_voters.pop(0)

    return fraud, comparisons

rawDescription: Merge Sort 2 Lists Finding Common Elements


code:
nested = [line.strip().split(',') for line in in_file][1:]
nested = [line[:1] + [int(x) for x in line[1:]] for line in nested]

rawDescription: How to make only some strings of nested lists into integers?

description: Make only some strings of nested lists into integers


code:
db_name = 'default'
if instance._state.db is not None:
    db_name = instance._state.db
db_backend = settings.DATABASES[db_name]['ENGINE'].split('.')[-1]
MYSQL = db_backend == 'mysql'

rawDescription: Get DB Type in Django

description: Get DB Type in Django


code:
import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
from matplotlib.collections import PatchCollection
import numpy as np

xy = np.random.random((10,2))
z = np.random.random(10)

patches = [RegularPolygon((x,y), 5, 0.1) for x, y in xy]
collection = PatchCollection(patches, array=z, edgecolors='white', lw=2)

fig, ax = plt.subplots()
# So that the white edges show up...
ax.patch.set(facecolor='black')
ax.add_collection(collection)
ax.autoscale()

plt.show()

rawDescription: How to find the color white with Matploblib user-defined colormap?

description: Find the color white with Matploblib user-defined colormap


code:
class MySpider(BaseSpider):
    ...

    def parse(self, response):
         jsonresponse = json.loads(response.body_as_unicode())

         item = MyItem()
         item["firstName"] = jsonresponse["firstName"]

         return item

rawDescription: Scraping a JSON response with Scrapy

description: Scraping a JSON response with Scrapy


code:
import Tkinter as tk

class Testing(tk.LabelFrame):
    def __init__(self, parent, main):

        self.buttonWidth = 10

        self.parent=parent
        self.main = main # save the instantiating class

        tk.LabelFrame.__init__(self, self.parent,
            text="Test Operations",
            padx=10,
            pady=10
        )

        self.taskButton = tk.Button(
            self,
            text="Do A Task",
            width=self.buttonWidth,
            command=self.doATask,
        )

        self.taskButton.pack()

    def doATask(self):
        #want to execute function in Results.getResult() but don't know how
        self.main.results.getResult()  #<--what you can do

class Results(tk.LabelFrame):

    def __init__(self, parent, main):
        self.parent = parent
        self.main = main # save the instantiating class
        tk.LabelFrame.__init__(self, self.parent, text="Visual Results")

        self.resultLbl = tk.Label(self, text="Result")

        self.resultLbl.pack()

    def getResult(self):
        self.resultLbl.config(bg='yellow')

class Application(tk.Frame):

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

        self.testing = Testing(self.parent, self)
        self.results = Results(self.parent, self)

        self.testing.pack(fill=tk.X)
        self.results.pack(fill=tk.X)

if __name__ == "__main__":
    root = tk.Tk()
    root.title("Modular GUI App")

    Application(root).pack()
    root.mainloop()

rawDescription: How to access a method in one inherited tkinter class from another inherited tkinter class

description: Access a method in one inherited tkinter class from another inherited tkinter class


code:
import glob
import shutil
import os

src_dir = "your/source/dir"
dst_dir = "your/destination/dir"
for jpgfile in glob.iglob(os.path.join(src_dir, "*.jpg")):
    shutil.copy(jpgfile, dst_dir)

rawDescription: Copy all JPG file in a directory to another directory in Python?


code: is_truthy = bool

rawDescription: Pythonic way to get boolean value of object


code:
WEEKDAY_ORDER = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
def sorted_schedule(s):
    return sorted(s, key=lambda k: WEEKDAY_ORDER.index(k.split()[0]))  # <---
schedule2 = set(['Wednesday afternoon', 'Friday'])
print(sorted_schedule(schedule2))

rawDescription: Sort list, based on a full and/or partial match


code:
import pylab

pylab.plot([1,2,3],[4,5,6], label ='test')
lg = pylab.legend()
lg.get_frame().set_edgecolor('blue')
ax = pylab.axes()

for line in ax.yaxis.get_ticklines():
    line.set_color('blue')
for line in ax.xaxis.get_ticklines():
    line.set_color('blue')

for label in ax.yaxis.get_ticklabels():
    label.set_color('blue')
for label in ax.xaxis.get_ticklabels():
    label.set_color('blue')

pylab.show()

rawDescription: Python graph- change axis marker colours and legend border colours


code:
for index, param_name in enumerate(parameters):
    container = QtGui.QWidget(self)
    layout = QtGui.QHBoxLayout(container )

    hlayouts.append(container)
    labels.append(QtGui.QLabel("%s"%param_name))
    sliders.append(QtGui.QSpacerItem(10,10,hPolicy=QtGui.QSizePolicy.Expanding))
    spins.append(QtGui.QDoubleSpinBox())

    spins[index].setValue(float(values.get(param_name)))
    container.setStyleSheet("background-color:black;")

    layout.addWidget(labels[index])
    layout.addItem(sliders[index])
    layout.addWidget(spins[index])

vlayout = QtGui.QVBoxLayout(self)
for widget in hlayouts:
    vlayout.addWidget(widget)

rawDescription: Set background color of layout

description: Set background color of layout


code:
from subprocess import Popen

updater = ['/usr/bin/python', '{PATH TO}/updater_script.py', '&']
Popen(updater)

rawDescription: How to avoid process termination when parent process terminates in python

description: Avoid process termination when parent process terminates in python


code:
def animate(i):
    t = np.arange(0.0,40,dt) + i*dt
    Y = integrate.odeint(dY_dx,Y0,t)
    y1,y2 = Y.T

    line1.set_data(x, y1)
    line2.set_data(x, y2)

    time_text.set_text(time_template%t)
    return line1, line2, time_text

rawDescription: Python - Animating a numerical solution to the wave equation


code:
def round_down(num, divisor):
    return num - (num%divisor)

In [2]: round_down(19,10)
Out[2]: 10

In [3]: round_down(19,5)
Out[3]: 15

In [4]: round_down(10,10)
Out[4]: 10

rawDescription: Rounding down integers to nearest multiple


code:
def main():
   import my_module # import ONLY if main is called
   # code

rawDescription: Importing package only when main is run in python package


code:
def on_file_activate(self, widget, event):
    if event.button != 1:
        return False  #only intercept left mouse button
    md = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, "herp derp, I only needed one click")
    md.run()
    md.destroy()
    return True

rawDescription: Python GTK menu item takes two clicks to activate


code:
delay = 0.5
while True:
    print("Loading."),
    sys.stdout.flush()
    time.sleep(delay)
    print("\b."),
    sys.stdout.flush()
    time.sleep(delay)
    print("\b."),
    sys.stdout.flush()
    time.sleep(delay)
    print("\r                        \r"),
    sys.stdout.flush()
return

rawDescription: Only one process prints in unix, multiprocessing python


code: w = min(MAXVAL, max(MINVAL,SLOPE*shape[0]))

rawDescription: How can I apply a function to an image proportionally to his size in python?

description: Apply a function to an image proportionally to his size in python


code:
output = io.StringIO()
output.write('x,y\n')
output.write('1,2\n')
output.seek(0)
pd.read_csv(output)

rawDescription: StringIO and pandas read_csv

description: StringIO and pandas read_csv


rawDescription: Matplotlib: Color bar not working as it should?


code:
def seasons(row):
    return row['Year'] % 2011 * 4 + row['Season']

df.reset_index(inplace=True)
df['seasons'] = df.apply(seasons, axis=1)
df.set_index(['Season', 'Year', 'Location'], inplace=True)

rawDescription: Unique IDs for sorted groups of indices

description: Unique IDs for sorted groups of indices


code:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

X, Y = np.mgrid[:2*np.pi:0.2,:2*np.pi:0.2]
U = np.cos(X)
V = np.sin(Y)

fig, ax = plt.subplots(1,1)
Q = ax.quiver(X, Y, U, V, pivot='mid', color='r', units='inches')

ax.set_xlim(-1, 7)
ax.set_ylim(-1, 7)

def update_quiver(num, Q, X, Y):
    """updates the horizontal and vertical vector components by a
    fixed increment on each frame
    """

    U = np.cos(X + num*0.1)
    V = np.sin(Y + num*0.1)

    Q.set_UVC(U,V)

    return Q,

# you need to set blit=False, or the first set of arrows never gets
# cleared on subsequent frames
anim = animation.FuncAnimation(fig, update_quiver, fargs=(Q, X, Y),
                               interval=10, blit=False)

plt.show()

rawDescription: Plotting animated quivers in Python

description: Plotting animated quivers in Python


code:
# setup x and y ranges and precision
xx = np.arange(-0.5,5.5,0.01)

max_tolerance = 100.
min_tolerance = -100.

yy = 1/(xx-2)
yy[yy > max_tolerance] = np.inf
yy[yy < min_tolerance] = -np.inf

ax.plot(xx, yy, zorder=100, linewidth=3, color='red')

rawDescription: Graphing any rational functions in python considering the asymptotes


rawDescription: Querying multiple Django models

description: Querying multiple Django models


code:
def new_user(name, password):
    salt = uuid.uuid4().hex
    hash = hashlib.sha512(password + salt).hexdigest()
    c.execute("INSERT INTO Credentials(usernameDB,passhashDB,salt) VALUES(?,?,?)",
              (name, hash, salt))

def check_login(name, password):
    c.execute("SELECT salt, passhashDB FROM Credentials WHERE usernameDB = ?",
              (name,))
    for row in c:
        salt = row[0]
        actual_hash = row[1]
        input_hash = hashlib.sha512(password + salt).hexdigest()
        if input_hash != actual_hash:
            raise Exception("invalid password")
    else:
        raise Exception("invalid user name")

rawDescription: Salt initializes differently each time server starts, trouble with credentials database in python bottle and sqlite3


code:
st='''\
##########
#####----#
##--#----#
##--#----#
##--#----#
##--#----#
#####----#
##########
-####----#
########## '''

def max_size(mat, taken):
    """Find the largest X or a square not taken in the matrix `mat`."""
    nrows, ncols = len(mat), len(mat[0])
    assert nrows==ncols
    dirs=(0,1),(1,0),(1,1) # right, down, right and down
    counts = [[0]*ncols for _ in range(nrows)]
    for i in reversed(range(nrows)):     # for each row
        assert len(mat[i]) == ncols      # matrix must be rectangular
        for j in reversed(range(ncols)): # for each element in the row
            if mat[i][j] != taken:
                if i < (nrows - 1) and j < (ncols - 1):
                    add=1+min(counts[i+x][j+y] for x,y in (dirs))
                else:
                    add=1      # edges
                counts[i][j]=add

    for line in counts: print(line)
    return max(c for rows in counts for c in rows)   # max X (or Y) number elements

table=[[c for c in s.strip()] for s in st.splitlines()]

print (max_size(table,'#')**2)

rawDescription: counting number of rectangles in a binary matrix

description: Counting number of rectangles in a binary matrix


code:
#!/usr/bin/python
import gtk

class PyApp(gtk.Window):

    def __init__(self):
        super(PyApp, self).__init__()

        """ Window - Background embed """
        button_rc = """
        pixmap_path "/var/tmp"
        # Button - image apply
        style "window" {
          bg_pixmap[NORMAL] = "menu/keyboard_new.png"
        }

        style "deButton" {
          fg[PRELIGHT] = { 0, 1.0, 1.0 }
          bg[PRELIGHT] = { 0, 0, 1.0 }
          bg[ACTIVE] = { 1.0, 0, 0 }
          fg[ACTIVE] = { 0, 1.0, 0 }
          bg[NORMAL] = { 1.0, 1.0, 0 }
          fg[NORMAL] = { .99, 0, .99 }
          bg[INSENSITIVE] = { 1.0, 1.0, 1.0 }
          fg[INSENSITIVE] = { 1.0, 0, 1.0 }
          engine "pixmap" {
              image {
                  function = BOX
                  file     = "menu/commonbutton1.png"
                  stretch  = TRUE
              }
          }
          bg_pixmap[NORMAL] = "menu/commonbutton1.png"
        }
        # imports
        widget "*.deButton" style "deButton"
        widget_class "*GtkButton*" style "button"
        widget_class "GtkWindow" style "window"
        """

        self.set_title("Keyboard")
        self.set_size_request(600, 300)
        self.set_position(gtk.WIN_POS_CENTER)
        gtk.rc_parse_string(button_rc)  ##<<<<< include the design
        vbox = gtk.VBox(False, 2)

        fix  = gtk.Fixed() ## YumYumYum
        """ Row 1 """
        fix.put( gtk.Button("1") , 20 , 20 )
        fix.put( gtk.Button("2") , 80 , 20 )
        fix.put( gtk.Button("3") , 150 , 20 )
        """ Row 2 """
        fix.put( gtk.Button("Q") , 20 , 80 )
        """ Row 3 """
        fix.put( gtk.Button("A") , 60 , 130 )
        """ Row 4 """
        fix.put( gtk.Button("Z") , 60 , 190 )
        """ Row 5 """
        fix.put( gtk.Button("SPACE") , 180 , 240 )

        vbox.pack_end(fix, True, True, 0)
        self.add(vbox)
        self.connect("destroy", gtk.main_quit)
        self.show_all()

PyApp()
gtk.main()

rawDescription: PyGTK - how can I use this table layout to match the background image?


code:
import os
import re

result = []
reg_compile = re.compile("test\d{8}")
for dirpath, dirnames, filenames in os.walk(myrootdir):
    result = result + [dirname for dirname in dirnames if  reg_compile.match(dirname)]

rawDescription: Python 3 filtering directories by name that matches specific pattern


code:
class Hero_Health():

    def __init__(self, current_h):
        self.current_hero_health = current_h

    def current_hero_health(self):
        self.current_hero_health = 3 ; # this is stored total of hero health

rawDescription: Python - Pass a variable to a function

description: Python - Pass a variable to a function


code:
def api_post_get_many(result=None, **kw):
    for key in result.keys():
        if key != 'objects':
            del result[key]

rawDescription: Modify JSON response of Flask-Restless

description: Modify JSON response of Flask-Restless


code:
def get_field_display(klass, field, value):
    f = klass._meta.get_field(field)
    return dict(f.flatchoices).get(value, value)

# usage
get_field_display(User, 'name', 'JSmith001')

rawDescription: Django: using values() and get_FOO_display()?


code:
from django.db.models import Min, Q

conversations = Conversation.objects.filter(Q(sender=user) | Q(receiver=user)) \
                                    .annotate(min_date=Min('message__creation_date')) \
                                    .order_by('-min_date')

rawDescription: Complex Ordering In Django with respect to related models


rawDescription: Shortest way to do this Python 3 list operation in PHP, JavaScript?


code:
kwargs = {}
args = []
for i in my_parameters.useful_token_concept:
    bits = i.split('=')
    if len(bits) > 1:
       kwargs[bits[0].strip()] = bits[1].strip()
    else:
       args.append(bits[0].strip())

params = soup.find(*args, **kwargs)

rawDescription: Parameter list with variable types Python 3?


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

x_data = np.linspace(0, 2*np.pi, 30)
y_data = np.sin(x_data) + normal(0, 0.2, 30)

def func(x, a, b, c, d):
    return a * np.sin(b*x + c) + d

parameter, covariance_matrix = curve_fit(func, x_data, y_data)

x = np.linspace(min(x_data), max(x_data), 1000)
plt.plot(x_data, y_data, 'rx', label='data')
plt.plot(x, func(x, *parameter), 'b-', label='fit')   # the star is to unpack the parameter array
plt.show()

rawDescription: Trouble with curve fitting - matplotlib

description: Trouble with curve fitting - matplotlib


code:
with open('output.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(data)

rawDescription: How to write List of lists in csv file in python

description: Write List of lists in csv file in python


code:
import pylab as pl
import numpy as np

x, y = np.mgrid[-1:1:100j, 0:1:100j]

z = ... # your function

contour_levels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 200, 300, 400]

cmap = pl.cm.BuPu
colors = cmap(np.linspace(0, 1, len(contour_levels)))
pl.contour(x, y, z, levels=contour_levels, colors=colors)

rawDescription: Contourplot with 2 different step sizes in matplotlib


code:
s = pd.Series(pd.date_range('2012-01-01', periods=3))
s
s[0]
s.dt.to_pydatetime()

rawDescription: Converting a `pandas.tslib.Timestamp` object to `datetime`


rawDescription: Non-numbered pages in ReportLab

description: Non-numbered pages in ReportLab


code:
def inverse_transform(self, X):
    return np.dot(X, self.components_) + self.mean_

rawDescription: Extracting PCA components with sklearn

description: Extracting PCA components with sklearn


code: B = filter(lambda x: x <= 1, A)

rawDescription: How to delete current element read in list by Python?

description: Delete current element read in list by Python


code:
start, stop, step = 0, 42, 5
range(start, stop+step, step)
range(start, stop+1, step)

rawDescription: Pythonic way to use range with excluded last number?


rawDescription: loop through a certain type of file in a folder (python)

description: Loop through a certain type of file in a folder (python)


code:
cmd = 'svn update http://myserver/myrepo'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output, stderr = p.communicate()
status = p.returncode
if output.strip().startswith('Skipped'):
    logging.error('A fatal has error occurred while executing command: ' + cmd)
    exit(-1)

rawDescription: how to find if svn command executed successfully or not in python

description: Find if svn command executed successfully or not in python


code:
df=pd.DataFrame({ 'x':range(30) })
df = df.rolling(10).mean()           # version 0.18.0 syntax
df[4::5]                             # take every 5th row

rawDescription: Sliding Window over Pandas Dataframe

description: Sliding Window over Pandas Dataframe


code:
lst=["item1","item2","item3"]

itemlist=Listbox(window)
for item in lst:
    itemlist.insert(END,item)
itemlist.place(x=100,y=120)

rawDescription: tkinter listbox display problems

description: Tkinter listbox display problems


code:
class User(models.Model):
    first_name = models.CharField(max_length=256)
    last_name = models.CharField(max_length=256)
    slug = models.SlugField(max_length=256, unique=True, default=uuid.uuid1)

    def make_slug(self):
        return self.first_name + self.last_name[0]

    def save(self, *args, **kwargs):
       self.slug = self.make_slug()
       super().save(*args, **kwargs)

rawDescription: Callable not defined for django.db.models field default


code:
with open(parafile, "rb") as paracsv:
    #Read in parameter values as a dictionary
    paradict = csv.DictReader(paracsv)
    has_rows = False
    for line in paradict:
        has_rows = True

    if not has_rows:
        return None

rawDescription: How to check whether csv.DictReader object is empty or not

description: Check whether csv.DictReader object is empty or not


code: return np.fabs((XX - X ** 2 / count) / d)

rawDescription: What algorithm does Pandas use for computing variance?


code:
lst = [1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1]

index = 0
leftCount, rightCount = 0, lst.count(0)
while leftCount < rightCount:
    if lst[index] == 1:
        leftCount += 1
    else:
        rightCount -= 1
    index += 1

print(lst[:index], index, lst[index + 1:])

rawDescription: Dividing array into two parts. Need better solution


rawDescription: How to make two functions share the same non global variable (Python)

description: Make two functions share the same non global variable (Python)


code: dist = numpy.linalg.norm(a-b)

rawDescription: How can the euclidean distance be calculated with numpy?


code:
import os

def assert_no_umd():
    mod = __file__
    if os.environ.get("UMD_ENABLED", "").lower() == "true":
        namelist = os.environ.get("UMD_NAMELIST", None)
        if namelist is not None:
            namelist = namelist.split(',')
            if mod not in namelist:
                 raise ValueError('UMD active!!')

rawDescription: Writing a Python module that can tell when it's hit with Spyder's UMD (user module deleter)


code:
import ast

original_ast = ast.parse("""
import sys
for a in range(0,10):
    print(a)
sys.exit(0)""")

altered_ast = ast.parse("""
import sys
for a in range(0,10):
    print(a + 1)
sys.exit(0)""")

ast.dump(original_ast) == ast.dump(altered_ast)

rawDescription: How to ensure no logic changes when editing code style/whitespace?

description: Ensure no logic changes when editing code style/whitespace


code:
class RepeatedDeferred:
    def __init__(self):
        self.callbacks = []

        self.df = defer.Deferred()

    def addCallback(self, callback):
        self.callbacks.append(callback)

        self.df.addCallback(callback)

    def callback(self, res):
        self.df.callback(res)

        self.df = defer.Deferred()
        for c in self.callbacks:
            self.df.addCallback(c)

rawDescription: Twisted: deferred that fires repeatedly?

description: Twisted: deferred that fires repeatedly


code:
import re
s = 'This is a good doll http://www.google.com/a/bs/jdd/etc/etc/a.py'
re.sub(r'(?:https?://)([^/]+)(?:\S+)', r"\1", s)

rawDescription: Shorten the url to domain name in a string in python 2.7


code:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(nrows=3, ncols=2)

for ax in axs.flat:
    plotxy(ax,x,y)

rawDescription: plot a large number of axis objects using one command through a loop

description: Plot a large number of axis objects using one command through a loop


key: value

rawDescription: Python json.dump() to javascript JSON.parse()


code:
fig = plt.figure()
ax = fig.add_subplot(111)
ln, = plt.plot([])
plt.ion()
axes=plt.axes()
plt.show()
plotdata = []
while self.Run:
  plotdata.append(getsensordata())
  plotdata.append(AH_SHT)
  ln.set_ydata(plotdata)
  ln.set_xdata(range(len(plotdata)))
  ax.relim()
  ax.autoscale_view()
  plt.draw()

rawDescription: Python: Live plotting updates whole line instead of one point


code:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class RemoteDriverStartService():

    options = webdriver.ChromeOptions()
    options.add_experimental_option("Proxy", "null")
    options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
    caps = options.to_capabilities()  # Here's the interesting part :)

    def start_driver(self):
        return webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',
                                desired_capabilities=self.caps)

rawDescription: Passing chromeOptions to RemoteDriver, with Python bindings


code:
import re

# Don't bother matching everything, just the bits that matter.
pat = re.compile(r'AssemblyFileVersion.*\.(\d+)"')

# ... lines omitted which set up read_file, write_file etc.

for line in read_file:
    m = pat.search(line)
    if m:
        start, end = m.span(1)
        line = line[:start] + str(int(line[start:end]) + 1) + line[end:]
    write_file.write(line)

rawDescription: Increment a Version Number using Regular Expression


code:
@bottle.route('/status')
@bottle.route('/status/<filename>')
def server_static(filename='index.html'):
    root = os.path.join(os.path.dirname(__file__), 'status', 'public_html')
    return bottle.static_file(filename, root=root)

rawDescription: How to serve index.html by default via bottle?

description: Serve index.html by default via bottle


code: request = self.factory.get(reverse('home'), HTTP_USER_AGENT='Mozilla/5.0')

rawDescription: Add HTTP_USER_AGENT to Django RequestFactory request?


code:
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.link.open_newwindow", 1)
browser = webdriver.Firefox(firefox_profile=fp)

rawDescription: Python - Selenium : Submit form without opening new tab or new window


rawDescription: How to get replSetGetStatus in Pymongo 3.2+?

description: Get replSetGetStatus in Pymongo 3.2+


code:
from matplotlib import animation

fig = figure()
N = 10
r = random.randint(-100,100,2*N).reshape(N,2)
line, = plt.plot(r[:,0], r[:,1],'o')

lag_len = 10
history_x = np.zeros((N,lag_len))
history_y = np.zeros((N,lag_len))
trails = [plot(hx,hy)[0] for hx,hy in zip(history_x,history_y)]

def update_frame(i):
    frame_num = i
    newdatax = r[:,0] + random.rand(N)
    newdatay = r[:,1] + random.rand(N)
    line.set_ydata(newdatay)
    line.set_xdata(newdatax)
    history_x[:,frame_num%lag_len] = newdatax
    history_y[:,frame_num%lag_len] = newdatay
    for hx,hy,ln_h in zip(history_x,history_y,trails):
         ln_h.set_xdata(hx)
         ln_h.set_ydata(hy)

    plt.title("At timestep: %d" %i)
    plt.hold(True)
    return (line,) + tuple(trails)

anim = animation.FuncAnimation(fig, update_frame,
                           frames=100, interval=20)

rawDescription: Matplotlib crude animation, show trace

description: Matplotlib crude animation, show trace


rawDescription: Python Regex char class

description: Python Regex char class


code:
import dis
assignment = compile('''\
e = 10.0
f = 10.0
''', '<stdin>', 'exec')
dis.dis(assignment)
assignment.co_consts

rawDescription: Get different results in Sublime Text and terminal when I run python code?


code:
user_input = raw_input("Please enter a word: ")
ls = ["a", "f" , "x" , "u"]
user_list = []
inDict = True

for i in user_input:
    if not(i in ls):
        inDict = False
        break
if(inDict):
    user_list.append(user_input)

rawDescription: Append a string to a list after checking


code:
df = pd.read_csv(StringIO(a.strip()), delimiter=r"\s+", header=None)
df2 = pd.read_csv(StringIO(a.strip()), delim_whitespace=True, header=None)
df2

rawDescription: Parsing text file in pandas

description: Parsing text file in pandas


code:
import os
import stat
import tempfile

...

f, tmpname = tempfile.mkstemp(dir=directory)
os.write(f, content)
# See http://docs.python.org/library/os.html#os.fsync
f.flush()
os.fsync(f.fileno())
os.close(f)
# Ensure it is webserver readable
os.chmod(tmpname, stat.S_IREAD | stat.S_IWRITE | stat.S_IWUSR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
# Rename is an atomic operation in POSIX
# See: http://docs.python.org/library/os.html#os.rename
os.rename(tmpname, fn)

rawDescription: Django and dynamically generated images

description: Django and dynamically generated images


code:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.lines import Line2D

class DraggablePoint:

    # http://stackoverflow.com/questions/21654008/matplotlib-drag-overlapping-points-interactively

    lock = None #  only one can be animated at a time

    def __init__(self, parent, x=0.1, y=0.1, size=0.1):

        self.parent = parent
        self.point = patches.Ellipse((x, y), size, size * 3, fc='r', alpha=0.5, edgecolor='r')
        self.x = x
        self.y = y
        parent.fig.axes[0].add_patch(self.point)
        self.press = None
        self.background = None
        self.connect()

        if self.parent.list_points:
            line_x = [self.parent.list_points[0].x, self.x]
            line_y = [self.parent.list_points[0].y, self.y]

            self.line = Line2D(line_x, line_y, color='r', alpha=0.5)
            parent.fig.axes[0].add_line(self.line)

    def connect(self):

        'connect to all the events we need'

        self.cidpress = self.point.figure.canvas.mpl_connect('button_press_event', self.on_press)
        self.cidrelease = self.point.figure.canvas.mpl_connect('button_release_event', self.on_release)
        self.cidmotion = self.point.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)

    def on_press(self, event):

        if event.inaxes != self.point.axes: return
        if DraggablePoint.lock is not None: return
        contains, attrd = self.point.contains(event)
        if not contains: return
        self.press = (self.point.center), event.xdata, event.ydata
        DraggablePoint.lock = self

        # draw everything but the selected rectangle and store the pixel buffer
        canvas = self.point.figure.canvas
        axes = self.point.axes
        self.point.set_animated(True)
        if self == self.parent.list_points[1]:
            self.line.set_animated(True)
        else:
            self.parent.list_points[1].line.set_animated(True)
        canvas.draw()
        self.background = canvas.copy_from_bbox(self.point.axes.bbox)

        # now redraw just the rectangle
        axes.draw_artist(self.point)

        # and blit just the redrawn area
        canvas.blit(axes.bbox)

    def on_motion(self, event):

        if DraggablePoint.lock is not self:
            return
        if event.inaxes != self.point.axes: return
        self.point.center, xpress, ypress = self.press
        dx = event.xdata - xpress
        dy = event.ydata - ypress
        self.point.center = (self.point.center[0]+dx, self.point.center[1]+dy)

        canvas = self.point.figure.canvas
        axes = self.point.axes
        # restore the background region
        canvas.restore_region(self.background)

        # redraw just the current rectangle
        axes.draw_artist(self.point)

        if self == self.parent.list_points[1]:
            axes.draw_artist(self.line)
        else:
            self.parent.list_points[1].line.set_animated(True)
            axes.draw_artist(self.parent.list_points[1].line)

        self.x = self.point.center[0]
        self.y = self.point.center[1]

        if self == self.parent.list_points[1]:
            line_x = [self.parent.list_points[0].x, self.x]
            line_y = [self.parent.list_points[0].y, self.y]
            self.line.set_data(line_x, line_y)
        else:
            line_x = [self.x, self.parent.list_points[1].x]
            line_y = [self.y, self.parent.list_points[1].y]

            self.parent.list_points[1].line.set_data(line_x, line_y)

        # blit just the redrawn area
        canvas.blit(axes.bbox)

    def on_release(self, event):

        'on release we reset the press data'
        if DraggablePoint.lock is not self:
            return

        self.press = None
        DraggablePoint.lock = None

        # turn off the rect animation property and reset the background
        self.point.set_animated(False)
        if self == self.parent.list_points[1]:
            self.line.set_animated(False)
        else:
            self.parent.list_points[1].line.set_animated(False)

        self.background = None

        # redraw the full figure
        self.point.figure.canvas.draw()

        self.x = self.point.center[0]
        self.y = self.point.center[1]

    def disconnect(self):

        'disconnect all the stored connection ids'

        self.point.figure.canvas.mpl_disconnect(self.cidpress)
        self.point.figure.canvas.mpl_disconnect(self.cidrelease)
        self.point.figure.canvas.mpl_disconnect(self.cidmotion)

rawDescription: Draggable line with draggable points

description: Draggable line with draggable points


code:
class MakeItView(CreateView):
    model = myModel
    template_name = 'whatever.html'

    def get_context_data(self, **kwargs):
        context = super(MakeItView, self).get_context_data(**kwargs)
        if int(self.kwargs['user_id']) != self.request.user.id:
            raise PermissionDenied
        return context

rawDescription: Django function based view to class based view


code:
goodfiles = fnmatch.filter(files, pattern)
badfiles = set(files).difference(goodfiles)

rawDescription: get also element that don't match fnmatch

description: Get also element that don't match fnmatch


code: STATIC_URL = "//static.example.com"

rawDescription: Switching Djanto's STATIC_URL to HTTPS when site is accessed via HTTPS


code:
for a in range(1,99):
    print("%d03" % a)

rawDescription: Fill a list with a range of formated strings in python


code:
import tkinter as tk
from tkinter import ttk

class LabeledCombobox(tk.Frame):

    def __init__(self, master, dictionary, *args, **kw):
        tk.Frame.__init__(self, master, *args, **kw)
        self.dictionary = dictionary

        self.combo = ttk.Combobox(self, values=sorted(list(dictionary.keys())),
                                  state='readonly')
        self.combo.current(0)
        self.combo.pack(fill="both")
        self.combo.bind('<<ComboboxSelected>>', self.on_selection)

        self.label = tk.Label(self, text=self.value())
        self.label.pack(fill="both", expand=True)

    def value(self):
        return self.dictionary[self.combo.get()]

    def on_selection(self, event=None):  # Just to test
        self.label.config(text=self.value())

lookup = {'Arkitekt': 'A', 'Geoteknik': 'B',
          'Ingeniør Anlæg': 'C', 'Procesanlæg': 'D'}

documentcode = {'Aftaler': 'AGR', 'Analyse': 'ANA',
                'Myndigheder': 'AUT', 'Sagsbasis': 'BAS'}

if __name__ == "__main__":
    root = tk.Tk()
    root.title("Labeled comboboxes")

    combo1 = LabeledCombobox(root, lookup, bd=1, relief="groove")
    combo1.pack(side="left", padx=(2, 2), pady=5)
    combo2 = LabeledCombobox(root, documentcode, bd=1, relief="groove")
    combo2.pack(side="right", padx=(2, 2), pady=5)

    root.mainloop()

rawDescription: Print combobox value in label

description: Print combobox value in label


code:
import csv
test = ['a','b','c','d','e']

with open('names.csv', 'wb') as csvfile:
    fieldnames = ['acol', 'bcol', 'ccol']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    for t in test:
        writer.writerow({'acol': t, 'bcol': t, 'ccol': t})

rawDescription: Python append to csv file without white rows and without repeating columns


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

dictMy = {'apple' : [[0, 1], [0, 2], [3, 2]], 'pear' : [[2, 3], [3, 5], [0, 2]], 'peach' : [[2, 5], [3, 8], [0, 0]]}
color=iter(cm.rainbow(np.linspace(0,1,len(dictMy))))

for key in dictMy:
    curLabel = key
    c=next(color)
    for idx, item in enumerate(dictMy[key]):
        x = item[0]
        y = item[1]
        if idx == 0:
            plt.scatter(x, y, c=c, label=key)
        else:
            plt.scatter(x, y, c=c)

plt.legend()
plt.show()

rawDescription: How to write a legend using dictionary keys?

description: Write a legend using dictionary keys


code:
def clean_history(sel, domains):
    temp = sel.get_location()
    for domain in domains:
        sel.open(domain)
        sel.delete_all_visible_cookies()
    sel.open(temp)

rawDescription: Selenium Internet Explorer 8 caching issues


code:
my_options = {"bg" : "gray60", "font" : ("Tahoma", 20)}

tk.Label(root, text = "Same background", **my_options).grid(row = 1, column = 1)
tk.Button(root, text = "and font.", **my_options).grid(row = 1, column = 2, padx = (5, 0))

rawDescription: Setting Tkinter default widgets kwargs

description: Setting Tkinter default widgets kwargs


code:
import math
import pygame

def main():
    screen = pygame.display.set_mode((800, 800))
    pygame.display.set_caption('Foondus v0.1')
    pygame.init()
    home = HomeBase(300, 300, screen)
    while True:
        screen.fill((0, 0, 0))
        pygame.display.update()

class HomeBase:

    def __init__(self, x, y, screen):
        self.x, self.y, self.screen = x, y, screen
        self.base_image = pygame.image.load('base.png')
        self.gun_image = pygame.image.load('gun.png')
        self.screen.blit(self.base_image, (self.x, self.y))
        self.screen.blit(self.gun_image, (self.x, self.y))

if __name__ == '__main__':
    main()

rawDescription: Pass an __init__ value from a higher class into a subclass requiring that same argument


code:
def send(self, recipients, mime_message):
    """
    From the MIME message (object from standard Python lib), we extract
    information to know to who send the mail and then send it using the
    SMTP server.

    Recipients list must be passed, since it includes BCC recipients
    that should not be included in mime_message header.
    """
    self.smtp_connection.sendmail(
        self.email_account,
        recipients + [self.email_account],
        mime_message.as_string()
    )

    ### On the IMAP connection, we need to move the mail in the "SENT" box
    # you may need to be smarter there, since this name may change
    sentbox_name = 'Sent'

    # 1. Get the mail just sent in the INBOX
    self.imap_connection.select_folder('INBOX', readonly=False)
    sent_msg_id = self.imap_connection.search(['UNSEEN', 'FROM', self.username])
    if sent_msg_id:
        # 2. Mark it as read, to not bother the user at each mail
        self.imap_connection.set_flags(sent_msg_id, '\Seen')
        # 3. Copy the mail in the sent box
        self.imap_connection.copy(sent_msg_id, sentbox_name)
        # 4. Mark the original to delete and clean the INBOX
        self.imap_connection.set_flags(sent_msg_id, '\Deleted')
        self.imap_connection.expunge()

rawDescription: Show mail sent through SMTP server in my IMAP "Sent" mailbox


code:
import csv
with open ('data.txt', 'r') as f:
    col_one = [float(row[0]) for index, row in enumerate(csv.reader(f, delimiter= '\t')) if index != 0]

rawDescription: converting a list with strings into a list with floats

description: Converting a list with strings into a list with floats


code:
def worker(leads):
    payload = {"action":"createOrUpdate","input":leads}
    r = requests.post(url, params=params, data=json.dumps(payload), headers=headers)
    return r.text

if __name__ == "__main__":
    pool = multiprocessing.Pool(multiprocessing.cpu_count() * 2)  # cpu_count() * 2 processes running in the pool
    responses = pool.map(worker, read_test_data(TEST_FILE))

    skipped_count = 0
    for raw_response in responses:
        http_response = json.loads(raw_response)
        for i in http_response['result']:
            if (i['status'] == "skipped" and i['reasons'][0]['code'] == "1004"):
                skipped_count += 1
    print("Number of records skipped: " + str(skipped_count))

rawDescription: Python - multiprocessing threads don't close when using Queue


code:
from lxml import etree
etree.fromstring('abc	www.abc.com').xpath("//td/following-sibling::a/text()")

rawDescription: get the value of href using lxml in html table

description: Get the value of href using lxml in html table


code:
originalinput = input("Please enter an input: ")
manipulateinput = input("Please enter an input to be manipulated: ")
output = ''
while len(output) < len(originalinput):
    output += manipulateinput
output = output[:len(originalinput)]

rawDescription: Manipulating string to repeat based on the length of another string


code:
def mostConsecutiveYears(names):
    """
    Compute which name occurs the most times consecutively in a
    list of names.
    :param names (list of Name): A list of name objects
    :return: A tuple containing best name (str) and the count (int)
    :rtype: tuple
    """
    bestName = names[0] # Be careful you can get an error if your list is empty
    bestCount = 1
    currentName = names[0]
    currentCount = 1

    for name in names[1:]:

        if name == currentName:
            currentCount += 1
        else:
            currentCount = 1
            currentName = name

        if currentCount > bestCount:
            bestName = currentName
            bestCount = currentCount

    return bestName, bestCount

print(mostConsecutiveYears(['Emily','Emily','Emily','Isabel']))

rawDescription: Counting consecutive names

description: Counting consecutive names


code:
from importlib import import_module

for module_name, class_name in (('capacity_hdd_parser', 'CapacityHDDParser'),
                                ('capacity_ssd_parser', 'CapacitySSDParser'),
                                ('checksum_parser', 'ChecksumParser')):
    data_returned = getattr(import_module(module_name), class_name)().parse(logset_path)
    # Other processing here

rawDescription: Looping through imported classes in python


code: keys.sort(key=lambda x: [int(y) for y in x.split('.')])

rawDescription: Sorting numbers in string format with Python


code:
import numpy as np
import pandas as pd

data = np.arange(9).reshape(3,3)
IDs = np.array([["A", "A", "B"],
                ["A", "B", "C"],
                ["B", "C", "C"]])
df = pd.DataFrame({'ID':IDs.ravel(), 'data':data.ravel()})
print(df)
#   ID  data
# 0  A     0
# 1  A     1
# 2  B     2
# 3  A     3
# 4  B     4
# 5  C     5
# 6  B     6
# 7  C     7
# 8  C     8

print(df.groupby('ID').agg([np.mean, np.std]))
#         data
#         mean       std
# ID
# A   1.333333  1.527525
# B   4.000000  2.000000
# C   6.666667  1.527525

rawDescription: Efficiently processing values in one array based on a ID value in another


code:
print('Lets play a game with your age!')
age = int(input('Enter your age'))
if age <= 10:
    print("You still need to wear what your Mom puts out for you!")
elif age  >= 11 and age <= 15:
    print("Congratulations! You are old enough to chew gum and walk at the same time!")
elif age >= 16 and age <= 18:
    print("You are old enough to sit in the emergency exit row of an airplane!")
elif age >= 19 and age <= 29:
    print("Wow! You are REALLY old!!!")
else:
    print("Your age is off the charts. Are you sure you're not a dinosaur?")

rawDescription: Python Code only gives first response

description: Python Code only gives first response


rawDescription: How to get a reliable unicode character count in Python?

description: Get a reliable unicode character count in Python


rawDescription: Pandas DataFrame filtering

description: Pandas DataFrame filtering


code:
classs = input("Class [1, 2 or 3] -  ")

if classs =='1':
    name = input("What is your name? -  ")

    datafile = '1.txt'
    found = False

    # This is a better way to open the file, closes when done
    with open(datafile, 'r') as f:
        data = f.readlines()
        for line in data:
            if name in line:
                print("True")
                found = True
        if found == False:
            print("False")

rawDescription: Python HELP - Output specific data from .txt file and output it in python


code:
class NonVerticalLine:

    def __init__(self, point_1, point_2):

            self.point_1=point_1
            self.point_2=point_2

    @property
    def slope(self):

            p1=self.point_1
            p2=self.point_2
            return (p2.y-p1.y)/(p2.x-p1.x)

rawDescription: Bounded Function displaying address in Python


code: {k: v.get('score') for item in data for k, v in item.items()}

rawDescription: parse json without explciitely calling the key-name

description: Parse json without explciitely calling the key-name


code:
import cgi
import json

form = cgi.FieldStorage()

param = form.getvalue("param")

if param:
    json.dumps({'success': True, 'message':  param })
else:
    json.dumps({'success': False, 'message':  'did not receive parameter' })

rawDescription: Check if key:value pair exist in POST

description: Check if key:value pair exist in POST


code:
self.process = QProcess()
self.connect(self.process, SIGNAL("readyReadStdout()"), self.readOutput)
self.connect(self.process, SIGNAL("readyReadStderr()"), self.readErrors)
tarsourcepath="sudo tar xvpf "+ self.path1
self.process.setArguments(QStringList.split(" ",tarsourcepath))
self.process.start()

def readOutput(self):

    self.textBrowser2.append(QString(self.process.readStdout()))
    if self.process.isRunning()==False:
        self.textBrowser2.append("\n Completed Successfully")

def readErrors(self):
    self.textBrowser2.append("error: " + QString(self.process.readLineStderr()))

rawDescription: How to Redirect a Python Console output to a QTextBox


code:
@main.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        a = Auth()
        # without this return it will fall through to render_template
        return a.login(form.username.data, form.password.data, form.remember_me.data)
    return render_template('login.html', form=form)

rawDescription: Flask helper classes and return values

description: Flask helper classes and return values


code:
newTimeCol = int(timecol)
newSPCol = int(SPcol)

rawDescription: Calling a data frame object to set a variable in Python