rawDescription: python union of multiple ranges

description: Python union of multiple ranges


code:
import matplotlib.pyplot as plt
from matplotlib import ticker

rawDescription: How to chart times (mm:ss) in Matplotlib (formatting output values)

description: Chart times (mm:ss) in Matplotlib (formatting output values)


code:
# This SConstruct orchestrates building 3 subdirs

import os

subdirs = ['libfoo_subrepo', 'barapp_subrepo', 'test']
env = Environment()

for subdir in subdirs:
    SConscript(os.path.join(subdir, 'SConscript'), exports = ['env'])

rawDescription: Real Hierarchical Builds with SCons?

description: Real Hierarchical Builds with SCons


code:
df = pd.concat(df_all, axis=1).dropna(axis=0)
print(df)

rawDescription: Looping through df dictionary in order to merge df's in Pandas


code:
array = tuple(range(5))
Tuple.self_reference(array, 1)
array
Tuple.self_reference(array, 3)
array

rawDescription: Building Self-Referencing Tuples

description: Building Self-Referencing Tuples


code:
[['temperature_Resu09_les_spec_r0.0450.0'],
 ['temperature_Resu05_les_spec_r0.0300.0',
  'temperature_Resu06_les_spec_r0.0300.0',
  'temperature_Resu07_les_spec_r0.0300.0',
  'temperature_Resu08_les_spec_r0.0300.0',
  'temperature_Resu09_les_spec_r0.0300.0'],
 ['temperature_Resu05_les_spec_r0.0400.0',
  'temperature_Resu06_les_spec_r0.0400.0',
  'temperature_Resu07_les_spec_r0.0400.0',
  'temperature_Resu08_les_spec_r0.0400.0',
  'temperature_Resu09_les_spec_r0.0400.0'],
 ['temperature_Resu05_les_spec_r0.0450.0',
  'temperature_Resu06_les_spec_r0.0450.0',
  'temperature_Resu07_les_spec_r0.0450.0',
  'temperature_Resu08_les_spec_r0.0450.0'],
 ['temperature_Resu05_les_spec_r0.0350.0',
  'temperature_Resu06_les_spec_r0.0350.0',
  'temperature_Resu07_les_spec_r0.0350.0',
  'temperature_Resu08_les_spec_r0.0350.0',
  'temperature_Resu09_les_spec_r0.0350.0']]

rawDescription: Python: Group a list of file names according to common name identifier


code:
for elt in removal(list_with_files, remove_list):
  print(elt)

rawDescription: remove certain files in a list matching a pattern

description: Remove certain files in a list matching a pattern


rawDescription: Pandas groupby: How to get a union of strings


code:
class Filter(object):
    def __init__(self, filter_name=None):
         self.filter_name = filter_name

    def __call__(self, function):
         my_jinja_env.filters[self.filter_name or function.__name__] = function
         return function

@Filter()
def i_love_you(name):
    ''' say I love you to the name you entered.
    usage: {{ "John" | i_love_you }} => "I Love You, John!"'''

    return "I Love You, %s!" %name

rawDescription: Python lambdas in Jinja2

description: Python lambdas in Jinja2


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

# $ pip install hachoir-{core,parser,metadata}
from hachoir_core.cmd_line import unicodeFilename
from hachoir_core.i18n import getTerminalCharset
from hachoir_metadata import extractMetadata
from hachoir_parser import createParser

filename = sys.argv[1]
charset = getTerminalCharset()
filename, real_filename = unicodeFilename(filename, charset), filename
parser = createParser(filename, real_filename=real_filename)
metadata = extractMetadata(parser)
print("Duration (hh:mm:ss.f): %s" % metadata.get('duration'))

rawDescription: How to get .avi files length

description: Get .avi files length


code:
timeit.timeit('itertools.repeat(0, 10)', 'import itertools', number = 1000000)
timeit.timeit('[0] * 10', 'import itertools', number = 1000000)

rawDescription: Create List of Single Item Repeated n Times in Python


code:
def process(fin):
    for line in fin:
        pass # do something

try:
    with gzip.open(phrasetablefile, 'rb') as fin:
        process(fin)
except:
    with open(phrasetablefile, 'rb') as fin:
        process(fin)

rawDescription: Checking for gzip or plain text and reading the file without checking extensions - python


code: split('43 mark the 2nd', ' ')

rawDescription: How can I make this custom filter work in django?

description: Make this custom filter work in django


rawDescription: Python - quick interpolate by left value


code:
import codecs
codecs.BOM_UTF32

rawDescription: Python 3: Demystifying encode and decode methods


code: df_negAssets.index[2] == 59

rawDescription: Map a row index back to the original indexed dataframe


code:
coords = (lat, long)
query_string = "distance(location, geopoint(%s, %s)) < 500" % (lat, long)
index.search(query.string)

rawDescription: Finding all the locations in a given radius using Google App Engine GQL


code:
import numpy as np
import MySQLdb
import matplotlib.pyplot as plt # This is meant for scripts
# import pylab as pl # This is meant for interactive sessions;
import operator

conn = MySQLdb.connect(
    host="localhost",
    user="root",
    passwd="root",
    db="myproject")

with conn:
    cur = conn.cursor()

    #will fetch all recoreds called monoiso field
    cur.execute("SELECT monoiso FROM pmass_selectedion")
    rows = cur.fetchall()

monoisos = [row[0] for row in rows]

# Make a histogram of `monoisos` with 50 bins.
n, bins, histpatches = plt.hist(monoisos, 50, facecolor = 'green')
plt.show()

rawDescription: Scatter plot of 10k record extracted from database


code: self._input_data = tf.placeholder(tf.int32, [batch_size, num_steps])

rawDescription: what's the difference between two implentations of RNN in tensorflow

description: What's the difference between two implentations of RNN in tensorflow


code:
from ast import literal_eval
s = set([10, 20, 30])
lis = str(list(s))
set(literal_eval(lis))

rawDescription: Python convert set to string and vice versa


rawDescription: Why does this script print an extraneous 'none' in the output


code: self.assertTrue(all(is_number(v) for v in d.itervalues()))

rawDescription: Check numeric values from a dictionary by unit test


rawDescription: Python How to modify Y value in a list of X,Y points for lines


rawDescription: How can I check the value of a DNS TXT record for a host?

description: Check the value of a DNS TXT record for a host


code:
def show_frame(self, cont):
    frame = self.frames[cont]
    frame.tkraise()
    try:
        frame.postupdate()
    except AttributeError:
        pass

rawDescription: Tkinter focus_set and focus_force not working as expected


code: return min(S) # 'max' would also work here

rawDescription: Python | Access the sole element of a set


code:
a=np.column_stack((id,x,y))
Y=np.zeros(a.shape[0], dtype=dt)
Y['ID'] = a[:,0]
Y['Shape'] = a[:,1:]
Y

rawDescription: Methods of creating a structured array

description: Methods of creating a structured array


rawDescription: Capture live network traffic from a virtual machine using python


code: zip(*lst)

rawDescription: Reshape list of lists based on position of the element


code:
df
grouped = df.groupby('minute').sum().sort('values', ascending=False)
grouped.index[:2]
grouped.index[:2].tolist()

rawDescription: Python - Get group names from aggregated results in pandas


rawDescription: Python, RuntimeError: dictionary changed size during iteration


code:
def version(relayws):
    return map(int, relayws.split('_')[1].replace('-', '.').split('.'))

rawDescription: Python max() function doesnt give correct results when two strings are compared


code:
from datetime import datetime

dt = datetime.strptime('Apr 17, 2016 02:00 AM', '%b %d, %Y %I:%M %p')
if dt < datetime.now():
    print('{} alread past'.format(dt))

rawDescription: datetime comparison with string?

description: Datetime comparison with string


code:
for item in diff:
    print(item)

7.39
4.38

rawDescription: compare list elements

description: Compare list elements


code:
from re import match
data = ['text', 'foo2', 'foo1', 'sample']
indeces = (i for i,val in enumerate(data) if match('foo', val))

rawDescription: How to find an index of an item in a list, searching the item with a regular expression in Python?

description: Find an index of an item in a list, searching the item with a regular expression in Python


code:
import timeit

kabie=("isplit_kabie",
"""
import itertools
def isplit_kabie(iterable,splitters):
    return [list(g) for k,g in itertools.groupby(iterable,lambda x:x in splitters) if not k]
""" )

ssplit=("ssplit",
"""
def ssplit(seq,splitters):
    seq=list(seq)
    if splitters and seq:
        result=[]
        begin=0
        for end in range(len(seq)):
            if seq[end] in splitters:
                if end > begin:
                    result.append(seq[begin:end])
                begin=end+1
        if begin<len(seq):
            result.append(seq[begin:])
        return result
    return [seq]
""" )

ssplit2=("ssplit2",
"""
def ssplit2(seq,splitters):
    seq=list(seq)
    if splitters and seq:
        splitters=set(splitters).intersection(seq)
        if splitters:
            result=[]
            begin=0
            for end in range(len(seq)):
                if seq[end] in splitters:
                    if end > begin:
                        result.append(seq[begin:end])
                    begin=end+1
            if begin<len(seq):
                result.append(seq[begin:])
            return result
    return [seq]
""" )

emile=("magicsplit",
"""
def _itersplit(l, *splitters):
    current = []
    for item in l:
        if item in splitters:
            yield current
            current = []
        else:
            current.append(item)
    yield current

def magicsplit(l, splitters):
    return [subl for subl in _itersplit(l, *splitters) if subl]
""" )

emile_improved=("magicsplit2",
"""
def _itersplit(l, *splitters):
    current = []
    for item in l:
        if item in splitters:
            if current:
                yield current
                current = []
        else:
            current.append(item)
    if current:
        yield current

def magicsplit2(l, splitters):
    if splitters and l:
        return [i for i in _itersplit(l, *splitters)]
    return [list(l)]
""" )

karl=("ssplit_karl",
"""
def ssplit_karl(original,splitters):
    indices = [i for (i, x) in enumerate(original) if x in splitters]
    ends = indices + [len(original)]
    begins = [0] + [x + 1 for x in indices]
    return [original[begin:end] for (begin, end) in zip(begins, ends)]
""" )

ryan=("split_on",
"""
from functools import reduce
def split_on (seq, delims, remove_empty=True):
    '''Split seq into lists using delims as a delimiting elements.

    For example, split_on(delims=2, list=xrange(0,5)) yields [ [0,1], [3,4] ].

    delims can be either a single delimiting element or a list or
    tuple of multiple delimiting elements. If you wish to use a list
    or tuple as a delimiter, you must enclose it in another list or
    tuple.

    If remove_empty is False, then consecutive delimiter elements or delimiter elements at the beginning or end of the longlist'''
    delims=set(delims)
    def reduce_fun(lists, elem):
        if elem in delims:
            if remove_empty and lists[-1] == []:
                # Avoid adding multiple empty lists
                pass
            else:
                lists.append([])
        else:
            lists[-1].append(elem)
        return lists
    result_list = reduce(reduce_fun, seq, [ [], ])
    # Maybe remove trailing empty list
    if remove_empty and result_list[-1] == []:
        result_list.pop()
    return result_list
""" )

cases=(kabie, emile, emile_improved, ssplit ,ssplit2 ,ryan)

data=(
    ([1, 4, None, 6, 9, None, 3, 9, 4 ],(None,)),
    ([1, 4, None, 6, 9, None, 3, 9, 4 ]*5,{None,9,7}),
    ((),()),
    (range(1000),()),
    ("Split me",('','')),
    ("split me "*100,' '),
    ("split me,"*100,' ,'*20),
    ("split me, please!"*100,' ,!'),
    (range(100),range(100)),
    (range(100),range(101,1000)),
    (range(100),range(50,150)),
    (list(range(100))*30,(99,)),
    )

params="seq,splitters"

