code:
user_list = [
    {'hostname': 'server1', 'username': 'john'},
    {'hostname': 'server2', 'username': 'jack'},
    {'hostname': 'server2', 'username': 'jonny'},
    {'hostname': 'server3', 'username': 'jules'},
    {'hostname': 'server1', 'username': 'jonny'},
    {'hostname': 'server1', 'username': 'jeff'}]

def sort_by_preferred_users(key):
    preferred_users = ['john', 'jonny', 'jeff']
    username = key['username']
    return preferred_users.index(username) if username in preferred_users else len(preferred_users)

user_list.sort(key=sort_by_preferred_users)
new_user_list = []
server_list = []

for d in user_list:
    hostname = d['hostname']
    if hostname not in server_list:
        new_user_list.append(d)
        server_list.append(hostname)
        print d

rawDescription: Remove Dictionary entries based on specific criteria in a List of Dictionaries


code:
from sqlalchemy import *
from sqlalchemy import create_engine, orm
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship

metadata = MetaData()
Base = declarative_base()
Base.metadata = metadata

db = create_engine('<db connection URL>',echo=False)
metadata.reflect(bind=db)

cause_code_table = metadata.tables['cause_code']
ndticket_table = metadata.tables['ndticket']

sm = orm.sessionmaker(bind=db, autoflush=True, autocommit=True, expire_on_commit=True)
session = orm.scoped_session(sm)

q = session.query(ndticket_table,cause_code_table).join(cause_code_table)
for r in q.limit(10):
    print r

rawDescription: Can SQLAlchemy automatically create relationships from a database schema?


code:
import fileinput
import sys
import json
import xmltodict

for line in fileinput.input():
        o=xmltodict.parse(line)
        t=json.dumps(o)
        print t + "\n"

rawDescription: GNU Parallel to run Python script on huge file


code: print my_matrix.astype('i')

rawDescription: Matrix-like printing of 2D arrays in Python


rawDescription: Removing parentheses and comma

description: Removing parentheses and comma


code:
if __name__ == '__main__':
  id=0
  print '#'*30, 'INSERT', '#'*30
  id += id
  e = insert(Employee, {'id': id, 'type': 'engineer'})
  print 'pass' if count(session, Employee) == count(session, Engineer) else 'fail'

  print '#'*30, 'DELETE', '#'*30
  session.delete(e);
  session.commit();
  print 'pass' if count(session, Employee) == count(session, Engineer) else 'fail'
  session.flush()

rawDescription: SQLAlchemy cascading polymorphic column updates


code:
import re
p = re.compile(ur'\/([a-zA-Z0-9]+)\.')
test_str = u"..."

re.findall(p, test_str)

rawDescription: Regex match between forward slash and a dot


rawDescription: Python List Help (Find Biggest number)

description: Python List Help (Find Biggest number)


code:
def disallow_class(*klass_names):
    def function_handler(fn):
        def decorated(self, *args, **kwargs):
            if self.__class__.__name__ in klass_names:
                print "access denied to class: %s" % self.__class__.__name__
                return None
            return fn(self, *args, **kwargs)
        return decorated
    return function_handler

class MainClass(object):

    @disallow_class('DisallowedClass', 'AnotherDisallowedClass')
    def my_method(self, *args, **kwargs):
        print "my_method running!! %s" % self

class DisallowedClass(MainClass): pass

class AnotherDisallowedClass(MainClass): pass

class AllowedClass(MainClass): pass

if __name__ == "__main__":
    x = DisallowedClass()
    y = AnotherDisallowedClass()
    z = AllowedClass()
    x.my_method()
    y.my_method()
    z.my_method()

rawDescription: Python decorator to limit a method to a particular class?


code:
import httplib
conn = httplib.HTTPConnection("goo.gl")
conn.request("HEAD", "/IwruD")
res = conn.getresponse()
print res.getheaders()

rawDescription: Django: check if an image exists at some particular url


code:
from pyparsing import *

# Read the input data
filename = "data.txt"
FIN      = open(filename)
TEXT     = FIN.read()

# Define a simple grammar for the text, multiply the first col by 2
digits = Word(nums)
digits.setParseAction(lambda x:int(x[0]) * 2)

blocks   = Group(Word(alphas) + digits)
grammar  = OneOrMore(blocks)

# Parse the results
result = grammar.parseString( TEXT )
# This gives a list of lists
# [['cat', 6], ['dog', 10], ['foo', 14]]

# Open up a new file for the output
filename2 = "data2.txt"
FOUT = open(filename2,'w')

# Walk through the results and write to the file
for item in result:
    print item
    FOUT.write("%s %i\n" % (item[0],item[1]))
FOUT.close()

rawDescription: python & pyparsing newb: how to open a file

description: Python & pyparsing newb: how to open a file


code:
import re
p = re.compile(ur'\b(\w+):(\w+)(?=;)')
test_str = u".pancake{height:200px;taste:delicious;}"

re.findall(p, test_str)

rawDescription: Regex matching CSS

description: Regex matching CSS


code:
f = open('test.txt','r')  # Opening the "wrong" way
for line in f:
    print line

rawDescription: Unable to read huge (20GB) file from CPython


code:
import win32api
import win32net
import win32netcon,win32wnet

username='user'
password='psw'

try:
    win32wnet.WNetAddConnection2(win32netcon.RESOURCETYPE_DISK, 'Z:','\\\\192.168.1.18\\D$', None, username,password, 0)
    print "connection established successfully"
except:
    print  "connection not established"

rawDescription: Read all system's drive data in network using python


code:
from itertools import product
L = ["A", "B", "C"]
def f(c): return c.lower()

fL = [f(x) for x in L]
for i in product(*zip(L, fL)):
    print i

rawDescription: Given an iterable, how to apply a function in every possible combination?


rawDescription: get some substring from readline() in python with regular expression

description: Get some substring from readline() in python with regular expression


code:
from selenium import webdriver
import re

driver = webdriver.PhantomJS()
driver.set_window_size(1120, 550) #For bug
driver.get("http://localhost:8000")

pattern = r"""
    \s*         #Match whitespace, 0 or more times, followed by...
    (\d+)       #a digit, one or more times, captured, followed by
    \s*         #whitespace, 0 or more times, followed by...
    [|]         #vertical bar, followed by...
    \s*         #whitespace, 0 or more times, followed by...
    \d+         #a digit, one or more times
"""
regex = re.compile(pattern, re.X)

table = driver.find_element_by_id('ambassadors-for-assignment')
trs = table.find_elements_by_tag_name('tr')

for tr in trs:
    tds = tr.find_elements_by_tag_name('td')

    for td in tds:
        match_obj = re.search(regex, text)

        if match_obj and match_obj.group(1) == '0':
            success_button = tr.find_element_by_css_selector('button.btn-success')
            print success_button.get_attribute('type')
            success_button.click()

rawDescription: how to dynamically read a specific cell value in a table using selenium and python

description: Dynamically read a specific cell value in a table using selenium and python


code:
s = "623746xyz3"
for xyz in range(1000):
    sxyz = s.replace('xyz', str(xyz))
    print int(sxyz)

rawDescription: Python - Number with a variable part

description: Python - Number with a variable part


code:
for i in range(b+1, len(a)):
    print i, a[i]

rawDescription: For loops over a list

description: For loops over a list


code: print 1

rawDescription: Python: How to import 2.7 module into 3.4 program?


code:
x = range(3)
x
print x

rawDescription: IPython - have print default to print head & tail on long variables


code:
import nltk
grammar = nltk.parse_cfg("""
S -> CP | VP
CP -> VP C VP | CP C VP | VP C CP
VP -> NP V
NP -> 'Kim' | 'Dana' | 'everyone'
V -> 'arrived' | 'left' |'cheered'
C -> 'or' | 'and'
""")

print "======= Kim arrived ========="
sent = "Kim arrived".split()
parser = nltk.ChartParser(grammar)
for t in parser.nbest_parse(sent):
    print t

print "\n======= Kim arrived or Dana left ========="
sent = "Kim arrived or Dana left".split()
parser = nltk.ChartParser(grammar)
for t in parser.nbest_parse(sent):
    print t

print "\n=== Kim arrived or Dana left and everyone cheered ===="
sent = "Kim arrived or Dana left and everyone cheered".split()
parser = nltk.ChartParser(grammar)
for t in parser.nbest_parse(sent):
    print t

rawDescription: Python and NLTK: How to analyze sentence grammar?


code:
from datetime import datetime
from ordereddefaultdict import OrderedDefaultdict

object_list = [{'timestamp': datetime.strptime("01/01/2014", "%d/%m/%Y")},
               {'timestamp': datetime.strptime("12/05/2014", "%d/%m/%Y")},
               {'timestamp': datetime.strptime("03/01/2014", "%d/%m/%Y")},
               {'timestamp': datetime.strptime("01/01/2014", "%d/%m/%Y")},]

date_grouped_objects = OrderedDefaultdict(list)

for obj in sorted(object_list):
    key = obj['timestamp'].strftime("%d/%m/%Y")  # convert datetime to string
    date_grouped_objects[key].append(obj)

for key, value in date_grouped_objects.iteritems():
    print '[{!r}] = {}'.format(key, value)

rawDescription: Python - Sort defaultdict by key

description: Python - Sort defaultdict by key


rawDescription: How can I calculate a hash for a filesystem-directory using Python?

description: Calculate a hash for a filesystem-directory using Python


code:
def card_output(value, suit_number):
    suit, symbol = SUITS[suit_number]
    return ['_' * (CARD_WIDTH + 2)] + map(card_row, [
        '',
        NAMES.get(value, "%2d" % value),
        'of',
        suit,
        symbol * CARD_WIDTH,
        '_' * CARD_WIDTH])

# method 1. randomly select until we have 5 unique values
hand1 = set()
while len(hand1) < NUM_CARDS:
    hand1.add((random.randint(2, 14), random.randint(0, 3)))

# method 2. make a deck then sample 5 cards from it
deck = [(value, suit_number) for value in range(2, 14) for suit_number in range(3)]
hand2 = random.sample(deck, NUM_CARDS)

print 'Welcome to SANDCRAB. This is your deck of cards.'
for hand in [hand1, hand2]:
    for row in zip(*[card_output(value, suit_number) for value, suit_number in hand]):
        print ''.join(row)

rawDescription: How to remove duplicates in card game- python

description: Remove duplicates in card game- python


code: print a.data

rawDescription: How to disregard the NaN data point in numpy array and generate the normalized data in Python?

description: Disregard the NaN data point in numpy array and generate the normalized data in Python


code:
cards = define_cards()
for i, card in enumerate(cards):
    print i, card

rawDescription: How to use for loop to print cards?

description: Use for loop to print cards


code:
id = "3333333"
url = "https://example.com/%s" % id
print url

rawDescription: Passing a variable in url?

description: Passing a variable in url


code:
import sys
import zmq
from  multiprocessing import Process
import time
import json
import cPickle as pickle

def worker():
    context = zmq.Context()
    work_receiver = context.socket(zmq.PULL)
    work_receiver.connect("tcp://127.0.0.1:5557")

    for task_nbr in range(1000000):
        message = work_receiver.recv_pyobj()

    sys.exit(1)

def main():
    Process(target=worker, args=()).start()
    context = zmq.Context()
    ventilator_send = context.socket(zmq.PUSH)
    ventilator_send.bind("tcp://127.0.0.1:5557")
    msg = {
            'something': "More",
            "another": "thing",
            "what?": range(200),
            "ok": ['asdf', 'asdf', 'asdf']
            }
    for num in range(1000000):
        ventilator_send.send_pyobj(msg)

if __name__ == "__main__":
    start_time = time.time()
    main()
    end_time = time.time()
    duration = end_time - start_time
    msg_per_sec = 1000000 / duration

    print "Duration: %s" % duration
    print "Messages Per Second: %s" % msg_per_sec

rawDescription: how can I speed up simultaneous read and write of multiprocessing queues?

description: Speed up simultaneous read and write of multiprocessing queues


code:
import re

def _human_key(key):
    parts = re.split('(\d*\.\d+|\d+)', key)
    return tuple((e.swapcase() if i % 2 == 0 else float(e))
            for i, e in enumerate(parts))

