code:
from xml.dom import minidom

xmldoc = minidom.parse(datasource)

def get_xmltext(parent, subnode_name):
    node = parent.getElementsByTagName(subnode_name)[0]
    return "".join([ch.toxml() for ch in node.childNodes])

matchingNodes = [node for node in xmldoc.getElementsByTagName("article")
           if get_xmltext(node, "DOI") == '10.1016/B978-0-12-381015-1.00004-6']

for node in matchingNodes:
    print "title:", get_xmltext(node, "title")
    print "abstract:", get_xmltext(node, "abstract")

rawDescription: How to find a specific tag in an XML file and then access its parent tag with Python and minidom

description: Find a specific tag in an XML file and then access its parent tag with Python and minidom


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

for i,nexti in zip(a,a[1::]):
    print i,nexti

rawDescription: Print current value and next value during iteration in Python


code:
from tornado import ioloop
from tornado.httpclient import AsyncHTTPClient

def handle_request(response):
    if response.error:
        print "Error:", response.error
    else:
        print response.body
    ioloop.IOLoop.instance().stop()

http_client = AsyncHTTPClient()
http_client.fetch("http://www.google.com/", handle_request)
ioloop.IOLoop.instance().start()

rawDescription: How to immediately execute AsyncHTTPClient() without server (using interactive interpreter)

description: Immediately execute AsyncHTTPClient() without server (using interactive interpreter)


code:
from math import *

def f(x, y):
    return (3 * x) + (10 * y)

for x in range(5):
    for y in range(8):
        print "f(%d, %d) = %d" % (x, y, f(x, y))

rawDescription: 1 Using the substitution method for solving recurrences 1


code:
# Setup test data based on incomplete sample
bytes = "\x16\x00\x00\x00\xdc\x5a\x9f\xd2\x31\x04\xca\x01\xba\x81\x89\x1a\x81\xe2\xcd\x01\xba\x81\x89\x1a\x81\xe2\xcd\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\x00\x52\x00\x65\x00\x63\x00\x79\x00\x63\x00\x6c\x00\x65\x00\x2e\x00\x42\x00\x69\x00\x6e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
bytes = bytes + "\x00"*(592-len(bytes))

import struct
import codecs

# typedef struct _WIN32_FIND_DATA {
#   DWORD    dwFileAttributes;
#   FILETIME ftCreationTime;
#   FILETIME ftLastAccessTime;
#   FILETIME ftLastWriteTime;
#   DWORD    nFileSizeHigh;
#   DWORD    nFileSizeLow;
#   DWORD    dwReserved0;
#   DWORD    dwReserved1;
#   TCHAR    cFileName[MAX_PATH];
#   TCHAR    cAlternateFileName[14];

fmt = "<L3Q4L520s28s"

attrs, creation, access, write, sizeHigh, sizeLow, reserved0, reserved1, name, alternateName = struct.unpack(fmt, bytes)
name = codecs.utf_16_le_decode(name)[0].strip('\x00')
alternateName = codecs.utf_16_le_decode(alternateName)[0].strip('\x00')
print name

rawDescription: Parsing C Structs in Python

description: Parsing C Structs in Python


code:
import sys

InputValue = sys.argv[1] if len(sys.argv) > 1 else raw_input("Enter a temperature in Celsius: ")

Celsius = float(InputValue)
Fahrenheit = 9.0/5.0 * Celsius + 32
print Fahrenheit

rawDescription: How do I accept user input after the script in python?

description: Accept user input after the script in python


code:
from bs4 import BeautifulSoup, Comment
import re

input_text = ""

for line in open('output.txt'):
    input_text+=line

#extract all CSS comments
text = re.sub('\/*', '', input_text)
text = re.sub('\*/', '', text)

soup = BeautifulSoup(text)

#extract all HTML comments
comments = soup.findAll(text=lambda text:isinstance(text, Comment))
[comment.extract() for comment in comments]

print soup

rawDescription: Extract both CSS and HTML comments from HTML doc (Python)


code:
total = 0
FCS = arcpy.ListFeatureClasses()
FCS.sort()
for fc in FCS:
        if fc.startswith("Book"):
                getCountResult = arcpy.GetCount_management(fc)
                DM1 = int(getCountResult.getOutput(0))
                print "\t" + fc +" Record Count = "+  str(DM1)
                total += DM1
print "Book_** Record Count = " + str(total)
# and the number of feature classes (In case there were hundreds)
fcCount = len(FCS)
print '\n' '\t' "FeatureClasses found = " + str(fcCount)

rawDescription: How to sum of values in multiple featureclasses?

description: Sum of values in multiple featureclasses


code:
from numpy import *

signal = array([-2.,  8., -6.,  4.,  1., 0.,  3.,  5.])
from numpy.fft import *

f = fft(signal)

print f

f = rfft(signal)

print f

rawDescription: Where in Python can I find real_fft() these days? It's not in numpy.fft


code:
def userlogin(self, ID, password):
        try:
            statement = conn.execute('SELECT ID FROM USERS')

            for row in statement:
                if ID in row:  # compare one by one
                    print "%d" % ID
                    pas = conn.execute('SELECT PASSWORD FROM USERS WHERE ID = %d' % (ID))

                    if password in pas:
                        print "welcome user %d" % ID

                    else:
                        print "password incorrect!" # ID is matched
                    conn.close()
                    return
            print "Incorrect ID! If you are not a user, please register here."
        except IOError:
                print "Select statement could not execute!"
        finally:
                conn.close()

rawDescription: login function using sqlite in python

description: Login function using sqlite in python


code:
import socket
import time

def wait_for_socket(host, port, retries, retry_delay=30):
    retry_count = 0
    while retry_count <= retries:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex((host, port))
        if result == 0:
            print "Port is open"
            break
        else:
            print "Port is not open, retrying..."
            time.sleep(retry_delay)

rawDescription: To check whether AWS instance is up after reboot using python


code:
import clr
clr.AddReference('System')
from System import *

with IO.Ports.SerialPort("COM3") as serialPort:
    serialPort.BaudRate = 9600
    serialPort.DataBits = 8
    serialPort.Open()

    while 1:
        print serialPort.ReadLine()

rawDescription: IronPython / C# I can't reopen a serial port after Script has been run


code:
for i in range(0,5):
    print i

rawDescription: Python For loop and range function

description: Python For loop and range function


code:
import collections

lines = [[0.2, 'A'], [0.1, 'A'], [0.3, 'A'], [0.3, 'B'], [0.2, 'C'], [0.5, 'C']]
amounts = collections.defaultdict(float)
for amount, letter in lines:
    amounts[letter] += amount

for letter, amount in sorted(amounts.iteritems()):
    print amount, letter

rawDescription: Python: merging tally data

description: Python: merging tally data


code:
archive = tarfile.open(filepath, mode='r')
print os.path.commonprefix(archive.getnames())

rawDescription: With Python's 'tarfile', how can I get the top-most directory in a tar archive?


code:
d = {'apfel': 'apple', 'jablko': 'apple', 'ananas': 'pineapple', 'manzana': 'apple', 'pomme': 'apple', 'pina': 'pineapple'}
print d["pomme"]

rawDescription: Most efficient way to translate words in Python


code:
import pexpect
MW = "/usr/local/maple12/bin/maple -tu"
X = '1+1;'
child = pexpect.spawn(MW)
child.expect('#--')
child.sendline(X)
child.expect('#--')
out = child.before
out = out[out.find(';')+1:].strip()
out = ''.join(out.split('\r\n'))
print out

rawDescription: Grabbing the output of MAPLE via Python

description: Grabbing the output of MAPLE via Python


code:
#!/usr/bin/env python

from  PyQt4.QtGui import *
from  PyQt4.QtCore import *
from PyQt4.QtSvg import *
import sys

if __name__ == '__main__':

    app = QApplication(sys.argv)

    data = """... (my valid svg) ..."""

    svg = QSvgRenderer(QByteArray(data))
    qim = QImage(int(2000), int(1000), QImage.Format_ARGB32)
    painter = QPainter()

    painter.begin(qim)
    svg.render(painter)
    painter.end()

    print "null:", qim.isNull()
    qim.save('test2.png')

rawDescription: QSvgRenderer segmentation fault

description: QSvgRenderer segmentation fault


code:
import urllib2
import lxml.html
import itertools
url = "http://targetstudy.com/school/schools-in-chhattisgarh.html"
req = urllib2.Request(url, headers={ 'User-Agent': 'Mozilla/5.0' })
stuff = urllib2.urlopen(req).read().encode('ascii', 'ignore')
tree = lxml.html.fromstring(stuff)
print stuff

links = [url]
visited = []
while len(links) > 0:
    # take a link out of the list and mark it as visited
    link = links.pop()
    visited.append(link)

    # open the link and read the contents
    req = urllib2.Request(link, headers={ 'User-Agent': 'Mozilla/5.0' })
    stuff = urllib2.urlopen(req).read().encode('ascii', 'ignore')
    tree = lxml.html.fromstring(stuff)
    print stuff

    # for every link in the page
    for new_link in tree.xpath("(//ul[@class='pagination']/li/a)[last()]/@href"):
        # if link has not been visited yet and is not in the list to visit next
        if new_link not in links and new_link not in visited:
            # add the new link to the list of links to visit
            links.append(new_link)

rawDescription: Make Urllib2 move through pages

description: Make Urllib2 move through pages


code:
def load_module_attr (path):
    modname, attr = path.rsplit ('.', 1)
    mod = __import__ (modname, {}, {}, [attr])
    return getattr (mod, attr)

def my_view (request):
    model_name = "myapp.models.Song" # Get from command line, user, wherever
    model = load_module_attr (model_name)
    print model.objects.all()

rawDescription: is there similar syntax to php's $$variable in python

description: Is there similar syntax to php's $$variable in python


code:
import re
print re.sub(r'\[URL\](.*?)\[/URL\]',r'\1', "Hello here is a [URL]www.url.com[/URL] and it's great.")

rawDescription: Pattern matching and replacing in Python


code:
from lxml import etree
import requests

URL = "http://en.wikipedia.org/wiki/List_of_school_districts_in_Alabama"
response = requests.get(URL)

parser = etree.HTMLParser()
tree = etree.fromstring(response.text, parser)

for row in tree.xpath('//*[@id="mw-content-text"]/table[1]/tr'):
    links = row.xpath('./td[1]/a')
    if links:
        link = links[0]
        print link.text, link.attrib.get('href')

