code:
prev_line = ''
for line in all_lines:
    if not re.match('(?:(?:\d\d:){3}(?:\d\d) ){2}.*', line):
        prev_line += ' ' + line
    else:
        if prev_line:  # prevents the first flag '' prev_line from printing
            print prev_line
        prev_line = line
print prev_line  # make sure to print/dump the last one

rawDescription: concatenate lines in Python if next line not matches pattern

description: Concatenate lines in Python if next line not matches pattern


code:
# in your program
from messages import messages
print messages['struct1'][0]["type"]
print messages['struct1'][1]['type']
print messages['struct1'][1]['enums'][0]
print messages['struct2'][0]['type']

rawDescription: Parsing a datafile in python (2.5.2)

description: Parsing a datafile in python (2.5.2)


code:
Skyrim = "Skyrim"
GTAIV = "Skyrim"
GTASA = "Skyrim"
Games = Skyrim + " " + GTAIV + " " + GTASA

if raw_input("Enter: ") == "Games":
    print Games

rawDescription: python - How to make an "if input ==" conditional

description: Python - How to make an "if input ==" conditional


code:
import re
print re.sub(r'^\s+(.*)\n', r'<s> \1 </s> ', text, flags=re.M)

rawDescription: How to insert html like tag "<s></s>" for a string using regular expression in python?

description: Insert html like tag "<s></s>" for a string using regular expression in python


code: print u'\xd3'

rawDescription: Convert from hex character to Unicode character in python


code:
import bs4
S = '''<foo> <bar a="3"/> </foo>'''
soup = bs4.BeautifulSoup(S, 'xml')
print soup.prettify()

rawDescription: How to get BeautifulSoup 4 to respect a self-closing tag?

description: Get BeautifulSoup 4 to respect a self-closing tag


code:
for img in soup.select('a.image > img'):
     print img['src']

rawDescription: Fetching Image from URL using BeautifulSoup


rawDescription: How to solve non-linear equations using python

description: Solve non-linear equations using python


code: print nuc[frame:].translate(table)

rawDescription: How to find a open reading frame in Python

description: Find a open reading frame in Python


code:
for element in root:
    print 'parent: ', element.tag, '|', element.attrib
    print element.text
    for all_tags in element.findall('.//'):
        print 'child: ', all_tags.tag, '|', all_tags.attrib
        if all_tags.text:
            print all_tags.text, '|', all_tags.tail

rawDescription: How to parse all children and grandchildren elements from a current node using xml.etree.ElementTree in python

description: Parse all children and grandchildren elements from a current node using xml.etree.ElementTree in python


code:
import io
dictionary = io.open('dictionary/de.dict', encoding='utf8')
for line in dictionary:
    if line[0].isupper():
        print "Uppercase word", line

rawDescription: Compare german umlaut in Python

description: Compare german umlaut in Python


code:
import re

examples = [
  "John Jeffries",
  "John Jeffries, M.D.",
  "John Jeffries, MD",
  "John Jeffries and Jim Smith",
  "John and Jim Jeffries",
  "John Jeffries & Jennifer Wilkes-Smith, DDS, MD",
  "John Jeffries, CPA & Jennifer Wilkes-Smith, DDS, MD",
  "John Jeffries, C.P.A & Jennifer Wilkes-Smith, DDS, MD",
  "John Jeffries, C.P.A., MD & Jennifer Wilkes-Smith, DDS, MD",
  "John Jeffries M.D. and Jennifer Holmes CPA",
  "John Jeffries M.D. & Jennifer Holmes CPA",
  'John Jimmy Jeffries, C.P.A., MD & Jennifer Wilkes-Smith, DDS, MD',
  'John Jeffries, C.P.A., MD & Jennifer Jenny Wilkes-Smith, DDS, MD'
]

def IsTitle(inp):
  return re.match('^([A-Z]\.?)+$',inp.strip())

def ParseName(name):
  #Titles are separated from each other and from names with ","
  #We don't need these, so we remove them
  name = name.replace(',',' ')
  #Split name and titles on spaces, combining adjacent spaces
  name = name.split()
  #Build an output object
  ret_name = {"first":None, "middle":None, "last":None, "titles":[]}
  #First string is always a first name
  ret_name['first'] = name[0]
  if len(name)>2: #John Johnson Smith/PhD
    if IsTitle(name[2]): #John Smith PhD
      ret_name['last']   = name[1]
      ret_name['titles'] = name[2:]
    else:                #John Johnson Smith, PhD, MD
      ret_name['middle'] = name[1]
      ret_name['last']   = name[2]
      ret_name['titles'] = name[3:]
  elif len(name) == 2:   #John Johnson
    ret_name['last'] = name[1]
  return ret_name

def CombineNames(inp):
  if not inp[0]['last']:
    inp[0]['last'] = inp[1]['last']

def ParseString(inp):
  inp = inp.replace("&","and")    #Names are combined with "&" or "and"
  inp = re.split("\s+and\s+",inp) #Split names apart
  inp = map(ParseName,inp)
  CombineNames(inp)
  return inp

for e in examples:
  print e
  print ParseString(e)

rawDescription: Can I use python 're' to parse complex human names?

description: Use python 're' to parse complex human names


code:
#!/usr/bin/env python

import os, time, sys, re
from sets import Set

def sorting_merge(o_file , c_file, m_file ):

    pat = re.compile('[0123]\d/[01]\d/\d{2} [012]\d:[0-6]\d:[0-6]\d'
                     '(?=[ \t]+'
                     '[0123]\d/[01]\d/\d{2} [012]\d:[0-6]\d:[0-6]\d'
                     '|'
                     '[ \t]+aborted/deleted)')

    def kl(line,pat = pat):
        return time.mktime(time.strptime((pat.search(line).group()),'%d/%m/%y %H:%M:%S'))

    head = []
    output = Set()

    fa = open(o_file)
    fa.readline() # first line is skipped
    for line1 in fa:
        if pat.search(line1):  break # first line after the heading
        else:  head.append(line1) # line of the header
    for line in fa:
        output.add(line.rstrip())
    output.add(line1.rstrip())
    fa.close()

    fb = open(c_file)
    for line1 in fb:
        if pat.search(line1):  break
    for line in fb:
        output.add(line.rstrip())
    output.add(line1.rstrip())
    fb.close()

    if '' in output:  output.remove('')
    output = [ (kl(line),line) for line in output]
    output.sort()

    fm = open(m_file,'w')
    fm.write(time.strftime('On %d/%m/%y %H:%M:%S\n')+(''.join(head)))
    for t,line in output:
        fm.write(line+'\n')
    fm.close()

te = time.clock()
sorting_merge('A.txt','B.txt','C.txt')
print time.clock()-te

rawDescription: How to merge only the unique lines from file_a to file_b?

description: Merge only the unique lines from file_a to file_b


code:
import json
with open("file.txt", "r") as manipulated_file:
    manipulated_file_lines = manipulated_file.readlines()
with open("file.txt", "w") as  manipulated_file:
    for line in manipulated_file_lines:
        processed_list = json.loads(line.replace("'",'"'))
        list_to_be_written=": ".join(map(str,processed_list))
        # this should be tweaked if you want full precision for numbers

        print list_to_be_written

        manipulated_file.write(list_to_be_written)

rawDescription: Manipulating data of file several times

description: Manipulating data of file several times


code:
from ipywidgets import widgets

# Create text widget for output
output_variable = widgets.Text()

# Create text widget for input
input_text = widgets.Text()

# Define function to bind value of the input to the output variable
def bind_input_to_output(sender):
    output_text.value = input_text.value

# Tell the text input widget to call bind_input_to_output() on submit
input_text.on_submit(bind_input_to_output)

# Display input text box widget for input
input_text

# Display output text box widget (will populate when value submitted in input)
output_text

# Display text value of string in output_text variable
output_text.value

# Define new string variable with value of output_text, do something to it
uppercase_string = output_text.value.upper()
print uppercase_string

rawDescription: Using Ipython ipywidget to create a variable?


code: print sorted(ListB, key=int, reverse=True)

rawDescription: How to sort integer list in python descending order

description: Sort integer list in python descending order


code:
from mpi4py import MPI
comm = MPI.COMM_WORLD

obj = "I am an example. My ID is unique to each communicator."
mpi_id = 'rank %i has id %s'%(comm.rank, str(id(obj)))
comm.send(mpi_id, tag=11, dest=0)

mpi_id_list = []
if comm.rank==0:
   mpi_id_list = []
   for i in range(comm.size):
      mpi_id_list.append( comm.recv(source=i, tag=11))

   print mpi_id_list

#broadcasting the list
mpi_id_list = comm.bcast(mpi_id_list, root=0)

#now, the list is the same on all processes.
print "rank "+str(comm.rank)+" has list "+str(mpi_id_list)

rawDescription: mpi4py append value id to list for each communicator instance

description: Mpi4py append value id to list for each communicator instance


code:
N = 40000
r = 0.1
x1 = np.random.normal(size = N)
x1 = (x1 - min(x1)) / (max(x1)-min(x1))
x2 = np.random.normal(size = N)
x2 = (x2 - min(x2)) / (max(x2)-min(x2))
X = np.vstack([x1, x2]).T

tic = time.time()
grid0 = gt.get_centers0(X, r)
toc = time.time()
print 'Method 0: ' + str(toc - tic)

tic = time.time()
grid2 = gt.get_centers2(X, r, 10)
toc = time.time()
print 'Method 2: ' + str(toc - tic)

tic = time.time()
grid3 = gt.get_centers_fast(X, r)
toc = time.time()
print 'Method 3: ' + str(toc - tic)

rawDescription: Build an approximately uniform grid from random sample (python)


code:
import MySQLdb

def db_dropdown():  # Execute query
    db = MySQLdb.connect(user='root', db='test', passwd='test', host='ip-address')  # Your DB details here
    cursor = db.cursor()

    sql = 'SELECT SUM(regressnum) FROM testlog WHERE buildnumber = %s AND productid = 41))' \
          % build_number  # Parse in a variable into the query
    cursor.execute(sql)
    list_tested = cursor.fetchall()  # Get query response and store in variable
    list_tested = [i for sub in list_tested for i in sub]  # Convert to list from tuple

    return list_tested

def print_dropdown(data):  # Print the dropdown
    print '<div>'
    print '<select>'
    for i in data:
        print '<option value="%s"selected>%s</option>' % (i, i)
    print '</select>'
    print '</div>'

rawDescription: How to get MySQL database values in dropdown using python cgi?

description: Get MySQL database values in dropdown using python cgi


code:
def timing(f):
    def wrap(*args):
        time1 = time.time()
        ret = f(*args)
        time2 = time.time()
        print '%s function took %0.3f ms' % (f.func_name, (time2-time1)*1000.0)
        return ret
    return wrap

rawDescription: Python time measure function

description: Python time measure function


code:
import re

d = {'b':'BAR','c':'CORE','d':'DEAD','f':'FAN',
     'g':'GO','h':'HHH','j':'JIU','k':'KOAN',
     'l':'LOW','m':'MY','n':'NERD','p':'PI',
     'q':'QIM','r':'ROAR','s':'SING','t':'TIP',
     'v':'VIEW','w':'WAVE','x':'XOR',
     'y':'YEAR','z':'ZOO',
     'T':'tears','Y':'yearling','X':'xylophone'}

ded = dict((v,k) for k,v in d.iteritems())
print ded

tu = ('This is my first regex python example '
       'Yahooa yahoouuee bbbiirdd',

      'bbbiirdd',

      'fookirooksooktook',

      'crrsciencezxxxxxXscienceokjjsciencq')

reg   = re.compile(r'([bcdfghj-np-tv-zBCDFGHJ-NP-TV-Z])(\1?)')

othergr = '|'.join(ded.keys())
dereg = re.compile('science([^aeiouAEIOU])|(%s)' % othergr)

def Frepl(ma, d=d):
    g1,g2 = ma.groups()
    if g2:  return 'science' + g2
    else:   return d[g1]

def Fderepl(ma,ded=ded):
    g = ma.group(2)
    if g:  return ded[g]
    else:  return 2*ma.group(1)

