code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import matplotlib.animation as animation

def step():
    while True:
        yield np.random.randint(4, size=(N, N))

def animate(data, quadmesh):
    quadmesh.set_array(data.ravel())
    return [quadmesh]

N = 12
maze = np.random.randint(4, size=(N, N))

fig, ax = plt.subplots()
cmap = mcolors.ListedColormap(['w','grey','green','red'])
x, y = np.meshgrid(np.arange(N), np.arange(N))
quadmesh = ax.pcolormesh(x, y, maze, edgecolor='k',cmap=cmap)

ani = animation.FuncAnimation(
    fig, animate, step,
    interval=10, fargs=(quadmesh,), repeat=True, blit=True)
plt.show()

rawDescription: Python - matplotlib.pyplot ion() slow

description: Python - matplotlib.pyplot ion() slow


code:
def getArea(self):
    if (self.type == "Square"):
        return self.width * self.height
    elif (self.type == "Circle"):
        return math.pi*(self.radius**2)

rawDescription: Python Abstract Programming

description: Python Abstract Programming


code:
import maya.cmds as cmds

cmds.window()
cmds.frameLayout()
cmds.animCurveEditor()
cmds.showWindow()

rawDescription: How can I create a new Graph Editor in Maya using Python?

description: Create a new Graph Editor in Maya using Python


code:
import re
re.split('\W+', 'Words, words, words.')

rawDescription: How to split line at non-printing ascii character in Python

description: Split line at non-printing ascii character in Python


rawDescription: Python parsing list of string

description: Python parsing list of string


code:
import random
nums = [10*random.random() for x in range(5)]

rawDescription: Create a random array with a particular average


code:
responsemap = {
    (True,  True,  False): "a and b",
    (True,  False, True):  "a and c",
    (True,  True,  True):  "a and b and c"
}

def Hello(a=None, b=None, c=None):
    return responsemap.get((bool(a), bool(b), bool(c)), None)

rawDescription: method overloading python

description: Method overloading python


code: from django.template import RequestContext

rawDescription: Basic MVT issue in Django

description: Basic MVT issue in Django


code:
import networkx as nx
class MyGraph(nx.Graph):
    def __init__(self, data=None, val=None, **attr):
        super(MyGraph, self).__init__()
        self.val = val
        self.is_built = True

    def connected_component_iter(self):
        """
        Yields connected components.
        """
        assert self.is_built is True
        for subgraph in nx.connected_component_subgraphs(self):
            yield subgraph

    def subgraph(self, nbunch):
        bunch =self.nbunch_iter(nbunch)
        # create new graph and copy subgraph into it
        H = self.__class__(val=self.val)
        # copy node and attribute dictionaries
        for n in bunch:
            H.node[n]=self.node[n]
        # namespace shortcuts for speed
        H_adj=H.adj
        self_adj=self.adj
        # add nodes and edges (undirected method)
        for n in H.node:
            Hnbrs={}
            H_adj[n]=Hnbrs
            for nbr,d in self_adj[n].items():
                if nbr in H_adj:
                    # add both representations of edge: n-nbr and nbr-n
                    Hnbrs[nbr]=d
                    H_adj[nbr][n]=d
        H.graph=self.graph
        return H

G = MyGraph(val='val')
G.add_edges_from([(0, 1), (1, 2), (1, 3), (3, 5), (3, 6), (3, 7), (4, 8), (4, 9)])

for subgraph in G.connected_component_iter():
    print(subgraph.nodes(), subgraph.val)

rawDescription: inheriting networkx Graph and using nx.connected_component_subgraphs

description: Inheriting networkx Graph and using nx.connected_component_subgraphs


code:
from collections import Counter

Output = Counter(t[0] for t in TupleOfTuples).items()

rawDescription: How can I convert this tuple of tuples into a count of its elements?

description: Convert this tuple of tuples into a count of its elements


code:
a = a[..., np.newaxis]
# or
a = a.reshape(a.shape + (1,))

rawDescription: How can I create an array of 1-element arrays from an array?

description: Create an array of 1-element arrays from an array


code:
a = "I am inclin- ed to ask simp- le quest- ions"
result = re.findall('([a-zA-Z]+)-\s+(\w+)', a)
[''.join(item) for item in result]

rawDescription: removing relevant hyphens in text

description: Removing relevant hyphens in text


code:
class User(ModelBase):
    def __init__(self):
        self.name = None
        self.hats = []

rawDescription: Issue with multiple records of the same subdocument returned for MongoDb and Python


code:
def list_to_tree(data):
    out = {
        0: { 'id': 0, 'parent_id': 0, 'name': "Root node", 'sub': [] }
    }

    for p in data:
        out.setdefault(p['parent_id'], { 'sub': [] })
        out.setdefault(p['id'], { 'sub': [] })
        out[p['id']].update(p)
        out[p['parent_id']]['sub'].append(out[p['id']])

    return out[0]

rawDescription: Building a menu tree from an adjacency list in Python


rawDescription: Generate list of range tuples with given boundaries in python


code:
def findLine(prog, target):
   for l,line in enumerate(prog):
      progX = line.split()
      if progX[0] == target:
          return l  #This is the one we want, return the index

rawDescription: Python 3 - Finding which index of a list contains a particular string as a substring


rawDescription: Pandas compare next row

description: Pandas compare next row


code:
df2 = pd.DataFrame(df2.stack('SampleLocation', dropna=False))
df2_lagged = df2_lagged.stack('SampleLocation', dropna=False)

rawDescription: Use Python/Pandas to Match Sample Pairs Yearly Data


code:
from multiprocessing import Pool
pool = Pool(processes=4)              # start 4 worker processes
result_list = pool.map(process_x, my_list)

rawDescription: MultiThreading with a python loop

description: MultiThreading with a python loop


code:
class Foo(object):
  def method(self):
    pass

f = Foo()

rawDescription: Different way to create an instance method object in Python


rawDescription: Is there a simple string parser for Python?


code:
test1
test2

rawDescription: Selecting Tags With Multiple Part Class in BeautifulSoup


code: data = np.array(data[::-1])

rawDescription: Sorted data while plotting heatmap

description: Sorted data while plotting heatmap


rawDescription: pandas pivot dataframe to 3d data

description: Pandas pivot dataframe to 3d data


code:
import io

print(isinstance(io.IOBase(), ABCFileLike))  # False
ABCFileLike.register(io.IOBase)
print(isinstance(io.IOBase(), ABCFileLike))  # True

rawDescription: How to make a file-like class work with `isinstance(cls, io.IOBase)`?

description: Make a file-like class work with `isinstance(cls, io.IOBase)`


code: option = request.form['options']

rawDescription: flask handle form with radio buttons

description: Flask handle form with radio buttons


rawDescription: Pandas: get index of removed row

description: Pandas: get index of removed row


rawDescription: column max with list of matrices

description: Column max with list of matrices


code:
layout = QtGui.QHBoxLayout()
# add all the checkboxes
for kick in self.kicks:
    layout.addWidget(kick)
# tell your widget to actually use this layout
self.setLayout(layout)

rawDescription: Qt - Create Widget dynamically

description: Qt - Create Widget dynamically


code: [itertools.islice(lst, i, None, 2) for i in range(2)]

rawDescription: Python: How to transform a 'flat' sequence into a sequence of tuples?


code:
class Test:
    def __init__(self):
        pass

    @LazyProperty
    def test(self):
        print('Doing some very slow stuff.')
        return 100

rawDescription: Declaring class attributes in __init__ vs with @property


rawDescription: Query current directory in Python (the one the script is running from)


code: t.sort_values(('Count', 2016))

rawDescription: Pandas Sort MultiIndex Pivot Table

description: Pandas Sort MultiIndex Pivot Table


rawDescription: Graphs: contours, arrow and naming

description: Graphs: contours, arrow and naming


code:
thread = Thread(target=datapath_thread, args=(
    controller,
    datapaths.listDatapaths,
    "datapaths.log",
    counter
))

rawDescription: how to pass a function to a tuple from a thread

description: Pass a function to a tuple from a thread


code:
import geocoder
import multiprocessing as mp
import pandas as pd

def reverse_gecode(coordinates):
    return geocoder.google(coordinates, method = 'reverse').postal

if __name__ == '__main__':
    gps = pd.DataFrame({'lat': [27.950575, 40.6936488],
                        'lon': [-82.4571776, -89.5889864]})  # dataframe mehtod
    coords = gps[['lat','lon']].astype(str).apply(lambda x: (x[0],x[1]), axis=1).tolist()
    # multiprocessings
    pool = mp.Pool(processes = (mp.cpu_count() - 1))
    gps['zip_code'] = pool.map(reverse_gecode, coords)
    print(gps)
    pool.close()
    pool.join()

rawDescription: python: using multiprocessing with a dataframe for geocoding

description: Python: using multiprocessing with a dataframe for geocoding


code:
class AddressEntryForm(Form):
    name = TextField()

class AddressesForm(Form):
    """A form for one or more addresses"""
    addresses = FieldList(FormField(AddressEntryForm), min_entries=1)

rawDescription: Add input fields dynamically with wtforms


code:
from collections import defaultdict

self.files = defaultdict(list)

for file in files:
    lexer = guess_lexer__for_filename(file, None)
    self.files[type(lexer)].append(file)

rawDescription: Is it reasonable to use type(a) as a dictionary key?


code:
import networkx as nx
G = nx.MultiGraph()
G.add_edge(1, 2, key='xyz', weight=2)
G.add_edge(1, 2, key='abc', weight=1)
G.edges(data=True)

rawDescription: Changing edge attributes in networkx multigraph


rawDescription: Compare 2d numpy array to itself to form 3d array


rawDescription: Sage plot slope field and differential equation


rawDescription: Python Pandas: Comparison of Min Max Values Per Group


rawDescription: How to cross 2 two-dimensional arrays in python?

description: Cross 2 two-dimensional arrays in python


rawDescription: Numpy - summing up a list of vectors

description: Numpy - summing up a list of vectors


code:
import re
sample = 'I want this string to be splitted into smaller strings.'
re.findall(r't[^s]*s', sample, flags=re.I)

rawDescription: Split string multiple times in Python

description: Split string multiple times in Python


code:
end = len(somelist)
while end and somelist[end - 1][1] == 0:
    end -= 1
newlist = somelist[:end]

rawDescription: Filter elements from the end of a list

description: Filter elements from the end of a list


code:
from __future__ import print_function

for x in range(3):
    print('*', end='')

rawDescription: How to print from a loop onto a single line

description: Print from a loop onto a single line


code:
import re
YEAR = "\d{4}"
DATE = "\d\d\d\d-\d\d-\d\d"
def parse_citation(s):
    regex = "(.*?) CiteSeerX\s+{date} {date} ({year}).*$".format(date=DATE, year=YEAR)
    data = re.findall(regex, s)
    if data:
        # we have a match extract the first group
        return data[0]
    else:
        return None

c1 = """May god bless our families studied. CiteSeerX  2009-05-24 2007-11-19 2004"""
c2 = """May god bless our families studied. CiteSeerX 2009-05-24 2007-11-19 2004 application/pdf text http //citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.1.1483 http //www.biomedcentral.com/content/pdf/1471-2350-5-20.pdf en Metadata may be used without restrictions as long as the oai identifier remains attached to it."""
print(parse_citation(c1))
print(parse_citation(c2))
# Output:
# ('May god bless our families studied.', '2004')
# ('May god bless our families studied.', '2004')

rawDescription: How to extract text before a specific keyword in python?

description: Extract text before a specific keyword in python


code:
while menu != None:
    menu = lipgui.choicebox("Select an option:", choices=["choice1", "choice2", "choice3"])
    file = open("file.txt", "a")
    if menu == "choice1":
       text_to_write = lipgui.enterbox("Text to write:")
       file.write(text_to_write)

rawDescription: Python how to keep writing to a file without erasing what's already there