rawDescription: Access element using xpath?

description: Access element using xpath


code:
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction import DictVectorizer

training_set = [
  {'p1':'A', 'p2':'T', 'p3':'G', 'p4':'C', 'p5':'T',
   'p6':'A', 'p7':'C', 'p8':'T', 'p9':'G', 'p10':'A',
   'mass':370.2, 'temp':70.0},
  {'p1':'A', 'p2':'C', 'p3':'G', 'p4':'T', 'p5':'A',
   'p6':'C', 'p7':'T', 'p8':'G', 'p9':'A', 'p10':'T',
   'mass':400.3, 'temp':67.2},
]

target = [1, 0]

vec = DictVectorizer()
train = vec.fit_transform(training_set).toarray()

clf = RandomForestClassifier(n_estimators=1000)
clf = clf.fit(train, target)

# The following part fails.
test_set =   {
  'p1':'A', 'p2':'T', 'p3':'G', 'p4':'C', 'p5':'T',
  'p6':'A', 'p7':'C', 'p8':'T', 'p9':'G', 'p10':'A',
  'mass':370.2, 'temp':70.0}

test = vec.transform(test_set).toarray()
print clf.predict_proba(test)

rawDescription: Handling categorical features using scikit-learn


code:
lookup = {}
with open("champions.txt") as f:
    for line in f:
        line = line.rstrip().split('-', 1)
        if not line: continue # skip any blank lines

        name = line[0].strip()
        lookup[name.lower()] = name
        if len(line) == 2:  # any alternative names given?
            for item in line[1].split(','):
                lookup[item.strip()] = name

print 'lookup table:'
for alt_name, real_name in sorted(lookup.items()):
    print '{}: {}'.format(alt_name, real_name)
print

while True:
    print "which champ (Enter to quit): "  # ask user for input
    value = raw_input().lower()  # get the input and make it lowercase
    if not value: break

    real_name = lookup.get(value)
    if real_name:
        print 'found:', value, '-->', real_name
    else:
        print 'error: no match for', value

rawDescription: Need to match string to line in file in Python


code:
import dbus

T_SERVICE_NAME = "org.mpris.Totem"
T_OBJECT_PATH = "/Player"
T_INTERFACE = "org.freedesktop.MediaPlayer"

session_bus= dbus.SessionBus()

totem = session_bus.get_object(T_SERVICE_NAME, T_OBJECT_PATH)
totem_mediaplayer = dbus.Interface(totem, dbus_interface=T_INTERFACE)

print totem_mediaplayer.PositionGet()

rawDescription: Get current timepoint from Totem application


code:
lst = [
    [1,2,3],
    [1,2,4],
    [1,2,6],
    [2,5,4],
    [2,5,5],
    [3,7,9],
]

search = [1, None, 6]
new = [a for a in lst if
    any(y is not None and x != y for x, y in zip(a, search))
]

print new

rawDescription: Python list remove another list with unknown variable


code:
from itertools import chain

n = [3,5,7]
o = [4,10,8]
p = [4,10,5]

print sum(chain(n, o, p))
# 56

rawDescription: Easier way to add multiple list items?

description: Easier way to add multiple list items


code:
from binascii import unhexlify
unhexlify(''.join('ec a7 80 ec 97 ad 2f ea b5 ad ea b0 80 0a'.split()))
print unhexlify(''.join('ec a7 80 ec 97 ad 2f ea b5 ad ea b0 80 0a'.split())).decode('utf8')
print repr(unhexlify(''.join('ec a7 80 ec 97 ad 2f ea b5 ad ea b0 80 0a'.split())).decode('utf8'))

rawDescription: Get UTF-8 character codes from Python unicode string


code:
import subprocess as sp
PATH="ABCXYZ"
username = "admin"
password = "pass"
query = "select * from user;"
command = "/usr/local/%s/dbscript -u %s -p %s -q %s" % (PATH, username, password, query)
#Next line print '/usr/local/ABCXYZ/dbscript -u admin -p pass -q select * from user;' for me.
print command #
sp.Popen(command.split(), shell=True)

rawDescription: Pass a variable to a path/command in python


code:
import pickle
d = { "abc" : [1, 2, 3], "qwerty" : [4,5,6] }
afile = open(r'C:\d.pkl', 'wb')
pickle.dump(d, afile)
afile.close()

#reload object from file
file2 = open(r'C:\d.pkl', 'rb')
new_d = pickle.load(file2)
file2.close()

#print dictionary object loaded from file
print new_d

rawDescription: Easiest way to persist a data structure to a file in python?


code:
import sys
import MySQLdb

# Database configuration
config = {
    'host': 'localhost',
    'user': 'root',
    'passwd': 'password',
}

"""
Connect to MySQL, and attempt to select a database.
Then, query it.
"""
db = MySQLdb.connect(**config)

try:
    db.select_db('my_database_name')
except:
    print "Database selection failed."
    sys.exit(1)

cursor = db.cursor()

cursor.execute("""
    SELECT * FROM my_table_name;
""")

rows = cursor.fetchall()

# Do something with rows of data.
# ...

rawDescription: How to push data from MySQL to HTML using Python

description: Push data from MySQL to HTML using Python


code:
import Tkinter as tk
import ttk
import pickle

FILENAME = "save.pickle"

class Example(tk.Frame):
    def __init__(self, parent):
        self.create_widgets(parent)
        self.restore_state()

    def create_widgets(self, parent):
        tk.Frame.__init__(self, parent, borderwidth=9,relief="flat")

        self.previous_values = []

        l1 = tk.Label(self, text="Enter a mathematical expression:", anchor="w")
        l2 = tk.Label(self, text="Result:", anchor="w")
        self.expressionVar = tk.StringVar()
        self.expressionEntry = ttk.Combobox(self, textvariable=self.expressionVar, values=("No recent values",))
        self.resultLabel = tk.Label(self, borderwidth=2, relief="groove", width=1)
        self.goButton = tk.Button(self, text="Calculate!", command=self.calculate)

        l1.pack(side="top", fill="x")
        self.expressionEntry.pack(side="top", fill="x",padx = (12, 0))
        l2.pack(side="top", fill="x")
        self.resultLabel.pack(side="top", fill="x", padx = (12, 0), pady=4)
        self.goButton.pack(side="bottom", anchor="e", pady=4)

        self.expressionEntry.bind("<Return>", self.calculate)

        # this binding saves the state of the GUI, so it can be restored later
        root.wm_protocol("WM_DELETE_WINDOW", self.save_state)

    def calculate(self, event=None):
        expression = self.expressionVar.get()
        try:
            result = "%s = %s" % (expression, eval(expression))
            self.previous_values.append(expression)
            self.previous_values = self.previous_values[-8:]
            self.expressionVar.set("")
            self.expressionEntry.configure(values=self.previous_values)

        except:
            result = "invalid expression"

        self.resultLabel.configure(text=str(result))

    def save_state(self):
        try:
            data = {
                "previous": self.previous_values,
                "expression": self.expressionVar.get(),
                "result": self.resultLabel.cget("text"),
            }
            with open(FILENAME, "w") as f:
                pickle.dump(data, f)

        except Exception as e:
            print "error saving state:", str(e)

        root.destroy()

    def restore_state(self):
        try:
            with open(FILENAME, "r") as f:
                data = pickle.load(f)
            self.previous_values = data["previous"]
            self.expressionEntry.configure(values=self.previous_values)
            self.expressionVar.set(data["expression"])
            self.resultLabel.configure(text=data["result"])
        except Exception as e:
            print "error loading saved state:", str(e)

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

rawDescription: Save and Load GUI-tkinter

description: Save and Load GUI-tkinter


code:
import time
import signal

# determines if the loop is running
running = True

def cleanup():
  print "Cleaning up ..."

def main():
  global running

  # add a hook for TERM (15) and INT (2)
  signal.signal(signal.SIGTERM, _handle_signal)
  signal.signal(signal.SIGINT, _handle_signal)

  # is True by default - will be set False on signal
  while running:
    time.sleep(1)

# when receiving a signal ...
def _handle_signal(signal, frame):
  global running

  # mark the loop stopped
  running = False
  # cleanup
  cleanup()

if __name__ == '__main__':
  main()

rawDescription: Python - Infinite loop script running in background + restart


code:
def extract_np(psent):
  for subtree in psent.subtrees():
    if subtree.node == 'NP':
      yield ' '.join(word for word, tag in subtree.leaves())

cp = nltk.RegexpParser(grammar)
parsed_sent = cp.parse(tagged_sent)
for npstr in extract_np(parsed_sent):
    print npstr

rawDescription: NLTK: How do I traverse a noun phrase to return list of strings?


code:
import random
def rps():
    computerchoice = random.randint(1,3)
    if computerchoice == 1:
        computerchoice = "rock"
    elif computerchoice == 2:
        computerchoice = "paper"
    elif computerchoice == 3:
        computerchoice = "scissors"
    choice = raw_input("Rock, paper, or scissors?:")
    choice = choice.lower()
    if choice not in ["scissors","paper","rock"]: # check if choice is valid
        rps()
    print "The computer chose " + computerchoice + "."
    if choice == computerchoice:   # move on to your comparison checks
        choice + computerchoice
        print "It's a draw!"
    elif choice + computerchoice == "rockpaper":
        print "Computer wins, paper covers rock!"
    elif choice + computerchoice == "rockscissors":
        print "Player wins, rock crushes scissors!"
    elif choice + computerchoice == "paperrock":
        print "Player wins, paper covers rock!"
    elif choice + computerchoice == "paperscissors":
        print "Computer wins, scissors cut paper!"
    elif choice + computerchoice == "scissorsrock":
        print "Computer wins, rock crushes scissors!"
    elif choice + computerchoice == "scissorspaper":
        print "Player wins, scissors cuts paper!"
rps()

rawDescription: python code not working

description: Python code not working


code:
from OpenSSL.crypto import load_certificate, FILETYPE_PEM

cert_file_string = open("esx.crt", "rb").read()
cert = load_certificate(FILETYPE_PEM, cert_file_string)

sha1_fingerprint = cert.digest("sha1")
print sha1_fingerprint

rawDescription: How to capture the output of openssl in python

description: Capture the output of openssl in python


code:
latex_preamble = """
\usepackage{upquote}
"""

