code:
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker

session_factory = sessionmaker(bind=some_engine)
AsyncSession = scoped_session(session_factory)

def store_movie_details(movie_id):

    session = AsyncSession()
    movie_details = scrape_data(movie_id)
    new_movie = Movie(**movie_details) # Movie is my model.

    session.add(new_movie)
    session.commit()

rawDescription: Pyramid: Multi-threaded Database Operation


code:
cursor = db.coll.aggregate(
    [
        {"$group": {"_id": "$ID", "unique_ids": {"$addToSet": "$_id"}, "count": {"$sum": 1}}},
        {"$match": {"count": { "$gte": 2 }}}
    ]
)

response = []
for doc in cursor:
    del doc["unique_ids"][0]
    for id in doc["unique_ids"]:
        response.append(id)

coll.remove({"_id": {"$in": response}})

rawDescription: pymongo: remove duplicates (map reduce?)

description: Pymongo: remove duplicates (map reduce?)


code:
import pandas as pd
df = pd.concat([pd.read_csv('file%d.csv' % x) for x in range(1,41)])
df.to_csv('output.csv')

rawDescription: How to read some csv files in a folder in python

description: Read some csv files in a folder in python


code:
class Connection(object):
 def __init__(self, parent):
  self.parent = parent
class ConnectableObject(object):
 def __init__(self):
  pass
 def connect(self):
  return Connection(self)
conobj = ConnectableObject()
con = conobj.connect()
con.parent

rawDescription: Creating a connection object in Python

description: Creating a connection object in Python


code:
def quiz():
    print('Welcome. This is a 10 question math quiz\n')
    score = 0
    for i in range(10):
        correct = askQuestion()
        if correct:
            score += 1
            print('Correct!\n')
        else:
            print('Incorrect!\n')
    print('Your score was {}/10'.format(score))

rawDescription: i cant seem to get it to print the Score out when i run quiz() it should show score out of 10 but it dose not

description: I cant seem to get it to print the Score out when i run quiz() it should show score out of 10 but it dose not


code: is_checked = "check1" in request.POST

rawDescription: How to read checkbox value on post?

description: Read checkbox value on post


code:
def cn(n):
   c = y*np.exp(-1j*2*n*np.pi*time/period)
   return c.sum()/c.size

def f(x, Nh):
   f = np.array([2*cn(i)*np.exp(1j*2*i*np.pi*x/period) for i in range(1,Nh+1)])
   return f.sum()

y2 = np.array([f(t,50).real for t in time])

plot(time, y)
plot(time, y2)

rawDescription: How to calculate a Fourier series in Numpy?

description: Calculate a Fourier series in Numpy


code:
def edit_resource(res_name, res_id):
    res = False
    with db_pool().manager() as conn:
        cursor = conn.cursor()
        sql = """UPDATE `resource` SET `resource_name` = %s WHERE id = %s;"""
        if cursor.execute(sql, (res_name, res_id)) > 0:
          res = True
        conn.commit()
    return res

rawDescription: How to catch when SQL UPDATE SETs not existing line

description: Catch when SQL UPDATE SETs not existing line


code:
plt1 = pg.PlotWidget(w)
img = pg.QtGui.QGraphicsPixmapItem(pg.QtGui.QPixmap(fileName))
plt1.addItem(img)

# depending on your preference, you probably want to invert the image:
img.scale(1, -1)

# OR invert the entire view:
plt.invertY(True)

rawDescription: How to insert an image from file into a PlotWidget (plt1 = pg.PlotWidget(w);)?

description: Insert an image from file into a PlotWidget (plt1 = pg.PlotWidget(w);)


code:
is_fail = (df.Status != 'OK')
cumulative_fails = is_fail.cumsum()
fail_idx, = is_fail.nonzero()
days_since_last_fail = arange(len(is_fail))
days_since_last_fail[fail_idx[0]:] -= fail_idx[cumulative_fails[fail_idx[0]:]-1]

rawDescription: Calculate difference between values using a filter using Pandas


code:
def defined(dct, keys):
    return zip(*[([k] + [v[m] for m in keys])
                 for k, v in dct.iteritems()
                 if all(m in v for m in keys)])

rawDescription: find all dictionaries that each have an indeterminate number of defined keys

description: Find all dictionaries that each have an indeterminate number of defined keys


code:
def bufferedFetch():
    with open("bigfile.txt","r") as f:
        for line in f:
            yield line

rawDescription: Python Generator memory benefits for large readins?


code: ch = logging.StreamHandler(sys.stdout)

rawDescription: Avoid red background color for logging output in Ipython Notebook


code:
from bs4 import BeautifulSoup
html = """
something

something else

hi there

ok
"""
soup = BeautifulSoup(html, "html.parser")
[tag.name for tag in soup.find_all()]
[str(tag) for tag in soup.find_all()]

rawDescription: Get all HTML tags with Beautiful Soup

description: Get all HTML tags with Beautiful Soup


code:
lines = cv2.HoughLinesP(...)
with open('tmp.csv', 'w') as f:
    for l in lines:
        f.write(','.join(str(x) for x in l) + "\n")

rawDescription: Accessing elements of [[[i j k l]]]

description: Accessing elements of [[[i j k l]]]


code:
import numpy as np
a = np.arange(10)
np.roll(a,2)
np.roll(a,-2)

rawDescription: how to do circular shift in numpy

description: Do circular shift in numpy


code:
import pandas as pd
from sklearn.preprocessing import LabelEncoder

df = pd.DataFrame({
'colors':  ["R" ,"G", "B" ,"B" ,"G" ,"R" ,"B" ,"G" ,"G" ,"R" ,"G" ],
'skills':  ["Java" , "C++", "SQL", "Java", "Python", "Python", "SQL","C++", "Java", "SQL", "Java"]
})

def encode_df(dataframe):
    le = LabelEncoder()
    for column in dataframe.columns:
        dataframe[column] = le.fit_transform(dataframe[column])
    return dataframe

#encode the dataframe
encode_df(df)

rawDescription: Encoding Column variable with string values by integer values in Python or Sklearn


code:
def listfiles(directory):
    return [f for f in os.listdir(directory)
              if os.path.isdir(os.path.join(directory, f))]

rawDescription: Is there a way to get all the directories but not files in a directory in Python?


code:
original_grayscale = numpy.asarray(some_PIL_image.convert('L'), dtype=float)
blurred_grayscale = scipy.ndimage.filters.gaussian_filter(original_grayscale, blur_parameter)
difference_image = original_grayscale - (multiplier * blurred_grayscale);
image_to_be_labeled = ((difference_image > threshold) * 255).astype('uint8')  # not sure if it is necessary

labelarray, particle_count = scipy.ndimage.measurements.label(image_to_be_labeled)

rawDescription: Counting particles using image processing in python


rawDescription: Find element in list

description: Find element in list


rawDescription: Configure Django URLS.py to keep #anchors in URL after it rewrites it with a end /


code: a = wd.execute_script("return checkdata('" + code + "');")

rawDescription: How to execute a Javascript function in python with selenium

description: Execute a Javascript function in python with selenium


code:
import win32gui, win32con

hwnd = get_my_window_handle()
win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE)

rawDescription: GUI's Over Running Programs in python

description: GUI's Over Running Programs in python


code:
import kivy
kivy.require('1.9.0')

from kivy.app import App
from kivy.uix.video import Video

class MyApp(App):
    def build(self):
        video = Video(source='mymovie.mp4')
        video.state='play'
        video.options = {'eos': 'loop'}
        video.allow_stretch=True
        return video

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

rawDescription: Kivy VideoPlayer fullscreen, loop, and hide controls


rawDescription: Python - How to show addition of integers digits


code:
def avgdistance(item1, item2, lst):
    distlist = []
    curr = item1 if (lst.index(item1) < lst.index(item2)) else item2
    index = lst.index(curr)
    for i in range(len(lst)):
        item = lst[i]
        if (item == item1) or (item == item2):
            if item == curr:
                index = i
            else:
                distlist.append(i - index - 1)
                curr = item
                index = i
    if (len(distlist) > 0):
        return sum(distlist)/len(distlist)
    else:
        return 0

rawDescription: Python: Average distance between string elements in a list


code:
import csv
namelist = ['name1', 'name2']
fout = open('out.csv','w')
fout.write('header1,header2,header3')
fin = open('temp.csv','r')
reader = csv.DictReader(fin)
for row in reader:
  if row['header1'] in namelist:
      fout.write(newrow(row))

rawDescription: Python - have list and want to pull values from a table and put in new table


code:
from selenium import webdriver

browser = webdriver.Firefox()
browser.get('https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa')

# Add this line
browser.switch_to.frame('authFrame')

login_form = browser.find_element_by_id('appleId')
...

rawDescription: Using selenium on Itunes web site

description: Using selenium on Itunes web site


code:
from Spotfire.Dxp.Application.Visuals import VisualContent
contentTable = myTableVisualization.As[VisualContent]()
if contentTable.TableColumns.Contains(myData.Columns["MyColumn"]):
        contentTable.TableColumns.Remove(myData.Columns["MyColumn"])

rawDescription: Remove/ delete or hide column in spotfire


rawDescription: Converting pandas.DataFrame to bytes

description: Converting pandas.DataFrame to bytes


code:
bad_symbols = '(){}\n'
s = 'hello () { world \n }'
s = ''.join([x for x in s if x not in bad_symbols])

rawDescription: Is there a one-liner for removing whitespace and specific characters?


code:
string = "abcdefghijklmnopqrstuvwxyz"
charsUntilLetter = string.index('d')  # 3 characters before 'd'

rawDescription: count characters to characters per line Python

description: Count characters to characters per line Python


code:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
# set up orthographic map projection with
# perspective of satellite looking down at 50N, 100W.
# use low resolution coastlines.
# don't plot features that are smaller than 1000 square km.
map = Basemap(projection='ortho', lat_0 = 50, lon_0 = -100,
              resolution = 'l', area_thresh = 1000.)
# plot surface
map.warpimage(image='venuscyl4.jpg')
# draw the edge of the map projection region (the projection limb)
map.drawmapboundary()
# draw lat/lon grid lines every 30 degrees.
map.drawmeridians(np.arange(0, 360, 30))
map.drawparallels(np.arange(-90, 90, 30))
plt.show()

rawDescription: Plotting cylindrical map data over a 3D sphere in Python


