code:
from uiautomator import device as d
d(index = 2, className='android.widget.TextView').click() #menu button
print 'inside menu button'
d(index='15', className='android.widget.TextView').click() #contacts
print 'inside contacts'
#loop to add a contact
d(resourceId= 'com.android.contacts:id/menu_add_contact', className='android.widget.TextView').click()

rawDescription: python script does not work but individual shell command does

description: Python script does not work but individual shell command does


code:
import json
sample = '{"destination":"Provence-Alpes-C\\303\\264te d\'Azur"}'
repair(sample)
json.loads(repair(sample))
print json.loads(repair(sample))['destination']

rawDescription: Parsing JSON using Pandas - issue with additional \ escape characters


code:
largest = None
smallest = None
numI = 0
while True:
    num = raw_input("Prompt you")
    if num == "done":
        break
    try:
        numI = int(num)
    except:
        print "Invalid input"
        continue
    if largest==None:
        largest=numI
    if smallest==None:
        smallest=numI
    if numI > largest or numI < smallest:
        if numI > largest:
            largest = numI
        else:
            smallest = numI

print "Maximum is",largest
print "Minimum is",smallest

rawDescription: Returns unexpected value

description: Returns unexpected value


code:
import sympy

x, y, z = sympy.symbols('xyz')

p1 = 2*x*x
p2 = 3*y + 1
p3 = z

p4 = p1*p2*p3

print p4

print p4.diff(x)
print p4.diff(y)
print p4.diff(z)

rawDescription: Libraries for manipulating multivariate polynomials


code:
myDict = {}

with open('d.fasta', 'rU') as fileData:
    tag = None
    for line in fileData:
        line = line.strip()
        if not line:
            continue
        if line[0] == '>':
            tag = line[1:]
            myDict[tag] = ''
        else:
            assert tag is not None, 'Invalid format, found gene without tag'
            myDict[tag] += line

print myDict

rawDescription: Using regex to transform data into a dictionary in Python


code:
import fileinput
for line in fileinput.input('myPOST.txt', inplace=True):
    if ':' in line:
        continue # skip it
    print line.rstrip('\n') # stdout redirected to file

rawDescription: How to delete a line from a file using python

description: Delete a line from a file using python


code:
addressSplit = addressComponents[3].split("\n")
print addressSplit[0] # Zip code
print addressSplit[1].strip() # Phone number

rawDescription: Python - read BeautifulSoup snippet by row? (or other ways of scraping the data I want)


code:
text = """
A lake is a body of relatively still water of considerable size, localized in a basin, that is surrounded by land apart from a river, stream, or other form of moving water that serves to feed or drain the lake. Lakes are inland and not part of the ocean and therefore are distinct from lagoons, and are larger and deeper than ponds.[1][2] Lakes can be contrasted with rivers or streams, which are usually flowing. However most lakes are fed and drained by rivers and streams.
Natural lakes are generally found in mountainous areas, rift zones, and areas with ongoing glaciation. Other lakes are found in endorheic basins or along the courses of mature rivers. In some parts of the world there are many lakes because of chaotic drainage patterns left over from the last Ice Age. All lakes are temporary over geologic time scales, as they will slowly fill in with sediments or spill out of the basin containing them.
Many lakes are artificial and are constructed for industrial or agricultural use, for hydro-electric power generation or domestic water supply, or for aesthetic or recreational purposes.
Etymology, meaning, and usage of "lake"[edit]
Oeschinen Lake in the Swiss Alps
Lake Tahoe on the border of California and Nevada
The Caspian Sea is either the world's largest lake or a full-fledged sea.[3]
The word lake comes from Middle English lake ("lake, pond, waterway"), from Old English lacu ("pond, pool, stream"), from Proto-Germanic *lakō ("pond, ditch, slow moving stream"), from the Proto-Indo-European root *leǵ- ("to leak, drain"). Cognates include Dutch laak ("lake, pond, ditch"), Middle Low German lāke ("water pooled in a riverbed, puddle"), German Lache ("pool, puddle"), and Icelandic lækur ("slow flowing stream"). Also related are the English words leak and leach.
There is considerable uncertainty about defining the difference between lakes and ponds, and no current internationally accepted definition of either term across scientific disciplines or political boundaries exists.[4] For example, limnologists have defined lakes as water bodies which are simply a larger version of a pond, which can have wave action on the shoreline or where wind-induced turbulence plays a major role in mixing the water column. None of these definitions completely excludes ponds and all are difficult to measure. For this reason there has been increasing use made of simple size-based definitions to separate ponds and lakes. One definition of lake is a body of water of 2 hectares (5 acres) or more in area;[5]:331[6] however, others[who?] have defined lakes as waterbodies of 5 hectares (12 acres) and above,[citation needed] or 8 hectares (20 acres) and above[citation needed] (see also the definition of "pond"). Charles Elton, one of the founders of ecology, regarded lakes as waterbodies of 40 hectares (99 acres) or more.[7] The term lake is also used to describe a feature such as Lake Eyre, which is a dry basin most of the time but may become filled under seasonal conditions of heavy rainfall. In common usage many lakes bear names ending with the word pond, and a lesser number of names ending with lake are in quasi-technical fact, ponds. One textbook illustrates this point with the following: "In Newfoundland, for example, almost every lake is called a pond, whereas in Wisconsin, almost every pond is called a lake."[8]
One hydrology book proposes to define it as a body of water with the following five chacteristics:[4]
it partially or totally fills one or several basins connected by straits[4]
has essentially the same water level in all parts (except for relatively short-lived variations caused by wind, varying ice cover, large inflows, etc.)[4]
it does not have regular intrusion of sea water[4]
a considerable portion of the sediment suspended in the water is captured by the basins (for this to happen they need to have a sufficiently small inflow-to-volume ratio)[4]
the area measured at the mean water level exceeds an arbitrarily chosen threshold (for instance, one hectare)[4]
With the exception of the sea water intrusion criterion, the other ones have been accepted or elaborated upon by other hydrology publications.[9][10]
""".split()

from nltk import bigrams

bgs = bigrams(text)
lake_bgs = filter(lambda item: item[0] == 'lake', bgs)

from collections import Counter
c = Counter(map(lambda item: item[1], lake_bgs))
print c.most_common()

rawDescription: How to find the most frequent words before and after a given word in a given text in python?

description: Find the most frequent words before and after a given word in a given text in python


code: print Choice.poll.field.rel.to.__name__   # prints 'Poll'

rawDescription: Get an attribute's class from its name in Python?


code:
v = [1, 3, 7, 11]
double_preceding(v)
print v

rawDescription: A double preceding function with bug

description: A double preceding function with bug


code:
from bs4 import BeautifulSoup

data = """
<ROOT>
    <RECORD ID="123">
        <TEXT>
This is an example text written at <TAG TYPE="DATE">December 29th</TAG> to illustrate the problem.
        </TEXT>
    </RECORD>
</ROOT>
"""

soup = BeautifulSoup(data, "xml")

for text in soup.find_all('TEXT'):

    record = text.parent
    for tag in text.find_all('TAG'):
        begin = text.text.index(tag.text)
        end = len(tag.text) + begin

        record.append(soup.new_tag(tag.name, BEGIN=begin, END=end))

        tag.unwrap()

print soup

rawDescription: How to extract tag offsets in xml document using Python BeautifulSoup

description: Extract tag offsets in xml document using Python BeautifulSoup


code:
import re
string = "hello 123 world 5678 897 word"
number_length = 3
pattern= r"\D(\d{%d})\D" % number_length   # \D to avoid matching 567
print re.findall(pattern, string)

rawDescription: Find x-digit number in a text using Python


code:
from Bio import Entrez
from Bio import SeqIO

Entrez.email = "sample@example.org"

handle = Entrez.efetch(db="nuccore",
                       id="CP001665",
                       rettype="gb",
                       retmode="text")

whole_sequence = SeqIO.read(handle, "genbank")

print whole_sequence[6373:6422]

rawDescription: Querying NCBI for a sequence from ncbi via Biopython


code:
ind1 = np.array([2, 6])
ind2 = np.array([2, 3])
print index_slice(ind1, ind2)
ind1 = np.array([2, 6, 1])
ind2 = np.array([2, 3, 4])
print index_slice(ind1, ind2)

rawDescription: Function to slice indices in Numpy

description: Function to slice indices in Numpy


code:
for detail in details:
    if 'NHS' in str(detail):
        print detail

rawDescription: Python - Looping through HTML Tags and using IF


code:
import glob
import os
import shutil

for f in glob.glob("/some/path/*2000*h10v03*"):
   print f
   shutil.copy2(f, os.path.join("/some/target/dir/", os.path.basename(f)))

rawDescription: Search files in folder using part of the name and save/copy to different folder using Python


code:
from lxml import html
from urlparse import urlparse
doc = html.fromstring(html_data)
links = doc.cssselect("a")
domains = set([])
for link in links:
    try: href=link.attrib['href']
    except KeyError: continue
    parsed=urlparse(href)
    domains.add(parsed.netloc)
print domains

rawDescription: Extract domain from body of email

description: Extract domain from body of email


code:
def pastChoice():
    prevMove = []

    while True:
        choice = raw_input("> ")
        prevMove.append(choice)
        print prevMove
pastChoice()

rawDescription: Input user into a list

description: Input user into a list


code:
import sys
buffer = []
while run:
    line = sys.stdin.readline().rstrip('\n')
    if line == 'quit':
        run = False
    else:
        buffer.append(line)

print buffer

rawDescription: taking multiline input with sys.stdin

description: Taking multiline input with sys.stdin


code: print next(itertools.dropwhile(lambda x: x < 100, fib_function()))

rawDescription: Return the first occurance of a certain string length using itertools in Python


code:
import os
import re

def query():
    f = open('company.txt', 'r')
    names = re.findall(r"<h2>(.+?)</h2>.*?area_code = '(.+?)'", f.read(), re.DOTALL)
    for name in names:
        print name[0] + " | " + name[1]

if __name__=="__main__":
    query()

rawDescription: Running multiple Regex once

description: Running multiple Regex once


code:
from itertools import *
from collections import *

s = [5, 7, 9, 12, 6]
m = 15

c = Counter(s)
u = list(c)
ul = len(u)
t = [c[x] for x in u]
p = product(*[xrange(i+1) for i in t])
e = ([a for b in [[u[i]] * x[i] for i in range(ul)] for a in b] for x in p)
r = [n for n in e if sum(n) == m]

print r

rawDescription: Most efficient method of getting possible sum from list in python


code:
class M2(type):
    def __new__(meta, name, bases, atts):
        print "M2 called for " + name
        return super(M2, meta).__new__(meta, name, bases, atts)
class C2(object):
    __metaclass__ = M2
type(C2)
class Sub2(C1, C2):
    pass

rawDescription: Customary To Inherit Metaclasses From type?


code:
x, y = chasis.split()
print x
print y

rawDescription: In Python find and extract only required info


code:
print audio.pprint()
audio.delall('TCON')
print audio.pprint()

rawDescription: Delete Music File Metadata Tag

description: Delete Music File Metadata Tag


code:
def headerMenu(self, pos):
    globalPos = self.mapToGlobal(pos)
    menu = QMenu()
    menu.addAction("test item")
    selectedItem = menu.exec_(globalPos)
    if selectedItem:
        print "selected: ", selectedItem

rawDescription: Right clicking on QHeaderView inside of QTreeView


code:
import subprocess as s
names = "bla blub moep"
cmd = ['awk', '-f', 'sth.awk', 'vars=' + names, 'file']
print s.check_output(cmd)

rawDescription: Python subprocess with vars

description: Python subprocess with vars


code:
import MySQLdb
import re
database = MySQLdb.connect (host="localhost" , user="test" , passwd="" ,db="test")
cursor = database.cursor()
query= """INSERT INTO test (id, num) VALUES (%s, %s)"""

prix = "215,325"
prix = str(prix)
prix = re.sub('[^0-9.]+', '', prix )
prix = float(prix)
values = (23,prix)

cursor.execute(query,values)
cursor.close();
database.commit()
database.close()
print "Done"

rawDescription: Import Data from Excel to Mysql Python

description: Import Data from Excel to Mysql Python


code:
for data in 0x11, 0x10, 0x21, 0x23:
    xor = 0
    while data:
        xor ^= data & 1
        data >>= 1
    print xor

