code:
import timeit
timeit.Timer('random.randint(0,10)', 'import random').repeat()
timeit.Timer('random.randrange(0,10)', 'import random').repeat()
timeit.Timer('r._randbelow(10)', 'import random; r = random.Random()').repeat()
timeit.Timer('random.uniform(0,10)', 'import random').repeat()

rawDescription: Random number generation python 3.x default libraries


code:
def read_file(f, param):
    if isinstance(f, basestring):
        with open(f, 'rb') as real_f:
            return read_file2(real_f, param)
    else:
        return read_file2(real_f, param)

def read_file2(f, param):
    # Now f should be a file object
    ...

rawDescription: conditionally close file on exit from function

description: Conditionally close file on exit from function


code:
sp = subprocess.Popen("testfile.bat", stdin=subprocess.PIPE)
sp.stdin.write("\r\n") # send the CR/LF for pause
sp.stdin.close() # close so that it will proceed

rawDescription: How can I surpass "Press any key to continue ..." in python?

description: Surpass "Press any key to continue ..." in python


code:
class TopToolbar(plugins.PluginBase):
    """Plugin for moving toolbar to top of figure"""

    JAVASCRIPT = """
    mpld3.register_plugin("toptoolbar", TopToolbar);
    TopToolbar.prototype = Object.create(mpld3.Plugin.prototype);
    TopToolbar.prototype.constructor = TopToolbar;
    function TopToolbar(fig, props){
        mpld3.Plugin.call(this, fig, props);
    };

    TopToolbar.prototype.draw = function(){
      // the toolbar svg doesn't exist
      // yet, so first draw it
      this.fig.toolbar.draw();

      // then change the y position to be
      // at the top of the figure
      this.fig.toolbar.toolbar.attr("y", 2);

      // then remove the draw function,
      // so that it is not called again
      this.fig.toolbar.draw = function() {}
    }
    """
    def __init__(self):
        self.dict_ = {"type": "toptoolbar"}

rawDescription: mpld3: How to change the location of the toolbar using a plugin?

description: Mpld3: How to change the location of the toolbar using a plugin


code:
from datetime import datetime

dt = datetime.strptime( date, '%Y-%m-%d %H:%M' )
dt_plus_25 = dt + datetime.timedelta( 0, 2*60*60 + 30*60 )

rawDescription: adding 2 1/2 hours to a time object in python

description: Adding 2 1/2 hours to a time object in python


code:
skip=(slice(None,None,2),slice(None,None,2))
plt.quiver(x[skip], y[skip], u[skip], v[skip], color='black', headwidth=1, scale = 10, headlength=4)

rawDescription: Python Quiver Options

description: Python Quiver Options


code:
class SubClass(BaseClass):
    ...

rawDescription: Python version of Java's 'class extends'?


code: state_pcts = state_office.groupby(level=0).apply(lambda x: 100*x/float(x.sum()))

rawDescription: Pandas percentage of total with groupby

description: Pandas percentage of total with groupby


code:
import numpy

def mk_array(elems):
    '''Makes the nparray from an array of 24 numbers'''
    ttarray = np.array(elems) # perhaps [ float(a) for a in elems ] is needed
    ttarray.shape = (2,3,4)
    ttarray = np.swapaxes(ttarray,1,2)
    ttarray = np.swapaxes(ttarray,0,2)
    return ttarray

def ttarray_tms(traveltimes):
    '''It defines the 3-D array, organized as I want.'''
    arrays = list()
    with open (traveltimes, 'r') as file_in:
        ret = "." # force the loop
        while ret != "":
            newarray = [ file_in.readline() for i in range(24) ]
            ret = file_in.realine()
            if ret != "": # avoid an empty array
                ttarray = mk_array(newarray)
                arrays.append(ttarray)
    return arrays

rawDescription: Creating arrays while processing big file


code: help(range)

rawDescription: Extension functions and 'help'

description: Extension functions and 'help'


code:
with open('myexecutable', 'w') as efile:
    efile.write('all my code goes here')

rawDescription: Saving python script in executable format


code:
def getbook():
    bookname = input("What is the name of the text file?")
    bookFile = open(bookname, 'r')
    bookString = bookFile.read()
    lowerBook = bookString.lower()
    wordList = lowerBook.split()
    return wordList

import string

def listAllThe(longString):
    theList = set()
    for i in longString:
        if i.startswith('the'):
            theList.add(i)
    return theList

def final():
    book = getbook()
    getList = listAllThe(book)
    print (getList)

final()

rawDescription: Python Text Files, print list with all words that begin with 'the'


code:
import random
b = list(a)
random.shuffle(b)

rawDescription: Shuffle position of elements in a list

description: Shuffle position of elements in a list


code:
from gdata.gauth import AuthSubToken
from gdata.spreadsheets.client import SpreadsheetsClient

atok = AuthSubToken(token_string=get_access_token())  # acess token via protocol
data = SpreadsheetsClient().get_worksheets(key, auth_token=atok)

rawDescription: Google spreadsheets api do not use OAuth2?


code:
import json
from django.forms.models import model_to_dict

results = Property.objects.raw(mysql_query)
data = []
for result in results:
    model = model_to_dict(result)
    model['image_url'] = model.property_image_set.first().url
    data.append(model)

markers = json.dumps(data)

rawDescription: serializing two different models with django

description: Serializing two different models with django


rawDescription: How to use list/array comprehension in python to generate cross product pairs

description: Use list/array comprehension in python to generate cross product pairs


rawDescription: Mimicking the behavior of HTML5 pattern attributes for inputs using web2py?