code:
from Tkinter import * #Tkinter; If you are using 3.x, change to tkinter, emphasis on all lowercase
from ttk import Frame, Style #You need this for the frame and style;
import random
class Questions(Frame):

    def __init__(self, parent):
            global root
            global question
            global corinc
            global number
            global a
            global b
            global c
            global d
            global qA
            global curbutton
            global ques#Current Question to be asked
            global inc#Incorrect tally
            global cors#Correct tally
            global qnumber#Number of current question
            global qnum

            question=StringVar()
            question.set('Question 1')#Current Question Label Textvariable

            a=StringVar()
            b=StringVar()
            c=StringVar()
            d=StringVar()

            corinc=StringVar()
            corinc.set('')#Displays Correct or Incorrect, At first displays nothing
            number=0#Number of Question - 1
            #number=random.randint(0, len(curbutton)-1 #UNCOMMENT THIS to make selection random

            qA={'What Is 5068.25 x 4?' : 'a', 'Which answer has an average of 5?' : 'c'}
            ##Put questions in format 'question' : 'properanswerbuttontoclick'; see above example

            curbutton={1: 'a:20 273b:19 732c:21 327d:20 372', 2: 'a:1,2,3,4,5b:1,2,3,4,5,6c:3,4,5,6,7d:1,0,2,9,3'}
            ###^^^Put options in format questionnumber : 'a:firstoptionb:secondoptionc:thirdoptiond:fourthoption'!
            ###Otherwise, this will not work properly!

            ques=StringVar()
            inc=0
            cors = 0
            qnumber=1
            qnum=StringVar()

            self.parent = parent
            Frame.__init__(self, parent)
            #Binding Goes here; eg parent.bind('<Return>, fun.ction)
            self.initUI()

    def new_question(self):
        ques.set(qA.keys()[number])#qA.keys()[number] gets the key at the location number in the Dictionary qA
        a.set(str(list(curbutton[number+1].split('a:')[1].split('b:'))[0]))#These Grab the values for the key number+1, and display the PROPERLY FORMATTED results
        b.set(str(list(curbutton[number+1].split('b:')[1].split('c:'))[0]))#on the buttons
        c.set(str(list(curbutton[number+1].split('c:')[1].split('d:'))[0]))
        d.set(str(list(curbutton[number+1].split('d:'))[1]))
        qnum.set(str(number)+'/' + str(len(curbutton)))#Displays questionnumber/totalquestions
        self.update_idletasks

    def initUI(self):
        global corinc
        global a1
        global a2
        global a3
        global a4

        self.parent.title("Quiz")
        self.style = Style()
        self.style.theme_use("clam")
        self.new_question()#Sets the button and answer values
        q=Label(textvariable=question)#Question Number
        q.pack()
        aq=Label(textvariable=ques)#Question being asked
        ci=Label(textvariable=corinc)#Displays Correct or Incorrect
        ci.pack()
        aq.pack()

        a1= Button(textvariable=a, width=10, bg='orange',
                   cursor="circle", command= lambda: self.check('a'))#lambda allows you to pass a function with arguments, e.g. self.check('a') instead of self.check
        a1.pack()#                                                   #It must be there if you are passing arguments
        a2= Button(textvariable=b, width=10, bg='orange',
                   cursor="circle", command= lambda: self.check('b'))
        a2.pack()
        a3= Button(textvariable=c, width=10, bg='orange',
                   cursor="circle", command= lambda: self.check('c'))
        a3.pack()
        a4= Button(textvariable=d, width=10, bg='orange',
                   cursor="circle", command= lambda: self.check('d'))
        a4.pack()
        qnumb=Label(textvariable=qnum)#displays qnum, see line 46
        qnumb.pack()

    def correct(self):
        global cors
        global number
        cors = cors+1#Adds to correct tally
        number=number+1#changes question number
        #number=random.randomint(number, len(curbutton))
        corinc.set('Correct')#displays correct
        self.update_idletasks#UPDATES ALL WIDGETS
        question.set('Question ' + str(number+1))#Sets question number e.g. Question 1
        global qnumber
        qnumber=qnumber+1
        if qnumber == len(curbutton):
            qnumber=len(curbutton)
        qnum.set(str(number)+'/' + str(len(curbutton)))#See line 62
        self.update_idletasks#Updates
        try:
            self.new_question()#Calls New question function
        except IndexError:#If it raises the error IndexError, it means there are no more q/a's listed and it will direct to
            self.quiz_complete()#Completion 'Page'

    def quiz_complete(self):
        question.set('Quiz Complete')
        corinc.set('Correct: ' + str(cors) + '\nIncorrect: ' + str(inc))#Take a guess
        ques.set('')
        a.set('Quit')
        a1.configure(command=self.quitq)
        b.set('Take Quiz Again')
        a2.configure(command=self.restart)
        c.set('')
        d.set('')
        self.update_idletasks

    def restart(self):
        self.update_idletasks
        global number
        corinc.set('')
        question.set('Question 1')
        a1.configure(command= lambda : self.check('a'))
        a2.configure(command= lambda : self.check('b'))
        global cors
        cors=0
        global inc
        inc=0
        number=0
        #number=random.randint(0, len(curbutton)-1)
        self.new_question()
    def incorrect(self):
        global inc
        inc=inc+1#adds to incorrect tally
        corinc.set('Incorrect')
        self.update_idletasks
    def check(self, awn):
        if awn==qA.values()[number]: self.correct()#This checks if the value at location number matches the correct answer given on the qA dict
        else: self.incorrect()#^^
    def quitq(self):
        quit()

def main():
    global root
    root = Tk()
    #root.iconbitmap(default='Drive:/Pythonpath/icon.ico')#Wont work for me but you can try it
    root.geometry("300x200+100+100")#In format widthxheight+distancefromtop+distancefromleftside
    app = Questions(root)
    root.mainloop()

main()

rawDescription: How to update widget?

description: Update widget


code:
from django.db.models import Count
comments = Comments.objects.annotate(num_answers=Count('answers'))

rawDescription: Django. How to annotate a object count from a related model


code:
import requests
from bs4 import BeautifulSoup
r = requests.get('http://feeds.chicagotribune.com/~r/ChicagoBreakingNews/~3/T2Zg3dk4L88/story01.htm')
soup = BeautifulSoup(r.text, 'lxml')
soup.find('div', id='story-body') is not None
soup = BeautifulSoup(r.text, 'html5')
soup.find('div', id='story-body') is not None

rawDescription: Beautifulsoup lost nodes

description: Beautifulsoup lost nodes


code:
image = Image.open('test.png')
tf = tempfile.TemporaryFile(mode='r+b')
name = tf.name
del tf
image.save(name, 'png')
dji=DjInfo()
dji.picture.save('test.png', name)

rawDescription: Writing an image from PIL's 'Image' class into a model instance's ImageFile field


code:
# To fit your estimator
gnb.fit(data_numpy[0], data_numpy[1])
# To serialize
import pickle
with open('our_estimator.pkl', 'wb') as fid:
    pickle.dump(gnb, fid)
# To deserialize estimator later
with open('our_estimator.pkl', 'rb') as fid:
    gnb = pickle.load(fid)

rawDescription: Saving a cross-validation trained model in Scikit


code:
def make_random():
    return generate_random_unique_code_for_model(['myapp_name', 'mymodel_name'], 'myfield_name')

class mymodel_name(models.Model):
    #other fields
    myfield_name = models.CharField(default=make_random)

rawDescription: Django - passing extra arguments into default callable function


code:
for n in numbers:
    frequencies[n] += 1

rawDescription: How do you get number frequencies in a list, then index into another list?

description: Get number frequencies in a list, then index into another list


code:
students = []
n = int(input('How many student do you have to enter:'))
for x in range(n):
    name = input('Enter the student name:')
    dob = input('Enter the student dob:')
    students.append({'name': name, 'dob': dob})
print('\n'.join(['name: {0}\ndob: {1}'.format(x.get('name'), x.get('dob')) for x in students]))

rawDescription: python using the loop and input user name

description: Python using the loop and input user name


code:
#get indices of rows to update
rowsToUpdate = np.nonzero(a[:,0]<0)[0]
#increment first element of target rows by 1000
a[rowsToUpdate,0] += 1000

rawDescription: Masked Array Calculation on np.array

description: Masked Array Calculation on np.array


code:
s = "A=3,B=2,C=1,D=5"
sorted(s.split(','), key = lambda m: int(m.split('=')[1]))
','.join(sorted(s.split(','), key = lambda m: int(m.split('=')[1])))

rawDescription: Python: Split Integers and Strings in Lists


code:
#!/usr/bin/env python

import cv

class Target:

    def __init__(self):
        self.capture = cv.CaptureFromCAM(0)
        cv.NamedWindow("Target", 1)

    def run(self):
        # Capture first frame to get size
        frame = cv.QueryFrame(self.capture)
        frame_size = cv.GetSize(frame)
        color_image = cv.CreateImage(cv.GetSize(frame), 8, 3)
        grey_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1)
        moving_average = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_32F, 3)

        first = True

        while True:
            closest_to_left = cv.GetSize(frame)[0]
            closest_to_right = cv.GetSize(frame)[1]

            color_image = cv.QueryFrame(self.capture)

            # Smooth to get rid of false positives
            cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3, 0)

            if first:
                difference = cv.CloneImage(color_image)
                temp = cv.CloneImage(color_image)
                cv.ConvertScale(color_image, moving_average, 1.0, 0.0)
                first = False
            else:
                cv.RunningAvg(color_image, moving_average, 0.020, None)

            # Convert the scale of the moving average.
            cv.ConvertScale(moving_average, temp, 1.0, 0.0)

            # Minus the current frame from the moving average.
            cv.AbsDiff(color_image, temp, difference)

            # Convert the image to grayscale.
            cv.CvtColor(difference, grey_image, cv.CV_RGB2GRAY)

            # Convert the image to black and white.
            cv.Threshold(grey_image, grey_image, 70, 255, cv.CV_THRESH_BINARY)

            # Dilate and erode to get people blobs
            cv.Dilate(grey_image, grey_image, None, 18)
            cv.Erode(grey_image, grey_image, None, 10)

            storage = cv.CreateMemStorage(0)
            contour = cv.FindContours(grey_image, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)
            points = []

            while contour:
                bound_rect = cv.BoundingRect(list(contour))
                contour = contour.h_next()

                pt1 = (bound_rect[0], bound_rect[1])
                pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
                points.append(pt1)
                points.append(pt2)
                cv.Rectangle(color_image, pt1, pt2, cv.CV_RGB(255,0,0), 1)

            if len(points):
                center_point = reduce(lambda a, b: ((a[0] + b[0]) / 2, (a[1] + b[1]) / 2), points)
                cv.Circle(color_image, center_point, 40, cv.CV_RGB(255, 255, 255), 1)
                cv.Circle(color_image, center_point, 30, cv.CV_RGB(255, 100, 0), 1)
                cv.Circle(color_image, center_point, 20, cv.CV_RGB(255, 255, 255), 1)
                cv.Circle(color_image, center_point, 10, cv.CV_RGB(255, 100, 0), 1)

            cv.ShowImage("Target", color_image)

            # Listen for ESC key
            c = cv.WaitKey(7) % 0x100
            if c == 27:
                break

if __name__=="__main__":
    t = Target()
    t.run()

rawDescription: How do I track motion using OpenCV in Python?

description: Track motion using OpenCV in Python


rawDescription: Sort list of date strings

description: Sort list of date strings


code:
for k in sorted(d, key=d.get, reverse=True):
    print("{:>10}{:>20}".format(k, d[k]))

rawDescription: Printing a dictionary using .format in python


code:
from tkinter import *

class MyButton(Button):

    def __init__(self, *args, **kwargs):
        Button.__init__(self, *args, **kwargs)
        self['bg'] = 'red'

root = Tk()
root.geometry('200x200')

my_button = MyButton(root, text='red button')
my_button.pack()

root.mainloop()

rawDescription: Python tkinter making 'classes' for buttons and labels


code:
client = mqtt.Client()
client.connect(address)
#runs a thread in background to call loop function internally.
#In addition, this also reconnects to broker on a lost connection.
client.loop_start()

while True:
    data = getdata()
    client.publish("$ahmed",data)
client.loop_stop()

rawDescription: The appropriate value of max_packets for MQTT loop function


code:
from lxml import html
import requests
import csv
page = requests.get('http://chem.sis.nlm.nih.gov/chemidplus/rn/75-07-0')
tree = html.fromstring(page.text)

if ("Name of Substance" in page.text):
    chem_name = tree.xpath('//*[text()="Name of Substance"]/..//div')[0].text_content()