code:
def plus_minus(s):
    for t in itertools.product(["", "+", "-"], repeat=len(s) - 1):
        yield "".join(itertools.chain.from_iterable(zip(s, t))) + s[-1]

rawDescription: Taking a string of numbers and inserting + and - operators


rawDescription: Analysis of running time on recursive function


code:
driver.find_element_by_class_name("_LJ").click()
# or
driver.find_element_by_class_name("_qxg").click()
# or
driver.find_element_by_class_name("xpdarr").click()
# or
driver.find_element_by_class_name("_WGh").click()
# or
driver.find_element_by_class_name("vk_arc").click()

rawDescription: Clicking Specific button with Selenium

description: Clicking Specific button with Selenium


rawDescription: How to view all elements of a Python Dictionary for Just One Value of one of the Elements in a Tuple Key

description: View all elements of a Python Dictionary for Just One Value of one of the Elements in a Tuple Key


code: ci[unq_i] = unq_ci

rawDescription: Vectorized version of for-loop + numpy.where


code:
obj_1 = {u'd':{u'results': [{u'Desc':u'This is a description...',
                            u'Title':u'This is a title...'},

                            {u'Desc':u'This is a description...',
                            u'Title':u'This is a title...'}]
        }}

rawDescription: Extracting info from a python dictionary


code: writeRow = ['{:0>3}'.format(t), len(c) , ','.join(c.keys()), ','.join(c.values())]

rawDescription: Writing csv with python

description: Writing csv with python


code:
foo= '\u65E5\u672C\u8A9E'
url= 'http://www.example.jp/something?foo='+urllib.parse.quote(foo, encoding= 'cp932')

rawDescription: how to open a URL with non utf-8 arguments

description: Open a URL with non utf-8 arguments


code: df.select(((col("UK") + col("US") + col("CAN")) / lit(3)).alias("mean"))

rawDescription: Spark DataFrame: Computing row-wise mean (or any aggregate operation)


code: return redirect(reverse('about'))

rawDescription: Can any one help me to learn reverse url in django


code:
[{'End Date': '16/11/2009', 'Description': '', 'All day event': 'false',
  'Start Time': '10:00 PM', 'End Time': '11:00 PM', 'Start Date': '16/11/2009',
  'Subject': 'Play football'},
 {'End Date': '20/11/2009', 'Description': '', 'All day event': 'false',
  'Start Time': '07:00 PM', 'End Time': '08:00 PM', 'Start Date': '20/11/2009',
  'Subject': 'Watch 2012'}]

rawDescription: I am getting this sort of CSV data while making Http request to the CSV file. Very malformed string


code:
for key, value in result.items():
   result[key] = [v for v in value if v != '']

rawDescription: Iterate an operation on a dictionary in python


code:
import os
import pandas as pd

df = pd.DataFrame()
xls = pd.ExcelFile(path)

rawDescription: How to read Excel Workbook (pandas)

description: Read Excel Workbook (pandas)


code: df1 = data[(data['B'] == const1) & (data['C'] == const2) & (data['E'] == const3)]

rawDescription: python pandas beginner: multi-dimensional data-analysis workflow (groupby+agg+plot)

description: Python pandas beginner: multi-dimensional data-analysis workflow (groupby+agg+plot)


code:
from pyspark.mllib.fpm import FPGrowth

sets = [
    FPGrowth.FreqItemset(
       items=[u'A_String_0'], freq=303),
    FPGrowth.FreqItemset(
        items=[u'A_String_0', u'Another_String_1'], freq=302),
    FPGrowth.FreqItemset(
        items=[u'B_String_1', u'A_String_0', u'A_OtherString_1'], freq=301)
]

[x for x in sets if x.items[0] != 'A_String_0']
## [FreqItemset(items=['B_String_1', 'A_String_0', 'A_OtherString_1'], freq=301)]

rawDescription: Python 2.7 : delete item from list by value


code:
def scraper(self, **kwargs):

    if kwargs.get('year') is not None:
        scrapeurl = '{0}&manufacturer={manufacturer}&model={model}&year={year}'
        return someFinalFunction() # not sure why this takes no arguments

    elif kwargs.get('model') is not None:
        scrapeurl = '{0}&manufacturer={manufacturer}&model={model}'

    elif kwargs.get('manufacturer') is not None:
        scrapeurl = '{0}&manufacturer={manufacturer}'

    else:
        raise KeyError

    j = getJSONFromUrl(scrapeurl.format(url, **kwargs))
    key, values = j.popitems()

    for value in values:
        kwargs[key] = value
        return self.scraper(**kwargs)

rawDescription: Dynamically assigning variable with locals() isn't working in recursive function


code:
import sys
from PyQt5.QtCore import QObject, QUrl, Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine

def show(text):
    print(text)

if __name__ == "__main__":
  app = QApplication(sys.argv)
  engine = QQmlApplicationEngine()
  engine.load('main.qml')
  win = engine.rootObjects()[0]
  win.textUpdated.connect(show)
  win.show()
  sys.exit(app.exec_())

rawDescription: Qt-how to connect a function to a signal in qml with python? using QQmlApplicationEngine


rawDescription: Python - Seaborn: Modifying the heatmap legend


code:
class TextPickleType(PickleType):
    impl = Text

import json
class SomeOtherEntity(Base):
    __tablename__ = 'some_other_entity'
    id = Column(Integer, primary_key=True)
    attributes = Column(TextPickleType(pickler=json))

rawDescription: Python dicts in sqlalchemy

description: Python dicts in sqlalchemy


rawDescription: Looking for dictionary words in text file using dictionary in python


code:
strings = 'a b c d e f g'
mylist = strings.split()
for x, y in zip(mylist, mylist[1:]):
    print(x, y, sep='', end=' ') # ab bc cd de ef fg

rawDescription: Python: Easier way to change/edit items in a list?


rawDescription: Condensing Multiple List Comprehensions

description: Condensing Multiple List Comprehensions


code:
hello
foo

rawDescription: Python calling a function with the same name as named argument


code:
def _publish_artifact(name, version, path, summary):
    base_url = 'http://server:8081/artifactory/{0}'.format(PYPI_REPOSITORY)
    properties = ';version={0};pypi.name={1};pypi.version={0};pypi.summary={2}'\
             .format(version, name, summary)
    url_path = '/Company/{0}/{1}/{0}-{1}.zip'.format(name, version)
    url = '{0}{1}{2}'.format(base_url, properties, url_path)

    dist_file = r'{0}\dist\{1}-{2}.zip'.format(path, name, version)
    files = {'upload_file': open(dist_file, 'rb')}
    s = requests.Session()
    s.auth = ('username', 'password')
    reply = s.put(url, files=files)
    logger.info('HTTP reply: {0}'.format(reply))

rawDescription: Artifactory PyPi repo layout with build promotion


rawDescription: Fastest way to populate wx.Choice?

description: Fastest way to populate wx.Choice


code:
def uniqueFrequency(newWords):
    '''Function returns a list of unique words with amount of occurances of that
word in the text file.'''
    var = "";
    unique = sorted(set(newWords.split()))
    for i in unique:
        var = str(var) + i + " " + str(newWords.count(i)) + "\n"
    return var

rawDescription: Unique words save to text file as a word per line


code:
def lettercount(pos):
    return {c: pos.count(c) for c in pos}

rawDescription: frequency of letters in column python

description: Frequency of letters in column python


code:
from itertools import chain
myset = set(chain(iterable1, iterable2, iterable3, iterable4))

rawDescription: Creating unique list of objects from multiple lists


code:
ob_3 = three()
ob_3.data1

rawDescription: Simple python communication class error?

description: Simple python communication class error


code:
@pytest.yield_fixture(scope='session')
def server():
    server = Server(host,env, dns, random_name, random_passwd)
    server.create()
    yield server
    server.delete()

rawDescription: Pytest - create an object shared by multiple tests


rawDescription: Numpy - add row to array

description: Numpy - add row to array


code:
def init():
    print('myplugin2')

rawDescription: Telling __import__ where to look - am I stuck with sys.path?


code:
def bar(func, iterable):
    return [func(i) for i in iterable if i]
import dis; dis.dis(bar)

rawDescription: Python 3: Most efficient way to create a [func(i) for i in range(N)] list comprehension


code:
scores = []
for i in range(scoresno):
    score = input("Enter exam score: ")
    scores.append(score)

total = sum(scores)
average = total/float(scoresno)

rawDescription: Sum all range variables in Python?

description: Sum all range variables in Python


rawDescription: Numbers in a Matrix

description: Numbers in a Matrix


code:
import pandas as pd
fin_series.resample("q",pd.Series.last_valid_index)

rawDescription: pandas asfreq returns NaN if exact date DNE

description: Pandas asfreq returns NaN if exact date DNE


rawDescription: pyparsing: getting results from parsed data

description: Pyparsing: getting results from parsed data


code:
if item == 'encode_b64':
    process = base64.encodestring
elif item == 'encode_url':
    process = urllib.quote_plus
else:
    process = default_encode

def default_encode(s):
    s.encode("hex")
...
bottomtext.insert(INSERT, process(var))

rawDescription: Building commands from variables in Python and Tkinter


rawDescription: python find intersection timeranges in array

description: Python find intersection timeranges in array


code:
def MyFunc(X): return ones((5,2,3)) if X%2 else array([])
concatenate([MyFunc(X).reshape((-1,2,3)) for X in range(6)]).shape

rawDescription: Build an ever growing 3D numpy array

description: Build an ever growing 3D numpy array


rawDescription: how to set on zero 3d numpy array?

description: Set on zero 3d numpy array


code:
import csv

reader = csv.reader(
    open('file.text', 'rb'),
    quotechar="'", skipinitialspace=True)
thelist = list(reader)

rawDescription: Strip outside quotes from text when writing to list in Python


code:
from datetime import datetime,time

s = "11048 10386 11717532234 1171753234 19"

start = time(8, 0, 0) # start 8:00:00 am
end = time(20, 0, 0) # end 8:00:00 pm
spl = s.split() # split line

# convert col 2 and 3 to datetime objects
t1 = datetime.fromtimestamp(float(spl[2])).time()
t2 = datetime.fromtimestamp(float(spl[3])).time()

print(t1 >= start and t2 <= end)

rawDescription: Obtain data from one time to another

description: Obtain data from one time to another


code:
def read_feature_bin_file(filepath):

    with open(filepath, 'rb') as binf:
        header = numpy.fromfile(binf, count=3, dtype='<i4')
        if header[2] not in [1, 5]:  # Check endianness
            header = header.view('>i4')

        rows, cols, dt = header
        dtype = 'u1' if dt==1 else header.dtype.byteorder + 'f4'
        features = numpy.fromfile(binf, dtype)

    features.shape = (rows, cols)
    return features

rawDescription: Safest way to save binary file with numpy to be read


code:
empList = []
empName = 'Lastname, Firstname'
nameSplit = tuple(empName.split(","))
empList.append(nameSplit)
empList
empName = 'Else, Someone'
nameSplit = tuple(empName.split(","))
empList.append(nameSplit)
empList

rawDescription: Input into tuple that goes into list

description: Input into tuple that goes into list


code: R = map(lambda x: (x), R);

rawDescription: How to find the index of an array within an array

description: Find the index of an array within an array


code:
_x_singleton = None

def XFactory():
    global _x_singleton

    if _x_singleton is None:
        _x_singleton = X()
    return _x_singleton

rawDescription: python: get constructor to return an existing object instead of a new one

description: Python: get constructor to return an existing object instead of a new one


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

x = np.arange(100)
y = x
t = x
plt.scatter(x, y, c=t)
plt.show()

rawDescription: Scatter plot and Color mapping in Python


code: b = arr(a)

rawDescription: How to plot an image with non-linear y-axis with Matplotlib using imshow?

description: Plot an image with non-linear y-axis with Matplotlib using imshow


rawDescription: Python Creating a nested list with random module


