code:
import uno
from os.path import abspath, isfile, splitext
from com.sun.star.beans import PropertyValue
from com.sun.star.task import ErrorCodeIOException
from com.sun.star.connection import NoConnectException

FAMILY_TEXT = "Text"
FAMILY_SPREADSHEET = "Spreadsheet"
FAMILY_PRESENTATION = "Presentation"
FAMILY_DRAWING = "Drawing"
DEFAULT_OPENOFFICE_PORT = 8100

FILTER_MAP = {
    "pdf": {
        FAMILY_TEXT: "writer_pdf_Export",
        FAMILY_SPREADSHEET: "calc_pdf_Export",
        FAMILY_PRESENTATION: "impress_pdf_Export",
        FAMILY_DRAWING: "draw_pdf_Export"
    },
    "html": {
        FAMILY_TEXT: "HTML (StarWriter)",
        FAMILY_SPREADSHEET: "HTML (StarCalc)",
        FAMILY_PRESENTATION: "impress_html_Export"
    },
    "odt": { FAMILY_TEXT: "writer8" },
    "doc": { FAMILY_TEXT: "MS Word 97" },
    "rtf": { FAMILY_TEXT: "Rich Text Format" },
    "txt": { FAMILY_TEXT: "Text" },
    "docx": { FAMILY_TEXT: "MS Word 2007 XML" },
    "ods": { FAMILY_SPREADSHEET: "calc8" },
    "xls": { FAMILY_SPREADSHEET: "MS Excel 97" },
    "odp": { FAMILY_PRESENTATION: "impress8" },
    "ppt": { FAMILY_PRESENTATION: "MS PowerPoint 97" },
    "swf": { FAMILY_PRESENTATION: "impress_flash_Export" }
}

class DocumentConverter:

    def __init__(self, port=DEFAULT_OPENOFFICE_PORT):
        localContext = uno.getComponentContext()
        resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)
        try:
            self.context = resolver.resolve("uno:socket,host=localhost,port=%s;urp;StarOffice.ComponentContext" % port)
        except NoConnectException:
            raise Exception, "failed to connect to OpenOffice.org on port %s" % port
        self.desktop = self.context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", self.context)

    def convert(self, inputFile, outputFile):

        inputUrl = self._toFileUrl(inputFile)
        outputUrl = self._toFileUrl(outputFile)

        document = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0, self._toProperties(Hidden=True))
        #document.setPropertyValue("DocumentTitle", "saf" ) TODO: Check how this can be set and set doc update mode to  FULL_UPDATE

        if self._detectFamily(document) == FAMILY_TEXT:
            indexes = document.getDocumentIndexes()
            for i in range(0, indexes.getCount()):
                index = indexes.getByIndex(i)
                index.update()

            try:
                document.refresh()
            except AttributeError:
                pass

            indexes = document.getDocumentIndexes()
            for i in range(0, indexes.getCount()):
                index = indexes.getByIndex(i)
                index.update()

        outputExt = self._getFileExt(outputFile)
        filterName = self._filterName(document, outputExt)

        try:
            document.storeToURL(outputUrl, self._toProperties(FilterName=filterName))
        finally:
            document.close(True)

    def _filterName(self, document, outputExt):
        family = self._detectFamily(document)
        try:
            filterByFamily = FILTER_MAP[outputExt]
        except KeyError:
            raise Exception, "unknown output format: '%s'" % outputExt
        try:
            return filterByFamily[family]
        except KeyError:
            raise Exception, "unsupported conversion: from '%s' to '%s'" % (family, outputExt)

    def _detectFamily(self, document):
        if document.supportsService("com.sun.star.text.GenericTextDocument"):
            # NOTE: a GenericTextDocument is either a TextDocument, a WebDocument, or a GlobalDocument
            # but this further distinction doesn't seem to matter for conversions
            return FAMILY_TEXT
        if document.supportsService("com.sun.star.sheet.SpreadsheetDocument"):
            return FAMILY_SPREADSHEET
        if document.supportsService("com.sun.star.presentation.PresentationDocument"):
            return FAMILY_PRESENTATION
        if document.supportsService("com.sun.star.drawing.DrawingDocument"):
            return FAMILY_DRAWING
        raise Exception, "unknown document family: %s" % document

    def _getFileExt(self, path):
        ext = splitext(path)[1]
        if ext is not None:
            return ext[1:].lower()

    def _toFileUrl(self, path):
        return uno.systemPathToFileUrl(abspath(path))

    def _toProperties(self, **args):
        props = []
        for key in args:
            prop = PropertyValue()
            prop.Name = key
            prop.Value = args[key]
            props.append(prop)
        return tuple(props)

if __name__ == "__main__":
    from sys import argv, exit

    if len(argv) < 3:
        print "USAGE: python %s <input-file> <output-file>" % argv[0]
        exit(255)
    if not isfile(argv[1]):
        print "no such input file: %s" % argv[1]
        exit(1)

    try:
        converter = DocumentConverter()
        converter.convert(argv[1], argv[2])
    except Exception, exception:
        print "ERROR!" + str(exception)
        exit(1)

rawDescription: Word XML to RTF conversion

description: Word XML to RTF conversion


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup('''\
<select>
  <option value="0">2002/12</option>
  <option value="1">2003/12</option>
  <option value="2">2004/12</option>
  <option value="3">2005/12</option>
  <option value="4">2006/12</option>
  <option value="5" selected>2007/12</option>
</select>
''')
for option in soup.find_all('option'):
    print 'value: {}, text: {}'.format(option['value'], option.text)

rawDescription: python BeautifulSoup get select.value not text

description: Python BeautifulSoup get select.value not text


code:
from all_imports import *

print dir()
print math.pi

rawDescription: Writing list of import modules in a separate .py file


code:
import sys, json

# Load the data that PHP sent us
try:
    data = json.loads(sys.argv[1])
except:
    print "ERROR"
    sys.exit(1)

# Generate some data to send to PHP
result = {'status': 'Yes!'}

# Send it to stdout (to PHP)
print json.dumps(result)

rawDescription: executing Python script in PHP and exchanging data between the two

description: Executing Python script in PHP and exchanging data between the two


code:
from xml.etree import ElementTree as ET

data = """<?xml version="1.0"?>
<data>
<product>
    <Product_Code>code1</Product_Code>
    <Size>x</Size>
    <Quantity>1</Quantity>
</product>
<product>
    <Product_Code>code3</Product_Code>
    <Size>c</Size>
    <Quantity>5</Quantity>
</product>
<product>
    <Product_Code>code2</Product_Code>
    <Size>z</Size>
    <Quantity>2</Quantity>
</product>
<product>
    <Product_Code>code3</Product_Code>
    <Size>a</Size>
    <Quantity>1</Quantity>
</product>
<product>
    <Product_Code>code1</Product_Code>
    <Size>y</Size>
    <Quantity>1</Quantity>
</product>
<product>
    <Product_Code>code3</Product_Code>
    <Size>b</Size>
    <Quantity>5</Quantity>
</product>
</data>"""

tree = ET.fromstring(data)

codes = {}
for product in tree.findall('.//product'):
    code = product.find('Product_Code').text
    size = product.find('Size').text
    quantity = product.find('Quantity').text

    codes[code] = codes[code] if code in codes else {}
    codes[code][size] = quantity

print codes

rawDescription: Element Tree: How to find all child elements with a specific value


code:
#file a.py

import inspect
def my_func():
    dad_name = inspect.stack()[1][1]
    if inspect.getmodulename(dad_name) == 'b':   #or whatever check on the filename
         print 'You are module b!'
    elif inspect.getmodulename(dad_name) == 'c':
         print 'You are module c!'
    else:
         print 'You are not b nor c!'

#file b.py
import a

a.my_func()

#file c.py

import a
a.my_func()

#file d.py
import a
a.my_func()

rawDescription: How to know from inside a module, during run time, which script has called a function in it

description: Know from inside a module, during run time, which script has called a function in it


code:
def is_close(user,company,radius, debug=0):
    from math import cos, pi, sqrt
    EarthRadius   = 6.371E6 # metre, = 6371 km
    deg2rad = pi/180.0
    radius_x = EarthRadius*cos(company[0]*deg2rad)
    radius_y = EarthRadius
    dx, dy = ((user[1]-company[1])*deg2rad*radius_x,
              (user[0]-company[0])*deg2rad*radius_y)
    if debug:print dx, dy, sqrt(dx*dx+dy*dy)
    return True if (dx*dx+dy*dy) <= (radius*radius) else False

user, company = (48.860384,2.338520), (48.859282,2.343369)
is_close(user,company,500, 1)
is_close(user,company,500)

rawDescription: Is the User's lat/lng inside the Company's lat/lng boundary


code:
import random

SEP = chr(0x29)

with open('file.dat', 'wb') as outfile:
    outfile.write("<doc></doc>")
    outfile.write(SEP)
    data = ''.join(chr(random.randint(0, 255)) for i in range(1024))
    outfile.write(data)

with open('file.dat', 'rb') as infile:
    data = infile.read()

xml, sep, binary_data = data.partition(SEP)

print xml
print len(binary_data)

rawDescription: Splitting binary file content in two parts using single byte separator in python


code: print reduce(multiply, matrices)

rawDescription: deleting and inserting values in matrix multiplication python language

description: Deleting and inserting values in matrix multiplication python language


code:
location = next((part for part in hostname.split('.') if part in colo), None)
if location is None:
    print 'Not a recognized location'

rawDescription: How to compare list with actual string in Python?

description: Compare list with actual string in Python


code:
import pandas as pd

mylist = [('t1', 0, 23),
          ('t1', 1, 24),
          ('t1', 2, 25),
          ('t2', 0, 22),
          ('t2', 1, 25),
          ('t2', 2, 26)]

df = pd.DataFrame()

for col, row, val in mylist:
    if col not in df.columns:
        #df[col] = pd.Series() # new column with NaN
        df[col] = 0 # new column with zeros

    if row not in df.index:
        #df.loc[row] = pd.Series() # new row with NaN
        df.loc[row] = 0 # new row with zeros

    df[col][row] = val

print df
#   t1  t2
#0  23  22
#1  24  25
#2  25  26

df.to_csv('filename.csv', sep=' ')

rawDescription: Re-arrange List of Tuple in Python

description: Re-arrange List of Tuple in Python


code:
from imdb import IMDb
ia = IMDb()

movie = ia.get_movie('0816692')
print "Name of the movie: ", movie
for i in movie['director']:
    print "Director: ", i
    director = ia.search_person(i["name"])[0]
    ia.update(director)
    print "Movies directed by %s:" % director
    for movie_name in director["director movie"]:
        print movie_name

rawDescription: Get list of movies directed by director- IMDbPY


code:
import xml.dom.minidom as minidom
doc = minidom.parse(filename)

memoryElem = doc.getElementsByTagName('memory')[0]
print ''.join( [node.data for node in memoryElem.childNodes] )
print memoryElem.getAttribute('unit')

rawDescription: Reading XML file and fetching its attributes value in Python


code:
class FooBar(Foo):
    __slots__ = ('z')
    def __init__(self, a, z):
        self.z = z
        super(FooBar, self).__init__(a)

FooBar(1, 1) == FooBar(2, 1)
# True

print FooBar(1, 1).__slots__
# 'z'

rawDescription: Equality of Python classes using slots

description: Equality of Python classes using slots


rawDescription: Adding the resulting TFIDF calculation to the dataframe of the original documents in Pyspark


code:
import json
import subprocess

f = subprocess.Popen(["cat", "/tmp/foo.json"], stdout=subprocess.PIPE)
j = f.stdout.read()
o = json.loads(j.decode("UTF-8"))
print o
print o["def1"]["val1"]
print o["def2"]["9"]["val1"]

rawDescription: Distinguishing 2 lines in a file read

description: Distinguishing 2 lines in a file read


code:
import os
import time

sc_clk_tck = os.sysconf_names['SC_CLK_TCK']
HZ = os.sysconf(sc_clk_tck)

with open('/proc/stat') as f:
    for ln in f.readlines():
        if ln.startswith('btime'):
            btime = ln.split()[1]