rawDescription: Sphinx pdf output: Apostrophes in python source are replaced by right single quotes


code:
# Start out False
found = False
for r in range(sheet.nrows):
    for c in range(sheet.ncols):
        cell = sheet.cell(r, c)
        if cell.value == city:
            loc = r
            found = True # set it to True if we find the value
# Now, the for loops are done.
if not found:
    print "not found"

rawDescription: Python if-else within loop not working (skips if)


code:
#!/usr/bin/python
import os.path
path = '/dir1/dir2/dir3/file.exe'
name = os.path.basename(path)
print name

rawDescription: read string backwards and terminate at first '/'

description: Read string backwards and terminate at first '/'


code:
for pos in range(len(hand)):
    card_count = hand.count(hand[pos])
    if card_count >= 2:
        print "Hand has", card_count, hand[pos], "'s"

rawDescription: how to check an array has cards in ascending order or two cards the same or three cards the same etc

description: Check an array has cards in ascending order or two cards the same or three cards the same etc


code:
for dirpath, dirnames, filenames in os.walk(mydir):
    if not dirnames:
        print dirpath, "has 0 subdirectories and", len(filenames), "files"

rawDescription: How to list only down-most directories in Python?

description: List only down-most directories in Python


code:
import collections

with open('strings.txt') as f:
    d = collections.Counter(s for line in f for s in line.split())

with open('count.txt', 'a') as f:
    for word in sorted(d.iterkeys()):
        string = "{:<18}\t\t\t{}\n".format(word, d[word])
        print string,
        f.write(string)

rawDescription: Why my code is recording into the file only when I run it second time?


code:
func_start = time.time()

print 'Function finishes in: %.3f ms' % ((time.time() - func_start) * 1000)

rawDescription: Use time.time() on Linux for profiling

description: Use time.time() on Linux for profiling


code:
import sys
subjects = ["History", "Science"]
topics_science = ["Light", "X-mas Exam Review"]
topics_history = ["Italian Renaissance"]
science_xmasreview = ["Q. What do we use to catogorize a habitat?",
                      "A. Damp or Dry, Hot or Cold, Windy or Calm, Dim or Bright.",
                      "Q. What is something that only eats plants?",
                      "A. Herbivore",
                      "Q. What are the properties of oxygen?"]

science_light = [
    "Q. What is an object the gives out light?",
    "A. Light source",
    "Q. What is the speed of light?",
    "A. 300 million meters per second.",
    "Q. What does transparent mean?",
    "A. Light can pass completely through a transparent material."]

history_renaissance = [
    "Q. What did Lorenzo do differently from Cosimo?",
    "A. Lorenzo made no effort to conceal his power",
    "Q. Why did the Pope want Lorenzo dead?",
    "A. Because the Pope wanted more power and saw Lorenzo as a threat.",
    "Q. Who did the Pazzi plot with to kill Lorenzo?",
    "A. Pope Sixtus IV"]

def printer(list):
    n = 0
    l = len(list)
    print
    while n < l:
        print list[n]  # <-- Changed this from a return to a print statement
        newline()
        n += 1
    while n == l:
        n += 1

def qanda(x):
    print
    print "Enter 1 for just questions"
    print "Enter 2 for just answers"
    print "Enter 3 for both"
    qa = raw_input("Enter number: ")
    qa = str(qa)
    if qa == '1':
        printer(x[::2])
    elif qa == '2':
        printer(x[1::2])
    elif qa == '3':
        printer(x)
    else:
        print "Not recognized"

def newline():
    raw_input()
    print

def subjectchoice():
    if subject == "1":
        print
        history()
    elif subject == "2":
        print
        science()
    else:
        print 'Not recognized.'
def science():
    print topics_science
    print "Enter 1 for Light"
    print "Enter 2 for X-mas Exam Review"
    topicchoice = raw_input("Enter number: ")
    topicchoice = str(topicchoice)
    if topicchoice == "1":
        qanda(science_light)
    elif topicchoice == "2":
        qanda(science_xmasreview)
    else:
        print "Not recognized"
        sys.exit
def history():
    print topics_history
    print "Enter 1 for Italian Renaissance"
    topicchoice = raw_input("Enter number: ")
    topicchoice = str(topicchoice)
    if topicchoice == "1":
        return qanda(history_renaissance)
    else:
        print "Not recognized"
        sys.exit()
print subjects
print "Enter 1 for History"
print "Enter 2 for Science"
subject = raw_input("Enter number: ")
subject = str(subject)
subjectchoice()

rawDescription: Why isn't this code returning these lists? Does this have to do with using 'return' rather than 'print'?


code:
columns = ['id', 'name']
print session.query(select(from_obj=User, columns=columns)).all()

rawDescription: Dynamically query a subset of columns in sqlalchemy


code:
def substring(string, substringList=[]):
    # Recursive function to create all the substrings
    #   of the given string

    if len(string) == 0:
        return substringList

    else:
        substringList.append(string)
        substring(string[1:], substringList)
        return substringList

print substring("bananas")

rawDescription: PYTHON Return a list from a recursive function


code: print "%.2f kg = %.2f lb = %.2f gal = %.2f l" % (var1, var2, var3, var4)

rawDescription: Python, print all floats to 2 decimal places in output


code:
"""
Intended to run on Windows resume. Scans for bluetooth devices and if a
particular device is not present, locks the computer.
"""
import bluetooth
import ctypes
import sys

# Bluetooth UID of "token" device
wanted = "XX:XX:XX:XX:XX:XX"

print "performing inquiry..."

# Hack.... if 0 devices are present, pybluez throws an exception
passed = False
try:
    discovered = bluetooth.discover_devices()
    if wanted in discovered:
        print "Found phone"
        passed = True
    else:
        passed = False
except:
    passed = False

if not passed:
    print "Locking"
    ctypes.windll.user32.LockWorkStation()

rawDescription: Interesting Python system utilities you have made?


code:
weekmask = 'Sun Mon Tue Wed Thu'
custombday = pd.offsets.CustomBusinessDay(weekmask=weekmask)

print pd.bdate_range('2016-5-10','2016-5-17', freq=custombday)

DatetimeIndex(['2016-05-10', '2016-05-11', '2016-05-12', '2016-05-15',
               '2016-05-16', '2016-05-17'],
              dtype='datetime64[ns]', freq='C')

rawDescription: pandas: bdate_range first set sunday as first day of week

description: Pandas: bdate_range first set sunday as first day of week


code: print a['description']

rawDescription: Python - Pyzabbix Output Format

description: Python - Pyzabbix Output Format


code:
#The structure of ListNode
class ListNode:
  def __init__(self, val):
    self.val = val
    self.next = None

# Supposes you have a circular linked list and you have a reference to head. You can do as follows to print the whole list.
current = head.next
while current != head: # stop when it comes back to head
  print current.val
  current = current.next

rawDescription: Circular list detection in Python?

description: Circular list detection in Python


code:
s='=?ISO-8859-1?Q?Mr_Kr=F6ber?= [mr.kroeber@gmail.com]'
from email.header import decode_header
decode_header(s)
u = u' '.join(w.decode(e or 'ascii') for w,e in decode_header(s))
u
print u

rawDescription: Receiving non-ascii emails with App Engine Python results in strange character encoding


code:
import mailbox
mails = mailbox.mbox(filename.mbox)
for message in mails: print message['subject']

rawDescription: Open source project for downloading mailing list archives preferably in Python


code:
def dist_metadata(setup_py):
    '''Get the dist object for setup.py file'''

    with open(setup_py) as f:
        d = f.read()

    try:
        # we have to do this with current globals else
        # imports will fail. secure? not really. A
        # problem? not really if your setup.py sources are
        # trusted
        exec d in globals(), globals()
    except SystemExit:
        pass

    return distutils.core._setup_distribution

rawDescription: How to extract dependencies from a PyPi package

description: Extract dependencies from a PyPi package


code:
doc = lxml.html.parse(url)
scripts = doc.xpath('//script')
for script in scripts:
    match = re.findall(r"DetailPage\.StateController\.setState\('parent_asin', '(.*)'\);", script.text)
    if match:
        print match[0]

rawDescription: Python Crawler - Check to see if a javascript line exists and if so, parse it


code:
text = 'aabbababacccssdd'

from itertools import groupby
print [''.join(g) for k, g in groupby(text)]
# # ['aa', 'bb', 'a', 'b', 'a', 'b', 'a', 'ccc', 'ss', 'dd']

rawDescription: How to group sentences of identical characters in python ( 'aabcdd' -> ['aa', 'b', 'c', 'dd'] )?

description: Group sentences of identical characters in python ( 'aabcdd' -> ['aa', 'b', 'c', 'dd'] )


code:
import re

js = open("yourfile.js", "r").readlines()[:2]

matcher_rex = re.compile(r'^var\s+(?P<varname>\w+)\s+=\s+"(?P<varvalue>[\w\s]+)";?$')
for line in js:
    matches = matcher_rex.match(line)
    if matches:
        name, value = matches.groups()
        print name, value

rawDescription: Read JavaScript variables from a file with Python


code:
from collections import defaultdict
try:
  from io import StringIO # 2.6+, 3.x
except ImportError:
  from StringIO import StringIO # 2.5

data = defaultdict(int)

#with open("filename", "r") as f: # if a real file
with StringIO("10\n5\n-1\n-1\n-1\n1\n1\n0\n2") as f:
  for line in f:
    data[int(line)] += 1

for number, count in data.iteritems():
  print number, "was found", count, "times"

rawDescription: How to use dict in python?

description: Use dict in python


code:
class Example(Document):
    pass

db = Example._get_db()
for collection_name in db.collection_names():
    print collection_name

rawDescription: How do I iterate over collections in a database in mongoengine?

description: Iterate over collections in a database in mongoengine


code:
from lxml import etree
xml_str = "<parent><child>text</child><child>other text</child></parent>"
root = etree.fromstring(xml_str)
print etree.tostring(root, pretty_print=True)

rawDescription: Specify newline character for cElementTree


code:
import sys

if len(sys.argv) != 2:
    print 'Add an input file as parameter'
    sys.exit(1)

print 'opening file: %s' % sys.argv[1]

