code:
print a
import xml.etree.ElementTree as ET
root = ET.fromstring(a)
[tuple([y.attrib['value'] for y in x]) for x in root[1]]

rawDescription: Parse comma separated list from xml in Python


code:
from itertools import groupby, chain, izip

ilen = lambda it: sum(1 for dummy in it)

def get_squeezed_counts(a):
    """
    squeeze a sequence to a sequnce of value/count.
    E.g. ['a', 'a', 'a', 'b'] --> [['a',3], ['b',1]]
    """
    return [ [ v, ilen(it) ] for v, it in groupby(a) ]

def get_element_dist(counts, index, depth):
    """
    For a given index in a "squeezed" list, return the distance (in the
    original-array) with a given depth, or None.
    E.g.
    get_element_dist([['a',1],['b',2],['c',1]], 0, depth=1) --> 1     # from a to first b
    get_element_dist([['a',1],['b',2],['c',1]], 1, depth=1) --> 2     # from first b to c
    get_element_dist([['a',1],['b',2],['c',1]], 0, depth=2) --> 3     # from a to c
    get_element_dist([['a',1],['b',2],['c',1]], 1, depth=2) --> None  # from first b to end of sequence
    """
    seen = set()
    sum_counts = 0
    for i in xrange(index, len(counts)):
        v, count = counts[i]
        seen.add(v)
        if len(seen) > depth:
            return sum_counts
        sum_counts += count
    # reached end of sequence before finding the next value
    return None

def get_squeezed_dists(counts, depth):
    """
    Construct a per-squeezed-element distance list, by calling get_element_dist()
    for each element in counts.
    E.g.
    get_squeezed_dists([['a',1],['b',2],['c',1]], depth=1) --> [1,2,None]
    """
    return [ get_element_dist(counts, i, depth=depth) for i in xrange(len(counts)) ]

def get_dists(a, depth):
    counts = get_squeezed_counts(a)
    squeezed_dists = get_squeezed_dists(counts, depth=depth)
    # "Unpack" squeezed dists:
    return list(chain.from_iterable(
        xrange(dist, dist-count, -1)
        for (v, count), dist in izip(counts, squeezed_dists)
        if dist is not None
    ))

print get_dists(['a','b','b','c','d','a','b','b','c','c','c','d'], depth = 1)
# => [1, 2, 1, 1, 1, 1, 2, 1, 3, 2, 1]
print get_dists(['a','a','a'], depth = 1)
# => []
print get_dists(['a','b','b','c','d','a','b','b','c','c','c','d'], depth = 2)
# => [3, 3, 2, 2, 2, 3, 5, 4]
print get_dists(['a','b','a', 'b'], depth = 2)
# => []

rawDescription: Distance to non consecutive elements in a numpy array


rawDescription: convert exponential to decimal in python

description: Convert exponential to decimal in python


code:
profile = FirefoxProfile('/home/test/.mozilla/firefox/mfgrtrtr.Default3')
browser = webdriver.Firefox(firefox_profile=profile)
browser.maximize_window()
with open("your_file_name") as in_file:
    for url in in_file:
        # get website
        browser.get(url.strip())
        # get current url
        print browser.current_url
        # get name & get phone number
        name = browser.find_element_by_class_name("name")
        print name.text
        phone = browser.find_element_by_class_name("phone")
        print phone.text

rawDescription: Python Selenium use list of Urls

description: Python Selenium use list of Urls


code:
from scipy.integrate import simps
from numpy import seterr, pi, sqrt, linspace, tan, cos, exp
N = 100
a = 2.
t = linspace(0, pi / 2, N)

x = tan(t)
f = cos(a * x) * exp(-x ** 2) * (1 + x ** 2)

print "numerical solution  = ", 2 * simps(f, t)

print "analytical solution = ",sqrt(pi) * exp(-a ** 2 / 4)

rawDescription: Integration on python

description: Integration on python


code:
for gridY in range(0,4):
    print gridY
    for gridX in range(0,4):
        print gridX

rawDescription: Python loop loops 2 times

description: Python loop loops 2 times


code:
from lxml import etree

tree = etree.XML('''<Placemark>
<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
<ExtendedData><SchemaData schemaUrl="#osa_20140910_areas">
    <SimpleData name="gml_id">219</SimpleData>
    <SimpleData name="ID">219</SimpleData>
    <SimpleData name="CODE">520</SimpleData>
    <SimpleData name="DISTRICT">102</SimpleData>
    <SimpleData name="STOR_DISTRICT">1</SimpleData>
    <SimpleData name="COUNTY">91</SimpleData>
    <SimpleData name="WHOLE_CODE">911102520.000000000000000</SimpleData>
    <SimpleData name="NAME">Calgary</SimpleData>
</SchemaData></ExtendedData>
</Placemark>''')
print tree
for simple_data in tree.xpath('//SimpleData[@name!="NAME"]'):
  simple_data.getparent().remove(simple_data)

print etree.tostring(tree)

rawDescription: Python remove all child elements except one


code:
#/usr/bin/python
import subprocess

cpu = subprocess.check_output(["top -n 5 -b | awk '/sshd/ {print  $9}'"], shell=True)
values = cpu.split()
print values

rawDescription: Python append specific times to list

description: Python append specific times to list


code:
import pandas as pd

df = pd.DataFrame({'A': ['foo', 'foo', 'foo', 'foo', 'bar', 'bar','bar','bar',], 'C_object':['str1', 'str2', 'str2', 'str2','str1', 'str1', 'str1', 'str2'], 'D_num': [10, 2, 2, 2, 10, 10, 10, 2]})

df2=pd.DataFrame({'C_object_len_unique': df.groupby('A')['C_object'].apply(lambda x: len(x.unique())), \
                  'C_object_most_common': df.groupby('A')['C_object'].agg(lambda x:x.value_counts().index[0]), \
                  'D_num_len_unique' : df.groupby('A')['D_num'].apply(lambda x: len(x.unique())), \
                  'D_num_most_common': df.groupby('A')['D_num'].agg(lambda x:x.value_counts().index[0]) \
                  }).reset_index()
print df2

rawDescription: Apply several functions to a grouped by dataframe in python


code:
"""
dumpimages.py
    Downloads all the images on the supplied URL, and saves them to the
    specified output file ("/test/" by default)

Usage:
    python dumpimages.py http://example.com/ [output]
"""

from BeautifulSoup import BeautifulSoup as bs
import urlparse
from urllib2 import urlopen
from urllib import urlretrieve
import os
import sys

def main(url, out_folder="/test/"):
    """Downloads all the images at 'url' to /test/"""
    soup = bs(urlopen(url))
    parsed = list(urlparse.urlparse(url))

    for image in soup.findAll("img"):
        print "Image: %(src)s" % image
        filename = image["src"].split("/")[-1]
        parsed[2] = image["src"]
        outpath = os.path.join(out_folder, filename)
        if image["src"].lower().startswith("http"):
            urlretrieve(image["src"], outpath)
        else:
            urlretrieve(urlparse.urlunparse(parsed), outpath)

def _usage():
    print "usage: python dumpimages.py http://example.com [outpath]"

if __name__ == "__main__":
    url = sys.argv[-1]
    out_folder = "/test/"
    if not url.lower().startswith("http"):
        out_folder = sys.argv[-1]
        url = sys.argv[-2]
        if not url.lower().startswith("http"):
            _usage()
            sys.exit(-1)
    main(url, out_folder)

rawDescription: Download image file from the HTML page source using python?


code:
import imghdr
from glob import glob

SUPPORTED_IMG_TYPES = 'gif jpeg png'.split()
IMG_SEQ_FILE = '/0/pictures.txt'
GLOB_PATTERN = '/0/picture%d.*'

def send_to_twitter(filename):
    print "sent %s to twitter" % filename
    return True

with open(IMG_SEQ_FILE, 'r+') as f:
    seq = int(f.readline().strip())
    for name in glob(GLOB_PATTERN % seq):
        img_type = imghdr.what(name)
        if img_type in SUPPORTED_IMG_TYPES:
            if send_to_twitter(name):
                f.seek(0)
                seq += 1
                f.write(str(seq))
                break
        else:
            if not img_type:
               print "%s is not an image file" % name
            else:
               print "%s unsupported image type: %s" % (name, img_type)

rawDescription: Twitter image bot posts every 2 hours

description: Twitter image bot posts every 2 hours


code:
source_files = ['source1.py', 'source2.py']
new_files = ['new1.py', 'new2.py']

old_lines, new_lines = set(), set()
for source in source_files:
   with open(source) as sf:
       old_lines.update(sf)
for new in new_files:
   with open(new) as nf:
       new_lines.update(nf)
for line in old_lines - new_lines:
    print line + " does not exist in refactored code"

rawDescription: Does file content fully exist in library files


rawDescription: Python - Loop through a 2D List - either append values to the current row or delete it


code:
import sys

# we need this to restore our sys.stdout later on
org_stdout = sys.stdout

# we open a file
f = open("test.txt", "w")
# we redirect standard out to the file
sys.stdout = f
# now everything that would normally go to stdout
# now will be written to "test.txt"
print "Hello world!\n"
# we have no output because our print statement is redirected to "test.txt"!
# now we redirect the original stdout to sys.stdout
# to make our program behave normal again
sys.stdout = org_stdout
# we close the file
f.close()
print "Now this prints to the screen again!"
# output "Now this prints to the screen again!"

# we check our file
with open("test.txt") as f:
    print f.read()
# output: Hello World!

rawDescription: Closing a file with stdout being written to it


code:
s = "C:\level1\level2\level3\level4\level5\level6\level7"
l = s.split('\\')
print l[4], l[5]

rawDescription: Finding string between 2 specific character


code:
@csrf_exempt
def pagelogin(request):

    context =RequestContext(request)

    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = authenticate(username=username, password=password)
        if user is not None:
                # Is the account active? It could have been disabled.
                if user.is_active:
                    # If the account is valid and active, we can log the user in.
                    # We'll send the user back to the homepage.
                    login(request, user)

                    #return HttpResponseRedirect('/home/')

                    return render(request,'index.html')
                else:
                    # An inactive account was used - no logging in!
                    return HttpResponse("Your  account is disabled.")
        else:
            # Bad login details were provided. So we can't log the user in.
            print "Invalid login details: {0}, {1}".format(username, password)
            return HttpResponse("Invalid login details supplied.")

    # The request is not a HTTP POST, so display the login form.
    # This scenario would most likely be a HTTP GET.
    else:
        # No context variables to pass to the template system, hence the
        # blank dictionary object...
        #messages.success(request, 'You have successfully logged in to your account')
        return render(request,'pagelogin.html')

rawDescription: Syntax Error on Login in django

description: Syntax Error on Login in django


code:
from junit_xml import TestSuite, TestCase

test_cases=[]

lines = open('testresults.log').readlines()

for line in lines:
    testresult = line.split(",")
    test_cases.append(testresult[0])

print test_cases

rawDescription: Append object to end of list

description: Append object to end of list


code:
# initialize the arrays outside the loop
cs = []
times = []

# loop for input until the user doesn't enter 'y'
inputgameoption = 'y'
while(inputgameoption == 'y'):
    creepscore = int(input("Finished a game? Type your creep score first then press enter.     Next, enter the duration of your game and press enter "))
    gameduration = int(input("Game duration should be rounded to the nearest minute, [ex. 24:25 becomes 24] "))

    # add their current inputs
    cs.append(creepscore)
    times.append(gameduration)

    # prompt to continue
    inputgameoption=input("Enter another game?")

print 'creepscores : ', cs
print 'times : ', times

rawDescription: How do I append an item to a list using the same variable name?

description: Append an item to a list using the same variable name


code:
someformat = 'Hello {}!'
someformat
print someformat.format('World')

rawDescription: Format a string later?

description: Format a string later


code:
n = 10
i = 2

mylist = ["0"] * n
mylist[i-1] = "1"
print " ".join(mylist)

rawDescription: python fixed string size

description: Python fixed string size


code:
import re
t="[abc/This is something] [testxyz] --include=*/ --include='some string/**' --exclude=*"
string = re.findall(r"\[(.*?)\]", t)
print string

