code:
import schedule
import time
from functools import partial
# your specified list of times
times = [1.76493425, 3.10174059, 4.49576803, 10.99379224, 18.84178369]
# define a job you want to do
def job(t, start):
    print ('hello world', ' - timing: ', t, ' - now: ', time.time()- start)
    # pop the job right away from schedule.jobs, so it runs only once
    return schedule.CancelJob
# get the starting time
start = time.time()
# for each time add what to do
for t in times:
    # using partial so i can pass arguments to job
    schedule.every(t).seconds.do(partial(job, t, start))
# and run it inside a lop
while True:
    schedule.run_pending()
    # schedule.jobs is just a list of jobs
    if not schedule.jobs:
        break

rawDescription: scheduling a print command at specific timings after the script started in Python

description: Scheduling a print command at specific timings after the script started in Python


code:
def RandomSub():
    rndstring = "something"
    return rndstring

def Button1_Click(param1, param2):
    rndstring = RandomSub()
    # use rndstring here

rawDescription: Subs from VB.Net in Python

description: Subs from VB.Net in Python


code:
def custom_agg(s, default=np.sum, other={}):
    if s.name in other.keys():
        return other[s.name](s)
    else:
        return default(s)

rawDescription: Python pandas to partially collapse 2d matrix


code:
def myadd(x, y, z):
    return x+y+z

funcs = {}

funcs["add"] = {
    "func": myadd,
    "args": (3, 4)
}

func = funcs["add"]["func"]
args = funcs["add"]["args"]
result = func(*args, z=2)

print(result)  #=> 9

rawDescription: Function in Class __init__ Dict | Functions Evaluated on Call of Class


code:
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()

    if event.type ==  pygame.KEYDOWN:
        if event.key == pygame.K_UP:
            if index==25:
                index=25
            else:
                index=index+1

            nameString=ABC[index]
        elif event.key == pygame.K_DOWN:
            if index==0:
                index=0
            else:
                index=index-1
            nameString=ABC[index]
        elif event.key == pygame.K_RETURN:
            nameStringFinal=nameStringFinal+nameString

screen.blit(fondo,(0,0))
show=fuente.render(nombreString,0,(0,0,0))
screen.blit(despliegue,(200,200))

rawDescription: Manage keydown in pygame

description: Manage keydown in pygame


code: CRISPY_TEMPLATE_PACK = 'bootstrap3'

rawDescription: how to use bootstrap for the field that's being displayed with {{ form|crispy }} for django

description: Use bootstrap for the field that's being displayed with {{ form|crispy }} for django


code:
df = df.set_index('session')
df.sum(axis=1)

rawDescription: Count binary ones and find intersections in Pandas DataFrame


code:
def fizz_count(x):
    return x.count('fizz')

rawDescription: Counting a specific string in the list in Python


code:
import collections
mydict = collections.defaultdict(list)

rawDescription: Most efficient way to add new keys or append to old keys in a dictionary during iteration in Python?


code:
@register.filter
def is_favourite_of(object, user):
    return Favourite.objects.is_favourite(user, object)

rawDescription: How to create a specific if condition templatetag with Django?

description: Create a specific if condition templatetag with Django


rawDescription: Create a Pandas dataframe with counts of items spanning a date range


code: print( pd.Timedelta('1h30min') )

rawDescription: convert "1h30min" string to minutes in python

description: Convert "1h30min" string to minutes in python


code:
import csv

with open('fomc.csv') as infile:
    data= []
    for row in csv.reader(infile):
        data.append([float(field.strip()) if field.strip() else None for field in row])

rawDescription: Most intelligent way of reading some data into Python


code:
b = np.where(a < 0.,'LOW','HIGH').astype('S7')

c = np.where((-1. < a) & (a < 1.), 'MIDDLE',b)

rawDescription: How to update a 2d numpy array?

description: Update a 2d numpy array


code:
def make_sleep():
    async def sleep(delay, result=None, *, loop=None):
        coro = asyncio.sleep(delay, result=result, loop=loop)
        task = asyncio.ensure_future(coro)
        sleep.tasks.add(task)
        try:
            return await task
        except asyncio.CancelledError:
            return result
        finally:
            sleep.tasks.remove(task)

    sleep.tasks = set()
    sleep.cancel_all = lambda: sum(task.cancel() for task in sleep.tasks)
    return sleep

rawDescription: Interrupt all asyncio.sleep currently executing


code:
labels = ["brown", "black", "blue", "brown", "brown", "black"]
label_to_number = defaultdict(partial(next, count(1)))
[(label_to_number[label], label) for label in labels]

rawDescription: Python: Enumerate a list of string 'keys' into ints


rawDescription: Converting List to Dict

description: Converting List to Dict


code: cards = {card['name']: Card(**card) for card in card_list}

rawDescription: Creating multiple class instances from a list of names in Python?


code:
import random
import string
from Tkinter import *

root = Tk()

def addCheckBox():
    checkBoxName = "".join(random.choice(string.letters) for _ in range(10))
    c = Checkbutton(root, text=checkBoxName)
    c.pack()

b = Button(root, text="Add a checkbox", command=addCheckBox)
b.pack()

premadeList = ["foo", "bar", "baz"]

for checkBoxName in premadeList:
    c = Checkbutton(root, text=checkBoxName)
    c.pack()

root.mainloop()

rawDescription: Dynamically add checkboxes with tkinter in python 2.7


code:
class timeval(Structure):
    _fields_ = [("tv_sec", c_long), ("tv_usec", c_long)]

class S(Structure):
    _fields_ = [("x",c_int), ("t", timeval)]

rawDescription: Using struct timeval in Python

description: Using struct timeval in Python


code:
class Base (object):
    def get_message (self):
        try:
            return self.get_message_impl ()
        except Exception as detail:
            print ("error:", detail)
        return None

    def get_message_impl (self):
        raise Exception ("Not Implemented")

rawDescription: How can I wrap an empty function to be implemented by a child class in a try-except block?

description: Wrap an empty function to be implemented by a child class in a try-except block


rawDescription: Regular Expressions working differently in Python and Ruby


rawDescription: How to construct regex for this text

description: Construct regex for this text


code:
import os
import redis

def get_redis_client():
    # Read settings from environment variables
    redis_db_name = os.environ.get('REDIS_NAME')
    redis_host = os.environ.get('REDIS_HOST')
    redis_port = os.environ['REDIS_PORT']

    # create a redis connection
    redis_client = redis.StrictRedis(
        host=redis_host,
        port=redis_port,
        db=redis_db_name,
    )

    # return newly created redis connection
    return redis_client

redis_client = get_redis_client()

SOME_SETTING_STORED_IN_REDIS = redis_client.get('some_setting_stored_in_redis')

rawDescription: Sharing objects between view and application with Pyramid


code:
@contextlib.contextmanager
def container(self, name=None):
    self.AddWidget(name=name, type='container', push=True)
    yield
    self.PopParentWidget()

rawDescription: What's a good way to express hierarchical grouping of widgets in Python?


code:
10% done
21% done
31% done
42% done
52% done
63% done
73% done
84% done
94% done
100% done
b114ecaab65bc5b02f5a129bd29d1864

rawDescription: Getting percentage complete of an md5 checksum


code:
theta = revolve_steps
#convert rho to a column vector
rho_column = rho.reshape(600,1)
x = rho_column.dot(np.cos(theta))
y = rho_column.dot(np.sin(theta))
# expand z into a 2d array that matches dimensions of x and y arrays..
# i used np.meshgrid
zs, rs = np.meshgrid(z, rho)

#plotting
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
fig.tight_layout(pad = 0.0)
#transpose zs or you get a helix not a revolve.
# you could add rstride = int or cstride = int kwargs to control the mesh density
ax.plot_surface(x, y, zs.T, color = 'white', shade = False)
#view orientation
ax.elev = 30 #30 degrees for a typical isometric view
ax.azim = 30
#turn off the axes to closely mimic picture in original question
ax.set_axis_off()
plt.show()

#ps 600x600x600 pts takes a bit of time to render

rawDescription: Python: How to revolve a surface around z axis and make a 3d plot?


code:
for level in levels.values():
    newLevels = loadLinkedLevels(level, p.screen, levels.keys())

levels.update(newLevels)

rawDescription: How am I to get all the connections?

description: How am I to get all the connections


code:
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("http://racing4everyone.eu/2015/10/25/formula-e-201516formula-e-201516-round01-china-race/")

# wait for the page to load, switch to iframe
wait = WebDriverWait(driver, 10)
frame = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "iframe[src*=video]")))
driver.switch_to.frame(frame)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".controls")))

# get the page source
page_source = driver.page_source

driver.close()

# parse the HTML
soup = BeautifulSoup(page_source, "html.parser")
script = soup.find_all("script")
print(script)

rawDescription: Use BeautifulSoup to obtain "View Element" code instead of "View Source" code


code:
def create_attribute(name, value):
    attr = Attribute()
    attr.name = name
    attr.value = value
    return attr

return [create_attribute(attr['internalname'], attr['value'])
        for attr in attributes if attr['internalname'] in attributenames]

rawDescription: Pythonic way using list comprehension

description: Pythonic way using list comprehension


code: my_ordered_dict = OrderedDict({key1:value1, key2:value2,})

rawDescription: Python: Writing an empty dictionary with strings thats ordered


code:
fruits = ['apple#1', 'apple#2', 'apple#3', 'apple#4']
[(f1, f2) for f1 in fruits for f2 in fruits if f1 != f2]

rawDescription: String loops in Python

description: String loops in Python


code:
z
wanted
wanted_arr = np.vstack((map(np.array,wanted)))
wanted_arr
z[((wanted_arr[:,None] == z).all(2)).any(0)]

rawDescription: Find rows of numpy array that are members of a set


code:
posts = Post.query.paginate(page, 3, False)
return render_template('index.html', posts=posts)

rawDescription: Pagination in Flask does not work

description: Pagination in Flask does not work


rawDescription: What is the purpose of __name__?

description: What is the purpose of __name__


code:
clnt.py
import socket
import string

address = ('localhost',22085)
mySocket = socket.socket()

mySocket.connect(address)
print("Connected successfully!")
mySocket.send('blah'.encode())
print(mySocket.recv(1024))

rawDescription: python multithreaded server

description: Python multithreaded server


code: n ^ sum(2**i for i in range(0, len(bin(n))-2, 2))

rawDescription: How to invert alternate bits of a number

description: Invert alternate bits of a number


code:
op = random.choice(ops.keys())
op

rawDescription: python random arithmetic game

description: Python random arithmetic game


rawDescription: In Python, how can I check if 2 numbers in a list are within a certain percentage of each other?


code: ['D', 'B', 'banana', 'orange', 'B', 'A', 'apple', 'pear']

rawDescription: How do I shuffle a list with constraints in Python where some elements cannot appear after another element

description: Shuffle a list with constraints in Python where some elements cannot appear after another element


code: mystring = re.sub(r'(\d) ', r'\1,',mystring)

rawDescription: Replacing spaces after any digit using re.sub


code:
df
modes = df.groupby('colB')['colA'].transform(lambda x: x.mode().iloc[0])
modes

rawDescription: backfilling data from one column into another

description: Backfilling data from one column into another


code:
import xml.etree.ElementTree as ET

path = 'in.xml'

tree = ET.parse(path)
root = tree.getroot()
prev = None

def elements_equal(e1, e2):
    if type(e1) != type(e2):
        return False
    if e1.tag != e1.tag: return False
    if e1.text != e2.text: return False
    if e1.tail != e2.tail: return False
    if e1.attrib != e2.attrib: return False
    if len(e1) != len(e2): return False
    return all([elements_equal(c1, c2) for c1, c2 in zip(e1, e2)])

for page in root:                     # iterate over pages
    elems_to_remove = []
    for elem in page:
        if elements_equal(elem, prev):
            print("found duplicate: %s" % elem.text)   # equal function works well
            elems_to_remove.append(elem)
            continue
        prev = elem
    for elem_to_remove in elems_to_remove:
        page.remove(elem_to_remove)
# [...]
tree.write("out.xml")