def benchmark(func,code,data,params='',times=10000,rounds=3,debug=''):
    assert(func.isidentifier())
    tester = timeit.Timer(stmt='{func}({params})'.format(
                                func=func,params=params),
                          setup="{code}\n".format(code=code)+
            (params and "{params}={data}\n".format(params=params,data=data)) +
            (debug and """ret=repr({func}({params}))
print({func}.__name__.rjust(16),":",ret[:30]+"..."+ret[-15:] if len(ret)>50 else ret)
                       """.format(func=func,params=params)))
    results = [tester.timeit(times) for i in range(rounds)]
    if not debug:
        print("{:>16s} takes:{:6.4f},avg:{:.2e},best:{:.4f},worst:{:.4f}".format(
            func,sum(results),sum(results)/times/rounds,min(results),max(results)))

def testAll(cases,data,params='',times=10000,rounds=3,debug=''):
    if debug:
        times,rounds = 1,1
    for dat in data:
        sdat = tuple(map(repr,dat))
        print("{}x{} times:".format(times,rounds),
              ','.join("{}".format(d[:8]+"..."+d[-5:] if len(d)>16 else d)for d in map(repr,dat)))
        for func,code in cases:
            benchmark(func,code,dat,params,times,rounds,debug)

if __name__=='__main__':
    testAll(cases,data,params,500,10)#,debug=True)

rawDescription: Split a list into nested lists on a value


code:
import time
mouse = Mouse()
mouse.click((20, 10), "left")
time.sleep(2.0)

mouse.click((100, 100), "right")

rawDescription: ctypes mouse_events

description: Ctypes mouse_events


code:
def count_substitutions(s1, s2):
    return sum(x != y for (x, y) in zip(s1, s2))

rawDescription: Python comparing two strings

description: Python comparing two strings


code:
import datetime
def endtime_sort_key(item):
    return datetime.datetime.strptime(item['endtime'], "%Y-%m.%d %H:%M:%S")

used_names = set()
final_result = []
for k, g in itertools.groupby(query_result, key=lambda x: x['item']):
    final_result.append(sorted(g, key=endtime_sort_key, reverse=True)[0])

rawDescription: python sort dictionary by key and list by another key

description: Python sort dictionary by key and list by another key


code:
class bla(str):
     def toto(self): return 1
bla('2123').toto()

rawDescription: Adding method to Python's NoneType

description: Adding method to Python's NoneType


code:
@asyncio.coroutine
def main():
    yield from coro1()
    yield from coro2()
    yield from asyncio.sleep(5)

asyncio.get_event_loop().run_until_complete(main())

rawDescription: What should I decorate with @asyncio.coroutine for async operations?


code:
#!/usr/bin/env python3

from ftplib import FTP
from optparse import OptionParser

def brute(host, username, password):
    try:
        ftp = FTP(host)
        ftp.login(username, password)
        ftp.retrlines('LIST')
        print ('Ftp server connected using the provided username "' + username + '" and     password "' + password + '"')
        ftp.quit()
    except:
        print ('Could not connect to the ftp server using the provided username "' + username + '" and password "' + password + '"')

def main():
    parser = OptionParser(usage="usage: python3 <program name>.py -t <target IP> -p <password file>")
    parser.add_option("-t", type="string",
                      help="Enter target host IP",
                      dest="targetHost")
    parser.add_option("-p", type="string",
                      help="Enter password file",
                      dest="passFile")
    (options, args) = parser.parse_args()

    if not options.passFile or not options.targetHost:
        parser.print_help()
    else:
        with open(options.passFile) as f:
            data = [ line.strip().split(':') for line in f ]

        for username, password in data:
            brute(options.targetHost, username, password)

main()

rawDescription: Python FTP brute forcing

description: Python FTP brute forcing


code:
def sq(n):
    """
    Return the square of n.
    """
    return n * n

rawDescription: What is the standard Python docstring format?


code:
def expander(s):
    import re
    from itertools import product
    pat = r"\(([^)]*)\)"
    pieces = re.split(pat, s)
    pieces = [piece.split("|") for piece in pieces]
    for p in product(*pieces):
        yield "".join(p)

rawDescription: Python regex string expansion

description: Python regex string expansion


code:
import itertools
for c in itertools.product(' -', repeat=2): print ('X%sY%s2' % c).replace(' ', '')

rawDescription: Combine elements of a list with all possible separators


rawDescription: Getting Tag Names with BeautifulSoup

description: Getting Tag Names with BeautifulSoup


code:
from django.db.models import Sum

total_m2 = Investment.objects.filter(plan = plan).order_by('financial_institution').filter(maturity_date__year=next_year).aggregate(Sum('maturity_amount'))

rawDescription: Handling NoneType when using addition

description: Handling NoneType when using addition


code:
newimg = img.copy()
newimg[labels != 0, :] = 255

rawDescription: numpy efficient array multiplication

description: Numpy efficient array multiplication


rawDescription: How to remove all whitespaces in string and get exactly one number?( Python)

description: Remove all whitespaces in string and get exactly one number?( Python)


code: pts = unit_coords + llcorners[corner_indices]

rawDescription: Pythonic way to generate random uniformly distributed points withing hollow square lamina


code:
def post(self):
    return [self.request.get(var) for var in ['var1', 'var2']]

rawDescription: Using a string as a variable name

description: Using a string as a variable name


code:
q = quad(p, q, r)
q(x)

rawDescription: Should I use a class? (Python)

description: Should I use a class? (Python)


code:
def getModel(db_table):
  class MyClassMetaclass(models.base.ModelBase):
    def __new__(cls, name, bases, attrs):
      name += db_table
      return models.base.ModelBase.__new__(cls, name, bases, attrs)

  class MyClass(models.Model):
    __metaclass__ = MyClassMetaclass

    class Meta:
      db_table = db_table

  return MyClass

rawDescription: Single Django model, multiple tables?

description: Single Django model, multiple tables


code:
def your_func(input_file):
    char_value = {}
    with open('keyvalue.txt', 'r') as f:
        for row in f:
            char_value[row.split()[0]] = row.split()[1]

    res = []
    with open(input_file) as f:
        for row in f:
            for word in row.split():
                for c in word:
                    # Little trick to append only if key exists
                    c in char_value and res.append(char_value[c])
    return '*'.join(res)

print(your_func("input.txt"))

# >>> 123*123*456*456*456*789*123*456*789

rawDescription: assign numeric values from a text to characters in a string using python 2

description: Assign numeric values from a text to characters in a string using python 2


code:
grouped = (lst[i:i+3] for i in range(0,len(lst),3))

with open("output.txt","w") as f:
    for tup in grouped:
        f.write("\t".join(tup)+"\n")

rawDescription: Outputting appeded list of items into a file


code:
from functools import partial

def test(MainWindow):
    MainWindow.textEdit.append('button pushed')

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)

        # blah blah

        self.textEdit = QtGui.QTextEdit(self)
        self.pushButton = QtGui.QPushButton(self)
        test_func = partial(test, MainWindow)  # Calling test_func(arg) actually calls test(MainWindow, arg)
        self.pushButton.clicked.connect(test_func)

rawDescription: PyQT: How to access MainWindow widgets from an external function?


code:
replacements = {'zero':'0', 'temp':'bob', 'garbage':'nothing'}

with open('path/to/input/file') as infile, open('path/to/output/file', 'w') as outfile:
    for line in infile:
        for src, target in replacements.iteritems():
            line = line.replace(src, target)
        outfile.write(line)

rawDescription: replacing text in a file with Python

description: Replacing text in a file with Python


code:
import csv

with open('scorefile.csv', 'r') as csv_file:
    rows = csv.reader(csv_file)

rawDescription: Sorting a csv file alphabetically on Python


code:
numcols, numrows = 300, 300
xi = np.linspace(data.Lon.min(), data.Lon.max(), numrows)
yi = np.linspace(data.Lat.min(), data.Lat.max(), numcols)
xi, yi = np.meshgrid(xi, yi)

x, y, z = data.Lon.values, data.Lat.values, data.Z.values
points = np.vstack((x,y)).T
values = z
wanted = (xi, yi)
zi = griddata(points, values, wanted)

rawDescription: use of matlab meshgrid

description: Use of matlab meshgrid


code:
for line_no, line in enumerate(names):
    if line_no >= amount:  # if we've seen enough already
        break  # stop looping
    print(line)  # otherwise show the current line

rawDescription: For range iteration

description: For range iteration


code: destfile.write('{} resolves to {}'.format(ip, hostname))

rawDescription: How to split the return of socket,gethostbyaddr and write to file?

description: Split the return of socket,gethostbyaddr and write to file


code:
class ConcreteTestCase(ProjectCoreViewCase):
    def setUp(self):
        super(ConcreteTestCase, self).setUp(
            SQLALCHEMY_DATABASE_URI='your_test_specific_connection_uri',
        )

rawDescription: Flask initialisation for unit test and app


code:
with open(filename, "r") as fin:
    line_list = [line.split() for line in fin]

rawDescription: Why aren't the lists populating in this code?


code:
possibleusershipplaces = {1,2,3,4,5,6,7,8,9}
while True:
    try:
        players_chosen_hit = int(input('Where do you want to try to hit the AI?: 1-9  '))
        if players_chosen_hit in possibleusershipplaces:
            break
    except ValueError:
        print("Invalid entry")

rawDescription: Python - Error Checking

description: Python - Error Checking


code:
# use iteritems for Python2
# list comprehension
shared1 = [k for k, v in A.items() if B.get(k) == v]
# set comprehension
shared1 = {k for k, v in A.items() if B.get(k) == v}

rawDescription: Hashing or dictionaries for Venn diagram of dictionaries?


code:
from django.contrib.sitemaps import Sitemap
from blog.models import Entry

class BlogSitemap(Sitemap):
    changefreq = "never"
    priority = 0.5

    def items(self):
        return Entry.objects.filter(is_draft=False)

    def lastmod(self, obj):
        return obj.pub_date

rawDescription: Django 1.6: What should be the location of the sitemap.py file?


code:
arr.T.shape
arr.transpose(1, 2, 0).shape

rawDescription: How to save a 3 channel numpy array as image

description: Save a 3 channel numpy array as image


code:
from __future__ import print_function
import itertools

try:
    from winreg import *
except ImportError: # Python 2
    from _winreg import *

KEY_READ_64 = KEY_READ | KEY_WOW64_64KEY
ERROR_NO_MORE_ITEMS = 259

def iterkeys(key):
    for i in itertools.count():
        try:
            yield EnumKey(key, i)
        except OSError as e:
            if e.winerror == ERROR_NO_MORE_ITEMS:
                break
            raise

def itervalues(key):
    for i in itertools.count():
        try:
            yield EnumValue(key, i)
        except OSError as e:
            if e.winerror == ERROR_NO_MORE_ITEMS:
                break
            raise

def val2addr(val):
    return ':'.join('%02x' % b for b in bytearray(val))

NET_UNMANAGED = (r"SOFTWARE\Microsoft\Windows NT\CurrentVersion"
                 r"\NetworkList\Signatures\Unmanaged")

def printNets(keystr=NET_UNMANAGED):
    key = OpenKey(HKEY_LOCAL_MACHINE, keystr, 0, KEY_READ_64)
    print('\n[*] Networks You Have Joined.')
    for guid in iterkeys(key):
        netKey = OpenKey(key, guid)
        netName, macAddr = '', ''
        for name, data, rtype in itervalues(netKey):
            if name == 'FirstNetwork':
                netName = data
            elif name == 'DefaultGatewayMac':
                macAddr = val2addr(data)
        if netName:
            print('[+]', netName, macAddr)
        CloseKey(netKey)
    CloseKey(key)

rawDescription: How do I use Python to retrieve Registry values?

description: Use Python to retrieve Registry values


code:
class C(object):
    prop = FixedValueProperty('abc')

o = C()
o.prop
C.prop
C.prop.value
type(o).prop.value

rawDescription: python attribute lookup without any descriptor magic?

description: Python attribute lookup without any descriptor magic