#output ['abc/This is something', 'testxyz']

rawDescription: How to use \1 backreference in python regex properly?

description: Use \1 backreference in python regex properly


code:
import time
from Cocoa import NSSpeechSynthesizer
while 1:
    sp = NSSpeechSynthesizer.alloc().initWithVoice_(None)
    sp.startSpeakingString_("What would you like me to say?")
    say_1 = raw_input("What would you like me to say?")
    sp.startSpeakingString_(say_1)

    while sp.isSpeaking():    # loop until it finish to speak
        time.sleep(0.9)       # be nice with the CPU

    print 'done speaking'

rawDescription: How to make python wait until the previous task is finished?

description: Make python wait until the previous task is finished


code:
def moveEvent(self, event):
    self.checkVisibility()
    event.ignore()

def resizeEvent(self, event):
    self.checkVisibility()
    event.ignore()

def checkVisibility(self):
    print "Checking visibility"
    for page in self.getPages():
        if not page.visibleRegion().isEmpty():
            if page.was_visible:
                pass
            else:
                print page.page_number, "became visible"
                page.was_visible = True
                self.applyImageToPage(page)
        else:
            if page.was_visible:
                print page.page_number, "became invisible"
                page.was_visible = False
            else:
                pass
def applyImageToPage(self, page):
    print "applying image to page", page.page_number
    source = self.getSourcePage(self.getPageNumber(page))
    scale = self.display.scale
        # this is where the error occurs
    image = source.renderToImage(72 * scale, 72 * scale)
    pixmap = QtGui.QPixmap.fromImage(image)
    page.setPixmap(pixmap)

rawDescription: What is the most efficient way to display multiple pixmaps into a scroll area?


code:
try:
    import non_existant_module
except ImportError as e:
    print e.message # prints "No module named non_existant_module"

rawDescription: Organizing optional imports in Python

description: Organizing optional imports in Python


code: print "FINISHED"

rawDescription: How can I get to know if a running is finished in python syder environment?

description: Get to know if a running is finished in python syder environment


code:
#!/usr/bin/python

def interact():
    fn = raw_input("Enter a filename: ")
    return fn

def load_maze(fn):
    myfile = open(fn)
    maze_txt = myfile.read()
    myfile.close()
    return maze_txt

print load_maze(interact())

rawDescription: How to ask for a file in Python?

description: Ask for a file in Python


code:
"""
Naval Fate.

Usage:
  naval_fate ship new <name>...
  naval_fate ship <name> move <x> <y> [--speed=<kn>]
  naval_fate ship shoot <x> <y>
  naval_fate mine (set|remove) <x> <y> [--moored|--drifting]
  naval_fate -h | --help
  naval_fate --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
  --moored      Moored (anchored) mine.
  --drifting    Drifting mine.
"""

from docopt import docopt, DocoptExit
try:
    arguments = docopt(__doc__, version=1)
except DocoptExit:
    print __doc__

rawDescription: Display the complete docstring with docopt without -h


code:
txtLabel = "Put some value into dialog"
inputText, ok = QInputDialog.getText(self, "Dialog Name", txtLabel)
if ok:
    print ".........."
    print inputText

rawDescription: How to open a new window in pyside

description: Open a new window in pyside


code:
from threading import Thread
from time import sleep
import wx

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="TEST", size=(400, 400))
        self.Show()
        self.__someDialog = None
        self.__myThread = None

        self.__okButton = wx.Button(self, -1, "Press me")
        self.Bind(wx.EVT_BUTTON, self.__onOK)

        self.__myThread = Thread(target=self.__waitThenClose, name="Closer")
        self.__myThread.setDaemon(True)
        self.__myThread.start()

    def __onOK(self, evt):
        self.__someDialog = SomeDialog(self)
        self.__someDialog.ShowModal()

    def closeOpenDialogs(self, evt=None):
        lst = wx.GetTopLevelWindows()

        for i in range(len(lst) - 1, 0, -1):
            dialog = lst[i]
            if isinstance(dialog, wx.Dialog):
                print "Closing " + str(dialog)
                # dialog.Close(True)
                wx.CallAfter(dialog.Close)
                # sleep(1)
                # dialog.Destroy()

    def __waitThenClose(self):

        for x in range(0, 10):
            print "Sleeping..."
            sleep(1)

        wx.CallAfter(self.closeOpenDialogs)
        wx.CallAfter(self.Close, True)

class SomeDialog(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, id=-1, title='Some Dialog')
        self.SetSize((300, 300))
        self.__anotherDialog = None
        self.__okButton = wx.Button(self, -1, "Press me")

        self.Bind(wx.EVT_BUTTON, self.__onOK)
        wx.EVT_CLOSE(self, self.__on_btn_cancel)

    def __onOK(self, evt):
        self.__anotherDialog = AnotherDialog(self)
        self.__anotherDialog.SetWindowStyleFlag(wx.FRAME_FLOAT_ON_PARENT|wx.DEFAULT_DIALOG_STYLE)
        self.__anotherDialog.Show()

    def __on_btn_cancel(self, event):
        event.Skip()
        self.EndModal(wx.ID_CANCEL)

class AnotherDialog(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, id=-1, title='Another Dialog')
        self.SetSize((200, 200))
        wx.EVT_CLOSE(self, self.__on_btn_cancel)
        parent.Disable()

    def __on_btn_cancel(self, event):
        event.Skip()
        self.GetParent().Enable()
        # self.EndModal(wx.ID_CANCEL)

if __name__ == "__main__":

    app = wx.App()

    mainFrame = MainFrame()

    app.MainLoop()

rawDescription: wxPython - Automatically closing nested modal dialogs

description: WxPython - Automatically closing nested modal dialogs


code:
import re
string = 'file_name.c: 120'
find = re.findall("(\.[cm])*", string)
find = filter(None, find)
print find

rawDescription: Find expression in python

description: Find expression in python


code:
my_file = []
with open('file.csv','r') as fi:
    for i, row in enumerate(csv.reader(fi, delimiter=',', skipinitialspace=True)):
         if i % 3 == 2:
             my_file.append(row)
         else:
             my_file.append(row[:-3])

#if you want to save a new csv file
with open('new_file.csv', 'wb') as new_fi:
    new_fi_writer = csv.writer(new_fi, delimiter=', ')
    for line in my_file:
        new_fi_writer.writerow(line)

#alternatively (if you just want to print the lines)
for line in my_file:
    print ' '.join(line)

rawDescription: How to remove specific columns of every nth line of csv file using python

description: Remove specific columns of every nth line of csv file using python


code: print u[4]

rawDescription: print unicode characters in python interpreter

description: Print unicode characters in python interpreter


code:
from django.contrib.admindocs.views import load_all_installed_template_libraries
from django import template

app_libs = template.libraries.items()
builtin_libs = [(None, lib) for lib in template.builtins]

for module_name, library in builtin_libs + app_libs:
    for tag_name, tag_func in library.tags.items():
        print 'Tag: ', tag_name
    for filter_name, filter_func in library.filters.items():
        print 'Filter: ', filter_name

rawDescription: List of all reserved keywords in django templates?


code:
from sklearn.grid_search import GridSearchCV
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
# Build a classification task using 3 informative features
X, y = make_classification(n_samples=1000,
                           n_features=10,
                           n_informative=3,
                           n_redundant=0,
                           n_repeated=0,
                           n_classes=2,
                           random_state=0,
                           shuffle=False)

rfc = RandomForestClassifier(n_jobs=-1,max_features= 'sqrt' ,n_estimators=50, oob_score = True)

param_grid = {
    'n_estimators': [200, 700],
    'max_features': ['auto', 'sqrt', 'log2']
}

CV_rfc = GridSearchCV(estimator=rfc, param_grid=param_grid, cv= 5)
CV_rfc.fit(X, y)
print CV_rfc.best_params_

rawDescription: How to get Best Estimator on GridSearchCV (Random Forest Classifier Scikit)

description: Get Best Estimator on GridSearchCV (Random Forest Classifier Scikit)


code:
print "Want to play a game? Add numbers until you reach 1001!"
print "Current total is 0!"
# Don't name a variable `sum` -- it overrides the built-in
total = 0
# This will loop until `total` equals 1001
while total != 1001:
    store = raw_input("Enter a number!")
    num = int(store)
    # This is the same as `total=total+num`
    total += num
    print total
    # If we have gone over 1001, reset `total` to 0
    if total > 1001:
        print "Oops! Too Far! Start Again!"
        total = 0
# When we get here, the total will be 1001
print "Congratulations! You won!"

rawDescription: Adding Sums in Python

description: Adding Sums in Python


code:
lists = [["a", 1], ["b", 2], ["c", 3]]
lists1 = []

def check_if_two(r):
   if 2 not in r:
     lists1.append(r)

for s in lists:
   check_if_two(s)

print lists1

rawDescription: Delete the list in the list by specific value


code:
# hard-coded input, could also readlines() from a file
lines = ["- Blah, Blah, and",
         "  Blah, Blah, Blah,",
         "  Blah, Blah",
         "- Blah, Blah, and",
         "  Blah, Blah, Blah,",
         "  Blah, Blah"]

out = [(" " if line.startswith(" ") else "\n") + line.strip() for line in lines]
out = ''.join(out)[1:].split('\n')

print out

rawDescription: Concatenate strings read from file with python?


code:
import difflib

sourcename = 'xiaoshanwujzw'
destname = 'bindresult'

with open('%s.txt' % destname, 'w+') as destfile:

  # we read in the file so that on subsequent runs of this script, we
  # won't duplicate the lines.
  known_lines = set(destfile.readlines())

  with open('%s.txt' % sourcename) as sourcefile:
    for line in sourcefile:
      similar = False
      for known in known_lines:
        ratio = difflib.SequenceMatcher(None, line, known).ratio()
        if ratio > 0.8:
          print ratio
          print line
          print known
          similar = True
          break
      if not similar:
        destfile.write(line)
        known_lines.add(line)

rawDescription: python similar string removal from multiple files

description: Python similar string removal from multiple files


code:
while True:
    #Receiving from client
    data = conn.recv(4096)
    all_data += data
    print 'Received Message : ' + all_data
    if not data:
        print 'Final Received Message : ' + all_data
        headers = {'Content-Type':'text/plain'}
        req = urllib2.Request('<my server link>', None, headers)
        try:
            urllib2.urlopen(req, str(all_data))
        except urllib2.HTTPError, error:
            print "error posting the request " + error.reason
        break

rawDescription: Node-Red receiving response as JSON

description: Node-Red receiving response as JSON


code:
from scrapy.http import Request
from scrapy.selector import Selector
from scrapy.spider import BaseSpider

class MySpider(BaseSpider):
    name = "my_spider"
    domains = ['Auto']
    departments = {"All Departments": "0", "Apparel": "5438", "Auto": "91083", "Baby": "5427", "Beauty": "1085666",
                   "Books": "3920", "Electronics": "3944", "Gifts": "1094765", "Grocery": "976759", "Health": "976760",
                   "Home": "4044", "Home Improvement": "1072864", "Jwelery": "3891", "Movies": "4096", "Music": "4104",
                   "Party": "2637", "Patio": "5428", "Pets": "5440", "Pharmacy": "5431", "Photo Center": "5426",
                   "Sports": "4125", "Toys": "4171", "Video Games": "2636"}
    keyword = 'Auto'

    allowed_domains = ['ebay.com']

    def start_requests(self):
        for domain in self.domains:
            if domain in self.departments:
                url = 'http://www.ebay.com/search/search-ng.do?search_query=%s&ic=16_0&Find=Find&search_constraint=%s' % (self.keyword, self.departments.get(domain))
                print "YIELDING"
                yield Request(url)

    def parse(self, response):
        print "IN PARSE"
        sel = Selector(response)
        links = sel.select('//a[@class="ListItemLink"]/@href')
        for link in links:
            href = link.extract()[0]
            yield Request('http://www.ebay.com/' + href, self.parse_data)

    def parse_data(self, response):
        # do your actual crawling here
        print "IN PARSE DATA"