rawDescription: Python remove duplicate elements from xml tree


rawDescription: Output BST in Nested Form

description: Output BST in Nested Form


code: #! python2.7

rawDescription: Add python27 to windows path while using python 3.3


code:
import datetime as DT
t1 = DT.datetime.strptime('00:05:36.0100000', '%H:%M:%S.%f0')
t2 = DT.datetime(1900,1,1)

print((t1-t2).total_seconds() / 60.0)

rawDescription: How to convert time object into total minutes python

description: Convert time object into total minutes python


code:
codes, uniques = pd.factorize(pd.concat([df1['item'], df2['item']]))
df1['item'] = codes[:len(df1)] + 1
df2['item'] = codes[len(df1):] + 1

rawDescription: How to factorize two data frame meanwhile with python-pandas?

description: Factorize two data frame meanwhile with python-pandas


code:
from itertools import groupby
d = [
    {"players": [{"id": 179, "name": "Santosh"}], "coach": "Xavi Alonso"},
    {"players": [{"id": 180, "name": "Hari"}], "coach": "Xavi Alonso"},
    {"players": [{"id": 175, "name": "Hitman"}], "coach": "Anna Puyol"}
]
sorted_d = sorted(d, key=lambda _: _['coach'])
for g, data in groupby(sorted_d, key=lambda _: _['coach']):
    print({"coach": g, "players": [player for item in data for player in item["players"]]})

rawDescription: Merge duplicates list of dictionaries item value


code:
vowels = {'a','e','i','o','u'}
create_nick = lambda name: name[0] + ''.join(l for l in name[1:] if l not in vowels)[:7]
if not self.nickname:
    self.nickname = create_nick(self.name) if len(self.name)>8 else self.name
...

rawDescription: Best practice: prepopulate SlugField in Django (without Admin module)


code: np.allclose([np.nan], [np.nan])

rawDescription: Test if two numpy arrays are (close to) equal, including shape


rawDescription: How do I do multiple joins and aggregation with django ORM?

description: Do multiple joins and aggregation with django ORM


rawDescription: floating number string formatting in python

description: Floating number string formatting in python


code: result = string2 in string1

rawDescription: find if string1 is contained in string2 in python

description: Find if string1 is contained in string2 in python


code: C = [a - b for a, b in zip(A, B)]

rawDescription: Subtract values in one list from corresponding values in another list - Python


code:
# assume that you got that text inside a variable
scraped = """
callCount=1
page=/job.do?uno=auo&internal=1
httpSessionId=724D68422B64D786F28E57EB3EE9D07D.newweb04
scriptSessionId=D0E5AF56ACF10360C6B960329CBB012B883
c0-scriptName=normalAjaxService
c0-methodName=getJobDetailByCustId
c0-id=0
c0-param0=number:1000000157
c0-param1=number:551740849947131
batchId=2
"""
param_list = [line.split('=', 1) for line in scraped.split('\n')]
formdata = {p[0]:p[1] for p in param_list}

rawDescription: Scrapy Post Request with payload

description: Scrapy Post Request with payload


code:
import sys
print(sys.argv)

rawDescription: How to pass a variable to magic ´run´ function in IPython

description: Pass a variable to magic ´run´ function in IPython


code:
from django.core.exceptions import ValidationError
from django.db import models

BAD_WORDS = ['abc', 'def']

class TitleMixin(models.Model):
    title = models.CharField(max_length=256)

    class Meta:
        abstract = True

    def clean(self, *args, **kwargs):
        super(TitleMixin, self).clean(*args, **kwargs)
        if any(w in self.title for w in BAD_WORDS):
            raise ValidationError('Bad Title')

rawDescription: Alternatives to creating and iterating through a list for "bad" values each time clean is called in django?


code:
def make_login_key():
    counter = count()
    current_key = [None]
    def key(line):
        if line.startswith('login'):
            current_key[0] = next(counter)
        return current_key[0]
    return key

rawDescription: Split multiple nested key,values and extract to text file


code:
def pipeinto(data, *prog):
    import subprocess
    sp = subprocess.Popen(prog, stdin=subprocess.PIPE)
    sp.stdin.write(str(data))
    sp.stdin.close()
    return sp

rawDescription: Creating a pipe from the inside of the python interpreter


code:
src = [
    {'a': 1, 'b': 2},
    {'a': 3}
]
get_all_values(src)
get_values(src, 'a')

rawDescription: generate list using a particular key from a list of objects

description: Generate list using a particular key from a list of objects


code:
class Item01Object(object):
    properties = ["a","c"]
    ATTR = None
def SetProperties( ItemObject ):
    for propName, cls in AttrContainer.__dict__.iteritems():
        if propName in ItemObject.properties:
            setattr(ItemObject, propName, cls.__dict__[propName])
    return ItemObject()

item = SetProperties(Item01Object)
item
item.a
item.a = 20
item.a

rawDescription: Python: Copy properties with it's functions (fget, fset, fdel) from one class to another


rawDescription: return an unpacked list in python

description: Return an unpacked list in python


code: import pkg1.util as util

rawDescription: Basic Python imports question

description: Basic Python imports question


code:
# social authentication pipeline
SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.user.get_username',
    'social.pipeline.user.create_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details',
# custom pipeline
    'user_profile.pipeline.create_user_profile'
)

rawDescription: Django: How to identifying through which link user done social authentication?


rawDescription: variable +=% possible or how to get the next interval of a number after another number

description: Variable +=% possible or how to get the next interval of a number after another number


code:
def two_lines(x, a, b, c, d):
    out = np.empty_like(x)
    mask = x < 10
    out[mask] = a*x[mask] + b
    out[~mask] = c*x[~mask] + d
    return out

rawDescription: How to apply piecewise linear fit for a line with both positive and negative slopes in Python?

description: Apply piecewise linear fit for a line with both positive and negative slopes in Python


code:
import numpy as np
import aplpy

def get_contour_verts(cn):
    contours = []
    # for each contour line
    for cc in cn.collections:
        paths = []
        # for each separate section of the contour line
        for pp in cc.get_paths():
            xy = []
            # for each segment of that section
            for vv in pp.iter_segments():
                xy.append(vv[0])
            paths.append(np.vstack(xy))
        contours.append(paths)

    return contours

# This line is copied from the question's code, and assumes that has been run previously
CS = plt.contour(Xgrid, Ygrid, Hsmooth,levels=loglvl,extent=extent,norm=LogNorm())

contours = get_contour_verts(CS) # use the linked code

gc = aplpy.FITSFigure('2MASS.fits',figsize=(10,9))

# each level comes with a different set of vertices, so you have to loop over them
for contours_at_level in Contour_arrays:
    clines = [cl.T for cl in contours_at_level]
    img.show_lines(clines,color='red', zorder=100)

rawDescription: How could I convert a contour plot (matplotlib) to FITS format with header?


rawDescription: Python 3, is using sys.stdout.buffer.write() good style?


code:
parser = argparse.ArgumentParser()
parser.add_argument('--resize', nargs='?', const=True)

rawDescription: Is there a way to create argument in python's argparse that returns true in case no values given


code:
from django.db import models
from django.forms import ModelForm
from django import forms

class Answer(models.Model):
    answer = models.CharField(max_length=100)

    def __unicode__(self):
        return self.answer

class Question(models.Model):
    title = models.CharField(max_length=100)
    answers = models.ManyToManyField('Answer')

class QuestionForm(ModelForm):
    class Meta:
        model = Question
        fields = ('title', 'answers')
        widgets = {
            'answers': forms.RadioSelect(),
        }

rawDescription: how to create a dynamically-created radio buttons form in django

description: Create a dynamically-created radio buttons form in django


code:
import codecs

codec_name = 'cp437ex'

_table = u'\0\u263a\u263b\u2665\u2666\u2663\u2660\u2022\u25d8\u25cb\u25d9\u2642\u2640\u266a\u266b\u263c\u25ba\u25c4\u2195\u203c\xb6\xa7\u25ac\u21a8\u2191\u2193\u2192\u2190\u221f\u2194\u25b2\u25bc !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u2302\xc7\xfc\xe9\xe2\xe4\xe0\xe5\xe7\xea\xeb\xe8\xef\xee\xec\xc4\xc5\xc9\xe6\xc6\xf4\xf6\xf2\xfb\xf9\xff\xd6\xdc\xa2\xa3\xa5\u20a7\u0192\xe1\xed\xf3\xfa\xf1\xd1\xaa\xba\xbf\u2310\xac\xbd\xbc\xa1\xab\xbb\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556\u2555\u2563\u2551\u2557\u255d\u255c\u255b\u2510\u2514\u2534\u252c\u251c\u2500\u253c\u255e\u255f\u255a\u2554\u2569\u2566\u2560\u2550\u256c\u2567\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256b\u256a\u2518\u250c\u2588\u2584\u258c\u2590\u2580\u03b1\xdf\u0393\u03c0\u03a3\u03c3\xb5\u03c4\u03a6\u0398\u03a9\u03b4\u221e\u03c6\u03b5\u2229\u2261\xb1\u2265\u2264\u2320\u2321\xf7\u2248\xb0\u2219\xb7\u221a\u207f\xb2\u25a0\xa0'

decoding_map = { i: ord(ch) for i, ch in enumerate(_table) }

encoding_map = codecs.make_encoding_map(decoding_map)

class Codec(codecs.Codec):
    def encode(self, input, errors='strict'):
        return codecs.charmap_encode(input, errors, encoding_map)

    def decode(self, input, errors='strict'):
        return codecs.charmap_decode(input, errors, decoding_map)

class IncrementalEncoder(codecs.IncrementalEncoder):
    def encode(self, input, final=False):
        return codecs.charmap_encode(input, self.errors, encoding_map)[0]

class IncrementalDecoder(codecs.IncrementalDecoder):
    def decode(self, input, final=False):
        return codecs.charmap_decode(input, self.errors, decoding_map)[0]

class StreamReader(Codec, codecs.StreamReader):
    pass

class StreamWriter(Codec, codecs.StreamWriter):
    pass

def _register(encoding):
    if encoding == codec_name:
        return codecs.CodecInfo(
            name=codec_name,
            encode=Codec().encode,
            decode=Codec().decode,
            incrementalencoder=IncrementalEncoder,
            incrementaldecoder=IncrementalDecoder,
            streamreader=StreamReader,
            streamwriter=StreamWriter)

codecs.register(_register)

rawDescription: Selected character maps within utf-8

description: Selected character maps within utf-8


rawDescription: passing value to other module python

description: Passing value to other module python


code:
my_series = pd.Series([10,20,30,40], index=['foo','intercept','baz','intercept'])
# foo          10
# intercept    20
# baz          30
# intercept    40
# dtype: int64

mask = my_series.index.isin(['intercept'])
print(my_series.loc[~mask])

rawDescription: Grab all but one element in pandas series


code:
import re

valid_json = re.sub(r'(?<={|,)([a-zA-Z][a-zA-Z0-9]*)(?=:)', r'"\1"', jsonp_payload)

rawDescription: Python: Bad JSON - Keys are not quoted

description: Python: Bad JSON - Keys are not quoted


code: print ('{0:02d}'.format(a.month))  # python 2.7+ -- '{:02d}' will work

rawDescription: datetime objects format

description: Datetime objects format


rawDescription: Place objects in the middle of a frame in tkinter python


code:
n = len(delta_vec)
jj, kk = np.triu_indices(n, 1)
ww = 1. / np.exp((mjd_list[kk] - mjd_list[jj]) / avg_time)
w_sum = np.sum(ww)
PP = delta_vec[jj] * delta_vec[kk]
j_index = np.sum(ww * np.sign(PP) * np.sqrt(np.abs(PP)))

rawDescription: Nested python loop revisited

description: Nested python loop revisited


code:
BEFORE: ['apple', 'orange']
AFTER: ['apple', 'orange', 'banana']

rawDescription: how to avoid global in python threads when sharing resources?

description: Avoid global in python threads when sharing resources


rawDescription: Printing Simple message in Python

description: Printing Simple message in Python


code:
axis = 1
idx = (slice(None),) * axis + (b,)
a[idx]