code:
for k,v in items.items():
    for i in v:
        if i not in newdic:
              newdic[i] = set()
        newdic[i].add(k)

rawDescription: Reversing logic of a product-country mapping


rawDescription: Learning Python, Why isn't this working?

description: Learning Python, Why isn't this working


rawDescription: Choosing particular rows from pandas dataframe


code:
lines = ["<KeyPlat> = 1228.3", "<KeyTran> = 690.3", "<KeyTotal> = 1918.6"]

f1 = open("testtable.txt", "w")

for line in lines:
    values = line.split(' ')
    if "KeyPlat" in values[0]:
        print("{0:10s}{1:10s}".format("Platform", values[2]), file=f1)
    elif "KeyTran" in values[0]:
        print("{0:10s}{1:10s}".format("Transfers", values[2]), file=f1)
    elif "KeyTotal" in values[0]:
        print("{0:10s}{1:10s}".format("Total", values[2]), file=f1)

f1.close()

rawDescription: Reading and writing text files

description: Reading and writing text files


rawDescription: overwrite HTML object with ajax and javascript

description: Overwrite HTML object with ajax and javascript


code:
chars = '\u4132'.encode('utf-16be')
chars
chars = bytes('\u4132', 'utf-16be')
chars  # Just the same.
chars[0]
'%x' % chars[0]
hex(chars[0])
['%x' % c for c in chars]
[hex(c) for c in chars]

rawDescription: getting bytes from unicode string in python

description: Getting bytes from unicode string in python


rawDescription: Inverse lookup in pandas

description: Inverse lookup in pandas


rawDescription: Scrapy only scrapes the first start url in a list of 15 start urls


code:
def createFreeSpaces(first_byte, last_byte):
    return ['555555 0 0 "FREE: [{}.{}]"'.format(x,y) for x in range(first_byte + 1, last_byte) for y in range(8)]

rawDescription: Modifying this simple array creation function on a more Pythonic way


code:
def duplicates(the_list):
    return len(the_list)!=len(set(the_list))

rawDescription: Python 3.3: Birthday Probablity

description: Python 3.3: Birthday Probablity


code:
height = 2 * sin(math.radians(iso_angle)) * grid_length
width = 2 * cos(math.radians(iso_angle)) * grid_length

rawDescription: Basic trigonometry isn't working correctly in python


rawDescription: How to use argparse to let user make changes to output of existing program?

description: Use argparse to let user make changes to output of existing program


code:
type(difference)  # <class 'datetime.timedelta'>

difference.days  # 351
difference.seconds  # 40732 seconds + 351 days
difference.total_seconds()  # 30367132.648078 seconds total

rawDescription: Subtracting Months/Years

description: Subtracting Months/Years


code:
def update_state(f):
    c, s = dlg.Update(f, "Processing frame %d"%f)
    return c
self.engine.processMovie(progress=update_state)

rawDescription: usage of generators as a progression notifier

description: Usage of generators as a progression notifier


code:
print(result.reorder_levels([1,0],axis=0).sort(axis=0))
#              present  Data1  Data2
# Name1 Name2
# a     1            1     80      6
#       2            1     61      8
#       4            3     45      7
# b     2            1     61      8
#       4            5     45      7
#       8            1     30      3

rawDescription: Pandas: Merge or join dataframes based on column data?


code:
# main.py

from judge import Judge

j = Judge()
j.call()

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

description: Implement a pluginable class and register in different file


code:
def most_common(self, n=None):
    '''List the n most common elements and their counts from the most
    common to the least.  If n is None, then list all element counts.

    >>> Counter('abcdeabcdabcaba').most_common(3)
    [('a', 5), ('b', 4), ('c', 3)]

    '''
    # Emulate Bag.sortedByCount from Smalltalk
    if n is None:
        return sorted(self.iteritems(), key=_itemgetter(1), reverse=True)
    return _heapq.nlargest(n, self.iteritems(), key=_itemgetter(1))

rawDescription: How does collections.Counter achieve fast sort time?


rawDescription: Finding count of duplicate values and ordering in a Pandas dataframe


code:
import itertools
import plyvel

db = plyvel.DB('my-database', create_if_missing=True)
cnt = itertools.count(1)  # start counting at 1
for word in reader:
    key = word.encode('utf-8')
    value = db.get(key)
    if value is not None:
        # We've seen this word before.
        idx = int(value)
    else:
        # We've not seen this word before.
        idx = next(cnt)
        db.put(key, str(idx).encode('ascii'))

    print(idx)

rawDescription: Faster than dict way to assign index to strings python


rawDescription: XML Tree parsing with condition in Python


code:
def join_dates(l):
    date = None
    for t in l:
        if len(t) == 1:
            date = t
        else:
            yield t + date

rawDescription: Joining specific tuples within a list

description: Joining specific tuples within a list


code:
# Works
test.testWcharParam(u'test')

# Doesn't work
test.testWcharParam('test')

rawDescription: Exporting const wchar_t* c++ function with Boost Python


rawDescription: Using a comparator function to sort

description: Using a comparator function to sort


code: s = s.reindex(bd)

rawDescription: Extend index on business day range

description: Extend index on business day range


code:
import random

x = zip(list1, list2)
random.shuffle(x)

list1, list2 = zip(*x)

rawDescription: Randomize two lists by maintaining the sequence of mapping of values in the lists


code: words = input_string.split()

rawDescription: How to preserve and reverse a string?

description: Preserve and reverse a string


rawDescription: How to sort a structured list of stock data for later access in Python?

description: Sort a structured list of stock data for later access in Python


code:
STATICFILES_STORAGE = 'myapp.s3utils.StaticRootS3BotoStorage'
DEFAULT_FILE_STORAGE = 'myapp.s3utils.MediaRootS3BotoStorage'

rawDescription: Django 1.8 Cache busting + Amazon S3

description: Django 1.8 Cache busting + Amazon S3


code:
total_count = Answer.objects.filter(option__question=question_id).count()
perc_dict = { }
for o in q.option_set.all():
    cnt = Answer.objects.filter(option=o).count()
    perc = cnt * 100 / total_count
    perc_dict.update( {o.value: perc} )

#after this the perc_dict will have percentages for all options that you can pass to template.

rawDescription: django: calculate percentage based on object count

description: Django: calculate percentage based on object count


rawDescription: How Do I Change the Data Stored in a Text File in Python?


rawDescription: is it possible to add some new syntax in javascript?

description: Is it possible to add some new syntax in javascript


code:
current = 247
result_list = [current]

while current != 1:
    if current % 2 == 0:
        current /= 2
    else:
        current = current * 3 + 1

    result_list.append(current)

rawDescription: Is it safe to append to a list during iteration if I want to iterate over the added value?


code:
import Tkinter

root = Tkinter.Tk()
defaultbg = root.cget('bg')
Tkinter.Button(root,text="Press me!", bg=defaultbg).pack()
root.mainloop()

rawDescription: Default window colour Tkinter and hex colour codes


code:
import numpy
import pickle
import pygame

base = (pickle.load(open("g.pickle"))+1) * 128
base = base[..., None].repeat(3, -1).astype("uint8")

surface = pygame.surfarray.make_surface(base)

screen = pygame.display.set_mode((100, 100))
screen.blit(surface, (0, 0))
pygame.display.flip()

rawDescription: How can I tranform this (100, 100) numpy array into a grayscale sprite in pygame?

description: Tranform this (100, 100) numpy array into a grayscale sprite in pygame


code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from mpl_toolkits.mplot3d import Axes3D

nrow=30
ncol=100

X, Y = np.meshgrid(ncol, nrow)
Y3d, X3d = np.mgrid[0:Y, 0:X]
Z = np.sin(Y3d/Y)*np.sin(X3d/X)

fig = plt.figure()
for i in range(4):
    ax = fig.add_subplot(2,2,i,projection='3d')
    ax.auto_scale_xyz([0, 100], [0, 30], [0, 0.2])

    surf = ax.plot_surface(X3d, Y3d, Z, cmap='autumn', cstride=2, rstride=2)
    ax.set_xlabel("X-Label")
    ax.set_ylabel("Y-Label")
    ax.set_zlabel("Z-Label")
    ax.pbaspect = [1., .33, 0.25]
    ax.dist  = 7

rawDescription: Move 3D plot to avoid clipping by margins


code:
def twopointcorr(x,y,s,dr):
    width = np.max(x)-np.min(x)
    height = np.max(y)-np.min(y)

    n = len(x)

    maxR = np.sqrt((width/2)**2 + (height/2)**2)

    r = np.arange(0, maxR, dr)
    corrfun = r*0
    rw = r*0
    for j in range(0, n):
        hypot = np.sqrt((x[j]-x)**2+(y[j]-y)**2)
        ind = [np.abs(r-h).argmin() for h in hypot]
        for k, v in enumerate(ind):
            if j==k:
                continue
            corrfun[v] += (s[k]-s[j])**2
            rw[v] += 1

    return r, corrfun, rw

rawDescription: Vectorizing for loop with repeated indices in python


code: Template("a small number : ${number}e6").substitute(number=5)

rawDescription: python : template var without space

description: Python : template var without space


code: print (result.sectionTwo.base)

rawDescription: Cannot parse correctly this file with pyparsing


rawDescription: Python binary search always returns target not found value


code:
p = re.compile(r"[A-Z]{3}[a-z][A-Z]{3}")

word = "".join(re.findall(p, 'udkehtEOajidfEEfjaiERBdEHY alijdfilaOEPvOEG'))

rawDescription: How to find a group of consecutive leters and check cases with python re

description: Find a group of consecutive leters and check cases with python re


code:
thing1_x
thing1_y

rawDescription: Chart with lots of (but varied number of) Y values for each X value


code:
for table in soup.find('table', class_='tabelle_grafik'):
    count = len(table.find('a'))

rawDescription: BeautifulSoup, how to stop executing at certain tag


rawDescription: Python functools partial efficiency

description: Python functools partial efficiency


code:
from datetime import datetime
from dateutil.relativedelta import relativedelta

def get_5_previous_year_months(a_day):
    """Returns a list of year, month tuples for the current and previous
    5 months relative to a_day"""
    current_year, current_month = a_day.year, a_day.month
    first_of_month = datetime(current_year, current_month, 1)
    previous_months = (first_of_month - relativedelta(months = months)
            for months in range(0, 5))
    return ((pm.year, pm.month) for pm in previous_months)

def get_current_and_5_previous_months():
    return get_5_previous_year_months(datetime.today())

rawDescription: Creating and rendering structure with years and months in django


code:
for name, info in data['objects'].items():
    print('{}: {}'.format(name, info['hash']))

rawDescription: How do I go about parsing the contents of a large JSON file with patterns as opposed to having to manually access each entry?

description: Go about parsing the contents of a large JSON file with patterns as opposed to having to manually access each entry


code:
if line.split()[2].find('-') != -1:
    fout.write(line)

rawDescription: remove rows which have a specific attribute less than or equal to 0

description: Remove rows which have a specific attribute less than or equal to 0


code: subprocess.call(["bash", "-c", " ".join("my_shell_script.sh",my_argument)])

rawDescription: How to call a distant shell script from a python file?

description: Call a distant shell script from a python file


code:
class Month():
    pass

a = Month()
a.df = 2

rawDescription: Named Stuctured Data storage in Python

description: Named Stuctured Data storage in Python


code:
a = ["No", 1, "No"]
[0 if i == "No" else i for i in a]

rawDescription: How to "else" in a list comprehension when the condition is non-boolean?


code:
a = np.random.randint(5, size=10)
a
unq, unq_inv, unq_cnt = np.unique(a, return_inverse=True, return_counts=True)
np.split(np.argsort(unq_inv), np.cumsum(unq_cnt[:-1]))
unq

rawDescription: How to get lists of indices to unique values efficiently?

description: Get lists of indices to unique values efficiently


code:
x = range(20)
y = 2*np.array(x)
pylab.scatter(x, y, color='rgb')