with open(sys.argv[1]) as input, open('output', 'w+') as output:
    for line in input:
        if line is not None:
            if line == '\n':
                pass
            elif 'OK' in line:
                pass
            else:
                new_line = line.split(' ', 7)
                output.write('%s,%s,%s,%s / %s\n' % (new_line[1], new_line[2], new_line[3], new_line[4], new_line[6]))
                # just for checking purposes let's print the lines
                print '%s,%s,%s,%s / %s' % (new_line[1], new_line[2], new_line[3], new_line[4], new_line[6])

rawDescription: Python - Formatting specific data within text files


code:
import random

secret = random.randint(1,49)
attempts = 0
for attempts in range(7):
    guess=input("I have thought of a number between 1 and 50. you have to try and guess it: ")
    if secret==guess:
        print "Well Done! you guessed it "
    elif secret < guess:
        print "too high"
        guess=input(" Enter to have another go")
    elif secret > guess:
        print "too low"
        guess=input("Enter to have another go")
    if attempts == 6:
        print "Game Over"

rawDescription: Python guessing game not working

description: Python guessing game not working


code:
import math

def memoize(f):
    memo = {}
    def helper(x):
        if x not in memo:
            memo[x] = f(x)
        return memo[x]
    return helper

@memoize
def isprime (n):
    if n == 1:
        return False
    elif n == 2:
        return True
    else:
        for x in range (2, int(math.sqrt(n))+1):
            if n % x == 0:
                return False
                break
        else:
            return True

def factors (a):
    if a == 1:
        return []
    elif isprime(a):
        return [a]
    else:
        for x in range (2, int(math.sqrt(a))+1):
            if a % x == 0:
                return [x] + factors(a/x)

testnumber = int(input("Enter a number."))
print factors(testnumber)

rawDescription: Prime factorization python

description: Prime factorization python


code: [j for i in items for j in '-g', i]

rawDescription: Extending lists using list comprehension in python


code:
import requests
x = requests.get('http://www.google.com')
if x.status_code == 200:
    print x.content

rawDescription: Read label values and store into a variable


code:
import pythoncom, pyHook

def OnKeyboardEvent(event):
    print 'Ascii:', event.Ascii, chr(event.Ascii)
    print 'Key:', event.Key
    print 'KeyID:', event.KeyID
    print 'ScanCode:', event.ScanCode
    print 'Extended:', event.Extended

    return True #for pass through key events, False to eat Keys

hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()

rawDescription: Knowing if any key is pressed, wxPython

description: Knowing if any key is pressed, wxPython


code:
CONSUMER_KEY = '9pux1XcwXXXXXXXXXX'     # This is api_key
CONSUMER_SECRET = 'brtXoXEXXXXXXXXXXXXX'   # This is secret_key

USER_TOKEN = '27138ae8-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXb'   # This is oauth_token
USER_SECRET = 'ca103e23-XXXXXXXXXXXXXXXXXXXXXXXX7bba512625e'   # This is oauth_secret
RETURN_URL = 'http://localhost:8000'

from linkedin import linkedin
from oauthlib import *

# Define CONSUMER_KEY, CONSUMER_SECRET,
# USER_TOKEN, and USER_SECRET from the credentials
# provided in your LinkedIn application

# Instantiate the developer authentication class

authentication = linkedin.LinkedInDeveloperAuthentication(CONSUMER_KEY, CONSUMER_SECRET,
                                                      USER_TOKEN, USER_SECRET,
                                                      RETURN_URL, linkedin.PERMISSIONS.enums.values())

# Pass it in to the app...

application = linkedin.LinkedInApplication(authentication)

# Use the app....

g = application.get_profile()
print g

rawDescription: How to use linkedin API with python

description: Use linkedin API with python


code:
from sys import exit #To exit if the user does not want to try again
print 'Welcome to the game of NIM: '
def game():
    player=1
    state=21
    print 'The number of objects is now ',state
    while True:
        print 'Player ',player
        while True:
            move=input('What is your move? ')
            if move in [1,2,3] and move<state:
                break
            print 'Illegal move.'
        state=state-move
        print 'The number of objects is now ',state

        if state==1:
            print 'Player ',player,' wins!'
            break
        if player==1:
            player=2
        else:
            player=1
    if raw_input('Would you like to try again? ').lower().startswith('y'):
        return #To exit from the function cleanly
    sys.exit() #To exit from the program cleanly if the user does not want to try again
def run():
    while True:
        game()
if __name__ == '__main__':
    run()

rawDescription: Python 2.7.8 How to restart the game code


code: print "%s Whatever" % self.name

rawDescription: Getting the result of a particular thread in python


code:
import datetime

startTime = datetime.datetime.now() - datetime.timedelta(hours=2)
endTime = datetime.datetime.now() + datetime.timedelta(hours=4)

rest = endTime - datetime.datetime.now()
total = endTime - startTime
print "left: {:.2%}".format(rest.total_seconds()/total.total_seconds())

rawDescription: Python, datetime "time gap" percentage

description: Python, datetime "time gap" percentage


code:
import numpy as np

x = range(2,8)
y = [4,2,np.nan,7,np.nan,np.nan]

for a,b in zip(x,y):
    if str(b) == 'nan':
        x.remove(a)
        y.remove(b)

print x,y

rawDescription: Keras fitting ignoring nan values

description: Keras fitting ignoring nan values


code:
result = [dic[i] for i in tup]

print tuple(result)
("alpha", "bad", "change")

rawDescription: Is there any way to retrieve data from dictionary with a tuple?


code:
import socket, struct

packed_value = struct.pack('!I', 4294967295 & int('-1136577616'))
addr = socket.inet_ntoa(packed_value)

print addr

rawDescription: How to return the number in socket.inet_aton?

description: Return the number in socket.inet_aton


code:
from collections import defaultdict
from nltk import word_tokenize
import math

text = """Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor my old car has a nice diesel engine sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."""

word_index = defaultdict(list)

for i,word in enumerate(word_tokenize(text)):
    word_index[word].append(i)

# First occurence of 'car'
car = word_index['car'][0]
# Indices of 'diesel'
diesel = word_index['diesel']

nearest_diesel = min(diesel, key=lambda x:abs(x-car))

distance = math.fabs(car - nearest_diesel)

print distance

rawDescription: How to efficiently check neighbor elements for characteristic

description: Efficiently check neighbor elements for characteristic


code:
def pivot_nested_dict(nested_dict):
    stateDict = {}
    for i in nested_dict:
        dict2 = nested_dict[i]
        for j in dict2:
            if j not in stateDict:
                stateDict[j] = {i: dict2[j]}
            else:
                stateDict[j][i] = dict2[j]
    return stateDict

print pivot_nested_dict(inp)

rawDescription: How to update a key in a dictionary without overwriting the old value

description: Update a key in a dictionary without overwriting the old value


code:
if __name__=='__main__':
    print 'file1'

rawDescription: Is importing a file in python same as running that file?


code:
from StringIO import StringIO

def process(fd):
    print fd.read()

contents = 'The quick brown file'

buffer = StringIO()
buffer.write(contents)
buffer.seek(0)

process(buffer)  # prints "The quick brown file"

rawDescription: Can you convert file contents to a file object?

description: Convert file contents to a file object


code:
import string

def variation(stem, word):
    return word.lower() in [stem, stem + 'es', stem + 's']

def variations(sentence, stem):
    sentence = cleanPunctuation(sentence).split()
    return ( (i, w) for i, w in enumerate(sentence) if variation(stem, w) )

def cleanPunctuation(sentence):
    exclude = set(string.punctuation)
    return ''.join(ch for ch in sentence if ch not in exclude)

def firstVariation(sentence, stem):
    for i, w  in variations(sentence, stem):
        return i, w

sentence = "First coach, here another two coaches. Coaches are nice."

print firstVariation(sentence, 'coach')

# print all variations/forms of 'coach' found in the sentence:
print "\n".join([str(i) + ' ' + w for i,w in variations(sentence, 'coach')])

rawDescription: Find different realization of a word in a sentence string - Python


rawDescription: Print single variable per line from list


code:
import pycurl
import cStringIO

curl = pycurl.Curl()
buff = cStringIO.StringIO()
curl.setopt(pycurl.URL, 'http://example.org')
curl.setopt(pycurl.WRITEFUNCTION, buff.write)
curl.perform()

print "status code: %s" % curl.getinfo(pycurl.HTTP_CODE)

rawDescription: Is there a python library equivalent to php's curl_getinfo?


code:
from sklearn.feature_extraction.image import extract_patches

data = np.array([[0, 0, 1, 1, 0, 0],
                 [1, 0, 0, 1, 0, 0],
                 [1, 0, 1, 0, 0, 0],
                 [1, 1, 0, 0, 0, 0]])

patches = extract_patches(data, patch_shape=(2, 2), extraction_step=(2, 2))
non_zero_count_patches = (patches > 0).any(axis=-1).any(axis=-1).astype(int)
print non_zero_count_patches

rawDescription: Changing structure of numpy array enforcing given value


code:
import xml.etree.ElementTree as ET
root = ET.Element("a")
root.text = 'text1 ' #First Text in the Element a
b = ET.SubElement(root, "b")
b.text = 'text2' #Text in the first b
b.tail = ' text3 ' #Text immediately after the first b but before the second
b = ET.SubElement(root, "b")
b.text = 'text4'
b.tail = ' text5'
print ET.tostring(root)
#This prints text1 text2 text3 text4 text5

rawDescription: Multiple text nodes in Python's ElementTree? HTML generation


code:
while not curr_node.nextSibling().isNull():
     print curr_node, len(children)
     children.append(curr_node.nextSibling())
     curr_node = curr_node.nextSibling()

rawDescription: python QtWebkit: nextSibing doesn't return null or None

description: Python QtWebkit: nextSibing doesn't return null or None


code:
from lxml import etree

xml = '<doc>\n<foo>rain in spain</foo>\n</doc>'
root = etree.fromstring(xml)

print root.find('foo').sourceline # 2

root.append(etree.Element('bar'))
print etree.tostring(root)
print root.find('bar').sourceline # None

rawDescription: Any one have an example that uses the element.sourceline method from lxml.html


code:
import numpy as np

reference = np.array([np.nan, 1])
signal = np.array([2, 2])
idx = ~np.isnan(reference)

print np.all(reference[idx] < signal[idx])

rawDescription: Evaluate with NaN

description: Evaluate with NaN


code:
def connections(self):
    for group in self.widgets:
        self.widgets[group].clicked.connect(lambda g=group: self.openMenu(g))

    def openMenu(self,group):
        print group

