rawDescription: Regex for parsing version number

description: Regex for parsing version number


code:
import pandas as pd

a = pd.read_csv("process.csv")
b = pd.read_csv("files.csv")
b = b.dropna(axis=1)
merged = a.merge(b, on='time')
merged.to_csv("combine2.csv", index=False)

rawDescription: Python: Append column to CSV from a different csv file


code: for i in progress.dots(range(your_start_value)): pass

rawDescription: Using the clint progress bar to show the status of urllib.urlretrieve()


code:
import argparse
usage = '\r{}\nusage: %(prog)s etc.'.format('Version a b'.ljust(len('usage:')))
parser = argparse.ArgumentParser(usage=usage)
parser.parse_args(['-h'])

rawDescription: python argparse print usage text after description

description: Python argparse print usage text after description


rawDescription: Find Closest Vector from a List of Vectors | Python


code:
@patch('mylib.requests.post')
class MyTest(TestCase):

    def test_foo(self, post_mock):
        post_mock.side_effect = mocked_post

        # Some test logic

        self.assertEqual(post_mock.call_count, 3)

rawDescription: How to get the call count using Mock @patch?

description: Get the call count using Mock @patch


code:
a = """
XXXXXXXXXXXXXXXIXXX
X                 X
X XXXXXXXXXXXXXXXXX
X             X   X
X XXX XXXXX X X X X
X   X X   X X X X X
X X X XXX X X X X X
X X X     X X   X X
XXXXXXXXXXXOXXXXXXX
"""
[[x for x in line] for line in a.split('\n')]

rawDescription: How can I transform a string into a matrix

description: Transform a string into a matrix


code:
import pandas as pd

def twonumbers(x):
    return [2*x, 3*x]

df = pd.DataFrame([1,4,11], columns=['v1'])
df['v2'], df['v3'] = twonumbers(df['v1'])

rawDescription: Define two columns with one map in Pandas DataFrame


code:
import asyncio

@asyncio.coroutine
def ok():
    yield from asyncio.sleep(1)
    return 5

@asyncio.coroutine
def select1(*futures, loop=None):
    if loop is None:
        loop = asyncio.get_event_loop()
    return (yield from next(asyncio.as_completed(futures)))

@asyncio.coroutine
def select2(*futures, loop=None):
    if loop is None:
        loop = asyncio.get_event_loop()
    done, running = yield from asyncio.wait(futures,
                                            return_when=asyncio.FIRST_COMPLETED)
    result = done.pop()
    return result.result()

@asyncio.coroutine
def example():
    queue = asyncio.Queue()
    result = yield from select1(ok(), queue.get())
    print('got {}'.format(result))
    result = yield from select2(queue.get(), ok())
    print('got {}'.format(result))

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(example())

rawDescription: Select first result from two coroutines in asyncio


code:
test_job_id = uuid.uuid4()
test_job_name = 'TestJobTest'
test_job_proj = 'TestProjectTest'
test_job_def_tmpl = """<joblist>
  <job>
    <id>{0}</id>
    <loglevel>INFO</loglevel>
    <sequence keepgoing='false' strategy='node-first'>
      <command>
        <node-step-plugin type='localexec'>
          <configuration>
            <entry key='command' value='echo "Hello World! (from:${{option.from}})"' />
          </configuration>
        </node-step-plugin>
      </command>
      <command>
        <node-step-plugin type='localexec'>
          <configuration>
            <entry key='command' value='sleep ${{option.sleep}}' />
          </configuration>
        </node-step-plugin>
      </command>
    </sequence>
    <description></description>
    <name>{1}</name>
    <context>
      <project>{2}</project>
      <options>
        <option name='from' value='Tester' />
        <option name='sleep' value='0' />
      </options>
    </context>
    <uuid>{0}</uuid>
  </job>
</joblist>"""
test_job_def = test_job_def_tmpl.format(test_job_id, test_job_name, test_job_proj)

rawDescription: Create Job using Rundeckrun?

description: Create Job using Rundeckrun


code: keys = sorted(keys)

rawDescription: Selecting items matching a specific condition from a list


code:
inst_dict = {}
for file in [('butter.txt', 'peanut'), ('tape.txt', 'duck'), ('style.txt', 'hair'), ('spaghetti.txt', 'italy'), ('cheese.txt','style')]:
    inst_dict[file[1]] = codecs.open(file[0], mode='w')

rawDescription: How to add many codecs.open to ' for ' loop

description: Add many codecs.open to ' for ' loop


code: color="k"

rawDescription: Python: matplotlib - Black and White Scatter


code:
lst = [{'fruit': 'apple', 'qty':'4', 'color': 'green'},{'fruit': 'orange', 'qty':'6', 'color': 'orange'},{'fruit': 'melon', 'qty':'2', 'color': 'yellow'}]
next((fruit['fruit'] for fruit in lst if fruit['color'] == 'yellow'), None)
next((fruit['fruit'] for fruit in lst if fruit['color'] == 'maroon'), None) is None
next(fruit['fruit'] for fruit in lst if fruit['color'] == 'maroon')

rawDescription: How to get a value from a dict in a list of dicts

description: Get a value from a dict in a list of dicts


code:
import urllib2
import timeit
urllib2_source = open(urllib2.__file__.rstrip('c')).read()
timeit.timeit("compile(source, '', 'exec')", 'from __main__ import urllib2_source as source', number=1000)
_ / 1000.0

rawDescription: Why are .pyc files created on import?

description: Why are .pyc files created on import


code:
WM_SHELLHOOKMESSAGE = None
def wndproc(hWnd, uMsg, lParam, wParam):
    if WM_SHELLHOOKMESSAGE is None:
        WM_SHELLHOOKMESSAGE = user32.RegisterWindowMessage('SHELLHOOK')
    if uMsg == WM_SHELLHOOKMESSAGE and wParam == HSHELL_FLASH:
        got_flashing_window_with_hwnd(lParam)

rawDescription: Python. Get flashing/blinking windows of a chat


code:
def getDose(voxInd):
    return voxMapBeamlet.getrow(voxInd).sum()

rawDescription: Accessing a large number of unsorted array elements in Python


rawDescription: Only print when run as script?

description: Only print when run as script


code:
class Student(models.Model):
    cell_phone = models.CharField(max_length=14)

    class Meta:
        abstract = True

class EmployeeStudent(User, Student):
    user = models.OneToOneField(User)
    extension = models.CharField(max_length=4, null=True, blank=True, default=None)

class CustomerStudent(User, Student):
    first_name = models.CharField(max_length=25)
    last_name = models.CharField(max_length=25)
    email = models.CharField(max_length=25)

rawDescription: Using two database tables for one control in Django Admin


code: newfunc = type(h)(h.__code__, cleaned_globals, h.__name__, h.__defaults__, h.__closure__)

rawDescription: Change what dictionary serves as a function's global scope


code: neighbours_of_B = graph['B']

rawDescription: Finding reachable vertices of a vertice in a graph


code:
id = []
data = []
[(id.append(x[0]), data.append(x[2])) for x in fetched]

rawDescription: Get component from Array sqlite

description: Get component from Array sqlite


rawDescription: Dynamically instantiate object of the python class similar to PHP new $classname?


rawDescription: Recursive dictionary modification in python


rawDescription: I need to linearly interpolate a numpy array of dimension nx3x3


code: x.write(folder + '/' + item + '/' + f, arcname = f)

rawDescription: Python: Getting files into an archive without the directory?


rawDescription: How can I create functions that handle polynomials?

description: Create functions that handle polynomials


code: [Row(ts=u'03052015 04:13:20', recordType=u'NEW', keycol=u'val1', col1=u'val5', data=u'val3'), Row(ts=u'03062015 04:13:20', recordType=u'NEW1', keycol=u'val2', col1=u'val6', data=u'val3'), Row(ts=u'03072015 04:13:20', recordType=u'NEW2', keycol=u'val3', col1=u'val7', data=u'val3'), Row(ts=u'03082015 04:13:20', recordType=u'NEW3', keycol=u'val4', col1=u'val8', data=u'val3')]

rawDescription: How to map structured data to schemaRDD in Spark?

description: Map structured data to schemaRDD in Spark


code:
# Pivot the DataFrame.
pivoted_df = df.pivot(columns='col1', index='image_group', values='col2')

# Clear the index and column name.
pivoted_df.index.name = None
pivoted_df.columns.name = None

rawDescription: Pivot groups of row data into columns using Pandas


rawDescription: How to filter objects in Django that do not have any other objects pointing to them?

description: Filter objects in Django that do not have any other objects pointing to them


rawDescription: Python Regex match every other word

description: Python Regex match every other word


code:
def gimme_my_form(field_tuple):
    class MyForm(ModelForm):
        class Meta:
            model = MyModel
            fields = field_tuple
    return MyForm

rawDescription: How to create Django FormWizard for one Model?

description: Create Django FormWizard for one Model


code:
def convert(n, difficulties=[0, 46, 91, 136, 201]):
    if n < difficulties[0]:
        raise ValueError
    for difficulty, end in enumerate(difficulties):
        if n < end:
            return difficulty
    else:
        return len(difficulties)

rawDescription: Mapping a range of integers to a single integer


rawDescription: Count the number of possible paths through a grid


rawDescription: Python instance of class defined inside a method is still able to use local variables inside the method when called outside the method


code:
tokens = re.search(r"url:\s'(.*?)',\suid:\s'(.*?)',\stype:\s'(.*?)'", value)
url, uid, type = tokens.groups()
url
uid
type

rawDescription: Write a Regular Expression in Python to match substring


code: deactivate

rawDescription: Virtualenv for different version of django


code:
for num_list in col_one:
    holder = list(number)
    for num in num_list:
        holder.remove(num)
    col_two.append(holder)

rawDescription: Randomizing an array into two arrays

description: Randomizing an array into two arrays


code:
def parse_number(text):
    match = re.match(_pattern, text)
    if match is None or not (match.group("integer_part") or
                             match.group("decimal_part")):    # failed to match
        return None                      # consider raising an exception instead

    num_str = match.group("number")      # get all of the number, without the junk
    sep = match.group("sep")
    if sep:
        num_str = num_str.replace(sep, "")     # remove thousands separators

    if match.group("decimal_part"):
        point = match.group("point")
        if point != ".":
            num_str = num_str.replace(point, ".")  # regularize the decimal point
        return float(num_str)

    return int(num_str)

rawDescription: Fuzzy smart number parsing in Python

description: Fuzzy smart number parsing in Python


code:
(Post
 .select(Post, fn.COUNT(Tag.id))
 .join(Tag, JOIN.LEFT_OUTER)
 .where(Tag.name == 'BadTag')
 .group_by(Post)
 .having(fn.COUNT(Tag.id) == 0))

rawDescription: Peewee SQL query join where none of many match


code:
from itertools import chain
from operator import methodcaller

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

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


code:
# M2Crypto
cert = ssl.get_server_certificate(('www.google.com', 443))
x509 = M2Crypto.X509.load_cert_string(cert)
x509.get_subject().as_text()
# 'C=US, ST=California, L=Mountain View, O=Google Inc, CN=www.google.com'

# OpenSSL
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
x509.get_subject().get_components()
#[('C', 'US'),
# ('ST', 'California'),
# ('L', 'Mountain View'),
# ('O', 'Google Inc'),
# ('CN', 'www.google.com')]

rawDescription: How can I retrieve the TLS/SSL peer certificate of a remote host using python?

description: Retrieve the TLS/SSL peer certificate of a remote host using python


code:
bigfont = tkFont.Font(family="Helvetica",size=20)
root.option_add("*TCombobox*Listbox*Font", bigfont)

rawDescription: Combobox fontsize in tkinter

description: Combobox fontsize in tkinter


code:
def count_function(sequence, item):
    index = 0
    count = 0

    while index < len(sequence):
        if sequence[index] == item:
            count = count + 1
        index = index + 1

    return count

rawDescription: If there any way to add this code to a function?


rawDescription: Python: Twitter API tweets/search: Flatten nested dictionary to columns