else:
    chem_name = ""

if ("MeSH Heading" in page.text):
    mesh_name = tree.xpath('//*[text()="MeSH Heading"]/..//div')[1].text_content()
else:
    mesh_name = ""

names1 = [chem_name, mesh_name]
with open('testchem.csv', 'wb') as myfile:
    wr = csv.writer(myfile)
    wr.writerow(names1)

rawDescription: How to select nodes in html from lxml?

description: Select nodes in html from lxml


code:
import itertools

limits = [(3,10), (4,11), (2,6), (8,11), (9,11)]
ranges = [range(*lim) for lim in  limits]

results = []
for comb in itertools.combinations(ranges,3):
    intersection = set(comb[0]).intersection(comb[1])
    intersection = intersection.intersection(comb[2])
    if intersection and intersection not in results and\
       not any(map(intersection.issubset, results)):
        results = filter(lambda res: not intersection.issuperset(res),results)
        results.append(intersection)

result_limits =  [(res[0], res[-1]+1) for res in map(list,results)]

rawDescription: Finding range overlap between at least three ranges Python


code:
import requests
url = 'http://tinyurl.com/m7ppct4'
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36'
headers = {'User-Agent': user_agent}
r = requests.get(url, headers=headers)

rawDescription: Python request.get(url) returns empty content


code: ast.literal_eval("""(u'bretagne tr\xe9minou 23archiefdingen', u'chicago, il')""")

rawDescription: How to parse "stringified" python objects from file back into python objects?

description: Parse "stringified" python objects from file back into python objects


code: filtered_list = [tup for tup in list if len(tup) == 2]

rawDescription: remove tuples with only one item -- python

description: Remove tuples with only one item -- python


code:
s = "it's e.t.!"
import re
re.sub(r"\b(?<!')[a-z]", lambda m: m.group().upper(), s)

rawDescription: Lowercase letter after certain character?


code:
def with_named_setup(setup=None, teardown=None):
    def wrap(f):
        return with_setup(
            lambda: setup(f.__name__) if (setup is not None) else None,
            lambda: teardown(f.__name__) if (teardown is not None) else None)(f)
    return wrap

@with_named_setup(setup_func, teardown_func)
def test_one():
    pass

@with_named_setup(setup_func, teardown_func)
def test_two():
    pass

rawDescription: Get name of current test in setup using nose


code:
r = requests.get(..., stream=True, timeout=initial_timeout)
r.raise_for_status()

if int(r.headers.get('Content-Length')) > your_maximum:
    raise ValueError('response too large')

size = 0
start = time.time()

for chunk in r.iter_content(1024):
    if time.time() - start > recieve_timeout:
        raise ValueError('timeout reached')

    size += len(chunk)
    if size > your_maximum:
        raise ValueError('response too large')

    # do something with chunk

rawDescription: Python requests, how to limit received size, transfer rate, and/or total time?


code:
from Crypto.PublicKey import RSA
from Crypto import Random
random_generator = Random.new().read
private_key = RSA.generate(1024, random_generator)
public_key = private_key.publickey()
enc_data = public_key.encrypt('abcdefgh', 32)
private_key.decrypt(enc_data)

rawDescription: Python - How do I implement readers/writers in a Bulletin Board Pattern with encrypted posts


code:
from django.core.exceptions import ImproperlyConfigured
from django.db.models import get_app, get_models

APPS_MODELS = {}

def get_model(app_name, model_name):

    app_models = APPS_MODELS.get(app_name)
    if app_models:
        return app_models.get(model_name)

    try:
        app = get_app(app_name)
    except ImproperlyConfigured: # no such app
        return None

    APPS_MODELS[app_name] = dict((model._meta.model_name, model)
                                     for model in get_models(app))

    return APPS_MODELS[app_name].get(model_name)

rawDescription: How to unambiguously identify a Model as a lowercase string in Django

description: Unambiguously identify a Model as a lowercase string in Django


code:
for x in range(0,10):
    for y in range(0,10):
        key = "pass:" + str(x) + str(y)
        try:
            plaintext = subprocess.check_output(['openssl', 'aes-128-cbc', '-d', '-in', 'ciphertext', '-base64', '-pass', key])
        except subprocess.CalledProcessError as e:
            print("{key} failed".format(key=key))
        else:
            print (plaintext)

rawDescription: Handling spawned process results

description: Handling spawned process results


code: my_dict = dict(zip(names,zip(*data)))

rawDescription: Merging tuples in a tuple with list and converting to dict


code:
with open('output.csv', 'wb') as output:
    input = csv.reader(open('faultyfile.csv', 'rb'))
    output = csv.writer(output, dialect=input.dialect)
    for line in input:
        if len(line) < 5:
            line.extend([''] * (5 - len(line)))
        output.writerow(line)

rawDescription: create standard compliant file

description: Create standard compliant file


rawDescription: delete one level in a python dict keeping the values

description: Delete one level in a python dict keeping the values


code: tmpvalue[mask] = value

rawDescription: Uniform way of using array indexing for 0D & 1D numpy arrays


code:
for elt in itertools.product(*ListOfLists):
   ...

rawDescription: Best way to iterate through unknown number of lists in general?

description: Iterate through unknown number of lists in general


code:
from datetime import date
from dateutil.rrule import rrule, DAILY

iterdates=iter(rrule(DAILY, dtstart=date(2012, 9, 1), until=date(2012, 9, 30)))

rawDescription: iterator for dates in python

description: Iterator for dates in python


code:
img = Image.open(path).convert("RGB")
pixels = img.load()

rawDescription: Reading PNG with PIL in Python

description: Reading PNG with PIL in Python


code:
def find_things(tags=None, user=None, published_since=None):
    # all queries begin with something common, which may
    # be an empty dict, but here's an example
    query = {
        'is_published': True
    }
    if tags:
        # assume that it is an array of strings
        query['tags'] = {'$in': tags}
    if user:
        # assume that it is a string
        query['user'] = user
    if published_since:
        # assume that it is a datetime.datetime
        query['date_published'] = {'$gte': published_since}
    # etc...

    return db.collection.find(query)

rawDescription: 'Stringing together' a pymongo query based on a set of conditions


rawDescription: Making the CMD window to appear while executing an .EXE created by py2exe


code:
import csv
myarr = [[10.3,11.2,10.7],[13.4,12.6,12.7],[12.56,14.21,11.33]]
with open("myarray.csv", "wb") as f:
    writer = csv.writer(f)
    writer.writerows(myarr)

rawDescription: saving arrays in an array to csv file using python

description: Saving arrays in an array to csv file using python


code:
def nametrigger(phenny, input):
    names = re.split(' ', input)
    names = [n.split('!')[0] for n in names]
    names = [n.replace('~','') for n in names]
    namesdb = shelve.open(phenny.logdir+'/nicks')
    namesdb[input.args[2]] = names
    namesdb.close()
nametrigger.event = '353'
nametrigger.rule = '(.*)'
nametrigger.priority = 'high'

rawDescription: Grabbing list of nicks in IRC channel with Phenny bot


code:
import numpy as np
from scipy import signal

d0 = np.random.random_integers(10, 12, (5, 5))
d1 = np.random.random_integers(1, 3, (5, 5))
d2 = np.random.random_integers(4, 6, (5, 5))
d3 = np.random.random_integers(7, 9, (5, 5))
d4 = np.random.random_integers(1, 3, (5, 5))
d5 = np.random.random_integers(13, 15, (5, 5))

data = np.array([d0, d1, d2, d3, d4, d5])

data = data.reshape(6, 25)

data = data.T

minimas = []
for x in data:
    minima = (signal.argrelmin(x, axis=0))
    minimas.append(x[minima])
tup = list(a.tolist() for a in minimas)
ab = (np.array(tup).reshape(25, 2))
print(ab[0]) #[3 1]

rawDescription: store results as tuples of large array

description: Store results as tuples of large array


code:
import socket

