code:
import copy
l = ['a','b','c']
st = ['d','e']
count = 0
f = {}
for s in st:
    l.append(s)
    f[count] = copy.copy(l)
    count += 1
    print f

rawDescription: adding list value into dictionary after appending new value into list during each iteration

description: Adding list value into dictionary after appending new value into list during each iteration


code:
def call_async(func, *args, **kw):
  threading.Thread(target=func, args=args, kwargs=kw).start()

def do_something(msg):
  print msg

call_async(do_something, "Don't overuse threads!")

rawDescription: Execute Function Every X Seconds On Class Init


code:
import sys
filename = sys.argv[1]
args = dict([arg.split('=', maxsplit=1) for arg in sys.argv[2:]])
print filename
print args

rawDescription: Python: Pass a generic dictionary as a command line arguments


code:
content = {"2":"String1", "1":"String2", "3":"String3"}
print content["1"]

rawDescription: Using two lists to define a variable in python


code:
import sys, time, msvcrt

timeout = 5
startTime = time.time()
inp = None

print "Press any key to configure or wait 5 seconds... "
while True:
    if msvcrt.kbhit():
        inp = msvcrt.getch()
        break
    elif time.time() - startTime > timeout:
        break

if inp:
    print "Config selected..."
else:
    print "Timed out..."

rawDescription: Python wait x secs for a key and continue execution if not pressed


code:
for ul in soup.find_all('ul'):
    print 'ul start'
    for idx, li in enumerate(ul.findChildren('li')):
        if idx in range(3):
            print li

rawDescription: How do you find all list items between two tags with BeautifulSoup?

description: Find all list items between two tags with BeautifulSoup


code:
def mc_wo_sample(kernel,x1,y1,lboundx,lboundy):
    nsamples = 50000
    volume = (x1-lboundx)*(y1-lboundy)
    # generate uniform points in range
    xrand = np.random.rand(nsamples,1)*(x1-lboundx) + lboundx
    yrand = np.random.rand(nsamples,1)*(y1-lboundy) + lboundy
    randvals = np.hstack((xrand,yrand)).transpose()
    print randvals.shape
    return (volume*kernel(randvals).sum())/nsamples

rawDescription: Different integration results using Monte Carlo vs scipy.integrate.nquad


code:
def argmax(some_list):
    return max(enumerate(some_list), key=lambda x:x[1])[0]

def helper_to_subtract_pairs(acc, x):
    return acc[:-1] + [x[0] - acc[-1]] + [x[1]]

def max_gap(prime_list):
    gaps = reduce(helper_to_subtract_pairs, prime_list, [0])[1:-1]
    max_idx = argmax(gaps)
    return gaps[max_idx], prime_list[max_idx:max_idx + 2]

L3 = [(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61),
      (71, 73), (101, 103), (107, 109), (137, 139)]

print max_gap(L3)

rawDescription: Calculating the gap between pairs of twin primes in python


code:
a = a.lower()
b = b.lower()
c = max(a, b, key=len)
d = min(a, b, key=len)
print d == c[-len(d):]

rawDescription: Python - boolean matter

description: Python - boolean matter


code:
import bottle
from beaker.middleware import SessionMiddleware
from cork import Cork
import logging

logging.basicConfig(format='localhost - - [%(asctime)s] %(message)s', level=logging.DEBUG)
log = logging.getLogger(__name__)
bottle.debug(True)

# Use users.json and roles.json in the local example_conf directory
aaa = Cork('example_conf', email_sender='federico.ceratto@gmail.com', smtp_url='smtp://smtp.magnet.ie')

app = bottle.app()
session_opts = {
    'session.cookie_expires': True,
    'session.encrypt_key': 'please use a random key and keep it secret!',
    'session.httponly': True,
    'session.timeout': 3600 * 24,  # 1 day
    'session.type': 'cookie',
    'session.validate_key': True,
}
app = SessionMiddleware(app, session_opts)

# #  Bottle methods  # #

def postd():
    return bottle.request.forms

def post_get(name, default=''):
    return bottle.request.POST.get(name, default).strip()

@bottle.post('/login')
def login():
    """Authenticate users"""
    username = post_get('username')
    password = post_get('password')
    aaa.login(username, password, success_redirect='/', fail_redirect='/login')

@bottle.route('/user_is_anonymous')
def user_is_anonymous():
    if aaa.user_is_anonymous:
        return 'True'

    return 'False'

@bottle.route('/logout')
def logout():
    aaa.logout(success_redirect='/login')

@bottle.post('/register')
def register():
    """Send out registration email"""
    aaa.register(post_get('username'), post_get('password'), post_get('email_address'))
    return 'Please check your mailbox.'

@bottle.route('/validate_registration/:registration_code')
def validate_registration(registration_code):
    """Validate registration, create user account"""
    aaa.validate_registration(registration_code)
    return 'Thanks. Go to login'

@bottle.post('/reset_password')
def send_password_reset_email():
    """Send out password reset email"""
    aaa.send_password_reset_email(
        username=post_get('username'),
        email_addr=post_get('email_address')
    )
    return 'Please check your mailbox.'

@bottle.route('/change_password/:reset_code')
@bottle.view('password_change_form')
def change_password(reset_code):
    """Show password change form"""
    return dict(reset_code=reset_code)

@bottle.post('/change_password')
def change_password():
    """Change password"""
    aaa.reset_password(post_get('reset_code'), post_get('password'))
    return 'Thanks. Go to login'

@bottle.route('/')
def index():
    """Only authenticated users can see this"""
    aaa.require(fail_redirect='/login')
    return 'Welcome! Admin page Logout'

@bottle.route('/restricted_download')
def restricted_download():
    """Only authenticated users can download this file"""
    aaa.require(fail_redirect='/login')
    return bottle.static_file('static_file', root='.')

@bottle.route('/my_role')
def show_current_user_role():
    """Show current user role"""
    session = bottle.request.environ.get('beaker.session')
    print "Session from simple_webapp", repr(session)
    aaa.require(fail_redirect='/login')
    return aaa.current_user.role

# Admin-only pages

@bottle.route('/admin')
@bottle.view('admin_page')
def admin():
    """Only admin users can see this"""
    aaa.require(role='admin', fail_redirect='/sorry_page')
    return dict(
        current_user=aaa.current_user,
        users=aaa.list_users(),
        roles=aaa.list_roles()
    )

@bottle.post('/create_user')
def create_user():
    try:
        aaa.create_user(postd().username, postd().role, postd().password)
        return dict(ok=True, msg='')
    except Exception, e:
        return dict(ok=False, msg=e.message)

@bottle.post('/delete_user')
def delete_user():
    try:
        aaa.delete_user(post_get('username'))
        return dict(ok=True, msg='')
    except Exception, e:
        print repr(e)
        return dict(ok=False, msg=e.message)

@bottle.post('/create_role')
def create_role():
    try:
        aaa.create_role(post_get('role'), post_get('level'))
        return dict(ok=True, msg='')
    except Exception, e:
        return dict(ok=False, msg=e.message)

@bottle.post('/delete_role')
def delete_role():
    try:
        aaa.delete_role(post_get('role'))
        return dict(ok=True, msg='')
    except Exception, e:
        return dict(ok=False, msg=e.message)

# Static pages

@bottle.route('/login')
@bottle.view('login_form')
def login_form():
    """Serve login form"""
    return {}

@bottle.route('/sorry_page')
def sorry_page():
    """Serve sorry page"""
    return '
Sorry, you are not authorized to perform this action
'

# #  Web application main  # #

def main():

    # Start the Bottle webapp
    bottle.debug(True)
    bottle.run(app=app, quiet=False, reloader=True)

if __name__ == "__main__":
    main()

rawDescription: How to make authorization for bottle.py through html form?

description: Make authorization for bottle.py through html form


code:
import dis
dis.dis(one)
print "---"
dis.dis(two)

rawDescription: efficiency vs. readability: obfuscation when using nested boolean index arrays

description: Efficiency vs. readability: obfuscation when using nested boolean index arrays


code:
import re
import requests
from bs4 import BeautifulSoup

r = requests.get('http://www.digitalseo.in/')
data = r.text
soup = BeautifulSoup(data, "html.parser")

for i in soup.find_all(href=re.compile("mailto")):
    print i.string

rawDescription: Python web Automation to get Email from Webpage


code:
s = 'some s\\tring'
t
s           # just `s` gives the repr
print a     # prints `s`
# ^^^ notice the lack of slashes or quotes above

rawDescription: Why appears backslash in text?

description: Why appears backslash in text


code:
for date in a:
    print date,
    for key in a[date]:
        print '{}={}'.format(key, a[date][key]),
    print

rawDescription: how to enumerate OrderedDict in python

description: Enumerate OrderedDict in python


code:
import re
consequitivedots = re.compile(r'\.{3,}')
inputstring = '''\
Friends and family......................................-1
XBox 360 ................................................-2
'''
consequitivedots.sub('', inputstring)
print consequitivedots.sub('', inputstring)

rawDescription: Remove consecutive dots(periods) from a string?


code:
def frob(f, x):
    return x if str(x) == str(x)[::-1] else f(f, x + int(str(x)[::-1]))

print frob(frob, 29)

rawDescription: Generating list until certain condition is met using a single line


code:
from Crypto.Hash import SHA
from Crypto import Random

key = RSA.importKey(open('privkey.der').read())

dsize = SHA.digest_size
sentinel = Random.new().read(15+dsize)      # Let's assume that average data length is 15

cipher = PKCS1_v1_5.new(key)
message = cipher.decrypt(ciphertext, sentinel)

digest = SHA.new(message[:-dsize]).digest()
if digest==message[-dsize:]:                # Note how we DO NOT look for the sentinel
     print "Encryption was correct."
else:
     print "Encryption was not correct."

rawDescription: RSA communication between Javascript and Python


code:
import cPickle
import numpy
from scipy.spatial import kdtree

# patch module-level attribute to enable pickle to work
kdtree.node = kdtree.KDTree.node
kdtree.leafnode = kdtree.KDTree.leafnode
kdtree.innernode = kdtree.KDTree.innernode

x, y = numpy.mgrid[0:5, 2:8]
t1 = kdtree.KDTree(zip(x.ravel(), y.ravel()))
r1 = t1.query([3.4, 4.1])
raw = cPickle.dumps(t1)

# read in the pickled tree
t2 = cPickle.loads(raw)
r2 = t2.query([3.4, 4.1])
print t1.tree.__class__
print repr(raw)[:70]
print t1.data[r1[1]], t2.data[r2[1]]

rawDescription: Saving KDTree object in Python?

description: Saving KDTree object in Python


code:
import time
import struct

import Quartz.CoreGraphics as CG

class ScreenPixel(object):
    """Captures the screen using CoreGraphics, and provides access to
    the pixel values.
    """

    def capture(self, region = None):
        """region should be a CGRect, something like:

        >>> import Quartz.CoreGraphics as CG
        >>> region = CG.CGRectMake(0, 0, 100, 100)
        >>> sp = ScreenPixel()
        >>> sp.capture(region=region)

        The default region is CG.CGRectInfinite (captures the full screen)
        """

        if region is None:
            region = CG.CGRectInfinite
        else:
            # TODO: Odd widths cause the image to warp. This is likely
            # caused by offset calculation in ScreenPixel.pixel, and
            # could could modified to allow odd-widths
            if region.size.width % 2 > 0:
                emsg = "Capture region width should be even (was %s)" % (
                    region.size.width)
                raise ValueError(emsg)

        # Create screenshot as CGImage
        image = CG.CGWindowListCreateImage(
            region,
            CG.kCGWindowListOptionOnScreenOnly,
            CG.kCGNullWindowID,
            CG.kCGWindowImageDefault)

        # Intermediate step, get pixel data as CGDataProvider
        prov = CG.CGImageGetDataProvider(image)

        # Copy data out of CGDataProvider, becomes string of bytes
        self._data = CG.CGDataProviderCopyData(prov)

        # Get width/height of image
        self.width = CG.CGImageGetWidth(image)
        self.height = CG.CGImageGetHeight(image)

    def pixel(self, x, y):
        """Get pixel value at given (x,y) screen coordinates

        Must call capture first.
        """

        # Pixel data is unsigned char (8bit unsigned integer),
        # and there are for (blue,green,red,alpha)
        data_format = "BBBB"

        # Calculate offset, based on
        # http://www.markj.net/iphone-uiimage-pixel-color/
        offset = 4 * ((self.width*int(round(y))) + int(round(x)))

        # Unpack data from string into Python'y integers
        b, g, r, a = struct.unpack_from(data_format, self._data, offset=offset)

        # Return BGRA as RGBA
        return (r, g, b, a)