code:
class MainWindow(QtGui.QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.build_ui()
        self.imported_data = {}
        timer = QTimer()
        timer.timeout.connect(check_for_files)
        timer.start(10000)  # periodically check for new files

    def build_ui(self):
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

    def check_for_files(self):
        # check for new files here
        if new_file:
            self.import_data(file_name)
            self.ui.filename_list.addItem(file_name)  # add file name widget
            self.plot_data(file_name)

    def import_data(self, file_name):
        # open file and read data to file_data
        self.ui.imported_data[file_name] = file_data

    def plot_data(self, file_name):
        self.ui.graph.add_plot(self.imported_data[file_name])

rawDescription: PyQT application - where to hold data that is then processed by other widgets?


code:
from ast import literal_eval

def param(a,b):
   return "Hello world" + literal_eval(a) + literal_eval(b)

In [9]: param('" foo"', "'bar'")
Out[9]: 'Hello world foobar'

rawDescription: Changing a python variable based on its content


code:
loc = {}
for x in range(N):
    for y in range(M):
        loc.setdefault(matrix[x][y], []).append((x,y))

rawDescription: Python hash structure

description: Python hash structure


code:
def pop_gen(self):
    temp_popul = [Gib(self.gib_text[:], self.chance) for i in range(10)]

rawDescription: Object instances as list elements

description: Object instances as list elements


rawDescription: Reading from and writing to process using subprocess


rawDescription: How to check the last element of a python list?

description: Check the last element of a python list


code:
try:
    from opencv.cv import *
    from opencv.highgui import *
except:
    #
    # Different OpenCV installs name their packages differently.
    #
    from cv import *

if __name__ == '__main__':
    import sys
    #
    # 1 = Force the image to be loaded as RGB
    #
    img = LoadImage (sys.argv[1], 1)
    NamedWindow ('img')
    ShowImage ('img', img)
    WaitKey ()

    #
    # Canny and Harris expect grayscale  (8-bit) input.
    # Convert the image to grayscale.  This is a two-step process:
    #   1.  Convert to 3-channel YCbCr image
    #   2.  Throw away the chroma (Cb, Cr) and keep the luma (Y)
    #
    yuv = CreateImage(GetSize(img), 8, 3)
    gray = CreateImage(GetSize(img), 8, 1)
    CvtColor(img, yuv, CV_BGR2YCrCb)
    Split(yuv, gray, None, None, None)

    canny = CreateImage(GetSize(img), 8, 1)
    Canny(gray, canny, 50, 200)
    NamedWindow ('canny')
    ShowImage ('canny', canny)
    WaitKey()

    #
    # The Harris output must be 32-bit float.
    #
    harris = CreateImage (GetSize(img), IPL_DEPTH_32F, 1)
    CornerHarris(gray, harris, 5, 5, 0.1)

rawDescription: how to process image with opencv in python?

description: Process image with opencv in python


code:
import os, os.path
import shutil
for src in os.listdir('../scr_path/01048'):
    s = os.path.join('../scr_path/01048',src)
    d = os.path.join('../destination_path/01048',src)
    for src1 in os.listdir(s):
        shutil.move(os.path.join(s,src1),d)

rawDescription: merge child directory into parent directory structure python

description: Merge child directory into parent directory structure python


code:
import wx

class ISC_status(wx.Panel):
    def __init__(self, parent, id=wx.ID_ANY):
        wx.Panel.__init__(self, parent, id, style=wx.SUNKEN_BORDER)
        self.initGUI()
# NOTE: remove next line for 'best fit' instead of 275x200
        self.SetMinSize((275, 200))
        self.Layout()
        self.Show()

    def initGUI(self):
# NOTE: here you created an additional (inner) panel
        #main container panel for all of the ISC status information
        sizer = wx.BoxSizer(wx.VERTICAL)
        #make the three individual checkboxes
        self.superviseCheck = wx.CheckBox(self,-1,'Supervisory Trouble')
        self.pirCheck = wx.CheckBox(self,-1,'PIR Activity')
        self.tamperCheck = wx.CheckBox(self,-1,'Tamper Tripped')
        #make the MAC hbox section
        self.macHbox = wx.BoxSizer(wx.HORIZONTAL)
        self.macLabel = wx.StaticText(self,-1,'MAC ADDR: ')
        macFont1 = wx.Font(10, wx.FONTFAMILY_DEFAULT,wx.FONTSTYLE_NORMAL,wx.FONTWEIGHT_BOLD)
        self.macLabel.SetFont(macFont1)
        macFont2 = wx.Font(10, wx.FONTFAMILY_DEFAULT,wx.FONTSTYLE_NORMAL,wx.FONTWEIGHT_NORMAL)
        self.macAddr = wx.StaticText(self,-1,'001CFAF10009F1EE')
        self.macAddr.SetFont(macFont2)
        self.macHbox.Add(self.macLabel,0,wx.RIGHT,5)
        self.macHbox.Add(self.macAddr,0,0,0)
        #make the low battery hbox section
        self.lowBatCheck = wx.CheckBox(self,-1,'Low Battery Voltage')
        self.batVoltText = wx.StaticText(self,-1, '9.9V')
        self.lowBatHbox = wx.BoxSizer(wx.HORIZONTAL)
        self.lowBatHbox.Add(self.lowBatCheck, 0,wx.RIGHT,20)
        self.lowBatHbox.Add(self.batVoltText, 0,wx.ALL,0)
        #make the pirCount hbox section
        self.pirCountHbox = wx.BoxSizer(wx.HORIZONTAL)
        self.pirCountNumber = 3
        self.pirLabel = wx.StaticText(self,-1,'PIR Count: ')
        self.pirCount = wx.StaticText(self,-1,str(self.pirCountNumber))
        self.pirReset = wx.Button(self,-1,label='Reset PIR Count')
        self.pirCountHbox.Add(self.pirLabel,0,wx.RIGHT,3)
        self.pirCountHbox.Add(self.pirCount,0,wx.RIGHT, 40)
        self.pirCountHbox.Add(self.pirReset,0,wx.ALIGN_RIGHT,0)
        self.pirReset.Bind(wx.EVT_BUTTON,self.resetPIR)
        #add all the parts to the main vbox
        sizer.Add(self.macHbox,0,wx.ALL,5)
        sizer.Add(self.superviseCheck,0,wx.ALL,5)
        sizer.Add(self.lowBatHbox,0,wx.ALL,5)
        sizer.Add(self.tamperCheck,0,wx.ALL,5)
        sizer.Add(self.pirCheck,0,wx.ALL,5)
        sizer.Add(self.pirCountHbox,0,wx.ALL,5)
# NOTE: here you set the sizer to the inner panel but there was
#       no sizer to connect it to ISC_status itself so it was
#       unreachable for the main frame and the layout was ignored
        self.SetSizer(sizer)

    def resetPIR(self,event):
        pass

    def updateOnSerial(self,flags):
        pass

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        sizer = wx.BoxSizer(wx.VERTICAL)
        row = wx.BoxSizer(wx.HORIZONTAL)
# NOTE: set proportion to 0 if you want them 'fixed size'
        row.Add(ISC_status(self),1,wx.ALL,5)
        row.Add(ISC_status(self),1,wx.ALL,5)
        sizer.Add(row,0,wx.EXPAND)
        self.SetSizerAndFit(sizer)

app = wx.PySimpleApp()
frame = TestFrame().Show()
app.MainLoop()

rawDescription: Why does the formatting of my wxPython widget get fouled up?


rawDescription: Django ordering using filtered foreignkey


rawDescription: cleaning data with Python (for statement)

description: Cleaning data with Python (for statement)


code:
import sys
sys.argv.append('syncdb')

from django.core.management import execute_manager
import settings # Your project's settings. Assumed to be in the same directory.

execute_manager(settings)

rawDescription: How to make syncdb work automatically in Django?

description: Make syncdb work automatically in Django


code:
class GenoModel(DNAFeature):

    def __init__(self, start, end):
        self.setStart(start)
        self.setEnd(end)
        self.strand = None
        self.sequencename = None
        self.exons = []
        self.translStart = None
        self.translStop = None
        self.displayId = None

    def getFeats(self):
        self.exons.sort(cmp=self.start)
        return self.exons

    def addFeat(self, f):

        if type(f) == DNAFeature:
            self.exons.append(f)
        else:
            raise TypeError("Cannot add feature as it is not of type DNAFeature")

    def setTranslStart(self, i):

        if type(i) != int:
            raise TypeError("Cannot set translStart as it is not of type int")
        elif i < 0:
            raise ValueError("Cannot set tanslStart to a negative int")
        else:
            self.translStart = i

    def getTranslStart(self):
        return self.translStart

    def setTranslStop(self, i):

        if type(i) != int:
            raise TypeError("Cannot set translStop as it is not of type int")
        elif i <= 0:
            raise ValueError("Cannot set tanslStop to anything less than 1")
        else:
            self.translStop = i

    def getTranslStop(self):
        return self.translStop

    def setDisplayId(self, s):

        if type(s) != str:
            raise TypeError("Cannot set desiplayId as it is not of type string")
        else:
            self.displayId = s

    def getDisplayId(self):
        return self.displayId

rawDescription: Subclass/Child class

description: Subclass/Child class


code:
def find(a):
    index=a
    ret = []
    while index<len(z)-1:
        if z[index]=="T":
            for index in range (index+20,index+30):
                if z[index]=="A" and z[index+1]=="G" and z[index+2]=="T":
                    a=index
                    ret.append(a)
        index=index+1
    return ret

rawDescription: Storing multiple values from single function in python


code:
import re
infilename = "infile.txt"
outfilename = "outfile.txt"

infile = open(infilename)
outfile = open(outfilename, "w")

pat = re.compile('^(.+?(\d*)) *$',re.M)
def zaa(mat):
    if mat.group(2):
        return '("%s" "#%s")' % (mat.group(1),str(int(mat.group(2))+9))
    else:
        return '("%s" "#")' % (mat.group(1))

outfile.write('(bookmarks \n')
for line in infile:
    outfile.write(pat.sub(zaa,line))
outfile.write(')')

rawDescription: Some questions about Regex in Python

description: Some questions about Regex in Python


code:
import csv

list_of_dictionaries = [
{'name' : 'John', 'city' : 'Las Vegas', 'hobbies' : ['running', 'gambling', 'weight-lifting']},
{'name' : 'Jane', 'city' : 'Chicago', 'hobbies' : ['video games', 'yoga']},
{'name' : 'Jack', 'city' : 'New York', 'hobbies' : ['sports', 'drinking', 'travel', 'fashion']}
]

with open('out.csv', 'wb') as csvfile:
    csv = csv.writer(
        csvfile,
        delimiter=',',
        quotechar='"',
        quoting=csv.QUOTE_MINIMAL
    )

    csv.writerow(['Name','City','Field1','Field2','...'])

    for item in list_of_dictionaries:
        # Include any multi-value fields as:
        # for field1, field2, ... in zip(item['field1', 'field2', ...]):
        #    csv.writerow([item['name'], item['city'], field1, field2, ...])
        for hobby in item['hobbies']:
            csv.writerow([item['name'], item['city'], hobby])

rawDescription: Printing list of dictionaries in Python to .csv in unique rows


code:
text = re.sub('|'.join([remove1, remove2, remove3, remove4, remove5, remove6
, remove7, remove8, remove9]), '', text)

rawDescription: Cleaning(Re.sub) when a text has too many new lines in python


rawDescription: pandas df to dict to series back to df does not produce original df

description: Pandas df to dict to series back to df does not produce original df


rawDescription: hiding console when run in os.startfile()?

description: Hiding console when run in os.startfile()


code: np.cross(a[None], b[:, None])

rawDescription: Numpy cross-product on rectangular grid

description: Numpy cross-product on rectangular grid


code:
cr.save()
cr.scale(scale_xy, scale_xy)
cr.xxx_place_image(...)
cr.restore()

rawDescription: How to scale down pixbuf image without losing quality?

description: Scale down pixbuf image without losing quality


code:
from multiprocessing.pool import ThreadPool
from urllib import urlretrieve

def loadpage(x):
    urlretrieve('http://google.com/search?q={}'.format(x), '{}.html'.format(x))

p = ThreadPool(10) # the max number of webpages to get at once
p.map(loadpage, range(50))

rawDescription: How to simultaneously download webpages using python?

description: Simultaneously download webpages using python


code:
import base64
example = b'\x01'*10
example
result = base64.b64encode(example).decode('ascii')
print(repr(result))

rawDescription: Convert file to base64 string on Python 3


code:
class Tag(models.Model):
    test_case_counter = models.IntegerField(default=0)
    my_date_field = models.DateField(auto_now=True,auto_add=True)

    class Meta: # notice indentation
        ordering = ['my_date_field']
        app_label = 'project'

rawDescription: How to write sort function

description: Write sort function


code:
selectedCurves = cmds.listConnections( t="animCurve")
for curve in selectedCurves:
    mykeys = cmds.keyframe(curve, query=True, valueChange=True, absolute=True )
    low = min(mykeys)
    cmds.scaleKey(curve, valuePivot=low, valueScale=1.5 )

rawDescription: Maya/Python: How do I scale multiple selected animation curves each from their own specific pivot point?


code:
from scipy.misc import imread
import scipy.ndimage as ndimage

# Load the image
raw = imread("bG2W9mM.png")

# Convert the image to greyscale, using the red channel
grey = raw[:,:,0]

# Simple thresholding of the image
threshold = grey>200

radius = 10
distance_img = ndimage.distance_transform_edt(threshold)
morph_laplace_img = ndimage.morphological_laplace(distance_img,
                                                  (radius, radius))
skeleton = morph_laplace_img < morph_laplace_img.min()/2

import matplotlib.cm as cm
from pylab import *
subplot(221); imshow(raw)
subplot(222); imshow(grey, cmap=cm.Greys_r)
subplot(223); imshow(threshold, cmap=cm.Greys_r)
subplot(224); imshow(skeleton, cmap=cm.Greys_r)
show()

rawDescription: Fit curve to segmented image

description: Fit curve to segmented image


rawDescription: Setting values in a numpy arrays indexed by a slice and two boolean arrays


code:
if issue.fields.status in ('open', 'reopened'):
    # Move the ticket from opened to closed.
    jira.transition_issue(ticket, transitionId=131)

rawDescription: Is it possible to change jira issue status with python-jira?


code:
def underscored_class_span(elem, numbered=re.compile(u'_\d').match):
    if elem.name != 'span': return False
    classes = elem.get('class', [])
    return u'_' in classes and any(numbered(c) for c in classes)

for span in soup.find_all(underscored_class_span):
    span.decompose()

rawDescription: Bug in python bs4 analyzer classes?

description: Bug in python bs4 analyzer classes


code:
class parent_class:
    def __init__(self):
        self.title = multilang("hello world");

class multilang:
    def __init__(self, text):
        pass
    def __getitem__(self, key):
        if key == 'en':
            return "hello world";

rawDescription: Magic Methods in Python

description: Magic Methods in Python


code:
response = self.test_client.post(
    request_path,
    data=data,
    follow_redirects=False
)
expectedPath = '/'
self.assertEqual(response.status_code, 302)
self.assertEqual(urlparse(response.location).path, expectedPath)

rawDescription: Flask unit testing: Getting the response's redirect location


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

# load your pandas DataFrames  df1 and df2 here

ax = [plt.subplot(211), plt.subplot(211)]
pars = {'FR': {'color': 'r'},
        'stim_current': {'color': 'k'}}
ls_style = ['dashed', 'solid']
for ax_idx, name in enumerate(['FR', 'stim_current']):
    for df, ls in zip([df1, df2], ls_style):
        ax[ax_idx].plot(df.index, df[name], ls=ls, **pars[name])

rawDescription: pandas, matplotlib: a way to assign same colors, line styles for same column labels across subplots?

description: Pandas, matplotlib: a way to assign same colors, line styles for same column labels across subplots


code:
import logging
import sys

class CGLogger(logging.Logger):
    def __init__(self,name):
        logging.Logger.__init__(self,name)
        self.mainhandler = logging.StreamHandler(sys.stdout)
        self.addHandler(self.mainhandler)

    def stop_main_logging(self):
        self.removeHandler(self.mainhandler)

    def log_to_file(self, fn):
        self.filehandler = logging.FileHandler(fn)
        self.addHandler(self.filehandler)

    def stop_logging_to_file(self):
        self.removeHandler(self.filehandler)

    def restart_main_logging(self):
        self.addHandler(self.mainhandler)

    def switch_to_file_logging(self, fn):
        self.stop_main_logging()
        self.log_to_file(fn)

    def switch_to_main_logging(self):
        self.stop_logging_to_file()
        self.restart_main_logging(fn)

logging.setLoggerClass(CGLogger)

rawDescription: Logging separate files for different Processes in Python


code:
import json

json.dumps({'a':1,'b':2}) #this will return a string with your dict in json format.

#'{"a": 1, "b": 2}'

rawDescription: How to write a Python dictionary in JSON format?

description: Write a Python dictionary in JSON format


code:
from threading import Thread
import time
class A(Thread):
   def __init__(self, i):
       Thread.__init__(self)
       self._index = i

   def run(self):
       time.sleep(0.010)
       for i in range(self._index):
           print('Thread A running with index i: %d' % i)
           time.sleep(0.100)

class B(object):

   def __init__(self, index):
       x = A(i=index)
       print ('starting thread.')
       x.start()
       print('waiting ...')
       x.join()
       print('Thread is complete.')

if __name__ == '__main__':
   B(10) # Here I call the class B that should start the thread of A

rawDescription: Create instance of a Thread class within the same file in python?


rawDescription: PySpark 1.5 How to Truncate Timestamp to Nearest Minute from seconds


code:
import numpy as np
from scipy import pi,sin,cos
import matplotlib.pyplot as plt
import math as m

image = np.zeros(shape=(201,201))

a=4
b=3
pa = 240

def dEllipse(x,y,a,b,PA):
    the = -PA+90.0
    cos_a,sin_a=cos(the*pi/180.0),sin(the*pi/180.0)
    Xs = (x*cos_a - y*sin_a)**2.0
    Ys = (x*sin_a + y*cos_a)**2.0
    r = np.sqrt((Xs/a**2) + (Ys/b**2))
    return r

x = np.arange(0-100.0, 201-100.0)
y = np.arange(0-100.0, 201-100.0)
xx, yy = np.meshgrid(x, y)

new_image= dEllipse(xx,yy,a,b,pa)

plt.imshow(new_image)
plt.show()

rawDescription: How to parallelise 2 for loops using 1 function in python

description: Parallelise 2 for loops using 1 function in python


code:
from django.contrib.sites.models import Site
class Person(models.Model):
   name = models.CharField(max_length=20)
   site = models.ForeignKey(Site)
   on_site = models.ForeignKey(Site,related_name="site_users")

rawDescription: Show all model instances regardless of site in django admin


code:
keys = []
data = {}
names = {}

for line in open('file1', 'r'):
    keys.append(line.strip())
    data[line.strip()] = []

for line in open('file2', 'r'):
    name, key = line.strip().split()
    if key in data:
        data[key].append(name)
        names[name] = key

for line in open('file3', 'r'):
    nickname, name = line.strip().split()
    if name in names:
        data[names[name]].append(nickname)

with open('file4', 'w') as file4:
    for key in keys:
        file4.write('>{0} {1}\n'.format(key, ' '.join(data[key])))

rawDescription: Python multidimensional dict()

description: Python multidimensional dict()


code:
df["date"] = pd.to_datetime(df["date"]) # only if it isn't already
df = df.set_index("date")
(df > 85).groupby(df.index.date).sum()

rawDescription: How do I group hourly data by day and count only values greater than a set amount in Pandas?

description: Group hourly data by day and count only values greater than a set amount in Pandas


code:
# The Pylons WSGI app
app = PylonsApp()

#Profile the app
app = AccumulatingProfileMiddleware(
        app,
        log_filename='/profiling.log',
        cachegrind_filename='/cachegrind.out',
        discard_first_request=True,
        flush_at_shutdown=True,
        path='/__profile__'
)

rawDescription: measuring page request timing in pylons app?

description: Measuring page request timing in pylons app


code:
from scrapy.contrib.spiders import CrawlSpider,Rule
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request
from scrapySpider.items import SPage
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor

class TestSpider4(CrawlSpider):
    name = "spiderSO"
    allowed_domains = ["cumulodata.com"]
    start_urls = ["http://www.cumulodata.com/"]

    extractor = SgmlLinkExtractor()

    rules = (
        Rule(extractor,callback='parse_links',follow=True),
        )

    def parse_start_url(self, response):
        list(self.parse_links(response))

    def parse_links(self, response):
        hxs = HtmlXPathSelector(response)
        links = hxs.select('//a')
        for link in links:
            title = ''.join(link.select('./@title').extract())
            url = ''.join(link.select('./@href').extract())
            meta={'title':title,}
            cleaned_url = "%s/?1" % url if not '/' in url.partition('//')[2] else "%s?1" % url
            yield Request(cleaned_url, callback = self.parse_page, meta=meta,)

    def parse_page(self, response):
        hxs = HtmlXPathSelector(response)
        item=SPage()
        item['url'] = response.url
        item['title']=response.meta['title']
        item['h1']=hxs.select('//h1/text()').extract()
        return item

rawDescription: Why don't my Scrapy CrawlSpider rules work?


code:
funcs = (
    (20, f),
    (40, g),
    (float('inf'), h),
)

def superFunction(x):
    for limit, f in funcs:
        if x <= limit:
            return f(x)

rawDescription: Is there a pythonic way to group range based segmented functions into a single function?


code:
color_bar.set_alpha(1)
color_bar.draw_all()
# pylab.draw() or pyplot.draw() might be necessary

rawDescription: Partially transparent scatter plot, but with a solid color bar


code:
import numpy as np

mat = np.array([[1,2,3],
                [4,5,6],
                [7,8,9]]) # initialize mat to see format

handle = open('myfile.dat', 'ab')
for n in range(N):
    # perform linalg calculations on mat ...
    meta = foo # based on current mat

    tmp = np.hstack( ([[n]], [[meta]], (mat.copy()).reshape(1,mat.size)) )
    np.savetxt(handle, tmp, fmt='%+.8e')

handle.close()

rawDescription: numpy savetxt: save a matrix as row

description: Numpy savetxt: save a matrix as row


code:
def foo(stringio):
    datalist = []
    while True:
        chunk = stringio.read(256)
        i = chunk.find('Z')
        if i == -1:
            datalist.append(chunk)
        else:
            datalist.append(chunk[:i+1])
            break
        if len(chunk) < 256:
            break
    return ''.join(datalist)

rawDescription: fast way to read from StringIO until some byte is encountered

description: Fast way to read from StringIO until some byte is encountered


code:
def collapse(x):
    uniq = x.unique()
    if len(uniq) == 1:
        return uniq[0]
    else:
        return list(uniq)

rawDescription: Collapsing Data using GroupBy Pandas

description: Collapsing Data using GroupBy Pandas


code:
##### import modules
import time
import datetime
from datetime import datetime
from datetime import timedelta
##### create list of possible loop start times; this here creates start times every 5 minutes, starting the loops at 01:30, 06:30, 11:30, etc., after the hour
dt_cur = datetime.now()
dt_4_list = datetime.strptime(str(dt_cur.year) + '-' + str(dt_cur.month) + '-' + str(dt_cur.day) + ' ' + str(dt_cur.hour) + ':1:30', '%Y-%m-%d %H:%M:%S')
ls_dt = [dt_4_list]
for i in range(288):
    dt_4_list = dt_4_list + timedelta(minutes = 5)
    ls_dt.append(dt_4_list)
##### begin the loop
loop_timer = 0
while loop_timer < 275:
    if loop_timer > 0:
        ##### check if the loop started before the exact time due to time.sleep() not being exactly accurate
        time_check = datetime.now().strftime("%S.%f")
        time_check = float(time_check)
        loop_time_check = 0
        while time_check < 30 and loop_time_check < 100:
            time_check = datetime.now().strftime("%S.%f")
            time_check = float(time_check)
            print(time_check, '- WARNING: time_check < 30; loop =', loop_time_check + 1, flush=True)
            time.sleep(.025)
            loop_time_check += 1
            if loop_time_check == 100:
                print(time_check, '- WARNING: exiting script early; time_check < 30 100 times', flush=True)
                raise SystemExit()
    ########################################################################################################################
    ##### start doing  stuff here ##########################################################################################
    dt_cur = datetime.now()
    print(dt_cur, flush=True)
    for i in range(5):          ##### this is where your own script does it's work before resting until the next loop
        print(i, flush=True)
        time.sleep(1)
    ##### end doing stuff here #############################################################################################
    ########################################################################################################################
    ##### create the current datetime var
    dt_cur = datetime.now()
    print(dt_cur, flush=True)
    ##### create the dt_next_loop time var, increment the loop
    for d in ls_dt:
        if d > dt_cur:
            dt_next_loop = d
            break
    loop_timer += 1
    ##### get the seconds to sleep the script but make it about 5 seconds short of the actual time to begin the next loop
    sleep_secs = (dt_next_loop - dt_cur) - timedelta(seconds=5)
    sleep_secs = timedelta.total_seconds(sleep_secs)
    time.sleep(sleep_secs)
    ##### sleep the script about 5 seconds more before the actual time to begin the next loop (sleep.time() is not precisely accurate
    ##### over minutes and doing this again 5 seconds before the time to begin the next loop will ensure more accuracy)
    sleep_secs = datetime.now().strftime("%S.%f")
    sleep_secs = 30 - float(sleep_secs)
    time.sleep(sleep_secs)

rawDescription: sleep a python loop to iterate at exact times

description: Sleep a python loop to iterate at exact times


code:
import tkFileDialog
f = tkFileDialog.askopenfilename()
# Go on from there; f is a handle to the file that the user picked

rawDescription: Drag drop file onto Python script

description: Drag drop file onto Python script


code:
from boto.s3.connection import S3Connection

conn = S3Connection(AWS_ACCESS_KEY_ID,
                    AWS_SECRET_ACCESS_KEY, is_secure=False)

# The generated temp_url remains valid for 60 seconds in this case
temp_url = conn.generate_url(60, 'GET', bucket=bucket_name,
                             key=file_name, force_http=True)

rawDescription: Sending 'secure' financial statements on S3


code: myDatetime = datetime.strptime(myString.split(".")[0], "%Y-%m-%dT%H:%M:%S")

rawDescription: String to DATETIME in MySQL

description: String to DATETIME in MySQL


code:
pattern = re.compile('|'.join(map(re.escape, keys)), re.IGNORECASE)
def format_term(term):
    return '[%s](%s)' % (term, term.lower())

converted_text = pattern.sub(lambda m: format_term(m.group(0)), text)

rawDescription: How to replace by regular expression to lowercase in python

description: Replace by regular expression to lowercase in python


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

# Declare numpy array with nans
y=np.array([np.nan,10005,10005,10006,10006,10006,10007,10007,10007, 10008,10007,10008,10008,10008, np.nan,10010,10010,10010, np.nan, np.nan, np.nan, np.nan])
x=np.arange(y.size)

for yv in np.unique(y):
    if yv != np.nan:
        idx = y == yv
        plt.plot(x[idx],y[idx],marker='o',linestyle='-',color='b')
plt.margins(.1,.1)
plt.show()

rawDescription: Connect points with same value in python matplotlib


code:
from matplotlib import pyplot as plt
plt.plot(range(10))
plt.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom='off',      # ticks along the bottom edge are off
    top='off',         # ticks along the top edge are off
    labelbottom='off') # labels along the bottom edge are off