code:
import itertools

for items in itertools.izip(deque1, deque2, deque3):
    for item in items:
        some_action(item)

for item in itertools.chain(deque1, deque2, deque3):
    some_action(item)

rawDescription: An elegant and fast way to consecutively iterate over two or more containers in Python?


code: df = df.set_index(['month'])

rawDescription: creating multiple dataframes which contain subsets of an orginal pandas dataframe

description: Creating multiple dataframes which contain subsets of an orginal pandas dataframe


rawDescription: How do you write one element of multiple lists to a file before moving on to the next element?

description: Write one element of multiple lists to a file before moving on to the next element


code:
def func(a, b):
    c = np.array([1,a,a**2,a**3])
    d = np.array(b)
    return sum(c*d)

rawDescription: SUM does not work

description: SUM does not work


rawDescription: Python: Pandas: Speeding up an Apply Function


code:
class EmptyPaginator(flask.ext.sqlalchemy.Pagination):
    def __init__(self):
        super(EmptyPaginator, self).__init__(None, 1, 1, 0, [])

    def next(self, error_out=False):
        self.page += 1
        return self

    def prev(self, error_out=False):
        self.page -= 1
        return self

rawDescription: empty paginate object sqlalchemy

description: Empty paginate object sqlalchemy


code: from IPython import embed

rawDescription: Can I use IPython in an embedded interactive Python console?

description: Use IPython in an embedded interactive Python console


rawDescription: Returning the number of likes of a facebook page using Graph API in Python to my .html page


code:
import types

class SomeClass(object):
    def __init__(self,label):
        self.label = label

    def __str__(self):
        return self.label

    def __getattr__(self, name):
        # If name begins with f create a method
        if name.startswith('f'):
            def myfunc(self):
                return "method " + name + " on SomeClass instance " + str(self)
            meth = types.MethodType(myfunc, self, SomeClass)
            return meth
        else:
            raise AttributeError()

rawDescription: Generic methods in python

description: Generic methods in python


rawDescription: In pandas, how do I replace label IDs with label strings?


code:
# Reshape the 3D array to a 2D array merging the first two dimensions
Ar = A.reshape(-1,A.shape[2])

# Perform lex sort and get the sorted indices and xy pairs
sorted_idx = np.lexsort(Ar.T)
sorted_Ar =  Ar[sorted_idx,:]

# Get the count of rows that have at least one TRUE value
# indicating presence of unique subarray there
unq_out = np.any(np.diff(sorted_Ar,axis=0),1).sum()+1

rawDescription: Efficiently count the number of occurrences of unique subarrays in NumPy?


code: for instance in Example.objects.all(): instance.save()

rawDescription: Django ORM to sum two columns to fill a third one


code:
class OptionAdmin(admin.ModelAdmin):

   form = forms.OptionForm

rawDescription: Grouping CheckboxSelectMultiple Options in Django


code:
def gen():
    yield 1
    yield 2
    yield 3
    yield 4
    yield 5
import ipdb
ipdb.set_trace()

g1 = gen()

text = "aha" + "bebe"

mylst = range(10, 20)

rawDescription: Convert generator object to list for debugging


code:
[
{'device': '/dev/bus/usb/001/009', 'tag': 'Apple, Inc. Optical USB Mouse [Mitsumi]', 'id': '05ac:0304'},
{'device': '/dev/bus/usb/001/001', 'tag': 'Linux Foundation 2.0 root hub', 'id': '1d6b:0002'},
{'device': '/dev/bus/usb/001/002', 'tag': 'Intel Corp. Integrated Rate Matching Hub', 'id': '8087:0020'},
{'device': '/dev/bus/usb/001/004', 'tag': 'Microdia ', 'id': '0c45:641d'}
]

rawDescription: Simple way to query connected USB devices info in Python?


code:
# On Windows/wininst, add a .py extension and an .exe launcher
if group=='gui_scripts':
    ext, launcher = '-script.pyw', 'gui.exe'
    old = ['.pyw']
    new_header = re.sub('(?i)python.exe','pythonw.exe',header)
else:
    ext, launcher = '-script.py', 'cli.exe'
    old = ['.py','.pyc','.pyo']
    new_header = re.sub('(?i)pythonw.exe','python.exe',header)

if os.path.exists(new_header[2:-1]) or sys.platform!='win32':
    hdr = new_header
else:
    hdr = header

rawDescription: Create launchable GUI script from Python setuptools (without console window!)


code:
import urllib, urlparse
qs = "param1=1234&param2=abcd&param3=efgh&param4=ijkl"
qry = urlparse.parse_qs(qs)
del qry['param2']
urllib.urlencode(qry, True)

rawDescription: Python RegEx Remove / Replace

description: Python RegEx Remove / Replace


code:
N = 10
G=nx.grid_2d_graph(N,N)
pos = dict( (n, n) for n in G.nodes() )
labels = dict( ((i, j), i + (N-1-j) * N ) for i, j in G.nodes() )
nx.relabel_nodes(G,labels,False)

rawDescription: NetworkX: how to build an Erdos-Renyi graph from a set of predetermined positions?


code: self.t0["fg"] = "red"

rawDescription: How to alter attributes in Tkinter

description: Alter attributes in Tkinter


code:
for x in tuesday:
 print (x)

rawDescription: Use loop to print an array

description: Use loop to print an array


code:
thetas = np.linspace(0, 2*np.pi, 180)
values = np.sin(6 * thetas)

polar_heat(values, thetas, fraction=0.3)

rawDescription: Python plot data as a function of angle in a ring


code: print ("This is your eight character key. \n"," ".join(ASCIIKey))

rawDescription: How to print only the result of the encryption and not the intermediate steps?

description: Print only the result of the encryption and not the intermediate steps


code:
data = [65, 203, 96, 66]
b = ''.join(chr(i) for i in data)
import struct
struct.unpack('>f', b)

rawDescription: combine four bytes and convert to float

description: Combine four bytes and convert to float


code:
from datetime import datetime
import pandas as pd
from bokeh.charts import Area, show, output_notebook
import bokeh.models.glyphs
import bokeh.models.sources

df = pd.DataFrame()
df['date'] = [datetime(2016, 1, 1), datetime(2016, 1, 2), datetime(2016, 1, 3)]
df['v1'] = [1, 2, 3]
df['v2'] = [4, 4, 3]

p = Area(df, x='date', y=['v1', 'v2'], title="Area Chart",
         xscale='datetime', stack=True,
         xlabel='time', ylabel='values',
         # We need to disable the automatic legend and add the correct one later
         legend=False)

# Create a data source
ds = bokeh.models.sources.ColumnDataSource(dict(x=df['date'], y=[0,3,2]))
# Define the line glyph
line = bokeh.models.glyphs.Line(x='x', y='y', line_width=4, line_color='blue')
# Add the data source and glyph to the plot
p.add_glyph(ds, line)

# Manually update the legend
legends = p._builders[0]._legends
legends.append( ('x',[p.renderers[-1]] ) )
# Activate the legend
p.legend=True
# Add it to the chart
p.add_legend(legends)

output_notebook()
show(p)

rawDescription: Add additional curve to bokeh chart

description: Add additional curve to bokeh chart


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

N = 1000
xTrue = np.linspace(0, 1000, N)
yTrue = 3 * xTrue
xData = xTrue + np.random.normal(0, 100, N)
yData = yTrue + np.random.normal(0, 100, N)
xData = np.reshape(xData, (N, 1))
yData = np.reshape(yData, (N, 1))
data = np.hstack((xData, yData))

mu = data.mean(axis=0)
data = data - mu
# data = (data - mu)/data.std(axis=0)  # Uncommenting this reproduces mlab.PCA results
eigenvectors, eigenvalues, V = np.linalg.svd(data.T, full_matrices=False)
projected_data = np.dot(data, eigenvectors)
sigma = projected_data.std(axis=0).mean()
print(eigenvectors)

fig, ax = plt.subplots()
ax.scatter(xData, yData)
for axis in eigenvectors:
    start, end = mu, mu + sigma * axis
    ax.annotate(
        '', xy=end, xycoords='data',
        xytext=start, textcoords='data',
        arrowprops=dict(facecolor='red', width=2.0))
ax.set_aspect('equal')
plt.show()

rawDescription: Basic example for PCA with matplotlib

description: Basic example for PCA with matplotlib


code: my_data = zip(*rand_lists)

rawDescription: how do I export my lists into an excel compatible file?

description: Export my lists into an excel compatible file


code: random.randrange(0, 11, 2)

rawDescription: Random number between specific values

description: Random number between specific values


code:
import test
a = test.Array()
a = [[0, 1], [2, 3]]
test.print_array(a)

rawDescription: Passing Python array to c++ function with SWIG


code:
import subprocess
import time
argument = '...'
proc = subprocess.Popen(['python', 'bar.py', argument], shell=True)
time.sleep(3) # <-- There's no time.wait, but time.sleep.
pid = proc.pid # <--- access `pid` attribute to get the pid of the child process.

rawDescription: How to determine pid of process started via os.system

description: Determine pid of process started via os.system


code:
counts = df.price_point.value_counts()
n = counts.high

rawDescription: Store the counts for a specific value in a column in Pandas dataframe


code:
def fix_line(line):
    if line[:9].isalnum():
        return line[10:].lstrip()
    else:
        return line

rawDescription: Match a specific number of characters in string and delete python


rawDescription: Remove all lists of multiple sets where the specific index is same


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

x = np.arange(11)
y = - x**2
plt.bar(x, y)

rawDescription: How can I properly plot -dBc values in matplotlib?

description: Properly plot -dBc values in matplotlib


code:
class ContextManagerShelf(shelve.DbfilenameShelf):
    def __enter__(self):
        return self
    def __exit__(self, *exc_info):
        self.close()

rawDescription: Supporting the "with open" syntax without using contextlib.closing()


code:
import matplotlib.pyplot as plt
from matplotlib.dates import datetime as dt

raw_dates = ["2013-08-30 13:33:11", "2013-08-29 13:33:11", "2013-08-28 13:33:11"]
x = [dt.datetime.strptime(d, '%Y-%m-%d %H:%M:%S') for d in raw_dates]
y = [125, 120, 112]

plt.plot(x, y)

rawDescription: Matplotlib - How to convert dates from SQLite to matplotlib firmat?


code: appl.set_index('Date', inplace = True)

rawDescription: Retrieving .loc index values in pandas dataframe


code: e = root.xpath('.//a[contains(text(),"TEXT A")]')

rawDescription: How to use lxml to find an element by text?

description: Use lxml to find an element by text


code:
b = ['d']
c = ['e', 'f']
h = []
a = [b,c,h]

def recur(l):
    if not l: # keep going until list is empty
        return 0
    else:
        return recur(l[1:]) + len(l[0]) # add length of list element 0 and move to next element

In [8]: recur(a)
Out[8]: 3

rawDescription: How to count items in list recursively

description: Count items in list recursively


code:
from django.core import serializers
from models import Book

data = serializers.serialize("xml", Book.objects.all())

rawDescription: Django ease of building a RESTful interface


code:
from functools import partial
map(partial(add_x_to_input, some_value_for_x), myList)

rawDescription: Map function and input function parameters


code:
TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages",
    "your_app.variables.my_variables",
)

rawDescription: Django import variables from file

description: Django import variables from file


code:
import ssl
import urllib2
import urllib2_ssl # https://gist.github.com/zed/1347055
opener = urllib2.build_opener(urllib2_ssl.HTTPSHandler(
    ssl_version=ssl.PROTOCOL_TLSv1, #XXX you need to modify urllib2_ssl
    ca_certs='cacert.pem')) # http://curl.haxx.se/ca/cacert.pem.bz2
opener.open('https://example.com/').read()

rawDescription: I want to choose the Transport Layer Security protocol in urllib2


code:
def p_expression_binop(p):
    '''expression : expression '+' expression
                  | expression plustext expression
    '''

rawDescription: How to make a calculator with strings and numbers as mixed input using parser python ply