rawDescription: How do I alternate between different marker colors on matplotlib?

description: Alternate between different marker colors on matplotlib


code:
from Tkinter import *
import tkMessageBox

t = Tk()

def button1_click():
    tkMessageBox.showinfo("Message", "Bang!")

def button2_click():
    button1.invoke()

button1 = Button(t, text="Button 1", command=button1_click)
button1.pack()

button2 = Button(t, text="Button 2", command=button2_click)
button2.pack()

mainloop()

rawDescription: How to execute python tkinter button without clicking mouse

description: Execute python tkinter button without clicking mouse


code:
from pyparsing import *

text = """C:/data/my_file.txt.c:10:0x21:name1:name2:0x10:1:OK
C:/data/my_file2.txt.c:110:0x1:name2:name5:0x12:1:NOT_OK
// blah-de blah blah blah
./data/my_file3.txt.c:110:0x1:name2:name5:0x12:10:OK"""

# define an expression for your file reference
one_thing = Combine(
    oneOf(list(alphas.upper())) + ':/' +
    Word(alphanums + '_-./'))

# define a catchall expression for everything else (words of non-whitespace characters,
# excluding ':')
another_thing = Word(printables, excludeChars=':')

# define an expression of the two; be sure to list the file reference first
thing = one_thing | another_thing

# now use plain old pyparsing delimitedList, with ':' delimiter
list_of_things = delimitedList(thing, delim=':')

parser = OneOrMore(Group(list_of_things))

# ignore comments starting with double slash
parser.ignore(dblSlashComment)

# run it and see...
parser.parseString(text).pprint()

rawDescription: What is the best way to parse a colon separated string with pyparsing


code:
import requests
from bs4 import BeautifulSoup
response = requests.get('http://www.wunderground.com/history/airport/LAX/2002/1/1/DailyHistory.html')
soup = BeautifulSoup(response.content)
humidity = soup.find(text='Average Humidity')
next_cell = humidity.find_parent('td').find_next_sibling('td')
next_cell.string

rawDescription: How to use beautifulsoup when HTML element doesn't have a class name?

description: Use beautifulsoup when HTML element doesn't have a class name


code:
import numpy as np
with open('data', 'rb') as fin, open('out.csv', 'w+b') as fout:
    arr = np.genfromtxt(fin, usecols=(0,1,2), delimiter='', dtype=float)
    mask = (arr[:, 0] >= 0.807375) & (arr[:, 0] <= 0.807982)
    arr = arr[mask]
    np.savetxt(fout, arr, fmt='%g')

rawDescription: How to read only some lines from a large file in python?

description: Read only some lines from a large file in python


code:
#!/usr/bin/env python
import os
import time

while True:
  os.system('echo " foo" >> /Users/bob/foostore.txt')
  time.sleep(1)

rawDescription: Running Python in background on OS X

description: Running Python in background on OS X


code:
def run() :
    while True :
        task = q.get()
        # simulate processing time *before* actual reporting
        time.sleep(2)
        print('Just finished task number',task)
        q.task_done()

rawDescription: The correct implementation of queue.Queue when multithreading in Python


code:
import matplotlib.pyplot as plt

def main():
    # Make an example pie plot
    fig = plt.figure()
    ax = fig.add_subplot(111)

    labels = ['Beans', 'Squash', 'Corn']
    wedges, plt_labels = ax.pie([20, 40, 60], labels=labels)
    ax.axis('equal')

    make_image_map(fig, wedges, labels, 'temp.html')

def make_image_map(fig, wedges, labels, html_filename):
    """Makes an example static html page with a image map of a pie chart.."""
    #-- Save the figure as an image and get image size ------------------------
    # Be sure to explictly set the dpi when saving the figure
    im_filename = 'temp.png'
    fig.savefig(im_filename, dpi=fig.dpi)

    # Get figure size...
    _, _, fig_width, fig_height = fig.bbox.bounds

    #-- Get the coordinates of each wedge as a string of x1,y2,x2,y2... -------
    coords = []
    for wedge in wedges:
        xy = wedge.get_verts()

        # Transform to pixel coords
        xy = fig.get_transform().transform(xy)

        # Format into coord string and convert to <0,0> in top left...
        xy = ', '.join(['%0.2f,%0.2f' % (x, fig_height - y) for x, y in xy])
        coords.append(xy)

    #-- Build web page --------------------------------------------------------
    header = """
    <html>
    <body>
    <img src="{0}" alt="Pie Chart" usemap="#pie_map" width="{1}" height="{2}" />
    """.format(im_filename, fig_width, fig_height)

    # Make the image map
    map = '<map name="pie_map">\n'
    for label, xy in zip(labels, coords):
        href = 'http://images.google.com/images?q={0}'.format(label)
        area = '<area shape="poly" coords="{0}" href="{1}" alt="{2}" />'
        area = area.format(xy, href, label)
        map += '    ' + area + '\n'
    map += '</map>\n'

    footer = """
    </body>
    </html>"""

    # Write to a file...
    with file(html_filename, 'w') as outfile:
        outfile.write(header + map + footer)

if __name__ == '__main__':
    main()

rawDescription: how to create hyperlink in piechart

description: Create hyperlink in piechart


rawDescription: how to convert long date into time

description: Convert long date into time


code:
import csv

with open('output_file.csv', 'w') as f:
    csv.writer(f).writerows(time_deltas(infile))

rawDescription: How do I get my output in column format?

description: Get my output in column format


code:
f = F()
r = ode(f, jac)
r.integrate(tmax)
print(f.calls)

rawDescription: How to find the number of times the ODE's derivative is called in one invocation.

description: Find the number of times the ODE's derivative is called in one invocation.


code:
firstName, lastName, number = match.groups()[:3]
children = [c.strip() for c in match.group(4).strip().split('\n')]

rawDescription: reverse template with python

description: Reverse template with python


code:
import paho.mqtt.client as mqtt
import sys, tty, termios
## Publisher reads a keyboard input
def getch():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd,termios.TCSADRAIN, old_settings)
    return ch

##Publisher connects to MQTT broker
mqttc= mqtt.Client("python_pub")
mqttc.connect("iot.eclipse.org", 1883)
mqttc.loop_start()

while True:
    char= getch()
    mqttc.publish("Labbo/control", str(char))

rawDescription: Controlling Program with MQTT and Python


code:
def __repr__(self):
    return "OrderedSet(%r)" % self.list

rawDescription: How to use __repr__ to create new object from it?

description: Use __repr__ to create new object from it


code:
# python python.py
{'Images': ['01-some_image1.jpg', '02-some_image2.jpg'], 'Music': ['01-some_song1.mp3', '02-some_song2.mp3', '03-some_song3.mp3']}

rawDescription: Copying files to directories as specified in a file list with python


rawDescription: If everything is an object, why can't I add attributes to some?


code:
import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['text.usetex'] = True

x = np.arange(10000, 10011)
plt.plot(x)
plt.show()

rawDescription: standard form matplotlib -- change e to \times 10

description: Standard form matplotlib -- change e to \times 10


code:
with open('/Users/Chandlers_Mac/Downloads/Lab9-2.txt') as f,open("CTL.py", "w") as out:
    for line in f:
        out.write(line.title().rstrip()+"#\n")

rawDescription: How can I use the ".title() command to read a text file and capitalize every word in the file?

description: Use the ".title() command to read a text file and capitalize every word in the file


code: a.intersection(c) == c

rawDescription: Check if none of the multiple chars appears in string A?


code:
containers = Containers()
containers.add("Jason", "Antarctica", 56)
containers.add("Julio", "Dubai", 84)
containers.add("Mason", "Antarctica", 84)

rawDescription: How to have a linked list within a linked list in Python 3.x

description: Have a linked list within a linked list in Python 3.x


code:
b, c = [e[0] for e in zipped], [e[1] for e in zipped]
b
c

rawDescription: a list of tuples into two lists

description: A list of tuples into two lists


code:
import twill.shell
twill.shell.main()

rawDescription: How to log in to a website using installed twill?

description: Log in to a website using installed twill


code:
def cache_result(func):
    def wrapper(*args, **kwds):
        if not wrapper.cached:
            wrapper.value = func(*args, **kwds)
            wrapper.cached = True

        return wrapper.value

    wrapper.cached = False

    return functools.update_wrapper(wrapper, func)

rawDescription: Python method which computes the result only on the first call, without extra parameters


rawDescription: My Python for loop is giving the result None


rawDescription: Building block matrix from array in the correct order


code:
datetime.datetime.strptime("2004+88", "%Y+%j")
pd.to_datetime("2004+88", format="%Y+%j")

rawDescription: In python convert day of year to month and fortnight


rawDescription: List of integers with recurring odd numbers


code: def isin(a, b): return not (Counter(b) - Counter(a))

rawDescription: A list contains another list with repetition


code: type(name, bases, dict)

rawDescription: Python 3 dynamically assign base class

description: Python 3 dynamically assign base class


code:
import os , os.path
l = os.listdir()
for i in l:
    if i.endswith('.shp'):
        s1 = s.split('.')[0] + '.txt'

rawDescription: Script Loop through files in directory

description: Script Loop through files in directory


code:
df = DataFrame(np.random.rand(3,2), index = list('abc'), columns=list('df'))
df

rawDescription: How to retrieve all indexes from pandas DataFrame

description: Retrieve all indexes from pandas DataFrame


code:
s_b = set(tuple(y) for x in b for y in x)
s_a = set(tuple(x) for x in a)
s_a & s_b

rawDescription: Finding common elements from two lists of lists


rawDescription: Python Regular Expression searching backwards


rawDescription: Python GTK3 Treeview buttons

description: Python GTK3 Treeview buttons


code:
def getFileInfo(self):
    global filename
    filename = askopenfilename()
    return open(filename, mode="rb")

rawDescription: How to print a return value from a function inside a class

description: Print a return value from a function inside a class


code:
t = Test()
args = [1,2,3]
kwargs= {'a':1,'b':2}
t.f1(args,kwargs)
t.f1(kwargs)

rawDescription: how to make *args optional in python when **kwargs is given?

description: Make *args optional in python when **kwargs is given


code:
if x <= number < y:
    range_list.append(number)

rawDescription: Getting Python range() to recognise a decimal eg. 70.555555


rawDescription: pandas dataframe count uniques with respect to another column

description: Pandas dataframe count uniques with respect to another column


code: {i:(IDsums[itr],value_sums[itr]) for itr,i in enumerate(unqID)}

rawDescription: Summing data from array based on other array in Numpy


code: [s for s in mp.rcParams.keys() if 'linewidth' in s]

rawDescription: matplotlib linewidth when saving a PDF

description: Matplotlib linewidth when saving a PDF


code:
import timeit
timeit.timeit("''.join(reversed(myList))", 'from __main__ import myList')
timeit.timeit("''.join(myList[::-1])", 'from __main__ import myList')

rawDescription: How do I reverse items from a list and store it as a string in python

description: Reverse items from a list and store it as a string in python


code: existing_lines = [line.rstrip('\n') for line in open(filename, 'r')]

rawDescription: How to compare all lines in some file with another line?

description: Compare all lines in some file with another line


code:
df = DataFrame(np.random.randn(400,1),index=date_range('20130101',periods=400,freq='15T'))
df = df.take(df.index.indexer_between_time('9:00','10:00'))
df

rawDescription: Python: Delete specific timestamp index row (independently of date)


code:
francesco.py
def main():
    print("Hi, it is me, Francesco, keeping things simple.")

rawDescription: python setup.py sdist and custom setup keywords don't play together

description: Python setup.py sdist and custom setup keywords don't play together


code: return any((x0, y0) in v for v in A.itervalues())

rawDescription: how to check if a value exists in any array in python dictionary values?

description: Check if a value exists in any array in python dictionary values


rawDescription: A single CSV column includes commas - Python


