code:
"""Password Cache

This module provides a portable interface for caching passwords.

Operations:

    Store a password.
    Retrieve a password (which may prompt for a password if it needs it).
    Test whether or not we have a password stored.
    Clear a stored password.

    Passwords are identified by a combination key app/service/user.
"""
import sys, os, random, hashlib

random.seed() # Init random number generator from other random source or system time

class PasswordCacheBase(object):
    """Base class for common functionality between different platform implementations"""
    def __init__(self, application=None):
        """PasswordCache(application)

        Creates a new store for passwords (or opens an existing one).
        The application name may be any string, but defaults to the script name.
        """
        if application is None:
            self.application = os.path.basename(sys.argv[0])
        else:
            self.application = application

    def get(self, service, user, getpass=None, cache=False):
        """Retrieve a password from the store"""
        raise NotImplementedError()

    def set(self, service, user, password):
        """Save a password in the store"""
        raise NotImplementedError()

    def exists(self, service, user):
        """Check whether a password exists"""
        try:
            pwd = self.get(service, user)
        except KeyError:
            return False
        return True

    def clear(self, service, user):
        raise NotImplementedError()

    def salt(self, service, user):
        """Get a salt value to help prevent encryption collisions. The salt string is 16 bytes long."""
        salt = hashlib.md5("%r|%s|%s|%s" % (random.random(), self.application, service, user)).digest()
        return salt

if sys.platform=="win32":
    """Interface to Windows data protection api.

    Based on code from:
    http://osdir.com/ml/python.ctypes/2003-07/msg00091.html
    """
    from ctypes import *
    from ctypes.wintypes import DWORD
    import _winreg
    import cPickle as pickle

    LocalFree = windll.kernel32.LocalFree
    # Note that CopyMemory is defined in term of memcpy:
    memcpy = cdll.msvcrt.memcpy
    CryptProtectData = windll.crypt32.CryptProtectData
    CryptUnprotectData = windll.crypt32.CryptUnprotectData

    # See http://msdn.microsoft.com/architecture/application/default.aspx?pull=/library/en-us/dnnetsec/html/SecNetHT07.asp
    CRYPTPROTECT_UI_FORBIDDEN = 0x01

    class DATA_BLOB(Structure):
        # See d:\vc98\Include\WINCRYPT.H
        # This will not work
        # _fields_ = [("cbData", DWORD), ("pbData", c_char_p)]
        # because accessing pbData will create a new Python string which is
        # null terminated.
        _fields_ = [("cbData", DWORD), ("pbData", POINTER(c_char))]

    class PasswordCache(PasswordCacheBase):
        def set(self, service, user, password):
            """Save a password in the store"""
            salt = self.salt(service, user)
            encrypted = self.Win32CryptProtectData(
                '%s' % password, salt)
            key = self._get_regkey()
            try:
                data = self._get_registrydata(key)
                data[service, user] = (salt, encrypted)
                self._put_registrydata(key, data)
            finally:
                key.Close()

        def get(self, service, user, getpass=None, cache=False):
            data = self._get_registrydata()
            try:
                salt, encrypted = data[service, user]
                decrypted = self.Win32CryptUnprotectData(encrypted, salt)
            except KeyError:
                if getpass is not None:
                    password = getpass()
                    if cache:
                        self.set(service, user, password)
                    return password
                raise
            return decrypted

        def clear(self, service=None, user=None):
            key = self._get_regkey()
            try:
                data = self._get_registrydata(key)
                if service is None:
                    if user is None:
                        data = {}
                    else:
                        for (s,u) in data.keys():
                            if u==user:
                                del data[s,u]
                else:
                    if user is None:
                        for (s,u) in data.keys():
                            if s==service:
                                del data[s,u]
                    else:
                        if (service,user) in data:
                            del data[service,user]

                self._put_registrydata(key, data)
            finally:
                key.Close()

        def _get_regkey(self):
            return _winreg.CreateKey(
                _winreg.HKEY_CURRENT_USER,
                r'Software\Python\Passwords')

        def _get_registrydata(self, regkey=None):
            if regkey is None:
                key = self._get_regkey()
                try:
                    return self._get_registrydata(key)
                finally:
                    key.Close()

            try:
                current = _winreg.QueryValueEx(regkey, self.application)[0]
                data = pickle.loads(current.decode('base64'))
            except WindowsError:
                data = {}
            return data

        def _put_registrydata(self, regkey, data):
            pickled = pickle.dumps(data)
            _winreg.SetValueEx(regkey,
                self.application,
                None,
                _winreg.REG_SZ,
                pickled.encode('base64'))

        def getData(self, blobOut):
            cbData = int(blobOut.cbData)
            pbData = blobOut.pbData
            buffer = c_buffer(cbData)
            memcpy(buffer, pbData, cbData)
            LocalFree(pbData);
            return buffer.raw

        def Win32CryptProtectData(self, plainText, entropy):
            bufferIn = c_buffer(plainText, len(plainText))
            blobIn = DATA_BLOB(len(plainText), bufferIn)
            bufferEntropy = c_buffer(entropy, len(entropy))
            blobEntropy = DATA_BLOB(len(entropy), bufferEntropy)
            blobOut = DATA_BLOB()
            # The CryptProtectData function performs encryption on the data
            # in a DATA_BLOB structure.
            # BOOL WINAPI CryptProtectData(
            #    DATA_BLOB* pDataIn,
            #    LPCWSTR szDataDescr,
            #    DATA_BLOB* pOptionalEntropy,
            #    PVOID pvReserved,
            #    CRYPTPROTECT_PROMPTSTRUCT* pPromptStruct,
            #    DWORD dwFlags,
            #    DATA_BLOB* pDataOut
            if CryptProtectData(byref(blobIn), u"win32crypto.py", byref(blobEntropy),
                None, None, CRYPTPROTECT_UI_FORBIDDEN, byref(blobOut)):
                return self.getData(blobOut)
            else:
                return None

        def Win32CryptUnprotectData(self, cipherText, entropy):
            bufferIn = c_buffer(cipherText, len(cipherText))
            blobIn = DATA_BLOB(len(cipherText), bufferIn)
            bufferEntropy = c_buffer(entropy, len(entropy))
            blobEntropy = DATA_BLOB(len(entropy), bufferEntropy)
            blobOut = DATA_BLOB()

            if CryptUnprotectData(byref(blobIn), None, byref(blobEntropy), None, None,
                CRYPTPROTECT_UI_FORBIDDEN, byref(blobOut)):
                return self.getData(blobOut)
            else:
                return None
else: # Not Windows, try for gnome-keyring
    import gtk # ensure that the application name is correctly set
    import gnomekeyring as gkey

    class Keyring(object):
        def __init__(self, name, server, protocol):
            self._name = name
            self._server = server
            self._protocol = protocol
            self._keyring = k = gkey.get_default_keyring_sync()
            import pdb; pdb.set_trace()
            print dir(k)

    class PasswordCache(PasswordCacheBase):
        def __init__(self, application=None):
            PasswordCacheBase.__init__(self, application)
            self._keyring = gkey.get_default_keyring_sync()

        def set(self, service, user, password):
            """Save a password in the store"""
            attrs = {
                "application": self.application,
                "user": user,
                "server": service,
            }
            gkey.item_create_sync(self._keyring,
                    gkey.ITEM_NETWORK_PASSWORD, self.application, attrs, password, True)

        def get(self, service, user, getpass=None, cache=False):
            attrs = {
                "application": self.application,
                "user": user,
                "server": service}
            try:
                items = gkey.find_items_sync(gkey.ITEM_NETWORK_PASSWORD, attrs)
            except gkey.NoMatchError:
                if getpass is not None:
                    password = getpass()
                    if cache:
                        self.set(service, user, password)
                    return password
                raise KeyError((service,user))
            return items[0].secret

        def clear(self, service=None, user=None):
            attrs = {'application':self.application}
            if user is not None:
                attrs["user"] = user
            if service is not None:
                attrs["server"] = service

            try:
                items = gkey.find_items_sync(gkey.ITEM_NETWORK_PASSWORD, attrs)
            except gkey.NoMatchError:
                return
            for item in items:
                gkey.item_delete_sync(self._keyring, item.item_id)

rawDescription: Storing Application Sensitive Data

description: Storing Application Sensitive Data


code:
import itertools
letters = ('',' ', 'abc','def','ghi','jkl','mno','pqrs','tuv','wxyz')
reverse_dict = dict((str(ind), group)
                    for ind,group in enumerate(letters)
                    for c in group
                    if letters)

print reverse_dict
end_nums = '3965'
print end_nums, 'can produce', tuple(''.join(w) for w in itertools.product(*map(reverse_dict.get, end_nums)))
word = 'word'
print('%r = %s' % (word,''.join(key for c in word for key, item in reverse_dict.items() if c in item)))

rawDescription: Finding All Possible Combinations of Letters using an API and Python


code:
from ctypes import c_char_p
dll.getTheString.restype = c_char_p # c_char_p is a pointer to a string
print type(dll.getTheString())

rawDescription: How does Python receive a string from a C function?


code:
from Tkinter import *

master = Tk()

var = BooleanVar()

def cb():
    print "variable is {0}".format(var.get())

c = Checkbutton(master, text="Press me", variable=var, command=cb)
c.pack()

mainloop()

rawDescription: Tkinter Checkbutton doesn't change my variable


code:
import arcpy

fc = r'C:\path\to\featureclass'

count = 1 # Start a counter

with arcpy.da.SearchCursor(fc, ["fieldA", "FieldB", "FieldC"]) as cursor:
    for row in cursor:
        # Do something...
        if count >= 2:
            print "Processing stopped because iterations >= 2"
            sys.exit()

        count = count + 1

rawDescription: How to limit for loop iterations within cursor?

description: Limit for loop iterations within cursor


code:
class Test(object):
    def testme():
        print 'no'
Test.testme()

rawDescription: Does python consider any method without cls or self arguments implicitly as static?


code:
CYRILLIC = u"абцдэфгхийклмнопрстувъыьзАБЦДЭФГХИЙКЛМНОПРСТУВЫЗ "
LATIN = u"abcdefghijklmnoprstuv$y'zABCDEFGHIJKLMNOPRSTUVYZ "

DIGRAPHS = u"шжяеёющчШЖЯЕЁЮЩЧШЖЯЕЁЮЩЧ"
LATIN_DIGRAPHS = [u'sh', u'zh', u'ja', u'je', u'jo', u'ju', u'sx', u'ch',
                  u'Sh', u'Zh', u'Ja', u'Je', u'Jo', u'Ju', u'Sx', u'Ch',
                  u'SH', u'ZH', u'JA', u'JE', u'JO', u'JU', u'SX', u'CH']

MAPPING = dict(zip(list(LATIN) + LATIN_DIGRAPHS, CYRILLIC + DIGRAPHS))
DIGRAPH_FIRST_LETTER = u'szjcSZJC'

def latin_to_cyrillic(word):
    translation = []
    possible_digraph = False

    for letter in word:
        if possible_digraph:
            combination = previous_letter + letter
            if combination in LATIN_DIGRAPHS:
                translation.append(MAPPING[combination])
                possible_digraph = False
            else:
                translation.append(MAPPING[previous_letter])
                if letter in DIGRAPH_FIRST_LETTER:
                    previous_letter = letter
                else:
                    translation.append(letter)
                    possible_digraph = False
        else:
            if letter in DIGRAPH_FIRST_LETTER:
                possible_digraph = True
                previous_letter = letter
            else:
                translation.append(MAPPING[letter])
    if possible_digraph:
        translation.append(MAPPING[previous_letter])
    return ''.join(translation)

print latin_to_cyrillic('sjo')
print latin_to_cyrillic('jojajo')

rawDescription: Python code: Transliterator fails at certain digraphs


code:
def mainWork(self, mainProject, victims):
    self.whatever = 42
def send(self):
    print self.whatever

rawDescription: transfer data between one class methods in python

description: Transfer data between one class methods in python


code:
doc = """<root>
 <district>
  <house><room><door/></room></house>
 </district>
 <district>
  <street>
   <house>and so on</house>
  </street>
 </district>
</root>"""
from xml.etree import cElementTree as ET
r = ET.XML(doc)
for house in r.findall('.//house'):
  print house, house.findall('.//door')