description: Make a calculator with strings and numbers as mixed input using parser python ply


rawDescription: Why isn't my hover able to click a dynamically created ajax box?


rawDescription: How can I simulate .fetchall() in Python?

description: Simulate .fetchall() in Python


rawDescription: Python+parsing custom config file

description: Python+parsing custom config file


code:
out = []
labels = ['dns', 'ip', 'description']
for line in data:
    out.append(dict(zip(labels, line.split('#'))))

rawDescription: python: read and split files in to list of dictionaries

description: Python: read and split files in to list of dictionaries


code: get_signature(b'KEY', b'TEXT')

rawDescription: Why my python and objective-c code get different hmac-sha1 result?


rawDescription: Iterating and selecting a specific array from a multidimensional array in Python


code:
{'Default_profile': {'title': {'path_to_value': "//div[@id='ctitle']/text()",
                               'required': False,
                              'selector_type': 'XPATH'}}}

rawDescription: Use key/value inheritence but leave it out of the result in pyyaml


code:
for str, func in d.items():
    func(str)

# prints abc

rawDescription: dictionary of built in methods in python

description: Dictionary of built in methods in python


code:
import re

VAR_PATTERN = re.compile(r'#(\w+)#')

def expand_re(pat_dict: {str: str}):  # remove type decl for Python 2.x
    pat_dict = dict(pat_dict)  # make a copy; don't ever modify input parameters

    def substitute_var(matchobj):
        var_name = matchobj.group(1)
        return '(' + pat_dict[var_name] + ')'

    while True:
        found_ref = False

        for key, val in pat_dict.items():
            newval = re.sub(VAR_PATTERN, substitute_var, val)
            if newval != val:
                pat_dict[key] = newval
                found_ref = True

        if not found_ref:  # no more names to replace
            break
    return pat_dict

pd = dict(a='correct',b='#a#',c='#b#',d='#c#',e='#d#',f='#e#',g='#f#')

print('result =', expand_re(pd))

rawDescription: How to modify each item in the dictionary with the value from the rest of others items in the same dictionary combined exhaustedly?

description: Modify each item in the dictionary with the value from the rest of others items in the same dictionary combined exhaustedly


code: column8 = [x[7] for x in lst]

rawDescription: Read specific column from .dat-file in Python


code:
fig,axes = plt.subplots(5,2,sharex=True,sharey=True,figsize=fig_size)

for ax in axes:
    ax.set_xlabel('Common x-label')
    ax.set_ylabel('Common y-label')

rawDescription: Common xlabel/ylabel for matplotlib subplots


code:
for row in df2.itertuples():
    Mydict[feature_name+'_'+str(row[1])]={'P_'+feature_name+'_'+str(row[1])+'_C':(row[2]*float(P_C))/MySumC, \
                                          'P_'+feature_name+'_'+str(row[1])+'_NC':(row[3]*float(P_NC))/MySumNC}

rawDescription: Dynamically creating variables, while doing map/apply on a dataframe in pandas to get key names for the values in Series object returned


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

dept_cities = df.groupby('ArCityArCountry')

for city, departures in dept_cities:
    print(city)
    print([list(r) for r in departures.loc[:, ['ID', 'DptCityDptCountry', 'DateDpt', 'DateAr']].to_records()])

rawDescription: Pandas cvs find date between startdate to enddate


code: divide_by = (division[:,np.newaxis,:] * actg).sum(axis=-1)

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


code:
def foo(a, b=None, c=None, d=None):
    if not b: b = 1
    if not c: c = 2
    if not d: d = 3

rawDescription: What is the most pythonic way to call a method with optional parameters?


rawDescription: comparing column values based on other column values in pandas

description: Comparing column values based on other column values in pandas


code:
child.setFlags(child.flags() | Qt.ItemIsUserCheckable)
child.setCheckState(0, Qt.Unchecked)

rawDescription: PyQt Tree Widget, adding check boxes for dynamic removal


code:
with open('data.txt') as data_file:
    data = data_file.read()

arr = np.fromstring(data, sep=' ')

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


code:
from urllib.parse import quote

safefilename = quote(filename, safe='')

rawDescription: Reversible encoding of string to unix filename


rawDescription: python list.pop() modifies original list (not just copy)

description: Python list.pop() modifies original list (not just copy)


code:
def schema(x):
    if x == 'M':
        step=1
    else:
        step=2
    return step

rawDescription: Return function value to range argument step


code:
import numpy as np
x = np.array([[1, 2], [3, 4]])
y = np.array([[1, 2, 3], [3, 4, 5]])
z = np.array([[7], [8]])

arr = np.ma.empty((2,3,3))
arr.mask = True
arr[:x.shape[0],:x.shape[1],0] = x
arr[:y.shape[0],:y.shape[1],1] = y
arr[:z.shape[0],:z.shape[1],2] = z
print(arr.mean(axis = 2))

rawDescription: Python: Calculating Mean of arrays with different lengths


rawDescription: Evaluating a multivariable function on an array with python/numpy


code:
class MySumSubclass(sum):
    pass

rawDescription: Python builtin functions aren't really functions, right?


code:
for q, (a, b, c) in enumerate(zip(abc, xyz, mnl)):
    print(a, b, c)
    if int(c) > int(a) < int(b):
        print("a", "\t", a)
    elif int(c) > int(b) < int(a):
        print("b", "\t", b)
    elif int(b) > int(c) < int(a):
        print("c", "\t", c)

rawDescription: Python skipping negative values

description: Python skipping negative values


code:
for col in range(GRIDWIDTH):
    for row in range(GRIDHEIGHT):
       houses[col][row].foo() # or houses[(col, row)].foo()

rawDescription: Iterating through instances of a class by instance attribute


code:
instance = form.save(commit=False)
if request.user.is_authenticated():
    instance.user = request.user
instance.save()

rawDescription: Automatically attaching the logged in user to a post he/she creates


code:
pandas.set_option('display.max_columns', 0) # Display any number of columns
pandas.set_option('display.max_rows', 0) # Display any number of rows

rawDescription: equivalent of R's View for Python's pandas

description: Equivalent of R's View for Python's pandas


code:
result = [alist[0].copy()]
result[0]['c'] = [d['c'] for d in alist]

rawDescription: concatenate dictionaries over a key

description: Concatenate dictionaries over a key


code:
class OrganizationalUnit(models.Model):
    name = models.CharField(max_length=100)
    parent = models.ForeignKey(
        'self',
        blank=True, null=True,
        related_name='children',
    )
    checkout_number = models.IntegerField(default=0)

rawDescription: How can I get a total count of a model's related objects and the model's children's related objects?

description: Get a total count of a model's related objects and the model's children's related objects


code:
def find_dict(graph, label, key_value=None, limit=None):
    """ Iterate through a set of labelled nodes, optionally filtering
    by property key/value dictionary
    """
    if not label:
        raise ValueError("Empty label")
    from py2neo.cypher.lang import cypher_escape
    if key_value is None:
        statement = "MATCH (n:%s) RETURN n,labels(n)" % cypher_escape(label)
    else:
        # quote string values
        d = {k: "'{}'".format(v) if isinstance(v, str) else v
             for k, v in key_value.items()}

        cond = ""
        for prop, value in d.items():
            if not isinstance(value, tuple):
                value = ('=', value)

            if cond == "":
                cond += "n.{prop}{value[0]}{value[1]}".format(
                    prop=prop,
                    value=value,
                )
            else:
                cond += " AND n.{prop}{value[0]}{value[1]}".format(
                    prop=prop,
                    value=value,
                )

        statement = "MATCH (n:%s ) WHERE %s RETURN n,labels(n)" % (
            cypher_escape(label), cond)
    if limit:
        statement += " LIMIT %s" % limit
    response = graph.cypher.post(statement)
    for record in response.content["data"]:
        dehydrated = record[0]
        dehydrated.setdefault("metadata", {})["labels"] = record[1]
        yield graph.hydrate(dehydrated)
    response.close()

def find_dict_one(graph, label, key_value=None):
    """ Find a single node by label and optional property. This method is
    intended to be used with a unique constraint and does not fail if more
    than one matching node is found.
    """
    for node in find_dict(graph, label, key_value, limit=1):
        return node

rawDescription: py2neo: Graph.find_one with multiple key/values

description: Py2neo: Graph.find_one with multiple key/values


code:
def interact():
    # Remove the console handler
    for handler in  logger.handlers:
        if not isinstance(handler, logging.FileHandler):
            saved_handler = handler
            logger.removeHandler(handler)
            break

    # Interact
    logger.info('to file only')

    # Add the console handler back
    logger.addHandler(saved_handler)

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


rawDescription: Concatenate or print list elements with a trailing comma in Python


code:
for parent in soup.select('div.parent'):
    column = parent.select('div.column')[1]

rawDescription: soup.select css nth of type?

description: Soup.select css nth of type


code:
def my_action():
    value = request.vars.value
    # handle the variable
    redirect(URL('index'), client_side=True)

rawDescription: Web2py: Pass a variable from view to controller


code:
def create_list(n):
    import random
    my_list = []
    for i in range(n):
        my_list.append(random.randint(1,6))
    return my_list

rawDescription: I need to get my main function working

description: I need to get my main function working


code:
import re
re.compile('a') == 'a'  # PatternObject == str  => always false
re.compile('a').search('a')
re.search('a', 'a')

rawDescription: re.compile does not work properly

description: Re.compile does not work properly


code: mybytes = data[16:20]

rawDescription: Reading UDP Packets

description: Reading UDP Packets


rawDescription: Extract string from tuple embedded in a list, Python 3


code:
account_sandy = Account.get_or_insert('Sandy')
sandy_message = Message.get_or_insert('greeting', parent=account_sandy.key)
sandy_message_rev = Revision.get_or_insert('2', parent=sandy_message.key)

rawDescription: GAE Implications of NDB hierarchy and entity groups


code:
def addDictionary(States, Transition, Languaje, Tr):
    for s, t in zip(States, Tr):
        Transition[s] = dict(zip(Languaje, t))

rawDescription: iterating through list of list and dictionary python

description: Iterating through list of list and dictionary python


code: any(isinstance(e, int) and e > 0 for e in [1,2,'joe'])

rawDescription: any() function in Python with a callback

description: Any() function in Python with a callback


rawDescription: Numpy Element-wise Standard Deviation

description: Numpy Element-wise Standard Deviation


code: new_s = input_str[:count+1]

rawDescription: How to remove the trailing white spaces in python?

description: Remove the trailing white spaces in python


rawDescription: Difference between np.random.seed() and np.random.RandomState()


code:
class ListOfFigures(TableOfContents):
    def notify(self, kind, stuff):
        """ The notification hook called to register all kinds of events.
            Here we are interested in 'Figure' events only.
        """
        if kind == 'TOCFigure':
            self.addEntry(*stuff)

class ListOfTables(TableOfContents):
    def notify(self, kind, stuff):
        """ The notification hook called to register all kinds of events.
            Here we are interested in 'Table' events only.
        """
        if kind == 'TOCTable':
            self.addEntry(*stuff)

rawDescription: Add figure and tables list to Table of Content using reportlab


code:
dfrm['Date'] = dfrm.index
dfrm

rawDescription: How to take out the column index name in dataframe

description: Take out the column index name in dataframe


code:
input1 = '5a+6b'
input2 = '2a+3b+9c'
s = "+".join((input1, input2))
s
d = {}
for i in s.split("+"):
    d[i[-1]] = d.get(i[-1], 0) + int(i[:-1])

d
"+".join("".join((str(j),i)) for i, j in d.items())

rawDescription: Python string processing based on the numeric part: '5a+6b' + '2a+3b+9c' = '7a+9b+9c'


code:
def get_queryset(self):
    query_set = super(WorkoutList, self).get_queryset()
    query_params = self.request.QUERY_PARAMS.dict()
    if 'date' in query_params and 'exclude_app_install_time' in query_params:
        query_set = query_set.filter(created__contains=date).exclude(
            app_install_time=query_params['exclude_app_install_time'])
    return query_set

