code:
import urllib2
import urllib

url = 'http://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_hd.pl'

forecast_time = '06' #What time the forecast is (00, 06, 12, 18)
forecast_hours = '09' #How many hours ahead to forecast (2 or 3 digits)
forecast_date = '20120705' #What date the forecast is for yyyymmdd

get_parameters = {
    'subregion':'',
    'toplat':90, #Top of bounding box (North)
    'bottomlat':-90, #Bottom of bounding box (South)
    'leftlon':-90, #Left of bounding box (West)
    'rightlon':90, #Right of bounding box (East)
}

get_parameters['file'] = 'gfs.t' + forecast_time + 'z.mastergrb2f' + forecast_hours

on_variables = [
    'lev_1000_mb',
    'lev_975_mb',
    'lev_950_mb',
    'lev_925_mb',
    'lev_900_mb',
    'lev_850_mb',
    'lev_800_mb',
    'lev_750_mb',
    'lev_700_mb',
    'lev_650_mb',
    'lev_600_mb',
    'lev_550_mb',
    'lev_500_mb',
    'lev_450_mb',
    'lev_400_mb',
    'lev_350_mb',
    'lev_300_mb',
    'lev_250_mb',
    'lev_200_mb',
    'lev_150_mb',
    'lev_100_mb',
    'lev_70_mb',
    'lev_30_mb',
    'lev_20_mb',
    'lev_10_mb',

    'var_HGT',  #Height (geopotential m)
    'var_RH',  #Relative humidity (%)
    'var_TMP', #Temperature (K)
    'var_UGRD', #East-West component of wind (m/s)
    'var_VGRD', #North-South component of wind (m/s)
    'var_VVEL' #Vertical Windspeed (Pa/s)
]

get_parameters.update(dict((param, 'on') for param in on_variables))

#Select date and time
get_parameters['dir'] = '/gfs.' + forecast_date + forecast_time + '/master'

print('Downloading GRIB file for date '+forecast_date+' time ' +forecast_time + ',    forecasting '+forecast_hours+' hours ahead...')

req = urllib2.urlopen(url + '?' + urllib.urlencode(get_parameters), timeout = 300) # Theres bug in apache for non-used GET varaibles so we have to manually add them to the url ...
local_filename = forecast_date + '_' + forecast_time + '_' + forecast_hours + '.grib'
local_file = open('/home/dantayaga/bovine_aerospace/grib/data/' + local_filename, 'wb')

local_file.write(req.read())
local_file.close()

print('Requested grib data written to file ' + local_filename)

rawDescription: Urllib and urllib2 returns "IOError: [Errno socket error] [Errno -2] Name or service not known" but Firefox downloads file with no trouble


code:
with open('yolo.txt', 'r') as readf, open('yip.txt', 'w') as writef:
    for line in reversed(readf.readlines()):
        writef.write(line)

rawDescription: Python Read From File, Write in Reversed Order


code:
import win32gui

def enum_window_titles():
    def callback(handle, data):
        titles.append(win32gui.GetWindowText(handle))

    titles = []
    win32gui.EnumWindows(callback, None)
    return titles

titles = enum_window_titles()

rawDescription: Get the name of a window using Windows API


code:
def calculate(match):
    return '%s%s'%(match.group(1), str(int(match.group(2))+1))

editor.rereplace(r'(=>\s*"?)(\d+)', calculate)

rawDescription: Notepad++ Regex + python script (addition in the replace)


code: btnHnd= win32api.FindWindowEx(hwnd, 0 , "Button", "#Title")

rawDescription: Finding Handle of a control inside a window


code:
class Test(object):
    def __new__(*args):
        return False

rawDescription: Set python object to variable

description: Set python object to variable


code: pool.apply_async(do_job, (first_bit,))

rawDescription: Python multiprocessing starts processes, but only one is active


code: [z0] * len(seconds)

rawDescription: Python list with constant value

description: Python list with constant value


code:
for fromNode in g:
    neighbors = g[fromNode]
    for toNode in neighbors:
        distance = neighbors[toNode]
        print("%s -> %s (%d)" % (fromNode, toNode, distance))

rawDescription: Shortest path algorithm using dictionaries [Python]


code:
def returnSetOfList(scheme, key):
    a_dict = dict(scheme)
    return set( a_dict[key] )

relational_scheme = [["F",["DC"]],["A",["EBD"]],["DC" , ["BAF"]],["E",["DB"]]]
returnSetOfList(relational_scheme, "F")

rawDescription: Multidimensional List get Position of a String Python


code:
import sys, os
packagepath = os.getcwd() + '/packages'
sys.path.append(packagepath)

rawDescription: Python Import using __init__.py instead of adding to sys.path


code: all_results = x_results | y_results

rawDescription: Django combine two queries without Q

description: Django combine two queries without Q


code:
import array
import os

fn = 'data.bin'
a = array.array('h')
a.fromfile(open(fn, 'rb'), os.path.getsize(fn)/a.itemsize)

rawDescription: Improve speed of reading and converting from binary file with python


code: cube[tuple(vector)]

rawDescription: How to address elements of multidimensional array with variables?

description: Address elements of multidimensional array with variables


code:
def choose_animation(self):
    """
    Pops a choose so the user can select a folder containing their image sequence.
    """
    sequence_dir = filedialog.askdirectory() # ask for a directory for the image sequence
    if not sequence_dir: # if the user canceled,
        return # we're done
    files = os.listdir(sequence_dir) # get a list of files in the directory
    try:
        # make a list of tkinter images, sorted by the portion of the filenames between the last '_' and the last '.'.
        self.image_sequence = [ImageTk.PhotoImage(Image.open(os.path.join(sequence_dir, filename)).resize(((self.screen_size),(self.screen_size))))
        for filename in sorted(os.listdir(sequence_dir), key=lambda x: int(x.rpartition('_')[2][:-len(x.rpartition('.')[2])-1]))]
        self.start_animation() # no error? start the animation
    except: # error? announce it
        if self.audio:
            messagebox.showerror(title='Error', message='Could not load animation.')
        else:
            self.status_message.config(text='Error: could not load animation.')

def choose_framerate(self):
    """
    Pops a chooser for the framerate.
    """
    framerate_window = Toplevel()
    framerate_window.focus_set()
    framerate_window.title("Framerate selection")
    try: # try to load an img for the window's icon (top left corner of title bar)
        framerate_window.tk.call('wm', 'iconphoto', framerate_window._w, ImageTk.PhotoImage(Image.open("ico.png")))
    except: # if it fails
        pass # leave the user alone
    enter_field = Entry(framerate_window) # an Entry widget
    enter_field.grid(row=0, column=0) # grid it
    enter_field.focus_set() # and focus on it
    def set_to(*args):
        try:
            # use this framerate if it's a good value
            self.framerate = float(enter_field.get()) if 0.01 < float(enter_field.get()) <= 100 else [][0]
            framerate_window.destroy() # and close the window
        except:
            self.framerate = 10 # or just use 10
            framerate_window.destroy() # and close the window
    ok_button = Button(framerate_window, text='OK', command=set_to) # make a Button
    ok_button.grid(row=1, column=0) # grid it
    cancel_button = Button(framerate_window, text='Cancel', command=framerate_window.destroy) # cancel button
    cancel_button.grid(row=2, column=0) # grid it
    framerate_window.bind("<Return>", lambda *x: set_to()) # user can hit Return to accept the framerate
    framerate_window.bind("<Escape>", lambda *x: framerate_window.destroy()) # or Escape to cancel

def start_animation(self):
    """
    Starts the animation.
    """
    if not self.animating: # if animation is off
        try:
            self.animating = True # turn it on
            self.bg_img = self.image_sequence[0] # set self.bg_img to the first frame
            self.set_square_color('light', 'clear') # clear the light squares
            self.set_square_color('dark', 'clear') # clear the dark squares
            self.animate(0) # start the animation at the first frame
        except: # if something failed there,
            if self.audio: # they probably haven't set an animation. use messagebox,
                messagebox.showerror(title='Error', message='Animation not yet set.')
            else: # or a silent status_message update to announce the error.
                self.status_message.config(text='Error: animation not yet set.')
    else: # otherwise
        self.animating = False # turn it off

def animate(self, counter):
    """
    Animates the images in self.image_sequence.
    """
    self.board.itemconfig(self.bg_ref, image=self.image_sequence[counter]) # set the proper image to the passed element
    if self.animating: # if we're animating,
        # do it again with the next one
        self.parent.after(int(1000//self.framerate), lambda: self.animate(counter+1 if (counter+1)<len(self.image_sequence) else 0))

rawDescription: How to run a 30 frame image sequence completely within Python/Tkinter?

description: Run a 30 frame image sequence completely within Python/Tkinter


code:
m, n, k = 5, 7, 3
tuple(n if i == k else 1 for i in range(m))

rawDescription: creating a non-literal python tuple programmatically

description: Creating a non-literal python tuple programmatically


code: df.loc[df['line_race'] == 0, 'rating'] = 0

rawDescription: How to conditionally update DataFrame column in Pandas

description: Conditionally update DataFrame column in Pandas


code:
import pyaudio, wave, sys

CHUNK = 8192
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 10

WAVE_OUTPUT_FILENAME = 'Audio_.wav'
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
                channels = CHANNELS,
                rate = RATE,
                input = True,
                input_device_index = 0,
                frames_per_buffer = CHUNK)

print("* recording")

frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)
print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

rawDescription: Add recoding duration with pyaudio in Python


code:
data3['diffs'] = data3.groupby('ticker')['value'].transform(Series.diff)
data3.sort_index(inplace=True)
data3

rawDescription: Computing diffs in Pandas after using groupby leads to unexpected result


code: sum(user.waitTime for user in self.done)/float(len(self.done))

rawDescription: average of attribute in a list of objects

description: Average of attribute in a list of objects


code:
class Pipeline(object):
    def __init__(self):
        super(Pipeline, self).__init__()
        self.items = []

    def process_item(self, item, spider):
        self.items.append(item)
        if len(self.items) >= 1000:
            self.insert_current_items()
        return item

    def insert_current_items(self):
        items = self.items
        self.items = []
        self.insert_to_database(items)

    def close_spider(self, spider):
        self.insert_current_items()

rawDescription: Buffered pipeline using scrapy

description: Buffered pipeline using scrapy


code:
lista_results = []
for y in range(0,10):
    lista_temp = []     # we have a new lista_temp for every y
    for x in range(0,10):
        lista_temp.append(something_here)
    lista_results.append(lista_temp)

rawDescription: Python: Emptying temporary list used in a loop without losing referenced information


code:
import gtk

if __name__ == "__main__":
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    scroll = gtk.ScrolledWindow()
    scroll.set_policy(gtk.POLICY_NEVER, gtk.POLICY_ALWAYS)
    view = gtk.TextView()
    view.set_wrap_mode(gtk.WRAP_CHAR)
    scroll.set_size_request(256, 256)
    scroll.add(view)
    window.add(scroll)
    window.show_all()
    gtk.main()

rawDescription: Why does this pygtk TextView refuse to wrap?


code: re.sub(r'\b[\dx]+\b', '', "big $price chair, 5x10")

rawDescription: finding a pattern in string python

description: Finding a pattern in string python


code:
g = nx.Graph()
g.add_nodes_from( [0,1] )
g.nodes()
g.add_node(2)
g.add_path([0,2,1])
g.nodes()
g.edges()

rawDescription: Is it possible to add a new node to an existing edge using Networkx


code:
import csv

with open('location.csv', newline='') as fp_in:
    reader = csv.reader(fp_in, delimiter=',')
    data = list(reader)

data.sort(key=lambda x: (len(x[-1].strip()) != 2, x[-1].strip()))

with open("locout.csv", "w", newline='') as fp_out:
    writer = csv.writer(fp_out, delimiter=',')
    writer.writerows(data)