rawDescription: how to make start_urls in scrapy pick up url generated by another python function?

description: Make start_urls in scrapy pick up url generated by another python function


code:
def stats(inF):

    num_lines = 0
    num_words = 0
    num_chars = 0

    with open(inF, 'r') as input_file:
        for line in input_file:
            num_lines += 1
            line_words = line.split()
            num_words += len(line_words)
            for word in line_words:
                num_chars += len(word)

    print  'line count: %i, word count: %i, character count: %i' % (num_lines, num_words, num_chars)

stats('test.txt')

rawDescription: How to count all lines, words, and characters in a file

description: Count all lines, words, and characters in a file


code:
l1 = [1,2,3]
l2 = [4,5,6]
l3 = [7,8,9]
l4 = [10,11,12]

def increment_at_position_k(l1, l2, l3, l4, k):
    l1, l2, l3, l4 = list(l1), list(l2), list(l3), list(l4) # copy lists (otherwise the list is modified directly)
    l1[k] += 1
    l2[k] += 1
    l3[k] += 1
    l4[k] += 1
    return (l1, l2, l3, l4, k)

for iterations in [0]:
    results = []
    for i in [1,2]:
        ret = increment_at_position_k(l1, l2, l3, l4, i)
        results.append(ret)
    for res in results:
        local_l1, local_l2, local_l3, local_l4, k = res # unpack result
        #update global list
        l1[k] = local_l1[k]
        l2[k] = local_l2[k]
        l3[k] = local_l3[k]
        l4[k] = local_l4[k]
print l1
print l2
print l3
print l4

rawDescription: Multiprocessing two functions using and modifying the same list


code:
import wx

class Example(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)

        btn = wx.Button(self, label="press me")
        self.Sizer = wx.BoxSizer()
        self.Sizer.Add(btn)

        self.ctrl_down = False

        self.Bind(wx.EVT_KEY_UP, self.OnUpdateCtrlState)
        self.Bind(wx.EVT_KEY_DOWN, self.OnUpdateCtrlState)
        btn.Bind(wx.EVT_KEY_UP, self.OnUpdateCtrlState)
        btn.Bind(wx.EVT_KEY_DOWN, self.OnUpdateCtrlState)
        btn.Bind(wx.EVT_BUTTON, self.OnButton)

    def OnUpdateCtrlState(self, event):
        self.ctrl_down = event.ControlDown()
        print self.ctrl_down
        event.Skip()

    def OnButton(self, event):
        if self.ctrl_down:
            wx.MessageBox("control down")

app = wx.App(False)
app.TopWindow = f = Example()
f.Show()
app.MainLoop()

rawDescription: Check if key is held down, wxpython?

description: Check if key is held down, wxpython


code:
palindrome = "Rail at a liar"

def is_palindrome(text):
    text = text.lower()                               #Avoid case issues
    text = ''.join(ch for ch in text if ch.isalnum()) #Strips down everything but alphanumeric characters
    return text == text[::-1]

if is_palindrome(palindrome):
    print "Yes, it's a palindrome."
else:
    print "No, it's not a palindrome."

rawDescription: Remove members of a list in another list


code:
import json

class MongoEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, ObjectId):
            return str(obj)
        return json.JSONEncoder.default(self, obj)

print json.dumps(my_mong_obj, cls=MongoEncoder, indent=2)

rawDescription: custom json encoders in python/tornado

description: Custom json encoders in python/tornado


code:
import pandas as pd
import functools

class HTMLFormatter2(pd.core.format.HTMLFormatter):
    def write_th(self, s, indent=0, tags=None):
        super(HTMLFormatter2, self).write_th(s.upper(), indent, tags)

def replace_html_formatter(func, formatter):
    @functools.wraps(func)
    def wrapped_f(*args, **kw):
        try:
            old, pd.core.format.HTMLFormatter = pd.core.format.HTMLFormatter, formatter
            return func(*args, **kw)
        finally:
            pd.core.format.HTMLFormatter = old
    return wrapped_f

pd.DataFrame.to_html2 = replace_html_formatter(pd.DataFrame.to_html, HTMLFormatter2)

df = pd.DataFrame([[1,2,3],[4,5,6]], columns=["a", "b", "c"])
print df.to_html2()

rawDescription: How to modify a method of a class where this class inherits in at least 2 levels?

description: Modify a method of a class where this class inherits in at least 2 levels


code:
from netaddr import *
ip = IPNetwork('192.168.1.130/26')
supernet = ip.supernet(24)[0]
print list(supernet.subnet(26))

rawDescription: netaddr network ID of subnets

description: Netaddr network ID of subnets


code:
myhtml = """ <tag>I need this stuff</tag>
blah blah
<tag>I need this stuff too
</tag>
blah blah """
for item in myhtml.split("</tag>"):
  if "<tag>" in item:
      print item [ item.find("<tag>")+len("<tag>") : ]

rawDescription: Easy way to get data between tags of xml or html files in python?


code:
#!/usr/bin/python
from multiprocessing import Process,Queue
import random
import time

def printrand():
   #Checks whether Queue is empty and runs
   while q.empty():
      rand = random.choice(range(1,100))
      time.sleep(1)
      print rand

if __name__ == "__main__":
   #Queue is a data structure used to communicate between process
   q = Queue()
   #creating the process
   p = Process(target=printrand)
   #starting the process
   p.start()
   while True:
      ip = raw_input("Write something: ")
      #if user enters stop the while loop breaks
      if ip=="stop":
         #Populating the queue so that printramd can read and quit the loop
         q.put(ip)
         break
   #Block the calling thread until the process whose join()
   #method is called terminates or until the optional timeout occurs.
   p.join()

rawDescription: Python - run two commands at the same time


code:
class TreeModel(QtGui.QStandardItemModel):
    def setData(self, index, value, role):
        if role == QtCore.Qt.EditRole:
            print 'old:', self.itemFromIndex(index).text()
            print 'new:', value
        return QtGui.QStandardItemModel.setData(self, index, value, role)

rawDescription: How to get previous name for an item when changed in a QTreeView in PySide

description: Get previous name for an item when changed in a QTreeView in PySide


code:
from java.util import ArrayList

for interface in ArrayList.getInterfaces():
    print interface.getName()

rawDescription: How to get a list of interfaces from a java class in jython?

description: Get a list of interfaces from a java class in jython


code:
import cPickle as pickle # faster implementation

path = 'test.dat'
obj = ('Hello, world!', 123, {'x': 0})

# save to disk
with open(path, 'wb') as fp:
    pickle.dump(obj, fp)

# load from disk
with open(path, 'rb') as fp:
    obj = pickle.load(fp)

print obj

rawDescription: Is there an NSCoding-like facility in Python?


code:
soup = BeautifulSoup(html)
print soup.prettify()
print soup.find('div', { 'class' : 'foo' } ).h3.next_sibling.strip()

rawDescription: How to select only this text node using BeautifulSoup and Python?

description: Select only this text node using BeautifulSoup and Python


code:
def find_missing(series):
    A = map(int, series.split(' '))
    a, b, n, sumA = A[0], A[-1], len(A), sum(A)

    if (a+b)*n/2 == sumA:
        return None #no element missing

    return (a+b)*(n+1)/2-sumA

print find_missing("1 3 5 9")    #7
print find_missing("-1 1 3 5 9") #7
print find_missing("9 6 0")      #3
print find_missing("1 2 3")      #None
print find_missing("-3 1 3 5")   #-1

rawDescription: Is there a better way to write the following method in python?


code:
# with is like your try .. finally block in this case
with open('stats.txt', 'r') as file:
    # read a list of lines into data
    data = file.readlines()

print data
print "Your name: " + data[0]

# now change the 2nd line, note that you have to add a newline
data[1] = 'Mage\n'

# and write everything back
with open('stats.txt', 'w') as file:
    file.writelines( data )

rawDescription: Editing specific line in text file in python


code:
from thread import start_new_thread
from threading import Condition, Timer

class ConditionWithoutPolling():
    """Implements wait() with a timeout without polling.  Wraps the Condition
    class."""
    def __init__(self, condition):
        self.condition = condition
        self.wait_timeout_condition = Condition()

    def wait(self, timeout=None):
        """Same as Condition.wait() but it does not use a poll-and-sleep method
        to implement timeouts.  Instead, if a timeout is requested two new
        threads are spawned to implement a non-pol-and-wait method."""
        if timeout is None:
            # just use the original implementation if no waiting is involved
            self.condition.wait()
            return
        else:
            # this new boolean will tell us whether we are done waiting or not
            done = [False]

            # wait on the original condition in a new thread
            start_new_thread(self.wait_on_original, (done,))

            # wait for a timeout (without polling) in a new thread
            Timer(timeout, lambda : self.wait_timed_out(done)).start()

            # wait for EITHER of the previous threads to stop waiting
            with self.wait_timeout_condition:
                while not done[0]:
                    self.wait_timeout_condition.wait()

    def wait_on_original(self, done):
        """Waits on the original Condition and signals wait_is_over when done."""
        self.condition.wait()
        self.wait_is_over(done)

    def wait_timed_out(self, done):
        """Called when the timeout time is reached."""
        # we must re-acquire the lock we were waiting on before we can return
        self.condition.acquire()
        self.wait_is_over(done)

    def wait_is_over(self, done):
        """Modifies done to indicate that the wait is over."""
        done[0] = True
        with self.wait_timeout_condition:
            self.wait_timeout_condition.notify()

    # wrap Condition methods since it wouldn't let us subclass it ...
    def acquire(self, *args):
        self.condition.acquire(*args)
    def release(self):
        self.condition.release()
    def notify(self):
        self.condition.notify()
    def notify_all(self):
        self.condition.notify_all()
    def notifyAll(self):
        self.condition.notifyAll()

def test(wait_timeout, wait_sec_before_notification):
    import time
    from threading import Lock
    lock = Lock()
    cwp = ConditionWithoutPolling(Condition(lock))
    start = time.time()

    def t1():
        with lock:
            print 't1 has the lock, will wait up to %f sec' % (wait_timeout,)
            cwp.wait(wait_timeout)
        time_elapsed = time.time() - start
        print 't1: alive after %f sec' % (time_elapsed,)

    # this thread will acquire the lock and then conditionally wait for up to
    # timeout seconds and then print a message
    start_new_thread(t1, ())

    # wait until it is time to send the notification and then send it
    print 'main thread sleeping (will notify in %f sec)' % (wait_sec_before_notification,)
    time.sleep(wait_sec_before_notification)
    with lock:
        cwp.notifyAll()
        print 'notification sent, will continue in 2sec'
    time.sleep(2.0) # give the other time thread to finish before exiting

if __name__ == "__main__":
    print 'test wait() ending before the timeout ...'
    test(2.0, 1.0)

    print '\ntest wait() ending due to the timeout ...'
    test(2.0, 4.0)

rawDescription: Is there an alternative to the threading.Condition variables in python that better support timeouts without polling?


code:
parser = argparse.ArgumentParser()
print 'parser.prog:', parser.prog

rawDescription: How do you get the name of the program using argparse in python?

description: Get the name of the program using argparse in python


code:
for root, dirs, files in os.walk(folder):
    for name in files:
        (base, ext) = os.path.splitext(name)
        if ext in "csv":
            print os.path.join(root, name)

rawDescription: walking along and processing files in directory in python

description: Walking along and processing files in directory in python


code:
import string
import itertools

def alphanumeric_strings():
    for n in itertools.count():
        for t in itertools.product(string.ascii_letters + string.digits, repeat=n):
            yield ''.join(t)

for x in alphanumeric_strings():
    print x

rawDescription: Python: Cycle Through the Alphabet

description: Python: Cycle Through the Alphabet


code:
n = 3
w = n * [(None, "*")]
print w

rawDescription: Can you multiply words in python?

description: Multiply words in python


code:
linelist = ["a", "b", "", "c", "d", "e", "", "a"]
questionchunks = []
qlist = []
linelist.append('') # append an empty str at the end to avoid the other condn
for line in linelist:

    if (line != "" ):
        questionchunks.append(line)      # add the element to each of your chunk
    else:
        qlist.append(questionchunks)   # append chunk
        questionchunks = []       # reset chunk