code:
def getAllTheLetters(begin='a', end='z'):
    beginNum = ord(begin)
    endNum = ord(end)
    for number in xrange(beginNum, endNum+1):
        yield chr(number)

rawDescription: How do I iterate through a string in Python?

description: Iterate through a string in Python


code:
class Foo(object):
    def __init__(self, required_arg=None):
        if required_arg is None:
            raise ValueError("Expected non-None value for required_arg")

rawDescription: Is there an elegant and pythonic way to require inputs for a class constructor?


code:
arr = []
for item in data['clubs']:
    arr.append(item)
print(arr)

rawDescription: Inserting dict objects from a list with key-value pairs to a new list


code:
from urllib.parse import urljoin

result_links = [urljoin(url, tag['href']) for tag in results]

rawDescription: How to extract html links with a matching word from a website using python

description: Extract html links with a matching word from a website using python


code:
inp = "easyCrate0/CMS_TRACKER_SY1527_4".split("/")
common = []
for k in Dict:
    if all(x in Dict[k].values() for x in inp): # if all of the values match the user input, add it to common list
        common.append(k)
print (common)
['25465466', '470311412']

rawDescription: dictionary of dictionaries, get keys in common for values

description: Dictionary of dictionaries, get keys in common for values


code:
a = ['first', 'second', 'third']
a[0]
a[2]

rawDescription: What does '[0]' mean in Python?

description: What does '[0]' mean in Python


code:
data.set_index('date_stamp', inplace=True)
print (data.resample('5S').first())

rawDescription: Removing nth row in Pandas

description: Removing nth row in Pandas


code:
float('inf')
np.inf == float('inf')

rawDescription: How to represent inf or -inf in Cython with numpy?

description: Represent inf or -inf in Cython with numpy


code:
class CommentForm(forms.ModelForm):

  class Meta:

    model=Comment
    fields = ['for_blog']

rawDescription: Adding a comment for a blog post using UpdateView


rawDescription: Solving 5 Linear Equations in Python

description: Solving 5 Linear Equations in Python


code:
from collections import defaultdict

my_array = [
  {'name': 'top_parent', 'description': None, 'parent': None,
   'children': [{'name': 'child_one'},
                {'name': 'child_two'}]},
  {'name': 'child_one', 'description': None, 'parent': 'top_parent',
   'children': []},
  {'name': 'child_two', 'description': None, 'parent': 'top_parent',
   'children': [{'name': 'grand_child'}]},
  {'name': 'grand_child', 'description': None, 'parent': 'child_two',
   'children': []}
]

def build_nest_None(my_array):
    childmap = [(d['name'], set(x['name'] for x in d['children']) or None)
                for d in my_array]
    all_dicts = dict((name, kids and {}) for (name, kids) in childmap)
    results = all_dicts.copy()
    for (name, kids) in ((x, y) for x, y in childmap if y is not None):
        all_dicts[name].update((kid, results.pop(kid)) for kid in kids)
    return results

def build_nest_empty(my_array):
    all_children = set()
    all_dicts = defaultdict(dict)
    for d in my_array:
        children = set(x['name'] for x in d['children'])
        all_dicts[d['name']].update((x, all_dicts[x]) for x in children)
        all_children.update(children)
    top_name, = set(all_dicts) - all_children
    return {top_name: all_dicts[top_name]}

def build_bottom_up(my_array, use_None=False):
    all_dicts = defaultdict(dict)
    for d in my_array:
        name = d['name']
        all_dicts[d['parent']][name] = all_dicts[name]

    if use_None:
        for d in all_dicts.values():
            for x, y in d.items():
                if not y:
                    d[x] = None

    return all_dicts[None]

print(build_nest_None(my_array))
print(build_nest_empty(my_array))
print(build_bottom_up(my_array, True))
print(build_bottom_up(my_array))

rawDescription: Build nested tree-like dict from an array of dicts with children


code:
relation_values = thingie.relation_set.all().values_list('pk', flat=True)
profiles = Profile.objects.filter(relation__in=relation_values)

rawDescription: How Do I Select all Objects via a Relationship Model


code:
import functools

def counted(method):
    @functools.wraps(method)
    def wrapped(obj, *args, **kwargs):
        if hasattr(obj, 'count'):
            obj.count += 1
        else:
            obj.count = 1
        return method(obj, *args, **kwargs)
    return wrapped

rawDescription: Decorate methods per instance in Python

description: Decorate methods per instance in Python


rawDescription: what's faster: merging lists or dicts in python?

description: What's faster: merging lists or dicts in python


code: [list(g) for _,g in groupby(sorted(flat, key=len), key=len)]

rawDescription: Nest a flat list based on an arbitrary criterion


rawDescription: Selecting columns in numpy based on a Boolean vector


code:
new_d = {k: list(itemgetter(*srt_key)(v)) for k,v in d.items()}

print(new_d)

rawDescription: Python: sort lists in dictonary of lists, where one list is a key to sorting


code:
# Random input array
A = np.random.rand(4,3,1,5,1)

# Axis to be removed
rem_axis = 2

# Squeezed output array
A_squeezed = A.reshape(np.delete(A.shape,rem_axis))

rawDescription: numpy squeeze along an axis wont work

description: Numpy squeeze along an axis wont work


code:
eq = self.data == self.data.map
layerflags = eq.reshape(-1, 3).any(axis=0)
if layerflags.any():
    self.data[:] = self.map.data

rawDescription: increasing speed of python list operations and comparisons in a custom table class

description: Increasing speed of python list operations and comparisons in a custom table class


code:
matrix  = zip(*matrix)
matrix

rawDescription: ColSum of matrix from file python

description: ColSum of matrix from file python


rawDescription: how to replace an integer from a column for another integer contained in another file, python or awk

description: Replace an integer from a column for another integer contained in another file, python or awk


code: next([x] + [y] + z.split(';') for x, y, z in (item.split(';', 2) for item in lis) if x== 'plu' or y=='ean')

rawDescription: Fastest possible way to iterate through specific list in python


rawDescription: Dynamically add legends to matplotlib plots in python


code:
print(str(lang()))
print(str(port()))

rawDescription: def function returning wrong value (python 2.7)

description: Def function returning wrong value (python 2.7)


code:
start_job(hostname)
('', '')

rawDescription: Paramiko run script and exit

description: Paramiko run script and exit


code: foo = Table('foo', meta, autoload=True, autoload_with=engine)

rawDescription: Using existing table in sqlalchemy

description: Using existing table in sqlalchemy


code:
with lock:
   shared_int = 55 # Some write operation

rawDescription: How to share an integer between threads in python

description: Share an integer between threads in python


rawDescription: Python in vs iteration for nested list

description: Python in vs iteration for nested list


rawDescription: Displaying a floor division

description: Displaying a floor division


code:
class ListWithAView(list):

    class ListView(object):
        def __init__(self, list, start, stop, step):
            self.list  = list
            self.start = start
            self.stop  = stop
            self.step  = step

        def __iter__(self):
            for i in xrange(self.start, self.stop, self.step):
                yield self.list[i]

        def __len__(self):
            return (self.stop - self.start) / self.step

        def __getitem__(self, i):
            if isinstance(i, slice):
                return type(self)(self.list, (i.start or 0) + self.start,
                                  min(self.start + (i.stop or 0), self.stop),
                                  i.step * self.step if i.step else self.step)
            if isinstance(i, int) and i < len(self):
                return self.list[i+self.start]
            raise IndexError("invalid index: %r" % i)

        def __setitem__(self, i, v):
            if isinstance(i, int):
                self.list[i+self.start] = v
            else:
                raise IndexError("invalid index: %r" % i)

        def __repr__(self):
            return "<slice [%s:%s:%s] of list id 0x%08x>: %s" % (self.start, self.stop, self.step, id(self.list), self)

        def __str__(self):
            return str(list(self))

        __str__ = __repr__

    @property
    def view(self):
        return self.ListView(self, 0, len(self), 1)

rawDescription: Does Python have a shorthand notation for passing part of a list without copy?


code:
def f(a):
    return a+5

rawDescription: Python: have a user-defined function as an input while keeping the source-code inaccessible?


code:
out = open('csv.csv','w')
for i in listOfActors:
    line = ''
    for j in i:
        line = line+j+','
    out.write(line+'\n')

out.close()

rawDescription: Removing string unicode marks from list and having each item on separate line


rawDescription: HTTP GET packet sniffer in Scapy

description: HTTP GET packet sniffer in Scapy


code:
def function(file):
    lines = []
    for line in f:
        lines.append(line)
    return lines

rawDescription: How to Open a file through python

description: Open a file through python


code:
blah
bar

rawDescription: understanding a python descriptors example (TypedProperty)

description: Understanding a python descriptors example (TypedProperty)


rawDescription: Get matplotlib color from style palette

description: Get matplotlib color from style palette


code:
class MyTable(models.Model):
    class Meta:
        unique_together = (('key1', 'key2'),)

    key1 = models.IntegerField()
    key2 = models.IntegerField()

rawDescription: Composite primary key in django

description: Composite primary key in django


code:
str_to_int('012345')
str_to_int('fedcba')
int_to_str(74565)
int_to_str(-74566)

rawDescription: Python: Convert hex string to int and back


code:
def MyFunc(s, sub):
    n, m = len(sub), len(s)
    return sum(sub == s[i:i + n] for i in range(m - n + 1))

rawDescription: Improving Python function to count occurrences of a substring


rawDescription: How do I use optparse to just split the command-line arguments into options and positional args?

description: Use optparse to just split the command-line arguments into options and positional args


code:
O = np.where(mx == R, R, 0)
O

rawDescription: python: Faster local maximum in 2-d matrix

description: Python: Faster local maximum in 2-d matrix


code:
def parse(self, response):
    hxs = HtmlXPathSelector(response)
    xpath = '/html/body/div[1]/div[4]/div[2]/ul/li/a/@href'
    a_of_the_link = '/html/body/div[1]/div[4]/div[2]/ul/li/a/text()'
    for text, link in zip(hxs.select(a_of_the_link).extract(), hxs.select(xpath).extract()):
        yield Request(urljoin(response.url, link),  meta={'a_of_the_link': text},
                          headers={'User-Agent': USER_AGENT},
                          callback=self.parse_linkpage,
                          dont_filter=True)

rawDescription: I want to return the 'a' of the URL instead of the whole URL


code:
dates = (u'2015-06-23',u'2015-06-29')
df['received_date'] = pd.to_datetime(df['received_date'])
df = df.set_index('received_date')
return df[dates[0]:dates[1]]

rawDescription: how to compare unicode date u'2006-07-23' format and 25-06-15 08:42:43.830000000 PM using python pandas?

description: Compare unicode date u'2006-07-23' format and 25-06-15 08:42:43.830000000 PM using python pandas


code:
class Car:
    def __init__(self, make, year):
        self.make = make
        self.year = year

    def __repr__(self):
        return 'Car({!r}, {!r})'.format(self.make, self.year)

rawDescription: Class object display 'class object at ...'


code:
allrecords.append(Record('thomas', 'park', 'noon', 0))
allrecords.append(Record('jim', 'pool', 'afternoon', 5))
allrecords.append(Record('sandy', 'school', 'noon', 0))
allrecords.append(Record('alex', 'mall', 'night', 20))

rawDescription: Good libraries in R or Python for analytics of network/graph data


code:
con = MySQLdb.connect(db='test')
con.literal([1,2,3])
cur = con.cursor()
cur.execute("select * from test where id in %(foo)s", {'foo': [1,2,3]})
cur._executed

rawDescription: Mysql named placeholders in python used in the IN clause


code:
m = QtGui.QFrame.staticMetaObject
m.enumerator(m.indexOfEnumerator('Shadow')).valueToKey(QtGui.QFrame.Sunken)
'Sunken'

rawDescription: pyqt convert enum value to key

description: Pyqt convert enum value to key


code:
from flask_wtf.csrf import CsrfProtect

CsrfProtect(app)

rawDescription: How to use Flask-WTForms CSRF protection with AJAX?