host = ''
port = 8090
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(backlog)
while 1:
    client, address = s.accept()
    data = client.recv(size)

    if data:
        client.send(bytes('HTTP/1.0 200 OK\r\n', 'UTF-8'))
        client.send(bytes("Content-Type: text/html\r\n\r\n", 'UTF-8'))
        client.send(bytes('
Hello World', 'UTF-8'))
    client.close()

rawDescription: python 3 http socket not working

description: Python 3 http socket not working


code:
from graphics import GraphWin

winmap2 = GraphWin("Details", 200, 200)
winmap2.setBackground("Black")

x = 200
y = 100
winmap2.master.geometry('%dx%d+%d+%d' % (200, 200, x, y))
winmap2.mainloop()

rawDescription: Python Graphics library: How to set the x and y coordinates of the window?


code:
import csv
import os
import glob

rows = list()
path_to_files = r'C:/csv-directory'
file_list = ['file1.csv', 'file2.csv']

# Step 1 & 2 - read all files into one big collection
#              and ignore any blank lines

# If you have a large list of files:
# for filename in glob.iglob('{}/*.csv'.format(path_to_files)):
#   with open(filename) as f:

for filename in file_list:
   with open(os.path.join(path_to_files, filename)) as f:
      reader = csv.reader(f, delimiter=',')
      rows += list(reader)

# Step 3 & 4 - Perform some operation on the
#              big CSV file, and provide output
#              as CSV file
with open(os.path.join(path_to_files, 'result.csv'), 'w') as f:
   writer = csv.writer(f, delimiter=',')
   for row in rows:
       # do something with row
       writer.writerow(row)

rawDescription: Binding together 4 Python scripts

description: Binding together 4 Python scripts


code:
import xlsxwriter

workbook = xlsxwriter.Workbook('example.xlsx')
worksheet = workbook.add_worksheet()

cell_format = workbook.add_format({'align': 'center'})

worksheet.merge_range('A1:B1', "")
worksheet.write_rich_string('A1','Example', cell_format)

workbook.close()

rawDescription: Horizontal text alignment in openpyxl

description: Horizontal text alignment in openpyxl


code:
import os
import sqlite3 as sqlite

def make_movie_table(cursor):
    cursor.execute('CREATE TABLE movies(id INTEGER PRIMARY KEY, title VARCHAR(20), link VARCHAR(20))')

def make_series_table(cursor):
     cursor.execute('CREATE TABLE series(title VARCHAR(30) PRIMARY KEY,series_link VARCHAR(60),number_of_episodes INTEGER,number_of_seasons INTEGER)')

def make_episode_table(cursor):
    cursor.execute('CREATE TABLE episodes(id INTEGER PRIMARY KEY,title VARCHAR(30),episode_name VARCHAR(15), episode_link VARCHAR(40), Date TIMESTAMP, FOREIGN KEY (title) REFERENCES series(title) ON DELETE CASCADE)')

def make_version_table(cursor):
    cursor.execute('CREATE TABLE schema_versions(version VARCHAR(6))')
    cursor.execute('insert into schema_versions(version) values ("0.1.0")')

def create_database(sqlite_file):
    if not os.path.exists(sqlite_file):
        connection = sqlite.connect(sqlite_file)
        cursor = connection.cursor()
        cursor.execute("PRAGMA foreign_keys = ON")
        make_movie_table(cursor)
        make_series_table(cursor)
        make_episode_table(cursor)
        make_version_table(cursor)
        connection.commit()
        connection.close()

def upgrade_database(sqlite_file):
    if os.path.exists(sqlite_file):
        connection = sqlite.connect(sqlite_file)
        cursor = connection.cursor()

        cursor.execute("select max(version) from schema_versions")
        row = cursor.fetchone()
        database_version = row[0]
        print('current version is %s' % database_version)

        if database_version == '0.1.0':
            print('upgrading version to 0.1.1')
            cursor.execute('alter table series ADD COLUMN new_column1 VARCHAR(10)')
            cursor.execute('alter table series ADD COLUMN new_column2 INTEGER')
            cursor.execute('insert into schema_versions(version) values ("0.1.1")')

        #if database_version == '0.1.1':
            #print('upgrading version to 0.1.2')
            #etc cetera

        connection.commit()
        connection.close()

#I need to add 2 columns to the series table, when the user upgrade the software.

if __name__ == '__main__':
    create_database('/tmp/db.sqlite')
    upgrade_database('/tmp/db.sqlite')

rawDescription: Database change with software update

description: Database change with software update


code:
import json
from django.http import HttpResponse

...

class MyResource(ModelResource):

    ...

    def get_list(self, request, **kwargs):
        resp = super(MyResource, self).get_list(request, **kwargs)

        data = json.loads(resp.content)

        data['myfields'] = MyDataFields

        data = json.dumps(data)

        return HttpResponse(data, content_type='application/json', status=200)

rawDescription: How to add extra object to tasty pie return json in python django

description: Add extra object to tasty pie return json in python django


code:
class CommentListInline(admin.TabularInline):
    model = Comment
    fields = ('comment',)
    readonly_fields = fields
    extra = 0
    can_delete = False

    def has_add_permission(self, request):
        return False

class CommentAddInline(admin.TabularInline):
    model = Comment
    fields = ('comment',)
    extra = 1
    can_delete = False

    def has_change_permission(self, request, obj=None):
        return False

class PageAdmin(admin.ModelAdmin):
    inlines = [CommentListInline, CommentAddInline]

rawDescription: Django Admin - Make previous inline forms uneditable


code:
dict(sorted(x,key=lambda k:isinstance(k,int),reverse=True) for x in z)
Out[33]: {12: u'11:22:33:44:54:ce', 33: u'11:22:33:44:55:ff'}

rawDescription: How to get dictionary objects from a list of tuple objects?

description: Get dictionary objects from a list of tuple objects


code:
from datetime import datetime, timedelta

class MyObject:
    startTime = models.DateTimeField(default=datetime.utcnow)
    expiredTS = models.DateTimeField(
        default=lambda: datetime.utcnow() + timedelta(days=1))

rawDescription: How to default a Django DateTimeField to utcnow() + 1 day?

description: Default a Django DateTimeField to utcnow() + 1 day


code:
class MySpider(CrawlSpider):
    name = 'livejournal.com'
    allowed_domains = ['livejournal.com']
    start_urls = ['http://www.papa-gen.livejournal.com']

    rules = (
        Rule(SgmlLinkExtractor(allow=('skip=', )), callback='parse_item'),
    )

    def parse_item(self, response):
        # here is where the parsing happens
        pass

rawDescription: Access the previous and next arrows on a web page


code:
newdata = ['BSD', 'Yes']
...
writer.writerow(row + newdata)

rawDescription: Python: read output script twice and write two columns to csv


code:
mr = CategorizedPlaintextCorpusReader(path_to_your_reviews, r'(?!\.).*\.txt',
        cat_pattern=r'(neg|pos)/.*')

rawDescription: "Cloning" a corpus in NLTK?

description: "Cloning" a corpus in NLTK


code:
[services]
schedule.json = project.module.MySchedule

rawDescription: Custom JSON Response from Scrapy Spider Deployed via Scrapyd


code:
class MyModelForm(ModelForm):
    class Meta:
        model = MyModel
        widgets = {
            'description': Textarea(attrs={'rows': 4, 'cols': 40}),
        }

rawDescription: How to override the model field in django form

description: Override the model field in django form


code:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox() # Get local session of firefox
browser.get(yoursite) # Load page
elem = browser.find_element_by_name("email") # Find the query box
elem.send_keys(email)

ps = browser.find_element_by_name("password") # Find the query box
ps.send_keys(passwd + Keys.RETURN)

rawDescription: login a webpage using python and display the result in the browser

description: Login a webpage using python and display the result in the browser


code:
seq = '012345'
swapped_pairs = zip(seq[1::2], seq[::2])
''.join(item for pair in swapped_pairs for item in pair)

rawDescription: Comprehension for flattening a sequence of sequences?


code: filter(lambda sublist:not any(set(sublist).intersection(x) for x in list2),list1)

rawDescription: comparing contents of 2 lists of lists

description: Comparing contents of 2 lists of lists


code:
from evdev import InputDevice, categorize, ecodes
from select import select
dev = InputDevice('/dev/input/event1')
print(dev)
while True:
   r,w,x = select([dev], [], [])
   for event in dev.read():
       if event.type == ecodes.EV_KEY:
           print(categorize(event))
# hitting a and holding space

rawDescription: How can I capture mouseevents and keyevents using python in background on linux

description: Capture mouseevents and keyevents using python in background on linux


code:
cursor.execute('SELECT * FROM ...')
cols = [i for i, col in enumerate(cursor.description) if col[0].startswith('word')]
for row in cursor:
    columns = [row[c] for c in cols]

rawDescription: What's the most efficient way to get this information from the database?


code:
import pexpect
c=pexpect.spawn('your_command')
c.expect("expected_output_regular_expression", timeout=10)

rawDescription: Tools for implementing a watchdog timer in python


code:
def main(size):
    while True:
        depth = input("Enter depth that is an integer: ")
        try:
            depth = int(depth)
        except ValueError:
            print('Not an integer')
        else:
            break

    turtle.left(120)
    turtle.speed(100)
    triangle(depth, size)

rawDescription: recursion leads to never ending error when it does not pass if statement

description: Recursion leads to never ending error when it does not pass if statement


code:
class Foo(object):
    @classmethod
    def a(cls):
        Foo.b() # gives: this is foo
        Bar.b() # gives: this is bar
        cls.b() # gives: this is bar
    @staticmethod
    def b():
        print("this is foo")

class Bar(Foo):
    @staticmethod
    def b():
        print("this is bar")

Bar.a()

rawDescription: How to refer overriden class functions in python

description: Refer overriden class functions in python


code:
def get_success_url(self):
    if self.request.POST.get('save'):
        return reverse('success_url_for_save')
    elif self.request.POST.get('save_and_continue'):
        return reverse('success_url_for_save_and_continue', kwargs={'pk':self.object.pk})
    else:
        return reverse('fallback_success_url')

rawDescription: Django multiple success urls

description: Django multiple success urls


code:
'We are at a boat sale near a dock.'.replace(" a ", " ")
# Result: We are at boat sale near dock. #

rawDescription: regular expression remove words within string python

description: Regular expression remove words within string python


code: self.crackButton.bind('<ButtonRelease-1>', lambda e: self.crack(Filenamelist, forceclick))

rawDescription: Python/Tkinter force wait on button click


code:
import os, re
values = ['alpha','123']

BASE_PATH = '/Users/name/Desktop/'
OUTPUT_FILE_NAME = 'output.txt'

query_file = os.path.join(BASE_PATH, 'query.txt') # 'a\nb\nc\nd\n4\n5\n6\n'

def func(query_file, reading_files):
    with open(query_file,'r') as f:
        query = f.read().split('\n')

    # TODO: check query

    for fbase in reading_files:
        in_file_name = os.path.join(BASE_PATH, '{}.txt'.format(fbase))
        out_dir = os.path.join(BASE_PATH, fbase)
        out_file_name = os.path.join(out_dir, OUTPUT_FILE_NAME)

        # TODO: check if out_dir exists but is regular file
        if not os.path.isdir(out_dir):
            os.mkdir(out_dir)

        with open(in_file_name, 'r') as in_file, open(out_file_name, 'w') as out_file:
            reading = in_file.read()

            for line in query:
                m = re.search(line, reading)
                if m is not None:
                    out_file.write("{}\n".format(str(m.start())))

print(func(query_file, values))

rawDescription: Create folders in directory from values in list. Python


rawDescription: Issue with BASIC HTTP authentication using Selenium in Python


code:
def main():
    number_of_values = int(input('Please enter number of values: '))  # int

    myList = create_list(number_of_values)  # myList = function result
    total = get_total(myList)

    print('the list is: ', myList)
    print('the total is ', total)

def get_total(value_list):
    total = 0
    for num in value_list:
        total += num
    return total

def create_list(number_of_values):
    myList = []
    for _ in range(number_of_values):  # no need to use num in loop here
        num = int(input('Please enter number: '))  # int
        myList.append(num)
    return myList

if __name__ == '__main__':  # it's better to add this line as suggested
    main()

rawDescription: How to add elements to a List in python?

description: Add elements to a List in python


code:
df = pd.read_fwf("2002FemPreg.dat",
colspecs=[(0,13), (274, 276), (276, 277), (277, 279), (422, 440)],
names=["caseid", "preglength", "outcome", "birthorder", "finalweight"])
df.head()

rawDescription: Best way to create a dataframe from several lists

description: Create a dataframe from several lists


code:
hf.spell("pipoteuse")
hf.stem("pipoteuses")  # try some classic plural stem
hf.analyze("pipoteuse")
hf.add_with_affix("pipoteuse", "chanteuse")
hf.spell("pipoteuse")
hf.analyze('pipoteuse')
hf.stem("pipoteuses")

rawDescription: How to add a new word to .dic/.aff files with pyhunspell?

description: Add a new word to .dic/.aff files with pyhunspell


code:
import csv
with open(path,'rb') as f:
    reader = csv.reader(f,delimiter='$')
    res = [ {'id':line[0],'date':line[1],'text':line[2]} for line in reader ]
    res = res[1:]

rawDescription: Using python to parse through text file with delimiter


rawDescription: pandas series.sum() how to skip some indexes

description: Pandas series.sum() how to skip some indexes


code:
import collections as co
import itertools as it

def unique(list_):
    return len(set(list_)) == len(list_)

def get_combos(branches):
    by_parent = co.defaultdict(list)

    for branch in branches:
        by_parent[branch.p].append(branch)

    combos = it.product(*by_parent.values())

    return it.ifilter(lambda x: unique([b.c for b in x]), combos)

rawDescription: Combinatorics in Python

description: Combinatorics in Python


code:
import logging
mylog = logging.getLogger(__name__)
mylog.info('start')
while True:
    try:
       out = dostuff(data)
       mylog.debug('got {}, output={}'.format(data, out))
    except KeyboardInterrupt:
       break
    except Exception:
       mylog.exception('uhoh')
mylog.info('done')

rawDescription: Does Python timeout after a certain period of time?


code: df1.merge(df2[['sameid', 'addthiscolumn']], left_on='id', right_on='sameid')

rawDescription: Adding A Specific Column from a Pandas Dataframe to Another Pandas Dataframe


code:
foundId = False
with open('datafile.txt', 'r') as datafile:
    for line in datafile:
        if foundId:
            if line.startswith('RhPhase'):
                print('Found RhPhase for ID 543:')
                print(line)

                # end reading the file
                break
            elif line.startswith('ID:'):
                print('Error: Found another ID without finding RhPhase first')
                break

        # if we haven’t found the ID yet, keep looking for it
        elif line.startswith('ID:543'):
                foundId = True

rawDescription: Parse a file in python to find first a string, then parse the following strings until it find another string


code:
after_model_change(form, model, is_created)
#Called from create_model after successful database commit.

after_model_delete(model)
#Called from delete_model after successful database commit (if it has any meaning for a store backend).

on_form_prefill(form,id)
# Called from edit_view, if the current action is rendering the form rather than receiving client side input, after default pre-filling has been performed.

on_model_change(form,model,is_created)
#Called from create_model and update_model in the same transaction (if it has any meaning for a store backend).

on_model_delete(model)
# Called from delete_model in the same transaction (if it has any meaning for a store backend).

rawDescription: Could someone please show me where I can find Flask-Admin callbacks?


code:
s = StringIO.StringIO('1, "text1,text2", "text3, text4", a, b, c')
list(csv.reader(s, skipinitialspace=True))

rawDescription: Read CSV file with comma within fields in Python


rawDescription: python: Find associated value from 2 lists

description: Python: Find associated value from 2 lists


code:
import sys

class Outer:
    class Inner:
        @classmethod
        def parent_class(cls):
            outer_name = cls.__qualname__.split('.')[-2]
            outer_module = sys.modules[cls.__module__]
            outer_cls = vars(outer_module)[outer_name]
            print(outer_cls)

Outer.Inner.parent_class()  # <class '__main__.Outer'>

rawDescription: Access outer class object from inner class in Python 3


code:
def SubElement(parent, nsptag_str, **extra):
    nsptag = NamespacePrefixedTag(nsptag_str)
    nsmap = nsptag.nsmap
    element = oxml_parser.makeelement(nsptag.clark_name, nsmap=nsmap, **extra)
    parent.append(element)
    return element

rawDescription: I need a polyfill for objectify.SubElement


rawDescription: Matplotlib contour plot from xyz data with scale


code: activemq.prefetchSize: 1

rawDescription: Multiple consumers & producers connected to a message queue, Is that possible in AMQP?


code:
value = 'fred, debbie, dale, mitch, tintin, denmark'
memberList = value.split(',')

rawDescription: turn textarea values into python list

description: Turn textarea values into python list


code:
import ast

s="""{
"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {
        "value": "New",
        "onclick": Handlers.NEW
      },
      {
        "value": "Open",
        "onclick": Handlers.OPEN
      },
      {
        "value": "Custom",
        "onclick": "function(){doSomething(Handlers.OPEN);}"

      }
    ]
  }
}
}"""

def evaluate(obj):
    if isinstance(obj, ast.Module):
        return evaluate(obj.body[0])
    elif isinstance(obj, ast.Expr):
        return evaluate(obj.value)
    elif isinstance(obj, ast.Dict):
        return {key.s: parse(value) for key, value in zip(obj.keys, obj.values)}
    elif isinstance(obj, ast.List):
        return [parse(element) for element in obj.elts]
    elif isinstance(obj, ast.Str):
        return obj.s
    elif isinstance(obj, ast.Attribute):
        return evaluate(obj.value) + "." + obj.attr
    elif isinstance(obj, ast.Name):
        return obj.id
    elif isinstance(obj, ast.Num):
        return obj.n
    else:
        print("I apparently forgot", type(obj))

x = evaluate(ast.parse(s))
print(x)

rawDescription: Python: Parse JSON string with embedded Javascript constants/variables


code:
from nltk.stem.wordnet import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
lemmatizer.lemmatize('is', 'v')
lemmatizer.lemmatize('provided', 'v')
lemmatizer.lemmatize('using', 'v')

rawDescription: How to get the infinitive form of the verb using NLTK (pos tagging)

description: Get the infinitive form of the verb using NLTK (pos tagging)


code:
class MyTemplateAdmin(admin.ModelAdmin):
    def __init__(self, *args, **kwargs):
        super(MyTemplateAdmin, self).__init__(*args, **kwargs)
        #see if there are new templates
        #and set the inlines list property

    list_display = ('name')
    search_fields = ['name']

rawDescription: Django Admin: add inlines dynamically

description: Django Admin: add inlines dynamically


code:
class City(object):
    def __init__(self, name, time, local):
        self.name = name
        self.local = local
        self.time = time

class Store(object):
    def __init__(self):
        self.data = {}

    def readfile(self, filename):
        with open(filename, 'r') as datafile:
            subdata = []
            for line in datafile:
                if line.startswith('----'):
                    city = City(subdata[0], subdata[1], subdata[2])
                    self.data[subdata[0]] = city
                    subdata = []
                else:
                    subdata.append(line.rstrip())

    def city_named(self, city_name):
        return self.data[city_name]

store = Store()
store.readfile('Data.txt')

example_city = store.city_named('City1')

print(example_city.name)
print(example_city.time)
print(example_city.local)

rawDescription: Python - list search

description: Python - list search


code:
from flask import Flask
from werkzeug.routing import BaseConverter

app = Flask(__name__)

class RegexConverter(BaseConverter):
        def __init__(self, url_map, *items):
                super(RegexConverter, self).__init__(url_map)
                self.regex = items[0]

app.url_map.converters['regex'] = RegexConverter

# To get all URLs ending with "/number"
@app.route("/<regex('.*\/([0-9]+)'):param>/")
def go_to_one(param):
    return param.split("/")[-1]

# To get all URLs ending with a number
@app.route("/<regex('.*([0-9]+)'):param>/")
def go_to_one(param):
    return param.split("/")[-1]

# To get all URLs without a number
@app.route("/<regex('[^0-9]+'):param>/")
def go_to_two(param):
    return param

@app.route('/')
def hello_world():
    return 'Hello World!'

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

rawDescription: Define a route for url ending with integer in python


code:
LOGINFORM = {'name': FORMNAME, 'input':
     [{nick: {'name': name, 'value': value, 'type': sort}}
       for nick, name, value, sort in zip(nicks, names, values, types)]}

rawDescription: build a complex python dictionary from multiple lists and variables

description: Build a complex python dictionary from multiple lists and variables


code:
import time
import threading
import win32ui, win32gui, win32com, pythoncom, win32con
from win32com.client import Dispatch

class IeThread(threading.Thread):
    def run(self):
        pythoncom.CoInitialize()
        ie = Dispatch("InternetExplorer.Application")
        ie.Visible = 0
        ie.Navigate('http://website/file.xml')

def PushButton(handle, label):
    if win32gui.GetWindowText(handle) == label:
        win32gui.SendMessage(handle, win32con.BM_CLICK, None, None)
        return True

IeThread().start()
time.sleep(3)  # wait until IE is started
wnd = win32ui.GetForegroundWindow()
if wnd.GetWindowText() == "File Download - Security Warning":
    win32gui.EnumChildWindows(wnd.GetSafeHwnd(), PushButton, "&Save");
    time.sleep(1)
    wnd = win32ui.GetForegroundWindow()
if wnd.GetWindowText() == "Save As":
    win32gui.EnumChildWindows(wnd.GetSafeHwnd(), PushButton, "&Save");

rawDescription: Downloading file using IE from python

description: Downloading file using IE from python


code:
class A(object): pass
class B(A): pass
class C(object): pass
issubclass(A, A)
issubclass(B, A)
issubclass(C, A)

rawDescription: How can I check if a type is a subtype of a type in Python?

description: Check if a type is a subtype of a type in Python


code:
def get_file_size(filename):
    "Get the file size by seeking at end"
    fd= os.open(filename, os.O_RDONLY)
    try:
        return os.lseek(fd, 0, os.SEEK_END)
    finally:
        os.close(fd)

rawDescription: Query size of block device file in Python


code:
import re
re.sub('([A-Z]{1})', r'_\1','OneWorldIsNotEnoughToLive').lower()

# result '_one_world_is_not_enough_to_live'

rawDescription: Replace uppercase characters with lowercase+extra characters


code:
class InterviewMixin():
    ...

class InterviewSubmit(Form, InterviewMixin):
    submit = SubmitField('New submit')

class InterviewUpdate(Form, InterviewMixin):
    submit = SubmitField('New update')

rawDescription: Update text of submit button in wtforms

description: Update text of submit button in wtforms


code: heading.get_text().replace('\n',' ').replace('NYT Now', '')

rawDescription: How to remove \n from the middle of texts in a list of texts in python

description: Remove \n from the middle of texts in a list of texts in python


code:
cols = ['col1', 'col2', 'col3', 'col4']

@click.option('--columns', is_flag=False, default=','.join(cols), show_default=True,
              metavar='<columns>', type=click.STRING, help='Sets target columns')
@click.command()
def main(columns):
    # split columns by ',' and remove whitespace
    columns = [c.strip() for c in columns.split(',')]

    # validate passed columns
    for c in columns:
        if c not in cols:
            raise click.BadOptionUsage("%s is not an available column." % c)

    print(columns)

rawDescription: How to supply multiple options with array validation?

description: Supply multiple options with array validation


code:
thingy = r''' ok: [162.243.95.241] => {"cache_control": "private", "changed": false, "content": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<ApiResponse Status=\"OK\" xmlns=\"http://api.namecheap.com/xml.response\">\r\n  <Errors />\r\n  <Warnings />\r\n  <RequestedCommand>namecheap.domains.create</RequestedCommand>\r\n  <CommandResponse Type=\"namecheap.domains.create\">\r\n    <DomainCreateResult Domain=\"123er321test.com\" Registered=\"true\" ChargedAmount=\"8.1800\" DomainID=\"33404\" OrderID=\"414562\" TransactionID=\"679462\" WhoisguardEnable=\"false\" FreePositiveSSL=\"false\" NonRealTimeDomain=\"false\" />\r\n  </CommandResponse>\r\n  <Server>WEB1-SANDBOX1</Server>\r\n  <GMTTimeDifference>--5:00</GMTTimeDifference>\r\n  <ExecutionTime>9.008</ExecutionTime>\r\n</ApiResponse>", "content_length": "647", "content_location": "https://api.sandbox.namecheap.com/xml.response", "content_type": "text/xml; charset=utf-8", "date": "Thu, 21 Nov 2013 03:23:51 GMT", "item": "", "redirected": false, "server": "Microsoft-IIS/7.0", "status": 200, "x_aspnet_version": "4.0.30319", "x_powered_by": "ASP.NET"}'''
j = thingy.partition('=>')[-1]
obj = json.loads(j)
xml = obj['content']
soup = BeautifulSoup(xml)
soup
soup.find('apiresponse')['status']

rawDescription: VERY BAD XML trying to parse with Python


code:
#function for decrypting file
def decryptfile(openFile2, outfile): # <- CHANGED to add outfile
    for line in openFile2:
        for word in line.split():
            empty = ''
            for char in word:
                if char not in string.punctuation:
                    char=char.lower()
                    empty = empty+char
                if len(empty) == 2:
                    outfile.write(rulesdecrypt[empty]) # <- CHANGED to write to file
                    empty = ''
            if len(empty) == 1:
                outfile.write(rulesdecrypt[empty]) # <- CHANGED to write to file

rawDescription: Use substitution cipher in python to encrypt and decrypt a .txt file and output to a new .txt file


code:
def connect_to_b(self):

    self.b()

rawDescription: Connecting three classes from methods inside of them in python


code:
#!/usr/bin/python

f = open("blah.txt");       # open file
for line in f:              # iterate over lines in file
  line = line.strip()       # strip leading and trailing white space
  print ("," + line + ",")  # print line between commas
f.close() # close file      # close file when done

rawDescription: Print beginning and ending with a comma in Python?


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

fig, ax1 = plt.subplots(1,1)

ys = np.random.random((5, 6))
ax1.plot(range(5), ys)
ax1.set_title('Default color cycle')
plt.show()

# From the sample matplotlibrc:
#axes.color_cycle    : b, g, r, c, m, y, k  # color cycle for plot lines
                                            # as list of string colorspecs:
                                            # single letter, long name, or
                                            # web-style hex

# setting color cycle after calling plt.subplots doesn't "take"
# try some hex values as **string** colorspecs
mpl.rcParams['axes.color_cycle'] = ['#129845','#271254', '#FA4411', '#098765', '#000009']

fig, ax2 = plt.subplots(1,1)
ax2.plot(range(5), ys)
ax2.set_title('New color cycle')

n = 6
color = plt.cm.coolwarm(np.linspace(0.1,0.9,n)) # This returns RGBA; convert:
hexcolor = map(lambda rgb:'#%02x%02x%02x' % (rgb[0]*255,rgb[1]*255,rgb[2]*255),
               tuple(color[:,0:-1]))

mpl.rcParams['axes.color_cycle'] = hexcolor

fig, ax3 = plt.subplots(1,1)
ax3.plot(range(5), ys)
ax3.set_title('Color cycle from colormap')

plt.show()

rawDescription: use matplotlib color map for color cycle

description: Use matplotlib color map for color cycle


code:
Point = namedtuple('Point', ['x', 'y'])
Point3D = namedtuple('Point3D', Point._fields + ('z',))
Book = namedtuple('Book', ['id', 'title', 'authors'])

rawDescription: Python naming convention - namedtuples

description: Python naming convention - namedtuples


code:
pygame.init()
pygame.time.set_timer(USEREVENT + 1, 100)
while True:
    for event in pygame.event.get():
        if event.type == USEREVENT + 1:
           functionName()
        if event.type == QUIT:
            pygame.quite()
            sys.exit()

rawDescription: execute function every x number of secods python

description: Execute function every x number of secods python


code:
# forms.py
from django import forms
from .models import Event

class EventForm(forms.ModelForm):
    class Meta:
        model = Event

# views.py
from django.http import HttpResponseRedirect
from django.views.generic import TemplateView
from django.forms.models import inlineformset_factory
from .models import EventDay, Event
from .forms import EventForm

class EventCreate(TemplateView):
    template_name = 'event_create.html'

    def get(self, request, *args, **kwargs):
        "GET forms ready!"
        # get form for Event
        event_form = EventForm()
        # here's the 'magic' inlineformset, better read the
        # django documentation about this
        EventDayFormSet = inlineformset_factory(Event, EventDay)
        formset = EventDayFormSet()
        # add to context and return response
        context = {'form': event_form, 'formset': formset}
        return self.render_to_response(context)

    def post(self, request, *args, **kwargs):
        "Handle form submission on POST request"
        # get form for Event with POST data
        event_form = EventForm(data=request.POST)
        # get formset for EventDay with POST data
        EventDayFormSet = inlineformset_factory(Event, EventDay)
        formset = EventDayFormSet(data=request.POST)
        if event_form.is_valid() and formset.is_valid():
            # valid forms, OK to save
            event = event_form.save()
            # EventDay needs a ForeignKey for Event since the field is
            # not nullable. Save the forms without committing to database...
            eventdays = formset.save(commit=False)
            for eventday in eventdays:
                # ... and add the ForeignKey field
                eventday.event = event
                eventday.save()
            # TODO use reverse('name_of_the_view_to_redirect_to') instead of '/'
            return HttpResponseRedirect('/')
        # Some error occurred with the forms, display errors and forms
        # so the user can fix it
        context = {'form': event_form, 'formset': formset}
        return self.render_to_response(context)

rawDescription: With Django, how can I add my model's ForeignKey to CreateView?


code: from idol7stats.idol7stats import *

rawDescription: Packaging a Python library

description: Packaging a Python library


rawDescription: How do I truncate a list?

description: Truncate a list


code:
import random

def some(x, n):
    return x.ix[random.sample(x.index, n)]

rawDescription: Random row selection in Pandas dataframe


code:
# initialise the whole list with some value
r = [-1]*N

for j, s_j in enumerate(s):
    r[s_j] = j

# if any element of r is still -1 then you know it did not appear in s

rawDescription: Better way to repeatedly get the position of elements in a list?


code:
import requests

s = requests.Session()
s.get('http://www.sicom.gov.co/precios/controller?accion=Home&option=SEARCH_PRECE')
response = s.get('http://www.sicom.gov.co/precios/controller?accion=ExportToExcel')

with file('out.xls','wb') as f:
    f.write(response.content)

rawDescription: Can't download entire file using urllib2


code:
colors = plt.cm.GnBu(np.linspace(0, 1, 10))
df.plot(kind='bar', stacked=True, figsize=(20, 10), color=colors)

rawDescription: Pandas stacked bar chart duplicates colors for large legends


code:
def widg1_changed(name, new_value):
    widg2.value = 5 * new_value

widg1.on_trait_change(widg1_changed, 'value')

rawDescription: Is it possible to link IPython widgets less naively?


code:
import csv

res = []
infile = open(filename, "r")
oufile = open(oufilename, "w")
inf = csv.reader(infile)
inf.next()
ouf = csv.writer(oufile)
ouf.writerow(["name", "count", "gender"])
for row in inf:
    res.append([row[0], row[1], "F"])
    res.append([row[2], row[3], "M"])
ouf.writerows(res)
infile.close()
oufile.close()

rawDescription: Manipulating multiple CSV files in python


code: getattr(o, "p")

rawDescription: Dynamic method resolution on a super() object


code:
from pytest import fixture

@fixture(scope="module")
def or_datas(request):
    # Something done before a test.

    def fin():
        # Here, just do something after the test.
        ...

    request.addfinalizer(fin)

# How to use it ?

def test_something(or_datas):
    # Note the argument corresponding the function decorated
    # by fixture.
    ...

rawDescription: py.test : do something just after a test

description: Py.test : do something just after a test


code:
class MODULEENTRY32(Structure):
    _fields_ = [( 'dwSize' , DWORD ) ,
                ( 'th32ModuleID' , DWORD ),
                ( 'th32ProcessID' , DWORD ),
                ( 'GlblcntUsage' , DWORD ),
                ( 'ProccntUsage' , DWORD ) ,
                ( 'modBaseAddr' , POINTER(BYTE) ) ,
                ( 'modBaseSize' , DWORD ) ,
                ( 'hModule' , HMODULE ) ,
                ( 'szModule' , c_char * 256 ),
                ( 'szExePath' , c_char * 260 ) ]

rawDescription: How to enumerate modules in python 64bit

description: Enumerate modules in python 64bit


code:
import os
os.system("""cd ../linux-3.11/; cp arch/x86/configs/byt_32_tz_emgd_defconfig .config; make ARCH=i386 oldconfig; make ARCH=i386 -j 4""")

rawDescription: Create bash file using python

description: Create bash file using python


rawDescription: Django filter where ManyToMany field contains ALL of list


code:
class dog(object):
	def __init__(self, height, width, lenght):
		self.height = height
		self.width = width
		self.length = length

	def revert(self):
		self.height = 1
		self.width = 2
		self.length = 3

dog1 = dog(5, 6, 7)
dog2 = dog(2, 3, 4)

dog1.revert()

rawDescription: Classes in Python

description: Classes in Python


code: [tuple([x] + y) for x, y in zip(a, b)]

rawDescription: Creating a list of tuples(?) from a list and list of lists of even length


code:
import sys
import os
import struct
from ctypes import (CDLL, get_errno)
from ctypes.util import find_library
from socket import (
    socket,
    AF_BLUETOOTH,
    SOCK_RAW,
    BTPROTO_HCI,
    SOL_HCI,
    HCI_FILTER,
)

if not os.geteuid() == 0:
    sys.exit("script only works as root")

btlib = find_library("bluetooth")
if not btlib:
    raise Exception(
        "Can't find required bluetooth libraries"
        " (need to install bluez)"
    )
bluez = CDLL(btlib, use_errno=True)

dev_id = bluez.hci_get_route(None)

sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)
sock.bind((dev_id,))

err = bluez.hci_le_set_scan_parameters(sock.fileno(), 0, 0x10, 0x10, 0, 0, 1000);
if err < 0:
    raise Exception("Set scan parameters failed")
    # occurs when scanning is still enabled from previous call

# allows LE advertising events
hci_filter = struct.pack(
    "<IQH",
    0x00000010,
    0x4000000000000000,
    0
)
sock.setsockopt(SOL_HCI, HCI_FILTER, hci_filter)

err = bluez.hci_le_set_scan_enable(
    sock.fileno(),
    1,  # 1 - turn on;  0 - turn off
    0, # 0-filtering disabled, 1-filter out duplicates
    1000  # timeout
)
if err < 0:
    errnum = get_errno()
    raise Exception("{} {}".format(
        errno.errorcode[errnum],
        os.strerror(errnum)
    ))

while True:
    data = sock.recv(1024)
    # print bluetooth address from LE Advert. packet
    print(':'.join("{0:02x}".format(x) for x in data[12:6:-1]))

rawDescription: Finding Bluetooth low energy with python


code:
import hashlib
from PIL import Image
from itertools import chain

img = Image.open(r'A.tif')
img.seek(0)

md5 = hashlib.md5()

if isinstance(img.getpixel((0,0)), int):
    md5.update(bytes(img.getdata()))
else:
    md5.update(bytes(chain.from_iterable(img.getdata())))

print(md5.hexdigest())

rawDescription: Python Hash of First Image in MultiTIf

description: Python Hash of First Image in MultiTIf


code:
class InstalledModel(models.Model):
    @property
    def another_model_choices(self):
        if isinstance(self, MyModel):
            return {'some_condition': True}
        return None

    base_field = models.ForeignKey(AnotherModel, limit_choices_to=another_model_choices)

rawDescription: Override field arguments in inherited django models


code:
headers={
    'Content-Disposition': 'attachment; filename=' + stream['filename']
}
for header in ('content-type', 'content-length', 'transfer-encoding'):
    if header in binary.headers:
        headers[header] = binary.headers[header]
return flask.Response(binary.raw, headers=headers)

rawDescription: Download doesn't start in web browser by flask.Response


rawDescription: Updating a field on multiple Resources in tastypie


rawDescription: numpy: print array with indentation

description: Numpy: print array with indentation


rawDescription: Search not exists in Django

description: Search not exists in Django


code:
import maya.cmds as cmds

class image(object):
    def __init__(self,name):
        plane = cmds.nurbsPlane(axis=(0,0,0),width = 10)
        shader = cmds.shadingNode('surfaceShader', asShader=True)
        SG = cmds.sets(empty=True, renderable=True,
                       noSurfaceShader=True, name=shader+"SG")
        cmds.connectAttr(shader+'.outColor', SG+".surfaceShader",
                         force=True)
        img = cmds.shadingNode('file', asTexture=True)
        cmds.setAttr(img+'.fileTextureName', name, type='string')
        cmds.connectAttr(img+'.outColor', shader+'.outColor',
                         force=True)
        # you should connect to a texture placement node and
        # its numerous connectins here
        cmds.sets(plane[0], edit=True, forceElement=SG)

name=r'C:\Desert.jpg'
temp = image(name)

rawDescription: maya python place image on a plane

description: Maya python place image on a plane


code:
import pyPdf
pdf = pyPdf.PdfFileReader(open("file.pdf"))
pdf.decrypt("password")

rawDescription: decrypting pdf protected by aes-256bit using the right password

description: Decrypting pdf protected by aes-256bit using the right password


code:
import numpy
from pylab import *
import time

class StdOutListener():
  def __init__(self):
    self.start_time = time.time()
    self.x = []
    self.y = []
    self.my_average = []
    self.line_actual, = plot(self.x, self.y)                  # line stores a Line2D we can update
    self.line_average, = plot(self.x, self.my_average)       # line stores a Line2D we can update

  def on_data(self, new_value):
    time_delta = time.time() - self.start_time                # on our x axis we store time since start
    self.x.append(time_delta)
    self.y.append(new_value)
    self.my_average.append(numpy.mean(self.y))
    self.line_actual.set_data(self.x, self.y)
    self.line_average.set_data(self.x, self.my_average)
    ylim([min(self.y), max(self.y)])        # update axes to fit the data
    xlim([min(self.x), max(self.x)])
    draw()                                  # redraw the plot

ion()                                       # ion() allows matplotlib to update animations.

out_listener = StdOutListener()
for i in range(1000):
  out_listener.on_data(i + numpy.random.randint(-5,5))

rawDescription: How to continuously stream data points from dynamically growing list using matplotlib.animation for real time graphing?

description: Continuously stream data points from dynamically growing list using matplotlib.animation for real time graphing


rawDescription: Trying to pack binary bits represented by bytes into a byte in python


code:
x = '1 (2,3), 4 (5)'
gp = re.compile('\d+\s*\([\d,]+\)(?=,?)')
# digit, whitespace, (, digits or commas, ), maybe a comma
for token in gp.findall(x):
    token = token.split("(", 1)
    left, right = int(token[0]), map(int, token[1][:-1].split(","))
    for elt in right:
            print((left, elt))

rawDescription: Rearrange a grouping of numbers into a list of tuples


code: b = a[:]  # There are various other options such as list(a), copy.copy(a) etc.

rawDescription: Python: How to create new identical list object?


code:
import random

capital_of = {'Australia':'Canberra',
              'England':'London',
              'Indonesia':'Jakarta',
              'Canada':'Ottawa',}

score = 0
for country, capital in random.sample(capital_of.items(), len(capital_of)):
    guess = input("What is the capital of %s? " % country)
    if guess.lower() == capital.lower():
        print("Correct, you have scored")
        score +=1
    else:
        print('Sorry, you have entered an incorrect answer')

print("Score: {}".format(score))

rawDescription: Python - Do I need to remove instances from a dictionary?


code:
# NYSE
url_nyse = "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nyse&render=download"
# Nasdaq
url_nasdaq = "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download"
# AMEX
url_amex = "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=amex&render=download"

import pandas as pd

df = pd.DataFrame.from_csv(url_nyse)
stocks = df.index.tolist()

rawDescription: How to pull stock data for every stock on a given exchange

description: Pull stock data for every stock on a given exchange


code: line = self.app.stdout.readline()

rawDescription: Streaming stdin/stdout in Python

description: Streaming stdin/stdout in Python


code:
tmp = np.array([-10**10, 10**10])
tmp_max = tmp.max()
log_D = log(sum(exp(tmp - tmp_max))) + tmp_max
log_v = tmp - log_D
v = np.exp(log_v)
v

rawDescription: How to order this computation for numerical stability?

description: Order this computation for numerical stability


rawDescription: Cannot get the reverse URL from 3rd party packages in Django


code:
lines = open('myfile.txt').readlines()
for i in range(0, 1000000, 15000):
   open('{0}_{1}.txt'.format(i+1, i+15000), 'w').writelines(lines[i:i+15000])

rawDescription: Cut a large text file in small files

description: Cut a large text file in small files


code:
class FindableModel(db.Model):
  def __getattr__(self, name):
    if not name.startswith("find_by_"):
      raise AttributeError(name)
    field = name[len("find_by_"):]
    return lambda value: self.all().filter(field, value)

rawDescription: Dynamic Finders and Method Missing in Python


code:
def remove_one():
    text = E1.get().replace('1', "")
    E1.delete(0, END)
    E1.insert(0, text)

rawDescription: How to edit what an entry widget displays

description: Edit what an entry widget displays


code:
import peewee as pw

myDB = pw.MySQLDatabase("mydb", host="mydb.crhauek3cxfw.us-west-2.rds.amazonaws.com", port=3306, user="user", passwd="password")

class MySQLModel(pw.Model):
    """A base model that will use our MySQL database"""
    class Meta:
        database = myDB

class User(MySQLModel):
    username = pw.CharField()
    # etc, etc

# when you're ready to start querying, remember to connect
myDB.connect()

rawDescription: Accessing remote MySQL database with peewee


code:
import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(400, 320), style=(wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN))

        # Setting up menu
        filemenu = wx.Menu()
        set_m = filemenu.Append(wx.ID_PREFERENCES, 'Settings', 'Settings')
        # filemenu.Append(US)
        filemenu.AppendSeparator()
        exit_m = filemenu.Append(wx.ID_EXIT)

        # Creating the menubar
        menubar = wx.MenuBar()
        menubar.Append(filemenu, 'Menu')
        self.SetMenuBar(menubar)

        self.Bind(wx.EVT_MENU, self.OnSettings, set_m)

    def OnSettings(self, e):
        SetFrame().Show()