rawDescription: Qt - Connect slot with argument using lambda


code:
#!/usr/bin/env python

import re

ret = {}

with open('log','r') as theFile:
    for line in theFile:
        match = re.search(r'Q \[.+\].+\(\d+\)([^\(]+)\(\d+\)([^\(]+)',line.strip())
        if match != None:
            key = ' '.join(match.groups())
            if key not in ret.keys():
                ret[key] = 1
            else:
                ret[key] += 1

for k in ret.keys():
    print '%s %d' % (k,ret[k])

rawDescription: Parse Microsoft DNS Debug logs

description: Parse Microsoft DNS Debug logs


code:
speed1 = .3    # must be between 0 and 1
speed2 = .7

time1 += speed1
if time1 >= 1.:
    time1 -= 1.
    print "speed1"

rawDescription: events happening at different speeds within a clock timer

description: Events happening at different speeds within a clock timer


code:
import os
import xlrd
import datetime

excel = os.path.join(os.getcwd(), 'test.xlsx')
book = xlrd.open_workbook(excel, 'r')
data = book.sheet_by_name('Sheet1')

for row in range(data.nrows)[1:]:
    excel_date = data.row_values(rowx=row)[0]
    print datetime.datetime(* xlrd.xldate_as_tuple(excel_date, datemode=0))

rawDescription: Python read datestring from excel file

description: Python read datestring from excel file


code:
import lxml.html

# Parse the HTML document
html = lxml.html.parse(open('/path/to/source/file').read())

# find div elements which contains a div child with class='list-item-data'
for parent in html.xpath("//div[@class='list-item-data']/.."):

    # get and check the date
    # note xpath returns a list of elements, here we assume only the first match is of
    # interest (based on the stated structure of the document)
    date = parent.xpath("./div[@class='list-item-actdate']/span")[0].text
    if not date.startswith("Today "):
        continue

    # print the link address
    href = parent.xpath(".//a")[0].attrib['href']
    print href

rawDescription: Select by date search result of a site with Python


code:
import itertools
choices = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"
for length in range(len(string), 20):
    itr = itertools.product(choices, repeat = length)
    if string != '' and length == len(string):
        itr = itertools.dropwhile(tuple(string).__ne__, itr)
    for entry in itr:
        string = ''.join(entry)
        print string

rawDescription: Continue creating list of the possible strings in python


code:
myxml = """\
<data>
    <element>
        <name>myname</name>
    </element>
    <element>
        3
        <name>another name</name>
    </element>
</data>
"""

dom = xml.dom.minidom.parseString(myxml)
nodelist = dom.getElementsByTagName("element")[1].getElementsByTagName("name")
for node in nodelist:
    print node.toxml()

rawDescription: xml.dom.minidom getting elements by tagname

description: Xml.dom.minidom getting elements by tagname


code:
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-p", nargs='?', const="mypass", default="default")

args = parser.parse_args()

if args.p:
    print args.p

rawDescription: [Python argparse]set the behaviour of an argument to be interactive if no value is given


code:
import tarfile
tar = tarfile.open("file.tgz")
for file in tar.getmembers():
    print file.name

rawDescription: Get the directory structure of a tgz file in python


code:
import uuid

o = {
    "name": "Unknown",
    "parent": "Uncategorized",
    "uuid": "06335e84-2872-4914-8c5d-3ed07d2a2f16"
}

print uuid.UUID(o['uuid']).hex

rawDescription: python: how to convert a valid uuid from String to UUID?

description: Python: how to convert a valid uuid from String to UUID


code:
atest = {'8P00WW': 0.5, '8P00WV': 0.2, '8P032I': 0.2, '8P00C8': 0.1} #total: 1.0
btest = {'8P0D37': 0.3, '8P0B5C': 0.25, '8P0CMG': 0.2}              #total: .75
ctest ={'8P03KK': 0.9, '8P0DVD': 0.4, '8P05JV': 0.05}               #total: 1.35

def fit_to_one(d):
    s = sum(d.values())
    for k,v in d.items():
        d[k] = v/s

    return d

print fit_to_one(atest)
print fit_to_one(btest)
print fit_to_one(ctest)

rawDescription: fit over/under dict percentage values to 100%?

description: Fit over/under dict percentage values to 100%


code:
import re
with open ("file.xml", "r") as myfile:
    data = myfile.read()

data = re.sub(r"android:name=\"android\.permission\.[A-Z\_]+", "xxxx", data)
print data

rawDescription: Search and replace a line in a .xml file in Python


code:
facebook_likes = [{
    "first_name": "John",
    "last_name": "Smith",
    "image_link": "link",
    "likes": ["foo"]
}, {
    "first_name": "John",
    "last_name": "Doe",
    "image_link": "link",
    "likes": ["foo", "bar"]
}]

for like in facebook_likes:
    print like
    print like["likes"]
    print like["likes"][0]

rawDescription: Organizing of Dynamic Lists of Lists

description: Organizing of Dynamic Lists of Lists


code:
def co_range(*args):
    _range = iter(xrange(*args))
    while True:
        sent = yield next(_range)
        if sent:
            _range = iter(xrange(*sent))
            yield

co = co_range(10)

for x in co:
    print x
    if x == 5:
        co.send((15,20))

0
1
2
3
4
5
15
16
17
18
19

rawDescription: Can I kick the counter in for loop (to skip some number of iterations)?

description: Kick the counter in for loop (to skip some number of iterations)


code:
fob = open('C:\Users\Joshua\Desktop\Database.txt','r')
logins = fob.readline()
fob.close()
#...
if accounter in logins:
    print 'You are in!!'
else:
    print 'Why did it not work?!'

rawDescription: How to check lines from a file in Python 2.7?

description: Check lines from a file in Python 2.7


code:
def createBucket():
    while True:
        c = boto.s3.connect_to_region(REGION)
        try:
            bucketname = raw_input("Enter bucket name ")
            if bucketname: # will be True for any non-empty string
                c.create_bucket(bucketname)
                break
            print("bucketname cannot be an empty string")
        except S3CreateError:
            print "bucket name already exist"
    return bucketname

rawDescription: show always an empty message until user enter a word

description: Show always an empty message until user enter a word


code:
import re

MEASUREMENT_RE = re.compile(r"\b\d{2}:\d{2} \d{2} \d{3}.\d{3} \d \d\b")

with open(source_file, mode="r") as f:
    print "\n".join(MEASUREMENT_RE.findall(f.read()))

rawDescription: Break line after specific expression and add to running list


code:
import itertools

mylist = ['W', 'X', 'Y', 'Z']
for pair in itertools.combinations(mylist, 2):
    print pair        # pair is a tuple of 2 elements

rawDescription: copying iterators and producing unordered self-cartesian product

description: Copying iterators and producing unordered self-cartesian product


code:
import re
def get_values(sql):
    sql = sql.upper()
    between_regex = '(\d+)\s+AND\s+(\d+)'
    ranges = [range(int(a), int(b)) for a, b in re.findall(between_regex, sql)]

    in_regex = '\[(.*)]'
    ranges += [[int(y) for y in x.split(',')] for  x in re.findall(in_regex, sql)]
    return [x for r in ranges for x in r]

print get_values("IN BETWEEN 30 AND 35")
print get_values("(in between 35 and 40) and (in [56,57,58])")
print get_values("(in between 30 and 35) and (IN BETWEEN 40 AND 45)")

#[30, 31, 32, 33, 34]
#[35, 36, 37, 38, 39, 56, 57, 58]
#[30, 31, 32, 33, 34, 40, 41, 42, 43, 44]

rawDescription: How can I get a list like this [30,31,32,33,34,35] when i give "in between 30 and 35" to a python function

description: Get a list like this [30,31,32,33,34,35] when i give "in between 30 and 35" to a python function


code:
import subprocess
import os.path

def start_editor(editor,file_name):

    if not os.path.isfile(file_name): # If file doesn't exist, create it
        with open(file_name,'w'):
            pass

    command_line=editor+' '+file_name # Add any desired command line args
    p = subprocess.Popen(command_line)
    p.wait()

file_name='test.txt' # Probably known from elsewhere
editor='notepad.exe' # Read from environment variable if desired

start_editor(editor,file_name)

with open(file_name,'r') as f: # Do something with the file, just an example here
    for line in f:
        print line

rawDescription: How do I invoke a text editor from a command line program like git does?

description: Invoke a text editor from a command line program like git does


code:
import numpy as np
m = 2.5
std = np.sqrt(10)
v = m + std*np.random.randn(1000)

print np.mean(v)  # 2.43375955445
print np.var(v)  # 9.9049376296

rawDescription: Numpy statistics, creating an array with certain statistics properties


code:
text = 'United States of America has many big cities like New York.'
pattern = '([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*(?:\s+[a-z]+)?(?:\s+[A-Z][a-z]+)+)'
result = re.findall(pattern,text)
print result
['United States of America', 'New York']

rawDescription: Get consecutive capital letters using python regex


code:
page=urllib2.urlopen(url)
soup=BeautifulSoup(page.read())

for a in soup.findAll('a'):
  if a['href'].endswith('.php'):
     print a['href']

rawDescription: Python BeautifulSoup Extracting PHP Links


code:
proc = subprocess.Popen(['perl', 'somescript.pl'], stdout=subprocess.PIPE)
buf = StringIO()
CHUNKSIZE = 1024  # how much to read at a time

while True:
    # do whatever other time-consuming work you want here, including monitoring
    # other processes...

    # this keeps the pipe from filling up
    buf.write(proc.stdout.read(CHUNKSIZE))

    proc.poll()
    if proc.returncode is not None:
        # process has finished running
        buf.write(proc.stdout.read())
        print "return code is", proc.returncode
        print "output is", buf.getvalue()

        break

rawDescription: nonblocking subprocesses in python

description: Nonblocking subprocesses in python


code:
print a.argmax(axis=1)
array([2, 0, 1])

rawDescription: Determine Index of Highest Value in Python's NumPy


code:
import struct
import pefile
import pydasm

pe = pefile.PE(filename)
print "PE Signature: " + hex(pe.VS_FIXEDFILEINFO.Signature)
print "Image Base: " + hex(pe.OPTIONAL_HEADER.ImageBase)
print "Address of EntryPoint: " + hex(pe.OPTIONAL_HEADER.AddressOfEntryPoint)
print "RVA Number and Size: " + hex(pe.OPTIONAL_HEADER.NumberOfRvaAndSizes)
print "Number of Sections within PE: " + hex(pe.FILE_HEADER.NumberOfSections)