rawDescription: unbound method get_queryset() must be called with WorkoutList instance as first argument (got nothing instead)

description: Unbound method get_queryset() must be called with WorkoutList instance as first argument (got nothing instead)


code:
def unpack_helper(fmt, data):
    size = struct.calcsize(fmt)
    return struct.unpack(fmt, data[:size]), data[size:]

rawDescription: packing and unpacking variable length array/string using the struct module in python

description: Packing and unpacking variable length array/string using the struct module in python


code:
class AbstractClassifier(object):
    @property
    def name(self):
        return self.__class__.__name__

class ClassifierA(AbstractClassifier):
    def __call__(self, data):
        return 1
ClassifierA = ClassifierA()       # see below for alternatives

rawDescription: Fudging the line between classes and functions


code:
import pandas as pd

df = df.groupby((df["x.count"] != df["x.count"].shift()).cumsum().values).first()

rawDescription: Delete the entire row if the a value in value is equal to previous row in Python


code: x = x[indices]

rawDescription: Numpy: choose, where, partition or something else?


code:
rawdict.restore(0)   # set rawdict.data to rawdict._buffer[0]
print(rawdict.data)
# {2}
print(rawdict._buffer)
# deque(['{3}', '{4}', '{5}', '{6}', '{2}'], maxlen=5)

rawDescription: How to build an undo storage with limit?

description: Build an undo storage with limit


rawDescription: Plotting pandas timedelta

description: Plotting pandas timedelta


rawDescription: How to trigger something on the close of a loop?

description: Trigger something on the close of a loop


code:
import datetime as dt
import numpy as np
import pandas as pd

dates_list = [dt.datetime(2015,11,i+1) for i in range(3)]
month_day_list = [d.strftime("%m%d") for d in dates_list]

dataframe_collection = {}

for month_day in month_day_list:
    new_data = np.random.rand(3,3)
    dataframe_collection[month_day] = pd.DataFrame(new_data, columns=["one", "two", "three"])

for key in dataframe_collection.keys():
    print("\n" +"="*40)
    print(key)
    print("-"*40)
    print(dataframe_collection[key])

rawDescription: How to create an array of dataframes in Python

description: Create an array of dataframes in Python


code:
import subprocess
import os
p = subprocess.Popen(["unzip", "-tqq", "encrypted.zip"],
                     stdin=subprocess.PIPE, preexec_fn=os.setsid)
p.stdin.close() #just in case
p.wait()

rawDescription: prevent unexpected stdin reads and lock in subprocess

description: Prevent unexpected stdin reads and lock in subprocess


code:
from django.contrib.gis.geos import *

class LocationResource(ModelResource):
    class Meta:
        queryset = Building.objects.all()
        resource_name = 'location'

    def apply_sorting(self, objects, options=None):
        if options and "longitude" in options and "latitude" in options:
            pnt = fromstr("POINT(" + options['latitude'] + " " + options['longitude'] + ")", srid=4326)
            return objects.filter(building_point__distance_lte=(pnt, 500))

        return super(LocationResource, self).apply_sorting(objects, options)

rawDescription: How can I measure distance with tastypie and geodjango?

description: Measure distance with tastypie and geodjango


code:
state = ' '.join(x if x in guesses else '_' for x in secret)
state

rawDescription: Python: hangman program

description: Python: hangman program


code:
from ast import literal_eval
literal_eval(repr(lis).replace(",)",")"))

rawDescription: Convert list of single element tuples to list of elements


code:
from unidecode import unidecode
my_list = ['guest', 'demo', ':', 'eric', 'iverson', '\xe2\x80\x99s', 'itty', 'bitty', 'search', 'february', '16', 'th', ',', '2010', 'by', 'daniel', 'tunkelang', 'respond', 'i', '\xe2\x80\x99m', 'back', 'from', 'vacation', ',', 'and', 'still', 'digging', 'my', 'way', 'out', 'of', 'everything', 'that', '\xe2\x80\x99s', 'piled', 'up', 'while', 'i', '\xe2\x80\x99ve', 'been', 'offline', 'while', 'i', 'catch', 'up', ',', 'i', 'thought', 'i', '\xe2\x80\x99d', 'share', 'with', 'you', 'a', 'demo', 'that', 'eric', 'iverson', 'was', 'gracious', 'enough', 'to', 'share', 'with', 'me', 'it', 'uses', 'yahoo', '!', 'boss', 'to', 'support', 'an', 'exploratory', 'search', 'experience', 'on', 'top', 'of', 'a', 'general', 'web', 'search', 'engine', 'when', 'you', 'perform', 'a', 'query', ',', 'the', 'application', 'retrieves', 'a', 'set', 'of', 'related', 'term', 'candidates', 'using', 'yahoo', '\xe2\x80\x99s', 'key', 'terms', 'api', 'it', 'then', 'scores', 'each', 'term', 'by', 'dividing', 'it', 'is', 'occurrence', 'count', 'within', 'the', 'result', 'set', 'by', 'it', 'is', 'global', 'occurrence', 'count', '\xe2\x80\x93a', 'relevance', 'measure', 'similar', 'to', 'one', 'my', 'former', 'colleagues', 'and', 'i', 'used', 'at', 'endeca', 'in', 'enterprise', 'contexts', 'you', 'can', 'try', 'out', 'the', 'demo', 'yourself', 'at', 'http', '://www', 'ittybittysearch', 'com', '/', 'while', 'it', 'has', 'rough', 'edges', ',', 'it', 'produces', 'nice', 'results', '\xe2\x80\x93especially', 'considering', 'the', 'simplicity', 'of', 'the', 'approach', 'here', '\xe2\x80\x99s', 'an', 'example', 'of', 'how', 'i', 'used', 'the', 'application', 'to', 'explore', 'and', 'learn', 'something', 'new', 'i', 'started', 'with', '["', 'information', 'retrieval', '"]', 'i', 'noticed', '\xe2\x80\x9c', 'interactive', 'information', 'retrieval', '\xe2\x80\x9d', 'as', 'a', 'top', 'term', ',', 'so', 'i', 'used', 'it', 'to', 'refine', 'most', 'of', 'the', 'refinement', 'suggestions', 'looked', 'familiar', 'to', 'me', '\xe2\x80\x93but', 'an', 'unfamiliar', 'name', 'caught', 'my', 'attention', ':', '\xe2\x80\x9c', 'anton', 'leuski', '\xe2\x80\x9d', 'following', 'my', 'curiosity', ',', 'i', 'refined', 'again', 'looking', 'at', 'the', 'results', ',', 'i', 'immediately', 'saw', 'that', 'leuski', 'had', 'done', 'work', 'on', 'evaluating', 'document', 'clustering', 'for', 'interactive', 'information', 'retrieval', 'further', 'exploration', 'made', 'it', 'clear', 'this', 'is', 'someone', 'whose', 'work', 'i', 'should', 'get', 'to', 'know', '\xe2\x80\x93check', 'out', 'his', 'home', 'page', '!', 'i', 'can', '\xe2\x80\x99t', 'promise', 'that', 'you', '\xe2\x80\x99ll', 'have', 'as', 'productive', 'an', 'experience', 'as', 'i', 'did', ',', 'but', 'i', 'encourage', 'you', 'to', 'try', 'eric', '\xe2\x80\x99s', 'demo', 'it', '\xe2\x80\x99s', 'simple', 'examples', 'like', 'these', 'that', 'remind', 'me', 'of', 'the', 'value', 'of', 'pursuing', 'hcir', 'for', 'the', 'open', 'web', 'speaking', 'of', 'which', ',', 'hcir', '2010', 'is', 'in', 'the', 'works', 'we', '\xe2\x80\x99ll', 'flesh', 'out', 'the', 'details', 'over', 'the', 'next', 'weeks', ',', 'and', 'of', 'course', 'i', '\xe2\x80\x99ll', 'share', 'them', 'here']
my_clean_list = [unidecode(x.decode('utf8')) for x in my_list]
my_clean_list

rawDescription: eliminate unwanted data from list using python

description: Eliminate unwanted data from list using python


code:
a = my_new_generator()
a.next()

rawDescription: Where is the best place to put support functions in a class?


rawDescription: I have a list with item in lists and I want to give them a name for each list python


code:
from sympy import Symbol, prod

def lag_l(xx, j):
    x = Symbol("x")
    parts = ((x - x_i) / (xx[j] - x_i) for i, x_i in enumerate(xx) if i != j)
    return prod(parts)

def lag_L(xx, yy):
    return sum(y*lag_l(xx, j) for j, y in enumerate(yy))

def lag_fn(xx, yy, x):
    return lag_L(xx, yy).subs({'x': x})

rawDescription: Generating a string with python using loops with logic conditions issues against join function alternative?


code:
import httplib2
from oauth2client.client import SignedJwtAssertionCredentials
from apiclient.discovery import build
import gdata.spreadsheets
import gdata.spreadsheets.client
import gdata.gauth
import os

path = os.path.join(os.path.split(__file__)[0],'privatekey.pem')
with open(path) as keyfile:
  private_key = keyfile.read()

credentials = SignedJwtAssertionCredentials(
  '<client email>',
  private_key,
  scope=(
    'https://www.googleapis.com/auth/drive',
    'https://spreadsheets.google.com/feeds',
    'https://docs.google.com/feeds',
  ))
http_auth = credentials.authorize(httplib2.Http())
authclient = build('oauth2','v2',http=http_auth)

auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)

gd_client = gdata.spreadsheets.client.SpreadsheetsClient()
gd_client = auth2token.authorize(gd_client)

# Open the main roster feed
roster_sheet_key = '<key from spreadsheet url>'
feed = gd_client.GetWorksheets(roster_sheet_key)

rawDescription: Python gdata API returning empty data with oauth2


code: newlist = c1 + c2 + c3

rawDescription: Putting items of a list into another list in Python?


rawDescription: Django access m2m model value in template


code:
# let's say that data frame with observations and their mins is called omf
min_time = lambda row: row['epoch'] - omf[omf['observation'] == row['observation']]['min']
timeseries['new_epoch'] = timeseries.apply(min_time,axis=1)

rawDescription: Create five minute time blocks (Pandas/Python)


code: foo = self._setup['bar']

rawDescription: python syntax - meaning of self._setup

description: Python syntax - meaning of self._setup


code:
from pprint import pprint
from itertools import groupby
pprint([list(g) for k, g in groupby(my, key=lambda x:x.date())])

rawDescription: Python how create sublists based by date?


code:
def closestPair(Px, Py):
    if len(Px) <= 3:
        return brute(Px)

    mid = len(Px) / 2
    # get left and right half of Px
    q, r = Px[:mid], Px[mid:]
     # sorted versions of q and r by their x and y coordinates
    Qx, Qy = [x for x in q if Py and  x[0] <= Px[-1][0]], [x for x in q if x[1] <= Py[-1][1]]
    Rx, Ry = [x for x in r if Py and x[0] <= Px[-1][0]], [x for x in r if x[1] <= Py[-1][1]]
    (p1, q1) = closestPair(Qx, Qy)
    (p2, q2) = closestPair(Rx, Ry)
    d = min(dist(p1, p2), dist(p2, q2))
    mn = min((p1, q1), (p2, q2), key=lambda x: dist(x[0], x[1]))
    (p3, q3) = closest_split_pair(Px, Py, d, mn)
    return min(mn, (p3, q3), key=lambda x: dist(x[0], x[1]))

rawDescription: Closest Pair Implemetation Python

description: Closest Pair Implemetation Python


code: cursor.execute("INSERT INTO table VALUES(?,?,?,?,?)", args)

rawDescription: Only inserting specific rows from csv file into Sqlite3 database using python


rawDescription: Django 1.6: Clear data in one table

description: Django 1.6: Clear data in one table


code:
from collections import defaultdict

snowfall = defaultdict(float)

for info in lst:
    snowfall[info['year']] += info['snow']

snowfall = [{'year': year, 'totalsnow': snowfall[year]}
            for year in sorted(snowfall, reverse=True)]