description: Use Flask-WTForms CSRF protection with AJAX


rawDescription: Filtering out values in specific columns with Pandas


code: ax.margins( margin_you_desire )

rawDescription: Add padding between bars and Y-Axis

description: Add padding between bars and Y-Axis


code:
def by_4h(d):
    dt = datetime.datetime.strptime(d["Date[G]"]+"-"+d["Time[G]"], "%d-%b-%Y-%H:%M:%S.%f")
    return (dt.year, dt.month, dt.day, dt.hour // 4)

rawDescription: Grouping value by time frames

description: Grouping value by time frames


code:
url(r'^$', 'index', name='index'),
url(r'^blog$', 'blog', name='blog'),

rawDescription: Best way of linking to a page in Django

description: Best way of linking to a page in Django


rawDescription: Serializing a user-defined class in Python


code:
tup4 = My_tuple(**dict(zip(vars, vals)))
tup4

rawDescription: creating namedtuple instances with kwargs

description: Creating namedtuple instances with kwargs


code:
Usage

class LightDMUser(QObject):
    __metaclass__ =  set_properties_meta("display background")

rawDescription: Can anyone help condense this Python code?


code:
import json

def read_objects(filename):
    decoder = json.JSONDecoder()

    with open(filename, 'r') as inputfile:
        line = next(inputfile).strip()
        while line:
            try:
                obj, index = decoder.raw_decode(line)
                yield obj
                line = line[index:]
            except ValueError:
                # Assume we didn't have a complete object yet
                line += next(inputfile).strip()
            if not line:
                line += next(inputfile).strip()

rawDescription: Adding brackets and commas to multiple JSON objects


rawDescription: How to run code on all possible combination of 3 strings

description: Run code on all possible combination of 3 strings


code: [line.rpartition('=')[-1] for line in subprocess.check_output(['ping', '-c', '3', 'localhost']).splitlines()[1:-4]]

rawDescription: Using fping to get ping times in Python

description: Using fping to get ping times in Python


code: style_id = tree.xpath('//ul[@class="mb-padding product-attribute-list"]/li[1]/text()[2]')[0].replace(',','').strip()

rawDescription: How to extract the text between two spans with lxml (or BeautifulSoup)?

description: Extract the text between two spans with lxml (or BeautifulSoup)


code: 'function(%s)' % ', '.join(args)

rawDescription: Formatting a data structure into a comma-separated list of arguments


rawDescription: Stretching a panel to fill the whole window as opposed to just a corner


code: np.argmax(np.max(x, axis=1))

rawDescription: find row or column containing maximum value in numpy array

description: Find row or column containing maximum value in numpy array


code:
def k(l):
  return l[1]

rawDescription: Sorting or Finding Max Value by the second element in a nested list. Python


code:
def containsLetter(searchLetter, hulkLine):
    return [x for x in hulkLine if searchLetter in x]
containsLetter('i', hulkLine)

rawDescription: Write a function named containsLetter that identifies all of the strings in a list that contain a specified letter and returns a list of those strings


code: timer_id = set_timer(delay_period, callback)

rawDescription: Differentiate between single and double click in python


code:
import numpy as np
import matplotlib.pyplot as plt

# color vector with N x 3 colors, where N is the maximum number of classes and the colors are in RGB
mycolors = np.array([
  [ 0, 0, 1],
  [ 0, 1, 0],
  [ 1, 0, 1],
  [ 1, 1, 0],
  [ 0, 1, 1],
  [ 0, 0, 0],
  [ 0, .5, 1]])

# negate the colors
mycolors = 1 - mycolors

# extents of the area
x0 = -2
x1 = 2
y0 = -2
y1 = 2

# grid over the area
X, Y = np.meshgrid(np.linspace(x0, x1, 1000), np.linspace(y0, y1, 1000))

# calculate the classification and probabilities
classes = classify_func(X, Y)
probabilities = prob_func(X, Y)

# create the basic color map by the class
img = mycolors[classes]

# fade the color by the probability (black for zero prob)
img *= probabilities[:,:,None]

# reverse the negative image back
img = 1 - img

# draw it
plt.imshow(img, extent=[x0,x1,y0,y1], origin='lower')
plt.axis('equal')

# save it
plt.savefig("mymap.png")

rawDescription: How to create colormap of confidence estimates for k-Nearest Neighbor Classification

description: Create colormap of confidence estimates for k-Nearest Neighbor Classification


code:
a = []
a.append(a)
print(a)

rawDescription: what does [[...]] mean in python?

description: What does [[...]] mean in python


code:
def prime_numbers(limit=1000000):
    '''Prime number generator. Yields the series
    2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ...
    using Sieve of Eratosthenes.
    '''
    yield 2
    sub_limit = int(limit**0.5)
    flags = [True, True] + [False] * (limit - 2)
    # Step through all the odd numbers
    for i in range(3, limit, 2):
        if flags[i]:
            continue
        yield i
        # Exclude further multiples of the current prime number
        if i <= sub_limit:
            for j in range(i*i, limit, i<<1):
                flags[j] = True

rawDescription: Speed up bitstring/bit operations in Python?


rawDescription: Accessing array elements by internal data index and order


code:
def save_file(filename, new_list):
    with open(filename, 'w') as f:
        f.write('\n'.join(new_list) + '\n')

def save_file(filename, new_list):
    with open(filename, 'w') as f:
        f.writelines(line + '\n' for line in new_list)

def save_file(filename, new_list):
    with open(filename, 'w') as f:
        for line in new_list:
            f.write(line + '\n')

rawDescription: Python, adding a file into another file

description: Python, adding a file into another file


rawDescription: How to send the javascript dictionary object to django view?

description: Send the javascript dictionary object to django view


code: switch(x<0, 0, x)

rawDescription: Theano HiddenLayer Activation Function

description: Theano HiddenLayer Activation Function


rawDescription: Numpy.unique - getting output with consistent depth


code:
def getPermutations(string, prefix=""):
    if len(string) == 1:
        yield prefix + string
    else:
        for i in xrange(len(string)):
            for perm in getPermutations(string[:i] + string[i+1:], prefix+string[i]):
                yield perm

rawDescription: Python: using a recursive algorithm as a generator


code:
def permutations(iterable, r=None):
    # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
    # permutations(range(3)) --> 012 021 102 120 201 210
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    if r > n:
        return
    indices = range(n)
    cycles = range(n, n-r, -1)
    yield tuple(pool[i] for i in indices[:r])
    while n:
        for i in reversed(range(r)):
            cycles[i] -= 1
            if cycles[i] == 0:
                indices[i:] = indices[i+1:] + indices[i:i+1]
                cycles[i] = n - i
            else:
                j = cycles[i]
                indices[i], indices[-j] = indices[-j], indices[i]
                yield tuple(pool[i] for i in indices[:r])
                break
        else:
            return

rawDescription: How do i create a permutation on Python without using the itertools module nor recursive programming?

description: Create a permutation on Python without using the itertools module nor recursive programming


code: print(''.join(repr(t) + '\n' for t in itertools.combinations(x,3) if all(len(col) == len(set(col)) for col in zip(*t))))

rawDescription: Finding unique solution puzzle python

description: Finding unique solution puzzle python


code:
dynamicFibo(12)
dynamicFibo(14)

rawDescription: Dynamic Programming - Fibonacci

description: Dynamic Programming - Fibonacci


code: lambda t: (t.name == 'span' and 'green' in t.get('class',[])) or t.name == 'h1'

rawDescription: Beautiful soup queries

description: Beautiful soup queries


code:
issubclass(bool, int)
True * 5

rawDescription: python pandas/numpy True/False to 1/0 mapping

description: Python pandas/numpy True/False to 1/0 mapping


code: bin_pow(a, n, sqr=lambda x: x.sqr())

rawDescription: Referencing a class' method, not an instance's


code:
def get_context_data(self, **kwargs):
    context = super(BrainImageIndexView, self).get_context_data(**kwargs)
    context["result"] = self.something()
    return context

rawDescription: Django passing variable as a parameter to a function in a view


code: deactivate

rawDescription: How to change python version used by python-green

description: Change python version used by python-green


code: y == e

rawDescription: what does it mean to be `in` numpy array

description: What does it mean to be `in` numpy array


code: [" ".join(item) for item in x.astype('S4')]

rawDescription: convert multidimensional numpy array to list of string

description: Convert multidimensional numpy array to list of string


code:
class DontDoThis(type):
    def __getitem__(self, arg):
        return self(arg)

class SeriouslyDont(object):
    __metaclass__ = DontDoThis
    def __init__(self, arg1, arg2=None):
        if arg2 is None:
            arg1, arg2 = arg1
        self.arg1 = arg1
        self.arg2 = arg2

rawDescription: How to have multiple ways to construct an object of same class in python?

description: Have multiple ways to construct an object of same class in python


code:
class Country(db.Model):
    name = db.StringProperty(required=True)

class State(db.Model):
    country = db.ReferenceProperty(Country, collection_name='states')
    name = db.StringProperty(required=True)

class City(db.Model):
    state = db.ReferenceProperty(State, collection_name='cities')
    name = db.StringProperty(required=True)

rawDescription: Google app engine - structuring model layout with regards to parents?


code:
items = ["List: Name-Here", "List: Name-Here, Here, Here'Here, Here-Here'Here, Here Here Here", "List: Here, Here Here, Here Here Here Here"]
[len(x.split(",")) for x in items]

rawDescription: Searching with regex for a number of names in a list?


code:
acceptable = set()
#...
acceptable = set(filter(is_rank_y_top_10,
                        filter(is_rank_x_top_100, acceptable)))

rawDescription: set(filter) repetition

description: Set(filter) repetition


rawDescription: Is there a better way than this to implement Softmax Action Selection for Reinforcement Learning?


rawDescription: Sort rows and columns of a matrix by another list with numpy


code: df = pd.DataFrame(dictA).T

rawDescription: Create pandas dataframe from nested dict with outer keys as df index and inner keys column headers


rawDescription: Python - one result from a list

description: Python - one result from a list


code:
[['AHCTF1', 'Sample1', 'Sample2', 'Sample4'], ['AHCTF1', 'Sample2', 'Sample7', 'Sample12'], ['AHCTF1', 'Sample5', 'Sample6', 'Sample7']]
{'AHCTF1': {'Sample12', 'Sample1', 'Sample2', 'Sample5', 'Sample4', 'Sample7', 'Sample6'}}

rawDescription: Python lists, csv, duplication removal

description: Python lists, csv, duplication removal


rawDescription: how to interpolate data in csv file with rolling/moving average? (Python)

description: Interpolate data in csv file with rolling/moving average? (Python)


code:
def function(a, b, c, d):
    l= locals()
    for i in l:
        key = l[i]
        if isinstance(key, str):
            l[i] = [l[i]]
    l = l.values()
    print (l)
function('one', 'two', ['bla','bla'], ['ble'])

rawDescription: Convert function arguments to lists

description: Convert function arguments to lists


code:
class RefCycle:
    def __init__(self):
        self.refcycle = self
    def __del__(self):
        print('finalizing')
        print(self.refcycle)

rawDescription: How many times the finalizer method is called and zombies (PEP 442)


rawDescription: python for loop using lambda syntax

description: Python for loop using lambda syntax


rawDescription: How to separate filename from path in python (PyQt4.QtCore.QString)

description: Separate filename from path in python (PyQt4.QtCore.QString)


code:
max_month = months[max_index]
min_month = months[min_index]

rawDescription: Calculate rainfall with lists in python

description: Calculate rainfall with lists in python


code:
splitted = line.split('\t')
self.id = splitted.pop(0)
self.x1, self.y1, self.x2, self.y2 = map(float, splitted)

rawDescription: Python: tuple assignment while at same time converting type


rawDescription: Converting an "If Else" statement to a JSON file


code:
import wx
import p

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Checking Menubar from external Panel")

        mainPanel = wx.Panel(self, -1)
        mainPanel.SetBackgroundColour(wx.Color(0, 255, 0))
        sizer = wx.BoxSizer(wx.VERTICAL)
        mainPanel.SetSizer(sizer)

        subPanel = p.MyPanel(parent=mainPanel)
        sizer.Add(subPanel, 1, wx.EXPAND)

#         subPanel.createMenuBar()

        self.Show()
        sizer.Layout()

if __name__ == '__main__':
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

rawDescription: wxpython setting frame menubar from panel

description: Wxpython setting frame menubar from panel


code:
scoreReader = csv.reader(readfile)
scores = [row for row in scoreReader]
scores = scores[1::]

rawDescription: Add column from one .csv to another .csv file using Python


rawDescription: Using Python to Decode Steganography Images (example images at Wikipedia)


code:
import time
import threading

from dbus.mainloop.glib import DBusGMainLoop

try:
    from gi.repository import GObject
except ImportError:
    import gobject as GObject

DBusGMainLoop(set_as_default=True)

def myThread(run_event):
    while run_event.is_set():
        print("Thread: Print every 1 sec")
        time.sleep(1)
    print("Thread: Exit")
    return

try:
    run_event = threading.Event()
    run_event.set()

    myT = threading.Thread(target=myThread, args=(run_event,))
    myT.start()

    GObject.threads_init()
    mainloop = GObject.MainLoop()
    mainloop.run()

except (KeyboardInterrupt, SystemExit):
    mainloop.quit()
    run_event.clear()
    myT.join()
    print("Host: KeyboardInterrupt")

rawDescription: Python DBusGMainLoop inside thread and try and catch block


rawDescription: Why can't I add a tuple to a list with the '+' operator in Python?


code:
import view

view = decorator(view)

rawDescription: Overriding Django views with decorators

description: Overriding Django views with decorators


code:
help_text = """Explanation text goes here. Candidates will be able to see
             this after they have taken a questionnaire. To change this,
             refer to the setting on questionnaire administration. Max
             length is 1000 characters."""

rawDescription: Django help_text line length convention

description: Django help_text line length convention


rawDescription: How can i append database table column name as key to a list to convert it as dict

description: Append database table column name as key to a list to convert it as dict


code:
table = [[], [], [], []]
# We can't just sort the channel names because 'channel11' < 'channel2'
channel_numbers = []
for channel_name in band2.keys():
    if channel_name.startswith('channel'):
        channel_number = int(channel_name[7:])
        channel_numbers.append(channel_number)
    else:
        raise ValueError("channel name doesn't follow pattern")
channel_numbers.sort()

for channel_number in channel_numbers:
    channel_data = band2['channel%d' % channel_number]
    table[0].append('Channel %d' % channel_number)
    table[1].append(str(channel_data[0]))
    table[2].append('%s/%s' % (channel_data[1], channel_data[2]))
    table[3].append(str(channel_data[3]))

for line in table:
    print('\t'.join(line))

rawDescription: Print out dictionary in a table format?

description: Print out dictionary in a table format


code:
for m in p.finditer(s):
    print(m.start(1), s[m.start(1)])

rawDescription: Finding punctuation marks in noisy text

description: Finding punctuation marks in noisy text


code:
if request.method == 'POST':
    data = request.form
    #do something with data

rawDescription: flask: how to architect an POST endpoint that interfaces with HTML dropdowns

description: Flask: how to architect an POST endpoint that interfaces with HTML dropdowns


code: sorted(results, key=lambda t:t[0], reverse=True)

rawDescription: Python does not sort sql query result

description: Python does not sort sql query result


code:
newInput = raw_input("Enter a value ")
OldFile = read_file("myfile.txt")
value = OldFile.find("value"+6)
newFile = OldFile[:value] + newInput + OldFile[OldFile.find("\n",value+1):]

rawDescription: Edit text in file in python

description: Edit text in file in python


code:
def power_law2(x, c):
    return a_one * (x + c) ** b_one

x_data_two = np.random.rand(10)
y_data_two = np.random.rand(10)

c_two = curve_fit(power_law2, x_data_two, y_data_two)[0][0]

rawDescription: How to fit datasets so that they share some (but not all) parameter values

description: Fit datasets so that they share some (but not all) parameter values


code:
def flatten(dictionary, prefix=[], result={}):

    for k, v in dictionary.iteritems():
        type_v = type(v)
        if type_v == dict:
            flatten(v, prefix+[k], result)
        elif type_v == str:
            prefix_str = ':'.join(prefix)
            if not prefix_str in result:
                result[prefix_str] = {}
            result[prefix_str][k] = v
        else:
            raise TypeError('%s not permissible in data structure' % type_v)

    return result

nested = {
    'A': {
        'B': {
            'C': 'C val',
            'G': 'G val',
        },
        'D': {
            'E': {
                'F': 'F val',
            }
        }
    }
}

expected = flatten(nested)
print(expected)

rawDescription: Concatenating keys in nested dictionary

description: Concatenating keys in nested dictionary


code:
import unittest

def stradd(a, b):
    return a + b

class test_hello(unittest.TestCase):
    def test_1(self):
        self.assertEqual(stradd("a", "b"), "ab")

def run_my_tests(test_case):
    case = unittest.TestLoader().loadTestsFromTestCase(test_case)
    result = unittest.TestResult()
    case(result)
    if result.wasSuccessful():
        return True
    else:
        print("Some tests failed!")
        for test, err in result.failures + result.errors:
            print(test)
            print(err)
        return False

if run_my_tests(test_hello):
    # All tests passed, so we can run our programm.
    print(stradd("Hello, ", "world"))

rawDescription: How to use Python unit tests in self-contained program?

description: Use Python unit tests in self-contained program


code:
is_none = df.set_index(['Company', 'date'], inplace=True)
df  # the dataframe you want
is_none # has the value None

rawDescription: set multi index of an existing data frame in pandas

description: Set multi index of an existing data frame in pandas


code: names_and_jobs = {id: names[id] + (jobs[id],) for id in names}

rawDescription: Join lists by value

description: Join lists by value


code:
id(temp)
id(board)

rawDescription: Python - Function has a list as argument. How to return another list without changing the first?


code:
class RaspberryThread(threading.Thread):
    def __init__(self, function):
        self.running = False
        self.function = function
        super(RaspberryThread, self).__init__()

    def start(self):
        self.running = True
        super(RaspberryThread, self).start()

    def run(self):
        while self.running:
            self.function()

    def stop(self):
        self.running = False

rawDescription: Killing or stopping an active thread

description: Killing or stopping an active thread


code:
def assign_attribute_value(request, id_item_type):

    def integer_attr(item_type):
        ...
        return render_to_response('des/item_type/assign_attribute_value.html', ctx, context_instance=RequestContext(request))

    def string_attr(item_type):
        ...
        return render_to_response('des/item_type/assign_attribute_value.html', ctx, context_instance=RequestContext(request))

    def boolean_attr(item_type):
        ...
        return render_to_response('des/item_type/assign_attribute_value.html', ctx, context_instance=RequestContext(request))

    def date_attr(item_type):
        ...
        return render_to_response('des/item_type/assign_attribute_value.html', ctx, context_instance=RequestContext(request))

    type_func = {0: integer_attr,
                 1: string_attr,
                 2: boolean_attr,
                 3: date_attr,
    }

    item_type = ItemType.objects.get(id=id_item_type)

    type_func[item_type.choice](item_type=item_type)

rawDescription: Can't return a HttpResponse inside a def in a view


rawDescription: python 2.7 equivalent of built-in method int.from_bytes

description: Python 2.7 equivalent of built-in method int.from_bytes


code:
def fts5_installed(cls):
    if sqlite3.sqlite_version_info[:3] < FTS5_MIN_VERSION:
        return False

    # Test in-memory DB to determine if the FTS5 extension is installed.
    tmp_db = sqlite3.connect(':memory:')
    try:
        tmp_db.execute('CREATE VIRTUAL TABLE fts5test USING fts5 (data);')
    except:
        try:
            sqlite3.enable_load_extension(True)
            sqlite3.load_extension('fts5')
        except:
            return False
        else:
            cls._meta.database.load_extension('fts5')
    finally:
        tmp_db.close()

    return True

rawDescription: python and sqlite3, check if I can use fts5 extension?

description: Python and sqlite3, check if I can use fts5 extension


rawDescription: Is it possible to import class method without instantiating class?


rawDescription: Python .join() with a 2d array

description: Python .join() with a 2d array


code:
writer.writerows([[field(employee=row) for field in template_filter]
                  for row in data])

rawDescription: Django/Jinja2 : Generate (data for) CSV via template syntax


code:
import pandas as pd
from StringIO import StringIO
with open('example.txt') as myfile:
    data = myfile.read().replace(',"\n', '\n')
pd.read_csv(StringIO(data), header=None)

rawDescription: Python Pandas and Generator to Process Lines in CSV


code: M = tuple(M[i*Nb:i*Nb+Nb] for i in range(0, len(M), Nb))

rawDescription: Buffers and Memoryview Objects explained for the non-C programmer


code: txt['yscrollcommand'] = scrollb.set

rawDescription: How to attach a Scrollbar to a Text widget?

description: Attach a Scrollbar to a Text widget


code: level0 = df.columns.str.extract(r'([^\d]*)', expand=False)

rawDescription: Creating Multi-hierarchy pivot table in Pandas


code:
# old style
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222, sharex=ax1, sharey=ax1)
ax3 = fig.add_subplot(223, sharex=ax1, sharey=ax1)
ax3 = fig.add_subplot(224, sharex=ax1, sharey=ax1)