print qlist

rawDescription: Split list of strings into list of sublists based on a string


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

from sklearn.datasets import make_classification
from sklearn.ensemble import ExtraTreesClassifier

# Build a classification task using 3 informative features
X, y = make_classification(n_samples=1000,
                           n_features=10,
                           n_informative=3,
                           n_redundant=0,
                           n_repeated=0,
                           n_classes=2,
                           random_state=0,
                           shuffle=False)

# Build a forest and compute the feature importances
forest = ExtraTreesClassifier(n_estimators=250,
                              random_state=0)

forest.fit(X, y)

importances = forest.feature_importances_ #array with importances of each feature

idx = np.arange(0, X.shape[1]) #create an index array, with the number of features

features_to_keep = idx[importances > np.mean(importances)] #only keep features whose importance is greater than the mean importance
#should be about an array of size 3 (about)
print features_to_keep.shape

x_feature_selected = X[:,features_to_keep] #pull X values corresponding to the most important features

print x_feature_selected

rawDescription: how can we get to know the selected and omitted features (columns ) names (header) using scikit-learn

description: How can we get to know the selected and omitted features (columns ) names (header) using scikit-learn


code:
l = ['exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi', 'netcdf',
    'pdcurses-devel',     'msvcrt', 'gdal-grass', 'iconv', 'qgis-devel',
    'qgis1.1', 'php_mapscript']

l.sort()# Alphabetizes l

if len(l) % 2 != 0:
    l.append(" ")

split = len(l)/2
l1 = l[0:split]
l2 = l[split:]
for key, value in zip(l1,l2):
    print "{0:<20s} {1}".format(key, value)

rawDescription: printing either a list or dictionary into columns

description: Printing either a list or dictionary into columns


code:
def read():
    fileObj = open("file.txt","r")
    lines   =  fileObj.readlines()
    for line in lines:
        print line,
    fileObj.close()

read()

rawDescription: Reading a file in Python, contents do not get displayed


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

plt.figure(figsize=(10,10))

ax1 = plt.subplot(221)
ax2 = plt.subplot(222)
ax4 = plt.subplot(224)

#Make some sample data as a sum of two elliptical gaussians:
x = range(200)
y = range(200)

X,Y = np.meshgrid(x,y)

def twoD_gaussian(X,Y,A=1,xo=100,yo=100,sx=20,sy=10):
    return A*np.exp(-(X-xo)**2/(2.*sx**2)-(Y-yo)**2/(2.*sy**2))

Z = twoD_gaussian(X,Y) + twoD_gaussian(X,Y,A=0.4,yo=75)

ax2.imshow(Z) #plot it

#calculate projections along the x and y axes for the plots
yp = np.sum(Z,axis=1)
xp = np.sum(Z,axis=0)

ax1.plot(yp,np.linspace(0,len(yp),len(yp)))
ax4.plot(np.linspace(0,len(xp),len(xp)),xp)

#Here is the business:
def image_statistics(Z):
    #Input: Z, a 2D array, hopefully containing some sort of peak
    #Output: cx,cy,sx,sy,skx,sky,kx,ky
    #cx and cy are the coordinates of the centroid
    #sx and sy are the stardard deviation in the x and y directions
    #skx and sky are the skewness in the x and y directions
    #kx and ky are the Kurtosis in the x and y directions
    #Note: this is not the excess kurtosis. For a normal distribution
    #you expect the kurtosis will be 3.0. Just subtract 3 to get the
    #excess kurtosis.
    import numpy as np

    h,w = np.shape(Z)

    x = range(w)
    y = range(h)

    #calculate projections along the x and y axes
    yp = np.sum(Z,axis=1)
    xp = np.sum(Z,axis=0)

    #centroid
    cx = np.sum(x*xp)/np.sum(xp)
    cy = np.sum(y*yp)/np.sum(yp)

    #standard deviation
    x2 = (x-cx)**2
    y2 = (y-cy)**2

    sx = np.sqrt( np.sum(x2*xp)/np.sum(xp) )
    sy = np.sqrt( np.sum(y2*yp)/np.sum(yp) )

    #skewness
    x3 = (x-cx)**3
    y3 = (y-cy)**3

    skx = np.sum(xp*x3)/(np.sum(xp) * sx**3)
    sky = np.sum(yp*y3)/(np.sum(yp) * sy**3)

    #Kurtosis
    x4 = (x-cx)**4
    y4 = (y-cy)**4
    kx = np.sum(xp*x4)/(np.sum(xp) * sx**4)
    ky = np.sum(yp*y4)/(np.sum(yp) * sy**4)

    return cx,cy,sx,sy,skx,sky,kx,ky

#We can check that the result is the same if we use the full 2D data array
def image_statistics_2D(Z):
    h,w = np.shape(Z)

    x = range(w)
    y = range(h)

    X,Y = np.meshgrid(x,y)

    #Centroid (mean)
    cx = np.sum(Z*X)/np.sum(Z)
    cy = np.sum(Z*Y)/np.sum(Z)

    ###Standard deviation
    x2 = (range(w) - cx)**2
    y2 = (range(h) - cy)**2

    X2,Y2 = np.meshgrid(x2,y2)

    #Find the variance
    vx = np.sum(Z*X2)/np.sum(Z)
    vy = np.sum(Z*Y2)/np.sum(Z)

    #SD is the sqrt of the variance
    sx,sy = np.sqrt(vx),np.sqrt(vy)

    ###Skewness
    x3 = (range(w) - cx)**3
    y3 = (range(h) - cy)**3

    X3,Y3 = np.meshgrid(x3,y3)

    #Find the thid central moment
    m3x = np.sum(Z*X3)/np.sum(Z)
    m3y = np.sum(Z*Y3)/np.sum(Z)

    #Skewness is the third central moment divided by SD cubed
    skx = m3x/sx**3
    sky = m3y/sy**3

    ###Kurtosis
    x4 = (range(w) - cx)**4
    y4 = (range(h) - cy)**4

    X4,Y4 = np.meshgrid(x4,y4)

    #Find the fourth central moment
    m4x = np.sum(Z*X4)/np.sum(Z)
    m4y = np.sum(Z*Y4)/np.sum(Z)

    #Kurtosis is the fourth central moment divided by SD to the fourth power
    kx = m4x/sx**4
    ky = m4y/sy**4

    return cx,cy,sx,sy,skx,sky,kx,ky

#Calculate the image statistics using the projection method
stats_pr = image_statistics(Z)

#Confirm that they are the same by using a 2D calculation
stats_2d = image_statistics_2D(Z)

names = ('Centroid x','Centroid y','StdDev x','StdDev y','Skewness x','Skewness y','Kurtosis x','Kurtosis y')

print 'Statistis\t1D\t2D'
for name,i1,i2 in zip(names, stats_2d,stats_pr):
    print '%s \t%.2f \t%.2f'%(name, i1,i2)

plt.show()

rawDescription: Image skewness& kurtosis in python

description: Image skewness& kurtosis in python


code:
@app.route('/upload', methods=['POST'])
def upload():
    if request.method == 'POST':
        file = request.files['file']
        extension = secure_filename(file.filename).rsplit('.', 1)[1]
        options = {}
        options['retry_params'] = gcs.RetryParams(backoff_factor=1.1)
        options['content_type'] = 'image/' + extension
        bucket_name = "gcs-tester-app"
        path = '/' + bucket_name + '/' + str(secure_filename(file.filename))
        if file and allowed_file(file.filename):
            try:
                with gcs.open(path, 'w', **options) as f:
                    f.write(file.stream.read())# instead of f.write(str(file))
                    print jsonify({"success": True})
                return jsonify({"success": True})
            except Exception as e:
                logging.exception(e)
                return jsonify({"success": False})

rawDescription: Uploading files to Google Cloud Storage with Flask


code:
key_file = open("key.txt", "r")
key = key_file.read().split(',')
text_file = open("plaintext.txt", "r")
text = text_file.read().split(' ')
key = map(int, key)

for i in key:
    print text[i - 1]

text_file.close()
key_file.close()

rawDescription: Print a list in a specific order in Python


code:
import sys
import subprocess

job_param1 = 12.5
job_param2 = 5.0
jobName = "python my_script.py %f %f" % (job_param1,job_param2)
cmd = "module load Python/2.7; sleep 0.2; %s" % jobName
echoArgs = ["echo", "-e", "'%s'" % cmd]
print(" ".join(echoArgs))
qsubArgs = ["qsub","-cwd"]
print(" ".join(qsubArgs))

wholeCmd = " ".join(echoArgs) + " | " + " ".join(qsubArgs)
out = subprocess.Popen(wholeCmd, shell=True, stdout=subprocess.PIPE)
out = out.communicate()[0]

jobId = out.split()[2]
print jobId

rawDescription: Submitting jobs with numeric arguments to QSUB using Python


rawDescription: automatic array calculus in python (numpy)

description: Automatic array calculus in python (numpy)


code: print os.popen('free -m').read()

rawDescription: Python converting from tuple to string

description: Python converting from tuple to string


code:
def worker():
    w = Database_connection()
    sql = "select stars from galaxy"
    for result in  w.db_call(sql, "x", "x", "x", "x"):
        if result:
            jobs = []
            startTime = datetime.now()
            for i in range(1):
               p = multiprocessing.Process(target=worker)
               jobs.append(p)
               p.start()
            print "Runtime: " + str(datetime.now()-startTime)

rawDescription: Multi-processing and Queue

description: Multi-processing and Queue


code:
print "Enter 5 items on shopping list"
for i in xrange(5): # range starts from 0 and ends at 5-1 (so 0, 1, 2, 3, 4 executes your loop contents 5 times)
    shopping = raw_input()
    mylist.append(shopping) # add input to the list

print mylist # at this point your list contains the 5 things entered by user

rawDescription: How to store user input into list on Python

description: Store user input into list on Python


code:
data = [
    {'name':'John', 'color':'red'},
    {'name':'Bob', 'color':'green'},
    {'name':'Tom', 'color':'blue'}
]
colors = ['blue', 'red', 'green']
result = []

for c in colors:
    result.extend([d for d in data if d['color'] == c])

print result

rawDescription: Return a list of dictionaries that match the corresponding list of values in python


code:
import numpy as np

for xi, yi in np.loadtxt('blah.txt'):
  print xi
  print yi

rawDescription: Parsing text in python

description: Parsing text in python


code:
keys = links.keys()
n = len(keys)
for i in range(n):
    thisKey = keys[i]
    nextKey = keys[(i + 1) % n]
    nextValue = links[nextKey]
    print thisKey, nextValue

rawDescription: How to custom handle iteration in python dictionary?

description: Custom handle iteration in python dictionary


code:
import time
import Queue
import threading
import pandas as pd
from collections import namedtuple

Event = namedtuple('Event', ['type', 'ask', 'time'])

eventQueue = Queue.Queue()

for i in xrange(150):
    eventQueue.put(Event(type='TICK', ask=i, time=i))

ts = []

def pullQueue(eventQueue):
    while True:
        try:
            event = eventQueue.get(False)
        except Queue.Empty:
            pass
        else:
            if event is not None:
                if event.type == 'TICK':
                    global ts
                    ts.append(pd.Series([event.ask], index=[event.time]))
        time.sleep(0.01)
evalThread = threading.Thread(target=pullQueue,args=(eventQueue, ))
evalThread.start()

time.sleep(0.01)
print ts

time.sleep(0.01)
print ts

rawDescription: Altering a global var within function - more specifically a pandas series


code:
base_canada_tax = 0.13
provinces = {'alberta': 0.05, 'ontario': base_canada_tax, 'new brunswick': base_canada_tax, 'nova scotia': base_canada_tax}

country = raw_input('What country are you from? ').lower()
total_price = int(raw_input('What was your total price? '))
if country == 'canada':
    province_in = raw_input('What province are you from? ').lower()
    total_price *=  1 + provinces.get(province_in, base_canada_tax)