rawDescription: sum of one value in the list of dictionary based on one key in dict

description: Sum of one value in the list of dictionary based on one key in dict


code:
def func():
    global a
    b = {'1': 'd'}
    a = copy.deepcopy(b)

rawDescription: Save a dictionary value in python functions


code: from QuantLib import *

rawDescription: Calculating EuropeanOptionImpliedVolatility in quantlib-python


rawDescription: Flatten nested dictionaries while checking content


code:
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util

class MainHandler(webapp.RequestHandler):
    def get(self):
        self.redirect('http://jacob.bearce.me/')

application = webapp.WSGIApplication([('/.*', MainHandler)],
                                     debug=True)

def main():
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()

rawDescription: Exclude One URL From Python URL Masking

description: Exclude One URL From Python URL Masking


code:
# models.py
class Author(models.Model):
    name = models.CharField()

class Book(models.Model):
    name = models.CharField()
    authors = models.ManyToManyField(Author)

rawDescription: Django: a related model values list as part of a values list query


rawDescription: Equivalent to Lambda Expressions in Python


code:
with open(...) as fin:
    fin.read()   # read first time

with open(...) as fin:
    fin.read()   # read again

rawDescription: Re-read an open file Python

description: Re-read an open file Python


rawDescription: How to urlencode multi valued keys in python?

description: Urlencode multi valued keys in python


code:
# read data from file
with open("classa2.txt") as inf:
    lines = [line.rstrip() for line in inf]

# sort highest scores first
lines.sort(key=sort_key, reverse=True)

print("\n".join(lines))

rawDescription: how to sort files by the value of a dictionary using python

description: Sort files by the value of a dictionary using python


code:
def twos_complement(n, w=32):
    if n & (1 << (w - 1)): n = n - (1 << w)
    return n

rawDescription: How to make python look hex numbers as two's complemented?

description: Make python look hex numbers as two's complemented


code: map(list, combinations(terms, r=2))

rawDescription: Python create many-to-many relationships from a list


code: int_values = [int(v) for v in cell.internal_values if type(v) == float]

rawDescription: Strip and convert values/numbers out of a list


code:
foo = (
    "this text will be " +
    "joined into one long string. " +
    "It is concatenated " +
    "explictly using the `+` operator."
)

rawDescription: String Concatenation: Putting large chuck of text in Python code


code:
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "YourProject.settings")

from YourApp.models import YourModel

rawDescription: Uploading Data from a Python File into a Django Database


rawDescription: convert selected datetime to date in sqlalchemy

description: Convert selected datetime to date in sqlalchemy


code:
for k, v in d.items():
    if not v: del d[k]

rawDescription: remove the keys which has blank values

description: Remove the keys which has blank values


code:
x.ix[(x.groupby(pd.Grouper(key='Timestamp', freq='5T'))['Volume'].transform('sum') > 65000)
     &
     (x.Timestamp.dt.hour==9)
     &
     (x.Timestamp.dt.minute>=30) & (x.Timestamp.dt.minute<=34)].set_index('Timestamp')

rawDescription: Select certain dates from Pandas dataframe


code:
from collections import OrderedDict
counts_and_means = \
counts_and_means

rawDescription: Pandas: Best idioms for renaming, reordering columns while aggregating


code: [x for x in myList if x.n == 30]  # list of all elements with .n==30

rawDescription: Searching a list of objects in Python

description: Searching a list of objects in Python


code:
import ctypes

mkl_rt = ctypes.CDLL('libmkl_rt.so')
mkl_set_num_threads = mkl_rt.MKL_Set_Num_Threads
mkl_get_max_threads = mkl_rt.MKL_Get_Max_Threads

rawDescription: Using mkl_set_num_threads with numpy

description: Using mkl_set_num_threads with numpy


rawDescription: Negative RegEx pattern matching in Python equivalent to Perl(!~ operator)


code:
import os
f = open("out.txt", "r")
os.chdir("base location")
for line in f:
    s = line.strip()
    if len(s)>0:
        # ignore blank or whitespace-only lines
        os.mkdir(s)

rawDescription: Python reading whitespace-separated file lines as separate lines


code:
class Comment(Model):
   ...
   class Meta:
        ordering = ['-pub_date']

rawDescription: Render multiple quersets in html page in Django


code:
for key, group in groupby(line.split(';'), key=lambda i: int(i) > 4):
    if key:
        for i in group:
            print(i)
    else:
        print(';'.join(group))

rawDescription: How does one splice a list and create a new list with the spliced section?

description: Splice a list and create a new list with the spliced section


code:
from BeautifulSoup import BeautifulSoup

# On the current page
source = driver.page_source
soup = BeautifulSoup(source)

href = soup('<the tag containing the anchor>',{'id':'toplevel_page_menu'})[0]('ul')[0]('li')[2]('a')[0]['href']

rawDescription: Extract link from XPath using Selenium Webdriver and Python?


code:
import asyncio

l = asyncio.Lock()
check_lock = asyncio.Lock()

async def test():
    async with check_lock:
        print('locked' if l.locked() else 'unlocked')
        await asyncio.ensure_future(l.acquire())
        print('now', 'locked' if l.locked() else 'unlocked')

    await asyncio.sleep(1)
    l.release()

async def main():
    await asyncio.gather(test(), test())

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

rawDescription: Is there any way to guarantee asyncio.Task will be started immediately?


code:
# search the input HTML for matches to the entryExpr expression, and build up lookup dict
lookup = {}
for entry in entryExpr.searchString(sourcehtml):
    for i in range(entry.start, entry.end+1):
        lookup[i] = tuple(entry.vals)

rawDescription: Parse HTML Table with Python BeautifulSoup


rawDescription: Join two pandas series by values

description: Join two pandas series by values


code: logging.Formatter('%(thread)d - %(threadName)s - %(levelname)s - %(message)s')

rawDescription: how do I create logging / loggers on a per thread basis?

description: Create logging / loggers on a per thread basis


code:
func_dict = {
    'lt'   : operator.lt,
    'gt'   : operator.gt,
    'nand' : lambda x, y: not (x and y)
}

rawDescription: Python: Pass inequality as string in dict for evaluation


code: df = df.iloc[(df.index.get_level_values(0).str.contains('ba')) | (df.index.get_level_values(1).str.contains('ne'))]

rawDescription: Pandas dataframe with MultiIndex: check if string is contained in index level


code:
import numpy

DELETION, INSERTION, MATCH = range(3)

def smith_waterman(seq1, seq2, insertion_penalty = -1, deletion_penalty = -1,
                   mismatch_penalty = -1, match_score = 2):
    """
    Find the optimum local sequence alignment for the sequences `seq1`
    and `seq2` using the Smith-Waterman algorithm. Optional keyword
    arguments give the gap-scoring scheme:

    `insertion_penalty` penalty for an insertion (default: -1)
    `deletion_penalty`  penalty for a deletion (default: -1)
    `mismatch_penalty`  penalty for a mismatch (default: -1)
    `match_score`       score for a match (default: 2)

    See <http://en.wikipedia.org/wiki/Smith-Waterman_algorithm>.

    >>> for s in smith_waterman('AGCAGACT', 'ACACACTA'): print s
    ...
    AGCAGACT-
    A-CACACTA
    """
    m, n = len(seq1), len(seq2)

    # Construct the similarity matrix in p[i][j], and remember how
    # we constructed it -- insertion, deletion or (mis)match -- in
    # q[i][j].
    p = numpy.zeros((m + 1, n + 1))
    q = numpy.zeros((m + 1, n + 1))
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            deletion = (p[i - 1][j] + deletion_penalty, DELETION)
            insertion = (p[i][j - 1] + insertion_penalty, INSERTION)
            if seq1[i - 1] == seq2[j - 1]:
                match = (p[i - 1][j - 1] + match_score, MATCH)
            else:
                match = (p[i - 1][j - 1] + mismatch_penalty, MATCH)
            p[i][j], q[i][j] = max((0, 0), deletion, insertion, match)

    # Yield the aligned sequences one character at a time in reverse
    # order.
    def backtrack():
        i, j = m, n
        while i > 0 or j > 0:
            assert i >= 0 and j >= 0
            if q[i][j] == MATCH:
                i -= 1
                j -= 1
                yield seq1[i], seq2[j]
            elif q[i][j] == INSERTION:
                j -= 1
                yield '-', seq2[j]
            elif q[i][j] == DELETION:
                i -= 1
                yield seq1[i], '-'
            else:
                assert(False)

    return [''.join(reversed(s)) for s in zip(*backtrack())]

rawDescription: How do I decide which way to backtrack in the Smith–Waterman algorithm?

description: Decide which way to backtrack in the Smith–Waterman algorithm


code:
decorated = [(i,row) for i,lol in enumerate([a,b]) for row in lol]
new_lol = sorted(decorated, key=lambda x: x[1][1 if x[0]==0 else 2])
new_lol = [x[1] for x in new_lol]

rawDescription: How can I sort 2 lists of lists by column?

description: Sort 2 lists of lists by column


code:
import re

a=["Britney spears", "red dog", "\xa2xe3"]

regex = re.compile('|'.join(re.escape(x) for x in a))

b=["cat","dog","red dog is stupid", "good stuff \xa2xe3", "awesome Britney spears"]

b = [regex.sub("",x) for x in b ]
print (b)  #['cat', 'dog', ' is stupid', 'good stuff ', 'awesome ']

rawDescription: check if element of list is present in elements of another list

description: Check if element of list is present in elements of another list


code:
with open(filename, 'rb') as f:
    byte_str = f.read()

rawDescription: Workin with binary in python

description: Workin with binary in python


code:
def site_graph(request):
    metrics=[]
    sites = Site.objects.order_by('name')
    for site in sites:
        metrics_site={}
        metrics_site["site"] = site
        metrics_site["metrics"] = site.metric_set.all()
        metrics.append(metrics_site)
    return render(request, 'da/site_graph.html', {'metrics': metrics})

rawDescription: Django Get Primary + Foreign Attributes

description: Django Get Primary + Foreign Attributes


code:
from django.contrib.auth.models import User
get_model_metadata(User, meta_whitelist=['max_length'])

rawDescription: How can i extract metdata from django models

description: Extract metdata from django models


code:
['This is the description text body that I am trying to capture with regex.',
 'And Another Section On two lines']

rawDescription: RegEx to capture multi-line text body

description: RegEx to capture multi-line text body


code:
from pandas import DataFrame
df = DataFrame(dict(a=range(10), b=range(10)))

type(df['b'])
# <class 'pandas.core.series.Series'>
type(df[['b']])
# <class 'pandas.core.frame.DataFrame'>

rawDescription: Extract a column from a DataFrame

description: Extract a column from a DataFrame


code:
@app.template_filter('to_decimal')
def to_decimal_filter(f):
    return decimal.Decimal(f)

rawDescription: Jinja2 convert float to decimal mid calculation


code:
def wow():
    return "spam"

# locals() returns a dict of variables and functions that are locally defined
print(locals()["wow"]())

rawDescription: Call a function using getattr in Python

description: Call a function using getattr in Python


code:
def sum(a, b, c, d):
    return a + b + c + d

values1 = (1, 2)
values2 = { 'c': 10, 'd': 15 }
s = sum(*values1, **values2)

rawDescription: What does the Star operator mean in Python?


code: df['diff'] = df['col'].diff()

rawDescription: built in method like pandas.sub() that does row wise subtraction

description: Built in method like pandas.sub() that does row wise subtraction


rawDescription: Returning Data from Python Function for use with Jinja2 Template (Flask)


code:
df = pd.DataFrame([0])
df.ix[0,0]
df[0][0]

rawDescription: No Column Names in pandas python

description: No Column Names in pandas python


rawDescription: Empty EmbeddedDocument field?

description: Empty EmbeddedDocument field


rawDescription: How do I strip patterns or words from the end of the string backwards?

description: Strip patterns or words from the end of the string backwards


code:
from astropy.table import Table
orig_catalogue = Table.read('filename.fits')