plt.show()
plt.savefig('plot')
plt.clf()

rawDescription: Remove xticks in a matplot lib plot?

description: Remove xticks in a matplot lib plot


code: rounded_val = len(src_string) & (-2)

rawDescription: Whats the best way to round an odd number to an even one?


code:
from IPython import embed
embed()

rawDescription: Equivalent IPython code in .12 series

description: Equivalent IPython code in .12 series


code:
import fnmatch
import os

targets = ['Hello', 'World', 'Foo', 'Bar']
matches = []
for root, dirnames, filenames in os.walk('BrowserClient'):
  for filename in fnmatch.filter(filenames, 'file.png'):
    for word in open(filename, 'r').read().split():
      for item in target:
        if word == item:
          matches.append([filename, item])

rawDescription: How can I check if a string exists in any file within a directory / subdirectory?

description: Check if a string exists in any file within a directory / subdirectory


code:
arguments = ('a', 1, 10)
somefunction(*(arguments[:2] + [10]))

rawDescription: Is there anyway to get the names of passed arguments to a function in python?


rawDescription: Solving 'Triangle Quest' puzzle in Python


code:
# resize your img

# create file
file_name = files.blobstore.create(mime_type='application/octet-stream')
with files.open(file_name, 'a') as f:
    f.write(img)

# Finalize the file. Do this before attempting to read it.
files.finalize(file_name)

# Get the file's blob key
blob_key = files.blobstore.get_blob_key(file_name)

rawDescription: Resize Image before Upload to Google App engine


code:
def foo(path, field_storage, temporary):
    """This is function foo

    Parameters
    ----------
    path : str
        The path of the file to wrap
    field_storage : :class:`FileStorage`
        The :class:`FileStorage` instance to wrap
    temporary : bool
        Whether or not to delete the file when the File instance
        is destructed

    Returns
    -------
    describe : type
        Explanation
    ...

    Examples
    --------
    These are written in doctest format, and should illustrate how to
    use the function.

    >>> a=[1,2,3]
    >>> print [x + 3 for x in a]
    [4, 5, 6]
    ...
    """

    pass

rawDescription: Are there any real alternatives to reStructuredText for Python documentation?


code:
for y in agg.columns:
    if(agg[y].dtype == np.float64 or agg[y].dtype == np.int64):
          treat_numeric(agg[y])
    else:
          treat_str(agg[y])

rawDescription: how to check the dtype of a column in python pandas