rawDescription: Rearranging Data in CSV by Certain Data in a Column


code:
class C(object):

    def __init__(self):
        self.__a = 0

    @property
    def a(self):
        return self.__a

    # You can use the decorator to create setters too...
    @a.setter
    def a(self, value):
        self.__a = value

rawDescription: Pylint warning `W0212` with properties accessing a protected member: how to avoid?


code:
@hosts(server1)
def server1_task():
    ...

@hosts(server2)
def server2_task():
    ...

rawDescription: Can I have multiple Python fabfiles in one project directory?

description: Have multiple Python fabfiles in one project directory


code:
import itertools
for a, b, c in itertools.izip (list1, list2, list3):
    ...

rawDescription: Looping through more than one lists - Python


code:
class A(forms.ModelForm):
    class Meta:
        model = ModelA

    def __init__(self, *args, **kwargs):
        super(A, self).__init__(*args, **kwargs)
        self.fields['your_field'].queryset = self.fields['your_field'].queryset \
                .filter(some_filter_value=1)

rawDescription: How can I customize a drop-down list form element in django?

description: Customize a drop-down list form element in django


code:
def numLen(s, n):
    return sum(1 for x in s.split() if len(x) == n)

rawDescription: Attempting to print the number of words that have a certain length


code: from App import main

rawDescription: Importing a file from outside directory Python


code:
class A():
    def __init__(self):
        self.a = 200
        self.y = 400

a = A()
l = ['x', 'y', 'z']
for item in l:
    setattr(a, item, 1000)

a.x
a.y
a.z

rawDescription: Evaluate string to an object and changing / assigning value


code: plt.legend(loc = 'best', prop={'size':11}) # 'best' automatically decides which is the best position for the legend

rawDescription: Python plotting: label without color code


code:
class MainHandler(tornado.web.RequestHandler):
    def get(self):
        roomid = self.get_argument('roomid', None)
        presenterid = self.get_argument('presenterid', None)
        if roomid is None or presenterid is None:
            self.redirect('/') # root url
            return
        self.write('I got the output ya {} {}'.format(roomid, presenterid))

application = tornado.web.Application([
    (r"/index\.html", MainHandler),
])

rawDescription: How to handle the dict of parms in tornado?

description: Handle the dict of parms in tornado


rawDescription: Python3 list printing without spaces

description: Python3 list printing without spaces


code:
sns.set_style('darkgrid', {'axes.linewidth': 2, 'axes.edgecolor':'black'})
sns.boxplot(x='species', y='sepal length (cm)', data=iris_new)
plt.show()

rawDescription: In Python's Seaborn, is there any way to do the opposite of `despine`?


code:
import xlwt
wb = xlwt.Workbook()
for shx in 0, 1:
    ws = wb.add_sheet("S%d" % shx)
    # Excel 2003: Tools > Options > International > View current sheet right-to-left
    # Excel 2007: Page Layout > Sheet Right-to-Left
    ws.cols_right_to_left = shx
    ws.write(0, 0, "qwerty")
wb.save("colsR2L.xls")

rawDescription: right to left excel page layout in python xlwt

description: Right to left excel page layout in python xlwt


code:
import os
all_files = os.listdir('.') # gets all files in current directory

rawDescription: viewing files in python?

description: Viewing files in python


code:
bankBalance = 1000000
format(bankBalance, ",")

rawDescription: How to convert an integer to a comma separated string

description: Convert an integer to a comma separated string


code:
import hashlib
salt = 'uphi8eiV'
hash = 'a7a2a98cc06f5b2935db5d2866670cc8b48116022e5bc30095b6167ddc2f3f96'
if hashlib.md5.hexdigest(salt + rawInput).hexdigest() == hash:
  print('Correct password')

rawDescription: Real Security in Python

description: Real Security in Python


code:
def get_rand_file(folder, match=None):
   if match == None:
       return random.choice(os.listdir(folder))
   else:
       return random.choice([s for s in os.listdir(folder) if match in s])

rawDescription: return a random item in a list of matching strings from a list of strings efficiency

description: Return a random item in a list of matching strings from a list of strings efficiency


code:
data = np.genfromtxt('pratice_data.txt',
                    dtype=[('first column name','f8'),('second column name','i8')],
                    delimiter=' ',
                    skip_header = 14)

rawDescription: Need help importing specific data (variable values) from a text file and ignoring non useful text and metadata


code:
list=[1,2,3,4,5]
k = [str(x) for x in list]
k
"".join(k)
["".join(k)]

[int("".join(k))]

rawDescription: Merging items in a list - Python

description: Merging items in a list - Python


rawDescription: Time to decimal time in Python

description: Time to decimal time in Python


code:
from flask import Response

# construct your app

@app.route("/get-file")
def get_file():
    results = generate_file_data()
    generator = (cell for row in results
                    for cell in row)

    return Response(generator,
                       mimetype="text/plain",
                       headers={"Content-Disposition":
                                    "attachment;filename=test.txt"})

rawDescription: In python using Flask, how can I write out an object for download?


code:
import numpy as np
import matplotlib.pyplot as plt
data = [ ("data1", 34), ("data5", 22),
        ("data12", 11), ( "data8", 28),
        ("data53", 57), ( "data11", 39),
        ("data12", 23), ( "data15", 98)]
N = len(data)
x = np.arange(1,N+1)
y = [ num for (s, num) in data ]
labels = [ s for (s, num) in data ]
width = 1
bar1 = plt.bar( x, y, width, color="y" )
plt.ylabel( 'Intensity' )
plt.xticks(x + width/2.0, labels )

y_shift = 5
x_shift = width/2.0
for x, y in zip(x, y):
    plt.annotate("%i" % y, (x + x_shift, y + y_shift), ha= 'center')

plt.ylim(0,110)

plt.show()

rawDescription: Matplotlib Annotation not working

description: Matplotlib Annotation not working


code:
import urllib2

listOfStocks = ["AAPL", "MSFT", "GOOG", "FB", "AMZN"]

urls = []

for company in listOfStocks:
    urls.append('http://real-chart.finance.yahoo.com/table.csv?s=' + company + '&d=6&e=28&f=2015&g=m&a=11&b=12&c=1980&ignore=.csv')

Output_File = open('C:/Users/rshuell001/Historical_Prices.csv','w')

New_Format_Data = ''

for counter in range(0, len(urls)):

    Original_Data = urllib2.urlopen(urls[counter]).read()

    if counter == 0:
        New_Format_Data = "Company," + urllib2.urlopen(urls[counter]).readline()

    rows = Original_Data.splitlines(1)

    for row in range(1, len(rows)):

        New_Format_Data = New_Format_Data + listOfStocks[counter] + ',' + rows[row]

Output_File.write(New_Format_Data)
Output_File.close()

rawDescription: How can write scraped content to a CSV file?


code:
from __future__ import division  # use real division in Python2.x
from matplotlib.dates import date2num
import matplotlib.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# Get a dataset
data_url = 'http://datasets.flowingdata.com/earthquakes1974.csv'
df = pd.read_csv(data_url, parse_dates=['time'])
# Select a random subset of that dataframe to generate some variance in dates, magnitudes, ...
data = np.random.choice(df.shape[0], 10)
records = df.loc[data]
# Taint the dataset to add some bigger variance in the magnitude of the
# quake to show that the offset varies with the size of the marker
records.mag.values[:] = np.arange(10)
records.mag.values[0] = 50
records.mag.values[-1] = 100

dates = [date2num(dt) for dt in records.time]
f, ax = plt.subplots(1,1)
ax.scatter(dates, records.depth, s=records.mag*100, alpha=.4)  # markersize is given in points**2 in recentt versions of mpl
for _, record in records.iterrows():
    # Specify an offset for the text annotation:
    # it is approx the radius of the disc + 10 points to the right
    dx, dy = np.sqrt(record.mag*100)/f.dpi/2 + 10/f.dpi, 0.
    offset = transforms.ScaledTranslation(dx, dy, f.dpi_scale_trans)
    ax.text(date2num(record.time), record.depth, s=record.place,
        va='center',  ha='left',
        transform=ax.transData + offset)
ax.set_xticks(dates)
ax.set_xticklabels([el.strftime("%Y-%M") for el in records.time], rotation=-60)
ax.set_ylabel('depth of earthquake')
plt.show()

rawDescription: Python Matplotlib : how to put label next to each element in the bubble plot


code:
import csv

data_row = [1, 2.0, "three", "pi,plus,one"]

# Assuming Python 2.x
# ...in Python 3 use text mode: open("...", 'w', newline='')
with open("datafile.csv", 'wb') as datafile:
    datawriter = csv.writer(datafile)
    datawriter.writerow(data_row)

rawDescription: Whats the pythonic way to convert a heterogenous list of scalars into a string


code:
import Tkinter as tk

class ButtonEntry(tk.Frame):
    def __init__(self,master,ss):
        tk.Frame.__init__(self)
        self.var=tk.StringVar()
        self.var.set(ss)
        self.Button=tk.Button(self,text='Button',command=lambda :self.var.set("foo!"))
        self.Entry=tk.Entry(self,textvariable=self.var)
        self.Button.grid(row=0,column=0)
        self.Entry.grid(row=0,column=1)

class App(tk.Frame):
    def __init__(self,master=None):
        tk.Frame.__init__(self,master)
        self.BEs=[]
        for i in range(10):
            b=ButtonEntry(self,'Button %d'%i)
            b.grid(row=i,column=0)
            self.BEs.append(b)

if __name__ == '__main__':
    root=tk.Tk()
    f=App(root)
    f.grid(row=0,column=0)
    root.mainloop()

rawDescription: How can I get TK buttons, generated by a for loop, to pass input to their command? (Python)

description: Get TK buttons, generated by a for loop, to pass input to their command? (Python)


code:
a = (sixteen_bit_val & 0xE000) >> 13
b = (sixteen_bit_val & 0x1800) >> 11
# ... and so on ..

rawDescription: Can I set a particular number of bits to keep for Python's logical shift left?

description: Set a particular number of bits to keep for Python's logical shift left


rawDescription: How to align text to the right in ttk Treeview widget?

description: Align text to the right in ttk Treeview widget


rawDescription: Replace value of dictionary with key from another dictionary


code:
self.aRadioButton = Radiobutton(top, text="Blue",variable = v, value = 0, command=lambda: top.configure(bg="Blue"))
self.aRadioButton = Radiobutton(top, text="Red",variable = v, value = 1, command=lambda: top.configure(bg="Red"))

rawDescription: Updating the background colour in Toplevel widget


code:
key = Key(bucket=bucket, name=my_key_id)
data = key.get_contents_as_string()

rawDescription: Boto doubles all get request cycles by design


code:
from flask import Flask
from your.application import CustomDataStructure

CUSTOM_DATA_STRUCTURE = CustomDataStructure('/data/lives/here')

# @app.routes, etc.

rawDescription: Sharing Memory in Gunicorn?

description: Sharing Memory in Gunicorn


rawDescription: Sort by list in list in python?

description: Sort by list in list in python


code:
time = int(input('How many minutes were you exercising? '))

calories = 0
for i in range(time):
    calories += 3.4

print('You burned {} calories'.format(calories))

rawDescription: I need to write a program in Python to calculate the calories burned while doing an activity


code:
import datetime
today = datetime.date(2014, 9, 30)
next_week = today + datetime.timedelta(days=7)
next_week.isocalendar()[1]

rawDescription: Get next and last ISO week number in a year


code:
def increment(a, name):
    for i, x in enumerate(a):
        if x[0] == name:
            a[i] = (x[0], x[1] + 1)
            break

rawDescription: How to find index of tuple in list of tuples in Python?

description: Find index of tuple in list of tuples in Python


code:
cursor.execute("""INSERT INTO
                      State
                      (state, relevant_id)
                  VALUES
                      (%s, %s);""",
               (state_values, relevant_id))