class SetFrame(wx.Frame):
    title = 'Settings'
    def __init__(self):
        wx.Frame.__init__(self, wx.GetApp().TopWindow, title=self.title, size=(400, 250), style=(wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN))
        self.Bind(wx.EVT_CLOSE, self.onClose)
        self.MakeModal()

    #----------------------------------------------------------------------
    def onClose(self, event):
        """
        Make the frame non-modal as it closes to re-enable other windows
        """
        self.MakeModal(False)
        self.Destroy()

class MyApp (wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, title='Parent-Frame')
        self.frame.Show()
        self.SetTopWindow(self.frame)
        return True

if __name__ == '__main__':
    app = MyApp(0)
    app.MainLoop()

rawDescription: Making top window inaccessible in wx.python


code:
from PyQt4 import QtGui
from functools import partial
import sys

class MainWidget(QtGui.QWidget):
    def __init__(self):
        super(MainWidget, self).__init__()

        self.another = Another()
        self.simple = Simple()
        self.grid = QtGui.QGridLayout()

        self.main_widget()

    def main_widget(self):
        show_another_button = QtGui.QPushButton("Show Another")
        show_another_button.clicked.connect(self.another.show_form)
        self.grid.addWidget(show_another_button, 0, 0)

        show_simple_button = QtGui.QPushButton("Show Simple")
        show_simple_button.clicked.connect(self.simple.show_simple)
        self.grid.addWidget(show_simple_button, 0, 1)

        another_button = QtGui.QPushButton("Print Hello")
        another_button.clicked.connect(partial(print, "Hello"))
        self.grid.addWidget(another_button, 0, 2)

        self.setLayout(self.grid)