rawDescription: build new table from rows in astropy

description: Build new table from rows in astropy


code:
@app.route('/resources', methods=['GET'])
def resources_get_all():
    get_limits()
    resources = []
    data = Resource.customq(LIMIT, OFFSET).all()

rawDescription: Default scope for queries using Python + Flask-SqlAlchemy


rawDescription: implementing strange calculator

description: Implementing strange calculator


code:
for num in ... :
    your_count = num[0] # gets the first column

rawDescription: How to convert neo4j return types to python types

description: Convert neo4j return types to python types


code:
mymap = open('data.txt').readlines()
mymap = map(str.strip,mymap) # to remove '\n'

rawDescription: How to create sprites using ConfigParser in Pygame

description: Create sprites using ConfigParser in Pygame


code:
return sqrt(max(
    abs(x - cx) + abs(y - cy)
    for cx, cy in chests))

rawDescription: returning the largest or smallest result from a for loop in python

description: Returning the largest or smallest result from a for loop in python


code:
grb = df.groupby(['ID0', 'Time'])
df['Data0.2'] = grb['Data0'].transform(lambda ts: ts[::-1])
df['Data1.2'] = grb['Data1'].transform(lambda ts: ts[::-1])

rawDescription: Widening Pandas Data Frame, Similar to Pivot or Stack/Unstack


code:
codeLoc = "../rcode"
r_call = "source('" + codeLoc + "/filename.R')"
ro.r(r_call)

rawDescription: assigning then attaching a filepath name in python using rpy2

description: Assigning then attaching a filepath name in python using rpy2


rawDescription: How does python do list slices and modification on the LHS?


code:
from functools import wraps
import warnings

def deprecated(message, newfunc):
    def _deprecated(func):
        @wraps(func)
        def _wrapped(*args, **kwds):
            warnings.warn(message + ', ' + newfunc,
                DeprecationWarning, stacklevel=2)
            return func(*args, **kwds)
        return _wrapped
    return _deprecated

rawDescription: Get specific info about calling code in Python decorators


rawDescription: Using numpy fromfile on binary file returns 1 dimension ndarray


code: dfts.groupby(lambda x : x.month).mean()

rawDescription: Group by multiple time units in pandas data frame


code:
from datetime import datetime

now = datetime.now()
seconds_since_midnight = (now - now.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds()

rawDescription: Get seconds since midnight in python

description: Get seconds since midnight in python


code:
#include <iostream>
#include <fstream>

rawDescription: C++ string parsing (python style)

description: C++ string parsing (python style)


code:
obj = weak_obj()
func = weak_func()

rawDescription: Can I weak reference methods?

description: Weak reference methods


code:
from sqlalchemy.orm import aliased
page1 = aliased(Page)
page2 = aliased(Page)
qry = (session
        .query(Page).filter(Page.slug=='currentlevel')
        .join(page1, Page.parent).filter(page1.slug=="secondlevel")
        .join(page2, page1.parent).filter(page2.slug=="firstlevel")
        )

rawDescription: Foreign key querines in SQLAlchemy

description: Foreign key querines in SQLAlchemy


code:
inp = ''
desired = 'hello'
while inp != desired:
    inp = raw_input('Enter a greeting: ')

rawDescription: I can't seem to understand loops

description: I can't seem to understand loops


rawDescription: Splitting large text file by a delimiter in Python


code:
def expQuestions(s):
    """For OCR w/ a questionmark in them, expand questions with
    @s for all possibilities"""
    numqs = s.count("?")

    blah = list(s)
    for expqs in cartesian_product([(0,1,2,3)]*numqs):
        newblah = blah[:]
        qi = 0
        for i,c in enumerate(newblah):
            if newblah[i] == '?':
                newblah[i] = '@'*expqs[qi]
                qi += 1
        yield "".join(newblah)

rawDescription: elegant way to match two wildcarded strings

description: Elegant way to match two wildcarded strings


code:
def name_gen():
    with open('C:/python/namessample.txt') as nameFile:
        for name in nameFile.read().split(','):
            yield name.strip('"')

rawDescription: generating strings from a file

description: Generating strings from a file


code:
def GetSelections(self):
    """
    GetSelections() -> DataViewItemArray

    Returns a list of the currently selected items.
    """

rawDescription: wxPython | wxDataViewListCtrl get all selected rows/items

description: WxPython | wxDataViewListCtrl get all selected rows/items


code: has_duplicates = len(set(b)) < len(b)

rawDescription: how to find whether this python list within list contains duplicates or not?

description: Find whether this python list within list contains duplicates or not


code:
for i, btn in enumerate(self.buttons):
    btn['command'] = lambda idx=i: self.removeButton(idx)

rawDescription: Making Buttons that Locate Themselves in a List


code:
from bs4 import BeautifulSoup

doc = ...
soup = BeautifulSoup(doc, "xml")  # Directs bs to use lxml

rawDescription: Reading <content:encoded> tags using BeautifulSoup 4


code:
import os
import sys
import jinja2
# path to lib direcotory
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'lib'))

rawDescription: Uploading Python third party libraries

description: Uploading Python third party libraries


code:
def suitWindow(self):
        self.tableWindow.title("Blackjack Table")
        self.tableWindow.config(bg="dark green")

        self.EHandEntry=Entry(self.tableWindow,fg="white",bg="dark green",width=10)
        self.EHandEntry.grid(row=5,column=9)

        self.EHandSubmit=Button(self.tableWindow,text="Bet",fg="white",padx=15,pady=5,command=self.betE,bg="dark green",activebackground="dark green",activeforeground="white")
        self.EHandSubmit.grid(row=5,column=10)

        self.SHandEntry=Entry(self.tableWindow,fg="white",bg="dark green",width=10)
        self.SHandEntry.grid(row=8,column=6)

        self.SHandSubmit=Button(self.tableWindow,text="Bet",fg="white",padx=15,pady=5,command=self.betST,bg="dark green",activebackground="dark green",activeforeground="white")
        self.SHandSubmit.grid(row=8,column=7)
        self.WHandEntry=Entry(self.tableWindow,fg="white",bg="dark green",width=10)
        self.WHandEntry.grid(row=5,column=3)

        self.WHandSubmit=Button(self.tableWindow,text="Bet",fg="white",padx=15,pady=5,command=self.betW,bg="dark green",activebackground="dark green",activeforeground="white")
        self.WHandSubmit.grid(row=5,column=4)

rawDescription: destroy() not destroying on repeats

description: Destroy() not destroying on repeats


code:
from django.db.models import Count

d = {
    x['field_name']: x['count']
        for x in model.objects
            .annotate(count=Count('field_name'))
            .values('field_name', 'count')
            .distinct()
}

rawDescription: django - aggregate data according to field in model

description: Django - aggregate data according to field in model


code:
a = "foo baz bar"
"".join([s.capitalize() for s in a.rsplit()])
FooBazBar

rawDescription: Convert post title to CamelCase

description: Convert post title to CamelCase


code:
from functools import partial

def power(a, pow=2):
    ret = 1
    for _ in range(pow):
        ret *= a
    return ret

cube = partial(power, pow=3)

cube(5)

rawDescription: Python, assign function to variable, change optional argument's value


code:
class MyString:
    def __div__(self, sep):
        return self.str.split(sep)
m = MyString()

rawDescription: Is there any way besides __init__ to receive values when constructing a class?


code: idx = numpy.argmax(ra['f0']-ra['f2'])

rawDescription: numpy recarray minimum diffrences

description: Numpy recarray minimum diffrences


rawDescription: Creating a sphere packing nearest neighbor list from integers


rawDescription: How to recover lists that are saved in Series

description: Recover lists that are saved in Series


code:
# on each line, get the 3rd comma-delimited field and break out the
# first two space-separated chunks of the licence key

rawDescription: How to read, in a line, all characters from column A to B

description: Read, in a line, all characters from column A to B


code:
d = {}
for sublist, meaningful_identifier in zip(my_list, my_meaningful_identifiers):
    d[meaningful_identifier] = sublist

rawDescription: Building multiple lists from single list with multiple lists within


rawDescription: Convert a unixtime to a datetime object and back again (pair of time conversion functions that are inverses)


code:
from rk4 import rk4
import numpy as np

def diff(x):
    return x

def mercury(u0,phi0,dphi):
    x=np.array([u0,phi0])
    dt=2
    x=rk4(diff,  # here we send the function to rk4
          x, dt)
    return x
mercury(1,1,2)

rawDescription: Defining a global function in a Python script


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

data = np.random.random((4, 4))

fig, ax = plt.subplots()
# Using matshow here just because it sets the ticks up nicely. imshow is faster.
ax.matshow(data, cmap='seismic')

for (i, j), z in np.ndenumerate(data):
    ax.text(j, i, '{:0.1f}'.format(z), ha='center', va='center')

plt.show()

rawDescription: Show the values in the grid using matplotlib


code: df.approxQuantile("x", [0.5], 0.25)

rawDescription: How to find median using Spark

description: Find median using Spark


code:
import numpy as np
import matplotlib.pyplot as plt
from numpy.random import *

def get_text_positions(x_data, y_data, txt_width, txt_height):
    a = zip(y_data, x_data)
    text_positions = y_data.copy()
    for index, (y, x) in enumerate(a):
        local_text_positions = [i for i in a if i[0] > (y - txt_height)
                            and (abs(i[1] - x) < txt_width * 2) and i != (y,x)]
        if local_text_positions:
            sorted_ltp = sorted(local_text_positions)
            if abs(sorted_ltp[0][0] - y) < txt_height: #True == collision
                differ = np.diff(sorted_ltp, axis=0)
                a[index] = (sorted_ltp[-1][0] + txt_height, a[index][1])
                text_positions[index] = sorted_ltp[-1][0] + txt_height
                for k, (j, m) in enumerate(differ):
                    #j is the vertical distance between words
                    if j > txt_height * 2: #if True then room to fit a word in
                        a[index] = (sorted_ltp[k][0] + txt_height, a[index][1])
                        text_positions[index] = sorted_ltp[k][0] + txt_height
                        break
    return text_positions

def text_plotter(x_data, y_data, text_positions, axis,txt_width,txt_height):
    for x,y,t in zip(x_data, y_data, text_positions):
        axis.text(x - txt_width, 1.01*t, '%d'%int(y),rotation=0, color='blue')
        if y != t:
            axis.arrow(x, t,0,y-t, color='red',alpha=0.3, width=txt_width*0.1,
                       head_width=txt_width, head_length=txt_height*0.5,
                       zorder=0,length_includes_head=True)

rawDescription: Matplotlib overlapping annotations

description: Matplotlib overlapping annotations


rawDescription: How does one insert a key value pair into a python list?

description: Insert a key value pair into a python list


code: pointDistFunc(p1, p2) == distFunc(p1.x, p1.y, p2.x, p2.y)

rawDescription: Python Effecient Wrappers

description: Python Effecient Wrappers


code:
import foo

class fubar(object):
   #code

   def method(self, obj):
       #more stuff
       foo.show(obj)

rawDescription: organising classes and modules in python

description: Organising classes and modules in python


code:
import roboclaw
x = CopyScope(roboclaw)

x.crc_clear()
x.crc_update(4)

print(x._crc)
print(roboclaw._crc) #this will actually raise an error because it isn't defined in the original module yet.

rawDescription: Encapsulate the scope of an imported module


code:
from django.shortcuts import get_object_or_404, render

def theview(request, category_id=None, post_id=None):
    categories = Category.objects.all()
    posts = None
    selected_cat = None
    selected_post = None
    if category_id:
        selected_cat = get_object_or_404(Category, pk=category_id)
        posts = selected_cat.post_set.all()
        if post_id:
            selected_post = posts.filter(pk=post_id)

    return render(
        request,
        'template.html',
        {
            'categories': categories,
            'selected_cat': selected_cat,
            'posts': posts,
            'selected_post': selected_post
        }
    )

rawDescription: Multiple Django Model Queries Changing Independently in One View