rawDescription: Sort N-D numpy array by another 1-D array


code:
def Test():
    main_dir = ["FolderA", "FolderB"]
    common_dir = ["SubFolder1", "SubFolder2", "SubFolder3"]

    for dir1 in main_dir:
        for dir2 in common_dir:
            try: os.makedirs(os.path.join(dir1,dir2))
            except OSError: pass

rawDescription: How to create directories and sub directories efficiently and elegently in Python 2.7?

description: Create directories and sub directories efficiently and elegently in Python 2.7


code:
{'active': True,
 'full_path': '/tmp/x',
 'items': {'var': {'active': True,
                   'full_path': '/tmp/x/var',
                   'items': {'log': {'active': True,
                                     'full_path': '/tmp/x/var/log',
                                     'items': {},
                                     'type': 'd'},
                             'www': {'active': True,
                                     'full_path': '/tmp/x/var/www',
                                     'items': {'index.html': {'active': True,
                                                              'full_path': '/tmp/x/var/www/index.html',
                                                              'type': 'f'}},
                                     'type': 'd'}},
                   'type': 'd'}},
 'type': 'd'}

rawDescription: Building a dictionary from directory structure


code:
current = connections[node1]
while True:
    try:
         current = network[current]
    except KeyError:
         break
connections[node1] = current

rawDescription: Non Sequential Loop Optimization in Python


code:
def nested_property(func):
    """Make defining properties simpler.
    """
    names = func()
    names['doc'] = func.__doc__
    return property(**names)

class Hidex(object):
    def __init__(self, x):
        self.__x = x
    @nested_property
    def x():
        def fget(self):
            return ~self.__x
        def fset(self, x):
            assert isinstance(x, int), 'val must be int'
            self.__x = ~x
        return locals()

rawDescription: if a property sample work

description: If a property sample work


code:
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(imgray,mask = mask)
mean_val = cv2.mean(im,mask = mask)

rawDescription: openCV python, get the region from closed curve

description: OpenCV python, get the region from closed curve


code:
QPUPZVVHUNSN
EFJACZEBYQEB
QBQJJEEOYTZY
EOJUSUEAJEEK
QWRWLIWDTDBD

rawDescription: What's the best way to generate random strings of a specific length in Python?


code:
import scikits.timeseries as ts
import datetime

a = datetime.datetime(2011,8,1)
b = datetime.datetime(2011,8,29)

diff_business_days = ts.Date('B', b) - ts.Date('B', a)
# returns 20

rawDescription: Find day difference between two datetimes (excluding weekend days) in Python?


code:
x="@peter I really love that shirt at #Macy. http://bit.ly//WjdiW4"
' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
x="@shawn Titanic tragedy could have been prevented Economic Times: Telegraph.co.ukTitanic tragedy could have been preve... http://bit.ly/tuN2wx"
' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
x="I am at Starbucks http://4sq.com/samqUI (7419 3rd ave, at 75th, Brooklyn) "
' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())

rawDescription: how to remove hashtag, @user, link of a tweet using regular expression

description: Remove hashtag, @user, link of a tweet using regular expression


code:
import ast

obj = ast.literal_eval(datastring)

rawDescription: Convert string to array of dictionaries

description: Convert string to array of dictionaries


code:
# Validate that the date is in the YYYY-MM-DD format.
import time
try:
  time.strptime(val, '%Y-%m-%d')
  is_in_valid_format= True
except ValueError:
  is_in_valid_format = False

rawDescription: python regular expression to validate types of strings

description: Python regular expression to validate types of strings


code:
driver = webdriver.Firefox()
driver.get("http://www.phila.gov/revenue/RealEstateTax/default.aspx")
assert "Revenue Department" in driver.title

rawDescription: Some nudges for a first-time scraper

description: Some nudges for a first-time scraper


rawDescription: Pull files into script from two directories back


code:
while True:
    foo()
    time.sleep(1800)
    foo_kill()
    bar()
    time.sleep(1800)
    bar_kill()

rawDescription: Python: Alternating functions every x minutes


code:
#! /usr/bin/env python
from __future__ import print_function
import re

def open_func_file(args):
    functionality_file_context = ''
    func_file = ''
    for func_file in args.functionality_file:
        functionality_file_context = open(func_file).readlines()
    return func_file, functionality_file_context

def func_override_parameter_value(
        fline, func_file, function_line, value):
    """Mocking the undefined function."""
    return 42

args = "some_file_name"
x = 'XXX'
y = 'YYY'
value = ''
name = ''
myDict = {
    "Bike": 0,
    "Car": 2,
}
commentregex = re.compile('^[^#]')  # ignore lines that start with comment (#)
func_file, functionality_file_context = open_func_file(args)
for fline in functionality_file_context:
    if fline.strip():  # ignore empty lines or lines with only whitespace
        if (commentregex.match(fline)):
            # replace tabs with whitespace
            replacetab = re.compile(r'\s+')
            fline = replacetab.sub(' ', fline)

            function_line = fline.split(' ', 2)
            if len(function_line) != 3:
                exit("Something wrong with this line: "
                     "" + fline.strip() + "\" in: " + func_file)

            name = x + function_line[0].strip() + y
            value = func_override_parameter_value(
                fline, func_file, function_line, value)
            print(name + str(value))
            myDict[name] = value

rawDescription: Add results from a for loop in a dict, Python


code:
mygrid = [list(decode[i:i+4]) for i in range(0,len(decode),4)]
        # ^ add call to list

rawDescription: cipher exceeds the 4X4 grid and prints out wrong cipher

description: Cipher exceeds the 4X4 grid and prints out wrong cipher


code: [i for i, j  in enumerate(input_string.split()) if j in keywords]

rawDescription: Finding an index of one of alternative keywords


code: cur.mogrify("SELECT * FROM table WHERE column IN %s;", ((1,2,3),))

rawDescription: Passing list of parameters to SQL in psycopg2


code:
result = df.set_index(['Athlete', 'race']).unstack('race')
#           Distance       Race      Rank        Time
# race             1     2    1    2    1   2       1       2
# Athlete
# A.Moyet       400m   NaN    A  NaN    2 NaN   49.00     NaN
# C.Marconi     800m   NaN    B  NaN    5 NaN  104.12     NaN
# M.Smith       400m  800m    A    B    1   3   48.57  102.66

rawDescription: Pivot Pandas Dataframe with a Mix of Numeric and Text Fields


code:
values = [1, 2, 3, 4, 5]
thecsv = csv.writer(open("your.csv", 'wb'))
thecsv.writerow(values)

rawDescription: Convert Python List to Column in CSV

description: Convert Python List to Column in CSV


rawDescription: python regex "SNPEFF_AMINO_ACID_CHANGE=p.Pro3Pro/c.9T>C" extract values for the keys

description: Python regex "SNPEFF_AMINO_ACID_CHANGE=p.Pro3Pro/c.9T>C" extract values for the keys


rawDescription: How do I use a django join to select customer_name for every id that is returned in a result

description: Use a django join to select customer_name for every id that is returned in a result


rawDescription: Python: How to get a value of datetime.today() that is "timezone aware"?


code:
import itertools

for a, b in itertools.permutations(A, 2):
    do_stuff(a, b)

rawDescription: How to do C++ style(indexed) nested loops in python?

description: Do C++ style(indexed) nested loops in python


code:
from PIL import Image
import numpy as np

im = Image.open('py.png')
image_array = np.array(im)

rawDescription: convert image to byte and split into fragments

description: Convert image to byte and split into fragments


code: tree.iter(tag = 'element10')

rawDescription: XML parsing: set great-great-...-grandchild element as a root in Python


code:
[buildout]
develop = src/my.package

rawDescription: How to setup for development of plone products

description: Setup for development of plone products


code:
c.HubFactory.control = 10203,10103
c.HubFactory.task = 10205,10105
c.HubFactory.mux = 10204,10104
c.HubFactory.regport = 10101
c.HubFactory.hb = 10102,10112

rawDescription: How to setup ssh tunnel for ipython cluster (ipcluster)

description: Setup ssh tunnel for ipython cluster (ipcluster)


rawDescription: Python sys.argv[1:] not picking up command line options


code:
def foo(bar):
    global bar # SyntaxError
    bar = bar + 1

rawDescription: Why would people use globals() to define variables


code:
for variable in self.variable_list:
       var_type,var_text_ctrl,var_name = variable
       if var_type == 'f' :
           setattr(self, var_name, var_text_ctrl.GetValue())

rawDescription: Using Eval in Python to create class variables


code:
df['critic_vector'] = df[['minor','major']].abs().idxmax(axis=1)
df['critic'] = abs(df.lookup(df.index, df.critic_vector))
df

rawDescription: Pandas: optimizing some python code by getting rid of DataFrame.apply()


code:
"""
An experimental support for curvilinear grid.
"""
import numpy as np
import  mpl_toolkits.axisartist.angle_helper as angle_helper
import matplotlib.cm as cmap
from matplotlib.projections import PolarAxes
from matplotlib.transforms import Affine2D

from mpl_toolkits.axisartist import SubplotHost

from mpl_toolkits.axisartist import GridHelperCurveLinear

def curvelinear_test2(fig):
    """
    polar projection, but in a rectangular box.
    """
    global ax1

    # see demo_curvelinear_grid.py for details
    tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform()

    extreme_finder = angle_helper.ExtremeFinderCycle(10, 60,
                                                     lon_cycle = 360,
                                                     lat_cycle = None,
                                                     lon_minmax = None,
                                                     lat_minmax = (0, np.inf),
                                                     )

    grid_locator1 = angle_helper.LocatorHMS(12) #changes theta gridline count
    tick_formatter1 = angle_helper.FormatterHMS()

    grid_locator2 = angle_helper.LocatorDMS(6)
    tick_formatter2 = angle_helper.FormatterDMS()

    grid_helper = GridHelperCurveLinear(tr,
                                        extreme_finder=extreme_finder,
                                        grid_locator1=grid_locator1,
                                        tick_formatter1=tick_formatter1,
                                        grid_locator2=grid_locator2,
                                        tick_formatter2=tick_formatter2
                                        )

    ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper)

    # make ticklabels of right and top axis visible.
    ax1.axis["right"].major_ticklabels.set_visible(True)
    ax1.axis["top"].major_ticklabels.set_visible(True)
    ax1.axis["bottom"].major_ticklabels.set_visible(True) #Turn off?
    # let right and bottom axis show ticklabels for 1st coordinate (angle)
    ax1.axis["right"].get_helper().nth_coord_ticks=0
    ax1.axis["bottom"].get_helper().nth_coord_ticks=0

    fig.add_subplot(ax1)

    grid_helper = ax1.get_grid_helper()

    ax1.set_aspect(1.)
    ax1.set_xlim(-4,15) # moves the origin left-right in ax1
    ax1.set_ylim(-3, 20) # moves the origin up-down

    ax1.set_ylabel('90$^\circ$ + Declination')
    ax1.set_xlabel('Ascension')
    ax1.grid(True)
    #ax1.grid(linestyle='--', which='x') # either keyword applies to both
    #ax1.grid(linestyle=':', which='y')  # sets of gridlines

    return tr

import matplotlib.pyplot as plt
fig = plt.figure(1, figsize=(5, 5))
fig.clf()

tr = curvelinear_test2(fig) # tr.transform_point((x, 0)) is always (0,0)
                            # => (theta, r) in but (r, theta) out...
r_test =   [0, 1.2, 2.8, 3.8, 5,  8,  10, 13.3, 17]  # distance from origin
deg_test = [0,  -7, 12,  28,  45, 70, 79, 90,   100] # degrees ascension
out_test = tr.transform(zip(deg_test, r_test))

sizes = [40, 30, 10, 30, 80, 33, 12, 48, 45]
#hues = [.9, .3, .2, .8, .6, .1, .4, .5,.7] # Oddly, floats-to-colormap worked for a while.
hues = np.random.random((9,3)) #RGB values

# Use this block to generate colored points with a colorbar.
cm = plt.cm.get_cmap('RdYlBu_r')
z = np.random.random((len(r_test), 1))  # RGB values