rawDescription: How to xor hex value in Python (1 bit answer)

description: Xor hex value in Python (1 bit answer)


code:
print "name    {}".format("\t".join(vals))
for key in keys:
    strs = "\t".join(str(int(round(float(dic[key][v])))) if v in dic[key] else '0'  for v in vals)
    print "{}    {}".format(key, strs )

rawDescription: Creating fingerprint file from flat file in python


code:
s = 'unicode'
print ''.join(['\\x%02x' % (ord(c)) for c in s])

rawDescription: Forcely output of ascii string as bytes

description: Forcely output of ascii string as bytes


code:
source
import multiprocessing
import time

def f(ls,lso, ):
    print "input ls:"
    print ls
    print "input lso:"
    print lso

    ls[0] += 1
    ls[1][0] += 1
    ls_2 = ls[2]
    print ls_2
    ls_2[0] += 1
    print ls_2
    ls[2] = ls_2
    print "output ls:"
    print ls
    tmp = []
    tmp.append([1, 2, 3])
    tmp.append([5, 6, 7])
    lso = tmp
    print "output lso:"
    print lso

if __name__ == '__main__':
    manager = multiprocessing.Manager()
    #ls = manager.list([1, [1], [1]])
    ls = manager.list()
    lso = manager.list(
        [[0, 0, 0], [0, 0, 0]]
        )
    tmp = []
    tmp.append(1)
    tmp.append([1])
    tmp.append([1])
    ls[:] = tmp

    print 'before', ls, lso
    p = multiprocessing.Process(target=f, args=(ls, lso, ))
    p.start()
    p.join()
    print 'after', ls, lso

rawDescription: multiprocessing.Manager() communication with main programm didn't work

description: Multiprocessing.Manager() communication with main programm didn't work


code:
s = """\
mytuple = [('Andrew','Andrew@gmail.com','20'),('Jim',"Jim@gmail.com",'12'),("Sarah","Sarah@gmail.com",'43'),("Jim","Jim@gmail.com",'15'),("Andrew","Andrew@gmail.com",'56')]
set(zip(*mytuple)[1])
"""
print timeit.timeit(stmt=s, number=100000)
0.0740020275116

rawDescription: Dropping values from a list of tuples

description: Dropping values from a list of tuples


code:
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns

tips = sns.load_dataset("tips")

ax = sns.boxplot(x="day", y="total_bill", data=tips)

for c in ax.get_children():
    if type(c) == matplotlib.patches.PathPatch:
        print c.get_extents()

rawDescription: Coordinates of boxes in Seaborn boxplot

description: Coordinates of boxes in Seaborn boxplot


code:
import re
disks=[]

OUTPUT = '''
FRU 0_2_3 enclosure 1
FRU 0_2_4 enclosure 1
FRU 0_2_5 enclosure 1
FRU 0_2_6 enclosure 1
'''
#OUTPUT=fbeclicommand("li -all") #This is the command on some exe that will gather the output and save it to this variable.
OUTPUTSplit= OUTPUT.split('\n')
for line in OUTPUTSplit:
    if re.search("\d{1,3}_\d{1,3}_\d{1,3}", line):
        disks.append(line.split(' ')[1])

print disks

rawDescription: Python: Saving parts of str to an array

description: Python: Saving parts of str to an array


code:
import json
script=["Linux","Windows"]
something = ["first","second"]
for i,j in zip(script,something):
    site="http:abc.com/pqr/xyz/symbol={0}".format(j)
    print i, site

rawDescription: how to pass list values through url

description: Pass list values through url


code:
from threading import Thread, Lock

shared_balance = 0

class Deposit(Thread):
    def __init__(self, lock):
        super(Deposit, self).__init__()
        self.lock = lock
    def run(self):
        global shared_balance
        for i in xrange(1000000):
            with self.lock:
                shared_balance += 100

class Withdraw(Thread):
    def __init__(self, lock):
        super(Withdraw, self).__init__()
        self.lock = lock
    def run(self):
        global shared_balance
        for i in xrange(1000000):
            with self.lock:
                shared_balance -= 100

shared_lock = Lock()
thread1 = Deposit(shared_lock)
thread2 = Withdraw(shared_lock)

thread1.start()
thread2.start()

thread1.join()
thread2.join()

print shared_balance

rawDescription: Explanation of program's behavior (Python)


code:
import selenium.webdriver as webdriver
import contextlib
import pandas as pd
@contextlib.contextmanager
def quitting(thing):
    yield thing
    thing.quit()

with quitting(webdriver.Firefox()) as driver:
    link = 'http://aws.amazon.com/ec2/pricing/'
    driver.get(link)
    content = driver.page_source
    with open('/tmp/out.html', 'wb') as f:
        f.write(content.encode('utf-8'))
    data = pd.read_html(content)
    print len(data)

rawDescription: pandas.read_html returns only one table

description: Pandas.read_html returns only one table


code:
import cStringIO
buf = cStringIO.StringIO()
handler = wx.richtext.RichTextXMLHandler()
handler.SetFlags(wx.richtext.RICHTEXT_HANDLER_INCLUDE_STYLESHEET)
handler.SaveStream(rt.GetBuffer(), buf)
print buf.getvalue()

rawDescription: save data in wxRichTextCtrl

description: Save data in wxRichTextCtrl


code:
from difflib import SequenceMatcher

li = [\
([u'Houston', u'Alabama'],
 [u'Houst', u'Alab']),

([u'Florida State', u'North Carolina State'],
 [u'NCSt', u'FlaSt']),

([u'Penn State', u'Iowa'],
 [u'PnSt', u'Iowa']),

([u'Oklahoma', u'Texas'],
 [u'Texas', u'Okla']),

([u'Florida Atlantic', u'South Florida'],
 [u'SFla', u'FlAtl']),

([u'Georgia', u'Tennessee'],
 [u'Geo', u'Tenn']),

([u'San Jose State', u'Idaho'],
 [u'UI', u'SJSU']),

([u'Washington State', u'Arizona State'],
 [u'ArzSt', u'WshSt']),

([u'Fresno State', u'Nevada'],
 [u'Nevad', u'FrsSt']),

([u'Oregon State', u'Arizona'],
 [u'ARIZ', u'OSU']),

([u'Clemson', u'Virginia Tech'],
 [u'VTech', u'Clem']),

([u'Chattanooga', u'Arkansas'],
 [u'UTC', u'AR']),

([u'USC', u'Stanford'],
 [u'USC', u'Stanf']),

([u'Baylor', u'Colorado'],
 [u'BU', u'CU']),

([u'North Texas', u'Louisiana-Lafayette'],
 [u'NoTex', u'LaLaf']),

([u'Tulane', u'Army'],
 [u'TLN', u'ARMY']),

([u'Troy', u'Florida International'],
 [u'TROY', u'FIU']),

([u'Louisiana-Monroe', u'Arkansas State'],
 [u'ASU', u'ULM']),

([u'Texas Tech', u'Iowa State'],
 [u'TT', u'ISU']),

([u'Akron', u'Western Michigan'],
 [u'AKRON', u'WMU']),

([u'Liberty', u'Toledo'],
 [u'LIBERTY', u'TOLEDO']),

([u'Virginia', u'Middle Tennessee'],
 [u'Virg', u'MTnSt']),

([u'Oklahoma State', u'Texas A&M'],
 [u'TexAM', u'OKSt']),

([u'Notre Dame', u'UCLA'],
 [u'NDame', u'UCLA']),

([u'Rutgers', u'Cincinnati'],
 [u'Cincy', u'Rutgr']),

([u'Ohio State', u'Purdue'],
 [u'Prdue', u'OhSt']),

([u'LSU', u'Florida'],
 [u'Fla', u'LSU']),

([u'Air Force', u'UNLV'],
 [u'AFA', u'UNLV']),

([u'Nebraska', u'Missouri'],
 [u'Misso', u'Neb']),

([u'New Mexico State', u'Boise State'],
 [u'NMxSt', u'BoiSt']),

([u'Pittsburgh', u'Navy'],
 [u'Navy', u'Pitt']),

([u'Wake Forest', u'Florida State'],
 [u'WFrst', u'FlaSt']),

([u'San Jose State', u'Hawaii'],
 [u'Hawa', u'SJSt']),

([u'UCF', u'South Florida'],
 [u'UCF', u'SFla']) ]

def comp(N,D,sq = SequenceMatcher(None)):
    sq.set_seqs(N[0],D[0])
    a = sq.ratio()
    sq.set_seqs(N[1],D[1])
    b = sq.ratio()

    sq.set_seqs(N[0],D[1])
    x = sq.ratio()
    sq.set_seqs(N[1],D[0])
    y = sq.ratio()

    if a>x and b>y:
        return (N[0],D[0]), (N[1],D[1])
    else:
        return (N[0],D[1]),(N[1],D[0])

print '\n'.join('%-30s   %s' % comp(N,D) for N,D in li)

rawDescription: Pythonic Name Matching

description: Pythonic Name Matching


code:
for fruit_name, fruit_list in fruit_stand.items():
    # Ignore fruit_name and iterate over the fruits in fruit_list
    for fruit in fruit_list:
        print fruit

rawDescription: Breaking down instances of classes into lists, or: a better way to sum class variables?


code:
interface_node = tree.find('//ns:ROW_interface', ns)

Interface = namedtuple('Interface', ['interface', 'state'])
interface = Interface(interface=interface_node.find('.//ns:interface', ns).text,
                      state=interface_node.find('.//ns:state', ns).text)

print interface

rawDescription: Parsing XML file with lxml in Python

description: Parsing XML file with lxml in Python


code:
a = [1, 3, 5, 6, 8]
def mapfunction((i, el)):
    return i * el
map(lambda (i, el): i * el, enumerate(a))
map(mapfunction, enumerate(a))

rawDescription: Can I use index information inside the map function?

description: Use index information inside the map function


code:
import re

with open('input.txt') as f_input:
    data = f_input.read()
    blocks = re.findall(r'(^hg19\..*?)\n*?(?=^hg19\.|\Z)', data, re.S + re.M)

allowed = set(["10917", "11396", "1116920"])
blocks = [block for block in blocks if block.split('\n', 1)[0].split()[1] in allowed]

for block in blocks:
    print block
    print '----'

rawDescription: Using python, I want to extract and print blocks that contain 10917, 11396 and 1116920 in first line of each block


code:
lines = '''\
50 / 50
40 / 50
40 / 50
45 / 50
50 / 50
'''
for strings in lines.splitlines():
    if "/" in strings:
        print strings
        pass_, total = map(float, strings.split('/'))
        passed += pass_
        totals += total

print "The average is: {:.1%}".format(passed / totals)

rawDescription: Analyzing cumbersome information

description: Analyzing cumbersome information


code:
import wmi

wmi_obj = wmi.WMI()
wmi_sql = "select IPAddress,DefaultIPGateway from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"
wmi_out = wmi_obj.query( wmi_sql )

for dev in wmi_out:
    print "IPv4Address:", dev.IPAddress[0], "DefaultIPGateway:", dev.DefaultIPGateway[0]

rawDescription: How can I get the default gateway IP with Python?

description: Get the default gateway IP with Python


code:
source='/tmp'

import os
import re
import datetime as dt

pat=re.compile(r'^Data_(A|B)_(\d{4}-\d{2}-\d{2}_\d+-\d+-\d+-\d+)')

def test_list(l):
    return (len(l)==2 and {t[1] for t in l}==set('AB'))

def round_time(dto, round_to=60):
    seconds = (dto - dto.min).seconds
    rounding = (seconds+round_to/2) // round_to * round_to
    return dto + dt.timedelta(0,rounding-seconds,-dto.microsecond)

fnames={}
for fn in os.listdir(source):
    p=os.path.join(source, fn)
    if os.path.isfile(p):
        m=pat.search(fn)
        if m:
            d=round_time(dt.datetime.strptime(m.group(2), '%Y-%m-%d_%H-%M-%S-%f'), round_to=60)
            fnames.setdefault(str(d), []).append((p, m.group(1)))

for k, v in [(k, v) for k, v in fnames.items() if not test_list(v)]:
    for fn in v:
        print fn[0]    # This is the file that does NOT have a pair -- delete?

rawDescription: Python: Compare Filenames in Folder

description: Python: Compare Filenames in Folder