packet_time = (packet_jiffy / HZ) + btime
print time.ctime(packet_time)
print "Seconds ago %d" % (time.time() - packet_time)

rawDescription: Convert "jiffy" time

description: Convert "jiffy" time


code:
class YourClass(object):

    def __init__(self):
        self.rand1 = random.randint(0, 10)
        self.rand2 = random.randint(0, 10)

    def your_func_as_a_method(self, x, y):
        result = self.rand1*x + self.rand2*y
        print result
        return result

rawDescription: Getting a particular value from a function


code:
def chunks(l, n):
    #Generator to yield n sized chunks from l
    for i in xrange(0, len(l), n):
        yield l[i: i + n]

with open('p_match.txt') as f:
    for chunk in chunks(f.readlines(), 6):
        sequence_id = chunk[0].split()[-1].strip()
        for i in (2,3):
            start = int(chunk[i].split()[0].strip())
            sequence = chunk[i].split()[-2].strip()
            stop = start + len(sequence)
            print sequence_id, start, stop

rawDescription: How to extract coordinates in P-match result?

description: Extract coordinates in P-match result


code:
def f_arg1(x) :
    return x
def f_arg2(x, y) :
    return (x, y)

class A(object) :
    def __init__(self) :
        d = {"arg1" : ((1,), f_arg1),
             "arg2" : ((2,3), f_arg2)}
        for key, value in d.items() :
            a, f = value
            setattr(self, key, f(*a))

a = A()
print a.arg1
print a.arg2

rawDescription: Map return value of function calls to object attribute


code:
mydict={}
exec "val1=100" in mydict
mydict['val1']

rawDescription: Given module m and code object c, what does "exec c in m.__dict__" do?


code:
import sys, os, stat, time

def get_times(p):
    s = os.stat(p)
    return (
        os.path.getatime(p),
        os.path.getmtime(p),
        os.path.getctime(p),
        s[stat.ST_ATIME],
        s[stat.ST_MTIME],
        s[stat.ST_CTIME],
    )

def main(p, delay=1):
    delay = float(delay)
    (a,b) = (1000000000, 1500000000)

    open(p,'w').close()

    print 'initial:'
    print get_times(p)

    for t in [ (a,a), (a,b), (b,a), (b,b) ]:
        print
        print 'test:', t
        os.utime(p,t)
        print get_times(p)
        time.sleep(delay)
        print get_times(p)

main(*sys.argv[1:])

rawDescription: Inconsistency in modified/created/accessed time on mac


code:
print myid, token
# P17544 436

rawDescription: Is there way to find the pharse and capture next token value


code:
## @package func
#  Module docs

## A function
#
#  More documentation.
def foo():
    print "Hello World!"

## Another function.
#
#  This function simply calls foo()
def bar():
    foo()

rawDescription: Doxygen python linking to functions

description: Doxygen python linking to functions


code:
D_values = []  # an empty list, not dict
Average = []
for path in glob("F:\Thermal Motion\*.txt"):
    t, x, y = np.loadtxt(path, unpack=True)
    #append to list instead of dict:
    D_values.append([((x[i]/.115 - x[0]/.115)**2 + (y[i]/.155 - y[0]/.155)**2)**0.5
                      for i in range(len(x))])
print D_values

rawDescription: Average of nth element of each list in a dictionary


code:
Data = [
    '1	', '2	', '3	', '4	',
    'A	', 'B	', 'C	', 'D	',
    'I	', 'II	', 'III	', 'IV	',
]

lists, result = [], []
for i in range(0, len(Data), 4):
    lists.append(Data[i:i+4])

for currentList in zip(*lists):
    result += list(currentList)
print result

rawDescription: Python sort list by algorithm

description: Python sort list by algorithm


code:
A=[2,-3,4,5,-4,3,-3,5,-2,1]
dp = [[0, 0]]
for i, x in enumerate(A):
    curr = list(dp[-1])
    if x < 0 and curr[0] + x > curr[1]:
        curr[1] = curr[0] + x
    elif x > 0 and curr[1] + x > curr[0]:
        curr[0] = curr[1] + x
    dp.append(curr)

print dp[-1][0]

rawDescription: Disjoint maxsubarray with alternate signs


code:
import numpy as np

y = np.array([2,1,5,2])          # y axis
x = np.array([1,2,3,4])          # x axis
m = np.ma.masked_where(y>2, y)   # filter out values larger than 5
print list(m)
print np.ma.compressed(m)

# mask x the same way
m_ = np.ma.masked_where(y>2, x)   # filter out values larger than 5
# print here the list
print list(m_)
print np.ma.compressed(m_)

rawDescription: how to apply a mask from one array to another array?

description: Apply a mask from one array to another array


code:
for item in s:
    keyList = []
    for key in d:
        if item in d[key]:
            keyList.append(key)
    print item, keyList

rawDescription: print corresponding key of dictionary for each element in a set

description: Print corresponding key of dictionary for each element in a set


code:
# Get a set of unique first elements for each list
set1 = set([n[0] for n in mark1])
set2 = set([n[0] for n in mark2])

# Items in mark1 but not mark2
set1_diffs = set1.difference(set2)
if set1_diffs:
  print 'Marks {0} in mark1 but not mark2'.format(list(set1_diffs))

# Items in mark2 but not mark1
set2_diffs = set2.difference(set1)
if set2_diffs:
  print 'Marks {0} in mark2 but not mark1'.format(list(set2_diffs))

rawDescription: Python: data unmatched in csv output

description: Python: data unmatched in csv output


code:
class what:
    def __init__(self):
        def yolo(self):
            self.extra = "Hello"
            print self.extra

        self.yolo = yolo.__get__(self)

what().yolo()

rawDescription: exec to add a function into a class

description: Exec to add a function into a class


rawDescription: Parse Specific Text File to CSV Format with Headers


code: print "The answer to ", x, "is", eval(x)

rawDescription: Python: Read the content of a variable

description: Python: Read the content of a variable


code:
import os

def three_tries():
    for i in range(3):
        filename = raw_input('please enter filename: ')
        try:
            infile = open(filename,'r')
            return infile
        except IOError:
            if not os.path.exists(filename): print 'File does not exist'
            else: print 'Error opening file'
    print '\nyou have exceed your three tries'
    print 'program ending... byebye...'
    exit()

print 'this is a foo bar script'
print 'i am going to ask the user for a filename'
infile = three_tries()
# Do whatever you want with the file, e.g.
print '\nreading file...'
print infile.read()

rawDescription: How to limit checking of file names in python?

description: Limit checking of file names in python


code:
import testa
print testa.var

rawDescription: How to set dynamic global variables in Python?

description: Set dynamic global variables in Python


code:
# Get the element tree from the file name and not a file object
tree = ElementTree.parse('config.xml')

# Get the root of the tree
root = tree.getroot()

# To get the 'Date' tag and print its text
date_tag = root.find('date').find('info_date').find('date').find('Date')
print date_tag.text

# Get the `aboutme` tag and print its text
about_me_tag = root.find('infoaboutauthor').find('nestedprofile').find('aboutme').find('CharacterString')
print about_me_tag.text

rawDescription: How to parse the xml to find the text value of the following node in python?

description: Parse the xml to find the text value of the following node in python


code:
print "calculating fib(1000000)"
res = fib(1000000)
print "displaying the result..."
print res

rawDescription: Fast Fibonacci slows to a crawl over 1,000,000


code:
from ast import literal_eval
from itertools import izip    #For memory efficiency use this over zip

with open('file.txt') as f:

    #Firstly collect the first column from each line in a list
    column_1 = [line.split(None, 1)[0] for line in f if not line.isspace()]
    f.seek(0)  #Move the file pointer to the start of the file

    # Now we need to use the second column from each line and convert it to
    # a Python list using ast.literal_eval.
    # Then we pass each of these lists to izip with `*` to get a transpose
    # of the data.

    for x in izip(*(literal_eval(line.split(None, 1)[1]) for line in
                                                        f if not line.isspace())):

        #Here x contains items on the same index from each list.

        print '"Coordinates:" {0}, {1}'.format(*x[0][:2])

        for a, b in izip(column_1, x):
            print "{0}, {1}".format(a, b[-1])
        print

rawDescription: Extract data from a list of tuples

description: Extract data from a list of tuples


code:
from os.path import join
import numpy
import struct
from time import time

def main():

    #Set the path name and filename
    folder = join("Tone_Tests","1khz_10ns_0907153323")
    fn = join(folder,"Channel1.raw32")

    #Test 1
    start = time()
    f = open(fn,'rb')
    array = read_fromstring(f)
    f.close()
    print "Test fromString = ",time()-start
    del array

    #Test 2
    start = time()
    f = open(fn,'rb')
    array = read_struct(f)
    f.close()
    print "Test fromStruct = ",time()-start
    del array

    #Test 3
    start = time()
    f = open(fn,'rb')
    array = read_fromfile(f)
    f.close()
    print "Test fromfile = ",time()-start
    del array

def read_fromstring(f):
    #Use Numpy fromstring, read each 4 bytes, convert, store in list
    data = []

    chunk = f.read(4)

    while chunk:
        num_chunk = numpy.fromstring(chunk, dtype = 'float32')
        data.append(num_chunk)

        chunk = f.read(4)

    return numpy.array(data)

def read_struct(f):
    #Same as numpy froms string but using the struct.
    data = []

    chunk = f.read(4)

    while chunk:
        num_chunk = struct.unpack('<f',chunk)
        data.append(num_chunk)

        chunk = f.read(4)

    return numpy.array(data)

def read_fromfile(f):
    return numpy.fromfile(f, dtype = 'float32', count = -1)

rawDescription: Python Binary File Manipulation Speed Up


code: print int(t, 16) >> 20

rawDescription: how to re-order bytes in a python hex string and convert to long

description: Re-order bytes in a python hex string and convert to long


code:
with open("datafile") as myfile:
    head = [next(myfile) for x in xrange(N)]
print head

rawDescription: Read first N lines of a file in python

description: Read first N lines of a file in python


code:
import re
s = "562: DEBUG, CIC, Parameter(Auto_Gain_ROI_Size) = 4"
t = "711: DEBUG, VSrc, Parameter(Auto_Contrast) = 0 "
myreg = re.compile(r'Parameter\((.*?)\)')
print myreg.search(s).group(1)
print myreg.search(t).group(1)

rawDescription: How to print string inside a parentheses in a line in Python?

description: Print string inside a parentheses in a line in Python


code:
matches=re.findall(r'(\bdemeanou?r\b)', 'demeanor demeanour xyzdemeanour demeanourxyz')
print matches, len(matches)
# ['demeanor', 'demeanour'] 2

rawDescription: regex is counting only one pattern, when two same patterns are kept consecutive.why?

description: Regex is counting only one pattern, when two same patterns are kept consecutive.why


code:
f = (x for x in [1,2,3])
print f
'%x' % id(f)

rawDescription: how do you obtain the address of an instance after overriding the __str__ method in python

description: Obtain the address of an instance after overriding the __str__ method in python


code:
import os.path
from one import two
print two.__name__.replace('.', os.path.sep)

rawDescription: What's the best way to retrieve the module path from the project in python?


code:
with open('file.dat', 'rb') as f:
    for chunk in iter(lambda: f.read(32), b''):
        print chunk.encode('hex')

rawDescription: pythonic way to hex dump files

description: Pythonic way to hex dump files


code:
montreal_json = pd.DataFrame.from_dict(many_jsons[0])
print montreal_json['features'][0]['geometry']

rawDescription: Python: Read several json files from a folder


code:
def find_terms(words, max_words_per_term):
    if len(words) == 0: return []
    return [" ".join(words[:i+1]) for i in xrange(min(len(words), max_words_per_term))] + find_terms(words[1:], max_words_per_term)

doc = 'a b c d e f'
words = doc.split(None)
for term in find_terms(words, 3):
    print term

rawDescription: How do I generate multi-word terms recursively?

description: Generate multi-word terms recursively


code:
if canbe==False:
    print 'added ' + str(i)
    total+=i