if __name__ == '__main__':
    # Timer helper-function
    import contextlib

    @contextlib.contextmanager
    def timer(msg):
        start = time.time()
        yield
        end = time.time()
        print "%s: %.02fms" % (msg, (end-start)*1000)

    # Example usage
    sp = ScreenPixel()

    with timer("Capture"):
        # Take screenshot (takes about 70ms for me)
        sp.capture()

    with timer("Query"):
        # Get pixel value (takes about 0.01ms)
        print sp.width, sp.height
        print sp.pixel(0, 0)

    # To verify screen-cap code is correct, save all pixels to PNG,
    # using http://the.taoofmac.com/space/projects/PNGCanvas

    from pngcanvas import PNGCanvas
    c = PNGCanvas(sp.width, sp.height)
    for x in range(sp.width):
        for y in range(sp.height):
            c.point(x, y, color = sp.pixel(x, y))

    with open("test.png", "wb") as f:
        f.write(c.dump())

rawDescription: Python Get Screen Pixel Value in OS X

description: Python Get Screen Pixel Value in OS X


rawDescription: How to make a sorted dictionary class?

description: Make a sorted dictionary class


code:
# Imports #
from random import random
import statsmodels.api as smapi
from statsmodels.formula.api import ols
import statsmodels.graphics as smgraphics
# Make data #
x = range(30)
y = [y*(10+random())+200 for y in x]
# Add outlier #
x.insert(6,15)
y.insert(6,220)
# Make fit #
regression = ols("data ~ x", data=dict(data=y, x=x)).fit()
# Find outliers #
test = regression.outlier_test()
outliers = ((x[i],y[i]) for i,t in enumerate(test.icol(2)) if t < 0.5)
print 'Outliers: ', list(outliers)
# Figure #
figure = smgraphics.regressionplots.plot_fit(regression, 1)
# Add line #
smgraphics.regressionplots.abline_plot(model_results=regression, ax=figure.axes[0])

rawDescription: Can scipy.stats identify and mask obvious outliers?


code:
for attr in cmds.listAttr( "myCube", r=True ):
    try:
        print attr, " ", cmds.getAttr("myCube."+attr)
    except:
        print "Error reading data"

rawDescription: PYTHON in MAYA: get all attributes

description: PYTHON in MAYA: get all attributes


code:
import re
re.search(ur'[a-zA-Z\'-]', ' ')  # does not match space
re.search(ur'[\uac00-\ud7af]', ' ')  # does not match space
re.search(ur'[\u0000-\u00FF]', ' ')  # match space

rawDescription: Regex not splitting text the same way

description: Regex not splitting text the same way


code:
#!/usr/bin/python
from pydhcplib.dhcp_packet import *
from pydhcplib.dhcp_network import *

netopt = {'client_listen_port':68,
       'server_listen_port':67,
       'listen_address':"0.0.0.0"}

class Client(DhcpClient):
    def __init__(self, options):
         DhcpClient.__init__(self,options["listen_address"],
                        options["client_listen_port"],
                        options["server_listen_port"])

    def HandleDhcpOffer(self, packet):
        print packet.str()
    def HandleDhcpAck(self, packet):
        print packet.str()
    def HandleDhcpNack(self, packet):
        print packet.str()

client = Client(netopt)
# Use BindToAddress if you want to emit/listen to an internet address (like 192.168.1.1)
# or BindToDevice if you want to emit/listen to a network device (like eth0)
client.BindToAddress()

while True :
    print client.GetNextDhcpPacket()

rawDescription: DHCP request library

description: DHCP request library


code:
def get_smallest_length(x):
    return [k for k in x.keys() if len(x.get(k))==min([len(n) for n in x.values()])]

def get_largest_sum(x):
    return [k for k in x.keys() if sum(x.get(k))==max([sum(n) for n in x.values()])]

x = {'a': [4, 2], 'c': [4, 3], 'b': [3, 4], 'e': [4], 'd': [4, 3], 'g': [4], 'f': [4]}

print get_smallest_length(x)
print get_largest_sum(x)

rawDescription: Python miminum length/max value in dictionary of lists


code:
msj = level_3 or level_2 or level_1
if msj:
    print msj

rawDescription: Python: Shorten If / Elif control flow tree (printing highest order string that is not None)


code:
total_rate = 0
vrates = 0
funrate = 0
rates5 = 0

def facilities():
    global total_rate, vrates, funrate, rates5

    def food():
        global rates5
        # files are used
        rates5 = 1
        # calculate the rate and store the total rate in rates5

    def recreation():
        global total_rate, vrates, funrate, rates5
        # files are used
        funrate = 2
        # calculate the rate and store the total rate in funrate

    def transport():
        global total_rate, vrates, funrate, rates5
        # files are used
        vrates = 3
        # calculate the rate and store the total rate in vrates

    food()
    recreation()
    transport()

facilities()

total_rate=rates5 + vrates + funrate
print "The total amount=", total_rate

rawDescription: Something wrong with this file program?

description: Something wrong with this file program


code:
from operator import itemgetter

after_short_program = {
    'Evgeni Plushenko':1,
    'Evan Lysacek':2,
    'Daisuke Takahashi':3,
    'Stephane Lambiel':4,
    'Nobunari Oda':5,
}

after_free_skate = {
    'Evan Lysacek':1,
    'Daisuke Takahashi':2,
    'Evgeni Plushenko':3,
    'Stephane Lambiel':4,   # These are tied
    'Nobunari Oda':4,       # at 4th place
}

for k,v in sorted(after_free_skate.items(),key=itemgetter(1)):
    diff = after_short_program[k]-v
    print "%s (%+d)"%(k,diff)

rawDescription: Positional Comparisons in Python

description: Positional Comparisons in Python


code:
while True:
    name = raw_input("What is your name?")
    print 'You entered:', repr(name)
    if name == "Jane":
        print name + " is awesome"
        break
    else:
        print name + " is not awesome"

rawDescription: Raw input not working as I expected - am I missing a module?


code:
if word != solution_list1[0]:
    print "Sorry, you are wrong. Try again!"

rawDescription: Nested If Expression with user validation - repeating the prompt for user input


code:
import cPickle

class MemoizeMutable:
    def __init__(self, fn):
        self.fn = fn
        self.memo = {}
    def __call__(self, *args, **kwds):
        import cPickle
        str = cPickle.dumps(args, 1)+cPickle.dumps(kwds, 1)
        if not self.memo.has_key(str):
            print "miss"  # DEBUG INFO
            self.memo[str] = self.fn(*args, **kwds)
        else:
            print "hit"  # DEBUG INFO

        return self.memo[str]

rawDescription: Python - anyone have a memoizing decorator that can handle unhashable arguments?


code:
#!/usr/bin/python
import json

ES = {
    "settings": {
        "number_of_shards" : "1"
    },
    "mappings": {
        "_default_": {
            "_timestamp_": {
                "enabled" : "true",
                "store" : "true"
            }
        }
    }
}

print json.dumps(ES,indent=2, separators=(',', ': '))

rawDescription: Unable to store or print json data in exact same format


code:
def validate_input(validation_fn_name):
    def validation_decorator(func):
        def validate_input_action(self, *args):
            error = getattr(self, validation_fn_name)(*args)
            if error is not None:
                raise error
            else:
                arglist = [self] + list(args)
                return func(*arglist)
        return validate_input_action
    return validation_decorator

class Foo(object):

    def validate_length(self, arg1):
        if len(arg1) < 3:
            return ValueError('%r is too short' % arg1)

    @validate_input('validate_length')
    def bar(self, arg1):
        print "Arg1 is %r" % arg1

if __name__ == "__main__":
    f = Foo()
    f.bar('hello')
    f.bar('')

rawDescription: Decorator to invoke instance method

description: Decorator to invoke instance method


code:
#!/usr/bin/python
import sys

print "executing in other.py"

if len(sys.argv) > 1:
    print "was passed value of ", sys.argv[1]
else:
    print "no arguments were passed"

rawDescription: How can I specify an alias in ERIC Python IDE?

description: Specify an alias in ERIC Python IDE


code:
class Widget:
    def __init__(self):
        self.bar = False
    def someFunction(self):
        self.bar = True

w = Widget()
w.someFunction()
print w.bar

rawDescription: Change a variable from a function

description: Change a variable from a function


code:
import re
from bs4 import BeautifulSoup