code:
#!/usr/bin/env python

""" A bit field class

    See http://stackoverflow.com/q/31960327/4014959

    Written by PM 2Ring 2015.08.12
"""

class BitFields(object):
    """ A bit field class

        fieldwidth is a tuple or list containing the
        bit width of each field, from least significant
        to most significant.
    """
    def __init__(self, totalwidth, fieldwidths):
        if sum(fieldwidths) != totalwidth:
            raise ValueError, "Field width error"

        self.fieldwidths = fieldwidths
        self.num_fields = len(fieldwidths)

        #Calculate field offsets
        self.offsets = []
        pos = 0
        for w in fieldwidths:
            self.offsets.append(pos)
            pos += w

        #Set up bitfield attribute names
        self.field_names = ['b' + str(i) for i in range(self.num_fields)]
        self.clear()

    #Set all fields to zero
    def clear(self):
        for f in self.field_names:
            setattr(self, f, 0)

    #A generator expression of all the field values
    def _all_fields(self):
        return (getattr(self, f) for f in self.field_names)

    def __str__(self):
        return ', '.join(['%s: 0x%x' % (f, v)
            for f, v in zip(self.field_names, self._all_fields())])

    #Get the register value as an int
    @property
    def value(self):
       return sum(v<<p for v, p in zip(self._all_fields(), self.offsets))

    #Set field values
    def regset(self, **kwargs):
        for f, v in kwargs.items():
            setattr(self, f, v)

#Test
fields = (16, 8, 12, 12, 6, 4, 6)
reg = BitFields(64, fields)

#Set some fields by attribute
reg.b0 = 10
reg.b1 = 1
reg.b2 = 3

#Print the register using its __str__ method
print reg

#Print a single field
print reg.b1

#Print the current value of the register in decimal and as a hex string
v = reg.value
print v, hex(v)

#Reset all fields to zero
reg.clear()
print reg

#Set some fields by keyword
reg.regset(b0=7, b1=3, b2=1)
print reg

#Set some fields using a dict
field_dict = {'b0':5, 'b3':0xa, 'b4':0xf}
reg.regset(**field_dict)
print reg

rawDescription: How to do bit manipulation of 64 bit data and output to a C function in a dll

description: Do bit manipulation of 64 bit data and output to a C function in a dll


code:
for entry in courselist:
    if 'calc' in entry.lower():
        print entry

rawDescription: pyparsing: extracting strings containing specific text

description: Pyparsing: extracting strings containing specific text


code:
from datetime import datetime
import pytz
from dateutil import rrule
# this is raw data I get from the DB, according to django docs I store it in UTC
raw = datetime.utcnow().replace(tzinfo=pytz.UTC)
# in addition I need to store the timezone so I can do dst the calculations
tz = pytz.timezone("Europe/Warsaw")
# this means that the actual local time would be
local = raw.astimezone(tz)
# but rrule doesn't take into account DST and local time, so I must convert aware datetime to naive
naive = local.replace(tzinfo=None)
# standard rrule
r = rrule.rrule(rrule.DAILY,interval=180,count=10,dtstart=naive)
for dt in r:
    # now we must get back to aware datetime - since we are using naive (local) datetime,
    print tz.localize(dt)

rawDescription: How to handle DST and TZ in recurring events?

description: Handle DST and TZ in recurring events


code:
import RDF
parser=RDF.Parser(name="ntriples") #as name for parser you can use ntriples, turtle, rdfxml, ...
model=RDF.Model()
stream=parser.parse_into_model(model,"file://file_path","http://your_base_uri.org")
for triple in model:
    print triple.subject, triple.predicate, triple.object

rawDescription: Parse large RDF in Python

description: Parse large RDF in Python


code:
for t, row in enumerate(MyDictReader(open(train))):
    pass
print row

rawDescription: empty key while using dictread

description: Empty key while using dictread


code:
import numpy as np
import re
from bs4 import BeautifulSoup

html_doc = '''

    Journal of     Infection (2015)

    xx
    , 1
    e
    4

'''

soup = BeautifulSoup(html_doc,'html.parser')
my_att = [i.attrs['style'] for  i in soup.find_all("div")]
css = ''.join(my_att)
print css
width_list = map(float,re.findall(r'(?<=width:)(\d+)(?=px;)', css))
height_list = map(float,re.findall(r'(?<=height:)(\d+)(?=px;)', css))
print np.mean(height_list)
print np.mean(width_list)

rawDescription: compute the average height and the average width of div tag

description: Compute the average height and the average width of div tag


code:
0xfffffffff418c5c1L
0xbe73a3fL

rawDescription: how to convert negative integer value to hex in python

description: Convert negative integer value to hex in python


code:
import re
f = open("data.txt", "r")
regex = re.compile(r'Wavelength \(Angstrom\)=([0-9]+\.?[0-9]*)')
for line in f:
    result = regex.search(line)
print result.group(1)

rawDescription: How to read in metadata (with tags) from a text file using Python

description: Read in metadata (with tags) from a text file using Python


code:
from selenium import webdriver
from browsermobproxy import Server

server = Server(environment.b_mob_proxy_path)
server.start()
proxy = server.create_proxy()
service_args = ["--proxy-server=%s" % proxy.proxy]
driver = webdriver.PhantomJS(service_args=service_args)

proxy.new_har()
driver.get('url_to_open')
print proxy.har  # this is the archive
# for example:
all_requests = [entry['request']['url'] for entry in proxy.har['log']['entries']]

rawDescription: Network capturing with Selenium/PhantomJS


code:
import re

pattern = re.compile("pid=(\w+)")
for item in g_1:
    links = soup.find_all('a', href=re.compile('^/on/demandware.store/Sites-BNY-Site/default/Product-Variation'))
    for link in links[1:]:
        match = pattern.search(link["href"])
        if match:
            print match.group(1)

rawDescription: Extract single href from a web page

description: Extract single href from a web page


code:
loop_count, o_count = 0, 0
while loop_count<len(phrase):
    # loop_count represents how many times we have looped so far
    if "o" == phrase[loop_count].lower(): # We ask if the current letter is 'o'
        o_count += 1 # If it is an 'o', increment count
    loop_count += 1 # Increment this every time, it tracks our loop count

print o_count

rawDescription: Trying to count the number of times a letter appears using only a while loop


code:
for item in list[:-1]:
    print "Not last: ", item
print "Last: ", list[-1]

rawDescription: How to treat the last element in list differently in Python?

description: Treat the last element in list differently in Python


code:
import socket
import sys

def main():
    sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
    sock.settimeout(1)
    try:
        sock.sendto('', ('127.0.0.1', 6666))
        reply, _ = sock.recvfrom(4096)
        print reply
    except socket.timeout:
        sys.exit(1)
    finally:
        sock.close()

if __name__ == '__main__':
    main()

rawDescription: Run python script without importing class everytime


code:
from collections import OrderedDict
from json import JSONDecoder

def make_unique(key, dct):
    counter = 0
    unique_key = key

    while unique_key in dct:
        counter += 1
        unique_key = '{}_{}'.format(key, counter)
    return unique_key

def parse_object_pairs(pairs):
    dct = OrderedDict()
    for key, value in pairs:
        if key in dct:
            key = make_unique(key, dct)
        dct[key] = value

    return dct

data = """
{"foo": {"baz": 42, "baz": 77}, "foo": 7, "foo": 23}
"""

decoder = JSONDecoder(object_pairs_hook=parse_object_pairs)
obj = decoder.decode(data)
print obj

rawDescription: Python json parser allow duplicate keys

description: Python json parser allow duplicate keys


rawDescription: Zipping specific elements of a list in Python


code:
if i.startswith("'") and i.endswith("'"):
    print "found"

rawDescription: Python find string enclosed in single quotes


code:
import json
not_encoded = {"created_at":"Fri Aug 08 11:04:40 +0000 2014"}
encoded_data = json.dumps(not_encoded)
print encoded_data
double_encode = json.dumps(encoded_data)
print double_encode

rawDescription: Python: Dump to Json adds additional double quotes and escaping of quotes


code:
setup = """
def create_generator(num):
    mylist = range(num)
    for i in mylist:
        yield i

class Generator(object):

    def __init__(self, num):
        self.start = 0
        self.end = num

    def __iter__(self):
        return self

    def next(self):
        if self.start == self.end:
            raise StopIteration
        else:
            self.start = self.start + 1
            return self.start

class Generator2(object):

    def __init__(self, num):
        self.mylist = range(num)

    def __iter__(self):
        for i in self.mylist:
            yield i
"""

import timeit

print timeit.timeit('for p in create_generator(1000):p', setup, number=1000)
print timeit.timeit('for p in Generator(1000):p', setup, number=1000)
print timeit.timeit('for p in Generator2(1000):p', setup, number=1000)

rawDescription: python - Overhead on looping over an iterable class

description: Python - Overhead on looping over an iterable class


code:
for i, x in reversed(list(enumerate(lines[0]))):
    if x not in headers:
        for line in lines:
            line.pop(i)
print lines

rawDescription: Python: pop item in list of lists by index


code:
from guppy import hpy
h = hpy()
print h.heap()

rawDescription: Python: memory usage statistics per object-types (or source code line)


code:
len(packets)

for i in range(len(packets)):
    print packets[i].summary()

# or better:
for i in packets:
    print i.summary()

rawDescription: Scapy - count number of sniffed packets?

description: Scapy - count number of sniffed packets


code:
def savitzky_golay(y, window_size, order, deriv=0, rate=1):

    import numpy as np
    from math import factorial

    try:
        window_size = np.abs(np.int(window_size))
        order = np.abs(np.int(order))
    except ValueError, msg:
        raise ValueError("window_size and order have to be of type int")
    if window_size % 2 != 1 or window_size < 1:
        raise TypeError("window_size size must be a positive odd number")
    if window_size < order + 2:
        raise TypeError("window_size is too small for the polynomials order")
    order_range = range(order+1)
    half_window = (window_size -1) // 2
    # precompute coefficients
    b = np.mat([[k**i for i in order_range] for k in range(-half_window, half_window+1)])
    m = np.linalg.pinv(b).A[deriv] * rate**deriv * factorial(deriv)
    # pad the signal at the extremes with
    # values taken from the signal itself
    firstvals = y[0] - np.abs( y[1:half_window+1][::-1] - y[0] )
    lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] - y[-1])
    y = np.concatenate((firstvals, y, lastvals))
    return np.convolve( m[::-1], y, mode='valid')

rawDescription: how to smooth a curve in python

description: Smooth a curve in python


code:
print my_dict
{'Steve': ['Red', 'Vauxhall'], 'David': ['Red', 'Ford', 'Blue', 'Aspergers']}

rawDescription: How to combine multiple lines in CSV into one line

description: Combine multiple lines in CSV into one line


code:
class Wrapper(object):
    def __init__(self,wrapped_class):
        self.wrapped_class = wrapped_class()

    def __getattr__(self,attr):
        orig_attr = self.wrapped_class.__getattribute__(attr)
        if callable(orig_attr):
            def hooked(*args, **kwargs):
                self.pre()
                result = orig_attr(*args, **kwargs)
                # prevent wrapped_class from becoming unwrapped
                if result == self.wrapped_class:
                    return self
                self.post()
                return result
            return hooked
        else:
            return orig_attr

    def pre(self):
        print ">> pre"

    def post(self):
        print "<< post"

rawDescription: Create a wrapper class to call a pre and post function around existing functions?


code:
lookup = dict(map(str.strip, line.split(':')) for line in re.split('<.*?>', text) if ':' in line)
print lookup['Your Hunger Level']
# 'Very Hungry'

rawDescription: How to match the following with regex in python?

description: Match the following with regex in python


code:
import cql
con = cql.connect('localhost', keyspace='ks1', cql_version='3.0.0')
cursor = con.cursor()
last = ""
while last != None:
    cursor.execute("select * from test1 where a=:a and b>:b limit 5", {"a": "a", "b": last})
    last = None
    for row in cursor:
        print row
        last = row[1]

rawDescription: Cassandra pagination: How to use get_slice to query a Cassandra 1.2 database from Python using the cql library


code:
df = pd.read_table('file.csv', sep=' ', header=None, parse_dates=[[0,1]])
print df.to_string()

rawDescription: Calcuate Duration and Average of CSV file data using python


code:
if 'A' in b.keys():
    print b['A']