rawDescription: plotting autoscaled subplots with fixed limits in matplotlib

description: Plotting autoscaled subplots with fixed limits in matplotlib


rawDescription: Widget repeaters in gtk3 using python and glade


code:
import re
s = """Isaac Peter bla hello
Isaac Peter bla Asimov hello"""
re.findall(r'(?m)^Isaac(?!.*?Asimov).*$', s)

rawDescription: Python re- match if not substring

description: Python re- match if not substring


code:
import csv, itertools, operator
with open('data.csv', 'rb') as infile:
    results = []
    # uses the header row to get field names, each row will be a dict
    rows = csv.DictReader(infile)
    # keeps track of all the custom names we've seen
    all_custom_vals = set()
    for id_val, group in itertools.groupby(rows, operator.itemgetter('ID')):
        collapsed_row = {}
        for row in group:
            collapsed_row['ID'] = row['ID']
            collapsed_row['STUFF'] = row['STUFF']
            collapsed_row[row['Custom']] = row['Custom Value']
            all_custom_vals.add(row['Custom'])
        results.append(collapsed_row)

rawDescription: Convert values in CSV column to individual columns


code:
def bar():
    pass
bar = foo(bar)

rawDescription: Optionally use decorators on class methods


code:
@classmethod
def connect(cls, location=None):
    path = '.\\resources\\database\\'
    with open(path + 'info.csv', 'rU') as f:
        csv_data = csv.reader(f, delimiter=',')
        for row in csv_data:
            cls.data.append(Entry(*row))
        cls.data = cls.data[1:]

rawDescription: Confusion over how to share and modify class level data between instances.


code:
def stuff( request, kind ):
    content= news_blog.objects.filter( kind=kind )
    return render_to_response( kind+"_page", { 'content': content } )

rawDescription: Reusing a Django app within a single project


code:
with open('/path/to/image.png', 'r') as f:
    img = YourImage(f.read())
    Bullet.class_init(img)
    for _ in range(1,10000):
        Bullet()

rawDescription: Python class attributes usage and initialization


code:
import sys

if __name__ == "__main__":
    for line in sys.stdin:
        sys.stderr.write("DEBUG: got line: " + line)
        sys.stdout.write(line)

rawDescription: How to make a python script "pipeable" in bash?

description: Make a python script "pipeable" in bash