html = BeautifulSoup('ABCD0000000
')

for br in html('br'):
    br.extract()

print html.find_all('a', text=re.compile('ABCD0000000', re.IGNORECASE))

rawDescription: BeautifulSoup 4: Dealing with urls containing <br />


code:
a = 'safasfeqefscwaeeafweeaeawaw'
b = 'tsafdstrdfadsdfdswdfafdwaed'
c = 'optykop;lvhopijresokpghwji7'

va = word2vec(a)
vb = word2vec(b)
vc = word2vec(c)

print cosdis(va,vb)
print cosdis(vb,vc)
print cosdis(vc,va)

rawDescription: cosine similarity between two words in a list

description: Cosine similarity between two words in a list


code:
for comp in computer_list:
    print comp.name, comp.status, comp.date, comp.kind

rawDescription: What is a better way to store a list of properties against one item in Python?


code:
import pandas as pd

df1 = pd.read_csv('test/GapInTimestamps.csv', sep=",", index_col=[0], parse_dates=[0])
print df1.head()

#                     value
#Date/Time
#2015-01-01 00:00:00     52
#2015-01-01 01:00:00      5
#2015-01-01 02:00:00     12
#2015-01-01 03:00:00     54
#2015-01-01 04:00:00     47
print df1.info()

#<class 'pandas.core.frame.DataFrame'>
#DatetimeIndex: 34857 entries, 2015-01-01 00:00:00 to 2018-12-25 23:00:00
#Data columns (total 1 columns):
#value    34857 non-null int64
#dtypes: int64(1)
#memory usage: 544.6 KB
#None

Result  = df1.resample('60min')
print Result.head()

#                     value
#Date/Time
#2015-01-01 00:00:00     52
#2015-01-01 01:00:00      5
#2015-01-01 02:00:00     12
#2015-01-01 03:00:00     54
#2015-01-01 04:00:00     47
print Result.info()

#<class 'pandas.core.frame.DataFrame'>
#DatetimeIndex: 34920 entries, 2015-01-01 00:00:00 to 2018-12-25 23:00:00
#Freq: 60T
#Data columns (total 1 columns):
#value    34857 non-null float64
#dtypes: float64(1)
#memory usage: 545.6 KB
#None

#find values with NaN
resultnan =  Result[Result.isnull().any(axis=1)]

rawDescription: Fill datetimeindex gap by NaN

description: Fill datetimeindex gap by NaN


code:
import csv
import sys

from xml.etree import ElementTree
from xml.dom import minidom

video_data = ((256, 336000),
              (512, 592000),
              (768, 848000),
              (1128, 1208000))

with open(sys.argv[1], 'rU') as f:
    reader = csv.DictReader(f)
    for row in reader:
        switch_tag = ElementTree.Element('switch')

        for suffix, bitrate in video_data:
            attrs = {'src': ("mp4:soundcheck/{year}/{id}/{file_root_name}_{suffix}.mp4"
                             .format(suffix=str(suffix), **row)),
                     'system-bitrate': str(bitrate),
                     }
            ElementTree.SubElement(switch_tag, 'video', attrs)
        print minidom.parseString(ElementTree.tostring(switch_tag)).toprettyxml()

rawDescription: CSV Rows to XML files using Python

description: CSV Rows to XML files using Python


code:
class Foo:
    def foo(self): return 'foo'

class Bar(Foo):
    def bar(self): return self.foo() + 'bar'

foo = Foo()

foo.__class__ = Bar

print foo.bar()

rawDescription: extend an instance at runtime

description: Extend an instance at runtime


code:
import subprocess

try:
    subprocess.Popen("python b.py".split()).communicate()
except KeyboardInterrupt:
    print "a.py got ctrl-c"

rawDescription: Python sub process Ctrl+C

description: Python sub process Ctrl+C


code:
import re

ss = '''When colour goes [xxxx] home into the eyes,
And lights that shine are shut again,
With danc[yyy]ing girls and sweet birds' cries
Behind the gateways[ZZZZ  ] of the brain;
And that no-place which gave them birth, shall close
The [AAA]rainbow [UUUUU] and [BBBB]the rose:—'''

print ss,'\n'

dico_lines = dict( (n,repr(line)) for n,line in enumerate(ss.splitlines(True),1))

def repl(mat, countline =[1]):
    if mat.group(1):
        print "line %s: detecting \\n , the counter of lines is incremented -> %s" % (countline[0],countline[0]+1)
        countline[0] += 1
        return mat.group(1)
    else:
        print "line %s: removing %10s  in  %s" % (countline[0],repr(mat.group()),dico_lines[countline[0]])
        return ''

print '\n'+re.sub(r'(\n)|\[.*?\] ?',repl,ss)

rawDescription: Using regex to replace object within brackets in a text file


code:
for k1, set_1 in res_1.iteritems():
    for k2, set_2 in res_2.iteritems():
      print k1
      print k2,
      l = [v for v in set_1 - set_2]
      print '(' + ', '.join(l) + ')'

rawDescription: Unable to get set intersection to work

description: Unable to get set intersection to work


code:
import cv2
import numpy

img = cv2.imread('inp.png', 0)
_, contours, _ = cv2.findContours(img.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_L1)
print len(contours)
centres = []
for i in range(len(contours)):
  moments = cv2.moments(contours[i])
  centres.append((int(moments['m10']/moments['m00']), int(moments['m01']/moments['m00'])))
  cv2.circle(img, centres[-1], 3, (0, 0, 0), -1)

print centres

cv2.imshow('image', img)
cv2.imwrite('output.png',img)
cv2.waitKey(0)

rawDescription: OpenCV get centers of multiple objects

description: OpenCV get centers of multiple objects


code:
import random

# adapt parameters to your own usage scenario
class_count = 50
class_size = 1000
list_count_per_class = 100
large_list_sizes = list(range(100, 1000))
small_list_sizes = list(range(0, 100))
large_list_probability = 0.5

if False:  # change to true to generate the test data file (takes a while)
  with open("/tmp/test.txt", "w") as f:
    lists = []
    classes = [range(class_size*i, class_size*(i+1)) for i in range(class_count)]
    for c in classes:
      # distribute each class across ~300 lists
      for i in xrange(list_count_per_class):
        lst = []
        if random.random() < large_list_probability:
          size = random.choice(large_list_sizes)
        else:
          size = random.choice(small_list_sizes)
        nums = set(c)
        for j in xrange(size):
          x = random.choice(list(nums))
          lst.append(x)
          nums.remove(x)
        random.shuffle(lst)
        lists.append(lst)
    random.shuffle(lists)
    for lst in lists:
      f.write(" ".join(str(x) for x in lst) + "\n")

setup = """
# Niklas'
def merge_niklas(lsts):
  sets = [set(lst) for lst in lsts if lst]
  merged = 1
  while merged:
    merged = 0
    results = []
    while sets:
      common, rest = sets[0], sets[1:]
      sets = []
      for x in rest:
        if x.isdisjoint(common):
          sets.append(x)
        else:
          merged = 1
          common |= x
      results.append(common)
    sets = results
  return sets

# Rik's
def merge_rik(data):
  sets = (set(e) for e in data if e)
  results = [next(sets)]
  for e_set in sets:
    to_update = []
    for i,res in enumerate(results):
      if not e_set.isdisjoint(res):
        to_update.insert(0,i)

    if not to_update:
      results.append(e_set)
    else:
      last = results[to_update.pop(-1)]
      for i in to_update:
        last |= results[i]
        del results[i]
      last |= e_set
  return results

# katrielalex's
def pairs(lst):
  i = iter(lst)
  first = prev = item = i.next()
  for item in i:
    yield prev, item
    prev = item
  yield item, first

import networkx
def merge_katrielalex(lsts):
  g = networkx.Graph()
  for lst in lsts:
    for edge in pairs(lst):
      g.add_edge(*edge)
  return networkx.connected_components(g)

# agf's (optimized)
from collections import deque
def merge_agf_optimized(lists):
  sets = deque(set(lst) for lst in lists if lst)
  results = []
  disjoint = 0
  current = sets.pop()
  while True:
    merged = False
    newsets = deque()
    for _ in xrange(disjoint, len(sets)):
      this = sets.pop()
      if not current.isdisjoint(this):
        current.update(this)
        merged = True
        disjoint = 0
      else:
        newsets.append(this)
        disjoint += 1
    if sets:
      newsets.extendleft(sets)
    if not merged:
      results.append(current)
      try:
        current = newsets.pop()
      except IndexError:
        break
      disjoint = 0
    sets = newsets
  return results

# agf's (simple)
def merge_agf_simple(lists):
  newsets, sets = [set(lst) for lst in lists if lst], []
  while len(sets) != len(newsets):
    sets, newsets = newsets, []
    for aset in sets:
      for eachset in newsets:
        if not aset.isdisjoint(eachset):
          eachset.update(aset)
          break
      else:
        newsets.append(aset)
  return newsets

# alexis'
def merge_alexis(data):
  bins = range(len(data))  # Initialize each bin[n] == n
  nums = dict()

  data = [set(m) for m in data ]  # Convert to sets
  for r, row in enumerate(data):
    for num in row:
      if num not in nums:
        # New number: tag it with a pointer to this row's bin
        nums[num] = r
        continue
      else:
        dest = locatebin(bins, nums[num])
        if dest == r:
          continue # already in the same bin

        if dest > r:
          dest, r = r, dest   # always merge into the smallest bin

        data[dest].update(data[r])
        data[r] = None
        # Update our indices to reflect the move
        bins[r] = dest
        r = dest

  # Filter out the empty bins
  have = [ m for m in data if m ]
  return have

def locatebin(bins, n):
  while bins[n] != n:
    n = bins[n]
  return n

lsts = []
size = 0
num = 0
max = 0
for line in open("/tmp/test.txt", "r"):
  lst = [int(x) for x in line.split()]
  size += len(lst)
  if len(lst) > max: max = len(lst)
  num += 1
  lsts.append(lst)
"""

setup += """
print "%i lists, {class_count} equally distributed classes, average size %i, max size %i" % (num, size/num, max)
""".format(class_count=class_count)

import timeit
print "niklas"
print timeit.timeit("merge_niklas(lsts)", setup=setup, number=3)
print "rik"
print timeit.timeit("merge_rik(lsts)", setup=setup, number=3)
print "katrielalex"
print timeit.timeit("merge_katrielalex(lsts)", setup=setup, number=3)
print "agf (1)"
print timeit.timeit("merge_agf_optimized(lsts)", setup=setup, number=3)
print "agf (2)"
print timeit.timeit("merge_agf_simple(lsts)", setup=setup, number=3)
print "alexis"
print timeit.timeit("merge_alexis(lsts)", setup=setup, number=3)

rawDescription: Python: simple list merging based on intersections


code:
import boto.ec2
conn=boto.ec2.connect_to_region("eu-west-1")
reservations = conn.get_all_instances()
for res in reservations:
    for inst in res.instances:
        if 'Name' in inst.tags:
            print "%s (%s) [%s]" % (inst.tags['Name'], inst.id, inst.state)
        else:
            print "%s [%s]" % (inst.id, inst.state)

rawDescription: Obtaining tags from AWS instances with boto


rawDescription: Find (start:end) positions that sublists occur within a list . Python


code:
import re
import itertools

rex = re.compile(r'.*/(.*?)[_.]')
for (style, images) in itertools.groupby(cropped, lambda s: rex.match(s).group(1)):
    print '\nStyle:', style
    for img in images:
        print ' ', img

rawDescription: python group file names by exact string

description: Python group file names by exact string


code:
recursive_trial(0, 0, 6)
print grid

rawDescription: 5*5 grid, in every 3*3 square of the grid must be 4 "lights"


code: print timeit.timeit("3.0 ** i", setup='i=100')

rawDescription: Why is 2**100 so much faster than math.pow(2,100)?


code:
import itertools
l1 = [1, 2, 3]
l2 = [4, 5, 6, 7]
listZipped = itertools.izip_longest(l1, l2)

for item in listZipped:
    print item

rawDescription: how to print the list of strings from the arrays

description: Print the list of strings from the arrays


code:
import re

data = """document.write('
');
document.write('

   some text

  ');
document.write('

    7.70.022

 ');
document.write('
');"""

pattern = re.compile(r"document\.write\('\n?([^']*?)(?:\n\s*)?'\);")
data = pattern.sub('\g<1>', data)
print data

rawDescription: How to remove all "document.write(' ');" with beautifulsoup

description: Remove all "document.write(' ');" with beautifulsoup


code:
m[0]
print m[0].toarray()

rawDescription: How should I go about subsampling from a scipy.sparse.csr.csr_matrix and a list


code:
import numpy as np
a = np.arange(32)
a
a.data
id(a.data)
id(a[0])
id(a[1])
for item in a:
  print id(item)

rawDescription: Why is a `for` over a Python list faster than over a Numpy array?


code:
from pprint import pprint

road_dict = {0:[1L,2L], 1:[3L], 2:[4L,5L]} # road_id:home_id
dist_dict = {1L: 4.1, 2L: 2.1, 3L: 4, 4L: 2.1, 5L: 6} # home_id:distance
num_dict = {0:[2,4], 1:[5], 2:[1,3]} # road_id:[house_numbers]

homes = []
for road_id, home_ids in road_dict.items():
    home_numbers_on_road = num_dict[road_id]

    for home_id, home_number in zip(home_ids, home_numbers_on_road):
        home = {'home_id':     home_id,
                'home_number': home_number,
                'road_id':     road_id,
                'distance':    dist_dict[home_id]}

        homes.append(home)

pprint(homes)

rawDescription: Sorting and re-numbering values in Python dictionary


code:
def f(x):
    print x
    print x.iloc[-1]
    return x.iloc[-1]

df['latest_regular'] = df.groupby('ID')['regular_entry'].transform(f)
print df

rawDescription: Groupby - How to apply result of logic check to all rows


code:
import numpy as np

def main():
    np.random.seed(1) # For reproducibility
    data = generate_data((10, 6))

    indices = rightmost_min_col(data)
    new_data = pop_col(data, indices)

    print 'Original data...'
    print data
    print 'Modified data...'
    print new_data

def generate_data(shape):
    return np.random.randint(0, 10, shape)

def rightmost_min_col(data):
    nrows, ncols = data.shape[:2]
    min_indices = np.fliplr(data).argmin(axis=1)
    min_indices = (ncols - 1) - min_indices
    return min_indices

def pop_col(data, col_indices):
    nrows, ncols = data.shape[:2]
    col_indices = col_indices[:, np.newaxis]
    row_indices = np.arange(ncols)[np.newaxis, :]
    mask = col_indices != row_indices
    return data[mask].reshape((nrows, ncols-1))

if __name__ == '__main__':
    main()

rawDescription: Numpy: How to get rid of the minima along axis=1, given the indices - in an efficient way?


code:
import scipy.stats as stats

# Parameters for this particular alpha distribution
a, loc, scale = 3.14, 5.0, 2.0

# Create frozen distribution
rv1 = stats.gamma(a, loc, scale)
rv2 = stats.gamma(a, loc=loc, scale=scale)

# Do something with frozen parameters
print 'positional and keyword'
print 'frozen args : {}'.format(rv1.args)
print 'frozen kwds : {}'.format(rv1.kwds)
print
print 'positional only'
print 'frozen args : {}'.format(rv2.args)
print 'frozen kwds : {}'.format(rv2.kwds)

rawDescription: How to get parameter arguments from a frozen spicy.stats distribution?

description: Get parameter arguments from a frozen spicy.stats distribution


code:
for span in soup.findAll('span'):
   if span.a:
       print span.a["href"]

rawDescription: Search html using BeautifulSoup in Python


code:
printing = False # Don't print until you've found a header
for line in f:
    if line == "--": # Once footer is found stop printing
        print line
        printing = False
    if printing: # Currently in between header and footer
        print line
    if line == "+SECTION1\n": # Once header is found start printing
        print line
        printing = True

rawDescription: Print between to points in a txt file

description: Print between to points in a txt file


code: print adj_close.iloc[locs, :].head(10)

rawDescription: R-Python: Getting Monthly, weekly index points


code:
foo = [4, 7, 2, 9]
tup = foo[foo.index(min(foo))], foo[0]
print tup
foo[0] = tup[0]
print foo
dx = foo.index(min(foo))
print dx
foo[dx] = tup[1] # foo[dx] equivalent to foo[foo.index(min(foo))]
print foo

rawDescription: Python value swapping does nothing

description: Python value swapping does nothing


code:
import re
text = "ThisFileName.txt"
print re.sub('([a-z])([A-Z])', r'\1 \2', text)
# Prints: "This File Name.txt"

rawDescription: Python regex: Turn "ThisFileName.txt" into "This File Name.txt"


code:
import matplotlib.pyplot as plt
import sys

class peg_tester():
    def __init__(self):
        self.fig = plt.figure(figsize=(3,1))
        self.ax = self.fig.add_axes([0,0,1,1])
        self.ax.set_xlim([-0.5,2.5])
        self.ax.set_ylim([-0.25,0.25])
        self.ax.text(-0.4, 0.15,
                'One click on the hole, and I get 2 events not 1',
                fontsize=8)

        self.holes = self.ax.scatter([1], [0], color='black', picker=0)
        self.pegs = self.ax.scatter([0], [0], s=100, facecolor='#dd8800',
                edgecolor='black', picker=0)

        self.fig.canvas.mpl_connect('pick_event', self.handler)

        # Creating a timer with a interval of 100 ms
        self.timer=self.fig.canvas.new_timer(interval=100)
        # Queue of pegs and holes that have been picked and waiting to be processed
        self.list_pegs_holes=[]
        self.timer.add_callback(self.update_pos,self.list_pegs_holes)
        self.timer.start()
        plt.show()

    # Add the artist to the queue of artists awaiting to be processed
    def handler(self, event):
        self.list_pegs_holes.append(event.artist)

    # Management of the queue
    def update_pos(self,list_pegs_holes):
        while len(list_pegs_holes)!=0:
            artist=list_pegs_holes.pop(0)
            if artist is self.holes:
                # If I get a hole event, then move a peg (to that hole) ...
                # but then I get a peg event also with no extra clicks!
                offs = self.pegs.get_offsets()
                offs[0,:] = [1,0] # Moves left peg to the middle
                self.pegs.set_offsets(offs)
                self.fig.canvas.draw()
                print 'picked a hole, moving left peg to center'
            elif artist is self.pegs:
                print 'picked a peg'
            sys.stdout.flush() # Necessary when in ipython qtconsole

if __name__ == "__main__":
    pt = peg_tester()

rawDescription: Matplotlib pick event order for overlapping artists


code:
from BeautifulSoup import BeautifulSoup
import re

# Assuming that html is your input as a string:
soup = BeautifulSoup(html)

all_contacts = []

def mailto_link(e):
    '''Return the email address if the element is is a mailto link,
    otherwise return None'''
    if e.name != 'a':
        return None
    for key, value in e.attrs:
        if key == 'href':
            m = re.search('mailto:(.*)',value)
            if m:
                return m.group(1)
    return None

for ul in soup.findAll('ul'):
    contact = {}
    for li in soup.findAll('li'):
        s = li.find('span')
        if not (s and s.string):
            continue
        if s.string == 'Email:':
            a = li.find(mailto_link)
            if a:
                contact['email'] = mailto_link(a)
        elif s.string == 'Website:':
            a = li.find('a')
            if a:
                contact['website'] = a['href']
        elif s.string == 'Phone:':
            contact['phone'] = unicode(s.nextSibling).strip()
    all_contacts.append(contact)

print all_contacts

rawDescription: How to get span value using python,BeautifulSoup

description: Get span value using python,BeautifulSoup


rawDescription: Printing nested lists in certain directions - Python


code:
print 5 * 10
# the five, ten, and the 50 are now gone.
template = "meow {}"
template.format('splat!')
# 'splat!' and 'meow splat!' are both gone, but your template still exists.
template
template = template % 'hiss!'  # this evaluates to "template = 'meow hiss!'"
template  # our template is now gone, replaced with 'meow hiss!'

rawDescription: Better Method in Python to Call Changed Dict Values Than This?


code:
mydataset = (
        ('Amy', '1', None, '1'),
        ('Kat', None, '1', '1'),
        ('Leo', None, None, '1')
)

def states(cols, data):
    """
    This function receives one of the tuples with data and returns a pair
    where the first element is the name from the tuple, and the second
    element is a set with all matched states. Well, at least *I* think
    it is more readable :)
    """
    name = data[0]
    states = set(state for state, value in zip(cols, data) if value == '1')
    return name, states

pairs = (states(state_cols, data) for data in mydataset)
# Since dicts can receive an iterator which yields pairs where the first one
# will become a key and the second one will become the value, I just pass
# a list with all pairs to the dict constructor.
print dict(pairs)

rawDescription: How do I create a dictionary mapping strings to sets given a list and a tuple of tuples?

description: Create a dictionary mapping strings to sets given a list and a tuple of tuples


rawDescription: iterate list and compare it's values with dict

description: Iterate list and compare it's values with dict


code:
import time

import second

timer = second.Timer()

print timer.timestamp()

rawDescription: Importing class from another file - where to put 'import' statements that the class requires


code:
from jinja2 import Template

ts = """
{% for game, gameSlots in assignedGameSlotsInMyGames.iteritems() %}
{% for gameSlot2 in gameSlots %}
<tr>
<td> {{gameSlot2.user.email}} </td>
<td> {{gameSlot2.gameCharacter.characterType}} </td>
</tr>
{% endfor %}
{% endfor %}
"""

t = Template(ts)

d = {'assignedGameSlotsInMyGames':
     {'game1': [{'user': {'email': 'a@a.ru'},
                 'gameCharacter':{'characterType':'testCharacterType1'}},
                {'user': {'email': 'b@b.ru'},
                 'gameCharacter':{'characterType':'testCharacterType2'}}],
      'game2': [{'user': {'email': 'a2@a.ru'},
                 'gameCharacter':{'characterType':'testCharacterType12'}},
                {'user': {'email': 'b2@b.ru'},
                 'gameCharacter':{'characterType':'testCharacterType22'}}]
     }
}

print t.render(d)

rawDescription: Python (2.7) dictionary Jinja confusion

description: Python (2.7) dictionary Jinja confusion


code:
import csv
for line in csv.DictReader(open('t.tsv')): print line # don't forget to make your file coma-separated.

rawDescription: How to read a specific row of a csv file in python?

description: Read a specific row of a csv file in python


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


code:
idf = vectorizer._tfidf.idf_
print dict(zip(vectorizer.get_feature_names(), idf))

rawDescription: tf-idf feature weights using sklearn.feature_extraction.text.TfidfVectorizer

description: Tf-idf feature weights using sklearn.feature_extraction.text.TfidfVectorizer


code:
import win32com.client as win32
import glob
import os

excel = win32.gencache.EnsureDispatch('Excel.Application')

for excel_filename in glob.glob(r'c:\excel_files_folder\*.xlsx'):
    print excel_filename
    wb = excel.Workbooks.Open(excel_filename)
    wb.SaveAs(os.path.splitext(excel_filename)[0] + '.xls', FileFormat=56, ConflictResolution=2)

excel.Application.Quit()

rawDescription: convert xlsx files to xls inside folders and subfolders in Excel VBA or Python

description: Convert xlsx files to xls inside folders and subfolders in Excel VBA or Python


code: print out

rawDescription: Applying a disc shaped mask to several locations in a NumPy array


code: print A.class_property

rawDescription: difference between an object property and type property

description: Difference between an object property and type property


code:
import numpy as np
import cv2
from cv2 import cv

# Load image as string from file/database
fd = open('foo.jpg')
img_str = fd.read()
fd.close()

# CV2
nparr = np.fromstring(img_str, np.uint8)
img_np = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR) # cv2.IMREAD_COLOR in OpenCV 3.1