description: Check the dtype of a column in python pandas


code:
import numpy as np
import pandas as pd
from matplotlib import pylab

z=pd.DataFrame( np.array([[1,2,3],[1,3,2]]).T )

z.plot(marker='o') # Plot the data, with a marker set.
pylab.xlim(0,3) # Change the axes limits so that we can see the annotations.
pylab.ylim(0,4)
ax = pylab.gca()
for i in z.index: # iterate through each index in the dataframe
    for v in z.ix[i].values: # and through each value being plotted at that index
        # annotate, at a slight offset from the point.
        ax.annotate(str(v),xy=(i,v), xytext=(5,5), textcoords='offset points')

rawDescription: How to set marker style of Dataframe plot in Python Pandas?

description: Set marker style of Dataframe plot in Python Pandas


code:
br.addheaders = [('user-agent', '   Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3'),
('accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')]

rawDescription: Python, mechanize, proper syntax for setting multiple headers?


code:
import os
import subprocess

path = '/tmp/mypipe'
if not os.path.exists(path):
    os.mkfifo(path)

with open(path, 'w') as f:
    subprocess.call(['cat'], stdout=f)

rawDescription: Python Keep Named Pipe Open

description: Python Keep Named Pipe Open


code:
path_to_temporary_directory = tempfile.mkdtemp()
# work on the temporary directory
# ...
# move the necessary files to the destination
shutil.move(source, destination)
# delete the temporary directory (recommended)
shutil.rmtree(path_to_temporary_directory)

rawDescription: Compile latex from python

description: Compile latex from python


code:
with open(filename, "rt") as f:
    a = eval(f.read())

rawDescription: Python 2.7: set the value of a variable with a symbolic expression written in a text file


code:
from collections import Counter
d = {'apple': {'a': 1, 'b': 4, 'c': 2}, 'orange': {'a': 4, 'c': 5}, 'pear': {'a': 1, 'b': 2}}
sum(map(Counter, d.values()), Counter())

rawDescription: summing items in nested dictionary with different keys

description: Summing items in nested dictionary with different keys


code:
class Person(db.Model):
  name = db.StringProperty(required = True)
  profession = db.StringProperty(required = True)
  driverLicense = db.IntegerProperty(required = True)
  address = db.PostalAdressProperty(required = True)

class Contract(db.Model):
  idContract = db.IntegerProperty(required = True)
  contractType = db.StringProperty(required = True, choices=set(["Purchase Agreement",        "Rental House", "Rental Car"]))
  contractDate =  db.DateProperty (required = True, auto_now = True, auto_now_add = True)
  place = db.StringProperty(required = True)

class ContractingParty(db.Model):
  person = db.ReferenceProperty(People, required=True, collection_name="party_to_contracts")
  condition = db.StringProperty(required = False, choices=set(["buyer", "seller", "renter", "owner", "witness"]))

rawDescription: How to model a contract database (with several buyers or sellers) using GAE datastore

description: Model a contract database (with several buyers or sellers) using GAE datastore


code: indata = open(from_file).read()

rawDescription: Learn Python the Hard Way Exercise 17 Extra Question(S)


code: your_canvas_widget.create_line(x, y, x + 1, y)

rawDescription: TkInter: draw one pixel

description: TkInter: draw one pixel


code:
#1 Open file and read it line by line
#2 Split each line in array, delimiter space
#3 If array lenght != 0
##4 If array[0] == START_DATE
###4 array[1] = Your_new_date
###4 array[1] = Your_new_time
#5 Register and close the file

rawDescription: Python - search and replace adjacent text in tab seperated text file


code:
f = MyForm({})
f.has_changed()
f = MyForm({})
f.has_changed()
f = MyForm({'name': 'test'})
f.has_changed()
f = MyForm({'name': 'test'}, initial={'name': 'test'})
f.has_changed()

rawDescription: Django: How to check if the user left all fields blank (or to initial values)?


code:
def outerFunction():
    class Helper:
        val = None
    helper = Helper()

    def innerFunction():
        helper.val = "some value"

rawDescription: How do I access outer functions variables inside a closure(python 2.6)?

description: Access outer functions variables inside a closure(python 2.6)


code:
d = pandas.DataFrame(np.random.randn(3, 2), columns=["A", "B"])
d
d.Col = 8
d.Col    # Attribute is there
d['Col']    # But it is not a columns, just a simple attribute
d['Col'] = 100    # Create a real column
d.Col    # Attribute blocks access to column
d['Col']    # Column is available via item access
del d.Col    # Delete the attribute
d.Col     # Columns is now available as an attribute (!)
d['Col']    # And still as an item

rawDescription: my_dataframe.new_column = value?

description: My_dataframe.new_column = value


code:
if ((opponentBoard[row][col] == const.MISSED) or (opponentBoard[row][col] == const.HIT) or (row == 12)):
    if ((row > 5) and (col < 6)):
       exec_func();

rawDescription: ORs and AND in if statement in Python

description: ORs and AND in if statement in Python


code: hex(ord(b))

rawDescription: python: convert UUID to a string which is a C unsigned char[16] initializer

description: Python: convert UUID to a string which is a C unsigned char[16] initializer


code:
import httplib
from socket import error

with open("in.txt") as f, open("out.txt","a") as out: # change to w if you want to overwrite each time
    for line in f:
        try:
            conn = httplib.HTTPConnection(line.rstrip())
            conn.request("HEAD", "/index.php")
            r1 = conn.getresponse()
            print(r1.reason)
            if r1.reason == "OK":
                out.write(line)
        except error as e:
            print(e)

rawDescription: How to to do a index.php check for each IP in text file

description: To do a index.php check for each IP in text file


code:
l = ['a', 'b', 'c', 'b', 'a', 'd']
import itertools as it
dict(it.izip(reversed(l), reversed(xrange(len(l)))))

rawDescription: Create a position-mapped dictionary from a list in python


code:
tmp = np.bincount(idx, w)
v[:len(tmp)] += tmp

rawDescription: Numpy accumulating one array in another using index array


rawDescription: Python check if an item is in a list

description: Python check if an item is in a list


code:
import pyaudio
import struct
import math

SHRT_MAX=32767 # short uses 16 bits in complement 2

def my_sin(t,frequency):
    radians = t * frequency * 2.0 * math.pi
    pulse = math.sin(radians)
    return pulse

#pulse_function creates numbers in [-1,1] interval
def generate(duration = 5,pulse_function = (lambda t: my_sin(t,1000))):
    sample_width=2
    sample_rate = 44100
    sample_duration = 1.0/sample_rate
    total_samples = int(sample_rate * duration)
    p = pyaudio.PyAudio()
    pformat = p.get_format_from_width(sample_width)
    stream = p.open(format=pformat,channels=1,rate=sample_rate,output=True)
    for n in range(total_samples):
        t = n*sample_duration
        pulse = int(SHRT_MAX*pulse_function(t))
        data=struct.pack("h",pulse)
        stream.write(data)

#example of a function I took from wikipedia.
major_chord = f = lambda t: (my_sin(t,440)+my_sin(t,550)+my_sin(t,660))/3

#choose any frequency you want
#choose amplitude from 0 to 1
def create_pulse_function(frequency=1000,amplitude=1):
    return lambda t: amplitude * my_sin(t,frequency)

if __name__=="__main__":
    # play fundamental sound at 1000Hz for 5 seconds at maximum intensity
    f = create_pulse_function(1000,1)
    generate(pulse_function=f)
    # play fundamental sound at 500Hz for 5 seconds at maximum intensity
    f = create_pulse_function(500,1)
    generate(pulse_function=f)
    # play fundamental sound at 500Hz for 5 seconds at 50% intensity
    f = create_pulse_function(500,0.5)
    generate(pulse_function=f)

rawDescription: synthesizing an audio pitch in python

description: Synthesizing an audio pitch in python


code:
for s in s_tuples:
    if l > max_length:
        break
    output.append(s[0])
    l += len(get_words_from(s[0]))
return ''.join(output)

rawDescription: Python string append

description: Python string append


code:
import re
if re.match(r'http://localhost:13555/ChessBoard_x\d\d_y\d\d\.bmp', URL):
    print("TRUE")
else:
    print("FALSE")

rawDescription: how to compare two strings ignoring few characters

description: Compare two strings ignoring few characters


code:
import sys, thread
from Tkinter import *
from os import system as run
from time import sleep

r = Tk()
r.title('Remote Support')
t = StringVar()
t.set('Completing Remote Support Initalisation         ')
l = Label(r,  textvariable=t).pack()
def quit():
    #do cleanup if any
    r.destroy()
but = Button(r, text='Stop Remote Support', command=quit)
but.pack(side=LEFT)

def d():
    sleep(2)
    t.set('Completing Remote Support Initalisation, downloading, please wait         ')
    run('sleep 5') #test shell command
    t.set('Preparing to run download, please wait         ')
    run('sleep 5')
    t.set("OK thanks! Remote Support will now close         ")
    sleep(2)
    quit()

sleep(2)
thread.start_new_thread(d,())
r.mainloop()

rawDescription: Basic GUI for shell commands with Python Tk threading and os.system calls


code: 'select * from table offset %s limit %s order by %s' % (0,5,'sort_column')

rawDescription: safely specifying 'order by' clause from user input in python / postgresql / psycopg2

description: Safely specifying 'order by' clause from user input in python / postgresql / psycopg2


code: command = 'ssh -t -t buildMachine.X.lan \'sudo su - buildbot  \'build-set sets/set123\'\''

rawDescription: using subprocess to ssh and execute commands

description: Using subprocess to ssh and execute commands


code:
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(myWidget, proportion=1, flag=wx.EXPAND|wx.ALL, border=5)
sizer.Add(myOtherWidget, proportion=0, flag=wx.ALL, border=5)

rawDescription: Change cell size in python wx widget

description: Change cell size in python wx widget


code:
import os

keep = True
with open(pathToInput) as infile, open(pathToOutput, 'w') as outfile, open(pathToSave) as savefile:
    for line in infile:
        if line.startswith("RBE3"):
            keep = False
        elif not line.startswith("*"):
            keep = True
        if keep:
            outfile.write(line)
        else:
            savefile.write(line)

os.remove(pathToInput)
os.rename(pathToOutput, pathToInput)

rawDescription: Remove all lines between the pattern

description: Remove all lines between the pattern


code:
mat = np.arange(100).reshape(10, -1)
rdd = sc.parallelize(mat)

np.all(rdd.first() == mat[0])
## True

rawDescription: convert numpy matrix into pyspark rdd

description: Convert numpy matrix into pyspark rdd


code:
__name__
x = __name__
id(__name__)
id(x)
__name__ is x

rawDescription: Python id of name and '__main__' different


code:
import wx
from matplotlib.patches import Rectangle
from matplotlib.widgets import Lasso
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg

class MyNavToolbar(NavigationToolbar2WxAgg):
    """wx/mpl NavToolbar hack with an additional tools user interaction.
    This class is necessary because simply adding a new togglable tool to the
    toolbar won't (1) radio-toggle between the new tool and the pan/zoom tools.
    (2) disable the pan/zoom tool modes in the associated subplot(s).
    """
    ID_LASSO_TOOL = wx.NewId()
    def __init__(self, canvas):
        super(NavigationToolbar2WxAgg, self).__init__(canvas)

        self.pan_tool  = self.FindById(self._NTB2_PAN)
        self.zoom_tool = self.FindById(self._NTB2_ZOOM)

        self.lasso_tool = self.InsertSimpleTool(5, self.ID_LASSO_TOOL,
                            wx.ArtProvider.GetBitmap(wx.ART_ADD_BOOKMARK),
                            isToggle=True)
        self.Bind(wx.EVT_TOOL, self.on_toggle_lasso_tool, self.lasso_tool)
        self.Bind(wx.EVT_TOOL, self.on_toggle_pan_zoom, self.zoom_tool)
        self.Bind(wx.EVT_TOOL, self.on_toggle_pan_zoom, self.pan_tool)

    def get_mode(self):
        """Use this rather than navtoolbar.mode
        """
        if self.lasso_tool.IsToggled():
            return 'lasso'
        else:
            return self.mode

    def untoggle_mpl_tools(self):
        """Hack city: Since I can't figure out how to change the way the
        associated subplot(s) handles mouse events: I generate events to turn
        off whichever tool mode is enabled (if any).
        This function needs to be called whenever any user-defined tool
        (eg: lasso) is clicked.
        """
        if self.pan_tool.IsToggled():
            wx.PostEvent(
                self.GetEventHandler(),
                wx.CommandEvent(wx.EVT_TOOL.typeId, self._NTB2_PAN)
            )
            self.ToggleTool(self._NTB2_PAN, False)
        elif self.zoom_tool.IsToggled():
            wx.PostEvent(
                self.GetEventHandler(),
                wx.CommandEvent(wx.EVT_TOOL.typeId, self._NTB2_ZOOM)
            )
            self.ToggleTool(self._NTB2_ZOOM, False)

    def on_toggle_lasso_tool(self, evt):
        """Lasso tool handler.
        """
        if evt.Checked():
            self.untoggle_mpl_tools()

    def on_toggle_pan_zoom(self, evt):
        """Called when pan or zoom is toggled.
        We need to manually untoggle user-defined tools.
        """
        if evt.Checked():
            self.ToggleTool(self.ID_LASSO_TOOL, False)
        # Make sure the regular pan/zoom handlers get the event
        evt.Skip()

class ScatterPanel(FigureCanvasWxAgg):
    """Contains the guts for drawing scatter plots.
    """
    def __init__(self, parent, **kwargs):
        self.figure = Figure()
        FigureCanvasWxAgg.__init__(self, parent, -1, self.figure, **kwargs)
        self.canvas = self.figure.canvas
        self.SetMinSize((100,100))
        self.figure.set_facecolor((1,1,1))
        self.figure.set_edgecolor((1,1,1))
        self.canvas.SetBackgroundColour('white')

        self.subplot = self.figure.add_subplot(111)
        self.navtoolbar = None
        self.lasso = None
        self.redraw()

        self.canvas.mpl_connect('button_press_event', self.on_press)
        self.canvas.mpl_connect('button_release_event', self.on_release)

    def lasso_callback(self, verts):
        pass

    def on_press(self, evt):
        """canvas mousedown handler
        """
        if evt.button == 1:
            if self.canvas.widgetlock.locked():
                return
            if evt.inaxes is None:
                return
            if self.navtoolbar and self.navtoolbar.get_mode() == 'lasso':
                self.lasso = Lasso(evt.inaxes, (evt.xdata, evt.ydata), self.lasso_callback)
                self.canvas.widgetlock(self.lasso)

    def on_release(self, evt):
        """canvas mouseup handler
        """
        # Note: lasso_callback is not called on click without drag so we release
        #   the lock here to handle this case as well.
        if evt.button == 1:
            if self.lasso:
                self.canvas.draw_idle()
                self.canvas.widgetlock.release(self.lasso)
                self.lasso = None
        else:
            self.show_popup_menu((evt.x, self.canvas.GetSize()[1]-evt.y), None)

    def redraw(self):
        self.subplot.clear()
        self.subplot.scatter([1,2,3],[3,1,2])

    def get_toolbar(self):
        if not self.navtoolbar:
            self.navtoolbar = MyNavToolbar(self.canvas)
            self.navtoolbar.Realize()
        return self.navtoolbar

if __name__ == "__main__":
    app = wx.PySimpleApp()
    f = wx.Frame(None, size=(600,600))
    p = ScatterPanel(f)
    f.SetToolBar(p.get_toolbar())
    f.Show()
    app.MainLoop()

rawDescription: Add new navigate modes in matplotlib

description: Add new navigate modes in matplotlib


code:
for host in ip_addr_list:
    port = "23"
    user = "test"
    password = "test"
    print(host)
    tn = telnetlib.Telnet(host,port)
    tn.read_until(b"ser name:")
    tn.write(user.encode('ascii')+b"\n")
    tn.read_until(b"assword:")
    tn.write(password.encode('ascii')+b"\n")
    tn.write(b"sh ru"+b"\n")
    tn.write(b"exit"+b"\n")
    str_all = tn.read_all()
    re.sub("^\s+|\n|\r|\s+$", '', str_all.decode())
    file = open('{0}'.format(host),"wb")
    file.write(str_all)
    num_ips += 1

rawDescription: Python IP output (print) is not working correctly


code:
regex = '(?P<name>
(.*?)
)' # Note the extra group inside the

def slugify(s):
    return s.replace(' ', '-') # bare-bones slugify

def anchorize(matchobj):
    return '%s' % (slugify(matchob.group(2)), matchobj.group(1))

text = re.sub(regex, anchorize, text)

rawDescription: Adding anchors to h2 in text using python and regexp


code:
myformatter.format('{user!u} did la-dee-dah on {date}, and {pronoun!l} liked it. ',
                      user=x, date=y, pronoun=z)

rawDescription: Python: Capitalize a word using string.format()


code:
for key, value in legJointConnectors.iteritems():
    if side != 'L':
        key = 'R_' + key[2:]
        value = ['R_' + v[2:] for v in value]
    cmds.connectJoint(value, key, pm=True)

rawDescription: Rename dictionary keys/values in python

description: Rename dictionary keys/values in python


code:
def lcs3(a, b, c):
    n = len(a)
    m = len(b)
    l = len(c)

    if 0 in (n, m, l):
        return 0

    E = [[[0] * (l + 1) for _ in range(m + 1)] for _ in range(n + 1)]

    for i in reversed(range(n + 1)):
        for j in reversed(range(m + 1)):
            for k in reversed(range(l + 1)):
                if i == n or j == m or k ==l:
                    E[i][j][k] = 0
                elif a[i] == b[j] == c[k]:
                    E[i][j][k] = 1 + E[i + 1][j + 1][k + 1]
                else:
                    E[i][j][k] = max(E[i + 1][j][k], E[i][j + 1][k], E[i][j][k + 1])

    return E[0][0][0]

rawDescription: longest common subsequence of three integer sequences

description: Longest common subsequence of three integer sequences


code:
import getpass

password = getpass.getpass()
print(password)

rawDescription: How do I convert a password into asterisks while it is being entered?

description: Convert a password into asterisks while it is being entered


code:
import calendar
timestamp = calendar.timegm(datetime.utcnow().timetuple())

rawDescription: Why does a date generated after another can appear to be before the previous one in Python?


code:
from pymongo import MongoClient
CONNECTION = MongoClient('/tmp/mongodb-27017.sock')

rawDescription: How to connect to mongodb via unix socket in python

description: Connect to mongodb via unix socket in python