code: a += flattenList(i)

rawDescription: Flattening a list recursively - why doesn't my code return what I want?


rawDescription: Generating Silence with pyDub

description: Generating Silence with pyDub


code:
class Search:  # The class does not include the search methods, at first
    def __init__(self):
        self.conditions = {}

def make_set_condition(option):  # Factory function that generates a "condition setter" for "option"
    def set_cond(self, value):
        self.conditions[option] = value
        return self
    return set_cond

for option in ('price', 'name'):  # The class is extended with additional condition setters
    setattr(Search, option, make_set_condition(option))

Search().name("Nice name").price('$3').conditions  # Example
{'price': '$3', 'name': 'Nice name'}

rawDescription: Dynamically add methods to a class in Python 3.0


rawDescription: How can I slice a timeseries dataframe in Pandas based on multiple conditions?

description: Slice a timeseries dataframe in Pandas based on multiple conditions


code: someList += [someValue] * someNumber

rawDescription: Pythonic way to add several identical values to a list


rawDescription: Python SqlAlchemy order_by DateTime?

description: Python SqlAlchemy order_by DateTime


code:
# write some random arrays to an example binary file
x = np.random.randn(100, 200)
y = np.random.randn(300, 400)

with open('/tmp/testbin', 'wb') as f:
    np.array(x.shape).tofile(f)
    x.tofile(f)
    np.array(y.shape).tofile(f)
    y.tofile(f)

# read the contents back
x1, y1 = iter_arrays('/tmp/testbin')

# check that they match the input arrays
assert np.allclose(x, x1) and np.allclose(y, y1)

rawDescription: How can I read successive arrays from a binary file using `np.fromfile`?

description: Read successive arrays from a binary file using `np.fromfile`


code:
def log_ceil(num, base=2):
    """Calculate the 'ceiling log' for the specified number."""
    val = 0
    while (base ** val) < num:
        val += 1
    return val

rawDescription: LgCeil - My professor says my code does not work for 1<n<=4


code:
index = 0
while index < len(list):
   if s == "b" or s == "c":
       del list[index]
       continue
   index += 1

rawDescription: how to correctly modify the iterator of a loop in python from within the loop

description: Correctly modify the iterator of a loop in python from within the loop


code:
word = '1xxx1'
from_char = 'x'
to_char = '5'
[(c,) if c != from_char else (from_char, to_char) for c in word]

rawDescription: String Replacement Combinations

description: String Replacement Combinations


code:
def find_prefix_suffixes(strings, prefixes):
    prefix_suffix = dict()
    for s in strings:
        for prefix in sorted(list(prefixes), key=lambda x: len(x), reverse=True):
            if s.startswith(prefix):
                if prefix in prefix_suffix:
                    prefix_suffix[prefix].add(s.replace(prefix, '', 1))
                else:
                    prefix_suffix[prefix] = set([s.replace(prefix, '', 1)])
    return prefix_suffix

rawDescription: Left factoring using Python

description: Left factoring using Python


code:
def replace_groups(data):
    a,b,c, = np.unique(data, True, True)
    _, ret = np.unique(b[c], False, True)
    return ret

rawDescription: numpy replace groups of elements with integers incrementally

description: Numpy replace groups of elements with integers incrementally


code:
import re
bool(re.match(br'\d+$', b'123'))

rawDescription: Python - How can I change bytes in a file


code:
d = {'foo': 'bar', 'baz': 'bazbaz'}
d1 = {'foo': 'baz'}
keys = d.viewkeys() | d1.viewkeys()
{k : d.get(k, '') + d1.get(k, '') for k in keys}

rawDescription: Python - Combine two dictionaries, concatenate string values?


code:
from string import punctuation
text = 'i like: a, b, 007 and c!!'
re.findall('\w+|[{0}]+'.format(punctuation),text)

rawDescription: python regex: how to split string into distinct groups based on alphabets, digits and punctuation

description: Python regex: how to split string into distinct groups based on alphabets, digits and punctuation


code:
f = bz2.BZ2File(path)
f.seek(512)
headerbytes = f.read(1024)

rawDescription: Read large file header (~9GB) inside tarfile without full extraction


code: np.savetxt( "filename.txt", your_array )

rawDescription: Writing numpy arrays to files

description: Writing numpy arrays to files


code:
class TestProduction(object):
    @patch("production_code.os_name","posix")
    def test_platform_string_posix(self):
        assert_equal('posix-y path', production.platform_string())

rawDescription: python cross platform testing: mocking os.name

description: Python cross platform testing: mocking os.name


code: age = models.IntegerField(blank=True, null=True)

rawDescription: How to set NULL for IntegerField instead of setting 0?

description: Set NULL for IntegerField instead of setting 0


rawDescription: Reading data from specially formatted text file


code:
pivoted = df.pivot('ID', 'Output', 'ID')
pivoted.fillna(0, inplace=True)
pivoted[pivoted > 0] = 1
pd.merge(pivoted.reset_index(), df[['ID', 'Timestamp']].drop_duplicates(), left_on='ID', right_on='ID')
pivoted

rawDescription: Python multiple rows to one row

description: Python multiple rows to one row


code:
my_list = ['1\tMelkor\tMorgoth\tSauronAtDolGoldul','2\tThingols\tHeirIsDior\tSilmaril','3\tArkenstone\tIsProbablyA\tSilmaril']
[sub.split('\t') for sub in my_list]

rawDescription: Partitioning elements in list by \t . Python


rawDescription: Two bar charts in matplotlib overlapping the wrong way


code: lines = output.splitlines()

rawDescription: How do I get all of the output from my .exe using subprocess and Popen?

description: Get all of the output from my .exe using subprocess and Popen


code:
content = r'''\documentclass{article}
\begin{document}
...
\textbf{\huge %(school)s \\}
\vspace{1cm}
\textbf{\Large %(title)s \\}
...
\end{document}
'''

rawDescription: Generating pdf-latex with python script

description: Generating pdf-latex with python script


code:
class DescName(object):

    def __init__(self):
        self.cache = {None: self}

    def __get__(self, obj, cls=None):
        return self.cache[obj]

    def __set__(self, obj, value):
        cls = obj.__class__

        if value in cls._names:
            raise AttributeError('EXISTING NAME %s' % value)

        try:
            cls._names.remove(self.cache[obj])
        except KeyError:  # 1st time name is used
            pass
        cls._names.add(value)
        self.cache[obj] = value

class MyClass(object):
    _names = set()

    name = DescName()

    def __init__(self, name, *args, **kwargs):
        self.name = name

a = MyClass('hello')
print('a.name:', a.name)
b = MyClass('goodbye')
print('b.name:', b.name)

try:
    c = MyClass('hello')
except AttributeError:
    print('Duplicate Name caught')
else:
    print('c.name:', c.name)

a.name = 'see you again'
print('a.name:', a.name)

try:
    a.name = b.name
except AttributeError:
    print('CANNOT SET a.name to b.name')
else:
    print('a.name %s = %s b.name' % (a.name, b.name))

rawDescription: Ensure uniqueness of instance attribute in python


code:
import subprocess as sp
programName = "notepad.exe"
fileName = "file.txt"
sp.Popen([programName, fileName])

rawDescription: Open a text file using notepad as a help file in python?


code:
def parse_shot_success(string):
    if 'scores' in string:
        return 'scores'
    elif 'blocks' in string or 'misses' in string:
        return 'misses'
    else:
        return 'not a shot'

rawDescription: Searching fro string and converting it to another string python


code:
with plt.style.context('/path/to/stylesheet'):
    plt.plot([1, 2, 3, 4])

rawDescription: Is there a context manager for temporarily changing matplotlib settings?


code:
import collections
import datetime
date_bins = collections.defaultdict(list)
for record in records:
    datepart, _ = record.split("T")
    date = datetime.datetime.strptime(datepart, "%Y-%m-%d")
    date_bins[date].append(record)

rawDescription: How to categorize and count imported data

description: Categorize and count imported data


code:
with open("util/file_reader.py") as f:
    src = f.read()

# set command line arguments
import sys
sys.argv = ["file_reader.py", "C:/file_address"]

# use "__main__.py" as file name, so module thinks its the main module
code = compile(src, "__main__.py", "exec")
exec(code)

rawDescription: Equivalent of Ipython run command in normal python


code:
if request.method == "POST":
 theForm = UploadForm(request.POST)
 if theForm.is_valid():
  theUploadedFileName=theForm.cleaned_data["uploadedFile.name"]

rawDescription: How to use non valid identifiers as django form field names

description: Use non valid identifiers as django form field names


code:
from yourproject.decorators import sso_login_required
from django.utils.decorators import method_decorator

class SSOMixin(object):

    @method_decorator(sso_login_required)
    def dispatch(self, *args, **kwargs):
        return super(SSOMixin, self).dispatch(*args, **kwargs)

rawDescription: Customize Django class base view at global (project wide)


code: firstRow = dict.fromkeys('qwertyuiop', 1)

rawDescription: What's the most pythonic way to build this dictionary?


rawDescription: How to give id to point and draw line point to point using matplotlib.plyplot in python

description: Give id to point and draw line point to point using matplotlib.plyplot in python


code: cursor = db.cursor(dictionary=True)

rawDescription: python mysql.connector DictCursor?

description: Python mysql.connector DictCursor


code:
def __getitem__(self, key):
    if key == "temperature":
        return CurrentWeatherTemperature()
    else:
        return self.data[key]

rawDescription: what is the dict class used for

description: What is the dict class used for


code:
def add_months(sourcedate,months):
    month = sourcedate.month - 1 + months
    year = sourcedate.year + month / 12
    month = month % 12 + 1
    day = min(sourcedate.day,calendar.monthrange(year,month)[1])
    return datetime.date(year,month,day)

rawDescription: converting string 'yyyy-mm-dd' into datetime python

description: Converting string 'yyyy-mm-dd' into datetime python


code:
temp_dict = dict(zip(["dept", "sku"], key))
temp_list = [item["qty"] for item in grp]
temp_dict["qty"] = sum(temp_list)
temp_dict["avg"] = temp_dict["qty"] / len(temp_list)
result.append(temp_dict)

rawDescription: Group by multiple keys and summarize/average values of a list of dictionaries


code: reader = csv.reader(csvfile, dialect='excel', delimiter=',', quotechar='|')

rawDescription: How to parse of a simple csv file?

description: Parse of a simple csv file


rawDescription: How to pickle individual steps in sklearn's Pipeline?

description: Pickle individual steps in sklearn's Pipeline


code: from SomeModule import *

rawDescription: sum function in python

description: Sum function in python


rawDescription: Rotate pandas DataFrame 90 degrees

description: Rotate pandas DataFrame 90 degrees


code:
DF = DataFrame()
for sample,data in D_sample_data.items():
    SR_row = pd.Series(data.D_key_value)
    DF = DF.append(SR_row,ignore_index=True)

rawDescription: Is it possible to append Series to rows of DataFrame without making a list first?


code:
with open('somefilename') as f:
    for token in read_by_tokens(f):
        print(token)

rawDescription: Python: Read whitespace separated strings from file similar to readline


rawDescription: Conditional counting within groups

description: Conditional counting within groups


code:
df['rwg'] = np.nan
df.loc[df[2]=='reward', 'rwg'] = range(1, sum(df[2]=='reward')+1 )
df['rwg'] = df['rwg'].ffill()   # bfill() if you want to group the other way
df['rwg'] = df['rwg'].fillna(0) # the initial group without a preceding reward
dfrwg = df.groupby('rwg').sum()
print(dfrwg)

rawDescription: Grouping pandas data frame by time intervals


rawDescription: Dealing with missing data in an one column text file


code:
def parse(text):
    chunks = ['']

    for character in text:
        if character.isdigit():
            if chunks[-1].isdigit():   # If the last chunk is already a number
                chunks[-1] += character  # Add onto that number
            else:
                chunks.append(character) # Start a new number chunk
        elif character in '+-/*':
            chunks.append(character)  # This doesn't account for `1 ++ 2`.

    return chunks[1:]