rawDescription: Project euler #23 tricky conflict

description: Project euler #23 tricky conflict


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:
iterable = iter([1,2,3])
peek, iterable = peek(iterable)
print "peek: %s" % peek
print "iterable: %s " % list(iterable)

rawDescription: How do I check the content of iterable object?

description: Check the content of iterable object


code:
def annotate(gen):
    prev_i, prev_val = 0, gen.next()
    for i, val in enumerate(gen, start=1):
        yield prev_i, prev_val
        prev_i, prev_val = i, val
    yield '-1', prev_val
for i, val in annotate(iter(range(4))):
    print i, val

rawDescription: Pythonic way of determining if the current element is the first or last element of a generator?


code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_cap = {'os': 'Windows', 'os_version': 'xp', 'browser': 'IE', 'browser_version': '7.0' }

driver = webdriver.Remote(
    command_executor='http://username:key@hub.browserstack.com:80/wd/hub',
    desired_capabilities=desired_cap)

driver.get("http://www.google.com")
if not "Google" in driver.title:
    raise Exception("Unable to load google page!")
elem = driver.find_element_by_name("q")
elem.send_keys("BrowerStack")
elem.submit()
print driver.title
driver.quit()

rawDescription: Using selenium at hosted app?

description: Using selenium at hosted app


code:
def test(*args, **kwargs):
    target = kwargs.get('target', '<default-value>')
    print target

rawDescription: why doesn't ** unpack kwargs in function calls?

description: Why doesn't ** unpack kwargs in function calls


code:
with open(file, "rb") as f:
    byte = f.read(20)

for b in byte:
    print b

rawDescription: Extract first 20 bytes in a binary header


code:
from pykml.factory import KML_ElementMaker as K
from lxml import etree

#line_points here comes from a geojson object
data = json.loads(open('tib.json').read())
line_points = data['features'][0]['geometry']['coordinates']

_doc = K.kml()

doc = etree.SubElement(_doc, 'Document')

for i, item in enumerate(line_points):
    doc.append(K.Placemark(
        K.name('pl'+str(i+1)),
        K.Point(
            K.coordinates(
                str(item).strip('[]').replace(' ', '')
                )
        )
    )
)

doc.append(K.Placemark(
    K.name('path'),
    K.LineStyle(
        K.color('#00FFFF'),
        K.width(10)
    ),
    K.LineString(
        K.coordinates(
            ' '.join([str(item).strip('[]').replace(' ', '') for item in line_points])
        )
    )
))

s = etree.tostring(_doc)

print s

rawDescription: How to display multiple placemarks and paths?

description: Display multiple placemarks and paths


code:
import cgi
form = cgi.FieldStorage()
print form["username"]

rawDescription: How are POST and GET variables handled in Python?


code:
data = []
with open("file.txt") as f:
    for line in f:
        lineData = {}
        lineSplit = line.split()
        for i in range(1,len(lineSplit)-1,2):
            lineData[lineSplit[i]] = lineSplit[i+1]
        data.append(lineData)
print data

rawDescription: Python nested for loop to split a file into lines then each line into variables