code:
#first create a sprite class
class Card(pygame.sprite.Sprite):
    def __init__(self,img,pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(os.path.join('img','cards', img))
        self.rect = self.image.get_rect()
        self.rect.center = pos

#then create a group
mycards = pygame.sprite.Group()

#then add a sprite to the group
holders.add(Card('ace_spade.jpg',coord))

rawDescription: Pygame group update takes no arguments

description: Pygame group update takes no arguments


code:
import getpass
getpass.getpass  # the getpass function
getpass.getpass('Enter password: ')  # call it with a prompt

rawDescription: Special Characters For Python

description: Special Characters For Python


code:
# post.content here is uploaded content of text field
post.content = re.sub(
    r'<img (class=".*?" )?src="data:image/(?P<fmt>.+?);base64,(?P<dat>(?:[A-Za-z0-9+/]{4}){2,}(?:[A-Za-z0-9+/]{2}[AEIMQUYcgkosw048]=|[A-Za-z0-9+/][AQgw]==))".*?>',
    lambda x: proc_blob(post, x), post.content)

def proc_blob(post, mo):
    data = mo.group('dat')
    fmt = mo.group('fmt')
    fname = "{}_{}.{}".format(post.id, str(time.time()).replace('.', '_'), fmt)
    full_name = os.path.join(IMAGES_ROOT, fname)
    fh = open(full_name, "wb")
    fh.write(base64.b64decode(data))
    fh.close()

    # you can apply a watermark here

    # Optionally in addition we can store image filename in database (for example to remeber what images are related to post)
    im = app.models.Image(post=post, filename=fname)
    im.save()

    return '<img src="/images/{}" class="img-responsive" />'.format(fname)

rawDescription: Store blob image on server with python

description: Store blob image on server with python


code:
lib.test_function(u'x'.encode('utf8'),
                  u'y'.encode('utf8'),
                  u'z'.encode('utf8'))

rawDescription: How can ctypes be used to parse unicode strings?


code:
doc = open('your file')
for line in doc.readline():
    if len(line) != 0:
        line.split(',')

rawDescription: Iterating the lines of a text file and then returning a dictionary in python?


code:
def __call__(self, tag, *args):
    # produce a context manager
    cm = WriterContext(self, tag)
    if args:
        with cm:
            for arg in args:
                self.write(arg)
    else:
        return cm

rawDescription: Determine if called from with statement

description: Determine if called from with statement


code:
@route('/form', method='POST')
def print_form():
    value = request.forms.get('answer')
    time_given_by_server =  request.forms.get('time_given_by_server')#also hidden

    captcha = hashlib.sha1("%s%s"% (request.forms.get('captcha'), time_give_by_server)).hexdigest()
    if value != catcha or time.time()-time_given_by_server > (60*5):#5 mintues is ok i guess...
        return 'Wrong.'
    else:
        return 'Correct.'

rawDescription: Implementing Simple CAPTCHA in Bottle

description: Implementing Simple CAPTCHA in Bottle


code:
infodict = {
    'queryset': Post.objects.all(),
    'date_field': 'date',
    'template_name': 'index.html',
    'template_object_name': 'latest_post_list',
}

urlpatterns = patterns('django.views.generic.date_based',
    url(r'^gindex/$', 'archive_index', dict(infodict, template_name='gindex.html')),
    url(r'^hindex/$', 'archive_index', dict(infodict, template_name='hindex.html')),
)

rawDescription: Specifying different template names in Django generic views


code:
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.base import runTouchApp

dropdown = DropDown()
dropdown.size_hint_y = 0.5
for index in range(100):
    btn = Button(text='Value %d' % index, size_hint_y=None, height=44)
    btn.bind(on_release=lambda btn: dropdown.select(btn.text))
    dropdown.add_widget(btn)

mainbutton = Button(text='Hello', size_hint=(None, None))
mainbutton.bind(on_release=dropdown.open)

dropdown.bind(on_select=lambda instance, x: setattr(mainbutton, 'text', x))

runTouchApp(mainbutton)

rawDescription: Kivy: Limit size of dropdown

description: Kivy: Limit size of dropdown


code: print( len(test['foo']))

rawDescription: Know how many entries a key has in a dictionary


code:
def Loans(x):
    x = int(input("What is your FICO score? "))
    if x >= 760:
           print ("Rate = 3.080")
    elif x >= 700:
           print ("Rate = 3.302")
    elif x >= 680:
           print ("Rate = 3.479")
    elif x >= 660:
           print ("Rate = 3.693")
    elif x >= 640:
           print ("Rate = 4.123")
    elif x >= 620:
           print ("Rate = 4.66")
    else:
           print ('Approval = No')

Loans(10)

rawDescription: Python - Not executing function

description: Python - Not executing function


code:
from PyQt4 import QtCore, QtGui

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.central_widget = QtGui.QStackedWidget()
        self.setCentralWidget(self.central_widget)
        login_widget = LoginWidget(self)
        login_widget.button.clicked.connect(self.login)
        self.central_widget.addWidget(login_widget)
    def login(self):
        logged_in_widget = LoggedWidget(self)
        self.central_widget.addWidget(logged_in_widget)
        self.central_widget.setCurrentWidget(logged_in_widget)

class LoginWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(LoginWidget, self).__init__(parent)
        layout = QtGui.QHBoxLayout()
        self.button = QtGui.QPushButton('Login')
        layout.addWidget(self.button)
        self.setLayout(layout)
        # you might want to do self.button.click.connect(self.parent().login) here

class LoggedWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(LoggedWidget, self).__init__(parent)
        layout = QtGui.QHBoxLayout()
        self.label = QtGui.QLabel('logged in!')
        layout.addWidget(self.label)
        self.setLayout(layout)

if __name__ == '__main__':
    app = QtGui.QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()

rawDescription: How do I switch layouts in a window using PyQt?? (Without closing/opening windows)

description: Switch layouts in a window using PyQt?? (Without closing/opening windows)


rawDescription: Python ( email.message_from_string ) how to find out which values i can use : subject , to, from, etc..


code:
import Tkinter as tk

class App:
    def __init__(self):
        self.root=tk.Tk()
        self.vsb = tk.Scrollbar(orient="vertical", command=self.OnVsb)
        self.lb1 = tk.Listbox(self.root, yscrollcommand=self.vsb.set)
        self.lb2 = tk.Listbox(self.root, yscrollcommand=self.vsb.set)
        self.vsb.pack(side="right",fill="y")
        self.lb1.pack(side="left",fill="x", expand=True)
        self.lb2.pack(side="left",fill="x", expand=True)
        self.lb1.bind("<MouseWheel>", self.OnMouseWheel)
        self.lb2.bind("<MouseWheel>", self.OnMouseWheel)
        for i in range(100):
            self.lb1.insert("end","item %s" % i)
            self.lb2.insert("end","item %s" % i)
        self.root.mainloop()

    def OnVsb(self, *args):
        self.lb1.yview(*args)
        self.lb2.yview(*args)

    def OnMouseWheel(self, event):
        self.lb1.yview("scroll", event.delta,"units")
        self.lb2.yview("scroll",event.delta,"units")
        # this prevents default bindings from firing, which
        # would end up scrolling the widget twice
        return "break"

app=App()

rawDescription: Scrolling multiple Tkinter listboxes together


code:
binds = {}

finance_engine = create_engine(url1)
binds.update(dict.fromkeys(finance_metadata.sorted_tables, finance_engine))
# The following line is required when mappings to joint tables are used (e.g.
# in joint table inheritance) due to bug (or misfeature) in SQLAlchemy 0.5.4.
# This issue might be fixed in newer versions.
binds.update(dict.fromkeys([Employee, Customer, Invoice], finance_engine))

staff_engine = create_engine(url2)
binds.update(dict.fromkeys(staff_metadata.sorted_tables, staff_engine))
# See comment above.
binds.update(dict.fromkeys([Project, Hour], staff_engine))

session = sessionmaker(binds=binds)()

rawDescription: With sqlalchemy how to dynamically bind to database engine on a per-request basis


code:
from __future__ import print_function

import httplib2
from oauth2client.service_account import ServiceAccountCredentials
import gdata.apps.emailsettings.client
import gdata.gauth

def setup_credentials():
        key_path = '/Users/tim/Dropbox/pycharm_projects_27/gmail_admin/<blabla>.json'
        API_scopes =['https://apps-apis.google.com/a/feeds/emailsettings/2.0/']
        credentials = ServiceAccountCredentials.from_json_keyfile_name(key_path, scopes=API_scopes)
        return credentials

if __name__ == "__main__":
    credentials = setup_credentials()

    # pass the email address of a domain super-administrator
    delegated_credentials = credentials.create_delegated('tim@vci.com.au')
    http = httplib2.Http()
    #http = credentials.authorize(http)
    http = delegated_credentials.authorize(http)  #this is necessary

    url_get_sig = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/vci.com.au/tim/signature'
    r = http.request(url_get_sig,"GET")
    print (r)

    # now use the library
    client = gdata.apps.emailsettings.client.EmailSettingsClient(domain='vci.com.au')
    auth2token = gdata.gauth.OAuth2TokenFromCredentials(delegated_credentials)
    auth2token.authorize(client)
    r = client.retrieve_signature('tim')
    print (client)

rawDescription: Manage gmail signatures with python service account


rawDescription: Remove the 0b in binary

description: Remove the 0b in binary


code: data = request.body.read()

rawDescription: bottle python put equivalent to post for data

description: Bottle python put equivalent to post for data


rawDescription: how to use the output/value of variable in python

description: Use the output/value of variable in python


code:
from Tkinter import (Tk, Frame, Button, Entry, Canvas, Text,
    LEFT, DISABLED, NORMAL, RIDGE, END)

rawDescription: Python import as tuple

description: Python import as tuple


code: message = "You need to burn another {} logs to level up".format(wfmlvl - fmlvl)

rawDescription: Python 2.7.10 Tkinter curly brackets around text in textbox


code:
import os
import zipfile

zipfiles = [z for z in os.listdir('.') if z.endswith('.zip')]

for zfile in zipfiles:
    with zipfile.ZipFile(zfile) as z:
        with z.open('ans.txt') as f:
            if b'marks=' in f.read().lower():
                print(z.filename)

rawDescription: How to read files from every zip folder present in a directory

description: Read files from every zip folder present in a directory


code:
print(...)
option = input(...)
option_class = input(...)

if option.lower() == 'avg' and option_class == '1':  # .lower() on option.
    name = input(...)
    with open(one) as f:  # '-r' is the default so omitting it is cleaner
        # 1. iterate the file handler directly without .readlines().
        # 2. use list comprehension
        # 3. avoid shadowing "list" by using, say, "the_list"
        the_list = [int(l.strip().split()[-1]) for l in f if l.strip() and name in l.split()]

        b = sum(the_list)
        length = len(the_list)
        # 1. dont repeat sum() & len() calls
        # 2. default to 0 avg to avoid division by 0.
        avg = float(b) / length if length else 0

rawDescription: How to work out average of scores from a text file Python

description: Work out average of scores from a text file Python


code:
[(key, value) for key, values in input.items() for value in values]
[(1, 23), (1, 24), (2, 21), (3, 23)]
'\n'.join('({},{})'.format(key, value) for key, values in input.items() for value in values)
print('\n'.join('({},{})'.format(key, value) for key, values in input.items() for value in values))
','.join('({},{})'.format(key, value) for key, values in input.items() for value in values)

rawDescription: Python Remove two for loop and solve using some iterator tool


code:
from Tkinter import *

class GUI():
    def __init__(self):
        self.namelist = ["Mark","Anna","Jason","Lenna",
                         "Leo","Zucharich","Robinson",
                         "AReallyLongNameThatMightExist"]
        self.canvas = Canvas(width=1200,height=700)
        self.canvas.pack(expand=YES,fill=BOTH)

    def Friends(self):
        frame = Frame(self.canvas)
        frame.place(x=600,y=300)
        #Frame for showing names of friends
        row = 0
        for x in self.namelist:
            label = Label(frame,text="%s "%x)
            chatButton = Button(frame,text="Chat")
            delButton = Button(frame,text="Delete")
            setcloseButton = Button(frame,text="Set Close")
            setgroupButton = Button(frame,text="Set Group")
            label.grid(row=row, column=0, sticky="W")
            chatButton.grid(row=row, column=1)
            delButton.grid(row=row, column=2)
            setcloseButton.grid(row=row, column=3)
            setgroupButton.grid(row=row, column=4)
            row = row + 1
        mainloop()

GUI = GUI()
GUI.Friends()

rawDescription: Aligning Label in Frame Tkinter

description: Aligning Label in Frame Tkinter


rawDescription: Python - transform a list

description: Python - transform a list


rawDescription: Python pyCrypto RSA encrypt method gives same results using private or public key


code:
class CustomerResource(ModelResource):

    locations = fields.ToManyField('device.resources.LocationResource',
            'location_set', null=True)
    current_location = fields.ToOneField('device.resources.LocationResource',
            'current_location', null=True)
    default_location = fields.ToOneField('device.resources.LocationResource',
            'default_location', null=True)

    class Meta:
        queryset = Customer.objects.all()
        resource_name = 'customers'
        validation = CleanedDataFormValidation(form_class=RegistrationForm)
        list_allowed_methods = ['get', 'post']
        detail_allowed_methods = ['get', 'put', 'patch', 'delete']
        authorization = Authorization()
        excludes =['is_superuser', 'is_active', 'is_staff', 'password', 'last_login',]
        filtering = {
            'location': ('exact'),
        }

    def obj_create(self, bundle, **kwargs):
        bundle.obj = self._meta.object_class.objects.create_user(
            username=kwargs['username'],
            email=kwargs['email'],
            password=kwargs['password1'],
        )
        return bundle

rawDescription: Create a User Object with a Call to TastyPie API


code:
accepted_strings = {'auth', 'authpriv', 'daemon'}

if facility in accepted_strings:
    do_stuff()

rawDescription: Comparing a string to multiple items in Python


code:
mymodels = MyModel.objects.filter(status=1)
objs = [obj for obj in mymodels] # save the objects you are about to update
mymodels.update(status=4)
print(objs)

rawDescription: How to modify a model after bulk update in django?

description: Modify a model after bulk update in django


code:
import os
import shutil
from os import path
from os.path import join, getmtime
from time import time

archive = "bak"
current = "cur"

def archive_old_versions(days = 3):
    for root, dirs, files in os.walk(current):
        for name in files:
            fullname = join(root, name)
            if (getmtime(fullname) < time() - days * 60 * 60 * 24):
                shutil.move(fullname, join(archive, name))

rawDescription: How to express this Bash command in pure Python

description: Express this Bash command in pure Python


code:
for line in sorted(os.listdir(d)):
    f.write(line + "\n")

rawDescription: Python os.listdir() keeps skipping over some files?


code:
class PLMSpider(CrawlSpider):
    name = 'plm'
    allowed_domains = ["patientslikeme.com"]

    # pseudo-start_url
    login_url = "https://www.patientslikeme.com/login"

    # start URLs used after login
    start_urls = [
        "https://www.patientslikeme.com/forum/diabetes2/topics",
    ]

    rules = (
        # you want to do the login only once, so it's probably cleaner
        # not to ask the CrawlSpider to follow links to the login page
        #Rule(SgmlLinkExtractor(allow=(r"patientslikeme.com/login")), callback='login_parse', follow=True),

        # you can also deny "/login" to be safe
        Rule(SgmlLinkExtractor(restrict_xpaths=("//div[@class='content-section']"),
                               deny=('/login',)),
             callback='post_parse', follow=False),

        Rule(SgmlLinkExtractor(restrict_xpaths=("//div[@class='pagination']"),
                               deny=('/login',)),
             callback='page_parse', follow=True),
    )

    def __init__(self, **kwargs):
        ScrapyFileLogObserver(open("debug.log", 'w'), level=logging.DEBUG).start()
        CrawlSpider.__init__(self, **kwargs)

    # by default start_urls pages will be sent to the parse method,
    # but parse() is rather special in CrawlSpider
    # so I suggest you create your own initial login request "manually"
    # and ask for it to be parsed by your specific callback
    def start_requests(self):
        yield Request(self.login_url, callback=self.login_parse)

    # you've got the login page, send credentials
    # (so far so good...)
    def login_parse(self, response):
        log.msg("Login attempted")
        return [FormRequest.from_response(response,
                    formdata={'userlogin[login]': username, 'userlogin[password]': password},
                    callback=self.after_login)]

    # so we got a response to the login thing
    # if we're good,
    # just do as if we were starting the crawl now,
    # basically doing what happens when you use start_urls
    def after_login(self, response):
        log.msg("Post login")
        if "Login unsuccessful" in response.body:
            self.log("Login failed", level=log.ERROR)
            return
        else:
            return [Request(url=u) for u in self.start_urls]
            # alternatively, you could even call CrawlSpider's start_requests() method directly
            # that's probably cleaner
            #return super(PLMSpider, self).start_requests()

    def post_parse(self, response):
        url = response.url
        log.msg("Post parse attempted for {0}".format(url))
        item = PLMItem()
        item['url'] = url
        return item

    def page_parse(self, response):
        url = response.url
        log.msg("Page parse attempted for {0}".format(url))
        item = PLMItem()
        item['url'] = url
        f = open("body.txt", "w")
        f.write(response.body)
        f.close()
        return item

    # if you want the start_urls pages to be parsed,
    # you need to tell CrawlSpider to do so by defining parse_start_url attribute
    # https://github.com/scrapy/scrapy/blob/master/scrapy/contrib/spiders/crawl.py#L38
    parse_start_url = page_parse

rawDescription: Scrapy CrawlSpider isn't following the links on a particular page


code:
import re

secret_string = "secret string"

pattern = re.compile(r"Enter (\d+)\w+ character")

number_string = driver.find_element_by_css_selector("tr > td.white > input").text
number = int(pattern.search(number_string).group(1))

target_input = driver.find_element_by_css_selector("tr > td.mem > input[type=password]")

target_input.send_keys(secret_string[number])

rawDescription: Random banking type authentication using python and selenium


code:
def get_upload_datetime(myurl):
    info = urllib2.urlopen(myurl).info()
    datetime = re.search("Last-Modified: (.+)", str(info))
    if datetime:
        return datetime.groups()[0]

rawDescription: Get File Upload Time From Server

description: Get File Upload Time From Server


code:
def createDSLAlgorithm(code):
    def coderunner(*args, **kwargs):
        DSLInterpreter().interpret(code, *args, **kwargs)
    return coderunner

rawDescription: Breaking up functions into passive (algorithm) and active (execution) objects


code:
query  = ("""INSERT INTO %s " % cfg['mysql.table']
              "('hostname', 'timestamp', 'metric', 'average', 'peakhigh', 'peaklow', 'gsamp', 'zsamp', 'nsamp')"
              "VALUES ( %s, %s, %s, %s, %s, %s, %s, %s, %s )"""
             )

rawDescription: Building multi-line database queries in python


code:
from itertools import islice

n = 4
with open('file.txt') as f, open('out.txt', 'w') as out:
    out.writelines(islice(f, n-1)) #write the lines before the nth line
    out.writelines(islice(f, 1, None)) #write the rest of the lines except nth

rawDescription: Most efficiently removing the n-th row of tab-delimited txt


rawDescription: python3 - os.path changes

description: Python3 - os.path changes


code:
from pandas import concat
from pandas.stats.moments import expanding_mean, expanding_count

def handler(grouped):
    se = grouped.set_index('Date')['Sale'].sort_index()
    # se is the (ordered) time series of sales restricted to a single basket
    # we can now create a dataframe by combining different metrics
    # pandas has a function for each of the ones you are interested in!
    return  concat(
        {
            'MeanToDate': expanding_mean(se), # cumulative mean
            'MaxToDate': se.cummax(),         # cumulative max
            'SaleCount': expanding_count(se), # cumulative count
            'Sale': se,                       # simple copy
            'PrevSale': se.shift(1)           # previous sale
        },
        axis=1
     )

# we then apply this handler to all the groups and pandas combines them
# back into a single dataframe indexed by (Basket, Date)
# we simply need to reset the index to get the shape you mention in your question
new_df = df.groupby('Basket').apply(handler).reset_index()

rawDescription: Python - time series alignment and "to date" functions


code:
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.priceline.com/")
driver.find_element_by_id("hotel-btn-submit-retl").click()
driver.close()

rawDescription: Is it possible to have python read csv returned from javascript?


code:
#read the csv file
with open('csv_file.csv','r') as f:
   lines=[l.split(',') for l in f.readlines()]
#Aggregate by the 3rd field by the first two fields
import collections
d=collections.defaultdict(list)
for l in lines:
   d[(l[0],l[1])].append(l[2])

rawDescription: How to create an array from a csv file in python

description: Create an array from a csv file in python


code:
def negaMax(self, board, rules, ply, player):
        """ Implements a minimax algorithm. """
        if ply == 0:
            return self.positionEvaluation()

        max_eval = float('-infinity')

        move_list = board.generateMoves(rules, player)
        for move in move_list:
            board.makeMove(move, player)
            currentEval = -self.negaMax(board, rules, ply - 1, board.getOtherPlayer(player))
            board.unmakeMove(move, player)

            if currentEval > max_eval:
                max_eval = currentEval
        return max_eval

rawDescription: Chess negamax function

description: Chess negamax function


code:
with open("test.csv",'r') as f:
    with open("updated_test.csv",'w') as f1:
        f.next() # skip header line
        for line in f:
            f1.write(line)

rawDescription: Removing first line of CSV file - Python


code: dict(zip(col_names, p))

rawDescription: Defining a dictionary with a very large amount of columns


code:
try:
    import MySQLdb as mysql
except ImportError:
    import mymodule as mysql

rawDescription: Selecting a Python Class

description: Selecting a Python Class


code:
class MyApiKeyAuthentication(ApiKeyAuthentication):
    def get_key(self, user, api_key):
        """
        Attempts to find the API key for the user. Uses ``ApiKey`` by default
        but can be overridden.
        """
        from tastypie.models import ApiKey

        try:
            api_key = ApiKey.objects.get(user=user, key=api_key)
            current_time = datetime.utcnow()
            current_time = current_time.replace(tzinfo=pytz.utc)

            week = timedelta(7)

            if not (current_time - api_key.created) < week:
                api_key.delete()
                return self._unauthorized()
            else:
                api_key.created = current_time
                api_key.save()

        except ApiKey.DoesNotExist:
            return self._unauthorized()

        return True

rawDescription: Tastypie auto log out

description: Tastypie auto log out


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

x = np.arange(100).reshape(10,10)
y = np.zeros((10,10))

# Make a region in y that we're interested in...
y[4:6, 1:8] = 1

y = np.ma.masked_where(y == 0, y)

plt.imshow(x, cmap=mpl.cm.bone)
plt.imshow(y, cmap=mpl.cm.jet_r, interpolation='nearest')

plt.show()

rawDescription: Imoverlay in python

description: Imoverlay in python


code:
def user_link(self, obj):
        return '%s' % (
            urlresolvers.reverse('admin:auth_user_change', args=(obj.user.id,)), obj.user
            )
user_link.allow_tags = True
user_link.short_description = 'User'

rawDescription: Django-admin. Link to edit user profile from list_display


code:
from PyQt4 import QtCore, QtGui

try:
  _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
  _fromUtf8 = lambda s: s
class Ui_TextFile(object):
  def setupUi(self, TextFile,file):
    self.text= open(file,"r").read()

    TextFile.setObjectName(_fromUtf8("TextFile"))
    TextFile.resize(683, 531)
    self.scrollArea = QtGui.QScrollArea(TextFile)
    self.scrollArea.setGeometry(QtCore.QRect(10, 50, 661, 471))
    self.scrollArea.setWidgetResizable(True)
    self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
    self.scrollAreaWidgetContents = QtGui.QWidget()
    self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 657, 467))
    self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
    self.textEdit = QtGui.QTextEdit(self.scrollAreaWidgetContents)
    self.textEdit.setGeometry(QtCore.QRect(0, 0, 661, 461))
    self.textEdit.setObjectName(_fromUtf8("textEdit"))
    self.textEdit.setText(self.text)
    self.scrollArea.setWidget(self.scrollAreaWidgetContents)
    self.label = QtGui.QLabel(TextFile)
    self.label.setGeometry(QtCore.QRect(280, 20, 67, 17))
    self.label.setObjectName(_fromUtf8("label"))

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

  def retranslateUi(self, TextFile):
    TextFile.setWindowTitle(QtGui.QApplication.translate("TextFile", "Form", None, QtGui.QApplication.UnicodeUTF8))
    self.label.setText(QtGui.QApplication.translate("TextFile", "Text File", None, QtGui.QApplication.UnicodeUTF8))