for section in pe.sections:
    print 'Section Name: ' + (section.Name)

rawDescription: How do I find the offset and signature of the PE header in python?

description: Find the offset and signature of the PE header in python


code:
from heapq import heappush, heappop
import operator

def prob(ps):
    """ returns the probability that *not* all ps are True """
    return 1-reduce(operator.mul, ps)

def gen(ps):
    # turn each to a tuple
    items = ((x,) for x in sorted(ps, reverse=True))

    # create a priority queue, sorted by probability
    pq = [(prob(x),x) for x in items]

    # because you wanted this
    yield ()

    # as long as there are valid combinations
    while pq:
        # get the best un-yielded combination, the pq makes sure of that
        p, x = heappop(pq)
        yield x

        # generate all the combinations from this item
        for other in ps:

            # keeping the tuples sorted -> unique combinations
            if other < x[-1]:

                # create a new combination
                new = x+(other,)
                item = prob(new), new

                # add it to the queue
                heappush(pq,item)

a = [1, 0.1, 0.5]
print list(gen(a))

rawDescription: Algorithm to get every possible subset of a list, in order of their product, without building and sorting the entire list (i.e Generators)


code:
import base64
secret = base64.b64encode('bob')
secret
print base64.b64decode('Ym9i')

rawDescription: How to design user id for a website?

description: Design user id for a website


code:
class MyDecorator(object):
    def __init__(self, debug):
        self.debug = debug
    def myfunc(self, a):
        def decorator(fn):
            def new_fn():
                if self.debug:
                    print a
                fn()
            return new_fn
        return decorator
    def myotherfunc(self, x):
        def decorator(fn):
            def new_fn():
                if self.debug:
                    print x
                fn()
            return new_fn
        return decorator

rawDescription: Decorator that can take both init args and call args?


rawDescription: tz-aware datetime series producing UTC-based .date() output in pandas series apply(lambda) operation

description: Tz-aware datetime series producing UTC-based .date() output in pandas series apply(lambda) operation


code:
#!/usr/bin/python

import csv
import sys

#input number you want to search
number = raw_input('Enter number to find\n')

#read csv, and split on "," the line
csv_file = csv.reader(open('test.csv', "rb"), delimiter=",")

#loop through csv list
for row in csv_file:
    #if current rows 2nd value is equal to input, print that row
    if number == row[1]:
         print row

rawDescription: Python: searching csv and return entire row


code:
xmlstr = """<TopoDefs>
  <Device Name="SNB" LogicalType="CORE"
          IdCode="0x03886A21" Mask="0xf0000000">
    <Variable name="A0" value="0x52001321" type="GREEN" />
    <Variable name="A1" value="0x51001321" type="GREEN" />
    <Variable name="A2" value="0x03001321" type="GREEN" />
    <Variable name="A3" value="0x14001321" type="GREEN" />
    <Variable name="B0" value="0x5A001321" type="BLUE" />
    <Variable name="B1" value="0x51001321" type="BLUE" />
    <Variable name="B2" value="0x04001321" type="BLUE" />
    <Variable name="B3" value="0x05001321" type="BLUE" />
  </Device>
</TopoDefs>"""

import xml.etree.ElementTree as ET
document = ET.fromstring(xmlstr)

for dev in document.iter('Device'):
    for var in dev.iter('Variable'):
        var.set('value', 'new_value')

print ET.tostring(document)

rawDescription: Read and write value in xml based on attribute


code:
import json
print json.dumps({'4': 5, '6': 7}, sort_keys=True,
                 indent=4, separators=(',', ': '))

rawDescription: Saving dict as JSON so that they are human readable


code:
#!/usr/bin/env python

from PyQt4 import QtGui, QtCore
import sys

class RotatedButton(QtGui.QPushButton):
    def __init__(self, text, parent, orientation = "west"):
        super(RotatedButton,self).__init__(text, parent)
        self.orientation = orientation

    def paintEvent(self, event):
        painter = QtGui.QStylePainter(self)
        painter.rotate(90)
        painter.translate(0, -1 * self.width());
        painter.drawControl(QtGui.QStyle.CE_PushButton, self.getSyleOptions())

    def minimumSizeHint(self):
        size = super(RotatedButton, self).minimumSizeHint()
        size.transpose()
        return size

    def sizeHint(self):
        size = super(RotatedButton, self).sizeHint()
        size.transpose()
        return size

    def getSyleOptions(self):
        options = QtGui.QStyleOptionButton()
        options.initFrom(self)
        size = options.rect.size()
        size.transpose()
        options.rect.setSize(size)
        options.features = QtGui.QStyleOptionButton.None
        if self.isFlat():
            options.features |= QtGui.QStyleOptionButton.Flat
        if self.menu():
            options.features |= QtGui.QStyleOptionButton.HasMenu
        if self.autoDefault() or self.isDefault():
            options.features |= QtGui.QStyleOptionButton.AutoDefaultButton
        if self.isDefault():
            options.features |= QtGui.QStyleOptionButton.DefaultButton
        if self.isDown() or (self.menu() and self.menu().isVisible()):
            options.state |= QtGui.QStyle.State_Sunken
        if self.isChecked():
            options.state |= QtGui.QStyle.State_On
        if not self.isFlat() and not self.isDown():
            options.state |= QtGui.QStyle.State_Raised

        options.text = self.text()
        options.icon = self.icon()
        options.iconSize = self.iconSize()
        return options

class Main(QtGui.QFrame):
    def __init__(self):
        QtGui.QFrame.__init__(self)

        self.application = QtCore.QCoreApplication.instance()
        self.layout = QtGui.QHBoxLayout()
        self.button = RotatedButton("Hello", self, orientation="west")
        self.layout.addWidget(self.button)
        self.setLayout(self.layout)

if __name__ == '__main__':

    application = QtGui.QApplication(sys.argv)
    application.main = Main()
    application.main.show()
    sys.exit(application.exec_())

rawDescription: How to rotate a QPushButton?

description: Rotate a QPushButton


rawDescription: Compare list elements in python

description: Compare list elements in python


code:
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as lda
clf = lda(solver='eigen', shrinkage = 'auto')
clf.fit(tr_train, targ_train)
print clf.explained_variance_ratio_

description: Explained_variance_ratio_ in sklearn.discriminant_analysis


code:
with open('vec.txt') as f:
   file_string = f.read().strip()
   numbers = [float(s) for s in file_string.split()[1:]]
   print numbers

rawDescription: How can I get vector from the saved vector text file?

description: Get vector from the saved vector text file


code:
x = '\xa5'
print repr(x)

rawDescription: python CLI-like variable dump

description: Python CLI-like variable dump


code:
InputData = """5090071648,2013-06-05,2013-09-05,P000001,1133997,223.010,20,2013-09-10,104,xxxxxx,AP COMMENT,002091
301067,2013-09-06,2013-09-11,P000002,1133919,42.000,20,2013-10-31,103,xxxxxx,AP COMMENT,002143
301067,2013-09-06,2013-09-11,P000002,1133919,359.400,20,2013-10-31,103,xxxxxx,AP COMMENT,002143
301067,2013-09-06,2013-09-11,P000003,1133910,23.690,20,2013-10-31,103,xxxxxx,AP COMMENT,002143"""

OutD = {}
ValueD = {}

for Line in InputData.split('\n'):
    # commas in comments won't matter because we are joining after anyway
    Fields = Line.split(',')
    PO = Fields[3]
    Value = float(Fields[5])
    # set up the output string with a placeholder for .format()
    OutD[PO] = ",".join(Fields[:5] + ["{0:.3f}"] + Fields[6:])
    # add the value to the old value or to zero if it is not found
    ValueD[PO] = ValueD.get(PO,0.0) + Value

# the output is unsorted by default, but you could sort or preserve original order
for POKey in ValueD:
    print OutD[POKey].format(ValueD[POKey])

rawDescription: How to 'flatten' lines from text file if they meet certain criteria using Python?


code:
import time

def watch(fn, words):
    fp = open(fn, 'r')
    while True:
        new = fp.readline()
        # Once all lines are read this just returns ''
        # until the file changes and a new line appears

        if new:
            for word in words:
                if word in new:
                    yield (word, new)
        else:
            time.sleep(0.5)

fn = 'test.py'
words = ['word']
for hit_word, hit_sentence in watch(fn, words):
    print "Found %r in line: %r" % (hit_word, hit_sentence)

rawDescription: How to implement a pythonic equivalent of tail -F?

description: Implement a pythonic equivalent of tail -F


code:
#! /usr/bin/env python

import telnetlib
import readline
import os
import sys
import atexit
import cmd
import string

HOST='127.0.0.1'
PORT='4118'

CONSOLE_PROMPT='console> '

class ConsoleClient(cmd.Cmd):
    """Simple Console Client in Python.  This allows for readline functionality."""

    def connect_to_console(self):
        """Can throw an IOError if telnet connection fails."""
        self.console = telnetlib.Telnet(HOST,PORT)
        sys.stdout.write(self.read_from_console())
        sys.stdout.flush()

    def read_from_console(self):
        """Read from console until prompt is found (no more data to read)
        Will throw EOFError if the console is closed.
        """
        read_data = self.console.read_until(CONSOLE_PROMPT)
        return self.strip_console_prompt(read_data)

    def strip_console_prompt(self,data_received):
        """Strip out the console prompt if present"""
        if data_received.startswith(CONSOLE_PROMPT):
            return data_received.partition(CONSOLE_PROMPT)[2]
        else:
            #The banner case when you first connect
            if data_received.endswith(CONSOLE_PROMPT):
                return data_received.partition(CONSOLE_PROMPT)[0]
            else:
                return data_received

    def run_console_command(self,line):
        self.write_to_console(line + '\n')
        data_recved = self.read_from_console()
        sys.stdout.write(self.strip_console_prompt(data_recved))
        sys.stdout.flush()

    def write_to_console(self,line):
        """Write data to the console"""
        self.console.write(line)
        sys.stdout.flush()

    def do_EOF(self, line):
        try:
            self.console.write("quit\n")
            self.console.close()
        except IOError:
            pass
        return True

    def do_help(self,line):
        """The server already has it's own help command.  Use that"""
        self.run_console_command("help\n")

    def do_quit(self, line):
        return self.do_EOF(line)

    def default(self, line):
        """Allow a command to be sent to the console."""
        self.run_console_command(line)

    def emptyline(self):
        """Don't send anything to console on empty line."""
        pass