rawDescription: How do I create new xml from ElementTree?

description: Create new xml from ElementTree


code:
ind = something()
for field in ['id', 'name']:
    print getattr(ind, field)

rawDescription: Python + making a function more generic

description: Python + making a function more generic


code:
ep =  1412673904406

from datetime import datetime

print datetime.fromtimestamp(ep/1000).strftime("%A")
Tuesday

def ep_to_day(ep):
    return datetime.fromtimestamp(ep/1000).strftime("%A")

rawDescription: Python convert epoch time to day of the week


code:
import subprocess
import re

output = subprocess.check_output(["less","BAG_15m_kzh_2012_de.pdf"])

re_data_prefix = re.compile("^[0-9]+[.].*$")
re_data_fields = re.compile("(([^ ]+[ ]?)+)")
for line in output.splitlines():
    if re_data_prefix.match(line):
        print [l[0].strip() for l in re_data_fields.findall(line)]

rawDescription: How to scrape tables in thousands of PDF files?

description: Scrape tables in thousands of PDF files


rawDescription: Python why variable and return show difference value from print variable?


code:
import datetime

now = datetime.datetime.now()

print
print "Current date and time using str method of datetime object:"
print str(now)

print
print "Current date and time using instance attributes:"
print "Current year: %d" % now.year
print "Current month: %d" % now.month
print "Current day: %d" % now.day
print "Current hour: %d" % now.hour
print "Current minute: %d" % now.minute
print "Current second: %d" % now.second
print "Current microsecond: %d" % now.microsecond

print
print "Current date and time using strftime:"
print now.strftime("%Y-%m-%d %H:%M")

print
print "Current date and time using isoformat:"
print now.isoformat()

rawDescription: how can you print the time and date in python

description: Print the time and date in python


code:
import re
replace = re.compile(ur'најавени', flags=re.IGNORECASE | re.UNICODE)
docText = u"трендафилов во мвр се Најавени нови смени поврзани со безбедноста и борбата против организираниот криминал"
print docText
docText = replace.sub(u"најавени1",docText)
print docText

rawDescription: Regular expression unicode replace not working


code:
for char in sys.argv[1].lower():
  print nato.get(char, char) # try to get nato[char] otherwise return char

rawDescription: Pythonic mapping of an array (Beginner)

description: Pythonic mapping of an array (Beginner)


code:
Covered = '---'
Flagged = '-P-'

board = []
for x in range(12):
    board += [[Covered] * 12]

mines = set([
    (1, 12), (8, 2), (5, 5), (9, 4), (11, 11), (0, 9),
    (5, 5), (6, 7), (9, 9), (9, 10), (11, 5)
])

def count_mines(a, b):
    c = 0
    if (a - 1, b - 1) in mines: c += 1
    if (a + 0, b - 1) in mines: c += 1
    if (a + 1, b - 1) in mines: c += 1
    if (a - 1, b + 0) in mines: c += 1
    if (a + 1, b + 0) in mines: c += 1
    if (a - 1, b + 1) in mines: c += 1
    if (a + 0, b + 1) in mines: c += 1
    if (a + 1, b + 1) in mines: c += 1
    return c

def floodfill(board, row, col):

    if board[row][col] == Covered:
        count = count_mines(row, col)

        if count > 0:
            board[row][col] = ' ' + str(count) + ' '

        else:
            board[row][col] = '   '

            if row > 0:
                floodfill(board, row - 1, col)
            if row < len(board[row]) - 1:
                floodfill(board, row + 1, col)
            if col > 0:
                floodfill(board, row, col - 1)
            if col < len(board) - 1:
                floodfill(board, row, col + 1)

def show(board):
    for b in board:
        print "".join(b)

floodfill(board, 0, 0)
show(board)

rawDescription: Python: floodfill algorithm for minesweeper


code:
#!/usr/bin/python

#imported modules that are used

import csv
import glob
import os
import sys
import linecache

# Where the two arguments that are passed when running the program are stored.
input_path = sys.argv[1] #input parameter accepted when running the program
output_file = sys.argv[2] #output parameter is the name of the our put file

#The stored header for the .csv
header = [] #Where I store the header to be printed later

#Opens the output folder that will be written to with the .csv extension
with open(output_file+'.csv','wb') as outfile:
    filewriter = csv.writer(outfile)
#The header written here from the list created above ^
    filewriter.writerow(header)

#The loop for the files found from the specified input that are named with a date and eyepiece information for a name
    for input_file in glob.glob(os.path.join(input_path,'*/[0-2]*.csv')):
        with open(input_file,'rU') as csv_file:
            print "reading {:s}".format(input_file) #prints the names of the files processed to the console for a quick verification
            reader_hopefully = csv.reader(csv_file, delimiter=',', quoting=csv.QUOTE_NONE)
            next(reader_hopefully,None) #skips per line in the csv being read
            row=next(reader_hopefully,None) #the row that is actually necessary stored in a variable
            filewriter.writerow(row) #Writes necessary row to csv
            csv_file.close() #Closes open csv when finished reading it
    outfile.close()

rawDescription: looping through folder of csvs python

description: Looping through folder of csvs python


code:
def buyDialog(self):
    global current, price, qtyToBuy
    dialog = BuyDialog()
    result = dialog.exec_()

    qtyToBuy = dialog.ui.buySpinBox.value() # add this line here

    if result:
        dialogResult = (current, price, qtyToBuy)
        print dialogResult #debug
        return dialogResult

rawDescription: PyQt SpinBox always returning 0, unless previously set with setValue


code:
link = 'https://graph.facebook.com/123'
result = browser.open(link)
data = json.load(result)
print data['id']

rawDescription: How to re.search html source with regex in python

description: Re.search html source with regex in python


code:
from mako.template import Template
print Template("<%import math%>${math.log(testvar)}").render(testvar=2)

rawDescription: How to use log in Mako?

description: Use log in Mako


code:
def f():
  p = subprocess.Popen("sleep 10; echo ciao", shell=True, stdout=subprocess.PIPE)
  while True:
    r, w, x = select.select([p.stdout.fileno()],[],[],1.0)
    if r: return p.stdout.read()
    print 'not ready yet'

f()

rawDescription: Trying to call readline() on a file object in python but it's pausing


code:
with open("filename", "rtU") as f:
    columns = f.readline().strip().split(" ")
    numRows = 0
    sums = [0] * len(columns)

    for line in f:
        # Skip empty lines
        if not line.strip():
            continue

        values = line.split(" ")
        for i in xrange(len(values)):
            sums[i] += int(values[i])
        numRows += 1

    for index, summedRowValue in enumerate(sums):
        print columns[index], 1.0 * summedRowValue / numRows

rawDescription: How to find the average of multiple columns in a file using python

description: Find the average of multiple columns in a file using python


code:
print unichr(0x2588)        # or:
print u"\u2588"

rawDescription: python - >how to print a 'block' using ascii #219 in mac

description: Python - >how to print a 'block' using ascii #219 in mac


code:
class Foo          (object):
    # ^class name  #^ inherits from object

    bar = "Bar" #Class attribute.

    def __init__(self):
        #        #^ The first variable is the class instance in methods.
        #        #  This is called "self" by convention, but could be any name you want.
        #^ double underscore (dunder) methods are usually special.  This one
        #  gets called immediately after a new instance is created.

        self.variable = "Foo" #instance attribute.
        print self.variable, self.bar  #<---self.bar references class attribute
        self.bar = " Bar is now Baz"   #<---self.bar is now an instance attribute
        print self.variable, self.bar

    def method(self, arg1, arg2):
        #This method has arguments.  You would call it like this:  instance.method(1,2)
        print "in method (args):", arg1, arg2
        print "in method (attributes):", self.variable, self.bar

a = Foo() # this calls __init__ (indirectly), output:
                 # Foo bar
                 # Foo  Bar is now Baz
print a.variable # Foo
a.variable = "bar"
a.method(1, 2) # output:
               # in method (args): 1 2
               # in method (attributes): bar  Bar is now Baz
Foo.method(a,1,2) #<--- Same as a.method(1, 2).  This makes it a little more explicit what the argument "self" actually is.

class Bar(object):
    def __init__(self, arg):
        self.arg = arg
        self.Foo = Foo()

b = Bar(a)
b.arg.variable = "something"
print a.variable # something
print b.Foo.variable # Foo

rawDescription: Passing variables, creating instances, self, The mechanics and usage of classes: need explanation


code:
from scipy.sparse import coo_matrix
nele=30
nbus=40
col    = [ 2, 3, 6]
row    = [ 5, 5, 5]
val    = [ 0.1 + 0.1j, 0.1 - 0.2j, 0.1 - 0.4j]
test = coo_matrix((val, (row,col)), shape=(nele, nbus), dtype=complex)

print test.col
#[2 3 6]
print test.row
#[5 5 5]
print test.data
#[ 0.1+0.1j  0.1-0.2j  0.1-0.4j]

rawDescription: Add values to a Scipy sparse matrix with indexes and values


code:
with open("/var/log/nagios/objects.cache") as nagiosFile:
    hostgroups = {}
    hostgroup_name = None
    for line in nagiosFile.readlines():
        if "hostgroup_name" in line:
            hostgroup_name = line.split()[-1]
        if "members" in line:
            for members in line.split():
                hostgroups[hostgroup_name] = members.split(",")
    print hostgroups

rawDescription: Parsing file via Python

description: Parsing file via Python


code:
from itertools import product
rangeone = int(raw_input("Set range1: "))
rangetwo = int(raw_input("Set range2: "))

question = raw_input("Give password: ")

alphabets = 'abcdefghijklmnopqrstuvwxyz1234567890'

stop = False
for random in range(rangeone, rangetwo):
    loops = product(alphabets, repeat=random)
    for y in loops:
        password = ''.join(y)
        if password == question:
            print "done"
            stop = True
            break   # breaks the inner loop
    if stop: break  # breaks the outer loop

rawDescription: Breaking 2 for loops in Python

description: Breaking 2 for loops in Python


code:
list = ["word1", "word2", "word3"]
try:
   print list.index("word1")
except ValueError:
   print "word1 not in list."

rawDescription: Position in an list? (Python)

description: Position in an list? (Python)


code:
import urllib2
f = urllib2.urlopen('http://www.python.org/')
print f.read(100)

rawDescription: Call .php file from .py file

description: Call .php file from .py file


code:
from bs4 import BeautifulSoup as BS
s = "hello how are this is my code you"
soup = BS(s)
codetags = soup.find_all('code')
for codetag in codetags:
   codetag.extract()
print soup

rawDescription: python - remove text that is inside certain tag

description: Python - remove text that is inside certain tag


code:
from __future__ import division

import scipy.stats
from numpy import *

def normpdf(x, mu, sigma):
    u = (x-mu)/abs(sigma)
    y = (1/(sqrt(2*pi)*abs(sigma)))*exp(-u*u/2)
    return y

print scipy.stats.norm.pdf(20, 20, 10)
print normpdf(20, 20, 10)

print scipy.stats.norm.pdf(15, 20, 10)
print normpdf(15, 20, 10)

print scipy.stats.norm.pdf(10, 20, 10)
print normpdf(10, 20, 10)

rawDescription: Alternative for scipy.stats.norm.pdf?

description: Alternative for scipy.stats.norm.pdf


code:
import re, fileinput

def main():
  for line in fileinput.input():
    process = False
    for nope in ('BEGIN TRANSACTION','COMMIT',
                 'sqlite_sequence','CREATE UNIQUE INDEX'):
      if nope in line: break
    else:
      process = True
    if not process: continue
    m = re.search('CREATE TABLE "([a-z_]*)"(.*)', line)
    if m:
      name, sub = m.groups()
      line = '''DROP TABLE IF EXISTS %(name)s;
CREATE TABLE IF NOT EXISTS %(name)s%(sub)s
'''
      line = line % dict(name=name, sub=sub)
    else:
      m = re.search('INSERT INTO "([a-z_]*)"(.*)', line)
      if m:
        line = 'INSERT INTO %s%s\n' % m.groups()
        line = line.replace('"', r'\"')
        line = line.replace('"', "'")
    line = re.sub(r"([^'])'t'(.)", r"\1THIS_IS_TRUE\2", line)
    line = line.replace('THIS_IS_TRUE', '1')
    line = re.sub(r"([^'])'f'(.)", r"\1THIS_IS_FALSE\2", line)
    line = line.replace('THIS_IS_FALSE', '0')
    line = line.replace('AUTOINCREMENT', 'AUTO_INCREMENT')
    print line,