c=[]

rawDescription: How to access this dictionary of dictionaries with a numbered index in Python?

description: Access this dictionary of dictionaries with a numbered index in Python


code:
def upside_down_asterix_triangle(i, t=0):
    if i == 0:
        return 0
    else:
        print ' ' * ( t + 1 ) + '*' * ( i * 2 - 1 )
        return upside_down_asterix_triangle( i - 1, t + 1 )

upside_down_asterix_triangle(5)

rawDescription: Asterisk triangle in python

description: Asterisk triangle in python


code:
longg = dict.fromkeys((0,1,2,3,4,5,),0)

for tu,x in mylist:
    for i,el in enumerate(tu):
        longg[i] = max(longg[i],len(str(el)))

fofo = '  '.join('%'+str(longg[i])+'s' for i in xrange(0,6)) + '  %s'
print '\n'.join(fofo % (a,b,c,d,e,f,g) for (a,b,c,d,e,f),g in mylist)

rawDescription: Pretty printing a list in a tabular format


code:
import csv
reader = csv.reader(open('test.csv'))

result = {row[0]:row[1:] for row in reader if row and row[0]}
print result

rawDescription: Reading csv file in Python and create a dictionary


code:
from Crypto.Cipher import Blowfish
from binascii import hexlify, unhexlify

perl_pass = unhexlify("9033c838e4418fbdc50a9fc0813745296d195d59954008f94b2b36a8e65dca959686206960a7828a")

key = unhexlify("781e5e245d69b566979b86e28d23f2c78e938564cd1410f0ec1c1781466a6738bab0a6ed984c75ab34c68bbf7558077714043c5bdb959e46")
iv  = '$KJh#(}q'
num_padding = ord(Blowfish.new(key, Blowfish.MODE_CBC, iv).decrypt(perl_pass)[-1])

print Blowfish.new(key, Blowfish.MODE_CBC, iv).decrypt(perl_pass)[:(-1*num_padding)]

rawDescription: Using PyCrypto to decrypt Perl encrypted password


code:
from operator import itemgetter

words = {'fall':4, 'down':4, 'was':3, 'a':3, 'zebra':2, 'bitter':1, 'betty':1}
sorted_words = [v for v in sorted(words.iteritems(), key=lambda(k, v): (-v, k))]
print(sorted_words)

rawDescription: sorting of python dict, first by value then by key

description: Sorting of python dict, first by value then by key


code:
print handle.read()
# >gi|75192198|sp|Q9MAH8.1|TCP3_ARATH RecName: Full=Transcription factor TCP3
# MAPDNDHFLDSPSPPLLEMRHHQSATENGGGCGEIVEVQGGHIVRSTGRKDRHSKVCTAKGPRDRRVRLS
# APTAIQFYDVQDRLGFDRPSKAVDWLITKAKSAIDDLAQLPPWNPADTLRQHAAAAANAKPRKTKTLISP
# PPPQPEETEHHRIGEEEDNESSFLPASMDSDSIADTIKSFFPVASTQQSYHHQPPSRGNTQNQDLLRLSL
# QSFQNGPPFPNQTEPALFSGQSNNQLAFDSSTASWEQSHQSPEFGKIQRLVSWNNVGAAESAGSTGGFVF
# ASPSSLHPVYSQSQLLSQRGPLQSINTPMIRAWFDPHHHHHHHQQSMTTDDLHHHHPYHIPPGIHQSAIP
# GIAFASSGEFSGFRIPARFQGEQEEHGGDNKPSSASSDSRH

rawDescription: How do I navigate results of a Biopython Entrez efetch?

description: Navigate results of a Biopython Entrez efetch


code:
numbers = [1, 2, 3, 4, 5, 6]
letters = ['a', 'b', 'c', 'd', 'e', 'f']

#Using zip to create a list of tuples containing pairs of elements of numbers & letters
print zip(numbers, letters)

#zip works on other iterable objects, including strings
print zip(range(1, 7), 'abcdef')

#zip can handle more than 2 iterables
print zip('abc', 'def', 'ghi', 'jkl')

#zip can be used in a for loop to process two (or more) iterables simultaneously
for n, l in zip(numbers, letters):
    print n, l

#Using zip in a list comprehension to make a list of lists
print [[l, n] for n, l in zip(numbers, letters)]

#zip stops if one of the iterables runs out of elements
print [[n, l] for n, l in zip((1, 2), letters)]
print [(n, l) for n, l in zip((3, 4), letters)]

#Turning an iterable into an iterator object using using the iter function
iletters = iter(letters)

#When we take some elements from an iterator object it remembers where it's up to
#so when we take more elements from it, it continues from where it left off.
print [[n, l] for n, l in zip((1, 2, 3), iletters)]
print [(n, l) for n, l in zip((4, 5), iletters)]

#This list will just contain a single tuple because there's only 1 element left in iletters
print [(n, l) for n, l in zip((6, 7), iletters)]

#Rebuild the iletters iterator object
iletters = iter('abcdefghijkl')

#See what happens when we zip multiple copies of the same iterator object.
print zip(iletters, iletters, iletters)

#It can be convenient to put multiple copies of an iterator object into a list
iletters = iter('abcdefghijkl')
gang = [iletters] * 3

#The gang consists of 3 references to the same iterator object
print gang

#We can pass each iterator in the gang to zip as a separate argument
#by using the "splat" syntax
print zip(*gang)

#A more compact way of doing the same thing:
print zip(* [iter('abcdefghijkl')]*3)

rawDescription: How to get a split up a list of numbers and insert into another list

description: Get a split up a list of numbers and insert into another list


code:
class c_ulong(_ctypes._SimpleCData):
    _type_ = "L"

    def __rshift__(self, other):
        print '__rshift__', self, other
        if hasattr(other, 'value'):
            other = other.value
        return c_ulong(self.value >> other)

c_ulong_type = PyTypeObject.from_address(id(c_ulong))

rawDescription: ctypes reimplementation of rshift for c_ulong

description: Ctypes reimplementation of rshift for c_ulong


code:
ref_dict = {}
with open("so_cnt_file.txt") as infile:
    next(infile)
    cur_start_row = 0
    cur_rows = []
    for line in infile:
        row, col = [int(val) for val in line.strip().split(" ") if val]
        if col == 0:
            for cur_row in cur_rows:
                ref_dict[cur_row] = row - cur_row - 1
            cur_start_row = row
            cur_rows = []
            continue
        cur_rows.append(row)
print ref_dict

rawDescription: Python counting lines in files using exact locations


code:
import re
from urlparse import unquote

from bs4 import BeautifulSoup
import requests

url = "http://rads.stackoverflow.com/amzn/click/1597805483"
page = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36'})
soup = BeautifulSoup(page.content)

script = soup.find('script', text=lambda x:'bookDescEncodedData' in x)
match = re.search(r'bookDescEncodedData = "(.*?)",', script.text)
if match:
    description_html = BeautifulSoup(unquote(match.group(1)))
    print description_html.text

rawDescription: how to scrape imbeded script on webpage in python

description: Scrape imbeded script on webpage in python


code:
file1 = open('file1.txt', 'r')
file2 = open('file2.txt', 'w')
for lines in file1.readlines():
    if lines == '\n':
        print 'Empty line'
    else:
        file2.write(lines+'\n')

file1.close()
file2.close()

rawDescription: Stripping empty lines from the print statement in python2.7


code:
from collections import namedtuple
OSMap = namedtuple("OSMap", ("os", "version"))

os_map = dict()
os_map["A"] = OSMap("Windows", "7")
os_map["B"] = OSMap("Ubuntu", "13.10")
print os_map["A"].version, os_map["A"].os
print os_map["B"].version, os_map["B"].os

rawDescription: Python datastructure for map like access


code:
with open('file.txt', 'r') as handle:
    for gene_id, nucleotides in parse_file(handle):
        print gene_id, nucleotides

rawDescription: Rewind File Pointer to Previous Line Python File IO


code:
from subprocess import *

c = 'dir' #Windows

handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True)
print handle.stdout.read()
handle.flush()

rawDescription: subprocess.Popen in different console

description: Subprocess.Popen in different console


code:
for i in testDict:
     print i, testDict[i]

rawDescription: Python Iterate Dictionary by Index

description: Python Iterate Dictionary by Index


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

am_i_alive()

rawDescription: What are the errors in this code?

description: What are the errors in this code


code:
import numpy as np

treedarray = np.zeros([3,3,3])
print treedarray

rawDescription: python 3d array like in C ++

description: Python 3d array like in C ++


code:
class mylist(list):
    def __new__(cls, obj=None):
        return list.__new__(cls, obj)

    def mymethod(self, msg):
        print msg, id(self)

def main():
    a = mylist('abc')
    print a, id(a)
    a.mymethod('hello')

if __name__ == '__main__':
    main()

rawDescription: Wrap an object into a derived class

description: Wrap an object into a derived class


code:
mytring = u"æthelred"
print mytring.capitalize()

rawDescription: Capitalize doesn't work on special characters - Python


code:
import re
p = re.compile(ur'(\[[^\]]*\][^\[]+)(?:\s|$)')
test_str = u"[abc]\nline_one xxxxxxxxxxxxxx\nline_two xxxxxxxxxxxxxx\n[pqr]\nline_four xxxxxxxxxxxxxx\nline_five xxxxxxxxxxxxxx\n[xyz]\nline_six xxxxxxxxxxxxxx\nline_seven xxxxxxxxxxxxxx"

re.findall(p, test_str)

rawDescription: regular expression to extract sections

description: Regular expression to extract sections


code:
a = 0
b = 0
value = 0
for value in range(100):
    if a == value:
        print "foo", #HERE IS THE ELUSIVE COMMA!
    if b == value :
        print "bar"
    a = a + 1
    b = b + 1

rawDescription: Execute multiple if loops

description: Execute multiple if loops


code:
row = ["Col1", "Col2 has [[foo]] in it", "Col3", "Col4 has [[foo]]] and [[bar]]"]

for col in row:
    print re.findall(r'\[\[(.*?)\]\]', col, re.M|re.I)

rawDescription: Regex Expression with special charachters


code:
start = 0
end = 0

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

print sum([x for x in range(start, end+1) if x % 2 == 0])

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


code:
for element in tree.findall('.//item') + tree.findall('.//test'):
    print element.text

rawDescription: ElementTree findall 'or' operator

description: ElementTree findall 'or' operator


code: print unichr(169)

rawDescription: Copyright symbol for a Pyramid app

description: Copyright symbol for a Pyramid app


code:
healthy=['39','41','49','50','51','52','53','54','56']

df['type'] = np.where(df['Geneid'].astype(str).isin(healthy), 'h', 'd')

#get last column to list
print df.columns[-1].split()
['type']

#create new list from last column and all columns without last
cols = df.columns[-1].split() + df.columns[:-1].tolist()
print cols
['type', 'Geneid', 'PRKCZ.exon1', 'PRKCZ.exon2', 'PRKCZ.exon3', 'PRKCZ.exon4',
 'PRKCZ.exon5', 'PRKCZ.exon6', 'PRKCZ.exon7', 'PRKCZ.exon8', 'PRKCZ.exon9',
 'PRKCZ.exon10', 'FLNA.exon31', 'FLNA.exon32', 'FLNA.exon33', 'FLNA.exon34',
 'FLNA.exon35', 'FLNA.exon36', 'FLNA.exon37', 'FLNA.exon38', 'MTCP1.exon1', 'MTCP1.exon2']

rawDescription: Renaming a subset of index from a dataframe


code:
import httplib2
import os

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://mail.google.com/'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Gmail API Quickstart'

def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'gmail-quickstart.json')

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatability with Python 2.6
            credentials = tools.run(flow, store)
        print 'Storing credentials to ' + credential_path
    return credentials

import base64
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import mimetypes
from httplib2 import Http

from apiclient import errors

from apiclient.discovery import build
credentials = get_credentials()
service = build('gmail', 'v1', http=credentials.authorize(Http()))

def SendMessage(service, user_id, message):
  """Send an email message.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    message: Message to be sent.

  Returns:
    Sent Message.
  """
  try:
    message = (service.users().messages().send(userId=user_id, body=message)
               .execute())
    print 'Message Id: %s' % message['id']
    return message
  except errors.HttpError, error:
    print 'An error occurred: %s' % error