class Another(QtGui.QWidget):
    def __init__(self):
        super(Another, self).__init__()
        print("another initialized")

    def show_form(self):
        print("another called")
        grid = QtGui.QGridLayout()

        self.setLayout(grid)

        self.show()

class Simple(QtGui.QDialog):
    def __init__(self):
        super(Simple, self).__init__()
        print("simple initialized")

    def show_simple(self):
        print("simple called")
        self.show()

def main():
    app = QtGui.QApplication(sys.argv)
    ex = MainWidget()
    ex.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

rawDescription: Simple PyQt4 showing other windows

description: Simple PyQt4 showing other windows


rawDescription: how to detect if a point lies in a non-rectangular region in a 2D numpy array?

description: Detect if a point lies in a non-rectangular region in a 2D numpy array


code:
def f(a, *unused):
    return a

rawDescription: How can I denote unused function arguments?

description: Denote unused function arguments


code:
import bluetooth
sockfd = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sockfd.connect(('00:24:7E:9E:55:0D', 1)) # BT Address
sockfd.send('ATZ\r')
time.sleep(1)
sockfd.send(chr(26))
sockfd.close()

rawDescription: Send AT-command through bluetooth from python application


code:
import subprocess
from glob import glob
subprocess.call(["mock"] + glob("*.src.rpm"))