rawDescription: Python: Math expression parsing

description: Python: Math expression parsing


code:
import logging
import Queue
import threading
import time
import watchdog.observers as observers
import watchdog.events as events

logger = logging.getLogger(__name__)

SENTINEL = None

class MyEventHandler(events.FileSystemEventHandler):
    def on_any_event(self, event):
        super(MyEventHandler, self).on_any_event(event)
        queue.put(event)
    def __init__(self, queue):
        self.queue = queue

def process(queue):
    while True:
        event = queue.get()
        logger.info(event)

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG,
                        format='[%(asctime)s %(threadName)s] %(message)s',
                        datefmt='%H:%M:%S')

    queue = Queue.Queue()
    num_workers = 4
    pool = [threading.Thread(target=process, args=(queue,)) for i in range(num_workers)]
    for t in pool:
        t.daemon = True
        t.start()

    event_handler = MyEventHandler(queue)
    observer = observers.Observer()
    observer.schedule(
        event_handler,
        path='/tmp/testdir',
        recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

rawDescription: Python Notify when all files have been transferred


code:
#settings.py
SERVER_NAME="dev.app.com"
DEBUG=True
SECRET_KEY='xxxxxxxxxx'

#settings_production.py
SERVER_NAME="app.com"
DEBUG=False

rawDescription: Flask: How to manage different environment databases?


code:
import sys
f = sys.stdout
f.write('abc')

rawDescription: Is it possible to have an alias for sys.stdout in python?


code:
runs = [[res.partition(':')[-1].strip() for res in group]
        for key, group in groupby(f, key=lambda x: x[:1] == "A") if key]

rawDescription: Using `itertools.groupby()` to get lists of runs of strings that start with `A`?


code:
for a, b in zip(listA, listB):
    a.append(b)

rawDescription: Appending in lists of list

description: Appending in lists of list


rawDescription: Scrape text not contained in any element


code: 'Text with newlines\n\nand a caret at the end^\n\nwhich will be split'.split('^\n\n')

rawDescription: Splitting paragraphs that begin as indents


code:
import Tkinter as tki
class App(object):
     def __init__(self,root):
         self.root = root

         self.txt_frm = tki.Frame(self.root, width=900, height=900)
         self.txt_frm.pack(fill="both", expand=True)
         button3 = tki.Button(self.txt_frm,text="CLICK 1", command = self.retrieve_inpu)
         button3.grid(column=0,row=2)
         self.entry = tki.Entry(self.txt_frm)
         self.entry.grid(column=1,row=0)

         #place holder for label variable
         self.label = None

     def retrieve_inpu(self):
        ent = self.entry.get()

        #destroy the widget if it has been created
        #you will have a bunch of orphans if you don't
        if self.label:
            self.label.destroy()

        self.label = tki.Label(self.txt_frm,text=ent)
        self.label.grid(column=0,row=3)

root = tki.Tk()
app = App(root)
root.mainloop()

rawDescription: tkinter Display current label,deleting the previous

description: Tkinter Display current label,deleting the previous


rawDescription: Difference between numpy.array shape (R, 1) and (R,)


code:
list = ["Maurice is smart","Maurice is dumb","pie","carrots"]

new_list = [x for x in list if "Maurice" in x]

print(new_list)

rawDescription: Save list items to a new list if the item contains specific substring


code:
import re
s = "askdfghj... Hello world my name is Alex and I am 18 years all ...askdfgj."
re.search(r'my.*Alex', s).group()

rawDescription: Grep a range of words from a text file python


code:
# contents of C:\tmp\eggs\libs\foo-1.0.egg\foo.py
version=(1,0)

rawDescription: How can I call python module inside versioned package folder?

description: Call python module inside versioned package folder


code:
import numpy as np

def improved(prob_matrix, items):
    # transpose here for better data locality later
    cdf = np.cumsum(prob_matrix.T, axis=1)
    # random numbers are expensive, so we'll get all of them at once
    ridx = np.random.random(size=n)
    # the one loop we can't avoid, made as simple as possible
    idx = np.zeros(n, dtype=int)
    for i, r in enumerate(ridx):
        idx[i] = np.searchsorted(cdf[i], r)
    # fancy indexing all at once is faster than indexing in a loop
    return items[idx]

def original(prob_matrix, items):
    choices = np.zeros((n,))
    # This is slow, because of the loop in Python
    for i in range(n):
        choices[i] = np.random.choice(items, p=prob_matrix[:,i])
    return choices

def vectorized(prob_matrix, items):
    s = prob_matrix.cumsum(axis=0)
    r = np.random.rand(prob_matrix.shape[1])
    k = (s < r).sum(axis=0)
    return items[k]

m = 10
n = 10000 # Or some very large number

items = np.arange(m)
prob_weights = np.random.rand(m, n)
prob_matrix = prob_weights / prob_weights.sum(axis=0, keepdims=True)

rawDescription: Fast random weighted selection across all rows of a stochastic matrix


code:
class Bar(object):
    def __init__(self):
        self.ds = {}

    def recur(self, n):
        x=do_foo(n)
        if foo(n):
            self.ds[n] = self.recur(x)
        else:
            return x

rawDescription: Python: Recursion: Passing some value down through the calls


code:
from nltk.stem.wordnet import WordNetLemmatizer

lemmatizer = WordNetLemmatizer()
print(lemmatizer.lemmatize('cacti'))
# -> cactus

rawDescription: Classifying List Entries in Python

description: Classifying List Entries in Python


rawDescription: Map a function's argument, which is a lambda, to a tuple?


code: return self.year and self.year == date.year

rawDescription: Logical operator "and" in python

description: Logical operator "and" in python


code: user = User.get_by_username(session, "me")

rawDescription: The pythonic way to handle sqlalchemy models


code:
def myfunc(x, y, z):
    ...

somename.myfunc = myfunc

rawDescription: Load python module not from a file

description: Load python module not from a file


rawDescription: Find first element of numpy ndarray of unknown shape


code:
import requests
s = requests.session()
s.post('https://example.com/dologin', data={'username': 'adam', 'password': 'hunter2'})
html = s.get('https://example.com/other_page').text
# and continue as in the first example

rawDescription: how to grab a web image which has a dynamic src ID in python

description: Grab a web image which has a dynamic src ID in python


rawDescription: Python: Convert '3.5' to integer

description: Python: Convert '3.5' to integer


code:
a = input('Enter number for factorial operation: ')
a
type(a)
isinstance(a, str)
isinstance(a, int)
a = int(input('Enter number for factorial operation: '))
a
type(a)
isinstance(a, str)
isinstance(a, int)

rawDescription: Python 3 factorial

description: Python 3 factorial


code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoLocator

class MyLocator(AutoLocator):
    def view_limits(self, vmin, vmax):
        multiplier = 5.0
        vmin = multiplier * np.floor(vmin / multiplier)
        vmax = multiplier * np.ceil(vmax / multiplier)
        return vmin, vmax

fig, ax = plt.subplots()
ax.plot([-29, 31], [-29, 31])

ax.xaxis.set_major_locator(MyLocator())
ax.yaxis.set_major_locator(MyLocator())
ax.autoscale()

plt.show()

rawDescription: Matplotlib - change automatic axis range


code:
def checkSwitches():
     return [EHbox.isChecked(),AHbox.isChecked(),DHbox.isChecked(),L1box.isChecked()]

rawDescription: Using checkboxes in python to select which files to write/export


code:
class Chair(object):

    MAX_WEIGHT = 300

    def __init__(self):
        super().__init__()

        self.weight = 5
        self.currentWeight = self.weight
        self.holding = None
        self.broken = False

    def hold(self, item):
        self.holding = item
        self.currentWeight = self.weight + item.weight
        self.checkWeight()

    def checkWeight(self):
        if self.holding.weight > self.MAX_WEIGHT:
            self.broken = True
            ...

rawDescription: Python-iptables how to optimize code

description: Python-iptables how to optimize code


rawDescription: Changing time components of pandas datetime64 column


code:
fname="gencode.v19.annotation.gtf"

results = []
for line in open(fname,'r'):
    if not line.startswith('##'):
        col1=line.split()[0]
        col3=line.split()[2]
        col4=line.split()[3]
        col5=line.split()[4]
        if "protein_coding" in line and col3=="exon":
            data = col1,col4,col5
            results.append(data)
            print(data)

with open('results.txt','w') as datafile:
    for data in results:
        dataFile.write(str(data)) # Fix this: I dont know what formatting you want

rawDescription: Write three columns of file to new text file using python


rawDescription: Breaking up substrings in Python based on characters


code:
def __setitem__(self, key, item):
    putenv(key, item)
    self.data[key] = item

rawDescription: python environment case senstivity - os.environ[...]

description: Python environment case senstivity - os.environ[...]


code:
from subprocess import Popen, PIPE

p = Popen(["gocr", "-"], stdin=PIPE)
im.save(p.stdin, 'PPM')
p.stdin.close()
p.wait()

rawDescription: Piping image on Windows

description: Piping image on Windows


code:
from bs4 import BeautifulSoup
import re
sample = '''\

Case Number: 121018261

Added: 10/22/2012

    Case Bond: $2,000,000.00 Set By: Spokane County Superior Court

Charge 1  RCW: 9A.56.210: ROBBERY-2ND DEG
 Report Number: 120160423 Report Agency: 002 - SPOKANE CITY

Charge 2 RCW: 9A.56.210: ROBBERY-2ND DEG
Report Number: 120160423Report Agency: 002 - SPOKANE CITY

Case Number: 121037010

Added: 10/21/2012

    Case Bond: $150,000.00 Set By: Spokane County Superior Court

Charge 1 RCW: 9A.44.050(1)(A): RAPE-2ND(FORCIBLE)
 Report Number: 120345597 Report Agency: 001 - SPOKANE COUNTY

'''
soup = BeautifulSoup(sample)
case_top = soup.find_all(style=re.compile("border-top:solid 1px #666"))
for case in case_top:
    wrapper = soup.new_tag('div', **{'class': 'case'})
    case.insert_before(wrapper)
    while wrapper.next_sibling:
        wrapper.append(wrapper.next_sibling)
        if wrapper.find('table', class_='bookinfo'):
            # moved over the bookinfo table, time to stop
            break

soup.body

rawDescription: How to insert unescaped html fragment in Beautiful Soup 4

description: Insert unescaped html fragment in Beautiful Soup 4


rawDescription: vectorize numpy unique for subarrays

description: Vectorize numpy unique for subarrays


code:
def getDummyData(reactor, x):
    """returns a deferred object that will have a value in some random seconds
    sets up a callLater on the reactor to trgger the callback of d"""
    d = defer.Deferred()
    pause = random.randint(1,10)
    reactor.callLater(pause, d.callback, (x, pause))
    return d

def main(reactor):
    """makes a collection of deffered calls and then fires them. Stops reactor at end"""
    deferred_calls = [getDummyData(reactor, r) for r in range(0,100)]
    d = defer.gatherResults(deferred_calls, consumeErrors = True)
    d.addCallback(printData)
    return d

rawDescription: stop a twisted reactor after a gatherResults has finished

description: Stop a twisted reactor after a gatherResults has finished


code:
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static")
)

rawDescription: Get the path of "static" folder in django automatically


code:
class Track(object):
    def __init__(self, parent, idx):
        self.parent, self.idx = parent, idx

def parser(text):
    trees = []
    tracks = {}
    for line in text.splitlines():
        line = line.strip()
        key, value = map(lambda s: s.strip(), line.split(':', 1))
        nodes = value.split()
        if len(nodes):
            parent = Node(key)
            for i, node in enumerate(nodes):
                tracks[node] = Track(parent, i)
                parent.nodes.append(Leaf(node))
            curnode = parent
            if curnode.tag in tracks:
                t = tracks[curnode.tag]
                t.parent.nodes[t.idx] = curnode
            else:
                trees.append(curnode)
        else:
            curnode = Leaf(key)
            if curnode.tag in tracks:
                # well, how you want to handle it?
                pass # ignore
            else:
                trees.append(curnode)
    return trees