rawDescription: Inserting multiple strings into 1 table

description: Inserting multiple strings into 1 table


code:
def restore_object(self, attrs, instance=None):
    if not instance:
        instance = super(RegisterationSerializer, self).restore_object(attrs, instance)
    instance.set_password(attrs.get('password'))
    return instance

rawDescription: How to create a django User using DRF's ModelSerializer

description: Create a django User using DRF's ModelSerializer


code:
template_dir = os.path.abspath(
               os.path.join(os.path.dirname(__file__),
                            '../Frontend/templates'))

rawDescription: How do I link to another directory with python?

description: Link to another directory with python


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

#-- Generate some data...-------
x = np.linspace(-10, 50, 100)
y = x**2 + 3*x + 8

# Add a lot of noise to part of the data...
y[x < 10] += np.random.random(sum(x < 10)) * 300

# Now let's extract only the part of the data we're interested in...
x_filt = x[x > 10]
y_filt = y[x > 10]

# And fit a line to only that portion of the data.
model = np.polyfit(x_filt, y_filt, 2)

# And plot things up
fig, axes = plt.subplots(nrows=2, sharex=True)
axes[0].plot(x, y, 'bo')
axes[1].plot(x_filt, y_filt, 'bo')
axes[1].plot(x, np.polyval(model, x), 'r-')

plt.show()

rawDescription: Plot x-y data if x entry meets condition python


code:
df['new_column'] = df[['lat', 'long']].values.tolist()
df

rawDescription: adding column to pandas dataframe as list of other columns

description: Adding column to pandas dataframe as list of other columns


code:
''' Wrapper to MKL FFT routines '''

import numpy as _np
import ctypes as _ctypes

mkl = _ctypes.cdll.LoadLibrary("mk2_rt.dll")
_DFTI_COMPLEX = _ctypes.c_int(32)
_DFTI_DOUBLE = _ctypes.c_int(36)
_DFTI_PLACEMENT = _ctypes.c_int(11)
_DFTI_NOT_INPLACE = _ctypes.c_int(44)
_DFTI_INPUT_STRIDES = _ctypes.c_int(12)
_DFTI_OUTPUT_STRIDES = _ctypes.c_int(13)

def fft2(a, out=None):
    '''
Forward two-dimensional double-precision complex-complex FFT.
Uses the Intel MKL libraries distributed with Enthought Python.
Normalisation is different from Numpy!
By default, allocates new memory like 'a' for output data.
Returns the array containing output data.
'''

    assert a.dtype == _np.complex128
    assert len(a.shape) == 2

    inplace = False

    if out is a:
        inplace = True

    elif out is not None:
        assert out.dtype == _np.complex128
        assert a.shape == out.shape
        assert not _np.may_share_memory(a, out)

    else:
        out = _np.empty_like(a)

    Desc_Handle = _ctypes.c_void_p(0)
    dims = (_ctypes.c_int*2)(*a.shape)

    mkl.DftiCreateDescriptor(_ctypes.byref(Desc_Handle), _DFTI_DOUBLE, _DFTI_COMPLEX, _ctypes.c_int(2), dims )

    #Set input strides if necessary
    if not a.flags['C_CONTIGUOUS']:
        in_strides = (_ctypes.c_int*3)(0, a.strides[0]/16, a.strides[1]/16)
        mkl.DftiSetValue(Desc_Handle, _DFTI_INPUT_STRIDES, _ctypes.byref(in_strides))

    if inplace:
        #Inplace FFT
        mkl.DftiCommitDescriptor(Desc_Handle)
        mkl.DftiComputeForward(Desc_Handle, a.ctypes.data_as(_ctypes.c_void_p) )

    else:
        #Not-inplace FFT
        mkl.DftiSetValue(Desc_Handle, _DFTI_PLACEMENT, _DFTI_NOT_INPLACE)

        #Set output strides if necessary
        if not out.flags['C_CONTIGUOUS']:
            out_strides = (_ctypes.c_int*3)(0, out.strides[0]/16, out.strides[1]/16)
            mkl.DftiSetValue(Desc_Handle, _DFTI_OUTPUT_STRIDES, _ctypes.byref(out_strides))

        mkl.DftiCommitDescriptor(Desc_Handle)
        mkl.DftiComputeForward(Desc_Handle, a.ctypes.data_as(_ctypes.c_void_p), out.ctypes.data_as(_ctypes.c_void_p) )

    mkl.DftiFreeDescriptor(_ctypes.byref(Desc_Handle))

    return out

def ifft2(a, out=None):
    '''
Backward two-dimensional double-precision complex-complex FFT.
Uses the Intel MKL libraries distributed with Enthought Python.
Normalisation is different from Numpy!
By default, allocates new memory like 'a' for output data.
Returns the array containing output data.
'''

    assert a.dtype == _np.complex128
    assert len(a.shape) == 2

    inplace = False

    if out is a:
        inplace = True

    elif out is not None:
        assert out.dtype == _np.complex128
        assert a.shape == out.shape
        assert not _np.may_share_memory(a, out)

    else:
        out = _np.empty_like(a)

    Desc_Handle = _ctypes.c_void_p(0)
    dims = (_ctypes.c_int*2)(*a.shape)

    mkl.DftiCreateDescriptor(_ctypes.byref(Desc_Handle), _DFTI_DOUBLE, _DFTI_COMPLEX, _ctypes.c_int(2), dims )

    #Set input strides if necessary
    if not a.flags['C_CONTIGUOUS']:
        in_strides = (_ctypes.c_int*3)(0, a.strides[0]/16, a.strides[1]/16)
        mkl.DftiSetValue(Desc_Handle, _DFTI_INPUT_STRIDES, _ctypes.byref(in_strides))

    if inplace:
        #Inplace FFT
        mkl.DftiCommitDescriptor(Desc_Handle)
        mkl.DftiComputeBackward(Desc_Handle, a.ctypes.data_as(_ctypes.c_void_p) )

    else:
        #Not-inplace FFT
        mkl.DftiSetValue(Desc_Handle, _DFTI_PLACEMENT, _DFTI_NOT_INPLACE)

        #Set output strides if necessary
        if not out.flags['C_CONTIGUOUS']:
            out_strides = (_ctypes.c_int*3)(0, out.strides[0]/16, out.strides[1]/16)
            mkl.DftiSetValue(Desc_Handle, _DFTI_OUTPUT_STRIDES, _ctypes.byref(out_strides))

        mkl.DftiCommitDescriptor(Desc_Handle)
        mkl.DftiComputeBackward(Desc_Handle, a.ctypes.data_as(_ctypes.c_void_p), out.ctypes.data_as(_ctypes.c_void_p) )

    mkl.DftiFreeDescriptor(_ctypes.byref(Desc_Handle))

    return out

rawDescription: Threaded FFT in Enthought Python

description: Threaded FFT in Enthought Python


code:
def cond_match(regexp, line, grp):
    match = re.search(regexp, line)
    if match:
        return match.group(grp)
    else:
        return None

for line in blkid:
    line = line.strip()
    partition = Partition()
    partition.identifier = cond_match(r'^(/dev/[a-zA-Z0-9]+)', line, 0)
    partition.label = cond_match(r'LABEL="((?:[^"\\]|\\.)*)"', line, 1)
    partition.uuid = cond_match(r'UUID="((?:[^"\\]|\\.)*)"', line, 1)
    partition.type = cond_match(r'TYPE="((?:[^"\\]|\\.)*)"', line, 1)
    partitions.add(partition)

rawDescription: Python equivalent of PHP's @

description: Python equivalent of PHP's @


code:
class UserType( models.Model ) :
    TYPE_CHOICES = (
        ( 'ad', 'administrator'    ),
        ( 'mo', 'moderator'        ),
        ( 'vi', 'viewer'           ),
        ( 'pm', 'property manager' ),
        ( 'po', 'property owner'   ),
        ( 'vm', 'vendor manager'   ),
        ( 've', 'vendor'           ),
        ( 'te', 'tenant'           ),
        )
    STR_CHOICES = { key : value for (key,value) in TYPE_CHOICES }

    name = models.CharField( max_length = 2, choices = TYPE_CHOICES )

    def __str__(self):
        return self.STR_CHOICES[self.name]

rawDescription: How to show the full name in a ManyToManyField with choices (tuples)?

description: Show the full name in a ManyToManyField with choices (tuples)


code:
from configmodule import config
config.get(spam, eggs)

rawDescription: Python ConfigParser - usage across modules