def CreateMessage(sender, to, subject, message_text):
  """Create a message for an email.

  Args:
    sender: Email address of the sender.
    to: Email address of the receiver.
    subject: The subject of the email message.
    message_text: The text of the email message.

  Returns:
    An object containing a base64 encoded email object.
  """
  message = MIMEText(message_text)
  message['to'] = to
  message['from'] = sender
  message['subject'] = subject
  return {'raw': base64.b64encode(message.as_string())}

testMessage = CreateMessage('EMAIL ADDRESS', 'EMAIL ADDRESS', 'subject', 'YOUR MESSAGE')

testSend = SendMessage(service, 'me', testMessage)

rawDescription: Where do I get the Authorized Gmail API service instance? (python, gmail api)


code:
EPSILON = 0.0001
number = 9.3
while number % 0.5 > EPSILON:
    number -= 0.1
print number

rawDescription: Python: weird floating-point math results


code: print u'\xa3'

rawDescription: Converting html source content into readable format with Python 2.x


code:
import threading
import time
import random

def worker(f_n):
    with open(f_n) as f:
        time.sleep(random.random())
        print f_n, " => ", f.read()

f_list = ['a','b', 'c', 'd', 'e']

threads = []
for f_n in f_list:
    t = threading.Thread(target=worker, args=(f_n))
    threads.append(t)

for t in threads:
    t.start()

for t in threads:
    t.join()

rawDescription: Read in parallel multiples files in Python


code:
def main():
    number = int(raw_input("What number do you want to find the sum of cubes for?"))
    #this doesn't do anything but change n to 0
    #for i in range(number):
    #    n = n - 1
    print "The result of the sums is:", sumNCubes(number)
main()

rawDescription: How to add the sum of cubes using a function in python?

description: Add the sum of cubes using a function in python


code:
source
import networkx as nx

class Agent(object):
    def __init__(self, id):
        self.id = id
    def __repr__(self):
        return '<Agent #{}>'.format(self.id)

g = nx.complete_graph(5)
print g.nodes()

nx.relabel_nodes(g, mapping=Agent, copy=False)
print g.nodes()

rawDescription: Using graph generator with a custom class as node


code:
lineskip1 = 0
for line in airtemp:
    lineskip1 += 1
    if lineskip1 <= 12:  # Skip lines numbered 1 through 12
        continue
    print line

rawDescription: How can I skip the header in Python?

description: Skip the header in Python


code:
from subprocess import Popen, PIPE
pp = Popen('dir', shell=True, stdout=PIPE)

for ln in pp.stdout:
    print '#',ln

rawDescription: Python, redirecting the stream of Popen to a python function


code:
x = np.empty((3,4), dtype=np.object)
print x
x[2,3] = range(5)
x[1,2] = 2
x[1,3] = (item*2 for item in xrange(10))
print x

rawDescription: Python: Multi-dimensional array of different types


code:
data = urllib2.urlopen('http://www.example.com').read()
print data

rawDescription: HTTP Banner Grabbing with Python

description: HTTP Banner Grabbing with Python


code:
f_open = open('file.txt','r')
data = f_open.read()
f_open.close()

print data

rawDescription: importing a text file in python

description: Importing a text file in python


code:
# blood pressure one week ago:
print data[(datetime.datetime.now() - WEEK).date()]['blood_pressure']

# the entire data for two months ago:
print data[(datetime.datetime.now() - 2 * MONTH).date()]

rawDescription: Python question relating to dated values in a csv file


code:
import csv

with open(inputPath, "r") as f_input:
    csv_input = csv.reader(f_input, delimiter=" ", skipinitialspace=True)
    first_row = csv_input.next()

    print "@relation 'o_wekaarff.arff'"

    for col in first_row[:-2]:
        print "@ATTRIBUTE 'att [{0}]' numeric".format(col)

    for col, attribute in zip(first_row[-2:], ["binary", "string"]):
        print "@ATTRIBUTE 'att [{0}]' {1}".format(col, attribute)

    print "\n@data"

    for row in csv_input:
        print ",".join(row)

rawDescription: Automatically identifying ultimate and penultimate columns as variable in python


code:
def get_logp(self):
    if self.verbose > 1:
        print '\t' + self.__name__ + ': log-probability accessed.'
    logp = self._logp.get()
    if self.verbose > 1:
        print '\t' + self.__name__ + ': Returning log-probability ', logp

    try:
        logp = float(logp)
    except:
        raise TypeError, self.__name__ + ': computed log-probability ' + str(logp) + ' cannot be cast to float'

    if logp != logp:
        raise ValueError, self.__name__ + ': computed log-probability is NaN'

    # Check if the value is smaller than a double precision infinity:
    if logp <= d_neg_inf:
        if self.verbose > 0:
            raise ZeroProbability, self.errmsg + ": %s" %self._parents.value
        else:
            raise ZeroProbability, self.errmsg

    return logp

def set_logp(self,value):
    raise AttributeError, 'Potential '+self.__name__+'\'s log-probability cannot be set.'

logp = property(fget = get_logp, fset=set_logp, doc="Self's log-probability value conditional on parents.")

rawDescription: Decorators in PyMC

description: Decorators in PyMC


code:
def iterate_with_parent(elem):
    stack = []
    while 1:
        for child in reversed(elem):
            stack.append((child, elem))
        if not stack: return
        elem, parent = stack.pop()
        yield elem, parent
showtag = lambda e: e.tag if e is not None else None
showtext = lambda e: repr((e.text or '').rstrip())
for e, p in iterate_with_parent(page):
    print e.tag, showtext(e), showtag(p)

rawDescription: Iterating multiple (parent,child) nodes using Python ElementTree


code:
var_value = "whatever you want the variable to be"

jsfile = open("myscript.js", "rb")
for line in jsfile:
  print  line.replace("$MYVAR$", var_value)

jsfile.close()

rawDescription: Setting variable values in JavaScript files


code:
import gurobipy  as grb
GRB = grb.GRB
m = grb.Model()

x = m.addVar(0.0, 1.0, vtype=GRB.CONTINUOUS)
y = m.addVar(0.0, 1.0, vtype=GRB.CONTINUOUS)
m.update()
# add constraints so that y >= |x - 0.75|
m.addConstr(y >= x-0.75)
m.addConstr(y >= 0.75 - x)
m.setObjective(y)
m.update()
m.optimize()
print x.X
# 0.75
x.vtype=GRB.BINARY
m.optimize()
print x.X
# 1.0

rawDescription: Gurobi, How to change a continuous variable to a binary variable


code:
import collections

def isPalindrome(s):
    return s.lower() == s[::-1].lower()

def anagramchk(word,chkword):
    return sorted(word.lower()) == sorted(chkword.lower())

def allAnagrams(dictfilepath):
    answer = collections.defaultdict(list)
    with open(dictfilepath) as dictfile:
        for line in dictfile:
            word = line.strip().lower()
            answer[''.join(sorted(word))].append(word)
    return answer

def fetchAllAnagrams(wordin, anagrams):
    return anagrams[''.join(sorted(wordin.lower()))]

def main(dictfilepath, palsfilepath):
    anagrams = allAnagrams(dictfilepath)
    with open(palsfilepath) as palfile:
        for line in palfile:
            word = line.strip().lower()
            if isPalindrome(word):
                for anagram in anagrams[''.join(sorted(word))]:
                    if isPalindrome(anagram):
                        print "%s is an anagram of %s" %(anagram, word)

rawDescription: Finding a palindrome from anagrams

description: Finding a palindrome from anagrams


code:
import wx

class Mainframe(wx.Frame):
    myId1 = wx.NewId()
    myId2 = wx.NewId()

    def __init__(self, parent):
        wx.Frame.__init__(self, parent)
        self.panel = wx.Panel(self)

        sizer = wx.BoxSizer(wx.VERTICAL)

        txt1 = wx.StaticText(self.panel, id=self.myId1,
                             label='test', name="test1")
        txt2 = wx.StaticText(self.panel, id=self.myId2,
                             label='test', name="test2")

        btn = wx.Button(self.panel, label="Find by id")
        btn.Bind(wx.EVT_BUTTON, self.onFindById)
        btn2 = wx.Button(self.panel, label="Find by name")
        btn2.Bind(wx.EVT_BUTTON, self.onFindByName)

        sizer.Add(txt1, 0, wx.ALL, 5)
        sizer.Add(txt2, 0, wx.ALL, 5)
        sizer.Add(btn, 0, wx.ALL, 5)
        sizer.Add(btn2, 0, wx.ALL, 5)

        self.panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onFindById(self, event):
        """"""
        print "myId1 = " + str(self.myId1)
        print "myId2 = " + str(self.myId2)

        txt1 = wx.FindWindowById(self.myId1)
        print type(txt1)

        txt2 = wx.FindWindowById(self.myId2)
        print type(txt2)

    #----------------------------------------------------------------------
    def onFindByName(self, event):
        """"""
        txt1 = wx.FindWindowByName("test1")
        txt2 = wx.FindWindowByName("test2")

if __name__ == '__main__':
        app = wx.App(False)
        frame = Mainframe(None)
        frame.Show()
        app.MainLoop()

rawDescription: How can I change label of statictext without naming it

description: Change label of statictext without naming it


code: print "mo.group({:}) output is : {:}".format(i,x)

rawDescription: How to print out the command used to execute code without executing it in Python

description: Print out the command used to execute code without executing it in Python


code:
import random
import operator

# Easier to use a mutable list than an immutable string for insertion.
sequence = list('123456789123456789')
insertions = '999 888 777 666 555 444 333 222 111'.split()
locations = [random.randrange(len(sequence)) for i in xrange(10)]
modifications = zip(locations,insertions)
print modifications
# sort them by location.
# Since Python 2.2, sorts are guaranteed to be stable,
# so if you insert 999 into 1, then 222 into 1, this will keep them
# in the right order
modifications.sort(key=operator.itemgetter(0))
print modifications
# apply in reverse order
for i,seq in reversed(modifications):
    print 'insert {} into {}'.format(seq,i)
    # Here's where using a mutable list helps
    sequence[i:i] = list(seq)
    print ''.join(sequence)

rawDescription: Insertions algorithm in sequence python

description: Insertions algorithm in sequence python


code:
s = 'smith, bob;jones, bill;doe, john'
for lname, fname in [q.split(",") for q in s.split(";")]:
    print fname, lname

rawDescription: Python and flipping the order of lastname, firstname


code:
status=''
text=text.strip()
for index,line in enumerate(text):
    if index>3:
        status = line
if 'ON' in status:
    print "it's on"
else:
    print "it's off"

rawDescription: Read a URL in Raspberry Pi

description: Read a URL in Raspberry Pi


code:
class Capture():
    def __init__(self):
        self.capturing = False
        self.c = cv2.VideoCapture(0)

    def startCapture(self):
        print "pressed start"
        self.capturing = True
        cap = self.c
        while(self.capturing):
            ret, frame = cap.read()
            cv2.imshow("Capture", frame)
            cv2.waitKey(5)
        cv2.destroyAllWindows()

    def endCapture(self):
        print "pressed End"
        self.capturing = False
        # cv2.destroyAllWindows()

    def quitCapture(self):
        print "pressed Quit"
        cap = self.c
        cv2.destroyAllWindows()
        cap.release()
        QtCore.QCoreApplication.quit()

rawDescription: OpenCV Video Capture with PyQt4

description: OpenCV Video Capture with PyQt4


code:
import numpy as np

def test(row):
    return np.cumsum(row)

x = np.arange(20).reshape((4,5))
print np.apply_along_axis(test, 1, x)

rawDescription: numpy ndarrays: row-wise and column-wise operations

description: Numpy ndarrays: row-wise and column-wise operations


code:
def output(*args):
    print ' '.join(str(i) for i in args)

rawDescription: How can I apply a list as a variable number of arguments to the python print built-in?

description: Apply a list as a variable number of arguments to the python print built-in


code: for i in range(10): print DoDict['do1'](fruits)

rawDescription: How can I insert a random element into a dictionary and have it randomised whenever I call it?

description: Insert a random element into a dictionary and have it randomised whenever I call it


code:
from HTMLParser import HTMLParser