code:
import csv
with open('curr.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',')
    print [j for i in spamreader for j in i]

rawDescription: Import csv as list in python

description: Import csv as list in python


code:
# Your script.
import foo

foo.main()

# The file being imported.
def main():
    print "running foo.main()"

if __name__ == "__main__":
    main()

rawDescription: Python dynamically importing a script, need to have its __name__ == "__main__" code to be called


code:
import lxml.html
from lxml.etree import XPath
url = "http://gbgfotboll.se/information/?scr=table&ftid=51168"
date = '2014-09-27'

rows_xpath = XPath("//*[@id='content-primary']/table[3]/tbody/tr[td[1]/span/span//text()='%s']" % (date))
time_xpath = XPath("td[1]/span/span//text()[2]")
team_xpath = XPath("td[2]/a/text()")

html = lxml.html.parse(url)

for row in rows_xpath(html):
    time = time_xpath(row)[0].strip()
    team = team_xpath(row)[0]
    print time, team

rawDescription: python parse specific data on html table using lxml and xpath

description: Python parse specific data on html table using lxml and xpath


code:
cursor.execute("SHOW columns FROM table_name")
print [columns[0] for column in cursor.fetchall()]

rawDescription: python: MYSQLdb. how to get columns name without executing select * in a big table?

description: Python: MYSQLdb. how to get columns name without executing select * in a big table


code:
layout = '%(group)s/foo-%(locale)s/file.txt'
print format_to_re(layout)
pattern = re.compile(format_to_re(layout))
print pattern.match('something/foo-en-gb/file.txt').groupdict()

rawDescription: Python: Convert format string to regular expression


code:
import module1
print module1.foobar
module1.main()
module1.foobar[0] = 100
module1.main()
module1.x = "exex"
module1.printx()

rawDescription: How do I make a variable accessible from another python script when accessed from modulename.main() method

description: Make a variable accessible from another python script when accessed from modulename.main() method


code:
import xml.etree.ElementTree as ET

s = '<html><textarea name="content[country]">foo</textarea><textarea name="content[country]">bar</textarea></html>'
tree = ET.fromstring(s)
print [n.text for n in tree.iterfind('.//textarea[@name="content[country]"]')]
# ['foo', 'bar']

rawDescription: Get content of textarea

description: Get content of textarea


code:
for i,j,k in os.walk('.'):
    print i, j, k

rawDescription: How can I get all filename in different directories?

description: Get all filename in different directories


code:
from collections import Counter
user_to_count_map = Counter()
for username in list_of_usernames:
    user_to_count_map[username] += 1
for username, counter in sorted(user_to_count_map.items()):
    print username, counter[0]

rawDescription: Using setdefault, but not wanting to reassign back to map/dict


code:
# This will fail with TypeError: unhashable type: 'set'
rdd.map(lambda (k, v): (set(k), v)).groupByKey().collect()

rawDescription: A list as a key for PySpark's reduceByKey


code:
for node in G.nodes(data=True):
    print node

rawDescription: Processing XML file with networkx in python


code:
from collections import OrderedDict
lis =  [['a','x','y',2], ['b','xx','yy',2], ['c','xxx','yyy',3],['a','yx','yx',2]]
dic = OrderedDict()
for x in lis:
   key = (x[0],x[-1])
   if key not in dic:
      dic[key] = x[1:-1]
   else:
      val =  dic[key]
      dic[key] = [a+b for a,b in zip(val,x[1:-1])]

print [[k[0]] + v + [k[1]] for k,v in dic.items()]

rawDescription: python find duplicate lists in list of lists

description: Python find duplicate lists in list of lists


code:
from itertools import groupby

my_dict = {14:1, 15:2, 16:4, 11:5, 20:1, 22:5, 25:2}
key_fn, result = lambda (x, _): x / 5 + 1  if x % 5 else x / 5, {}

for item, grp in groupby(sorted(my_dict.items(), key = key_fn), key_fn):
    result[((item - 1) * 5 + 1, item * 5)] = sum(count for _, count in grp)

print result

rawDescription: How to group key:value pairs in a python dictionary based on certain range or interval?

description: Group key:value pairs in a python dictionary based on certain range or interval


code:
ColorVal = Datatype('ColorVal')
ColorVal.declare('white')
ColorVal.declare('black')
ColorVal = ColorVal.create()

mycolor = Const("mycolor", ColorVal)

print ColorVal.recognizer(0) # is_white
print ColorVal.recognizer(1) # is_black

print simplify(ColorVal.is_white(mycolor)) # returns is_white(mycolor)
print simplify(ColorVal.is_black(mycolor)) # returns is_black(mycolor)

mycolorVal = ColorVal.white # set to value white
print simplify(ColorVal.is_white(mycolorVal)) # true
print simplify(ColorVal.is_black(mycolorVal)) # false

# compare "variable" versus "value" with return of is_white, is_black, etc.: if it gives a boolean value, it's a value, if not, it's a variable
print "var vs. value"
x = simplify(ColorVal.is_white(mycolor))
print is_true(x) or is_false(x) # returns false, since x is is_white(mycolor)
y = simplify(ColorVal.is_white(mycolorVal))
print is_true(y) or is_false(y) # true

ColorValEnum, (whiteEnum,blackEnum)  = EnumSort("ColorValEnum",["whiteEnum","blackEnum"])
mycolorEnum = Const("mycolorEnum",ColorValEnum)

print ColorValEnum.recognizer(0) # is_whiteEnum
print ColorValEnum.recognizer(1) # is_blackEnum

# it appears that declaring an enum does not properly create the recognizers in the Python API:
#print simplify(ColorValEnum.is_whiteEnum(mycolorEnum)) # error: gives DatatypeSortRef instance has no attribute 'is_whiteEnum'
#print simplify(ColorValEnum.is_blackEnum(mycolorEnum)) # error: gives DatatypeSortRef instance has no attribute 'is_blackEnum'

rawDescription: how to check if a const in z3 is a variable or a value?

description: Check if a const in z3 is a variable or a value


code:
m = re.match(".*<\html>(.*)",my_html_text_string)
print m.groups()

rawDescription: Capturing regular expression in Python

description: Capturing regular expression in Python


code:
codec = 'XVID'
def demo(arg1, arg2, arg3, arg4):
    print arg1, arg2, arg3, arg4

demo(*codec)

rawDescription: Is there an efficient way of passing arguments?


rawDescription: Multiply scipy.lti transfer functions

description: Multiply scipy.lti transfer functions


rawDescription: Reading in repeated blocks of data using pandas and python


code:
"""some sample log"""
import os
import logging
import logging.config
import logging.handlers
from datetime import datetime

logger = logging.getLogger(__name__)

class CustomFormatter(logging.Formatter):
    """used for colored terminal logging"""
    BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
    colors = dict(
        CRITICAL='\33[1;9%dm' % RED,
        ERROR='\33[9%dm' % RED,
        WARNING='\33[9%dm' % YELLOW,
        INFO='\33[1;9%dm' % GREEN,
        DEBUG='\33[9%dm' % BLUE,
        NOTSET='\33[%dm' % BLACK,  # reset
    )

    def __init__(self, fmt=None, datefmt=None, color=False):
        fmt = fmt if fmt else ('%(asctime)s %(module)-11s %(levelname)-9s '
                               '%(threadName)-10s %(message)s')
        logging.Formatter.__init__(self, fmt, datefmt)
        self.color = color

    def formatTime(self, record, datefmt=None):
        create_time = datetime.fromtimestamp(record.created)
        if datefmt:
            s = create_time.strftime(datefmt)
        else:
            t = create_time.strftime("%Y-%m-%d %H:%M:%S")
            s = "%s.%03d" % (t, record.msecs)
        return s

    def format(self, record):
        s = super(CustomFormatter, self).format(record)
        if self.color:
            s = self.colors[record.levelname] + s + self.colors['NOTSET']
        return s

class LibFilter(logging.Filter):
    """custom filter to send suds logging to a separate file"""
    def filter(self, record):
        if [x for x in '__main__', __name__.split('.', 1)[0]
                if x in record.name]:
            return True
        else:
            create_time = datetime.fromtimestamp(record.created)
            t = create_time.strftime("%Y-%m-%d %H:%M:%S")
            s = "%s.%03d" % (t, record.msecs)
            with open('logs/%s.log' % record.name, 'a') as f:
                f.write(s + '\n')
                f.write(record.getMessage() + '\n\n')
            return False

def setup_logging():
    """use dictConfig to setup logging"""
    log_file = 'logs/app.log'
    doRollover = os.path.isfile(log_file)
    logging.config.dictConfig(
        dict(
            version=1,
            disable_existing_loggers=False,
            filters=dict(libfilter={'()': LibFilter}),
            formatters=dict(
                colorformatter={'()': CustomFormatter, 'color': True},
                fileformatter={'()': CustomFormatter}),
            handlers=dict(
                console={
                    'class': 'logging.StreamHandler',
                    'filters': ['libfilter'],
                    'formatter': 'colorformatter',
                    'level': 'DEBUG'},
                filehandler={
                    'class': 'logging.handlers.RotatingFileHandler',
                    'filename': log_file,
                    'backupCount': 3,
                    'filters': ['libfilter'],
                    'formatter': 'fileformatter',
                    'level': 'DEBUG'}),
            root=dict(level='INFO', handlers=['console', 'filehandler'])
        )
    )
    if doRollover:
        for handler in logging.getLogger('').handlers:
            if getattr(handler, 'doRollover', None):
                handler.doRollover()

rawDescription: No handler found for library with logging configured


code:
browser.execute_script("var a = document.getElementById('result-error').style; a.display = 'block'; a.visibility = 'visible'; a.opacity = 100;")
el = browser.find_element_by_class_name("one")
print el.text

rawDescription: Using Selenium to extract text...but text has 0 opacity


rawDescription: How to count the number of times a certain pattern in a sublist occurs within a list and then append that count to the sublist?

description: Count the number of times a certain pattern in a sublist occurs within a list and then append that count to the sublist


code:
def f(s, c, start):
    i = s.find(c, start)
    if i < 0:
        return [s]
    else:
        return f(s, c, i+1) + f(s[:i]+s[i+1:], c, i)

s = 'aAabbAA'
print f(s, 'A', 0)
# ['aAabbAA', 'aAabbA', 'aAabbA', 'aAabb', 'aabbAA', 'aabbA', 'aabbA', 'aabb']

rawDescription: Getting a list of strings with character removed in permutation


code:
import random

number = random.randint(1,6)

def start():
    print("Do you want to start?")
    answer = raw_input("Type 'Yes' to start!")
    if answer == "yes":
        print "Rolling the dice... The number is... ", number
    else:
        print("Aww :( ")

start()

rawDescription: Function Is Not Running

description: Function Is Not Running


code: print "\n".join(s for s in list if sub in s)

rawDescription: python: finding substring within a list

description: Python: finding substring within a list


code:
import re

def find_number_in_filename(fn):
    m = re.match(r"(\d+)([A-Za-z]+)$", fn)
    if m:
        prefix, suffix, num_length = "", m.group(2), len(m.group(1))
        return prefix, suffix, num_length

    m = re.match(r"([A-Za-z]+)(\d+)$", fn)
    if m:
        prefix, suffix, num_length = m.group(1), "", len(m.group(2))
        return prefix, suffix, num_length

    return fn, "", 0

example_fn = ("000foo", "bar14", "baz0", "file10name")
for fn in example_fn:
    prefix, suffix, num_length = find_number_in_filename(fn)
    if num_length == 0:
        print "%s: does not match" % fn
    else:
        print "%s -> %s[%d-digits]%s" % (fn, prefix, num_length, suffix)

        all_numbered_versions = [("%s%0"+str(num_length)+"d%s") % (prefix, ii, suffix) for ii in range(0,10**num_length)]
        print "\t", all_numbered_versions[0], "through", all_numbered_versions[-1]

rawDescription: enumerating numbers in strings

description: Enumerating numbers in strings


code:
for container in soup.select('.results > li'):
    for paper in container.select('.art-abs-url span'):
        print paper.text.strip()

        for author in container.select('.prefNameLink'):
            print author.text.strip()

rawDescription: Why beautiful soup does not return the results in order?


code:
def initfloatmethod():
    a,b=float(3),2
    while True:
        print a/b

rawDescription: In Python 2.* division which has more overhead?


code:
from xml.etree.ElementTree import iterparse

NS = '{http://www.mediawiki.org/xml/export-0.3/}'

with open('mw.xml') as f:
    for event, elem in iterparse(f):
        if elem.tag == '{0}page'.format(NS):
            title = elem.find("{0}title".format(NS))
            contr = elem.find(".//{0}username".format(NS))

            if title is not None:
                print title.text
            if contr is not None:
                print contr.text

            elem.clear()

rawDescription: Extracting page titles and contributors from MediaWiki XML


code:
import inspect

class A(object):
    @property
    def name():
        return "Masnun"

def method_with_property(klass):
    props = []

    for x in inspect.getmembers(klass):

        if isinstance(x[1], property):
            props.append(x[0])

    return props

print method_with_property(A)

rawDescription: Lookup @property methods with inspect module


code:
import code

exprs = [
    'd = {}',
    'd',
    'd["x"] = 1',
    'd',
   ]

for e in exprs:
    print '>>> %s' % e
    cmd = code.compile_command(e)
    r = eval(cmd)
    if r:
        print repr(r)

rawDescription: Executing python code from script and getting interpreter-style output


code:
def Lagrange (Lx, Ly):
    X=sympy.symbols('X')
    if  len(Lx)!= len(Ly):
        print "ERROR"
        return 1
    y=0
    for k in range ( len(Lx) ):
        t=1
        for j in range ( len(Lx) ):
            if j != k:
                t=t* ( (X-Lx[j]) /(Lx[k]-Lx[j]) )
        y+= t*Ly[k]
    return y

rawDescription: Lagrange interpolation in Python: as a result matematical formula


code:
fs = []
for i in range(3):
    def f(arg=i**3):
        return arg
    fs.append(f)
print [f() for f in fs]

rawDescription: python what happens when a function is called?

description: Python what happens when a function is called


code:
import imp

def user_import(name, path):
    mod_file, mod_path, mod_desc = imp.find_module(name, [path])
    return imp.load_module(name, mod_file, mod_path, mod_desc)

# load the real 'string' module
import string
print string.digits

# load the user 'string' module which lives under the path './foo'
mod = user_import('string', './foo')
print mod.digits

rawDescription: How to import a user module which has the same name with a system module?

description: Import a user module which has the same name with a system module


code:
import string
import collections

def cleanedup(fh):
    for line in fh:
        word = ''
        for character in line:
            if character in string.ascii_letters:
                word += character
            elif word:
                yield word
                word = ''

with open ('DQ.txt') as doc:
    wordlist = collections.Counter(cleanedup(doc))
    print wordlist.most_commond(5)

rawDescription: Python program: foreign language word-frequency dictionary


code:
def divide(x):
    left, _, right = str(x).partition(".")
    first = [int(left[0])]
    second = [int(i) for i in left[1:]]
    third = [int(i) for i in right]
    return first, second, third

print divide(123456)
print divide(123.456)

rawDescription: Using Float questions

description: Using Float questions


code:
from operator import itemgetter
d.sort(key = itemgetter(0))
print d

rawDescription: Python, sort a dict to represent a histogram


code:
import itertools
calclists = [[1,2,3], [4,5,6], [7,8,9]]
comb_itr = itertools.combinations(itertools.chain.from_iterable(calclists), 2)
for comb in comb_itr:
    print comb

rawDescription: Editing script to account for every combination of two lists


code:
from time import sleep

from bs4 import BeautifulSoup
from numpy import genfromtxt
import requests

my_data = genfromtxt('ASINS.csv', delimiter=',', dtype=None)

# initialize a session
session = requests.Session()
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36'}

for ASIN in my_data[:20]:
    url = "http://www.amazon.com/gp/product/" + ASIN[1:11]
    sleep(1.5)

    response = session.get(url, headers=headers)
    soup = BeautifulSoup(response.content)

    print url
    # get best seller rankings
    for rank in soup.select('ul.zg_hrsr li.zg_hrsr_item'):
        print rank.text
    print "----"

rawDescription: scraper only returning results for first 2 inputs

description: Scraper only returning results for first 2 inputs


code: print [len(s) for s in 'a0ddb0gf0'.split('0') if s]

rawDescription: What is the fastest way to generate a list of the lengths of sub-strings in a string, given a separator?


code:
#!/usr/bin/env python
#http://stackoverflow.com/questions/14629710/python-setting-attributes-from-module-to-module/14629838#14629838

class Player(object):
    def __init__(self):
        self.name = ' '
        self.hp = 0
        self.current_hp = 0
        self.strength = 0
        self.dexterity = 0
        self.constitution = 0

class PlayerPool(object):
    _players = dict()

    @classmethod
    def getPlayerByName(cls, name):
        if not name in cls._players:
            newPlayer = Player()
            newPlayer.name = name
            cls._players[newPlayer.name] = newPlayer
        return cls._players[name]

class Scene(object):
    pass

class CharacterCreation(Scene):
    def enter(self):
        character = PlayerPool.getPlayerByName("foobar-hero")
        character.hp = 10
        print "%s has %s points of hp" % (character.name, character.hp)
        return 'barracks'

class Barracks(Scene):
    def enter(self):
        character = PlayerPool.getPlayerByName("foobar-hero")
        print "%s has %s points of hp" % (character.name, character.hp)
        return 'shop'

if __name__ == "__main__":
    step1 = CharacterCreation()
    if step1.enter() == "barracks":
        step2 = Barracks()
        step2.enter()

rawDescription: python: setting attributes from module to module

description: Python: setting attributes from module to module


code:
l = ["a", "b", "c", "d"]

def printGrid (numsPerRow, l):
    printStr = ""
    numsInRow = 1
    for i in range(len(l)):

        item = l[i]
        if numsInRow % numsPerRow == 0:
            printStr += "{0}\n".format(item)
            numsInRow = 1
        else:
            printStr += "{0}\t".format(item)
            numsInRow += 1
    return printStr

print printGrid(2, l)

rawDescription: Print a list of strings in a grid format - Python


code:
modifiedDirList = []
for name in dirList:
    match = re.search(r'\.mp3', name)
    if match:
        i += 1
        modifiedDirList.append(name)

print modifiedDirList

rawDescription: Why are these strings escaping from my regular expression in python?


code:
import subprocess
proc = subprocess.Popen(['ssh', 'user@server'])
result = proc.wait()
print result

rawDescription: Python script ending in SSHing the terminal into a remote server


code:
def rest(a, b):
    print a - b

def sum(a, b):
    print a + b

def run(adictio):
    for method, (a, b) in adictio.iteritems():
        method(a, b)

mydictio = {rest:(3, 2), sum:(4, 5)}

run(mydictio)

rawDescription: Running bunch of python methods on a single piece of data


rawDescription: I am creating a dictionary in python

description: I am creating a dictionary in python


code:
import xmlrpclib

s = xmlrpclib.ServerProxy('http://localhost:8000')
print s.myfunction(2, 4)

rawDescription: How to send a xml-rpc request in python?

description: Send a xml-rpc request in python


code:
def ret_this():
    print "This."

rawDescription: Python return value from scheduled event


code:
import re

p = re.compile("(?<! .)([.] |! )")
t = u"Great buy w. all amenities! Use on all cars. come on in "

last = None
for m in p.finditer(t):
    last = m

if last is not None:
    print m.start()

rawDescription: regexp, python: index of last occurrence. Has negative look-behind

description: Regexp, python: index of last occurrence. Has negative look-behind


code:
proc = subprocess.Popen(["ls", "-l"], stdout=PIPE, stderr=PIPE)

# Here you can get the PID
global child_pid
child_pid = proc.pid

# Now we can wait for the child to complete
(output, error) = proc.communicate()

if error:
    print "error:", error

print "output:", output

rawDescription: How to kill a python child process created with subprocess.check_output() when the parent dies?

description: Kill a python child process created with subprocess.check_output() when the parent dies


code:
n = int(input("Insert a number: "))
sum =0
while n>0:
    sum+=n
    n-=1
print sum

rawDescription: Python. Using input and while

description: Python. Using input and while


code:
for genes in genetics:
    if genes.lower().count("a") == genes.lower().count("g") == 2:
        genes = ''.join(genes)
    print Counter(genes)

rawDescription: Rearrange strings in list alphabetically and by case


code:
print a%2==0
# [[False  True]
#  [False  True]]

rawDescription: Walk through each column in a numpy matrix efficiently in Python


rawDescription: iterating over lists finding standard deviation of each point

description: Iterating over lists finding standard deviation of each point


code:
a, b = divmod(23, 10)
a, b
print "Tens: %d\nOnes: %d" % divmod(23, 10)

rawDescription: Splitting a 2 digit number into 10's and 1's in python


rawDescription: Split lines into paragraphs

description: Split lines into paragraphs


code:
date1 = datetime.date(2004, 9, 25)
date2 = datetime.date(2004, 10, 8)
day = datetime.timedelta(days=1)

while date1 <= date2:
    print date1.strftime('%Y.%m.%d')
    date1 = date1 + day

rawDescription: Creating a loop for two dates

description: Creating a loop for two dates


code:
array1 = 'A','B','C','D'
array2 = 'E','F','G','H'
array3 = 'I','J','K','L'
for elems in zip(array1, array2, array3):
    print ''.join(elems)

rawDescription: python combine array item by item

description: Python combine array item by item


code:
x = 0
y = 0
print (x,y)
for i in range(1,5):
    y = i
    print [(x,y) for x in range(i)]
    x = i
    print [(x,y) for y in range(i+1)]

rawDescription: Placing graphs in a landscape grid (a python chalenge)


code:
from multiprocessing import Pool

def process_line(line):
    return "FOO: %s" % line

if __name__ == "__main__":
    pool = Pool(4)
    with open('file.txt') as source_file:
        # chunk the work into batches of 4 lines at a time
        results = pool.map(process_line, source_file, 4)

    print results

rawDescription: Processing single file from multiple processes in python


code:
from lxml.html import fromstring

html_data = """

Product Composition

93% Polyamide 7% Elastane

Lining: 100% Polyester

Dress Length: 90 cm

Product Attributes;

: Boat Neck, Long Sleeve, Midi, Zip, Concealed, Laced, Side

Lining Type: Full Lining

"""

tree = fromstring(html_data)
data = {}
for strong in tree.xpath('//p/strong'):
    parent = strong.getparent()

    description = []
    next_p = parent.getnext()
    while next_p is not None and not next_p.xpath('.//strong'):
        description.append(next_p.text)
        next_p = next_p.getnext()

    data[strong.text] = " ".join(description)

print data

rawDescription: how to make a selection for the dictionary?

description: Make a selection for the dictionary


code:
import re
samples = '''\
john   smith    23    21.01.1994 02:23|
mr    magoo    30.04.05 23:37|
    Mr bing bong    29.01.10 20:53|
'''.splitlines()
dtpattern = re.compile(r'(?<=\s)\d{2}\.\d{2}\.(?:\d{2}|\d{4})\s+\d{2}:\d{2}(?=|)')
for line in samples:
    print dtpattern.search(line).group()

rawDescription: How to split() a string before date

description: Split() a string before date


code:
import urllib2

downloaded_data  = urllib2.urlopen('http://...')

for line in downloaded_data.readlines():
    print line

rawDescription: get csv data from a website

description: Get csv data from a website


code:
for root, dirs, files in os.walk(rootdir):
    for file in files:
        gain = xy.method(os.path.join(root, file))
        print gain

rawDescription: iterating through folders and from each use one specific file in a method python

description: Iterating through folders and from each use one specific file in a method python


code:
for field in test:
    count = count + 1
    print count

    HarbName[count] = str(field.getValue("NAME"))

rawDescription: Changing a variable during a loop, Python


code:
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
import threading
import time

class UdpListener(DatagramProtocol):
    message_lock = threading.Lock()
    messages = []

    def datagramReceived(self, data, (host, port)):
        with self.message_lock:
            self.messages.append(data)

    def getAndClearMessages(self):
        with self.message_lock:
            res = self.messages
            self.messages = []
        return res

class Messenger(threading.Thread):

    listener = None

    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        while True:
            time.sleep(1)
            recivedMessages = self.listener.getAndClearMessages()
            length = len(recivedMessages)
            #doSomethingWithMessages(recivedMessages)
            print(length)

listener = UdpListener()

messenger = Messenger()
messenger.listener = listener
messenger.start()

reactor.listenUDP(5556, listener)
reactor.run()

rawDescription: Is this python code thread safe (thread with twisted)?


code:
exec open(YOUR_FILE).read()
f1 = open(FIRST_FILE,"w")
f2 = open(SECOND_FILE,"w")
for line in pjr_files:
    f1.write(line)
for line in pjr_files_2:
    f2.write(line)

rawDescription: Python extracting blocks of text from a file


code:
import re
import random
import sys
import time

# get_patterns makes up some test data.
def get_patterns():
    rng = random.Random(1)  # fixed seed, for reproducibility
    n = 300
    # Generate up to n unique integers between 60k and 80k.
    return list(set([str(rng.randint(60000, 80000)) for _ in xrange(n)]))

def original(rs, matches):
    for eachcsv in matches:
        with open(eachcsv, 'r') as f:
            lines = f.readlines()
            for entry in rs:
                for line in lines:
                    if entry in line:
                        print("found %s in %s" % (entry, eachcsv))

def mine(rs, matches):
    my_rx = re.compile(build_regex(rs))
    for eachcsv in matches:
        with open(eachcsv, 'r') as f:
            body = f.read()
            matches = my_rx.findall(body)
            for match in matches:
                print "found %s in %s" % (match, eachcsv)

def build_regex(literal_patterns):
    return "|".join([re.escape(pat) for pat in literal_patterns])

def print_elapsed_time(label, callable, args):
    t1 = time.time()
    callable(*args)
    t2 = time.time()
    elapsed_ms = (t2 - t1) * 1000
    print "%8s: %9.1f milliseconds" % (label, elapsed_ms)

def main(args):
    rs = get_patterns()
    filenames = args[1:]
    for function_name_and_function in (('original', original), ('mine', mine)):
        name, func = function_name_and_function
        print_elapsed_time(name, func, [rs, filenames])
    return 0

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

rawDescription: How can I efficiently search for many strings at once in many files?

description: Efficiently search for many strings at once in many files


code:
from collections import Counter
with open('input') as f:
    words = f.read().upper().split()
symbols = [word for word in words if len(word) > 1 and word[0]=="$" and word[1:].isalpha()]
freqs = Counter(symbols)
for key in sorted(freqs):
    print '%-8s : %3i' % (key, freqs[key])

rawDescription: Stock Symbol Frequency using Regex in Python


code:
from collections import Counter
def main():
    #use open() for opening file.
    #Always use `with` statement as it'll automatically close the file for you.
    with open(r'C:\Data\test.txt') as f:
        #create a list of all words fetched from the file using a list comprehension
        words = [word for line in f for word in line.split()]
        print "The total word count is:", len(words)
        #now use collections.Counter
        c = Counter(words)
        for word, count in c.most_common():
           print word, count
main()

rawDescription: A program that opens a text file, counts the number of words and reports the top N words ordered by the number of times they appear in the file?


code:
vectors = [generate_vector() for i in range(0, 10)]
print sorted([findbest(vector) for vector in vectors], key=sum)

rawDescription: Python - sort lists based on their sum

description: Python - sort lists based on their sum


code:
import sys
import linecache

def _traceit(frame, event, arg):
    '''
    http://www.dalkescientific.com/writings/diary/archive/2005/04/20/tracing_python_code.html
    '''
    if event == "line":
        lineno = frame.f_lineno
        filename = frame.f_globals["__file__"]
        if (filename.endswith(".pyc") or
            filename.endswith(".pyo")):
            filename = filename[:-1]
        name = frame.f_globals["__name__"]
        line = linecache.getline(filename, lineno)
        print "%s  # %s:%s" % (line.rstrip(), name, lineno,)
    return _traceit

def _passit(frame, event, arg):
    return _passit

def traceit(on=True):
    if on: sys.settrace(_traceit)
    else: sys.settrace(_passit)

def mktemp(src):
    pass

def my_mktemp(src):
    mktemp(src)
    p=src

traceit()
for src in ('cpfs.c','log.c',):
    my_mktemp(src)
traceit(False)

rawDescription: Can Python be made to generate tracing similar to bash's set -x?


code:
fname = 'GLA06_634_1102_001_0079_3_01_0001.DAT'

with open(fname,'rb') as fi:
    recl = None
    numhead = None

    # Loop in case the required headers are not the first two one
    # and/or in wrong order
    for line in fi:
        if line.startswith('Recl='):
            recl = int(line[5:-2])
        if line.startswith('Numhead='):
            numhead = int(line[8:-2])

        if recl is not None and numhead is not None:
            break

    offset = recl*numhead

    print "Binary data starts at ", offset
    fi.seek(offset)

rawDescription: get number of header lines beforehand

description: Get number of header lines beforehand


code:
mylist = ['hai', 'hello', 'hey']
print mylist.index('hello')

rawDescription: How to compare if an item in a list has the same value AND position as an item in another list? Python 2.7

description: Compare if an item in a list has the same value AND position as an item in another list? Python 2.7


code:
from math import *

def max_number(n):
    digit_count = int(log(abs(n+1),10)) + 1
    digits = sorted([(n / 10 ** (x - 1) % 10)  for x in range(digit_count,0,-1) ], reverse=True)
    return reduce(lambda x, y:10*x + y, digits)

print max_number(1000)
print max_number(999)
print max_number(2345128)
print max_number(231)

rawDescription: Find the biggest number formed by digits of input numer


code:
class ThingType(type):
    __stuff__ = ["foo", "bar", "baz"]

    @classmethod
    def __iter__(cls):
        return iter(cls.__stuff__)

class Thing(object):
    __metaclass__ = ThingType

for thing in Thing:
    print thing

rawDescription: Defining magic methods on classes

description: Defining magic methods on classes


code:
a = [5]
b = a
b = b + [6]        # created a new list
print id(a), id(b) # different

rawDescription: Why equal integers behaves differently to equal lists?


code:
with open("dictionary.txt","r") as f:
    for line in f:
        print "Count " + line + str((a_master).count(line.strip()))

rawDescription: Counting occurences of words that appear in a list using Python


code:
from StringIO import StringIO
import datetime as dt
import pandas as pd
import dateutil.parser as parser

def addtime(temp):
    temp = parser.parse(temp) + dt.timedelta(hours=16)
    return temp

data = """\
    '12-31-2012',5100, 5200
    '01/1/2013',5300,5400"""

tdata = pd.read_csv(StringIO(data),
    names = ['date', 'field1', 'field'], index_col = None,
    parse_dates =['date'], header= None, date_parser=addtime)

print tdata

rawDescription: Date conversion in pandas csv reader

description: Date conversion in pandas csv reader


code:
import numpy as np
import numpy.lib.recfunctions

# Define the starting arrays as structured arrays with two fields ('key' and 'field')
dtype = [('key', np.int), ('field', np.float)]
x = np.array([(1, 2),
             (2, 4),
             (3, 6),
             (4, np.NaN),
             (5, 10)],
             dtype=dtype)

y = np.array([(0, -5),
             (1, 0),
             (2, 5),
             (5, 20),
             (6, 25)],
             dtype=dtype)

# You want an outer join, rather than the default inner join
# (all values are returned, not just ones with a common key)
join = np.lib.recfunctions.join_by('key', x, y, jointype='outer')

# Now we have a structured array with three fields: 'key', 'field1', and 'field2'
# (since 'field' was in both arrays, it renamed x['field'] to 'field1', and
#  y['field'] to 'field2')

# This returns a masked array, if you want it filled with
# NaN's, do the following...
join.fill_value = np.NaN
join = join.filled()

# Just displaying it... Keep in mind that as a structured array,
#  it has one dimension, where each row contains the 3 fields
for row in join:
    print row

rawDescription: merging indexed array in Python

description: Merging indexed array in Python


code:
def foo(*mylist):
    bar("first", *mylist)

def bar(*vals):
    print "|".join(vals)

foo("a","b")

rawDescription: How to unpack a list?

description: Unpack a list


code: re.sub(ur"[ \s*[\] ]+", "", st, flags=re.UNICODE).strip()

rawDescription: Add characters to Python unicode strip?

description: Add characters to Python unicode strip


code:
def getLetters(data):
    for child in data:
        # use other default values here if more suitable
        c1 = None
        c2 = None
        if child.attrib['c1']:
           c1 = child.attrib['c1']
        if child.attrib['c2']:
           c2 = child.attrib['c2']
        yield {'c1':c1, 'c2':c2} # yield the mini-dictionary for every child
    # no return needed here

# you can process the output in the same way:
for entry in getLetters(data):
    print 'c1', entry['c1'], 'c2', entry['c2']

rawDescription: Iterated return in constructors

description: Iterated return in constructors


code:
class A(object):

    class C():
       def Bar(self):
           print "Goodbye"
           A.name()

    @staticmethod
    def name():
        print "FooBar"

A.C().Bar()

rawDescription: Python subclasses calling parent function


code:
import re

def evaluatesimple(s):
  return eval(s)

def evaluate(s):
  while 1:
    simplesums=re.findall("\[([^\]\[]*)\]",s)
    if (len(simplesums) == 0):
      break
    replacements=[('[%s]' % item,str(evaluatesimple(item))) for item in simplesums]
    for r in replacements:
      s = s.replace(*r)
  return s

print evaluate("[9+[7*3+[1+2]]-5]")

rawDescription: Storing and evaluating nested string elements


code:
import numpy as np

def rolling(a, window):
    shape = (a.size - window + 1, window)
    strides = (a.itemsize, a.itemsize)
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

a = np.arange(10)
print rolling(a, 3)

rawDescription: Efficient Numpy 2D array construction from 1D array


code:
colors = [ [1, "red", 3], [2, "red", 2], [3, "red", 3], [5, "red", 1], [3, "green", 9], [10, "green", 4] ]

def avg(list):
    return sum(list) / float(len(list))

def process(colors, threshold=2):
    colors_combined = {}
    colors_processed = []

    # sort colors by their name
    for color in colors:
        position, color_name, count = color

        if color_name not in colors_combined.keys():
            colors_combined[color_name] = []

        colors_combined[color_name].append([position, count])

    # print colors_combined

    # process the data
    for color in colors_combined.keys():
        data = colors_combined[color]

        if len(data) == 1: # there can't be a case, where len(data) = 0
            colors_processed.append([data[0], color, data[1]])
        else: # more than 1 positions to check
            last_position = data[0][0]
            positions = [last_position]
            count_combined = data[0][1]

            for element in data[1:]:
                if abs(last_position - element[0]) <= threshold: # element is inside of the distance
                    positions.append(element[0])
                    count_combined += element[1]
                else:
                    colors_processed.append([avg(positions), color, count_combined])
                    positions = [element[0]]
                    count_combined = element[1]

                last_position = element[0]

            if len(positions) > 0: # the last processed elements where inside the distance, but not added
                colors_processed.append([avg(positions), color, count_combined])

    return colors_processed

print process(colors)

rawDescription: Analysing and calculating new values in list of lists in Python3


code:
lc = sorted(sorted([k] + [v[x] for x in sorted(v)] for k,v in myDict.items()))
for row in lc:
    print row

rawDescription: Shortening a list comprehension that taps into a nested dictionary


code:
def sh(dir):
    a = xlwt.Workbook()
    sheet = a.add_sheet('sheet1')
    n = 0
    for r,d,f in os.walk(dir):
        for files in f:
            if files.endswith(".xml"):
                print files
                dom=parse(os.path.join(r, files))
                name = dom.getElementsByTagName('rev')
                title = dom.getElementsByTagName('title')
                sheet.write(n, 0, files)
                sheet.write(n, 1, title[0].firstChild.nodeValue)
                sheet.write(n, 2, name[0].firstChild.nodeValue)
                n += 1
                print title[0].firstChild.nodeValue
                print name[0].firstChild.nodeValue
    a.save('sha.xls')

rawDescription: Printing the output in Excel

description: Printing the output in Excel


code:
def foo():
    print "foo"
class A:
    def bar( self ):
        print "bar"
a = A()
foo
a.bar

rawDescription: Adding a Method to an Existing Object Instance


code:
import time
import random
import threading

def run(incoming):
        time.sleep(5)
        print incoming

while True:
    hash = random.getrandbits(128)
    threading.Thread(target = run,args = (hash,)).start()
    time.sleep(1)

rawDescription: Calling a function and each call works simultaneously and not in a queue


code:
# intellectual property belongs to imdb
import urllib2
from bs4 import BeautifulSoup

# get the SOUP: tree structure out of the HTML page
soup = BeautifulSoup(urllib2.urlopen("http://www.imdb.com/title/tt0413573/episodes?season=10"))

result = {}
for div in soup.find_all("div", {"class":"airdate"}):
    # get the date and number and store in a dictionary
    date = div.text.encode('utf-8').strip()
    number = div.find_previous_sibling()['content']
    result[number] = date

print result

rawDescription: find all matching words in string from URL

description: Find all matching words in string from URL


code: print paramdata.values

rawDescription: Output data from all columns in a dataframe in pandas


code:
print u'æƒ¨äº‹'.encode('cp1252').decode('utf8')
print u'æœ€'.encode('cp1252').decode('utf8')

rawDescription: Converting incorrectly encoded Chinese characters in MySQL to UTF-8


code:
import globals
def main():
    globals.default()
    print globals.value

if __name__ == "__main__":
    main()

rawDescription: Python global variable scoping

description: Python global variable scoping


code:
hdr = {'Accept': 'text/html,application/xhtml+xml,*/*',"user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"}
url = 'http://www.carnextdoor.com.au/'

req=urllib2.Request(url,headers=hdr)
#Here I get the error with and without using the header or going html = urllib2.urlopen(url).read()
html = urllib2.urlopen(req).read()
soup=BeautifulSoup(html,"html5lib")
print soup

rawDescription: HTTP Error 403: Forbidden urlib2 Python 2.7


code:
import cmd

class EchoCmd(cmd.Cmd):
    """Simple command processor example."""

    def do_echo(self, line):
        print line

    def do_EOF(self, line):
        return True

if __name__ == '__main__':
    EchoCmd().cmdloop()

rawDescription: Is it bad practice to use python's getattr extensively?


code:
from selenium import webdriver

driver = webdriver.Firefox() # webdriver.PhantomJS()
driver.get('http://bp1.shoguto.com/detail.php?userg=hhchpxqhacciliq')
print driver.title
driver.quit()

rawDescription: Python: why site is not parsing?

description: Python: why site is not parsing


code:
import gzip
f=gzip.open('Onlyfinnaly.log.gz','rb')
file_content=f.read()
print file_content

rawDescription: Read from a gzip file in python

description: Read from a gzip file in python


code:
table = '''\
|London          |       Jul  9 2014  1:21PM  |john         |
|New York        |       Jul  9 2014  1:21PM  |peter        |
|New Zeland      |       Jul  9 2014  1:21PM  |Mr. Jones    |
'''.splitlines()
for line in table:
    line = '|'.join([c.strip() for c in line.split('|')])
    print line

rawDescription: remove leading and trailing spaces but not in between the words

description: Remove leading and trailing spaces but not in between the words


code:
group_pool_photos = []
group_id = '544735@N20'    # caterpillarequipment's group_id
page = 1
success = True
while True:
    response = flickr.groups.pools.getPhotos(group_id=group_id, page=page)
    if response['stat'] != 'ok':
        print 'Error occurred in flickr.groups.pools.getPhotos'
        pprint(response)
        success = False
        break

    if len(response['photos']['photo']) == 0:
        break

    group_pool_photos.extend(response['photos']['photo'])
    page += 1

if success:
    print 'Info for all pool photos successfully retrieved. Total photos {}'.format(len(group_pool_photos))

rawDescription: python flickr api for group search and get image data set

description: Python flickr api for group search and get image data set


code:
import collections
import itertools

te_seqs = ["ATCTACTGATG", "ATACAGTACATAGA", "ATAGACAGTTGTGCG", "GTCGATACGT"]

sites = map(collections.Counter, itertools.izip_longest(*te_seqs))
for site in sites:
  A = site.get("A", 0)
  T = site.get("T", 0)
  G = site.get("G", 0)
  C = site.get("C", 0)
  total = float(A + T + G + C)
  print A / total, T / total, G / total, C / total

rawDescription: Access every character in a list of strings avoiding nested for loops


code:
for i in xrange(1900,2001):
    for j in xrange(2000, 4001):
        year = str(i)
        image = str(j)
        new_link = 'http://www.fdci.org/imagelibrary/EventCollection/'+year+'/Big/IMG_'+image+'.jpg'
        print new_link
        #Now you will get the possible links within the given ranges,
        #then you can use urllib2 to fetch the response from the link
        # and do whatever you wanna do

rawDescription: Download numbered image files in numbered folders from internet using python


code:
from collections import OrderedDict
from operator import itemgetter
from timeit import Timer
from random import randint, random

N = 100000
xs = [('a', 10), ('b', 9), ('c', 4), ('d', 7), ('e', 3), ('f', 0), ('g', -5), ('h', 9)]

od = OrderedDict(xs)
d = dict(xs)

def f1od_min():
    return min(od, key=od.get)

def f2od_min():
    return min(od.iteritems(), key=itemgetter(1))[0]

def f1d_min():
    return min(d, key=d.get)

def f2d_min():
    return min(d.iteritems(), key=itemgetter(1))[0]

def f1od():
    for k in od: pass

def f2od():
    for t in od.iteritems(): pass

def f1d():
    for k in d: pass

def f2d():
    for t in d.iteritems(): pass

print 'min'
print(Timer(stmt='f1od_min()', setup='from __main__ import f1od_min').timeit(number=N))
print(Timer(stmt='f2od_min()', setup='from __main__ import f2od_min').timeit(number=N))
print(Timer(stmt='f1d_min()', setup='from __main__ import f1d_min').timeit(number=N))
print(Timer(stmt='f2d_min()', setup='from __main__ import f2d_min').timeit(number=N))
print
print 'traverse'
print(Timer(stmt='f1od()', setup='from __main__ import f1od').timeit(number=N))
print(Timer(stmt='f2od()', setup='from __main__ import f2od').timeit(number=N))
print(Timer(stmt='f1d()', setup='from __main__ import f1d').timeit(number=N))
print(Timer(stmt='f2d()', setup='from __main__ import f2d').timeit(number=N))

rawDescription: Understanding performance difference

description: Understanding performance difference


code:
import xml.etree.ElementTree as ET

class Parser:

    def __init__(self):
        self.output_list = []  # collected output lines
        self.il = 0            # indentation level

    def __iter__(self):
        return iter(self.output_list)

    def out(self, s):
        '''Output the indented string to the output list.'''
        self.output_list.append('    ' * self.il + s)

    def indent(self, num=1):
        '''Increase the indentation level.'''
        self.il += num

    def dedent(self, num=1):
        '''Decrease the indentation level.'''
        self.il -= num

    def parse(self, elem):
        '''Call the parser of the elem.tag name.

        The tag name appended to "parse_" and then the name of that
        function is called.  If the function is not defined, then
        self.parse_undefined() is called.'''

        fn_name = 'parse_' + elem.tag
        try:
            fn = getattr(self, fn_name)
        except AttributeError:
            fn = self.parse_undefined
        return fn(elem)

    def loop(self, elem):
        '''Helper method to loop through the child elements.'''
        for e in elem:
            self.parse(e)

    def parseXMLfile(self, fname):
        '''Reads the XML file and starts parsing from the root element.'''
        tree = ET.parse(fname)
        script = tree.getroot()
        assert script.tag == 'script'
        self.parse(script)

    ###################### ELEMENT PARSERS #######################

    def parse_undefined(self, elem):
        '''Called for the element that has no parser defined.'''
        self.out('PARSING UNDEFINED for ' + elem.tag)

    def parse_script(self, elem):
        self.loop(elem)

    def parse_stage(self, elem):
        self.out('')
        self.out('Parsing the stage: ' + elem.attrib['id'])
        self.indent()
        self.loop(elem)
        self.dedent()

    def parse_initialise(self, elem):
        self.out('')
        self.out('#---------- ' + elem.tag + ' ----------')
        self.loop(elem)

    def parse_variable(self, elem):
        tt = str   # default type
        if elem.attrib['type'] == 'Integer':
            tt = int
        # elif ... etc for other types

        # Conversion of the value to the type because of the later repr().
        value = tt(elem.attrib['value'])

        id_ = elem.attrib['id']

        # Produce the line of the output.
        self.out('{0} = {1}'.format(id_, repr(value)))

    def parse_execute(self, elem):
        self.out('')
        self.out('#---------- ' + elem.tag + ' ----------')
        self.loop(elem)

    def parse_if(self, elem):
        assert elem[0].tag == 'condition'
        condition = self.parse(elem[0])
        self.out('if ' + condition + ':')
        self.indent()
        self.loop(elem[1:])
        self.dedent()

    def parse_condition(self, elem):
        assert len(elem) == 0
        return elem.text

    def parse_then(self, elem):
        self.loop(elem)

    def parse_else(self, elem):
        self.dedent()
        self.out('else:')
        self.indent()
        self.loop(elem)

    def parse_error(self, elem):
        assert len(elem) == 0
        errorID = elem.attrib.get('errorID', None)
        fieldID = elem.attrib.get('fieldID', None)
        self.out('error({0}, {1})'.format(errorID, fieldID))

    def parse_setNextStage(self, elem):
        assert len(elem) == 0
        self.out('setNextStage --> ' + elem.text)

if __name__ == '__main__':
    parser = Parser()
    parser.parseXMLfile('data.xml')
    for s in parser:
        print s

rawDescription: Writing a Python tool to convert XML to Python?


code:
import re
if re.search(r'^\[city\..*?\]$', mystring) is not None:
    print 'success'

rawDescription: Python - Does the string contain a specific string within brackets?


rawDescription: Assigning values to letters and summing a sequence (not based on alphabetic order)


code:
import math

def nCr(n,r):
    f = math.factorial
    return f(n) / f(r) / f(n-r)

if __name__ == '__main__':
    print nCr(4,2)

rawDescription: Is there a math nCr function in python?

description: Is there a math nCr function in python


code:
for result in results['results']:
    print result['id']

rawDescription: Exception Value: list indices must be integers, not str


code:
def new_prettify():
    reparsed = parseString(CONTENT)
    print '\n'.join([line for line in reparsed.toprettyxml(indent=' '*2).split('\n') if line.strip()])

rawDescription: Empty lines while using minidom.toprettyxml


rawDescription: numpy - Comparing two elements of a matrix and filling in the relative value in a different position

description: Numpy - Comparing two elements of a matrix and filling in the relative value in a different position


code: print var.replace('\n', '\\n')

rawDescription: Sensing new line?

description: Sensing new line


code:
import re

str = 'ABCDX PPP [ATT:A01AD05 B01AC06 N02BA01]'
matched = re.search('([\S\s]+?)\s\[.*?(ATT:[^\]]+)', str)
if matched:
    first = matched.group(1)
    result = matched.group(2).split(' ')
    result[0:0] = [first]
    print result

rawDescription: Python regex expression and return an array


code:
import json, subprocess
file_name = u'/path/to/some/file.mp3'
command=["/path/to/ffmpeg/bin/ffprobe",
         '-print_format', 'json',
         '-v', 'quiet',
         '-show_error',
         '-show_format',
         #'-show_frames',
         #'-show_packets',
         '-show_streams',
         '-show_program_version',
         '-show_library_versions',
         '-show_versions',
         '-show_private_data',
         file_name]
process_data=subprocess.Popen(command, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
returncode = process_data.wait()
json_result=json.loads(process_data.stdout.read())
print json_result.get(u'format')

rawDescription: Can I decode this audio format?

description: Decode this audio format


code:
from lxml import etree

text = "<x>hello, <z>keep me</z> and <y>ignore me</y>, and here's some <y>more</y> text</x>"

tree = etree.fromstring(text)

print etree.tostring(tree, pretty_print=True)

# Remove the <z> tags, but keep their contents:
etree.strip_tags(tree, 'z')

print '-' * 72
print etree.tostring(tree, pretty_print=True)

# Remove all the <y> tags including their contents:
etree.strip_elements(tree, 'y', with_tail=False)

print '-' * 72
print etree.tostring(tree, pretty_print=True)

rawDescription: stripping inline tags with python's lxml

description: Stripping inline tags with python's lxml


code:
from scipy import weave

coorX = -11.8
coorY = -7.9
INC = 0.01296

code="""
int i,j, pasa = 0;
double yminf,xminc,xmaxc,ymaxf;

for (i=0; i < 1296;i++){

    yminf = coorY + INC*(i);
    ymaxf = yminf + INC;

    for (j=0; j < 1936;j++){

        xminc = coorX + INC*(j);
        xmaxc = xminc + INC;
        pasa = 1;
        break;
    }
    if (pasa == 1){
    break;
    }
}
py::tuple ret(4);
ret[0] = yminf;
ret[1] = xminc;
ret[2] = xmaxc;
ret[3] = ymaxf;
return_val = ret;
"""
yminf,xminc,xmaxc,ymaxf = weave.inline(code,['coorX','coorY','INC'])
print yminf,xminc,xmaxc,ymaxf

rawDescription: Python weave to speed up our code

description: Python weave to speed up our code


code:
a=list(range(3))
a.reverse()
print a

rawDescription: The usage of reverse() in Python

description: The usage of reverse() in Python


code:
if "":
    print "non-empty"
else:
    print "empty"

rawDescription: Problem with Python's is None test?

description: Problem with Python's is None test


code:
import collections,itertools

data = [[1, 2, 3, 13, 4, 5, 6, 7, 17],
        [1, 2, 3, 13, 4, 5, 6, 7],
        [1, 2, 3, 13, 4, 5, 6, 7, 8],
        [1, 2, 3, 13, 4, 5, 6, 7],
        [1, 2, 3, 13, 4, 5, 6, 7, 19],
        [1, 2, 3, 13, 4, 5, 6, 7]]

interscntion = set.intersection(*map(set,data))

d = collections.Counter([i for j in data for i in j if i not in list(interscntion)])
if len(set(it[1] for it in d.most_common()))>1:
    print [max(d.most_common(),key=lambda x:x[1])[0]]
else:
    print [j[0] for j in d.most_common()]

rawDescription: how to predict unique list in lists?

description: Predict unique list in lists


code:
if rank == 0:
    cb = numpy.zeros(size)
    rstat = MPI.Status()
    for i in range((size-1)*3):
        dataarr = numpy.zeros(2,dtype='i')
        comm.Recv([dataarr,MPI.INT],source=MPI.ANY_SOURCE, tag=1)
        newdeltaw = dataarr[0]
        neww = dataarr[1]
        print "newdelw is",newdeltaw,"neww is",neww
        cb[neww]=cb[neww]+newdeltaw
        print "cb=",cb

else:
    data = rank
    for i in range(3):
        senddata = numpy.array([rank,data],dtype='i')
        comm.Send([senddata, MPI.INT],dest=0,tag=1)

rawDescription: Ensuring two messages come from the same MPI task


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

rng = pd.date_range('1/1/2011 01:00:00', periods=12, freq='H')
df = pd.DataFrame(np.random.randn(12, 1), index=rng, columns=['A'])

print json.dumps(df.T.as_matrix().tolist(),indent=4)

rawDescription: Pandas Dataframe output to JSON

description: Pandas Dataframe output to JSON


code:
import mystuff

mystuff.apple()
print mystuff.tangerine

rawDescription: Python module object without a file

description: Python module object without a file


code:
def listFiles(dir):
    rootdir = dir
    for root, subFolders, files in os.walk(rootdir):
        for file in files:
            yield os.path.join(root,file)
    return

for f in listFiles(r"Your Path"):
    if f[-1].isalpha():
        os.rename(f,f[:-1]+f[-1].lower())
        print "Renamed " + f + "to" + f[:-1]+f[-1].lower()

rawDescription: How to change last letter of filename to lowercase if it is a letter?

description: Change last letter of filename to lowercase if it is a letter


code:
for row in rows:
  print 'SELECT ID FROM One.dbo.Two WHERE ID = %s' % row

rawDescription: How to strip "()," from Python, PyODBC, SQL Returns?

description: Strip "()," from Python, PyODBC, SQL Returns


code:
print re.findall(r"^(?!\w+-\s+.+$).+", text, re.MULTILINE)
['hello bye']

rawDescription: How to find lines that do not match pattern in a multiline block?

description: Find lines that do not match pattern in a multiline block


code: print utext, len(utext)

rawDescription: How to understand unicode and utf-8

description: Understand unicode and utf-8


code:
for i in range(1, max_items + 1):
    page = page_number(i, items_per_page)
    item = i - (page - 1) * items_per_page
    print 'GOTO page %s take item %s' % (page, item)

rawDescription: Iterate over double values to produce page numbers


code:
import re
zen = """Beautiful is better than ugly. \
Explicit is better than implicit. \
Simple is better than complex. \
Complex is better than complicated. \
Flat is better than nested. \
Sparse is better than dense. \
Readability counts. \
Special cases aren't special enough to break the rules. \
Although practicality beats purity. \
Errors should never pass silently. \
Unless explicitly silenced. \
In the face of ambiguity, refuse the temptation to guess. \
There should be one-- and preferably only one --obvious way to do it. \
Although that way may not be obvious at first unless you're Dutch. \
Now is better than never. \
Although never is often better than *right* now. \
If the implementation is hard to explain, it's a bad idea. \
If the implementation is easy to explain, it may be a good idea. \
Namespaces are one honking great idea -- let's do more of those!"""

searchvar = 'Dutch'
dutchre = re.compile(r"""((?:\S+\s*){,5})(%s)((?:\S+\s*){,5})""" % searchvar, re.IGNORECASE | re.MULTILINE)
print dutchre.findall(zen)
#[("obvious at first unless you're ", 'Dutch', '. Now is better than ')]

rawDescription: Python - Optimal code to find preceding and following five words from a given point in a line


rawDescription: Python 3 - Associating two lists

description: Python 3 - Associating two lists


code:
import murmur3
import timeit

# without seed
print murmur3.murmur3_x86_64('samplebias')
# with seed value
print murmur3.murmur3_x86_64('samplebias', 123)

# timing comparison with str __hash__
t = timeit.Timer("murmur3.murmur3_x86_64('hello')", "import murmur3")
print 'murmur3:', t.timeit()

t = timeit.Timer("str.__hash__('hello')")
print 'str.__hash__:', t.timeit()

rawDescription: fast, large-width, non-cryptographic string hashing in python

description: Fast, large-width, non-cryptographic string hashing in python


code:
dataset2D = [ [6, 4], [0, 0, 0, 1], [1, 0, 2, 0], [2, 2, 0, 1], [1, 1, 1, 0], [0, 0, 1, 1], [1, 0, 2, 1] ]
partition2D = [ ['A', '1', '2', '4'], ['B', '3', '5'], ['C', '6'] ]

dataset2D_size = dataset2D[0]

for partition in partition2D:
    label = partition[0]

    row_indices = [int(i) for i in partition[1:]]

    rows = [dataset2D[row_idx] for row_idx in row_indices]

    for column in zip(*rows):
        print column,
    print

rawDescription: Iterate over two nested 2D lists where list2 has list1's row numbers


code:
try: # strptime throws an exception if the input doesn't match the pattern
    datetime.datetime.strptime(userInDate, '%Y-%m-%d')
except ValueError:
    print "Invalid Input. Please try again.\n"
else:
    isValid=True
    d = userInDate

rawDescription: python - convert date input to date output without time

description: Python - convert date input to date output without time


code:
import re,ast

res = ["FAV_VENUE_CITY_NAME == 'Mumbai' & EVENT_GENRE == 'KIDS' & count_EVENT_GENRE >= 1",
"FAV_VENUE_CITY_NAME == 'Mumbai' & EVENT_GENRE == 'FANTASY' & count_EVENT_GENRE >= 1",
"FAV_VENUE_CITY_NAME =='Mumbai' & EVENT_GENRE == 'FESTIVAL' & count_EVENT_GENRE >= 1",
"FAV_VENUE_CITY_NAME == 'New Delhi' & EVENT_GENRE == 'WORKSHOP' & count_EVENT_GENRE >= 1",
"FAV_VENUE_CITY_NAME == 'Mumbai' & EVENT_GENRE == 'EXHIBITION' & count_EVENT_GENRE >= 1",
"FAV_VENUE_CITY_NAME == 'Bangalore' & FAV_GENRE == '|DRAMA|'",
"FAV_VENUE_CITY_NAME = 'Mumbai' &  & FAV_GENRE == '|ACTION|ADVENTURE|SCI-FI|'",
"FAV_VENUE_CITY_NAME == 'Bangalore' & FAV_GENRE == '|COMEDY|'",
"FAV_VENUE_CITY_NAME == 'Bangalore' & FAV_GENRE == 'DRAMA' & FAV_LANGUAGE == 'English'",
"FAV_VENUE_CITY_NAME == 'New Delhi' & FAV_LANGUAGE == 'Hindi' & count_EVENT_LANGUAGE >= 1"]

FAV_VENUE_CITY_NAME = []
EVENT_GENRE = []
FAV_GENRE = []
FAV_LANGUAGE = []
count_on_field = []

pat =[ """FAV_VENUE_CITY_NAME[\s==]+(.*?)&""","""EVENT_GENRE[\s==]+(.*?)&""","""FAV_GENRE[\s==]+(.*?)(?:\s|&|$)""","""FAV_LANGUAGE[\s==]+(.*?)(?:\s|&|$)""","""count_(\w+)"""]

def matcher(st,indx,lst):
    if re.compile(pat[indx]).search(st):
        lst.append(re.compile(pat[indx]).search(s).groups()[0].strip())
def uniquer(l):
    try:
        return map(ast.literal_eval,list(set(l)))
    except:
        return list(set(l))

for s in res:
    matcher(s,0,FAV_VENUE_CITY_NAME)
    matcher(s,1,EVENT_GENRE)
    matcher(s,2,FAV_GENRE)
    matcher(s,3,FAV_LANGUAGE)
    matcher(s,4,count_on_field)

print uniquer(FAV_GENRE),uniquer(FAV_LANGUAGE),uniquer(FAV_VENUE_CITY_NAME),uniquer(EVENT_GENRE),uniquer(count_on_field)

rawDescription: filter out some items from a list and store in different arrays in python

description: Filter out some items from a list and store in different arrays in python


code:
class Person():

    def __init__(self,name,number,email):
        self.name = name
        self.number = number
        self.email = email
        self.storage = {self.name: [self.number, self.email]}

    def getStorage(self):
        return self.storage

contact = Person('Mike','1-800-foo-spam','email@email.com')

print contact.storage
# or
print contact.getStorage()

rawDescription: How do i store multiple object attibutes to a single dictionary key?

description: Store multiple object attibutes to a single dictionary key


code:
#!/usr/bin/env python
import urllib2
import httplib
import ssl
import socket
import os

CERT_FILE = os.path.join(os.path.dirname(__file__), 'cacert.pem')

class ValidHTTPSConnection(httplib.HTTPConnection):
        "This class allows communication via SSL."

        default_port = httplib.HTTPS_PORT

        def __init__(self, *args, **kwargs):
            httplib.HTTPConnection.__init__(self, *args, **kwargs)

        def connect(self):
            "Connect to a host on a given (SSL) port."

            sock = socket.create_connection((self.host, self.port),
                                            self.timeout, self.source_address)
            if self._tunnel_host:
                self.sock = sock
                self._tunnel()
            self.sock = ssl.wrap_socket(sock,
                                        ca_certs=CERT_FILE,
                                        cert_reqs=ssl.CERT_REQUIRED)

class ValidHTTPSHandler(urllib2.HTTPSHandler):

    def https_open(self, req):
            return self.do_open(ValidHTTPSConnection, req)

opener = urllib2.build_opener(ValidHTTPSHandler)

def test_access(url):
    print "Acessing", url
    page = opener.open(url)
    print page.info()
    data = page.read()
    print "First 100 bytes:", data[0:100]
    print "Done accesing", url
    print ""

# This should work
test_access("https://www.google.com")

# Accessing a page with a self signed certificate should not work
# At the time of writing, the following page uses a self signed certificate
test_access("https://tidia.ita.br/")

rawDescription: Urllib and validation of server certificate


code:
cities.sort()
for i in range(len(cities)):
    print "(%d, '%s', 0, '%s')" % (i+1, cities[i], cities[i])

rawDescription: Python Loop for mysql statement

description: Python Loop for mysql statement


code:
lst=[{"a":2,"b":3,"c":4},{"b":4},{"b":2,"d":6},{"d":4}]

for count in range(len(lst)-1):
   for a in lst[count]:
      if a in lst[count+1]:
         print "dic"+str(count)+"\t"+str(a)+"\tis common to next dic"

rawDescription: Comparing two dictionaries in list in python


code:
for element in stack[::-1]:
    print element

rawDescription: Python Stack Iteration, best practices

description: Python Stack Iteration, best practices


code:
a = [1,2,3,4,1,2]

a.reverse()
print len(a) - a.index(min(a)) - 1

rawDescription: Python: Finding the last index of min element?


code:
import random
import time
def rand_vector(size=3):
    return [random.randint(1, 100) for _ in xrange(3)]
def rand_list(length=200):
    return [rand_vector() for _ in xrange(length)]

print "Generating lists..."
L1 = rand_list(200)
L2 = rand_list(200)

print "Running slow..."
s = time.time()
print calc_slow(L1, L2)
print "Slow for (%d, %d) took %.2fs" % (len(L1), len(L2), time.time() - s)

print "Running fast..."
s = time.time()
print calc_fast(L1, L2)
print "Fast for (%d, %d) took %.2fs" % (len(L1), len(L2), time.time() - s)

rawDescription: efficient algorithm instead of looping

description: Efficient algorithm instead of looping


code:
def f_with_return():
    return 'something'

def f_without_return():
    f_with_return()  # ignores the return value!
    print "I won't return something"

rawDescription: In python, return value only when the function is used in an assignment


code:
def insertionSort(h):
    if h == None:
        return None
    #Make the first node the start of the sorted list.
    sortedList= h
    h=h.next
    sortedList.next= None
    while h != None:
        curr= h
        h=h.next
        if curr.data<sortedList.data:
            #Advance the nodes
            curr.next= sortedList
            sortedList= curr
        else:
            #Search list for correct position of current.
            search= sortedList
            while search.next!= None and curr.data > search.next.data:
                search= search.next
            #current goes after search.
            curr.next= search.next
            search.next= curr
    return sortedList

def printList(d):
    s=''
    while d:
        s+=str(d.data)+"->"
        d=d.next
    print s[:-2]

l= unorderedList()
l.add(10)
l.add(12)
l.add(1)
l.add(4)
h= l.head
printList(h)

result= insertionSort(l.head)
d= result
printList(d)

rawDescription: How do I implement SelectionSort and InsertionSort on a linked list in Python?

description: Implement SelectionSort and InsertionSort on a linked list in Python


code:
for q in range(len(B)):
    y = scipy.spatial.distance.cdist(A,[B[q]])
    print y

rawDescription: finding the distance between a set of points using scipy.spatial.distance.cdist(X, Y) in python

description: Finding the distance between a set of points using scipy.spatial.distance.cdist(X, Y) in python


code:
for x in my_list:
    print x

rawDescription: Python truncating numbers in integer array


code:
import sys
from django.core.management.base import BaseCommand

from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer

#import thrift files here

#now define the service handler according to your thrift method declaration
class ServiceHandler:
    def __init__(self):
        pass
        #self.log = {}
    def thriftMethodName(self, arg):
        print "hello world!"
        #here you have access to anything in the django framework
        return True

class Command(BaseCommand):
    def handle(self, *args, **kwargs):
        handler = ServiceHandler()
        processor = SaleService.Processor(handler)
        transport = TSocket.TServerSocket(port=9090)
        tfactory = TTransport.TBufferedTransportFactory()
        pfactory = TBinaryProtocol.TBinaryProtocolFactory()

        server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

        # You could do one of these for a multithreaded server
        #server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
        #server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)

        self.stdout.write('Starting thrift server...')
        server.serve()
        self.stdout.write('done.')

rawDescription: Non-blocking Thrift-Server with Django

description: Non-blocking Thrift-Server with Django


code:
for check in xrange(0, 5, 2):
    print check

rawDescription: replace while loop with range function

description: Replace while loop with range function


code:
class dog:
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def print_a(self):
        print self.x.a
        print self.y.a

rawDescription: How to refer to the lower classes perameters of a class in a class in python

description: Refer to the lower classes perameters of a class in a class in python


code:
from bs4 import BeautifulSoup

data = """

    cookbook
    legal
    google
    contents

"""

soup = BeautifulSoup(data)
print [a.text for a in soup.find_all('a', href=lambda x: 'Table_of_Contents' not in x)]

rawDescription: Matching Against Unwanted Links

description: Matching Against Unwanted Links


code:
from faker.factory import Factory
import csv

def writeTo_csv(fake):
    with open('test.csv', 'wb') as csvfile:
        write_to_csv = csv.writer(csvfile, delimiter=' ', quotechar='|',     quoting=csv.QUOTE_MINIMAL)
        write_to_csv.writerow(['id,', 'TITLE,', 'FNAME,', 'SNAME,', 'GENDER,',     'ADD1,', 'ADD2,', 'ADD3,', 'ADD4,', 'PCODE,', 'PHONE,', 'MOBILE,', 'DOB'])
        firstName1 = "first_name"
        firstNameList = [(firstName1, getattr(fake, firstName1)())]
        print "%s = %s" % (firstName1, getattr(fake, firstName1)())
        print firstNameList
        write_to_csv.writerow((firstNameList[0][1],))

if __name__ == "__main__":
    fake = Factory.create('en_GB')
    writeTo_csv(fake)

rawDescription: I need some help Write random test data to csv file using faker


code: print ' ' * 10

rawDescription: Python - print "x" "y" times when passed in a list of [x,y]


code:
import time
import os
print "start (%d)" %(os.getpid())
time.sleep(10)
print "end (%d)" %(os.getpid())

rawDescription: How to spawn Python2.5 process through Apache and return immediately under Windows

description: Spawn Python2.5 process through Apache and return immediately under Windows


code:
import weakref

class Single(object):

    _instance = None

    def __init__(self):
        print "I've been born"

    def __del__(self):
        print "I'm dying"

    @classmethod
    def get_instance(cls):
        if cls._instance is not None and cls._instance() is not None:
            return cls._instance()
        instance = cls()
        cls._instance = weakref.ref(instance)
        return instance

rawDescription: How to delete object created by another module?

description: Delete object created by another module


code:
class wrapper:
    def __init__(self, myList):
        self.myList = myList

    def getArrayIterator(self):
        for i, arr in enumerate(self.myList):
            yarr = (yield arr)
            if yarr is not None:
                self.myList[i] = yarr

#set list of arrays in wrapper
myList = [1, 2, 3, 4, 5]
w = wrapper(myList)
witer = w.getArrayIterator()
try:
    a = next(witer)
    for i in range(6, 11):
        print a,
        a = witer.send(i)
except StopIteration:
    pass
print
print w.myList

rawDescription: Write access for iterators ? Or: sending values to generators of numpy Arrays


code: print '\n'.join('{}. {}'.format(i, d[k]) for (i,k) in enumerate(sorted(d), 1))

rawDescription: How to print current loop number in an iteration over a dictionary in Python?

description: Print current loop number in an iteration over a dictionary in Python


code:
# In judgemethods.py

# A global list of judge_methods
JUDGE_METHOD_LIST = []

# Your decorator to add stuff to this list
def register_judge_method(f):
    JUDGE_METHOD_LIST.append(f)
    return f

@ register_judge_method
def some_judging_method():
    print 'Judging you'

rawDescription: How can I implement a pluginable class and register in different file

description: Implement a pluginable class and register in different file


code:
import re
import collections
words = re.findall('\w+', open('1976.03.txt').read().lower())
print collections.Counter(words)

rawDescription: How can I find frequency of a specific word from document in python?

description: Find frequency of a specific word from document in python