main()

rawDescription: Translating Perl to Python

description: Translating Perl to Python


code:
text = 'डिनर के बाद चहलकदमी को पारिवारिक परंपरा बनाएं'
print text
text

rawDescription: Python list printing utf-8 literals for the contained dictionary elements with HINDI text


code:
import yaml
from collections import OrderedDict
def dump_anydict_as_map(anydict):
    yaml.add_representer(anydict, _represent_dictorder)

def _represent_dictorder( self, data):
    if isinstance(data, Document):
        return self.represent_mapping('tag:yaml.org,2002:map', data.__getstate__().items())
    else:
        return self.represent_mapping('tag:yaml.org,2002:map', data.items())

class Document(object):
    def __init__(self, name):
        self.name = name
        self.otherstuff = 'blah'
    def __getstate__(self):
        d = OrderedDict()
        d['name'] = self.name
        d['otherstuff'] = self.otherstuff
        return d

dump_anydict_as_map(Document)
doc = Document('obj-20111227')
print yaml.dump(doc, indent=4)

rawDescription: Controlling Yaml Serialization Order in Python


code:
import numpy as np
for i in finalvalues.values():
    print np.percentile(map(int,i),95)

rawDescription: calculate 95 percentile of the list values in python

description: Calculate 95 percentile of the list values in python


rawDescription: Parse file in python

description: Parse file in python


code:
import threading
import requests

def download(link, filelocation):
    r = requests.get(link, stream=True)
    with open(filelocation, 'wb') as f:
        for chunk in r.iter_content(1024):
            if chunk:
                f.write(chunk)

def createNewDownloadThread(link, filelocation):
    download_thread = threading.Thread(target=download, args=(link,filelocation))
    download_thread.start()

for i in range(0,5):
    file = "C:\\test" + str(i) + ".png"
    print file
    createNewDownloadThread("http://stackoverflow.com/users/flair/2374517.png", file)

rawDescription: How to download a file over Http with multi-Thread (Asynchronous download) using Python 2.7

description: Download a file over Http with multi-Thread (Asynchronous download) using Python 2.7


code:
VOWELS = "AEIOUY"
CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"

def striped(text):

    my_sum = 0
    text = text.replace(".", " ").replace(",", " ").split()
    vowels = VOWELS.lower()
    consonants = CONSONANTS.lower()

    for word in text:
        word = word.lower()

        if ((all(c in vowels for c in list(word[::2]))\
            and all(c in consonants for c in list(word[1::2])))\
        or (all(c in consonants for c in list(word[::2]))\
            and all(c in vowels for c in list(word[1::2]))))\
        and len(word) > 1:
            print (word)
            my_sum += 1

    return my_sum

print striped("My name is")

rawDescription: List slicing issues in python

description: List slicing issues in python


code:
import csv
with open('Students.csv','r') as csvfile:
    reader = csv.DictReader(csvfile, delimiter=',', skipinitialspace=True)
    subjectcount={}
    for row in reader:
        for subject in row['Subjects'].lower().split(','):
            if subject.strip() not in subjectcount:
                subjectcount[subject.strip()] = 1
            else:
                subjectcount[subject.strip()] += 1
    for k,v in subjectcount.items():
        print k, v

rawDescription: How to calculate word frequency of a string phrase in a column of a csv file using python?

description: Calculate word frequency of a string phrase in a column of a csv file using python


code:
import os
import glob
import json
import numpy
import csv
import pickle
import nltk
import math
from nltk.corpus import stopwords
from nltk import PorterStemmer

stop = stopwords.words('english')
stemmer=PorterStemmer()
dic = {}
final = []

for name in glob.glob('/Users/jorjis/Desktop/test/*'):
    with open(name, 'r') as jfile:
        values = json.load(jfile)
    data = values['article']['body'].split('\n\n')
    desc = values['article']['description']
    title = values['article']['title']
    dic={'title': title, 'description': desc, 'paragraphs': data}
    final.append(dic)

# get json string from that dictionary
jsondata = json.dumps(final)
print jsondata

rawDescription: Save select fields from multiple json files to new json


code:
from Tkinter import Tk, Button, GROOVE

root = Tk()

def appear(index, letter):
    # This line would be where you insert the letter in the textbox
    print letter

    # Disable the button by index
    buttons[index].config(state="disabled")

letters=["A", "T", "D", "M", "E", "A", "S", "R", "M"]

# A collection (list) to hold the references to the buttons created below
buttons = []

for index in range(9):
    n=letters[index]

    button = Button(root, bg="White", text=n, width=5, height=1, relief=GROOVE,
                    command=lambda index=index, n=n: appear(index, n))

    # Add the button to the window
    button.grid(padx=2, pady=2, row=index%3, column=index/3)

    # Add a reference to the button to 'buttons'
    buttons.append(button)

root.mainloop()

rawDescription: Disabling buttons after click in Tkinter


code:
import time,os,win32api
from msvcrt import getch

def addToClipBoard(text):
    command = 'echo ' + text.strip() + '| clip'
    os.system(command)

def testpress(key):
    return (win32api.GetKeyState(key) & (1 << 7)) != 0

key = 17 #ctrl key
key2= ord('V')
copy=1

while True:
    keydown = testpress(key)
    key2down = testpress(key2)
    if keydown and key2down:
        print 'CtrlV pressed!'
        if copy==1:
          addToClipBoard('Foo')
        elif copy==2:
            addToClipBoard('Shoo')
        elif copy==3:
            addToClipBoard('THA END')
        if copy>3:
            exit(1)
        copy+=1
    time.sleep(0.10)

rawDescription: Sending Strings Queue to Clipboard in python


code:
Index_tickers = pd.read_csv('C:\\Users\\ME\\Dropbox\\MktData\\Index_list\\Index_tickers.csv')
Ticker = Index_tickers.ticker
writeHeader = True

for ticker in Index_tickers.ticker:
    index_data = pd.read_csv('C:\\Users\\ME\\Dropbox\\MktData\\Index_list\\' + ticker + '_1.csv')
    mkt_data = index_data[['Date', 'Time', 'Open', 'High', 'Low', 'Close', 'Volume']]
    numRow = 2

    while numRow < endRow:
        dOpen0 = mkt_data.ix[numRow, 'Open']
        dHigh0 = mkt_data.ix[numRow, 'High']
        dLow0 = mkt_data.ix[numRow, 'Low']
        dClose0 = mkt_data.ix[numRow, 'Close']
        dDate0 = mkt_data.ix[numRow, 'Date']
        dTime0 = mkt_data.ix[numRow, 'Time']
        dTicker0 = index_data.ix[numRow, 'Ticker']

        dHaClose0 = (dOpen0 + dHigh0 + dLow0 + dClose0) / 4
        dClose1 = mkt_data.ix[numRow -2  , 'Close']
        dOpen1 = mkt_data.ix[numRow -2  , 'Open']
        dHaOpen0 = (dClose1 + dOpen1) / 2
        dHaHigh0 = max(dHigh0, dHaOpen0, dHaClose0)
        dHaLow0 = min(dLow0, dHaOpen0, dHaClose0)

        dHaGreen0 = dHaClose0 > dHaOpen0
        dHaRed0 = dHaClose0 < dHaOpen0
        dNumRow = numRow
        numRow = numRow + 1
        df = pd.DataFrame({'numRow' : pd.Series(dNumRow), 'Time' : pd.Series(dTime0), 'Date' : pd.Series(dDate0), 'Ticker' : pd.Series(dTicker0), 'Open0' : pd.Series(dOpen0), 'High0' : pd.Series(dHigh0), 'Low0' : pd.Series(dLow0), 'Close0' : pd.Series(dClose0)})
        #print df
        if writeHeader:
            df.to_csv('C:\Users\\ME\\Dropbox\\MktData\HaDetail.csv')
            writeHeader = False
        else:
            df.to_csv('C:\Users\\ME\\Dropbox\\MktData\HaDetail.csv', header=False, mode='a')

rawDescription: saving dataframe to CSV in python 2

description: Saving dataframe to CSV in python 2


code:
import bs4

soup = bs4.BeautifulSoup('<td align="left" class="foo" style="white-space:nowrap;">  </td>')
for td in soup.findAll('td', {'class': 'foo'}):
    td.attrs['title'] = 'spam'
    td.append('egg text')
print soup

rawDescription: BeautifulSoup - Insert text/var into every given td class


code:
# test_client.py
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2

# Register the streaming http handlers with urllib2
register_openers()

# Start the multipart/form-data encoding of the file "DSC0001.jpg"
# "image1" is the name of the parameter, which is normally set
# via the "name" parameter of the HTML <input> tag.

# headers contains the necessary Content-Type and Content-Length
# datagen is a generator object that yields the encoded parameters
datagen, headers = multipart_encode({"image1": open("DSC0001.jpg")})

# Create the Request object
request = urllib2.Request("http://localhost:5000/upload_image", datagen, headers)
# Actually do the request, and get the response
print urllib2.urlopen(request).read()

rawDescription: Using MultipartPostHandler to POST form-data with Python


code:
import sys
import termios
import tty

attr = termios.tcgetattr( sys.stdin )
tty.setraw( sys.stdin )

for i in range(16) :
    d = sys.stdin.read(1)
    print ord( d[0] )
    print "\r",

termios.tcsetattr( sys.stdin, termios.TCSADRAIN, attr )

rawDescription: How to return a character from standard input on OS X in Python?

description: Return a character from standard input on OS X in Python


code:
import re

data = """
fault-filtertime 5
foo
bar
baz
! TH
"""

regex_str = "fault-filtertime 5(.*?)! TH"
regex = re.compile(regex_str, re.DOTALL)
result = re.search(regex, data)
print result.group(1)

rawDescription: Regex does not match the intended line

description: Regex does not match the intended line


code:
with open('input1.txt', 'rb') as file1:
    file1_data = dict(line.split(None, 2)[1::-1] for line in file1 if line.strip())
with open('input2.txt', 'rb') as file2, open('output.txt', 'wb') as outputfile:
    output = csv.writer(outputfile, delimiter='|')
    for line in file2:
        row = line.strip().split('|')
        key, n = row[0], ' '+row[1].split(' ')[1]
        if key in file1_data:
            row = [row[0], row[1][:row[1].find(' ')], file1_data[key] + n]
            output.writerow(row)
            print row

rawDescription: Comparing two text files not giving required result


code:
import xlrd

book_input = xlrd.open_workbook('input.xls')
sheet_input = book_input.sheet_by_index(0)

for row in xrange(sheet_input.nrows):
    for col in xrange(sheet_input.ncols):
        cell_type = sheet_input.cell_type(row, col)
        cell_value = sheet_input.cell_value(row, col)
        if cell_type == 1 or (cell_type == 2 and cell_value >= 1):
            print "%s:%s" % (col + 1, cell_value)

rawDescription: Python Excel: How to add a cell value and a constant and then print back to the excel


code:
import os
top='/home/aix'
for root, dirs, files in os.walk(top, topdown=True):
  print root
  dirs.sort(reverse=True)

rawDescription: os.walk() in reverse?

description: Os.walk() in reverse


code:
import sys
from functools import wraps
from .util_iter import isiterable
from .util_print import Indenter

IGNORE_EXC_TB = not '--noignore-exctb' in sys.argv