class MyHTMLParser(HTMLParser):

    data = ""
    search_tag = None
    grab_data = False

    def feed(self, data, tag_to_search_for):

        self.search_tag = tag_to_search_for
        HTMLParser.feed(self, data)

    def handle_starttag(self, tag, attrs):

        if tag == self.search_tag:
            self.grab_data = 1

    def handle_data(self, data):

        if self.grab_data:
            self.data = data

    def handle_endtag(self, tag):

        if tag == self.search_tag:
            self.grab_data = 0

xml = """<Test_input_data>
    <web_pins type="list" orientation="column">
        <web_pin_id type="column_definition" data_type="int" index="1"/>
        <pin_center_node type="column_definition" data_type="int" index="2"/>
        <journal_center_node type="column_definition" data_type="int" index="3"/>
         1  1012  5011
         2  2012  5012
         3  3012  5013
    </web_pins>
</Test_input_data>"""

parser = MyHTMLParser()
parser.feed(xml, "web_pins")
print parser.data #Ta-daa!

rawDescription: How to extract strange XML-table via Python

description: Extract strange XML-table via Python


rawDescription: Finding the amount of time difference between dates in python


code:
from itertools import product
print [''.join(letters)
       for letters in product(firsts, seconds, thirds, fourths, fifths)]

rawDescription: Python script to generate words from letters and two letter combination


code:
import pdb
Pdb = pdb.Pdb

Pdb._do_list = Pdb.do_list
def pdb_list_wrapper(self, arg):
    if arg.strip().lower() in ('r', 'reset', 'c', 'current'):
        self.lineno = None
        arg = ''
    self._do_list(arg)
Pdb.do_list = Pdb.do_l = pdb_list_wrapper

a = 1
b = 2

pdb.set_trace()

print a, b

rawDescription: In pdb how do you reset the list (l) command line count?


code:
mapped = map(lambda x: offline_optimize(**x), inputs)
print len(mapped), mapped[0]

rawDescription: Apply a function to a list of dictionaries in python


code:
import glib

from pyudev import Context, Monitor

try:
    from pyudev.glib import MonitorObserver

    def device_event(observer, device):
        print 'event {0} on device {1}'.format(device.action, device)
except:
    from pyudev.glib import GUDevMonitorObserver as MonitorObserver

    def device_event(observer, action, device):
        print 'event {0} on device {1}'.format(action, device)

context = Context()
monitor = Monitor.from_netlink(context)

monitor.filter_by(subsystem='usb')
observer = MonitorObserver(monitor)

observer.connect('device-event', device_event)
monitor.start()

glib.MainLoop().run()

rawDescription: How can I listen for 'usb device inserted' events in Linux, in Python?

description: Listen for 'usb device inserted' events in Linux, in Python


code:
import numpy as np
matrix1 = np.zeros([10,10])
replacewith = 1
atposition = (5,5)
matrix1[atposition] = replacewith
print matrix1

rawDescription: Manipulating numpy matrices..replacing a value


code:
import os

folder = 'zeFolder'
brothers = set(['brotherOne', 'brotherTwo', folder])

for dirpath, dirnames, filenames in os.walk('/'):
    if set(dirnames).issuperset(brothers) :
        print 'matches on %s' % os.path.join(dirpath, 'zeFolder')

rawDescription: How can I get a specific folder with a specific folders sibling in python

description: Get a specific folder with a specific folders sibling in python


code:
def highlight_many(text, keywords):
    replacement = lambda match: "\033[91m" + match.group() + "\033[39m"
    text = re.sub("|".join(map(re.escape, keywords)), replacement, text, flags=re.I)
    print text

rawDescription: Highlight multiple words using Regex function


code:
import subprocess
import sys
p = subprocess.Popen(["python printandwait.py"], shell=True, stdout=subprocess.PIPE)
while True:
    print "Looping"
    line = p.stdout.readline()
    if not line:
        break
    print line.strip()
    sys.stdout.flush()

rawDescription: Getting progress message from a subprocess


code:
start_value = 10;
end_value = 20;
for i in xrange(start_value, end_value):
  print i

rawDescription: How to write a special for loop case of C++ in Python?

description: Write a special for loop case of C++ in Python


code:
elements = self.driver.find_elements_by_xpath("//td[contains(., 'Remarks')]/following-sibling::td")
print elements[1].text

rawDescription: Get text of td following the second occurrence of an element in Selenium using Python


code:
template_tuple = (0, 0, 'X', 0, 'Y', 'Z')
template_tuple_len = len(template_tuple)

for row in (
    '\t'.join(('1', '2', '3', '4', '5', '6')),
    '\t'.join(('1', '', '3', '4', '5', '6')),
    '\t'.join(('1', '2', '3', '4'))
    ):
    print [v if len(v) else template_tuple[i] for split_row in
           (row.strip().split('\t'),) for (i, v) in enumerate(
            split_row + [''] * (template_tuple_len - len(split_row)))]

rawDescription: Template Python list with default element values


code:
import argparse

#description of this script
__description__ = "This script parse a Database file in order to find the genes identifiers and provide them to a other file.[description to correct]\nTake the IDs from databaseFile and output the targetFile content enriched with IDs"

# -*- -*-  -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*-
#classes used to handle and operate on data
class ChromosomeIdentifierIndex():
    def __init__(self):
        self.chromosomes = {}

    def register(self, chromosomeNumber, positionOnChromosome, identifier):
        if not chromosomeNumber in self.chromosomes:
            self.chromosomes[chromosomeNumber] = {}

        self.chromosomes[chromosomeNumber][positionOnChromosome] = identifier

    def __setitem__(self, ref, ID):
        """ Allows to use alternative syntax to chrsIndex.register(number, position, id) : chrsIndex[number, position] = id """
        chromosomeNumber, positionOnChromosome = ref[0],ref[1]
        self.register(chromosomeNumber, positionOnChromosome, ID)

    def __getitem__(self, ref):
        """ Allows to get IDs using the syntax: chromosomeIDindex[chromosomenumber,positionOnChromosome] """
        chromosomeNumber, positionOnChromosome = ref[0],ref[1]
        try:
            return self.chromosomes[chromosomeNumber][positionOnChromosome]
        except:
            return "."

    def __repr__(self):
        for chrs in self.chromosomes.keys():
            print "Chromosome : ", chrs
            for position in self.chromosomes[chrs].keys():
                print "\t", position, "\t", self.chromosomes[chrs][position]

class Chromosome():
    def __init__(self, string):
        self.values   = string.split("\t")
        self.chrs     = self.values[0]
        self.position = self.values[1]
        self.ID       = self.values[2]

    def __str__(self):
        return "\t".join(self.values)

    def setID(self, ID):
        self.ID = ID
        self.values[2] = ID

class DefaultWritter():
    """ Use to print if no output is specified """
    def __init__(self):
        pass
    def write(self, string):
        print string
    def close(self):
        pass

# -*- -*-  -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*-
#The code executed when the scrip is called
if __name__ == "__main__":

    #initialisation
    parser = argparse.ArgumentParser(description = __description__)
    parser.add_argument("databaseFile"  , help="A batch file that contains many informations, including the IDs.")
    parser.add_argument("targetFile"    , help="A file that contains informations, but miss the IDs.")
    parser.add_argument("-o", "--output", help="The output file of the script. If no output is specified, the output will be printed on the screen.")
    parser.add_argument("-l", "--logs"  , help="The log file of the script. If no log file is specified, the logs will be printed on the screen.")
    args = parser.parse_args()

    output = None
    if args.output == None:
        output = DefaultWritter()
    else:
        output = open(args.output, 'w')

    logger = None
    if args.logs == None:
        logger = DefaultWritter()
    else:
        logger = open(args.logs, 'w')

    #start of the process

    idIndex = ChromosomeIdentifierIndex()

    #build index by reading the database file.
    with open(args.databaseFile, 'r') as database:
        for line in database:
            if not line.startswith("#"):
                chromosome = Chromosome(line)
                idIndex[chromosome.chrs, chromosome.position] = chromosome.ID

    #read the target, replace the ID and output the result
    with open(args.targetFile, 'r') as target:
        for line in target:
            if not line.startswith("#"):
                chromosome = Chromosome(line)
                chromosome.setID(idIndex[chromosome.chrs, chromosome.position])
                output.write(str(chromosome))
            else:
                output.write(line)

    output.close()
    logger.close()

rawDescription: Get data from a huge text file to replace data in another huge text file, efficiently (Python)


code:
for row in inputs:
    for k, v in row.iteritems():
        if not isinstance(v, basestring):
            try:
                row[k] = ', '.join(v)
            except TypeError:
                pass
    c.writerow(row)

print buf.getvalue()

rawDescription: Arranging one items per one column in a row of csv file in scrapy python


rawDescription: Matching substring in pattern in Python

description: Matching substring in pattern in Python


code:
print "We're going to write to a file you'll be prompted for"
targetfile = raw_input('Enter a filename: ')
targetfilefound = open(targetfile , 'w')
print "What do we write in this file?"
targetfilefound.write(raw_input())
targetfilefound.close()

rawDescription: Python raw_input for file writing

description: Python raw_input for file writing


code:
from collections import defaultdict
data_dict = defaultdict(list)
for data_row in data_list: # E.g., data_row = ['x1', 'y1', 'z1']
   data_dict[data_row[0]].append(data_row[1:])
print data_dict # defaultdict(<type 'list'>, {'x2': [['y3', 'z3']], 'x1': [['y1', 'z1'], ['y2', 'z2']]})

rawDescription: Python: Appending to a list in a dictionary


code:
def type_spec_iterable(obj, name):
    tps = set(type_spec(e) for e in obj)
    if len(tps) == 1:
        return name + "<" + next(iter(tps)) + ">"
    else:
        return name + "<?>"

def type_spec_dict(obj):
    tps = set((type_spec(k), type_spec(v)) for (k,v) in obj.iteritems())
    keytypes = set(k for (k, v) in tps)
    valtypes =  set(v for (k, v) in tps)
    kt = next(iter(keytypes)) if len(keytypes) == 1 else "?"
    vt = next(iter(valtypes)) if len(valtypes) == 1 else "?"
    return "dict<%s, %s>" % (kt, vt)

def type_spec_tuple(obj):
    return "tuple<" + ", ".join(type_spec(e) for e in obj) + ">"

def type_spec(obj):
    t = type(obj)
    res = {
        int: "int",
        str: "str",
        bool: "bool",
        float: "float",
        type(None): "(none)",
        list: lambda o: type_spec_iterable(o, 'list'),
        set: lambda o: type_spec_iterable(o, 'set'),
        dict: type_spec_dict,
        tuple: type_spec_tuple,
    }.get(t, lambda o: type(o).__name__)
    return res if type(res) is str else res(obj)

if __name__ == "__main__":
    class Foo(object):
        pass
    for obj in [
        1,
        2.3,
        None,
        False,
        "hello",
        [1, 2, 3],
        ["a", "b"],
        [1, "h"],
        (False, 1, "2"),
        set([1.2, 2.3, 3.4]),
        [[1,2,3],[4,5,6],[7,8,9]],
        [(1,'a'), (2, 'b')],
        {1:'b', 2:'c'},
        [Foo()], # todo - inheritance?
    ]:
        print repr(obj), ":", type_spec(obj)

rawDescription: How to determine type of nested data structures in Python?

description: Determine type of nested data structures in Python


code:
@app.task(base=NaiveAuthenticateServer)
def my_task(arg1, arg2):
    print arg1, arg2

rawDescription: User defined celery task class : init is getting called during import


code:
import networkx as nx
graph= nx.MultiGraph()

#add 4 nodes in the vertexs of a square. X and Y are the coordinates
graph.add_node(1,x=100,y=100)
graph.add_node(2,x=100,y=200)
graph.add_node(3,x=200,y=100)
graph.add_node(4,x=200,y=200)
graph.add_edge(1,2)
graph.add_edge(2,3)
graph.add_edge(3,4)
graph.add_edge(4,1)
                                                                                # assign positions
for n in graph:
    graph.node[n]['pos'] = '"%d,%d"'%(graph.node[n]['x'], graph.node[n]['y'])
p = nx.to_pydot(graph)
print p.to_string()
p.write('foo.dot')
# run neato -n2 -Tpng foo.dot >foo.png

rawDescription: Drawing nodes of multigraph in fixed position


code:
import pandas as pd
import numpy as np
import io