nums = ['9', 'aB', '1a2', '11', 'ab', '10', '2', '100ab', 'AB', '10a',
    '1', '1a', '100', '9.9', '3']
nums.sort(key=_human_key)

print '\n'.join(nums)

rawDescription: Python - Human sort of numbers with alpha numeric, but in pyQt and a __lt__ operator


code:
minimum_length, maximum_length = 1, 2
for length in range(minimum_length, maximum_length + 1):
    for combo in product(ascii_lowercase, repeat=length):
        print ''.join(combo)

rawDescription: Python: Every possible letter combination


rawDescription: splitting data using regex in python

description: Splitting data using regex in python


code:
foo()

def foo():
   print 'bar'

rawDescription: Pythonian structure?

description: Pythonian structure


code: print P

rawDescription: Dynamic programming in Python

description: Dynamic programming in Python


code:
class LoginPage_TestCase(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Ie("C:\QA\Automation\Python_projects\Selenium  Webdriver\IEDriverServer_Win32_2.45.0\IEDriverServer.exe")
        self.driver.get("http://riaz-pc.company.local:8080/clearcore")

        self.login_page = page.LoginPage(self.driver)

    def test_login_valid_user(self):
        self.login_page.userLogin_valid()
        self.login_page.isAdministration_present()
        assert self.login_page.isAdministration_present(), "Administration not found"
        if self.login_page.isAdministration_present() == "true":
            print "test login with a valid user passed"
        else:
            print "test login with a valid user failed"

    def test_login_invalid_user(self):
        print "test_login_invalid_user"
        # use self.login_page here

rawDescription: I am using unittest.TestCase if i have more than 1 test case it will not run. Only 1 test case runs


code:
from re import match

def rules(name):
    return match(r'^([A-Z][a-z]+(?:[A-Z]?[a-z]+)* ?){1,}$', name)

input = ['000000AAAAAARob Alsod', 'AAAPerson Person', 'Jeff the awesome Guy', 'Jenna DEeath', 'GEOFFERY EVERDEEN', 'shy guy', 'Theone Normalperson', '  Guywith Whitespace', 'Someone Middlename MacIntyre', '', 'Jack Really Awesome']
results = filter(rules, input)
print results

rawDescription: Using Regex to 'Clean Up' a List of Names


code:
vars = [0x64, 0x32, 0x16, True, 'a']
my_var = xyz(*vars)
print my_var.a

rawDescription: How to implement c++ type structures in python?

description: Implement c++ type structures in python


code:
consonant = ('b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z')
def isConsonant(user):
    for letter in user:
        if letter in consonant:
            print letter + "o" + letter

var = raw_input("type smth: ")
isConsonant(var)

rawDescription: Getting wrong output when working with tuple and list in Python


code:
class MyClass(object):
    def __init__(self, labels):
        for label in labels:
            setattr(self, label, functools.partial(self._myfunction, label))
    def _myfunction(self, label, request):
        print request
        return "app/data/mydata_%s.csv" % (label)

myfunction = MyClass(['a','b','c'])
print myfunction.c('request3')

rawDescription: how to loop list to pass string to function name in python

description: Loop list to pass string to function name in python


code:
for c in df.columns:
    print "---- %s ---" % c
    print df[c].value_counts()

rawDescription: pandas value_counts applied to each column

description: Pandas value_counts applied to each column


code:
def total_potential(z, cracks):
    return sum(c.get_potential(z) for c in cracks)
print total_potential(5.0, crack_list)
print total_potential(3.14, crack_list)

rawDescription: python: iterate using a class method

description: Python: iterate using a class method


code:
myList =[ [(' whether', None), (' mated', None), (' rooster', None), ('', None)] ,
          [(' produced', None), (' without', None), (' rooster', None), (' infertile', None), ('', None)] ]

print [[x[1] for x in el] for el in myList]
# [[None, None, None, None], [None, None, None, None, None]]

rawDescription: How to access tuple elements in a nested list

description: Access tuple elements in a nested list


code:
a = range(10)
for i, j in zip(a, a[1:]):
 print i, j

rawDescription: Is there an easy way to get next and prev values in a for-loop?


code:
for i,j in itertools.combinations([a,b,c],2):
    all_diffs=alldiffs(i,j)
    total=count_total(i,j)
    zero=count_zero(all_diffs)
    total=np.array(total)
    union=map(sub,total,zero)
    zero=np.array(zero).tolist()
    union=np.array(union).tolist()
    union=[list(x) for x in union]
    sim=[[float(aaa) / bbb for (aaa, bbb) in itertools.izip(aa, bb)] \
        for (aa, bb) in itertools.izip(zero, union)]
    sim_comb=sum(sim,[])
    sum_of_sim=sum(sim_comb)
    number_sum=len(sim_comb)
    ave=sum_of_sim/number_sum
    one_ave=1-ave
    print one_ave

rawDescription: Python: How to store for loop result in a list?


code:
import datetime

import yaml

time_format = '%H:%M:%S'

class A(yaml.YAMLObject):
    yaml_tag = u'!A'

    def __init__(self, val):
        self.val = val

    @classmethod
    def to_yaml(cls, dumper, data):
        dict_representation = {
            'val':data.val.strftime(time_format)
        }
        node = dumper.represent_mapping(u'!A', dict_representation)
        return node

    @classmethod
    def from_yaml(cls, loader, node):
        dict_representation = loader.construct_mapping(node)
        val = datetime.datetime.strptime(
            dict_representation['val'], time_format).time()
        return A(val)

if __name__ == '__main__':
    t = datetime.time()
    a = A(t)
    print yaml.dump([t,a], default_flow_style=False)

    loaded = yaml.load(yaml.dump([t,a]))
    print loaded[0] == t
    print loaded[1].val == t

rawDescription: Formatting custom class output in PyYAML


code:
def foo():
    return (2,)

if __name__ == '__main__':
    print foo() is foo()

rawDescription: In Python, when are two objects the same?


code:
from itertools import izip_longest as izip_l, chain
config = {'A': 3, 'B': 4, 'C': 2}

# Reconstruct the list of lists
expanded = [[k] * config[k] for k in config]

# Just zip them and ignore the None
print[item for item in chain.from_iterable(izip_l(*expanded)) if item]

rawDescription: distribute a list in python

description: Distribute a list in python


code:
photos = Photo.objects.filter(status=1).order_by('-position')
for photo in photos:
    tags = photo.tags.all()
    for tag in tags:
        print tag.name

rawDescription: Getting a specific tag object using django-taggit


code:
check = lambda arr, (k, v): any(d.get(k, None) == v for d in arr)
check(array, ('a', 5))
check(array, ('z', 5))

rawDescription: How to check whether a (key, value) pair is in a array which includes lots of dictionaries in Python

description: Check whether a (key, value) pair is in a array which includes lots of dictionaries in Python


code:
#! /usr/bin/env python

import re
from collections import deque

pattern = r'([A-Z]{2,}(?=[A-Z]|$)|[A-Z](?=[a-z]|$))'
chunks = deque(re.split(pattern, 'HDMWhoSomeMONKEYThingXYZ'))

result = []
while len(chunks):
  buf = chunks.popleft()
  if len(buf) == 0:
    continue
  if re.match(r'^[A-Z]$', buf) and len(chunks):
    buf += chunks.popleft()
  result.append(buf)

print ' '.join(result)

rawDescription: Regex divide with upper-case

description: Regex divide with upper-case


code:
from itertools import izip, count
data = ('spam', 'ham', 'eggs', 'monty')
for index, item in izip(count(len(data) - 1, -1), reversed(data)):
    print index, item

rawDescription: How to get reversed enumerate in python2?

description: Get reversed enumerate in python2


code:
class MyModel(db.Model):
  td = TimeDeltaProperty(required=True)

entity = MyModel(td=datetime.datetime.now()-some_datetime)
key = entity.put()

entity = db.get(key)
print entity.td

rawDescription: timedelta convert to time or int and store it in GAE (python) datastore

description: Timedelta convert to time or int and store it in GAE (python) datastore


code:
import re
p = re.compile(ur'\b\d+(?:\.\d+){3}\b', re.MULTILINE)
test_str = u"eth0      Link encap:Ethernet  HWaddr 00:18:F3:BE:18:1E\n          inet addr:192.168.10.15  Bcast:192.168.10.255  Mask:255.255.255.0\n          inet6 addr: fe80::218:f3ff:febe:181e/64 Scope:Link\n          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\n          RX packets:39456 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:45730 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:1000\n          RX bytes:25457904 (24.2 Mb)  TX bytes:6540945 (6.2 Mb)\n          Interrupt:20\n\nlo        Link encap:Local Loopback\n          inet addr:127.0.0.1  Mask:255.0.0.0\n          inet6 addr: ::1/128 Scope:Host\n          UP LOOPBACK RUNNING  MTU:16436  Metric:1\n          RX packets:182 errors:0 dropped:0 overruns:0 frame:0\n          TX packets:182 errors:0 dropped:0 overruns:0 carrier:0\n          collisions:0 txqueuelen:0\n          RX bytes:12812 (12.5 Kb)  TX bytes:12812 (12.5 Kb)"
print re.findall(p, test_str)

rawDescription: python regex to parse ifconfig output of different locales

description: Python regex to parse ifconfig output of different locales


code:
n = 0
while n <= 100:
    x = math.factorial(n) + 1
    if isSquare(x):
        print n
    n = n + 1

rawDescription: Finding if a number is a perfect square

description: Finding if a number is a perfect square


code: print [meta.get('content') for meta in soup.select('meta[itemprop="datePublished"]')]

rawDescription: beautifulsoup to retrieve the date

description: Beautifulsoup to retrieve the date


code: print "\n".join(remove_duplicates(["aa", "bb", "cc", "aa"]))

rawDescription: What's the fastest way to remove duplicate lines in a txt file(and also some lines which contain specific strings) using python?


rawDescription: Converting a List within a List from String to Integers


code: import hashlib; print hashlib.sha256('$1' + 'salt').hexdigest()

rawDescription: Bash args in python

description: Bash args in python


code:
for match in soup.find_all('td', class_='match-details'):
    home_tag = match.find('span', class_='team-home')
    home = home_tag and ''.join(home_tag.stripped_strings)
    score_tag = match.find('span', class_='score')
    score = score_tag and ''.join(score_tag.stripped_strings)
    away_tag = match.find('span', class_='team-away')
    away = away_tag and ''.join(away_tag.stripped_strings)
    if home and score and away:
        print home, score, away

rawDescription: Python Beautifulsoup4 website parsing

description: Python Beautifulsoup4 website parsing


code:
class Buildings(object):
    def __init__(self, function):
        self.function = function
    # Don't name an attribute the same as a method
    def myMethod(self):
        # Use self.function instead of function
        if self.function == '1':
            print 'blablabla'
        elif self.function == '2':
            print 'blablabla'

Granary = Buildings('1')
Granary.myMethod()

rawDescription: Object-Oriented Programming in Python

description: Object-Oriented Programming in Python


code:
from itertools import *

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

c = chain(*[combinations(s, i) for i in range(len(s)+1)])
r = [n for n in c if sum(n) == m]

print r

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


code:
# Import Collections to use Ordered Dictionary
import collections

# Module used to save contacts to database
import json

# The main class
def main():

    # Creates an empty list of contacts
    contacts = collections.OrderedDict()
    loop = True

    # Create a while loop for the menu that keeps looping for user input until loop = 0
    while loop == True:

        # Prints menu for the user in command line
        print """
        Contact Book App
        a) New Contact
        b) List Contacts
        c) Search Contacts
        d) Delete Contact
        e) Quit
         """

        # Asks for users input from a-e
        userInput = raw_input("Please select an option: ").lower()

        # OPTION 1 : ADD NEW CONTACT
        if userInput == "a":
            contact_name = raw_input("Enter name: ").title()

            contacts[contact_name] = {'name': contact_name,
                                     'phone': raw_input("Enter phone: "),
                                     'email': raw_input("Enter email: ")
                                     }

            json.dump(contacts, open('contacts.json','w'))

            print "Contact Added!"
            # For Debugging Purposes
            # print(contacts)

        # OPTION 2 : LIST ALL CONTACTS
        elif userInput == "b":
            print "Listing Contacts"
            try:
                contacts = json.load(open('contacts.json','r'))
                name_keys = list(contacts.keys())
            except:
                contacts = {}

            print "%-30s %-30s %-30s" % ('NAME','PHONE','EMAIL')

            for k in name_keys:
                print "%-30s %-30s %-30s" % (contacts[k]['name'], contacts[k]['phone'], contacts[k]['email'])

        # OPTION 3 : SEARCH CONTACTS
        elif userInput == "c":
            print "Searching Contacts"
            search = raw_input("Please enter name: ")
            # Want to be able to search contacts by name, phone number or email
            try:
                contacts = json.load(open('contacts.json','r'))
            except:
                contacts = []

            try:
                print "%-30s %-30s %-30s" % (contacts[search]['name'], contacts[search]['phone'], contacts[search]['email'])
            except KeyError:
                print "Not Found"

        # OPTION 4 : DELETE A CONTACT
        elif userInput == "d":
            print "Deleting Contact"
            contact_name = raw_input("Enter Contact Name: ")
            contacts = json.loads(open('contacts.json').read())

            try:
                contacts.pop(contact_name)
                json.dump(contacts, open('contacts.json','w'))
            except KeyError:
                print "Contact Not Found"

        # OPTION 5 : QUIT PROGRAM
        elif userInput == "e":
            print "Quitting Contact Book"
            loop = False
        else:
            print "Invalid Input! Try again."

main()

rawDescription: How to delete dictionary from list of dictionaries in JSON

description: Delete dictionary from list of dictionaries in JSON


code:
for i in range(len(job_IDs)):
    print "Job {item:15} {value[0]:>6}.{value[1]:<6} {units:3}".format(item=job_IDs[i]+':', value=memory_used[i].split('.') if '.' in memory_used[i] else (memory_used[i], '0'), units=memory_units[i])

rawDescription: Using Python's Format Specification Mini-Language to align floats


code:
b_names = {record.username for record in List_B}
for record_a in List_A:
    if record_a.username in b_names:
        print "Duplicate username: {0}".format(record_a.username)

rawDescription: Python 2.6: How can I compare two lists of same object types on one particular field, efficiently?


code:
# Find first with 2 neighbors
neighbors = G.successors(0)
for n in neighbors:
    if len(G.predecessors(n)+G.successors(n)) == 2:
        print "Found", n
        break
    nneighbors = set(G.successors(n))
    neighbors.extend(nneighbors)

rawDescription: Identify first node after source node having two neighbors in NetworkX DiGraph


code:
data = np.arange(24).reshape(2, 3, 4)
for dim_0_slice in data: # the first dimension is easy
    print dim_0_slice

for dim_1_slice in np.rollaxis(data, 1): # for the others, roll it to the front
    print dim_1_slice

for dim_2_slice in np.rollaxis(data, 2):
    print dim_2_slice

rawDescription: iterate over slices of an ndarray

description: Iterate over slices of an ndarray


code:
def walk_file_system_test():
    #patterns='*', single_level=False, yield_folders=False, yield_files=True, exclude_dirs='.svn'):
    print "Walk current directory with function defaults."
    for fsnode in walk_file_system(os.getcwd()):
        print fsnode

    #patterns='*', single_level=False, yield_folders=True, yield_files=True, exclude_dirs='.svn'):
    print "Walk current directory with yield_folders=True."
    for fsnode in walk_file_system(os.getcwd(), yield_folders=True):
        print fsnode

    #patterns='*', single_level=False, yield_folders=True, yield_files=False, exclude_dirs='.svn'):
    print "Walk current directory with yield_folders=True, yield_files=False."
    for fsnode in walk_file_system(os.getcwd(), yield_folders=True, yield_files=False):
        print fsnode

    #patterns='*', single_level=False, yield_folders=True, yield_files=False, exclude_dirs='.svn'):
    print "Walk current directory with yield_folders=True, yield_files=False, exclude_dirs='.svn;temp'."
    for fsnode in walk_file_system(os.getcwd(), yield_folders=True, yield_files=False, exclude_dirs='.svn;temp'):
        print fsnode

rawDescription: Os.walk wont work with directory matched with regex


code:
import re
strn = "word hereword word, there word"
search = "word"
print re.findall(r"\b" + search + r"\b", strn)

rawDescription: Match a word in a string using regex - python


code:
div = soup.find("div", {"class": "sorteringsvalg Alle"})
print div.find("input", value="Alle").next_sibling.strip()

rawDescription: parsing returned HTML by beautifulsoup

description: Parsing returned HTML by beautifulsoup


code:
x = FakeMutableString("Foo")
y = x
x.set("Bar")
print y             # Prints 'Bar' -- Change in x is reflected in y

rawDescription: Data-Binding in Python?

description: Data-Binding in Python


code:
def messages(self, msg):
    timer = threading.Timer(60, self.handle_timeout)
    # do other stuff
    # if a move is received in time you can cancel the alarm using:
    timer.cancel()

def handle_timeout(self):
    print "you lose"

rawDescription: Python - How to make a background response timeout function


code:
my_partition['first']  = [0,1,2]
my_partition['second'] = [3,4]
my_partition['third']  = [5,6,7]
my_partition['fourth'] = [8, 9]

# Define a helper to back-out the column that serves as a key for the set-like structure.
# Take 0th element, column index should only be part of one subset.
get_key = lambda x: [k for k,v in group.iteritems() if x in v][0]

# use itertools
import itertools

# Print out the common columns between each pair of groups.
for pair_x, pair_y in itertools.combinations(my_partition.keys(), 2):
    print pair_x, pair_y, (set(map(get_key, my_partition[pair_x])) &
                           set(map(get_key, my_partition[pair_y])))

rawDescription: Finding the sets of columns/rows that overlap


code:
print len("\xe3")
print ord("\xe3")

rawDescription: How to convert a unicode character representation from string to unicode in python?

description: Convert a unicode character representation from string to unicode in python


code:
if job.completed:
    print "Downloading archive"
    job.download_to_file(OUTPUT, chunk_size=1024*1024)

rawDescription: Downloading a large archive from AWS Glacier using Boto


code:
for i in ('foo', 'bar', 'baz'):
    print i
    i = 'spam'

rawDescription: Could I use a for loop instead of a while loop?


code:
import types

class DecoratedMetaClass(type):
    def __new__(meta, classname, bases, classDict):
        def pre():
            print "pre transaction"
        def post():
            print "post transaction"
        newClassDict={}
        for attributeName, attribute in classDict.items():
            if type(attribute) == types.FunctionType:
                def wrapFunc(f):
                    def wrapper(*args):
                        pre()
                        f(*args)
                        post()
                    return wrapper
                newAttribute = wrapFunc(attribute)
            else:
                newAttribute = attribute
            newClassDict[attributeName] = newAttribute
        return type.__new__(meta, classname, bases, newClassDict)

class MyClass(object):

    __metaclass__ = DecoratedMetaClass

    def __init__(self):
        print "init"
    def doBar(self, value):
        print "doing bar "+str(value)
    def doFoo(self):
        print "doing foo"

c = MyClass()
c.doFoo()
c.doBar(4)

rawDescription: Is there a __enter__ and __exit__ equivalent for all functions in a class?


code:
def f():
    foo = 3
    class FunctionObject(object):
        def __call__(self):
            return foo
    return FunctionObject()
print f()() # prints 3, since __call__ has access to the scope where it was defined,
            # though it doesn't have access to the scope where the FunctionObject
            # was created

rawDescription: Can someone explain to me the difference between a Function Object and a Closure


code:
my_random_list = [''.join(random.sample('HELP', 2)) for x in range(10)]
print my_random_list

rawDescription: Producing Random Pairs From A String

description: Producing Random Pairs From A String


code:
class Dtracker(dict):
    def __init__(self,*arg,**kw):
        super(Dtracker,self).__init__(*arg,**kw)
    def __getitem__(self,item):
        print "Accessed %s" % str(item)
        return dict.__getitem__(self, item)

def track( fn ):
    def tracked_fn( **kw ):
        kw= Dtracker( kw )
        fn( kw )
    return tracked_fn

@track
def libfn(kw):
    a = kw["foo"]
    print "a is %s" % a
    return a

rawDescription: Custom dictionary through **kw

description: Custom dictionary through **kw


code: print line

rawDescription: Getting Poorly Formatted Output (Python)


code:
def slice_special(a_list,slice_idx,minimum_value,maximum_value):
     return [item for item in a_list if minimum_value <= item[slice_idx] <= maximum_value]

print slice_special(L,1,2,4)

rawDescription: Python: Iterate through a list by value, not by index


code:
miles = float(raw_input ('Enter Miles: '))
while miles != 0:
    fuelEcon = miles / 20
    fuelCost = float(input ('Enter Cost of Fuel: $'))
    costOfTrip = miles * fuelCost
    fuelIncrease = (fuelCost * .1) + fuelCost
    futureTrip = miles * fuelIncrease

    print "Cost of Trip: $", costOfTrip
    print "Cost of Trip With 10% Increase in Fuel Cost: $", futureTrip

    miles = float(raw_input ('Enter Miles: '))

rawDescription: cannot create a working loop in python

description: Cannot create a working loop in python


code:
from StringIO import StringIO
from csv import DictReader

data1 = """
FirstName Surname Address Notes PurchaseOrder OrderDate
"Bob" "Smith" "123 Lucky Street" "Bad customer" "123ABC", 2013/10/20
"Zoe" "Jackson" "5 Mountain View Street" "Good customer" "abc211" 2014/01/01
""".strip()

data2 = """
FirstName Surname Address PhoneHome PhoneMobile PurchaseOrder OrderDate Total
"Bob" "Smith" "123 Lucky Street" "12345678" "1234567890" "123ABC" 2013/10/20, $100
"Zoe" "Jackson" "5 Mountain View Street" "87654321" "0987654321" "abc211" 2014/01/01 $1000
""".strip()

buf1 = StringIO(data1)
buf2 = StringIO(data2)

reader = DictReader(buf1, delimiter=" ")
for rec in reader:
    print rec

print "---next one comes---"

reader = DictReader(buf2, delimiter=" ")
for rec in reader:
    print rec

rawDescription: Parsing CSV data based on header fields using Pyparsing


code:
import first

def second_func():
    print 'inside second_func in second.py'

if __name__ == '__main__':
    # second.py executed as a script
    second_func()
    first.func1() # executing a function from first.py

rawDescription: Equivalent of source() of R in Python

description: Equivalent of source() of R in Python


code:
from numpy import *
import random
import matplotlib.pyplot as plt

S = 1000
N = 900
# first, make some fake data, with correlations on two spatial scales
#     density map
x = linspace(0, 2*pi, S)
sx = sin(3*x)*sin(10*x)
density = .8* abs(outer(sx, sx))
density[:,:S//2] += .2
#     make a point cloud motivated by this density
random.seed(10)  # so this can be repeated
points = []
while len(points)<N:
    v, ix, iy = random.random(), random.randint(0,S-1), random.randint(0,S-1)
    if True: #v<density[ix,iy]:
        points.append([ix, iy, density[ix,iy]])
locations = array(points).transpose()
print locations.shape
plt.imshow(density, alpha=.3, origin='lower')
plt.plot(locations[1,:], locations[0,:], '.k')
plt.xlim((0,S))
plt.ylim((0,S))
plt.show()
#     build these into the main data: all pairs into distances and z0 z1 values
L = locations
m = array([[math.sqrt((L[0,i]-L[0,j])**2+(L[1,i]-L[1,j])**2), L[2,i], L[2,j]]
                         for i in range(N) for j in range(N) if i>j])

rawDescription: Fast, elegant way to calculate empirical/sample covariogram


code:
global_list = []

def defineAList():
    local_list = ['1','2','3']
    print "For checking purposes: in defineAList, list is", local_list
    return local_list

def useTheList(passed_list):
    print "For checking purposes: in useTheList, list is", passed_list

def main():
    # returned list is ignored
    returned_list = defineAList()

    # passed_list inside useTheList is set to global_list
    useTheList(global_list)

main()

rawDescription: Python: Passing variables between functions


code:
import urllib2
import pandas as pd
import os
import xlrd

url = 'http://www.bankofengland.co.uk/statistics/Documents/yieldcurve/uknom05_mdaily.xls'
socket = urllib2.urlopen(url)

xd = pd.ExcelFile(socket)
df = xd.parse(xd.sheet_names[-1], header=None)

print df

rawDescription: Downloading data from a particular Workbook of a URL with an Excel File


code:
import csv

with open(csvfilename, 'rb') as infile:
    reader = csv.reader(infile)
    for row in reader:
        print repr(row[5])

rawDescription: Remove line break inside line row from CSV with regular expression


code:
def some_func():
    print 'in test 1, unproductive'

if __name__ == '__main__':
    # test1.py executed as script
    # do something
    some_func()

rawDescription: What is the best way to call a python script from another python script?


code:
import numpy as np
positions = np.array([1,1,4,4])
forces = np.array([4,5,8,9])
up = np.unique(positions)
uf = np.fromiter((forces[positions == val].sum() for val in up), dtype=int)

print up, uf

rawDescription: Sum elements in array with NumPy

description: Sum elements in array with NumPy


code:
#!/usr/bin/env python

''' Estimate time of data points falling within specified limits
    From http://stackoverflow.com/q/29430625/4014959
    Written 2015.04.03 by PM 2Ring,
    with help from Antti Haapala and Martijn Pieters
'''

from itertools import groupby

def estimate_time(values, lo_lim, hi_lim, sample_rate):
    #Find values that are in range
    in_range = [lo_lim <= v <= hi_lim for v in values]

    #Find runs of in-range values
    runs = [sum(1 for _ in group) for v, group in groupby(in_range) if v]

    #Estimate total time spent in-range
    total_time = sum(v if v > 1 else 0.5 for v in runs)
    return total_time / sample_rate

values = [1, 2, 3, 4, 5, 6, 7, 8, 4]
sample_rate = 10.0  # in Hz

lo_lim = 3
hi_lim = 5

print estimate_time(values, lo_lim, hi_lim, sample_rate)

rawDescription: Counting data points within limits, and applying buffer to isolated points [data analysis]


code:
from itertools import chain

words = "foo bar baz qux"

w = words.split()
print map(' '.join, chain.from_iterable(zip(*(w[i:] for i in range(i))) for i in range(1, len(w) + 1)))

rawDescription: Break string into words and phrases

description: Break string into words and phrases


rawDescription: matplotlib: Incorrect scale of axis

description: Matplotlib: Incorrect scale of axis


code:
import re
ut0 = re.compile(r'(\d)0+$')

thelist = [ut0.sub(r'\1', "%12f" % x) for x in a]

print '\n'.join(thelist)

rawDescription: Decimal alignment formatting in Python

description: Decimal alignment formatting in Python


code:
from selenium import webdriver

browser = webdriver.Chrome()
browser.get('https://stackoverflow.com/users/login')
username = browser.find_element_by_id("email")
password = browser.find_element_by_id("password")
username.send_keys("emailID")
password.send_keys("password")
browser.find_element_by_name("submit-button").click()
browser.refresh()
print browser.current_url

rawDescription: Extracting user data from stackoverflow using Selenium


code:
TEMPLATE_DIRS = ( "home/kevin/first/tutorial/temps" )
print TEMPLATE_DIRS
type(TEMPLATE_DIRS)

rawDescription: Django Tutorial - TemplateDoesNotExist at /polls/


code:
def check(*args):
    # do something project relevant, so just return True
    return True

def decorate(*categories):
    def wrap(f):
        def wrap_check_categories(*args, **kwargs):
            if check(*categories):
                return f(*args, **kwargs)
            else:
                raise Exception
        wrap_check_categories._args = categories    # <-- store the categories
        return wrap_check_categories
    return wrap

def get_categories(f):
    '''Returns decorator parameters'''
    # ... do some magic here
    return f._args

@decorate('foo', 'bar')
def fancy_func(*args, **kwargs):
    return args, kwargs

def main():
    ## should output ['foo', 'bar']
    print get_categories(fancy_func)

if __name__ == '__main__':
    main()

rawDescription: Get python decorator arguments at run time


code:
newperson = Person(name="Ben", address='9999 Gotham City, las vegas', phone='702-000-0000', gender ='male', prefix ='Waiter')
print newperson.userdata

rawDescription: Initializing Python class with array data


code:
def greetings(self, name):

    print "Hello %s!" % name
    return

rawDescription: Passing an argument to a function in a different script?


code:
import os
script = r"C:/Users/dell/byteyears.py"
head, tail = os.path.split(script)
newpath = os.path.join(head, 'Python27', tail)
print newpath

rawDescription: adding string in others

description: Adding string in others


code:
def hasWord(l, word):
    for w, wordclass in l:
        if w == word:
            return True
    return False

def hasTag(l, tag):
    for w, wordclass in l:
        if wordclass == tag:
            return True
    return False

if hasTag(your_list, 'JJ'): print your_list

rawDescription: List with tuples in python

description: List with tuples in python


code: print sorted(my_list,key=key)

rawDescription: python - sorting a list of lists by a key that's substring of each element

description: Python - sorting a list of lists by a key that's substring of each element


code:
data = json.loads(json_string)
cleanup(data)
json_string = json.dumps(data)
print json_string

rawDescription: How can I strip all the HTML content from a JSON file?

description: Strip all the HTML content from a JSON file


code:
def varied_step_range(start,stop,stepiter):
    step = iter(stepiter)
    while start < stop:
        yield start
        start += next(step)

for i in varied_step_range(0,10,[1,2,3,4]):
    print i

rawDescription: Change step value in range function

description: Change step value in range function


code:
fib_n = int(input("Fib number?"))
fibs = [fib(i) for i in range(fib_n)]
print " ".join(fibs)

rawDescription: How do I print a fibonacci sequence to the nth number in Python?

description: Print a fibonacci sequence to the nth number in Python


code:
in_array = np.asarray([[7, 40], [777, 440]])
for index in np.ndindex(in_array.shape[1:]):
    print "---"
    index = (slice(None),)+index
    print index
    print in_array[index]

rawDescription: numpy.ndindex and array slices

description: Numpy.ndindex and array slices


code:
import json

js="""
{"widget": {
        "debug": "on",
        "window": {
            "title": "Sample Konfabulator Widget",
            "name": "main_window",
            "width": 500,
            "height": 500
        }

    }}

"""
l=[{"Name": "abcd"}, {"Name": "efgh"}, {"Name": "hijk"}]

l_dict=json.loads(json.dumps(l))

js_dict = json.loads(js)
js_dict['widget']['window']['name']=l_dict[0]['Name']

print js_dict
print js_dict['widget']['window']['name']

rawDescription: How to add json array to a json document in python?

description: Add json array to a json document in python


code:
input = [
    {'Message_content': {
            'Argument': [{ 'Arg_name': 'acl_name' }, { 'Arg_name': 'acl_type' }],
            'Message': { 'Message_content': { 'Argument': [{ 'Arg_name': 'cat_name' }, { 'Arg_name': 'cat_enum' }] } }
    }},
    { 'Message_content': { 'Argument': [{ 'Arg_name': 'ac_name' }, { 'Arg_name': 'ac_type' }] } }
]

def handle_element(elem, output):
    try:
        arg = elem["Message_content"]["Argument"]
        output.append(arg)
    except KeyError:
        pass
    try:
        msg = elem["Message_content"]["Message"]
        print "nested"
        handle_element(msg, output)
    except KeyError:
        print "not nested"

out = []
for elem in input:
    handle_element(elem, out)

print out

rawDescription: Convert list of nested dictionary to a flat list of dictionary


code:
activites = sorted(activities,key = lambda item:int(item[1]))
print activities

rawDescription: Why am I not getting the the result of sorted function in expected order ?


rawDescription: Python ElementTree XML Modifying Elements with Multiple Values


code:
class IconButton(ButtonBehavior, Image):

    def __init__(self, angle=0, **kwargs):
        super(IconButton, self).__init__(**kwargs)

        self.canvas.before.add(PushMatrix())

        self.canvas.add(Color(rgba=(1,0,0,.5)))
        self.canvas.add(Line(rectangle=(self.x, self.y, self.width, self.height)))

        self.canvas.after.add(PopMatrix())

    def on_press(event):
        event.source = event.img_dn
        print '{} pressed'.format(event.id)

    def on_release(event):
        event.source = event.img_up
        print '{} released'.format(event.id)

rawDescription: Kivy: Rotate clickable image

description: Kivy: Rotate clickable image


code:
url = 'http://www.webservicex.net/stockquote.asmx?WSDL'
client = Client(url=url)
name = 'GetQuote'
impl = getattr(client.service, name)
print impl('IBM')

rawDescription: Unable utilize a methodname(from webservice)present in the form of a string.I tried utilizing getattr,but then not unable to extend it for suds


code:
import re
regex.sub(lambda(m) : m.group(2) + m.group(1), text)

rawDescription: Swap a character with its next character in paragraph


code:
s = "Hello World!"
print s[2:] # From the second (third) letter, print the whole string
print s[2:5] # Print from the second (third) letter to the fifth string
print s[-2:] # Print from right to left
print s[::2] # Print every second letter

rawDescription: Python left() equivalent?

description: Python left() equivalent


code:
import difflib

fileA = open("a.txt", "rt").readlines()
fileB = open("b.txt", "rt").readlines()

d = difflib.Differ()
diffs = d.compare(fileA, fileB)
lineNum = 0

for line in diffs:
   # split off the code
   code = line[:2]
   # if the  line is in both files or just b, increment the line number.
   if code in ("  ", "+ "):
      lineNum += 1
   # if this line is only in b, print the line number and the text on the line
   if code == "+ ":
      print "%d: %s" % (lineNum, line[2:].strip())

rawDescription: getting line-numbers that were changed

description: Getting line-numbers that were changed


code:
import time,threading,random

class func_thread(threading.Thread):
   def __init__(self,callback):
      self.callback = callback
      self.killed = False
      threading.Thread.__init__(self)
   def Kill(self):
      self.killed = True
   def run(self):
      while not self.killed:
          self.callback(func())

def func():
    time.sleep(random.uniform(0.5,2.0))
    return 5

seconds = 1
minutes = 60
update_interval = 25 * seconds
done_time = 5 * minutes
update_format ="# Results:%04d  Seconds Elapsed:%0.3f"
def main():
    my_results = []
    start_time = time.time()
    fs = func_thread(my_results.append)
    fs.start()
    while time.time() - start_time < done_time:
         print update_format%(len(my_results),time.time()-start_time)
         time.sleep(update_interval)
    fs.Kill()
    fs.join()
    print "DONE FINAL COUNT:",len(my_results)

rawDescription: Dynamic Output of time progression

description: Dynamic Output of time progression


code:
import datetime

string = "19 Nov 2015  18:45:00.000"
date = datetime.datetime.strptime(string, "%d %b %Y  %H:%M:%S.%f")

print date

rawDescription: Date Time split in python

description: Date Time split in python


code:
while month < 13:
    print "Month: " + str(month)
    print "Minimum monthly payment: " + str(round(minimum_monthly_payment, 2))
    balance = (balance - (balance*monthlyPaymentRate))*(1+(annualInterestRate/12))
    print "Remaining balance: " + str(round(balance, 2))
    month += 1

rawDescription: Program to count balance of credit

description: Program to count balance of credit


code:
li1 = [1,2]
li = [1,2]
li1 = li
li2 = li[:]
print li, li1, li2
li[0] = 0
print li, li1, li2

rawDescription: Python why would you use [:] over =

description: Python why would you use [:] over =


code: print "\n".join(regex)

rawDescription: Python re.findall prints list instead of string


code:
LEADING_BIT_MASK =  0b10000000
VALUE_MASK = 0b01111111

bytes = [0b10101010, 0b01010101, 0b0000000, 0b10000000]

for byte in bytes:
    value = byte & VALUE_MASK
    has_leading_bit = byte & LEADING_BIT_MASK
    if value == 0:
        print "EOL"
    elif has_leading_bit:
        print "leading one", value
    elif not has_leading_bit:
        print "leading zero", value

rawDescription: Python: Extracting bits from a byte

description: Python: Extracting bits from a byte


code:
def func(tup):
    print """
    Start %s
    End %s
    """ % tup

rawDescription: Loop through list of tuples, call elements independently


code:
mylist = [[15], [14, 15], [19, 20], [13], [3], [65, 19], [19, 20, 31]]
my_set_list = [set(item) for item in mylist]
for i1, item in enumerate(my_set_list):
    for i2 in xrange(i1 + 1, len(my_set_list)):
        for val in (item & my_set_list[i2]):
            print "Index {} matched with index {} for value {}".format(i1,i2,val)

rawDescription: Python: Compare elements in a list to each other


code: print func.__name__

rawDescription: Get function name as a string in python

description: Get function name as a string in python


rawDescription: String replacement using Python 3 re.sub


code:
list_1 = [1, 2, 3, 4]
list_2 = [5, 6, 7, 8]
list_3 = [9, 10, 11, 12]
list_4 = [13, 14, 15, 16]
main_list = [list_1, list_2, list_3, list_4]
for list in main_list:
    for num in list:
            print num

rawDescription: Storing the result of Minhash

description: Storing the result of Minhash


code:
import random
for i in xrange(1,len(urls)):
    print random.sample(urls,i)

rawDescription: How to obtain this behavior in python

description: Obtain this behavior in python


code:
import threading, Queue

def func1(num, q):
    while num < 100000000:
        num =  num**2
        q.put(num)

def func2(num, q):
    while num < 100000000:
        num = q.get()
        print num,

num = 2
q = Queue.Queue()
thread1 = threading.Thread(target=func1,args=(num,q))
thread2 = threading.Thread(target=func2,args=(num,q))
print 'setup'
thread1.start()
thread2.start()

rawDescription: How to share a variable between 2 threads

description: Share a variable between 2 threads


code:
#!/usr/bin/env python

import csv
reader = csv.reader(open('4956984.csv', 'rb'))

def gen_chunks(reader, chunksize=100):
    """
    Chunk generator. Take a CSV `reader` and yield
    `chunksize` sized slices.
    """
    chunk = []
    for i, line in enumerate(reader):
        if (i % chunksize == 0 and i > 0):
            yield chunk
            del chunk[:]
        chunk.append(line)
    yield chunk

for chunk in gen_chunks(reader):
    print chunk # process chunk

# test gen_chunk on some dummy sequence:
for chunk in gen_chunks(range(10), chunksize=3):
    print chunk # process chunk

# => yields
# [0, 1, 2]
# [3, 4, 5]
# [6, 7, 8]
# [9]

rawDescription: How do you split a csv file into evenly sized chunks in Python?

description: Split a csv file into evenly sized chunks in Python


code:
for f in grouper(files, 2):
   print f # f here is a tuple of two files. f[0] is the first and f[1] is the second one

rawDescription: How can I isolate part of a string in a list of strings and sort the list with it? (in Python)

description: Isolate part of a string in a list of strings and sort the list with it? (in Python)


code:
import itertools
a = [1,2,3]
b = [4,5,6]
c = [7,8,9]
l = [a,b,c]
print [x for x in itertools.product(*l)]

rawDescription: Write the same nested loop n levels deep


code:
seq = ["OK", "bad text2", "OK", "Also OK"]
print "\n".join(remove_duplicates(ifilter(is_line_ok, seq)))

rawDescription: What's the fastest way to remove duplicate lines in a txt file(and also some lines which contain specific strings) using python?


code:
from bs4 import BeautifulSoup

page = """

 good1
 1

 good2
 2

"""

soup = BeautifulSoup(page)
rows = soup.body.find_all('tr')
print [td.text.strip() for row in rows for td in row.find_all('td')]

rawDescription: BeautifulSoup help, how to extract content from not proper tags text in html file?


code:
class NotEnoughMagic(Exception):
    pass

class Character(object):
    fire = 0
    magic = 0
    hp = 100
    armor = 1
    def take_damage(self, amount):
        self.hp -= amount
        if self.hp <= 0:
            print '%s HAS DIED!' % type(self).__name__
    def walk(self):
        pass
    def attack(self, target, attack_points):
        target.take_damage(attack_points / target.armor)

class Monster(Character):
    fire = 50
    hp = 150
    armor = 2
    def punch(self, target):
        self.attack(target, 25)

class Magician(Character):
    magic = 10
    def magic_armor(self):
        self.hp += 20
        self.magic -= 10
    def sword_attack(self, target):
        self.attack(target, 20)
    def magic_attack(self, target):
        """Does tons of damage but uses all magic"""
        if self.magic > 0:
            self.attack(target, 100)
            self.magic = 0
        else:
            raise NotEnoughMagic()

rawDescription: Sharing Class Variables

description: Sharing Class Variables


code:
list1 = [1,2,3,4,5,6]
dict1 = {'a':[1,11],'b':[2,234],'c':[34,6]}
from itertools import chain
set1 = set(list1)
set2 = set(v for v in chain.from_iterable(dict1.values()))
print list(set1 - set2)

rawDescription: python - filter function for nested loop and if condition

description: Python - filter function for nested loop and if condition


code:
store = [
  {'url': 'n0:project:film:reel1_c09', 'range': '12-120', 'version': '09', 'name': 'reel1'},
  {'url': 'n0:project:film:reel1_c09', 'range': '12-120', 'version': '10', 'name': 'reel1'},
  {'url': 'n0:project:film:reel2_c09', 'range': None, 'version': '09', 'name': 'reel2'}
]

l = [reduce(lambda x, y: x if int(x['version']) > int(y['version']) else y, filter(lambda e: e['url'] == n, store)) for n in {d['url'] for d in store}]
print l

[{'url': 'n0:project:film:reel2_c09', 'range': None, 'version': '09', 'name': 'reel2'}, {'url': 'n0:project:film:reel1_c09', 'range': '12-120', 'version': '10', 'name': 'reel1'}]

rawDescription: Filtering python list of duplicated

description: Filtering python list of duplicated


code:
folder = "C:/Documents and Settings/DuffA/Bureaublad/shortcuts projects/klic01/11G008689_1"

import os
separ = os.sep

for n in os.listdir(folder):
    print n
    if os.path.isfile(folder + separ + n):
        filename_zero, extension = os.path.splitext(n)
        os.rename(folder + separ + n , folder + separ + filename_zero.replace('.','_').replace('+','_') + extension)

print '\n--------------------------------\n'
for n in os.listdir(folder):
    print n

rawDescription: find and replace within a filename

description: Find and replace within a filename


code:
result = []
for items in x:
    temp = []
    for item in items:
        temp.append(list(item))
    result.append(temp)
print result
# [[[1, 2, 3], [10, 9, 8]]]

rawDescription: how to change the inner element tuple into list?

description: Change the inner element tuple into list


code:
def altera(L1, L2):
    for elemento in L2:
        # create a new list and assign name L1
        L1 = L1 + [elemento]
    # create a new list and assign name L2
    L2 = L2 + [4]
    return L2

Lista1 = [1,2,3]
Lista2 = [1,2,3]

Lista3 = altera(Lista1, Lista2)

print Lista1
print Lista2
print Lista3

rawDescription: Difference between local and global variables in python


code:
import os
import datetime
import shutil
from shutil import copytree, ignore_patterns

files = os.listdir('/media/user/')

destination = '/home/user/Path/Backup/back_%s'%datetime.datetime.now()
try :
    for f in files:
        source = '/media/user/%s' % f
        copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
except Exception as e:
    print e

rawDescription: Python - How to copy content of USB(Flash) to system directory


code:
import ConfigParser

parser = ConfigParser.RawConfigParser()
parser.read("config.ini")
#Do your normal config processing here
#When it comes time to audit default vs. explicit,
#clear the defaults
parser._defaults = {}
#Now you will see which options were explicitly defined
print parser.options("Section 1")
print parser.options("Section 2")

rawDescription: Python ConfigParser: how to work out options set in a specific section (rather than defaults)


code:
if row[0] != checkID:
    if output != "":
        print output, "this is a test"

    checkID=row[0]
    output=row[0:11]

else:
    output=output + (row[8:11])

rawDescription: sql query in python--debugging appending values

description: Sql query in python--debugging appending values


code:
from scipy import stats
import matplotlib.pyplot as plt
plt.plot(data, stats.exponweib.pdf(data, *stats.exponweib.fit(data, 1, 1, scale=02, loc=0)))
_ = plt.hist(data, bins=np.linspace(0, 16, 33), normed=True, alpha=0.5);
plt.show()

rawDescription: Fitting a Weibull distribution using Scipy


code:
import copy

class Widget:
    def __init__(self, value):
        self.value = value

a = [Widget(4), Widget(8), Widget(15)]
b = copy.deepcopy(a[0:1])

print a[0] is b[0]
#result: False

rawDescription: How to build a new list from a list in Python?

description: Build a new list from a list in Python


rawDescription: why does python new york time zone display 4:56 instead 4:00?

description: Why does python new york time zone display 4:56 instead 4:00


code:
import serial, time

station1 = None
station2 = None

try:
    station1 = serial.Serial('/dev/ttyUSB0', 9600,timeout=0)
    print 'station 1 Is Ready!! Please Show your Card'
except Exception,e:
        print 'Station 1 is Down',e

try:
    station2 = serial.Serial('/dev/ttyUSB1', 9600,timeout=0)
    print 'station 2 Is Ready!! Please Show your Card'
except Exception,e:
        print 'Station 2 is Down',e

while True:
    if station1 != None:
        card_dataa1 = station1.read(12)
        if len(card_dataa1) > 0:    print card_dataa1
    if station2 != None:
        card_dataa2 = station2.read(12)
        if len(card_dataa2) > 0:    print card_dataa2

rawDescription: Jumbled out put of RFID Tags

description: Jumbled out put of RFID Tags


code:
import xml.dom.minidom

data = """<node-a>
    <node-1/>
    <node-2/>
    <node-3/>
</node-a>"""

dom = xml.dom.minidom.parseString(data)
node_a = dom.getElementsByTagName('node-a')[0]

node_4 = dom.createElement('node-4')
node_a.appendChild(node_4)

node_3 = dom.getElementsByTagName('node-3')[0]
node2_a = dom.createElement('node-2a')
node_a.insertBefore(node2_a, node_3)

print dom.toprettyxml()

rawDescription: Append child in the middle

description: Append child in the middle


code:
print df
#     a     b
#0  aaa  rrrr
#1   bb     k
#2  ccc     e
#condition if condition is True then len column a else column b
df['c'] = np.where(df['a'].map(len) > df['b'].map(len), df['a'].map(len), df['b'].map(len))
print df
#     a     b  c
#0  aaa  rrrr  4
#1   bb     k  2
#2  ccc     e  3

rawDescription: Creating a new column in Panda by using lambda function on two existing columns


code:
print df.columns.str.split('_').str[0].unique()
['a' 'b' 'c']

for x in df.columns.str[0].unique():
    df[x + '_ratio'] = df[x + '_us']/df[x + '_ww']

rawDescription: Working in Pandas with variable names with a common suffix


code:
s = "0.0.0.0 \nThis subnet mask is non-routable\nThere is no network portion"
print s

rawDescription: Continue printed text to next line in Python


code:
uber_dict = {}
grouped =  df.groupby(['ID'])
for name, group in grouped:
    mini_dict = {}
    working_list = []
    i = 1
    for idx,row in group.iterrows():
        working_list.append((row['CATEGORY'],row['DATE']))
        if row['SUCCESS'] == 1:
            df = pd.DataFrame(working_list,columns= ['CATEGORY','DATE']) # let's use a bit more pandas.
            df.set_index('CATEGORY',inplace=True)
            df["DATE"] = pd.to_datetime(df["DATE"]) # my dates are string format so casting
            df['DATE'] = df['DATE'].diff()
            df['DATE'].fillna(0,inplace=True)
            df['DATE'] = df['DATE'].astype('timedelta64[D]').astype('int')
            mini_dict[str(i)] = tuple(zip(*df.to_records()))
            working_list=[]
            i = i +1

    if mini_dict.keys(): # see http://stackoverflow.com/questions/53513/best-way-to-check-if-a-list-is-empty
        uber_dict[name] = mini_dict

print uber_dict

rawDescription: Pandas Grouping and Reducing DataFrame

description: Pandas Grouping and Reducing DataFrame


code:
import ast
from operator import itemgetter

def parse_item(s):
  name, score = ast.literal_eval(s)
  return name, int(score)

with open("infile", "r") as infile:
  items = [parse_item(line.strip()) for line in infile]

for item in sorted(items, key=itemgetter(1), reverse=True):
  print item

rawDescription: How to order these lines for a highscore table in Python

description: Order these lines for a highscore table in Python


code:
import my_python_module as q
import p

y = p.Y(q.X())
print y.x
print y.x.i

rawDescription: Cython: how to make an python object as a property of cython class


code:
#!/usr/bin/env python

#Create a weighted mean function with weights w1 & w2
def make_weighted_mean(w1, w2):
    wt = float(w1 + w2)
    def wm(a, b):
        return (w1 * a + w2 * b) / wt
    return wm

#Make the weighted mean function
weighted_mean = make_weighted_mean(1, 2)

#Test
print weighted_mean(6, 3)
print weighted_mean(3, 9)

rawDescription: Retrieve the ranking of elements in various list to compute the weighted average of their ranking scores Python


code:
import numpy as np
import ctypes as C

# allocate this as a normal numpy array with specified dtype
array_1d_double = np.array([1,2,3,4,5],dtype="float64")

# set the structure to contain a C double pointer
class Test(C.Structure):
    _fields_ = [("x", C.POINTER(C.c_double))]

# Instantiate the structure so it can be passed to the C code
test = Test(np.ctypeslib.as_ctypes(array_1d_double))

# You can also do:
# test = Test()
# test.x = np.ctypeslib.as_ctypes(array_1d_double)

print test.x
# outputs: <__main__.LP_c_double object at 0x1014aa320>

rawDescription: Access np.array in ctypes struct

description: Access np.array in ctypes struct


code:
from win32com.client import Dispatch
wbpath = 'C:\\example.xlsm'
xl = Dispatch("Excel.Application")
xl.Visible = 1
wb = xl.Workbooks.Open(wbpath)
vbcode = wb.VBProject.VBComponents(1).CodeModule
print vbcode.Lines(1, vbcode.CountOfLines)

rawDescription: Using Python to read VBA from an Excel spreadsheet


code:
import mechanize, fileinput

urls = open('F:\Python\url_list2.txt')
content = urls.readlines()
print content

rawDescription: why am I getting a 'closed file' message when I run my python script?

description: Why am I getting a 'closed file' message when I run my python script


rawDescription: heap sort in Python for advice

description: Heap sort in Python for advice


code: print sorted(d.items(), key=lambda item:item[0][1])

rawDescription: How to sort a dictionary by value where the key is a tuple

description: Sort a dictionary by value where the key is a tuple


code:
import subprocess
import sys

inname = 'foo.mp3'
outname = 'out.wav'
try:
    subprocess.check_call(['mpg123', '-w', outname, inname])
except CalledProcessError as e:
    print e
    sys.exit(1)

rawDescription: Read amplitude data from mp3

description: Read amplitude data from mp3


code:
def begin_grab(self):
    self.GrabIm=MyDialog(self)
    if self.GrabIm.exec_():  # this will be True if dialog is 'accept'ed, False otherwise
        if self.GrabIm.ui.radioButton.isChecked():
            #Call continuous grabber
            print "Grabbing continuously"
        else:
            #Call trigger server
            print "Grabbing triggered"

rawDescription: PyQt4 - closing a dialog window, exec_() not working


code:
print fm
print fm.instance._meta.get_field_by_name('fieldnamehere')

rawDescription: How to check the type of a many-to-many-field in django?

description: Check the type of a many-to-many-field in django


code:
from bs4 import BeautifulSoup
import html5lib
from html5lib import sanitizer
from html5lib import treebuilders
import urllib

url = 'http://the-url-to-scrape'
fp = urllib.urlopen(url)

# Create an html5lib parser. Not sure if the sanitizer is required.
parser = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("beautifulsoup"), tokenizer=sanitizer.HTMLSanitizer)
# Load the source file's HTML into html5lib
html5lib_object = parser.parse(file_pointer)
# In theory we shouldn't need to convert this to a string before passing to BS. Didn't work passing directly to BS for me however.
html_string = str(html5lib_object)

# Load the string into BeautifulSoup for parsing.
soup = BeautifulSoup(html_string)

for content in soup.findAll('div'):
    print content

rawDescription: malformed start tag error - Python, BeautifulSoup, and Sipie - Ubuntu 10.04

description: Malformed start tag error - Python, BeautifulSoup, and Sipie - Ubuntu 10.04


code:
words= 'military  troop  deployment  number  need'
sent = words.split()
linenos = {}

with open("file2", "r") as f1:
    for i, line in enumerate(f1):
        for word in sent:
            if word in line:
                if word in linenos:
                    linenos[word].append(i + 1)
                else:
                    linenos[word] = [i + 1]

matched2 = []
for word in sent:
    if word in linenos:
        matched2.append('%s %r' % (word, linenos[word][0]))
    else:
        matched2.append('%s <does not exist>' % word)

print ', '.join(matched2)

rawDescription: Get line numbers of words present in a file


code:
import json
json_docs = json.load(response)
print json_docs

rawDescription: How to get urllib2 response in json

description: Get urllib2 response in json


code: print first_list + list(set(second_list) - set(first_list))

rawDescription: Combining two lists and removing duplicates, without removing duplicates in original list


code:
def setup_module():
    print "common_setup"

def teardown_module():
    print "common_teardown"

def test_1():
    print "test_1"

def test_2():
    print "test_2"

rawDescription: Setup and teardown functions executed once for all nosetests tests


code: print s.encode('unicode-escape')

rawDescription: encode unicode characters to unicode escape sequences

description: Encode unicode characters to unicode escape sequences


code:
import sys

four = sys.argv[4]
print four[2]

rawDescription: How to pass a list and other values as command line arguments?

description: Pass a list and other values as command line arguments


rawDescription: Pandas DataFrame group by value and get column & row indexes


code:
import cgi
import urlparse
url = "http://example.com/?a=text&q2=text2&q3=text3&q2=text4"
qs = cgi.parse_qs(urlparse.urlparse(url)[4])
del(qs['q2'])
print qs

rawDescription: How do I remove a query string from URL using Python

description: Remove a query string from URL using Python


code:
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.svm import LinearSVC
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.multiclass import OneVsRestClassifier
from sklearn import preprocessing
from sklearn.metrics import accuracy_score

X_train = np.array(["new york is a hell of a town",
                "new york was originally dutch",
                "the big apple is great",
                "new york is also called the big apple",
                "nyc is nice",
                "people abbreviate new york city as nyc",
                "the capital of great britain is london",
                "london is in the uk",
                "london is in england",
                "london is in great britain",
                "it rains a lot in london",
                "london hosts the british museum",
                "new york is great and so is london",
                "i like london better than new york"])
y_train_text = [["new york"],["new york"],["new york"],["new york"],
                ["new york"],["new york"],["london"],["london"],
                ["london"],["london"],["london"],["london"],
                ["new york","England"],["new york","london"]]

X_test = np.array(['nice day in nyc',
               'welcome to london',
               'london is rainy',
               'it is raining in britian',
               'it is raining in britian and the big apple',
               'it is raining in britian and nyc',
               'hello welcome to new york. enjoy it here and london too'])

y_test_text = [["new york"],["new york"],["new york"],["new york"],["new york"],["new york"],["new york"]]

lb = preprocessing.MultiLabelBinarizer(classes=("new york","london","England"))
Y = lb.fit_transform(y_train_text)
Y_test = lb.fit_transform(y_test_text)

print Y_test

classifier = Pipeline([
('vectorizer', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', OneVsRestClassifier(LinearSVC()))])

classifier.fit(X_train, Y)
predicted = classifier.predict(X_test)
print predicted

print "Accuracy Score: ",accuracy_score(Y_test, predicted)

rawDescription: Using MultilabelBinarizer on test data with labels not in the training set


code: print df.to_string(index=False)

rawDescription: Pandas dataframe hide index functionality?


code:
print min([None, 1,2])
None < 1

rawDescription: List minimum in Python with None?

description: List minimum in Python with None


code:
def f(x):
    return x + 2

def g(y):
    return f(y * 2)

print g(1)

rawDescription: Return Statements in Recursion

description: Return Statements in Recursion


code:
aarr = {}
aarr["x","y","z"]=1
if ("x","y","z") in aarr:
    print 'success!'

aarr.keys()
aarr.keys()[0]

rawDescription: How to tell if non-scalar key exists in Python dictionary

description: Tell if non-scalar key exists in Python dictionary


code:
import mechanize

import requests
from bs4 import BeautifulSoup, SoupStrainer
import urlparse

import pprint

#-- Mechanize --
br = mechanize.Browser()

def get_links_mechanize(root):
    links = []
    br.open(root)

    for link in br.links():
        try:
            if dict(link.attrs)['class'] == 'page':
                links.append(link.absolute_url)
        except:
            pass
    return links

#-- Requests / BeautifulSoup / urlparse --
def get_links_bs(root):
    links = []
    r = requests.get(root)

    for link in BeautifulSoup(r.text, parse_only=SoupStrainer('a')):
        if link.has_attr('href') and link.has_attr('class') and 'page' in link.get('class'):
            links.append(urlparse.urljoin(root, link.get('href')))

    return links

#with open("C:\Users\Administrator\Desktop\\3.txt","r") as f:
#    for root in f:
#        links = get_links(root)
#        # <Do something with links>
root = 'http://www.apartmentguide.com/apartments/Alabama/Hartselle/'

print "Mech:"
pprint.pprint( get_links_mechanize(root) )
print "Requests/BS4/urlparse:"
pprint.pprint( get_links_bs(root) )

rawDescription: Get list of all paginated URL's from links in txt file in python requests


code:
from operator import itemgetter, attrgetter
from copy import deepcopy

student_list1 = [('tiffany', 'A', 15),
            ('jane', 'B', 10),
            ('ben', 'C', 8),
            ('simon', 'A', 21),
            ('eugene', 'A', 21),
            ('john', 'A', 15),
            ('jimmy', 'F', 1),
            ('charles', 'C', 9),
            ('freddy', 'D', 4),
            ('dave', 'B', 12)]

def get_sorted(student_list):
    q = sorted(student_list1, key=itemgetter(2,0), reverse=True)
    # Now that the list is sorted according to score and
    # name reversed, (meaning that while simon and eugene
    # will both be at the top, simon will always come first
    # we need to swap nearby names according to their value.
    for i in xrange(0, len(q)-1):
        if q[i][0] > q[i+1][0] and q[i][2] is q[i+1][2]:
            temp = q[i+1]
            q[i+1] = q[i]
            q[i]   = temp
    return q

print 'correct sorted order'
for i in get_sorted(student_list1):
    print i

q = get_sorted(student_list1)
our_dict = dict.fromkeys( [x[2] for x in q], [] )
for i in q:
    our_dict[i[2]] = our_dict[i[2]] + [i]

print 'grouped together'
print our_dict

# this gets some number of records from the dict
# according to the specifications of your program
def get_some(number):
    buff = []
    for i in sorted(our_dict.keys(), reverse=True):
        if len(buff) < number:
            buff += our_dict[i]

    return buff

print "let's get \'five\' elements from the list, like in your example"
print get_some(5)
print
print "let's get \'three\' elements from the list, like in your example"
print get_some(3)

rawDescription: Syntax in sorting code Python

description: Syntax in sorting code Python


code:
def loop(self):
    vTest = Source.objects.get(id=self.id)
    while vTest:
        with transaction.atomic():
            vTest = Source.objects.get(id=self.id).state
            print 'looping'
            time.sleep( 1 )
    print 'stopped looping'

rawDescription: Django attribute is not up to date

description: Django attribute is not up to date


code:
# recursively get all subclasses of a given class
def all_subclasses(cls):
    return cls.__subclasses__() + [g for s in cls.__subclasses__()
                                   for g in all_subclasses(s)]

class B(object):
    def foo(self): print '''some cool stuff here in B'''

class C(B):
    def foo(self): print '''very cool stuff in C'''

class D(B):
    def foo(self): print '''very cool stuff in D'''

class E(D):
    def foo(self): print '''very cool stuff in E'''

class A(object):
    def __init__(self):
        self._classes = [cls() for cls in all_subclasses(B)]

    def magic(self):
        for c in self._classes: c.foo()

# usage:
A().magic()

rawDescription: A Python Puzzler

description: A Python Puzzler


code:
import re

quoted = re.compile(r'search:(?:[\'"])([^"\']+)')
regex = re.compile(r'search:r(?:[\'"])([^"\']+)')
unquoted = re.compile(r'search:(?!r[\'"])([^ \'"]+)')

test_str = "search:foo search:romeo "\
           "search:'bar' search:\"baz\" "\
           "search:r'blah' search:r\"bleh\""\
           "search:isn'it something to catch ?"

"""
•search:'pattern' and search:"pattern" (quoted search)
•search:r'pattern' and search:r"pattern" (regex search)
•search:pattern (unquoted search)

"""
print quoted.findall(test_str)
print
print regex.findall(test_str)
print
print unquoted.findall(test_str)

rawDescription: negative lookbehind regex assertion in python

description: Negative lookbehind regex assertion in python


code:
import re

s="123 Some Road Name\nTown, City\nCounty\nPA23 6NH\n123 Some Road Name\nTown, City"\
    "County\nPA2 6NH\n123 Some Road Name\nTown, City\nCounty\nPA2Q 6NH"

#custom
print re.findall(r'\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\b', s)

#regex from #http://en.wikipedia.orgwikiUK_postcodes#Validation
print re.findall(r'[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}', s)

rawDescription: Python, Regular Expression Postcode search


code:
import sys
def my_func(arg1, arg2, arg3=sys.argv[1]):
    print arg1, arg2, arg3

rawDescription: Have command line argument as a default function argument


code:
#!/usr/bin/env python
"""
Farmers market

Usage:
  farmersmarket.py buy -i <item> -q <quantity> [<quantity>] [-p <price>] [-dvh]
  farmersmarket.py -d | --debug
  farmersmarket.py -v | --version
  farmersmarket.py -h | --help

Options:
  -i --item           Item.
  -q --quantity       Quantity.
  -p --price <price>  Price.
  -d --debug          Show debug messages.
  -h --help           Show this screen.
  -v --version        Show version.
"""

from docopt import docopt

print docopt(__doc__)

rawDescription: Docopt: options after repeating elements are interpeted as repeating elements


code:
import cProfile, pstats, StringIO
pr = cProfile.Profile()
pr.enable()

guard_condition = int(raw_input("Enter guard_condition: "))

d = {item: item for item in xrange(10000000)};

new_d = {key: value for key, value in d.iteritems() if key >= guard_condition }

def del_iter(d, guard_condition):
    for key in d.keys():
        if key < guard_condition:
            del d[key]

del_iter(d, guard_condition)

pr.disable()
s = StringIO.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print s.getvalue()

rawDescription: Python: fastest strategy for remove a lot of keys from dict


code: print 'Max: ', prim1[:1].values[0]

rawDescription: stripping index values in pandas

description: Stripping index values in pandas


rawDescription: Finding cosine similarity between 2 numbered datasets using Python


code:
import BeautifulSoup as bs

content = '''

Account Balance

                    Daily Earnings

                        150

                    Weekly Earnings

                        500

                    Monthly Earnings

                        1500

                    Total expended

                        430

                    Account Balance

                        840

                            <form id="request_bill" method="POST" action="index.php?page=dashboard">
                                <input type="hidden" name="secret_token" value="" />
                                <input type="hidden" name="request_payout" value="1" />
                                <input type="submit" class="btn blue large" value="Request Payout" />
                            </form>

'''

def parseo(archivohtml):
    html = archivohtml
    parsed_html = bs.BeautifulSoup(html)
    par = parsed_html.find('a', attrs={'id':'west1'}).findNext('td')
    print par.string.strip()

parseo(content)

rawDescription: Desperate parsing HTML with BeautifulSoup in Python


code:
def foo():
    print bar

exec foo.func_code in {"bar": "baz"}

rawDescription: How can I set the function environment for a Python function?

description: Set the function environment for a Python function


code:
print a[:, :, np.newaxis].shape
# (3, 2, 1)

rawDescription: understanding numpy's dstack function

description: Understanding numpy's dstack function


rawDescription: Django Query using .order_by() and .latest()


code: print array[1][0]

rawDescription: Array within an array?

description: Array within an array


code:
with open(csvFile, 'rb') as f:
    reader = csv.DictReader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
    print reader.fieldnames

rawDescription: DictReader, No quotes, tabbed file

description: DictReader, No quotes, tabbed file


code:
import sys
print sys.argv

for a in sys.argv:
    if a=="test1":
        print "Do something"
    elif a=="test2":
        print "Do something else"

rawDescription: Send Arguments from Batch File to Jython/Python


code: print a["x"]

rawDescription: NumPy - Set values in structured array based on other values in structured array


code:
newTable = []
Intr     = ''
LineCt   = 0

for line in fetch_lines:
    if line[2].startswith('#') or line[2].startswith('----'):
        # Within this if statement you can make adjustment to text item
        if line[2].startswith('#'):
           Intr =  Intr + " Cortar em dois"
           LineCt +=1
        if line[2].startswith('----'):
           Intr =  '!SERVIR DEPOIS!' + Intr
           LineCt +=1

for i,line in enumerate(fetch_lines):
    if line[2].startswith('#') or line[2].startswith('----'): pass
    elif i == len(fetch_lines) - LineCt:
        newTable.append([line[0],line[1], line[2], Intr , "" ])
        Intr     = ''
    elif i < len(fetch_lines):
        newTable.append([line[0],line[1],line[2], '', "" ])

print Intr
for e in newTable: print e

rawDescription: Sorting data from a table

description: Sorting data from a table


code:
###########################################
print "Function iterator using yield",

def gen():
    for x in range(0, 10):
        yield x

f = gen()
try:
    while True:
        print f.next(),
except StopIteration:
    pass

for x in gen():
    print x,

print

##########################################
print "Class iterator defining iter and next",

class gen2(object):

    def __init__(self):
        self.index = 0;
        self.limit = 10;

    def __iter__(self):
        return self

    def next(self):
        if self.index >= self.limit:
            raise StopIteration
        self.index += 1;
        return self.index - 1;

f = gen2()
try:
    while True:
        print f.next(),
except StopIteration:
    pass

for x in gen2():
    print x,
print

##########################################
print "Function iterator using iter() and sentinel",
def gen3():
    def g3():
        if g3.index is None:
            g3.index = 0
        g3.index += 1;
        return g3.index - 1

    g3.index = None
    return iter(g3, 10)

f = gen3()
try:
    while True:
        print f.next(),
except StopIteration:
    pass

for x in gen3():
    print x,
print

rawDescription: invoking yield for a generator in another function

description: Invoking yield for a generator in another function


code:
for node in g.nodes():
    g.node[node]["viz"] = {'position': {'x': pos[node][0],
                                        'y': pos[node][1]
                                        }
                           }

print g.nodes(data=True)

rawDescription: Adding multi-level attributes to nodes using NetworkX


code:
rectangles = [

    # A______
    # |      |
    # -------B

    #     A             B
    Rect( Point(10,10), Point(50,70)),
    Rect( Point( 8,10), Point(30,20)),
    Rect( Point(90,90), Point(99,99)),
];

clusters = [];

for rect in rectangles:
    matched = 0;
    for cluster in clusters:
        if ( rect_overlaps( rect, cluster ) ):
            matched=1
            cluster.left   = min( cluster.left,   rect.left   );
            cluster.right  = max( cluster.right,  rect.right  );
            cluster.top    = min( cluster.top,    rect.top    );
            cluster.bottom = max( cluster.bottom, rect.bottom );

    if ( not matched ):
        clusters.append( rect );

print "Clusters:"
for c in clusters:
    print c

rawDescription: Combining rectangle/square areas into bigger ones - imshow/python


code:
try:
    import json
except ImportError:
    import simplejson as json
import sys

def convert_file(infname, outfname):

    def explore(folder_name, folder_info):
        for child_dict in folder_info['children']:
            ctype = child_dict.get('type')
            name = child_dict.get('name')
            if ctype == 'url':
                url = child_dict.get('url')
                # print "name=%r url=%r" % (name, url)
                fw.write(name.encode('utf-8') + '
\n')
            elif ctype == 'folder':
                explore(name, child_dict)
            else:
                print "*** Unexpected ctype=%r ***" % ctype

    f = open(infname, 'rb')
    bmarks = json.load(f)
    f.close()
    fw = open(outfname, 'w')
    fw.write("\n")
    for folder_name, folder_info in bmarks['roots'].iteritems():
        explore(folder_name, folder_info)
    fw.write("")
    fw.close()

if __name__ == "__main__":
    convert_file(sys.argv[1], sys.argv[2])

rawDescription: python display unicode in html

description: Python display unicode in html


code:
import csv
with open('small.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        print row[0]  # note the additional [0]!

rawDescription: python - identify characters from numbers in csv file

description: Python - identify characters from numbers in csv file


code:
import urllib2
import re

URL="http://www.navlost.eu/aero/metar/?icao=KBOS&dt0=2010-07-14+02%3A00%3A00&c=1&rt=metar"
f = urllib2.urlopen(URL)
data = f.read()

r = re.compile('<code>(.*)</code>', re.I | re.S | re.M)
print r.findall(data)[0]

rawDescription: Parsing METAR webpage in python

description: Parsing METAR webpage in python


code:
class FunctionInput:
        def __init__(self, master, window, items):
            self.window = window
            self.items = items
            self.runProgram = True
            self.frame = tk.Frame(master)
            self.typeFrame = tk.Frame(self.frame)
            self.typeChoice = tk.StringVar(self.typeFrame)
            self.quitButton = tk.Button(self.frame, text = 'Quit', command = self.frame.quit)
            self.optLabel = tk.Label(self.frame, text = 'Type of Approximation: ')
            self.optL = tk.Radiobutton(self.typeFrame, text = 'Left Rectangular', variable = self.typeChoice, value = 'l')
            self.optR = tk.Radiobutton(self.typeFrame, text = 'Right Rectangular', variable = self.typeChoice, value = 'r')
            self.optT = tk.Radiobutton(self.typeFrame, text = 'Trapezoidal', variable = self.typeChoice, value = 't')
            self.optS = tk.Radiobutton(self.typeFrame, text = 'Simpsons Rule', variable = self.typeChoice, value = 's')
            self.optL.grid(row = 1, column = 1, padx = 5, pady = 5)
            self.optR.grid(row = 1, column = 2, padx = 5, pady = 5)
            self.optT.grid(row = 2, column = 1, padx = 5, pady = 5)
            self.optS.grid(row = 2, column = 2, padx = 5, pady = 5)
            self.optLabel.grid(row = 4)
            self.typeFrame.grid(row = 5)
            self.quitButton.grid(row = 6)
            # there were numerous other widgets and frames, but I only included the relevant ones
            self.frame.grid()

        def getInput(self):
            type_integration = self.typeChoice.get()
            self.frame.quit()
            return type_integration

def main():
    # some other code, win and axisLabels are defined prior to this
    root = tk.Tk(className = ' Function Grapher')
    app = FunctionInput(root, win, axisLabels)
    root.rowconfigure(0, weight = 1)
    root.columnconfigure(0, weight = 1)
    root.mainloop() # there is a button to exit the mainloop in my GUI
    print app.getInput()

if __name__ == '__main__': main()

rawDescription: Getting Input from Radiobutton

description: Getting Input from Radiobutton


code:
cmd = input("Please input a command: ")
while cmd != "QUIT":
    if cmd.startswith("INPUT"):
        if len(cmd) < 7:
            print "Usage: INPUT <filename>"
            continue
        filename = cmd[6:]
        file = open("filename")
        for line in file:
            line_list = line.strip().split(";")
            main_list.append(line_list)

rawDescription: Using part of input as a filename

description: Using part of input as a filename


code:
import random
import timeit

l = [random.randint(1,10000) for i in range(1000)]
l2 = [random.randint(1,10000) for i in range(1000)]

def f1():
    l3 = []
    for i in l:
        if i in l2:
            l3.append(i)
    return l3

def f2():
    l3 = []
    for i in set(l) & set(l2):
        l3.append(i)
    return l3

t1 = timeit.Timer('f1()', 'from __main__ import f1')
print t1.timeit(100) #2.0850549985

t2 = timeit.Timer('f2()', 'from __main__ import f2')
print t2.timeit(100) #0.0162533142857

rawDescription: Efficient way of Element lookup in a Python List?


code:
import numpy
import timeit
import itertools
import theano
import theano.tensor as tt

def numpy1(k0, kN, x):
    output = numpy.zeros_like(x)
    for k in range(k0, kN + 1):
        output += k * x
    return output

def numpy2(k0, kN, x):
    return numpy.arange(k0, kN + 1).sum() * x

def numpy3(k0, kN, x):
    return numpy.einsum('i->', numpy.arange(k0, kN + 1)) * x

def theano1_step(k, s_tm1, x):
    return s_tm1 + k * x

def compile_theano1():
    k0 = tt.lscalar()
    kN = tt.lscalar()
    x = tt.vector()
    outputs, _ = theano.scan(theano1_step, sequences=[tt.arange(k0, kN + 1)], outputs_info=[tt.zeros_like(x)],
                             non_sequences=[x], strict=True)
    return theano.function([k0, kN, x], outputs=outputs[-1])

def compile_theano2():
    k0 = tt.lscalar()
    kN = tt.lscalar()
    x = tt.vector()
    return theano.function([k0, kN, x], outputs=tt.arange(k0, kN + 1).sum() * x)

def numpy4(k0, kN, x):
    return ((kN - k0 + 1) * (k0 + kN) / 2) * x

def compile_theano4():
    k0 = tt.lscalar()
    kN = tt.lscalar()
    x = tt.vector()
    return theano.function([k0, kN, x], outputs=((kN - k0 + 1) * (k0 + kN) / 2) * x)

def main():
    iteration_count = 10
    k0 = 10
    kN = 10000
    x = numpy.random.standard_normal(size=(20000,)).astype(theano.config.floatX)

    functions = [numpy1, numpy2, numpy3, numpy4, compile_theano1(), compile_theano2(), compile_theano4()]
    function_count = len(functions)
    results = numpy.empty((iteration_count * function_count, x.shape[0]), dtype=theano.config.floatX)
    times = numpy.empty((iteration_count * function_count,), dtype=theano.config.floatX)

    for iteration in xrange(iteration_count):
        for function_index, function in enumerate(functions):
            start = timeit.default_timer()
            results[iteration * function_count + function_index] = function(k0, kN, x)
            times[iteration * function_count + function_index] = timeit.default_timer() - start

    for result1, result2 in itertools.izip(results[0::2], results[1::2]):
        assert numpy.allclose(result1, result2)

    for function_name, function_index in itertools.izip(
            ('numpy1', 'numpy2', 'numpy3', 'numpy4', 'theano1', 'theano2', 'theano4'),
            xrange(function_count)):
        time = times[function_index::function_count].mean()
        print '%8s %.8f' % (function_name, float(time))

main()

rawDescription: Theano scan for fast computations on an array


code:
import datetime
import itertools

#convert from a "year-day" string to a datetime object
def datetime_from_year_day(s):
    year = int(s[:4])
    days = int(s[4:])
    return datetime.datetime(year=year, month=1, day=1) + datetime.timedelta(days=days-1)

#returns the year whose end is closest to the date, whether in the past or future
def nearest_year_end(d):
    if d.month <= 6:
        return d.year-1
    else:
        return d.year

a = ['2003337', '2003345', '2003353', '2003361', '2004001', '2004009', '2004017', '2004025', '2004033', '2004041', '2004049', '2004057', '2004337', '2004345', '2004353', '2004361', '2005001', '2005009', '2005017', '2005025', '2005033', '2005041', '2005049', '2005057']

result = [list(v) for k,v in itertools.groupby(a, lambda s: nearest_year_end(datetime_from_year_day(s)))]
print result

rawDescription: Create a list of lists using Python

description: Create a list of lists using Python


code:
import socket

HOST = 'localhost'
PORT = 12349
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)

rawDescription: How to use client socket as a server socket python

description: Use client socket as a server socket python


code:
class MyClass(object):
  def __iter__(self):
    return iter([1, 2, 3])

a, b, c = MyClass()
print a, b, c

rawDescription: PHP list() equivalent in Python

description: PHP list() equivalent in Python


code:
third_level = re.compile('\n(\d+\.\d+\.\d+[^\n]*)')
for page in pages.finditer(s):
    page_no = page.group(1)
    for item in third_level.finditer(page.group()):
        print '{}\t{}'.format(item.group(1), page_no)

rawDescription: Extract section names and starting pages from a text file


code:
name = 'input2'
for elem in root:
    if elem.tag == 'vars':
        for var in elem:
           if var.attrib.get('name') == name:
               print var.text

rawDescription: How to get the child of child using Python's ElementTree

description: Get the child of child using Python's ElementTree


code:
import random
a = [int(1000*random.random()) for i in xrange(10)]
b = [int(1000*random.random()) for i in xrange(10)]
c = [int(1000*random.random()) for i in xrange(10)]
d = dict()
for i in xrange(10):
    for j in xrange(10 - i):
        k = 10 - i - j
        if k < len(c):
            d[(i,j,k)] = a[i]+b[j]+c[k]

print max(d.values())

rawDescription: How can I write this solution more efficiently?

description: Write this solution more efficiently


code:
import win32com.client

session = win32com.client.Dispatch('blpapicom.Session')
session.QueueEvents = True
session.Start()
started = session.OpenService('//blp/refdata')
dataService = session.GetService('//blp/refdata')
request = dataService.CreateRequest('HistoricalDataRequest')
request.GetElement('securities').AppendValue('5 HK Equity')
request.GetElement('fields').AppendValue('PX_LAST')
request.Set('periodicitySelection', 'DAILY')
request.Set('startDate', '20090119')
request.Set('endDate', '20090130')
cid = session.SendRequest(request)
ADMIN = 1
AUTHORIZATION_STATUS = 11
BLPSERVICE_STATUS = 9
PARTIAL_RESPONSE = 6
PUBLISHING_DATA = 13
REQUEST_STATUS = 4
RESOLUTION_STATUS = 12
RESPONSE = 5
SESSION_STATUS = 2
SUBSCRIPTION_DATA = 8
SUBSCRIPTION_STATUS = 3
TIMEOUT = 10
TOKEN_STATUS = 15
TOPIC_STATUS = 14
UNKNOWN = -1
stayHere = True
while stayHere:
    event = session.NextEvent();
    if event.EventType == PARTIAL_RESPONSE or event.EventType == RESPONSE:
        iterator = event.CreateMessageIterator()
        iterator.Next()
        message = iterator.Message
        securityData = message.GetElement('securityData')
        securityName = securityData.GetElement('security')
        fieldData = securityData.GetElement('fieldData')
        returnList = [[0 for col in range(fieldData.GetValue(row).NumValues+1)] for row in range(fieldData.NumValues)]
        for row in range(fieldData.NumValues):
            rowField = fieldData.GetValue(row)
            for col in range(rowField.NumValues+1):
                colField = rowField.GetElement(col)
                returnList[row][col] = colField.Value
        stayHere = False
        break
element = None
iterator = None
message = None
event = None
session = None
print returnList

rawDescription: Asynchronous data through Bloomberg's new data API (COM v3) with Python?


code:
selector = Selector(response=response)

title = selector.xpath('//title/text()').extract()[0]
description = selector.xpath('//edindex/text()').extract()
video_sources = selector.xpath('//video/source/@src').extract()[0]

code_url = selector.xpath('//meta[@name="EdImage"]/@content').extract()[0]
code = re.search(r'(\w+)-play-small.jpg$', code_url).group(1)

print title
print description
print video_sources
print code

rawDescription: python scrapy extract data from website

description: Python scrapy extract data from website


code:
a = (3, 5, 5)
for i, var in enumerate(a):
    if i == len(a) - 1:
        print 'last element:'
    print var

rawDescription: Python: last element of tuple

description: Python: last element of tuple


code:
punc = [u'i', u'am', u'going', u'home.', u'i', u'will', u'see,', u'if', u'i', u'can', u'do', u'that!', u'i', u'will', u'see?']
print [''.join(c for c in s if c not in string.punctuation) for s in punc]

rawDescription: Stripping punctuation from a list of lists of words


code:
for index, i in enumerate(['a', 'b', 'c']):
    print index, i

rawDescription: Python 2.7, populating tables with arrays


code: print [word for word in words if word[0].isupper() ]

rawDescription: Code to detect all words that start with a capital letter in a string


code:
from bs4 import BeautifulSoup

data = """<form method = "post" id="FORM1" name="FORM1">

    Club

       <input value="CLUB TENIS DE MESA PORTOBAIL" disabled class="txtmascaraform" type="TEXT" name="txtClub" size="60" maxlength="55">

    Nombre Equipo

    <input value="C.T.M. PORTOBAIL" disabled class="txtmascaraform" type="TEXT" name="txtNomEqu" size="100" maxlength="80">

     Telefono fijo

       <input value="63097005534" disabled class="txtmascaraform" type="TEXT" name="txtTelf" size="15" maxlength="10">

</form>"""

soup = BeautifulSoup(data)
form = soup.find("form", {'id': "FORM1"})
print [item.get('value') for item in form.find_all('input')]

# UPDATE for getting table cell values
table = form.find("table")
print [item.text.strip() for item in table.find_all('td')]

rawDescription: Extracting table data from html with python and BeautifulSoup


code:
import multiprocessing
import datetime
import random
import time

def funfun(number):
    time.sleep(random.randint(0,10))
    now = datetime.datetime.now()
    return "%s says hello, World! at time: %s"  % (number,now)

if __name__ == "__main__":
    pool = multiprocessing.Pool(10)
    for item in pool.imap(funfun,[i for i in range(10)]):
        print item

rawDescription: synchronization across multiple processes in python

description: Synchronization across multiple processes in python


code:
from numpy import array as a
gpstimes=a([151,152,153,190,649,652,920,996])
starts=a([100,600,900,1000])
ends=a([180,650,950,1000])
names=a(['a','b','c','d',])
names_for_times=[]

for time in gpstimes:
    start_pos=starts.searchsorted(time)
    end_pos=ends.searchsorted(time)
    if start_pos-1 == end_pos:
        print time, names[end_pos]
    else:
        print str(time) + ' was not within any frame'

rawDescription: Django lte/gte query on a list

description: Django lte/gte query on a list


code:
from nltk.corpus import movie_reviews
first_fileid =  movie_reviews.fileids(categories=["pos"])[0]
tokens_in_file = [i for i in movie_reviews.words(first_fileid)]
print tokens_in_file
print type(tokens_in_file)

rawDescription: NLTK bag of bigrams words function raises "Don't know how to concatenate types" error - python


code:
def wordIterator(dictionaryFilename):
    with open(dictionaryFilename,'r') as f:
        for line in f:
            word = line.strip()
            yield word

def largestAnagram(words):
    import collections
    d = collections.defaultdict(list)
    for word in words:
        sortedWord = str(sorted(word))
        d[ hash(sortedWord) ].append(word)
    maxKey = max( d.keys(), key = lambda k : len(d[k]) )
    return d[maxKey]

iter = wordIterator( 'C:\\Python32\\megalist.txt' )
#iter = ( word for word in iter if len(word) == 5 )
print largestAnagram(iter)

rawDescription: Python- Code Optimization Help- Find all dictionary-valid anagrams of a word