SC = ax1.scatter(out_test[:,0], #ax1 is a global
            out_test[:,1],
            s=sizes,
            c=z,
            cmap=cm,
            zorder=9) #on top of gridlines

# Colorbar
cbar = plt.colorbar(SC, shrink=1., pad=0.05)
cbar.ax.tick_params(labelsize=8)
cbar.set_label('colorbar', fontsize=8)

plt.show()

rawDescription: Produce a RA vs DEC equatorial coordinates plot with python


code:
VALUE, LEFT, RIGHT = 0, 1, 2

def get_deepest_max_sum(node):
    if node is None:
        return 0, 0
    else:
        deepest, most = max(get_deepest_max_sum(node[LEFT]), get_deepest_max_sum(node[RIGHT]))
        return 1+deepest, node[VALUE]+most

tree = [12, [1, None, None], [3, [4, None, None], [2, None, None]]]
print(get_deepest_max_sum(tree))

rawDescription: Finding the longest (deepest) chain in a Tree and summing the TreeNodes?


code: plt.imsave('test.png', data, cmap=cmap)

rawDescription: Saving an imshow-like image while preserving resolution


code: python /complete/path/to/script.py

rawDescription: Package data with relative path

description: Package data with relative path


code:
def find_letter(lst):  # You receive your list as lst
    lst=['o','hello', 1]  # Opppsss! You override it. It does not matter what you receive with lst above, now its value is ['o','hello', 1]
    n='o'

rawDescription: Python - check if a letter is in a list

description: Python - check if a letter is in a list


code:
is_motorcycle = df.Campaign.str.contains('Motorcycle')
df['vehicle'] = pd.Series(["MC"] * len(df)) * is_motorcycle

rawDescription: If value contains string, then set another column value


code:
def F_C():
    x = raw_input("Please Enter A Value")
    return (int(x) - 32)*0.55

rawDescription: Python Temperature Converter

description: Python Temperature Converter


code:
class GUI(pyglet.window.Window):
    def __init__(self):
        super(GUI, self).__init__(640,340, caption='Test')
        ...
        self.house = House()

    def render(self, *args):

        self.house.rotate(1)
        self.house._draw()

rawDescription: Pyglet unstable frame rate

description: Pyglet unstable frame rate


code:
df['ewma'] = pd.ewma(df["avg"], span=60, freq="D")
df

rawDescription: computing an EWMA of a DataFrame by time

description: Computing an EWMA of a DataFrame by time


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

exitCondition = threading.Event()

def inputActivity():
    while True:
        if exitCondition.is_set(): #exitCondition reached -> shutdown
            return
        else: #exitCondition not reached -> do work
            sleep(.1)

inThread = threading.Thread(target = inputActivity)
inThread.start()

sleep(.2) #<-critical

exitCondition.set()
print("exitCondition set")

inThread.join()

description: Threading.Condition.wait(timeout) ignores threading.Condition.notify()


code:
import base64
import requests

url = "https://www.gravatar.com/avatar/0eee0fa5a325a984dab6c1e9c7a1968f?d=retro&s=40"
response = requests.get(url)
content_type = response.headers["content-type"]
encoded_body = base64.encodebytes(response.content)
print("data:{};base64,{}".format(content_type, encoded_body))

rawDescription: Python: Retrieve a file from url and generate data: URI


rawDescription: how do I parse a specific string in python

description: Parse a specific string in python


code:
with open("temp_test.py") as f:
    p = ast.parse(f.read(), filename="<ast>", mode="exec")
Imports().visit(p)

rawDescription: How to textually find an imported name in a module

description: Textually find an imported name in a module


code:
def MakePairs(inputlist):
    return [zip(*item) for item in inputlist]

rawDescription: Speed up a function in Python 2.7

description: Speed up a function in Python 2.7