# CV
img_ipl = cv.CreateImageHeader((img_np.shape[1], img_np.shape[0]), cv.IPL_DEPTH_8U, 3)
cv.SetData(img_ipl, img_np.tostring(), img_np.dtype.itemsize * 3 * img_np.shape[1])

# check types
print type(img_str)
print type(img_np)
print type(img_ipl)

rawDescription: Python OpenCV load image from byte string


code:
import requests as rq

url = "http://api.sacbeelabs.com/v1/statepay/employee/search/name=/year=2013/department=CSU%20Sacramento.json"
data = "74XoegZ494trsvrus_As4B4handjZ494-Adl4B4olg494dnnk933pppAmWYXaaAYjh3mnWnakWq3-Ela-B-Oahkgjqaa07tw8tJmaWlYd07tw8tJiWha07tw8uH07tw8tJqaWl07tw8uHtrsu07tw8tJZakWlnhain07tw8uHGT-107tw8trTWYlWhainj4B4labalal494dnnk933mnWYfj-8albgjpAYjh3-Boamnejim3tt_v_rt_3YlWpgeic1nWXgam1bljh1paXkWca4B4nenga494TnWnaDVjlfalDTWgWlqDTaWlYdD1DUdaDTWYlWhainjDFaaBDTWYlWhainjBDGWgebjlieW4B4mYlV49sxzrB4mYlL49srwrB4peiV49sxzrB4peiL49_stB4oW4974Wcain494Oj-CeggW3wArD-I-6ss-MD-1Xoino-MDNeio-AD-Azx2xv-MDl-89tzAr-JDKaYfj3trsrrsrsDJelabj-A3tzAr4B4njoYd49bWgmaB4Zjh4954mnjlWca4B4WiehWneji4B4YWi-8WmtZ4B4paXmjYfan4B4pjlfal4B4WoZej4B4-8eZaj4B4m-8c4B4cajgjY46B4Ymm4954WiehWneji4B4nlWimbjlh468B4omal4974Woi494Koamn488"
headers = {
'Host': 'api.sacbeelabs.com',
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:28.0) Gecko/20100101 Firefox/28.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'X-SBAPI-Auth-Token': '0QNWbefXw6fQQcWXqK8vDw',
'X-SBAPI-SID': '3gbRqglHXAVDy1vwdcVVMf',
'X-SBAPI-CID': '2HuWho39ZcDUlTswYSWUd9',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Referer': 'http://www.sacbee.com/statepay/',
'Content-Length': '684',
'Origin': 'http://www.sacbee.com',
'Cookie': 'sbapi-cid=2HuWho39ZcDUlTswYSWUd9; sbapi-sid=3gbRqglHXAVDy1vwdcVVMf',
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache'
}

r = rq.post(url, data=data, headers=headers)
json_data = r.json()

base = json_data["result"]["employees"][0] # First employee.

name = base["name"]
first_name = name["first"]
last_name = name["last"]

pay = base["pay"]["total"]

title = base["title"]
dept = base["department"]

print first_name, last_name, pay, title, dept
# Your turn here...

rawDescription: Crawling tables from webpage

description: Crawling tables from webpage


code:
from bs4 import BeautifulSoup

data = """

    (1)

"""

soup = BeautifulSoup(data)
for sup in soup.find_all('sup'):
    sup.unwrap()

print soup.prettify()

rawDescription: Using Python and Regex,How do you remove <sup> tags from html?


code: print pythonFunction.function()

rawDescription: how to call a function from another file?

description: Call a function from another file


code:
df = df.astype(str)
df['Month'] = df['Month'].str.zfill(2)
df['Day'] = df['Day'].str.zfill(2)
print df

rawDescription: Append based on length of string

description: Append based on length of string


code:
newlist = []
for colors in dic.values():
    lis = colors[1:]        #do slicing here
    for item in lis:
        if item not in newlist:
            newlist.append(item)
print newlist #['green', 'blue', 'yellow', 'black']

rawDescription: Iterating over a dictionary of list elements


rawDescription: Split array into equally weighted chunks based on order