code:
real_stream['depth'] = real_stream.event.cumsum()
real_stream

rawDescription: pandas window functions: accessing to intermediate results

description: Pandas window functions: accessing to intermediate results


code:
TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.auth.context_processors.auth",
                               "django.core.context_processors.debug",
                               "django.core.context_processors.i18n",
                               "django.core.context_processors.media",
                               "django.core.context_processors.static",
                               "django.core.context_processors.tz",
                               "django.contrib.messages.context_processors.messages",
                               "django.core.context_processors.request",
                               "app_name.context_processors.debug_settings",)

rawDescription: Django Templates Development vs. Production


code:
def getGroups (iterable):
    group = []
    for item in iterable:
        group.append(item)
        if len(group) == 3 and group[2] == 'c':
            yield group
            group = []
        elif len(group) == 4 and group[2] == 'd':
            yield group
            group = []

for group in getGroups('abcabcabdcabcabdcabcabc'):
    print(group)

rawDescription: Python irregular loop

description: Python irregular loop


rawDescription: Pandas DataFrame column values in to list


rawDescription: Pandas DataFrame performance

description: Pandas DataFrame performance


code:
from SimpleXMLRPCServer import SimpleXMLRPCServer

# for demonstration purposes, just return an ever increasing integer
score = 0
def get_scores():
    global score
    score += 1
    return score

# create server, register get_scores function
server = SimpleXMLRPCServer(("localhost", 8000))
server.register_function(get_scores, "get_scores")

# start the server
server.serve_forever()

rawDescription: How to create a simple network connection in Python?

description: Create a simple network connection in Python


rawDescription: Creating a table with all possible variations of observations


code:
def filter_to_string(value):
    if value is None:
        return ''
    return str(value)

rawDescription: Cheetah template filters

description: Cheetah template filters


code:
with open('test', 'w') as f:
    f.write('hello world')

rawDescription: Python3x - Write a python script to run other python scripts?


code: vhat = exp( 0.5j * kx**2 * t) * uhat

rawDescription: Error in RK4 algorithm in Python

description: Error in RK4 algorithm in Python


rawDescription: pythonic way to customize classes

description: Pythonic way to customize classes


code: ces = odeint(rhs, init_cond, t)

rawDescription: Solve a ODE for arrays of parameters (Python)


code:
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest

manager = Manager()
request = QNetworkRequest(QUrl("http://www.google.com/"))
request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1")
manager.get(request)

rawDescription: what is the Default user-Agent of PyQt Web kit Qwebview and how to get it

description: What is the Default user-Agent of PyQt Web kit Qwebview and how to get it


code:
import csv
import glob
import os

directory = raw_input("INPUT Folder for Log Dump Files:")
output = raw_input("OUTPUT Folder for .csv files:")

txt_files = os.path.join(directory, '*.txt')

delimiter = ':'
for txt_file in glob.glob(txt_files):
    SavingList = []

    with open(txt_file, 'r') as text:
            for line in text:
                if line == 'test=delimiter=here\n':
                    delimiter = '='
                elif line == 'does-it-work\n':
                    delimiter = '-'
                elif line == "I'm:Not:Sure":
                    delimiter = ':'

                SavingList.append(line.split(delimiter))

    with open('%s.csv' %os.path.join(output, txt_file.split('.')[0]), 'wb') as output_file:
            writer = csv.writer(output_file)
            for m in xrange(len(SavingList)):
                writer.writerow(SavingList[m])

rawDescription: Text file to csv with glob. Need to change delimiter depending on section of file being read


code:
x = ['a', 'b', 'c', 'd', 'e', 'f']
xdict = dict(enumerate(x))

rawDescription: Show string values on x-axis in pyqtgraph


code:
from lib7 import *
from lib8 import *
from lib8 import *

rawDescription: Python : Import modules once then share with several files


code:
category
cat1         [23, 98]
cat2        [65, 798]
dtype: object

rawDescription: Python, use "order by" inside a "group concat" with pandas DataFrame


code:
def get_next_peak():
    return float(raw_input("Where is the next large peak?: "))

next_peak = []
next_peak.append(get_next_peak())
next_peak.append(get_next_peak())
next_peak.append(next_peak[1] - next_peak[0])

rawDescription: Subtracting numbers from inputted array

description: Subtracting numbers from inputted array


rawDescription: Avoiding TypeError when using timedelta to compare times


code: yourUniqueFieldName = models.TextField(max_length=100, unique=True)

rawDescription: Create Django Objects outside of Admin

description: Create Django Objects outside of Admin


rawDescription: Reverse and split string: Python

description: Reverse and split string: Python


code:
filled_1 = False
filled_2 = True
filled_3 = False
filled_4 = False

rawDescription: pygame circle not changing

description: Pygame circle not changing


code:
class MyClass(object):
    def __init__(self):
            pass
    def __A(self):
            print('Method __A()')

a=MyClass()
a
a._MyClass__A()

rawDescription: Testing Private Methods in Python (An Exception)


rawDescription: How to remove a column in a numpy array?

description: Remove a column in a numpy array


code:
msg = receiveFromSocket()
list_received = json.loads(msg)

rawDescription: Sending multiple lists (list of lists) over a network


code:
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType

class UserProfileOne(models.Model):
    pass

class UserProfileTwo(models.Model):
    pass

class UserProfile(models.Model):
    content_type    = models.ForeignKey(ContentType)
    object_id       = models.PositiveIntegerField(db_index=True)
    content_object  = generic.GenericForeignKey('content_type', 'object_id')

rawDescription: django how to use AUTH_PROFILE_MODULE with multiple profiles?

description: Django how to use AUTH_PROFILE_MODULE with multiple profiles


code:
from HTMLParser import HTMLParser

class MLStripper(HTMLParser):
    def __init__(self):
        self.reset()
        self.fed = []
    def handle_data(self, d):
        self.fed.append(d)
    def get_data(self):
        return ''.join(self.fed)

def strip_tags(html):
    s = MLStripper()
    s.feed(html)
    return s.get_data()

rawDescription: Strip HTML from strings in Python

description: Strip HTML from strings in Python


rawDescription: Fastest way to read every n-th row with numpy's genfromtxt


code:
ONE_OR_MULTIPLE_CHOICES = (
    (0, 'One'),
    (1, 'Multiple')
)

multiple = forms.TypedChoiceField(choices=ONE_OR_MULTIPLE_CHOICES,
                                  widget=forms.RadioSelect,
                                  coerce=int)

rawDescription: Django form-wizard typechoicefield returning string instead of boolean


code:
def make_callback(m_type, other_type):
    def callback(msg):
        m = m_type(msg)
        a = other_type(m)
        pub2.publish(a)
    return callback

callback1 = make_callback(MsgX, msg_type_a)
callback2 = make_callback(MsgY, msg_type_b)

rawDescription: How to avoid similar function definitions in Python?

description: Avoid similar function definitions in Python


code: target.write(' '.join(line1, linebreak, line2, linebreak, line3, linebreak))

rawDescription: Total Python Noob: why doesn't this work?


code:
df.pivot_table(index='Date', columns='Currency',
               values='Amount', aggfunc='sum', fill_value=0)

rawDescription: Make row value into column and sum for DataFrame


code:
class SimpleGenerator(object):
    @classmethod
    def get_description(cls):
        return cls.__name__

class AdvancedGenerator(SimpleGenerator):
    @classmethod
    def get_description(cls):
        desc = SimpleGenerator.get_description()
        return desc + ' Advanced(tm)'

AdvancedGenerator.get_description()

rawDescription: How to call a parent class's @classmethod from an overridden @classmethod in Python?

description: Call a parent class's @classmethod from an overridden @classmethod in Python


code: from django.db import models

rawDescription: Django Models: Where is Model class?

description: Django Models: Where is Model class


code:
s1[s1.notnull()]
s1[s1.notnull()] == s2[s2.notnull()]

rawDescription: Comparing pandas.Series for equality when they contain nan?


code:
import threading
import webbrowser
from wsgiref.simple_server import make_server

FILE = 'frontend.html'
PORT = 8080

def test_app(environ, start_response):
    if environ['REQUEST_METHOD'] == 'POST':
        try:
            request_body_size = int(environ['CONTENT_LENGTH'])
            request_body = environ['wsgi.input'].read(request_body_size)
        except (TypeError, ValueError):
            request_body = "0"
        try:
            response_body = str(int(request_body) ** 2)
        except:
            response_body = "error"
        status = '200 OK'
        headers = [('Content-type', 'text/plain')]
        start_response(status, headers)
        return [response_body]
    else:
        response_body = open(FILE).read()
        status = '200 OK'
        headers = [('Content-type', 'text/html'),
                   ('Content-Length', str(len(response_body)))]
        start_response(status, headers)
        return [response_body]

def open_browser():
    """Start a browser after waiting for half a second."""
    def _open_browser():
        webbrowser.open('http://localhost:%s/%s' % (PORT, FILE))
    thread = threading.Timer(0.5, _open_browser)
    thread.start()

def start_server():
    """Start the server."""
    httpd = make_server("", PORT, test_app)
    httpd.serve_forever()

if __name__ == "__main__":
    open_browser()
    start_server()

rawDescription: How to implement a minimal server for AJAX in Python?

description: Implement a minimal server for AJAX in Python


rawDescription: urllib2 file name

description: Urllib2 file name


code:
class A(object):
    def a(self):
       pass
    b = a

Aobj = A()
Aobj.a.__name__
Aobj.b.__name__

rawDescription: Get name of the bound method from instance of the bound method object in Python


rawDescription: Getting attributes from child class in many_to_many fields


code:
def process_range(id_range, store=None):
    """process a number of ids, storing the results in a dict"""
    if store is None:
        store = {}
    for id in id_range:
        store[id] = process_id(id)
    return store

rawDescription: Making multiple API calls in parallel using Python (IPython)


code: ''.join([c for c in string1 if c in string2])

rawDescription: Characters in Strings in Python

description: Characters in Strings in Python


rawDescription: Attributes of Python module `this`

description: Attributes of Python module `this`


code: g3 = reverse_graph(g)

rawDescription: Overhead in networkx reverse function?

description: Overhead in networkx reverse function


code:
import json
from google.appengine.api import urlfetch

# ...

class MainPage(webapp2.RequestHandler):
    def get(self):
        # ...
        for photo in data['photos']['photo']:
            farm_id = 1 # ???
            server_id = photo['server']
            photo_id = photo['id']
            secret = photo['secret']
            # ...

def _doget(method, auth=False, **params):
    # ...
    url = '%s%s/?api_key=%s&method=%s&%s&format=json'% \
      (HOST, API, API_KEY, method, urlencode(params))

    res = urlfetch.fetch(url).content

    # Flickr JSON api returns not valid JSON, which wrapped with "jsonFlickrApi(...)", so we get rid of it.
    if 'jsonFlickrApi(' in res:
        return res[14:-1]

    return json.loads(res)

rawDescription: Error while importing Flickr api using Python on Google App Engine


code:
df1=pd.DataFrame( np.random.randn(3,3), index=list('ABC'), columns=list('123') )
df2=pd.DataFrame( np.random.randn(3,3), index=list('DEF'), columns=list('123') )
df3=pd.DataFrame( np.random.randn(3,3), index=list('ABC'), columns=list('456') )
df4=pd.DataFrame( np.random.randn(3,3), index=list('DEF'), columns=list('456') )

rawDescription: Combining pandas data frames with overlapping columns / rows


code:
myStr.replace(","," ")
myStr.replace("'"," ")
myStr.replace("-","")
myStr.replace("_","")
myStr.replace("["," ")
myStr.replace("]"," ")

rawDescription: Is a Regular Expression Faster than using .replace()


code:
res = requests.get(URL, headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36',
})

rawDescription: How to start a query from a static website?

description: Start a query from a static website


code:
a = ['C:\\Mon20412\\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
    'C:\\Mon25312\\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx',
    'C:\\Mon20362\\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx']