print 'Your total price is {0}'.format(total_price)

rawDescription: A country that is not Canada

description: A country that is not Canada


code:
# Map names to functions. Note there is no need for the names
# to match the actual function name, and the functions can appear
# multiple times.
function_map = {
    'editContactInfo': editContactInfo,
    'editClientInfo': editClientInfo,
    'editAlias': editContactInfo,
}

# Get name of function to run etc. I'm assuming that the function
# name is stored in the variable function_name, and any parameters
# call it with is in the dictionary parameters (i.e., the functions
# all take keyword arguments).

# Do a lookup to get the function.
try:
    function = function_map[function_name]
except KeyError:
    # Replace with your own error handling code.
    print 'Function {0} does not exist.'.format(function_name)

# Call the function
result = function(**parameters)

rawDescription: CGI - Good Design Practice: setattr/eval/if-then?


code:
from xml.etree.ElementTree import parse, tostring
doc = parse('file.xml')
elem = doc.findall('original_spoken_locale')[0]
elem.text = 'new-value'
print tostring(doc.getroot())

rawDescription: How to replace the text inside an XML element?

description: Replace the text inside an XML element


code:
import requests as rq
import re
from bs4 import BeautifulSoup as bs
r=rq.get('http://www.amazon.in/gp/product/0007950306/ref=s9_al_bw_g14_i1?pf_rd_m=A1VBAL9TL5WCBF&pf_rd_s=merchandised-search-3&pf_rd_r=1XBKB22RGT2HBKH4K2NP&pf_rd_t=101&pf_rd_p=798805127&pf_rd_i=4143742031',stream=True)

for chunk in r.iter_content(chunk_size=1024):
    if chunk:
        data = chunk
        soup=bs(data)
        elem=soup.find_all('li',attrs={'id':'SalesRank'})
        if elem!=[]:
            s=re.findall('#[\d+,*]*\sin',str(elem[0]))
            print s[0].split()[0]
            break

rawDescription: Extracting text/numbers from HTML list using Python requests and lxml


code:
expected_query_seq = ""
for record in records:
   for alignment in record.alignments:
                hits = sorted((hsp.query_start, hsp.query_end, hsp.sbjct_start, hsp.sbjct_end, alignment.title, hsp.query, hsp.sbjct)\
                               for hsp in alignment.hsps)
                for q_start, q_end, sb_start, sb_end, title, query, sbjct in hits:
                      print title
                      print 'The query starts from position: ' + str(q_start)
                      print 'The query ends at position: ' + str(q_end)
                      print 'The hit starts at position: ' + str(sb_start)
                      print 'The hit ends at position: ' + str(sb_end)
                      print 'The  query is: ' + query
                      print 'The hit is: ' + sbjct

                      expected_query_seq += str(query[q_start:q_end])
print expected_query_seq

rawDescription: Concatenating subsets of blast hits according to their position to get complete hits


code:
"""
Create an G{n,m} random graph with n nodes and m edges
and report some properties.

This graph is sometimes called the Erd##[m~Qs-Rényi graph
but is different from G{n,p} or binomial_graph which is also
sometimes called the Erd##[m~Qs-Rényi graph.
"""
__author__ = """Aric Hagberg (hagberg@lanl.gov)"""
__credits__ = """"""
#    Copyright (C) 2004-2006 by
#    Aric Hagberg
#    Dan Schult
#    Pieter Swart
#    Distributed under the terms of the GNU Lesser General Public License
#    http://www.gnu.org/copyleft/lesser.html

from networkx import *
import sys

n=10 # 10 nodes
m=20 # 20 edges

G=gnm_random_graph(n,m)

# some properties
print "node degree clustering"
for v in nodes(G):
    print v,degree(G,v),clustering(G,v)

# print the adjacency list to terminal
write_adjlist(G,sys.stdout)

rawDescription: What is the most efficient graph data structure in Python?


code:
class MyBrowser(QWebPage):
    ''' Settings for the browser.'''

    def __init__(self):
        QWebPage.__init__(self)
        pass

    @staticmethod
    def userAgentForUrl(url=None):
        ''' Returns a User Agent that will be seen by the website. '''
        return "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 Safari/537.15"

print MyBrowser.userAgentForUrl()

rawDescription: Call function without creating an instance of class first


code:
#!/usr/bin/env python

import glob
import random
import os
import pandas

def rm_minus_rf(dirname):
    for r,d,f in os.walk(dirname):
        for files in f:
            os.remove(os.path.join(r, files))
        os.removedirs(r)

def create_testfiles(path):
    rm_minus_rf(path)
    os.mkdir(path)

    random.seed()
    for i in range(10):
        n = random.randint(10000,99999)
        for j in range(random.randint(0,20)):
            # year may repeat, doesn't matter
            year = 2015 - random.randint(0,20)
            with open("{}/{}-{}.csv".format(path, n, year), "w"):
                pass

def find_filesets(path="."):
    csv_files = {}
    for name in glob.glob("{}/*-*.csv".format(path)):
        # there's almost certainly a better way to do this
        key = os.path.splitext(os.path.basename(name))[0].split('-')[0]
        csv_files.setdefault(key, []).append(name)

    for key,filelist in csv_files.items():
        print key, filelist
        # do something with filelist
        create_merged_csv(key, filelist)

def create_merged_csv(key, filelist):
    with open('{}-aggregate.csv'.format(key), 'w+b') as outfile:
        for filename in filelist:
            df = pandas.read_csv(filename, header=False)
            df.to_csv(outfile, index=False, header=False)

TEST_DIR_NAME="testfiles"
create_testfiles(TEST_DIR_NAME)
find_filesets(TEST_DIR_NAME)

rawDescription: Merging CSVs with similar name python

description: Merging CSVs with similar name python


code:
import xlrd

book = xlrd.open_workbook('xlrd_test.xls')

for sheet in book.sheets():
    print sheet.row(0) # do stuff here - I'm printing the first line as example

# or if you need the sheet index for some purpose:
for shidx in xrange(0, book.nsheets):
    sheet = book.sheet_by_index(shidx)
    # would print 'Page N, first line: ....'
    print 'Page %d, first line: %s' % (shidx, sheet.row(0))

rawDescription: Recalling sheet names for a while loop

description: Recalling sheet names for a while loop


code:
import numpy
r=numpy.array([3,3,3])
k=numpy.array([numpy.nan,0,numpy.nan])
n=numpy.array([numpy.nan,0,0])
indxZeros=numpy.where(k==0)
indxNonZeros=numpy.where(k!=0)
d=numpy.empty(k.shape)
d[indxZeros]=0
d[indxNonZeros]=n[indxNonZeros]/k[indxNonZeros]
print d

rawDescription: Division by zero in numpy (sub)arrays

description: Division by zero in numpy (sub)arrays


code:
from subprocess import Popen, PIPE

cmd = [
    r"C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv", # in raw r"blah" string, you don't need to escape backslashes
    "solution.sln",
    "/build",
    "Debug|Win32"
]

p = Popen(cmd, stdout=PIPE, stderr=PIPE)
out = p.stdout.read() # reads full output into string, including line breaks

print out

rawDescription: When I write a python script to run Devenv with configure "Debug|Win32" it does nothing


code:
from optparse import OptionParser

def main():
    usage = "useage: %prog [options]"
    parser = OptionParser(usage)
    parser.add_option("-a", "--all", type="string", dest="search_and", help="find ALL lines in the file for the word1 AND word2")
    parser.add_option("-f", "--file", type="string", dest="filename", help="Name of file")
    (options, args) = parser.parse_args()

    if (options.search_and is None) or (options.filename is None):
        parser.error("not enough number of arguments")

    words = options.search_and.split(',')
    lines = open(options.filename).readlines()
    for idx, line in enumerate(lines):
        for word in words:
            if word.lower() in line.lower():
                print "The word, %s, was found at: Line %s" % (word, idx + 1)

if __name__ == "__main__":
    main()

rawDescription: optparse and strings

description: Optparse and strings


code:
from myClasses import MyClass
import inspect

source = inspect.getsource(MyClass)
print source

#class MyClass(object):
#    pass

rawDescription: Previewing the class or function definition source code in the interactive shell


code:
# first method
text = file.read()

if word in text:
    print "Yep!"

# second method
# goes through each line of the text checking
# more useful if you want to know where the line is

for i, line in enumerate(file):
    if word in line:
        print "Yep! Found %s on line: %s"%(word, i+1)

rawDescription: Python 'in' Operator Inexplicably Failing


code:
import pythoncom, pyHook

def OnKeyboardEvent(event):
  print 'MessageName:',event.MessageName
  print 'Message:',event.Message
  print 'Time:',event.Time
  print 'Window:',event.Window
  print 'WindowName:',event.WindowName
  print 'Ascii:', event.Ascii, chr(event.Ascii)
  print 'Key:', event.Key
  print 'KeyID:', event.KeyID
  print 'ScanCode:', event.ScanCode
  print 'Extended:', event.Extended
  print 'Injected:', event.Injected
  print 'Alt', event.Alt
  print 'Transition', event.Transition
  print '---'

# return True to pass the event to other handlers
  return True

# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()

rawDescription: Python Detect Keystrokes Sent to Another Application


code:
from dateutil import parser

date1= "Tue, 18 Jan 2015 15:00:37"
date2="Wed, 23 Jan 2015 15:00:37"
date1 = parser.parse(date1)
date2 = parser.parse(date2)
msg = {"Date": "Tue, 19 Jan 2015 15:00:37 +0000"}
if date2 >= parser.parse(msg['Date']).replace(tzinfo=None)  > date1:
     print msg['Date']

rawDescription: How to check how many emails were sent between dates?

description: Check how many emails were sent between dates


code:
import cx_Oracle
connstr = 'scott/tiger@127.0.0.1:1521/xe'
orcl = cx_Oracle.connect(connstr)

curs = orcl.cursor()
number_var = curs.var(cx_Oracle.NUMBER)
curs.execute(r"""BEGIN :my_cnt := 10; END;""", {'my_cnt':number_var})

print number_var.getvalue()

curs.close()
orcl.close()

rawDescription: How to return a SQLPLUS variable in Python using cxOracle

description: Return a SQLPLUS variable in Python using cxOracle


code:
def myreduce(func,collection,*args):
    """func takes 3 parameters.  The previous value,
       the current value, and the rest of the collection"""
    def new_func(x,y):
        try:
            return func(x[1],y[1],collection[y[0]:])
        except TypeError:
            return func(x,y[1],collection[y[0]:])
    return reduce(new_func,enumerate(collection),*args)

print myreduce(lambda x,y,rest:x+y+sum(rest),range(30))

rawDescription: Reducing collection of reduce in Python

description: Reducing collection of reduce in Python


code:
for i in range(10):
    temp = []
    temp.append(i)

print temp

rawDescription: Accessing the array outside the loop

description: Accessing the array outside the loop


code:
# reference like this: maze[y][x] or maze[row][col]
# with [0][0] starting at the top left
maze = [['b','a','a', 'a'],
        ['b','a','b', 'a'],
        ['b','a','b', 'b'],
        ['b','b','b', 'a'],
        ['b','a','b', 'a'],
        ['a','a','a', 'a']]

moves = {}

# Loop through all cells of the maze, starting in the top-left
for y, row in enumerate(maze):
    for x, value in enumerate(row):
#        print "y, x, val: ", y, x, value
        # for every cell, create an empty list of moves
        moves[y, x] = []
        # then if we can move from this cell
        # check each of its neighbours and if they are a 'b' add it
        # to the list of moves - assumes we can't move diagonally
        if value == 'b':
            if y - 1 > 0 and maze[y - 1][x] == 'b':
                moves[y, x].append((y - 1, x))
            if y + 1 < len(maze) and maze[y + 1][x] == 'b':
                moves[y, x].append((y + 1, x))
            if x - 1 > 0 and maze[y][x - 1] == 'b':
                moves[y, x].append((y, x - 1))
            if x + 1 < len(row) and maze[y][x+1] == 'b':
                moves[y, x].append((y, x+1))