code:
def get_party_stats(families, table_size=6):
    return (sum([len(f) for f in families]),
            sum([-(-len(f) // table_size) for f in families]))

rawDescription: Some questions on a function

description: Some questions on a function


code:
class C:
    def __getstate__(self):
        raise Exception

pickle.dumps(C())

rawDescription: Create object that cannot be pickled

description: Create object that cannot be pickled


code:
def func(bla, alba):
    pass
func = decorator_func(aaa, bar)(func)

rawDescription: Python decorators theory

description: Python decorators theory


code:
print(result)
# file write hi.txt 9

rawDescription: Python 3 - Replacing part of a string with imformation from the string


rawDescription: Convert n-dimensional numpy array to 2-dimensional index array


code:
import file

file.hello()

rawDescription: How to import all imports of another .py file

description: Import all imports of another .py file


code:
from itertools import dropwhile
def section(fle, start, stop):
    with open(fle) as f:
        # consume lines until we get to our start line
        drop = dropwhile(lambda x: x.strip() != start, f)
        # skip start
        next(drop, "")
        # loop starting at line after start line
        for line in drop:
            # if we reach stop line break
            if line.startswith(stop):
                break
            # else we just yield the line
            yield line.rstrip()

rawDescription: How to start checking from specific part of a list in python

description: Start checking from specific part of a list in python


code:
import random
# Author:
# File name: Lab01.py
# Date: 1/30/2014

# Purpose: To get information from the user and supply other functions with info
# Inputs: None
# Outputs: Bracket display, progress, and instructions
def main():
    word_list = ["max", "cat", "basket", "ship", "earth", "coat", "ocean"]  # creates set of words for game to chose from
    word = random.choice(word_list)  # chooses a random word form the list
    brick_display = ['_'] * len(word)  # Creates the same number of underscores that there are letters in the word
    guesses = []  # tracks the letters the user has used
    count = 0
    wrong = 0
    while True:
        print(' '.join(brick_display))
        print(guesses)
        print(word)
        try:
            guess = input('Please enter a letter >> ')
            if guess not in guesses:
                index = check_guess(guess, word)
                if index >= 0:
                    brick_display[index] = guess
                else:
                    wrong += 1
                    statement_producer(wrong)
            else:
                print('You already guessed that letter')

            if guess == 'exit':
                break
            count += 1
            guesses.append(guess)
            if count >= len(word):
                if check_for_win(word, brick_display):
                    print('You win!!')
                    again = int(input('Play again? 1 for YES, 2 for NO'))
                    word = random.choice(word_list)
                    brick_display = ['_'] * len(word)
                    guesses = []
                    count = 0
                    wrong = 0
                    if again == 1:
                        pass
                    else:
                        break
                elif wrong == 6:
                    print('You lose!!')
                    again = int(input('Play again? 1 for YES, 2 for NO'))
                    if again == 1:
                        pass
                    else:
                        break

        except ValueError:
            print('Please enter a letter')

print("Thanks for playing")

#Purpose: Checks if the guess is in the word
#Inputs: guess, word
#Outputs: If true: index of correct guess else: -1
def check_guess(guess, word):
    for i in range(len(word)):
        if guess == word[i]:
            return i
    return -1

#Purpose: Checks if the user has correctly guessed the word
#Inputs: word, guess
#Outputs:True or False
def check_for_win(word, guess):
    if ''.join(guess) == word:
        return True
    return False

#Purpose: Produces statement based on the number of wrong guesses
#Inputs: wrong
#Outputs: Statement
def statement_producer(wrong):
    if wrong == 1:
        print("draw body part: head")
    elif wrong == 2:
        print("draw body part: body")
    elif wrong == 3:
        print("draw body part: left arm")
    elif wrong == 4:
        print("draw body part: right arm")
    elif wrong == 5:
        print("draw body part: left leg")
    else:
        print("draw body part: right leg. You have lost")

main()

rawDescription: While loop inside while loop not helping. Wont exit


code: return Countries[n]

rawDescription: sorting a list of countries in descending order and returning the nth country

description: Sorting a list of countries in descending order and returning the nth country


code:
c = Counter()
c.foo
c.bar += 1
c.bar += 2
c.bar
l = Counter(list)
l.blub += [1,2,3]
l.blub

rawDescription: Python: Cleaner ways to initialize

description: Python: Cleaner ways to initialize


code:
import dill
dill.check(t.func)

rawDescription: Pathos multiprocessing can't call any package and function in the class


code:
r = console.push('def test2(n):\n  return n+10\n')
r = console.push('test2(10)')
r = console.push('test(10)\ntest(15)')

rawDescription: Make Python's Interactive Interpreter Class Print Evaluated Expressions


code:
def count(M, N):
    count = 0
    for i in range(len(N)-len(M)+1):
        if N[i:i+len(M)] == M:
            count += 1
    return count

rawDescription: Counting substrings in string

description: Counting substrings in string


code: src_dir_1 = os.path.join(src_dir, sdir, album, s + ".mp3")

rawDescription: Need to implement nested try statements with one exception


code:
a
b
np.take(b,a)
b[a.ravel()].reshape(a.shape)

rawDescription: Fast vectorized indexing in numpy

description: Fast vectorized indexing in numpy


code: my_mock(1, 2).return_value = value

rawDescription: How to setup return_value for a function call with specific signature?

description: Setup return_value for a function call with specific signature


code: dsum(a, b) == oddsum + evensum

rawDescription: Efficient way to find sum of digits of an entire sequence


code:
values = [somefunc(i) for i in range(5)]
MAKE_STANDARD_CONNECTION()
SEND_DATA(*values)

rawDescription: Implementing my own higher level test scripting language


rawDescription: Gradient calculation with python

description: Gradient calculation with python


code:
def biggest_even_number(xs):
    l = [i for i in xs if i % 2 == 0]
    return fold(max, float("-inf"), l)

rawDescription: Largest even number in a list using fold


code:
class Foo(object):
    def __init__(self, **kwargs):
            self.features = kwargs

    def getFeatures(self):
            return {feature for feature, isEnabled in self.features.items() if isEnabled}

x = Foo(red=True, yellow=False, green=True)
x.getFeatures()

rawDescription: filling up a list python

description: Filling up a list python


code:
flattened = sum(list1, [])
if len(flattened) > len(set(flattened)):
  print ("dups")
else:
  print ("no dups")

rawDescription: Finding repeated values in multiple lists


code:
b = B()
b.func(1)    # failed
b.func(1,2)

rawDescription: SWIG: Coping with baseclass overloaded methods in a derived class


rawDescription: How to show the whole image when using OpenCV warpPerspective

description: Show the whole image when using OpenCV warpPerspective


code:
d = Etudiant(12456)
d.addCours('csi2520')
d.addCours('csi2772')
d.addCours('csi2510')
d
d.cours('csi2510')
d.cours('csi4900')

rawDescription: Output a dict in python

description: Output a dict in python


rawDescription: Possible to go over nested lists with for loops in a similar way to dicts?


code: ['here', 'go', 'list', 'elements']

rawDescription: Python random sequence with seed

description: Python random sequence with seed


code:
import numpy as np

# in this example `compressed` is a Nx3 array of octets (`uint8`)
expanded = np.empty((compressed.shape[0], 4))
expanded[:,:3] = compressed
expanded[:, 3] = 0
expanded = expanded.view('uint32').reshape(-1)

rawDescription: Custom size array

description: Custom size array


code: { (p.id, ind) : {'id': p.id, 'position': ind} for ind, p in enumerate(p_list)}

rawDescription: How can I get the index value of a list comprehension?

description: Get the index value of a list comprehension


code:
def update(self, instance, validated_data):
    # instance is your Office object
    # You should update the Office fields here
    # instance.field_x = validated_data['field_x']

    # Let's grab the Country object again

    country_iso = validated_data.pop('country')
    country = Country.objects.get(iso=country_iso)

    # Update the Office object
    instance.country = country
    instance.save()

    return instance

rawDescription: Django REST Framework: create and update an object with a nested object value (instead of Primary Key)


code:
import operator
mylist = [[1,2,3],[4,5,6],[7,8,9]]
index = [1,2]
val = reduce(operator.getitem, index, mylist)
print(val)

rawDescription: Using a list as "list of indexes" to address elements of another list


code:
import threading
threading.Thread(target = httpd.shutdown).start()

rawDescription: Shutting down python TCPServer by custom handler


code:
from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
]

rawDescription: django how to include javascript in template

description: Django how to include javascript in template


code:
erik_tweets_before_today = [
    x for x in tweets
    if x['user']['name'] == 'Erik Allik'
    and x['created_at'].date() < datetime.date.today()
]

rawDescription: How to save and handle tweets saved as JSON in Python

description: Save and handle tweets saved as JSON in Python


rawDescription: looping over two fields of a big table

description: Looping over two fields of a big table


rawDescription: Benefits of panda's multiindex?

description: Benefits of panda's multiindex


code:
from itertools import imap
max(imap(len, d))

rawDescription: Find longest (string) key in dictionary

description: Find longest (string) key in dictionary


code:
from Bio.PDB import PDBParser, PDBIO

io = PDBIO()
pdb = PDBParser().get_structure("6CHY", "6CHY.pdb")

for chain in pdb.get_chains():
    io.set_structure(chain)
    io.save(pdb.get_id() + "_" + chain.get_id() + ".pdb")

rawDescription: How to extract all chains from a PDB file?

description: Extract all chains from a PDB file


code:
import unicodedata
print(ascii(unicodedata.normalize('NFD', '\u304c')))

rawDescription: Is there any way to do "logical" transformations between Japanese kana characters with different diacritical marks?


code:
class SymbNode:
    '''Class that records a node's address and symbol.'''

    def __init__(self, addr, symb):
        self.addr = addr
        self.symb = symb

    def __len__(self): # how "high" a node is above the root
        return len(self.addr)

    def _compare(self, other, segment):
        return self.addr == other.addr[:segment]

    def __le__(self, other):
        return self._compare(other, segment=len(self))

    def begets(self, other):
        return self._compare(other, segment=-1)

rawDescription: Composition of a hierarchy of functions

description: Composition of a hierarchy of functions


code:
#input
listOfBubbles = ['45', '5j', ' f', 'e', 's ', ' ', ' ', ' 45', 'q', '']
#Copy input, strip leading / trailing spaces. Remove empty items
stripped = [x.strip() for x in listOfBubbles if x.strip()]

# list(filtered) is ['45', '5j', 'f', 'e', 's', '45', 'q']
out = []
for val in filtered:
  try:
    out.append(int(val))
  except:
    # don't do anything here, but need pass because python expects at least one line
    pass
# out is [45, 45]

rawDescription: I can't find the logic error in this bubble sort code


code:
#instead of full_wordlist = combinations(...)

import re
with open("usr/share/dict/words","r") as f:
    words = (word for word in map(str.rstrip,f.readlines()) if not re.search(r"[^0-9a-fA-F]",word))
    # you can avoid the need for regex by doing:
    # words = (word for word in map(str.rstrip,f.readlines()) if
    #         not any(char not in "0123456789abcdefABCDEF" for char in word))
    collisions = [keypair for keypair in combinations(words,2)
                 if bin(int(keypair[0],base=16)^int(keypair[1],base=16)) == key]

rawDescription: Reducing the complexity of dictionary brute force in python


code:
def has23(nums):
    for i in nums:
        if i == 2 or i == 3:
            return True
    return False

rawDescription: Iteration over a list

description: Iteration over a list


code: d.groupby(level=0).apply(lambda t: stats.mannwhitneyu(t.course1, t.course2))

rawDescription: Mann–Whitney U test on Pandas dataframe

description: Mann–Whitney U test on Pandas dataframe


code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

y = 5
x = 2
# The data of the plot will be added in these lists
x_data_plot=[]
y_data_plot=[]
for N in range(x,x+y,2):
    #Constants and parameters
    epsilon = 0.01
    K00 = np.logspace(0,3,10,10)
    len1 = len(K00)
    y0 = [0]*(3*N/2+3)
    Kplot = np.zeros((len1,1))
    Pplot = np.zeros((len1,1))
    S = [np.zeros((len1,1)) for kkkk in range(N/2+1)]
    KS = [np.zeros((len1,1)) for kkkk in range(N/2)]
    PS = [np.zeros((len1,1)) for kkkk in range(N/2)]
    Splot = [np.zeros((len1,1)) for kkkk in range(N/2+1)]
    KSplot = [np.zeros((len1,1)) for kkkk in range(N/2)]
    PSplot = [np.zeros((len1,1)) for kkkk in range(N/2)]

    for series in range(0,len1):
        K0 = K00[series]
        Q = 10
        r1 = 0.0001
        r2 = 0.001
        a = 0.001
        d = 0.001
        k = 0.999
        S10 = 1e5
        P0 = 1
        tf = 1e10
        time = np.linspace(0,tf,len1)

        #Defining dy/dt's
        def f(y,t):
            for alpha in range(0,(N/2+1)):
                S[alpha] = y[alpha]
            for beta in range((N/2)+1,N+1):
                KS[beta-N/2-1] = y[beta]
            for gamma in range(N+1,3*N/2+1):
                PS[gamma-N-1] = y[gamma]
            K = y[3*N/2+1]
            P = y[3*N/2+2]

            # The model equations
            ydot = np.zeros((3*N/2+3,1))
            B = range((N/2)+1,N+1)
            G = range(N+1,3*N/2+1)
            runsumPS = 0
            runsum1 = 0
            runsumKS = 0
            runsum2 = 0

            for m in range(0,N/2):
                runsumPS = runsumPS + PS[m]
                runsum1 = runsum1 + S[m+1]
                runsumKS = runsumKS + KS[m]
                runsum2 = runsum2 + S[m]
                ydot[B[m]] = a*K*S[m]-(d+k+r1)*KS[m]

            for i in range(0,N/2-1):
                ydot[G[i]] = a*P*S[i+1]-(d+k+r1)*PS[i]

            for p in range(1,N/2):
                ydot[p] = -S[p]*(r1+a*K+a*P)+k*KS[p-1]+d*(PS[p-1]+KS[p])

            ydot[0] = Q-(r1+a*K)*S[0]+d*KS[0]+k*runsumPS
            ydot[N/2] = k*KS[N/2-1]-(r2+a*P)*S[N/2]+d*PS[N/2-1]
            ydot[G[N/2-1]] = a*P*S[N/2]-(d+k+r2)*PS[N/2-1]
            ydot[3*N/2+1] = (d+k+r1)*runsumKS-a*K*runsum2
            ydot[3*N/2+2] = (d+k+r1)*(runsumPS-PS[N/2-1])- \
                            a*P*runsum1+(d+k+r2)*PS[N/2-1]

            ydot_new = []
            for j in range(0,3*N/2+3):
                ydot_new.extend(ydot[j])
            return ydot_new

        # Initial conditions
        y0[0] = S10
        for i in range(1,3*N/2+1):
            y0[i] = 0
        y0[3*N/2+1] = K0
        y0[3*N/2+2] = P0

        # Solve the DEs
        soln = odeint(f,y0,time, mxstep = 5000)
        for alpha in range(0,(N/2+1)):
            S[alpha] = soln[:,alpha]
        for beta in range((N/2)+1,N+1):
            KS[beta-N/2-1] = soln[:,beta]
        for gamma in range(N+1,3*N/2+1):
            PS[gamma-N-1] = soln[:,gamma]

        for alpha in range(0,(N/2+1)):
            Splot[alpha][series] = soln[len1-1,alpha]
        for beta in range((N/2)+1,N+1):
            KSplot[beta-N/2-1][series] = soln[len1-1,beta]
        for gamma in range(N+1,3*N/2+1):
            PSplot[gamma-N-1][series] = soln[len1-1,gamma]

        u1 = 0
        u2 = 0
        u3 = 0

        for alpha in range(0,(N/2+1)):
            u1 = u1 + Splot[alpha]
        for beta in range((N/2)+1,N+1):
            u2 = u2 + KSplot[beta-N/2-1]
        for gamma in range(N+1,3*N/2+1):
            u3 = u3 + PSplot[gamma-N-1]

        K = soln[:,3*N/2+1]
        P = soln[:,3*N/2+2]
        Kplot[series] = soln[len1-1,3*N/2+1]
        Pplot[series] = soln[len1-1,3*N/2+2]
        utot = u1+u2+u3

    #Plot
    Kcrit = abs((Q/r2)*(1+epsilon)-utot)
    v,i = Kcrit.min(0),Kcrit.argmin(0)
    # Save the new points for x and y
    x_data_plot.append(N)
    y_data_plot.append(K00[i])

# Make the plot of all the points together
plt.plot(x_data_plot,y_data_plot)
plt.show()

rawDescription: Plotting within a for loop, with 'hold on' effect in matplotlib?


code: datetime.combine(date, time)

rawDescription: Combining a parsed time with today's date in Python


code:
for anchor in tbody.findAll('div', style="s1"):
    text = ''.join([x for x in anchor.contents if isinstance(x, bs4.element.NavigableString)])
    print([text])
    print([text.strip()])

rawDescription: How to use python beautiful soup to get only the level 1 navigableText?

description: Use python beautiful soup to get only the level 1 navigableText


code:
import collections
d=collections.OrderedDict()
for e in dates:
    day, hour, full_time=e.strftime('%Y%m%d'), e.strftime('%H'), e.strftime('%H%M%S')
    d.setdefault(day,collections.OrderedDict()).setdefault(hour, []).append(full_time)

rawDescription: Extract days, hours and seconds from a list of datetimes


code:
import time, weather, sport, horoscope
kwargs = {'temperature':30}
condition = 'weather'
while True:
    if condition == 'weather':
        weather.check_all(**kwargs)
    elif condition == 'sport':
        sport.check_all()
    elif condition == 'horoscope':
        horoscope.check_all()
    time.sleep(1)

rawDescription: How to split python script in parts and to import the parts in a loop?

description: Split python script in parts and to import the parts in a loop


code:
from datetime import datetime
from calendar import monthrange

year, month = int(month[:2]), int(month[2:])
year += 2000  # assume this century
day = monthrange(year, month)[1]

dt = datetime(year, month, day)  # time defaults to 00:00:00

rawDescription: Get a datetime format from a string in Python


code:
L = list # 'list' is a poor variable name, use something else
result = min((n.foo(args) for n in L),
             key=lambda x: ClassFred.objects.get(arg1=x))
# if you don't have to use arg1 as a named parameter:
result = min((n.foo(args) for n in L), key=ClassFred.objects.get)

rawDescription: Is this control structure a code smell?

description: Is this control structure a code smell


code:
def sort(names, rank):
    lst=[]
    for index, name in enumerate(names):
        lst.append((int(rank[index]), name))
    lst.sort(key=lambda x: (-x[0],len(x[1]),  ord(x[1][0])))
    output = []
    for row in lst:
        output.append(row[1] + " " + str(row[0]))
    return output

rawDescription: Sort by name and rank

description: Sort by name and rank


code:
from datetime import date, timedelta as td, datetime
d1 = datetime.strptime('1/1/2015', "%m/%d/%Y")
d2 = datetime.strptime('12/31/2015', "%m/%d/%Y")

AllDays = []
while(d1<=d2):
    AllDays.append(d1)
    d1 = d1 + td(days=1)

temps = np.random.normal( 20, 0.5, size=(500,365) )
temps = pd.DataFrame( temps.T, index=AllDays )

fig, ax = plt.subplots( 1, 1, figsize=(16,8) )
ax.plot( temps.index, temps.T.mean(), color='blue', linewidth=2 )

rawDescription: python plot distribution across mean

description: Python plot distribution across mean


code:
class Sites(models.Model):
    name = models.CharField(max_length=75)
    link = models.CharField(max_length=150)
    description = models.TextField(blank=True, null=True)

    def __str__(self):
        return self.name

class PaymentProcessors(models.Model):
    name = models.CharField(max_length=75)
    sites = models.ManyToManyField('Sites', related_name='payment_processors', through='SitePaymentProcessor')

    def __str__(self):
        return self.name

from django.core.validators import MaxValueValidator
class SitePaymentProcessor(models.Model):
    site = models.ForeignKey('Site')
    payment_processors = models.ForeignKey('PaymentProcessors')
    amount_limit = models.IntegerField(default=1000,
        validators=[
            MaxValueValidator(100)
        ])

rawDescription: Update Table Relationship in Django Admin


code: reduce(operator.getitem, c, b)

rawDescription: Append to a list using indexes stored in another list


code:
from datetime import datetime
naive_dt = datetime.strptime(date_str, '%Y-%m-%d')
dt = pytz.timezone('Europe/London').localize(naive_dt)

rawDescription: How to convert date string to a datetime object in a specified timezone

description: Convert date string to a datetime object in a specified timezone


code:
from itertools import groupby, product
import csv

output_header = ["a_id", "b_id", "var1", "lo", "up", "var2", "lo", "up"]

f_input = open('dataFile.csv', 'rb')
csv_input = csv.reader(f_input)
input_header = next(csv_input)

f_output = open('resultFile.csv', 'wb')
csv_output = csv.writer(f_output)
csv_output.writerow(output_header)

for k1, g1 in groupby(csv_input, key=lambda x: (x[0], x[1])):
    group1 = list(g1)

    if len(group1) > 1:
        for row in group1:
            csv_output.writerow(row + ['null'] * 3)

        p = [list(g2) for k2, g2 in groupby(group1, key=lambda x: x[2])]

        for pairs in product(*p):
            if len(pairs) > 1:
                csv_output.writerow(pairs[0] + pairs[1][2:])

f_input.close()
f_output.close()

rawDescription: Create different combination / patterns between the data of two columns of a csv file by python


code:
def adjacent_pairs(seq):
    it = iter(seq)
    a = it.next()
    for b in it:
        yield a, b
        a = b

for r1, r2 in adjacent_pairs(dm_revisions):
    summary = svn.diff_summarize(svn_path, revision1=r1, revision2=r2)

rawDescription: Python looping: idiomatically comparing successive items in a list


code:
#import itertools

table_="""
    0       50
    1       50
    ...
    1021    49
    1022    50
    1023    50""".split()

# Convert values to int. Throw away the indices - will recover them with enumerate()
table = [int(x) for x in table_[1::2]]

compressed_table = []
istart = 0
for i,v in enumerate(table):
    if v != table[i-1]:
        iend = i-1
        compressed_table.append((istart,iend,table[i-1]))
        istart = i
    else:
        continue # skip identical values
# Slightly ugly: append the last value, when the iterator was exhausted
compressed_table.append((istart,i,table[i]))

rawDescription: Compressing a sinewave table

description: Compressing a sinewave table


code:
def myslice(start, stop=None, step=None):
    return slice(start, (stop if stop is not None else start) + 1, step)

def parse(s):
    return [myslice(*map(int, x.split('-'))) for x in s.split(',')]

rawDescription: pythonic format for indices

description: Pythonic format for indices


rawDescription: comparing varied CSV files in python

description: Comparing varied CSV files in python


code:
itercars = iter(cars)
# add 'next(itercars)' here if you also want to skip the first
prev = next(itercars)
for car in itercars:
    # do work on 'prev' not 'car'
    # at end of loop:
    prev = car
# now you can do whatever you want to do to the last one on 'prev'

rawDescription: Skip first entry in for loop in python?

description: Skip first entry in for loop in python


code:
# if above does not work
combination[r-2] += 1
combination[r-1]  = combination[r-2] + 2

rawDescription: Generating non-consecutive combinations

description: Generating non-consecutive combinations


rawDescription: Replicating GROUP_CONCAT for pandas.DataFrame


code: boxtype = content.find("boxtype")

rawDescription: XML Not Parsing in Python 2.7 with ElementTree


code:
from collections import Counter
otherWord = 'GREEN'
otherWord = Counter(otherWord)
otherWord

rawDescription: Compare two strings, including duplicate letters?


code:
import re
import urllib

hand = urllib.request.urlopen("http://www.pythonlearn.com/code/mbox-short.txt")
numlist = []
# iterate over each line like a file object
for line in hand:
    stuff = re.search("^X-DSPAM-Confidence: ([0-9.]+)", line.decode("utf-8"))
    if stuff:
        numlist.append(float(stuff.group(1)))
print('Maximum:', max(numlist))

rawDescription: Reading Text File From Webpage by Python3


code:
def zipij(m=mem, ms=mems):
  return [(i, m) if i < m else (m, i) for i in ms]

rawDescription: Efficient reordering of coordinate pairs (2-tuples) in a list of pairs in Python


code:
def ordered_combinations(pop, n):
    pop = sorted(pop)

    for s in range(sum(pop[:n]), sum(pop[-n:])+1):
        yield from get_sums(pop, s, n)

def get_sums(pop, s, n):
    if n == 1:
        if s in pop:
            yield [s]
        return

    for i, v in enumerate(pop):
        if sum(pop[i:i+n]) > s:
            return
        for rest in get_sums(pop[i+1:], s-v, n-1):
            rest.append(v)
            yield rest

rawDescription: How do I iterate over a large number of tuples of integers in the order of their sum?

description: Iterate over a large number of tuples of integers in the order of their sum


code:
bob
['reading, singing', 'drawing']

rawDescription: PyParsing a string representing a function


code:
from email.encoders import encode_noop
from email.generator import BytesGenerator
from email.mime.application import MIMEApplication
import io

# Actual binary "file" I want to encode (in real life, this is a file read from disk)
bytesToEncode = b'Q\x0dQ'

# unique marker that we can find and replace after message generation
binarymarker = b'GuadsfjfDaadtjhqadsfqerasdfiojBDSFGgg'

app = MIMEApplication(binarymarker, _encoder=encode_noop)
b = io.BytesIO()
g = BytesGenerator(b)
g.flatten(app, linesep='\r\n')  # linesep for HTTP-compliant header line endings

# replace the marker with the actual binary data, then you have the output you want!
body = b.getvalue().replace(binarymarker, bytesToEncode)

rawDescription: Creating a multi-part MIME message with a binary component for an HTTP request in Python 3


code:
import matplotlib
from matplotlib import pylab

if matplotlib.is_interactive():
    pylab.ioff()
pylab.plot(range(10), range(10))
pylab.show()

rawDescription: Keep a figure "on hold" after running a script


code:
import numpy as np
a = np.random.rand(12, 3, 3)
np.linalg.inv(a)

rawDescription: Vectorized (partial) inverse of an N*M*M tensor with numpy


code:
fileList.pop(0)
fileList.pop(0)

rawDescription: Complex parsing query

description: Complex parsing query


code: html = BeautifulSoup(html1, features="xml")

rawDescription: Beautifulsoup removing HTML tags when parsing XML


rawDescription: How to slice memmap efficiently?

description: Slice memmap efficiently


code:
import passlib.hash, crypt

ctype = "6" #for sha512 (see man crypt)
salt = "qwerty"
insalt = '${}${}$'.format(ctype, salt)
password = "AMOROSO8282"

value1 = sha512_crypt.encrypt(password, salt=salt, rounds=5000)
value2 = crypt.crypt(password, insalt)
if not value1 == value2:
    print("algorithms do not match")
print("{}\n{}\n\n".format(value1, value2))

rawDescription: hashlib vs crypt.crypt() in Python. Why different results?

description: Hashlib vs crypt.crypt() in Python. Why different results


rawDescription: What are the possible numpy value format strings?


code:
with open('/tmp/scores.txt', 'r') as f, open('/tmp/mod_scores.txt', 'w') as fout:
    for line in f:
        if line.startswith('jill'):
            line=' '.join(line.split()[0:4])
        fout.write(line.strip()+"\n")

rawDescription: Replacing a line within a text file

description: Replacing a line within a text file


code:
for item in list2:
    print(list1[item-1])

rawDescription: Indices are not synchronous of using one lists indices in another


code:
class waypointForm(forms.ModelForm):
    def __init__(self, user, *args, **kwargs):
        super(waypointForm, self).__init__(*args, **kwargs)
        self.fields['waypoints'] = forms.ModelChoiceField(
            queryset=Waypoint.objects.filter(user=user)
        )

    class Meta:
        model = Waypoint

rawDescription: Creating a dynamic choice field

description: Creating a dynamic choice field


code:
import requests
r = requests.get('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml', stream=True)
from xml.etree import ElementTree as ET
tree = ET.parse(r.raw)
root = tree.getroot()
namespaces = {'ex': 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref'}
for cube in root.findall('.//ex:Cube[@currency]', namespaces=namespaces):
    print(cube.attrib['currency'], cube.attrib['rate'])

rawDescription: How to parse XML file from European Central Bank with Python

description: Parse XML file from European Central Bank with Python


code: sorted(list_of_objects, key=lambda obj: obj.method_name())

rawDescription: sort list of objects by return of object method

description: Sort list of objects by return of object method


code:
cam = AVTCamera()
cam = Camera('ximea')

rawDescription: Industrial vision camera with Python

description: Industrial vision camera with Python


rawDescription: replace elements to reformat the dataset

description: Replace elements to reformat the dataset


code:
def inorder(t):
    if t.left:
        for elem in inorder(t.left):
            yield elem
    yield t
    if t.right:
        for elem in inorder(t.right):
            yield elem

rawDescription: How to traverse a binary Tree with a recursive generator?

description: Traverse a binary Tree with a recursive generator


code:
from ctypes import *
libc = cdll.msvcrt

buf = create_string_buffer(256)
buf_u = create_unicode_buffer(256)

libc.scanf(b"%s",  buf)
libc.wscanf("%s",  buf_u)

print(buf.value)
print(buf_u.value)

rawDescription: How to translate the cast of a memory address in C into python ctypes call?

description: Translate the cast of a memory address in C into python ctypes call


rawDescription: Python Greedy Algorithm

description: Python Greedy Algorithm


code:
import logging # of course you have this
import inspect

# some lines to set logger
log = logging.getLogger('some_loger')
log.addHandler(logging.StreamHandler())

class SomeClass:
    def log_it(self, log_text):
        log.warn("DEPRECATION WARNING: {0[3]} was called from {1[3]} at line {1[2]} in {1[1]}".format(*inspect.stack()[:2]))

rawDescription: centralized logging method and logging current line number

description: Centralized logging method and logging current line number


rawDescription: Round up floats to first decimal point

description: Round up floats to first decimal point


code:
temp2 = {'0.1995': set(['X7:GATE', 'X3:GATE', 'IN1']), '0.199533': set(['X4:GATE', 'X8:GATE', 'IN2']), '0.399': set(['X3:GATE', 'X5:GATE', 'X1:GATE', 'IN0', 'X4:GATE', 'Y', 'X8:GATE'])}
y_key = '0.399'
y_values = temp2[y_key]
for test_d, test_p in temp2.iteritems():
    if test_d == y_key:
        continue
    y_values.difference_update(test_p)

temp2

rawDescription: python: how to compare more than two keys in the same dictionary?

description: Python: how to compare more than two keys in the same dictionary


code:
total = sum(a.itervalues(), 0.0)
a = {k: v / total for k, v in a.iteritems()}

rawDescription: Divide each python dictionary value by total value


code:
import os
import re

back_up = 6 * 256       # Go back from the end more than 6 lines worth

directory = r"C:\Users\hemanth_venkatappa\Desktop\TEST\Language"
output_filename = r"C:\Users\hemanth_venkatappa\Desktop\TEST\output.txt"

with open(output_filename, 'w') as f_output:
    for dirpath, dirnames, filenames in os.walk(directory):
        for filename in filenames:
             if filename.startswith('VCALogParser_output'):
                cur_file = os.path.join(dirpath, filename)
                with open(cur_file, "r") as f_input:
                    f_input.seek(0, os.SEEK_END)
                    backup = min(back_up , f_input.tell())
                    f_input.seek(-backup, os.SEEK_END)
                    last_lines = ''.join(f_input.readlines()[-6:])
                    try:
                        summary = ', '.join(re.search(r'(\d+ warning\(s\)).*?(\d+ error\(s\)).*?(\d+ scenarios\(s\))', last_lines, re.S).groups())
                    except AttributeError:
                        summary = "No summary"
                    f_output.write('{}: {}\n'.format(filename, summary))

rawDescription: How to read the last few lines within a file using Python?

description: Read the last few lines within a file using Python


code:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

rawDescription: Converting pixel grid to contours?

description: Converting pixel grid to contours


rawDescription: Difference between logical line and physical line in python


code:
df[0].replace(-9999, np.nan)
df[0].replace(-9999, np.nan).mean()

rawDescription: Calculate dataframe mean by skipping certain values in Python / Pandas


code:
class UnionFind:
    """Union-find data structure. Items must be hashable."""

    def __init__(self):
        """Create a new empty union-find structure."""
        self.weights = {}
        self.parents = {}

    def __getitem__(self, obj):
        """X[item] will return the token object of the set which contains `item`"""

        # check for previously unknown object
        if obj not in self.parents:
            self.parents[obj] = obj
            self.weights[obj] = 1
            return obj

        # find path of objects leading to the root
        path = [obj]
        root = self.parents[obj]
        while root != path[-1]:
            path.append(root)
            root = self.parents[root]

        # compress the path and return
        for ancestor in path:
            self.parents[ancestor] = root
        return root

    def union(self, obj1, obj2):
        """Merges sets containing obj1 and obj2."""
        roots = [self[obj1], self[obj2]]
        heavier = max([(self.weights[r],r) for r in roots])[1]
        for r in roots:
            if r != heavier:
                self.weights[heavier] += self.weights[r]
                self.parents[r] = heavier

rawDescription: Group a list of points/tuples

description: Group a list of points/tuples


code:
import argparse
import sqlite3
import textwrap
import time

parser = argparse.ArgumentParser()
parser.add_argument("n", help="num lines per commit", type=int)
arg = parser.parse_args()

con = sqlite3.connect('lorem.db')
cur = con.cursor()
cur.execute('drop table if exists Lorem')
cur.execute('create table Lorem (lorem STRING)')
con.commit()

with open('lorem.txt') as f: lorem=textwrap.wrap(f.read())
print('{} lines'.format(len(lorem)))

start = time.time()
for i, line in enumerate(lorem):
    cur.execute('INSERT INTO Lorem(lorem) VALUES(?)', (line,))
    if i % arg.n == 0: con.commit()
stend = time.time()

print('{} lines/commit: {:.2f}'.format(arg.n, stend-start))

rawDescription: connection.commit() performance impact

description: Connection.commit() performance impact


code:
from twisted.application.internet import TimerService

def foo():
    from twisted.internet import reactor
    reactor.callWhenRunning(reactor.stop)

from twisted.application.service import Application

application = Application("timer stop")
TimerService(3, foo).setServiceParent(application)

rawDescription: How to stop a TimerService from within the LoopingCall

description: Stop a TimerService from within the LoopingCall


code: [a[i:i+2] for i in range(0, len(a), 2)]

rawDescription: Reverse a string in Python two characters at a time (Network byte order)


code:
from bs4 import BeautifulSoup
url = "http://en.wikipedia.org/wiki/List_of_works_by_Vincent_van_Gogh"
soup = BeautifulSoup(urlopen(url))
table = soup.find('table', class_="wikitable")
for row in table.find_all('tr')[1:]:
    print(row.find_all('td')[1].text)

rawDescription: Wikipedia API JSON with Python

description: Wikipedia API JSON with Python


code: def tree(): return defaultdict(tree)

rawDescription: Managing the paths of a directory tree in python


code: ['CGACA', 'GAAGA']

rawDescription: Python:Comparing all list items to substring of string


rawDescription: pass pandas dataframe as parameter in mysql query

description: Pass pandas dataframe as parameter in mysql query


code:
Using
reverse('app_name:run-task', kwargs={'task': 'task-a'})

rawDescription: running custom command from django's admin backend

description: Running custom command from django's admin backend


code:
import lxml.etree as ET
import lxml.builder as builder

tree = ET.parse('VimKeys.xml')
root = tree.getroot()

E = builder.E
tables = []
for child in root:
    trs = []
    for c in child:
        trs.append(E('tr',
                     E('th', c.text),
                     E('td', c.attrib['description'])))
    tables.append(E('table',
                    E('caption', child.attrib['title']),
                    *trs))

newroot = E('root', *tables)
print(ET.tostring(newroot, pretty_print=True))

rawDescription: preserve entity references when parsing xml

description: Preserve entity references when parsing xml


code:
from itertools import groupby,islice
a = ['a', 'a', 'b', 'c', 'c', 'c', 'b']
[list(g) for k,g in groupby(a)]
[k for k,g in groupby(a) if len(list(g))>=2]

rawDescription: Repeating elements in list Python

description: Repeating elements in list Python


code:
import pylab as pl
import numpy as np

angle = np.arange(0, np.pi*2, 0.05)
r = 50 + np.random.normal(0, 2, angle.shape)

x = r * np.cos(angle)
y = r * np.sin(angle)

idx = np.random.permutation(angle.shape[0])
x = x[idx]
y = y[idx]

rawDescription: How to draw circle by data with matplotlib + python?

description: Draw circle by data with matplotlib + python


code: sorted([max(x) for x in ords], reverse=True) # puts "z" first because of reversing

rawDescription: Sorting dictionary by value and key length


code:
# ==>
#
# [('electronics', 'VCR'), ('toys', 'GI-Joe'), ('sporting_goods', 'Racquet')]
# [('electronics', 'VCR'), ('toys', 'GI-Joe'), ('fruits', 'Orange')]
# [('electronics', 'VCR'), ('toys', 'GI-Joe'), ('fruits', 'Apple')]
# [('electronics', 'VCR'), ('sporting_goods', 'Racquet'), ('fruits', 'Orange')]
# [('electronics', 'VCR'), ('sporting_goods', 'Racquet'), ('fruits', 'Apple')]
# [('toys', 'GI-Joe'), ('sporting_goods', 'Racquet'), ('fruits', 'Orange')]
# [('toys', 'GI-Joe'), ('sporting_goods', 'Racquet'), ('fruits', 'Apple')]

rawDescription: Creating restricted permutations of a list of items by category


code:
def get_id_tuple(f, args, kwargs, mark=object()):
    """
    Some quick'n'dirty way to generate a unique key for an specific call.
    """
    l = [id(f)]
    for arg in args:
        l.append(id(arg))
    l.append(id(mark))
    for k, v in kwargs:
        l.append(k)
        l.append(id(v))
    return tuple(l)

_memoized = {}
def memoize(f):
    """
    Some basic memoizer
    """
    def memoized(*args, **kwargs):
        key = get_id_tuple(f, args, kwargs)
        if key not in _memoized:
            _memoized[key] = f(*args, **kwargs)
        return _memoized[key]
    return memoized

rawDescription: How can I memoize a class instantiation in Python?

description: Memoize a class instantiation in Python


code: scores_list[ind] = "{} {}\n".format(line.rstrip(), score)

rawDescription: Updating a Python list based on a search append


code:
filter_data = {key: value for (key, value) in filter_data.items()
               if value}

rawDescription: Remove objects from query if None or Null


rawDescription: How to determine if numpy.vectorize() is needed?

description: Determine if numpy.vectorize() is needed


code:
class WidgetTest(unittest.TestCase):
    """Tests methods available in every ttk widget."""

    def setUp(self):
        support.root_deiconify()
        self.widget = ttk.Button(width=0, text="Text")
        self.widget.pack()
        self.widget.wait_visibility()

rawDescription: how to show a loading message in tkinter

description: Show a loading message in tkinter


code:
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

fig = plt.figure()
gs = GridSpec(2, 3, width_ratios=[10, 1, 1], height_ratios=[1, 10])

rawDescription: How to plot heat map with matplotlib?

description: Plot heat map with matplotlib


rawDescription: Best way to split a list into randomly sized chunks?

description: Split a list into randomly sized chunks


code:
from itsdangerous import TimedSerializer
s = TimedSerializer('sekrit')
token = s.dumps({'id': 'foobar'})
token
s.loads(token, max_age=3600)
s.loads(token, max_age=0)

rawDescription: python: API token generation with itsdangerous

description: Python: API token generation with itsdangerous


rawDescription: Split list into list of lists by regex

description: Split list into list of lists by regex


code: localexpiry = '%04d%02d%02d' % (difference.year, difference.month, difference.day)

rawDescription: Two-Digit dates in Python

description: Two-Digit dates in Python


code:
class Foo(models.Model):
    GENDER = (
      ('F', 'Female'),
      ('M', 'Male'),
    )
    name = models.CharField(max_length=150)
    gender = models.CharField(max_length=1, choices=GENDER)

rawDescription: Passing objects from template to view using Django


code:
import functools

def with_description(fn):

    @functools.wraps(fn)  # This is not strictly necessary, just pulls info (docstrings, etc) from fn and pushes into the wrapper
    def wrapper(*args, **kwargs):
        # Precheck
        installation_name = request.args.get('installation_name', '')
        if installation_name == '':
            data = {"description": "Installation Name is required"}
            return HttpResponse(json.dumps(data), status=400, mimetype='application/json')

        # If precheck passes, call the actual function
        fn(*args, **kwargs)

    return wrapper

rawDescription: Creating a Decorator

description: Creating a Decorator


rawDescription: Complicated sorting in Python

description: Complicated sorting in Python


code:
def char_standoff(tree):
    leaves, tok_standoff = standoff(tree)
    text = ' '.join(leaves)
    # Map leaf index to its start and end character
    starts = []
    offset = 0
    for leaf in leaves:
        starts.append(offset)
        offset += len(leaf) + 1
    starts.append(offset)
    return text, [(path, label, starts[start_tok], starts[end_tok] - 1)
                  for path, label, start_tok, end_tok in tok_standoff]

rawDescription: Converting NLTK phrase structure trees to BRAT .ann standoff


code:
dmin = 0.2
dmax = 0.3
pos = scipy.all( (Z[:,2] >= dmin, Z[:,2] <= dmax), axis=0 ).nonzero()
plot_tree( P, pos )

rawDescription: how do I get the subtrees of dendrogram made by scipy.cluster.hierarchy

description: Get the subtrees of dendrogram made by scipy.cluster.hierarchy


code:
dep_list = []
for i,dest in enumerate(dests):
    for src in srcs:
        if dest in src:
            dep_list.append(opcodes[i])

rawDescription: Python: Script to detect data Hazards

description: Python: Script to detect data Hazards


rawDescription: Is there a way to import an embedded python script into IronPython as a python module?


rawDescription: Converting Python (PyKinect) Skeleton data to Points (X,Y)


rawDescription: Python Pandas: How to get the row names from index of a dataframe?


code:
import zipfile
with zipfile.ZipFile('example.zip', 'r') as zf:
    zf.extract(filename)

rawDescription: Python opening a File within a Zip File

description: Python opening a File within a Zip File


code: L = list(range(int(1e6)))

rawDescription: Longest increasing subsequence in Python (For vs While loop)


rawDescription: Best way to locate this element in selenium?

description: Locate this element in selenium


rawDescription: Defining a function from IDL in Python

description: Defining a function from IDL in Python


code:
import ctypes as _ctypes
from ctypes import wintypes as _wintypes

_advapi32 = _ctypes.WinDLL('advapi32', use_last_error=True)

ERROR_INVALID_FUNCTION    = 0x0001
ERROR_FILE_NOT_FOUND      = 0x0002
ERROR_PATH_NOT_FOUND      = 0x0003
ERROR_ACCESS_DENIED       = 0x0005
ERROR_INSUFFICIENT_BUFFER = 0x007A

_LPBOOL  = _ctypes.POINTER(_wintypes.BOOL)
_LPDWORD = _ctypes.POINTER(_wintypes.DWORD)
_PSID = _ctypes.POINTER(_wintypes.BYTE)
_PSECURITY_DESCRIPTOR = _ctypes.POINTER(_wintypes.BYTE)
_SECURITY_INFORMATION = _wintypes.DWORD

OWNER_SECURITY_INFORMATION     = 0x00000001
GROUP_SECURITY_INFORMATION     = 0x00000002
DACL_SECURITY_INFORMATION      = 0x00000004
SACL_SECURITY_INFORMATION      = 0x00000008
LABEL_SECURITY_INFORMATION     = 0x00000010
ATTRIBUTE_SECURITY_INFORMATION = 0x00000020
SCOPE_SECURITY_INFORMATION     = 0x00000040
BACKUP_SECURITY_INFORMATION    = 0x00010000
UNPROTECTED_SACL_SECURITY_INFORMATION = 0x10000000
UNPROTECTED_DACL_SECURITY_INFORMATION = 0x20000000
PROTECTED_SACL_SECURITY_INFORMATION   = 0x40000000
PROTECTED_DACL_SECURITY_INFORMATION   = 0x80000000

class _SID_NAME_USE(_wintypes.DWORD):
    _sid_types = dict(enumerate('''
            User Group Domain Alias WellKnownGroup
            DeletedAccount Invalid Unknown
            Computer Label'''.split(), 1))

    def __init__(self, value=None):
        if value is not None:
            if value not in self._sid_types:
                raise ValueError('invalid SID type')
            super(_SID_NAME_USE, self).__init__(value)

    def __str__(self):
        if self.value not in self._sid_types:
            raise ValueError('invalid SID type')
        return self._sid_types[self.value]

_PSID_NAME_USE = _ctypes.POINTER(_SID_NAME_USE)

def _check_bool(result, func, args,
                WinError=_ctypes.WinError,
                get_last_error=_ctypes.get_last_error):
    if not result:
        raise WinError(get_last_error())
    return args

# msdn.microsoft.com/en-us/library/aa446639
_advapi32.GetFileSecurityW.errcheck = _check_bool
_advapi32.GetFileSecurityW.argtypes = (
    _wintypes.LPCWSTR,     # _In_      lpFileName
    _SECURITY_INFORMATION, # _In_      RequestedInformationRequested
    _PSECURITY_DESCRIPTOR, # _Out_opt_ pSecurityDescriptor
    _wintypes.DWORD,       # _In_      nLength
    _LPDWORD)              # _Out_     lpnLengthNeeded

# msdn.microsoft.com/en-us/library/aa446651
_advapi32.GetSecurityDescriptorOwner.errcheck = _check_bool
_advapi32.GetSecurityDescriptorOwner.argtypes = (
    _PSECURITY_DESCRIPTOR,  # _In_  pSecurityDescriptor
    _ctypes.POINTER(_PSID), # _Out_ pOwner
    _LPBOOL)                # _Out_ lpbOwnerDefaulted

# msdn.microsoft.com/en-us/library/aa379166
_advapi32.LookupAccountSidW.errcheck = _check_bool
_advapi32.LookupAccountSidW.argtypes = (
    _wintypes.LPCWSTR, # _In_opt_  lpSystemName
    _PSID,             # _In_      lpSid
    _wintypes.LPCWSTR, # _Out_opt_ lpName
    _LPDWORD,          # _Inout_   cchName
    _wintypes.LPCWSTR, # _Out_opt_ lpReferencedDomainName
    _LPDWORD,          # _Inout_   cchReferencedDomainName
    _PSID_NAME_USE)    # _Out_     peUse

def get_file_security(filename, request):
    length = _wintypes.DWORD()
    # N.B. This query may fail with ERROR_INVALID_FUNCTION
    # for some filesystems.
    try:
        _advapi32.GetFileSecurityW(filename, request,
                                   None, 0, _ctypes.byref(length))
    except WindowsError as e:
        if e.winerror != ERROR_INSUFFICIENT_BUFFER:
            raise
    if not length.value:
        return None
    sd = (_wintypes.BYTE * length.value)()
    _advapi32.GetFileSecurityW(filename, request,
                               sd, length, _ctypes.byref(length))
    return sd

def get_security_descriptor_owner(sd):
    sid = _PSID()
    sid_defaulted = _wintypes.BOOL()
    _advapi32.GetSecurityDescriptorOwner(sd,
                                         _ctypes.byref(sid),
                                         _ctypes.byref(sid_defaulted))
    return sid

def look_up_account_sid(sid):
    SIZE = 256
    name = _ctypes.create_unicode_buffer(SIZE)
    domain = _ctypes.create_unicode_buffer(SIZE)
    cch_name = _wintypes.DWORD(SIZE)
    cch_domain = _wintypes.DWORD(SIZE)
    sid_type = _SID_NAME_USE()
    _advapi32.LookupAccountSidW(None, sid,
                                name, _ctypes.byref(cch_name),
                                domain, _ctypes.byref(cch_domain),
                                _ctypes.byref(sid_type))
    return name.value, domain.value, sid_type

rawDescription: Howto determine file owner on windows using python without pywin32


code:
d = {}
for a in sys.argv[1:]:
    k, v = a.split('=')
    d[k] = int(v)
func(**d)

rawDescription: Python: Using run time inputs to change key values in a function


rawDescription: Code for generating all unique permutations recursively?


rawDescription: Add multiple rows into google spreadsheet using API


rawDescription: Python: Extracting values from list of values


code: logger = logging.getLogger('your app or module')

rawDescription: No handler found for library with logging configured


code:
def maybe(x):
    """ only keep odd value; returns either element or None """
    result = 11 * x
    if result & 1: return result

def do_stuff():
    yield maybe(1)
    yield maybe(6)
    yield maybe(5)

rawDescription: python 2.7 - is there a more succint way to do this series of yield statements (in python 3, "yield from" would help)

description: Python 2.7 - is there a more succint way to do this series of yield statements (in python 3, "yield from" would help)


rawDescription: Summing over lists in python - is there a better way?


code: filter(lambda x: not x in (4, 5, 7), xrange(10))

rawDescription: Reducing complexity of following code

description: Reducing complexity of following code


code:
import matplotlib.pyplot as plt
import numpy as np
x1 = y1 = np.linspace(0, 10, 100)
x2 = np.sin(x1)
y2 = np.cos(y1)
plt.plot(x1,x2,'g--^', label='regional')
plt.plot(y1,y2,'b-o', label='local')
plt.legend( loc='upper left', numpoints = 1 )
plt.show()

rawDescription: Python plot legend : markers appear twice


code:
english = "abcdefghijklmnopqrstuvwxyz" + ",';.?"
secret = []
with open("secrets.txt" , "r") as s:
    secret = [line.rstrip() for line in s]

cypher = dict((c, [x[i] for x in secret]) for i, c in enumerate(english))

def translate(s):
    secret_index = 0
    trans = []
    for c in s:
        # not sure how spaces are dealt with
        trans.append(cypher[c][secret_index] if not c == ' ' else ' ')
        secret_index = (secret_index + 1) % len(secret)
    return ''.join(trans)

print(translate("hello")) # vmbjc

rawDescription: Reading and writing to files in python, translating text in one file to another


code: [{'id1': k, 'price': temp[k]} for k in temp]

rawDescription: Merge values of same key, in list of dicts


code:
def grouping(data):
    dec=pd.qcut(data['V1'],10,labels=False)
    data['ranks']=dec
    return data
df_ranked=df.groupby('Date').apply(grouping)

rawDescription: ranks within groupby in pandas

description: Ranks within groupby in pandas


code:
dynamic_buttons = []

def onDoubleClick(event):
    ...
    button = Button(...)
    dynamic_buttons.append(button)
    button.pack()

rawDescription: Tkinter: How can I dynamically create a widget that can then be destroyed or removed?


rawDescription: Split on space but not if there is a colon followed by space or if there is a space in quotes


code:
class Operator:
    all = []
    def __init__(self, precedence):
        self.precedence = precedence
        self.all.append(self)  # Modifies the class variable, as we haven't
                               # introduced an instance variable with that name.

PLUS = Operator(0)
UPLUS = Operator(10)
MIN = Operator(0)
UMIN = Operator(10)
MULT = Operator(20)

# Freeze the collection of Operators
Operator.all = tuple(Operator.all)

rawDescription: Python: Grouping objects together

description: Python: Grouping objects together


code:
from collections import defaultdict

def bucket_list(nested_list, sort=True):
    bucket = defaultdict(list)
    for sublist in nested_list:
        bucket[len(sublist)].append(sublist)
    return [v for k,v in sorted(bucket.items())] if sort else bucket.values()

rawDescription: Best way to turn a list of lists into a more nested list of lists of lists with equal length?

description: Turn a list of lists into a more nested list of lists of lists with equal length


code:
km = KMeans()
km.fit(X)

rawDescription: Determine the cluster for a new document with Scikit


code: print("Your %s day trip in %s is %0.2f dollars"%(days, city, trip_cost(city, days)))

rawDescription: How to give return value of a function to print in Python

description: Give return value of a function to print in Python


code:
def strip_all_ext(path):
    head, tail = os.path.split(path)
    basename = tail.split(os.extsep)[0]
    return os.path.join(head, basename)

rawDescription: Iterate within directory to zip files with python


code:
from fabric import *
from something import find_instances

env.roledefs = {
    'eu-west-1' : find_instances('eu-west-1'),
    'eu-west-2' : find_instances('eu-west-2'),
}

@task
def restart_apache2():
    run('/etc/init.d/apache2 restart')

rawDescription: Python Fabric decorator

description: Python Fabric decorator


rawDescription: Questions about details of @property in Python


code:
if key.startswith('__tuple__('):
     key = tuple(key[10:-1].split(','))

rawDescription: JSON encoding for dict of dicts

description: JSON encoding for dict of dicts


code:
Slot.objects.bulk_create([
    Slot(name='main_hand'),
    Slot(name='off_hand'),
    Slot(name='head'),
    Slot(name='neck'),
    Slot(name='shoulder'),
    Slot(name='back'),
    Slot(name='chest'),
    Slot(name='wrist'),
    Slot(name='hands'),
    Slot(name='waist'),
    Slot(name='legs'),
    Slot(name='feet'),
    Slot(name='ring1'),
    Slot(name='ring2'),
    Slot(name='trinket1'),
    Slot(name='trinket2'),
])
Item.objects.bulk_create([
    Item(name='Short Sword'),
    Item(name='Sabre'),
    Item(name='Broadsword'),
    Item(name='Apprentice Broadsword'),
    Item(name='Monster Hunter'),
])
user1 = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
user2 = User.objects.create_user('paul', 'mccartney@thebeatles.com', 'mccartnypassword')
player1 = Player.objects.create_player_with_empty_slots(user=user1)
player2 = Player.objects.create_player_with_empty_slots(user=user2)

rawDescription: Efficient reverse of queryset

description: Efficient reverse of queryset


code:
"Campaign X" in mydf["Campaign"].values
"Campaign X" in dict(mydf["Campaign"]).values()

rawDescription: Why is get_values() needed in a membership test on a pandas DataFrame column?


code:
def remove_bad_substrings(s):
    badSubstrings = ["www.", ".com", ".net", ".museum"]
    for badSubstring in badSubstrings:
        s = s.replace(badSubstring, "")
    return s

sites=['www.hattrick.com', 'www.google.com',
'www.wampum.net', 'www.newcom.com', 'smithsonian.museum']
[remove_bad_substrings(s) for s in sites]

rawDescription: removing first four and last four characters of strings in list, OR removing specific character patterns

description: Removing first four and last four characters of strings in list, OR removing specific character patterns


rawDescription: Python datetime transform

description: Python datetime transform


code:
from collections import OrderedDict

class DefaultOrderedDict(OrderedDict):
    def __missing__(self, key):
        self[key] = type(self)()
        return self[key]

rawDescription: Creating a 3-dimensional OrderedDict with natural subscripting


code:
# if python 2.x
#from itertools import tee, izip
# if python 3
from itertols import tee
# http://docs.python.org/2/library/itertools.html#recipes
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    # if python 2.x
    #return izip(a, b)
    return zip(a, b)

new_data = []
with open('temp100.txt', 'r') as sqFile:
    with open("new_temp100.txt", "w") as new:
        for sLine, edit_line  in pairwise(seqFile):
            tmp_str = sLine + editLine[:20]
            if len(sLine) > 20:
                tmp_str = tmp_str + '/n'
            new.write(tmp_str)

rawDescription: performance - python text file edit 2GB files

description: Performance - python text file edit 2GB files


code:
from pandas.io.packers import pack
import cPickle as pkl
df = pd.DataFrame(np.random.rand(1000, 100))

rawDescription: What is the fastest way to serialize a DataFrame besides to_pickle?