rawDescription: Shell expansion in Python subprocess

description: Shell expansion in Python subprocess


code:
import struct
from zlib import crc32
import os

# PNG file format signature
pngsig = '\x89PNG\r\n\x1a\n'

def swap_palette(filename):
    # open in read+write mode
    with open(filename, 'r+b') as f:
        f.seek(0)
        # verify that we have a PNG file
        if f.read(len(pngsig)) != pngsig:
            raise RuntimeError('not a png file!')

        while True:
            chunkstr = f.read(8)
            if len(chunkstr) != 8:
                # end of file
                break

            # decode the chunk header
            length, chtype = struct.unpack('>L4s', chunkstr)
            # we only care about palette chunks
            if chtype == 'PLTE':
                curpos = f.tell()
                paldata = f.read(length)
                # change the 3rd palette entry to cyan
                paldata = paldata[:6] + '\x00\xff\xde' + paldata[9:]

                # go back and write the modified palette in-place
                f.seek(curpos)
                f.write(paldata)
                f.write(struct.pack('>L', crc32(chtype+paldata)&0xffffffff))
            else:
                # skip over non-palette chunks
                f.seek(length+4, os.SEEK_CUR)

if __name__ == '__main__':
    import shutil
    shutil.copyfile('redghost.png', 'blueghost.png')
    swap_palette('blueghost.png')

rawDescription: Changing palette's of 8-bit .png images using python PIL


code:
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt

df = pd.DataFrame({
    'customer_id': ['00001', '00002', '00002', '00003', '00003'],
    'transaction_date': ['1997-01-01 00:00:00',
                         '1997-01-12 00:00:00',
                         '1997-01-12 00:00:00',
                         '1997-01-02 00:00:00',
                         '1997-03-30 00:00:00']
                  })

markers = ['x', 'o', '+']

for index, row in df.iterrows():
    t = dt.datetime.strptime( row['transaction_date'], '%Y-%m-%d %H:%M:%S')
    x = (t - dt.datetime(1970,1,1) ).total_seconds()
    y = int(row['customer_id'])
    plt.scatter(x,y, marker= markers[y-1],s=200)

plt.yticks( range(1,4) )
plt.show()

rawDescription: In Python, how to chart customer transactions over time (see embedded image for example)


rawDescription: Regular expression to replace a character on odd repeated occurrences in Python


code: soup = BeautifulSoup(response, 'xml')

rawDescription: beautifulsoup not printing links

description: Beautifulsoup not printing links


code:
string = "(1,-5)"
map(int,re.findall(r'-?\d',string))

rawDescription: Using Python Regex to extract x and y from a coordinate pair


code:
cherrypy.tree.mount(RootApp(), '')
# you can do some config with cherrypy.config or on the mount third argument.
cherrypy.engine.start()
import webbrowser
webbrowser.open('localhost:8080')

rawDescription: Cherrypy and interactive python

description: Cherrypy and interactive python


code:
from cherrypy.process.plugins import Monitor

def foo():
    my.store.collect_data('things', 'stuff')

Monitor(cherrypy.engine, foo, frequency=300).subscribe()

rawDescription: CherryPy with additional threads for custom jobs


code:
import ctypes
from numpy.ctypeslib import ndpointer

lib = ctypes.CDLL('./library.so')
lib.function.restype = ndpointer(dtype=ctypes.c_int, shape=(10,))

res = lib.function()

rawDescription: How to return array from C++ function to Python using ctypes

description: Return array from C++ function to Python using ctypes