txt = u"""EventID  |  PictureID  |  Date
0        |  A          |  2009-07-01
1        |  A          |  2010-01-01
2        |  A          |  2010-02-01
3        |  A          |  2010-02-15
4        |  B          |  2010-01-01
5        |  C          |  2010-02-01
6        |  C          |  2010-02-15
7        |  A          |  2010-08-01
"""

df = pd.read_csv(io.StringIO(txt), sep=r"\s*\|\s*", parse_dates=["Date"])

def f(df):
    count = pd.Series(np.arange(1, len(df)+1), index=df["Date"])
    prev1day = count.index.shift(-1, freq="D")
    prev6month = count.index.shift(-183, freq="D")
    result = count.asof(prev1day).fillna(0).values - count.asof(prev6month).fillna(0).values
    return pd.Series(result, df.index)

df["PastSix"] = df.groupby("PictureID").apply(f)
print df

rawDescription: Pandas dataframe: add column that counts like-events in past


rawDescription: python: Help to implement an algorithm to find the minimum-area-rectangle for given points in order to compute the major and minor axis length

description: Python: Help to implement an algorithm to find the minimum-area-rectangle for given points in order to compute the major and minor axis length


code:
def all_paths_dfs(graph, start, end, path=None):
    if path is None:
        path = []

    path.append(start)
    all_paths = set()

    if start == end:
        all_paths.add(tuple(path))
    else:
        for neighbor in graph[start]:
            if neighbor not in path:
                all_paths |= all_paths_dfs(graph, neighbor, end, path)

    path.pop()
    return all_paths

if __name__ == "__main__":
    graph = {'A': {'B', 'C'},
             'B': {'A', 'D', 'E'},
             'C': {'A', 'F'},
             'D': {'B'},
             'E': {'B', 'F'},
             'F': {'C', 'E'},
             'G': {'K'}}

    print all_paths_dfs(graph, 'A', 'F')

rawDescription: How to turn a iterative DFS into a recursive DFS?

description: Turn a iterative DFS into a recursive DFS


code:
imgfile = open("%s.png" % imageid, "rb")
print imgfile.read()

rawDescription: Problems outputting an image over http using Python


code: print randomVar

rawDescription: Random sampling one element without making it a list in Python


code:
a = "Kitty Bates"
sev=[ ord(x) for x in a ]
S= sum(sev)

while(1):
    try:
        if(S>126):
            0/0
        print chr(S)
        break  #will break if ASCII is below 126

    except:
        S= S-44

rawDescription: Subtracting depending on an if function in python


code:
def student_data(name, age, student_number, enrolled):
 print "name:{0}, age:{1}, student_number:{2}, enrolled :{3}".fromat(name, age, student_number, enrolled)

rawDescription: Concatenating objects of different types into a string (Python)


code:
print test.__init__.func_defaults
name_dict[name] = test()
# ...

rawDescription: Python list should be empty on class instance initialisation, but it's not. Why?


code:
import scipy.sparse as sp
import numpy as np

def random_walk_transition_probability(n, left = 0.3, centre = 0.5, right = 0.2):
    m = 2*n+1
    A  = sp.csr_matrix((m, m))
    A += sp.diags(centre*np.ones(m), 0)
    A += sp.diags(left*np.ones(m-1), -1)
    A += sp.diags(right*np.ones(m-1),  1)
    x = np.zeros((m,1))
    x[n] = 1.0
    for i in xrange(n):
        x = A.dot(x)
    return x

print random_walk_transition_probability(4)

rawDescription: python library for evaluating random walks?

description: Python library for evaluating random walks


code:
def traverse(o, tree_types=(list, tuple)):
    if isinstance(o, tree_types):
        for value in o:
            for subvalue in traverse(value):
                yield subvalue
    else:
        yield o

data = [(1,1,(1,1,(1,"1"))),(1,1,1),(1,),1,(1,(1,("1",)))]
print list(traverse(data))
# prints [1, 1, 1, 1, 1, '1', 1, 1, 1, 1, 1, 1, 1, '1']

for value in traverse(data):
    print repr(value)
# prints
# 1
# 1
# 1
# 1
# 1
# '1'
# 1
# 1
# 1
# 1
# 1
# 1
# 1
# '1'

rawDescription: Traversing a "list" tree and get the type(item) list with same structure in python?


code:
import os
dir = '.'
allfiles = os.listdir(dir)
files = [ fname for fname in allfiles if fname.endswith('.mp4')]
print files

rawDescription: listdir a directory and find if a file name that endswith ".mp4""

description: Listdir a directory and find if a file name that endswith ".mp4""


code:
from __future__ import division
import sys
from operator import itemgetter
from collections import defaultdict

def main():

    line1 = sys.stdin.readline().split(" ")
    numberofselect = int(line1[1])

    qualitydict = {}
    songdict = defaultdict(list)

    for x, line in enumerate(sys.stdin, start=1):
        tokens = line.split()
        val = float(tokens[0]) * x
        qualitydict[tokens[1]] = val
        songdict[val].append(tokens[1])

    items = songdict.items()
    items.sort(key=itemgetter(0), reverse=True)
    a = 0
    for key, value in items:
            for element in value:
                if a < numberofselect:
                    print element
                    a += 1

main()

rawDescription: optimizing python code for performance

description: Optimizing python code for performance


code:
for i in sorted(d, key=str.lower):
    print i, d[i]

rawDescription: How to iterate over case insensitive sorted dictionary items?

description: Iterate over case insensitive sorted dictionary items


code:
class A(object):
    def __init__(self):
        self.val = 100

    def __str__(self):
        """What a looks like if your print it"""
        return 'A:'+str(self.val)

import pickle
a = A()
a_pickled = pickle.dumps(a)
a.val = 200
a2 = pickle.loads(a_pickled)
print 'the original a'
print a
print # newline
print 'a2 - a clone of a before we changed the value'
print a2
print

print 'Why are you trying to use __setstate__, not __init__?'
print

rawDescription: how to use 'pickle'

description: Use 'pickle'


code:
from collections import Counter

import csv
import itertools

with open('path/to/file') as infile:
    answer = Counter(itertools.chain.from_iterable(csv.reader(infile, delimiter="|")))
for k in sorted(answer, key=lambda k:answer[k]):
    print answer[k], "times", k

rawDescription: Parsing specific fields and counting the occurrence with python


rawDescription: picking last 3 digits from long decimal values

description: Picking last 3 digits from long decimal values


code:
class it(type):
    def __iter__(self):
        # Wanna iterate over a class? Then ask that class for iterator.
        return self.classiter()

class Foo:
    __metaclass__ = it # We need that meta class...
    by_id = {} # Store the stuff here...

    def __init__(self, id): # new isntance of class
        self.id = id # do we need that?
        self.by_id[id] = self # register istance

    @classmethod
    def classiter(cls): # iterate over class by giving all instances which have been instantiated
        return iter(cls.by_id.values())

if __name__ == '__main__':
    a = Foo(123)
    print list(Foo)
    del a
    print list(Foo)

rawDescription: Can I iterate over a class in Python?

description: Iterate over a class in Python


code:
ll =[['AAA','MG_00013'],
    ['AAA','MG_00177'],
    ['AAA','MG_00005'],
    ['BBB','MG_0045'],
    ['BBB','MG_00080'],
    ['CCC','MG_0002']]

d = {}
for l in ll:
    # If key is not the dict, insert the entry into dict
    if l[0] not in d:
        d[l[0]] = l[1]
    # If key is already in the dict, update the entry if value is smaller
    elif int(l[1][3:]) < int(d[l[0]][3:]):
        d[l[0]] = l[1]

print d

rawDescription: python select the lowest alphanumeric value with a reference from multiple lists

description: Python select the lowest alphanumeric value with a reference from multiple lists


code:
import time

def main():
    data_dict = {'test': 0}
    prev_time = time.time()
    while True:
        time.sleep(1)
        data_dict['test'] += 1

        if time.time() - prev_time >= 10:
            print data_dict
            prev_time = time.time()

if __name__ == '__main__':
    main()

rawDescription: Running a timer in python forever

description: Running a timer in python forever


code:
mystring = "position:absolute;top:188.97638px;left:642.519692px;font-size:10.0px;font-family:Ubuntu Mono;width:25%;float:left"
result = [x + ";" for x in mystring.split(";")]
print '\n'.join(result)

rawDescription: How to split the string in python

description: Split the string in python


code:
from itertools import combinations
for item in combinations(list_1, 2):
    if any(i in item[1] for i in item[0]):
        print item

rawDescription: Efficient way to find similar items in a list in Python


code:
def Serial_Checker(values,serial,field_length):
    serial_length = len(serial)
    cycler = field_length

    serial_valid = True

    while cycler < serial_length:
        if serial[cycler-field_length:cycler] in VALUES:
            cycler += field_length
        else:
            serial_valid = False
            break

    if serial_length%field_length <> 0: # % is the remainder operator
        serial_valid = False #In case there is leftover at the end of the serial

    return serial_valid

VALUES = ['01', '02', '03', '04', '05']
serial = "a;alskdjfadslkj"

Serial_Checker(VALUES,serial,2)

rawDescription: Handling unexpected values in Python

description: Handling unexpected values in Python


code:
import base64
def output(text):
    print "-".join([str(ord(x)) for x in base64.b64decode(text)])

from SimpleXMLRPCServer import SimpleXMLRPCServer
server = SimpleXMLRPCServer(('localhost', 1234))
server.register_function(output)
server.serve_forever()

rawDescription: How can I transfer binary data over xmlrpc (python)?

description: Transfer binary data over xmlrpc (python)


code:
import re

import timeit

# Method #1 (string ops) -> Green Cell's
def stringOps():
    s="a@whammy = c"
    replaceChar = s.split('@')[1].split(' ')[0]
    s.split('=')[0].replace(replaceChar, "'{0}'".format(replaceChar) ).replace(' ', '')
time1 = timeit.timeit('from __main__ import stringOps;stringOps()')
# Method #2 (regex)  -> Dawg's
def regex():
    s="a@bam = c"
    re.sub(r'(\w+)(\s*=\s*\w+$)', r"'\1'", s)

time2 = timeit.timeit('from __main__ import regex;regex()')

#%method 3 split_n_dice  -> my own
def slice_dice():
    txt="a@whammy = c"
    "%s@'%s'"%tuple(txt.split(' =')[0].split('@'))

time3 = timeit.timeit('from __main__ import slice_dice;slice_dice()')

print 'Method #1 took {0}'.format(time1)
print 'Method #2 took {0}'.format(time2)
print 'Method #3 took {0}'.format(time3)

rawDescription: What is the fastest way to do this string patten substitution in python?


code:
dreader = [{'id':1, 'name':'Bob', 'other_stuff':'xy'},
           {'id':2, 'name':'Jen', 'other_stuff':'xx'}]

for d in dict_filter(dreader, 'id', 'name'):
    print d

rawDescription: Python CSV DictReader ignore columns?

description: Python CSV DictReader ignore columns


code:
@command(help="Run the command.")
@option('--jar-file', cls=MutuallyExclusiveOption,
        help="The jar file the topology lives in.",
        mutually_exclusive=["other_arg"])
@option('--other-arg',
        cls=MutuallyExclusiveOption,
        help="The jar file the topology lives in.",
        mutually_exclusive=["jar_file"])
def cli(jar_file, other_arg):
    print "Running cli."
    print "jar-file: {}".format(jar_file)
    print "other-arg: {}".format(other_arg)

if __name__ == '__main__':
    cli()

rawDescription: Mutually exclusive option groups in python Click


code:
import hashlib
hash_to_crack = "5badcaf789d3d1d09794d8f021f40f0e"
dict_file = "dict.txt"

def main():
    with open(dict_file) as fileobj:
        for line in fileobj:
            line = line.strip()
            if hashlib.md5(line).hexdigest() == hash_to_crack:
                print "Successfully cracked the hash %s: It's %s" % (hash_to_crack, line)
                return ""
    print "Failed to crack the file."

if __name__ == "__main__":
    main()

rawDescription: Python MD5 Hash Passwords and Dictionary