def ignores_exc_tb(func):
    """ decorator that removes other decorators from traceback """
    if IGNORE_EXC_TB:
        @wraps(func)
        def wrapper_ignore_exctb(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except Exception:
                # Code to remove this decorator from traceback
                exc_type, exc_value, exc_traceback = sys.exc_info()
                # Remove two levels to remove this one as well
                raise exc_type, exc_value, exc_traceback.tb_next.tb_next
        return wrapper_ignore_exctb
    else:
        return func

def indent_decor(lbl):
    def indent_decor_outer_wrapper(func):
        @ignores_exc_tb
        @wraps(func)
        def indent_decor_inner_wrapper(*args, **kwargs):
            with Indenter(lbl):
                return func(*args, **kwargs)
        return indent_decor_inner_wrapper
    return indent_decor_outer_wrapper

def indent_func(func):
    @wraps(func)
    @indent_decor('[' + func.func_name + ']')
    @ignores_exc_tb
    def wrapper_indent_func(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper_indent_func

def accepts_scalar_input(func):
    '''
    accepts_scalar_input is a decorator which expects to be used on class methods.
    It lets the user pass either a vector or a scalar to a function, as long as
    the function treats everything like a vector. Input and output is sanatized
    to the user expected format on return.
    '''
    @ignores_exc_tb
    @wraps(func)
    def wrapper_scalar_input(self, input_, *args, **kwargs):
        is_scalar = not isiterable(input_)
        if is_scalar:
            iter_input = (input_,)
        else:
            iter_input = input_
        result = func(self, iter_input, *args, **kwargs)
        if is_scalar:
            result = result[0]
        return result
    return wrapper_scalar_input

def accepts_scalar_input_vector_output(func):
    '''
    accepts_scalar_input is a decorator which expects to be used on class
    methods.  It lets the user pass either a vector or a scalar to a function,
    as long as the function treats everything like a vector. Input and output is
    sanatized to the user expected format on return.
    '''
    @ignores_exc_tb
    @wraps(func)
    def wrapper_vec_output(self, input_, *args, **kwargs):
        is_scalar = not isiterable(input_)
        if is_scalar:
            iter_input = (input_,)
        else:
            iter_input = input_
        result = func(self, iter_input, *args, **kwargs)
        if is_scalar:
            if len(result) != 0:
                result = result[0]
        return result
    return wrapper_vec_output

rawDescription: Can decorators be removed from Python Exception tracebacks?


code:
import operator
ops = { 'or': operator.or_, 'and': operator.and_ }
print ops[op](True, False)

rawDescription: Alternative to eval in Python

description: Alternative to eval in Python


code:
def hello(x):
    global SHOULD_REPEAT
    print "hello"
    if SHOULD_REPEAT:
        root.after(10, hello) # wait 10ms then repeat

rawDescription: How can I control keyboard repeat delay in a Tkinter root window?

description: Control keyboard repeat delay in a Tkinter root window


code:
from __future__ import with_statement
import math

def get_stats(profession, salaries):
  n = float(len(salaries))
  mean = sum(salaries)/n
  stdev = 0
  for value in salaries:
    stdev += (value - mean)**2
  stdev = math.sqrt(stdev/(n))
  print profession, min(salaries), max(salaries), mean, stdev

with open('salaries.csv') as f:
  f.readline()
  # Create the list of salaries
  salaries = {}
  for line in f.readlines():
    country, profession, value = line.split(',')
    value = int(value.strip())
    profession = profession.strip()
    if salaries.has_key(profession):
        salaries[profession].append(value)
    else:
        salaries[profession] = [value]
  for k,v in salaries.items():
    get_stats(k,v)

rawDescription: Find the Standard Deviation from a CSV File using Python


code:
import numpy as np

# Generate nested list a.
N, M = 10, 7  # number of sublists and length of each sublist
a = np.random.rand(N, M)

# x,y indexes of max float in a.
print np.unravel_index(a.argmax(), a.shape)

A = np.full_like(a, -np.inf)  # use np.inf if doing np.argmin
A[3:5, 2:6] = a[3:5, 2:6]
np.unravel_index(A.argmax(), A.shape)

rawDescription: Getting index of maximum value in nested list


code:
a = [1,2,3,4,'']
print sum(x if not isinstance(x,str) else 0 for x in a)

rawDescription: Sum a python list without the string values


code:
from docutils.core import publish_doctree

source = """Some text ...

:foo: bar

Some text ...
"""

# Parse reStructuredText input, returning the Docutils doctree as
# an `xml.dom.minidom.Document` instance.
doctree = publish_doctree(source).asdom()

# Get all field lists in the document.
fields = doctree.getElementsByTagName('field')

d = {}

for field in fields:
    # I am assuming that `getElementsByTagName` only returns one element.
    field_name = field.getElementsByTagName('field_name')[0]
    field_body = field.getElementsByTagName('field_body')[0]

    d[field_name.firstChild.nodeValue] = \
        " ".join(c.firstChild.nodeValue for c in field_body.childNodes)

print d # Prints {u'foo': u'bar'}

rawDescription: Extract field list from reStructuredText


code:
import xml.etree.ElementTree as ET
root = ET.ElementTree(file="yourxml.xml")
RSAPublicKey = root.findall("./RSAPublicKey") # this search all RSAPublickey
for item in RSAPublicKey :
    Modulus= item .findall("./Modulus") #in rsapublic key modulus
    print Modulus

rawDescription: extract modulus and exponent from public key xml in python

description: Extract modulus and exponent from public key xml in python


code:
import multiprocessing as mp
import time
import sys

def foo(x):
    time.sleep(x)
    return x*x

pool = mp.Pool(1)

for x in (0.01, 1.0):
    try:
        result = pool.apply_async(foo, args = (x,)).get(timeout = 0.1)
    except KeyboardInterrupt:
        pool.terminate()
        sys.exit("Cancelled")
    except mp.TimeoutError:
        print('Timed out')
    else:
        print "Result: {r}".format(r = result)

rawDescription: Sandbox shell programs on time

description: Sandbox shell programs on time


code:
from ctypes import *
from ctypes.wintypes import *

#Constants

#For touchMask
TOUCH_MASK_NONE=          0x00000000 #Default
TOUCH_MASK_CONTACTAREA=   0x00000001
TOUCH_MASK_ORIENTATION=   0x00000002
TOUCH_MASK_PRESSURE=      0x00000004
TOUCH_MASK_ALL=           0x00000007

#For touchFlag
TOUCH_FLAG_NONE=          0x00000000

#For pointerType
PT_POINTER=               0x00000001#All
PT_TOUCH=                 0x00000002
PT_PEN=                   0x00000003
PT_MOUSE=                 0x00000004

#For pointerFlags
POINTER_FLAG_NONE=        0x00000000#Default
POINTER_FLAG_NEW=         0x00000001
POINTER_FLAG_INRANGE=     0x00000002
POINTER_FLAG_INCONTACT=   0x00000004
POINTER_FLAG_FIRSTBUTTON= 0x00000010
POINTER_FLAG_SECONDBUTTON=0x00000020
POINTER_FLAG_THIRDBUTTON= 0x00000040
POINTER_FLAG_FOURTHBUTTON=0x00000080
POINTER_FLAG_FIFTHBUTTON= 0x00000100
POINTER_FLAG_PRIMARY=     0x00002000
POINTER_FLAG_CONFIDENCE=  0x00004000
POINTER_FLAG_CANCELED=    0x00008000
POINTER_FLAG_DOWN=        0x00010000
POINTER_FLAG_UPDATE=      0x00020000
POINTER_FLAG_UP=          0x00040000
POINTER_FLAG_WHEEL=       0x00080000
POINTER_FLAG_HWHEEL=      0x00100000
POINTER_FLAG_CAPTURECHANGED=0x00200000

#Structs Needed

class POINTER_INFO(Structure):
    _fields_=[("pointerType",c_uint32),
              ("pointerId",c_uint32),
              ("frameId",c_uint32),
              ("pointerFlags",c_int),
              ("sourceDevice",HANDLE),
              ("hwndTarget",HWND),
              ("ptPixelLocation",POINT),
              ("ptHimetricLocation",POINT),
              ("ptPixelLocationRaw",POINT),
              ("ptHimetricLocationRaw",POINT),
              ("dwTime",DWORD),
              ("historyCount",c_uint32),
              ("inputData",c_int32),
              ("dwKeyStates",DWORD),
              ("PerformanceCount",c_uint64),
              ("ButtonChangeType",c_int)
              ]

class POINTER_TOUCH_INFO(Structure):
    _fields_=[("pointerInfo",POINTER_INFO),
              ("touchFlags",c_int),
              ("touchMask",c_int),
              ("rcContact", RECT),
              ("rcContactRaw",RECT),
              ("orientation", c_uint32),
              ("pressure", c_uint32)]

#Initialize Pointer and Touch info

pointerInfo=POINTER_INFO(pointerType=PT_TOUCH,
                         pointerId=0,
                         ptPixelLocation=POINT(950,540))

touchInfo=POINTER_TOUCH_INFO(pointerInfo=pointerInfo,
                             touchFlags=TOUCH_FLAG_NONE,
                             touchMask=TOUCH_MASK_ALL,
                             rcContact=RECT(pointerInfo.ptPixelLocation.x-5,
                                  pointerInfo.ptPixelLocation.y-5,
                                  pointerInfo.ptPixelLocation.x+5,
                                  pointerInfo.ptPixelLocation.y+5),
                             orientation=90,
                             pressure=32000)

def makeTouch(x,y,fingerRadius):
    touchInfo.pointerInfo.ptPixelLocation.x=x
    touchInfo.pointerInfo.ptPixelLocation.y=y

    touchInfo.rcContact.left=x-fingerRadius
    touchInfo.rcContact.right=x+fingerRadius
    touchInfo.rcContact.top=y-fingerRadius
    touchInfo.rcContact.bottom=y+fingerRadius

    #Initialize Touch Injection
    if (windll.user32.InitializeTouchInjection(1,1) != 0):
        print "Initialized Touch Injection"

    #Press Down
    touchInfo.pointerInfo.pointerFlags=(POINTER_FLAG_DOWN|
                                        POINTER_FLAG_INRANGE|
                                        POINTER_FLAG_INCONTACT)

    if (windll.user32.InjectTouchInput(1, byref(touchInfo))==0):
        print "Failed with Error: "+ FormatError()

    else:
        print "Touch Down Succeeded!"

    #Pull Up
    touchInfo.pointerInfo.pointerFlags=POINTER_FLAG_UP

    if (windll.user32.InjectTouchInput(1,byref(touchInfo))==0):
        print "Failed with Error: "+FormatError()

    else:
        print "Pull Up Succeeded!"

    return

#Ex:
#makeTouch(950,270,5)

rawDescription: Emulate Touch Event in Windows 8 using Python


code:
#as example to be short enough
command = "meme subset_{filenum}.fas > subset_{filenum}.MEME"

for i in range(100):
    param={"filenum":i} #this is used to replace {filenum} with i in comnmand
                        #param is dict with string as key
    command_t=command.format(**param)
    print command_t
    subprocess.call(command_t,shell=True)

rawDescription: running 100 files using a single command with subprocess

description: Running 100 files using a single command with subprocess


code:
import urllib,re
siteContent  = urllib.urlopen("http://site.com").read()
GetBoldWords = re.findall(r"<b>[\w\d ]+",siteContent)
print "Bold Words are :"
print getBoldWords

rawDescription: Identify Webpage

description: Identify Webpage


code:
import IPython.ipapi
ip = IPython.ipapi.get()

def run_this_plot(self, arg_s=''):
    """ Run
    Examples
    In [1]: import demo
    In [2]: rtp x y <z>
    Where x, y, and z are numbers of any type
    """
    args = []
    for arg in arg_s.split():
        try:
            args.append(self.shell.user_ns[arg])
        except KeyError:
            raise ValueError("Invalid argument: %r" % arg)
    print "args: ", args
    # Do something here with args, such as plot them

# Activate the extension
ip.expose_magic("rtp", run_this_plot)

rawDescription: Use variables from iPython in a script

description: Use variables from iPython in a script


code:
import signal,sys
def signal_handling(signum,frame):
    print "you chose to end the program"
    sys.exit()

signal.signal(signal.SIGINT,signal_handling)
while True:
    pass

rawDescription: Python- How to check if program gets aborted by user while running?


code:
import random

def guess_func():
    number = random.randint(1, 100)
    guesses = 0
    while True:
        guess = int(raw_input("Enter your guess: "))
        if guess < number:
            print('Your guess is too low.')
            guesses += 1

        if guess > number:
            print('Your guess is too high.')
            guesses += 1

        if guess == number:
            print "Congratulations! You guessed my number in [{}] guesses".format(guesses)
            print "Let's keep on guessing!"
            return guess_func()

guess_func()

rawDescription: Python random number generator

description: Python random number generator


code:
class Node(object):
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None

def depth(root, key, current_depth=0):
    if not root:
        return -1
    elif root.key == key:
        return current_depth
    elif key < root.key:
        return depth(root.left, key, current_depth + 1)
    else:
        return depth(root.right, key, current_depth + 1)

root = Node(3)
root.left = Node(2)
root.left.left = Node(1)

for i in xrange(1, 4):
    print 'key: {0}, depth: {1}'.format(i, depth(root, i))

# key: 1, depth: 2
# key: 2, depth: 1
# key: 3, depth: 0

rawDescription: Code different for Depth and Height - Binary Search Tree (Python)


code:
# Read the file into a dictionary
grade_book = {}
File = open('Test.txt', 'r')
for line in File:
    name, scores = line.split(':')
    grade_book[name] = scores.strip()
File.close()
print grade_book

# Accept scores
name = raw_input("Name: ")
while name != "":
    correct = raw_input("Number: ")
    if name in grade_book.keys():
        grade_book[name] += ',' + correct
    else:
        grade_book[name] = correct

    name = raw_input("Name: ")

# Write dictionary back to the file
File = open('Test.txt', 'w')
for name, scores in grade_book.items():
    out_line = name + ':' + scores + "\n"
    File.write(out_line)

File.close()

rawDescription: Python: Using an input to determinate what line to save on


code:
csv=[(1,1,"One","Un"),(1,2,"Two","Deux"),(2,1,"Three","Trois"),(2,1,"Four","Quatre")]
csvRdd=sc.parallelize(csv)
# Step 1
csvById12Rdd=csvRdd.map(lambda (id1,id2,value1,value2): ((id1,id2),(value1,value2))).groupByKey()
# Step 2
csvById1Rdd=csvById12Rdd.map(lambda ((id1,id2),group):(id1, (id2,group))).groupByKey()
# Print
def printit(one):
    id1, twos=one
    print("Id1:{}".format(id1))
    for two in twos:
        id2, values=two
        print("Id1:{} Id2:{}".format(id1,id2))
        for value1,value2 in values:
            print("Id1:{} Id2:{} Values:{} {}".format(id1,id2,value1,value2))

csvById1Rdd.foreach(printit)

rawDescription: cant perform 2 succesive groupBy in spark

description: Cant perform 2 succesive groupBy in spark


code:
import lxml.html
tree = lxml.html.parse('test.html')
for node in tree.xpath('/html/body/div/table[2]/tbody/tr/td[2]'):
    print node.text

rawDescription: what is the simplest way to get element content by XPath with Python?

description: What is the simplest way to get element content by XPath with Python


code:
with open('input.txt', 'w') as f:
  for i in xrange(998):
    f.write(uuid.uuid4().get_hex() + '\n')

from itertools import groupby, count
with open('input.txt', 'r') as f:
    samples = groupby(f, key=lambda k, line=count(): next(line)//100)
    for i in samples:
      print i

rawDescription: Split data into chunks and add numbers to results


code:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.dates as dates
import datetime, random
import matplotlib.ticker as ticker

def random_date():
      date = datetime.date(2008, 12,01)
      while 1:
        date += datetime.timedelta(days=30)
        yield (date)

def format_date(x, pos=None):
     return dates.num2date(x).strftime('%Y-%m-%d') #use FuncFormatter to format dates

r_d = random_date()
some_dates = [dates.date2num(r_d.next()) for i in range(0,20)]

fig = plt.figure()
ax = Axes3D(fig,rect=[0,0.1,1,1]) #make room for date labels

for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
    xs = np.array(some_dates)
    ys = np.random.rand(20)
    ax.bar(xs, ys, zs=z, zdir='y', color=c, alpha=0.8,width=8)

ax.w_xaxis.set_major_locator(ticker.FixedLocator(some_dates)) # I want all the dates on my xaxis
ax.w_xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
for tl in ax.w_xaxis.get_ticklabels(): # re-create what autofmt_xdate but with w_xaxis
       tl.set_ha('right')
       tl.set_rotation(30)

ax.set_ylabel('Series')
ax.set_zlabel('Amount')

plt.show()

rawDescription: Matplotlib: Formatting dates on the x-axis in a 3D Bar graph


code:
def crypto_aead_chacha20poly1305_encrypt(message,
                                         ad,
                                         nonce,
                                         key):

    mlen  = ctypes.c_ulonglong(len(message))
    adlen = ctypes.c_ulonglong(len(ad))

    c    =  ctypes.create_string_buffer(mlen.value+16L)
    clen  = ctypes.c_ulonglong(0)

    sodium.crypto_aead_chacha20poly1305_encrypt(c,
                                                ctypes.byref(clen),
                                                message,
                                                mlen,
                                                ad,
                                                adlen,
                                                None,
                                                nonce,
                                                key)
    return c.raw

rawDescription: Python Ctypes with RaspberryPi

description: Python Ctypes with RaspberryPi


code:
try:
    string.decode('utf-8')
    print "string is UTF-8, length %d bytes" % len(string)
except UnicodeError:
    print "string is not UTF-8"

rawDescription: Test a string if it's Unicode, which UTF standard is and get its length in bytes?


code:
STARTER = "Hello World"
FILENAME = "data.txt"
TARGET = "Blah="

with open(FILENAME) as f:
    value = None
    start_seen = False
    for line in f:
        if line.strip() == STARTER:
            start_seen = True
            continue

        if TARGET in line and start_seen:
            _,value = line.split('=')
            break

if value is not None:
    print "Got value %d" % int(value)
else:
    print "Nothing found"

rawDescription: Python: Get/Scan All Text After a Certain String


code:
class FunctionalTest(Test):
  def get(self, *args, **kw):
    self.response = app.get(*args, **kw)

  def post(self, *args, **kw):
    self.response = app.post(*args, **kw)

  def assert_response(self, expected):
    pattern = str(expected) if re.search(r'^\d+$', expected) \
                            else (r'^\d+ %s' % expected)
    assert re.search(pattern, self.response.status, re.IGNORECASE), \
           'Response status was not "%s": %s' % (expected, self.response.status)

  def assert_xpath(self, path, expected):
    element = ElementTree.fromstring(self.response.body)
    found_nodes = element.findall('.' + path)
    if type(expected) is int:
      assert_equal(expected, len(found_nodes))
    elif type(expected) is str or type(expected) is unicode:
      assert (True in [(node.text == expected) for node in found_nodes])
    else:
      raise Exception, "Unknown expected value: %r" % type(expected)

rawDescription: How do I go about setting up a TDD development process with Google App Engine?

description: Go about setting up a TDD development process with Google App Engine


code:
import PyQt4.QtGui as gui

app = gui.QApplication([])

w = gui.QWidget()
l = gui.QHBoxLayout(w)
w.setLayout(l)

lis1 = gui.QListWidget()
lis2 = gui.QListWidget()

lis1.addItems(["1","2","3"])
lis2.addItems(["4","5","6"])

def f():
    lis2.itemSelectionChanged.disconnect(g)
    for item in lis2.selectedItems():
        lis2.setItemSelected(item,False)
    lis2.itemSelectionChanged.connect(g)

def g():
    lis1.itemSelectionChanged.disconnect(f)
    for item in lis1.selectedItems():
        lis1.setItemSelected(item,False)
    lis1.itemSelectionChanged.connect(f)

print dir(lis1.itemSelectionChanged)

lis1.itemSelectionChanged.connect(f)
lis2.itemSelectionChanged.connect(g)

l.addWidget(lis1)
l.addWidget(lis2)

w.show()

app.exec_()

rawDescription: Enable a single selection between 2 Lists


code:
from collections import OrderedDict
board = OrderedDict([("1", [a,a,a,a,a,a]), ("2", [a,a,a,a,a,a])]) # continued for other keys

for k,v in board.items():
    print k,v

rawDescription: Trying to print multiple lines in a python dictionary in order


code:
import os
media_file_name = "media.txt"
media_file = open(media_filename)
file_list = media_file.readline()  # assuming its all csv on one line
for check_file in file_list.split(','):
    if os.path.isfile(check_file):
         print "%s is in place, good" % check_file
    else:
         print "%s is supposed to be here, but isn't" % check_file

rawDescription: How to compare directory listing against csv list?

description: Compare directory listing against csv list


code:
import time

print time.strftime('%I:%M:%S %p %Z')

rawDescription: Access Real Time on Computer

description: Access Real Time on Computer


code: print open('roger.txt').read()

rawDescription: Python Open a User Created Text File

description: Python Open a User Created Text File


code:
firstname = ("John", "Jake")
lastname = ("Smith", "Jones")
result = []
for firstname, lastname in zip(firstname, lastname):
    result.append('{}{}'.format(lastname[:2], firstname[0].lower()))
print result

rawDescription: Taking two lists and printing specific characters based on their postition


code:
if not args.input or not args.output:
        print 'Incorrect number of params'
        exit()
else:
        print "Correct number of params"

rawDescription: Understanding argument parsing with argparse in Python


code:
s = "A & B"
import re
matched = re.search(r'&\w+;', s)
if matched:
    print "escape char '{0}' found in string '{1}'".format(matched.group(), s)

rawDescription: Checking a python string for escaped characters


code:
import collections

the_input = raw_input("Enter numbers... ")
the_list = [int(x) for x in the_input.strip("[]").split(",")]
group_counter = collections.Counter(x//10 for x in the_list)
bin_range = xrange(min(group_counter.keys()), max(group_counter.keys()) + 1)
for bin_tens in bin_range:
    print "There were {} in {} to {}".format(group_counter[bin_tens], bin_tens*10, bin_tens*10+9)

rawDescription: Input integers to be counted by arrays or sections


code:
import sys, os

from waflib.Task import Task
from waflib.TaskGen import feature

class run_make(Task):
    def run(self):
        ret = self.exec_command(
                '%s; %s; %s' % (self.env.ACLOCAL, self.env.AUTOCONF, self.env.AUTOMAKE),
                cwd = self.cwd, stdout=sys.stdout)
        if ret != 0:
            return ret
        ret = self.exec_command('./configure', cwd = self.cwd, stdout=sys.stdout)
        if ret != 0:
            return ret
        ret = self.exec_command('make install -i', cwd=self.cwd, stdout=sys.stdout)
        self.exec_command('cp src/libsystemc.a %s' % self.outputs[0].abspath(), cwd=self.cwd)
        print "COPY"
        return ret

@feature("autotools_make")
def autotools_make(self):
    self.link_task = self.create_task('run_make',
            self.path.get_src().ant_glob('src/**/*.(cpp|h)'),
            [ self.path.get_bld().find_or_declare('libsystemc.a') ])
    self.link_task.cwd = self.path.get_src().abspath()
    self.target = self.name

def options(opt):
    opt.load('compiler_cxx')

def configure(cfg):
    cfg.load('compiler_cxx')

    cfg.find_program('aclocal',  mandatory=True)
    cfg.find_program('autoconf', mandatory=True)
    cfg.find_program('automake', mandatory=True)

def spring_cleaning(bld):
    cwd = bld.path.get_src().abspath()
    bld.exec_command('make clean', cwd=cwd)
    bld.exec_command('make uninstall', cwd=cwd)
    bld.exec_command('find . -name "*.a" -exec rm -f {} \;', cwd=cwd)
    bld.exec_command('rm -rf aclocal.m4 Makefile Makefile.in configure *.log', cwd=cwd)

def build(bld):
    bld(name='systemc',
        features='autotools_make')

    if bld.cmd == 'clean':
        spring_cleaning(bld)

rawDescription: Is it possible to compile library with waf using make install?


code:
# File: readline-example-2.py

class Completer:
    def __init__(self, words):
        self.words = words
        self.prefix = None
    def complete(self, prefix, index):
        if prefix != self.prefix:
            # we have a new prefix!
            # find all words that start with this prefix
            self.matching_words = [
                w for w in self.words if w.startswith(prefix)
                ]
            self.prefix = prefix
        try:
            return self.matching_words[index]
        except IndexError:
            return None

import readline

# a set of more or less interesting words
words = "perl", "pyjamas", "python", "pythagoras"

completer = Completer(words)

readline.parse_and_bind("tab: complete")
readline.set_completer(completer.complete)

# try it out!
while 1:
    print repr(raw_input(">>> "))

rawDescription: Python user input replay

description: Python user input replay


code:
if eod in ("Encrypt", "encrypt", "e", "E"):
    print "Enter your message here:"
    emessage_in = raw_input("> ")
    elength = len(emessage_in)
    elistm = list(emessage_in)
    emessage_out = '' # initialize an empty string
    for x in range(0, elength):
        e1[x] = elistm[x]
        e2[x] = ord(e1[x])
        e3[x] = 158 - e2[x]
        emessage_out += chr(e3[x]) # concatenate each character onto that string
        count = 0
    print emessage_out # move this out of the loop
elif eod in ("Decrypt", "dencrypt", "d", "D"):
    print "Enter your message here:"
    dmessage_in = raw_input("> ")
    dlength = len(dmessage_in)
    dlistm = list(dmessage_in)
    dmessage_out = '' # initialize an empty string
    for y in range(0, dlength):
        d3[y] = dlistm[y]
        d2[y] = ord(d3[y])
        d1[y] = 158 - d2[y]
        dmessage_out += chr(d1[y]) # concatenate each character onto that string
        count = 0
    print dmessage_out # move this out of the loop

rawDescription: I need to remove whitespace in this program in order for encryption/decryption to work


code:
def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        print "returning FORWARDED_FOR"
        ip = x_forwarded_for.split(',')[-1].strip()
    elif request.META.get('HTTP_X_REAL_IP'):
        print "returning REAL_IP"
        ip = request.META.get('HTTP_X_REAL_IP')
    else:
        print "returning REMOTE_ADDR"
        ip = request.META.get('REMOTE_ADDR')
    return ip

rawDescription: Django : Get user IP from development server


code:
#!/usr/bin/env python
from multiprocessing import Process
import os
import time
import datetime
def sleeper(name, seconds):
   time.sleep(seconds)
   print "PNAME:- %s"%name

if __name__ == '__main__':
   pros={}
   processes=[]
   i=0
   time2=0
   time1=datetime.datetime.now()
   for sec in range(5):
        flag=0
        while flag!=1:
                time2=datetime.datetime.now()
                if (time2-time1).seconds==1:
                        time1=time2
                        flag=1
                        print "Executing Per second"
                        for no in range(5):
                                i+=1
                                pros[i] = Process(target=sleeper, args=("Thread-%d"%i, 1))
                        j=i-5
                        for no in range(5):
                                j+=1
                                pros[j].start()
                        j=i-5
                        for no in range(5):
                                j+=1
                                processes.append(pros[j])
   for p in processes:
        p.join()

rawDescription: executing specific statement at a given rate in python

description: Executing specific statement at a given rate in python


code:
from django.db import connection

with connection.cursor() as cursor:
    cursor.execute('your_sql_here')
    for row in cursor.fetchall():
        print 'row fields:', row[0], row[1], row[3]

rawDescription: Sql select count to python code

description: Sql select count to python code


code:
for link in hxs.select('//a/@href').extract():
    url = link
    if not link.startswith('http://') and not link.startswith('#'):
        url = (self.start_urls[0] + link).replace('//','/')

    print url
    yield Request(url, callback=self.suppose_to_parse)

rawDescription: scrapy unable to make Request() callback

description: Scrapy unable to make Request() callback


code:
import re

def find_pair(x, y, mystring):
    return re.findall(r'\(\s*?%d,\s*?%d\s*?\)+' % (x, y), mystring);

print find_pair(2, 4, "(3,4) , (2,   4),(5,4), (2,3), ")

rawDescription: Searching (x,y) pair in sequence

description: Searching (x,y) pair in sequence


code:
import re
testString = r'this, is a test "one" it should only return the first item \"two\" and not the second'
pattern = r'(?<!\\)"([^"]*)(?<!\\)"'
          # ^^^^^^^        ^^^^^^^
print re.findall(pattern, testString)

rawDescription: Finding items in quotes, but not escaped quotes, in python using re


code:
from v1pysdk import V1Meta

with V1Meta (
     instance_url = 'https://www14.v1host.com/v1sdktesting',
     username = 'admin',
     password = 'admin'
     ) as v1:

    new_story = v1.Story.create(
        Name = "Temp Test for StackOverflow question",
        Scope = v1.Scope(0)
        )

    fetched_story = v1.Story.where(Number=new_story.Number).first()
    print fetched_story.Name

rawDescription: VersionOne.SDK.Python Exception while creating story


code:
import os, sys, textwrap

from PyQt4 import QtGui, QtCore

out_file = open("sleep_loop.py", 'w')
out_file.write(textwrap.dedent("""
    import time, sys

    while True:
        print "sleeping..."
        sys.stdout.flush()
        time.sleep(1)"""))
out_file.close()

def started():
    print "started"

def on_error(error):
    errors = ["Failed to start", "Crashed", "Timedout", "Read error",
              "Write Error", "Unknown Error"]
    print "error: ", errors[error]

def on_state_change(new_state):
    states = ["Not running", "Starting", "Running"]
    print "new state: ", states[new_state]

def on_out():
    print "got out"

app = QtGui.QApplication(sys.argv)
proc = QtCore.QProcess()
sig = QtCore.SIGNAL
proc.connect(proc, sig("started()"), started)
proc.connect(proc, sig("error(ProcessError)"), on_error)
proc.connect(proc, sig("readyReadStandardOutput()"), on_out)
proc.connect(proc, sig("stateChanged(ProcessState)"),
             on_state_change)
proc.start("python sleep_loop.py")

widget = QtGui.QWidget()
widget.show()
app.exec_()

proc.close()

rawDescription: No readyReadStandardOutput signal from QProcess


code:
import hashlib
import time

hash = hashlib.sha1()
hash.update(str(time.time()))
print hash.hexdigest()
print hash.hexdigest()[:10]

rawDescription: How to generate a fixed-length hash based on current date and time in Python?

description: Generate a fixed-length hash based on current date and time in Python


code:
import numpy as np

arr = np.array([[00, 10],
            [20, 30]])

x,y = 1,2

print arr[y-1,x-1] #Gives 20.

rawDescription: how to print values of specific coordinates in Numpy

description: Print values of specific coordinates in Numpy


code:
def ipAddressPrettyIn(self, value):
  return origIpAddressPrettyIn(self, value[:4])
origIpAddressPrettyIn = v2c.IpAddress.prettyIn
v2c.IpAddress.prettyIn = ipAddressPrettyIn
msg, rest = decoder.decode(wholeMsg, asn1Spec=v2c.Message())
print msg.prettyPrint()

rawDescription: PySNMP can not recognize response

description: PySNMP can not recognize response


rawDescription: Printing floating point in python

description: Printing floating point in python


code:
import numpy as np
a = np.array([(1,2,3), (4,5,6), (7,8,9)])
e = [4,2,9]
print np.array([ [ i for i in a.transpose()[j] if i != e[j] ]
    for j in range(len(e)) ]).transpose()

rawDescription: New array of smaller size excluding one value from each column


code:
#create the pdf
os.system("start wkhtmltopdf.exe result.html %s%s" %(output_directory, pdf_document_name))

#loop until pdf exists, but add some timer to avoid endless repetition
maxiterations = 60
exists = False
for i in range(maxiterations):
    if os.path.exists(os.path.join(output_directory, pdf_document_name)):
        exists = True
        break;
    time.sleep(1)
#open the current pdf
if exists:
    os.system("start %s%s" %(output_directory, pdf_document_name))
else:
    print 'Could not find file %s to open' % os.path.join(output_directory, pdf_document_name)

rawDescription: check for process stop under shell

description: Check for process stop under shell


code:
import sys
from PySide import QtGui, QtCore

class Window(QtGui.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        help_menu = QtGui.QMenu('&Help')
        about = help_menu.addAction('&About')
        about.triggered.connect(self.show_about)
        self.menuBar().addMenu(help_menu)

    def show_about(self):
        print 'shown'

app = QtGui.QApplication(sys.argv)
win = Window()
win.show()
app.exec_()

rawDescription: How do I add an "About box" to Mac application, using pyqt and py2app?

description: Add an "About box" to Mac application, using pyqt and py2app


code:
#!/usr/bin/env python

import pyudev
import evdev
import select
import sys
import functools
import errno

context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='input')
# NB: Start monitoring BEFORE we query evdev initially, so that if
# there is a plugin after we evdev.list_devices() we'll pick it up
monitor.start()

# Modify this predicate function for whatever you want to match against
def pred(d):
    return "keyboard" in d.name.lower() or "mouse" in d.name.lower()

# Populate the "active devices" map, mapping from /dev/input/eventXX to
# InputDevice
devices = {}
for d in map(evdev.InputDevice, evdev.list_devices()):
    if pred(d):
        print d
        devices[d.fn] = d

# "Special" monitor device
devices['monitor'] = monitor

while True:
    rs, _, _ = select.select(devices.values(), [], [])
    # Unconditionally ping monitor; if this is spurious this
    # will no-op because we pass a zero timeout.  Note that
    # it takes some time for udev events to get to us.
    for udev in iter(functools.partial(monitor.poll, 0), None):
        if not udev.device_node: break
        if udev.action == 'add':
            if udev.device_node not in devices:
                print "Device added: %s" % udev
                try:
                    devices[udev.device_node] = evdev.InputDevice(udev.device_node)
                except IOError, e:
                    # udev reports MORE devices than are accessible from
                    # evdev; a simple way to check is see if the devinfo
                    # ioctl fails
                    if e.errno != errno.ENOTTY: raise
                    pass
        elif udev.action == 'remove':
            # NB: This code path isn't exercised very frequently,
            # because select() will trigger a read immediately when file
            # descriptor goes away, whereas the udev event takes some
            # time to propagate to us.
            if udev.device_node in devices:
                print "Device removed (udev): %s" % devices[udev.device_node]
                del devices[udev.device_node]
    for r in rs:
        # You can't read from a monitor
        if r.fileno() == monitor.fileno(): continue
        if r.fn not in devices: continue
        # Select will immediately return an fd for read if it will
        # ENODEV.  So be sure to handle that.
        try:
            for event in r.read():
                pass
                print evdev.categorize(event)
        except IOError, e:
            if e.errno != errno.ENODEV: raise
            print "Device removed: %s" % r
            del devices[r.fn]

rawDescription: python script to detect hot-plug event

description: Python script to detect hot-plug event


code:
import re
lines=['[ AA BB ] (CC) DD "String to extract"',"Another string in Next line",'XX "Another string"']
for line in lines:
    if line.startswith("["):
        print re.findall(r'\"(.+?)\"',line)
['String to extract']

rawDescription: Parsing substring in python

description: Parsing substring in python


code:
class C:
    def func1(self):
     self.var1 = "something"

    def func2(self):
     print self.var1

foo = C()
foo.func1()
foo.func2()

rawDescription: Learning Python: print variable inside a function from another function


code:
import Image, urllib, StringIO
from math import log, exp, tan, atan, pi, ceil

EARTH_RADIUS = 6378137
EQUATOR_CIRCUMFERENCE = 2 * pi * EARTH_RADIUS
INITIAL_RESOLUTION = EQUATOR_CIRCUMFERENCE / 256.0
ORIGIN_SHIFT = EQUATOR_CIRCUMFERENCE / 2.0

def latlontopixels(lat, lon, zoom):
    mx = (lon * ORIGIN_SHIFT) / 180.0
    my = log(tan((90 + lat) * pi/360.0))/(pi/180.0)
    my = (my * ORIGIN_SHIFT) /180.0
    res = INITIAL_RESOLUTION / (2**zoom)
    px = (mx + ORIGIN_SHIFT) / res
    py = (my + ORIGIN_SHIFT) / res
    return px, py

def pixelstolatlon(px, py, zoom):
    res = INITIAL_RESOLUTION / (2**zoom)
    mx = px * res - ORIGIN_SHIFT
    my = py * res - ORIGIN_SHIFT
    lat = (my / ORIGIN_SHIFT) * 180.0
    lat = 180 / pi * (2*atan(exp(lat*pi/180.0)) - pi/2.0)
    lon = (mx / ORIGIN_SHIFT) * 180.0
    return lat, lon

############################################

# a neighbourhood in Lajeado, Brazil:

upperleft =  '-29.44,-52.0'
lowerright = '-29.45,-51.98'

zoom = 18   # be careful not to get too many images!

############################################

ullat, ullon = map(float, upperleft.split(','))
lrlat, lrlon = map(float, lowerright.split(','))

# Set some important parameters
scale = 1
maxsize = 640

# convert all these coordinates to pixels
ulx, uly = latlontopixels(ullat, ullon, zoom)
lrx, lry = latlontopixels(lrlat, lrlon, zoom)

# calculate total pixel dimensions of final image
dx, dy = lrx - ulx, uly - lry

# calculate rows and columns
cols, rows = int(ceil(dx/maxsize)), int(ceil(dy/maxsize))

# calculate pixel dimensions of each small image
bottom = 120
largura = int(ceil(dx/cols))
altura = int(ceil(dy/rows))
alturaplus = altura + bottom

final = Image.new("RGB", (int(dx), int(dy)))
for x in range(cols):
    for y in range(rows):
        dxn = largura * (0.5 + x)
        dyn = altura * (0.5 + y)
        latn, lonn = pixelstolatlon(ulx + dxn, uly - dyn - bottom/2, zoom)
        position = ','.join((str(latn), str(lonn)))
        print x, y, position
        urlparams = urllib.urlencode({'center': position,
                                      'zoom': str(zoom),
                                      'size': '%dx%d' % (largura, alturaplus),
                                      'maptype': 'satellite',
                                      'sensor': 'false',
                                      'scale': scale})
        url = 'http://maps.google.com/maps/api/staticmap?' + urlparams
        f=urllib.urlopen(url)
        im=Image.open(StringIO.StringIO(f.read()))
        final.paste(im, (int(x*largura), int(y*altura)))
final.show()

rawDescription: Capture embedded google map image with Python without using a browser


code:
name=input("Enter your name: ")
message=input("Enter your message:")
namefinal = ''
for letter  in name:
   if letter not in message:
       namefinal += letter
print namefinal

rawDescription: Deleting elements in Python list using user input


code:
from glob import glob
from os import path

config_dir = '/etc/netctl'

profiles = dict((i, {'full_path': v, 'ESSID': None, 'matches': []}) for (i, v) in enumerate(glob(config_dir + '/*')) if path.isfile(v))

for K, V in profiles.items():
    with open(V['full_path']) as f:
        for line in f:
            if line.startswith('ESSID'):
                V['ESSID'] = line.split('=',1)[1].strip()
                break # no need to keep reading.
    for k, v in profiles.items():
        if K == k or k in V['matches'] or not v['ESSID']:
            continue
        if V['ESSID'] == v['ESSID']:
            V['matches'].append(k)
            v['matches'].append(K)

for k, v in profiles.items():
    print k, v

rawDescription: Opening files found from os.listdir() and comparing lines inside?


code:
content = "import math\n\ndef f(x):\n    return math.log(x)"

import imp
my_module = imp.new_module('my_module')
exec content in my_module.__dict__ # in python 3, use exec() function

print my_module.f(2)

rawDescription: How to create a module object by content in Python

description: Create a module object by content in Python


code:
from HTMLParser import HTMLParser

class extract_href(HTMLParser):
    def handle_starttag(self, tag, attrs):
        if tag == "a":
            for key, val in attrs:
                if key == 'href':
                    print val

parser = extract_href()
parser.feed("""
link
""")

rawDescription: A fast way to extract all ANCHORs from HTML in python


code:
d = {
      'prefix1' : 'success',
      'prefix2' : 'success',
      'prefix3' : 'success',
      'prefix4' : 'success'
    }

for i in range(1,5):
    temp = "prefix%s" % i
    print d[temp]

rawDescription: Printing variables with incrementing suffixes


code:
p = re.compile('((:?0000)+)')
longestword = ""
for word in p.findall(ip6):
    if len(word[0])>len(longestword):
        longestword = word[0]
print longestword

rawDescription: Python re.search() not returning full group match


code:
for num in range(2, 10):
    if num % 2 == 0:
        print "Found an even number", num
        continue
    print "Found a number", num

rawDescription: Python : Exiting for loop?

description: Python : Exiting for loop


code:
from urllib import urlopen
import re
p = re.compile(r'
(.*?)')
text = urlopen('http://python.org/community/jobs').read()
print text
for url, name in p.findall(text):
    print '%s (%s)' % (name, url)

rawDescription: Why doesn't this simple Python program print out?


code:
#!/usr/bin/env python
import socket
from Queue import Queue
from threading import Thread

def getips(queue):
    for site in iter(queue.get, None):
        try: # resolve hostname
            result = socket.gethostbyname_ex(site)
        except IOError, e:
            print("error %s reason: %s" % (site, e))
        else:
            print("done %s %s" % (site, result))

def main():
    websites = "youtube google non-existent.example facebook yahoo live".split()
    websites = [name+'.com' for name in websites]

    # Spawn thread pool
    queue = Queue()
    threads = [Thread(target=getips, args=(queue,)) for _ in range(20)]
    for t in threads:
        t.daemon = True
        t.start()

    # Place work in queue
    for site in websites: queue.put(site)
    # Put sentinel to signal the end
    for _ in threads: queue.put(None)
    # Wait for completion
    for t in threads: t.join()

main()

rawDescription: Python socket.gethostbyname_ex() multithread fails


code:
import numpy as np

### original code
def f(a, b, arr):
    X = a * np.exp(-arr**2 / b)
    return np.sum(a * np.log(X) - arr)

A_axis = np.linspace(1, 5, 8)
B_axis = np.linspace(1, 2, 9)
XX, YY = np.meshgrid(A_axis, B_axis)
arr = np.arange(10)
ZZ = np.empty_like(XX)
for i in range(XX.shape[0]):
    for j in range(YY.shape[1]):
        ZZ[i,j] = f(XX[i,j], YY[i,j], arr)

### use broadcast
def f(a, b, arr):
    X = a * np.exp(-arr**2 / b)
    return np.sum(a * np.log(X) - arr, axis=-1)
B, A, C = np.ix_(B_axis, A_axis, arr)
result = f(A, B, C)

print np.allclose(ZZ, result)

rawDescription: Call function with internal sum over 2D array


code:
#!/usr/bin/env python

# added a test case
B = ['a', ['b', 'c',['d','e']], 'f', ['g', 'h']]
A = ['a', ['b', 'c',['d','e']]]

# found on stack overflow - flatten list of kids for parent
def flatten(iterable):
    """Recursively iterate lists and tuples.
    """
    for elm in iterable:
        if isinstance(elm, (list, tuple)):
            for relm in flatten(elm):
                yield relm
        else:
            yield elm

# add data to an existing tree (recursive)
def treeify(tree, l):
    if isinstance(l, list):
        # avoid looking back
        l.reverse()
    for index in range(len(l)):
        if isinstance(l[index], list):
            parent_name = l[index+1]
            # flatten kids to a list
            tree[parent_name] = list(flatten(l[index]))
            # continue in deeper lists
            treeify(tree, l[index])

tree = {}
treeify(tree, A)
print tree
tree = {}
treeify(tree, B)
print tree

rawDescription: convert nested list to dictionary in python

description: Convert nested list to dictionary in python


code:
from colorama import Fore, init
import re
init()

tagged_text = [('Stately', 'RB'), (',', ','), ('plump', 'VB'), ('Buck', 'NNP'), ('Mulligan', 'NNP'), ('came', 'VBD'),
                ('from', 'IN'), ('the', 'DT'), ('stairhead', 'NN'), (',', ','), ('bearing', 'VBG'), ('a', 'DT'),
                ('bowl', 'NN'), ('of', 'IN'), ('lather', 'NN'), ('on', 'IN'), ('which', 'WDT'), ('a', 'DT'),
                ('mirror', 'NN'), ('and', 'CC'), ('a', 'DT'),('razor', 'NN'), ('lay', 'NN'), ('crossed', 'VBD'),
                ('.', '.'), ('The', 'DET'), ('function', 'NN'), ('f', 'SYM'), ('(','('),('x','SYM'),(',',','),
                ('y','SYM'),(')',')'),('takes','VB'), ('two', 'CD'), ('arguments', 'NN'), ('.','.')]
origtext = 'Stately, plump Buck Mulligan came from the stairhead, bearing a bowl of lather on which a mirror and a razor lay crossed. The function f(x,y) takes two arguments.'

colordict = {'VB': Fore.GREEN, 'VBD': Fore.GREEN, 'NN': Fore.BLUE}

colorwords = ''
for word, tag in tagged_text:
    color = Fore.BLACK
    word = re.match(r'\s*%s\s*' % re.escape(word), origtext).group()
    origtext = origtext.split(word,1)[1]
    if tag in colordict:
        color = colordict[tag]
    colorwords += color + word

print colorwords

rawDescription: Coloring text in terminal according to part of speech


code:
while True:
   stringName = raw_input("Convert string to hex & ascii(type stop to quit): ").strip()
   if stringName == 'stop':
      break

   print "Hex value: ", stringName.encode('hex')
   print "ASCII value: ", ', '.join(str(ord(c)) for c in stringName)

rawDescription: Converting string to ascii using ord()

description: Converting string to ascii using ord()


code:
import os
import pandas as pd
from tempfile import NamedTemporaryFile

# Create an accessible temporary file, and then delete it. We only need a valid path.
f = NamedTemporaryFile(delete=False, suffix='.csv')
f.close()
os.unlink(f.name)  # Not deleting will result in a "File already exists" warning

xlCSVWindows = 0x17  # CSV file format, from enum XlFileFormat
xlwb.SaveAs(Filename=f.name, FileFormat=xlCSVWindows)  # Save the workbook as CSV
df = pd.read_csv(f.name)  # Read that CSV from Pandas
print df

rawDescription: From Password Protected Excel File to Python Object


code:
for link in entry.link:
    print link.href

rawDescription: GBase Snippets Feed, how do I extract the URL?


code:
from PIL import Image
import math

def run():
    im = Image.open("ZHiG0.jpg")
    pix = im.load()
    print im.size
    # print pix[0, 1]

    im2 = Image.new("RGB", (2400, 2400))

    for i in range(im.size[0]):
        for j in range(im.size[0]):
            im2.putpixel((i, j), (pix[i, j],pix[i, j],pix[i, j]))

    im2.save("ZHiG0-2400-2400.jpg")
run()

rawDescription: Python Imaging Library makes an image reddish


code:
import cv2
import numpy as np

cap = cv2.VideoCapture(0)
blue_threshold = 1000000  # This value you could change for what works best

while True:

    # Take each frame
    _, frame = cap.read()

    # Convert BGR to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # define range of blue color in HSV
    lower_blue = np.array([110,50,50])
    upper_blue = np.array([130,255,255])

    # Threshold the HSV image to get only blue colors
    mask = cv2.inRange(hsv, lower_blue, upper_blue)
    count = mask.sum()

    if count > blue_threshold:
       print 'I can see blue color'

    cv2.imshow('frame',frame)
    cv2.imshow('mask',mask)

    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()

rawDescription: How to print text when camera captures blue color?

description: Print text when camera captures blue color


code:
## {{{ http://code.activestate.com/recipes/577187/ (r9)
from Queue import Queue
from threading import Thread

class Worker(Thread):
    """Thread executing tasks from a given tasks queue"""
    def __init__(self, tasks):
        Thread.__init__(self)
        self.tasks = tasks
        self.daemon = True
        self.start()

    def run(self):
        while True:
            func, args, kargs = self.tasks.get()
            try: func(*args, **kargs)
            except Exception, e: print e
            self.tasks.task_done()

class ThreadPool:
    """Pool of threads consuming tasks from a queue"""
    def __init__(self, num_threads):
        self.tasks = Queue(num_threads)
        for _ in range(num_threads): Worker(self.tasks)

    def add_task(self, func, *args, **kargs):
        """Add a task to the queue"""
        self.tasks.put((func, args, kargs))

    def wait_completion(self):
        """Wait for completion of all the tasks in the queue"""
        self.tasks.join()

if __name__ == '__main__':
    from random import randrange
    delays = [randrange(1, 10) for i in range(100)]

    from time import sleep
    def wait_delay(d):
        print 'sleeping for (%d)sec' % d
        sleep(d)

    # 1) Init a Thread pool with the desired number of threads
    pool = ThreadPool(20)

    for i, d in enumerate(delays):
        # print the percentage of tasks placed in the queue
        print '%.2f%c' % ((float(i)/float(len(delays)))*100.0,'%')

        # 2) Add the task to the queue
        pool.add_task(wait_delay, d)

    # 3) Wait for completion
    pool.wait_completion()
## end of http://code.activestate.com/recipes/577187/ }}}

rawDescription: Is there a Python library equivalent of the Java's FixedThreadPool?


code:
import subprocess

ssid = {}
ssids = []
cmd = subprocess.Popen('netsh wlan show networks mode=BSSID',
                       shell=True, stdout=subprocess.PIPE)
for line in cmd.stdout:
    l = map(lambda x: x.strip(), line.strip().split(":"))
    if len(l) > 1:
        key, value = l[0], l[1]
        if key.startswith("SSID"):
            key = "name"
            if ssid:
                ssids.append(ssid)
            ssid = {}
            ssid[key] = value
        if "name" in ssid:
            ssid[key] = value
ssids.append(ssid)

# example of how to use ssids, list of ssid dictionaries
for ssid in ssids:
    print ssid["name"], ssid["Encryption"]

rawDescription: Python list of lists from CMD output

description: Python list of lists from CMD output


rawDescription: Python: printing values from a list of tuples


code:
import struct

def _get_block(s, count):
    if count <= 0:
        return ''
    buf = ''
    while len(buf) < count:
        buf2 = s.recv(count - len(buf))
        if not buf2:
            # error or just end of connection?
            if buf:
                raise RuntimeError("underflow")
            else:
                return ''
        buf += buf2
    return buf

def _send_block(s, data):
    while data:
        data = data[s.send(data):]

if False:
    def get_msg(s):
        count = struct.unpack('>i', _get_block(s, 4))[0]
        return _get_block(s, count)

    def send_msg(s, data):
        header = struct.pack('>i', len(data))
        _send_block(s, header)
        _send_block(s, data)

if True:

    def _get_count(s):
        buf = ''
        while True:
            c = s.recv(1)
            if not c:
                # error or just end of connection/
                if buf:
                    raise RuntimeError("underflow")
                else:
                    return -1
            if c == '|':
                return int(buf)
            else:
                buf += c

    def get_msg(s):
        return _get_block(s, _get_count(s))

    def send_msg(s, data):
        _send_block(s, str(len(data)) + '|')
        _send_block(s, data)

import threading
import socket
import time

def client(port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('0.0.0.0', port))
    print get_msg(s)
    print get_msg(s)
    s.shutdown(socket.SHUT_RDWR)
    s.close()

def server(port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind(('0.0.0.0', port))
    s.listen(1)
    c, addr = s.accept()
    send_msg(c, 'hello')
    send_msg(c, 'there')
    c.close()
    s.close()

if __name__ == '__main__':
    c = threading.Thread(target=server, args=(8999,))
    c.start()
    time.sleep(1)
    client(8999)
    c.join()
    print 'done'

rawDescription: Python: Size of message to send via socket


code:
import time
import calendar
from datetime import datetime
from itertools import repeat
acc=[{u'switch_id': 3, u'hangup_cause_id': 7, u'start_uepoch': datetime(2015, 5, 8, 13, 32, 1), u'duration': 32}, {u'switch_id': 3, u'hangup_cause_id': 10, u'start_uepoch': datetime(2015, 5, 8, 13, 32, 8), u'duration': 20}, {u'switch_id': 3, u'hangup_cause_id': 10, u'start_uepoch': datetime(2015, 5, 8, 13, 32, 10), u'duration': 17}]
t = datetime.now()
y = t.year
m = t.month
d = t.day
donnees=[]
for k in range(7):
    m = t.month
    data=[]
    liste=[]
    liste_time=[]
    for i in acc:
        if (i["start_uepoch"].year == y and i["start_uepoch"].month == m and i["start_uepoch"].day == d-k):
            liste.append([i["start_uepoch"],i["duration"]])
    for q in range(24):
        for mnt in range(60):
            temps = None
            liste2=[]
            ACD=0
            somme_duration=0
            n=0
            for p in liste:
                if (p[0].hour==q and p[0].minute == mnt):
                    liste2.append(p[1])
                    temps=p[0]
            if temps:
                if len(liste2)!=0:
                    for j in liste2:
                        somme_duration+=j
                        n+=1
                    ACD=round((float(somme_duration)/n)*100)/100
                    liste_time.append(calendar.timegm(temps.timetuple()))
                    data.append(ACD)
                else:
                    liste_time.append(calendar.timegm(temps.timetuple()))
                    data.append(0)
    donnees.append(data)
print donnees

rawDescription: creating list of lists alimented with data from database gives empty list

description: Creating list of lists alimented with data from database gives empty list


rawDescription: Finding largest prime factor too slow in Python


code:
headers = {'Referer': 'http://www.pricescope.com/hca.php'}

r = requests.post(url, data=params, headers=headers)
print r.content

rawDescription: POST Request in Ruby/Python returning 'No Data'


code:
def divisible_by_factor(mod_vals):
    return [i for i in range(1, 100) if all(i % j == 0 for j in mod_vals)]
print divisible_by_factor([2, 3, 5])

rawDescription: Most efficient way to check if numbers in a list is divisible by another number


code:
import xml.dom.minidom
data_in = """<Files>
  <Filter>
  </Filter>
</Files>
"""
data_add = ('./src/aaa.cxx','./src/bbb.cxx')
doc = xml.dom.minidom.parseString(data_in)
files= doc.getElementsByTagName("Files")[0]
for filter in files.getElementsByTagName("Filter"):
  for item in data_add:
    newNode = doc.createElement("File")
    newNode.setAttribute('RelativePath',item)
    filter.appendChild(newNode)
print doc.toxml()

rawDescription: adding nodes to xml file with same node name but different attributes with Python Dom

description: Adding nodes to xml file with same node name but different attributes with Python Dom


code:
from twisted.web import proxy, http
from twisted.internet import reactor
from twisted.python import log
import sys

log.startLogging(sys.stdout)

class MyProxy(proxy.Proxy):
    def dataReceived(self, data):

      # Modify the data here
      print data

      # perform the default functionality on modified data
      return proxy.Proxy.dataReceived(self, data)

class ProxyFactory(http.HTTPFactory):
  protocol=MyProxy

factory = ProxyFactory()
reactor.listenTCP(8080, factory)
reactor.run()

rawDescription: Python Twisted proxy - how to intercept packets


code:
import csv
file_data = self.request.get('file_in')
file_data_list = file_data.split('\n')
file_Reader = csv.reader(file_data_list)
for fields in file_Reader:
    print row

rawDescription: how to get data from csv file in python

description: Get data from csv file in python


code:
def partial_versioned_join(tablea, tableb, tablea_keys, tableb_keys):

    try:
        tablea_group, tablea_date = tablea_keys
        tableb_group, tableb_date = tableb_keys
    except ValueError, e:
        raise(e, 'Need to pass in both a group and date key for both tables')

    # Note: can't actually use group here as a field name due to sqlite
    statement = """SELECT a.group, a.{date_a} AS {temp_date}, b.*
                    FROM (SELECT tablea.group, tablea.{date_a}, tablea.{group_a},
                         MAX(tableb.{date_b}) AS tdate
                        FROM tablea
                        JOIN tableb
                        ON tablea.{group_a}=tableb.{group_b}
                        AND tablea.{date_a}>=tableb.{date_b}
                        GROUP BY tablea.{base_id}, tablea.{date_a}, tablea.{group_a}
                        ) AS a
                    JOIN tableb b
                    ON   a.{group_a}=b.{group_b}
                    AND  a.tdate=b.{date_b};
                    """.format(group_a=tablea_group, date_a=tablea_date,
                               group_b=tableb_group, date_b=tableb_date,
                               temp_date='join_date', base_id=base_id)
    # Note: you lose types here for tableb so you may want to save them
    pre_join_tableb = sqldf(statement, locals())
    return pd.merge(tablea, pre_join_tableb, how='inner',
                    left_on=['group'] + tablea_keys,
                    right_on=['group', tableb_group, 'join_date'])

rawDescription: Pandas joining based on date

description: Pandas joining based on date


rawDescription: Python equivalent of unix cksum function


code:
for textfile in findfiles(r'C:\\Top', '*.txt'):
    if not textfile.startswith(r'C:\\Top\\1'):
        filename = os.path.basename(textfile)
        print filename

rawDescription: Python - Open All Text Files in All Subdirectories Unless Text File Is In Specified Directory


code:
import re

s = "ABC12DEF3G56HIJ7"
pattern = re.compile(r'([A-Z]+)([0-9]+)')

for (letters, numbers) in re.findall(pattern, s):
    print numbers, '*', letters

rawDescription: Looping through python regex matches

description: Looping through python regex matches


code:
print "So, you're %r old, %r tall and %r heavy" % (age, height, weight)
print "So, you're %s old, %s tall and %s heavy" % (age, height, weight)

rawDescription: Formatters %r and %s - difference in theory but this script says otherwise


code:
obj = MyClass()
try:
    func = getattr(obj, "dostuff")
    func()
except AttributeError:
    print "dostuff not found"

rawDescription: Python - How to invoke an function on an object dynamically by name?


code:
total = 0

def get_input_from_player(player):
    while True:
        ask_user = input("Player " + str(player) + " Enter a number (1, 2 or 3):")
        if ask_user == 1 or ask_user == 2 or ask_user == 3:
            if 25 < ask_user + total:
                continue
            else:
                return ask_user
        else:
            print "invalid input"

def who_is_first_to_25():
    global total
    while True:
        playerOne = get_input_from_player(1)
        total += playerOne
        if 25 <= total:
            print "player 1 is first to 25"
            break
        playerTwo = get_input_from_player(2)
        total += playerTwo
        if 25 <= total:
            print "player 2 is first to 25"
            break
    total = 0
    print "----------------xxxxxxxxxxxxxxxxxxxxxx---------------------"

who_is_first_to_25()

rawDescription: adding condition of a subprogram in a different subprogram? python

description: Adding condition of a subprogram in a different subprogram? python


code:
def fun():
    print '{0} Here is a really long ' \
          'sentence with {1}'.format(3, 5)

rawDescription: Wrap long lines in Python

description: Wrap long lines in Python


code:
"foo" <> "bar"
"foo" <> "foo"

rawDescription: what does <> mean in Python

description: What does <> mean in Python


code:
info = col.find_and_modify({'status':'new'}, {'$set':{'status':'in_progress'}})
if info:
    print info.get('_id')

rawDescription: PyMongo: Update, $multi:false, get _id of updated document?


rawDescription: Why I can't iterate over a binary multiple line reading?


code:
try:
  thevariable
except NameError:
  print "well, it WASN'T defined after all!"
else:
  print "sure, it was defined."

rawDescription: Determine if variable is defined in Python


code:
import praw

user_name = "user_name_to_get"
user_agent = "subreddit analyzer"

r = praw.Reddit(user_agent=user_agent)
user = r.get_redditor(user_name)

subs = set()

try:
    overview = user.get_overview()

    for item in overview:
        subs.add(item.subreddit.display_name)

except praw.errors.NotFound:
    print("Unable to find user")

print subs

rawDescription: subreddits of user using PRAW or R

description: Subreddits of user using PRAW or R