rawDescription: Create mask from QImage using non-transparent pixels


code:
import pprint

my_previous_list = [[['a1a','a2a','a3a'], ['a1b','a2b','a3b'], ['a1c','a2c','a3c']],[['b1a','b2a','b3a'],['b1b','b2b','b3b'],['b1c','b2c','b3c']],[['c1a','c2a','c3a'],['c1b','c2b','c3b'],['c1c','c2c','c3c']]]

print('Original')
pprint.pprint(my_previous_list)

all_lists = []
for a in my_previous_list[0]:
    for b in my_previous_list[1]:
        for c in my_previous_list[2]:
            line = []
            line.append(a + b + c)
            all_lists.append(line)

print('Processed')
pprint.pprint(all_lists)

rawDescription: Python: Merge lists within tuples OR combining lists within lists within lists without itertools and thus tuple object


code:
class Server(object):
    _cp_config = {
        'tools.sessions.on': True,
        'tools.auth.on': True
    }
    auth = AuthController()
    @cherrypy.expose
    @require()
    def index(self):
        f = open ("file.txt","r")
        link = f.read()
        f.close()
        myText = "Hello World"
        html = """

%s

        """ %(myText, link)
        return html
    index.exposed = True

rawDescription: How to pass python variable to html variable?

description: Pass python variable to html variable


code:
select_sql = """  SELECT * FROM table
                  WHERE ((? = 'all' OR gender = ?) AND (? = 'all' OR hand = ?))
             """

cursor.execute(select_sql, (gender_var, gender_var, hand_var, hand_var))

rawDescription: 'Don't care' for a column in SQLite queries?


code:
# change GetPacket so the second argument is in, not out
packet = (c_ubyte * size)()
data.GetPacket(guid, size, packet)

rawDescription: Using COM interfaces with initialized OUT parameters via Python comtypes library


code:
import mechanize
req = mechanize.Request("http://foobar.com/")
req.add_header("Referer", "http://wwwsearch.sourceforge.net/mechanize/")
r = mechanize.urlopen(req)

rawDescription: Specifying redirect page to clicked link


code:
driver.find_element_by_xpath(".//input[contains(@onclick, '1 Bedroom Deluxe')]")

driver.find_element_by_css_selector("input[onclick*='1 Bedroom Deluxe']")

rawDescription: Find and click an item from 'onclick' partial value


code:
d = {
"modID1":
    {
        "sequences1": {"header1": 3},
        "sequences2": {"header2": 5},
        "sequences3": {"header3": 1}
    },
"modID2":
    {
        "sequences1": {"header1": 1},
        "sequences2": {"header2": 8},
    },
}
result = {
    k: OrderedDict(
        sorted(
            v.items(),  # "sequences1": {"header1": 3},"sequences2":     {"header2": 5},"sequences3": {"header3": 1}
            key=lambda h: next(  # h is  (sequences1,{"header1": 3})
                iter(
                    h[1].values()  # h[1] is {"header1": 3}
                )
            ),
            reverse=True
        )
    ) for k, v in d.items()}

print(result)
output is {'modID1': OrderedDict([('sequences2', {'header2': 5}), ('sequences1', {'header1': 3}), ('sequences3', {'header3': 1})]), 'modID2': OrderedDict([('sequences2', {'header2': 8}), ('sequences1', {'header1': 1})])}

rawDescription: python: sorting 3 levels nested dictionaries

description: Python: sorting 3 levels nested dictionaries


code:
wx.MessageBox('Lan1 = %s, lan2 = %s' % (str(lan1), str(lan2)),
              wx.OK | wx.ICON_INFORMATION)

rawDescription: How to display variables in a MessageBox

description: Display variables in a MessageBox


code:
n = pynotify.Notification("summary", "body\n next line", "dialog-warning")
n.show()

rawDescription: How to get pynotify to display line breaks and HTML?

description: Get pynotify to display line breaks and HTML


code:
result = l.search_s(base, ldap.SCOPE_SUBTREE, criteria, attributes)

users = []

for i in [entry for dn, entry in result if isinstance(entry, dict)]:
    dn = i.get('distinguishedName')[0].replace('\, ', '--').split(',')
    info = collections.defaultdict(list)
    info['id'] = i.get('sAMAccountName')[0]

    for part in dn:
        key,value = part.split('=',1)
        info[key].append(value)

    users.append(info)

max_cn = max(map(lambda u: len(u['CN']), users))
assert max_cn == 1

max_ou = max(map(lambda u: len(u['OU']), users))
max_dn = max(map(lambda u: len(u['DN']), users))

numflds = max_cn + max_ou + max_dn

fields = []
for u in users:
    f = [u['CN']]
    ou = u['OU'] + [''] * max_ou
    f.extend(ou[:max_ou])
    dn = u['DN'] + [''] * max_dn
    f.extend(dn[:max_dn])
    f.append(u['id'])

rawDescription: Extracting columns from a string or list in python


code:
"""
restricting execution based on request body size
"""
from pyramid.config import Configurator
from pyramid.view import view_config
from pyramid.events import NewRequest, subscriber
from webob import Response, Request
from webob.exc import HTTPBadRequest
import unittest

def restrict_body_middleware(app, max_size=0):
    """
    this is straight wsgi middleware and in this case only depends on
    webob. this can be used with any wsgi compliant web
    framework(which is pretty much all of them)
    """
    def m(environ, start_response):
        r = Request(environ)
        if r.content_length <= max_size:
            return r.get_response(app)(environ, start_response)
        else:
            err_body = """
            request content_length(%s) exceeds
            the configured maximum content_length allowed(%s)
            """ % (r.content_length, max_size)
            res = HTTPBadRequest(err_body)
            return res(environ, start_response)

    return m

def new_request_restrict(event):
    """
    pyramid event handler called whenever there is a new request
    recieved

    http://docs.pylonsproject.org/projects/pyramid/en/1.2-branch/narr/events.html
    """
    request = event.request
    if request.content_length >= 0:
        raise HTTPBadRequest("too big")

@view_config()
def index(request):
    return Response("HI THERE")

def make_application():
    """
    make appplication with one view
    """
    config = Configurator()
    config.scan()
    return config.make_wsgi_app()

def make_application_with_event():
    """
    make application with one view and one event subsriber subscribed
    to NewRequest
    """
    config = Configurator()
    config.add_subscriber(new_request_restrict, NewRequest)
    return config.make_wsgi_app()

def make_application_with_middleware():
    """
    make application with one view wrapped in wsgi middleware
    """
    return restrict_body_middleware(make_application())

class TestWSGIApplication(unittest.TestCase):
    def testNoRestriction(self):
        app = make_application()
        request = Request.blank("/", body="i am a request with a body")
        self.assert_(request.content_length > 0, "content_length should be > 0")
        response = request.get_response(app)
        self.assert_(response.status_int == 200, "expected status code 200 got %s" % response.status_int)

    def testRestrictedByMiddleware(self):
        app = make_application_with_middleware()
        request = Request.blank("/", body="i am a request with a body")
        self.assert_(request.content_length > 0, "content_length should be > 0")
        response = request.get_response(app)
        self.assert_(response.status_int == 400, "expected status code 400 got %s" % response.status_int)

    def testRestrictedByEvent(self):
        app = make_application_with_event()
        request = Request.blank("/", body="i am a request with a body")
        self.assert_(request.content_length > 0, "content_length should be > 0")
        response = request.get_response(app)
        self.assert_(response.status_int == 400, "expected status code 400 got %s" % response.status_int)

if __name__ == "__main__":
    unittest.main()

rawDescription: Restricting POST requests to a maximum size on Pyramid


code:
s = pd.date_range('2015-01', '2015-03', freq='5D')  # DatetimeIndex
pd.Series(1, index=s).resample('M', how='count')

rawDescription: How to Bin/Count based on dates in Python

description: Bin/Count based on dates in Python


rawDescription: How do I get Cyrillic in the output, Python?

description: Get Cyrillic in the output, Python


code:
from collections import Counter
wordlist = open('so.py', 'r').read().split()
word_counts = Counter(wordlist)

write_file = open('wordfreq.txt', 'w')
for w, c in sorted(word_counts.iteritems(), key=lambda x: x[1], reverse=True):
    write_file.write('{w}, {c}\n'.format(w=w, c=c))

rawDescription: Words, sorted by frequency, in a book (.txt file)


code:
def filtered(lines):
    for line in lines:
        if line.startswith('Student:') or line.startswith('Your score for this lab:'):
            yield line.rstrip().split()[-1]

with open('test.txt', 'r') as f:
    for student, score in zip(*[filtered(f)]*2):
        print(student, score)

rawDescription: I need help parsing txt using any script


rawDescription: How do I group by nth business day within month in Pandas?

description: Group by nth business day within month in Pandas


code:
import arcpy,os, easygui,sys

mssng_files = r'L:\SF_HMP - transferred to Ydrive\Maps\broken_links_missing_files.txt'
wkspc = easygui.enterbox("Enter workspace path:",title='Search for Files')
output_loc = easygui.enterbox("Output location:",title='Copy Files')

with open(mssng_files) as filelist:
  for line in filelist:
    files_to_find.append(line.strip())

for dirpath, dirnames, filenames in arcpy.da.Walk(wkspc,datatype='FeatureClass'):
  for filename in filenames:
        if filename in files_to_find:
            ws_l = os.path.join(dirpath,filename)
            arcpy.env.workspace = ws_l
            arcpy.FeatureClassToGeodatabase_conversion(ws_l,output_loc)

for dirpath, dirnames, filenames in arcpy.da.Walk(wkspc,datatype='RasterDataset'):
  for filename in filenames:
        if filename in files_to_find:
            ws_r = os.path.join(dirpath,filename)
            arcpy.env.workspace = ws_r
            arcpy.RasterToGeodatabase_conversion(ws_r,output_loc)

for dirpath, dirnames, filenames in arcpy.da.Walk(wkspc,datatype='Table'):
  for filename in filenames:
        if filename in files_to_find:
            ws_t = os.path.join(dirpath,filename)
            arcpy.env.workspace = ws_t
            arcpy.TableToGeodatabase_conversion(ws_t,output_loc)

rawDescription: trouble with FeatureClassToGeodatabase_conversion in arcpy, using .da.Walk

description: Trouble with FeatureClassToGeodatabase_conversion in arcpy, using .da.Walk


code:
import paramiko

source = r'C:\Somedir\somefile.txt'
dest = r'/home/user/file.txt'
hostname = 'linux.server.com'
port = 22 # default port for SSH
username = 'user'
password = 'secret'

try:
    t = paramiko.Transport((hostname, port))
    t.connect(username=username, password=password)
    sftp = paramiko.SFTPClient.from_transport(t)
    sftp.put(source, dest)
finally:
    t.close()

rawDescription: How to Transfer Files from Client to Server Computer by using python script?


code:
pl = readPlist(pathOrFile)
print(pl["1"])

rawDescription: Import XML dictionary in Python

description: Import XML dictionary in Python


code:
def init_process():
    global my_object
    my_object = create_costly_object()

def costly_function(task):
    global my_object
    solution = solve_task_using_my_object
    return solution

def psolve_problem():
    pool = multiprocessing.Pool(initializer=init_process)
    tasks = get_list_of_tasks()
    all_solutions = pool.map_async(costly_function, tasks)
    return all_solutions.get()

rawDescription: Creating and reusing objects in python processes


code: y = [np.random.RandomState(seed).rand() for seed in seed_array]

rawDescription: Element-wise seed for random Numpy arrays