def main():
    histfile = os.path.join(os.environ['HOME'], '.consolehistory')
    try:
        readline.read_history_file(histfile)
    except IOError:
        pass
    atexit.register(readline.write_history_file, histfile)

    try:
        console_client = ConsoleClient()
        console_client.prompt = CONSOLE_PROMPT
        console_client.connect_to_console()
        doQuit = False;
        while doQuit != True:
            try:
                console_client.cmdloop()
                doQuit = True;
            except KeyboardInterrupt:
                #Allow for ^C (Ctrl-c)
                sys.stdout.write('\n')
    except IOError as e:
        print "I/O error({0}): {1}".format(e.errno, e.strerror)
    except EOFError:
        pass

if __name__ == '__main__':
    main()

rawDescription: How make a twisted python client with readline functionality


code:
with open('arq.txt') as handle:
    intlist = list(map(int, handle.read().replace('\n', '')))

print intlist

rawDescription: Read txt file and put in list with python


code:
from scipy.stats import norm, zscore

def sample_power_probtest(p1, p2, power=0.8, sig=0.05):
    z = norm.isf([sig/2]) #two-sided t test
    zp = -1 * norm.isf([power])
    d = (p1-p2)
    s =2*((p1+p2) /2)*(1-((p1+p2) /2))
    n = s * ((zp + z)**2) / (d**2)
    return int(round(n[0]))

def sample_power_difftest(d, s, power=0.8, sig=0.05):
    z = norm.isf([sig/2])
    zp = -1 * norm.isf([power])
    n = s * ((zp + z)**2) / (d**2)
    return int(round(n[0]))

if __name__ == '__main__':

    n = sample_power_probtest(0.1, 0.11, power=0.8, sig=0.05)
    print n  #14752

    n = sample_power_difftest(0.1, 0.5, power=0.8, sig=0.05)
    print n  #392

rawDescription: Is there a python (scipy) function to determine parameters needed to obtain a target power?


code:
import sys, re

css = open( sys.argv[1] , 'r' ).read()

# remove comments - this will break a lot of hacks :-P
css = re.sub( r'\s*/\*\s*\*/', "$$HACK1$$", css ) # preserve IE<6 comment hack
css = re.sub( r'/\*[\s\S]*?\*/', "", css )
css = css.replace( "$$HACK1$$", '/**/' ) # preserve IE<6 comment hack

# url() doesn't need quotes
css = re.sub( r'url\((["\'])([^)]*)\1\)', r'url(\2)', css )

# spaces may be safely collapsed as generated content will collapse them anyway
css = re.sub( r'\s+', ' ', css )

# shorten collapsable colors: #aabbcc to #abc
css = re.sub( r'#([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3(\s|;)', r'#\1\2\3\4', css )

# fragment values can loose zeros
css = re.sub( r':\s*0(\.\d+([cm]m|e[mx]|in|p[ctx]))\s*;', r':\1;', css )

for rule in re.findall( r'([^{]+){([^}]*)}', css ):

    # we don't need spaces around operators
    selectors = [re.sub( r'(?<=[\[\(>+=])\s+|\s+(?=[=~^$*|>+\]\)])', r'', selector.strip() ) for selector in rule[0].split( ',' )]

    # order is important, but we still want to discard repetitions
    properties = {}
    porder = []
    for prop in re.findall( '(.*?):(.*?)(;|$)', rule[1] ):
        key = prop[0].strip().lower()
        if key not in porder: porder.append( key )
        properties[ key ] = prop[1].strip()

    # output rule if it contains any declarations
    if properties:
        print "%s{%s}" % ( ','.join( selectors ), ''.join(['%s:%s;' % (key, properties[key]) for key in porder])[:-1] )

rawDescription: Python script for minifying CSS?

description: Python script for minifying CSS


code:
import requests
from bs4 import BeautifulSoup

urls = ['https://www.google.com']
for i in range(len(urls)):
    r = requests.get(urls[i])
    soup = BeautifulSoup(r.content, 'lxml')
    items = soup.find_all('p')
    for item in items:
        if "2016 - Privacidad - Condiciones" in item.text:
            print "Python has found the targeted text"

rawDescription: Check if Python has written the targeted text


code:
import signal

def handler(signum, frame):
    print 'Ctrl+Z pressed, but ignored'

signal.signal(signal.SIGTSTP, handler)

while True:
   pass

rawDescription: How to prevent user stopping script by CTRL + Z?

description: Prevent user stopping script by CTRL + Z


code:
import threading

def worker(num):
    """thread worker function"""
    print 'Worker: %s' % num
    return

threads = []
for i in range(5):
    t = threading.Thread(target=worker, args=(i,))
    threads.append(t)
    t.start()

rawDescription: Load testing with Python's requests?

description: Load testing with Python's requests


code:
import BeautifulSoup

soup = BeautifulSoup.BeautifulSoup('''

    Names
    Fred, John, Barry, Roger, James

''')

result = []
for tag in soup.table.findAll('td')[1]:
  if isinstance(tag, BeautifulSoup.NavigableString):
    for name in tag.string.split(','):
      name = name.strip()
      if name:
        result.append({ 'name': name })
  else:
    result.append({ 'name': tag.string.strip(), 'url': tag["href"] })

print result

rawDescription: split a comma separated list with links in with beautifulsoup

description: Split a comma separated list with links in with beautifulsoup


code:
a = '@(.+)@ie'               #part of your provided string
a = lists(a)                 #convert a to a list
a = [hex(ord(i)) for i in a] #hex conversion of each list element
a.insert(0,'')               #include a "\" at the beginning of the string
a = '\\'.join(a)             #join list of hex values separated by "\" in a string
print a

rawDescription: How to print string containing hex as string containing hex?

description: Print string containing hex as string containing hex


code:
import re

if __name__=='__main__':
    myString = "[name = john adams, family = candy, age = 72],[ name = jeff, family = Thomson, age = 24]"
    answers=re.findall("\\[\\s*name = ([^,]+), family = (\\w+), age = (\\d+)\\]",myString)
    names=[x[0] for x in answers]
    families=[x[1] for x in answers]
    ages=map(int,(x[2] for x in answers))

    print "names: ",names
    print "families: ", families
    print "ages: ", ages

rawDescription: string conversion

description: String conversion


code:
with closing(connect("user", "host", "port", None)) as ssh:
    with closing(ssh.open_sftp()) as sftp:
        with closing(sftp.open("filePath")) as f:
            with gzip.GzipFile(mode='rb', fileobj=f) as fin:
                for line in fin:
                    print line

rawDescription: Read GZIP text file from remote server over SFTP


code:
#!/usr/bin/env python
import signal
import sys
def signal_handler(signal, frame):
    print 'You pressed Ctrl+C - or killed me with -2'
    sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
print 'Press Ctrl+C'
signal.pause()

rawDescription: How can I override the keyboard interrupt? (Python)

description: Override the keyboard interrupt? (Python)


code:
import numpy as np
import pandas as pd

### create random data
N = 1000
a = np.random.randint(0, 100, N)
b = np.random.randint(0, 30, N)
c = np.random.randint(0, 10, N)

df = pd.DataFrame({"a":a, "b":b, "c":c})

### calculate pivot sum
res = df.pivot_table(values="c", index="a", columns="b", aggfunc="sum", fill_value=0)

### create random rows to change
M = 100
row_index = np.unique(np.random.randint(0, N, M))
old_rows = df.iloc[row_index]
M = old_rows.shape[0]
new_rows = pd.DataFrame({"a":np.random.randint(0, 100, M),
                         "b":np.random.randint(0, 30, M),
                         "c":np.random.randint(0, 10, M)})

### update pivot table
sub_df = old_rows.pivot_table(values="c", index="a", columns="b", aggfunc="sum", fill_value=0)
add_df = new_rows.pivot_table(values="c", index="a", columns="b", aggfunc="sum", fill_value=0)
new_res = res.sub(sub_df, fill_value=0).add(add_df, fill_value=0)

### check result
df.iloc[row_index] = new_rows.values
res2 = df.pivot_table(values="c", index="a", columns="b", aggfunc="sum", fill_value=0)
print new_res.astype(int).equals(res2)

rawDescription: Is there a way to update pandas.pivot_table without reconstructing it?


code:
data_str = '''"Id","Name","CreatedById","Salary","Base_Salary","Type","Pay_cycle","Description","Code"
"a0u90000003R4Y9AAK","Freelance Hadoop developer","005900000039GMdAAM","","","","","",""
"a0u90000003R5UPAA0","Senior Hadoop developer","005900000039GMdAAM","","","","","",""
"a0u90000003R5V3AAK","Freelance Webmaster","005900000039GMdAAM","","","","","",""
"a0u90000004ZMUwAAO","Full-Stack Web Developer - PHP and Python","005900000039GMdAAM","","","","","",""'''

# in python 2 you need to decode the string before passing it to StringIO
data_io = io.StringIO(data_str.decode('utf-8'))
## this is not needed in python 3
# data_io = io.StringIO(data_str)

reader = csv.DictReader(data_io)
for row in reader:
    print row['Id'], row['Name']

rawDescription: How to use Python's CSV DictReader to parse a CSV output?

description: Use Python's CSV DictReader to parse a CSV output


code:
import subprocess, signal, os, threading, errno
from contextlib import contextmanager

class TimeoutThread(object):
    def __init__(self, seconds):
        self.seconds = seconds
        self.cond = threading.Condition()
        self.cancelled = False
        self.thread = threading.Thread(target=self._wait)

    def run(self):
        """Begin the timeout."""
        self.thread.start()

    def _wait(self):
        with self.cond:
            self.cond.wait(self.seconds)

            if not self.cancelled:
                self.timed_out()

    def cancel(self):
        """Cancel the timeout, if it hasn't yet occured."""
        with self.cond:
            self.cancelled = True
            self.cond.notify()
        self.thread.join()

    def timed_out(self):
        """The timeout has expired."""
        raise NotImplementedError

class KillProcessThread(TimeoutThread):
    def __init__(self, seconds, pid):
        super(KillProcessThread, self).__init__(seconds)
        self.pid = pid

    def timed_out(self):
        try:
            os.kill(self.pid, signal.SIGKILL)
        except OSError as e:
            # If the process is already gone, ignore the error.
            if e.errno not in (errno.EPERM, errno. ESRCH):
                raise e