print moves

rawDescription: Returning all "positions" of a list

description: Returning all "positions" of a list


code:
import thread
import threading

class Operation(threading._Timer):
    def __init__(self, *args, **kwargs):
        threading._Timer.__init__(self, *args, **kwargs)
        self.setDaemon(True)

    def run(self):
        while True:
            self.finished.clear()
            self.finished.wait(self.interval)
            if not self.finished.isSet():
                self.function(*self.args, **self.kwargs)
            else:
                return
            self.finished.set()

class Manager(object):

    ops = []

    def add_operation(self, operation, interval, args=[], kwargs={}):
        op = Operation(interval, operation, args, kwargs)
        self.ops.append(op)
        thread.start_new_thread(op.run, ())

    def stop(self):
        for op in self.ops:
            op.cancel()
        self._event.set()

if __name__ == '__main__':
    # Print "Hello World!" every 5 seconds

    import time

    def hello():
        print "Hello World!"

    timer = Manager()
    timer.add_operation(hello, 5)

    while True:
        time.sleep(.1)

rawDescription: Parallel while Loops in Python

description: Parallel while Loops in Python


code:
for state in state_list:
    print state['value'].split(".")[0], state.text

rawDescription: Manipulating BeautifulSoup's ResultSet list object


code:
prices = "€29,95 ipv €69,95  ezCast M2 Original "
re.findall(ur'[\$\\€](\d+(?:.\d{1,2})?)', prices.decode('utf-8'))

rawDescription: Python regex with unicode strings not working


code:
input = {'c1': ['f1', 'f2', 'f3'],
         'c2': ['f2'],
         'c3': ['f4'],
         'c4': ['f5', 'f4'],
         'c5': ['f3'],
         'c6': ['f6']}

result = []
founded = set()
for k,v in input.items():
    res = set()
    res.add(k)

    for l,m in input.items():
        if set(v) - set(m) != set(v):
            founded.add(l)
            res.add(l)
    for r in result:
        if res.issubset(r):
            res = None
            break
    if res: result.append(res)
print result

rawDescription: Python using groupby on a dicts

description: Python using groupby on a dicts


code:
if sys.argv[0] == __file__:
    print "It works!"

rawDescription: Is there a way to tell when a python file is loaded using PYTHONSTARTUP vs. being run as a script?


code:
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])

def create_gradient_brush():
    horGradient = QtGui.QLinearGradient(0, 0, 100, 0)
    verGradient = QtGui.QLinearGradient(0, 0, 0, 20)
    gradient = verGradient
    gradient.setColorAt(0.0, QtGui.QColor("blue"))
    gradient.setColorAt(1.0, QtGui.QColor("red"))
    brush = QtGui.QBrush(gradient)
    return brush

class Model(QtCore.QAbstractTableModel):

    # The cell size is most likely unavailable in the model, it could be
    # different per view, so we make a cell size-independent gradient.
    BG_BRUSH = create_gradient_brush()

    def __init__(self):
        QtCore.QAbstractTableModel.__init__(self)
        self.items = [[1, 'one', 'ONE'], [2, 'two', 'TWO'], [3, 'three', 'THREE']]

    def rowCount(self, parent=QtCore.QModelIndex()):
        return 3
    def columnCount(self, parent=QtCore.QModelIndex()):
        return 3

    def data(self, index, role):
        if not index.isValid(): return

        if role in [QtCore.Qt.DisplayRole, QtCore.Qt.EditRole]:
            return self.items[index.row()][index.column()]

        if role == QtCore.Qt.ForegroundRole:
            return QtGui.QColor("white")

        # BackgroundColorRole is obsolete, use BackgroundRole,
        # which returns a QBrush.
        if role == QtCore.Qt.BackgroundRole:
            return self.BG_BRUSH

def onClick(index):
    print 'clicked index:  %s'%index

tableModel=Model()
tableView=QtGui.QTableView()
tableView.setModel(tableModel)
tableView.clicked.connect(onClick)

tableView.show()
app.exec_()

rawDescription: How to assign QLinearGradient as QTableView items background color

description: Assign QLinearGradient as QTableView items background color


code:
d = {'one':1, 'two':2, 'three':3, 'four':4}
[d[key] for key in 'one', 'three']

rawDescription: Get elements from dict using multiple keys


code:
from time import time
def first():
    ftime = time()
    _foo = [x ** 2 for x in range(1, 101)]
    print "First", time()-ftime

def second():
    ftime = time()
    _foo = []
    for x in range(1, 101):
            _b=[x**2]
            _foo+=_b
    print "Second", time()-ftime

first()
second()
first()
second()
first()
second()

rawDescription: python:loop vs comprehension

description: Python:loop vs comprehension


code:
replace_dict = { 'bob': 'steve','foo': 'bar'}
item = 'this is a test string for {{foo}}, made using {{steve}}'
replace_lst = re.findall('\{\{([A-z]+)\}\}', item)
out = ''
for r in replace_lst:
    if r in replace_dict:
        item = item.replace('{{' + r + '}}', replace_dict[r])
print item

rawDescription: Find replacing dictionaries in regex

description: Find replacing dictionaries in regex


code:
class GUI (Tk):
    def __init__(self, **options):
        Tk.__init__(self, **options)
        self.varA = IntVar()
        self.varA.set(0)
        self.varB = IntVar()
        self.varB.set(0)
        checkbox_a = Checkbutton(self, text="Do A", variable=self.varA, command=self.onCheckButtonAPressed)
        checkbox_a.grid(column=1, row=7, sticky='WESN')
        checkbox_b = Checkbutton(self, text="Do B", variable=self.varB, command=self.onCheckButtonBPressed)
        checkbox_b.grid(column=2, row=4, sticky='WESN')

    def onCheckButtonBPressed(self):
        print self.varB.get()

    def onCheckButtonAPressed(self):
        print self.varA.get()

rawDescription: Python tkinter Checkboxes double check/uncheck


code:
def example():
    root = Tk()
    root.update()
    return root

print 'opening window'
window = example()
print 'closing window'
window.destroy()

rawDescription: Python Tkinter: Close a window opened by a function not running anymore


code:
hasht = \
{
    "A" : ["B", "D", "E"],
    "B" : ["C"],
    "C" : ["D", "E"],
    "D" : ["C", "E"],
    "E" : ["B"]
}

def recursive(start, finish):
    paths=[]
    def recursive_helper(path, finish):
        for i in hasht[path[-1]]:
            if i == finish:
                paths.append(path + [i])
                continue
            else:
                recursive_helper(path + [i], finish)
    recursive_helper([start], finish)
    return paths

print recursive("C", "C")

rawDescription: Generating a graph recursively - Python

description: Generating a graph recursively - Python


code:
#!/usr/bin/env py

from os.path import splitext
import sys

def parse_file_extension(gold_deck, depth):
    extention_list = [ "lvs", "cir", "spx"]
    fspi = open(gold_deck, 'r+')
    for data in fspi:
        if data.startswith('.include'):
            data = data.split()
            netlist_file,netlist_file_extension = splitext(data[1].strip("'"))
            if netlist_file_extension not in extention_list:
                netlist_file = parse_file_extension(netlist_file, depth+1)
            return netlist_file
    fspi.close()

def main(argv):
    gold_deck = "sample.txt"
    netlist_file = parse_file_extension(gold_deck, 0)
    print netlist_file

if __name__ == "__main__":
    sys.exit(main(sys.argv))

rawDescription: recursive search of File Extension in File

description: Recursive search of File Extension in File


code:
from datetime import datetime, timedelta

def gen_days( year ):
    start_date=datetime( year, 1, 1 )
    end_date=datetime( year, 12, 31 )
    d=start_date
    dates=[ start_date ]
    while d < end_date:
        d += timedelta(days=1)
        dates.append( d )
    return dates

if __name__=='__main__':
    d=gen_days( 2015 )
    print len(d)
    d2=gen_days( 2016 ) # leap year
    print len(d2)

rawDescription: How to generate range of dates: every day of a given year?

description: Generate range of dates: every day of a given year


code:
for i in range(0,100):
    n.append(i)
    if len(n)>10: n[:] = n[1:]
    print len(n) # will never go above 10

rawDescription: Removing unwanted data from lists

description: Removing unwanted data from lists


code:
cmd = ["java", "-Xmx256m", "-jar", "bin/HelloWorld.jar"]
cwd = r'/home/karen/sphinx4-1.0beta5-src/sphinx4-1.0beta5/'
p = subprocess.Popen(cmd, cwd=cwd, shell=True, stdout=subprocess.PIPE, bufsize=4024 )

out, err = p.communicate()
print out

rawDescription: Subprocess: not output

description: Subprocess: not output


code:
@classmethod
def save_params(cls, obj, filename):
    with open(filename, "w") as f:
        yaml.dump(obj, f, default_flow_style=False)

@classmethod
def load_params(cls, obj, filename):
    with open(filename, "r") as f:
        obj = yaml.load(f)
        print obj.aero_soot
        return obj

rawDescription: Loading serialised object from YAML and replacing current object with it


code:
# assemble the mail content
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
message = MIMEMultipart('alternative')
message.add_header('Subject', 'subject goes here')
# add a few more add_header calls here for things like "To", "Cc", "From"
message.attach(MIMEText('some code')) # plain text alternative
message.attach(MIMEText('
some code
', # html content
                        'html'))

# pipe the mail to sendmail
sendmail = os.popen('sendmail recipient@example.org', 'w')
sendmail.write(message.as_string())
if sendmail.close() is not None:
   print 'error: failed to send mail :-('

rawDescription: Python: send a mail in html with sendmail


code:
if len(sys.argv) < 2: print sys.modules[__name__].__doc__.strip()

cmd1 = sys.argv[1] if len(sys.argv) == 2 else raw_input('>')

def cmd_test():
   print "Run CMD test"

def cmd_something():
   print "run cmd somrthing"

try:
    globals()["cmd_%s"%cmd1]()
except KeyError:
    print "ERROR Unknown Command"

rawDescription: The best way of passing a single command to ad-hoc python scripts


code:
from StringIO import StringIO
import pycurl

url = 'http://www.google.com/'

storage = StringIO()
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.WRITEFUNCTION, storage.write)
c.perform()
c.close()
content = storage.getvalue()
print content

rawDescription: Retrieve the source code of a url using pycurl and a port number?


code:
import oauth2 as oauth
import time
import urllib2 as urllib

echo_base_url = 'http://pilot.echo360.com/ess/scheduleapi/v1'

consumer = oauth.Consumer(key ='xxxxx', secret='xxxx')
client = oauth.Client(consumer)
params = "<person><first-name>Jon</first-name><last-name>Doe</last-name><title>Super  Hero</title><email-address>jdoe17@echo360.com</email-address><block-alerts>false</block-alerts><time-zone>US/Eastern</time-zone><locale>en_US</locale><credentials><user-name>jdoe17@echo360.com</user-name><password>password</password></credentials><organization-roles><organization-role><organization-id>b1973c39-dc76-4cab-a4aa-3f9efd628df2</organization-id><role>role-name-admin</role></organization-role></organization-roles></person>"

resp, content = client.request(
                echo_base_url + "/people/",
                method = "PUT",
                body=params,
                headers={'Content-type': 'application/xml'}
                #force_auth_header=True
                )
print resp, content

rawDescription: How do I send a POST using 2-legged oauth2 in python?

description: Send a POST using 2-legged oauth2 in python


code:
import time
from threading import Timer

def timeout_handler(timeout=10):
    print time.time()
    timer = Timer(timeout, timeout_handler)
    timer.start()

timeout_handler()
while True:
    print "loop"
    time.sleep(1)

rawDescription: python -> time a while loop has been running

description: Python -> time a while loop has been running


code:
#!/bin/python

import inspect

def f(x):
  return x * x

print inspect.getsource(f)

rawDescription: Python: How to do a program that can show specific part of its own code?


code:
d = { "list01": [1, 3, 5],
      "list02": [2, 4, 6] }

def listProcessor(data, key):
    print key
    print data[key]

listProcessor(d, "list01")