rawDescription: Visualize tree in bash, like the output of unix "tree"


rawDescription: Python regex for matching all strings except a given string


code:
dct = dict(tuples)
val = dct.get(key) # None if item not found else the corresponding value

rawDescription: iterating quickly through list of tuples

description: Iterating quickly through list of tuples


code:
def read(from_filename, buffer, process_queue):
  file = open(from_filename, 'r')

  while True:
    slice = islice(file, buffer)
    to_process = []
    for line in slice:
      to_process.append(line)
    process_queue.put(to_process)
    if not to_process:
        # input ended
        break
  process_queue.put('kill')

rawDescription: Process not ending in python

description: Process not ending in python


code: fh = logging.FileHandler(log_name.format(function_name), mode='w')

rawDescription: How to avoid duplicate outputs using logging module

description: Avoid duplicate outputs using logging module


code:
def your_view(request):
    profile = request.user.get_profile()
    ...
    # Your code

rawDescription: get user profile in django

description: Get user profile in django


code:
def keep_most_relevant(d, cutoff=0.3):
    for k, v in list(d.items()):
        if k == 'count':
            continue
        if 'count' in d and v['count'] < d['count'] * cutoff:
            del d[k]
        else:
            keep_most_relevant(v)

rawDescription: Find Most Relevant Child in Dictionary

description: Find Most Relevant Child in Dictionary


rawDescription: python, shuffle until lists are completely different

description: Python, shuffle until lists are completely different


code: souceout.write (str(readfile))

rawDescription: Python - glob.glob doesn't find *.txt in specified filepath within Unix OS


code: _, b_rows = numpy.where(a_rownames == b_rownames[:, numpy.newaxis])

rawDescription: Manipulation with arrays in numpy

description: Manipulation with arrays in numpy


rawDescription: How do I do something like numpy's arange in Go?

description: Do something like numpy's arange in Go


code:
import re
re.search(r'\d+', 'itunes20140618.tbz').group()

rawDescription: Capture contents in regular expression

description: Capture contents in regular expression


code:
s=raw_input('Words: ')
s
pattern='^'+''.join('(?=.*{})'.format(word) for word in s.split())
pattern

rawDescription: Remove Rows Based on User-Input Conditions (Pandas,Python 3)


code: print("I a {name} find {site} {a} {a} {a} {a} useful".format(**locals()))

rawDescription: Use same variable multiple times in print


code:
@app.template_global()
def static_url(filename):
    static_url = app.config.get('STATIC_URL')

    if static_url:
        return urljoin(static_url, filename)

    return url_for('static', filename=filename)

rawDescription: Serve static files from a CDN rather than Flask in production


code:
Foo(0)
Foo('foo')
Foo(None)

rawDescription: __repr__ for an attribute that can be a string or None?


code:
def transform(self, document):
    freq = {}
    for term in document:
        i = self.indexOf(term)
        freq[i] = freq.get(i, 0) + 1.0
    return Vectors.sparse(self.numFeatures, freq.items())

rawDescription: What hashing function does Spark use for HashingTF and how do I duplicate it?


code:
import copy
s = copy.copy( fig.canvas.toolbar._views )
p = copy.copy( fig.canvas.toolbar._positions )
ax=plt.gca()
im=ax.images[-1]
im.colorbar.remove()
#in real code, color range is updated here
plt.colorbar()
fig.canvas.toolbar._views = s
fig.canvas.toolbar._positions = p

rawDescription: updating colorbar without resetting zoom history in matplotlib user interface

description: Updating colorbar without resetting zoom history in matplotlib user interface


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

A = np.random.randint(1, 5, size=(100, 100)).astype(np.double)
y, x = np.ogrid[-50:50, -50:50]
mask = x**2 + y**2 <= 30**2
A[~mask] = 0

rawDescription: border/edge operations on numpy arrays

description: Border/edge operations on numpy arrays


code:
type = socket.read(1)                                 # get the type of msg
dataToRead = struct.unpack("H", socket.read(2))[0]    # get the len of the msg
data = socket.read(dataToRead)                        # read the msg

if TYPE_FOO == type:
    handleFoo(data)

elif TYPE_BAR == type:
    handleBar(data)

else:
    raise UnknownTypeException(type)

rawDescription: How to use Python and Google's Protocol Buffers to deserialize data sent over TCP

description: Use Python and Google's Protocol Buffers to deserialize data sent over TCP


code: print(';'.join(str(emails.getEmail()) for emails in staffList))

rawDescription: Python 3 Joining elements of different strings in a List


code:
result_queue = Queue()
crawler = CrawlerWorker(MySpider(myArgs), result_queue)
crawler.start()
for item in result_queue.get():
    yield item

rawDescription: building scrapy spiders into my own program, i don't want to call scrapy from command line)

description: Building scrapy spiders into my own program, i don't want to call scrapy from command line)


code:
fig, axes = plt.subplots(ncols=2, sharex=True, sharey=True,figsize=(8,4))
fig.subplots_adjust(0,0,1,1,0,0)

rawDescription: Pyplot: Shared axes and no space between subplots


code: data = web.DataReader(ticker, "yahoo", datetime(2014,1,1), datetime(2015,12,31))

rawDescription: Isolating/manipulating data in a pandas Dataframe


code:
u1 = User.objects.create(name='u1')
u2 = User.objects.create(name='u2')
u3 = User.objects.create(name='u3')

a = Job.objects.create(name='a')
b = Job.objects.create(name='b')
c = Job.objects.create(name='c')

UserJob.objects.create(user=u1, job=a)
UserJob.objects.create(user=u2, job=a)
UserJob.objects.create(user=u2, job=b)
UserJob.objects.create(user=u3, job=a)
UserJob.objects.create(user=u3, job=c)

rawDescription: Django search multiple filters

description: Django search multiple filters


code:
import numpy as np
from scipy.cluster.vq import kmeans2
from scipy.spatial.distance import pdist, squareform
from sklearn import datasets
import matplotlib.pyplot as plt
from matplotlib import cm

def silhouette(X, cIDX):
    """
    Computes the silhouette score for each instance of a clustered dataset,
    which is defined as:
        s(i) = (b(i)-a(i)) / max{a(i),b(i)}
    with:
        -1 <= s(i) <= 1

    Args:
        X    : A M-by-N array of M observations in N dimensions
        cIDX : array of len M containing cluster indices (starting from zero)

    Returns:
        s    : silhouette value of each observation
    """

    N = X.shape[0]              # number of instances
    K = len(np.unique(cIDX))    # number of clusters

    # compute pairwise distance matrix
    D = squareform(pdist(X))

    # indices belonging to each cluster
    kIndices = [np.flatnonzero(cIDX==k) for k in range(K)]

    # compute a,b,s for each instance
    a = np.zeros(N)
    b = np.zeros(N)
    for i in range(N):
        # instances in same cluster other than instance itself
        a[i] = np.mean( [D[i][ind] for ind in kIndices[cIDX[i]] if ind!=i] )
        # instances in other clusters, one cluster at a time
        b[i] = np.min( [np.mean(D[i][ind])
                        for k,ind in enumerate(kIndices) if cIDX[i]!=k] )
    s = (b-a)/np.maximum(a,b)

    return s

def main():
    # load Iris dataset
    data = datasets.load_iris()
    X = data['data']

    # cluster and compute silhouette score
    K = 3
    C, cIDX = kmeans2(X, K)
    s = silhouette(X, cIDX)

    # plot
    order = np.lexsort((-s,cIDX))
    indices = [np.flatnonzero(cIDX[order]==k) for k in range(K)]
    ytick = [(np.max(ind)+np.min(ind))/2 for ind in indices]
    ytickLabels = ["%d" % x for x in range(K)]
    cmap = cm.jet( np.linspace(0,1,K) ).tolist()
    clr = [cmap[i] for i in cIDX[order]]

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.barh(range(X.shape[0]), s[order], height=1.0,
            edgecolor='none', color=clr)
    ax.set_ylim(ax.get_ylim()[::-1])
    plt.yticks(ytick, ytickLabels)
    plt.xlabel('Silhouette Value')
    plt.ylabel('Cluster')
    plt.show()

if __name__ == '__main__':
    main()

rawDescription: Equivalent of Matlab's cluster quality function?


code:
x = np.zeros((10, 3))
np.savetxt('/tmp/test.out', x)

rawDescription: Numpy matrix from text file

description: Numpy matrix from text file


code: df['new_column'] = df.apply(lambda row: my_fxn(row, global_dict), axis=1)

rawDescription: What is the `pandas` way to create a column in a dataframe by operating on each row?


code:
listOfSentences = ['mary had a little lamb.',
                   'she also had a little pram.',
                   'bam bam bam she also loves ham.',
                   'she ate the lamb.']

keyWords= [('bam', 3), ('lamb', 2), ('ate', 1)]
keyWords = dict(keyWords)

keySentences = []
for sentence in listOfSentences:
    score = sum(keyWords.get(word, 0) for word in sentence.split())
    if score > 0:
        keySentences.append((score, sentence))

keySentences = [sentence for score, sentence in sorted(keySentences, reverse=True)]
print(keySentences)

rawDescription: comparing sub elements in a list with another

description: Comparing sub elements in a list with another


code:
df1['val'] = df2.set_index(['c1','c2'])['val'].combine_first(df1.set_index(['c1','c2'])['val']).values
df1

rawDescription: how to update existing data frame in pandas?

description: Update existing data frame in pandas


code:
counts = Counter('abbba')
counts['a']
counts['b']

rawDescription: How to iterate over the elements of a map in python

description: Iterate over the elements of a map in python


code:
self.num_load = tokens[0]["loads"]
self.num_generator = tokens[0]["generators"]
self.num_transformer = tokens[0]["transformers"]
self.num_line = tokens[0]["lines"]
self.num_bus = tokens[0]["buses"]
self.power_rate = tokens[1]["rate"]

rawDescription: How should I organise my functions with pyparsing?


code: element = WebDriverWait(context.browser, 10).until(AnyEc(*args))

rawDescription: Iterating through list length and creating list of functions


code:
with open(name, "w") as data:
    data.write("text from text widget")

rawDescription: How to save text from a Text-widget Tkinter to a .doc using asksaveasfile?

description: Save text from a Text-widget Tkinter to a .doc using asksaveasfile


rawDescription: Is there a standard solution for Gauss elimination in Python?


code:
def __init__(self, *args, **kwargs):
    self.context = kwargs.pop('context', None)
    super(CustomLazyPaginator, self).__init__(*args, **kwargs)

rawDescription: How to do result post-processing before rendering with django endless-pagination?

description: Do result post-processing before rendering with django endless-pagination


code:
def retCircular(prime):
    prime_str = str(prime)
    family = [prime]
    for _ in range(len(prime_str)-1):
        prime_str = prime_str[1:] + prime_str[0]
        child = int(prime_str)
        if child == prime:
            break
        family.append(int(prime_str))
    return family

rawDescription: Project Euler #35 - Circular Primes (Incorrect Result by 1)


code:
def app_factory(global_config, **config):
    # initialization code / gevent monkey patch goes here
    # also you can assemble your wsgi stack.
    # then return your flask app
    return app

rawDescription: Which features are monkey patched by gunicorn gevent worker?


code:
def find_val_or_next_smallest(bst, x, best=None):
    """
    Get the greatest value <= x in a binary search tree.
    Returns None if no such value can be found.
    """
    if bst is None:
        return best
    elif bst.val == x:
        return x
    elif bst.val > x:
        return find_val_or_next_smallest(bst.left, x, best)
    else:
        # bst.val is guaranteed to be the best yet, since if we had
        # seen a better value higher up, the recursion would have gone
        # the other way from that node
        return find_val_or_next_smallest(bst.right, x, bst.val)

