code: zip(*np.where(a == 1))

rawDescription: How to find the index of a value in 2d array in Python?

description: Find the index of a value in 2d array in Python


code:
[['here', 'are', 'some', 'strings'],
 ['and',
  'some',
  'others',
  [['with', 'different', 'levels'], ['of', [['indentation']]]]]]

rawDescription: pyparsing with starting and ending string being the same

description: Pyparsing with starting and ending string being the same


rawDescription: how can i finding the index of non-ASCII character in python string?

description: Finding the index of non-ASCII character in python string


code: from __future__ import division

rawDescription: How do I divide the members of a list by the corresponding members of another list in Python?

description: Divide the members of a list by the corresponding members of another list in Python


code:
from itertools import groupby
x = [2345,67,24,24,11,11,6,6,6,6,6,3,3,3,3,3,1,1,1,1,1,1]
grouped_x = [(k, sum(1 for i in g)) for k,g in groupby(x)]
grouped_x

rawDescription: simple calculation in python

description: Simple calculation in python


code: plt.pcolormesh(x, y, z)

rawDescription: Plotting a 2D histogram using x, y and a cartesian product array. (Python)


code:
import os
from selenium import webdriver

data = """
Estimated Future Payouts \n
Under Non-Equity Incentive \n
Plan Awards \n
"""
with open("index.html", "w") as f:
    f.write("

%s

" % data)

driver = webdriver.Chrome()
driver.get("file://" + os.path.abspath("index.html"))

td = driver.find_element_by_tag_name("td")
driver.execute_script("arguments[0].style['border-bottom'] = '';", td)

print(td.get_attribute("outerHTML"))

driver.close()

rawDescription: python lxml: syntax for selectively deleting inline style attributes?

description: Python lxml: syntax for selectively deleting inline style attributes


code:
df["diff"] = df.groupby("uid")["timestamp"].diff()
df

rawDescription: python pandas: diff by user in a DataFrame containing multiple users

description: Python pandas: diff by user in a DataFrame containing multiple users


code:
axsliderA = axes([0.12, 0.85, 0.16, 0.075])
sA = Slider(axsliderA, 'A', -1, 1.0, valinit=0.5)
sA.on_changed(update)

rawDescription: Closing pyplot windows

description: Closing pyplot windows


code: df['Date'] = pd.to_datetime(df['Date'])

rawDescription: Pandas: Change dates in dataframe to same date format


code:
import csv
import operator

def sort_csv(csv_filename, types, sort_key_columns):
    """sort (and rewrite) a csv file.
    types:  data types (conversion functions) for each column in the file
    sort_key_columns: column numbers of columns to sort by"""
    data = []
    with open(csv_filename, 'rb') as f:
        for row in csv.reader(f):
            data.append(convert(types, row))
    data.sort(key=operator.itemgetter(*sort_key_columns))
    with open(csv_filename, 'wb') as f:
        csv.writer(f).writerows(data)

rawDescription: Sorting CSV in Python

description: Sorting CSV in Python


code:
def consec(n, iterable):
    result = set()
    prev = None # use object() here instead if None could be a value in the iterable
    count = 0
    for item in iterable:
        if item == prev:
            count += 1
            if count == n:
                result.add(prev)
        else:
            prev = item
            count = 1
    return result

rawDescription: How do I return a set of n consecutive integers given n and an iterable?

description: Return a set of n consecutive integers given n and an iterable


rawDescription: Python socket only accepting local connections


rawDescription: str performance in python

description: Str performance in python


code:
from operator import itemgetter
item=itemgetter(-1)
map(item, training_set)

rawDescription: How do I just extract the second component of a Python array?

description: Just extract the second component of a Python array


code:
template = '{} {{}} {{{{}}}}'
template.format('foo')
template.format('foo').format('bar')
template.format('foo').format('bar').format('baz')

rawDescription: How to get rid of repeating variable in str.format?

description: Get rid of repeating variable in str.format


rawDescription: Interpolate on a logarithmic scale in python


code:
A = np.zeros((8, 8))
A[np.ix_(rows, columns)] += 1

rawDescription: Composite Index updates for Numpy Matrices


code:
soup = BeautifulSoup(input_document)
for li in soup.find_all('li'):
    li.decompose()

rawDescription: How to remove content of <li>tags from HTML

description: Remove content of <li>tags from HTML


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

data1, data2 = np.random.random((2,10,10))

fig, ax = plt.subplots()
ax.imshow(data1, extent=[-10, 0, -10, 0])
ax.imshow(data2, extent=[10, 20, 10, 20])

ax.autoscale()

plt.show()

rawDescription: Matplotlib matrix/image explicitly state axis values


code:
args = [3, 6]
range(*args)            # call with arguments unpacked from a list [3, 4, 5]

rawDescription: Passing several arguments from an array to a function in a easy way


code:
from django.conf.urls.defaults import *
from django.contrib import admin

rawDescription: Get the version of Django for application


code:
v = {}

for key, value in sorted(d.iteritems()):
    v.setdefault(value, []).append(key)

rawDescription: Grouping Python dictionary keys as a list and create a new dictionary with this list as a value


code:
import rpy2.robjects as robjects
robjects.r("require(foreign)")
robjects.r('x=read.csv("test.csv")')
robjects.r('write.dta(x,"test.dta")')

rawDescription: Convert .CSV files to .DTA files in Python


code: first([None, None,1, 2,None]) # 1

rawDescription: Why don't Python's any/all return the found objects?


code:
def get_name_root(s):
    return "".join(s.split(".")[0].split("_")[1:-1])

def find_files_to_delete_for_group(group):
    sorted_group = sorted(group, key=extract_date)
    return sorted_group[:-5]

rawDescription: python deleting older files that match an unknown pattern (tricky)

description: Python deleting older files that match an unknown pattern (tricky)


code:
from itertools import product
n = 10

a = [[1 if i==0 or j==0 else 0 for i in range(n)] for j in range(n)]

for i,j in product(range(1, n), repeat=2):
         a[i][j] = a[i-1][j] + a[i][j-1]

rawDescription: Filling in a symmetric 2D array

description: Filling in a symmetric 2D array


rawDescription: Python Joining two dictionnary with similar content


code: participants = fields.List(Participant, tagname="participants")

rawDescription: How to parse Nested Elements in Python using dexml

description: Parse Nested Elements in Python using dexml


code:
path = os.path.join(os.path.expanduser("~"), "Desktop", "your_file.xls")
df.to_excel(path)

rawDescription: to_excel on desktop regardless of the user

description: To_excel on desktop regardless of the user


code: df_large.ix[df_small.index, 'col_3'] = df_small.col_3

rawDescription: Merge a larger pandas dataframe with specific values from a subset


code: lst = map(int, open('filename.txt').readlines())

rawDescription: Converting a string into a list in Python


code:
import csv

open("writefile.csv", "w").close() #Deletes everything in the file. Backspace this if you don't want the file contents deleted.

with open("readfile.csv", "r") as r, open("writefile.csv", "a") as w:
    reader = csv.reader(r, lineterminator = "\n")
    writer = csv.writer(w, lineterminator = "\n")

    for row in reader:
        if "SPTU" in row: #"SPTU" Has to be the entire value of the column. Please comment if you want to see if one of the columns CONTAINS "SPTU
            w.writerow(row)

rawDescription: Copy Specific Rows and Columns from a .csv into .xlsx


code:
B=sparse.bmat(lc)
B
B.A

rawDescription: Read a dense matrix from a file directly into a sparse numpy array?


code:
def shift_char(ch,shift):
    if not ch.isalpha():
        return ch   # return non-alphabetic characters as is
    is_upper = 'A' <= original_character <= 'Z'
    if is_upper:
        base_a = 'A'
    else:
        base_a = 'a'
    return chr(ord(base_a) + (ord(ch)-ord(base_a)+shift)%26)

rawDescription: How to change random letter using number in Python

description: Change random letter using number in Python


rawDescription: set thousands separators in iPython without string formatting

description: Set thousands separators in iPython without string formatting


rawDescription: Change level of python fileConfig logger


code:
strs = 'bacarat,baccarat\r'
strs.strip().split(',')

rawDescription: string split() in python 2.6

description: String split() in python 2.6


code: dict((char, [i for i,c in enumerate(a) if c==char]) for char in b)

rawDescription: Get the index of a list from another list in python


rawDescription: Selenium - Click Link where Text is IMG

description: Selenium - Click Link where Text is IMG


code:
timeit.timeit('L.sort()', 'import random;L = [(random.randint(0, 1000), i) for i in range(50000)]', number=1000)
timeit.timeit('L.sort(key=operator.itemgetter(0))', 'import random;import operator;L = [(random.randint(0, 1000), i) for i in range(50000)]', number=1000)

rawDescription: optimizing python code for performance

description: Optimizing python code for performance


code:
with open(filename) as input_file:
    contents = input_file.readlines()
person = contents[0]
birth_year = contents[1]
...
biography = contents[4:]

rawDescription: Python: How do you read a chunk of text from a file without knowing how long the file actually is?


rawDescription: Decimal approximation to expression (square root)


code:
from collections import defaultdict
dd=defaultdict(int)
for k, v in myDict.iteritems():
   dd[k]=len(v)

dd

rawDescription: Dictionary that counts occurrences of its items


code:
import requests
from bs4 import BeautifulSoup
try:
    # Python2
    from urllib import unquote
except ImportError:
    # Python3
    from urllib.parse import unquote

for i in range(0,500):
    o=36*i
    r=requests.get('http://www.barneys.com/barneys-new-york/men?start='+str(o)+'&format=page-element&sz=36')
    soup=BeautifulSoup(r.text)
    links=soup.find_all("a",{"class":"thumb-link"})
    for link in links:
        print(unquote(link.get('href')))

rawDescription: Unicode extraction

description: Unicode extraction


code:
data = {'a': 4, 'b': 1, 'e': 1}
result = [{'name':key, 'value':value} for key,value in data.items()]

rawDescription: python: Convert a Counter object to a json object with added key names

description: Python: Convert a Counter object to a json object with added key names


code: os.system('curl https://api.smartsheet.com/1.1/sheet/7846568935090052 -H "Authorization: Bearer 4ziycpnqjvto49oah6t6urd4un" -H "Accept: application/vnd.ms-excel" -o output.xls')

rawDescription: How can i execute this code in python?

description: Execute this code in python


code: arr = np.loadtxt('data', usecols=(0,1))

rawDescription: Python - Create an array from columns in file


rawDescription: Numpy 3D array transposed when indexed in single step vs two steps


code: result = [(key, sum(key in vals for nodes in digraph)) for key, vals in inputDict]

rawDescription: how to re-write for loop to list comprehension

description: Re-write for loop to list comprehension


rawDescription: python decorators stacking

description: Python decorators stacking


code:
fig,ax = plt.subplots(nrows = 2, ncols = 2)
ax[1,1].remove()
ax[1,1]=fig.add_subplot(2,2,4,projection='3d')

rawDescription: Can I turn an existing ax object into a 3d projection?

description: Turn an existing ax object into a 3d projection


code:
def reverse_tuple(t):
    return t[::-1]

sorted(dictionary.items(), key=reverse_tuple)

rawDescription: Sorting dictionary keys by value, then those with the same value alphabetically


rawDescription: datetime comparison with string?

description: Datetime comparison with string


code:
def counter (allWords):
    d = {}
    for word in allWords:
        if word in d:
            d[word].occ += 1
        else:
            d[word] = Word(word)

return d.items()

rawDescription: Counting word occurances in txt file, returning dictionary and converting dict to list with the help of classes


code:
from StringIO import StringIO
import pandas

datafile = StringIO("""\
bedrooms  day1   day2   day3     day(n)
2          75    N/A     70       50
2          60    60      60       60
4          100   110     N/A      N/A
3          75    80      80       N/A
""")

df = pandas.read_table(datafile, sep='\s+', index_col='bedrooms')
df.columns.names = ['days rented']
prices = df.stack()
prices.name = 'dollars'
prices.reset_index()

rawDescription: Pandas and linear regression: multiple y values for single x


rawDescription: picking last 3 digits from long decimal values

description: Picking last 3 digits from long decimal values


code:
# bar.py
class Bar: pass

rawDescription: python modify __metaclass__ for whole program

description: Python modify __metaclass__ for whole program


rawDescription: Numpy: check whether a bit is set to 1 or 0 in an array?


code:
xmlResponse = ftfy.fix_text(
    xmlFile.read().decode('utf-8'),
    fix_entities=False, uncurl_quotes=False, fix_latin_ligatures=False).encode('utf-8')

rawDescription: python - How to properly encode string in utf8 which is ISO8859-1 from xml

description: Python - How to properly encode string in utf8 which is ISO8859-1 from xml


code:
print = print

def print_something():
    print("print something")

rawDescription: Mocking print but allow using it in tests


code:
a = np.ones((5, 5), dtype=object)
import fractions
a *= fractions.Fraction(3, 4)
a

rawDescription: numpy array with mpz/mpfr values

description: Numpy array with mpz/mpfr values


code: e_dict = {n: str(n) * n for n in range(1, 30)}

rawDescription: Where is this variable defined?

description: Where is this variable defined


code:
class consumer(threading.Thread):
    def run(self):
        while True:
            queue_ret = queue.get()
            if queue_ret is _im_done:
                queue.put(_im_done)
                break
            '''normal execution'''

rawDescription: Need help on producer and consumer thread in python


code:
import cgi
import requests
import shutil

def download_url(url, directory):
    """Download file from url to directory

    URL is expected to have a Content-Disposition header telling us what
    filename to use.

    Returns filename of downloaded file.

    """
    response = requests.get(url, stream=True)
    if response.status != 200:
        raise ValueError('Failed to download')

    params = cgi.parse_header(
        response.headers.get('Content-Disposition', ''))[-1]
    if 'filename' not in params:
        raise ValueError('Could not find a filename')

    filename = os.path.basename(params['filename'])
    abs_path = os.path.join(directory, filename)
    with open(abs_path, 'wb') as target:
        response.raw.decode_content = True
        shutil.copyfileobj(response.raw, target)

    return filename

rawDescription: Downloading File in Python (With Requests?)


code:
import numpy as np

# I'll use numbers instead of words,
# but same exact concept
points_list = [[0,1,2],
               [0,3],
               [1,4],
               [0,2],
               [0,1,2,3],
               [0,1,2,4]]

scores = np.zeros((5,5))

for points in points_list:
    temp = np.array(points)[:, np.newaxis]
    scores[temp, points] += 1

diag = scores.diagonal()

key_col = (scores/diag)[:, 0]
key_col[0] = 0

points_2 = np.where(key_col > 0.5)[0]      # suppose 0.5 is the threshold
temp_2 = np.array(points_2)[:, np.newaxis] # step 1: we identified the points that are
                                           # close to 0
inner_scores = scores[temp_2, points_2]    # step 1: we are checking if those points are
                                           # are close to each other

rawDescription: Recognition of elements in a list - machine learning


code:
short_docs = [test_case.shortDescription() for test_case, mesg in test_result.failures]
docs = [test_case._testMethodDoc for test_case, mesg in test_result.failures]

rawDescription: Get the docstring of the test methods from a result of unit testing


code:
def spider_opened(self, spider):
    ...
    self.exporters = {}
    for category in settings.CATEGORIES:
        file = open('output/%s.xml' % category, 'w+b')
        exporter = XmlItemExporter(file)
        exporter.start_exporting()
        self.exporters[category] = exporter

def spider_closed(self, spider):
    for exporter in self.exporters.itervalues():
        exporter.finish_exporting()

def process_item(self, item, spider):
    self.exporters[item['category']].export_item(item)
    return item

rawDescription: Separate output file for every url given in start_urls list of spider in scrapy


code: import core.module

rawDescription: How to access whole sub python file from parent in order to use as parameter plus structure of python progr. with modules

description: Access whole sub python file from parent in order to use as parameter plus structure of python progr. with modules


rawDescription: Matplotlib wont join data points when the x axis isn't equally spaced


code:
start_bananas = blank.copy()
start_bananas.fill(100)

rawDescription: How to set limits to specific components of an equation when plotting them?

description: Set limits to specific components of an equation when plotting them


code:
import test_module

def main():
    test_module.TestMyCode.run_tests()

if __name__ == '__main__':
    main()

rawDescription: Invoking Test Methods with a Method for a unittest.TestCase Subclass Python


rawDescription: How to Print An Odd Number of Backslashes in Python


code: numpy.savetxt('file.csv', array, delimiter = ',', fmt = ['%d'] + ['%.4f']*999)

rawDescription: saving large array as csv with different fomat

description: Saving large array as csv with different fomat


rawDescription: How to link my QTableWidget subclass to my main program

description: Link my QTableWidget subclass to my main program


code:
def _empty_buffer(self):
    while not self.stop_printing:
        if self.current_line_idx < len(self.buffer):
            while not self.ok_received.is_set() and not self.stop_printing:
                logger.debug('waiting on ok_received')
                self.ok_received.wait(2)
            # START OF RACE WINDOW
            line = self._next_line()
            self.s.write(line)
            self.current_line_idx += 1
            # END OF RACE WINDOW
            self.ok_received.clear()
        else:
            break

rawDescription: Possible Race Condition in Serial read/write Code


rawDescription: Best practice to reduce memory usage when splitting array


rawDescription: Python: Making Dollars make Sense

description: Python: Making Dollars make Sense


code:
def SerialRead(dataReadEvent):
    ...
    dataReadEvent.set()
    threading.Timer(restart, SerialRead, args=(dataReadEvent,)).start()

rawDescription: Have textbox update every 5 seconds PyQt


code: ses_var = {}

rawDescription: How to obtain the client IP address in Autobahn Wamp Protocol?

description: Obtain the client IP address in Autobahn Wamp Protocol


code:
np.linspace(0, 1, num=0)
np.linspace(0, 1, num=1)
np.linspace(0, 1, num=2)

rawDescription: linspace that would always include the final point?

description: Linspace that would always include the final point


rawDescription: Learning loops and lists

description: Learning loops and lists


code:
# Python Variables
USR = 'user_here'
PWD = 'pass_here'

rawDescription: Python 3 Include Custom File with Variables


code: newls = [sum(ls[i:i+3]) for i in range(0, len(ls), 3)]

rawDescription: How to iterate through a list and sum members together

description: Iterate through a list and sum members together


rawDescription: python: Replace/substitute all whole-word match in a string

description: Python: Replace/substitute all whole-word match in a string


code:
d = defaultdict(list)
for index, key in enumerate(l):
    if key == "pattern":
        d[key].append(index)

rawDescription: python comprehension for loop with if statement and other actions following that

description: Python comprehension for loop with if statement and other actions following that


code:
import tempfile
import zipfile
import shutil
import os

def remove_from_zip(zipfname, *filenames):
    tempdir = tempfile.mkdtemp()
    try:
        tempname = os.path.join(tempdir, 'new.zip')
        with zipfile.ZipFile(zipfname, 'r') as zipread:
            with zipfile.ZipFile(tempname, 'w') as zipwrite:
                for item in zipread.infolist():
                    if item.filename not in filenames:
                        data = zipread.read(item.filename)
                        zipwrite.writestr(item, data)
        shutil.move(tempname, zipfname)
    finally:
        shutil.rmtree(tempdir)

rawDescription: overwriting file in ziparchive

description: Overwriting file in ziparchive


code:
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler

# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

# Create server
server = SimpleXMLRPCServer(("localhost", 8000),
                            requestHandler=RequestHandler)

def myfunction(x, y):
    status = 1
    result = [5, 6, [4, 5]]
    return (status, result)
server.register_function(myfunction)

# Run the server's main loop
server.serve_forever()

rawDescription: How to send a xml-rpc request in python?

description: Send a xml-rpc request in python


rawDescription: Python: Combine two lists, overwriting the final few lines of the first list


code:
class AdditionalKeyValuePair:
    def __init__(self, specialKey, specialValue):
        self.specialKey = specialKey
        self.specialValue = specialValue

    def __add__(self, d):
        if not isinstance(d, dict):
            raise Exception("Not a dict in AdditionalKeyValuePair")

        return DictAdditionalValueWrapper(d, self.specialKey, self.specialValue)

rawDescription: How to pass in a dictionary with additional elements in python?

description: Pass in a dictionary with additional elements in python


rawDescription: How to split the string in python

description: Split the string in python


code: ['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

rawDescription: Python: How to generate the code on the fly?


code:
import numpy as np
import numba as nb

@nb.jit
def sin_numba(x):
    n = x.shape[0]
    h = np.empty(n, np.float64)
    for k in range(n):
        h[k] = np.sin(x[k])

    return h

rawDescription: Why is Numpy outperforming this cython routine by a factor 3


rawDescription: PocketSphinx python and setting the acoustic model?


code:
def float_cast(item):
    try:
        return float(item)
    except ValueError:
        return item
line = [u'6.0', u'5.0', u'1.2', u'S\xf3lo', u'_START_', u'utilizamos', u'only', u'We', u'use', u'0', u'0', u'1', u'0', u'0', u'0', u'0.2', u'0.1', u'0.2', u'0', u'0', u'0', u'ADV', u'RB', u'9', u'0', u'OK']
result = [float_cast(item) for item in line]
result

rawDescription: Casting a list of strings into float if possible. - python


rawDescription: Python condition base print color string


code:
haver_vec = np.vectorize(haversine, otypes=[np.int16])
distance = df.groupby('id').apply(lambda x: pd.Series(haver_vec(df.coordinates, x.coordinates)))

rawDescription: Vectorizing Haversine distance calculation in Python


code:
pool = multiprocessing.Pool(None, initialize, work)
pool.map(worker, work)

rawDescription: multiprocess initializer and pickling

description: Multiprocess initializer and pickling


code: df.applymap(lambda x: isinstance(x, (int, float)))

rawDescription: finding non-numeric rows in dataframe in pandas?

description: Finding non-numeric rows in dataframe in pandas


code:
import numpy as np
def ppoints(n, a):
    """ numpy analogue or `R`'s `ppoints` function
        see details at http://stat.ethz.ch/R-manual/R-patched/library/stats/html/ppoints.html
        :param n: array type or number"""
    try:
        n = np.float(len(n))
    except TypeError:
        n = np.float(n)
    return (np.arange(n) + 1 - a)/(n + 1 - 2*a)

rawDescription: Imitating 'ppoints' R function in python


code:
import tempfile
content = form.getvalue('filename')
tmpf, tmpfn = tempfile.mkstemp()
tmpf.write(content)
tmpf.close()
# Now, give tmpfn to the function expecting a filename

os.unlink(tmpfn) # Finally, delete the file

rawDescription: python receive file from html form

description: Python receive file from html form


code: form = SomeForm(initial={'alive' : 0})

rawDescription: Help with validation in Models and Forms


code: max(enumerate(ld), key=lambda item: item[1]['size'])

rawDescription: Python list of dicts, get max value index


code:
SHIRT,RED,1
SHIRT,BLUE,0
SHIRT,GREEN,0
SHOE,RED,1
SHOE,BLUE,0

rawDescription: Using Python to manipulate data from a CSV only applying it to the first result


code:
def make_C(a=False, b=False):
    if a:
        return CAB()
    elif b:
        return CBA()

rawDescription: conditional class inheritance in python

description: Conditional class inheritance in python


code:
import BeautifulSoup
doc = BeautifulSoup.BeautifulStoneSoup(xml)
for item in doc.findAll('item'):
    for elt in item:
        if isinstance(elt,BeautifulSoup.Tag):
            print(elt)

rawDescription: Parse all item elements with children from RSS feed with beautifulsoup


code:
def copy( cells ):
    new = Cell(cells.value)
    current = new
    old = cells

    while old.next is not None:
        # copy the current cell
        ccopy = Cell(old.value)

        # add it
        current.next = ccopy

        # prepare for the next round
        current = ccopy
        old = old.next

    return new

rawDescription: Python copy and concatenate linked lists, preserve order


code:
df_s['time'] = pd.to_datetime(df_s['time'],unit='ms')
df_s

rawDescription: Getting rows from a data frame which satisfy a condition in pandas


code:
root = 'hello'
sub = 'hello is is ew'
sub[:len(root)]
sub[len(root):]

rawDescription: find all files indirectory and sub-directories and provide the path from directory

description: Find all files indirectory and sub-directories and provide the path from directory


code:
def unique_list(mylist):
    return list(set(mylist))

unique_list([1])
unique_list([1, 1])
unique_list([1, 1, 2])
unique_list([1, 3, 1, 2])
unique_list(['dog','cat','dog','fish'])

rawDescription: How do I make a function that returns a list of unique strings, given a list of strings with duplicates?

description: Make a function that returns a list of unique strings, given a list of strings with duplicates


rawDescription: Verify if provided string corresponds to pattern on python


code:
s = 'suppose_name_1'
index = s.rfind('_')  # Last index of '_'
s.replace(s[index+1:], str(int(s[index+1:]) + 1))

rawDescription: Check whether a string contains a numeric/digit/number in python


rawDescription: Inserting optparse input into a function call


code: your_query.filter(created__gte = today)

rawDescription: Implementing matching in database

description: Implementing matching in database


code:
from itertools import izip
map(sum, izip(a,b))

rawDescription: Concise vector adding in Python?

description: Concise vector adding in Python


code:
# To start redirecting stdout:
import sys
sys.stdout = StdoutRedirector( self )
# (where self refers to the widget)

# To stop redirecting stdout:
sys.stdout = sys.__stdout__

rawDescription: stdout to tkinter GUI

description: Stdout to tkinter GUI


code:
with open("foo.txt", "r") as f:
    for line in f:
        for i in line.split():
            if i.isdigit():
                my_list.append(int(i))

rawDescription: efficient way of reading integers from file

description: Efficient way of reading integers from file


code:
class Something(models.Model):
    name = models.CharField(max_length=25)

    def get_name_uppercase(self):
        return self.name.upper()

rawDescription: Django - Model field define unicode

description: Django - Model field define unicode


code:
import re
s1 = 'VCALogParser_output_ARW.log'
s2 = 'VCALogParser_output_CZC.log'
re.search(r'.*_(.*)\.log', s1).group(1)
re.search(r'.*_(.*)\.log', s2).group(1)

rawDescription: how to split the text using python?

description: Split the text using python


code:
from itertools import chain

list(chain.from_iterable(dictA.itervalues()))

rawDescription: List Comprehension of Lists Nested in Dictionaries


code:
pylab.text(1.,1.,'top-right', transform=ax.transAxes,
           horizontalalignment='right', verticalalignment='top')

pylab.text(0.,0.,'bottom-left', transform=ax.transAxes,
           horizontalalignment='left', verticalalignment='bottom')

rawDescription: Matplotlib, adding text with more than one line. Adding text that can follow the curve


rawDescription: Python Pandas: How to read only first n rows of CSV files in?


code:
import numpy as np

a = np.random.rand(10, 20, 30)

idx1 = np.where(a>0.2)
idx2 = np.where(a<0.4)

ridx1 = np.ravel_multi_index(idx1, a.shape)
ridx2 = np.ravel_multi_index(idx2, a.shape)
ridx = np.intersect1d(ridx1, ridx2)
idx = np.unravel_index(ridx, a.shape)

np.allclose(a[idx], a[(a>0.2) & (a<0.4)])

rawDescription: How to logically combine integer indices in numpy?

description: Logically combine integer indices in numpy


code: room = action()

rawDescription: Having trouble with functions calling other functions through dictionaries: could someone please explain this code to me?


code:
def numerology(z):
    zstr1=list(str(z))
    res=sum([int(i) for i in zstr1])
    if res>10:
        res=numerology(res)
    return res

rawDescription: converting iteration into a function

description: Converting iteration into a function


code:
def doublewrap(f):
    '''
    a decorator decorator, allowing the decorator to be used as:
    @decorator(with, arguments, and=kwargs)
    or
    @decorator
    '''
    @wraps(f)
    def new_dec(*args, **kwargs):
        if len(args) == 1 and len(kwargs) == 0 and callable(args[0]):
            # actual decorated function
            return f(args[0])
        else:
            # decorator arguments
            return lambda realf: f(realf, *args, **kwargs)

    return new_dec

rawDescription: How to create a Python decorator that can be used either with or without parameters?

description: Create a Python decorator that can be used either with or without parameters


code:
# read/parse CSV into pandas data frame
df = pd.read_csv('input.csv', delim_whitespace=True)

rawDescription: change column values in CSV using python

description: Change column values in CSV using python


code:
for row in rows:
    row = {name_map[name]: val for name, val in row.items()}

rawDescription: How do I re-map python dict keys

description: Re-map python dict keys


code:
data = ['1', 'I think. I am.'], ['2', 'I came. I saw. I conquered.']
dict([ [t[0], t[1].rstrip('.').split('.')] for t in data ])

rawDescription: Python break list values into sub-components and maintain key


rawDescription: Two functions same name python

description: Two functions same name python


code: np.where(vor.point_region == i)[0][0]

rawDescription: Get point associated with Voronoi region (scipy.spatial.Voronoi)


code: new['d'] = pd.to_datetime(new['d'].str[1:].astype(int), unit='s').dt.strftime('%Y-%m-%d %H:%M:%S')

rawDescription: Plot with pandas seems not to work well as I change the index of a DataFrame


code:
import time
import threading

import cherrypy
from cherrypy.process import plugins

UPDATE_INTERVAL = 0.5
REQUEST_DELAY = 0.1
UPDATE_DELAY = 0.1
THREAD_POOL_SIZE = 20

next_data = 1

class DataGateway(plugins.SimplePlugin):

    def __init__(self, bus):
        super(DataGateway, self).__init__(bus)
        self.data = next_data

    def start(self):
        self.bus.log("Starting DataGateway")
        self.bus.subscribe('dg:get', self._get_data)
        self.bus.subscribe('dg:update', self._update_data)
        self.bus.log("DataGateway has been started")

    def stop(self):
        self.bus.log("Stopping DataGateway")
        self.bus.unsubscribe('dg:get', self._get_data)
        self.bus.unsubscribe('dg:update', self._update_data)
        self.bus.log("DataGateway has been stopped")

    def _update_data(self, new_val):
        self.bus.log("Changing data, buffering should start!")
        self.data = new_val
        time.sleep(UPDATE_DELAY)
        self.bus.log("Continue serving buffered and new requests.")

    def _get_data(self):
        return self.data

def processData():
    """Backround task works for half hour three times a day,
        and when finishes it publish it in the engine buffer."""
    global next_data
    cherrypy.engine.publish("dg:update", next_data)
    next_data += 1

class DataPublisher(object):

    @property
    def data(self):
        return cherrypy.engine.publish('dg:get').pop()

    @cherrypy.expose
    def index(self):
        result = "I get " + str(self.data)
        cherrypy.engine.log(result)
        time.sleep(REQUEST_DELAY)
        return result

if __name__ == '__main__':
    conf = {
        'global': {
            'server.thread_pool': THREAD_POOL_SIZE,
            'server.socket_host': '127.0.0.1',
            'server.socket_port': 8080,
        }
    }
    cherrypy.config.update(conf)
    DataGateway(cherrypy.engine).subscribe()
    plugins.BackgroundTask(UPDATE_DELAY, processData).start()
    cherrypy.quickstart(DataPublisher())

rawDescription: CherryPy: how to stop and buffer incoming request while data is updated


rawDescription: How to assert two list contain the same elements in Python?

description: Assert two list contain the same elements in Python


code: ['note', 'to', 'from', 'heading', 'body']

rawDescription: List of distinct XML element names using BeautifulSoup


rawDescription: Pandas DataFrame row wise comparison

description: Pandas DataFrame row wise comparison


code:
# preferred module name would be test_protol as CamelCase convention are used for class name
import TestProtocol

# try to load all testcases from given module, hope your testcases are extending from unittest.TestCase
suite = unittest.TestLoader().loadTestsFromModule(TestProtocol)
# run all tests with verbosity
unittest.TextTestRunner(verbosity=2).run(suite)

rawDescription: Run unittests from a different file

description: Run unittests from a different file


code:
from bs4 import BeautifulSoup
import urllib2

response = urllib2.urlopen('https://www.symantec.com/index.jsp').read()
len(BeautifulSoup(response, 'html.parser').find_all("a"))
len(BeautifulSoup(response, 'html5lib').find_all("a"))
len(BeautifulSoup(response, 'lxml').find_all("a"))

rawDescription: Extracting <a> tags using BeautifulSoup

description: Extracting <a> tags using BeautifulSoup


code:
def lrange_literal_eval(lst):
    return [ast.literal_eval(x) for x in lst]

r_server.set_response_callback('LRANGE', lrange_literal_eval)

with r_server.pipeline() as pipe:
    pipe.lrange('test', 0, -1).delete('test')
    r_list = pipe.execute()[0]
    r_list.reverse()

rawDescription: How did you pop all valus in redis one time


code: values = re.findall(r'someinteger(.+?)withanother(.+?)', html)

rawDescription: how to add and sort multidimentional array in python?

description: Add and sort multidimentional array in python


rawDescription: How can I apply a function to pairs of elements in a list with Python?

description: Apply a function to pairs of elements in a list with Python


code: list(combinations(range(9), 2))

rawDescription: Python generate all possible configurations of numbers on a "board"


code:
first_list = a[0]
first_element_of_first_list = first_list[0]

rawDescription: Python: Picking a certain element from an array


code:
f = k.astype(float)
f
f[0][0] = 1e-3
f

rawDescription: Why does python 2.6 not update my numpy array?


code:
import pygame
import random

#----------------------------------------------------------------------

class Enemy():

    def __init__(self, image, x=0, y=0):

        self.image = pygame.image.load(image).convert()
        self.image.set_colorkey( (255,255,255) )

        self.rect = self.image.get_rect()

        self.rect.centerx = x
        self.rect.centery = y

    #------------

    def draw(self, screen):
        screen.blit(self.image, self.rect)

    #------------

    def update(self):
        # here change randomly positon
        self.rect.topleft = random.randint(60, 220+1), random.randint(0, 475+1)

#----------------------------------------------------------------------

rawDescription: Pygame enemy display random when player moves by keyboard


rawDescription: How to extract elements from a matrix using a vector of indices using numpy?

description: Extract elements from a matrix using a vector of indices using numpy


code:
def doStuff(test):
    test = "hello world"
    return test

def main():
    test = None
    test = doStuff(test)
    print(test)

rawDescription: Are variables instantiated to 'None' pass by value in Python?


rawDescription: Extracting elements inside and between brackets


rawDescription: communication with 0mq

description: Communication with 0mq


code:
from collections import defaultdict
from urllib import quote
from urlparse import parse_qsl, urlparse

rawDescription: Determing variance in length

description: Determing variance in length


code:
class YourModel(models.Model):
    last_updated = models.DateTimeField(auto_now=True)
    # Rest of the fields.

rawDescription: Implementing autocomplete in android based on django model


code:
import numpy as np
x = np.array([(1,'a'), (1, 'b'), (3,'a'), (3, 'b'), (3, 'c'), (4, 'd')], dtype=[('id', int),('category', str, 22)]  )
y = np.array([('a', 3.2, 0), ('b', -1, 0), ('c', 0, 0), ('d', 100, 0)], dtype=[('category', str, 20), ('value', float), ('output', int)] )

for idx in np.where(y['value'] > 3)[0]:
    row = y[idx]
    category = row['category']
    # Only the last record in `x` of the right category affects `y`.
    # So find the id value for that last record in `x`
    idval = x[x['category'] == category]['id'][-1]
    y[idx]['output'] = idval

print(y)

rawDescription: selecting records in a python structure based on a related structure

description: Selecting records in a python structure based on a related structure


code:
def test(s1, s2, op):
    return op(s1,s2)

test(set([1]), set([2]), set.union)  # set([1, 2])

rawDescription: How can I pass union/intersection methods as a parameter in Python?

description: Pass union/intersection methods as a parameter in Python


code:
from Tkinter import *

class Gui(object):
    def __init__(self, parent):
        self.top = parent

        self.checked1 = IntVar()
        self.checked2 = IntVar()

        self.c1_value = 1
        self.c2_value = 2

        self.c1 = Checkbutton(self.top, text='b1', variable=self.checked1)
        self.c2 = Checkbutton(self.top, text='b2', variable=self.checked2)
        self.b1 = Button(self.top, text="ADD", command=self.callback)
        self.l1 = Label(self.top)

        self.c1.pack()
        self.c2.pack()
        self.b1.pack()
        self.l1.pack()

    def callback(self):
        value = self.c1_value*self.checked1.get() + self.c2_value*self.checked2.get()
        self.l1.config(text=str(value))

root = Tk()

my_window = Gui(root)

root.mainloop()

rawDescription: How to create a GUI to add two numbers from different two checkboxes?

description: Create a GUI to add two numbers from different two checkboxes


code: dd

rawDescription: Summarize a list of dictionaries based on common key values


code:
niter = 10000

# Setup the progress counters
increment = int(0.01 * niter)
percent = 0
countdown = increment
nbars = 0
totalbars = 20
barincrement = int(100/totalbars)

sys.stdout.write("{0}0%\r".format(" " * totalbars))

for i in range(niter):
    doSomeWork()
    countdown -= 1

    # Update and print out progress bar and % indicator if required
    if countdown == 0:
      percent += 1
      countdown = increment
      if percent % barcinrement == 0:
        nbars += 1
      bars = "#" * nbars
      space = " " * (totalbars - nbars)

      sys.stdout.write("{0}{1}{2}%\r".format(bars, space, percent))

sys.stdout.write("\nOperation Complete!\n")

rawDescription: How to add progressbar in python

description: Add progressbar in python


rawDescription: Reshaping the array returned by matplotlib.pyplot.imread(*args, **kwargs)


code:
def getAdjacentCells(x,y):
  adjacentlist = []
  for dx in (-1, 0, +1):
    for dy in (-1, 0, +1):
      if(dx != 0 or dy != 0):
        adjacentlist.append(addr(x+dx, y+dy, 4, 4))
  return adjacentlist

rawDescription: Finding adjacent cells in a wraparound matrix


code:
from __future__ import unicode_literals
'this is a {} {}'.format('привет', 'мир')

rawDescription: How to build a utf8 string in python

description: Build a utf8 string in python


code:
#use `with` statement for handling files, it automatically closes the file for you.
with open('file.html') as fo, open('file1.html', 'w') as fo1:
   for line in fo:                   #reads one line at a time, memory efficient
      #do something with line, line.strip()
      fo1.write(line + '\n')              #write line to to fo1

rawDescription: How to modify file contents as string whilst having access to methods effecting lines within string?

description: Modify file contents as string whilst having access to methods effecting lines within string


code:
from subprocess import PIPE,Popen

p = Popen(["xcopy",sourceplayers, destinationplayers],stdin=PIPE)
p.stdin.write("All\n")
p = Popen(["xcopy",sourceuserdata, destinationuserdata],stdin=PIPE)
p.stdin.write("All\n")

rawDescription: python file overwriting with os module

description: Python file overwriting with os module


code:
def SomeServiceFactory():
    class SomeService(ServiceBase):
        @rpc(Unicode, _returns=Unicode)
        def echo_string(ctx, string):
            return string
    return SomeService

rawDescription: Deploy multiple web services, i.e. multiple wsdl files, in python


code:
def str_comp (a, b):
    return a.lower() == b.lower()

rawDescription: Writing string comparing function in python


code:
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.backends.backend_pdf import PdfPages

df = pd.DataFrame([[1,2,3],[7,0,3],[1,2,2]],columns=['col1','col2','col3'])

with PdfPages('multipage_pdf.pdf') as pdf:

    df.plot()
    pdf.savefig()
    plt.close()

    df.plot(kind='bar')
    pdf.savefig()
    plt.close()

rawDescription: how to export to pdf a graph based on a pandas dataframe?

description: Export to pdf a graph based on a pandas dataframe


code:
def addItemAttributes(tree, idList):
    (menu, cat, subcat, item, attribs) = idList;

    currDict = tree.setdefault(menu, {})\
        .setdefault(cat, {})\
        .setdefault(subcat, {})\
        .setdefault(item, {})

    for a in attribs:
        currDict[a[0]] = a[1]

rawDescription: Construction of 5 level tree structure in python


code:
crcbin = 0
with open(filename, 'rb') as f:
    while True:
        chunk = f.read(65536) # magic constant, use whatever you feel is appropriate
        if not chunk:
            break
        crcbin = binascii.crc32(chunk, crcbin)
crc32 = '%08x' % crcbin

rawDescription: Different values for linux crc32 vs python binascii


rawDescription: Django: Increment blog entry view count by one. Is this efficient?


code:
d = [('ID1',{'Experiment1': 40, 'Experiment2':59, 'Experiment3':65}),
     ('ID2',{'Experiment1': 68, 'Experiment2':21, 'Experiment3':39})]

rawDescription: 2D table format Python

description: 2D table format Python


code: strs = '[<User: mr--adhikari>, <User: awesome>]'

rawDescription: How to convert this list in a given format?

description: Convert this list in a given format


rawDescription: python3 variable expansion "a"*x to add empty elements in a list

description: Python3 variable expansion "a"*x to add empty elements in a list


code:
def myregex(pattern,text,dir=0):
    import re
    m = re.search(pattern, text)
    if m:
        yield m.group(0)
        if len(m.group('suffix')):
            for r in myregex(pattern, "%s%s%s" % (m.group('prefix'),m.group('suffix')[1:],m.group('end')),1):
                yield r
            if dir<1 :
                for r in myregex(pattern, "%s%s%s" % (m.group('prefix'),m.group('suffix')[:-1],m.group('end')),-1):
                    yield r

def myprocess(pattern, text):
    parts = pattern.split("*")
    for i in range(0, len(parts)-1 ):
        res=""
        for j in range(0, len(parts) ):
            if j==0:
                res+="(?P<prefix>"
            if j==i:
                res+=")(?P<suffix>"
            res+=parts[j]
            if j==i+1:
                res+=")(?P<end>"
            if j<len(parts)-1:
                if j==i:
                    res+=".*"
                else:
                    res+=".*?"
            else:
                res+=")"
        for r in myregex(res,text):
            yield r

def mycount(pattern, text):
    return set(myprocess(pattern, text))

rawDescription: Count overlapping regex matches once again


code:
def __str__(self):
    return '{}({}, #{}, {} shift, ${}/hr)'.format(
        self.__class__.__name__, self.get_name(), self.get_number(),
        self.display_shift(), self.get_pay())

rawDescription: How do I take the users input and if it equals 1 it would be day and a 2 would be night?

description: Take the users input and if it equals 1 it would be day and a 2 would be night


code: datetime.timedelta(13, 86399)

rawDescription: Why does timedelta function return 0 seconds when datetimes are perfect number of days apart?


code:
import os
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
plt.ion()
plt.show()

dir = 'path/to/pics'

for fname in os.listdir(dir):
    fname = os.path.join(dir, fname)
    im = plt.imread(fname)
    img = ax.imshow(im)
    plt.draw()
    accept = raw_input('OK? ')

rawDescription: how to make matplotlib draw sequence of images in same window

description: Make matplotlib draw sequence of images in same window


code:
total_length = test_length + train_length
divisor = gcd(test_length,train_length)
test_multiple = test_length/divisor
train_multiple = train_length/divisor
total_multiple = test_multiple + train_multiple

# Adjust the ratio if there isn't enough data for the requested folds
if total_length/total_multiple < folds:
  total_multiple = total_length/folds
  test_multiple = int(round(float(test_length)*total_multiple/total_length))
  train_multiple = total_multiple - test_multiple

groups = []
for i in range(folds,0,-1):
  float_size = float(total_length)/i
  int_size = int(float_size/total_multiple)*total_multiple
  test_size = int_size*test_multiple/total_multiple
  train_size = int_size*train_multiple/total_multiple
  test_length -= test_size    # keep track of the test data used
  train_length -= train_size  # keep track of the train data used
  total_length -= int_size
  groups.append((test_size,train_size))

# If the test_length or train_length are negative, we need to adjust the groups
# to "give back" some of the data.
distribute_overrun(groups,test_length,0)
distribute_overrun(groups,train_length,1)

rawDescription: Maintaining a ratio when splitting up data in python function


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

num = 5000
sizes = 0.4 * np.random.random(num)
xy = 50 * np.random.random((num, 2))

fig, ax = plt.subplots()

coll = matplotlib.collections.EllipseCollection(sizes, sizes,
                                                np.zeros_like(sizes),
                                                offsets=xy, units='x',
                                                transOffset=ax.transData,
                                                **kwargs)
ax.add_collection(coll)
ax.margins(0.01)
plt.show()

rawDescription: How can I plot many thousands of circles quickly?

description: Plot many thousands of circles quickly


code:
import scipy.io.wavfile
rate, data = scipy.io.wavfile.read('Happy Tree Friends.wav')
rate
data
data_bis = np.fft.ifft(np.fft.fft(data))
data_bis
data_bis.dtype

rawDescription: Issues with scipy.io wave file processing after applying fourier transforms


code:
somelist = ['index0', ['index10', 'index11', ['index120', 'index121', ['index1220']]]]
indices = [1, 2, 2, 0]
reduce(lambda l, i: l[i], indices, somelist)

rawDescription: Python: Accessing elements of multi-dimensional list, given a list of indexes


code:
"""
\s*       # zero or more white spaces
(xxx)     # the variable you're looking for
\s*       # zero or more white spaces
=         # an equal sign
\s*       # zero or more white space
(?:'|")?  # zero or one ' or " but non-capturing (i.e. don't store in group output)
(\w+)     # the value of the variable
(?:'|")?  # zero or one single quote OR zero or one double quote
"""

rawDescription: Search string with re module in html page


rawDescription: pandas value_counts() with multiple values in list form?

description: Pandas value_counts() with multiple values in list form


code:
class A(models.Model):
    def get_B(self):
       if(hasattr(self, 'b')):
           return self.b
       return None

class B(models.Model):
    ref_a = models.OneToOneField(related_name='ref_b', null=True)

rawDescription: Check if a OneToOne relation exists in Django


rawDescription: Efficient way to split R, G and B values from a file containing RGB values (Without NumPy)


code:
def cachedAttributes(**kwargs):
    def decorator(func):
        func.__dict__.update(kwargs)
        return func
    return decorator

rawDescription: Python write decorator for static method


code: df['author name:'].iloc[int(len(df) / 2.)]

rawDescription: pulling key by place from dictionary using pandas

description: Pulling key by place from dictionary using pandas


code: f = open('data.txt', 'r').readlines()

rawDescription: read a table according to a certain number of characters

description: Read a table according to a certain number of characters


code:
s = "
golly

NHS

foo
"
soup = BeautifulSoup(s)
details = soup.findAll('p')
type(details[0])    # prints: <class 'BeautifulSoup.Tag'>

rawDescription: Python - Looping through HTML Tags and using IF


code:
import collections
Parameters = collections.OrderedDict(
    (("current", '50mA'), ("voltage", '230'), ("resistance", '40 ohms'))
)
Parameters["inductance"] = "37"
Parameters["power"] = "100 watt"
Parameters

rawDescription: How to retain the order of items in dictionary?

description: Retain the order of items in dictionary


code:
a += 2           # a now points to the integer object 5, because you can't
                 # change 2 into 5 (integers are immutable)
print(c)         # prints 3. c still points to 3, because you never told
                 # Python to make c point anywhere else!

rawDescription: When does python know an initialization is a copy, so that change at one place automatically changes another place?


rawDescription: How put html tags into XSL stylesheet?

description: How put html tags into XSL stylesheet


code:
['Sao', 'Paulo']
['Rio', 'de', 'Janeiro']

rawDescription: How using literal to separate one or more words?


code:
table_name = MySQLdb.escape_string(table_name)
escaped_column_name = MySQLdb.escape_string(column_name)
column_name = '`%s`.`%s`' % (table_name, escaped_column_name)

alter_sql = 'ALTER TABLE %s ADD COLUMN %s TEXT' % (table_name, column_name)

rawDescription: Escaping MySQL reserved words with Python dbapi


code: space.set_default_collision_handler(near_callback, near_callback, None, None, None)

rawDescription: Simulating a simple physics related scenario


code:
from collections import defaultdict
result = defaultdict(set)
for key,value in my_dict.items():
    result[key].update(value)
    for elem in value:
        result[elem].add(key)

rawDescription: Match keys to values in dictionary in python


code: {name: func for name, func in filters.__dict__.items() if not name.startswith('__')}

rawDescription: Adding a modules functions automatically to a dict


code:
def set_val(d, keys, val):
    last = keys[-1]  # Key we want to set val on
    search_keys = keys[:-1]  # Keys we need to traverse
    reduce(lambda x,y: x[y], search_keys, d)[last] = val

rawDescription: Pythonic way of accessing recursively nested dict


rawDescription: How to print unsupported unicode characters on Windows cmd as e.g. "?" instead of raising exception?

description: Print unsupported unicode characters on Windows cmd as e.g. "?" instead of raising exception


code:
a = content_items + third_levels

def key(item):
    '''Extract digits from the beginning of item for a sort key.

    item is a string
    >>> key('1 ABC')
    (1, None, None)
    >>> key('1.2 ABC')
    (1, 2, None)
    >>> key('1.2.3 ABC')
    (1, 2, 3)
    >>>
    '''
    item = item.split()
    item = item[0].split('.')
    a, b, c = None, None, None
    try:
        a = int(item[0])
    except ValueError as e:
        pass
    try:
        b = int(item[1])
    except IndexError as e:
        pass
    try:
        c = int(item[2])
    except IndexError as e:
        pass
    return a, b, c

a.sort(key = key)
a.extend(appendices)

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


rawDescription: Replace space character between two numbers


code:
data = {}
for file in files:
    data[get_component(file)] = {}
    for key, value in get_data(file):
        data[get_component(file)][key] = value

rawDescription: How to associate elements in a set with multiple dict entries

description: Associate elements in a set with multiple dict entries


rawDescription: Is there a way to divide a 2d list(list of lists) into 2 or more separate 1-dimensional lists with for loop?


code: df = df.reset_index()

rawDescription: Convert pandas df multi index to columns


code:
html = '''<time datetime="2015-07-25T10:06:46-0700">2015-07-25 10:06am</time>'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
soup.findAll('time')[0].text

rawDescription: How would I extract this with Python regular expressions?


code:
def foo(bar, baz=None):
    baz = baz if baz is not None else blar()

rawDescription: Dynamic default arguments in python functions


code:
mydict = dict()
with open("sample.csv") as inputs:
    for line in inputs:
        date_time, value = line.strip().split()
        hour_min = date_time[:5]
        mydict.setdefault(hour_min, list()).append(int(value))

for hour_min, vlist in mydict.items():
    print("key:'{0}', value:'{1}'".format(hour_min, sum(vlist)))

rawDescription: Summing timeseries data in a python dict


code:
mylist + mylist #resulting list has 2 references to each element in mylist
                #stored as:
                #[mylist[0],mylist[1],...,mylist[0],mylist[1],...]
                #   ^ ----------------------^
                #   reference the same object

rawDescription: numpy multiple ndarray creation at once

description: Numpy multiple ndarray creation at once


code:
the_dict = {
    'amount_paid': request.POST.getlist('amount_paid[]'),
    'paid_date': request.POST.getlist('paid_date[]'),
    'method': request.POST.getlist('method[]'),
    'comments': request.POST.getlist('comments[]'),
}

rawDescription: Create a python dictionary of lists

description: Create a python dictionary of lists


code:
A.data
A.todense()
A = A.sign()
A.todense()

rawDescription: Is Scipy's sign() not guaranteed working?


code:
def read_my_data(filename_queue):

  class ImageRecord(object):
    pass
  result = ImageRecord()

  # Dimensions of the images in the dataset.
  label_bytes = 1
  # Set the following constants as appropriate.
  result.height = IMAGE_HEIGHT
  result.width = IMAGE_WIDTH
  result.depth = IMAGE_DEPTH
  image_bytes = result.height * result.width * result.depth
  # Every record consists of a label followed by the image, with a
  # fixed number of bytes for each.
  record_bytes = label_bytes + image_bytes

  assert record_bytes == 22501  # Based on your question.

  # Read a record, getting filenames from the filename_queue.  No
  # header or footer in the binary, so we leave header_bytes
  # and footer_bytes at their default of 0.
  reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
  result.key, value = reader.read(filename_queue)

  # Convert from a string to a vector of uint8 that is record_bytes long.
  record_bytes = tf.decode_raw(value, tf.uint8)

  # The first bytes represent the label, which we convert from uint8->int32.
  result.label = tf.cast(
      tf.slice(record_bytes, [0], [label_bytes]), tf.int32)

  # The remaining bytes after the label represent the image, which we reshape
  # from [depth * height * width] to [depth, height, width].
  depth_major = tf.reshape(tf.slice(record_bytes, [label_bytes], [image_bytes]),
                           [result.depth, result.height, result.width])
  # Convert from [depth, height, width] to [height, width, depth].
  result.uint8image = tf.transpose(depth_major, [1, 2, 0])

  return result

rawDescription: Attach a queue to a numpy array in tensorflow for data fetch instead of files?


code:
def base_book(request):
    (...)
    myImage = "two_scoops_django.png"
    return render(request, 'base_book.html',
      {'book': theBook, 'author': myAuthors, 'myImage': myImage}
      )

rawDescription: Django Image won't come through on template from image directory


code:
import numpy as np
import re

with open('data', 'r') as f:
    lines = (line.replace('(',' ').replace(')',' ') for line in f)
    arr = np.genfromtxt(lines)
print(arr)

rawDescription: Python numpy.genfromtxt

description: Python numpy.genfromtxt


rawDescription: Can pandas.DataFrame have list type column?


code:
requests_adapter = adapter.MyHttpAdapter(
    pool_connections=__CONCURRENT_LIMIT__,
    pool_maxsize=__CONCURRENT_LIMIT__,
    max_retries=0,
    pool_block=False,
    burst_window=datetime.timedelta(seconds=5),
    wait_window=datetime.timedelta(seconds=20))

requests_session = requests.session()
requests_session.mount('http://', requests_adapter)
requests_session.mount('https://', requests_adapter)

unsent_requests = (grequests.get(url,
                                 hooks={'response': handle_response},
                                 session=requests_session) for url in urls)
grequests.map(unsent_requests, size=__CONCURRENT_LIMIT__)

rawDescription: Limiting/throttling the rate of HTTP requests in GRequests


code:
import socket

server = 'irc.rizon.net'
channel = '#linux'
nick = 'MyBot'
port = 6667

ircsock = socket.socket()
ircsock.connect((server, port))

handle = ircsock.makefile(mode='rw', buffering=1, encoding='utf-8', newline='\r\n')

print('NICK', nick, file=handle)
print('USER', nick, nick, nick, ':'+nick, file=handle)

for line in handle:
    line = line.strip()
    print(line)

    if "PING" in line:
        print("PONG :" + line.split(':')[1], file=handle)

rawDescription: Creating a simple IRC bot in python. Having trouble


code:
s = Shop.objects.get(name="First Shop")
s.employee_set.all()

rawDescription: How do I create this object with the models I have set up?

description: Create this object with the models I have set up


code:
[TEST_CONFIG]
xunitFile = firefoxTests.xml

[rc_configuration]
command = GoogleChrome

rawDescription: python nose tests: specifying which node the test should be run in Selenium Grid

description: Python nose tests: specifying which node the test should be run in Selenium Grid


code:
from selenium import webdriver

webdriver.DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.Accept-Language'] = 'ru-RU'
driver = webdriver.PhantomJS()
driver.get('http://httpbin.org/headers')
print(driver.page_source)

rawDescription: Custom headers in Phantomjs Selenium WebDriver


code:
if regtype == wreg.REG_BINARY:
    value = value[:8] + chr(0x09) + value[9:]

rawDescription: Editing a REG_BINARY using _winreg

description: Editing a REG_BINARY using _winreg


code:
game_data = dict(player_id = BPL[0], action = ACT, id = GID)
c.post(gameurl, data = game_data)
game_data = dict(player_id = BPL[1], action = ACT, id = GID)
c.post(gameurl, data = game_data)

rawDescription: Connection aborted requests error

description: Connection aborted requests error


code:
dates = pd.date_range('2000-1-1', periods=N, freq='D')
codes = np.arange(N)
np.random.shuffle(codes)
codemap = dict(zip(dates, codes))
# {Timestamp('2000-01-01 00:00:00', offset='D'): 0,
#  Timestamp('2000-01-02 00:00:00', offset='D'): 1,
#  Timestamp('2000-01-03 00:00:00', offset='D'): 2}

rawDescription: Fancy indexing in pandas

description: Fancy indexing in pandas


code: {'phone': '702-000-0000', 'gender': 'male', 'prefix': 'Waiter', 'name': 'Ben', 'address': '9999 Gotham City, las vegas'}

rawDescription: Initializing Python class with array data


code:
import pandas as pd
import csv
import numpy as np

Input = pd.DataFrame({'AGE': 10, 'QUALITY':15}, index=[0])
print(Input)
AGE = Input['AGE']
QUALITY = Input['QUALITY']
loopcount = 2
i = 1
while i < loopcount:
    Input['DELTA'] = QUALITY * .1
    Input2 = pd.DataFrame(Input, copy=True) # Here is the change
    Input2['AGE'] = Input['AGE'] + 1
    Input2['DELTA'] = Input2['QUALITY'] * .1
    Input2['QUALITY'] = Input2['QUALITY'] + Input2['DELTA']
    Input = Input.append(Input2)
    i += 1

print (Input)

rawDescription: While loop overwrites field

description: While loop overwrites field


code:
import re
extract_number = re.compile('\[(\d+\.\d+)\]')
def sort_key(entry):
    return float(extract_number.search(entry).group(1))

yourlist = [
    '[6.1] (some text, line breaks, etc)',
    '[3.5] (some text, line breaks, etc)',
    '[55.999999] (some text, line breaks, etc)',
]
sorted(yourlist, key=sort_key)
from pprint import pprint
pprint(_)

rawDescription: Very specific sorting in Python

description: Very specific sorting in Python


code: t = soup.find(class_="label", text=lambda s: "Fiscal" in s and "year" not in s)

rawDescription: Use Multiple Conditions in BeautifulSoup


code:
import re

s = '| this is      |     my  | made up        string '
print(re.sub(r'\s*\|\s*', '|', s))

rawDescription: How can I trim the whitespace immediately preceding and following a given character in python?

description: Trim the whitespace immediately preceding and following a given character in python


code:
with open("myfile.txt") as fh:
    coords = [tuple(float(x) for x in line.strip('()\n').split(', ')) for line in fh]

rawDescription: Converting text document coordinates 'x, y' to float point list object


code: radecs2 = radecs[radecs.dec > - 10 * u.degree]

rawDescription: How to sort through a list of observable coordinates?

description: Sort through a list of observable coordinates


code: 'Rock', 'Paper', 'Scissors', 'Lizard', 'Spock', 'Rock', 'Paper', 'Scissors', 'Lizard', 'Spock'

rawDescription: Values not returning correctly when playing Rock-Paper-Scissors type game


code:
import logging
import textwrap
import itertools
'''
MyFormatter class
Adapted from: http://stackoverflow.com/questions/6847862/how-to-change-the-format-of-logged-messages-temporarily-in-python
              http://stackoverflow.com/questions/3096402/python-string-formatter-for-paragraphs
Authors: Vinay Sajip, unutbu
'''
class MyFormatter(logging.Formatter):

    #This function overwrites logging.Formatter.format
    #We conver the msg into the overall format we want to see
    def format(self,record):

        widths=[getTerminalSize()[0] - 12 ,10]
        form='{row[0]:<{width[0]}} {row[1]:<{width[1]}}'

        #Instead of formatting...rewrite message as desired here
        record.msg = self.Create_Columns(form,widths,[record.msg],["[%8s]"%record.levelname])

        #Return basic formatter
        return super(MyFormatter,self).format(record)

    def Create_Columns(self,format_str,widths,*columns):
        '''
        format_str describes the format of the report.
        {row[i]} is replaced by data from the ith element of columns.

        widths is expected to be a list of integers.
        {width[i]} is replaced by the ith element of the list widths.

        All the power of Python's string format spec is available for you to use
        in format_str. You can use it to define fill characters, alignment, width, type, etc.

        formatter takes an arbitrary number of arguments.
        Every argument after format_str and widths should be a list of strings.
        Each list contains the data for one column of the report.

        formatter returns the report as one big string.
        '''
        result=[]
        for row in zip(*columns):

            #Create a indents for each row...
            sub = []

            #Loop through
            for r in row:
                #Expand tabs to spaces to make our lives easier
                r = r.expandtabs()

                #Find the leading spaces and create indend character
                if r.find(" ") == 0:
                    i = 0
                    for letters in r:
                        if not letters == " ":
                            break
                        i += 1
                    sub.append(" "*i)
                else:
                    sub.append("")

            #Actually wrap and creat the string to return...stolen from internet
            lines=[textwrap.wrap(elt, width=num, subsequent_indent=ind) for elt,num,ind in zip(row,widths,sub)]
            for line in itertools.izip_longest(*lines,fillvalue=''):
                result.append(format_str.format(width=widths,row=line))
        return '\n'.join(result)

rawDescription: Logging Formatting Right Justify Level Name


code: libpd_message('recvr', 'hellooo')

rawDescription: Sending messages from Python to a Pure Data patch via libPd


rawDescription: Removing a char from String in Python

description: Removing a char from String in Python


code:
def main():
    ...

def other_things():
    ...

main()

rawDescription: Pythonian structure?

description: Pythonian structure


code:
from copy import copy, deepcopy
a = (1, 2, [])
c = copy(a)
d = deepcopy(a)
a[2].append(1000)
c
d

rawDescription: copy.copy vs copy.deepcopy performance on tuples

description: Copy.copy vs copy.deepcopy performance on tuples


code: value|striptags

rawDescription: Sanitizing HTML in submitted form data

description: Sanitizing HTML in submitted form data


code:
import pandas as pd

df = pd.read_table('data', sep='\s+')
kw = df[['Word',"Group","Subgroup"]]

words = df['Word']

md = {"m":"w","o":"z"}
md = {k: v.split(',') for k, v in md.items()}

newwords = []
for word in words:
    # Save both the original word and the new word
    newwords.append((word, word))
    for c in md:
        occ = word.count(c)
        pos = 0
        for _ in range(occ):
            pos = word.find(c, pos)
            for r in md[c]:
                tmp = word[:pos] + r + word[pos+1:]
                newwords.append((word, tmp))

newwords = pd.DataFrame(newwords, columns=['Word', 'New'])
# Merge on the original Word
result = pd.merge(newwords, kw, left_on='Word', right_on='Word', how='left')
result = result[['Group', 'Subgroup', 'New']]
result.columns =  ['Group', 'Subgroup', 'Word']
print(result)

rawDescription: Classify values in a list based on original DF (Python 3, Pandas)


code:
events = Event.objects.filter(active=True)
events = events.exclude(eventitem__rev1__isnull=True)
events = events.exclude(eventitem__rev1='')
events = events.exclude(eventitem__rev2__isnull=True)
events = events.exclude(eventitem__rev2='')

rawDescription: Most efficient Django query to return results spanning multiple tables


code:
stored_data = data.DefaultData()
for data in stored_data.values():
    data['pw'] = md5(salt + data['pw']).hexdigest()

from pprint import pprint
pprint(stored_data)

rawDescription: changing data in a struct in python

description: Changing data in a struct in python


code: np.genfromtxt('File1.csv', delimiter='\t', names=True, usecols=("Time", "Profit"))

rawDescription: Extract columns using "genfromtxt"

description: Extract columns using "genfromtxt"


code: labels = dict( [ value for value in enumerate(idvalue) ] )

rawDescription: Labelling nodes in networkx

description: Labelling nodes in networkx


code:
a2 = "43616c6562"
bytes2 = [a2[i:i+2] for i in range(0,len(a2),2)]
bytes2
''.join([chr(int(c,16)) for c in bytes2])

rawDescription: Decoding hex string gives me garbage

description: Decoding hex string gives me garbage


code:
class FilteredTermsManager(models.Manager):
    def get_query_set(self):
        return super(TermsManager, self).get_query_set().filter(condition_here)

class Term(models.Model):
    allTerms = models.Manger() # Establish this as the default/automatic manager
    objects = FilteredTermsManager()

    name = models.CharField(max_length=255)

rawDescription: Django, override many-to-many field ModelManager


rawDescription: Dealing with a string and byte in python


rawDescription: Numpy - Stacked memory view of two 1D arrays


code:
import functools
def try_x_times(x, exceptions_to_catch, exception_to_raise, fn):
    @functools.wraps(fn) #keeps name and docstring of old function
    def new_fn(*args, **kwargs):
        for i in xrange(x):
            try:
                return fn(*args, **kwargs)
            except exceptions_to_catch:
                 pass
        raise exception_to_raise
    return new_fn

rawDescription: Repeat Python function call on exception?


code:
nk = {'aaa','bbb','ccc'}
seen = set()             #keep as track of items seen so far in this set
with open(sys.argv[1], "rb") as f:
    ...
    for row in reader:
        #update `seen` with the items found common between `nk` and the current `row`
        seen.update(nk.intersection(row))
    ...

rawDescription: Find list of keywords in string

description: Find list of keywords in string


code:
a = {"cars": 1, "houses": 2, "schools": 3, "stores": 4}
b = {"Pens": 1, "Pencils": 2, "Paper": 3}

a.update(b)
print(a)

rawDescription: combine two json dictionary python

description: Combine two json dictionary python


code:
from itertools import product

for y, x in product(range(1, 9), range(4)):
    base = 20 if y % 2 == 0 else 10
    canvas.drawRect(base + (x * 20), y * 10, 10, 10)

rawDescription: Graphics window that creates a checkerboard with 64 squares,python


rawDescription: Looping over a MultiIndex in pandas

description: Looping over a MultiIndex in pandas


code:
# To train the tagger.
me_classifier = nltk.MaxentClassifier.train(train_documents, algorithm='iis', trace=0, max_iter=3)

rawDescription: My Maxent Classifier works fine with gis algorithm but does not work with iis algorithm. It is not throwing any error, just some warnings


code: matrixK = map(list, zip(*[row for i, row in enumerate(zip(*matrixK)) if i % 2]))

rawDescription: Delete columns from matrix according to its index


code:
idx2 = np.argmin(dist, axis = 1)
idx2

rawDescription: Interleave two numpy arrays based on element distances (python)


code:
from pyparsing import *

crazyVariable = Empty().addParseAction(replaceWith('comment')) + cStyleComment

print(crazyVariable.parseString('/* foo bar*/' ))

rawDescription: Pyparsing: How to insert new elements to a ParseResult


code:
savefile = open("backup.json")
li = json.loads(savefile.read())

rawDescription: Saving an Element in an Array Permanently


code:
c = 4
a = lambda p: p - c
a(7)

rawDescription: Sorting a list pairs by frequency of the pair elements


code:
df2 = df.asn.str.split(',').apply(pd.Series)          # break df.asn into columns
df2.index = df.Name                                   # set the index as df.Name
df2 = df2.stack().reset_index('Name')                 # stack and reset_index
df2

rawDescription: iPython Pandas: how to convert a cell with multiple values to multiple row?

description: IPython Pandas: how to convert a cell with multiple values to multiple row


code: xmlData.write( 'test.xml', pretty_print=True )

rawDescription: XML file edit with Python script

description: XML file edit with Python script


code:
for s in book.sheets():
    s.header_str = 'what you prefer'

rawDescription: header/footer in xlwt (python).

description: Header/footer in xlwt (python).


code:
for rownum in range(len(mapping['a'])):
    for l in letters:
        print(mapping[l][rownum], end='')
    print()

rawDescription: Python: Print first dictionary value for all keys on first line, second on second etc


code:
x = np.arange(0, 100, .1)
y = x.copy()

lims = [0, 24]

x = (x % lims[1])

fig, ax = matplotlib.pyplot.subplots()

for slc in unlink_wrap(x, lims):
    ax.plot(x[slc], y[slc], 'b-', linewidth=2)

ax.plot(x, y, 'r-', zorder=-10)
ax.set_xlim(lims)

rawDescription: Preventing plot joining when values "wrap" in matplotlib plots


code:
import win32api, win32con, win32gui, win32ui, win32service, os, time

def f_click(pycwnd):
    x=300
    y=300
    lParam = y <<15 | x
    pycwnd.SendMessage(win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam);
    pycwnd.SendMessage(win32con.WM_LBUTTONUP, 0, lParam);

def get_whndl():
    whndl = win32gui.FindWindowEx(0, 0, None, 'NB.txt - Notepad')
    return whndl

def make_pycwnd(hwnd):
    PyCWnd = win32ui.CreateWindowFromHandle(hwnd)
    return PyCWnd

def send_input_hax(pycwnd, msg):
    f_click(pycwnd)
    for c in msg:
        if c == "\n":
            pycwnd.SendMessage(win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
            pycwnd.SendMessage(win32con.WM_KEYUP, win32con.VK_RETURN, 0)
        else:
            pycwnd.SendMessage(win32con.WM_CHAR, ord(c), 0)
    pycwnd.UpdateWindow()

whndl = get_whndl()

def callback(hwnd, hwnds):
    if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
        hwnds[win32gui.GetClassName(hwnd)] = hwnd
    return True
hwnds = {}
win32gui.EnumChildWindows(whndl, callback, hwnds)
whndl = hwnds['Edit']

pycwnd = make_pycwnd(whndl)
msg = "It works !\n"
send_input_hax(pycwnd,msg)

rawDescription: Python win32api SendMesage

description: Python win32api SendMesage


code:
def is_container_iterable(o):
    try:
        object.__getattribute__(o, '__iter__')
    except AttributeError:
        return False
    try:
        object.__getattribute__(o, '__next__')
    except AttributeError:
        return True
    return False

is_container_iterable([])
is_container_iterable(())
is_container_iterable({})
is_container_iterable(range(5))
is_container_iterable(iter(range(5)))

rawDescription: how to check if an iterable allows more than one pass?

description: Check if an iterable allows more than one pass


code:
df['exchtime'] = pd.to_datetime(df['exchtime'],unit='s')
df

rawDescription: Aggregating over timestamps in pandas

description: Aggregating over timestamps in pandas


code:
csspath = os.path.join(os.path.dirname(__file__), 'static', 'css')
csss = [os.path.join('app_assets', 'css', fname) for fname in os.listdir(csspath)]
app_css = Bundle(*csss, output='packed.css', filters='cssmin')

rawDescription: Get files and put into list in another method


code:
rgb = img_array[:, :, :3]
tdx_n_exp = np.expand_dims(tdx_n, axis=2)
result = tdx_n_exp * rgb
plt.imshow(result[4:-3,4:-3])

rawDescription: Issue creating map shading in Matplotlib imshow by setting opacity to data gradient


code: block = a[y_coords[row, col][:, np.newaxis], x_coords[row, col]]

rawDescription: Indexes of fixed size sub-matrices of numpy array


code:
data = list(get_time_deltas(INFILE))
# .. use data for something some operation ...

# calculate average using the list
average = sum(d for _, d in data) / len(data)

rawDescription: How do I create a list of timedeltas in python?

description: Create a list of timedeltas in python


code: {{ request.current_page }}

rawDescription: render cms page within another page

description: Render cms page within another page


code: lists = numpy.split(image)

rawDescription: Convert a jpg to a list of lists

description: Convert a jpg to a list of lists


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

class wait_for_price(object):
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        try :
            element_text = EC._find_element(driver, self.locator).text.strip()
            return element_text != "0,00"
        except StaleElementReferenceException:
            return False

rawDescription: Amazon web scraping

description: Amazon web scraping


code:
def doit(d, order):
  return  [d[k] for k in sorted(order, key=order.get)]

rawDescription: sort dictionary by another dictionary

description: Sort dictionary by another dictionary


code:
from django.conf import settings
try:
    settings.configure()
except:
    pass
settings.LANGUAGE_CODE = 'zh-tw'
settings.USE_I18N = True
appdir = os.path.abspath( os.path.dirname( __file__ ) )
settings.LOCALE_PATHS = (
    os.path.join( appdir, 'locale' ),
 )
from django.utils.translation import *

rawDescription: how to make site show different language(chinese and english) on google-app-engine

description: Make site show different language(chinese and english) on google-app-engine


code:
mylist = [
    '''A Very very
    long String''',
    '''Another Very
    very
    very
    very long string''',
    ]

rawDescription: Is there any way to make code organized if I have a super long list?


code:
import inspect
s=inspect.signature(x.test)
s.parameters
list(s.parameters.keys())
s.parameters['x'].kind
s.parameters['x'].default

rawDescription: Parse Python file to retrieve variables, values, function definitions


rawDescription: Capture screen using Glut and OpenGL with Python


rawDescription: Converting Pandas groupby.groups result into dataframe, using index tuple value as row and columns name of dataframe


rawDescription: emulating the behavior of findWhere() in python

description: Emulating the behavior of findWhere() in python


code:
#!/usr/bin/env python

import curses

screen = curses.initscr()

try:
    screen.border(0)
    screen.refresh()

    box1 = curses.newwin(20, 20, 5, 5)
    box1.box()
    box1.refresh()

    screen.getch()

finally:
    curses.endwin()

rawDescription: Why won't my curses box draw?

description: Why won't my curses box draw


code:
import tkinter as tk
root = tk.Tk()
but = tk.Button(root, text = 'Hi')
but.pack()
print(but)
def cb(event):
    if event.widget == but:
        print('Success!')
but.bind('<Enter>', cb)
root.mainloop()

rawDescription: Multiple Buttons In One Method Python 3

description: Multiple Buttons In One Method Python 3


code:
from collections import defaultdict
d = defaultdict(list)
d

rawDescription: Simple file to dictionary

description: Simple file to dictionary


code:
class LineHighChart(object):
    #title = {'text':' ', 'x':-20}

    def __init__(self, title, subtitle, x_axis_list, y_axis_title, metric, series):

        self.title = {'text': title, 'x':-20}

        self.subtitle =  {
            'text': subtitle,
            'x': -20
        }

        self.xAxis = {
            'categories': x_axis_list
        }

        self.yAxis = {
            'title': {
                'text':y_axis_title
            },
            'plotLines': [{
                'value': 0,
                'width': 1,
                'color': '#808080'
            }]
        }

        self.tooltip = {
            'valueSuffix': metric
        }

        self.legend = {
            'layout': 'vertical',
            'align': 'right',
            'verticalAlign': 'middle',
            'borderWidth': 0
        }

        self.series = series

rawDescription: populate a class variable with the value of another class variable

description: Populate a class variable with the value of another class variable


code:
utc_date = date_aware_la.astimezone(pytz.utc)
print(utc_date)

rawDescription: Python: How to convert a timezone aware timestamp to UTC without knowing if DST is in effect


code:
b = [[1,2,3],[4,5,6],[1,4,5], [4,7,8]]

def create_dict_2(a):
    my_dict = {}
    for index, sublist in enumerate(a):
        for ele in sublist:
            if not ele in my_dict:
                my_dict[ele] = index
    return my_dict

rawDescription: Having a list of lists - how to get the index of a list containing an item?


code:
df["value"] = 1
P = df.pivot_table(index="a", columns="category", values="value", aggfunc=max)
P.loc[:,category_list.category.unique()].fillna(0)

rawDescription: For Loop alternative Pandas Python

description: For Loop alternative Pandas Python


code:
# keep lowercase characters and digits
# keep apostrophes for contractions (isn't, couldn't, etc)
# convert uppercase characters to lowercase
# replace all other printable symbols with spaces
TO_ALPHANUM_LOWER = str.maketrans(
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ'!#$%&()*+,-./:;<=>?@[]^_`{|}~ \t\n\r\x0b\x0c\"\\",
    "abcdefghijklmnopqrstuvwxyz'                                     "
)

def clean(s):
    """
    Convert string `s` to canonical form for searching
    """
    return s.translate(TO_ALPHANUM_LOWER)

class WordTree:
    __slots__ = ["children", "terminal"]

    def __init__(self, phrases=None):
        self.children = {}   # {"word": WordTrie}
        self.terminal = ''   # if end of search phrase, full phrase is stored here
        # preload tree
        if phrases:
            for phrase in phrases:
                self.add_phrase(phrase)

    def add_phrase(self, phrase):
        tree  = self
        words = clean(phrase).split()
        for word in words:
            ch = tree.children
            if word in ch:
                tree = ch[word]
            else:
                tree = ch[word] = WordTree()
        tree.terminal = " ".join(words)

    def inc_search(self, word):
        """
        Search one level deeper into the tree

        Returns
          (None,    ''    )  if word not found
          (subtree, ''    )  if word found but not terminal
          (subtree, phrase)  if word found and completes a search phrase
        """
        ch = self.children
        if word in ch:
            wt = ch[word]
            return wt, wt.terminal
        else:
            return (None, '')

    def parallel_search(self, text):
        """
        Return all search phrases found in text
        """
        found  = []
        fd = found.append
        partials = []
        for word in clean(text).split():
            new_partials = []
            np = new_partials.append
            # new search from root
            wt, phrase = self.inc_search(word)
            if wt:     np(wt)
            if phrase: fd(phrase)
            # continue existing partial matches
            for partial in partials:
                wt, phrase = partial.inc_search(word)
                if wt:     np(wt)
                if phrase: fd(phrase)
            partials = new_partials
        return found

    def tree_repr(self, depth=0, indent="  ", terminal=" *"):
        for word,tree in self.children.items():
            yield indent * depth + word + (terminal if tree.terminal else '')
            yield from tree.tree_repr(depth + 1, indent, terminal)

    def __repr__(self):
        return "\n".join(self.tree_repr())

rawDescription: Suggestion required - Python code performance improvement


code:
from django.template.defaultfilters import stringfilter
from urllib import unquote

@stringfilter
def unquote_raw(value):
    return unquote(value)

rawDescription: Django urldecode in template file

description: Django urldecode in template file


code:
a=np.zeros((300,128))
b=np.zeros((1000,128))
a.shape
b.shape
c=np.vstack((a,b))
c.shape
len(c)

rawDescription: merge sift descriptor matrix, numpyarray

description: Merge sift descriptor matrix, numpyarray


code:
import os, shutil, re
def rename_file (original_filename):
    name, extension = os.path.splitext(original_filename)
    #remove one or more dashes, surrounded by non-word characters.
    modified_name = re.sub(r"(\W)-+(\W)",r"\1\2",name)
    new_filename = modified_name + extension
    try:
        # moves files or directories (recursively)
        shutil.move(original_filename, new_filename)
    except shutil.Error:
        print ("Couldn't rename file %(original_filename)s!" % locals())

target_dir = r"/home/trinity/nmap"
targets = os.listdir(target_dir)
[rename_file(f) for f in targets]

rawDescription: Renaming files according to a set of rules


code:
res = expr.parseString('foo(bar:baz;x:y)')
type(res['foo'])
{ k:v.asDict() for k,v in res.asDict().items() }

rawDescription: Can't get pyparsing Dict() to return nested dictionary


rawDescription: Python round up integer to next hundred

description: Python round up integer to next hundred


code:
def variableFromOutside(y):
    global x
    x += y
    return y

rawDescription: Calling While loop Function in __main__?

description: Calling While loop Function in __main__


code:
lst = "aaabaabacabaacaca"
assert ''.join(flatten(go(iter(lst)))) == lst

rawDescription: How to flatten a list of list with variable lengths into a single list? python

description: Flatten a list of list with variable lengths into a single list? python


code:
import os
from subprocess import Popen, PIPE
from tempfile import NamedTemporaryFile

from django.template import loader, Context

class LaTeXException(Exception):
    pass

def process_latex(template, context={}, type='pdf', outfile=None):
    """
    Processes a template as a LaTeX source file.
    Output is either being returned or stored in outfile.
    At the moment only pdf output is supported.
    """
    t = loader.get_template(template)
    c = Context(context)
    r = t.render(c)

    tex = NamedTemporaryFile()
    tex.write(r)
    tex.flush()
    base = tex.name
    names = dict((x, '%s.%s' % (base, x)) for x in (
        'log', 'aux', 'pdf', 'dvi', 'png'))
    output = names[type]

    stdout = None
    if type == 'pdf' or type == 'dvi':
        stdout = pdflatex(base, type)
    elif type == 'png':
        stdout = pdflatex(base, 'dvi')
        out, err = Popen(
            ['dvipng', '-bg', '-transparent', names['dvi'], '-o', names['png']],
            cwd=os.path.dirname(base), stdout=PIPE, stderr=PIPE
        ).communicate()

    os.remove(names['log'])
    os.remove(names['aux'])

    # pdflatex appears to ALWAYS return 1, never returning 0 on success, at
    # least on the version installed from the Ubuntu apt repository.
    # so instead of relying on the return code to determine if it failed,
    # check if it successfully created the pdf on disk.
    if not os.path.exists(output):
        details = '*** pdflatex output: ***\n%s\n*** LaTeX source: ***\n%s' % (
            stdout, r)
        raise LaTeXException(details)

    if not outfile:
        o = file(output).read()
        os.remove(output)
        return o
    else:
        os.rename(output, outfile)

def pdflatex(file, type='pdf'):
    out, err = Popen(
        ['pdflatex', '-interaction=nonstopmode', '-output-format', type, file],
        cwd=os.path.dirname(file), stdout=PIPE, stderr=PIPE
    ).communicate()

    # If the output tells us to rerun, do it by recursing over ourself.
    if 'Rerun LaTeX.' in out:
        return pdflatex(file, type)
    else:
        return out

rawDescription: Automating PDF generation

description: Automating PDF generation


rawDescription: Pandas KeyError using pivot

description: Pandas KeyError using pivot


rawDescription: How to get a closure to refer to the value that a variable had at the time of its definition

description: Get a closure to refer to the value that a variable had at the time of its definition


code:
bb_data = Bbox.from_bounds(0.5, 2.5, 1.0, 0.3)
disp_coords = ax.transData.transform(bb_data)
fig_coords = fig.transFigure.inverted().transform(disp_coords)

rawDescription: Convert matplotlib data units to normalized units


code:
def clubs(request):
    data = requests.get('http://127.0.0.1:8000/app/clubs/').json()

    context = RequestContext(request, {
        'club': data,
    })

    return render_to_response('imgui/genres.html', context)

rawDescription: Consuming JSON into django view

description: Consuming JSON into django view


code:
list_products = ["", "", ""]
for i in range(len(list_products)):
     list_products[i] = input("First Product: ")
product1, product2, product3 = list_products

rawDescription: Python: list problens

description: Python: list problens


code:
clean_models = []
for m in tokenized_models:
    stop_m = [i for i in m if str(i).lower() not in stopset]
    clean_models.append(stop_m)

print(clean_models)

rawDescription: Removing stopwords from list using python3


rawDescription: Best way to have a python script copy itself?

description: Have a python script copy itself


code:
test = 'the cat sat on the mat'
words = test.split(' ')
words.index('cat')  # returns 1, add 1 to get the actual position.

rawDescription: How to find the position of a word inside a string in c# or python?

description: Find the position of a word inside a string in c# or python


code:
from fnmatch import fnmatch
fnmatch('John_Michael_Joseph_Smith', 'John*Smith')
fnmatch('John_Michael_Joseph_Smith', 'John_Michael*')

rawDescription: How to retrieve dicts from a list of dicts using wildcard key value

description: Retrieve dicts from a list of dicts using wildcard key value


code:
lis = range(11)
["{:02d}".format(x) for x in lis]

rawDescription: Convert range(r) to list of strings of length 2 in python


code:
class Interface(object):
    def __init__(self, real_object, interface):
        if isinstance(interface, str):
            interface = interface.split()
        self.interface  = interface
        self.object = real_object
        Interface.validate(self)

    def __getattribute__(self, attr):
        # Retrieve the real attributes of self,
        # bypassing the normal attribute mechanism:
        interface = object.__getattribute__(self, "interface")
        real_object =  object.__getattribute__(self, "object")
        if attr in interface:
            return getattr(real_object, attr)
        raise AttributeError

    def validate(self):
        interface = object.__getattribute__(self, "interface")
        real_object =  object.__getattribute__(self, "object")
        for attr in interface:
            try:
                getattr(real_object, attr)
            except AttributeError:
                raise ValueError("Passed object does not conform to given interface")

rawDescription: Selective exposure of methods and variables of one class to another in Python | (interfaces?)


code:
import important
important.private1() # OK

rawDescription: Always include file when importing module in python


code:
class StoredRandomNumber(ndb.Model):
    User = ndb.StringProperty()
    answer = ndb.IntegerProperty()
    GuessNumber = ndb.IntegerProperty(default=0)

rawDescription: Creating a random number between 1-100 that doesn't change everytime


code:
A = [0, 4, 1, 3, 2]
[i for i, v in sorted(enumerate(A), key=lambda iv: iv[1])]

rawDescription: Swapping indices with values in a Python list?


rawDescription: Python: Linking to a dictionary through a text string


code: script.py /path/to/directory

rawDescription: Running unit tests in a test suite (with each test as an individual test script)


code:
with open('FILE2') as file2:
    for line in file2:
        c1, c2, c3 = line.split()
        if (c1, c3) in file1_contents:
            print(c2)

rawDescription: Speed the process of matching two columns using python


code:
value = "ajax=1&htd=20131111&pn=p1&htv=l"
req = urllib2.Request("https://www.google.co.in/trends/hottrends/hotItems", value)

rawDescription: Curl request in python

description: Curl request in python


code:
class Collection(object):
    def __init__(self):
        self._profiles_1d = None

    @property
    def profiles(self):
        """One dimensional profiles"""
        return self._profiles_1d

    @profiles.setter
    def profiles(self, argtuple):
        args, kwargs = argtuple
        self._profiles_1d = profiles_1d(*args, **kwargs)

    @profiles.deleter
    def profiles(self):
        self._profiles_1d = None

rawDescription: Using a class instance as a class attribute, descriptors, and properties


code: od = collections.OrderedDict((item, dct[item]) for item in lst)

rawDescription: In Python, how do I write the contents of nested dictionaries to a file in a certain format?


code:
[Wind1]
Height=1
Direction=2
Speed=3
[Wind2]
Height=3
Direction=2
Speed=1

rawDescription: Write text from EXCEL to text as a LIST using PYTHON or other programming method


code:
df2 = df1.groupby([f(df['ColA'], df['ColB']),
                   g(df['ColC'], df['ColD'])]).sum()

rawDescription: pandas: groupby using a generic key function acting on one of more columns

description: Pandas: groupby using a generic key function acting on one of more columns


code: sorted(a, key=lambda x: difflib.SequenceMatcher(None, x, b).ratio(), reverse=True)

rawDescription: How to sort list of strings by best match (difflib ratio)

description: Sort list of strings by best match (difflib ratio)


code:
from collections import Counter

result = Counter((d['object'], d['color']) for d in inpt)

rawDescription: counting common key-value pairs in a list of dictionaries

description: Counting common key-value pairs in a list of dictionaries


code: df.iloc[:, -6: -1].values.T.ravel()

rawDescription: How do I make one list from multiple columns using Python's Pandas?

description: Make one list from multiple columns using Python's Pandas


code:
from newspaper import Article
import google

with open('output_file.csv', 'wb') as csvfile:
    lil_wayne_writer = csv.writer(csvfile)

    #Search Setup
    for url in google.search('Lil Wayne', num=10, stop=3, pause=0):
        article = Article(url)
        article.download()
        article.parse()
        article.nlp()
        lil_wayne_writer.writerow(
            [
                str(article.title.encode('ascii', 'ignore')),
                str(article.summary.encode('ascii', 'ignore')),
                str(article.url),
                str(article.keywords),
            ]
        )

rawDescription: Python3: How to store information from short script in a CSV file?


code:
@validate(schema=CourseForm(), form='add', post_only=False, on_get=True,
          auto_error_formatter=custom_formatter,
          variable_decode=True)
def submit(self):
    # Do whatever here.
    return 'Submitted!'

rawDescription: Pylons FormEncode with an array of form elements


rawDescription: Limit sum of entries in numpy array by adjusting negative entries


code:
[a for a in dir(public_key) if not a.startswith("_")]
[a for a in dir(private_key) if not a.startswith("_")]
[a for a in dir(private_key.private_numbers()) if not a.startswith("_")]
[a for a in dir(public_key.public_numbers()) if not a.startswith("_")]
[a for a in dir(private_key.private_numbers().public_numbers) if not a.startswith("_")]

rawDescription: Extract modulus n, private exponent and public exponent in Python cryptography library


code:
s = "\\Accounts01\\Accounts02"
"\n".join(s.split("\\\\"))
s = "\\\Accounts01\\\counts02"
"\n".join(s.split("\\\\"))

rawDescription: python split string from file

description: Python split string from file


rawDescription: Overwrite a specific column in a csv file using Python csv module


rawDescription: Return a list of missing values?

description: Return a list of missing values


code:
class Attribute(models.Model):
    name = models.CharField(max_length=20)

class Equipment(models.Model):
    name = models.CharField(max_length=20)
    attributes = models.ManyToManyField(Attribute)

equips = Equipment.objects.filter(
    attributes__name='wheels').exclude(attributes__name='blades')

rawDescription: Django model with filterable attributes

description: Django model with filterable attributes


code:
with open("tmob_notcleaned.csv", "rb") as infile, open("tmob_cleaned.csv", "wb") as outfile:
   reader = csv.reader(infile)
   next(reader, None)  # skip the headers
   writer = csv.writer(outfile)
   for row in reader:
       # process each row
       writer.writerow(row)

# no need to close, the files are closed automatically when you get to this point.

rawDescription: Skip the headers when editing a csv file using Python


code:
# import pyplot
from matplotlib import pyplot as plt

# set up filename to save it
pdf_file = PdfPages(sys.argv[1].split('.')[0] + "_graphs.pdf")

# set up axis object
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])

# plot your data
for i in range(0, len(list_of_data)):
    biorep = int(list_of_figure_key[i].split('.')[1])
    construct = int(list_of_figure_key[i].split('.')[0].split('_')[1])
    ax.plot(time, list_of_data[i], color=color_dict[construct],
        linestyle=linestyle_dict[biorep], label=list_of_figure_key[i] )

# modify axis limits and legend
ax.set_xlabel('time (hours)', fontsize=9)
ax.set_ylabel(ReadType, fontsize=9)
ax.set_xlim(min(time),max(time))
ax.legend(fontsize=8, loc='upper left', bbox_to_anchor=(1, .5), ncol=2)

# save final figure
plt.savefig(pdf_file)

rawDescription: How to shrink plot on x-axis in matplotlib?

description: Shrink plot on x-axis in matplotlib


code:
def my_func(*args):
  return reduce((lambda x, y: x + y), args)
my_func(1,2,3,4)

rawDescription: How to write a python function that adds all arguments?

description: Write a python function that adds all arguments


code:
if request.method == 'POST':
    for prediction in form.predictions:
        store=db.session.query(Fixture_prediction) \
            .filter(Fixture_prediction.user_id==user_id) \
            .filter(Fixture_prediction.fixture_id==prediction.fixture_id.data)\
            .update({'home_score':prediction.home_score.data\
            ,'away_score':prediction.away_score.data})
        db.session.commit()
        flash('Prediction added')
        return redirect(url_for('predictions'))

rawDescription: How do I correctly submit results from a wtformusing Jinja with a Python /Flask setup

description: Correctly submit results from a wtformusing Jinja with a Python /Flask setup


code:
from collections import defaultdict
d = defaultdict(list)

with open(infile, 'rt') as f:
    reader = csv.reader(f,delimiter=",")
    for row in reader:
        d[row[0]].append([row, set(row[2:])])

print([v[0][0] for k, v in d.items() if all(sub[1] == {"0","1"} for sub in v)])

[['John', '078888888', '0', '0', '1']]

rawDescription: How do I remove a dictionary from a list depending on a value being present in a list in Python?

description: Remove a dictionary from a list depending on a value being present in a list in Python


rawDescription: Opening and using Safari

description: Opening and using Safari


rawDescription: Python: Create list from variables as long as they are not None