@contextmanager
def processTimeout(seconds, pid):
    timeout = KillProcessThread(seconds, pid)
    timeout.run()
    try:
        yield
    finally:
        timeout.cancel()

def example():
    proc = subprocess.Popen(["sleep", "5"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)

    with processTimeout(1, proc.pid):
        print proc.communicate()

    resultcode = proc.wait()
    if resultcode < 0:
        print "error: %i" % resultcode

if __name__ == '__main__':
    example()

rawDescription: Multiple subprocesses with timeouts

description: Multiple subprocesses with timeouts


code:
pairs = "id=100 id2=200 id3=300".split(' ')
res = {}
for p in pairs:
    k,v = p.rsplit('=', 1)
    res[k] = v

print res # prints {'id2': '200', 'id': '100', 'id3': '300'}
# now we can send the dictionary to the method

rawDescription: Use a string of expressions in a method for python


code:
mydic = {"printer1": "printserver1", "printer2": "printserver2"}

template = r"""
FOR /F in blah blah blah
    do something with {key}
    with the value of {value}
    blah blah blah
END FOR
"""

for key, value in mydic.iteritems():
    print template.format(key=key, value=value)

rawDescription: trying to print dictionary key pair

description: Trying to print dictionary key pair


code:
buckets = defaultdict(float)
bucket_size = 1000

for line in lines:
    index,value = line.split()
    buckets[int(index)/bucket_size] += float(value)

for k,v in sorted(buckets.items()):
    print k,v/bucket_size

rawDescription: Trying to concatanate non-consistant data within range


code:
from Bio import SeqIO

for long_sequence_record in SeqIO.parse(open('long_sequences.fasta'), 'fasta'):
    long_sequence = str(long_sequence_record.seq)

    for short_sequence_record in SeqIO.parse(open('short_sequences.fasta'), 'fasta'):
        short_sequence = str(short_sequence_record.seq)

        if short_sequence in long_sequence:
            start = long_sequence.index(short_sequence) + 1
            stop = start + len(short_sequence) - 1
            print short_sequence_record.id, start, stop

rawDescription: Python: How to find coordinates of short sequences in a FASTA file?


code:
import oauth
import httplib

URL='http://localhost:5984/_session'
CONSUMER_KEY = 'example.com'
CONSUMER_SECRET = 'sekr1t'
TOKEN='user1'
SECRET='tokensekr1t'

consumer = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET)
token = oauth.OAuthToken(TOKEN, SECRET)
req = oauth.OAuthRequest.from_consumer_and_token(
    consumer, token=token, http_method='GET', http_url=URL, parameters={}
)
req.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), consumer,token)

con = httplib.HTTPConnection('localhost', 5984)
con.request('GET', URL,headers=req.to_header())
print con.getresponse().read()

rawDescription: Google UrlFetch Service with CouchDB OAuth


code:
import ast

dict_file = open("dict.txt", "r")
dict_string = dict_file.readline().strip()
dict_file.close()

d = ast.literal_eval(dict_string)
print d["one"]

#change your dictionary e.g:
d["foo"] = "bar"

f = open("dict.txt", "w")
f.write(str(d))
f.close()

rawDescription: Python: Accessing dictionary from txt file and adding new data to it and then storing this updted dict back in txt file


code:
class Factory():
    registred = {}

    @classmethod
    def register(cls, priority):
        registred = cls.registred
        def inner(inner_cls):
            registred[priority] = inner_cls
            return inner_cls
        return inner
    @classmethod
    def get(cls):
        return min(cls.registred.items())[1]()

@Factory.register(3)
class A():
    def test(self):
        print "A"

Factory.get().test()

@Factory.register(2)
class B():
    def test(self):
        print "B"

Factory.get().test()

@Factory.register(1)
class C(B):
    def test(self):
        print "C"

Factory.get().test()

rawDescription: prioritized decorator for instantiating instance of class

description: Prioritized decorator for instantiating instance of class


code:
import OpenSSL
from OpenSSL import crypto
import base64
key_file = open("C:\my.pem", "r")
key = key_file.read()
key_file.close()
password = "password of prk"

if key.startswith('-----BEGIN '):
    pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, key, password)
else:
    pkey = crypto.load_pkcs12(key, password).get_privatekey()
print pkey
data = "data"
sign = OpenSSL.crypto.sign(pkey, data, "sha256")
print sign

data_base64 = base64.b64encode(sign)
print data_base64

rawDescription: how to sign data using openssl with python

description: Sign data using openssl with python


code:
textstring = "No, thank you"
import re
print re.findall(r'\w+|\S+', textstring)
# ['No', ',', 'thank', 'you']

rawDescription: Splitting text in python but treating commas, periods etc as seperate 'words'


code:
from bs4 import BeautifulSoup
import urllib2
site= "http://en.wikipedia.org/wiki/Aldi"
hdr = {'User-Agent': 'Mozilla/5.0'}
req = urllib2.Request(site,headers=hdr)
page = urllib2.urlopen(req)
soup = BeautifulSoup(page.read())
table = soup.find('table', class_='infobox vcard')
result = {}
exceptional_row_count = 0
for tr in table.find_all('tr'):
    if tr.find('th'):
        result[tr.find('th').text] = tr.find('td').text
    else:
        # the first row Logos fall here
        exceptional_row_count += 1
if exceptional_row_count > 1:
    print 'WARNING ExceptionalRow>1: ', table
print result

rawDescription: How to extract the infobox vcard from wikipedia using the python wikipedia library

description: Extract the infobox vcard from wikipedia using the python wikipedia library


code:
def _login(self):
    s = requests.Session()
    url = self.URL_LOGIN
    payload = {'data[User][username]': self.usuario, 'data[User][password]': self.senha, "data[lembrar]": "on"}
    r = s.post(url, payload)
    html = r.content

    if "<title>Login - Legendas TV</title>" in html:
        return 0
    else:
        print 'Success on login!'
        self.session = s

        return 1

def _request(self, url, method='GET', data=None):
    if self.session:
        if method == 'GET':
            r = self.session.get(url, cookies=self.cookie, stream=True)
        if method == 'POST' and data:
            r = self.session.post(url, data=data, cookies=self.cookie)

        return r

rawDescription: How to get a session cookie in python

description: Get a session cookie in python


code:
import re,requests

s=requests.session()

main_url="https://angel.co/login?utm_source=top_nav_home"
headers={"Content-Type":"application/x-www-form-urlencoded","Host":"angel.co","Origin":"https://angel.co"\
,"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"} # Mostly you need to pass the headers . Default headers don't work always.  So be careful here

r1=s.get(main_url,headers=headers) #authenticity token is extracted from this page using the regex i mentioned below. This is required when you login to the page
token=re.search("""<input name="authenticity_token" type="hidden" value="(.*?)"[^>]*?>""",r1.text,re.S|re.I)
print token.group(1)
headers["Referer"]="https://angel.co/login?utm_source=top_nav_home"
headers["Upgrade-Insecure-Requests"]="1"
payload={"login_only":"true","user[email]":"youremail","user[password]":"yourpassword","authenticity_token":token.group(1),"utf8":"%25E2%259C%2593"} # the value of utf8  gets urlencoded once you send the request.
r2=s.post("https://angel.co/users/login",headers=headers,data=payload,cookies=r1.cookies)
print r2.status_code

rawDescription: unable to log in to a website with requests

description: Unable to log in to a website with requests


code:
data = r"pizza won't divorce you pizza won't betray you pizza won't cheat on you pizza won't fight with you  why don't people just \xe2\x80\xa6\n"

print re.findall(r"\\[a-z][a-z]?[0-9]+", data)

rawDescription: Unable to replace \xe2\x80\xa6\n in a string using regex in python


code:
c.execute("SELECT count(*) FROM users WHERE users.login=?", (name,))
playercount = c.fetchone()
if playercount[0]:
    print 'user exists'

rawDescription: unique value from DB sqlite3 in process registration

description: Unique value from DB sqlite3 in process registration


code:
class MyDecorator(object):
    def __init__(self, func, instance=None):
        self.func = func
        self.instance = instance

    def __call__(self, *args, **kw):
        if self.instance:
            print "is method - instance : %s" % self.instance
            args = (self.instance,) + args
        else:
            print "is_function"

        return self.func(*args, **kw)

    def __get__(self, instance, cls):
        return type(self)(self.func, instance)

rawDescription: How to distinguish between function and a method in decorator

description: Distinguish between function and a method in decorator


code:
import time, random
from multiprocessing import Process, Manager

class MP_Stuff():
    def __init__(self, parent, id):
        time.sleep(1 + random.random()*5) # simulate data processing
        print id , "done"
        parent.killMP(id)

class ParamHandler():
    def doFirstMP(self, IDs):
        self.mps_in_process = []
        self.ID_List = Manager().list(IDs)
        id = self.ID_List.pop(0)
        p = Process(target=MP_Stuff, args=(self, id))
        self.mps_in_process.append(id)
        p.start()
        p.join()
        print "joined"

    def doMP(self):
        for tmp in range(3): # nr of concurrent processes
            print self.ID_List
            if len(self.ID_List) > 0:
                id = self.ID_List.pop(0)
                p = Process(target=MP_Stuff, args=(self, id))
                self.mps_in_process.append(id)
                p.start()

    def killMP(self, kill_id):
        print "kill", kill_id
        self.mps_in_process.remove(kill_id)
        self.doMP()

if __name__ == '__main__':
    ID_List = [1,2,3,4,5,6]
    paramSet = ParamHandler()
    paramSet.doFirstMP(ID_List)

rawDescription: using list with python multiprocessing

description: Using list with python multiprocessing


code:
from Tkinter import *

class MyDialog:

    def __init__(self, parent):

        top = self.top = Toplevel(parent)

        Label(top, text="Value").pack()

        self.e = Entry(top)
        self.e.pack(padx=5)

        b = Button(top, text="OK", command=self.ok)
        b.pack(pady=5)

    def ok(self):

        print "value is", self.e.get()

        self.top.destroy()

root = Tk()
Button(root, text="Hello!").pack()
root.update()

d = MyDialog(root)

root.wait_window(d.top)
root.mainloop()

rawDescription: Input popup window for input into interactive python game