def k(a):
    x = a.split("\\")[-1].split("-")[3]
    y = filter(lambda x: x in "0123456789", x)
    return int("".join(list(y)))

print(sorted(a, key=k))

rawDescription: Sort a python list of strings with a numeric number


code:
from django.conf.urls import patterns, url
from myapp import views

urlpatterns = patterns('',
    # your other normal url patterns
    url(r'^(?P<itemnum>\d+)/$', views.confirm, name='confirm-page'),
)

rawDescription: Importing a class' primary key in to another file's definition using Django


code:
s
df
df = df.reindex(np.zeros(len(s)))
df
df['new_column'] = s.values
df

rawDescription: Adding a Pandas Dataframe-column to a new dataframe


rawDescription: How to bucket a time stamp in 5 minute intervals in python

description: Bucket a time stamp in 5 minute intervals in python


rawDescription: Convert a date string into YYYYMMDD

description: Convert a date string into YYYYMMDD


rawDescription: Numpy using where with varying array sizes


rawDescription: Django database shouldn't accept a null value


code:
method = someobj.methodname

if some_condition:
     method()

rawDescription: See what method was called on an object

description: See what method was called on an object


code:
while True:
    try:
        inp = input('Voer een door spatie gescheiden lijst met getallen in:').split()
        a, b  = map(int, inp)
        break
    except ValueError:
        print("Invalid input")
print(a,b)

rawDescription: python string input to integer conversion

description: Python string input to integer conversion


code:
from dirBar import *
import unittest

if __name__ == "__main__":

    unittest.main()

rawDescription: Python: How to run unittest.main() for all source files in a subdirectory?


code:
@patch('do_log')
def controller(handler, mock_do_log):
    mock_do_log.side_effect = lambda *args,**kwargs:my_new_do_log(handler,*args,**kwargs)
    myfunction()

rawDescription: Add extra arguments to side_effects in python Mock


code:
from django.test import Client
c = Client()
response = c.post('/login/', {'username': 'abc', 'password': '1234'})
response.status_code
response = c.get('/user/protected-page/')
response.content

rawDescription: How to test that a view is only accessed by staff users in Django

description: Test that a view is only accessed by staff users in Django


rawDescription: Fastest way to replace elements of a file's path and extension?


code:
cmds.editDisplayLayerMembers('Torso_Control_Shapes', object1, object2)
# where object1 and object2 are strings containing the names of your objects

rawDescription: Place object within a layer in Python - Maya


code:
import numpy as np
import numba as nb
import math

def rownowaga(u, v):
    wymiar_x = len(u)
    wymiar_y = len(u[1])
    f = [[[0 for j in range(wymiar_y)] for i in range(wymiar_x)] for k in range(9)]
    cx = [0., 1., 0., -1., 0., 1., -1., -1., 1.]
    cy = [0., 0., 1., 0., -1., 1., 1., -1., -1.]
    w = [4./9, 1./9, 1./9, 1./9, 1./9, 1./36, 1./36, 1./36, 1./36]
    for i in range( wymiar_x):
        for j in range (wymiar_y):
            for k in range(9):
                up = u[i][j]
                vp = v[i][j]
                udot = (up**2 + vp**2)
                cu = up*cx[k] + vp*cy[k]
                f[k][i][j] =  w[k] + w[k]*(3.0*cu + 4.5*cu**2 - 1.5*udot)
    return f

# Pull these out so that numba treats them as constant arrays
cx = np.array([0., 1., 0., -1., 0., 1., -1., -1., 1.])
cy = np.array([0., 0., 1., 0., -1., 1., 1., -1., -1.])
w = np.array([4./9, 1./9, 1./9, 1./9, 1./9, 1./36, 1./36, 1./36, 1./36])

@nb.jit(nopython=True)
def rownowaga_numba(u, v):
    wymiar_x = u.shape[0]
    wymiar_y = u[1].shape[0]
    f = np.zeros((9, wymiar_x, wymiar_y))

    for i in xrange( wymiar_x):
        for j in xrange (wymiar_y):
            for k in xrange(9):
                up = u[i,j]
                vp = v[i,j]
                udot = (up*up + vp*vp)
                cu = up*cx[k] + vp*cy[k]
                f[k,i,j] =  w[k] + w[k]*(3.0*cu + 4.5*cu**2 - 1.5*udot)
    return f

rawDescription: Numba : cell vars are not supported

description: Numba : cell vars are not supported


code: '/p:sld/p:cSld/p:spTree/p:sp/p:nvSpPr/p:nvPr/p:ph[@type="body" and  @sz="quarter"  and  @marL]'

rawDescription: hasAttribute in lxml?

description: HasAttribute in lxml


code:
from django.db import connection

query = str(ModelToRetrive.objects.all().query)
df = pandas.read_sql_query(query, connection)

rawDescription: How to read sql query to pandas dataframe / python / django

description: Read sql query to pandas dataframe / python / django


code:
s = "5-9-81-15"          # String to parse
lb = [1, 1, 76, 13]      # List of lower bounds
ub = [17, 37, 89, 27]    # List of upper bounds
vals = [int(x) for x in s.split('-')]   # vals contains [5, 9, 81, 15]
all(x > L and x < U for x, L, U in zip(vals, lb, ub))    # does the check

rawDescription: Using Python re.search to find range of numbers


code:
from itertools import count
from collections import defaultdict

counts = defaultdict(lambda c=count(): next(c))

aa = zip(*((counts[cell] for cell in col) for col in zip(*a)))

rawDescription: How to map a unique id (counting from 0) to items in sublists

description: Map a unique id (counting from 0) to items in sublists


rawDescription: What is subplot_adjust() doing to pyplot axes?


code:
allocations = product(*strategies.values())
for alloc in allocations:
    PWC[alloc] = [a for (a,b) in zip(help,alloc) if coalitions[a] >= sum(b)]

    # check whether you can stop looking at more values from the cartesian product
    if someCondition(alloc):
        break

rawDescription: Manual product with unknown number of arguments


code:
def pass1(A, B):
    nrow,ncol=A.shape
    Aptr=A.indptr
    Bptr=B.indptr
    Cp=np.zeros(nrow+1,int)
    mask=np.zeros(ncol,int)-1
    nnz=0
    for i in range(nrow):
        row_nnz=0
        for jj in range(Aptr[i],Aptr[i+1]):
            j=A.indices[jj]
            for kk in range(Bptr[j],Bptr[j+1]):
                k=B.indices[kk]
                if mask[k]!=i:
                    mask[k]=i
                    row_nnz += 1
        nnz += row_nnz
        Cp[i+1]=nnz
    return Cp

def pass2(A,B,Cnnz):
    nrow,ncol=A.shape
    Ap,Aj,Ax=A.indptr,A.indices,A.data
    Bp,Bj,Bx=B.indptr,B.indices,B.data

    next = np.zeros(ncol,int)-1
    sums = np.zeros(ncol,A.dtype)

    Cp=np.zeros(nrow+1,int)
    Cj=np.zeros(Cnnz,int)
    Cx=np.zeros(Cnnz,A.dtype)
    nnz = 0
    for i in range(nrow):
        head = -2
        length = 0
        for jj in range(Ap[i], Ap[i+1]):
            j, v = Aj[jj], Ax[jj]
            for kk in range(Bp[j], Bp[j+1]):
                k = Bj[kk]
                sums[k] += v*Bx[kk]
                if (next[k]==-1):
                    next[k], head=head, k
                    length += 1
        print(i,sums, next)
        for _ in range(length):
            if sums[head] !=0:
                Cj[nnz], Cx[nnz] = head, sums[head]
                nnz += 1
            temp = head
            head = next[head]
            next[temp], sums[temp] = -1, 0
        Cp[i+1] = nnz
    return Cp, Cj, Cx

def pass0(A,B):
    Cp = pass1(A,B)
    nnz = Cp[-1]
    Cp,Cj,Cx=pass2(A,B,nnz)
    N,M=A.shape[0], B.shape[1]
    C=sparse.csr_matrix((Cx, Cj, Cp), shape=(N,M))
    return C

rawDescription: Directly use Intel mkl library on Scipy sparse matrix to calculate A dot A.T with less memory


code:
val = 1
answer = [[i,j] for i,sub in enumerate(testlist) for j,num in enumerate(sub) if num==val]

rawDescription: Print Location of Value within a list of lists Python


code:
def mysplit(df):
    parts = np.split(df, np.where(np.isnan(df.value))[0])
    # removing NaN entries
    parts = [part[~np.isnan(part.value)] for part in parts
              if not isinstance(part, np.ndarray)]
    # removing empty DataFrames
    parts = [part for part in parts if not part.empty]
    return parts

rawDescription: Pandas plotting lines from different columns ignoring values


code:
root = Tk()

# derived width and height
width = 400
height = 40

# blank photo image
image = PhotoImage()

Label(root, image=image, compound='center', text='Hello World',
      width=width, height=height, bg='white').pack()

mainloop()

rawDescription: How to derive the font size from a height constraint?

description: Derive the font size from a height constraint


code: list_a, list_b, list_c = [get_value(x) for x in (1, 2, 3)]

rawDescription: Working with tuples in python?

description: Working with tuples in python


code:
class Guide(models.Model):
    name = models.CharField(max_length=50)
    sno = models.CharField(max_length=50)
    date_created = models.DateTimeField(auto_now_add=True, blank=True)
    date_modified = models.DateTimeField(auto_now=True, blank=True)

    def __unicode__(self):
        return unicode(self.name)

rawDescription: How to make django crispy form to hide a particular field?

description: Make django crispy form to hide a particular field


code:
import csv

new_rows = []

with open('input.csv', 'rb') as inf:
    with open('output.csv', 'wb') as outf:
        reader = csv.reader(inf)
        writer = csv.writer(outf)
        for row in reader:
            new_row = [ field.rstrip('[]') for field in row ]
            writer.writerow(new_row)

rawDescription: Strip all trailing occurrences of a character


rawDescription: Can I sort text by its numeric value in Python?

description: Sort text by its numeric value in Python


code: GoogleMaps.geocode(query, sensor='false', oe='utf8', ll='', spn='', gl='')

rawDescription: Retrieving the US Postal Zip code for a street address using Python


code:
def make_square(jagged):
    # Careful: this mutates the series list of list
    max_cols = max(map(len, jagged))
    for row in jagged:
        row.extend([None] * (max_cols - len(row)))
    return np.array(jagged, dtype=np.float)

make_square(series)
array([[  1.,   2.,   3.,   4.,  nan,  nan,  nan,  nan],
       [  1.,   2.,   3.,  nan,  nan,  nan,  nan,  nan],
       [  1.,  nan,  nan,  nan,  nan,  nan,  nan,  nan],
       [  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.]])

rawDescription: Does KNeighborsClassifier compare lists with different sizes?


code:
daysofweek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
hours = {}
for day in daysofweek:
    hour = -1
    while hour < 0 or hour > 24:
        hour = input("Enter the number of hours for week 1 " + day + ": ")
    hours[day] = hour

rawDescription: Python: nested loops not behaving how I want them to


rawDescription: Pythonic way to eval all octal values in a string as integers


code:
import copy

def resolve(arr):
    # create an index for easy access of the urls
    index = {item[0]: item[2] for item in arr}
    # copy the index
    mapping = copy.copy(index)

    # loop through the index
    for index_key in index:
        # get the current value
        value = index[index_key]
        # loop through the mapping as long as the final ip address is reached
        # but only if this url wasn't found before
        while value in mapping:
            # remember the new key (so it can be deleted afterwards)
            key = value
            # get the new value
            value = mapping[key]
            # save the found value as the new value (for later use)
            # this reduces the complexity (-> better performance)
            mapping[index_key] = value
            # delete the "one in the middle" out of the mapping array
            # so that the next item don't have to search for
            # the correct mapping (because the mapping has been found already)
            del mapping[key]

    return mapping

rawDescription: Grouping text lines so if A=B and B=C, then A=C


code: form = UpdateWidgetForm(request.form)

rawDescription: Multiple WTForm fields from same form class in HTML table?