for strt in tu:
    resu =   reg.sub(Frepl  , strt)
    bakk = dereg.sub(Fderepl, resu)
    print ('----------------------------------\n'
           'strt = %s\n'    'resu == %s\n'
           'bakk == %s\n'   'bakk == start : %s'
           % (strt, resu, bakk, bakk==strt))

rawDescription: How do I reverse regex substitution?

description: Reverse regex substitution


code:
from collections import Counter

counter = Counter()

with open('data.txt') as data:
    for line in data:
        counter.update(line.split())

for item, count in counter.items():
    print "%s , count: %s" % (item, count)

rawDescription: Find word in line and count number of line


code:
def foo(a, t=None):
    if t is None:
        t = [0] * len(a)
    print t

rawDescription: List building in Python function definition


code:
from multiprocessing import Process, Pipe
import time

def request(sleep, result):
  """Your http request example"""
  time.sleep(sleep)
  return result

class TimeoutWrapper():
  """Timeout wrapper using separate process"""
  def __init__(self, func, timeout):
    self.func = func
    self.timeout = timeout

  def __call__(self, *args, **kargs):
    """Run func with timeout"""
    def pmain(pipe, func, args, kargs):
      """Function to be called in separate process"""
      result = func(*args, **kargs) # call func with passed arguments
      pipe.send(result) # send result to pipe

    parent_pipe, child_pipe = Pipe() # Pipe for retrieving result of func
    p = Process(target=pmain, args=(child_pipe, self.func, args, kargs))
    p.start()
    p.join(self.timeout) # wait for prcoess to end

    if p.is_alive():
      p.terminate() # Timeout, kill
      return None # or raise exception if None is acceptable result
    else:
      return parent_pipe.recv() # OK, get result

print TimeoutWrapper(request, 3)(1, "OK") # prints OK
print TimeoutWrapper(request, 1)(2, "Timeout") # prints None

rawDescription: What should I do if socket.setdefaulttimeout() is not working?


code:
import calendar
import datetime

current = datetime.date.today().replace(day=1)
mylist = list()
for i in xrange(12):
    rng = calendar.monthrange(current.year, current.month)
    last = current.replace(day = rng[1])
    mylist.append(current.strftime("%b 1, %Y") + " - " + last.strftime("%b %d, %Y"))
    current = (current - datetime.timedelta(1)).replace(day=1)
print mylist

rawDescription: Python date format jan 1,2012 - jan 31,2012


code:
from xml.etree import ElementTree as etree

SOURCE = '''\
<root>
    <Enum name="Coupon Type" group="" defaultVal=""> </Enum>
    <Enum name="DayCountConvention" group="" defaultVal=""> </Enum>
</root>
'''

def main():
    root_node = etree.fromstring(SOURCE)
    for enum_node in root_node.findall('Enum'):
        enum_node.text = None
    print etree.tostring(root_node)

if __name__ == '__main__':
    main()

rawDescription: Remove Whitespaces in an xml tag using ElementTree in python 2.6


code:
import shelve

a = 'trxns.xml'
b = 'foobar.xml'
c = 'profile.xml'

urls = [a, b, c]
cache = shelve.open('my.cache', 'c')

try:
    cache[a] = a*1000
    cache[b] = b*10000
finally:
    cache.close()

cache = shelve.open('my.cache', 'c')

try:
    print cache.keys()
    print [url for url in urls if cache.has_key(url)]
    print cache.keys()
finally:
    cache.close()

rawDescription: Problem with shelve module?

description: Problem with shelve module


code:
from functools import partial
def foo(a, b):
    return (a if not isinstance(a, partial) else a()) + (b if not isinstance(b, partial) else b())

fun = partial(foo, 1, partial(foo, partial(foo, 2, 4), partial(foo, 3, 5)))
fun()
fun = partial(foo, 1, partial(foo, partial(foo, 2, 4), partial(foo, 3, 5)))
def print_pfunc(pfunc):
    return "{}({}{}{})".format(pfunc.func.__name__, ', '.join(str(arg) if not isinstance(arg, partial) else print_pfunc(arg) for arg in pfunc.args) if pfunc.args else '', ', ' if pfunc.args and pfunc.keywords else '', ', '.join('{}={}'.format(k, v if not isinstance(v, partial) else print_pfunc(v)) for k, v in pfunc.keywords) if pfunc.keywords else '')

print print_pfunc(fun)

rawDescription: Automatically remove named parameters from a Python function call


code:
import re
p = re.compile('http://jenkins.mycompany.com/job/[^\s]+')
line = 'There is a problem with http://jenkins.mycompany.com/job/app/4567. We   should fix this.'
result = p.search(line)
print result.group(0)

rawDescription: Extract string using regex in Python

description: Extract string using regex in Python


code:
u1 = User(first_name='Foo', last_name='Foo')
u2 = User(first_name='Bar', last_name='Bar')
u3 = User(first_name='Baz', last_name='Baz')
u1.contacts = [u2, u3]
session.add(u1)
session.commit()

# ... and in some other place in your code ...

u = User.query.get(1)
print u.contacts[0].first_name

rawDescription: Trouble mapping multiple foreign keys to parent table


code:
from itertools import groupby
with open('Input.txt') as fin:
    lines = sorted([line.rstrip() for line in fin])
for item, grp in groupby(lines, key = lambda x:x.split(".")[0]):
    print item, list(grp)

rawDescription: how to group a text file by first 3 characters of lines?

description: Group a text file by first 3 characters of lines


code:
print testPKL.__dict__
{'testNumber': 12}
print result.__dict__
{'testNumber': 12}

rawDescription: Python Pickle not saving entire object

description: Python Pickle not saving entire object


code:
import numpy as np