code:
class A(object):

    def test(self):
        print 'A'

class B(object):

    def test(self):
        print 'B'

class C(A, B):

    def test(self):
        B.test(self)

c = C()
c.test()

rawDescription: How do I call a method on a specific base class in Python?

description: Call a method on a specific base class in Python


code:
import os
from xml.sax.saxutils import escape as xml_escape

def DirAsXML(path):
    result = '<dir>\n<name>%s</name>\n' % xml_escape(os.path.basename(path))
    dirs = []
    files = []
    for item in os.listdir(path):
        itempath = os.path.join(path, item)
        if os.path.isdir(itempath):
            dirs.append(item)
        elif os.path.isfile(itempath):
            files.append(item)
    if files:
        result += '  <files>\n' \
            + '\n'.join('    <file>\n      <name>%s</name>\n    </file>'
            % xml_escape(f) for f in files) + '\n  </files>\n'
    if dirs:
        for d in dirs:
            x = DirAsXML(os.path.join(path, d))
            result += '\n'.join('  ' + line for line in x.split('\n'))
    result += '</dir>'
    return result

if __name__ == '__main__':
    print '<structure>\n' + DirAsXML(os.getcwd()) + '\n</structure>'

rawDescription: os.walk() python: xml representation of a directory structure, recursion

description: Os.walk() python: xml representation of a directory structure, recursion


code:
print "first one", tweets[0]
print "last one", tweets[-1]
print "number of tweets", len(tweets)

rawDescription: How to dynamicaly create a variable in a for?

description: Dynamicaly create a variable in a for


code:
class JoinAdapter(Adapter):
    def _decode(self, obj, context):
        return "".join(obj)

ADVERT_PAYLOAD = Struct("ADVERT_PAYLOAD",
    Field("AdvertAddress",6),
    JoinAdapter(GreedyRange(Field("AdvertData", 1)))
)

payload = '2\x15\x00a\x02\x00\x02\x01\x06\x07\x03\x03\x18\x02\x18\x04\x18\x03\x19\x00\x02\x02\n\xfe\t\tAS-D1532'
print ADVERT_PAYLOAD.parse(payload)

rawDescription: In python construct library (for parsing binary data), how to group the rest of data as one field?


code:
import re

def get_words(filename):
    with open(filename, 'r') as f_input:
        return set(w.lower() for w in re.findall(r'(\w+)', f_input.read()))

words1 = get_words('document1.txt')
words2 = get_words('document2.txt')

print words2 - words1

rawDescription: How to find words that are not the same between two text files

description: Find words that are not the same between two text files


code:
for row in result:
    for value in row:
        print value

rawDescription: Result of type long from a SqlProxyRow result in string


rawDescription: Get values from matplotlib AxesSubplot

description: Get values from matplotlib AxesSubplot


code:
#!/usr/bin/python
import pyglet
from time import time, sleep

class Window(pyglet.window.Window):
    def __init__(self, refreshrate):
        super(Window, self).__init__(vsync = False)
        self.frames = 0
        self.framerate = pyglet.text.Label(text='Unknown', font_name='Verdana', font_size=8, x=10, y=10, color=(255,255,255,255))
        self.last = time()
        self.alive = 1
        self.refreshrate = refreshrate
        self.click = None
        self.drag = False

    def on_draw(self):
        self.render()

    def on_mouse_press(self, x, y, button, modifiers):
        self.click = x,y

    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        if self.click:
            self.drag = True
            print 'Drag offset:',(dx,dy)

    def on_mouse_release(self, x, y, button, modifiers):
        if not self.drag and self.click:
            print 'You clicked here', self.click, 'Relese point:',(x,y)
        else:
            print 'You draged from', self.click, 'to:',(x,y)
        self.click = None
        self.drag = False

    def render(self):
        self.clear()
        if time() - self.last >= 1:
            self.framerate.text = str(self.frames)
            self.frames = 0
            self.last = time()
        else:
            self.frames += 1
        self.framerate.draw()
        self.flip()

    def on_close(self):
        self.alive = 0

    def run(self):
        while self.alive:
            self.render()
            # ----> Note: <----
            #  Without self.dispatc_events() the screen will freeze
            #  due to the fact that i don't call pyglet.app.run(),
            #  because i like to have the control when and what locks
            #  the application, since pyglet.app.run() is a locking call.
            event = self.dispatch_events()
            sleep(1.0/self.refreshrate)

win = Window(23) # set the fps
win.run()

rawDescription: Pyglet not running properly on AMD HD4250


code:
import pandas as pd
import numpy as np

data = {'Item1' : pd.DataFrame(np.random.randn(4, 3)), 'Item2' : pd.DataFrame(np.random.randn(4, 3))}

p = pd.Panel(data)

d = {'Item1':1, 'Item2':2}

for key, values in d.iteritems():

    p[key]['newcol'] = values

    print p[key]

rawDescription: Python - Add a column to each dataframe of a Pandas Panel


code:
old_input_states = [None] * 3
while True:
    input_states = [GPIO.input(n) for n in (37, 32, 29)]
    if input_states != old_input_states:
        inverted_input_states = [1 - i for i in input_states]
        print inverted_input_states
        time.sleep(0.2)
    old_input_states = input_states

rawDescription: Python Don't Repeat Consecutive Lines

description: Python Don't Repeat Consecutive Lines


code:
listy = [2,2,1,3,4,2,1,2,3,4,5]
found = set()
for i in listy:
    if not i in found:
        print i, listy.count(i)
        found.add(i)

rawDescription: Loop problem while iterating through a list and removing recurring elements


code: print text.count("cat")

rawDescription: A regular expression for finding a word within a string exactly "n" times


code:
weightSum = 0
for weight in elements.values():
    weightSum += weight
print weightSum
# output:
17,0073

rawDescription: Generating raw_inputs based on user input? [Python]


rawDescription: Add an object to a python list

description: Add an object to a python list


rawDescription: every other element in a list then reverse

description: Every other element in a list then reverse


code:
def fibonacciRecursive(n, print_recAdd=True):
    global recAdd
    if n == 1 or n == 0:
        retval = n  # <-- should this be 1?
    else:
        recAdd = recAdd + 1
        retval = fibonacciRecursive(n - 1, False) + fibonacciRecursive(n - 2, False)
    if print_recAdd:
        print recAdd
    return retval

rawDescription: I cannot print the final value of a variable


code:
import json

print json.dumps(your_data, sort_keys=True, indent=4, separators=(',', ': '))

rawDescription: Breaking down lists inside of lists into a multi line string


code:
from lxml import etree
from lxml.builder import E as buildE

class DummyCursor(object):
  def __init__(self,fields,rows=5):
    self.description = [[f] for f in fields]
    self.data = [ ["%s%02d" % (f,i) for f in fields] for i in range(rows) ]
  def fetchall(self):
    return self.data

def E(tag,parent=None,content=None):
  """Simple E helper"""
  element = buildE(tag)
  if content is not None:
    element.text = unicode(content)
  if parent is not None:
    parent.append(element)
  return element

def fetchXML(cursor):
  fields = [x[0] for x in cursor.description ]
  doc = E('data')
  for record in cursor.fetchall():
    r = E('row',parent=doc)
    for (k,v) in zip(fields,record):
      E(k,content=v,parent=r)
  return doc

doc = fetchXML(DummyCursor(['name','description']))

print etree.tostring(doc,pretty_print=True)

rawDescription: Creating xml from MySQL query with Python and lxml


code:
def list_search(input_list, search_term, place=1):
    terms = iter(input_list)
    new_list = []
    try:
        [new_list.append(terms.next()) for term in terms if term == search_term[place-1]]
    except StopIteration:
        pass
    return new_list

tests = [
    (['a','b','c','a','e'], 'a', 1),
    (['a','b','c','a','e'], 'b', 1),
    (['a','b','c','a','e'], 'ab', 2),
    (['a','b','c','a','e'], 'e', 1),
    (['a','a','a'], 'b', 1),
    (['a','a','a'], 'a', 1)]

for input_list, search_term, place in tests:
    print list_search(input_list, search_term, place)

rawDescription: Accessing a repeated element in the list and printing the element next to it


code:
def f(*args):
   print "len args:",len(args)
   for a in args:
      print a

tu = (('aa',), ('bb',), ('cc',))
f(tu)
f(*tu)

rawDescription: How do I convert tuple of tuples to list in one line (pythonic)?

description: Convert tuple of tuples to list in one line (pythonic)


code:
import numpy

x = numpy.arange(1000000, dtype=numpy.int32).reshape((-1,2))
bad = numpy.arange(0, 1000000, 2000, dtype=numpy.int32)

print x.shape
print bad.shape

cleared = numpy.delete(x, numpy.where(numpy.in1d(x[:,0], bad)), 0)
print cleared.shape

rawDescription: Removing rows in NumPy efficiently

description: Removing rows in NumPy efficiently


code:
te = [
      {
        "Name": "Bala",
        "phone": "None"
      },
      {
        "Name": "Bala",
        "phone": "None"
      },
      {
        "Name": "Bala",
        "phone": "None"
      },
      {
        "Name": "Bala",
        "phone": "None"
      },
      {
          "Name": "Bala1",
          "phone": "None"
      }
    ]

unique = { each['Name'] : each for each in te }.values()

print unique

rawDescription: Remove duplicate JSON objects from list in python


code:
def foo(*args, **kw):
    print args, kw
foo('arg1', 'arg2', foo='bar', spam='eggs')

rawDescription: what is the asterisk in "replace(*something)" for? (python)

description: What is the asterisk in "replace(*something)" for? (python)


code:
def dutchnametag(name):
    name = name.replace("art", "aart")
    print "HELLO! My Dutch name is", name

person = "Slartibartfast"
dutchnametag(person)

rawDescription: Python immutable object from within function


code:
# Using "with open()" to open the file handle and have it automatically closed for your when the program exits.
with open("test.txt", 'r+') as f:
    print "Name of the file: ", f.name
    # Iterate over each line in the test.txt file
    for line in f:
        # Using strip() to remove newline characters and white space from each end of the line
        # Using split(',') to create a list ("tokens") containing each segment of the line separated by commas
        tokens = line.strip().split(',')
        # Print out the very first element (position 0) in the tokens list, which should be the "class"
        print "class: ", tokens[0]
        # Print out all of the remaining elements in the tokens list, starting at the second element (i.e. position "1" since lists are "zero-based")
        # This is using a "slice".  "tokens[1:]" means return the contents of the tokens list starting at position 1 and continuing to the end
        # "tokens[1:3]" Would mean give me all of the elements of the tokens list starting at position 1 and ending at position 3 (excluding position 3).
        # Loop over the elements returned by the slice, assigning them one by one to the "argument" variable
        for argument in tokens[1:]:
            # Print out the argument
            print "argument: ", argument

rawDescription: Reading from a file/split function new character line


rawDescription: Python: Bytes to string with accented characters


code:
for tag in soup.findAll('source'):
    print tag.string

rawDescription: Removing a tag and its contents in xml using BeautifulSoup an lxml in Python


code:
import random

rollAgain = "Yes"

while rollAgain in ["Yes" , "yes", "y"]:
    diceChoice = input ("Which dice would you like to roll; 4 sided, 6, sided or 12 sided?")
    if diceChoice in ["4","6","12"]:
        print("You rolled a ",random.randint(1,int(diceChoice)))
    else:
        print "Please input 4,6, or 12."
    rollAgain = input ("Roll Again?")

print ("Thank you for playing")

rawDescription: Dice game query While loop

description: Dice game query While loop


code:
def calc(base, alist, blist):
    result = base[:]
    for num in range(3):
        for i, v in enumerate(result):
            result[i] = v + alist[num] - blist[num]
        print result

rawDescription: How can I solve this without list comprehensions

description: Solve this without list comprehensions


code:
def convert(text):
    def replace(match):
        d = {'1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five', '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine', 'N': 'North', 'E': 'East', 'S': 'South', 'W': 'West'}
        source = match.group(1).strip()

        if 'km' in source:
            source = source.split("km")[0].strip()
            return "{} kilometre{}".format(d.get(source, source), '' if source == '1' else 's')
        else:
            return d.get(source, source) + ' '

    return re.sub(r'(\d+ *km|[NESW]\s+(?=of))', replace, text)