code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
# import pyodbc

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8

    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(786, 520)
        self.buttonBox = QtGui.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(440, 480, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
        self.resultsView = QtGui.QPlainTextEdit(Dialog)
        self.resultsView.setGeometry(QtCore.QRect(10, 70, 771, 401))
        self.resultsView.setObjectName(_fromUtf8("resultsView"))
        self.sorgula = QtGui.QPushButton(Dialog)
        self.sorgula.setGeometry(QtCore.QRect(690, 10, 84, 33))
        self.sorgula.setObjectName(_fromUtf8("sorgula"))
        self.tarih1 = QtGui.QLineEdit(Dialog)
        self.tarih1.setGeometry(QtCore.QRect(10, 20, 151, 31))
        self.tarih1.setObjectName(_fromUtf8("tarih1"))
        self.tarih2 = QtGui.QLineEdit(Dialog)
        self.tarih2.setGeometry(QtCore.QRect(170, 20, 151, 31))
        self.tarih2.setObjectName(_fromUtf8("tarih2"))

        self.retranslateUi(Dialog)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
        self.sorgula.clicked.connect(self.lme)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def lme(self):
        textData = str(self.tarih1.text())
        cnxn = pyodbc.connect('DSN=;UID=;PWD=')
        cursor = cnxn.cursor()
        cursor.execute("select CONVERT(date,Tarih,104) as Tarih, LME, USD, EURO from dbo.DovizLme where Tarih = REPLACE(CONVERT(char(10), PARSE('%s' AS datetime USING 'tr-TR'),101),'/','.')" % textData)
        rows = cursor.fetchall()
        for row in rows:
            print("Tarih:", row[0], "LME:", row[1], "USD:", row[2], "EUR:", row[3])

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.sorgula.setText(_translate("Dialog", "Sorgula", None))
        self.tarih1.setPlaceholderText(_translate("Dialog", "Başlangıç Tarihi Giriniz", None))
        self.tarih2.setPlaceholderText(_translate("Dialog", "Bitiş Tarihi Giriniz", None))

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

rawDescription: PyQt and Buttons

description: PyQt and Buttons


code:
def do_GET(self):
    host = self.headers.get('Host')

rawDescription: Getting Host field from HTTP request in BaseHTTPRequestHandler


code:
if "\n" in line:
    lines.append(line.strip())

rawDescription: How do you delete \n from a line?

description: Delete \n from a line


code:
with open(r'C:\Users\Gord\Desktop\list_in.txt', 'r') as f_in:
    lines = f_in.readlines()

with open(r'C:\Users\Gord\Desktop\list_out.txt', 'w') as f_out:
    prev_line = ''
    for line in lines:
        if line != prev_line:
            f_out.write(line)
            prev_line = line

with open(r'C:\Users\Gord\Desktop\list_out.txt', 'r') as f:
    for line in f.readlines():
        print(line),

rawDescription: Displaying a File without Adjacent Duplicates in Python


code:
import urllib2

class MyRequest(urllib2.Request):
    GET = 'get'
    POST = 'post'
    PUT = 'put'
    DELETE = 'delete'

    def __init__(self, url, data=None, headers={},
                 origin_req_host=None, unverifiable=False, method=None):
       urllib2.Request.__init__(self, url, data, headers, origin_req_host, unverifiable)
       self.method = method

    def get_method(self):
        if self.method:
            return self.method

        return urllib2.Request.get_method(self)

opener = urllib2.build_opener(urllib2.HTTPHandler)
req = MyRequest('http://yourwebsite.com/put/resource/', method=MyRequest.PUT)

resp = opener.open(req)

rawDescription: custom methods in python urllib2

description: Custom methods in python urllib2


code:
from kivy.clock import mainthread

class LogoutButton(ActionPrevious):
    def __init__(self, **kwargs):
        super(LogoutButton, self).__init__(**kwargs)
        self.do_something()

    @mainthread
    def do_something(self):
        print(self.children)
        print(self.ids)
        lb = self.ids['title']
        self.remove_widget(lb)
        self.ids['prevlayout'].add_widget(lb)

rawDescription: How to move a widget that was defined in kv around the tree?

description: Move a widget that was defined in kv around the tree


code:
import csv
from collections import deque, defaultdict
from itertools import chain, islice, groupby
from operator import itemgetter

# defaultdict whose first access of a key will create a deque of size 3
# defaulting to [['x', 'y'], ['x', 'y'], ['x' ,'y']]
# Since deques are efficient at head/tail manipulation, then an insert to
# the start is efficient, and when the size is fixed it will cause extra
# elements to "fall off" the end...
names_previous = defaultdict(lambda: deque([['x', 'y']] * 3, 3))
with open('sample.csv', 'rb') as fin, open('sample_new.csv', 'wb') as fout:
    csvin = csv.reader(fin)
    csvout = csv.writer(fout)
    # Use groupby to detect changes in the date column. Since the data is always
    # asending, the items within the same data are contigious in the data. We use
    # this to identify the rows within the *same* date.
    # date=date we're looking at, rows=an iterable of rows that are in that date...
    for date, rows in groupby(islice(csvin, 1, None), itemgetter(1)):
        # After we've processed entries in this date, we need to know what items of data should
        # be considered for the names we've seen inside this date. Currently the data
        # is taken from the last occurring row for the name.
        to_add = {}
        for row in rows:
            # Output the row present in the file with a *flattened* version of the extra data
            # (previous items) that we wish to apply. eg:
            # [['x, 'y'], ['x', 'y'], ['x', 'y']] becomes ['x', 'y', 'x', 'y', 'x', y']
            # So we're easily able to store 3 pairs of data, but flatten it into one long
            # list of 6 items...
            # If the name (row[2]) doesn't exist yet, then by trying to do this, defaultdict
            # will automatically create the default key as above.
            csvout.writerow(row + list(chain.from_iterable(names_previous[row[2]])))
            # Here, we store for the name any additional data that should be included for the name
            # on the next date group. In this instance we store the information seen for the last
            # occurrence of that name in this date. eg: If we've seen it more than once, then
            # we only include data from the last occurrence.
            # NB: If you wanted to include more than one item of data for the name, then you could
            # utilise a deque again by building it within this date group
            to_add[row[2]] = row[3:5]
        for key, val in to_add.iteritems():
            # We've finished the date, so before processing the next one, update the previous data
            # for the names. In this case, we push a single item of data to the front of the deck.
            # If, we were storing multiple items in the data loop, then we could .extendleft() instead
            # to insert > 1 set of data from above.
            names_previous[key].appendleft(val)

rawDescription: How to pull X amounts of previous data into row in CSV

description: Pull X amounts of previous data into row in CSV


rawDescription: Split a line at the first special character Python


rawDescription: Filtering a Pandas DataFrame Without Removing Rows


code:
import base64

with open('input.zip', 'rb') as fin, open('output.zip.b64', 'w') as fout:
    base64.encode(fin, fout)

rawDescription: base64 encode a zip file in Python

description: Base64 encode a zip file in Python


code:
import os, time

pipe_path = "/tmp/mypipe"
if not os.path.exists(pipe_path):
    os.mkfifo(pipe_path)
# Open the fifo. We need to open in non-blocking mode or it will stalls until
# someone opens it for writting
pipe_fd = os.open(pipe_path, os.O_RDONLY | os.O_NONBLOCK)
with os.fdopen(pipe_fd) as pipe:
    while True:
        message = pipe.read()
        if message:
            print("Received: '%s'" % message)
        print("Doing other stuff")
        time.sleep(0.5)

rawDescription: Send message to a Python Script

description: Send message to a Python Script


code:
from itertools import count

class Obj(object):
  _ids = count(0)

  def __init__(self):
    self.id = self._ids.next()

rawDescription: Counting instances of a class?

description: Counting instances of a class


code:
import itertools
squares = (x*x for x in itertools.count())

rawDescription: Unbounded xrange()

description: Unbounded xrange()


rawDescription: Python - formatted chat client

description: Python - formatted chat client


code:
def _call_do_something(obj):
    obj.do_something ()
    return obj

# ...
obj_list = pool.map(_call_do_something, obj_list)

rawDescription: how can i use multiprocessing.Pool.map in python for function in objects?

description: Use multiprocessing.Pool.map in python for function in objects


rawDescription: Regarding condition on code

description: Regarding condition on code


code:
queryset = CitiesTable.objects.order_by('country', 'region_or_state')

countries_dict = {}

for country, group in groupby(queryset, lambda x: x.country):
    countries_dict[country] = {}
    for region, inner_group in groupby(group, lambda x: x.region_or_state):
        countries_dict[country][region] = list(inner_group)

rawDescription: Nested GROUP BY in django: returning Objects


code:
def canonicalize(state, canonical_states):
    return canonical_states.setdefault(state, state)

rawDescription: How to get the matching element of a set in Python 3?

description: Get the matching element of a set in Python 3


code:
x
x['unique'] = x.A.map(x.A.value_counts())
x

rawDescription: count of unique occurrences of a value pandas python

description: Count of unique occurrences of a value pandas python


code:
with r.pipeline() as pipe:
    while True:
        try:
            pipe.watch("a_hash")
            if pipe.exists("a_hash"):
                pipe.multi()
                pipe.hset("a_hash", "key", "value")
                pipe.execute()
            break
        except redis.WatchError:
            continue
        finally:
            pipe.reset()

rawDescription: Redis, only allow operation on existing keys


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

# code to simulate the perceptron goes here...

res = your_object.alpha_convergence_function()
res = np.asarray(res)

print('array size:', res.shape)

plt.xkcd()   # so you get the sketchy look :)

# first column -> x-axis, second column -> y-axis
plt.plot(res[:,0], res[:,1])
plt.show()

rawDescription: Drawing a 2D function in matplotlib

description: Drawing a 2D function in matplotlib


code:
def check_contain_chinese(check_str):
    for ch in check_str.decode('utf-8'):
        if u'\u4e00' <= ch <= u'\u9fff':
            return True
    return False

rawDescription: Judge Python str include Chinese

description: Judge Python str include Chinese


code:
class YourForm(forms.ModelForm):
    amount = forms.FloatField()

    def __init__(self, *args, **kwargs):
        super(YourForm, self).__init__(*args, **kwargs)
        self.fields['amount'].widget = forms.HiddenInput()
        self.fields['amount'].initial = ''

rawDescription: Hidden form field using none model forms


code:
import numpy as np

timesteps = []
timestep_results = []

f = open("ascii-full.csv", "rb")

# First line is number of rows (not counting the initial #)
rows = int(f.readline().strip()[1:].split()[-1])
counter = 0

# Second line is number of columns
columns = len(f.readline().strip().split())

# Next lines
for line in f:
    if line.startswith("#"):
        # it's a header: add time to timestep list, begin new array
        timesteps.append( float(line.strip().split("=")[1]) )
        timestep_results.append( np.zeros((rows, columns)) )
        counter = 0
    else:
        # it's data: add to array in appropiate row
        timestep_results[-1][counter] = map(float, line.strip().split())
        counter += 1

f.close()

rawDescription: Python: read timesteps from csv to arrays: Post-processing model-data with numpy;


code:
import requests

url = 'https://api.github.com/users/braitsch'
headers = {'Accept': 'application/json'}
auth = ('username', 'userpass')
response = requests.get(url, headers=headers, auth=auth)

with open('outputfile.json', 'w') as outf:
    outf.write(response.content)

rawDescription: Saving json response in python

description: Saving json response in python


code: l[l.index(start): l.index(end) + 1]

rawDescription: How to delete items on the left of value1 and on the right of value2 in a list of ints in Python?

description: Delete items on the left of value1 and on the right of value2 in a list of ints in Python


code:
import wx

class myFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Code Example', size=(300, 200))
        panel = wx.Panel(self, -1)
        sampleList = ['A', 'B', 'C', 'D']
        wx.StaticText(panel, -1, "Select option:", (15, 20))
        self.myChoice=wx.Choice(panel, -1, (85, 18), choices=sampleList)
        self.myChoice.Bind(wx.EVT_SET_FOCUS,self.onFocus)
        # options available in drop down list: A,B,C,D

    def onFocus(self,evt):
        # refresh your items with .Clear, .Append(), etc
        # I'm just adding new item every time user clicks on control
        self.myChoice.Append('''I'm new here''')

app = wx.PySimpleApp()
myFrame().Show()
app.MainLoop()

rawDescription: Filling wx.Choice during selection

description: Filling wx.Choice during selection


rawDescription: How to check if object attribute is of type method-wrapper?

description: Check if object attribute is of type method-wrapper


code:
import pygit2
repo = pygit2.Repository('.git')
branch = repo.lookup_branch('new')
ref = repo.lookup_reference(branch.name)
repo.checkout(ref)

rawDescription: How do you checkout a branch with pygit2?

description: Checkout a branch with pygit2


code:
{
    'ZACTION': 'AJAX',
    'ZMETHOD': 'LOGIN',
    'func': 'LOGIN',
    'USERNAME': '<enter username>',
    'USERPASS': '<enter password>'
},

rawDescription: using requests to login to a website that has javascript login form

description: Using requests to login to a website that has javascript login form


code:
self.CheckVar = IntVar(value=1)
self.checkbutton = Checkbutton(..., variable = self.CheckVar)

rawDescription: Python, Tkinter : if there a way to check checkboxes by default?


code:
def get(self, *args, **kwargs):
    return self.get_query_set().get(*args, **kwargs)

rawDescription: In a Django manager, why use self.get_query_set().get(kwarg=val) rather than self.get(kwarg=val)?


code: c = [x[0] for x in A]

rawDescription: First items in inner list efficiently as possible


code:
def make_filetype_aware_handler(handler_class):
    class DontRepeatFiletypeHandler(handler_class):

        def __init__(self, *args, **kwds):
            super().__init__(*args, **kwds)
            self.previous_types = set()

        def emit(self, record):
            if not record.file_type in self.previous_types:
                self.previous_types.add(record.file_type)
                super().emit(record)
    return DontRepeatFiletypeHandler

FiletypeStreamHandler = make_filetype_aware_handler(logging.StreamHandler)

logger = logging.getLogger()
logger.addHandler(FiletypeStreamHandler(sys.stderr))
logger.debug('Found file of type %(file_type)', file_type='x-type/zomg')

rawDescription: logging in continouous loop

description: Logging in continouous loop


rawDescription: Pandas Dataframe Mask based on index

description: Pandas Dataframe Mask based on index


code:
import lxml.html as lh
import cStringIO

content='''
<html>
    <body>
        <invalid html here/>
        <dont care> ... </dont care>
        <invalid html here too/>
        <interesting attrib1="naah, it is not this"> ... </interesting tag>
        <interesting attrib1="yes, this is what we want">
            <group>
                <line>
                    data
                </line>
            </group>
            <group>
                <line>
                    data1
                <line>
            </group>
            <group>
                <line>
                    data2
                <line>
            </group>
        </interesting>
    </body>
</html>
'''
doc=lh.parse(cStringIO.StringIO(content))
tags=doc.xpath('descendant::*[@attrib1="yes, this is what we want"]')
print(tags)
# [<Element interesting at b767e14c>]
for tag in tags:
    print(lh.tostring(tag))
# <interesting attrib1="yes, this is what we want"><group><line>
#                     data
#                 </line></group><group><line>
#                     data1
#                 <line></line></line></group><group><line>
#                     data2
#                 <line></line></line></group></interesting>

rawDescription: Find all tags with a specific attribute value


code:
def to_secs(time):
    time = [int(i) for i in time.split(':')][::-1]
    return sum(time[i]*(60**i) for i in range(len(time)))

def to_clock(secs):
    time = ['']*3
    for i in range(2, -1, -1):
        time[i] = str(int(secs/(60**i))).zfill(2)
        secs %= 60**i
    return ':'.join(time[::-1])

assert(to_secs('123:0') == 7380)
assert(to_clock(1) == '00:00:01')

rawDescription: convert string into seconds using ctime

description: Convert string into seconds using ctime


code:
db.define_table('review',
    ...,
    Field('reviewer', 'reference auth_user', default=auth.user_id, writable=False),
    ...)

rawDescription: Saving author of form Web2Py

description: Saving author of form Web2Py


code:
if form.is_valid():
    story = form.save()
    return HttpResponseRedirect(reverse('story_detail', args=(story.user, story.id)))

rawDescription: How to redirect to a newly created object in Django without a Generic View's post_save_redirect argument

description: Redirect to a newly created object in Django without a Generic View's post_save_redirect argument


code:
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.properties import ListProperty, ObjectProperty
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button

class MultiSelectSpinner(Button):
    """Widget allowing to select multiple text options."""

    dropdown = ObjectProperty(None)
    """(internal) DropDown used with MultiSelectSpinner."""

    values = ListProperty([])
    """Values to choose from."""

    selected_values = ListProperty([])
    """List of values selected by the user."""

    def __init__(self, **kwargs):
        self.bind(dropdown=self.update_dropdown)
        self.bind(values=self.update_dropdown)
        super(MultiSelectSpinner, self).__init__(**kwargs)
        self.bind(on_release=self.toggle_dropdown)

    def toggle_dropdown(self, *args):
        if self.dropdown.parent:
            self.dropdown.dismiss()
        else:
            self.dropdown.open(self)

    def update_dropdown(self, *args):
        if not self.dropdown:
            self.dropdown = DropDown()
        values = self.values
        if values:
            if self.dropdown.children:
                self.dropdown.clear_widgets()
            for value in values:
                b = Factory.MultiSelectOption(text=value)
                b.bind(state=self.select_value)
                self.dropdown.add_widget(b)

    def select_value(self, instance, value):
        if value == 'down':
            if instance.text not in self.selected_values:
                self.selected_values.append(instance.text)
        else:
            if instance.text in self.selected_values:
                self.selected_values.remove(instance.text)

    def on_selected_values(self, instance, value):
        if value:
            self.text = ', '.join(value)
        else:
            self.text = ''

kv = '''
BoxLayout:
    orientation: 'vertical'

    BoxLayout:

        Label:
            text: 'Select city'

        MultiSelectSpinner:
            id: city
            values: 'Sydney', 'Moscow', 'Warsaw', 'New York', 'Tokio'

    BoxLayout:

        Label:
            text: 'Select your favorite food'

        MultiSelectSpinner:
            id: food
            values: 'Fish and chips', 'Hot-dog', 'Hamburger'

    Label:
        text: 'You selected {} cities and {} as your favourite food.'.format(city.text, food.text)

<MultiSelectOption@ToggleButton>:
    size_hint: 1, None
    height: '48dp'

'''

runTouchApp(Builder.load_string(kv))

rawDescription: kivy spinner widget with multiple selection

description: Kivy spinner widget with multiple selection


code:
str = "[[Category:Political culture]]\n\n          [[Category:Political ideologies]]\n\n"
re.findall(r'Category:([^\[\]]*)', str)
re.findall(r'Category:[^\[\]]*', str)

rawDescription: Extract text between double square brackets in Python


code:
names = ["bob","phil","james"]

letter = input("Pick a letter.")
letter = letter.lower()
count = 0

for name in names:
    if name.startswith(letter):
        print(name)
        count += 1

rawDescription: How could I check if a value in an array begins with a certain letter?


code:
# end='\n' won't do anything since '\n' is already the default value for end
print(listing['href']+","+ str(listing.string), end='\n\n')

rawDescription: How Do I separate Prints In Python?

description: How Do I separate Prints In Python


code:
import base64

exe = 'c:/users/me/program.exe'
script = 'c:/users/me/newpython.py'
chunksize = 6144
linelength = 80

with open(exe,'rb') as inf, open(script,'w') as outf:
    outf.write('program_data = """')

    while True:
        progdata = inf.read(chunksize)
        if len(progdata):
            progdata = progdata.encode("base64")
            outf.writelines(progdata[i:i+linelength] for i in range(0, len(progdata), linelength))
        else:
            break

    outf.write('""".decode("base64")')

rawDescription: files's binary data stored as variable inside python file

description: Files's binary data stored as variable inside python file


code:
def getTables(self, db=None):
    db = db or self._db

rawDescription: Python "self" is not defined in function arguments list


code:
lst = []
for i in xrange(4):
    lst.append({})
    lst[i]['a'] = False
any(d['a'] for d in lst)
lst[1]['a'] = True
any(d['a'] for d in lst)

rawDescription: Does a True value exist in a list of dictionaries?


code:
import numpy as np

A = np.array([3,4,4,3,6])
B = np.array([2,5,2,6,3,6,2,2,5])

def ismember(a, b):
    # tf = np.in1d(a,b) # for newer versions of numpy
    tf = np.array([i in b for i in a])
    u = np.unique(a[tf])
    index = np.array([(np.where(b == i))[0][-1] if t else 0 for i,t in zip(a,tf)])
    return tf, index

tf,ix=ismember(A,B)
print(tf)
# [ True False False  True  True]
print(ix)
# [4 0 0 4 5]
print(A[tf])
# [3 3 6]
print(B[ix[tf]])
# [3 3 6]

rawDescription: Replicating the indices result of Matlab's ISMEMBER function in NumPy?


code:
movie_records = sorted([(m['total_tickets'], m) for m in db.movies.find()])

# Select distinct on total_tickets
distinct_records = []
prev = None
while movie_records:
    record = movie_records.pop(0)
    if record[0] != prev:
        prev = record[0]
        distinct_records.append(record[1])

for index, movie in enumerate(distinct_records):
    tickets_pvalue = 1 - (index + 1.0) / len(distinct_records)
    shows.update({"id": movie["id"]},
                 {'$set': {"total_tickets_pvalue": tickets_pvalue}})

rawDescription: p values and lists in python

description: P values and lists in python


rawDescription: Python - How to convert this timestamp format to datetime?


code:
def traverse(path):
    files = os.listdir(path)
    for i in files:
        if os.path.isdir(os.path.join(path,i)):
            traverse(os.path.join(path,i))
        elif i.endswith('.txt'):
            print(os.path.join(path,i))

rawDescription: Traversing Directories

description: Traversing Directories


code:
import requests

ip = requests.get('http://httpbin.org/ip').json()['origin']

rawDescription: how can i find my public facing ip from python program not proxy

description: Find my public facing ip from python program not proxy


code:
# some code left out for brevity

class InputsModelExtendedForm(ModelForm):
    class Meta:
        model = InputsModelExtended

    def __init__(self, *args, **kwargs):
        service_choices = kwargs.pop('service_choices')
        super(InputsModelExtendedForm, self).__init__(*args, **kwargs)

        if service_choices:
            self.fields['service'] = forms.ChoiceField(choices=service_choices,
                                                       required=False)

# example usage:

def my_view(request):
    service_choices = (
        ('Foo', 'Foo',),
        ('Bar', 'Bar',),
        ('Baz', 'Baz',),
    )

    form = InputsModelExtendedForm(request.POST or None, service_choices=service_choices)

rawDescription: modifying django modelform select set

description: Modifying django modelform select set


code:
def clean(self):
    if self.scam and self.whitelist:
        raise ValidationError("Can't set whitelist and scam simultaneously.")
    if self.scam is False and self.whitelist is False:
        raise ValidationError("Negate either whitelist or scam or none.")

rawDescription: How to set django model field value based-on value of other field in the same Model

description: Set django model field value based-on value of other field in the same Model


code:
import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
        timer = QtCore.QTimer(self)
        timer.timeout.connect(self.showlcd)
        timer.start(1000)
        self.showlcd()

    def initUI(self):

        self.lcd = QtGui.QLCDNumber(self)
        self.lcd.setDigitCount(8)          # change the number of digits displayed
        self.setGeometry(30, 30, 800, 600)
        self.setWindowTitle('Time')

        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.lcd)
        self.setLayout(vbox)

        self.show()

    def showlcd(self):
        time = QtCore.QTime.currentTime()
        text = time.toString('hh:mm:ss')
        self.lcd.display(text)

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