def mirrored2(maxval, inc=1):
    return np.linspace(-maxval, maxval, 2*maxval // inc)

print mirrored2(1, 0.3)

rawDescription: Create list of numbers mirrored around zero (python)


code:
minyear = 2008
maxyear = 2013
new_a = []
for group in A:
    group = group
    years = [point[0] for point in group]
    print years
    for year in range(minyear,maxyear+1):
        if year not in years:
            group.append([year,0])
    new_a.append(sorted(group))
print new_a

rawDescription: Insert missing dates while keeping the date order in python list


code:
import re

linend = re.compile(r'\r\n\r\n|\n\n')
s = 'an example\n\nstring\n\nhere'
print linend.split(s, 1)
s = 'another\r\n\r\nexample\r\n\r\nhere'
print linend.split(s, 1)

rawDescription: python: how to find \r\n\r\n in one single search

description: Python: how to find \r\n\r\n in one single search


code:
import RPi.GPIO as GPIO
import time
import urllib

GPIO.setmode(GPIO.BCM)
GPIO_PIR = 7
GPIO.setup(GPIO_PIR,GPIO.IN)

Previous_State = 0
Pret_Counter = 0
pret_message = None

try:
    # this will only print the first time.
    print "Attente detection..."

    # this loop will continuously run
    while True:

        Current_State = GPIO.input(GPIO_PIR)

        # if we have a message, print it!
        if pret_message:
            print pret_message

        if Current_State and Previous_State==0:
            print "Mouvement detecte!"
            time.sleep(5)
            Previous_State=1
            # Someone moved.  reset detection counter.
            Pret_Counter = 0

        elif Pret_Counter > 9:
            # if we've been empty for 10 seconds,
            # don't bother incrementing our counter
            pret_message = "Room empty"

        elif Current_State==0 and Previous_State:
            print "Pret"
            Pret_Counter += 1
            Previous_State=0
            time.sleep(1)

except KeyboardInterrupt:
    print "Quit"
GPIO.cleanup()

rawDescription: a trivial Presence detector with raspberry using python

description: A trivial Presence detector with raspberry using python


code:
import fileinput

results = []
for line in fileinput.input('input.txt', inplace=1):
    data = line.split()
    if data[-1].strip() == '0':
        print line.strip()
    else:
        results.append(tuple(map(int, data[:-1])) + ({'volume': float(data[-1])}, ))

print results

rawDescription: Formatting files to be written

description: Formatting files to be written


code:
import Tkinter as tk

class IngredientAdder(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry("500x500+0+0")
        self.init_gui()

    # function to add new ingredients
    def add_ingredient_entry(self):
        entry = tk.Entry(self)
        entry.pack(side=tk.TOP)
        self.ingredient_entries.append(entry)

    # get contents of all entry boxes
    def save_recipe(self):
        for ingredient in self.ingredient_entries:
            print ingredient.get()
        print "[Recipe saved]"

    # build initial widgets
    def init_gui(self):

        # this is a list of ingredients entry boxes
        self.ingredient_entries = []

        # put a label at the top of the window and anchor it there
        tk.Label(self,text="Ingredients").pack(anchor=tk.N,side=tk.TOP)

        # Put these two buttons at the bottom of the window and anchor them there
        tk.Button(self,text="Save recipe",command=self.save_recipe).pack(anchor=tk.S,side=tk.BOTTOM)
        tk.Button(self,text="Add ingredient",command=self.add_ingredient_entry).pack(anchor=tk.S,side=tk.BOTTOM)

        # new ingredients will be added between the label and the buttons
        self.add_ingredient_entry()

cookbook = IngredientAdder()
cookbook.mainloop()

rawDescription: Tkinter, Python: How do I save text entered in the Entry widget? How do I move a label?


code:
class FrozenSetDict(dict):
    def __setitem__(self,idx,value):
        try:
            dict.__setitem__(self,frozenset(idx),value)
        except TypeError:
            dict.__setitem__(self,frozenset((idx,)),value)

d = FrozenSetDict()
d[1,2,3] = 4
d[1] = 5
print d

rawDescription: Alias for dictionary operation in Python


code:
print "Random numbers are: "
for _ in xrange(500):
    print rn(), rn(), rn(), rn(), rn(), rn()

rawDescription: Write multiple values into text file in Python?


code:
import re
string = "Fri Dec 13 03:49:06 +0000 2013,I need some HARD TRAP beats producers help me out here...louiethefifthonline@gmail.com,490060971,[]"
regex = "\w+@\w+\.com"
match = re.findall(regex,string)
print match

rawDescription: Print single word from Parsed Json text

description: Print single word from Parsed Json text


code:
import os
import sqlite3

_DBPATH = "./q6996603.sqlite"

def fresh_db():
    if os.path.isfile(_DBPATH):
        os.remove(_DBPATH)
    with sqlite3.connect(_DBPATH) as conn:
        cur = conn.cursor().executescript("""
            CREATE TABLE "mytable" (
                "id" INTEGER PRIMARY KEY AUTOINCREMENT, -- rowid
                "data" INTEGER
            );
            """)
    print "created %s" % _DBPATH

# functions are syntactic sugar only and use global conn, cur, rowid

def select():
    sql = 'select * from "mytable"'
    rows = cur.execute(sql).fetchall()
    print "   same connection sees", rows
    # simulate another script accessing tha database concurrently
    with sqlite3.connect(_DBPATH) as conn2:
        rows = conn2.cursor().execute(sql).fetchall()
    print "   other connection sees", rows

def count():
    print "counting up"
    cur.execute('update "mytable" set data = data + 1 where "id" = ?', (rowid,))

def commit():
    print "commit"
    conn.commit()

# now the script
fresh_db()
with sqlite3.connect(_DBPATH) as conn:
    print "--- prepare test case"
    sql = 'insert into "mytable"(data) values(17)'
    print sql
    cur = conn.cursor().execute(sql)
    rowid = cur.lastrowid
    print "rowid =", rowid
    commit()
    select()
    print "--- two consecutive w/o commit"
    count()
    select()
    count()
    select()
    commit()
    select()
    print "--- two consecutive with commit"
    count()
    select()
    commit()
    select()
    count()
    select()
    commit()
    select()

rawDescription: python sqlite3, how often do I have to commit?

description: Python sqlite3, how often do I have to commit


code:
for pr in primes(1000):
    print pr

rawDescription: Can this be made more pythonic?

description: Can this be made more pythonic


code:
import mmh3

def getHash(key, m, k):
    result = set()
    seed = 1
    while True:
        if len(result) == k:
            return list(result)
        else:
            b = mmh3.hash(key, seed) % m
            result.add(b)
            seed += 10
            print result

if __name__ == "__main__":
    print getHash("Hello", 100, 5)
    print getHash("Good Bye", 100, 5)

rawDescription: Generating k numbers of hash value with python


code:
def SubFib(startNumber, endNumber):
    for cur in F():
        if cur > endNumber: return
        if cur >= startNumber:
            yield cur

for i in SubFib(10, 200):
    print i

rawDescription: How to write the Fibonacci Sequence in Python

description: Write the Fibonacci Sequence in Python


rawDescription: Add items to a dictionary of lists

description: Add items to a dictionary of lists


code:
import numpy as np
x = np.arange(1,100)
m = (sin(x)+1).mean()
s = (sin(x)+1).std()
print m, s

rawDescription: By Python. making standard deviation and mean lines with different graphes


code:
textfile = "tos 0x0, ttl 62, offset 0, length:40, bad cksum 0 (->4203)!) 150.100.15.6.49153 > 150.100.0.2.57300:"

ttlToSearchFor = 62
ipToSearchFor = "150.100.15"
ipSide = "right"

#Get rid of colon in the length record
lengthRename = textfile.replace( "length:", "length " )
#Split by row
individualRow = lengthRename.split( ":" )
#Loop for each record (assuming they're separated by colons)
for i in range( len( individualRow ) ):
    splitByComma = individualRow[i].split( "," )
    #Make sure the record is valid
    if len( splitByComma ) == 5:
        #Get the ttl or any other info here
        ttl = splitByComma[1].rstrip().lstrip()
        #Remove the ttl text and any spaces to leave the number
        ttlNumber = ttl.replace( "ttl", "" ).replace( " ", "" )
        if ttlNumber == str( ttlToSearchFor ):
            #Get IPs
            splitBySpace = individualRow[i].split( " " )
            rightIP  = splitBySpace[-1]
            leftIP = splitBySpace[-3]
            #Search the left or right (or both) IPs for a match
            if (ipSide == "right" and ipToSearchFor in rightIP) or (ipSide == "left" and ipToSearchFor in leftIP) or (ipSide == "both" and (ipToSearchFor in leftIP or ipToSearchFor in rightIP)):
                print individualRow[i]
                #You found the record

rawDescription: Python - Parsing a text file

description: Python - Parsing a text file


code: print "H\u00eatres et \u00e9tang".decode("unicode_escape")

rawDescription: Converting a latin string to unicode in python


code:
import os, sys, subprocess, time, unittest

def server():
    try:
        for _ in xrange(5, 0, -1):
            print 'ding'
            time.sleep(1)
    except KeyboardInterrupt:
        pass

class TestClient(unittest.TestCase):
    def setUp(self):
        self.server_pid = None
        pid = os.fork()
        if not pid:             # child
            return server()
        # parent
        self.server_pid = pid

    def test1(self):
        print 'test server, PID',self.server_pid
        time.sleep(2)

    def tearDown(self):
        if not self.server_pid:
            return
        import signal
        os.kill(self.server_pid, signal.SIGINT)

rawDescription: How to run a blocking process on a background thread in python

description: Run a blocking process on a background thread in python


rawDescription: converting scrapy to lxml

description: Converting scrapy to lxml


code:
def receive_data(self):
    # Wait for a connection
    print >>sys.stderr, 'waiting for a connection'
    conn, client_address= self.sock.accept()
    print "Connected to client with IP---->", client_address
    while True:
        # read fix length header
        data = recv_exactly(conn, 5)
        if not data:
            print "Connection is Broken.."
            break
        msg_type, data_len = struct.unpack('Bi', data)

        if msg_type == 1:
            # read stream into file
            with open('test.xml', 'wb') as fp:
                while data_len:
                    # read next chunk. on the last chunk, limit recv to the
                    # known amount of outstanding data
                    data = conn.recv(min(data_len, BUFFER_SIZE))
                    if not data:
                        print "ERROR: TOO FEW BYTES RECEIVED"
                        break
                    fp.write(data)
                    data_len -= len(data)

    conn.send("Data Received")
    conn.close()

rawDescription: Python Socket Receive for multiple TCP headers


code:
import requests
import time

REFERER = "http://www.walmart.com/ip/Sony-Xperia-ZL-LTE-C6506-5-Smartphone-Unlocked/24566601"
ORIGIN = "http://www.walmart.com"

timestamp = str(time.time()).replace(".", "")
URL = "http://www.walmart.com/catalog/ajaxBridgeInterface.do?timestamp=%s" % timestamp

data = {'ajaxCalls': 'AjaxUrl|/catalog/fetch_dynamic_data.do?item_id=24566602|CallbackFunction|WALMART.bot.AjaxInterface.handleSuccess_DynamicData|RtnRespType|json|timeoutSetting|300|',
        'timestamp': timestamp}

response = requests.post(URL, data=data, headers={'Referer': REFERER, 'Origin': ORIGIN})

print response.json()

rawDescription: How do I send Ajax post request using urllib2 or Mechanize after opening the page?

description: Send Ajax post request using urllib2 or Mechanize after opening the page


code:
timeList = [ '0:00:00', '0:00:15', '9:30:56' ]
totalSecs = 0
for tm in timeList:
    timeParts = [int(s) for s in tm.split(':')]
    totalSecs += (timeParts[0] * 60 + timeParts[1]) * 60 + timeParts[2]
totalSecs, sec = divmod(totalSecs, 60)
hr, min = divmod(totalSecs, 60)
print "%d:%02d:%02d" % (hr, min, sec)

rawDescription: Python sum up time

description: Python sum up time


code:
# Measure some strings:
words = ['cat', 'window', 'defenestrate']
for w in words:
    print w, len(w)

rawDescription: Loop commands equivalent in IDL to python


code:
import socket
HOST = ''
PORT = 3820
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((HOST, PORT))
socket.listen(1)
conn, addr = socket.accept()
with open('myTransfer.txt', 'wb') as file_to_write:
    while True:
        data = conn.recv(1024)
        print data
        if not data:
            break
        file_to_write.write(data)
socket.close()

rawDescription: Python Client Server Transfer .txt Not Writing to File


code:
class MetaForm(type):
    def __new__(cls, name, bases, attrs):
        print "%s: %s" % (name, attrs)
        return type.__new__(cls, name, bases, attrs)

class BaseForm(object):
    my_attr = 1
    def __init__(self):
        print "BaseForm.__init__"

class Form(BaseForm):
    __metaclass__ = MetaForm
    def __init__(self):
        print "Form.__init__"

class CustomForm(Form):
    my_field = 2
    def __init__(self):
        print "CustomForm.__init__"

f = CustomForm()

rawDescription: Why does Django use a BaseForm?

description: Why does Django use a BaseForm


code:
from IPython import parallel

rc = parallel.Client()
view = rc.load_balanced_view()

for N in range(900,1000):
    if parallel_dumb_prime(N, view, 10):
        print N

rawDescription: How to use IPython.parallel map() with generators as input to function

description: Use IPython.parallel map() with generators as input to function


code:
from BeautifulSoup import BeautifulSoup
import urllib

def getit(pagetext, howmany=0):
  soup = BeautifulSoup(pagetext)
  results = []
  dls = soup.findAll('dl')
  for adl in dls:
    thedt = adl.dt
    while thedt:
      thea = thedt.a
      if thea:
        print 'SUBJECT:', thea.string
      thedd = thedt.findNextSibling('dd')
      if thedd:
        print 'CONTENT:',
        while thedd:
          for x in thedd.findAll(text=True):
            print x,
          thedd = thedd.findNextSibling('dd')
        print
      howmany -= 1
      if not howmany: return
      print
      thedt = thedt.findNextSibling('dt')

theurl = ('http://news.search.naver.com/search.naver?'
          'sm=tab%5Fhty&where=news&query=times&x=0&y=0')
thepage = urllib.urlopen(theurl).read()
getit(thepage, 3)

rawDescription: how to extract some text by use lxml?

description: Extract some text by use lxml


code:
pattern = re.compile(ur'\s?on\w+="[^"]+"\s?')
subst = u""
result = re.sub(pattern, subst, file)

rawDescription: Remove Script tag and on attributes from HTML


code:
import pandas as pd

df = pd.DataFrame({'id': [1,2,3,4],
       'path': ["p1,p2,p3,p4","p1,p2,p1","p1,p5,p5,p7","p1,p2,p3,p3"]})

a = [ 'p1', 'p2']

# see: http://stackoverflow.com/a/470602/1407427
reg_exp = ''.join(['(?=.*%s)' % (i) for i in a])

# alternatively: print df.path.str.match(reg_exp, as_indexer=True)
print df.path.str.contains(reg_exp)

rawDescription: Pandas DataFrame search all strings in a list available in a column


rawDescription: Meaning of the following syntax if T <> (None, None) in Python 2


code:
text = 'Read word'
for x in text:
    print x

rawDescription: Read Word from a Unicode Line instead of Char


code:
num = int(raw_input('How high do you want the diamond to be? ')) #To get input

for i in range(1, num+1): #For loop to loop through numbers
        print ' '*(num-i)+'* '*i #Print the indent first, and then the stars for the diamond
for i in range(1, num+1): #Second for loop for other half of diamond
    print ' '*(i)+'* '*(num-i) #Print the indent first, and then the stars for the diamond

rawDescription: Need help new to programming using Python and I am a little confused, its more like a really confused


code:
def f(x):
   print "evaluating", x
   return (x-3)**2, 2*(x-3)
f_, fprime = func_wrapper(f)
optimize.fmin_bfgs(f_, 1000, fprime)

rawDescription: scipy.optimize.fmin_bfgs single function computes both f and fprime

description: Scipy.optimize.fmin_bfgs single function computes both f and fprime


code:
import sys
if sys.argv[1]=='hi':
    print 'Salaam'
sys.exit(0)

rawDescription: return value from python script to shell script

description: Return value from python script to shell script


code:
it1,it2 = tee(iter(a))
next(it2)
for (token, lookahead_1) in izip_longest(it1,it2):
    print token, lookahead_1

rawDescription: Implement lookahead iterator for strings in Python


code:
import re

file=["Cat_Setup_v91.mb", "Cat_Setup_v01.mb", "Cat_Setup_v119.mb"]

print sorted(file, key=lambda x: int(re.findall("(?<=v)\d+", x)[0]))

rawDescription: Python - sorting incremented files

description: Python - sorting incremented files


code: print sorted(example_dict,key=lambda x: len(set(example_dict[x])))

rawDescription: Python: Sorted representation of dict of lists, sorted by number of elements in list, after removing duplicates


rawDescription: How do I count and PRINT digits that start with triple figures such as 001,002,003,004 etc. in python?

description: Count and PRINT digits that start with triple figures such as 001,002,003,004 etc. in python


code:
import jinja2

def include_file(name):
    return jinja2.Markup(loader.get_source(env, name)[0])

loader = jinja2.PackageLoader(__name__, 'templates')
env = jinja2.Environment(loader=loader)
env.globals['include_file'] = include_file

def render():
    return env.get_template('page.html').render()

if __name__ == '__main__':
    print render()

rawDescription: Insert static files literally into Jinja templates without parsing them


code:
import numpy as np
from numpy.random import rand, randint

print "integers"
c = randint(0, high=20, size=(101,101))
access_write_int(c, 5000)
print "32 bit float"
c = rand(101, 101).astype(np.float32)
access_write_float(c, 5000)
print "double precision"
c = rand(101, 101)
access_write_double(c, 5000)

rawDescription: Fast 2 dimensional array of floats for python (access/write)


code:
def stephenPochmann():
    for needle in search_space:
        if needle in s: print needle

rawDescription: Optimize runtime of calling 'in' many times on same string


code:
# Won't work for integers with more than this many digits, or negative integers.
MAX_DIGITS = 10
def _keyify(x):
    try:
        xi = int(x)
    except ValueError:
        return 'S{0}'.format(x)
    else:
        return 'I{0:0{1}}'.format(xi, MAX_DIGITS)

for k in sorted(a, key=_keyify):
    print k, a[k] # or whatever.

rawDescription: Sorting a dictionary having keys as string of numbers in python


code:
firstItemDate = lambda x:date(*map(int,(x.split(",",1)[0].split("/"))[::-1]))
print sorted(data[1:],key=firstItemDate)

rawDescription: Sorting csv column by date

description: Sorting csv column by date


code:
greeting = ['hello','my','name','is','bob','how','are','you','my','name','is']

def find_sub_list(sl,l):
    results=[]
    sll=len(sl)
    for ind in (i for i,e in enumerate(l) if e==sl[0]):
        if l[ind:ind+sll]==sl:
            results.append((ind,ind+sll-1))

    return results

print find_sub_list(['my','name','is'], greeting)
# [(1, 3), (8, 10)]

rawDescription: Find starting and ending indices of sublist in list


code:
import BeautifulSoup

if __name__ == "__main__":
    data = """

some title

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

Aliquam tincidunt mauris eu risus.

Vestibulum auctor dapibus neque.

    """
    soup = BeautifulSoup.BeautifulSoup(data)
    h2 = soup.find('h2')
    h2.name = 'h1'
    print soup

rawDescription: How to change tag name with BeautifulSoup?

description: Change tag name with BeautifulSoup


code:
# list labels for columns
columns = ['Masculine', 'Neuter', 'Feminine', 'Plural']

# list labels for rows
rows = ['Nominative', 'Accusative', 'Dative', 'Genitive']

# create a grid of answers for combination of column-rows
answers = [['der', 'das', 'die', 'die'],
        ['den', 'das', 'die', 'die'],
        ['dem', 'dem', 'der', 'den'],
        ['des', 'des', 'der', 'der']]

def correct_answer(column_name, row_name):
    # get correct indexes
    column_index = columns.index(column_name)
    row_index = rows.index(row_name)
    # used indexes to get answer
    return answers[row_index][column_index]

print correct_answer('Masculine', 'Accusative')

rawDescription: How do I find the attribute to two separate words in Python?

description: Find the attribute to two separate words in Python


code:
import socket

try:
    socket.gethostbyname('www.google.com')
except socket.gaierror as ex:
    print "not existe"

rawDescription: Python verify url goes to a page

description: Python verify url goes to a page


code:
import urllib2
import json

url2 = 'http://api.openweathermap.org/data/2.5/find?q=london,PK&units=metric'
data = urllib2.urlopen(url2)

# read the contents in and parse the JSON.
jsonData = json.loads(data.read())

# print it out nicely formatted:
print json.dumps(jsonData, sort_keys=True, indent=4, separators=(',', ': '))

rawDescription: Unable to format the output of a link in python


code:
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
import csv

# 1. reading the csv
status_records = {'1': {}, '-11': {}}

with open('data.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile)
    # 2. iterate through csv
    for row in reader:
        if row[0] == 'start_time': continue # first line
        hour = row[0][:-5]
        status = row[1]

        # if hour not present, add empty 'slot' in each status bin
        if hour not in status_records[status].keys():
            status_records['1'][hour] = 0
            status_records['-11'][hour] = 0
            status_records[status][hour] = 1 # add the status we just read
        else:
            status_records[status][hour] += 1 # update status-hour bin

status1   = status_records['1'].values()
status2 = status_records['-11'].values()

print status1, status2

N = len(status1)
ind = np.arange(N)
width = 0.35

p1 = plt.bar(ind, status1, width, color='g')
p2 = plt.bar(ind, status2, width, color='r', bottom=status1)

plt.ylabel('# of exit status')
plt.title('Exit status in comparison with time')
plt.yticks(np.arange(0,11,10))
plt.legend((p1[0], p2[0]), ('1', '-11'))
plt.show()

rawDescription: plot stacked bar chart from csv file using python

description: Plot stacked bar chart from csv file using python


code:
import re
import requests

def getVideosLinks(content):
    videos = re.findall('(http://[\.\w/_]+\.mp[34])', content)
    return videos

def prepareJSONurl(episode_hash):
    json_url = "http://pepto.portico.net2.tv/playlist/{hash}".format(hash=episode_hash)
    return json_url

def extractEpisodeHash(content):
    episode_hash = re.findall('<meta http-equiv="refresh" content="0; url=http:\/\/player\.net2\.tv\?episode=([\w]+)&restart', content)[0]
    return episode_hash

def extractIframeURL(content):
    iframe_url = None
    try:
        iframe_url = re.findall('<iframe src="(.*)" style', content)[0]
        is_video = True
    except:
        is_video = False
    return is_video, iframe_url

POPSCI_URL = "http://www.popsci.com/thorium-dream"

response = requests.get(POPSCI_URL)
is_video, iframe_url = extractIframeURL(response.content)

if is_video:
    response_from_iframe_url = requests.get(iframe_url)
    episode_hash = extractEpisodeHash(response_from_iframe_url.content)

    json_url = prepareJSONurl(episode_hash)
    final_response = requests.get(json_url)

    for video in getVideosLinks(final_response.content):
        print "Video: {}".format(video)
else:
    print "This is not a POPSCI video page :|"

rawDescription: How to scrape the video src url from video tag which is injected via javascript?

description: Scrape the video src url from video tag which is injected via javascript


code:
cur.execute("SELECT video_info, COUNT(distinct src_ip) "
            "FROM videoinfo "
            "GROUP BY video_info "
            "ORDER BY COUNT(DISTINCT src_ip) DESC")

counter = dict(cur)
for n in xrange(1, max(counter.itervalues()) + 1):
    perc = 100. / sum(1 for nb in counter.itervalues() if nb == n) / len(counter)
    if perc:
        print '%.f%% videos have been downloaded %d times' % (perc, n)

rawDescription: calculation in Mysql

description: Calculation in Mysql


code:
import sys
import os

try:
    Sfile = open("states.txt","r")
except IOError:
    print "Could not open file", "states.txt"
    sys.exit()
States = []

ReadLine = Sfile.readline()
while ReadLine != "":
    SN, SS, AS = ReadLine.split(",")
    States.append((SN, bool(int(SS)), bool(int(AS))))
    ReadLine = Sfile.readline()

print "States:\n", States, "\n"
Sfile.close()

try:
    Tfile = open("transitions.txt","r")
except IOError:
    print "Could not open file", "transitions.txt"
    sys.exit()
Transitions = []

ReadLine = Tfile.readline()
while ReadLine != "":
    ReadLine = ReadLine.rstrip()
    CS, IN, NS = ReadLine.split(",")
    Transitions.append((CS, IN, NS))
    ReadLine = Tfile.readline()

print "Transitions:\n", Transitions, "\n"
Tfile.close()

try:
    Strfile = open("strings2.txt","r")
except IOError:
    print "Could not open file", strings2.txt
    sys.exit()
Strings = []

ReadLine = Strfile.readline()
while ReadLine != "":
    Readline = ReadLine.rstrip()
    Strings.append(Readline)
    ReadLine = Strfile.readline()

print "Strings:\n", '\n'.join(Strings), '\n'
Strfile.close()

CurrentState = ''
Start = ''
RejectState= ''
AcceptState= ''

for S in Strings:
    if S != '':
            print "String:", S
            for C in S:
                print "Char:", C
                for (CS, IN, NS) in Transitions:
                    if CS == CurrentState and IN == C:
                            CurrentState =NS
                            break
                for  (SN, SS, AS) in States:
                    if SN == CurrentState and SS ==C:
                        CurrentState = NS

    if NS == AS:
            NS = AcceptState
            print "String", AcceptState, "is accepted"
    else:
            NS = RejectState
            print "String", RejectState, "is rejected"

rawDescription: Python Finite State Machine Issues (Skipping the Proccessing?)


code:
import pp

class TestClass(object):
    def __init__(self):
        pass
    def doSomething (self, number) :
        print number**2

class PPTask (object) :
    def __init__ (self) :
        pass
    def ppTask(self, number = 1) :
        sum = 0
        sum += number
        tc = pptest.TestClass()
        tc.doSomething(sum)
        return sum

if __name__ == '__main__' :
    job_server = pp.Server()
    job_list = []
    results = []
    for i in xrange(10) :
        pt = PPTask()
        job_list.append(job_server.submit(pt.ppTask, (1,), modules=('pptest',)))
    for job in job_list :
        results.append(job())
    for result in results :
        print result

rawDescription: Parallel Python - create objects into another class in parallel execution


code:
from lxml import etree

tree = etree.parse('test.xml')
NAMESPACES = {'p': 'http://schemas.openxmlformats.org/presentationml/2006/main',
              'a': 'http://schemas.openxmlformats.org/drawingml/2006/main'}

path = tree.xpath('/body/a:p', namespaces=NAMESPACES)

for outer_item in path:
    parts = []
    for item in outer_item.xpath('./a:r/a:rPr', namespaces=NAMESPACES):
        parts.append(item.getparent().xpath('./a:t/text()', namespaces=NAMESPACES)[0])

    print " ".join(parts)

rawDescription: text extraction using python lxml looping issue

description: Text extraction using python lxml looping issue


code:
objects = ['pencil','pen','keyboard','table','phone']
colors  = ['red','green','blue']
for n, obj in enumerate(objects):
    print n, obj, colors[n % len(colors)]

from itertools import cycle
for n, (obj, color) in enumerate(zip(objects, cycle(colors))):
    print n, obj, color

rawDescription: Python: Associate for loop with a list

description: Python: Associate for loop with a list


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

def SplitSets(iterable,set_size):
  #return a generator
  return ((i/set_size, i%set_size,e) for i,e in enumerate(iterable))

for a,b,c in SplitSets(items,3):
  print a,b,c

rawDescription: Python: Split list in to sets

description: Python: Split list in to sets


code:
def check_internet():
    for timeout in [1,5,10,15]:
        try:
            print "checking internet connection.."
            socket.setdefaulttimeout(timeout)
            host = socket.gethostbyname("www.google.com")
            s = socket.create_connection((host, 80), 2)
            s.close()
            print 'internet on.'
            return True

        except Exception,e:
            print e
            print "internet off."
    return False

rawDescription: Checking internet connection with Python


code:
import urllib2
import json
access_token = 'YOUR_ACCESS_TOKEN_GOES_HERE'
request = urllib2.Request('https://graph.facebook.com/263738183707778/?fields=feed.fields%28likes.summary%28true%29.limit%281%29%29.limit%28500%29&access_token='+access_token)
data = urllib2.urlopen(request).read()
data_parsed = json.loads(data)

for a in data_parsed['feed']['data']:
    if 'likes' in a:
        if int(a['likes']['summary']['total_count'])>1000:
            print a['id'].replace('263738183707778_','https://www.facebook.com/ilovetrolls/posts/')

data_parsed = data_parsed['feed']
while 'paging' in data_parsed:
    request = urllib2.Request(data_parsed['paging']['next'])
    data = urllib2.urlopen(request).read()
    data_parsed = json.loads(data)
    for a in data_parsed['data']:
        if 'likes' in a:
            if int(a['likes']['summary']['total_count'])>1000:
                print a['id'].replace('263738183707778_','https://www.facebook.com/ilovetrolls/posts/')

rawDescription: Changing the date range and running in loop


code:
class Meta(type):
    def __new__(cls, name, bases, dict_):
        new_class = type.__new__(cls, name, bases, dict_)
        new_class._derived = {}
        if isinstance(bases[0], Meta):
            bases[0]._derived[name] = new_class
        return new_class

class Magic(object):
    __metaclass__ = Meta

class A(Magic):
    pass

class B(Magic):
    pass

print Magic._derived

rawDescription: Dictionary of class subclasses

description: Dictionary of class subclasses


code:
bad_string = "You have to be vewy, vewy twicky to twap a wascally wabbit"
correct_string = bad_string.replace('w', 'r')
print correct_string

rawDescription: Python & Strings/Loops help please

description: Python & Strings/Loops help please


code:
from xml.etree import ElementTree as ET

source = '''<root>
<level>
  <name>Matthias</name>
  <age>23</age>
  <gender>Male</gender>
</level>
<level>
  <name>Foo</name>
  <age>24</age>
  <gender>Male</gender>
</level>
<level>
  <name>Bar</name>
  <age>25</age>
  <gender>Male</gender>
</level>
</root>'''

root = ET.fromstring(source)
levels = root.findall('.//level')
for level in levels:
    name = level.find('name').text
    age = level.find('age').text
    print name, age

rawDescription: Finding and Extracting Data using XML in Python


code:
if ret_0():
    print 'this will not be printed'
else:
    print '0 is boolean False'

if ret_None():
    print 'this will not be printed'
else:
    print 'None is also boolean False'

rawDescription: Why would a function end with "return 0" instead of "return" in python?


code:
import re
import difflib

def merge(string1, string2):
  diffs = difflib.SequenceMatcher(None, string2, string1).get_opcodes()
  result = ''
  last_index = 0
  for diff in [x for x in diffs if x[0] in ['insert', 'replace']]:
    if diff[0] == 'replace':
      result += string2[last_index:diff[1]] + string1[diff[3]:diff[4]] + string2[diff[1]:diff[2]]
    else:
      result += string2[last_index:diff[1]] + string1[diff[3]:diff[4]]
    last_index = diff[2]
  result += string2[last_index:]
  return result

original = "PROCEDURE TEST (X: STRING; {Hi, I'm a comment} Y: INT {me too!})"
stripped = re.sub(r'\s*\{[^}]*\}\s*', '', original)

if re.search('PROCEDURE TEST \(X: STRING;Y: INT\)', stripped):
  correct = stripped.replace('Y', 'CONST Y')
  print merge(original, correct)

rawDescription: python regex ignoring text

description: Python regex ignoring text


code:
t = time.gmtime(t)
print "%d/%d/%2d" % (t.tm_mon, t.tm_mday, t.tm_year % 100)

rawDescription: Reading a DWORD Date From Registry Using Python & _winreg


rawDescription: Matrices generation in Python

description: Matrices generation in Python


code:
def do_something_with_req(self):
  print self.req.getcode()

rawDescription: Python\Tkinter: Use the variable stored in a function of a class, outside the class


code:
import Tkinter as Tk

def countlines(event):
    (line, c) = map(int, event.widget.index("end-1c").split("."))
    print line, c

root = Tk.Tk()
root.geometry("200x200")
a = Tk.Text(root)
a.pack()
a.bind("<KeyRelease>", countlines)

root.mainloop()

rawDescription: Get of number of lines of a Text tkinter widget


code:
import re

fp = open('class_data.txt')
lines = fp.read(-1)
fp.close()

records = re.split('\n\s*\n', lines) #Split all the records
#print len(records)
for record in records:
    data =  record.split('\n')
    classid, classtime, top_students = data[0], data[1], re.split('^[A-Za-z ]*:', data[2])[1].split(',')
    attendance, score, completed = re.split('^[A-Za-z ]*:', data[4])[1], re.split('^[A-Za-z ]*:', data[5])[1], data[6]
    print classid, classtime, top_students, len(top_students), attendance, score, completed

rawDescription: Appending data to text file dependant on if statment


rawDescription: How to search nested list grid and give lettered coordinates in Python?

description: Search nested list grid and give lettered coordinates in Python


code:
def last_names(d):
    for v in d.values():
        print v[1][0]

last_names(sampleDict)    # Brown
                          # Jones

rawDescription: How to iterate through a list in a dictionary in python

description: Iterate through a list in a dictionary in python


code:
for B in A:
    print B.renderContents()[0]

rawDescription: Data extraction from webpage by beatifulsoup python


code:
p = soup.find("td", {"id": "overview-top"}).find("p", {"itemprop": "description"})

print ''.join(text for text in p.find_all(text=True)
              if text.parent.name != "a")

rawDescription: BeautifulSoup exclude content within a certain tag(s)


code:
def mytest(x, y):
    print x
    print y

rawDescription: Functioned called from another script returning results twice?


code:
def createEmptyGrid(grid_dimensions):
    grid = {}
    for x in range(grid_dimensions):
        for y in range(grid_dimensions):
            grid[x,y] = '.'
    return grid

def initial_grid(cell_dimensions, density, disease):
    grid = createEmptyGrid(cell_dimensions)
    return grid

def print_grid(grid, cell_dimensions):
    for y in range(cell_dimensions):
        for x in range(cell_dimensions):
            print grid[x,y], # note the comma
        print '' # start a new line after each row

grid = initial_grid(3, 0, 0)
print_grid(grid, 3)

rawDescription: How to make a grid from a dictionary?

description: Make a grid from a dictionary


code:
import requests
import lxml.html
import random
import sys

class pr_node:
        """WDM PR node"""

        url = ""
        links_to_node = set([])
        links_from_node = set([])
        PR_score = 0.01
        ttl_time = 0
        last_delta = 0  # used for debug only

        def __init__(self, url, ttl_time):
            """CTOR"""
            self.url = url
            self.links_to_node = set([])
            self.links_from_node = set([])
            self.PR_score = 0.01
            self.ttl_time = ttl_time

        def print_node_out_links(self):
            """print for q1a"""
            print "\n\n" + self.url + " with ttl " + str(self.ttl_time) + " = "
            s = self.links_to_node
            print "{" + "\, ".join(str(e) for e in s) + "}"

        def print_node_pr(self):
            """print for q1b"""
            print "\n\n" + self.url + " PR is: " + str(self.PR_score)

        def print_all(self):
            """print for q1b and debug"""
            print "url: " + self.url
            print "links_to_node: " + repr(self.links_to_node)
            print "links_from_node: " + repr(self.links_from_node)
            print "PR_score: " + str(self.PR_score)
            print "ttl_time: " + str(self.ttl_time)
            print "last_delta: " + str(self.last_delta)

def crawl(url, url_ttl):
        """crawl to new url, if ttl == 0 max depth reached, don't visit same url twice"""
        if url_ttl > 0 and (url not in visited_urls):

            # create new node p from parsed page
            print "crawling to " + url + "...\n"
            res = requests.get(url)
            doc = lxml.html.fromstring(res.content)
            p = pr_node(url, url_ttl)

            # add new PR node
            global pr_nodes
            pr_nodes[url] = p

            # get all wiki links, format to legit URL
            all_links_to_node = set([])
            for t in doc.xpath("//a[contains(@href, '/wiki/')]"):
                add_val = ""
                if not t.attrib['href'].startswith("http://") and t.attrib['href'].startswith("/wiki/"):
                    add_val = "http://en.m.wikipedia.org" + t.attrib['href']
                    all_links_to_node.add(add_val)
                elif t.attrib['href'].startswith("http://"):
                    add_val = t.attrib['href']
                    all_links_to_node.add(add_val)
                else:
                    pass

            # select random 10 of them and crawl to them
            iter_count = 0
            iter_stop_lim = 10
            while iter_count < iter_stop_lim and len(p.links_to_node) < iter_stop_lim and len(all_links_to_node) > 0:
                    # sample random site of linked sites
                    current_url = random.sample(all_links_to_node, 1)[0]
                    # don't sample it twice...
                    all_links_to_node.remove(current_url)
                    iter_count = + 1

                    # crawl if hav'nt been there and TTL enables you to check it
                    if not (current_url in visited_urls) and url_ttl > 1:
                        p.links_to_node.add(current_url)
                        crawl(current_url, url_ttl - 1)
                        visited_urls.add(url)

                    # if reached with TTL == 1 just check links to existing nodes
                    elif current_url in visited_urls and url_ttl == 1:
                        p.links_to_node.add(current_url)

        else:
            print "max depth reached or you've already been here"
        return

def calc_graph_pr(pr_nodes, pr_iter_count, damp_factor):
    """calculate and print the graph's PageRank"""
    current_iter = 0

    # use two graph copies to prevent auto-interference
    g1 = {}
    g2 = {}
    for node in pr_nodes.itervalues():
        g1[node.url] = node
        g2[node.url] = node

    g = [g1, g2]

    # do actual page rank here
    while current_iter < pr_iter_count:
        for p in g[current_iter % 2].itervalues():
            in_links_addition = 0
            # iterate over all pointing nodes and sum their PR/out_link_count
            for l in p.links_to_node:
                l_val = g[(current_iter - 1) % 2][l]
                l_val.delta = l_val.PR_score - g[current_iter % 2][l].PR_score
                in_links_addition += l_val.PR_score/len(l_val.links_from_node)
            # update w.r.t the computed sum and damp_factor
            p.PR_score = damp_factor + (1 - damp_factor) * in_links_addition
        current_iter += 1

    # WLOG could be also g[1]...
    pr_nodes = g[0]

    for p in pr_nodes.itervalues():
        p.print_node_pr()

    return

def update_graph_links():
    """register each node with neighbours pointing at him"""
    global pr_nodes
    for node in pr_nodes.itervalues():
        for u in node.links_to_node:
            if u in pr_nodes:
                pr_nodes[u].links_from_node.add(node.url)
    return

if __name__ == '__main__':

    urlToCrawl = "http://en.m.wikipedia.org/wiki/Nikola_Mitrovic"

    # crawl to the requested site as default
    if len(sys.argv) > 2:
        sys.exit("Unexpected input")
    elif len(sys.argv) == 1:
        pass
    else:
        urlToCrawl = sys.argv[1]

    print_q1a = False
    print_q1b = True

    # set global data structures for crawling and ranking
    visited_urls = set([])
    pr_nodes = {}

    # parameters for PageRank
    glob_pr_iter_count = 100
    glob_damp_factor = 0.2

    # perform crawl in depth 3
    crawl(urlToCrawl, 3)

    if print_q1a:
        for p in pr_nodes.itervalues():
            p.print_node_out_links()

    elif print_q1b:
        # first update the backlinks then start ranking
        update_graph_links()
        calc_graph_pr(pr_nodes, glob_pr_iter_count, glob_damp_factor)
    else:
        pass

rawDescription: PageRank toy example fails to converge

description: PageRank toy example fails to converge


code:
from datetime import timedelta
from collections import defaultdict

def add_datum( dd, v ):
    end_dt = get_datetime(v['endTime'])
    start_dt = get_datetime(v['startTime'])
    total_duration = end_dt - start_dt

    hour_start = datetime( year = start_dt.year,
                           month = start_dt.month,
                           day = start_dt.day,
                           hour = start_dt.hour )
    hour_end = hour_start + timedelta( hours = 1 )

    while hour_start < end_dt:
        dt = min([hour_end, end_dt]) - max([ hour_start, start_dt ])
        fraction = 1.0 * dt.total_seconds() / total_duration.total_seconds()
        dd[ hour_start ]['hour'] = hour_start
        dd[ hour_start ]['entries'] += v['entriesDuringPeriod'] * fraction
        dd[ hour_start ]['exits'] += v['exitsDuringPeriod'] * fraction # exits

        hour_start = hour_end
        hour_end = hour_end + timedelta( hours = 1 )
    return dd

dd = defaultdict(lambda: {'entries':0,'exits':0})
all_data = [{ 'endTime': '03-25-14T21:40:30',
              'entriesDuringPeriod': 158,
              'exitsDuringPeriod': 597,
              'startTime': '03-25-14T17:03:23' },
            { 'endTime': '03-26-14T01:00:00',
              'entriesDuringPeriod': 29,
              'exitsDuringPeriod': 235,
              'startTime': '03-25-14T21:00:00' }]

[ add_datum( dd, i ) for i in all_data ]
res = dd.values()
res.sort( key = lambda i: i['hour'] )

print res
# [{'entries': 32.28038732182594,
#   'exits': 121.97083057677271,
#   'hour': datetime.datetime(2014, 3, 25, 17, 0)},
#  {'entries': 34.209418415829674,
#   'exits': 129.25963793829314,
#   'hour': datetime.datetime(2014, 3, 25, 18, 0)},
#  {'entries': 34.209418415829674,
#   'exits': 129.25963793829314,
#   'hour': datetime.datetime(2014, 3, 25, 19, 0)},
#  {'entries': 34.209418415829674,
#   'exits': 129.25963793829314,
#   'hour': datetime.datetime(2014, 3, 25, 20, 0)},
#  {'entries': 30.34135743068503,
#   'exits': 146.00025560834786,
#   'hour': datetime.datetime(2014, 3, 25, 21, 0)},
#  {'entries': 7.25,
#   'exits': 58.75,
#   'hour': datetime.datetime(2014, 3, 25, 22, 0)},
#  {'entries': 7.25,
#   'exits': 58.75,
#   'hour': datetime.datetime(2014, 3, 25, 23, 0)},
#  {'entries': 7.25,
#   'exits': 58.75,
#   'hour': datetime.datetime(2014, 3, 26, 0, 0)}]

rawDescription: Split value into bins based on time

description: Split value into bins based on time


rawDescription: How to turn a Dataframe into an Array with certain specifications on Python Pandas

description: Turn a Dataframe into an Array with certain specifications on Python Pandas


rawDescription: what is difference on iterating differently in python dictionaries?

description: What is difference on iterating differently in python dictionaries


code:
def to_tag(k, v):
    """Create a new tag for the given key k and value v"""
    return '<{key}>{value}<{key}/>'.format(key=k, value=get_content(k, v))

def get_content(k, v):
    """Create the content of a tag by deciding what to do depending on the content of the value"""
    if isinstance(v, str):
        # it's a string, so just return the value
        return v
    elif isinstance(v, dict):
        # it's a dict, so create a new tag for each element
        # and join them with newlines
        return '\n%s\n' % '\n'.join(to_tag(*e) for e in v.items())
    elif isinstance(v, list):
        # it's a list, so create a new key for each element
        # by using the enumerate method and create new tags
        return '\n%s\n' % '\n'.join(to_tag('{key}-{value}'.format(key=k, value=i+1), e) for i, e in enumerate(v))

d = {'Employee':{ 'Id' : 'TA23434', 'Name':'Kesavan' , 'Email':'k7@gmail.com' , 'Roles':[ {'Name':'Admin' ,'RoleId':'xa1234' },{'Name':'Engineer' , 'RoleId':'xa5678' }], 'Test':{'a':'A','b':'b'} }}

for k,v in d.items():
    print to_tag(k, v)

rawDescription: Convert python dictionary to xml string without using build-in functions


code:
from bs4 import BeautifulSoup

html = """

      Tests
      Failures
      Success Rate
      Average Time
      Min Time
      Max Time

     103
     24
     76.70%
     71 ms
     0 ms
     829 ms

"""

soup = BeautifulSoup(html)
table = soup.find("table", attrs={"class":"details"})

# The first tr contains the field names.
headings = [th.get_text() for th in table.find("tr").find_all("th")]

datasets = []
for row in table.find_all("tr")[1:]:
    dataset = zip(headings, (td.get_text() for td in row.find_all("td")))
    datasets.append(dataset)

print datasets

rawDescription: Extracting data from HTML table

description: Extracting data from HTML table


code:
from __future__ import division

import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

def ramp(t, temp_init, temp_final, t0, t1):
    slope = (temp_final - temp_init) / (t1 - t0)
    y = temp_init + np.minimum(slope * np.maximum(t - t0, 0.0), temp_final - temp_init)
    return y

np.random.seed(123)

dt = 0.5
t = np.arange(0, 100, dt)

# Generate a sample to work with.
temp_init = 20.0 + np.random.randn()
temp_final = 40.0 + np.random.randn()
t0 = 25.0
t1 = t0 + 40
y = ramp(t, temp_init, temp_final, t0, t1)
y += 0.25*np.random.randn(*t.shape)  # Add noise.

# Create an initial guess for the four parameters and use curve_fit
# to fit the ramp function to the sample.
T = t[-1] - t[0]
p0 = (20, 40, t[0] + 0.333*T, t[0] + 0.667*T)
popt, pcov = curve_fit(ramp, t, y, p0=p0)
fit_temp_init, fit_temp_final, fit_t0, fit_t1 = popt

print "             Input    Fit"
print "temp_init   %6.2f  %6.2f" % (temp_init, fit_temp_init)
print "temp_final  %6.2f  %6.2f" % (temp_final, fit_temp_final)
print "t0          %6.2f  %6.2f" % (t0, fit_t0)
print "t1          %6.2f  %6.2f" % (t1, fit_t1)

plt.plot(t, y, 'ro', alpha=0.15)
plt.plot(t, ramp(t, popt[0], popt[1], popt[2], popt[3]), 'k-', linewidth=1.5)
plt.grid(True)
plt.xlabel('t', fontsize=12)
plt.show()

rawDescription: Synchronizing Data in Python (or Excel)

description: Synchronizing Data in Python (or Excel)


code:
def okClicked(self, event):
    date = self.calend.GetDate()
    print date
    return date

rawDescription: wxpython event handling between multiple classes

description: Wxpython event handling between multiple classes


code:
variable = "stuff"
print "I would like to print {}".format(variable)

mapping = {'answer': 42, 'interest': 0.815}
print '''\
The answer to the ultimate question: {m[answer]:>10d}
Rate: {m[interest]:03.2%}!'''.format(m=mapping)

rawDescription: In Python what is it called when you use enclose a variable between 2 plus signs?


code: print "result is:", b.twomore(5)

rawDescription: Python: how to enable a called file to access functions defined in the calling file?


code:
result = []
for i in range(0,5):
   result.append((i, i+1))

print result

rawDescription: How would I use a attribute or a element that will split my data from adding with each other


code:
from itertools import cycle, izip
from operator import add, sub
from string import printable as alphabet

def vigenere(text, key, mode='encode'):
    alen = len(alphabet)
    key = cycle(key + key[-2:0:-1])
    op = add if mode == 'encode' else sub
    return ''.join(alphabet[op(alphabet.index(c),
                               alphabet.index(k)) % alen]
                   for c, k in izip(text, key))

c = vigenere('attack at dawn', 'secret', mode='encode')
print c  # CHFBqN8BF8FoIO
print vigenere(c, 'secret', mode='decode')

rawDescription: Python cypher: how to build a complexer shifter system?


code:
from collections import defaultdict
file2freq = defaultdict(int)
for c,d in zip(C,D):
    file2freq[c,d] += 1
for a,b in zip(A,B):
    print a,b,file2freq[a,b]

rawDescription: Counting the intersection of equivalent rows in two tables


code:
for d in dict_lst:
    dict_elem = d['a']
    print dict_elem

rawDescription: Refer to dictionary element in 'for' loop


code:
name = 'Programmstrukturen 2'
num1=5
output=''
for x in range(num1):
    for i in range(num1-1):
        output+=name[x+num1*i]    #you could simplify the long line like this

print output

rawDescription: Cut off string after a certain length

description: Cut off string after a certain length


code:
from SimpleHTTPServer import SimpleHTTPRequestHandler
import BaseHTTPServer
from urlparse import urlparse, parse_qs

class CustomHTTPRequestHandler(SimpleHTTPRequestHandler):
    def do_GET(self):
        query_string = urlparse(self.path).query
        fields = parse_qs(query_string) if query_string else {}
        if fields:
            print "username = {}, password = {}".format(fields.get('username')[0], fields.get('password')[0])
        return SimpleHTTPRequestHandler.do_GET(self)

if __name__ == '__main__':
    BaseHTTPServer.HTTPServer(('', 8008), CustomHTTPRequestHandler).serve_forever()

rawDescription: connect python server and html

description: Connect python server and html


code: print next(div.stripped_strings, u'')

rawDescription: python beautiful soup get Chinese immediately after class tag

description: Python beautiful soup get Chinese immediately after class tag


code:
import smtplib
import auth as auth
import ProtocolModel1 as PM
from elixir import *
from datetime import datetime

au = auth.UserAuth()
setup_all()
create_all()
table = PM.Protocol
records = table.query.all()

if len(records) == 0:
    print 'No records in Table'
else:
    for record in records:
        setup_all()
        if record.approved == 1:
            print 'Completed'
        else:
            FROMADDR = au.google_user
            LOGIN    = FROMADDR
            PASSWORD = au.google_password
            TOADDRS  = record.investigator.email
            SUBJECT  = "Weekly Research Update for " + record.irb_no + " - " + record.title
            date1 = datetime.strptime(str(record.action_date), '%Y-%m-%d')
            date2 = date1.strftime('%B %d, %Y')
            msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
            msg += 'Dear' + ' ' + record.investigator.prefix + ' ' + record.investigator.fname + ' ' + record.investigator.lname + ',' + '\n' + '\n'
            msg += 'Our records indicate that the most recent action on your research protocol titled ' + record.title + ' was taken on ' + str(date2) +'.' +'\n'
            msg += 'This action was: ' + record.action

            server = smtplib.SMTP('smtp.gmail.com', 587)
            server.set_debuglevel(1)
            server.ehlo()
            server.starttls()
            server.login(LOGIN, PASSWORD)
            server.sendmail(FROMADDR, TOADDRS, msg)
            server.quit()

rawDescription: Using python and elixir to send emails utilizing db tables with foreign keys


code:
import numpy as np
import pyproj4 as p4
np.random.seed(22)

## Python Implementation ##
dt = np.dtype([('field1', '<f8', (1,2)), ('field2', '<f8', (2,2)), ('field3', '<f8', (3,1))])
x = np.zeros(2, dtype=dt)

for name in x.dtype.names:
    m,n,p = x[name].shape
    x[name] = np.random.randn(m,n,p)
    it = np.nditer(x[name], ['c_index'], ['readonly'])
    for num in it:
        if it.index==0:
            print '{0:6s} {1:6d}  {2: 2.4f}'.format(name, it.index, num.item())
        else:
            print '{0:6s} {1:6d}  {2: 2.4f}'.format(' ', it.index, num.item())
print '-----------------------'
## C-API Implementation ##
p4.readarray(x)

rawDescription: Access a Numpy Recarray via the C-API

description: Access a Numpy Recarray via the C-API


code:
a = 'xxx xxx.suffix xxx xxx.suffix'
l = a.split()
patt = re.compile(r'(.*?)(\.suffix)?$')
print [patt.match(i).groups() for i in l]

rawDescription: Python Regex: How to match filenames with optional suffix? Why '(.*?)(\.suffix)?' doesn't work?


code:
print driver.title #to interact with the first driver
print second_driver.title #to interact with the second driver

rawDescription: How to open a new window on a browser using Selenium WebDriver for python?

description: Open a new window on a browser using Selenium WebDriver for python


code:
start = 1
G = nx.convert_node_labels_to_integers(G,first_label=start)
print G.nodes()

rawDescription: What is the most efficient way to move/rename a node in NetworkX?


code:
import numpy as np
from nltk import ngrams

def everygrams(sequence):
    """
    This function returns all possible ngrams for n
    ranging from 1 to len(sequence).
    >>> list(everygrams('a b c'.split()))
    [('a',), ('b',), ('c',), ('a', 'b'), ('b', 'c'), ('a', 'b', 'c')]
    """
    for n in range(1, len(sequence)+1):
        for ng in ngrams(sequence, n):
            yield ng

doc1 = "Singularity is still a confusing phenomenon in physics".split()
doc2 = "Quantum theory still wins over String theory".split()
_vec1 = list(everygrams(doc1))
_vec2 = list(everygrams(doc2))
# Create a full dictionary of all possible ngrams.
vec_dict = list(set(_vec1).union(_vec2))
print 'Vector Dict:', vec_dict, '\n'
# Now vectorize the documents
vec1 = [1 if ng in _vec1 else 0 for ng in vec_dict]
vec2 = [1 if ng in _vec2 else 0 for ng in vec_dict]
print 'Vectorzied:', vec1, vec2, '\n'
print 'Similarity:', np.dot(vec1, vec2), '\n'

rawDescription: Normalize ranking score with weights

description: Normalize ranking score with weights


code:
pixels = im.load()
print pixels[x, y]
pixels[x, y] = value    #value is a tuple in this format (normally) (235,154,124) or (Red,Green,Blue)

rawDescription: Getting Width and Height of an Inputted Picture in Python


code:
def set_increment(self):
    selection = "You selected the option " + str(self.var_x.get())
    self.var_freq_Hz.config(increment = self.var_x.get())
    print selection
    return

rawDescription: Radiobutton modifying increment values in spinbox Python


code:
import numpy as np
aa=np.arange(1, 366)
bb=np.array([355, 136, 155,154 ])
c=36

def short(bb,c):
    x0= np.min(np.abs(bb-c))
    x1= np.min(np.abs(366-bb+c))
    if x0<x1:
        return bb[np.argmin(bb-c)]
    else:
        return bb[np.argmin(366-bb+c)]

bb=np.array([355, 136, 155,154 ])
print short(bb,c)

bb=np.array([355, 136, 155,154,38])
print short(bb,c)

rawDescription: Find closest number in a circular array

description: Find closest number in a circular array


code:
import apsw

conn = apsw.Connection(':memory:')
curs = conn.cursor()
curs.execute("CREATE TABLE foo (id INTEGER, name VARCHAR(255))")
curs.executemany("INSERT INTO foo VALUES (?, ?)", [(1, 'bar'),(2, 'baz')])
print curs.execute("SELECT * FROM foo").fetchall()

[(1, u'bar'), (2, u'baz')]

rawDescription: Fetch All In APSW

description: Fetch All In APSW


code:
for r in Rank.objects.filter(date=utcnow()).order_by("value")\
    .prefetch_related():
    print r.student.name

rawDescription: How do I record daily rankings for a model in Django?

description: Record daily rankings for a model in Django


code:
template = ''.join(['{%(i)s:{widths[%(i)s]}}' % {'i': i} for i in range(len(maxLength))])
for line in lines:
    print template(*line, widths=maxLength)

rawDescription: dynamically building strings 'string'.format()

description: Dynamically building strings 'string'.format()


code:
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for row in a:
    print list(itertools.product(row, row))

rawDescription: Python - permutation and percentage

description: Python - permutation and percentage


code:
hash = {}
hash['one']  = {"index": 1, "value": "uno"}
hash['two']  = {"index": 2, "value": "dos"}
hash['three']= {"index": 3, "value": "tres"}
for ctg in sorted(hash.keys(),key=lambda x: hash[x]['index']):
   print hash[ctg]['index'],hash[ctg]['value']

rawDescription: Perl to Python hash sort

description: Perl to Python hash sort


code:
import urllib
import random
webpage = urllib.urlopen("http://www-01.sil.org/linguistics/wordlists/english/wordlist/wordsEn.txt").read()
webpage = webpage.splitlines() # Split text into lines.
print random.choice(webpage)

rawDescription: Random and list slicing working strangely with online world list


code:
for line in tup[0].splitlines():
    time, activity = line.split(': ', 1)
    print time, activity

rawDescription: split a string in python on linebreaks

description: Split a string in python on linebreaks


code:
data = np.rec.fromarrays(*x,
              names=['temp', 'pressure', 'x_vel', 'y_vel', 'z_vel'])
print data.temp

rawDescription: python numpy array / dict multiple inheritance

description: Python numpy array / dict multiple inheritance


code:
class CallableString(str):
    def __new__(class_, set, value):
        inst = str.__new__(class_, value)
        inst._set = set
        return inst
    def __call__(self, value):
        self._set(value)

class A(object):
    def __init__(self):
        self._attrib = "foo"
    def get_attrib(self):
        return CallableString(self.set_attrib, self._attrib)
    def set_attrib(self, value):
        try:
            value = value._value
        except AttributeError:
            pass
        self._attrib = value
    attrib = property(get_attrib, set_attrib)

a = A()
print a.attrib
a.attrib = "bar"
print a.attrib
a.attrib("baz")
print a.attrib

rawDescription: Python "callable" attribute (pseudo-property)


code:
import gzip, random

# Helper function to create some test data
def line(char,n):
    return ''.join([("%s"%char)*n,"\n"])

# Create the test data as in the example
filename = "test.zip"
FOUT = gzip.open(filename,'wb')
FOUT.write(line('a',2))
FOUT.write(line('b',3))
FOUT.write(line('c',5))
FOUT.write(line('d',5))
FOUT.write(line('e',5))
FOUT.close()

# Since we know the distribution, we know the length
length = 2+3+3*5+5 # 5 newlines

# Print 7 random points in the file
FIN = gzip.open(filename,'rb')
for n in xrange(7):
    FIN.seek(random.randrange(length),0)
    print "Position %3i, char: %s" %(FIN.tell(), [FIN.read(1)])

rawDescription: How to get a random line from within a gzip compressed file in python without reading it into memory

description: Get a random line from within a gzip compressed file in python without reading it into memory


code:
# %z is supported in Python 3.2 onwards. Older versions of python don't support that.
from datetime import datetime
date = '2014-12-18T19:00:00-07:00'
format = "%Y-%m-%dT%H:%M:%S%z"
datetime_obj = datetime.strptime(date, format)
print datetime_obj.strftime(format)

rawDescription: python : convert string to time object

description: Python : convert string to time object


code:
print timeit.repeat(stmt=test1, number=100, repeat=2)
print timeit.repeat(stmt=test2, number=100, repeat=2)
print timeit.repeat(stmt=test3, number=100, repeat=2)

rawDescription: Difference in complexity of append and concatenate for lists


code:
class A(object):
    def do_something(self):
        print id(self), "called"

class B(object):
    def get_a(self):
        return A()

class Proxy(object):
    def __init__(self, objs):
        self._objs = objs

    def __getattr__(self, name):
        def func(*args, **kwargs):
            return Proxy([getattr(o, name)(*args, **kwargs) for o in self._objs])
        return func

collection = [B(), B()]

proxy = Proxy(collection)
proxy.get_a().do_something()

rawDescription: How to create an object collection proxy in Python?

description: Create an object collection proxy in Python


code:
import os

filename = r"C:\Users\hemanth_venkatappa\Desktop\TEST\Language\test.txt"
back_up = 6 * 1024      # Go back from the end more than 6 lines worth.

with open(filename, "r") as f_input:
    f_input.seek(0, os.SEEK_END)
    backup = min(back_up, f_input.tell())
    f_input.seek(-backup, os.SEEK_END)
    print f_input.readlines()[-6:]

rawDescription: How to read the last few lines within a file using Python?

description: Read the last few lines within a file using Python


code:
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.utils import formataddr
from email.utils import make_msgid
from email.utils import formatdate

email = MIMEMultipart()
email['From'] = formataddr(('Jane Doe', 'jane@example.com'))
email['Subject'] = u'Test email'
email['Message-Id'] = make_msgid()
email['Date'] = formatdate(localtime=True)
email.attach(MIMEText(u'This is your email contents.'))
email.attach(MIMEApplication('your binary data'))
print email.as_string()

rawDescription: python can't send attachment files through email

description: Python can't send attachment files through email


code:
key1 = config.get('section1', 'key1').split(', ')
key1
for v in key1:
 print v

rawDescription: How to get nested value from option that are in config.ini file using python

description: Get nested value from option that are in config.ini file using python


code:
import codecs, re, time
from itertools import chain

import numpy as np

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

trainfile = 'train.txt'

# Vectorizing data.
train = []
word_vectorizer = CountVectorizer(analyzer='word')
trainset = word_vectorizer.fit_transform(codecs.open(trainfile,'r','utf8'))
tags = ['bs','pt','es','sr']

# Training NB
mnb = MultinomialNB()
mnb.fit(trainset, tags)

from sklearn.svm import SVC
svcc = SVC(kernel='linear', C=1)
svcc.fit(trainset, tags)

def most_informative_feature_for_class(vectorizer, classifier, classlabel, n=10):
    labelid = list(classifier.classes_).index(classlabel)
    feature_names = vectorizer.get_feature_names()
    topn = sorted(zip(classifier.coef_[labelid], feature_names))[-n:]

    for coef, feat in topn:
        print classlabel, feat, coef

def most_informative_feature_for_class_svm(vectorizer, classifier,  n=10):
    labelid = 3 # this is the coef we're interested in.
    feature_names = vectorizer.get_feature_names()
    svm_coef = classifier.coef_.toarray()
    topn = sorted(zip(svm_coef[labelid], feature_names))[-n:]

    for coef, feat in topn:
        print feat, coef

most_informative_feature_for_class(word_vectorizer, mnb, 'pt')
print
most_informative_feature_for_class_svm(word_vectorizer, svcc)

rawDescription: Problems obtaining most informative features with scikit learn?


code:
#!/usr/bin/python

import re

words={}

for line in open("cmudict.0.7a",'ro').readlines():
    split_idx = line.find(' ')
    words[line[0:split_idx]] = line[split_idx+1:-1]

user_input = raw_input("Words: ")

print
for word in user_input.split(' '):
    try:
        print "%25s %s" % (word, words[word.upper()])
    except:
        print "%25s %s" % (word, 'unable to find phonems for word')

rawDescription: Find words and combinations of words that can be spoken the quickest


code:
import subprocess
bufsize =1024
initial_data = get_initial_data_from_db()
p = subprocess.Popen("application -parameter1", shell=True, bufsize=bufsize,
          stdin=subprocess.PIPE,   close_fds=True)

p.stdin.write(initial_data)
print p.communicate()

rawDescription: Python: Running command line application to the script. Sending parameters in files


code:
from collections import defaultdict
import re

regex = re.compile(r'label\("([^"]*)",\s*"([^"]*)"\)')
results = defaultdict(list)

for id_, tag in regex.findall(s):
    results[id_].append(tag)

print results.items()

rawDescription: Grouping elements based on an identifier in regular expression (Python)


code:
import csv
import collections

infile = 'Result'

names = collections.Counter()

with open(infile) as input_file:
    for row in csv.reader(input_file, delimiter='\t'):
        names[row[0]] += 1

for name, count in names.iteritems():
    print name, count

rawDescription: count occurencies of a word

description: Count occurencies of a word


rawDescription: Efficiently select subsection of numpy array


code:
import re
pattern = re.compile("(?<=
).*?(?=
)", re.IGNORECASE | re.DOTALL)
print re.findall(pattern, data)

rawDescription: Python regex findall to capture repeated groups


code:
cursor = connection.cursor()
lOutput = cursor.var(cx_Oracle.STRING)
cursor.execute("""
            BEGIN
                :out := 'N';
            END;""",
            {'out' : lOutput})
print lOutput

rawDescription: cx_Oracle and output variables

description: Cx_Oracle and output variables


code:
import pandas as pd
from pandas.tseries.holiday import USFederalHolidayCalendar as calendar

dr = pd.date_range(start='2015-07-01', end='2015-07-31')
df = pd.DataFrame()
df['Date'] = dr

cal = calendar()
holidays = cal.holidays(start=dr.min(), end=dr.max())

df['Holiday'] = df['Date'].isin(holidays)
print df

rawDescription: Pandas: Checking if a date is a holiday and assigning boolean value


code:
count = 0
while (count < 9):
   print 'The count is:', count
   count = count + 1

print "Good bye!"

rawDescription: Python: How can I go back to previous code?


code:
import random
fortunes = ["Good things come to those who wait.",
            "Patience is a virtue.",
            "The early bird gets the worm.",
            "A wise man once said, everything in its own time and place.",
            "Fortune cookies rarely share fortunes."]
print random.choice(fortunes)

rawDescription: Fortune cookie game in Python

description: Fortune cookie game in Python


code:
tempListGen = ((a,c) for a,b,c in data if b == 'fruit')
tempListGen
print sorted(tempListGen)

rawDescription: python conditional list creation from 2D lists

description: Python conditional list creation from 2D lists


code:
def do_something(x):
    print x

rawDescription: How can I give an imported library access to current global variables?

description: Give an imported library access to current global variables


rawDescription: Python: Parsing timestamps and calculating time differences in milliseconds


code: print "union:", dict_ops(d2, d1, op.or_)

rawDescription: How can I perform set operations on Python dictionaries?

description: Perform set operations on Python dictionaries


code:
import math
def rotatePolygon(polygon,theta):
    """Rotates the given polygon which consists of corners represented as (x,y),
    around the ORIGIN, clock-wise, theta degrees"""
    theta = math.radians(theta)
    rotatedPolygon = []
    for corner in polygon :
        rotatedPolygon.append(( corner[0]*math.cos(theta)-corner[1]*math.sin(theta) , corner[0]*math.sin(theta)+corner[1]*math.cos(theta)) )
    return rotatedPolygon

my_polygon = [(0,0),(1,0),(0,1)]
print rotatePolygon(my_polygon,90)

rawDescription: Python function for rotating 2d objects

description: Python function for rotating 2d objects


code:
# Gets domains given synset.
for ss in wn.all_synsets():
    ssid = str(ss.offset()).zfill(8) + "-" + ss.pos()
    if synset2domains[ssid]: # not all synsets are in WordNet Domain.
        print ss, ss.lemma_names('fre'), ssid, synset2domains[ssid]

rawDescription: How to get domain of words using WordNet in Python?

description: Get domain of words using WordNet in Python


code:
from collections import Counter
import numpy as np
from sklearn.datasets import load_iris
from sklearn.svm import LinearSVC

data = load_iris()

# remove a feature to make the problem harder
# remove the third class for simplicity
X = data.data[:100, 0:1]
y = data.target[:100]
# shuffle data
indices = np.arange(y.shape[0])
np.random.shuffle(indices)
X = X[indices, :]
y = y[indices]

decision_boundary = 0
print Counter((clf.decision_function(X[50:]) > decision_boundary).astype(np.int8))
Counter({1: 27, 0: 23})

decision_boundary = 0.5
print Counter((clf.decision_function(X[50:]) > decision_boundary).astype(np.int8))
Counter({0: 39, 1: 11})

rawDescription: How to fix the false positives rate of a linear SVM?

description: Fix the false positives rate of a linear SVM


code:
# ---------------------------------------------
import numpy
from numpy.lib.stride_tricks import as_strided
import cPickle

def dumps(x, protocol=cPickle.HIGHEST_PROTOCOL):
    # flatten that keep real data order
    y = as_strided(x, shape=(x.size,), strides=(min(x.strides),))
    return cPickle.dumps([y,x.shape,x.strides],protocol=protocol)

def loads(pickled):
    y,shape,strides = cPickle.loads(pickled)
    return as_strided(y,shape=shape,strides=strides)

if __name__=='__main__':
    x = numpy.arange(2*3*4, dtype='uint8').reshape((2,3,4)).transpose(0,2,1)

    pickled = dumps(x)
    y = loads(pickled)

    print 'x strides =', x.strides
    print 'y strides =', y.strides
    print 'x==y:', (x==y).all()
# ---------------------------------------------

rawDescription: Preserving numpy strides in a python pickle file


code:
import time, sys

# The print statement effectively treats '\r' as a newline,
# so use sys.stdout.write() and .flush() instead ...
def carriage_return_a():
    sys.stdout.write('\r')
    sys.stdout.flush()

# ... or send a terminal control code to non-windows systems
# (this is what the `progress_bar` module does)
def carriage_return_b():
    if sys.platform.lower().startswith('win'):
        print '\r'
    else:
        print chr(27) + '[A'

bar_len = 10
for i in range(bar_len + 1):
    # Generate a fixed-length string of '*' and ' ' characters
    bar = ''.join(['*'] * i + [' '] * (bar_len - i))

    # Insert the above string and the current value of i into a format
    # string and print, suppressing the newline with a comma at the end
    print '[{0}] {1}'.format(bar, i),

    # Write a carriage return, sending the cursor back to the beginning
    # of the line without moving to a new line.
    carriage_return_a()

    # Sleep
    time.sleep(1)

rawDescription: progress bar for reading lines in text file

description: Progress bar for reading lines in text file


code:
#! /usr/bin/env python

import foo

def fullname(o):
  return o.__module__ + "." + o.__class__.__name__

bar = foo.Bar()
print fullname(bar)

rawDescription: Get fully qualified class name of an object in python


code:
print tag        # using tag.__str__
print [tag, tag] # using tag.__repl__ for each element

rawDescription: Get the right value in my array

description: Get the right value in my array


code: print ret_list

rawDescription: Python - Convert JSON object data into a List


rawDescription: How can i split a string into two on the first occurrence of a character

description: Split a string into two on the first occurrence of a character


code:
for line in catalogue:
    if line.split()[0] in s:
        print line

rawDescription: How do I efficiently crossmatch two ASCII catalogs?

description: Efficiently crossmatch two ASCII catalogs


code:
from graphics import *

def main():
    win = GraphWin("My Window", 100, 100)
    clickCounter = 0
    while clickCounter < 10:
        win.getMouse() # Pause to view result
        clickCounter += 1
        print clickCounter

main()

rawDescription: Python: Counting and printing mouse clicks (Zelle's Graphics)


code:
from nltk import pos_tag
from nltk.chunk import conlltags2tree
from nltk.tree import Tree

def stanfordNE2BIO(tagged_sent):
    bio_tagged_sent = []
    prev_tag = "O"
    for token, tag in tagged_sent:
        if tag == "O": #O
            bio_tagged_sent.append((token, tag))
            prev_tag = tag
            continue
        if tag != "O" and prev_tag == "O": # Begin NE
            bio_tagged_sent.append((token, "B-"+tag))
            prev_tag = tag
        elif prev_tag != "O" and prev_tag == tag: # Inside NE
            bio_tagged_sent.append((token, "I-"+tag))
            prev_tag = tag
        elif prev_tag != "O" and prev_tag != tag: # Adjacent NE
            bio_tagged_sent.append((token, "B-"+tag))
            prev_tag = tag

    return bio_tagged_sent

def stanfordNE2tree(ne_tagged_sent):
    bio_tagged_sent = stanfordNE2BIO(ne_tagged_sent)
    sent_tokens, sent_ne_tags = zip(*bio_tagged_sent)
    sent_pos_tags = [pos for token, pos in pos_tag(sent_tokens)]

    sent_conlltags = [(token, pos, ne) for token, pos, ne in zip(sent_tokens, sent_pos_tags, sent_ne_tags)]
    ne_tree = conlltags2tree(sent_conlltags)
    return ne_tree

ne_tagged_sent = [('Rami', 'PERSON'), ('Eid', 'PERSON'), ('is', 'O'),
('studying', 'O'), ('at', 'O'), ('Stony', 'ORGANIZATION'),
('Brook', 'ORGANIZATION'), ('University', 'ORGANIZATION'),
('in', 'O'), ('NY', 'LOCATION')]

ne_tree = stanfordNE2tree(ne_tagged_sent)

print ne_tree

rawDescription: Extract list of Persons and Organizations using Stanford NER Tagger in NLTK


code:
def am_i_alive(hit_points, threshold):
    if hit_points == 0:
        print 'dead'
    elif hit_points <= threshold:
        print 'almost dead'
    else:
        print 'alive'

am_i_alive(20, 5)
am_i_alive(3, 2)
am_i_alive(0, 10)

rawDescription: What are the errors in this code?

description: What are the errors in this code


code:
import inspect
import sys

def test():
    pass

functions = [name for name, obj in inspect.getmembers(sys.modules[__name__], inspect.isfunction)]
print functions

rawDescription: Finding All Defined Functions in Python Environment


code:
ts =  datetime.utcnow()
print ts.microsecond  #prints microseconds
print time.mktime(ts.timetuple()) + ts.microsecond * 1e-6

rawDescription: Python current timestamp for UTC with milisecond accuracy


code:
g.go('https://www.google.com.ua/')
g.doc.set_input("q","grab")
g.doc.submit(submit_name = 'btnK')
g.doc.select('//head/title').text()
print g.doc.select('//head/title').text()

rawDescription: How I can do Google search, using Grab

description: How I can do Google search, using Grab


code:
def selection_sort(A,n):
    moves=0
    for i in xrange(n):
        minn=i
        for j in xrange(i+1,n):
            if A[j]<A[minn]:
                minn=j
        moves+=minn-i
        A[i],A[minn]=A[minn],A[i]
    print moves,A

#examples:
selection_sort(['1', '0', '1', '0', '1', '0'],6)
selection_sort(['1', '0', '1', '1', '1', '1'],6)

rawDescription: python function sorting alternating disks

description: Python function sorting alternating disks


code:
import simplejson as json
from collections import defaultdict

def multidict(ordered_pairs):
    """Convert duplicate keys values to lists."""
    # read all values into lists
    d = defaultdict(list)
    for k, v in ordered_pairs:
        d[k].append(v)

    # unpack lists that have only 1 item
    for k, v in d.items():
        if len(v) == 1:
            d[k] = v[0]
    return dict(d)

print json.JSONDecoder(object_pairs_hook=multidict).decode(text)

rawDescription: SimpleJson handling of same named entities


code:
start = 0
end = 0

while start < 2 or end <= start:
    start = int(raw_input("Enter start -> "))
    end = int(raw_input("Enter end ->"))

total = 0
for x in range(start, end+1):
    if x % 2 == 0:
        total += x

print total

rawDescription: I need to add up the even numbers between two other numbers in Python


code:
import itertools
numbers = range(1,100)
items = set(list(itertools.combinations(numbers,3)))
for item in items:
    print item

rawDescription: Finding number of combinations

description: Finding number of combinations


code:
#!/usr/bin/env python
import urllib2
from bs4 import BeautifulSoup

ip  = "192.168.1.1"

xml = urllib2.urlopen('http://freegeoip.net/xml/' + ip).read()

soup = BeautifulSoup(xml)

print "IP Address: %s" % soup.ip.text
print "Country Code: %s" % soup.countrycode.text
print "Country Name: %s" % soup.countryname.text

rawDescription: Easiest way to sort XML in Python? [Skype Bot]


rawDescription: datetime strptime - set format to ignore part of string

description: Datetime strptime - set format to ignore part of string


code:
class Container:
    def __init__(self, source):
        self.source = source
    def to(self, target):
        print 'Dragging ' + self.source + ' to ' + target

def drag(source):
    return Container(source)

rawDescription: Simplifying code: classes as simple containers in Python


code:
import datetime as DT
import itertools

start_date=DT.date(2008,5,5)

def mkdate(datestring):
    return DT.datetime.strptime(datestring, "%Y-%m-%d").date()

def fortnight(date):
    return (date-start_date).days //14

raw = ("2010-08-01",
       "2010-06-25",
       "2010-07-01",
       "2010-07-08")
transactions=[(date,"Some data") for date in map(mkdate,raw)]
transactions.sort(key=lambda (date,data):date)

for key,grp in itertools.groupby(transactions,key=lambda (date,data):fortnight(date)):
    print(key,list(grp))

rawDescription: Group together arbitrary date objects that are within a time range of each other


code:
import collections

a = collections.Counter(listA)
b = collections.Counter(listB)

print a & b

rawDescription: List Comparison Algorithm: How can it be made better?


code:
barber={'jon': 1,'sam':8,'sarah': 10 }

if barber.get("sam")  > 4: # get value of sam
   print barber.get("sam")
8

rawDescription: Dynamic list of booleans python

description: Dynamic list of booleans python


code:
from collections import OrderedDict
from bisect import bisect_left
from pprint import pprint

dct = OrderedDict()
for t, v in highhz:
    dct.setdefault(t, []).append(v)
times = list(dct)

for t, v in lowhz:
    ind = bisect_left(times, t) - 1
    dct[times[ind]].append(v)
#----
for k, v in dct.items():
    if len(v) == 1:
        v.append(float('nan'))
#----
print [[k] + v for k, v in dct.items()]
#[[0, 1, 1.5], [2, 2, nan], [4, 4, nan], [5, 5, 5.6], [6, 6, nan], [7, 7, nan], [8, 8, nan]]

rawDescription: How do I merge two time series with different time values in python?

description: Merge two time series with different time values in python


rawDescription: Python Regex to find reccuring patterns or binary strings


code:
import binascii
import hashlib

the_hash = hashlib.md5('Dummy String').hexdigest()
print the_hash
the_unhex = binascii.unhexlify(the_hash)
print the_unhex

rawDescription: PHP to python pack('H')

description: PHP to python pack('H')


code:
from mpmath import *
mp.dps = 1000000
val=e/4/(10**7)
from time import time
start = time(); y1 = asin(x); print time() - start
start = time(); y2 = asin1(x); print time() - start
nprint(y2 - y1)

rawDescription: Fastest possible method for the arcsin function on small, arbitrary floating-point values


code:
import datetime

my_time = "13:11:06"
new_time = datetime.datetime.strptime("2016 " + my_time, "%Y %H:%M:%S") - datetime.timedelta(hours=5, minutes=30)
print new_time.strftime("%H:%M:%S")

rawDescription: Substract current time to -GMT in python


code:
parts = [s.strip() for s in parts]
print parts  # prints ['hello, world', '1', '2', '3']

rawDescription: parsing CSV files backwards

description: Parsing CSV files backwards