code:
class DarkServerProtocol(Protocol):
    """
    Shim protocol for servers.
    """

    peer = None
    buf = ""

    def __init__(self, endpoint):
        self.endpoint = endpoint
        print "Protocol created..."

    def challenge(self, challenge):
        log.msg("Challenged: %s" % challenge)
        # ...omitted for brevity...
        return is_valid(challenge)

    def connectionMade(self):
        pcf = ProxyClientFactory()
        pcf.setServer(self)
        d = self.endpoint.connect(pcf)
        d.addErrback(lambda failure: self.transport.loseConnection())

        self.transport.pauseProducing()

    def setPeer(self, peer):
        # Our proxy passthrough has succeeded, so we will be seeing data
        # coming through shortly.
        log.msg("Established passthrough")
        self.peer = peer

    def dataReceived(self, data):
        self.buf += data

        # Examine whether we have received a challenge.
        if self.challenge(self.buf):
            # Excellent; change protocol.
            p = DarkAMP()
            p.factory = self.factory
            self.transport.protocol = p
            p.makeConnection(self.transport)
        elif self.peer:
            # Well, go ahead and send it through.
            self.peer.transport.write(data)

rawDescription: How can I create a non-http proxy with Twisted

description: Create a non-http proxy with Twisted


code:
import StringIO
import csv

# the text between double quotes will be treated
# as a single element and not parsed by commas
line = '1,2,3,"1,2,3",4'

csv_file = StringIO.StringIO(line)
csv_reader = csv.reader(csv_file)
for data in csv_reader:
    # output: ['1', '2', '3', '1,2,3', '4']
    print data

rawDescription: How to parse a string using a CSV parser in Python?

description: Parse a string using a CSV parser in Python


code:
with open(filename) as f:
    for line in f:
        if not 'e' in line: print line.strip()

rawDescription: has_no_e() exercise from Think Python

description: Has_no_e() exercise from Think Python


code:
xml_string="""
    <xml>
        <codeblock>
            <syntax>configure</syntax>
            <syntax>      context
            local</syntax>
            <syntax>           interface <variable>interface_name</variable></syntax>
            <syntax>                ip
            address <variable>ip_address</variable> <variable>subnet_mask</variable></syntax>
            <syntax>                exit</syntax>
            <syntax>           server
            ftpd</syntax>
            <syntax>           ssh
            key <variable>data</variable>
            length <variable>octets</variable></syntax>
            <syntax>           ssh
            key <variable>data</variable>
            length <variable>octets</variable></syntax>
            <syntax>           ssh
            key <variable>data</variable>
            length <variable>octets</variable></syntax>
        </codeblock>
    </xml>
"""

from bs4 import BeautifulSoup
soup = BeautifulSoup(xml_string,"xml")
for syntax in soup.find_all('syntax'):
    new_tag = soup.new_tag('syntax')
    for child in syntax.contents:
        if "\n" in child:
            new_tag.append(child.replace("\n", " "))
        else:
            new_tag.append(child)
    syntax.replace_with(new_tag)

print soup

rawDescription: Remove CRLF from a string in an XML tag using BeautifulSoup


code:
#!/usr/bin/env python
import re

with open("person.html", "r") as html_file:
    data=html_file.read()
list_of_strings = re.findall(r'\$[A-Za-z]+[A-Za-z0-9]*', data)
print list_of_strings

rawDescription: How to parse HTML using ElementTree to find a particular RegEx?

description: Parse HTML using ElementTree to find a particular RegEx


code:
vowels = consonants = total = 0
mystr = "abcdefghi"
for char in mystr:
    if char in "aeiou":
        vowels += 1
    elif char in "bcdfghjklmnpqrstvwxyz":
        consonants += 1
    total += 1
print "Vowels: {}\nConsonants: {}\nTotal: {}".format(vowels, consonants, total)

rawDescription: Multiple-Target Assignments

description: Multiple-Target Assignments


code:
d = {'ID1':[('Experiment1', 40), ('Experiment2', 59), ('Experiment3', 65)],
 'ID2':[('Experiment1', 68), ('Experiment2', 21), ('Experiment3', 39)],
 'ID3':[('Experiment1', 57), ('Experiment2', 15), ('Experiment4', 99)]}

COLUMN_WIDTH = 20
STRING_WHEN_MISSING = '""'
PADDING_STRING = "."

#first determine what are the columns - from the first line
columns = ["id"] #there is always at least the id column
columns = columns + [colName for (colName, colValue) in d.items()[0][1]]

print "".join([ col.ljust(COLUMN_WIDTH) for col in columns])

#browse the lines, order by ID Ascending
for (rowId, rowValues) in sorted(d.items(), key= lambda x: x[0].lower()):
    #print rowId
    #print rowValues
    rowValuesDict = dict(rowValues) #make it a dict with access by key
    rowValuesDict["id"] = rowId
    #print rowValuesDict
    print "".join([ str(rowValuesDict.get(col, STRING_WHEN_MISSING)).ljust(COLUMN_WIDTH, PADDING_STRING) for col in columns])

rawDescription: 2D table format Python

description: 2D table format Python


code:
import numpy

zeros = numpy.zeros(shape=(1,11)) #making 2D array with zeros

var = None

for i in range(1, 3):
    i1 = open("in" + str(i) + ".txt")

    a = numpy.loadtxt(i1, dtype='S')

    if var is None:
        var = numpy.vstack((a, zeros))
    else:
        var = numpy.vstack((var, a, zeros))

print var

rawDescription: Concatanating array by While on Python

description: Concatanating array by While on Python


code:
from functools import partial
from multiprocessing.dummy import Pool, Queue, Process # use threads
from subprocess import Popen, PIPE

def run(i, output):
    p = Popen(["cmd", str(i)], stdout=PIPE, bufsize=1)
    for line in iter(p.stdout.readline, b''):
        output((p.pid, line)) # collect the output
    p.stdout.close()
    return p.wait()

def print_output(q):
    for pid, line in iter(q.get, None):
        print pid, line.rstrip()

q = Queue()
Process(target=print_output, args=[q]).start() # start printing thread
return_codes = Pool(4).map(partial(run, output=q.put_nowait),
                           range(10)) # run 10 subprocesses, 4 at a time
q.put(None) # exit printing thread

rawDescription: Print multithread subprocess

description: Print multithread subprocess


code:
f=FamilyTree('Family1', ['B', 'C', 'D'])
print(f.child1, f.child2, f.child3)
i = 0
while i < f.child_nbr:
    print getattr(f, 'child{0}'.format(i+1))
f.add_child('E')
print(f.child4)
f.add_child('F', 'G')
print(f.child5, f.child6)

rawDescription: Dynamically adding attributes to __init__ method in python


code:
for line in data:
    print line[:33].strip()

rawDescription: read a table according to a certain number of characters

description: Read a table according to a certain number of characters


code:
tp = pd.read_csv('Check1_900.csv', sep='\t', iterator=True, chunksize=1000)
print tp
#<pandas.io.parsers.TextFileReader object at 0x00000000150E0048>
df = pd.concat(tp, ignore_index=True)

rawDescription: python - Using pandas structures with large csv(iterate and chunksize)

description: Python - Using pandas structures with large csv(iterate and chunksize)


code:
with open('C:/Users/Public/Documents/Scripts/test.csv', 'rU') as data:
    reader = csv.reader(data)
    # using list comprehension
    result = [(row[0], float(row[1])) for row in reader]

print result

rawDescription: Converting some CSV elements to float Python


code: print 'break'.join('\033[{0}mcolour\33[0m'.format(i) for i in range(30, 38))

rawDescription: How can I highlight regex matches in Python?

description: Highlight regex matches in Python


code:
db.query('SELECT EXISTS(SELECT 1 FROM episodes WHERE changed > $date) has_changed_after_date', vars={'date': date})
# ...
print result[0]['has_changed_after_date']
# or
print result[0].has_changed_after_date

rawDescription: web.py storage object can't access value

description: Web.py storage object can't access value


code:
import lxml.objectify
doc = """<?xml version='1.0' encoding='utf-8'?><soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:body><createsessionresponse soapenv:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/"><createsessionreturn xsi:type="xsd:string">59C3F170141E9CF6F5BF98FB39B0237B</createsessionreturn></createsessionresponse></soapenv:body></soapenv:envelope>"""
obj = lxml.objectify.fromstring(doc)
obj
for e in obj.iter():
    print repr(e)

rawDescription: How can i access element value in SOAP response with lxml objectify

description: Access element value in SOAP response with lxml objectify


code:
print [[' '.join(x) for x in sample[:-1]]
                        for sample in my_load_files(text_folder, 'File_*')]

rawDescription: How to load_files and process a .txt file with scikit-learn?

description: Load_files and process a .txt file with scikit-learn


code:
def convertTime(input):
    h, m = int(input[0:2]), int(input[2:4])

    postfix = 'am'

    if h > 12:
        postfix = 'pm'
        h -= 12

    print '{}:{:02d}{}'.format(h or 12, m, postfix)

convertTime('0000')
convertTime('1337')
convertTime('0429')
convertTime('2359')
convertTime('1111')

rawDescription: 24-Hour Time Conversion to 12-Hour Clock (ProblemSetQuestion) on Python


code:
import re
text = 'Found to A from:\n\t-B\n\t-C\n\t-G\nFound to K from:\n\t-B\n\t-D\n\t-E\nFound to A from:\n\t-D\nMax time: 20s'
for name in ['A', 'K']:
    print name
    groups = re.findall('(?:Found to ' + name + ' from:)((?:\\n\\s*-(?:[A-Z]))+)', text)
    print reduce(lambda i,j: i + j, map(lambda x: re.findall('\n\s*-([A-Z])', x), groups))

rawDescription: Find repeated pattern in text with "Re" Python


code:
te = clock()
for line in liner('fofo.txt',':;:', 65536):
    pass
print clock()-te

fh = open('fofo.txt', 'rb')
zenBreaker = SpecialDelimiters(fh, ':;:', 65536)

te = clock()
for line in zenBreaker:
    pass
print clock()-te

rawDescription: Special End-line characters/string from lines read from text file, using Python


code:
def FASTA(filename):
  try:
    f = file(filename)
  except IOError:
    print "The file, %s, does not exist" % filename
    return

  order = []
  sequences = {}
  counts = {}

  for line in f:
    if line.startswith('>'):
      name = line[1:].rstrip('\n')
      name = name.replace('_', ' ')
      order.append(name)
      sequences[name] = ''
    else:
      sequences[name] += line.rstrip('\n').rstrip('*')
      for aa in sequences[name]:
        if aa in counts:
            counts[aa] = counts[aa] + 1
        else:
            counts[aa] = 1

  print "%d sequences found" % len(order)
  print counts
  return order, sequences

x, y = FASTA("drosoph_b.fasta")

rawDescription: How to count amino acids in fasta formated file?

description: Count amino acids in fasta formated file


code:
lst=[
{'field_id': u'36908'},{'field_name': u'Code'},{'field_value': u'900321'},
{'field_id': u'36909'},{'field_name': u'Description'}, {'field_value': u'TIG 2.4MM TUNGSTEN (EACH ROD)'},
{'field_id': u'36910'}, {'field_name': u'Quantity'}, {'field_value': u'2'},
{'field_id': u'36911'}, {'field_name': u'Price'}, {'field_value': u'21.00'},
{'field_id': u'36912'}, {'field_name': u'Line Total'}, {'field_value': u'42.00'},
{'field_id': u'36908'}, {'field_name': u'Code'}, {'field_value': u'92.01.15.08'},
{'field_id': u'36909'}, {'field_name': u'Description'}, {'field_value': u'BINZEL .8MM MIG TIPS MB15'},
{'field_id': u'36910'}, {'field_name': u'Quantity'}, {'field_value': u'6'},
{'field_id': u'36911'}, {'field_name': u'Price'}, {'field_value': u'2.60'},
{'field_id': u'36912'}, {'field_name': u'Line Total'}, {'field_value': u'15.60'}]

new_lst=[] # List to save output
dic={} # Temporary dictionary to create output dictionary
count=0 # Count variable to count the list element
def iterating_list(lst): # Function to iterate over list
    for value in lst:
        yield value
iterating=iterating_list(lst)

for value in iterating :
    if value.get('field_name'): # If `field_name` matches in the given lists
    #By default get method return `None` when there is no given key
        dic.update({value.get('field_name'):next(iterating).get('field_value')})
        count+=1
    if count==5: # Resetting when count reaches to 5
        count=0
        new_lst.append(dic)
        dic={}
print new_lst

rawDescription: Merge every Every 6 dictionary into single dictionary of List