print convert('1 km N of Piedmont, California')
print convert('1km N of China')

rawDescription: Specific String replacement

description: Specific String replacement


code:
import sys
print sys.path

rawDescription: Python modules not found over terminal but on python shell, Linux


code:
import re
s = 'category:buffer overflow  analyzer:data flow analyzer:buffer'
print re.sub(r"\s(?=[^\s]*:)",'\n',s)

rawDescription: Read a file and cut each line based on multiple criteria


code:
import os

def my_action(source, target, env):
    cmd = r'''echo its a small world after all \
        its a small world after all'''
    print cmd
    return os.system(cmd)

my_cmd_builder = Builder(
    action=my_action,  # <-- CRUCIAL PIECE OF SOLUTION
    suffix = '.foo')

env = Environment()
env.Append( BUILDERS = {'MyCmd' : my_cmd_builder } )

my_cmd = env.MyCmd('foo.foo',os.popen('which bash').read().strip())

rawDescription: How can I generate multi-line build commands?

description: Generate multi-line build commands


rawDescription: Python, Numpy - Trying split an array according to a condition


code:
import socket

HOST, PORT = "10.XX.XX.XX", 5000
ctrl_cyc="1234567"
data = ""
data += str(ctrl_cyc)

    # Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    # Connect to server and send data
    sock.connect((HOST, PORT))
    sock.sendall(bytes(data + "\n"))
    data = sock.recv(1024)
    print data

finally:
    sock.close()

rawDescription: tcp server c# on windows, tcp client python raspberry pi

description: Tcp server c# on windows, tcp client python raspberry pi


code:
for index, line in enumerate(bgp_table_list):
    # print line.split(), len(line.split())

    current_line = line.split()
    # If 7 items are in the list it's the start of a network
    if len(current_line) == 7:
        best = True if '>' in current_line[0] else False
        network = current_line[1]
        attached_ip = current_line[2]
        print "Found Starting Network", network, attached_ip, "Best Path: {}".format(best)
        for i in range(1, 1000):
            try:
                line = bgp_table_list[index+i]
            except IndexError:
                break
            subline_line = line.split()
            best_sub = 'True' if '>' in subline_line[0] else 'False'
            if len(subline_line) == 6:
                print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
            elif len(subline_line) == 5:
                print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
            elif len(subline_line) == 7:
                break

rawDescription: How to check next iter item without proceeding in For loop in Python

description: Check next iter item without proceeding in For loop in Python


code:
def length(x):
    b = 0
    for i in x:
        b += 1
    print b
length('foo')

rawDescription: For Loop Count Single Word String Not Work?


code:
import sys, os, hashlib, StringIO, bencode

def pieces_generator(info):
    """Yield pieces from download file(s)."""
    piece_length = info['piece length']
    if 'files' in info: # yield pieces from a multi-file torrent
        piece = ""
        for file_info in info['files']:
            path = os.sep.join([info['name']] + file_info['path'])
            print path
            sfile = open(path.decode('UTF-8'), "rb")
            while True:
                piece += sfile.read(piece_length-len(piece))
                if len(piece) != piece_length:
                    sfile.close()
                    break
                yield piece
                piece = ""
        if piece != "":
            yield piece
    else: # yield pieces from a single file torrent
        path = info['name']
        print path
        sfile = open(path.decode('UTF-8'), "rb")
        while True:
            piece = sfile.read(piece_length)
            if not piece:
                sfile.close()
                return
            yield piece

def corruption_failure():
    """Display error message and exit"""
    print("download corrupted")
    exit(1)

def main():
    # Open torrent file
    torrent_file = open(sys.argv[1], "rb")
    metainfo = bencode.bdecode(torrent_file.read())
    info = metainfo['info']
    pieces = StringIO.StringIO(info['pieces'])
    # Iterate through pieces
    for piece in pieces_generator(info):
        # Compare piece hash with expected hash
        piece_hash = hashlib.sha1(piece).digest()
        if (piece_hash != pieces.read(20)):
            corruption_failure()
    # ensure we've read all pieces
    if pieces.read():
        corruption_failure()

if __name__ == "__main__":
    main()

rawDescription: Extract the SHA1 hash from a torrent file


code:
regex = r"^(A*B)*$"

pattern = re.compile(regex)
for s in "AABAABB", "ABCAAB", "B", "BBB", "AABAABBx", "xAABAABB":
    if pattern.match(s):
        print "Matched:", s

rawDescription: Check if a string is a complete match for a regex expression in python


code:
class FileSystem():

    def __init__(self,filePath=None):
        self.children = []
        if filePath != None:
            try:
                self.name, child = filePath.split("/", 1)
                self.children.append(FileSystem(child))
            except (ValueError):
                self.name = filePath

    def addChild(self, filePath):
        try:
            thisLevel, nextLevel = filePath.split("/", 1)
            try:
                if thisLevel == self.name:
                    thisLevel, nextLevel = nextLevel.split("/", 1)
            except (ValueError):
                self.children.append(FileSystem(nextLevel))
                return
            for child in self.children:
                if thisLevel == child.name:
                    child.addChild(nextLevel)
                    return
            self.children.append(FileSystem(nextLevel))
        except (ValueError):
            self.children.append(FileSystem(filePath))

    def getChildren(self):
        return self.children

    def printAllChildren(self, depth = -1):
        depth += 1
        print "\t"*depth + "Name: "+ self.name
        if len(self.children) > 0:
            print "\t"*depth +"{ Children:"
            for child in self.children:
                child.printAllChildren(depth)
            print "\t"*depth + "}"

records = ["base/images/graphs/one.png", "base/images/tikz/two.png",
"base/refs/images/three.png", "base/one.txt", "base/chapters/two.txt"]

myFiles = FileSystem(records[0])
for record in records[1:]:
    myFiles.addChild(record)

myFiles.printAllChildren()

rawDescription: Creating Nested Dictionary from File hierarchy


code:
m = re.match(r"^Set HP (\d+) to (\d+)$", userstring)
if m:
    print m.groups()

rawDescription: How do match and extract groups from regex in Python?


code:
import timeit
s = """paragraph = "five hundred to one hundred words separated by spaces"
array_of_strings = set(['hundred', 'spaces', ])  # make a set..

set(paragraph.split()).intersection(array_of_strings)
"""
t = timeit.Timer(stmt=s)
print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)

rawDescription: rough estimate time it takes to do string comparisons Python

description: Rough estimate time it takes to do string comparisons Python


code:
content='''
   **001**     Math        **02/20/2013**  A

   **001**     Literature  **03/02/2013**  B

   **002**     Biology     **01/01/2013**  A

   **003**     Biology     **04/08/2013**  A

   **001**     Biology     **05/01/2013**  B

   **002**     Math        **03/10/2013**  C
'''

from collections import defaultdict

lines = content.split("\n")
items_iter = (line.split() for line in lines if line.strip())

aggregated = defaultdict(list)

for items in items_iter:
    stud, class_, date, grade = (t.strip('*') for t in items)
    aggregated[stud].append((class_, grade, date))

for stud, data in aggregated.iteritems():
    full_grades = [';'.join(items) for items in data]
    print '{},#{}'.format(stud, '#'.join(full_grades))

rawDescription: How to Aggregate records in Python?

description: Aggregate records in Python


code:
#!/usr/bin/env python

raw_input() # Bot1: What is your name?
print "Bot2"
greeting = raw_input() # Bot1: Hello Bot2, my name is Bot1
print "Hello {}".format(greeting.split()[-1])

rawDescription: Python: Communicate with always running subprocesses


code:
import re

line_in = "gi|393925858|gb|AGTA02071966.1|    739  .   G   A   121.2   PASS    NS=74;AN=2;DP=8448  GT:DP:GQ:EC:SG  0/1:262:99:116:R    1:32:93:32:A    0:87:42:72:R    .:0:0:0:.   .:0:0:0:.   0/1:222:99:113:R"

line_parts = line_in.split()
output_parts = []

for part in line_parts:

    output_part = part
    matches = re.match(r'0(:\d+:\d+:\d+:[.|\w])', part)

    if matches:
        output_part = part.replace('0', '0/0')

    else:
        matches = re.match(r'1(:\d+:\d+:\d+:[.|\w])', part)
        if matches:
            output_part = part.replace('1', '1/1')

    output_parts.append(output_part)

line_out = ' '.join(output_parts)
print line_out

rawDescription: split the elements in the line, how to rejoin them with the updated value

description: Split the elements in the line, how to rejoin them with the updated value


code:
p1 = Player("Jane")
p2 = Player("John")
ROSTER
for p in ROSTER:
  print p.name, p.score

rawDescription: Simple OOP query. Python: saving object names in __init__


code:
import random

def obfuscate(word):
    new_word = ''
    for char in word:
        if random.random() > 0.1:
            new_word += random.choice(["!", "%", "*", "#", "@"])
        else:
            new_word += char
    return new_word

user_input = raw_input("Enter a 4-letter word:  ")
if len(user_input) == 4:
    print "Original:", user_input
    print "Obfuscate:", obfuscate(user_input)

rawDescription: Python, obfuscating letters in a list

description: Python, obfuscating letters in a list


code:
text = soup.get_text()
print text

rawDescription: using BeautifulSoup to pick up texts separated by ":"

description: Using BeautifulSoup to pick up texts separated by ":"


code:
b = np.array([True,True,True,False,False,True,True,False,True,True,True,True,False])
idx = np.argwhere(np.diff(np.r_[False, b, False])).reshape(-1, 2)
idx[:, 1] -= 1
print idx

rawDescription: numpy: search of the first and last index in an array

description: Numpy: search of the first and last index in an array


code:
from decorator import decorator
def _dynamic_programming(f, *args, **kwargs):
    print "Inside decorator", id(f.cache)
    try:
        f.cache[args]
    except KeyError:
        f.cache[args] = f(*args, **kwargs)
    return f.cache[args]

def dynamic_programming(f):
    f.cache = {}
    print "Original cache", id(f.cache)
    def clear():
        f.cache = {}
        print "New cache", id(f.cache)
    f.clear = clear
    return decorator(_dynamic_programming, f)

@dynamic_programming
def fib(n):
    if n <= 1:
        return 1
    else:
        return fib(n-1) + fib(n-2)

print fib(4)
print id(fib.cache)
fib.clear()
print id(fib.cache)
print fib(10)
print id(fib.cache)

rawDescription: Reassign a function attribute makes it 'unreachable'


rawDescription: Python: Start each newline with a specific character


code:
class Helloer(object):
    def __init__(self, owner):
        self.owner= owner
    def hello(self):
        print 'hello '+self.owner.person

import myframework
myframework.MyFramework.addAddon('helloer', Helloer)

rawDescription: Putting separate python packages into same namespace?


code:
import fileinput
with open('output.txt', 'w') as f2:
    lines = []
    for line in fileinput.input('input.txt', inplace = True):
        if line.startswith('>'):
             if lines:
                 print "".join(lines),
                 lines = [line]
             else:
                 lines.append(line)
        elif line.rstrip('\n') == 'Sequence unavailable':
             f2.writelines(lines + [line])
             lines = []
        else:
             lines.append(line)

    with open('input.txt','a') as f:
        f.writelines(lines)

rawDescription: Deleting specific text from text file

description: Deleting specific text from text file


code:
import json

class NoIndent(object):
    def __init__(self, value):
        self.value = value
    def __repr__(self):
        if not isinstance(self.value, (list, tuple)):
            return repr(self.value)
        else:  # assume it's a list or tuple of coordinates stored as dicts
            delimiters = '[]' if isinstance(self.value, list) else '()'
            pairs = ('{!r}:{}'.format(*component)
                         for coordinate in self.value
                             for component in sorted(coordinate.items()))
            pairs = ('{{{}, {}}}'.format(*pair)
                         for pair in zip(*[iter(pairs)]*2))
            return delimiters[0] + ', '.join(pairs) + delimiters[1]

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        return(repr(obj) if isinstance(obj, NoIndent) else
               json.JSONEncoder.default(self, obj))

data_structure = {
    'layer1': {
        'layer2': {
            'layer3_1': NoIndent([{"x":1,"y":7}, {"x":0,"y":4},
                                  {"x":5,"y":3}, {"x":6,"y":9}]),
            'layer3_2': 'string'
        }
    }
}