rawDescription: How can I make the following function into a tail-recursive function?

description: Make the following function into a tail-recursive function


code:
lists = [[]] * 5
lists
lists[0].append(1)
lists

rawDescription: How can I make multiple empty arrays in python?

description: Make multiple empty arrays in python


code:
with AlphaPeekReader(io.open(filename, 'rb')) as alphafile:
    alphafile.readalpha(3)

rawDescription: character reading in a variety way

description: Character reading in a variety way


code:
import tensorflow as tf

with tf.name_scope('hidden') as scope:
  a = tf.constant(5, name='alpha')
  W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0), name='weights')
  b = tf.Variable(tf.zeros([1]), name='biases')

rawDescription: Why do we use name parameter when creating a variable in Tensorflow?


code:
from ui import Ui_MainWindow
class MyUi(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(criticalPath, self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

rawDescription: How do i connect from main window to next window in pyqt?

description: Connect from main window to next window in pyqt


code:
@memoized
def recAnswer(current, target, count):
    """Unmemoized original function"""

    if count == 0:
        return int(current == target)       # False: 0, True: 1

    total = 0
    for next in dict[current]:
        total += recAnswer(next, target, count - 1)

    return total

rawDescription: Permutation of phone numbers starting at button x, ending at button y, and z numbers long


code:
def parallel_micro2(nsims):
    decay_time = np.zeros(nsims)
    t = dt
    half_time = np.exp(-ln2*dt/t12)
    np.random.seed(s)
    indices = np.where(decay_time==0)[0]
    u = len(indices)
    while u:
        decayed = np.random.random(u) > half_time
        decay_time[indices[decayed]] = t
        indices = indices[np.logical_not(decayed)]
        u = len(indices)
        t += dt
    return decay_time

rawDescription: Python/Numpy - Speeding up Monte Carlo method for radioactive decay


code:
class Expense(ndb.Model):
    amount = ndb.FloatProperty(required=False)
    description = ndb.StringProperty()
    vendor = ndb.KeyProperty(kind=Vendor)

    @property
    def vendor_name(self):
      return self.vendor.get().friendlyName

rawDescription: Google App Engine Datastore modeling issue


code:
import ConfigParser

parser = ConfigParser.ConfigParser()
parser.read('myoption.ini')
day = parser.get('options','day').split(',')
month = parser.get('options','month').split(',')
holiday = parser.get('options','holiday').split(',')

rawDescription: Python String to list processing

description: Python String to list processing


rawDescription: Comparing/combining two dictionaries

description: Comparing/combining two dictionaries


code:
def file_minus_extension(self):
    basename, extension = os.path.splitext(self.file.url)
    return basename

rawDescription: Returning a file path in Django without the file extension


code:
from Tkinter import *

class hisGui:

    def __init__(self):
        self.myGui = Tk()

        self.kiloMent = IntVar()
        self.mileMent = IntVar()

        self.myGui.title("Distance Converter")
        self.myGui.geometry("450x200+500+200")

        myLabel = Label(text="Welcome! Please enter your value then choose your option:",fg="blue",justify='center')
        myLabel.pack()

        self.miles = StringVar()
        self.convertTwo = StringVar()
        self.finalKilo = Label(textvariable = self.miles,fg='red',justify='center').place(x=200,y=80)
        self.finalMile = Label(textvariable = self.convertTwo, fg = 'red',justify='center').place(x=200,y=170)

        kiloEntry = Entry(self.myGui, textvariable =   self.kiloMent,justify='center').pack()

        kilo2milesButton = Button(text = "Kilometers to Miles!", command =     self.kiloFunc).pack()

        mileEntry = Entry(self.myGui, textvariable =     self.mileMent,justify='center').place(x=130,y=105)

        miles2kiloButton = Button(text = "Miles to Kilometers!", command = self.mileFunc).place(x=150,y=135)

        reset = Button(text = "Reset Values!", command = self.reset_values).place(x=10,y=165)

        quit = Button(text="Quit", command = self.closeWin).place(x=385,y=165)

        self.myGui.mainloop()

    def closeWin(self):
        self.myGui.destroy()                   #Close Window Function

    def kiloFunc(self):
        myText = self.kiloMent.get()          #Kilometers to Miles Function
        convert = 0.62
        self.miles.set(myText * convert)

    def mileFunc(self):
        myText2 = self.mileMent.get()         #Miles to Kilometers Function
        self.convertTwo.set(myText2 // 0.62)

    def reset_values(self):
        self.kiloMent.set(0)
        self.mileMent.set(0)      #Resets the Data
        self.miles.set("")
        self.convertTwo.set("")

if __name__ == '__main__':
    Gui = hisGui()

rawDescription: Why can't I get my labels to clear when the "Reset" button is pressed?


code:
from IPy import IP
ip_dotted = str(IP(ip_dec))

rawDescription: Sqlalchemy change decimal IP address to dotted


code:
A = np.vstack((np.ones((len(molecules),)),
               [Psat(molecule) for molecule in molecules]))
b = np.array([1, 760])

rawDescription: Dynamically build a lambda function in python


rawDescription: Struggling with unicode in Python

description: Struggling with unicode in Python


rawDescription: Representing a multi-select field for weekdays in a Django model


code: ' Dougherty & Co.',

rawDescription: replacing quotes inside a pandas df to count unique terms

description: Replacing quotes inside a pandas df to count unique terms


rawDescription: Numpy indexing multidimensional arrays with array and slice


code: x[abs(x-x.mean()) <= x.std()]

rawDescription: Numpy way to determine the value(s) in an array which is causing a high variance


code:
@receiver(m2m_changed, sender=Video.category.through)
def video_category_changed(sender, **kwargs):
    instance = kwargs.pop('instance', None)
    pk_set = kwargs.pop('pk_set', None)
    action = kwargs.pop('action', None)
    if action == "pre_add":
        if 1 not in pk_set:
            c = Category.objects.get(pk=1)
            instance.category.add(c)
            instance.save()

rawDescription: Using Django's m2m_changed to modify what is being saved pre_add


rawDescription: Split string in Python into chunks with constant length, but right-aligned


rawDescription: Multiple URL key-value pair parameters that are optional in Django


code:
def my_view(request):
    ...
    return redirect('/some/url/')

rawDescription: Django - template not loading, perpetual 302 message


code:
from itertools import groupby

operators = {'+', '-', '*', '(', ')'}
fragments = ['a', '=', 'x+y*z', '//', 'moo', '-', 'spam*(eggs-ham)']
separated = []
for fragment in fragments:
    for is_operator, group in groupby(fragment, lambda c: c in operators):
        if is_operator:
            separated.extend(group)
        else:
            separated.append(''.join(group))

rawDescription: Separating strings (list elements) with many spliters without loosing spliter from list


code: date_format = '%Y-%m-%d'

rawDescription: How to calculate the number of days between two given dates

description: Calculate the number of days between two given dates


code:
#print('''foo        logging.debug(('''foo)
#bar''')       -->   #bar''')

rawDescription: Convert commented out print statements to logging calls - Python


code:
import gtk

class CellRenderer:
    def __init__(self):
       window = gtk.Window()
       window.set_default_size(200,200)

       liststore = gtk.ListStore(str, str)
       liststore.append(["Fedora","http://fedoraproject.org"])
       liststore.append(["Slackware","http://www.slackware.com"])
       liststore.append(["Sidux","http://sidux.com"])

       treeview = gtk.TreeView(liststore)
       column_text = gtk.TreeViewColumn("Distributions")
       column_sample= gtk.TreeViewColumn("Links")
       treeview.append_column(column_text)
       treeview.append_column(column_sample)

       cellrenderer_text = gtk.CellRendererText()
       column_text.pack_start(cellrenderer_text,False)
       column_text.add_attribute(cellrenderer_text,"text",0)

       cellrenderer_sample = gtk.CellRendererText()
       column_sample.pack_start(cellrenderer_sample, False)
       column_sample.add_attribute(cellrenderer_sample,"text", 1)

       window.connect("destroy", lambda q : gtk.main_quit())
       window.add(treeview)
       window.show_all()

CellRenderer()
gtk.main()

rawDescription: Different Column values not displayed in PYGTK TreeView


rawDescription: How to get current time in Python

description: Get current time in Python


code:
class Foo(Model):
    id = Column(Integer, primary_key=True)
    native_linux_user = Column(String, nullable=False)
    is_active = Column(Boolean)

    __table_args__ = (
        Index('idx_unique_native_linux_user_is_active', native_linux_user,
            unique=True,
            postgresql_where=(is_active==True),
        ),
    )

rawDescription: Create a `CheckConstraint` within a `UniqueConstraint`


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

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


code:
regexes = (regex1, regex2, regex3)
for regex in regexes:
    match = re.search(regex, s)
    if match:
        #do stuff
        break

rawDescription: too many indentation when checking a string with many regular expressions serially in python

description: Too many indentation when checking a string with many regular expressions serially in python


rawDescription: how to get sums in CSV file python

description: Get sums in CSV file python


rawDescription: Combining two sorted lists in Python

description: Combining two sorted lists in Python


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

class Numberise(object):
    def __init__(self, name):
        self.name = name

    def __get__(self, instance, owner):
        if self.name not in instance.__dict__:
            raise (AttributeError, self.name)
        return '%o'%(instance.__dict__[self.name])

    def __set__(self, instance, value):
        print ('setting value to: %d'%value)
        instance.__dict__[self.name] = value

class Register(object):
    width = Numberise("truewidth")
    def __init__(self, width=16, name='unNamed'):
        super(Register, self).__init__()
        tuple_args = inspect.getargvalues(inspect.currentframe()) #shorthand
        for arg in tuple_args[0]:
            setattr(self, arg, tuple_args[3][arg])

if __name__ == "__main__":

    new_regs = [Register(width=i) for i in range(10)]
    for i,reg in enumerate(new_regs):
        reg.width = i

    for R in new_regs:
        print ('In extract(). Id:%s name:%s, width:%s, truewidth:%d'%(id(R), R.name, R.width, R.truewidth))

rawDescription: class attribute changing value for no reason

description: Class attribute changing value for no reason


rawDescription: Several query params URL in DRF

description: Several query params URL in DRF


code:
tznow = timezone.now()
'{:%d-%m-%Y:%H:%M:%S}.{:03d}'.format(tznow, tznow.microsecond // 1000)

rawDescription: Python/Django timestamp including milliseconds


code:
li = [
  { 'country': 'UK', 'city': 'Manchester' },
  { 'country': 'UK', 'city': 'Liverpool' },
  { 'country': 'France', 'city': 'Paris' }
]
{d['city']: d['country'] for d in li}

rawDescription: python map array of dictionaries to dictionary?

description: Python map array of dictionaries to dictionary


code:
import random

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

def encode_string(s):
    shift = random.randint(1,25)
    return ''.join(shift_char(i,shift) for i in s), shift

def decode_string(s,shift):
    return ''.join(shift_char(i,-shift) for i in s)

# usage
s = 'Hello, StackOverflow!'
enc_s, shift = encode_string(s)
dec_s = decode_string(enc_s,shift)

rawDescription: How to change random letter using number in Python

description: Change random letter using number in Python


code: data = np.genfromtxt(fname, autostrip=True)

rawDescription: python loads more memory than the actual size of the file

description: Python loads more memory than the actual size of the file


code:
import datetime
from collections import defaultdict

def avg(lst):
    return sum(lst)/len(lst)

rawDescription: Averaging along DataFrame columns with some missing rows


rawDescription: calculate maximum value possible from a given number of bits

description: Calculate maximum value possible from a given number of bits


code:
def inop():
    '0' in []

dis.dis(inop)

rawDescription: Python source code for built-in "in" operator


code:
df
df.comments.dropna()

rawDescription: Cant get NaN elements dropped in pandas dataFrame


code: s.resample('15Min', how=len)

rawDescription: Reindex time-stamped data with date_range


code: DATA[((DATA == PoolOfExtLines[:,None,:]).all(2)).any(0),-1] = -1

rawDescription: Numpy array: Changing the values of the last column when lines of a 2d-array are equal to lines of another 2d-array


code: B.f is B.f

rawDescription: Python classmethods: what's the difference between a member of an instance and a member of the class?


code:
from sklearn.datasets import fetch_rcv1
rcv1 = fetch_rcv1()

rawDescription: Twitter/ general categorisation training corpus


code:
for a in range(1, 15):
 for a2 in range(14-a, 0, -1):
     print(" ", end='')
 for a1 in range(1, a+ 1):
     print("*", end='')
 print()

rawDescription: For/While Loops to make *-triangles

description: For/While Loops to make *-triangles


code:
parser.add_argument('-r','--readfile', type=argparse.FileType('r'), default='-')
parser.add_argument('-w','--writefile', type=argparse.FileType('w'), default='-')

rawDescription: Argparse optional stdin read and/or stdout out


code:
for tag in soup.find_all('a'):
    link = tag.get('href')
    if 'http://t.co/' in link:
       webbrowser.open_new_tab(link)

rawDescription: Why doesn't my code loading links in a browser tab work?


code: line = line.split("(")[0]

rawDescription: remove the numbers from txt file

description: Remove the numbers from txt file


code:
class MyTabWidget(QtGui.QTabWidget):

    def __init__(self, parent = None):
        super(MyTabWidget, self).__init__(parent)

        self.subwidget = CustomWidget(self)

        self.left_tab_widget = QtGui.QWidget()
        self.leftLayout = QtGui.QVBoxLayout(self.left_tab_widget)
        self.leftLayout.addWidget(self.subwidget)

        self.right_tab_widget = QtGui.QWidget(self)
        self.rightLayout = QtGui.QVBoxLayout(self.right_tab_widget)
        label = QtGui.QLabel("Some additional data", self.right_tab_widget)
        self.rightLayout.addWidget(label)

        self.addTab(self.left_tab_widget, "Left Tab")
        self.addTab(self.right_tab_widget, "Right Tab")

        self.currentChanged.connect(self.onCurrentChanged)

    def onCurrentChanged(self, index):
        if index == 0:
            self.leftLayout.addWidget(self.subwidget)
        else:
            self.rightLayout.addWidget(self.subwidget)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    widget = MyTabWidget()
    widget.show()
    app.exec_()

rawDescription: Is there any way to show widget it two different tabs?


Numpy.bitwise_and.reduce behaving unexpectedly


rawDescription: Pythonic Way to reverse nested dictionaries


code:
adictionary = {'dest':'ination', 'bla':2}
options = Struct(**adictionary)
options.dest
options.bla

rawDescription: creating python keyed arrays

description: Creating python keyed arrays


rawDescription: Variables in NumPy

description: Variables in NumPy


code:
a = np.random.rand(8)*10
a.sort()
a
pd.Series((np.round(a*1000)/1000)*1e9,dtype='timedelta64[ns]')

rawDescription: reduce time precision of time series to milliseconds

description: Reduce time precision of time series to milliseconds


code:
from collections import Counter
li=[ [1,2,3], [2,3,4], [1,2,3] ]
Counter(str(e) for e in li)

rawDescription: How to count number of unique lists within list?

description: Count number of unique lists within list


code:
# in UserPost model (adding related_name)
user = models.ForeignKey(User, related_name="posts")

class UserResource(ModelResource):
    # ....
    def dehydrate(self, bundle):
        posts = bundle.obj.posts.all()
        bundle.data['locations'] = [post.location for post in posts]
        return bundle
    # ...

rawDescription: Combine Related Resources With TastyPie

description: Combine Related Resources With TastyPie


code:
render = web.template.render('templates/')

class index:
    def GET(self):
        greeting = "Hello World"
        return render.index(greeting=greeting)

rawDescription: LearnPythonTheHardWay Exercise 50

description: LearnPythonTheHardWay Exercise 50


code:
dt1=np.dtype(('i4', [('bytes','u1',4)]))
x2=x.view(dtype=dt1)
x2.dtype
x2['bytes']
x2

rawDescription: Converting 32-bit integer into array of four 8-bit integers in Python


rawDescription: Change wallpaper in Python for user while being system


code:
length_of_file = 5000
results = np.empty(length_of_file)
with open('myfile.txt', 'r') as f:
    for i, line in enumerate(f):
        results[i] = processing_func(line)

rawDescription: Appending arrays in numpy

description: Appending arrays in numpy


code:
with open(file_location, 'wb') as file:
    pickle.dump(key_value_mapping, file)

rawDescription: Writing and reading variables in a file in Python


code:
def toppints(*args):
    if any(args):
       condiments = ('bacon','lettuce','tomato','mayo')
       return "The food has "+" ".join(x for x,y in zip(condiments,args) if y) + '.'
    else:
       return "You must be full"

rawDescription: Python, inserting a boolean to a corresponding value


code: result = array[:, idx]

rawDescription: removing entries from a numpy array

description: Removing entries from a numpy array


code:
from django.forms.models import model_to_dict

data = self.get_queryset()

for item in data:
   item['product'] = model_to_dict(item['product'])

return HttpResponse(json.simplejson.dumps(data), mimetype="application/json")

rawDescription: <Django object > is not JSON serializable


code:
import multiprocessing, time, signal
p = multiprocessing.Process(target=time.sleep, args=(1000,))
print(p, p.is_alive())
p.start()
print(p, p.is_alive())
p.terminate()
time.sleep(0.1)
print(p, p.is_alive())
p.exitcode == -signal.SIGTERM

rawDescription: Process identification using python multiprocessing library


code:
from lxml import etree

# remove the b prefix in Python 2
# needed in python 3 because
# "Unicode strings with encoding declaration are not supported."
et = etree.XML(b"""...""")

ns = {
    'lom': 'http://ltsc.ieee.org/xsd/LOM',
    'zs': 'http://www.loc.gov/zing/srw/',
    'dc': 'http://purl.org/dc/elements/1.1/',
    'voc': 'http://www.schooletc.co.uk/vocabularies/',
    'srw_dc': 'info:srw/schema/1/dc-schema'
}

# according to docs, .xpath returns always lists when querying for elements
# .find returns one element, but only supports a subset of XPath
record = et.xpath("zs:records/zs:record", namespaces=ns)[0]
# in this example, we know there's only one record
# but else, you should apply the following to all elements the above returns

name = record.xpath("//voc:name", namespaces=ns)[0].text
print("name:", name)

lom_entry = record.xpath("zs:recordData/srw_dc:dc/"
                         "lom:metaMetadata/lom:identifier/"
                         "lom:entry",
                         namespaces=ns)[0].text

print('lom_entry:', lom_entry)

lom_ids = [id.text for id in
           record.xpath("zs:recordData/srw_dc:dc/"
                        "lom:classification/lom:taxonPath/"
                        "lom:taxon/lom:id",
                        namespaces=ns)]

print("lom_ids:", lom_ids)

rawDescription: Good python XML parser to work with namespace heavy documents


code:
n = 10
F = np.tile(np.arange(0,n,0.1)[None,None,:,None,None], [n, n, 1, n, n])
X = np.tile(np.arange(0,n)[None,None,:,None], [n, n, 1, n])

a,b,c,d = np.ogrid[:n,:n,:n,:n]
argmaxD = F[a,b,c,d,X].argmax(axis=-1)

rawDescription: Evaluate array at specific subarray

description: Evaluate array at specific subarray


code:
from itertools import groupby
[next(v) for k,v in groupby(new_lis,key=lambda x:x[0])]

rawDescription: Sorting list of tuples based on multiple keys and returning top results


code:
result_passed = {}
for a in data:
    result_passed[a] = []
    for b in data[a]:
        passed = True
        for c in data[a][b]:
            if c != 'Passed':
                passed = False
                break
        if passed:
            result_passed[a].append(b)

rawDescription: Python - iterate text file and create dictionary of dictionary


code:
from functools import wraps

def string_builder(func):
    @wraps(func)
    def wrapped(*args, **kwargs):
        return ''.join(func(*args, **kwargs))
    return wrapped

rawDescription: string_builder decorator in python 2.7 - how to do it and should I?

description: String_builder decorator in python 2.7 - how to do it and should I


code:
class MyClass():
    def __init__(self): pass

    some_method = CustomFunction('some_method')

rawDescription: Possible to change a Python method's __str__() attribute?


rawDescription: How to vectorize bigrams with the hashing-trick in scikit-learn?

description: Vectorize bigrams with the hashing-trick in scikit-learn


code:
for i, record in enumerate(data):
    data[i]['OtherData'] = {}
    for key in list(record.keys()):
        if key in other_data:
            data[i]['OtherData'][key] = record[key]
            del data[i][key]

print(json.dumps(data, indent=4, sort_keys=True))

[
    {
        "ADRNR": 2692629,
        "AlertID": "",
        "AlertTimestamp": "14-12-2015 14:44:14",
        "KOINH": null,
        "LAND1": "US",
        "LOEVM_x": null,
        "LOEVM_y": null,
        "MasterDataID": "10099",
        "MasterDataType": "Vendor",
        "NAME1": "LEGAL",
        "NODEL_x": null,
        "NODEL_y": null,
        "ORT01": null,
        "OtherData": {
            "BANKL": null,
            "BANKN": null,
            "BANKS": "nan",
            "BEGRU": "NPIV",
            "BUKRS": "2646",
            "C_Block": "No"
        },
        "PSTLZ": null,
        "RuleID": "Rule3",
        "RuleName": "Vendor and Bank Country is Different",
        "STCD1": null,
        "STCD2": null,
        "STCEG": null,
        "STRAS": null,
        "TELF1": null
    }
]

rawDescription: How to convert few columns of pandas dataframe into JSON record within another record?

description: Convert few columns of pandas dataframe into JSON record within another record


code:
class TestFilewise():
    def setup(self):
        import os
        self.fixture_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                "/fixtures/")
        assert os.access( self.fixture_dir, os.F_OK ), \
            "Oops! the fixture dir should be here: '%s'" % self.fixture_dir
        assert os.access(os.path.join(self.fixture_dir,
                    "profiles-source1.csv"), os.F_OK )

rawDescription: how to break up lines most reasonably, staying "within 80 chars" in python?

description: Break up lines most reasonably, staying "within 80 chars" in python


code:
def increment_counter(method):
    def wrapper(self, *args, **kw):
        self._COUNTER += 1
        return method(self, *args, **kw)
    return wrapper

rawDescription: How can I increase counter properties of class when a instance is called?

description: Increase counter properties of class when a instance is called


code:
if form.validate_on_submit():
    requestlist = models.Request.query

    if form.status.data != 'No Filter':
        requestlist = requestlist.filter(models.Request.status.like(form.status.data))

    if form.assigned.data != 'No Filter':
        requestlist = requestlist.filter(models.Request.assigned.like(form.assigned.data))

    if form.requestor.data != 'No Filter':
        requestlist = requestlist.filter(models.Request.requestedBy.like(form.requestor.data))

    return requestlist.all()

rawDescription: smarter sqlalchemy wtf dynamic filter in a flask app

description: Smarter sqlalchemy wtf dynamic filter in a flask app


code:
for i in URL:
    a = i.split('/')[0]
    #do something

rawDescription: Is there a way to extract the section before the first `/` from a list?


code: myList[0], myList[1] = myList[1], myList[0]

rawDescription: How to switch nodes at an index with the head of a list?

description: Switch nodes at an index with the head of a list


code:
class list:
  def GET(self,irl):
    thisfile = inspect.getfile(inspect.currentframe())
    thispath = os.path.dirname(os.path.abspath(thisfile))
    dir = os.listdir("%s/static/%s"%(thispath,irl))
    rel = irl
    files = []
    folds = []
    for item in dir:
      if os.path.isfile("%s/static/%s/%s"%(thispath,irl,item)):
        files.append(item)
      elif item!='res':
        folds.append(item)
    return render.list(rel,folds,files)

rawDescription: How do I serve up a directory list from web.py

description: Serve up a directory list from web.py


code:
path
'/var/log'

file_name
'test.txt'

rawDescription: most efficient way to substring path and file out of a string

description: Most efficient way to substring path and file out of a string


rawDescription: copying only upper triangular values of sympy Matrix into array from numpy.triu()?

description: Copying only upper triangular values of sympy Matrix into array from numpy.triu()


code:
csv_dialect = dict(delimiter=',', quotechar='|')
names = set()
with open('fileA', 'rb') as file_a:
    reader1 = csv.reader(file_a, **csv_dialect)
    next(reader1)
    for row in reader1:
        names.add((row[0], row[2]))

# `names` is now a set of all names (taken from columns 0 and 2) found in file A.

with open('fileA', 'ab') as file_a, open('fileB', 'rb') as file_b:
    writer = csv.writer(file_a, **csv_dialect)
    reader2 = csv.reader(file_b, **csv_dialect)
    next(reader2)
    for row in reader2:
        if (row[0], row[2]) not in names:
            # This row was not present in file A, add it.
            writer.writerow(row)

rawDescription: Comparing two CSV Files Based on Specific Data in two Columns


code:
name = "SpecialClass"
globals()[name] = ClassFactory(name, params)
instance = SpecialClass(...)

rawDescription: How can I dynamically create derived classes from a base class

description: Dynamically create derived classes from a base class


rawDescription: Better / More pythonic way to process the results of a function with multiple return values


code: [letter for letter, count in hand.iteritems()]

rawDescription: going through a dictionary and printing its values in sequence

description: Going through a dictionary and printing its values in sequence


code:
result = df.set_index(['Z', 'A', 'pos']).unstack('pos')
result.rename(columns=lambda x: '%s[%d]' % (x[0], x[1]+1)).reset_index()

rawDescription: Multiindex from array in Pandas with non unique data


code:
sentimentDict = {}

def loadSentiment():
    with open('Sentiment/positive_words.txt', 'r') as f:
        sentimentDict['positive'] = dict.fromkeys((line.strip() for line in f), 1)

    with open('Sentiment/negative_words.txt', 'r') as f:
        sentimentDict['negative'] = dict.fromkeys((line.strip() for line in f), 1)

rawDescription: What is this dictionary assignment doing?


code:
lastname = ['smith','jones','cook']
companies = ['company a','company b']
affiliations = {'matt smith':'company a','tom jones':'company b','karen cook':'company c'}

rawDescription: In python: How to find match of string in same row, compare part of (fuzzy) matched string to list?


code: itertools.takewhile(lambda a: a <= stop, itertools.count())

rawDescription: Assigning a condition to a variable in Python


code:
from mymodule import OUTPUT  # this is the base directory of the files
from gluon.contenttype import contenttype

def export():
    # allow to download files
    jid = request.args(0)
    fid = request.args(1)

    if None in (jid, fid):
        res = 'Invalid URL'
    else:
        # get the file row
        row = db.file(fid)

        # some checks (not necessary if you know what you're doing)
        jrow = db.job(jid)
        if row is None:
            res = "unknown file ID"
        elif jrow.id  is None:
            res = "unknown job ID"
        else:
            filename = row.name
            # jrow.perma_id, is a field in the 'job' DB, that I use to create a unique
            # directory name, so the files of job ID 'jid' are under: OUTPUT/perma_id/
            fullname = os.path.join(OUTPUT, jrow.perma_id, filename)

            ext = os.path.splitext(filename)[1]
            response.headers['Content-Type'] = contenttype(ext)
            response.headers['Content-disposition'] = 'attachment; filename=%s' % filename
            res = response.stream(open(fullname, "rb"), chunk_size=4096)

    return res

rawDescription: Web2py: downloading files / displaying images


code:
import random
import math

R = 10.
n_rings = 10.
n_angles = 10.
dr = 10./n_rings
da = 2*math.pi/n_angles

base_points_per_division = 3
increase_per_level = 1.1
points = []
ring = 0
while ring < n_rings:
    angle = 0
    while angle < n_angles:
        for i in xrange(int(base_points_per_division)):
             ra = angle*da + da*math.random()
             rr = r*dr + dr*random.random()
             x = rr*math.cos(ra)
             y = rr*math.sin(ra)
             points.append((x,y))
        angle += 1
    base_points_per_division = base_points_per_division*increase_per_level
    ring += 1

rawDescription: Method to uniformly randomly populate a disk with points in python


code:
import shlex
shlex.split('This "is a" test.')

rawDescription: Recreating a shell-like interpreter within python?


code:
dates = {
    'first record': {
        'first date': '1984',
        'last date': '2001',
    },
    'second record': {
        'first date': '1985',
        'last date': '2012',
    },
    'third record': {
        'first date': '1985',
        'last date': '2000',
    },
    'fourth record': {
        'first date': '2000',
        'last date': '2014',
    }
}

mn_first = min((int(d['first date'])) for d in dates.values())
mn_last = min((int(d['last date'])) for d in dates.values())

print([k for k,v in dates.iteritems()
       if int(v['first date']) == mn_first or int(v['last date']) == mn_last])

rawDescription: Python: Find smallest values in nested dict


code:
def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

is_number(x)

rawDescription: Check numeric values from a dictionary by unit test


code:
def triangle(length, amplitude):
     section = length // 4
     for direction in (1, -1):
         for i in range(section):
             yield i * (amplitude / section) * direction
         for i in range(section):
             yield (amplitude - (i * (amplitude / section))) * direction

rawDescription: Triangle wave shaped array in Python

description: Triangle wave shaped array in Python


code: url(r'^amt/', 'core.views.show_amt_page', {'SSL':True}, name='amt'),

rawDescription: Django: redirect to the same url with an appended querystring


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

description: Finding non-numeric rows in dataframe in pandas


code:
def fileIsImportant(change):

    if ".skipbuild" in change.files:
        return False

    # There could be more logic here to test other things...

rawDescription: How can I arbitrarily tell Buildbot to not schedule a build for a given change?

description: Arbitrarily tell Buildbot to not schedule a build for a given change


code:
class Foo(object):
    def __init__(self, name, age, years_programming):
        self.name = name
        self.age = age
        self.years_programming = years_programming
    def __repr__(self): # this will help us recognize our objs after we've sorted
        return "<name: {name}, age: {age}, years_prog: {years}>".format(name=self.name, age=self.age, years=self.years_programming)
f = Foo('Bar', 3, 1)
g = Foo('Baz', 4, 3)
h = Foo('John', 10, 0)
def by_years_experience(foo):
    return foo.age * foo.years_programming
sorted([f, g, h], key=by_years_experience)

rawDescription: Discuss the "sorted" function in Python

description: Discuss the "sorted" function in Python


code:
import csv
import itertools
import operator

data1 = '''\
AABBCCDD-1.2-2.4-2.6
AABBCCDD-1.2-2.4-2.6
AABBCCDD-1.2-2.4-2.6
AABBCCDD-1.2-2.4-2.6
EEFFGGHHII-2.4-5.6-7.5
'''

data2 = '''\
SHIRT-RED
SHIRT-BLUE
SHIRT-GREEN
SHOE-RED
SHOE-BLUE
'''

def setup():
    '''Generate some sample input files.'''
    with open('sample1.hsv','w') as f:
        f.write(data1)
    with open('sample2.hsv','w') as f:
        f.write(data2)

def process(infile,outfile):
    with open(infile,'r',newline='') as ifile, open(outfile,'w',newline='') as ofile:
        r = csv.reader(ifile,delimiter='-')
        w = csv.writer(ofile,delimiter=',')

        # key is the first column (offset 0)
        # group is an iterator over the lines that have the same key
        for key,group in itertools.groupby(r,operator.itemgetter(0)):
            # Add a final column to the row list.  1 for first item.
            w.writerow(next(group) + [1])
            # Remaining items in group get a zero value in new column.
            for other in group:
                w.writerow(other + [0])

if __name__ == '__main__':
    setup()
    process('sample1.hsv','sample1.csv')
    process('sample2.hsv','sample2.csv')

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


code: columns = ["id serial primary key", "name text"]

rawDescription: How to create a table with columns named after values in other table

description: Create a table with columns named after values in other table


code:
letterGoodness = dict(zip(string.ascii_uppercase,letterGoodness))
letterGoodness[' '] = 0
sum(letterGoodness[c] for c in yourstring)

rawDescription: A value in a list, python

description: A value in a list, python


code: ['colonel mustard', 'mrs. white', 'mr. green', 'miss scarlet', 'mrs. peacock', 'revolver', 'wrench', 'candle stick', 'lead pipe', 'knife', 'kitchen', 'hall', 'lounge', 'study', 'billiard room', 'conservatory', 'library', 'ballroom']

rawDescription: Removing items in one array based on whats in another array - python


rawDescription: Find position in a csv list

description: Find position in a csv list


code:
# mark each event day with a 1
aap1_10_years.loc[eventday_idxs, 'event'] = 1
# use cumsum to assign an event number to each event range
aap1_10_years['event'] = aap1_10_years['event'].fillna(0).cumsum()

rawDescription: How can i do calculations on subsets , Panda's way, without looping

description: Do calculations on subsets , Panda's way, without looping


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


code:
chained = itertools.chain.from_iterable(sixbit)
[''.join(bits) for bits in itertools.izip(*[chained]*8)]

rawDescription: iterating over list of string and combining string values Python

description: Iterating over list of string and combining string values Python


code:
import operator
mylist = [[2, 5, 3],
          [2, 4, 9],
          [0, 9, 1],
          [1, 1, 1],
          [4, 7, 5]]

mylist.sort(key=operator.itemgetter(1))

rawDescription: How to use the quick sort to sort by index in a list of a list

description: Use the quick sort to sort by index in a list of a list


code:
import os
os.system('python hello.py')

rawDescription: Python open file in specific application using xdg-utils?


code:
p = {"var": ["value", "value2"], "var2": "yetanothervalue"}
urlencode([(k, v) for k, vs in p.items()
    for v in isinstance(vs, list) and vs or [vs]])

rawDescription: Python urllib2 sending POST data

description: Python urllib2 sending POST data


code:
import win32com.client

ldap="SELECT cn, mail, msExchMailboxGuid From 'LDAP://OU=XXXXXXXX,DC=center,DC=intranet,DC=sinotrans'"

c = win32com.client.Dispatch('ADODB.Connection')
c.Open('Provider=ADsDSOObject')

rs,rc = c.Execute(ldap)
rs.MoveFirst()
while not rs.EOF:
    for e in rs.fields:
        if e.Name != 'msExchMailboxGuid':
            print('{}: {}'.format(e.Name, e.Value))
        else:
            if e.Value is not None:
                print('{}: {}'.format(e.Name, hex_to_guid(''.join(['{:x}'.format(ord(x)) for x in e.Value]))))
    print()
    rs.MoveNext()

rawDescription: How to get the value of msExchMailboxGuid of an Actvie Directory user by python script

description: Get the value of msExchMailboxGuid of an Actvie Directory user by python script


rawDescription: How to toggle enabled state of widgets based on QcomboBox index

description: Toggle enabled state of widgets based on QcomboBox index


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

wait = WebDriverWait(driver, 10)

# locate and switch to the iframe
iframe = driver.find_element_by_css_selector("#mainContent iframe")
driver.switch_to.frame(iframe)

# wait for the table to load
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.companyName')))

print(driver.page_source)

rawDescription: Web scraping with Selenium

description: Web scraping with Selenium