rawDescription: To display system clock time in LCD format


code:
"""
Convert from rectangular (x,y) to polar (r,w)
    r = sqrt(x^2 + y^2)
    w = arctan(y/x) = [-\pi,\pi]
"""
def polar(x, y):        # w in radians
    from math import hypot, atan2, pi
    return hypot(x, y), atan2(y, x)

model_features = []
model = params(model_body_contour)    # return tuple (center_x, center_y, area)
for contour in model_feature_contours:
    f = params(countour)
    range, angle = polar(f[0]-model[0], f[1]-model[1])
    model_features.append((angle, range, f[2]))

image_features = []
image = params(image_body_contour)
for contour in image_feature_contours:
    f = params(countour)
    range, angle = polar(f[0]-image[0], f[1]-image[1])
    image_features.append((angle, range, f[2]))

# sort image_features and model_features by angle, range
#
# correlate image_features against model_features across angle offsets
#    rotation = angle offset of max correlation
#    scale = average(model areas and ranges) / average(image areas and ranges)

rawDescription: Calculating element position by computing transformation


code: list = [self.queryQ.put(query) for query in queries]

rawDescription: python - putting list items in a queue

description: Python - putting list items in a queue


code:
def sliding(s, k):
    if len(s) < k: return []
    else: return [s[:k]] + sliding(s[1:], k)

rawDescription: python recursive function calls

description: Python recursive function calls


code:
if obj.groups.filter(name="Administrator").exists():
    obj.is_staff = True
else:
    obj.is_staff = False

rawDescription: Django admin is_staff based on group

description: Django admin is_staff based on group


code:
from tkinter import *