print json.dumps(data_structure, cls=MyEncoder, indent=2)

rawDescription: Can I implement custom indentation for pretty-printing in Python’s JSON module?

description: Implement custom indentation for pretty-printing in Python’s JSON module


code:
import numpy as np

ua = 5 # No idea what "ua" is in your code above...
low_inc, med_inc = 0.5, 2.0 # Again, no idea what these are...

num = 100
num_fields = 11

# Use more descriptive names than "col1"! I'm just generating the names as placeholders
dtype = {'names':['col%i'%i for i in range(num_fields)],
                 'formats':2*[np.int] + 2*[np.float] + 2*[np.int] + 2*[np.bool] + 3*[np.int]}
data = np.zeros(num, dtype=dtype)

# Being rather verbose...
data['col0'] = np.arange(num, dtype=np.int)
data['col1'] = int(ua) * np.ones(num)
data['col2'] = np.random.uniform(low_inc / 2, med_inc * 2, num)
data['col3'] = np.random.uniform(0, 6, num)
data['col4'] = np.random.randint(100, 5000, num)
data['col5'] = np.random.randint(100, 500, num)
data['col6'] = np.random.randint(0, 2, num).astype(np.bool)
data['col7'] = np.random.randint(0, 2, num).astype(np.bool)
data['col8'] = np.random.randint(100, 5000, num)
data['col9'] = np.random.randint(100, 5000, num)
data['col10'] = np.random.randint(100, 5000, num)

print data

rawDescription: specify dtype of each object in a python numpy array

description: Specify dtype of each object in a python numpy array


code:
def find(myStr, end=None):
    end = len(myStr) if end is None else end
    print end
# end is set to the supplied value
find("abc", 10)
# end is set to the length of myStr (its default value)
find("abc")

rawDescription: one argument default value depends on the len(otherArgument)

description: One argument default value depends on the len(otherArgument)


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

def f(x, b, c):
    return 1. / (c * np.exp(-b * x))

def f_log(x, b, c_ln):
    return b * x - c_ln

# some random data
b_org = 0.08
c_org = 100.5
x_data = np.linspace(0.01, 100., 50)
y_data = f(x_data, b_org, c_org) + np.random.normal(0, 0.5, len(x_data))

# fit the data
popt, pcov = curve_fit(f, x_data, y_data, p0=(0.1, 50))
print popt

# plot the data
xnew = np.linspace(0.01, 100., 5000)
plt.plot(x_data, y_data, 'bo')
plt.plot(xnew, f(xnew, *popt), 'r')
plt.show()

# your data
x_data = np.array([4, 6, 8, 10])
y_data = np.array([0.86, 0.73, 0.53, 0.3])

# fit the data
popt_log, pcov_log = curve_fit(f_log, x_data, y_data)
print popt_log

# plot the data
xnew = np.linspace(4, 10., 500)
plt.plot(x_data, y_data, 'bo')
plt.plot(xnew, f_log(xnew, *popt_log), 'r')
plt.show()

rawDescription: How to fit the following function using curve_fit

description: Fit the following function using curve_fit


code:
import re

text = """
    I also found another solution using
    regex, but I'm still a novice and
    really can't understand that solution.
    Any help would be appreciated.
"""

print re.sub(r'\b\w{4}\b', 'xxxx', text)

rawDescription: Replacing four letter words in a string

description: Replacing four letter words in a string


code:
import re
from markdown import Markdown
from markdown.inlinepatterns import ImagePattern, IMAGE_LINK_RE

RE_REMOTEIMG = re.compile('^(http|https):.+')

class CheckImagePattern(ImagePattern):

    def handleMatch(self, m):
        node = ImagePattern.handleMatch(self, m)
        # check 'src' to ensure it is local
        src = node.attrib.get('src')
        if src and RE_REMOTEIMG.match(src):
            print 'ILLEGAL:', m.group(9)
            # or alternately you could raise an error immediately
            # raise ValueError("illegal remote url: %s" % m.group(9))
        return node

DATA = '''
![Alt text](/path/to/img.jpg)
![Alt text](http://remote.com/path/to/img.jpg)
'''

mk = Markdown()
# patch in the customized image pattern matcher with url checking
mk.inlinePatterns['image_link'] = CheckImagePattern(IMAGE_LINK_RE, mk)
result = mk.convert(DATA)
print result

rawDescription: Check image urls using python-markdown

description: Check image urls using python-markdown


rawDescription: Sum of multiple list of lists index wise


code:
tuples = [('apples', 2, 'apple'), ('oranges', 3, 'orange')]
list_of_lines = list_of_lines = ['banana 1234', 'apple 5678', 'oranges 4321','apples 8765', 'orange 1111']
d = dict(l.split() for l in list_of_lines)
for t in tuples:
    print "'%s', %s, %s, '%s', %s" % (t[0], d[t[0]], t[1], t[2], d[t[2]])

rawDescription: Match members of a tuple to members of a list, python


code: print request.user.__dict__

rawDescription: In Django, how can I view all the properties of my user?


code:
#! /usr/bin/python

import cv2
from itertools import count

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

# Load the overlay image: glasses.png
imgGlasses = cv2.imread('eye.png')

#Check if the files opened
if  imgGlasses is None :
    exit("Could not open the image")
if  face_cascade.empty() :
    exit("Missing: haarcascade_frontalface_default.xml")
if  eye_cascade.empty() :
    exit("Missing: haarcascade_eye.xml")

# Create the mask for the glasses
imgGlassesGray = cv2.cvtColor(imgGlasses, cv2.COLOR_BGR2GRAY)
ret, orig_mask = cv2.threshold(imgGlassesGray, 10, 255, cv2.THRESH_BINARY)

#orig_mask = imgGlasses[:,:,3]

# Create the inverted mask for the glasses
orig_mask_inv = cv2.bitwise_not(orig_mask)

# Convert glasses image to BGR
# and save the original image size (used later when re-sizing the image)
imgGlasses = imgGlasses[:,:,0:3]
origGlassesHeight, origGlassesWidth = imgGlasses.shape[:2]

#cv2.imshow('Video', imgGlasses)
#cv2.waitKey()

video_capture = cv2.VideoCapture(0)

if not video_capture.isOpened() :
    exit('The Camera is not opened')

counter = count(1)

while True:
    print "Iteration %d" % counter.next()
    ret, frame = video_capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = frame[y:y+h, x:x+w]
        eyes = eye_cascade.detectMultiScale(roi_gray)

        #cv2.imshow('Video', roi_gray)
        #cv2.waitKey()

        #print 'X:%i, Y:%i, W:%i, H:%i' % (x, y, w, h)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
            print 'EX:%i, EY:%i, EW:%i, EH:%i' % (ex, ey, ew, eh)

        for (ex, ey, ew, eh) in eyes:
            glassesWidth = 3*ew
            glassesHeight = glassesWidth * origGlassesHeight / origGlassesWidth

            # Center the glasses on the bottom of the nose
            x1 = ex - (glassesWidth/4)
            x2 = ex + ew + (glassesWidth/4)
            y1 = ey + eh - (glassesHeight/2)
            y2 = ey + eh + (glassesHeight/2)

                # Check for clipping
            if x1 < 0:
                x1 = 0
            if y1 < 0:
                y1 = 0
            if x2 > w:
                x2 = w
            if y2 > h:
                y2 = h

            # Re-calculate the width and height of the glasses image
            glassesWidth = x2 - x1
            glassesHeight = y2 - y1

            # Re-size the original image and the masks to the glasses sizes
            # calcualted above
            glasses = cv2.resize(imgGlasses, (glassesWidth,glassesHeight), interpolation = cv2.INTER_AREA)
            mask = cv2.resize(orig_mask, (glassesWidth,glassesHeight), interpolation = cv2.INTER_AREA)
            mask_inv = cv2.resize(orig_mask_inv, (glassesWidth,glassesHeight), interpolation = cv2.INTER_AREA)

            # take ROI for glasses from background equal to size of glasses image
            roi = roi_color[y1:y2, x1:x2]

            # roi_bg contains the original image only where the glasses is not
            # in the region that is the size of the glasses.
            roi_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

            # roi_fg contains the image of the glasses only where the glasses is
            roi_fg = cv2.bitwise_and(glasses,glasses,mask = mask)

            # join the roi_bg and roi_fg
            dst = cv2.add(roi_bg,roi_fg)

            # place the joined image, saved to dst back over the original image
            roi_color[y1:y2, x1:x2] = dst
    #break
    #Display the resulting frame
    cv2.imshow('Video', frame)
    cv2.waitKey()
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

rawDescription: OpenCV with Python - Trying to overlay an image over the eyes on a webcam feed


code:
from pylab import cm
print cm.datad.keys()

rawDescription: How can I use a pre-made color map for my heat map in matplotlib?

description: Use a pre-made color map for my heat map in matplotlib


code:
class SetterProperty(object):
    def __init__(self, func, doc=None):
        self.func = func
        self.__doc__ = doc if doc is not None else func.__doc__
    def __set__(self, obj, value):
        return self.func(obj, value)

class Foo(object):
    @SetterProperty
    def var(self, value):
        print 'Setting var!'
        self.__dict__['var'] = value

rawDescription: Python class @property: use setter but evade getter?


code:
from io import BytesIO
import urllib2 as net
from lxml import etree
import lxml.html

request = net.Request("http://gbgfotboll.se/serier/?scr=table&ftid=57108")
response = net.urlopen(request)
data = response.read()

collected = [] #list-tuple of [(col1, col2...), (col1, col2...)]
dom = lxml.html.parse(BytesIO(data))
#all table rows
xpatheval = etree.XPathDocumentEvaluator(dom)
rows = xpatheval('//div[@id="content-primary"]/table[1]/tbody/tr')

for row in rows:
    collumns = row.findall("td")
    collected.append((
        collumns[0].find("a").text.encode("utf8"), # Lag
        collumns[1].text, # S
        collumns[5].text, # GM-IM
        collumns[7].text, # P - last collumn
    ))

for i in collected: print i

rawDescription: Extracting information from a table on a website using python, LXML & XPATH


code:
for d in data:
    print '%s,%d' % (','.join([str(x) for x in d]), clusters[d[0]])

rawDescription: Python Relating k-means cluster to instance


rawDescription: Subtlety accessing and modifying a multidimensionnal list


code:
import re
new_li=[e for e in li if re.match(r'\d\d\d-\d\d\d-\d\d\d\d', e)]

print new_li
print set(li)-set(new_li)    # show the ones removed...

rawDescription: Removing cells/rows that don't contain regex match with Python


rawDescription: Pythonic way of iterating over 3D array

description: Pythonic way of iterating over 3D array


code:
import sys
import time

print "begin"
sys.stdout.flush()

time.sleep(10)

print "end"
sys.stdout.flush()

rawDescription: python stdout to file does not respond in real time

description: Python stdout to file does not respond in real time


code:
PATH = os.path.join(os.path.expanduser('~/'), 'experiments', 'testfile')

class EventHandler(pyinotify.ProcessEvent):
    def __init__(self, *args, **kwargs):
        super(EventHandler, self).__init__(*args, **kwargs)
        self.file = open(PATH)
        self.position = 0
        self.print_lines()

    def process_IN_MODIFY(self, event):
        print 'event received'
        self.print_lines()

    def print_lines(self):
        new_lines = self.file.read()
        last_n = new_lines.rfind('\n')
        if last_n >= 0:
            self.position += last_n + 1
            print new_lines[:last_n]
        else:
            print 'no line'
        self.file.seek(self.position)

wm = pyinotify.WatchManager()
handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wm.add_watch(PATH, pyinotify.IN_MODIFY, rec=True)
notifier.loop()

rawDescription: Read new line with pynotify

description: Read new line with pynotify


code:
if True:
    pass
    print "hello"

rawDescription: Python ignore \x00\x00\x00\x00

description: Python ignore \x00\x00\x00\x00


code:
class Node(object):
    def __init__(self, tag, parent=None, children=None):
        self.tag = tag
        self.parent = parent
        self.children = [] if not children else children
        self.is_terminal = False if isinstance(self.children, list) else True

    def add_child(self, node):
        if not isinstance(node, Node):
            raise ValueError("Cannot append node of type: [%s]" % type(node))
        if self.is_terminal:
            raise ValueError("Cannot append node to terminal")
        else:
            self.children.append(node)