rawDescription: How to get the literal value of a parameter passed to a Python function?

description: Get the literal value of a parameter passed to a Python function


code:
payload = request.json
print payload
#if using py3 print(payload)

rawDescription: How to log some data on console?

description: Log some data on console


code:
from Tkinter import *

root = Tk()

parentlist = ['one','two','three']
l1 = Listbox()
for item in parentlist:
    l1.insert(END, item)

l1.grid(row=0, column=0)
l2 = Listbox()

l2.grid(row=0, column=1)

def onselect(event, test):
    w = event.widget
    index = w.curselection()[0]
    print "You selected: {0} and test variable is {1}".format(index, test)
    l2.insert(END, index ) # Instead of returning it, why not just insert it here?

l1select = l1.bind('<<ListboxSelect>>',lambda event: onselect(event, 'Test'))

root.mainloop()

rawDescription: Tkinter: How to dynamically insert items to another listbox?


code:
import socket
import time

command = '<xml version="1.0" encoding="UTF-8"><header/><body><code><body/>'

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.1100.2.2", 54239))

s.send(command)

time.sleep(2)
resp = s.recv(3000)

print resp

rawDescription: How to send raw XML in Python?

description: Send raw XML in Python


code: conn = psycopg2.connect(database='mydb', async=1)

rawDescription: Python: Start psql query, don't wait for response


code:
def click():
    global clicks
    clicks += 1
    Number.config(text=clicks) # <------
    print 'ok', clicks

rawDescription: How to refresh the GUI window in Tkinter

description: Refresh the GUI window in Tkinter


code:
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'user': 'hhhhh@gmail.com', 'pass': '123456'})
opener.open('https://www.domains.com/store/verify/', login_data)
hh = opener.open('https://www.domains.com/store/verify/', login_data)
print hh.read()

rawDescription: Python How to post form with request

description: Python How to post form with request


code:
from multiprocessing import Process, current_process

def somefunc():
    print current_process().name

if __name__ == '__main__':
    p = Process(target=somefunc)
    p.start()
    print p.name

rawDescription: python multiprocessing - access the process name inside the function called with Process.start(target=func)

description: Python multiprocessing - access the process name inside the function called with Process.start(target=func)


code:
for path, num in dictionary.iteritems():
    filename = os.path.basename(path)
    print '{}: {}'.format(filename, num)

rawDescription: Is there a way to tidy up a dictionary when printing it


code:
#!/usr/bin/env python
from multiprocessing import Process
from multiprocessing import Queue
from multiprocessing import Pool
import multiprocessing

number_list = range(10)
new_number_list = []

def add_new_number_list(number, lock):
    lock.acquire()
    print "%s is added." % str(number)
    global new_number_list
    new_number_list.append(number)
    print new_number_list
    lock.release()

def check(number_list_q, lock):
    while True:
        if not number_list_q.empty():
            value = number_list_q.get()
            if 0 == value % 3:
                add_new_number_list(value, lock)
        else:
            break
    return new_number_list

def main():
    manager = multiprocessing.Manager()
    number_list_q = manager.Queue()
    for i in number_list:
        number_list_q.put(i)
    lock = manager.Lock()
    p = Pool()
    # return the apply_async result
    new_number_list = p.apply_async(check, args=(number_list_q, lock))
    p.close()
    p.join()
    # get the result (it's ready since we 'join'-ed)
    print new_number_list.get()
    print 'Done.'

if __name__ == '__main__':
    main()

rawDescription: Final output don't return correct result in my Python multiprocessing code


code:
import types
from opcode import *
_have_code = (types.MethodType, types.FunctionType, types.CodeType,
              types.ClassType, type)

def dis(x):
    """Disassemble classes, methods, functions, or code.

    With no argument, disassemble the last traceback.

    """
    if isinstance(x, types.InstanceType):
        x = x.__class__
    if hasattr(x, 'im_func'):
        x = x.im_func
    if hasattr(x, 'func_code'):
        x = x.func_code
    if hasattr(x, '__dict__'):
        items = x.__dict__.items()
        items.sort()
        for name, x1 in items:
            if isinstance(x1, _have_code):
                print >> out,  "Disassembly of %s:" % name
                try:
                    dis(x1)
                except TypeError, msg:
                    print >> out,  "Sorry:", msg
                print >> out
    elif hasattr(x, 'co_code'):
        disassemble(x)
    elif isinstance(x, str):
        disassemble_string(x)
    else:
        raise TypeError, \
              "don't know how to disassemble %s objects" % \
              type(x).__name__

def disassemble(co, lasti=-1):
    """Disassemble a code object."""
    code = co.co_code
    labels = findlabels(code)
    linestarts = dict(findlinestarts(co))
    n = len(code)
    i = 0
    extended_arg = 0
    free = None
    while i < n:
        c = code[i]
        op = ord(c)
        if i in linestarts:
            if i > 0:
                print >> out
            print >> out,  "%3d" % linestarts[i],
        else:
            print >> out,  '   ',

        if i == lasti: print >> out,  '-->',
        else: print >> out,  '   ',
        if i in labels: print >> out,  '>>',
        else: print >> out,  '  ',
        print >> out,  repr(i).rjust(4),
        print >> out,  opname[op].ljust(20),
        i = i+1
        if op >= HAVE_ARGUMENT:
            oparg = ord(code[i]) + ord(code[i+1])*256 + extended_arg
            extended_arg = 0
            i = i+2
            if op == EXTENDED_ARG:
                extended_arg = oparg*65536L
            print >> out,  repr(oparg).rjust(5),
            if op in hasconst:
                print >> out,  '(' + repr(co.co_consts[oparg]) + ')',
            elif op in hasname:
                print >> out,  '(' + co.co_names[oparg] + ')',
            elif op in hasjrel:
                print >> out,  '(to ' + repr(i + oparg) + ')',
            elif op in haslocal:
                print >> out,  '(' + co.co_varnames[oparg] + ')',
            elif op in hascompare:
                print >> out,  '(' + cmp_op[oparg] + ')',
            elif op in hasfree:
                if free is None:
                    free = co.co_cellvars + co.co_freevars
                print >> out,  '(' + free[oparg] + ')',
        print >> out

def disassemble_string(code, lasti=-1, varnames=None, names=None,
                       constants=None):
    labels = findlabels(code)
    n = len(code)
    i = 0
    while i < n:
        c = code[i]
        op = ord(c)
        if i == lasti: print >> out,  '-->',
        else: print >> out,  '   ',
        if i in labels: print >> out,  '>>',
        else: print >> out,  '  ',
        print >> out,  repr(i).rjust(4),
        print >> out,  opname[op].ljust(15),
        i = i+1
        if op >= HAVE_ARGUMENT:
            oparg = ord(code[i]) + ord(code[i+1])*256
            i = i+2
            print >> out,  repr(oparg).rjust(5),
            if op in hasconst:
                if constants:
                    print >> out,  '(' + repr(constants[oparg]) + ')',
                else:
                    print >> out,  '(%d)'%oparg,
            elif op in hasname:
                if names is not None:
                    print >> out,  '(' + names[oparg] + ')',
                else:
                    print >> out,  '(%d)'%oparg,
            elif op in hasjrel:
                print >> out,  '(to ' + repr(i + oparg) + ')',
            elif op in haslocal:
                if varnames:
                    print >> out,  '(' + varnames[oparg] + ')',
                else:
                    print >> out,  '(%d)' % oparg,
            elif op in hascompare:
                print >> out,  '(' + cmp_op[oparg] + ')',
        print >> out

def findlabels(code):
    """Detect all offsets in a byte code which are jump targets.

    Return the list of offsets.

    """
    labels = []
    n = len(code)
    i = 0
    while i < n:
        c = code[i]
        op = ord(c)
        i = i+1
        if op >= HAVE_ARGUMENT:
            oparg = ord(code[i]) + ord(code[i+1])*256
            i = i+2
            label = -1
            if op in hasjrel:
                label = i+oparg
            elif op in hasjabs:
                label = oparg
            if label >= 0:
                if label not in labels:
                    labels.append(label)
    return labels

def findlinestarts(code):
    """Find the offsets in a byte code which are start of lines in the source.

    Generate pairs (offset, lineno) as described in Python/compile.c.

    """
    byte_increments = [ord(c) for c in code.co_lnotab[0::2]]
    line_increments = [ord(c) for c in code.co_lnotab[1::2]]

    lastlineno = None
    lineno = code.co_firstlineno
    addr = 0
    for byte_incr, line_incr in zip(byte_increments, line_increments):
        if byte_incr:
            if lineno != lastlineno:
                yield (addr, lineno)
                lastlineno = lineno
            addr += byte_incr
        lineno += line_incr
    if lineno != lastlineno:
        yield (addr, lineno)

class FakeFile(object):
    def __init__(self):
        self.store = []
    def write(self, data):
        self.store.append(data)

a = lambda x : x
b = lambda x : x # True
c = lambda x : 2 * x
d = lambda y : 2 * y # True
e = lambda x : 2 * x
f = lambda x : x * 2 # True or False is fine, but must be stable
g = lambda x : 2 * x
h = lambda x : x + x # True or False is fine, but must be stable

funcs = a, b, c, d, e, f, g, h

outs = []
for func in funcs:
    out = FakeFile()
    dis(func)
    outs.append(out.store)

import ast

def outfilter(out):
    for i in out:
        if i.strip().isdigit():
            continue
        if '(' in i:
            try:
                ast.literal_eval(i)
            except ValueError:
                i = "(x)"
        yield i

processed_outs = [(out, 'LOAD_GLOBAL' in out or 'LOAD_DECREF' in out)
                            for out in (''.join(outfilter(out)) for out in outs)]

for (out1, polluted1), (out2, polluted2) in zip(processed_outs[::2], processed_outs[1::2]):
    print 'Bytecode Equivalent:', out1 == out2, '\nPolluted by state:', polluted1 or polluted2

rawDescription: Developing a heuristic to test simple anonymous Python functions for equivalency


code:
import twitter
api = twitter.Api()

# try a large page number:
i = 181

try:
    result = api.GetSearch("Avatar", page=i)
    print [s.text for s in result]
except:
    print 'page is out of range'

rawDescription: How do I know how many result pages can I get using python-twitter GetSearch() method

description: Know how many result pages can I get using python-twitter GetSearch() method


code:
import pexpect
import sys

child = pexpect.spawn('ftp ftp.openbsd.org')
child.expect('(?i)name .*: ')
child.sendline('anonymous')
child.expect('(?i)password')
child.sendline('pexpect@sourceforge.net')
child.expect('ftp> ')
child.sendline('cd /pub/OpenBSD/3.7/packages/i386')
child.expect('ftp> ')
child.sendline('bin')
child.expect('ftp> ')
child.sendline('prompt')
child.expect('ftp> ')
child.sendline('pwd')
child.expect('ftp> ')
print("Escape character is '^]'.\n")
sys.stdout.write (child.after)
sys.stdout.flush()
child.interact() # Escape character defaults to ^]
# At this point this script blocks until the user presses the escape character
# or until the child exits. The human user and the child should be talking
# to each other now.

# At this point the script is running again.
print 'Left interactve mode.'

# The rest is not strictly necessary. This just demonstrates a few functions.
# This makes sure the child is dead; although it would be killed when Python exits.
if child.isalive():
    child.sendline('bye') # Try to ask ftp child to exit.
    child.close()
# Print the final state of the child. Normally isalive() should be FALSE.
if child.isalive():
    print 'Child did not exit gracefully.'
else:
    print 'Child exited gracefully.'

rawDescription: Handling an interactive script from another script , using python


code:
import pefile

path_to_dll = r"path_to_your_dll"
pe =  pefile.PE(path_to_dll, fast_load=True)

# If the PE file was loaded using the fast_load=True argument, we will need to parse the data directories:
pe.parse_data_directories()
for entry in pe.DIRECTORY_ENTRY_IMPORT:
    print entry.dll

rawDescription: Identify whether a dll is built in debug or release with python


code:
import threading
import Queue

active_queues = []