class Window(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        # create master window
        self.master = master

        # create root window on initialization
        self.create_rootwindow()

    def create_rootwindow(self):
        self.master.title("GUI")
        self.master.geometry("1024x748")

        self.create_upperframe()
        self.create_lowerframe()
        self.create_inputentries()
        self.create_btnframe()
        self.create_inputbtn()

        self.upperframe.pack(side="top", fill="both", expand=True, padx=4)
        self.lowerframe.pack(side="left", fill="both", expand=True, padx=4, pady=4)
        self.btnframe.pack(side="right", fill="both", padx=4, pady=4)

    def create_upperframe(self):
        self.upperframe = Frame(self.master, width=980, height=490)
        self.upperframe.config(background="#339900")

    def create_lowerframe(self):
        self.lowerframe = Frame(self.master, width=830, height=200)
        self.lowerframe.config(background="#336699")

    def create_inputentries(self):
        self.create_lowerframe()

        parameternames = [
            ('a', 'U'), ('b', 'U'), ('c', 'U'), ('d', 'U'), ('e', 'U'),
            ('f', 'U'), ('g', 'U'), ('h', 'U'), ('i', 'U'), ('j', 'U'),
            ('k', 'U'), ('l', 'U'), ('m', 'U'), ('n', 'U'), ('o', 'U'),
            ('p', 'U'), ('q', 'U'), ('r', 'U'), ('s', 'U'), ('t', 'U')]

        for i, data in enumerate(parameternames):
            r = i % 5
            c = (i // 5) * 3
            Label(self.lowerframe, text=data[0]).grid(row=r, column=c, pady=4, padx=0, sticky="nsew")
            Entry(self.lowerframe, width=10).grid(row=r, column=c+1, pady=4, padx=0, sticky="ew")
            Label(self.lowerframe, text=data[1]).grid(row=r, column=c+2, pady=4, padx=0, sticky="nswe")

            self.lowerframe.grid_columnconfigure(c+1, weight=1)

    def create_btnframe(self):
        self.btnframe = Frame(self.master, width=130, height=200)

    def create_inputbtn(self):
        self.create_btnframe()

        startanalysis_btn = Button(self.btnframe, text="Start Analysis", width=12, command=self.do_nothing)
        abortanalysis_btn = Button(self.btnframe, text="Abort Analysis", width=12, command=self.do_nothing)
        resetanalysis_btn = Button(self.btnframe, text="Reset Analysis", width=12, command=self.do_nothing)
        showresults_btn = Button(self.btnframe, text="Show Results", width=12, command=self.do_nothing)

        startanalysis_btn.pack(side="top", fill="x")
        abortanalysis_btn.pack(side="top", fill="x")
        resetanalysis_btn.pack(side="top", fill="x")
        showresults_btn.pack(side="top", fill="x")

    def do_nothing(self):
        pass

root = Tk()
app = Window(root)
root.mainloop()

rawDescription: tkinter grid: distribute rows and columns over frame height and width

description: Tkinter grid: distribute rows and columns over frame height and width


code:
from Tkinter import *
import time,random

root = Tk()
root.geometry("500x500")
bouncer = Label(root, text="=D")

def bounce ():
    X = random.randint(1,500)
    Y = random.randint(1,500)
    bouncer.place(x=X, y=Y)
    root.after(1000, bounce)

bounce()
root.mainloop()

rawDescription: Python - Tkinter - Fun with place()

description: Python - Tkinter - Fun with place()


code:
a = np.linspace(-2, 0, 6)
b = np.linspace(0, 2, 6)

out = np.empty((len(a), len(b), 2))
out[..., 0] = a[:, None]
out[..., 1] = b[None, :]
out = out.reshape(-1, 2)
out

rawDescription: Python: looping through values of two vectors


code:
result = {}
for item in data:
    result.setdefault(item[0], {}).update({item[1]: item[2]})

rawDescription: Turning a two dimensional list into a dictionary in python


code: newstr = oldstr[:oldstr.find("\t")]

rawDescription: Remove substrings of variable length from string


code:
import random

def main():
    random_int = random.randint(6, 12)
    print (random_int)
    elements = makelist(random_int)
    for i in sorted(elements):
        print (i,)

def makelist(random_int):
    num_list = []
    for count in range(random_int):
        num_list.append(random.randint(1, 100))
    return num_list

main()

rawDescription: program to generate a random length list of random numbers in Python

description: Program to generate a random length list of random numbers in Python


rawDescription: Multi Index Sorting in Pandas

description: Multi Index Sorting in Pandas


code:
from __future__ import print_function

from os import environ

from twisted.internet.task import react
from twisted.internet.endpoints import HostnameEndpoint
from twisted.web.client import ProxyAgent

def main(reactor, proxy_hostname):
    endpoint = HostnameEndpoint(reactor, proxy_hostname, 80)
    agent = ProxyAgent(endpoint)
    return agent.request(b"GET", b"http://google.com/").addCallback(print)

react(main, [environ["HTTP_PROXY"]])

rawDescription: Twisted getpage using proxy

description: Twisted getpage using proxy


code: httpd = ServerClass(("127.0.0.1", 80), HandlerClass)

rawDescription: Python 3 and http.server usage

description: Python 3 and http.server usage


code: response = requests.post(url, data=data, headers=headers)

rawDescription: Python POST request can't find data attribute


code:
def _do_upsert(self, conn, item, spider):
    """Perform an insert or update."""

    guid = self._get_guid(item)
    now = datetime.utcnow().replace(microsecond=0).isoformat(' ')

    conn.execute("""SELECT EXISTS(
        SELECT 1 FROM"""+spider.table_name+"""WHERE guid = %s
    )""", (guid, ))
    ret = conn.fetchone()[0]

rawDescription: Acces to spider variable in pipeline

description: Acces to spider variable in pipeline


code:
'''
Some string
with
enters.
'''

rawDescription: New line in python?

description: New line in python


code:
def foo(bar):
    root.findall(".//*[@name='{}']".format(bar))

rawDescription: How to concatenate individual quote marks in Python

description: Concatenate individual quote marks in Python


code:
class MyActiveManager(models.Manager):
    def active(self):
        return self.filter(is_active=True)

    def online(self):
        return self.filter(is_online=True)

class MyModel:
    ...
    objects = MyActiveManager()

...

active_objects = MyModel.objects.active()
online_objects = MyModel.objects.online()

rawDescription: Where should custom query functions be placed?


code:
from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.mapper = QtCore.QSignalMapper(self)
        self.toolbar = self.addToolBar('Foo')
        self.toolbar.setToolButtonStyle(QtCore.Qt.ToolButtonTextOnly)
        for text in 'One Two Three'.split():
            action = QtGui.QAction(text, self)
            self.mapper.setMapping(action, text)
            action.triggered.connect(self.mapper.map)
            self.toolbar.addAction(action)
        self.mapper.mapped['QString'].connect(self.handleButton)
        self.edit = QtGui.QLineEdit(self)
        self.setCentralWidget(self.edit)

    def handleButton(self, identifier):
        if identifier == 'One':
            text = 'Do This'
        elif identifier == 'Two':
            text = 'Do That'
        elif identifier == 'Three':
            text = 'Do Other'
        self.edit.setText(text)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.resize(300, 60)
    window.show()
    sys.exit(app.exec_())

rawDescription: How to pass arguments to callback functions in PyQt

description: Pass arguments to callback functions in PyQt


code:
query = "SELECT to_char(gltx.post_date, 'YYYYMMDD') FROM gltx WHERE (gltx.post_date > %s AND gltx.post_date < %s)"
args = (start_date, end_date)
cursor.execute(query, args)

rawDescription: In Python entering variables(dates) from raw_input into postgreSQL SELECT...WHERE


code: sorted(set(y))

rawDescription: Understanding the set() function

description: Understanding the set() function


code:
hourly = ts.resample('H')
hourly.index = pd.MultiIndex.from_arrays([hourly.index.hour, hourly.index.normalize()])
hourly.unstack().plot()

rawDescription: How to remove day from datetime index in pandas?

description: Remove day from datetime index in pandas


code:
import rstr
rstr.xeger(r'[A-Z]\d[A-Z] \d[A-Z]\d')

rawDescription: Generate a String that matches a RegEx in Python


code:
class Person(object):
    def __init__(self, name, turn):
        self.name = name
        self.turn = turn

    @classmethod
    def create(cls, turn):
        while True:
            name = input("What is the name of Player %d? " % turn)
            if name.isalpha():
                break;
            print("\nPlease share your name with me.\n")
        print("\nNice to meet you %s. It will be fun to play Battleship!\n" % name)
        return cls(name, turn)

Player1 = Person.create(1)
Player2 = Person.create(2)

print("Welcome to Battleship, %s!" % Player1.name)
print("You will take turn %s.\n" % Player1.turn)

rawDescription: Writing DRY Code in Python

description: Writing DRY Code in Python


code:
blink_enabled = False

def loop():
    webiopi.sleep(1)
    if blink_enabled:
        blink_once()

def blink_once():
    GPIO.digitalWrite(LIGHT1, GPIO.HIGH)
    time.sleep(0.05)
    GPIO.digitalWrite(LIGHT2, GPIO.HIGH)
    GPIO.digitalWrite(LIGHT1, GPIO.LOW)
    time.sleep(0.05)
    GPIO.digitalWrite(LIGHT3, GPIO.HIGH)
    GPIO.digitalWrite(LIGHT2, GPIO.LOW)
    time.sleep(0.05)

@webiopi.macro
def stopLoop():
    blink_enabled = False

@webiopi.macro
def idleLoop():
    blink_enabled = True

rawDescription: Exiting a continuous loop in python via webiopi


rawDescription: How do I check if two variables reference the same object in Python?

description: Check if two variables reference the same object in Python


code:
import os
import argparse

# Global preparser and options.
preparser = argparse.ArgumentParser(add_help=False)
preparser.add_argument("--user", dest="user", default=os.environ.get("USER"))

# ****** NEW *******
options, _ = preparser.parse_known_args() # Ignore what we haven't defined yet
user = options.user                       # Use this to define the default of --config

parser = argparse.ArgumentParser(parents=[ preparser ], add_help=True,
                   formatter_class=argparse.ArgumentDefaultsHelpFormatter)

# Sub-command parser and options.
subparsers = parser.add_subparsers()

command = subparsers.add_parser("command")

# ****** MODIFIED *******
command.add_argument("--config", dest="config", default="~%s/config" % (user,))

options = parser.parse_args()

rawDescription: argparse augment sub-command defaults via global options

description: Argparse augment sub-command defaults via global options


code:
@register.inclusion_tag("admin/change_list_results.html", takes_context=True)
def my_result_list(context, cl):
    """
    Displays the headers and data list together
    """
    myvar = cl.sample(context['request'])
    num_sorted_fields = 0

rawDescription: How can i pass the request object in inclusion tag in django

description: Pass the request object in inclusion tag in django


code:
def produce_callable(*args, **kw):
    instance = YourClass(*args, **kw)
    def callable_function(*args, **kw):
        """Docstring here"""
        return instance(*args, **kw)
    return callable_function

rawDescription: Is there a way to define the docstring for a python object that defines __call__?


code:
with open('example.csv', 'rb') as csvfile:

    #detect the delimiter used
    dialect = csv.Sniffer().sniff(csvfile.read(1024))

    #return to the beginning of the file
    csvfile.seek(0)

    #file should now open with the correct delimiter.
    reader = csv.reader(csvfile, dialect)

rawDescription: How to get the system wide list separator on Windows

description: Get the system wide list separator on Windows


rawDescription: Pairs and dict in python

description: Pairs and dict in python


code:
myseries[myseries == 7]
myseries[myseries == 7].index[0]

rawDescription: Find element's index in pandas Series

description: Find element's index in pandas Series


rawDescription: Removing a row in a numpy recarray

description: Removing a row in a numpy recarray


code:
data = np.array([[2.0, 3.0, 6.5, 6.5, 12.0],[1,2,3,4,5]], dtype=float)
data2 = np.fabs(data - 6.6)
np.unravel_index(data2.argmin(), data2.shape)

rawDescription: Get index of the element nearest to the given value


rawDescription: Numpy - easier way to change the value of one column of an array only?


code:
phone_numbers = {'Ted': '555-222-1234', 'Sally': '555-867-5309'}
def phonebook(dct, name):
    print("{}'s phone number is: {}".format(name, dct[name]))
phonebook(phone_numbers, 'Ted')

rawDescription: How to reference a dictionary key passed into a function?

description: Reference a dictionary key passed into a function


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

from skimage import data
from skimage.util import img_as_ubyte
from skimage import exposure
import skimage.morphology as morp
from skimage.filters import rank

# Original image
img = img_as_ubyte(data.moon())

# Global equalize
img_global = exposure.equalize_hist(img)

# Local Equalization, disk shape kernel
# Better contrast with disk kernel but could be different
kernel = morp.disk(30)
img_local = rank.equalize(img, selem=kernel)

fig, (ax_img, ax_global, ax_local) = plt.subplots(1, 3)

ax_img.imshow(img, cmap=plt.cm.gray)
ax_img.set_title('Low contrast image')
ax_img.set_axis_off()

ax_global.imshow(img_global, cmap=plt.cm.gray)
ax_global.set_title('Global equalization')
ax_global.set_axis_off()

ax_local.imshow(img_local, cmap=plt.cm.gray)
ax_local.set_title('Local equalization')
ax_local.set_axis_off()

plt.show()

rawDescription: local histogram equalization

description: Local histogram equalization


code:
lst = [0, 1, 2, 3, 4, 5]
new_lst = lst[1:]
new_lst.append(lst[0])

rawDescription: Python Start counting array of length x starting from second item and finishing on the first one


code:
def descriptive_func(df):
    """
    Takes a df where key is always equal and returns some summary.
    :type df: pd.DataFrame
    :rtype: pd.Series|pd.DataFrame
    """
    pass

# filter down to those rows we're interested in
valid_rows = b[b["key"].isin(set(keys))]

# this groups by the value and applies the descriptive func to each sub df in turn
summary = valid_rows.groupby("key").apply(descriptive_func)

rawDescription: Return subset/slice of Pandas dataframe based on matching column of other dataframe, for each element in column?


code:
# Say your csv file has 10 columns, 1-9 are features and 10
# is the Y you're trying to predict.
cols = range(0,10)
X = np.loadtxt('data.csv', delimiter=',', dtype=float, usecols=cols, ndmin=2, skiprows=1)
Y = np.loadtxt('data.csv', delimiter=',', dtype=float, usecols=(9,), ndmin=2, skiprows=1)
# note how for Y the usecols argument only takes a sequence,
# even though I only want 1 column I have to give it a sequence.

rawDescription: numpy wrong shape of imported data and separating the y value

description: Numpy wrong shape of imported data and separating the y value


code:
import os
import sys
from subprocess import Popen

# run all child scripts in parallel
processes = [Popen([sys.executable, filename], cwd=dirpath)
             for dirpath, dirname , filenames in os.walk('.')
             for filename in filenames
             if filename == 'Test.py']

# wait until they finish
for p in processes:
    p.wait()
print("all done")

rawDescription: How to use python script to start other python scripts and check whether all "child scripts" are finished?

description: Use python script to start other python scripts and check whether all "child scripts" are finished


code:
with open('myfile.csv') as f1:
    mytuples = []
    for line in f1:
        tokens = line.strip().split(',')
        mytuples.append( (int(tokens[0]), int(tokens[1]), tokens[2]) )

rawDescription: Convert first 2 elements of tuple to integer


code:
# generate a sample time series
import pandas as pd
from datetime import datetime, timedelta

s = pd.Series(["A", "B", "C", "D", "E"], index=pd.date_range("2000-01-01", periods=5, freq="12h"))
print(s)

# these two indices should access the same value ("C")
integer_index = 2
# Converts the string into a datetime object
date_index = datetime.strptime("2000-01-02 00:00", "%Y-%m-%d %H:%M")
print(date_index) # 2000-01-02 00:00:00

print(s[integer_index])  # prints "C"
print(s[date_index])  # prints "C"

print(s[integer_index - 2])  # prints "A"

one_day = timedelta(days=1)
print(s[date_index - one_day]) # prints "A"
print(date_index - one_day) # 2000-01-01 00:00:00

rawDescription: Arithmetic operations on datetime index in pandas


code:
def __repr__(self):
    return str(self._x)

rawDescription: How to get Python Class to Return Some Data and not its Object Address

description: Get Python Class to Return Some Data and not its Object Address


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

plt.rc('font',family='serif')

time = np.random.random(100)
radius = np.random.random(100)
theta = np.random.random(100)

fig = plt.figure()
ax1 = fig.add_subplot(111)
im = ax1.scatter(radius,theta,c=time,s=40,cmap=plt.cm.Reds)
ax1.set_xlabel('radius')
ax1.set_ylabel('theta')
plt.colorbar(im,ax=ax1)

fig.show()

rawDescription: Plot positions with different shades

description: Plot positions with different shades


code:
import collections
import functools
import threading

threadlocal = threading.local()

def threadlocal_var(varname, factory, *a, **k):
  v = getattr(threadlocal, varname, None)
  if v is None:
    v = factory(*a, **k)
    setattr(threadlocal, varname, v)
  return v

def monitoring(identifier=None, unique=True, use_function=False):
  def inner(f):
    assert (not use_function) or (identifier is None)
    if identifier is None:
      if use_function:
        identifier = f
      else:
        identifier = f.__name__
    if unique:
      monitored = threadlocal_var('uniques', set)
      if identifier in monitored:
        raise ValueError('Duplicate monitoring identifier %r' % identifier)
      monitored.add(identifier)
    counts = threadlocal_var('counts', collections.defaultdict, int)
    @functools.wraps(f)
    def wrapper(*a, **k):
      counts[identifier] += 1
      try:
        return f(*a, **k)
      finally:
        counts[identifier] -= 1
    return wrapper
  return inner

rawDescription: Efficient way to determine whether a particular function is on the stack in Python


rawDescription: How to convert dates with alphabetic months to timestamp

description: Convert dates with alphabetic months to timestamp


code:
class DNAFeature(Range):

    def __init__(self, start, end):
        self.setStart(start)
        self.setEnd(end)
        self.strand = None
        self.sequencename = None

    def setStrand(self, s):
        self.strand = s

    def getStrand(self):
        if self.sequenceName == 'plus':
            return 1
        elif self.sequenceName == 'minus':
            return -1
        else:
            return 0

    def setSequenceName(self, s):
        self.sequencename = s

    def getSequenceName(self, s):
        return self.sequenceName

rawDescription: Python Range Class/Subclass

description: Python Range Class/Subclass


code: print("First item: {:d}, second item: {:d} and third item: {:d}.".format(*tuple))

rawDescription: New style formatting with tuple as argument


code:
import unicodedata
unicodedata.normalize('NFKD', u"m\u00fasica").encode('ascii', 'ignore')

rawDescription: Replace non-ascii chars from a unicode string in Python


code:
#!/System/Library/Frameworks/Python.framework/Versions/2.5/Resources/Python.app/Contents/MacOS/Python
import sys, os
execdir = os.path.dirname(sys.argv[0])
executable = os.path.join(execdir, "Python")
resdir = os.path.join(os.path.dirname(execdir), "Resources")
libdir = os.path.join(os.path.dirname(execdir), "Frameworks")
mainprogram = os.path.join(resdir, "main.py")

sys.argv.insert(1, mainprogram)
pypath = os.getenv("PYTHONPATH", "")
if pypath:
    pypath = ":" + pypath
os.environ["PYTHONPATH"] = resdir + pypath
os.environ["PYTHONEXECUTABLE"] = executable
os.environ["DYLD_LIBRARY_PATH"] = libdir
os.environ["DYLD_FRAMEWORK_PATH"] = libdir
os.execve(executable, sys.argv, os.environ)

rawDescription: How to change the app name in OSX menubar in a pure-Python application bundle?

description: Change the app name in OSX menubar in a pure-Python application bundle


code:
import sys

def read_fasta(filename):
    fasta_dict = {}
    l = list()
    header = None
    with open(filename, 'r') as f:
        for line in f:
            if line.startswith('>'): # a new record
                # save the previous record to the dict
                if header:
                    fasta_dict[header] = ''.join(l)
                    del l[:]    # empty the list

                header = line.strip().split('>')[1]
            else:
                l.append(line.strip())

        # save the last record
        fasta_dict[header] = ''.join(l)

    return fasta_dict

fastadict = read_fasta(sys.argv[1])
print(fastadict)

rawDescription: Concatenating lines to a string in python


code:
session.query(SomeClass). \
    offset(x). \
    limit(y). \
    all()

rawDescription: How to return a range of result in sqlalchemy

description: Return a range of result in sqlalchemy


rawDescription: ipython notebook lost after deleting a folder

description: Ipython notebook lost after deleting a folder


code:
for i, c in enumerate(color_palette):
    _col_pick.insert(tk.END, c)
    _col_pick.itemconfig(i, {'bg':c, 'selectbackground':c})

rawDescription: Tkinter.Listbox(): Active line without change of color?


code:
routes_in = (
    #Map the default index to '/'
    ('/', '/myapp/default/index'),
    #Allow the admin application to be accessed
    ('/a', '/a/default/index'),
    ('/a/$anything', '/a/$anything'),
    #Re-wire actions as controllers unless the controller is also specified
    ('/$controller', '/myapp/$controller/index'),
    ('/$controller/', '/myapp/$controller/index'),
    ('/$controller/$action', '/myapp/$controller/$action'),
    #Map static access to our application
    ('/static/$anything', '/myapp/static/$anything'),
)
#Reverse only some of the mappings so that URL() works correctly
routes_out = (
    ('/myapp/default/index', '/'),
    ('/a/$anything', '/a/$anything'),
    ('/myapp/$controller/index', '/$controller'),
    ('/myapp/$controller/$action', '/$controller/$action'),
    ('/myapp/static/$anything', '/static/$anything'),
)

rawDescription: How to route a single web2py application with the shortest possible URL without losing the admin application

description: Route a single web2py application with the shortest possible URL without losing the admin application


code:
import heapq

heapq.nlargest(10, zip(x, y))

rawDescription: Finding Minimum in Python Arrays

description: Finding Minimum in Python Arrays


code:
for el in args_list:
    globals()[el] = __import__(el, globals(), locals())

rawDescription: Importing any number of modules passed from the command line? (Boils down to `how to unstring a string?`)


code:
def get_dynamic_settings(context_dict):
    context_dict['DEFAULT_FROM_EMAIL'] = '%s <noreply@otakupride.com>' % context_dict['SITE_NAME']
    context_dict['ROOT_URLCONF'] = 'mysite.urls.%s' % context_dict['SITE_SLUG']
    context_dict['TEMPLATE_DIRS'] += (os.path.join(PROJECT_ROOT, "templates", context_dict['SITE_SLUG']),)

rawDescription: Can I access the __dict__ object for the local scope?

description: Access the __dict__ object for the local scope


code:
# first convert Series to DataFrame
df_segmenti_t0 = DataFrame(segmenti_t0)

# save as excel spreadsheet
df_segmenti_t0.to_excel('prova.xls')

rawDescription: python pandas csv exporting

description: Python pandas csv exporting


code:
import re
re.sub(r'\bin\b', '', 'office administration in delhi')

rawDescription: Replace exact substring in python

description: Replace exact substring in python


code:
import csv
import tempfile
import shutil

input_file = 'minitest.csv'

with open(input_file, 'rb') as f, \
     tempfile.NamedTemporaryFile(delete=False) as out_f:

    # in order to be able to not have to read everything in memory, we have to
    # write every processed row to disk immediatley; for that, we need a temporary
    # file because we can't read and write a single file at the same time:
    reader = csv.reader(f)
    writer = csv.writer(out_f)

    # header row
    writer.writerow(next(reader))

    # note that this uses a generator not a list, so that writerows will lazily
    # evaluate each row as it writes them to disk
    writer.writerows(row + [float(row[2]) * float(row[5])] for row in reader)

# one everything's done, overwrite the original file with the new contents.
shutil.move(out_f.name, input_file)

rawDescription: How to multiply one column by another in a csv file and re-write

description: Multiply one column by another in a csv file and re-write


code:
from Tkinter import *

root = Tk()

var = IntVar() # instantiate the IntVar variable class
var.set(0)     # set it to 0 as the initial value

# the button command is a lambda expression that calls the set method on the var,
# with the var value (var.get) increased by 1 as the argument
Button(root, text="Next Customer", command=lambda: var.set(var.get() + 1)).pack()

# the label's textvariable is set to the variable class instance
Label(root, textvariable=var).pack()

mainloop()

rawDescription: Update label content on click using Python


code:
import datetime
import time

datetime.time()

rawDescription: "import time" and "from datetime import time" together?


code:
import textwrap
textwrap.wrap('The quick brown fox jumps over the lazy dog', 12)

rawDescription: How to trim text at word boundary in Python

description: Trim text at word boundary in Python


code:
def isPrime(n):
    return 0 == sum(map(divides(n),range(2,1+int(n**.5))))

rawDescription: Is number prime? (no recursion or loops)


code:
numbers_in_nxn = random.sample(range(n*n), k) # Use xrange in Python 2.x
tuples_in_nxn = [divmod(x,n) for x in numbers_in_nxn]

rawDescription: python: sampling without replacement from a 2D grid

description: Python: sampling without replacement from a 2D grid


code:
list1 = [...]
tmpSet = set(list1)
haveDuplicates = len(list1) != len(tmpSet)

rawDescription: Python methods to find duplicates

description: Python methods to find duplicates


code:
from collections import OrderedDict

apple_types = fruits['apple']['types']
types_sorted = OrderedDict(sorted(apple_types.items(), key=lambda x: -x[1]['sort']))

rawDescription: Sorting Python OrderedDict in "heavily nested" structure


code:
#!python3

def print_around_pattern(pattern, fname, numlines=10):
    """Prints the lines with the pattern from the fname text file.

    The pattern is a string, numline is the number of lines printed before
    and after the line with the pattern (with default value 10).
    """

    history = []
    cnt = 0
    with open(fname, encoding='utf8') as fin:
        for n, line in enumerate(fin):
            history.append(line)            # append the line
            history = history[-numlines-1:] # keep only the tail, including last line
            if pattern in line:
                # Print the separator and the history lines including the pattern line.
                print('\n{!r} at the line {} ----------------------------'.format(
                         pattern, n+1))
                for h in history:
                    print('{:03d}: {}'.format(n-numlines, h), end='')
                cnt = numlines              # set the counter for the next lines
            elif cnt > 0:
                # The counter indicates we want to see this line.
                print('{:03d}: {}'.format(n+1, line), end='')
                cnt -= 1                    # decrement the counter

if __name__ == '__main__':
    print_around_pattern('out_of_memory', 'loggy.json')
    ##print_around_pattern('out_of_memory', 'loggy.json', 3)  # three lines before and after

rawDescription: Python program for json files

description: Python program for json files


code:
mean = mx.mean(axis=0)
mean[mean.mask] = mx.fill_value
mean

rawDescription: Unmasking of masked Numpy array changes masked values to 0's


rawDescription: mod_wsgi/python sys.path.exend problems

description: Mod_wsgi/python sys.path.exend problems


code:
q = (session.query(Parent)
     .filter(Parent.children.any(Child.value.ilike('%{}%'.format(value1))))
     .filter(Parent.children.any(Child.value.ilike('%{}%'.format(value2))))
     .filter(~Parent.children.any(
         db.or_(Child.value.ilike('%{}%'.format(value3)),
                Child.value.ilike('%{}%'.format(value4)),
                )
     ))
)

rawDescription: SQLAlchemy - Filter query, exclude parent where one of many children meet criteria


code:
while x >= 0:

    if x <= 100:
        total = total + x
        count+=1
        nums.append(x)

    if x>100:
        print("Invalid entry.")

    x = int(input("Enter a number (enter -1 to terminate): "))

rawDescription: Python Average Scores

description: Python Average Scores


code:
def clean_email(self):
    email = self.cleaned_data["email"]
    try:
        User.objects.get(email=email)
    except User.DoesNotExist:
        return email
    raise forms.ValidationError('duplicate_email')

rawDescription: django objects.filter exclude yourself

description: Django objects.filter exclude yourself


code:
def translate(dict, text1, trnsl):
    with open("text1.txt") as text1, open("trnsl.txt", "w") as trnsl:
        for line in text1: # parse each line
            words = line.split() # break line into words, do more stuff here if punctuation
            for word in words:
                if word in dict: # check if translatable
                    trnsl.write(dict[word]) # get translation
                else:
                    trnsl.write(word)
                trnsl.write(' ') # re-add spaces between words
            trnsl.write('\n') # re-add endlines between lines

rawDescription: How to read in a text file, create a dictionary, read in another text file and translate it with said dictionary and write it into a new file

description: Read in a text file, create a dictionary, read in another text file and translate it with said dictionary and write it into a new file


code:
def password(pwd):
    if len(pwd) >= 10:
        is_num = False
        is_low = False
        is_up = False
        for ch in pwd:
            if ch.isdigit():
                is_num = True
            if ch.islower():
                is_low = True
            if ch.isupper():
                is_up = True
        return is_num and is_low and is_up
    return False

rawDescription: check strength of password

description: Check strength of password


rawDescription: Python Parse JSON value with space delimited subfields


code:
import tkinter as tk
import tkinter.scrolledtext as tkst
import tkinter.font as tkFont
import tkinter.ttk as ttk

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid()
        self.createWidgets()
    def createWidgets(self):
        self.logArea = tkst.ScrolledText(self,
                    wrap   = tk.WORD,
                    width  = 20,
                    height = 8,
                    #state="disabled",
                    name="logArea"
                )
        self.logArea.grid(padx=10, pady=10,row=1, sticky=tk.W)
        self.logArea.insert('1.0', 'test insertion')

root = tk.Tk()
app = Application(root)
app.mainloop()

rawDescription: ScrolledText not appearing with tkinter grid layout


code:
with open(COUNT_FILE, 'w') as f:
    f.write(str(int(COUNT)+1)+'\n')

rawDescription: Doing simple Bash file write in Python

description: Doing simple Bash file write in Python


code:
import evernote.edam.type.ttypes as Types
import evernote.edam.notestore.ttypes as NoteStore

from evernote.api.client import EvernoteClient

auth_token = "your developer token"

client = EvernoteClient(token=auth_token, sandbox=True)
note_store = client.get_note_store()

note_filter = NoteStore.NoteFilter()
note_filter.words = 'intitle:"test"'
notes_metadata_result_spec = NoteStore.NotesMetadataResultSpec()

notes_metadata_list = note_store.findNotesMetadata(note_filter, 0, 1, notes_metadata_result_spec)
note_guid = notes_metadata_list.notes[0].guid
note = note_store.getNote(note_guid, True, False, False, False)

rawDescription: Read My Own Evernotes

description: Read My Own Evernotes


code:
import inspect

def hello(name='World'):
    f = inspect.currentframe().f_back
    mod = f.f_code.co_filename
    lineno = f.f_lineno
    print('Hi, %s. You called this from %s at line # %d.' %
          (name, mod, lineno))

rawDescription: Python introspection - how to check current module / line of call from within function


rawDescription: How to select rows in a dataframe based on values in three columns?

description: Select rows in a dataframe based on values in three columns


code:
dt = datetime.datetime.strptime(dateVariable, "%b-%y")
if dt > datetime.now():
    dt = dt - datetime.timedelta(years=100)

rawDescription: Python Epoch Fail with 2-digit year

description: Python Epoch Fail with 2-digit year


code: yield StoreOutput("WordCount", filekey, output)

rawDescription: How do you specify the filename for the BlobstoreOutputWriter for a simple mapreduce job?

description: Specify the filename for the BlobstoreOutputWriter for a simple mapreduce job


code:
@click.command()
@click.option('--foo', default=[23, 42], type=click.FLOAT, multiple=True)
def cli(foo):
    for item in foo:
        click.echo(item)

@click.command()
@click.option('--arg', default=((1, 2), (3, 4)), nargs=2, multiple=True, type=click.INT)
def cli(arg):
    for item in arg:
        click.echo('<%d|%d>' % item)

rawDescription: how to set default for multi value options

description: Set default for multi value options


code:
import Tkinter as tk
import re
class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.L1 = tk.Label(self, text="Enter the query")
        self.L1.pack()
        self.entry = tk.Entry(self)
        self.button = tk.Button(self, text="Get", command=self.on_button)
        self.button.pack()
        self.entry.pack()
        self.text = tk.Text(self)
        self.text.pack()
    def on_button(self):
        s1=(self.entry.get())

        #here is the line added
        self.text.delete(1.0, tk.END) #delete the content of self.text

        with open("myxml.txt","rb")as f:
             row = f.readlines()
             for i, r in enumerate(row):
                if s1 in r:
                    for x in range(i-3,i+4):
                        s = row[x]
                        m = re.sub(r'<(\w+)\b[^>]*>([^<]*)</\1>', r'\1-\2', s)
                        self.text.insert(tk.END, re.sub(r'<[^<>]*>','', m))
app = SampleApp()
app.mainloop()

rawDescription: previous output remains even after passing next input

description: Previous output remains even after passing next input


code:
mylist = ["Wednesday_A_20", "Tuesday_C_22", "Thursday_A_30"]
key = lambda s: s.split('_')[1]
from itertools import groupby
[next(g) for k, g in groupby(sorted(mylist, key=key), key=key)]

rawDescription: Sort and set for no repeated section

description: Sort and set for no repeated section


code:
iframes = driver.find_elements_by_xpath("//div[@class='panel-body']//iframe")
len(iframes)  # 5
driver.switch_to.frame(iframes[0])
driver.find_element_by_xpath('//span[@domid="LastPrice"]')  # should work now

rawDescription: Unable to scrape data from a React.js data grid


code:
t = datetime.date.today()
w = today.weekday() + 1  # +1 because weekday() is 0-based, %w is 1-based

rawDescription: Python (if, elif, else) working with timestamp


code:
c = np.r_[:100]
xi = c.reshape((100, 1, 1))
yi = c.reshape((1, 100, 1))
zi = np.empty((100, 100, 25), dtype=int)
for x in xrange(100):
    for y in xrange(100):
        zi[x,y] = np.arange(x*25/100, x*25/100+25) # or whatever other function

newarray = oldarray[xi, yi, zi]

rawDescription: Numpy slicing x,y,z array for variable z


code:
key_params = dedent("""
    <GnupgKeyParms format="internal">
      %%ask-passphrase
      Key-Type: RSA
      Key-Length: 2048
      Name-Real: Jim Joe
      Expire-Date: 0
    </GnupgKeyParms>
""")

rawDescription: How to get gpg-agent to ask for passphrase when generating keys with GPGME?

description: Get gpg-agent to ask for passphrase when generating keys with GPGME


code:
A = np.zeros((10,20))
y, x  = np.ogrid[:A.shape[0], :A.shape[1]]
mask = x > y
A[mask] = 1

rawDescription: Filter numpy array according to predicate of position


code:
import cv2

def detect(path):
    img = cv2.imread(path)
    cascade = cv2.CascadeClassifier("/vagrant/detect/haarcascade_frontalface_alt.xml")
    rects = cascade.detectMultiScale(img, 1.3, 4, cv2.cv.CV_HAAR_SCALE_IMAGE, (20,20))

    if len(rects) == 0:
        return [], img
    rects[:, 2:] += rects[:, :2]
    return rects, img

def box(rects, img):
    for x1, y1, x2, y2 in rects:
        cv2.rectangle(img, (x1, y1), (x2, y2), (127, 255, 0), 2)
    cv2.imwrite('/vagrant/img/detected.jpg', img);

rects, img = detect("/vagrant/img/one.jpg")
box(rects, img)

rawDescription: Detect face, get the boarder of face

description: Detect face, get the boarder of face


code:
import subprocess
subprocess.check_call( ['ls'], cwd='/tmp' )

rawDescription: how to start external system application in python specifying startup directory?

description: Start external system application in python specifying startup directory


code: prec, rec, fbeta, supp = precision_recall_fscore_support(y_test, y_pred, labels=classifier.classes_)

rawDescription: Scikit-learn: precision_recall_fscore_support returns strange results


code: quiver(values_x, values_y, vector_field_x, vector_field_y)

rawDescription: Change axes ticks of quiver - Python

description: Change axes ticks of quiver - Python


code:
import sip
...

sip.delete(self.day_objs_list[temp_day]['frame'])

rawDescription: Can I assign signal to new object in destructor of old one by deleteLater()

description: Assign signal to new object in destructor of old one by deleteLater()