def is_terminal(e):
    return len(e) == 2 and type(e[0]) == str and type(e[1]) == str

def from_list(lst, root):
    lst = list(lst)
    if not lst:
        return
    for e in lst:
        if is_terminal(e):
            tag, children = e
            print "terminal", tag, "with root", root.tag
            root.add_child(Node(tag=tag, children=children, parent=root))
        else:
            e = list(e)
            tag, children = e.pop(0), e
            print "non terminal", tag, "with root", root.tag
            newroot = Node(tag=tag, parent=root)
            root.add_child(newroot)
            from_list(children, root=newroot)

rawDescription: defining multi branching nested tree in python recursively

description: Defining multi branching nested tree in python recursively


code:
import collections

#tuples of (length, position)
rods = [(18, 21), (17, 2), (15, 3), (14, 21), (14, 5), (13, 6), (13, 7),
        (13, 8), (13, 9), (13, 10), (13, 11), (13, 12), (13, 13), (13, 14),
        (13, 15), (13, 16), (13, 17), (13, 18), (13, 19), (13, 20), (13, 21),
        (13, 22), (13, 23), (13, 24), (13, 25), (13, 26), (12, 5), (12, 21),
        (12, 2)]

lengths = (length for length, position in rods)
length_freq = collections.Counter(lengths)
((best_length, _),) = length_freq.most_common(1)
print best_length

#cumulative frequency of rods near best_length, with unique position:
rod_filter = ((l, p) for l, p in rods if best_length - 1 <= l <= best_length + 1)
tally = len(set((best_length, p) for l, p in rod_filter))

print length_freq
print tally

rawDescription: Total number of similar items which meet uniqueness criteria


code:
from collections import Counter
counter_pdf = Counter(pdf_list)
counter_doc = Counter(pdf_doc)

print counter_pdf  # Counter({'ShandeepResumeAm.doc': 2, 'ShandeepResumeAm.pdf': 1, ....})

rawDescription: I want all PDF or DOCs to come first in the output


code:
from subprocess import Popen, STDOUT, PIPE
from time import sleep

handle = Popen('netsh wlan connect _Wifi_name_', shell=True, stdout=PIPE, stderr=STDOUT, stdin=PIPE)

sleep(5) # wait for the password prompt to occur (if there is one, i'm on Linux and sudo will always ask me for a password so i'm just assuming windows isn't retarded).
handle.stdint.write('mySecretP@ssW0rd\n')
while handle.poll() == None:
    print handle.stdout.readline().strip()

rawDescription: Connection to wi-fi using python

description: Connection to wi-fi using python


code:
import urllib
print urllib.quote(s.decode('utf8').encode('cp1251'))

rawDescription: Urlencode cyrillic characters in Python

description: Urlencode cyrillic characters in Python


code: print columns[0][0]  # first item of first list

rawDescription: how to show each element of array separately

description: Show each element of array separately


code:
s = 'AAABBBBCDDDDDDDDDDEEDDDD'

encoded = ''.join(encode(s))
print encoded

decoded = ''.join(decode(encoded))
print decoded

rawDescription: python encoder and decoder

description: Python encoder and decoder


code:
def func2():
    global var1
    print var1

    var1 = "World"

def func1():
    func2()

def main():
    global var1
    var1 = "Hello"
    func1()

    print var1

rawDescription: Access to a variable defined in main

description: Access to a variable defined in main


code:
nodes =    [{'child': 'x', 'parent': '', 'key': '', 'title': 'w'},
 {'child': 'y', 'parent': 'w', 'key': '', 'title': 'x'},
 {'child': 'z', 'parent': 'x', 'key': '', 'title': 'y'},
 {'child': '', 'parent': 'y', 'key': '', 'title': 'z'},
 {'child': '', 'parent': 'z', 'key': '1', 'title': 'a'},
 {'child': '', 'parent': 'z', 'key': '2', 'title': 'b'},
 {'child': '', 'parent': 'z', 'key': '3', 'title': 'c'},
 {'child': '', 'parent': 'z', 'key': '4', 'title': 'd'}]

treeData = []

def insert_in_tree(node, parent, tree=None):
    if tree == None:
        tree = treeData

    for subnode in tree:
        if not 'children' in subnode:
            subnode['children'] = []
        elif insert_in_tree(node, parent, subnode['children']):
            return True

        if subnode['title'] == parent:
            subnode['children'].append(node)
            return True
    return False

for node in nodes:
    parent = node['parent']
    del node['parent']
    del node['child']
    if parent == '':
        treeData.append(node)
    else:
        result = insert_in_tree(node, parent)
        if not result:
            insert_in_tree(node, parent, nodes)

import json
print json.dumps(treeData, indent=4)

rawDescription: Python tree structure dictonary creation issue


code:
array = [[1230768000000, 23], [1244073599000, 5], [1269206974000, 8], [1291908086000, 23]]

month_aggregate = dict()
for [d,v] in array:
    truncated = int(str(d)[:-3])
    year_month = datetime.utcfromtimestamp(truncated).date().isoformat()[:-3]
    # If the entry was not present previously create one with the current value v
    if not month_aggregate.has_key(year_month):
        month_aggregate[year_month] = v
    else:
        # Otherwise add the value to the previous entry
        month_aggregate[year_month] += v

# Create a JSON Array from the month_aggregate dictionary
month_aggregate_json_list = [ {'value':v, 'key':k} for k, v in month_aggregate.iteritems() ]
print month_aggregate_json_list

rawDescription: aggregate and sum daily data to month in python

description: Aggregate and sum daily data to month in python


code:
from bs4 import BeautifulSoup

data = """

 cell to remove

            some text

"""

soup = BeautifulSoup(data, 'html.parser')
cell = soup.table.tr.td
cell.string = ''
cell.attrs = {}

print soup.prettify(formatter='html')

rawDescription: Remove text from first cell HTML using python


code:
import binascii
# Global variables
state = [None] * 256
p = q = None

def setKey(key):
    ##RC4 Key Scheduling Algorithm (KSA)
    global p, q, state
    state = [n for n in range(256)]
    p = q = j = 0
    for i in range(256):
        if len(key) > 0:
            j = (j + state[i] + key[i % len(key)]) % 256
        else:
            j = (j + state[i]) % 256
        state[i], state[j] = state[j], state[i]

def byteGenerator():
    ##RC4 Pseudo-Random Generation Algorithm (PRGA)
    global p, q, state
    p = (p + 1) % 256
    q = (q + state[p]) % 256
    state[p], state[q] = state[q], state[p]
    return state[(state[p] + state[q]) % 256]

def encrypt(key, plaintext):
    ##Encrypt input string returning a byte list
    pt = string_to_list(plaintext)
    ct = rc4(key, pt)
    return list_to_string(ct, hex=True)

def decrypt(key, ciphertext):
    ##Decrypt input byte list returning a string
    ct = string_to_list(ciphertext, hex=True)
    pt = rc4(key, ct)
    return list_to_string(pt, hex=False)

def string_to_list(input_srt, hex=False):
    ##Convert a string into an int list
    if hex:
        res = [ord(ch) for ch in binascii.unhexlify(input_srt)]
    else:
        res = [ord(ch) for ch in input_srt]
    return res

def list_to_string(lst, hex=True):
    ##Convert an int list into a string
    if hex:
        res = ''.join(["%0.2X" % el for el in lst])
    else:
        res = ''.join([chr(el) for el in lst])
    return res

def rc4(key, ints):
    """Xor list of ints with output generated by RC4. Output list of ints"""
    setKey(string_to_list(key))
    return [x ^ byteGenerator() for x in ints]

# key = raw_input("Enter Key: ")
# ciphertext = raw_input("enter ciphertext: ")
key = 'secret'
plaintext = 'plain'

ciphertext = encrypt(key, plaintext)
print ciphertext
print decrypt(key, ciphertext)

rawDescription: RC4 decryption in python hexadecimal inputs


code:
today = datetime.date.today()
lastweek = today - datetime.timedelta(days=7)
for i in range(7):
    aday = lastweek + datetime.timedelta(days=i)
    first = datetime.datetime.combine(aday, datetime.time.min)
    last = datetime.datetime.combine(aday, datetime.time.max)
    print first, last

rawDescription: How to get the first and last second in python using datetime module

description: Get the first and last second in python using datetime module


code:
def callback(*args):
    if val.get() == target:
        print 'it worked!'

rawDescription: Python 3 tkinter: check when an entry has the value of a string


code:
import textwrap
argString="""
    line1
    line2
    line3
"""
print textwrap.dedent(argString)

rawDescription: How to remove white space characters from string in Python

description: Remove white space characters from string in Python


code:
import mechanize
import urllib

post_url = 'http://as0.mta.info/mnr/fares/get_fares.cfm'

orig = 295 #BEACON FALLS
dest = 292 #ANSONIA

params = urllib.urlencode({'dest_stat':dest, 'orig_stat':orig })
rq = mechanize.Request(post_url, params)

fares_page = mechanize.urlopen(rq)

print fares_page.read()

rawDescription: Python mechanize javascript

description: Python mechanize javascript


code:
l = [1,2,3,4]
print 1 in l # prints True
print 5 in l # prints False

rawDescription: Iterating dictionary in the order I added items, python


code:
import re
s = """
vk.com/idefiks
vk.com/id211452033
vk.com/id211452033
vk.com/id165402000
vk.com/id_lizalizaelizaveta
"""
p = re.compile('/id\d+')
print p.findall(s)

rawDescription: Find substrings in string using python

description: Find substrings in string using python


code:
sum = 0

for i in range(5):
  sum = sum + i

print sum

rawDescription: Advice with python program's for loops in order to calculate 103 Fibonacci element


code:
listA = (
    ('chr1', 'aatt'),
    ('chr8', 'tagg'),
    ('chr11', 'aaaa'),
    ('chr7', 'gtag'),
)

listB = (
    ('chr8', 'tagt'),
    ('chr1', 'tttt'),
    ('chr7', 'gtag'),
    ('chr11','aaaa'),
    ('chr9', 'atat'),
)

combined = set(listA).intersection(set(listB))
for c, d in combined:
    print c, d

rawDescription: How to intersect two horizontal lists using python?

description: Intersect two horizontal lists using python


code:
import difflib

datab = ['Pnk Flooyd', 'John Marvulli',
         'Ld Zeppelin', 'John Michael Marvulli',
         'Led Zepelin', 'Beetles', 'Pink Fl',
         'Beatlez', 'Beatles', 'Poonk LLoyds',
         'Pook Loyds']
print datab
print

li = []
s = difflib.SequenceMatcher()

def yield_ratios(s,iterable):
    for x in iterable:
        s.set_seq1(x)
        yield s.ratio()

for text_item in datab:
    s.set_seq2(text_item)
    for gathered in li:
        if any(r>0.45 for r in yield_ratios(s,gathered)):
            gathered.append(text_item)
            break
    else:
        li.append([text_item])

for el in li:
    print el

rawDescription: Python "in" Comparison of Strings of Different Word Length


rawDescription: Using multiple lists with formulas

description: Using multiple lists with formulas


code: print theCPPlibrary.cvar.CONST2

rawDescription: Swig-generated Constants in Python

description: Swig-generated Constants in Python


code:
from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def index():
  return render_template('template.html')

@app.route('/my-link/')
def my_link():
  print 'I got clicked!'

  return 'Click.'

if __name__ == '__main__':
  app.run(debug=True)

rawDescription: Call a python function within a html file


code:
@csrf_exempt
def list(request):
    # Handle file upload
    if request.method == 'POST':
        print "upload file----------------------------------------------"
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            print "otra vez.. es valido"
            print request.FILES

            newdoc = Jobpart(
                                partfile = request.FILES['docfile']
            )
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('dataports.views.list'))
    else:
        #print "nooooupload file----------------------------------------------"
        form = DocumentForm() # A empty, unbound form

    # Render list page with the documents and the form
    return render_to_response(
        'data_templates/list.html',
        {'form': form},
        context_instance=RequestContext(request)
    )

rawDescription: Uploading files from a Python client to a Django Server