class Ui_MainWindow(object):
  def setupUi(self, MainWindow):
    MainWindow.setObjectName(_fromUtf8("MainWindow"))
    MainWindow.resize(559, 207)
    self.centralWidget = QtGui.QWidget(MainWindow)
    self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
    self.label = QtGui.QLabel(self.centralWidget)
    self.label.setGeometry(QtCore.QRect(20, 40, 111, 17))
    self.label.setObjectName(_fromUtf8("label"))
    self.lineEdit = QtGui.QLineEdit(self.centralWidget)
    self.lineEdit.setGeometry(QtCore.QRect(130, 40, 411, 25))
    self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
    self.pushButton = QtGui.QPushButton(self.centralWidget)
    self.pushButton.setGeometry(QtCore.QRect(200, 90, 151, 27))
    self.pushButton.setObjectName(_fromUtf8("pushButton"))
    MainWindow.setCentralWidget(self.centralWidget)
    self.menuBar = QtGui.QMenuBar(MainWindow)
    self.menuBar.setGeometry(QtCore.QRect(0, 0, 559, 23))
    self.menuBar.setObjectName(_fromUtf8("menuBar"))
    MainWindow.setMenuBar(self.menuBar)
    self.mainToolBar = QtGui.QToolBar(MainWindow)
    self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
    MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
    self.statusBar = QtGui.QStatusBar(MainWindow)
    self.statusBar.setObjectName(_fromUtf8("statusBar"))
    MainWindow.setStatusBar(self.statusBar)

    self.retranslateUi(MainWindow)
    QtCore.QObject.connect(self.pushButton , QtCore.SIGNAL("clicked()") , self.OpenIT)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

  def retranslateUi(self, MainWindow):
    MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
    self.label.setText(QtGui.QApplication.translate("MainWindow", "Enter the path :", None, QtGui.QApplication.UnicodeUTF8))
    self.lineEdit.setText(QtGui.QApplication.translate("MainWindow", "Path of the text file...", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Open Text File", None, QtGui.QApplication.UnicodeUTF8))

    def OpenIT(self):
        TextFile = QtGui.QDialog()
        ui = Ui_TextFile()
        ui.setupUi(TextFile,file=str(self.lineEdit.text()))
        TextFile.exec_()

if __name__ == "__main__":
  import sys
  app = QtGui.QApplication(sys.argv)
  MainWindow = QtGui.QMainWindow()
  ui = Ui_MainWindow()
  ui.setupUi(MainWindow)
  MainWindow.show()
  sys.exit(app.exec_())

rawDescription: Opening a text file in another window using push button in PyQt


code:
import struct
result = struct.unpack('i', '\x00\xff\xff\xff')
result[0]

rawDescription: Decode data from nextion display [python]


code:
import numpy
a = numpy.ones((2,4))
a
a.tolist()
type(a.tolist())
type(a.tolist()[0])

rawDescription: Convert 2d numpy array into list of lists


code:
def readInput(fileName):
    try:
        with open(fileName, "r") as fh:
            list_of_players = [line.strip('\n').split(';') for line in fh]
        print("Player data has been loaded\n")
        return list_of_players
    except IOError:
        print("File not found")

def calculate_player_stats(player):
    atBats = float(player[3])
    hits = int(player[5])
    doubles = int(player[6])
    triples = int(player[7])
    homeruns = int(player[8])
    multibase_hits = sum((doubles, triples, homeruns))
    singles = hits - multibase_hits
    totalBases = singles + (2 * doubles) + (3 * triples) + (4 * homeruns)
    # or total_bases = hits + doubles + homeruns + 2 * (triples + homeruns)
    battingAvg = hits / atBats
    sluggingPct = totalBases / atBats
    print(battingAvg)
    print(sluggingPct)
    player.append(battingAvg)
    player.append(sluggingPct)

def calculateStats(list_of_players):
    for player in list_of_players:
        calculate_player_stats(player)

list_of_players = readInput(fileName)
if list_of_players:
    calculateStats(list_of_players)
return list_of_players

rawDescription: TypeError - Issue appending items to list


rawDescription: Generate pixel image in Python from array


code:
import random
def rand_with_parity(start, end):
    return 2*random.randint(0, (end-start)//2)+start

myfile = open('numbers.txt', 'w')

for count in range(3,8,1):
   file_size = rand_with_parity(5,19)
   myfile.write(str(file_size) + '\n')

myfile.close()

rawDescription: Python 3.3 - Generate odd numbers

description: Python 3.3 - Generate odd numbers


code: get_session('Mmusc20090126', False, True) #doctest: +ELLIPSIS

rawDescription: Doctest for dynamically created objects

description: Doctest for dynamically created objects


code:
class empDB:
    def __init__(self, lst= []):  #creates empty list
        self.lst = lst
    def appendEmp(self, emp=None):
        if emp is None:
            empt = Employee()
        self.lst=self.lst+[emp]

rawDescription: How do you get a method whose code instantiates another object to initialize a different object each time its called?

description: Get a method whose code instantiates another object to initialize a different object each time its called


code:
from pyspark.ml.feature import StringIndexer

indexer = StringIndexer(inputCol="user", outputCol="user_id")
indexed = indexer.fit(sparkdf ).transform(sparkdf)

rawDescription: pyspark add new column field with the data frame row number

description: Pyspark add new column field with the data frame row number


code:
os.system("open " + filename)

# You may need to add a sleep() here if the application is not already open and therefore needs time to load
os.system("""osascript -e 'tell application "System Events" to keystroke "f" using {command down}'""")
os.system("""osascript -e 'tell application "System Events" to keystroke \"""" + search_term + "\"'")

rawDescription: Open text file and search on OS X

description: Open text file and search on OS X


code:
with open("myfile", "r+") as f:
    lines = f.readlines()
    lines.sort()
    f.seek(0)
    f.writelines(lines)

rawDescription: Python - Sort lines in file based on first word?


code:
funcs = [mem[0] for mem in getmembers(module, isfunction)
         if mem[1].__module__ == module.__name__]

rawDescription: Ignore imported functions in a python module when using getmembers(module, isfunction)


code:
from cStringIO import StringIO
inmem_file = StringIO()
file_storage.save(inmem_file)  # save to memory
inmem_file.reset()  # seek back to byte 0, otherwise .read() will return ''
use_with_open_cv(inmem_file)

rawDescription: Load uploaded image without saving to HDD with python and OpenCV


code:
from subprocess import Popen, PIPE

process = Popen(['swfdump', '/tmp/filename.swf', '-d'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()

rawDescription: How to use subprocess popen Python

description: Use subprocess popen Python


code:
# define a sort key (Python 3 compatible)
def sort_key(value):
    "Sort by the value"
    return list(value.values())[-1]

ordered_groups = []
max_column = 34
for group in sorted(col_groups, key=sort_key, reverse=True):
    key, begin = list(group.items())[0]
    end = max_column - 1
    max_column = begin
    ordered_groups.append((key, (begin, end)))
dict(ordered_groups)

rawDescription: how to create range from a list of python dicts?

description: Create range from a list of python dicts


code:
search_input = forms.CharField(_(u'Search word'), required=False)
search_input.widget = forms.TextInput(attrs={'size': 10, 'title': 'Search',})

rawDescription: How do I set custom HTML attributes in django forms?

description: Set custom HTML attributes in django forms


code:
a = ['ONE', 'TWO', 'SEVEN', 'TWELVE', 'ONE', 'SEVEN']
[a[x+1] if x+1 < len(a) else None for x in range(len(a)) if a[x] == 'ONE']

rawDescription: Python: Get items that follow a specified element of a list


code:
max_mood = max(self.mood_scale)
min_mood = min(self.mood_scale)

rawDescription: What's the simplest way to get the highest and lowest keys from a dictionary?


code:
c.execute("SELECT COUNT(*) FROM my_db WHERE seq LIKE ?", (t + '%',))
rows = c.fetchone()[0]

rawDescription: More specific searching in sqlite3 for python


code:
import logging

logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('spam.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

rawDescription: How do I change the format of a Python log message on a per-logger basis?

description: Change the format of a Python log message on a per-logger basis


code:
new_df = pandas.DataFrame(index=DatesEOY, columns=YTDSales.columns)

num_to_sample = len(new_df)

f = lambda x: np.random.randint(x[1].min(), x[1].max(), num_to_sample)

output = pandas.concat([YTDSales, new_df], axis=0)

output[len(YTDSales):] = np.asarray(map(f, YTDSales.iteritems())).T

rawDescription: random sampling with pandas dataframe

description: Random sampling with pandas dataframe


code:
import sys

read_file = sys.argv[1]
write_file = "%s_processed.%s"%(sys.argv[1].split('.')[0],sys.argv[1].split('.')[1])
indexes = []
lines_to_write = []

with open(read_file,'r') as getindex:
    first_line = getindex.readline()
    for i, x in enumerate(first_line):
        if x == '*':
            indexes.append(i-11)

with open(read_file,'r') as getsnps:
    for line in getsnps:
        if line.startswith(">"):
            sequence = line.split("    ")[1]
            for z in indexes:
                string_to_append = "%s\t%s\t%s"%(line.split("    ")[0],z+1,sequence[z])
                lines_to_write.append(string_to_append)

with open(write_file,"w") as write_file:
    write_file.write("\n".join(lines_to_write))

rawDescription: Access each index result from enumerate() - Python


code:
#problem 14 project euler
import time
start=time.time()
has2={}
def collatz(x):
    seq=[]
    seq.append(x)
    temp=x
    while(temp>1):
        if temp%2==0:
            temp=int(temp/2)
            if temp in has2:
                seq+=has2[temp]
                break
            else:
                seq.append(temp)
        else:
            temp=3*temp+1
            if temp in has2:
                seq+=has2[temp]
                break
            else:
                seq.append(temp)

    has2[x]=seq
    return len(seq)

num=0
greatest=0
for i in range(1000000):
    c=collatz(i)
    if num<c:
        num=c
        greatest=i
print('{0} has {1} elements. calculation time ={2} seconds.'.format(greatest,num,time.time()-start))

rawDescription: Project Euler 14 code efficiency

description: Project Euler 14 code efficiency


code: "%02d:%02d:%02d" % (hours, minutes, seconds)

rawDescription: Format numbers to strings in Python

description: Format numbers to strings in Python


code:
import cherrypy

class HelloWorld(object):
    _cp_config = {
        'tools.json_out.on': True,
        'tools.json_in.on': True
    }

    @cherrypy.expose
    def index(self):
        return "Hello World!"

    @cherrypy.expose
    @cherrypy.config(**{'tools.json_in.on': False, 'tools.json_out.on': False})
    def foo(self):
        return "Hello World!"

cherrypy.quickstart(HelloWorld())

rawDescription: CherryPy: Turn off tool for one handler

description: CherryPy: Turn off tool for one handler


code:
local("hg pull")
res = local("hg update", capture=True)
print(blue(res))
local("hg status")

rawDescription: Colored output from fabric script

description: Colored output from fabric script


code:
from bitarray import bitarray

a = bitarray(2**20)

rawDescription: How to create an array of bits in Python?

description: Create an array of bits in Python


code:
import requests
from bs4 import BeautifulSoup

offset = 0
links = []
with requests.Session() as session:
    while True:
        r = session.get("http://rayleighev.deviantart.com/gallery/44021661/Reddit?offset=%d" % offset)
        soup = BeautifulSoup(r.content, "html.parser")
        new_links = [link["href"] for link in soup.find_all("a", {'class': "thumb"})]

        # no more links - break the loop
        if not new_links:
            break

        links.extend(new_links)
        print(len(links))
        offset += 24

print(links)

rawDescription: Soup.find_all is only returning Some of the results in Python 3.5.1


rawDescription: How to force SQLAlchemy to update rows

description: Force SQLAlchemy to update rows


code:
@app.route("/some/path")
def some_view():
    # ...
    post = Post.query.first()
    rendered_post_body = render_template_string(post.body)
    return render_template("some_template.html", post_body=rendered_post_body)

rawDescription: Call Flask functions inside templates

description: Call Flask functions inside templates


rawDescription: Python - Struct unpack bytes to ascii characters but with accents


code:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

BaseModel = declarative_base()

association = Table("message_component_association", BaseModel.metadata,
Column("message_id", Integer, ForeignKey("message.id")),
Column("component_id", Integer, ForeignKey("component.id")))

class Message(BaseModel):
    __tablename__ = "message"
    id = Column(Integer, primary_key=True)
    components = relationship("Component", secondary=association, backref="messages")  # backref instead of back_populates

class Component(BaseModel):
    __tablename__ = "component"
    __mapper_args__ = dict(polymorphic_on="type")  # Polymorphic identity removed
    id = Column(Integer, primary_key=True)
    type = Column(String(32))

    # relationship removed

class Component1(Component):
    __tablename__ = "component1"
    __mapper_args__ = dict(polymorphic_identity="component1")
    id = Column(Integer, ForeignKey("component.id"), primary_key=True)

    # relationship removed

engine = create_engine("sqlite://", echo=True)
BaseModel.metadata.create_all(engine)
session = Session(engine)

component_1 = Component1(id=1)
session.commit()

rawDescription: SQLAlchemy polymorphic many-to-many relation with abstract base in declarative syntax


code:
name = 'Project_Name'
glob.glob(os.path.join(directory, '{}_*.txt'.format(name)))

rawDescription: How does one get all files in a directory with a specific pattern in their names in Python

description: Get all files in a directory with a specific pattern in their names in Python


code:
os.chdir("Resources/maps_sideScrolling/HD")
replacements = {'tilewidth=\"16\"':'tilewidth=\"32\"', 'tileheight=\"16\"':'tileheight=\"32\"', '.png':'-hd.png'}

for files in os.listdir("."):
    if files.endswith("-hd.tmx"):
        fo = open(files, "rU+")
        text = fo.read()
        for src, target in replacements.iteritems():
                text = text.replace(src, target)
        fo.seek(0)
        fo.write(text)
        fo.truncate()
        fo.close()

rawDescription: Finding and replacing multiple strings in multiple files in python


code:
from sympy import *
x = Symbol('x')
y = x**3 + 2*x + 6
d = y.diff(x)

rawDescription: Numpy/ Scipy exact derivative?

description: Numpy/ Scipy exact derivative


code: print(a+b if a==1 else a-b)

rawDescription: If/else in python

description: If/else in python


code:
from Bio import SeqIO
file="sequence.gb"
output=open("Top3.faa", "w")
record=SeqIO.parse(file, "genbank")
rec=next(record)
print('The genes with the top 3 longest lengths have beens saved in Top3.faa')

# Largest genes and their size, sorted from the shortest to the longest.
# size first, gene name next, then seq.
largest_genes = [ (0, None, None) ] * 3;  # initialize with the number of genes you need.
for f in rec.features:
  end = f.location.end.position
  start = f.location.start.position
  length = end-start
  bug = (rec.seq)
  if f.type=='CDS' and 'gene' in f.qualifiers:
    if length > largest_genes[0][0]:  # [0] gives the first, [0] gives the length.
      # This one is larger than the smallest one we have.
      largest_genes = largest_genes[1:]  # drop the smallest one.
      # add this one
      largest_genes.append((length, f.qualifiers['gene'], str(bug[start:end])))
      largest_genes.sort()  # re-sort.

for length, name, seq in largest_genes:
  # name is not used but available.
  output.write('>')
  output.write(str(lenth))
  output.write('\n')
  output.write(seq)
  output.write('\n')
output.close()

rawDescription: Python. Trying to sort a file for 3 longest gene nucleotide sequences from genbank file into fasta file using BioPython


rawDescription: Simple 1 line For Loops

description: Simple 1 line For Loops


code:
for key in ['data-book-api', 'data-book-human', ...]:
    del thearticle[key]

rawDescription: python beautifulsoup shorten code

description: Python beautifulsoup shorten code


rawDescription: python stdin eof

description: Python stdin eof