code:
'''Test for ratio of Poisson intensities in two independent samples

Author: Josef Perktold
License: BSD-3

destination statsmodels

'''

from __future__ import division
import numpy as np
from scipy import stats

# copied from statsmodels.stats.weightstats
def _zstat_generic2(value, std_diff, alternative):
    '''generic (normal) z-test to save typing

    can be used as ztest based on summary statistics
    '''
    zstat = value / std_diff
    if alternative in ['two-sided', '2-sided', '2s']:
        pvalue = stats.norm.sf(np.abs(zstat))*2
    elif alternative in ['larger', 'l']:
        pvalue = stats.norm.sf(zstat)
    elif alternative in ['smaller', 's']:
        pvalue = stats.norm.cdf(zstat)
    else:
        raise ValueError('invalid alternative')
    return zstat, pvalue

def poisson_twosample(count1, exposure1, count2, exposure2, ratio_null=1,
                      method='score', alternative='2-sided'):
    '''test for ratio of two sample Poisson intensities

    If the two Poisson rates are g1 and g2, then the Null hypothesis is

    H0: g1 / g2 = ratio_null

    against one of the following alternatives

    H1_2-sided: g1 / g2 != ratio_null
    H1_larger: g1 / g2 > ratio_null
    H1_smaller: g1 / g2 < ratio_null

    Parameters
    ----------
    count1: int
        Number of events in first sample
    exposure1: float
        Total exposure (time * subjects) in first sample
    count2: int
        Number of events in first sample
    exposure2: float
        Total exposure (time * subjects) in first sample
    ratio: float
        ratio of the two Poisson rates under the Null hypothesis. Default is 1.
    method: string
        Method for the test statistic and the p-value. Defaults to `'score'`.
        Current Methods are based on Gu et. al 2008
        Implemented are 'wald', 'score' and 'sqrt' based asymptotic normal
        distribution, and the exact conditional test 'exact-cond', and its mid-point
        version 'cond-midp', see Notes
    alternative : string
        The alternative hypothesis, H1, has to be one of the following

           'two-sided': H1: ratio of rates is not equal to ratio_null (default)
           'larger' :   H1: ratio of rates is larger than ratio_null
           'smaller' :  H1: ratio of rates is smaller than ratio_null

    Returns
    -------
    stat, pvalue two-sided

    not yet
    #results : Results instance
    #    The resulting test statistics and p-values are available as attributes.

    Notes
    -----
    'wald': method W1A, wald test, variance based on separate estimates
    'score': method W2A, score test, variance based on estimate under Null
    'wald-log': W3A
    'score-log' W4A
    'sqrt': W5A, based on variance stabilizing square root transformation
    'exact-cond': exact conditional test based on binomial distribution
    'cond-midp': midpoint-pvalue of exact conditional test

    The latter two are only verified for one-sided example.

    References
    ----------
    Gu, Ng, Tang, Schucany 2008: Testing the Ratio of Two Poisson Rates,
    Biometrical Journal 50 (2008) 2, 2008

    '''

    # shortcut names
    y1, n1, y2, n2 = count1, exposure1, count2, exposure2
    d = n2 / n1
    r = ratio_null
    r_d = r / d

    if method in ['score']:
        stat = (y1 - y2 * r_d) / np.sqrt((y1 + y2) * r_d)
        dist = 'normal'
    elif method in ['wald']:
        stat = (y1 - y2 * r_d) / np.sqrt(y1 + y2 * r_d**2)
        dist = 'normal'
    elif method in ['sqrt']:
        stat = 2 * (np.sqrt(y1 + 3 / 8.) - np.sqrt((y2 + 3 / 8.) * r_d))
        stat /= np.sqrt(1 + r_d)
        dist = 'normal'
    elif method in ['exact-cond', 'cond-midp']:
        from statsmodels.stats import proportion
        bp = r_d / (1 + r_d)
        y_total = y1 + y2
        stat = None
        pvalue = proportion.binom_test(y1, y_total, prop=bp, alternative=alternative)
        if method in ['cond-midp']:
            # not inplace in case we still want binom pvalue
            pvalue = pvalue - 0.5 * stats.binom.pmf(y1, y_total, bp)

        dist = 'binomial'

    if dist == 'normal':
        return _zstat_generic2(stat, 1, alternative)
    else:
        return stat, pvalue

from numpy.testing import assert_allclose

# testing against two examples in Gu et al

print('\ntwo-sided')
# example 1
count1, n1, count2, n2 = 60, 51477.5, 30, 54308.7

s1, pv1 = poisson_twosample(count1, n1, count2, n2, method='wald')
pv1r = 0.000356
assert_allclose(pv1, pv1r*2, rtol=0, atol=5e-6)
print('wald', s1, pv1 / 2)   # one sided in the "right" direction

s2, pv2 = poisson_twosample(count1, n1, count2, n2, method='score')
pv2r = 0.000316
assert_allclose(pv2, pv2r*2, rtol=0, atol=5e-6)
print('score', s2, pv2 / 2)   # one sided in the "right" direction

s2, pv2 = poisson_twosample(count1, n1, count2, n2, method='sqrt')
pv2r = 0.000285
assert_allclose(pv2, pv2r*2, rtol=0, atol=5e-6)
print('sqrt', s2, pv2 / 2)   # one sided in the "right" direction

print('\ntwo-sided')
# example2
# I don't know why it's only 2.5 decimal agreement, rounding?
count1, n1, count2, n2 = 41, 28010, 15, 19017
s1, pv1 = poisson_twosample(count1, n1, count2, n2, method='wald', ratio_null=1.5)
pv1r = 0.2309
assert_allclose(pv1, pv1r*2, rtol=0, atol=5e-3)
print('wald', s1, pv1 / 2)   # one sided in the "right" direction

s2, pv2 = poisson_twosample(count1, n1, count2, n2, method='score', ratio_null=1.5)
pv2r = 0.2398
assert_allclose(pv2, pv2r*2, rtol=0, atol=5e-3)
print('score', s2, pv2 / 2)   # one sided in the "right" direction

s2, pv2 = poisson_twosample(count1, n1, count2, n2, method='sqrt', ratio_null=1.5)
pv2r = 0.2499
assert_allclose(pv2, pv2r*2, rtol=0, atol=5e-3)
print('sqrt', s2, pv2 / 2)   # one sided in the "right" direction

print('\none-sided')
# example 1 onesided
count1, n1, count2, n2 = 60, 51477.5, 30, 54308.7

s1, pv1 = poisson_twosample(count1, n1, count2, n2, method='wald', alternative='larger')
pv1r = 0.000356
assert_allclose(pv1, pv1r, rtol=0, atol=5e-6)
print('wald', s1, pv1)   # one sided in the "right" direction

s2, pv2 = poisson_twosample(count1, n1, count2, n2, method='score', alternative='larger')
pv2r = 0.000316
assert_allclose(pv2, pv2r, rtol=0, atol=5e-6)
print('score', s2, pv2)   # one sided in the "right" direction

s2, pv2 = poisson_twosample(count1, n1, count2, n2, method='sqrt', alternative='larger')
pv2r = 0.000285
assert_allclose(pv2, pv2r, rtol=0, atol=5e-6)
print('sqrt', s2, pv2)   # one sided in the "right" direction

# 'exact-cond', 'cond-midp'

s2, pv2 = poisson_twosample(count1, n1, count2, n2, method='exact-cond',
                            ratio_null=1, alternative='larger')
pv2r = 0.000428  # typo in Gu et al, switched pvalues between C and M
assert_allclose(pv2, pv2r, rtol=0, atol=5e-4)
print('exact-cond', s2, pv2)   # one sided in the "right" direction

s2, pv2 = poisson_twosample(count1, n1, count2, n2, method='cond-midp',
                            ratio_null=1, alternative='larger')
pv2r = 0.000310
assert_allclose(pv2, pv2r, rtol=0, atol=5e-4)
print('cond-midp', s2, pv2)   # one sided in the "right" direction

print('\none-sided')
# example2 onesided
# I don't know why it's only 2.5 decimal agreement, rounding?
count1, n1, count2, n2 = 41, 28010, 15, 19017
s1, pv1 = poisson_twosample(count1, n1, count2, n2, method='wald',
                            ratio_null=1.5, alternative='larger')
pv1r = 0.2309
assert_allclose(pv1, pv1r, rtol=0, atol=5e-4)
print('wald', s1, pv1)   # one sided in the "right" direction

s2, pv2 = poisson_twosample(count1, n1, count2, n2, method='score',
                            ratio_null=1.5, alternative='larger')
pv2r = 0.2398
assert_allclose(pv2, pv2r, rtol=0, atol=5e-4)
print('score', s2, pv2)   # one sided in the "right" direction

s2, pv2 = poisson_twosample(count1, n1, count2, n2, method='sqrt',
                            ratio_null=1.5, alternative='larger')
pv2r = 0.2499
assert_allclose(pv2, pv2r, rtol=0, atol=5e-4)
print('score', s2, pv2)   # one sided in the "right" direction

# 'exact-cond', 'cond-midp'

s2, pv2 = poisson_twosample(count1, n1, count2, n2, method='exact-cond',
                            ratio_null=1.5, alternative='larger')
pv2r = 0.2913
assert_allclose(pv2, pv2r, rtol=0, atol=5e-4)
print('exact-cond', s2, pv2)   # one sided in the "right" direction

s2, pv2 = poisson_twosample(count1, n1, count2, n2, method='cond-midp',
                            ratio_null=1.5, alternative='larger')
pv2r = 0.2450
assert_allclose(pv2, pv2r, rtol=0, atol=5e-4)
print('cond-midp', s2, pv2)   # one sided in the "right" direction

rawDescription: Implimentation of E-test for Poisson in Python


rawDescription: How can I pairwise sum two equal-length tuples

description: Pairwise sum two equal-length tuples


rawDescription: Plotting non-standard axes

description: Plotting non-standard axes


code: itertools.product([0, 1], repeat=14)

rawDescription: All combinations fourteen item array

description: All combinations fourteen item array


code:
a = np.arange(16, dtype=np.uint8).reshape(4,4)
a
imresize(a, (2,2), mode='P')
imresize(a, (2,2), mode='L')

rawDescription: Python code to quickly reduce the resolution of an image using numpy


code:
d2_dict['fresh_food', 'food']['eggs'] = 'delicious'
d2_dict

rawDescription: 2d dictionary with multiple keys per value


code:
def get_index_map(table):
    output = {}
    for r_index, row in enumerate(table):
        for c_index, letter in enumerate(row):
            output[letter] = (r_index, c_index)
    return output

rawDescription: Python-Location of a letter in a two dimensional string


rawDescription: Using same function as instance and classmethod in python


code:
import re
numbers = []
for line in log:
    digit_match = re.search("(\d+)$", line)
    if digit_match is not None:
        numbers.append(int(digit_match.group(1)))

rawDescription: How would you find text in a string in python and then look for a number after it?


code:
def is_cross_valid(self, form, formsets):
  return True

rawDescription: django - How to cross check ModelAdmin and its inlines?

description: Django - How to cross check ModelAdmin and its inlines


code: targetText2 = text.select(".Book-Title- ~ .Text")

rawDescription: Beautiful Soup 4 CSS sibling selector

description: Beautiful Soup 4 CSS sibling selector


code:
TextOFFilm = movieText.text

TextOFTopix = movieTopic.text

rawDescription: Python Selenium Get HTML Text in Variables


code: product(*('1!' if c == '1' else c for c in s))

rawDescription: How do I iterate through all sub-combinations of 1's and !'s?

description: Iterate through all sub-combinations of 1's and !'s


code:
#!/usr/bin/env python3
from itertools import groupby
from pprint import pprint

x = [
        {"foo":"A", "bar":"R", "baz":"X"},
        {"foo":"A", "bar":"R", "baz":"Y"},
        {"foo":"B", "bar":"S", "baz":"X"},
        {"foo":"A", "bar":"S", "baz":"Y"},
        {"foo":"C", "bar":"R", "baz":"Y"},
    ]

def fun(x, l):
    ks = ['foo', 'bar', 'baz']
    kn = ks[l]
    kk = lambda i:i[kn]
    for k,g in groupby(sorted(x, key=kk), key=kk):
        kg = [dict((k,v) for k,v in i.items() if k!=kn) for i in g]
        d = {}
        d[kn] = k
        if l<len(ks)-1:
            d[ks[l+1]+'s'] = list(fun(kg, l+1))
        yield d