class Worker(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.mailbox = Queue.Queue()
        active_queues.append(self.mailbox)

    def run(self):
        while True:
            data = self.mailbox.get()
            if data == 'shutdown':
                print self, 'shutting down'
                return
            print self, 'received a message:', data

    def stop(self):
        active_queues.remove(self.mailbox)
        self.mailbox.put("shutdown")
        self.join()

def broadcast_event(data):
    for q in active_queues:
        q.put(data)

t1 = Worker()
t2 = Worker()
t1.start()
t2.start()
broadcast_event("first event")
broadcast_event("second event")
broadcast_event("shutdown")

t1.stop()
t2.stop()

rawDescription: Python threading.Event() - Ensuring all waiting threads wake up on event.set()


code:
attrs = ['make', 'model', 'year', 'color']
print " ".join(map(lambda attr: getattr(car1, attr), attrs))

rawDescription: Printing Multiple Variables of a Python Class


code:
for text, type in pos_tag(word_tokenize("John's big idea isn't all that bad.")):
    if type == 'JJ':
        print 'text:', text
        print 'type:', type

rawDescription: What datatype is the output?

description: What datatype is the output


code:
import time

start_time = time.time()

# Your code here

stop_time = time.time()
print "Running time in sec:", stop_time - start_time

rawDescription: python -> password safety program

description: Python -> password safety program


rawDescription: How to print negative zero in Python

description: Print negative zero in Python


code:
class SoapCall(object):
    def __init__(self):
        self.client = None
        self.response = None

    def setup_client(self):
        credentials = {'username': 'stuff', 'password': 'stuff'}
        url = 'stuff'
        t = HttpAuthenticated(**credentials)
        if self.client is None:
            self.client = suds.client.Client(url, transport=t)

    def use_client(self):
        if self.client is None:
            self.client = self.setup_client()

        self.response = self.client.service.whatever
        print self.response

rawDescription: Python: Class definition without arguments


code:
def foo(condition):
   i=0
   while i < 10:
      if condition(i):
         print i
      i+=1

def is_even(x):
    return (x % 2) == 0

foo(is_even)

rawDescription: Looping over a condition defined as argument


code:
list1 = zip(list1[0::2], list1[1::2])
for elem1, elem2 in list1:
   print elem1, elem2

rawDescription: Looping through list in pairs or more

description: Looping through list in pairs or more


code:
string = "id: 10"
match = re.search("id: (\d+)", string)
if match:
    id = match.group(1)
    print id

rawDescription: Python regex to get returned value from a string


code:
import getpass, os, imaplib, email
from OpenSSL.crypto import load_certificate, FILETYPE_PEM

def getMsgs(servername="myimapserverfqdn"):
  usernm = getpass.getuser()
  passwd = getpass.getpass()
  subject = 'Your SSL Certificate'
  conn = imaplib.IMAP4_SSL(servername)
  conn.login(usernm,passwd)
  conn.select('Inbox')
  typ, data = conn.search(None,'(UNSEEN SUBJECT "%s")' % subject)
  for num in data[0].split():
    typ, data = conn.fetch(num,'(RFC822)')
    msg = email.message_from_string(data[0][1])
    typ, data = conn.store(num,'-FLAGS','\\Seen')
    yield msg

def getAttachment(msg,check):
  for part in msg.walk():
    if part.get_content_type() == 'application/octet-stream':
      if check(part.get_filename()):
        return part.get_payload(decode=1)

if __name__ == '__main__':
  for msg in getMsgs():
    payload = getAttachment(msg,lambda x: x.endswith('.pem'))
    if not payload:
      continue
    try:
      cert = load_certificate(FILETYPE_PEM,payload)
    except:
      cert = None
    if cert:
      cn = cert.get_subject().commonName
      filename = "%s.pem" % cn
      if not os.path.exists(filename):
        open(filename,'w').write(payload)
        print "Writing to %s" % filename
      else:
        print "%s already exists" % filename

rawDescription: How to fetch an email body using imaplib in python?

description: Fetch an email body using imaplib in python


code:
def func(container):
    container[0] = True

container = [False]
func(container)
print container[0]

rawDescription: Python: how to pass a reference to a function


code:
def test1(value):
    if value > 1: return True
    else: return False

def test2(value):
    if value % 4: return True
    else: return False

# using test1 as the default input value...
def main(test = test1):
    for i in range(20):
        if test(i):
            print i
            break

main(test1)
# prints 2

main(test2)
# prints 1

rawDescription: Variable function call in Python

description: Variable function call in Python


code:
import os
print os.name
import platform
platform.system()
platform.release()

rawDescription: Python: What OS am I running on?

description: Python: What OS am I running on


code:
graph = facebook.GraphAPI(user_short_lived_token_from_client)
app_id = 'app_id' # Obtained from https://developers.facebook.com/
app_secret = 'app_secret' # Obtained from https://developers.facebook.com/

# Extend the expiration time of a valid OAuth access token.
extended_token = graph.extend_access_token(app_id, app_secret)
print extended_token #verify that it expires in 60 days

rawDescription: How to extend facebook access token in python

description: Extend facebook access token in python


code: print os.lseek(fd, 0, os.SEEK_CUR)

rawDescription: Get position for file descriptor in Python


code:
import requests

session = requests.Session()

index_url   = 'http://s000.tinyupload.com/index.php'
upload_url  = 'http://s000.tinyupload.com/cgi-bin/upload.cgi?sid='

index_request = session.get(index_url)
PHPSESSID = index_request.cookies['PHPSESSID']

files = {'file': open('bitcoin.pdf', 'rb')}
r = requests.post(upload_url+PHPSESSID, files=files)

#Print "File upload finished" page
print r.text

#Print download link
import re
print re.search('http://s000\.tinyupload.com/\?file_id=[^<]+',r.text).group(0)

rawDescription: Upload a file using urllib.request tinyupload.com


code:
class newbot(object):
    def __init__(self):
        self.id = randomid()

bot1 = newbot()
bot2 = newbot()

# The two ids should be different, depending on your implementation of randomid().
print bot1.id
print bot2.id

rawDescription: Python multiple classes

description: Python multiple classes


code:
#!/usr/bin/python

import os
import sys

buf = sys.stdin.read(512)

print "Content-type: text/html\n\n";
print '<html>'
print '''
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="f">
<input type="submit">
</form>
'''
print buf

print '</html>'

rawDescription: Upload files without FieldStorage

description: Upload files without FieldStorage


code:
s ='''[22/Nov/2011 12:57:58] "GET /media/js/jquery-1.4.3.min.js HTTP/1.1" 304 0
[22/Nov/2011 12:57:58] "GET /media/js/fancybox/fancybox-x.png HTTP/1.1" 304 0
[22/Nov/2011 12:57:59] "GET /media/js/fancybox/fancybox-y.png HTTP/1.1" 304 0
[22/Nov/2011 12:57:59] "GET /media/js/fancybox/blank.gif HTTP/1.1" 304 0
[22/Nov/2011 12:57:59] "GET /ajax/pages/erlebnisse/ HTTP/1.1" 200 563
[22/Nov/2011 12:58:00] "GET /erlebnisse/alle-erlebnisse/ HTTP/1.1" 200 17114'''

import re

for m in re.findall('([-\w]+\.(?:jpg|gif|png))', s):
    print m

rawDescription: Searching images files with regular expressions


code:
try:
    import cPickle as pickle
except ImportError:
    import pickle

class MyClass(object):

    def __init__(self, file_name):
        self.array = []
        self.file_name = file_name
        self.load_data()

    def add_element(self, element):
        self.array.append(element)
        self.save_data()

    def load_data(self):
        try:
            with open(self.file_name, "r") as f:
                self.array = pickle.load(f)
        except IOError:
            pass

    def save_data(self):
        with open(self.file_name, "w") as f:
            pickle.dump(self.array, f)

def main():
    FILE_NAME = "test.pkl"
    a = MyClass(FILE_NAME)
    print "elements in array are", a.array
    for i in range(5):
        a.add_element(i)

if __name__ == "__main__":
    main()

rawDescription: python change source code while running

description: Python change source code while running


code:
ok = 0
for x in range(0,10):
    print "x=",x
    if ok == 1:
        ok = 0
        continue
    for y in range(0,20):
        print "y=",y
        if y == 5:
            ok = 1
            break

rawDescription: Python, next iteration of the loop over a loop


rawDescription: Manipulating python json dictionaries

description: Manipulating python json dictionaries


code:
import itertools

for start, stop in itertools.combinations(zipList, 2):
    print start, stop
    # now pass start, stop to your function

rawDescription: Iterating over Python objects

description: Iterating over Python objects


code:
for c in "Cadillac":
    n = ord(c)                  # convert the character to a number
    print '{0:08b}'.format(n)   # show the number with 8 binary digits

rawDescription: How should i understand built-in function bin(x)?


rawDescription: What is the difference between cross_val_score with scoring='roc_auc' and roc_auc_score?


code:
import urllib2

import caslib
import poster.encode
import poster.streaminghttp

opener = poster.streaminghttp.register_openers()
r, opener = caslib.login_to_cas_service(login_url, username, password,
                                        opener=opener)
params = {'file': open("test.txt", "rb"), 'name': 'upload test'}
datagen, headers = poster.encode.multipart_encode(params)
response = opener.open(urllib2.Request(upload_url, datagen, headers))
print response.read()

rawDescription: Python client for multipart form with CAS


code:
from BeautifulSoup import BeautifulSoup
import urllib

soup = BeautifulSoup(urllib.urlopen("http://www.ilrg.com/nlj250/"))

rows = soup.findAll(name='tr',attrs={'class':'small'})
for row in rows:
    number = row.find('td').text
    tds = row.findAll(name='td',attrs={'align':'left'})
    link = tds[0].find('a')['href']
    firm = tds[0].text
    office = tds[1].text
    attorneys = tds[3].text
    partners = tds[4].text
    associates = tds[5].text
    salary = tds[6].text
    print number, firm, office, attorneys, partners, associates, salary

rawDescription: How to select some urls with BeautifulSoup?

description: Select some urls with BeautifulSoup


code:
import GameLogic
import Rasterizer as r
r.showMouse(1)   #just in order to get the mouse visible within the game engine
print 'SetProperty.py\n'
cont=GameLogic.getCurrentController()
obj=cont.getOwner()
emptyOb = GameLogic.getCurrentScene().getObjectList()["OBEmpty"]
emptyOb.EmptyProp=obj
print 'object name, within emptyProp: ', emptyOb.EmptyProp

rawDescription: Problems getting object names

description: Problems getting object names


code:
import psycopg2, urllib2

image_url = 'http://example.com/theimage.jpg'
image_data = urllib2.urlopen(image_url).read()
media_id = 3

# triple quotes allows better string formating
sql = """
    with s as (
        select media_id
        from images
        where media_id = %(media_id)s
    )
    insert into images (media_id, data)
    select %(media_id)s, %(data)s
    where not exists (select 1 from s)
    returning media_id
;"""

# a parameter dictionary is clearer than a tuple
data_insert_image = {
    'media_id': media_id,
    'data': psycopg2.Binary(image_data)
}

conn = psycopg2.connect("host=localhost4 port=5432 dbname=db user=u password=p")
cursor = conn.cursor()
cursor.execute(sql, data_insert_image)
# fetchone returns a single tuple or null
result = cursor.fetchone()
conn.commit()

if result is not None:
    print 'Media Id {0} was inserted'.format(result[0])
else:
    print 'Media Id {0} already exists'.format(media_id)

rawDescription: What is the correct way to Insert an image into Postgres with python?


code:
print '%(language)s has %(number)03d quote types.' % \
      {"language": "Python", "number": 2}

rawDescription: What is this code: %(var)s ? Python maybe?


code:
import ctypes

lib = ctypes.CDLL('./hello.so')
lib.hello.restype = ctypes.POINTER(ctypes.c_char)

ptr = lib.hello("Frank")
print repr(ctypes.cast(ptr, ctypes.c_char_p).value)
lib.free_response(ptr)

rawDescription: ctypes segfault on OSX only

description: Ctypes segfault on OSX only