code:
paths = [
    os.path.join(os.path.dirname(__file__), 'mylib'),
    os.path.join(os.path.dirname(__file__), 'app', 'tags'),
    os.path.join(os.path.dirname(__file__), 'app', 'controllers'),
    os.path.join(os.path.dirname(__file__), 'app', 'common'),
    os.path.join(os.path.dirname(__file__), 'app', 'models'),
    os.path.join(os.path.dirname(__file__), 'app')
]
for path in paths:
    if os.path.exists(path):
        # Don't add paths that don't exist.
        sys.path.append(path)

rawDescription: How do I set the PYTHON_PATH variable for my Google App Engine app?

description: Set the PYTHON_PATH variable for my Google App Engine app


code: df.grouby('grp').apply(ols_res, ['x1', 'x2'], 'y')

rawDescription: Python pandas: how to run multiple univariate regression by group


rawDescription: Python - convert edge list to adjacency matrix


code:
import pandas.rpy.common as com

def savtocsv(filename):
    w = com.robj.r('foreign::read.spss("%s", to.data.frame=TRUE)' % filename)
    cols = list(com.robj.r("attr")(w, "variable.labels"))
    w = com.convert_robj(w)
    w.columns = cols
    return w

rawDescription: How to preserve Labels when SPSS file (.sav) imported into pandas via rpy?

description: Preserve Labels when SPSS file (.sav) imported into pandas via rpy


rawDescription: Implementing a Password Dialog with Tkinter


code:
for j in range(10):
    for i in range(10):
        print(j,end=" ")
    print()

rawDescription: nested loops results bunched together Python

description: Nested loops results bunched together Python


rawDescription: Efficiently Calculating a Euclidean Distance Matrix Using Numpy


code:
#!/usr/bin/env python3
from multiprocessing.dummy import Pool # use threads
from subprocess import Popen

def run_until_done(args):
    cmd, log_filename = args
    try:
        with open(log_filename, 'wb', 0) as logfile:
            p = Popen(cmd, stdout=logfile)
        return cmd, p.wait(), None
    except Exception as e:
        return cmd, None, str(e)

commands = ((('echo', str(d)), 'subprocess.%03d.log' % d) for d in range(500))
pool = Pool(128) # 128 concurrent commands at a time
for cmd, status, error in pool.imap_unordered(run_until_done, commands):
    if error is None:
       fmt = '{cmd} done, status {status}'
    else:
       fmt = 'failed to run {cmd}, reason: {error}'
    print(fmt.format_map(vars())) # or fmt.format(**vars()) on older versions

rawDescription: Python subprocess return code without waiting


code:
df1 = pd.pivot_table(df, values=['D'], rows=['B'], aggfunc=np.sum)
df2 = pd.pivot_table(df, values=['E'], rows=['B'], aggfunc=np.mean)
pd.concat((df1, df2), axis=1)

rawDescription: define aggfunc for each values column in pandas pivot table

description: Define aggfunc for each values column in pandas pivot table


rawDescription: Send e-mail if conditions are met, then sleep for one hour


rawDescription: Python BeautifulSoup parsing specific text


code:
class MainPanel(wx.lib.scrolledpanel.ScrolledPanel):
    def __init__(self,parent):
        wx.lib.scrolledpanel.ScrolledPanel.__init__(self, parent=parent)
        self.SetupScrolling()
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.fill()
        self.SetSizer(self.sizer)

    def fill(self):
        tup = [wx.StaticText(self, wx.ID_ANY, "I'm static text") for i in range(200)]
        self.sizer.AddMany(tup)
        self.Layout()

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="FrameTest", size=(600,800))
        self.InitMenu()
        self.panel = None
        self.panel = MainPanel(self)

    def InitMenu(self):
        self.menuBar = wx.MenuBar()
        menuFile = wx.Menu()
        menuFile.Append(wx.ID_NEW, "&New")
        self.Bind(wx.EVT_MENU, self.OnNew, id=wx.ID_NEW)
        self.menuBar.Append(menuFile, "&File")
        self.SetMenuBar(self.menuBar)

    def OnNew(self, evt):
        if self.panel:
            self.panel.sizer.Clear()
        self.panel.fill()

rawDescription: wxPython Panel in existing window: slow and small

description: WxPython Panel in existing window: slow and small


code:
def animate(i):
    if i > 9:
        i = 9
    graph.set_data(x[:i+1], y[:i+1])
    return graph

ani = FuncAnimation(fig, animate, interval=200)

rawDescription: How can i make points of a python plot appear over time?

description: Make points of a python plot appear over time


code:
from datetime import datetime
datetime

rawDescription: How to get a dot notation of a python module?

description: Get a dot notation of a python module


code:
import re
s="banana"

result = []
for i in range(len(s)):
    part = re.findall(r'(?=([^aeiou].{%d}))'%i, s,re.I)
    result.extend(part)

print(result)

rawDescription: Can number of matches in a regex be looped over?


code:
class FooListView(generics.ListAPIView):
    model = Foo
    serializer_class = FooSerializer
    filter_fields = ('category', )

rawDescription: How to add search parameters to GET request in Django REST Framework?

description: Add search parameters to GET request in Django REST Framework


code:
from itertools import *
import operator

def allsubsets(l):
    for s in chain( *map(lambda r: combinations(l,r), xrange(1,len(l))) ):
        yield s

# only count tuples that make consecutive ranges
def mk_cover(acc, item):
    ((curlo, curhi), tuplist, ovl) = acc
    (newlo, newhi)                 = item
    if newlo<=curhi:
        # ok, the new item possibly extends the range
        if newhi>=curhi:
            overlap = curhi - newlo
        else:
            overlap = newhi - newlo
            newhi   = curhi
        return ((curlo, newhi), tuplist+[item], ovl+overlap)
    else:
        # it doesn't so return the old accumulator
        return acc

# return a function that will inspect lists-of-tuples
# to see if they cover the range lo -> hi. If they do,
# append the covering list and the overlap to an
# accumulator; a list of solutions found so far
def mk_finder(lo, hi):
    def overlapper(acc, tuples):
        # inspect the subset of tuples, wether they
        # cover lo -> hi
        # sort by start value
        tuples                    = list(sorted(tuples, key=operator.itemgetter(0)))
        ((covlo, covhi), tl, ovl) = reduce(mk_cover, tuples[1:], (tuples[0], [tuples[0]], 0))
        if covlo<=lo and covhi>=hi:
            acc.append( ((covlo, covhi), tl, ovl) )
        return acc
    return overlapper

rawDescription: finding tuple sublist with minimal overlap

description: Finding tuple sublist with minimal overlap


rawDescription: Any better algorithm possible here?

description: Any better algorithm possible here


code:
import re
s2 = "start bla bla bla word1 bla bla bla word1 value1 word1 bla bla bla"
s1= "start bla bla bla word1 bla bla bla word1 bla bla bla word1 value1"
re.search(r'start(?:(?:(?!word1).)*word1){2}(?:(?!word1).)*?(value1)', s2)
re.search(r'start(?:(?:(?!word1).)*word1){2}(?:(?!word1).)*?(value1)', s2).group(1)
re.search(r'start(?:(?:(?!word1).)*word1){2}(?:(?!word1).)*?(value1)', s1)

rawDescription: How to match every thing except some character ? regex

description: Match every thing except some character ? regex


code: myButton = Button(root).pack()

rawDescription: Change the value of a variable with a button (Tkinter)


code:
import sys

if sys.stdout.isatty():
    print('hello')
else:
    print('goodbye')

rawDescription: Python 3.4.3 subprocess.Popen get output of command without piping?


code:
#set up a dataframe as in hte provided example
df=pd.DataFrame([[1,'A',22],[1,'B',21],[1,'C',1]],columns=['col1','col2','col3'])
#get the indeces to match the example
counted= df.set_index(['col1','col2'],drop=True,inplace=False)

rawDescription: Aggregation on aggregated values

description: Aggregation on aggregated values


code:
df.apply(lambda x: x.first_valid_index())
df.apply(lambda x: x.first_valid_index()).max()

rawDescription: Get index where both rows are not NaN

description: Get index where both rows are not NaN


code:
#!/usr/bin/python

from gi.repository import Gtk

class AboutDialog:
    def __init__(self):
        self.builder = Gtk.Builder()
        self.builder.add_objects_from_file("v0.glade", ("aboutwin", ""))
        self.ab = self.builder.get_object("aboutwin")
        self.builder.connect_signals(self)

    def on_aboutdialog_response(self, widget, signal):
        print("About close with button or (x)")
        self.ab.hide()

    def run(self):
        self.ab.run()

class MainHandler(Gtk.Window):
    def on_delete_window(self, widget, signal):
        print("Main window closed")
        Gtk.main_quit()

    def on_about_pressed(self, button):
        print("Run About Dialog")
        about = AboutDialog()
        about.run()

def main():
    builder = Gtk.Builder()
    builder.add_objects_from_file("v0.glade", ("signinwin", "") )
    builder.connect_signals(MainHandler())

    main = builder.get_object("signinwin")
    main.show_all()

    Gtk.main()

if __name__ == "__main__":
      main()

rawDescription: MultiWindow error Glade, Gtk and Python

description: MultiWindow error Glade, Gtk and Python


code:
import requests
r = requests.get(image_url)
# here r.url will return the redirected true image url
image_filename = os.path.basename(r.url)
f = open(image_filename, 'wb')
f.write(r.content)
f.close()

rawDescription: How to grab the output from python subprocess

description: Grab the output from python subprocess


code:
self.ag.actions()[0].setChecked(true)
#selects first action, or do this at creation time

rawDescription: getting radio button selection in PySide using Python 3.4

description: Getting radio button selection in PySide using Python 3.4


code:
def double(lst):
    return [x * 2 for x in lst]

rawDescription: How to double all the values in a list

description: Double all the values in a list


code:
allFiles = [f for f in os.listdir(detach_dir) if not f.endswith(".zip")]
for file in allFiles:
    archive = zipfile.ZipFile(token+'.zip', mode='a')
    archive.write(file)
    archive.close()
    os.unlink(file)

rawDescription: Trouble isolating emails when downloading via Python script


code:
s = f.read()
hashlib.sha1(s).hexdigest()

rawDescription: Efficient large file hash over network using Python


code:
class Weapon:
  def __init__( self, name = 'Stick', high = 1, low = 0 ):
    self.name = name
    self.high = high
    self.low = low

wepstats = ( 'Sword', 5, 55 )

sword = Weapon( *wepstats )

rawDescription: Python Dict w/Array Values Passed as Params?


code:
import sys
import ruamel.yaml
from ruamel.yaml.scalarstring import PreservedScalarString

returns = {'file_|-/usr/bin/gen-motd.py_|-/usr/bin/gen-motd.py_|-managed': {'comment': 'File /usr/bin/gen-motd.py updated', 'pchanges': {'diff': '--- \n+++ \n@@ -1,5 +1,7 @@\n #!/usr/bin/env python\n \n+import sys\n+import urllib2\n import socket\n import json\n import time\n'}, 'name': '/usr/bin/gen-motd.py', 'start_time': '16:18:54.060168', 'result': True, 'duration': 99.663, '__run_num__': 1, 'changes': {'diff': '--- \n+++ \n@@ -1,5 +1,7 @@\n #!/usr/bin/env python\n \n+import sys\n+import urllib2\n import socket\n import json\n import time\n'}, '__id__': '/usr/bin/gen-motd.py'}}

for k in returns:
    for k1 in returns[k]:
        if not isinstance(returns[k][k1], dict):
            continue
        for k2 in returns[k][k1]:
            v = returns[k][k1][k2]
            try:
                if '\n' in v:
                    while ' \n' in v:
                        v = v.replace(' \n', '\n') # remove EOL spaces
                    returns[k][k1][k2] = PreservedScalarString(v)
            except TypeError:
                continue

ruamel.yaml.round_trip_dump(returns, sys.stdout)

rawDescription: python convert to yaml formatting issue

description: Python convert to yaml formatting issue