pprint(list(fun(x, 0)))

rawDescription: Generic way to create nested dictionary from flat list in python


code:
def loadImage(self, event):
    self.image_file = next(self.images)
    image_file = os.path.join(IMAGE_DIR, self.image_file)
    bmp = wx.Image(image_file, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
    wx.StaticBitmap(self.panel, -1, bmp, pos=(200, 50), size=(417, 133))

rawDescription: wxPython Button to load multiple images

description: WxPython Button to load multiple images


rawDescription: Check if a list is a rotation of another list that works with duplicates


code:
class MyContainer(dict):
    def newmethod1(self, args):
        pass

    def newmethod2(self, args2):
        pass

def createImageDb(directory):
    d = MyContainer()
    # fill the container
    return d

rawDescription: Subclassing dict: should dict.__init__() be called?


code: list_id = [br.id for br in user_obj.browse(cr, uid, uid).company_ids]

rawDescription: Retrieve "id" as list from browse record of many2many field?


code:
for line in fileObj:
   splitline = line.rstrip().split(',')
   date, name, time, data1 = splitline[0:4]
   data2, data3 = splitline[18:20]

rawDescription: How to take multiple slices of a line from a file in python?

description: Take multiple slices of a line from a file in python


code:
d = dict.from_keys(range(100000))
for i, tup in enumerate(list_o_tuples):
    del d[i]
    d[list_o_tuples[0]] = list_o_tuples[1]

rawDescription: Initialising a large dict with unknown keys? Is there a better way than this?


code:
#!/usr/bin/python

def login(): # <-- this only defines the function
    with open("names.txt", "r") as fin:
        users = fin.read()
    if "Smith" in users:
        print("truth")
    else:
        print("false")

login() # <-- this actually runs the function you have defined

rawDescription: Strange behavior in python if/in word in file check


code: Counter({'hello': 4, 'hi': 1})

rawDescription: How to count elements in a list and return a dict?

description: Count elements in a list and return a dict


code: score = '#4=DFF\n#1=DDF\n'

rawDescription: How to sum value of integers based on position?

description: Sum value of integers based on position


code:
query = """
INSERT INTO
    `property`
    (`date`,`Url`,`Rooms`,`place`,`PId`,`Phonenumber1`,`Phonenumber2`,`Phonenumber3`,`Typeofperson`,` Nameofperson`,`typeofproperty`,`Sq.Ft`,`PerSq.Ft`,`AdDate`,`AdYear`)
VALUES
    (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
"""

# define your field values as a list
values = [today, a['href'] if a else 'NULL', ... ]

# execute
cursor.execute(query, values)

rawDescription: TypeError: unsupported operand type(s) for +: 'Tag' and 'str'


code:
import wx, wx.lib.newevent, threading
import win32event, win32pipe, win32file, pywintypes, winerror

NewMessage, EVT_NEW_MESSAGE = wx.lib.newevent.NewEvent()
class MessageNotifier(threading.Thread):
    pipe_name = r"\\.\pipe\named_pipe_demo"

    def __init__(self, frame):
        threading.Thread.__init__(self)
        self.frame = frame

    def run(self):
        open_mode = win32pipe.PIPE_ACCESS_DUPLEX | win32file.FILE_FLAG_OVERLAPPED
        pipe_mode = win32pipe.PIPE_TYPE_MESSAGE

        sa = pywintypes.SECURITY_ATTRIBUTES()
        sa.SetSecurityDescriptorDacl(1, None, 0)

        pipe_handle = win32pipe.CreateNamedPipe(
            self.pipe_name, open_mode, pipe_mode,
            win32pipe.PIPE_UNLIMITED_INSTANCES,
            0, 0, 6000, sa
        )

        overlapped = pywintypes.OVERLAPPED()
        overlapped.hEvent = win32event.CreateEvent(None, 0, 0, None)

        while 1:
            try:
                hr = win32pipe.ConnectNamedPipe(pipe_handle, overlapped)
            except:
                # Error connecting pipe
                pipe_handle.Close()
                break

            if hr == winerror.ERROR_PIPE_CONNECTED:
                # Client is fast, and already connected - signal event
                win32event.SetEvent(overlapped.hEvent)

            rc = win32event.WaitForSingleObject(
                overlapped.hEvent, win32event.INFINITE
            )

            if rc == win32event.WAIT_OBJECT_0:
                try:
                    hr, data = win32file.ReadFile(pipe_handle, 64)
                    win32file.WriteFile(pipe_handle, "ok")
                    win32pipe.DisconnectNamedPipe(pipe_handle)
                    wx.PostEvent(self.frame, NewMessage(data=data))
                except win32file.error:
                    continue

class Messages(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        self.messages = wx.TextCtrl(self, style=wx.TE_MULTILINE | wx.TE_READONLY)
        self.Bind(EVT_NEW_MESSAGE, self.On_Update)

    def On_Update(self, event):
        self.messages.Value += "\n" + event.data

app = wx.PySimpleApp()
app.TopWindow = Messages()
app.TopWindow.Show()
MessageNotifier(app.TopWindow).start()
app.MainLoop()

rawDescription: Showing data in a GUI where the data comes from an outside source


code:
from lxml import html

url = 'http://www.makospearguns.com/product-p/mcffgb.htm'
xpath = '//*[@id="v65-product-parent"]/tbody/tr[2]/td[2]/table[1]/tbody/tr/td/table/tbody/tr[2]/td[2]/table/tbody/tr[1]/td[1]/div/table/tbody/tr/td/font/div/b/span/text()'

src = load_source(url)

tree = html.fromstring(src)
text = tree.xpath(xpath)

rawDescription: How would you give Chrome's version of a webpage to python?


code:
import decimal
d = decimal.Decimal("9.0")
d
help(d.normalize)
d.normalize()
str(d)
str(d.normalize())

rawDescription: How can I "trim" significant figures of a Decimal in Python to only those that are non-zero?


code: ",".join("'%s'" % qb[0] for qb in qb_data)

rawDescription: using python join to join first element of each list

description: Using python join to join first element of each list


code: ((cl != cl.shift()) & cl).cumsum()

rawDescription: Pandas Label Sequences of 1 in a 0-1 valued Series


code: np.fmax.accumulate(a) + (a*0)

rawDescription: Compare element with previous along axis


code:
weight_sample_indexes = lambda weights, k: random.sample([val
        for val, cnt in enumerate(weights) for i in range(cnt)], k)

rawDescription: Weighted random sample in python

description: Weighted random sample in python


code:
def decimal_default(obj):
    if isinstance(obj, decimal.Decimal):
        return float(obj)
    raise TypeError

json.dumps({'x': decimal.Decimal('5.5')}, default=decimal_default)

rawDescription: Python to JSON Serialization fails on Decimal


rawDescription: Pygame the rectangle is not moving?

description: Pygame the rectangle is not moving


code: df = df.drop(df2, axis=1)

rawDescription: Python Pandas - Main DataFrame, want to drop all columns in smaller DataFrame


code:
mp.MasterProcess()
mp_proxy = MasterProcessProxy(mp.event_generator_remote)
mp_proxy.create_something(spam="eggs", bacon="baked beans")
# Will call pipe.send(["create_something", {"spam":"eggs", "bacon":"baked beans"}])

rawDescription: Python use a virtual class to apply a generic "pipe" pattern


code:
wanted_files = find_files_to_list('/original_project/', '*.html')
for filename in find_files('/copy_project/', '*'):
    if filename not in wanted_files:
        os.remove(filename)

rawDescription: How to find files and save them to different folder in python

description: Find files and save them to different folder in python


rawDescription: CherryPy index path

description: CherryPy index path


code:
def _new_game_board() -> [[str]]:
    """
    Creates a new game board.  Initially, a game board has the size
    BOARD_COLUMNS x BOARD_ROWS and is comprised only of strings with the
    value NONE
    """

    return [[None] * BOARD_COLUMNS for _ in range(BOARD_ROWS)]

ConnectFourGameState = namedtuple('ConnectFourGameState', ['board', 'turn'])

def new_game_state() -> ConnectFourGameState:
    """
    Returns a ConnectFourGameState representing a brand new game
    in which no moves have been made yet.
    """

    return ConnectFourGameState(board=_new_game_board(), turn=RED)

rawDescription: Printing a two dimensional list

description: Printing a two dimensional list


code:
xticks = ax.get_xticks()[1:-1] #< notice the slicing
yticks = ax.get_yticks()[1:-1] #< notice the slicing
xgrid, ygrid = np.meshgrid(xticks, yticks)

rawDescription: Instead of grid lines on a plot, can matplotlib print grid crosses?


code: sorted(prob.items(), key = lambda x: x[1], reverse = True)

rawDescription: how can I sort dictionary whose key is tuple and value is int?

description: Sort dictionary whose key is tuple and value is int


code:
company = Company.objects.get(pk=1)
offices = company.office_set.all()
for office in offices:
    employees = office.employee_set.all()

rawDescription: Django model relation

description: Django model relation


rawDescription: splitting merged words in python

description: Splitting merged words in python


code:
settings = QtCore.QSettings(org_name, app_name)
is_floating = main_win._ui.dockWin.isFloating()
settings.setValue('dockWin/isFloating', is_floating)
main_win._ui.dockWin.setFloating(True)
settings.setValue('geometry', main_win.saveGeometry())
settings.setValue('state', main_win.saveState())

rawDescription: Qt - pyside - saveGeometry() saveState()


code:
def create_grid(*commands):
    process = subprocess.Popen(
        ['grid.exe'],
        stdin=subprocess.PIPE)

    process.communicate('\n'.join(commands) + '\n')

rawDescription: How to run executable from python and pass it arguments asked for?

description: Run executable from python and pass it arguments asked for


code:
from itertools import count
from collections import Counter
lis = ['a','a','a','b','b','c']
c = Counter(lis)
dic = {k: count(1) for k in c}
[x + ( str(next(dic[x])) if c[x]>1 else '') for x in lis]

rawDescription: Best Way to Distinguish Equal List items in Python


code:
from pprint import pprint
pprint([OrderedDict(zip(names, subl)) for subl in list_of_lists])

rawDescription: How to make ordered dictionary from list of lists?

description: Make ordered dictionary from list of lists


rawDescription: Finding groups of letters, numbers, or symbols


code:
seen = set()
for x in foo:
    if x in seen:
        continue
    seen.add(x)
    # do something

rawDescription: What is the python equivalent of the Perl pattern to track if something has already been seen?


rawDescription: How can i create substrings from given strings in python?

description: Create substrings from given strings in python


rawDescription: Any possible combination in a list?

description: Any possible combination in a list


code:
pl1, = plot(x1, y1, '.', alpha=0.1, label='plot1')
pl2, = plot(x2, y2, '.', alpha=0.1, label='plot2')
p1 = Rectangle((0, 0), 1, 1, fc=pl1.get_color())
p2 = Rectangle((0, 0), 1, 1, fc=pl2.get_color())
legend((p1, p2), (pl1.get_label(), pl2.get_label()), loc='best')

rawDescription: Matplotlib Legend for Scatter with custom colours


rawDescription: Sort dict by sub-value in Python

description: Sort dict by sub-value in Python


code: pixels[kitty.width * y + x]

rawDescription: Get colour of specified pixel in window

description: Get colour of specified pixel in window


code:
result = []
for idx, row in df.iterrows():
    for dct in row['RANKS']:
        dct['ID'] = row['ID']
        dct['count'] = row['count']
        result.append(dct)
del df
result = pd.DataFrame(result)
result['rank'] = result['rank'].astype(np.int32)
result['price'] = result['price'].str.replace('$', '')
result['price'] = result['price'].astype('float')
print(result)

rawDescription: List of dicts as Pandas column

description: List of dicts as Pandas column


code:
from nltk import pos_tag
sent = 'The pizza was awesome and brilliant'.split()
pos_tag(sent)
sent = 'The pizza was good but pasta was bad'.split()
pos_tag(sent)

rawDescription: How to use nltk regex pattern to extract a specific phrase chunk?

description: Use nltk regex pattern to extract a specific phrase chunk


code: np.kron(flip, flip)

rawDescription: NumPy Tensor / Kronecker product of matrices coming out shuffled


rawDescription: How to get python to return a list of lists as a column matrix?

description: Get python to return a list of lists as a column matrix


code: ref_files = map(open, reflist)

rawDescription: How to open files from a list

description: Open files from a list


code:
with open('path', 'r', encoding='utf-8') as file:
    style_sheet = file.read()
app.setStyleSheet(style_sheet)

rawDescription: Pyside setStyleSheet() and long lines?

description: Pyside setStyleSheet() and long lines


rawDescription: Splitting an formatted data list

description: Splitting an formatted data list


code:
for lst in input:
    for i in range(len(lst)):
        if lst[i] not in item_node:
            item_node[lst[i]] = [lst[i]]
            if i == 0:
                output.append(item_node[lst[i]])
            else:
                item_node[lst[i-1]].append(item_node[lst[i]])

Out[58]: [['a', ['b', ['c'], ['d']], ['e']]]

rawDescription: python - going from a stack trace to a tree

description: Python - going from a stack trace to a tree


code:
with Foo() as foo:
    print ('Hello World!')

rawDescription: Why is __del__ called at the end of a with block?


code:
a = 23
h = "Liam Neeson"
k = {'a', 'b', 'c'}
set(dir()) - set(dir(__builtins__)) - {'__builtins__'}

rawDescription: Is there a way to see the variables declared in a session in Python?


code:
MyResolver.add_implicit_resolver(
    u'tag:yaml.org,2002:bool',
    re.compile(u'''^(?:true|True|TRUE|false|False|FALSE)$''', re.X),
    list(u'tTfF'))

rawDescription: Can't parse YAML correctly

description: Can't parse YAML correctly


code: username, password = queue, passwords.get(queue, None)

rawDescription: python, replace if/elif with dictionary

description: Python, replace if/elif with dictionary


code:
finger_names, finger_lengths
s_tuples = sorted(zip(finger_names, finger_lengths), key=lambda x: x[1])
finger_names, finger_lengths = zip(*s_tuples)
finger_names, finger_lengths

rawDescription: How to sort object attributes

description: Sort object attributes


code: df.resample('w', how='sum', axis=1)

rawDescription: How to use pandas to group pivot table results by week?

description: Use pandas to group pivot table results by week


rawDescription: Python: Converting HEX string to bytes

description: Python: Converting HEX string to bytes


code:
timeit.repeat("test('test')", setup="from __main__ import test")
timeit.repeat("testanon('test')", setup="from __main__ import testanon")

rawDescription: Performance gain using anonymous functions?


code:
from celery.task import task
@task
def process(i):
    # do something with i
    i += 1
    # return a result
    return i

rawDescription: Suggestions on distributing python data/code over worker nodes?


rawDescription: how to remove and add character/integer in python?

description: Remove and add character/integer in python


rawDescription: Iterative Comparison of raw_input

description: Iterative Comparison of raw_input


code:
response['pk-user'] = rental.pk
return response

rawDescription: Pass url with id to html page that is called by another url


code:
import numpy as np

A = [1,2,3,1,2,3,1,2,3,1,2]
B = [1,1,1,2,2,2,1,1,1,2,2]
C = [1,1,1,1,1,1,2,2,2,2,2]
D = [5,6,3,6,3,5,2,4,6,8,7]
A, B, C, D = map(np.array, [A, B, C, D])

fig = plt.figure()
c_unique = np.unique(C)
fig.set_size_inches(4 * len(c_unique), 4)

# Add axis for each unique C-value.
axes = [fig.add_subplot(1, len(c_unique), i + 1, projection='3d') for i, _ in enumerate(c_unique)]

for c, ax in zip(c_unique, axes):
    # Use boolean indexing of numpy-arrays to plot values for current C.
    ax.scatter(A[C == c], B[C == c], D[C == c])

    ax.set_xlabel('A')
    ax.set_ylabel('B')
    ax.set_zlabel('D')
    ax.set_title('C = {}'.format(c))

fig.tight_layout()

rawDescription: Plot specific data sets from arrays - Python


code: sum(len(c) == 1 for s in data for c in s.split('a')) # 9

rawDescription: Count specific characters with a specific condition in python


code:
with open(selectedCase) as infh:
    dataInput = [ast.literal_eval(line) for line in infh]

rawDescription: How to convert list lookalike into a list?

description: Convert list lookalike into a list


code:
import operator
stats = {'a':1000, 'b':3000, 'c': 100, 'd':3000}
max(stats.iteritems(), key=operator.itemgetter(1))[0]

rawDescription: Getting key with maximum value in dictionary?


code:
for col in mnX.T:
    col += cnA[:, 0].T

rawDescription: Modify numpy array column by column inside a loop


rawDescription: Python remove everything after a space with hex \x00


code:
def norm_spirals(a,k):
    se = 0.25 + 0.5*k + 0.5j*a
    return pi * abs(gamma(se)/gamma(se + 0.5)) ** 2.0

rawDescription: Order of operations and fast computation in Python


rawDescription: Python program don't run

description: Python program don't run


code:
for arg in arguments:
    (equal if '=' in arg else plain).append(arg)

rawDescription: Evaluating into two or more lists

description: Evaluating into two or more lists


code:
import numpy as np
import pandas as pd

df = pd.DataFrame({'EvntType': [2, 1, 2, 2, 2, 2, 2, 1, 2, 1, 2],
                   'Time': [15, 19, 21, 23, 25, 26, 28, 31, 33, 36, 39],
                   'Var1': [1, 1, 6, 3, 0, 2, 3, 5, 1, 5, 1],
                   'Var2': [17, 45, 43, 65, 76, 35, 25, 16, 25, 36, 21]})

# Remove rows where Var1 equals 0
df = df.loc[df['Var1'] != 0]

mask = df['EvntType']==1
times = df.loc[mask, 'Time']
idx = np.flatnonzero(mask)

result = pd.DataFrame(
    {'start_time': times[:-1],
     'time_inbetween': np.diff(times),
     'event_count': np.diff(idx)-1})

print(result)

rawDescription: Iterate through rows in pandas dateframe in a cleaner way using .iterrows() and keep track of rows inbetween specific values


code:
@login_required()
def index(request):
    rhi_model_form = RhiModelForm()
    activity_model_form = ActivityModelForm()
    rhi_list = Rhi.objects.all()
    activity_list = Activity.objects.all()
    context = {
        'rhi_model_form': rhi_model_form,
        'rhi_list': rhi_list,
        'activity_model_form': activity_model_form,
        'activity_list': activity_list,
    }
    return render(request, 'ec/index.html', context)

@login_required()
def add_rhi(request):
    if request.method == 'POST':
        rForm = RhiModelForm(request.POST)
        if rForm.is_valid():
            rForm.save()
            return redirect('ec:index')
        else:
            rhi_errors = rForm.errors
            rhi_model_form = RhiModelForm()
            context = {
                'rhi_errors': rhi_errors,
                'rhi_model_form': rhi_model_form,
            }
            return render(request, 'ec/index.html', context)

@login_required()
def add_activity(request):
    if request.method == 'POST':
        aForm = ActivityModelForm(request.POST)
        if aForm.is_valid():
            activity = aForm.save(commit=False)
            activity.lastModifiedBy = request.user
            activity.createdBy = request.user
            activity.save()
            return redirect('ec:index')
        else:
            activity_errors = aForm.errors
            activity_model_form = ActivityModelForm()
            context = {
                'activity_errors': activity_errors,
                'activity_model_form': activity_model_form,
            }
            return render(request, 'ec/index.html', context)

rawDescription: Django ModelForm - Multiple Forms in Template with Different Action Views and Redirects


rawDescription: Saving XML using ETree in Python. It's not retaining namespaces, and adding ns0, ns1 and removing xmlns tags


code:
def detect_language(line):
    maxchar = max(line)
    if u'\u0c00' <= maxchar <= u'\u0c7f':
        return 'telugu'
    elif u'\u0900' <= maxchar <= u'\u097f':
        return 'hindi'
    return 'english'

rawDescription: How to detect unicode character range in Python?

description: Detect unicode character range in Python


code: result = [(x, y) for x, y in c if isTrue(x, y)]

rawDescription: How to select element pairs of a list that satisfy some criterion in Python?

description: Select element pairs of a list that satisfy some criterion in Python


code:
import numpy as np
strc = np.zeros(4, dtype=[('x', int), ('y', int), ('z', int)])
strc_view = strc.view(int).reshape(len(strc), -1)
x = np.array([2, 3])
strc_view[0, [0, 1]] = x

rawDescription: numpy: how to fill multiple fields in a structured array at once

description: Numpy: how to fill multiple fields in a structured array at once


rawDescription: pandas dataframe view vs copy, how do I tell?

description: Pandas dataframe view vs copy, how do I tell


code:
key = input("Write a list name")
# print the list under 'lst' for the dictionary under 'key'
# print poland[key]["lst"]
# EDIT: python 3's print a function, thanks @Ffisegydd:
print(poland[key]["lst"])

rawDescription: Ask user and print a list

description: Ask user and print a list


rawDescription: regex to match words of length specified within string

description: Regex to match words of length specified within string


code:
import tkinter

class MessageBox(object):

    def __init__(self, msg, b1, b2, frame, t, entry):

        root = self.root = tkinter.Tk()
        root.title('Message')
        self.msg = str(msg)
        # ctrl+c to copy self.msg
        root.bind('<Control-c>', func=self.to_clip)
        # remove the outer frame if frame=False
        if not frame: root.overrideredirect(True)
        # default values for the buttons to return
        self.b1_return = True
        self.b2_return = False
        # if b1 or b2 is a tuple unpack into the button text & return value
        if isinstance(b1, tuple): b1, self.b1_return = b1
        if isinstance(b2, tuple): b2, self.b2_return = b2
        # main frame
        frm_1 = tkinter.Frame(root)
        frm_1.pack(ipadx=2, ipady=2)
        # the message
        message = tkinter.Label(frm_1, text=self.msg)
        message.pack(padx=8, pady=8)
        # if entry=True create and set focus
        if entry:
            self.entry = tkinter.Entry(frm_1)
            self.entry.pack()
            self.entry.focus_set()
        # button frame
        frm_2 = tkinter.Frame(frm_1)
        frm_2.pack(padx=4, pady=4)
        # buttons
        btn_1 = tkinter.Button(frm_2, width=8, text=b1)
        btn_1['command'] = self.b1_action
        btn_1.pack(side='left')
        if not entry: btn_1.focus_set()
        btn_2 = tkinter.Button(frm_2, width=8, text=b2)
        btn_2['command'] = self.b2_action
        btn_2.pack(side='left')
        # the enter button will trigger the focused button's action
        btn_1.bind('<KeyPress-Return>', func=self.b1_action)
        btn_2.bind('<KeyPress-Return>', func=self.b2_action)
        # roughly center the box on screen
        # for accuracy see: http://stackoverflow.com/a/10018670/1217270
        root.update_idletasks()
        xp = (root.winfo_screenwidth() // 2) - (root.winfo_width() // 2)
        yp = (root.winfo_screenheight() // 2) - (root.winfo_height() // 2)
        geom = (root.winfo_width(), root.winfo_height(), xp, yp)
        root.geometry('{0}x{1}+{2}+{3}'.format(*geom))
        # call self.close_mod when the close button is pressed
        root.protocol("WM_DELETE_WINDOW", self.close_mod)
        # a trick to activate the window (on windows 7)
        root.deiconify()
        # if t is specified: call time_out after t seconds
        if t: root.after(int(t*1000), func=self.time_out)

    def b1_action(self, event=None):
        try: x = self.entry.get()
        except AttributeError:
            self.returning = self.b1_return
            self.root.quit()
        else:
            if x:
                self.returning = x
                self.root.quit()

    def b2_action(self, event=None):
        self.returning = self.b2_return
        self.root.quit()

    # remove this function and the call to protocol
    # then the close button will act normally
    def close_mod(self):
        pass

    def time_out(self):
        try: x = self.entry.get()
        except AttributeError: self.returning = None
        else: self.returning = x
        finally: self.root.quit()

    def to_clip(self, event=None):
        self.root.clipboard_clear()
        self.root.clipboard_append(self.msg)

rawDescription: Correct way to implement a custom popup tkinter dialog box


code:
def serve_forever(self):
    """Handle one request at a time until doomsday."""
    while 1:
        self.handle_request()

rawDescription: How to stop BaseHTTPServer.serve_forever() in a BaseHTTPRequestHandler subclass?

description: Stop BaseHTTPServer.serve_forever() in a BaseHTTPRequestHandler subclass


code:
time_diff = groups.apply(lambda df: df.datetime.diff().mean())
time_diff.head()

rawDescription: Fastest way to calculate average of datetime rows using pandas


code: lista = [line.strip() for line in z]

rawDescription: readlines() without EOL at the of the lines

description: Readlines() without EOL at the of the lines


rawDescription: Matching urls in html link elements using regex


rawDescription: Can I determine if an application is being run normally vs via a unittest in Python?

description: Determine if an application is being run normally vs via a unittest in Python


code:
import difflib
difflib.get_close_matches('FO', ['FOO', 'BAR', 'BAZ'])
difflib.get_close_matches('F00', ['FOO', 'BAR', 'BAZ'])
difflib.get_close_matches('F0O', ['FOO', 'BAR', 'BAZ'])
difflib.get_close_matches('FO0', ['FOO', 'BAR', 'BAZ'])

rawDescription: Comparing strings in python to find errors


code:
barlist=plt.bar([1,2,3,4], [1,2,3,4])
barlist[0].set_color('r')
plt.show()

rawDescription: Setting Different Bar color in matplotlib Python


code:
from twisted.internet.threads import deferToThread
deferredObj = threads.deferToThread(run_app, self.appDataReceived)

rawDescription: Python Twisted with callInThread

description: Python Twisted with callInThread


code:
def filterOut2FromPartion(list_of_lists):
  final_iterator = []
  for sub_list in list_of_lists:
    final_iterator.append( [x for x in sub_list if x != 2])
  return iter(final_iterator)

filtered_lists = data.mapPartition(filterOut2FromPartion)

rawDescription: How does the pyspark mapPartitions function work?


rawDescription: Calculating the number of years in a pandas dataframe


code:
with open('input.dat', 'wb') as f:
  f.write(struct.pack("IIII", 3,1,2,3))

rawDescription: Store file contents in a buffer and assign it to a pointer in Python


code:
Implementation
import re

data  = """[SectionTitle0]
...
Name: NameOfTechC
...

[SectionTitle1]
...
Name: NameOfZoneC
...
"""

regexStr = r'\[([^\]]*)\][\r\n]+(?:(?!Name:).*[\r\n]+)*?Name:\s*(.*)'
regex    = re.compile(regexStr)
regex.findall(data)

rawDescription: Regex to find the second occurance of an entry


code:
import numpy as np
A = np.sin( np.arange(0, 2*np.pi, np.pi/20) ) # some sine wave
A < 0.1 # or == 0.0, gives an array containing True and False

rawDescription: Making sinusoids from a wave file rectangular


code: diagonal_matrix = diagonalize(A)

rawDescription: create module in python for diagonalizing matrix

description: Create module in python for diagonalizing matrix


rawDescription: Python and Django Blog Sample Project

description: Python and Django Blog Sample Project


code:
legend = ax.legend(loc='upper center', shadow=True)

handles, labels = ax.get_legend_handles_labels()
ax.legend(handles, labels )

rawDescription: Legend format lost after using: ax.legend(handles, labels )


code: self.after_id = self.after(self._FastBlinkTime, self.toggleLeds)

rawDescription: Python Tkinter use to emulate blinking with interaction of multiple buttons


code:
mystring = "Hello" 'World!'
mystring

rawDescription: inverted comma and string in python

description: Inverted comma and string in python


code:
import wikipedia
ny = wikipedia.page("New York")
ny.sections

rawDescription: Wikipedia API for python

description: Wikipedia API for python


code:
subplot(121)
psd(s1, nfft, 1/dt)
plt.title('signal1')

subplot(122)
psd(s2, nfft, 1/dt)
plt.title('signal2')

plt.tight_layout()
show()

rawDescription: How to use the cross-spectral density to calculate the phase shift of two related signals

description: Use the cross-spectral density to calculate the phase shift of two related signals


rawDescription: taking symbol in sum [sympy]

description: Taking symbol in sum [sympy]


code:
X = [scipy.array(line.split(','), dtype='float')
     for line in open('somefile.csv', 'r')]

rawDescription: Pythonic way to populate numpy array

description: Pythonic way to populate numpy array


code:
data = shelve.open("some_filename") # create a shelf instead
data["coords"] = [1,2]
data.close()                        # remember to close it

rawDescription: How to permanently modify a dictionary in python?

description: Permanently modify a dictionary in python


code:
from itertools import chain, imap
from operator import methodcaller

dict(chain.from_iterable(map(methodcaller('iteritems'), lst)))

rawDescription: Python - Convert list of single key dictionaries into a single dictionary


code:
from scrapy import log
from scrapy.http import Request
import tldextract

class DomainDepthMiddleware(object):
    def __init__(self, domain_depths, default_depth):
        self.domain_depths = domain_depths
        self.default_depth = default_depth

    @classmethod
    def from_crawler(cls, crawler):
        settings = crawler.settings
        domain_depths = settings.getdict('DOMAIN_DEPTHS', default={})
        default_depth = settings.getint('DEPTH_LIMIT', 1)

        return cls(domain_depths, default_depth)

    def process_spider_output(self, response, result, spider):
        def _filter(request):
            if isinstance(request, Request):
                # get max depth per domain
                domain = tldextract.extract(request.url).registered_domain
                maxdepth = self.domain_depths.get(domain, self.default_depth)

                depth = response.meta.get('depth', 0) + 1
                request.meta['depth'] = depth

                if maxdepth and depth > maxdepth:
                    log.msg(format="Ignoring link (depth > %(maxdepth)d): %(requrl)s ",
                            level=log.DEBUG, spider=spider,
                            maxdepth=maxdepth, requrl=request.url)
                    return False
            return True

        return (r for r in result or () if _filter(r))

rawDescription: Scrapy set depth limit per allowed_domains


code: [(a, b, date.strftime('%Y-%m-%d %H:%M:%S')) for (a, b, date) in Tuple]

rawDescription: How to apply a function to an element at a particular position in every tuple inside a tuple of tuples?

description: Apply a function to an element at a particular position in every tuple inside a tuple of tuples


code:
def model(self):
    return "{0}.{1}".format(self._meta.app_label, self._meta.object_name).lower()

rawDescription: Django-piston: How can I get app_label + model_name?


code:
import os
os.chdir("/dir/to/goto/before/running/the/script")

# rest of the logic goes here

rawDescription: Running Python Script from different working directory


rawDescription: Python - Convert unicode hex to string

description: Python - Convert unicode hex to string


code:
def getLeftAlignSeries(s):
   a = np.array(np.trim_zeros(s),'f')
   b = np.pad(a,(0,(len(s) - len(a))),mode='constant',constant_values=0)
   return b

rawDescription: Pandas - cohorting in a dataframe

description: Pandas - cohorting in a dataframe


code:
def parse(expr):
    if re.match(r'\A(("[\w\s]+"|\w+)\s+)*\Z', expr):
        return re.findall(r'("[\w\s]+"|\w+)', expr)

rawDescription: Extracting ALL matches of a nested regular expression in python


code:
def application():
    print("="*20)
    print("Enter 0 to multiply two numbers")
    print("Enter 1 to divide two numbers")
    print("Enter -1 to quit the application")
    print("="*20)
    return input("Enter your choice:- ")

choice = application()

while choice != -1:

    if choice == 0:
        first_number = input("Enter 1st number:-") # moved
        second_number = input("Enter 2nd number:- ") # moved

        multiply = first_number * second_number
        print("%d multiply by %d equal to %s" % (first_number, second_number, multiply))
    elif choice == 1:
        first_number = input("Enter 1st number:-") # moved
        second_number = input("Enter 2nd number:- ") # moved

        divide = first_number / second_number
        print("%d divided by %d equal to %s" % (first_number, second_number, divide))
    else:
        print("Invalid option.")

    choice = application()

print("Quit the application")

rawDescription: Indeteminate While Loop

description: Indeteminate While Loop


code: ['index', 'command', ['param1', 'param2', 'param3']]

rawDescription: Help with Python strings

description: Help with Python strings


code:
def __neg__(self):
    return Rational(-self.p, self.q)

rawDescription: Sympy classes Zero, One and NegativeOne, why they exist?


code:
from osgeo import gdal
ds = gdal.Open(path, gdal.GA_ReadOnly)
T0 = Affine.from_gdal(*ds.GetGeoTransform())
ds = None  # close

rawDescription: How to I get the coordinates of a cell in a geotif?


code:
def gt(nums, n):
    if not nums:
        return False
    return max(nums) > n

rawDescription: Check if the list is empty

description: Check if the list is empty


code:
from abc import ABCMeta, abstractmethod

class A(object):
    __metaclass__ = metaclass=ABCMeta

    @abstractmethod
    def do_sth(self):   pass

class A_imp1(A):
    def do_sth(self): return 1

class A_imp2(A):
    def do_sth(self): return 2

rawDescription: Python class interface or inheritance

description: Python class interface or inheritance


code:
import datetime
import pytz

time_zone = pytz.timezone('Asia/Kolkata')

# get naive date
date = datetime.datetime.now().date()
# get naive time
time = datetime.time(12, 30)
# combite to datetime
date_time = datetime.datetime.combine(date, time)
# make time zone aware
date_time = time_zone.localize(date_time)

# convert to UTC
utc_date_time = date_time.astimezone(pytz.utc)
# get time
utc_time = utc_date_time.time()

print(date_time)
print(utc_date_time)
print(utc_time)

rawDescription: Python Convert time to UTC format

description: Python Convert time to UTC format


code:
from collections import OrderedDict

mapping = OrderedDict()
for outer, inner, value in t:
    mapping.setdefault(outer, {})[inner] = value

result = mapping.items()

rawDescription: how can I convert python tuple to dict

description: Convert python tuple to dict


code:
a = {'blue': 4, 'green': {'yellow': {'black': 16}}, 'red': 3}
recurseDict(a)

rawDescription: Python: How to continue a nested for loop?


code:
with open('d:\word_list.txt', 'r') as inF:
 b = input('Enter word: ')
 for letter in b:
  inF.seek(0)
  for item in inF:
   if item.startswith(letter):
    print(item)

rawDescription: searching in text file word list and check all the words that start with each letter I input

description: Searching in text file word list and check all the words that start with each letter I input


code:
def code(noncelist):
    while True:
        client = blocktrail.APIClient(api_key="x", api_secret="x", network="BTC", testnet=False)
        address = client.address('x')
        latest_block = client.block_latest()
        nonce = latest_block['nonce']
        blockhash = latest_block['hash']
        print(nonce)
        noncestr = str(nonce)
        noncelist.append(noncestr)
        print(noncelist)
        time.sleep(60)

code([])

rawDescription: Creating a list in python using the same string but with different values attached to it


code:
from django.test import TestCase
from django.test.utils import override_settings

class LoginTestCase(TestCase):

    @override_settings(LOGIN_URL='/other/login/')
    def test_login(self):
        response = self.client.get('/sekrit/')
        self.assertRedirects(response, '/other/login/?next=/sekrit/')

rawDescription: See django.db.backends logs in stdout on pycharm


code:
foo = foobar2.clip_lower(0)
foo = foo['var1']+foo['var2']-foo['var3']-foo['var4']

rawDescription: Pandas: Adding conditionally

description: Pandas: Adding conditionally


code: [len(commonprefix((buffer(s, n), buffer(s, m)))) for n, m in zip(pos, pos[1:])]

rawDescription: Longest common prefix using buffer?

description: Longest common prefix using buffer


code:
df2 = DF_freqSpecies.replace(r'.*\((\d+\.*\d*)\).*', r'\1', regex=True)
df2.head()

rawDescription: Pandas DataFrame [cell=(label,value)], split into 2 separate dataframes


rawDescription: Speed up finding matches between two dictionaries (Python)


code:
def format_with_commas(x):
    s = format(x, ".0f")
    j = len(s) % 3
    if j:
         groups = [s[:j]]
    else:
         groups = []
    groups.extend(s[i:i + 3] for i in range(j, len(s), 3))
    return ",".join(groups)

rawDescription: Multiple format options in Python

description: Multiple format options in Python


code:
import json
from subprocess import Popen, PIPE

p = Popen(['./c-program', 'other', 'args'], stdin=PIPE, stdout=PIPE, bufsize=1)
p.stdin.write(json.dumps({'parameter': 8}).encode() + b'\n') # send input
p.stdin.flush()
result = json.loads(p.stdout.readline().decode()) # e.g., {"result": [0, 0, 7]}
# ...
p.communicate() # close pipes, wait for child to finish

rawDescription: How to Achieve Continuous Interactive Dialog with a Subprocess in Python?


rawDescription: python for loop

description: Python for loop


code:
import re
# Source: http://nedbatchelder.com/blog/200712/human_sorting.html
# Author: Ned Batchelder
def tryint(s):
    try:
        return int(s)
    except:
        return s

def alphanum_key(s):
    """ Turn a string into a list of string and number chunks.
        "z23a" -> ["z", 23, "a"]
    """
    return [ tryint(c) for c in re.split('([0-9]+)', s) ]

def sort_nicely(l):
    """ Sort the given list in the way that humans expect.
    """
    l.sort(key=alphanum_key)

data=[
    'title_text #49',
    'title_text #5',
    'title_text #50']
sort_nicely(data)
print(data)
# ['title_text #5', 'title_text #49', 'title_text #50']

rawDescription: Sorting dicts (contained in lists) alphanumerically in Python


rawDescription: How do I test dictionary-equality with Python's doctest-package?

description: Test dictionary-equality with Python's doctest-package


rawDescription: Deleting lines in external text files

description: Deleting lines in external text files


rawDescription: BYTE to python C-Type structure Conversion Issue


rawDescription: How to make Python format floats with certain amount of significant digits?

description: Make Python format floats with certain amount of significant digits


code:
def make_from(inputString):
    if inputString.endswith('y'):
        return inputString[:-1] + 'ies'
    if inputString.endswith(('o', 'ch', 's', 'sh', 'x', 'z')):
        return inputString[:] + 'es'
    return inputString + 's'

rawDescription: str.endswith() does not seem to consider the first if statment

description: Str.endswith() does not seem to consider the first if statment


code:
[u'Stoere HUMMER of Cadillic opbergtas (rood)',
 u'EHBO First Aid Rapid Response kit',
 u'LifeHammer met houder',
 u'Aluminium Midi Zaklamp met alarm inclusief 3x AAA batterij',
 u'Werkhandschoenen',
 u'IJskrabber',
 u'Afbreekmes',
 u'2x veiligheidshesje',
 u'Verbandschaar',
 u'Reddingsdeken',
 u'Verband + pleister 9 x rol verband diverse afmetingen Pleisters',
 u'Handschoenen',
 u'3 x steriele gaasjes']

rawDescription: Scrapy, receive just the text from an unordered list, including text between other html tags


code:
import struct

def readtoint(read):
    return struct.unpack('<i', read)[0]

rawDescription: Reading negative values from a file in python


rawDescription: Is there a Python library for handling complicated mathematical sets (constructed using mathematical set-builder notation)?


code:
{
  "parameters": [{
    "name": "BRANCH",
    "value": "foo",
  },
  {
    "name": "GITURL",
    "value": "git://example.com/repo",
  }]
}

rawDescription: Python request similar to my unix curl script


rawDescription: Boolean indexing of 2D ndarray based on row comparison


code:
handle = Entrez.efetch(db="protein",
                       id="NP_001012745",
                       rettype="fasta",
                       retmode="text")

rawDescription: Biopython Retrieving protein transcripts for a protein coding gene


rawDescription: Using python mocking library on sqlalchemy


code:
paths = ['path1', 'path2', 'path3']
sounds = { path: pygame.mixer.Sound(path) for path in paths }

# play a specific sound
sounds[name].play()

rawDescription: Variable Variables in Python

description: Variable Variables in Python


code:
class User(db.Model, ModelMixin):
      """ attributes with _  are not exposed with public_view """
      __tablename__ = "users"
      id = db.Column(db.Integer, primary_key=True)

rawDescription: Flask-SQLAlchemy Abstract Base Model

description: Flask-SQLAlchemy Abstract Base Model


code: from google.appengine.api import urlfetch, taskqueue, users, images

rawDescription: Making python imports more structured?

description: Making python imports more structured


code:
from pylab import *

# fake up some data
spread= rand(50) * 1
center = ones(25) * .5
flier_high = rand(10) * 100 + 100
flier_low = rand(10) * -100
data =concatenate((spread, center, flier_high, flier_low), 0)

# fake up some more data
spread= rand(50) * 1
center = ones(25) * .4
flier_high = rand(10) * 100 + 100
flier_low = rand(10) * -100
d2 = concatenate( (spread, center, flier_high, flier_low), 0 )
data.shape = (-1, 1)
d2.shape = (-1, 1)
data = [data, d2, d2[::2,0]]

# multiple box plots on one figure
fig = plt.figure(1, [5,4]) # Figure Size
ax = fig.add_subplot(111)  # Only 1 subplot
ax.boxplot(data)
ax.set_xlim(0.5,5)
ax.set_ylim(.1,300)
ax.set_yscale('log')

show()

rawDescription: matplotlib: inset axes for multiple boxplots

description: Matplotlib: inset axes for multiple boxplots


rawDescription: Selecting rows in a DataFrame that are not in a Series


code: ppend(n.rest)  # a new list is created but nothing is done with the result of the function

rawDescription: appending LinkedLists items to list using Recursion

description: Appending LinkedLists items to list using Recursion


code: test_perplexity = run_epoch(session, mtest, test_data, tf.no_op(), vocabulary)

rawDescription: getting word from id at Tensorflow RNN sample

description: Getting word from id at Tensorflow RNN sample


code:
#TCP server example
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 7000))
server_socket.listen(5)

print("TCPServer Waiting for client on port 7000")

while 1:
    client_socket, address = server_socket.accept()
    print("I got a connection from ", address)
    while 1:
        data = client_socket.recv(32)
        print("RECEIVED:",data)
        if (data == b'q' or data == b'Q'):
##          client_socket.shutdown(socket.SHUT_RDWR)
            client_socket.close()
            break;
    break;

rawDescription: Python 3: Server socket not closing properly, infinite loop of blank messages received


rawDescription: Length cutting through file handling

description: Length cutting through file handling


code:
from __future__ import print_function
import string
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import numpy as np

def char_to_pixels(text, path='arialbd.ttf', fontsize=14):
    """
    Based on http://stackoverflow.com/a/27753869/190597 (jsheperd)
    """
    font = ImageFont.truetype(path, fontsize)
    w, h = font.getsize(text)
    h *= 2
    image = Image.new('L', (w, h), 1)
    draw = ImageDraw.Draw(image)
    draw.text((0, 0), text, font=font)
    arr = np.asarray(image)
    arr = np.where(arr, 0, 1)
    arr = arr[(arr != 0).any(axis=1)]
    return arr

def display(arr):
    result = np.where(arr, '#', ' ')
    print('\n'.join([''.join(row) for row in result]))

for c in string.uppercase:
    arr = char_to_pixels(
        c,
        path='/usr/share/fonts/truetype/liberation/LiberationSerif-Bold.ttf',
        fontsize=9)
    print(arr.shape)
    display(arr)
    print()

rawDescription: Generate pixel matrices from characters in string


code: R = map(tuple, R)

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

description: Find the index of an array within an array


code:
Client
import xmlrpclib

class ServerProxy(object):
    def __init__(self, url):
        self._xmlrpc_server_proxy = xmlrpclib.ServerProxy(url)
    def __getattr__(self, name):
        call_proxy = getattr(self._xmlrpc_server_proxy, name)
        def _call(*args, **kwargs):
            return call_proxy(args, kwargs)
        return _call

#example usage
server = ServerProxy('http://localhost:8000')
server.test(1, 2)
server.test(arg2=2, arg1=1)
server.test(1, arg2=2)
server.test(*[1,2])
server.test(**{'arg1':1, 'arg2':2})

rawDescription: Using **kwargs with SimpleXMLRPCServer in python


code:
import time

from django import template

register = template.Library()

@register.filter
def epoch(value):
    try:
        return int(time.mktime(value.timetuple())*1000)
    except AttributeError:
        return ''

rawDescription: django Datefield to Unix timestamp

description: Django Datefield to Unix timestamp


code:
def methoddecoratormarker(func):
    func._decorate_me = True
    return func

def realmethoddecorator(func):
    # do something with func.
    # Note: it is still an unbound function here, not a method!
    return func

def classdecorator(klass):
    for name, item in klass.__dict__.iteritems():
        if getattr(item, '_decorate_me', False):
            klass.__dict__[name] = realmethoddecorator(item)

rawDescription: Is there a way apply a decorator to a Python method that needs informations about the class?


code:
s = unicode('©', 'utf-8').encode('utf-8')
s2 = u''.join(unichr(ord(c)) for c in s)

rawDescription: Convert Unicode string to UTF-8, and then to JSON


code:
import pickle
pickle.load(open( "save.p", "rb" ) )

rawDescription: NLTK PunktSequenceTokenizer return type or a way to use it faster in an iterative function?


rawDescription: how to slice a dataframe having date field as index?

description: Slice a dataframe having date field as index


code:
with open('/path/to/hostname_file.txt', 'r') as hosts:
    hostnames = [line.strip() for line in hosts.readlines()]

with open('/path/to/fw/config.txt', 'r') as fw_config:
    with open('/path/to/matches.txt', 'w+') as match:
        for line in fw_config:
            if len(hostnames) < 1:
                break
            if any(name in line for name in hostnames):
                match.write(line)
                hostnames.remove(name)

rawDescription: Comparing a list in a text file to a second large text file and printing the matches.


code:
def myfunc(*args):
    statement = "INSERT INTO table VALUES(" + ', '.join(str(x) for x in args) + ");"
    db.execute(statement)

rawDescription: How to efficiently unravel arguments to a database in Python

description: Efficiently unravel arguments to a database in Python


code:
if __name__ == '__main__':
    for name, thing in globals().iteritems():
        if issubclass(thing, AuthenticatedApiTestCase):
            obj = thing()
            obj.base_url = something_new

rawDescription: Python Metaprogramming. Generating massive amount of similar tests where a variable value is changed


code: actg = actg.reshape((-1, 3, 4))

rawDescription: Python: How to perform division according to its representation?


code:
# dUp= delta[delta > 0]
# dDown= delta[delta < 0]

# dUp = dUp.reindex_like(delta, fill_value=0)
# dDown = dDown.reindex_like(delta, fill_value=0)

dUp, dDown = delta.copy(), delta.copy()
dUp[dUp < 0] = 0
dDown[dDown > 0] = 0

RolUp = pd.rolling_mean(dUp, n)
RolDown = pd.rolling_mean(dDown, n).abs()

RS = RolUp / RolDown

rawDescription: Relative Strength Index in python pandas


code:
from scipy.spatial.distance import squareform
squareform(arr)

rawDescription: Using numpy's genfromtxt to load a triangular matrix with python


code: [m for m in msg if "one" not in m]

rawDescription: simple dropping elements from the list in python

description: Simple dropping elements from the list in python


code: MEDIA_ROOT = '/path/to/media'

rawDescription: How do I server a specific directory with possible sub directories in Django 1.7?

description: Server a specific directory with possible sub directories in Django 1.7


code:
from xml.sax import make_parser
from twisted.web.client import downloadPage

class StreamingXMLParser:
    def __init__(self):
        self._parser = make_parser()

    def write(self, bytes):
        self._parser.feed(bytes)

    def close(self):
        self._parser.feed('', True)

parser = StreamingXMLParser()
d = downloadPage(url, parser)
# d fires when the response is completely received

rawDescription: How do I fetch an XML document and parse it with Python twisted?

description: Fetch an XML document and parse it with Python twisted


code:
import sys
import os
import unittest
sys.path.append(os.path.join(os.path.dirname(__file__), 'App'))
import main

# do the tests

if __name__ == '__main__':
    unittest.run()

rawDescription: Python - organising code and test suite

description: Python - organising code and test suite


code:
def zip_values(l1, l2):
    if len(l1) > len(l2):
        left, right = l1, l2
    else:
        left, right = l2, l1
    ret = []

    # below, there's a nicer way to express this using enumerate(),
    # but I'm leaving all python magics for the purpose of the example:
    #     for position, value in enumerate(left):
    position = 0
    for value in left:
        if position < len(right):
            ret.append((left[position], right[position],))
        else:
            ret.append((left[position], None,))
        position += 1
    return ret

rawDescription: Getting specific values from different lists and how to


rawDescription: Build directory tree from dropbox API

description: Build directory tree from dropbox API


code: logger = logging.getLogger('MyScript')

rawDescription: Suspend the formating of the logger, then go back to it


rawDescription: IoT Python app with a Raspberry Pi and Bluemix: Pushing the button doesn't work


code:
#views.py
def some_view(request, id):
    if request.user.is_authenticated():
        # get the biddschedule from your DB
        # ...
        bids = models.Bids(id=id, owner=request.user, biddedschedule=biddedschedule)
        bids.save()
        return HttpResponse("OK")
    return Http404()

rawDescription: Create django object using a view with no form


code:
def changeLabel(*args): # require *args for trace
    #print "changeLabel", args

    custname_str = custname.get()
    custname_int = 0 # default value when error

    try:
        custname_int = int( custname_str )
    except:
        if custname_str != "": # if not empty
            custname.set(custname_str[:-1]) # remove last char
            return

    if 2 < custname_int <= 6:
        if relStatus.get() == 'HOSTS':
            label4Text.set("255.255.255.248")
        else: #if relStatus.get() == 'NETWORKS':
            label4Text.set("224.0.0.0")
    else:
        label4Text.set("-")

rawDescription: need assistance with Entry Tkinter in Python 2.5

description: Need assistance with Entry Tkinter in Python 2.5


rawDescription: Parse multi-line string up until first line with certain character


rawDescription: And and Or confusion

description: And and Or confusion


code:
def create_image(self, spinner, finishcb):
    spinner.start()

    def thread_run():
        # call heavy here
        heavy_ret = heavy()
        gobject.idle_add(cleanup, heavy_ret)

    def cleanup(heavy_ret):
        spinner.stop()
        t.join()
        finishcb(heavy_ret)

    # start "heavy" in a separate thread and immediately
    # return to mainloop
    t = threading.Thread(thread_run)
    t.start()

rawDescription: Using Gtk.spinner with a long time function


code:
start = soup.find('div', class_='main-content-column')
if start:
    news.image_url_list = []
    for item in start.children:
        if item.name != 'div':
            # skip children that are not <div> tags
            continue
        for para in item.children:
            if item.name != 'p':
                # skip children that are not <p> tags
                continue

rawDescription: BeautifulSoup: How to skip a child node within a find_all?


code:
e
e/e.sum(axis=1)[:,None]

rawDescription: numpy divide row by row sum

description: Numpy divide row by row sum


code:
hostname = domain
while '-' in domain:
    domain = domain.partition('.')[-1]
hostname = hostname[:-len(domain) - 1]

rawDescription: python split a string when a keyword comes after a pattern

description: Python split a string when a keyword comes after a pattern


code: list(text)

rawDescription: Removing letters from a string in Python


code:
from boto.s3.connection import S3Connection
from boto.s3.key import Key

conn = S3Connection('<aws access key>', '<aws secret key>')
bucket = conn.get_bucket('<bucket name>')
k = Key(bucket)
k.key = 'file_path_on_s3' # for example, 'images/bob/resized_image1.png'
k.set_contents_from_file(resized_photo)

rawDescription: Upload Images to Amazon S3 using Django

description: Upload Images to Amazon S3 using Django


rawDescription: Choose Pandas dataframe index after which data in a column is all higher than a specific value


code: pizza = contains_words("Do you like pizza?", ["yes", "no"])

rawDescription: Recursive function with same arguments

description: Recursive function with same arguments


code: a = sorted(f1, key=lambda l: int(l.split()[0][6:]))

rawDescription: Sorting names containing numbers

description: Sorting names containing numbers


code:
def parse_cities(fin):
    cities = {}
    distances = {}
    order = []
    index = 0
    valid_city = False
    for line in fin:
        if '[' in line:  # city line
            # .partition() will always return three strings
            citystate, left, rest = line.partition('[')
            coordinates, right, population = rest.partition(']')
            lat, comma, lng = coordinates.partition(',')

            # drop any surrounding whitespace
            citystate = citystate.strip()
            lat = lat.strip()
            lng = lng.strip()
            population = population.strip()

            # validity check
            valid_city = (
                # delimiters must exist
                left and right and comma and
                # numbers must be valid
                lat.isdigit() and lng.isdigit() and population.isdigit()
            )

            if not valid_city: continue

            order.append(citystate)
            cities[citystate] = (int(lat), int(lng), int(population))
            index = 0
        elif line and line[0].isdigit() and valid_city:  # distance line
            # citystate variable from previous iteration
            # index variable will be reset for each new city,
            #     but otherwise increase between the lines
            for dist in [int(w) for w in line.split()]:
                if index >= len(order): break
                citystate2 = order[index]
                distances[citystate,citystate2] = dist
                distances[citystate2,citystate] = dist
                index += 1
    return cities, distances

rawDescription: So How could I get the coordinates and population of the city (retun them to a list) by calling the city names that are already created?


code:
@app.route('/page_without_cache')
def page_without_cache():
   response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
   response.headers['Pragma'] = 'no-cache'
   return 'hello'

rawDescription: Disable cache on a specific page using Flask


code: list(ifilter(lambda t: t[:2] not in {(4,1), (3,1)}, permutations(my_list)))

rawDescription: Python - New list of permutations with omissions


code:
def plotSomething(x,y):
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.set_xscale("log", nonposx='clip')
    ax.set_yscale("log", nonposy='clip')
    ax.scatter(x,y)
    fig.savefig('test.png')

rawDescription: plotting with matplotlib from a module

description: Plotting with matplotlib from a module


code:
def main():
    app = QtGui.QApplication(sys.argv)
    win = window()
    win.display()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

rawDescription: PyQt4 jpeg/jpg unsupported image format

description: PyQt4 jpeg/jpg unsupported image format


code:
def add(a,b):
    return a + b

rawDescription: What does it mean to "call" a function in Python?


code:
def make_link(text,phrase,file_to_open):
    def callback(event=None):
        os.system("open %r"%file_to_open)#there are better ways of handling this
    apply_tag(text,phrase,phrase)#uses phrase as tag
    format_link(text,phrase,callback)

rawDescription: Converting a string automatically into a link to a pdf document


rawDescription: OpenCV: setting all pixels of specific BGR value to another BGR value


code:
from werkzeug.routing import Rule

app.url_rule_class = lambda path, **options: Rule(PREFIX + path, **options)

rawDescription: Add route prefix to Flask application running behing Gunicorn


code:
class Card(object):
    def __init__(self, rank, suite):
        self.rank = rank
        self.suite = suite

deck = [Card(r,s) for r in ranks for s in suite]
random.shuffle(deck)

rawDescription: How to make cards work in python

description: Make cards work in python


rawDescription: How to convert hex string "\x89PNG" to plain text in python

description: Convert hex string "\x89PNG" to plain text in python


rawDescription: How to get attributes in the order they are declared in a Python class?

description: Get attributes in the order they are declared in a Python class


code: df['date'] = df['col1'] + '.' +  df['col2'].apply(lambda x: str(x).zfill(2))

rawDescription: pandas - parsing column containing strings and column containing int into datetime

description: Pandas - parsing column containing strings and column containing int into datetime


code:
class MyClass:
    var = 1

    # we are executing this code as a single block
    # so you must reference the variable as is usual
    print(var)

    # default values are *not* inside the definition.
    # they are evaluated in the outer scope, so use plain "var" here
    def method(self, a_default=var):
        print(a_default)

    def other_method(self):

        # inside methods you are in a different scope
        print(MyClass.var)

        # equivalent *if* no "var" instance attributes exists
        print(self.var)

rawDescription: How to reference a static attribute from within a class in Python?

description: Reference a static attribute from within a class in Python


rawDescription: wx.lib.pubsub: How to change the value in a timer

description: Wx.lib.pubsub: How to change the value in a timer


code: lambda *outer: lambda *inner: tuple(a + b for a, b in zip(outer, inner))

rawDescription: How can I supply two arbitrary number of arguments to a lambda expression in python?

description: Supply two arbitrary number of arguments to a lambda expression in python


code:
body=''
for item in parsed:
    body = body + str( item['resource']['caseNumber'], \
        item['resource']['subject'], \
        item['resource']['status'], \
        item['resource']['severity'] + '\n' )

rawDescription: Encompass the output of a for loop into a variable in python


rawDescription: file.write() the length of file without knowing the length of file beforehand - python

description: File.write() the length of file without knowing the length of file beforehand - python


code:
...
context['unanswered_threads'] = ThreadVault.list_all_threads \
        .date_search_threads(date1, date2, field={'unanswered': True},
        column="latest_post_date").count()

rawDescription: Django 1.6 models.Manager Complaining about to many values to unpack


code:
def difference_deque(array, subset_length):
    assert subset_length > 1, "subset_length must be larger than 1"
    length = len(array)
    total_diff = max(array)-min(array)

    current_slice = collections.deque(array[:subset_length])
    current_min = min(current_slice)
    current_max = max(current_slice)
    max_diff = current_max - current_min
    max_diff_index = 0

    index = subset_length
    while index < length:
        i_new = index
        i_old = index-number
        index += 1
        new = array[i_new]
        old = current_slice.popleft()

        if new < current_min:
            current_min = new
            if old == current_max:
                current_max = max(current_slice)
            current_slice.append(new)
        elif new > current_max:
            current_max = new
            if old == current_min:
                current_min = min(current_slice)
            current_slice.append(new)
        elif old == current_min:
            current_slice.append(new)
            current_min = min(current_slice)
        elif old == current_max:
            current_slice.append(new)
            current_max = max(current_slice)
        else:
            current_slice.append(new)
            continue

        current_diff = current_max-current_min
        if current_diff > max_diff:
            max_diff = current_diff
            max_diff_index = i_old+1

        # shortcut-condition
        if max_diff == total_diff:
            print('shortcut at', (index-1)/(length-number), '%' )
            break

    return max_diff, max_diff_index

rawDescription: Efficient way to iterate over sublists of a huge list in python


code:
from foo_tasks import *

def update_hourly_weather():
    fn = '/weather/csv_data/hourly_weather.csv'
    run_update.delay(fn, "SELECT * from hourly_weather();")
    return True

update_hourly_weather()

rawDescription: Celery task PENDING

description: Celery task PENDING


code: "b" in vars(Foo).values()

rawDescription: Checking if a variable belongs to a class in python


rawDescription: Transpose Census Flat Files in Python

description: Transpose Census Flat Files in Python


code:
import subprocess

def foo():
    return subprocess.Popen(COMMAND.split(), stdout=subprocess.PIPE).communicate()[0]

rawDescription: Redirecting command output to a string - Python 3.4.3 - Not working on the new version


code: print('\n'.join([' '.join(map(str, x)) for x in a]))

rawDescription: (Single line) command to join list of lists, where the inner list are joined by spaces and the outer lists are joined by '\n'


code:
from collections import deque
_history_store = deque(maxlen=5)

def history(function, toAdd):
    global _history_store

    if function == 'add':
        _history_store.append(copy.deepcopy(toAdd))

    elif function == 'undo':
        if _history_store:
            return _history_store.pop()

        # No history to return.
        return None

    elif function == 'return':
        if _history_store:
            return copy.deepcopy(_history_store[-1])

        # No history to return.
        return None

rawDescription: Python lists not behaving as intended

description: Python lists not behaving as intended


code: some_function(**{col: var})

rawDescription: Can I use the value of a variable as a parameter name for a function?

description: Use the value of a variable as a parameter name for a function


code: roster.merge(t_data, on="Table")

rawDescription: Creating a gradebook with the pandas module


code:
# parse from file
et = etree.parse(source_xml)
value = et.xpath("your xpath xpr here")

rawDescription: Parsing XML from API response

description: Parsing XML from API response


rawDescription: Django development - Accessing results from a model


rawDescription: Merging dictionary pairs in a list of dictionaries


code:
import numpy as np

def mask_vectorized(inputs,outputs,pos_y):
    # Create a copy of outputs array for editing purposes
    pantilt_2d = outputs[:,:2].copy()

    # Get mask correspindig to IF conditional statements in original code
    mask_col0_lt = pantilt_2d[:,0]<-90
    mask_col0_gt = pantilt_2d[:,0]>90

    # Edit the first column as per the statements in original code
    pantilt_2d[:,0][mask_col0_gt] -= 180
    pantilt_2d[:,0][mask_col0_lt] += 180

    # Edit the second column as per the statements in original code
    pantilt_2d[ mask_col0_lt | mask_col0_gt,1] *= -1

    # Get vectorized tan_pan and tan_tilt
    tan_pan_tilt = np.tan(np.radians(pantilt_2d))

    # Vectorized calculation for: "tan_tilt * (target[1] .." from original code
    V = (tan_pan_tilt[:,1]*(inputs[:,1] - pos_y))/np.sqrt((tan_pan_tilt[:,0]**2)+1)

    # Setup output numpy array
    pos_array_vectorized = np.empty((num_samples,3))

    # Put in values into columns of output array
    pos_array_vectorized[:,0] = inputs[:,0] + tan_pan_tilt[:,0]*V
    pos_array_vectorized[:,1] = pos_y
    pos_array_vectorized[:,2] = inputs[:,2] + V

    # Convert to list, if so desired for the final output
    # (keeping as numpy array could boost up the performance further)
    return pos_array_vectorized.tolist()

rawDescription: Making this C array processing code more python (and even numpy)


code:
for row in reader:
    print(row)
    # do whatever with row

rawDescription: Why does the parsing of csv file not break things into their 'logical' order?


code:
mystr = 'cathy is a singer on fridays'
x = mystr.split()
x
x[3] = "dancer"
x
" ".join(x)

rawDescription: Should I be using regex in Python

description: Should I be using regex in Python


code:
a = Int(number=12345)
a.bits.odd, a.bits.half

rawDescription: python packet sniffer using raw socket

description: Python packet sniffer using raw socket


code:
class PizzaForm(forms.ModelForm):
    class Meta:
        model = Pizza

    # Representing the many to many related field in Pizza
    toppings = forms.ModelMultipleChoiceField(queryset=Topping.objects.all())

    # Overriding __init__ here allows us to provide initial
    # data for 'toppings' field
    def __init__(self, *args, **kwargs):
        # Only in case we build the form from an instance
        # (otherwise, 'toppings' list should be empty)
        if kwargs.get('instance'):
            # We get the 'initial' keyword argument or initialize it
            # as a dict if it didn't exist.
            initial = kwargs.setdefault('initial', {})
            # The widget for a ModelMultipleChoiceField expects
            # a list of primary key for the selected data.
            initial['toppings'] = [t.pk for t in kwargs['instance'].topping_set.all()]

        forms.ModelForm.__init__(self, *args, **kwargs)

    # Overriding save allows us to process the value of 'toppings' field
    def save(self, commit=True):
        # Get the unsave Pizza instance
        instance = forms.ModelForm.save(self, False)

        # Prepare a 'save_m2m' method for the form,
        old_save_m2m = self.save_m2m
        def save_m2m():
           old_save_m2m()
           # This is where we actually link the pizza with toppings
           instance.topping_set.clear()
           for topping in self.cleaned_data['toppings']:
               instance.topping_set.add(topping)
        self.save_m2m = save_m2m

        # Do we need to save all changes now?
        if commit:
            instance.save()
            self.save_m2m()

        return instance

rawDescription: Django ModelForm for Many-to-Many fields


code: zipped = None

rawDescription: python how do I empty or clear a zip()

description: Python how do I empty or clear a zip()


code:
import random
import bisect

def obsfucate_function(default):
    cumulative_weights = [16, 30, 45, 60, 75, 90, 100]
    values = ['!', '%', '&', '*', '#', '@', default]
    return values[bisect.bisect(cumulative_weights, random.randrange(100))]

rawDescription: Return original value, python

description: Return original value, python


code:
a
b
c
d
e

rawDescription: Save for loop result in variable

description: Save for loop result in variable


rawDescription: Sorting numpy array with np.lexsort containing float values


code: ''.join(c for c in s if c in char).translate(trans)

rawDescription: python replace multiple characters in a string

description: Python replace multiple characters in a string


code:
file1 = '''Cluster 0:
 giant(2)
  red(2)
   brick(1)
   apple(1)
Cluster 1:
 tiny(3)
  green(1)
   dot(1)
  blue(2)
   flower(1)
   candy(1)'''.split('\n')
file2 = '''Cluster 18:
 giant(2)
  red(2)
   brick(1)
   tomato(1)
Cluster 19:
 tiny(2)
  blue(2)
   flower(1)
   candy(1)'''.split('\n')

rawDescription: How to compare clusters?

description: Compare clusters


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

fig, ax = plt.subplots()
im = ax.imshow(np.random.random((10, 10)), cmap='gist_earth')
cbar = fig.colorbar(im)
cbar.set_label('Test', y=0.25)
plt.show()

rawDescription: How to move a colorbar label downward?

description: Move a colorbar label downward


code:
ts = meat.set_index(['date'])
ts.beef.max()
ts.beef.idxmax()
ts.ix[ts.beef.idxmax()]

rawDescription: finding a record with a max value in python

description: Finding a record with a max value in python


code:
myCombinedList = []
[myCombinedList.extend(inner) for inner in mylistOfLists]

rawDescription: Python: Combining a list of lists

description: Python: Combining a list of lists


code:
from matplotlib import pyplot as plt

fig, ax = plt.subplots(2, 2)

ax[0,0].imshow(img, cmap=plt.cm.gray)
ax[0,0].set_title('original')
ax[0,1].imshow(mask, cmap=plt.cm.gray)
ax[0,1].set_title('mask')
ax[1,0].imshow(masked, cmap=plt.cm.gray)
ax[1,0].set_title('masked original')
ax[1,1].imshow(trimmed, cmap=plt.cm.gray)
ax[1,1].set_title('trimmed original')

plt.show()

rawDescription: Crop out partial image using NumPy (or SciPy)


rawDescription: How to write small DSL parser with operator module in python

description: Write small DSL parser with operator module in python


code:
G = nx.DiGraph()
G.add_path([1,2,3,4])
G.add_path([3,'a','b'])
sub_G = nx.DiGraph()
create_subgraph(G, sub_G,3)

rawDescription: NetworkX DiGraph create subgraph (DiGraph) by node


code:
fields = ['a', 'b', '', 'd']
[index for index, value in enumerate(fields) if not value]

rawDescription: Doing validation across a large number of items using all()


rawDescription: Converting Tuple to Dictionary

description: Converting Tuple to Dictionary


rawDescription: How do you perform basic joins of two RDD tables in Spark using Python?

description: Perform basic joins of two RDD tables in Spark using Python


code:
for x,y,z in employees:
    employeeExp[(x, y)] += z

rawDescription: Using python groupby or defaultdict effectively?


code: domain = a.split('@')[1]

rawDescription: Python: Using split on a string and returning a tuple?


rawDescription: Stripping \r\n from a line

description: Stripping \r\n from a line


code:
nominalList = [1,2,3,4,5]
randomList = []

for n in xrange(0, len(nominalList)):
    randomList.append(random.normalvariate(0, SD))

    while nominalList[n] + randomList[n] < 0:
        randomList[n] = random.normalvariate(0, SD)

rawDescription: Replacing list values in a while loop

description: Replacing list values in a while loop


code: func(*r)

rawDescription: How can I apply a namedtuple onto a function?

description: Apply a namedtuple onto a function


rawDescription: Python finding repeating sequence in list of integers?


code:
document = collection.find_one({'_id': 111})
collection.replace_one({'_id': document['_id']}, dict(zip(fields, [document[field] for field in fields])))

rawDescription: How to unset all fields except some known fields?

description: Unset all fields except some known fields


code:
rects = [pygame.Rect(0,0,100,100), pygame.Rect(30,30,30,30)]
next((r for r in rects if r.collidepoint(10, 10)), None)
next((r for r in rects if r.collidepoint(200, 200)), None)

rawDescription: Looking for a concise way to check for point collision in a list of Rects


code:
chapter = (
    session.query(Chapter)
    .join(Book)
    .filter(Book.name == "One Thousand and One Nights")
    .filter(Chapter.name == "57th Arabian Tale")
    .one()
)

rawDescription: How do you query a one-to-many relationship in an SQLAlchemy object instance?

description: Query a one-to-many relationship in an SQLAlchemy object instance


rawDescription: For Base64 encode, prefer str.encode('base64_codec') or base64.b64encode(str)?


rawDescription: Regex - select string until ' / '

description: Regex - select string until ' / '


code:
import os
from django.conf import settings
from django.db.models import loading
from django.core import management

APPS_DIR = '/path_to/apps/'

for item in os.listdir(APPS_DIR):
    if os.path.isdir(os.path.join(APPS_DIR, item)):
        app_name = 'apps.%s' % item
    if app_name not in settings.INSTALLED_APPS:
        settings.INSTALLED_APPS += (app_name, )

rawDescription: dynamically loading django apps at runtime

description: Dynamically loading django apps at runtime


code:
class myClass:
    #------------------------------------------------------------------------
    @staticmethod
    def myFunction(param1, param2):
        """
        Some work here using param1 and param2
        """
    @staticmethod
    def myFunction2(param1, param2):
        """
        Some work here using param1 and param2
        """
    @staticmethod
    def myFunction3(param1, param2):
        """
        Some work here using param1 and param2
        """

myDict = {}
myDict["key1"] = myClass.myFunction
myDict["key2"] = myClass.myFunction2
myDict["key3"] = myClass.myFunction3
# and so on...

rawDescription: Preventing a class instance from being evaluated until it's parameters have been set


code:
x = gen_log_space(10000, 100)
x.size
len(set(x))
y = gen_log_space(2000, 50)
y.size
len(set(y))
y

rawDescription: logarithmically spaced integers

description: Logarithmically spaced integers


code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (C) 2013  Diego Torres Milano
Created on 2014-04-16 by Culebra v5.4.4

                      __    __    __    __
                     /  \  /  \  /  \  /  \
____________________/  __\/  __\/  __\/  __\_____________________________
___________________/  /__/  /__/  /__/  /________________________________
                   | / \   / \   / \   / \   \___
                   |/   \_/   \_/   \_/   \    o \
                                           \_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
'''

import re
import sys
import os

from com.dtmilano.android.viewclient import ViewClient

kwargs1 = {'verbose': True, 'ignoresecuredevice': False}
device, serialno = ViewClient.connectToDeviceOrExit(**kwargs1)
device.startActivity(component='com.google.android.email/com.android.email2.ui.MailActivityEmail')
kwargs2 = {'startviewserver': True, 'forceviewserveruse': False, 'autodump': False, 'ignoreuiautomatorkilled': True}
vc = ViewClient(device, serialno, **kwargs2)
vc.dump(window='-1')

# class=android.widget.FrameLayout
no_id1 = vc.findViewByIdOrRaise("id/no_id/1")

# class=android.view.View
android___id_action_bar_overlay_layout = vc.findViewByIdOrRaise("android:id/action_bar_overlay_layout")

# class=android.widget.FrameLayout
android___id_action_bar_container = vc.findViewByIdOrRaise("android:id/action_bar_container")

# class=android.view.View
android___id_action_bar = vc.findViewByIdOrRaise("android:id/action_bar")

# class=android.widget.LinearLayout
no_id5 = vc.findViewByIdOrRaise("id/no_id/5")

# class=android.widget.FrameLayout
no_id6 = vc.findViewByIdOrRaise("id/no_id/6")

# class=android.widget.ImageView
android___id_up = vc.findViewByIdOrRaise("android:id/up")

# class=android.widget.ImageView
android___id_home = vc.findViewByIdOrRaise("android:id/home")

# class=android.widget.LinearLayout
no_id9 = vc.findViewByIdOrRaise("id/no_id/9")

# class=android.widget.TextView text=u'Inbox'
android___id_action_bar_title = vc.findViewByIdOrRaise("android:id/action_bar_title")

# class=android.widget.TextView text=u'999+ unread'
android___id_action_bar_subtitle = vc.findViewByIdOrRaise("android:id/action_bar_subtitle")

# class=android.widget.LinearLayout
no_id12 = vc.findViewByIdOrRaise("id/no_id/12")

# class=android.widget.TextView
com_android_email___id_compose = vc.findViewByIdOrRaise("com.android.email:id/compose")

# class=android.widget.TextView
com_android_email___id_search = vc.findViewByIdOrRaise("com.android.email:id/search")

# class=android.widget.ImageButton
no_id15 = vc.findViewByIdOrRaise("id/no_id/15")

# class=android.widget.FrameLayout
android___id_content = vc.findViewByIdOrRaise("android:id/content")

# class=android.view.View
no_id17 = vc.findViewByIdOrRaise("id/no_id/17")

# class=android.widget.FrameLayout
no_id18 = vc.findViewByIdOrRaise("id/no_id/18")

# class=android.widget.FrameLayout
com_android_email___id_content_pane = vc.findViewByIdOrRaise("com.android.email:id/content_pane")

# class=android.widget.RelativeLayout
com_android_email___id_between_chrome = vc.findViewByIdOrRaise("com.android.email:id/between_chrome")

# class=android.widget.FrameLayout
com_android_email___id_conversation_list = vc.findViewByIdOrRaise("com.android.email:id/conversation_list")

# class=android.widget.ListView
android___id_list = vc.findViewByIdOrRaise("android:id/list")

# class=android.widget.LinearLayout
no_id23 = vc.findViewByIdOrRaise("id/no_id/23")

# class=android.widget.LinearLayout
no_id24 = vc.findViewByIdOrRaise("id/no_id/24")

# class=android.widget.LinearLayout
com_android_email___id_nested_folder_container = vc.findViewByIdOrRaise("com.android.email:id/nested_folder_container")

# class=android.widget.RelativeLayout
no_id26 = vc.findViewByIdOrRaise("id/no_id/26")

# class=android.widget.ImageView
com_android_email___id_folder_imageView = vc.findViewByIdOrRaise("com.android.email:id/folder_imageView")

# class=android.widget.LinearLayout
com_android_email___id_text_layout = vc.findViewByIdOrRaise("com.android.email:id/text_layout")

# class=android.widget.TextView text=u'0'
com_android_email___id_count_textView = vc.findViewByIdOrRaise("com.android.email:id/count_textView")

# class=android.widget.LinearLayout
com_android_email___id_show_more_folders_row = vc.findViewByIdOrRaise("com.android.email:id/show_more_folders_row")

# class=android.widget.TextView text=u'Show 1 more folders'
com_android_email___id_show_more_folders_textView = vc.findViewByIdOrRaise("com.android.email:id/show_more_folders_textView")

# class=android.widget.TextView text=u'0'
com_android_email___id_show_more_folders_count_textView = vc.findViewByIdOrRaise("com.android.email:id/show_more_folders_count_textView")

# class=android.widget.FrameLayout
no_id34 = vc.findViewByIdOrRaise("id/no_id/34")

# class=android.view.View
no_id35 = vc.findViewByIdOrRaise("id/no_id/35")

# class=android.widget.FrameLayout
no_id36 = vc.findViewByIdOrRaise("id/no_id/36")

# class=android.view.View
no_id37 = vc.findViewByIdOrRaise("id/no_id/37")

# class=android.widget.FrameLayout
no_id38 = vc.findViewByIdOrRaise("id/no_id/38")

# class=android.view.View
no_id39 = vc.findViewByIdOrRaise("id/no_id/39")

# class=android.widget.FrameLayout
no_id40 = vc.findViewByIdOrRaise("id/no_id/40")

# class=android.view.View
no_id41 = vc.findViewByIdOrRaise("id/no_id/41")

# class=android.widget.FrameLayout
no_id42 = vc.findViewByIdOrRaise("id/no_id/42")

# class=android.view.View
no_id43 = vc.findViewByIdOrRaise("id/no_id/43")

# class=android.widget.FrameLayout
no_id44 = vc.findViewByIdOrRaise("id/no_id/44")

# class=android.view.View
no_id45 = vc.findViewByIdOrRaise("id/no_id/45")

rawDescription: AVC unable to find view by id even if parameter 'forceviewserveruse': is True,


code:
if __name__ == "__main__":
    suite = eval(sys.argv[1])  # Be careful with this line!
    unittest.TextTestRunner().run(suite)

rawDescription: Python unittest: how to run only part of a test file?


code:
{
  "help": "http://data.gov.uk/api/3/action/help_show?name=package_show",
  "success": true,
  "result":
  {
    "name": "uk-open-access-non-vosa-sites",
    "license_title": "UK Open Government Licence (OGL)",
    "resources": [
        {
            "description": "A list of Authorised Testing Facilities (ATFs) and DVSA test stations ",
            "format": "CSV",
            "url": "http://dft.gov.uk/vosa/images/Find%20your%20nearest%20ATF%20or%20VOSA%20test%20station.csv",
            "id": "bd9aaa9f-2a9c-45f8-8246-6b99dd78208a",
            "resource_type": "file",
        }
    ]
  }
}

rawDescription: CKAN Filedownload with API

description: CKAN Filedownload with API


code: dict([(an_object.name, an_object) for an_object in object_list])

rawDescription: Best way to turn a list into a dict, where the keys are a value of each object?

description: Turn a list into a dict, where the keys are a value of each object


code:
def with_slave(fn):
    def go(*arg, **kw):
        s = Session()
        oldbind = s.bind
        s.bind = slave
        try:
            return fn(*arg, **kw)
        finally:
            s.bind = oldbind
    return go

rawDescription: read slave , read-write master setup

description: Read slave , read-write master setup


code:
s = 'First sentenceSecond sentence'
soup = BeautifulSoup(s)
soup.find_all("a", {"class": "x3-large"})

rawDescription: Want to get part of string using regular expression


code:
def repeat(times, f, *args):
    for _ in range(times):
        f(*args)

rawDescription: Python, how to pass an argument to a function pointer parameter?


code: 'My name is {surname}, {name} {surname}. I am {age}.'.format(**locals())

rawDescription: String formatting options: pros and cons


rawDescription: Python Float to Ord values

description: Python Float to Ord values


code:
ips = ['{}.{}.{}.{}'.format(*components)
       for components in itertools.product(*ranges)]

rawDescription: Python parse ip regex

description: Python parse ip regex


code:
# 'filename' is the full path name for a data file whose size
# exceeds the memory on the box it resides. #

import tokenize

data_reader = open(some_filename, 'r')
tokens = tokenize.generate_tokens(reader)
tokens.next()           # returns a single line from the large data file.

rawDescription: General techniques to work with huge amounts of data on a non-super computer


code:
from itertools import zip_longest

def informative_lines(filename):
    with open(filename) as infile:
        for line in infile:
            if not any(patt.match(line) for patt in patterns):
                yield line

paired_lines = zip_longest(informative_lines("foo.log"),
                           informative_lines("bar.log"))

for i, (line0, line1) in enumerate(paired_lines):
    if line0 != line1:
        print('mismatch at non-ignored line #', i)
        print(line0)
        print(line1)
        raise Exception("problem!")

print("hooray, files matched!")

rawDescription: Algorithm to compare two files in Python


code: data.columns = map(str.lower, data.columns)

rawDescription: python: make pandas dataframe column headers all lowercase

description: Python: make pandas dataframe column headers all lowercase


rawDescription: Python logging, supressing output

description: Python logging, supressing output


code:
for j in range(20):
    temp=[]
    for i in range(10):
            temp.append(0)
    array1.append(temp)
array1

rawDescription: Creating arrays with for and while loops - Python 2


rawDescription: How to assign one value to the entire (or partial) list in Python?

description: Assign one value to the entire (or partial) list in Python


code:
import numpy as np
phi = np.array([1, 2, 3, 4])
radius = 6
2 * np.pi * radius * np.cos(phi)

rawDescription: Aesthetic way of appending to a list in Python?


code:
# app2.py
from app1 import ClassX

__all__ = ['Class1', 'Class2']

class Class1:
    pass

class Class2:
    pass

rawDescription: Python, could I import * without Imported classes


code:
#!/usr/bin/env python
import itertools
import time
from flask import Flask, Response, redirect, request, url_for

app = Flask(__name__)

@app.route('/')
def index():
    if request.headers.get('accept') == 'text/event-stream':
        def events():
            for i, c in enumerate(itertools.cycle('\|/-')):
                yield "data: %s %d\n\n" % (c, i)
                time.sleep(.1)  # an artificial delay
        return Response(events(), content_type='text/event-stream')
    return redirect(url_for('static', filename='index.html'))

if __name__ == "__main__":
    app.run(host='localhost', port=23423)

rawDescription: Streaming data with Python and Flask

description: Streaming data with Python and Flask


code: item_dict['BBB'] # 10 (already converted to an int)

rawDescription: Find an entry in a list based on a partial string


code:
import numpy as np

N = 3
M = 3
a = np.arange((M+1)**N).reshape([M+1]*N)
s = [np.ones(M).astype(int) for i in range(N)]
s[0] = [0,1,2]
s[1] = [1,2,3]
s[2] = [1,2,3]
b = a[np.ix_(*s)]
print(b)

rawDescription: Passing a list to numpy.ix_, or how to slice a multidimensional array, when number of dimensions and selection criteria are not known beforehand?


code:
OLD: 'OR 125'
NEW: 'Orange       125'

OLD: 'BA  48'
NEW: 'Banana       48'

OLD: 'Pr 12'
NEW: 'Pear         12'

OLD: 'ba 4'
NEW: 'Banana       4'

OLD: 'Ba   10'
NEW: 'Banana       10'

OLD: 'Or 7'
NEW: 'Orange       7'

OLD: 'OR 6'
NEW: 'Orange       6'

OLD: 'AP 9'
NEW: 'Apple        9'

OLD: 'PR          3'
NEW: 'Pear         3'

OLD: 'or 22'
NEW: 'Orange       22'

OLD: 'pR 13'
NEW: 'Pear         13'

rawDescription: Capturing the replaced text using re.sub in python


code: print(u)

rawDescription: What does '*** Oldest frame' mean in ipdb?


code:
['Attendance w1-1 (Responses) - Form Responses 1.csv',
 'Attendance w1-2 (Responses) - Form Responses 1.csv',
 'Attendance w2-1 (Responses) - Form Responses 1.csv',
 'Attendance w2-2 (Responses) - Form Responses 1.csv',
 'Attendance w3-1 (Responses) - Form Responses 1.csv',
 'Attendance w3-2 (Responses) - Form Responses 1.csv',
 'Attendance w4-1 (Responses) - Form Responses 1.csv',
 'Attendance w4-2 (Responses) - Form Responses 1.csv',
 'Attendance w5-1 (Responses) - Form Responses 1.csv',
 'Attendance w5-2 (Responses) - Form Responses 1.csv',
 'Attendance w6-1 (Responses) - Form Responses 1.csv',
 'Attendance w6-2 (Responses) - Form Responses 1.csv',
 'Attendance w7-1 (Responses) - Form Responses 1.csv',
 'Attendance w7-2 (Responses) - Form Responses 1.csv',
 'Attendance w8-2 (Responses) - Form Responses 1.csv',
 'Attendance w9-2 (Responses) - Form Responses 1.csv',
 'Attendance w11-1 (Responses) - Form Responses 1.csv',
 'Attendance w12-1 (Responses) - Form responses 1.csv',
 'Attendance w12-2 (Responses) - Form responses 1.csv',
 'Attendance w13-2 (Responses) - Form responses 1.csv',
 'Attendance w14-1 (Responses) - Form responses 1.csv',
 'Attendance w14-2 (Responses) - Form responses 1.csv',
 'Attendance w15-1 (Responses) - Form responses 1.csv',
 'Attendance w15-2 (Responses) - Form responses 1.csv']

rawDescription: How to sort this list? python

description: Sort this list? python


rawDescription: Python Hexadecimal

description: Python Hexadecimal


code:
def getRequiredArgs(func):
    args, varargs, varkw, defaults = inspect.getargspec(func)
    if defaults:
        args = args[:-len(defaults)]
    return args   # *args and **kwargs are not required, so ignore them.

rawDescription: Can you list the keyword arguments a Python function receives?

description: List the keyword arguments a Python function receives