code:
import collections

scan0 = collections.defaultdict(set, {
    '172.16.0.41': set([(80, 'tcp'), (666, 'tcp')]),
    '172.16.0.163': set([(80, 'tcp'), (22, 'tcp')])
})

scan1 = collections.defaultdict(set, {
    '172.16.0.41': set([(80, 'tcp'), (22, 'tcp')]),
    '172.16.0.163': set([(80, 'tcp'), (22, 'tcp')])
})

hosts = sorted(set(scan0.keys() + scan1.keys()))

scan_same = dict()
scan_new = dict()
scan_del = dict()

for host in hosts:
    scan_same[host] = scan0[host] & scan1[host]
    scan_new[host] = scan1[host] - scan0[host]
    scan_del[host] = scan0[host] - scan1[host]

print()
print('-' * 10, 'Same')
for host, ports in scan_same.items():
    print(host, ':')
    for port in ports:
        print(':::', port[0], '/', port[1])
print()
print('*' * 10, 'Added')
for host, ports in scan_new.items():
    print(host, ':')
    for port in ports:
        print(':::', port[0], '/', port[1])

print()
print('=' * 10, 'Deleted')
for host, ports in scan_del.items():
    print(host, ':')
    for port in ports:
        print(':::', port[0], '/', port[1])

rawDescription: How to compare dictionaries and see what changed?

description: Compare dictionaries and see what changed


code:
def __init__(self, name, salary):
    self.name = name
    self.salary = salary

rawDescription: "For each attribute, give the class it is."


code:
import numpy as np
arr = np.array([(1490775.0, 12037425.0)], dtype=[('foo','<f8'),('bar','<f8')])
arr.flags.writeable = False

G = dict()
G[arr[0]] = 0

print(type(G.keys()[0]))
# <type 'numpy.void'>

print(type(G.keys()[0][0]))
# <type 'numpy.float64'>

print(type(G.keys()[0][1]))
# <type 'numpy.float64'>

print(type(G))
# <type 'dict'>

rawDescription: Dictionary not recognizing floating point keys


rawDescription: What's the best way to retrieve the module path from the project in python?


code:
li = ['Sensor','0x024']

for i in li:
    print (df.loc[df['title'].str.contains(i, case=False),'id'].tolist())
    [3, 4]
    [1, 4]

rawDescription: How to find duplicate words in a line using pandas?

description: Find duplicate words in a line using pandas


code:
for x, hay in enumerate(haystack):
  if needle in hay:
    return x

rawDescription: Python finding a value in a list of lists


code:
class A(object):
    def set(self, value):
        print(value + 1)
    def set2(self, value):
        A.set(self, value)

rawDescription: Python method override rule

description: Python method override rule


rawDescription: rsync --delete --files-from=list / dest/ does not delete unwanted files

description: Rsync --delete --files-from=list / dest/ does not delete unwanted files


code:
route1 = Blueprint('route1', __name__, template_folder='views')

@route1.route('/get/as_text',methods=['GET'])
def get_as_text_route():

    return json.jsonify(data="my data")

rawDescription: Modular routes in python flask

description: Modular routes in python flask


rawDescription: Python, neighbors on a regular grid

description: Python, neighbors on a regular grid


code:
from collections import OrderedDict
pages = {page_id: OrderedDict() for page_id in pages}
for supporter in combined_supporters:
    page_id = supporter.pop('page_id')
    pages[page_id][supporter['profile_id']] = supporter

from pprint import pprint
pprint(pages)

rawDescription: Re-mapping a sorted list into a dictionary


code:
idx = r[:,0] > 0 # idx is a bool array
r1 = r[idx]
v1 = v[idx]

r2 = r[~idx] # ~idx compute bit-wise NOT, element-wise
v2 = v[~idx]

rawDescription: How to efficiently split a pair of arrays based on a condition on one of them

description: Efficiently split a pair of arrays based on a condition on one of them


code:
for book in Book.objects.all().iterator():
    Book.objects.filter(pk=book.pk).update(activity=book.calculate_activity())

rawDescription: Iterating over a large Django queryset while the data is changing elsewhere


code: gender = data.findtext("s", "?")

rawDescription: Chatango xml parsing in python

description: Chatango xml parsing in python


rawDescription: Merge Dictionaries with different TimeStamp


code:
action = {'newChannel': (newChannel, hname),
             'newNetwork': (newNetwork, cname) , 'loginError': (loginError, nName)}

handler, param = action.get(eventType)
handler(param)

rawDescription: Dispatch dictionary but pass different parameters to functions


code:
A = [['Yes', 'lala', 'No'], ['Yes', 'lala', 'Idontknow'], ['No', 'lala', 'Yes'], ['No', 'lala', 'Idontknow']]
any(i[0]=='Yes' and i[2] == 'No' for i in A)

rawDescription: How to use sets in Python to find list membership?

description: Use sets in Python to find list membership


code:
from time import sleep
for i in range(5):
    sleep(10)

rawDescription: Help adding threading for GUI progress

description: Help adding threading for GUI progress


code:
#!/usr/bin/env python
from collections import Counter
from itertools import izip_longest

strings = ['foo_a','foo_b','foo_c','fnord']
threshold = .7 * len(strings)
prefix = []
for chars in izip_longest(*strings, fillvalue=''):
    char, count = Counter(chars).most_common(1)[0]
    if count < threshold:
        break
    prefix.append(char)
print(''.join(prefix))
# -> foo_

rawDescription: Find *most* common prefix of strings - a better way?


code:
def __lt__(self, other):
    selfPriority = (self.priority, self.pid)
    otherPriority = (other.priority, other.pid)
    return selfPriority < otherPriority

rawDescription: I notice I cannot use PriorityQueue for Objects?


code:
@app.route("/page")
def page():
    value = compute_value()
    return render_Template('page.html', value = value)

rawDescription: Update Javascript with results from Python script


code:
scores = {"A":1, "T":10, "J": 10, "K":10, "Q": 10, }
def calculate_hand(hand):
    hand_value = 0
    ace = False
    for card in hand:
        if card[0] == "A":
            ace =True;
        if card[:-1] in scores:  #used [:-1] insted of [0] because at first, thought it is 10 instead of T
            hand_value += scores[card[:-1]]
        else:
            hand_value += int(card[:-1])
    if ace and hand_value + 10 < 22:
        hand_value += 10
    return hand_value

hand1 = ['AD', 'AC']
hand2 = ['6C', 'JC']
hand3 = ['7D', 'AC']
value1 = calculate_hand(hand1) #12
value2 = calculate_hand(hand2) #16
value3 = calculate_hand(hand3) #18

rawDescription: Python Blackjack - Count Hand of Players


code:
def match(a, b):
  if len(a) == 0 or len(b) == 0:
    return []

  if a[0] == b[0]:
    return [a[0]] + match(a[1:], b[1:])

  return max(match(a, b[1:]), match(a[1:], b), key=len)

rawDescription: Pattern-matching algorithm for lists

description: Pattern-matching algorithm for lists


code:
import example
spam = example.Spam(42)
spam = example.Spam([42])

rawDescription: Boost Python iterable convertible masking non-iterable constructor


code:
def foo(**kwargs):
    for key,val in kwargs.iteritems():
        kwargs[key] = django.utils.html.escape(val)

    ...

rawDescription: HTML-escaping replacement fields when using str.format() in Python


code: logging.debug("something to store")

rawDescription: Google App Engine: traffic monitoring

description: Google App Engine: traffic monitoring


code:
o3 = O3()
o3.method
o3.method()
o3.var

rawDescription: Confused with getattribute and setattribute in python


code:
xpr = User.name.match("John Jacob Jingleheimer Schmidt").label("match_result")
q = session.query(User, xpr)

rawDescription: What's the equivalent of using `select()` with `text()` using the ORM?


code: from PyQt4 import QtCore, QtGui

rawDescription: Running python files

description: Running python files


code:
class callableList(list):
    def __call__(self, index):
        return self[index]

class foo(object):
    def __init__(self):
        self.x = callableList()

    @property
    def get_x(self):
        return self.x

rawDescription: Same name regular and property functions under the same class in python


code:
maxima = [float('-inf')] * 13
minima = [float('inf')] * 13
with open('names.csv') as input_file:
    for row in input_file:
        for col, value in row.split(','):
            value = float(value)
            maxima[col] = max(maxima[col], value)
            minima[col] = min(minima[col], value)

# This gets the value you called ``l``
combined_max_and_min = zip(maxima, minima)

rawDescription: nested for loop in python not working

description: Nested for loop in python not working


code:
class C:

    def __init__(self):
        self.data = np.zeros((100,))
        self.capacity = 100
        self.size = 0

    def update(self, row):
        for r in row:
            self.add(r)

    def add(self, x):
        if self.size == self.capacity:
            self.capacity *= 4
            newdata = np.zeros((self.capacity,))
            newdata[:self.size] = self.data
            self.data = newdata

        self.data[self.size] = x
        self.size += 1

    def finalize(self):
        data = self.data[:self.size]
        return np.reshape(data, newshape=(len(data)/5, 5))

rawDescription: Fastest way to grow a numpy numeric array


code:
import subprocess
import csv

p_tasklist = subprocess.Popen('tasklist.exe /fo csv',
                              stdout=subprocess.PIPE,
                              universal_newlines=True)

pythons_tasklist = []
for p in csv.DictReader(p_tasklist.stdout):
    if p['Image Name'] == 'python.exe':
        pythons_tasklist.append(p)

rawDescription: How to detect if a process is running using Python on Win and MAC

description: Detect if a process is running using Python on Win and MAC


code:
import pandas as pd
df = pd.DataFrame({'Athlete': ['M.Smith', 'A.Moyet', 'C.Marconi', 'M.Smith'],
                   'Distance': ['400m', '400m', '800m', '800m'],
                   'Race': ['A', 'A', 'B', 'B'],
                   'Rank': [1, 2, 5, 3],
                   'Time': [48.57, 49.0, 104.12, 102.66]})

df['race'] = df.groupby('Athlete').cumcount()+1
result = df.set_index(['Athlete', 'race']).unstack('race')
result = result.sortlevel('race', axis='columns')
result.columns = ['{}#{}'.format(col, n) for col, n in result.columns]
print(result)

rawDescription: Pivot Pandas Dataframe with a Mix of Numeric and Text Fields


code:
import itertools as IT

first = {
    ('monkey',1): 130.0,
    ('dog',2): 123.0,
    ('cat', 3): 130.0,
    ('cat', 4): 130.0,
    ('mouse', 6): 100.0
    }

counter = 0
ordered = sorted(first, key = lambda k: (first[k], k[1], k[0]))

second = {}
for key, group in IT.groupby(ordered, first.__getitem__):
    # group = list(group)
    # print(key, group)
    # (100.0, [('mouse', 6)])
    # (123.0, [('dog', 2)])
    # (130.0, [('monkey', 1), ('cat', 3), ('cat', 4)])
    previous = None
    for name, num in group:
        if name != previous:
            counter += 1
        second[num] = counter
        previous = name

print(second)

rawDescription: Dictionary value sorting

description: Dictionary value sorting


rawDescription: How to refresh the window with Tkinter

description: Refresh the window with Tkinter


code:
for i, v in enumerate(l):
    if i in idx:
        l[i] = 0

rawDescription: Best way to replace values in a list based on many indexes

description: Replace values in a list based on many indexes


code:
somelst = [1, 2, 3, 4, 5, 6]
replace2inplace(somelst)
somelst

rawDescription: Python list: exchange every n-th value with the (n+1)th


code: "\t".join(str(o) for o in flatten(seq))

rawDescription: How to convert dumbo sequence file input to tab separated text

description: Convert dumbo sequence file input to tab separated text


code:
import urllib

def getWeather(city):
    ''' given city name or postal code,
        return dictionary with current weather conditions
    '''
    url = 'http://www.google.com/ig/api?weather='
    try:
        f = urllib.urlopen(url + urllib.quote(city))
    except:
        return "Error opening url"
    s = f.read().replace('\r','').replace('\n','')
    if '<problem' in s:
        return "Problem retreaving weather (invalid city?)"

    weather = s.split('</current_conditions>')[0]  \
               .split('<current_conditions>')[-1]  \
               .strip('</>')
    wdict = dict(i.split(' data="') for i in weather.split('"/><'))
    return wdict

rawDescription: How to implement python to find value between xml tags?

description: Implement python to find value between xml tags


code:
class Ball(object):
    def __init__(self, size, color, name):
        self.name = name
        self.ball_props = {'size': size, 'color': color}

list_of_balls = [Ball(10, 'red', 'Fred'), Ball(20, 'blue', 'Frank')]

the_ball = next(b for b in list_of_balls if b.name == sys.argv[1])
the_value = the_ball.ball_props[sys.argv[2]]

rawDescription: python getting a specific attribute from a list of objects

description: Python getting a specific attribute from a list of objects


code:
def downloadData(n,i,d):
    for name, id, data in zip(n,i,d):
        URL = "http://www.website.com/data/{}".format(name) #downloads the file from the website. The last part of the URL is the name
        r = requests.get(URL)
        with open("data/{}_{}_{}.csv".format(name, id, data), "wb") as code: #create the file in the format name_id_dataType
            code.write(r.content)

rawDescription: Run 3 variables at once in a python for loop.


code:
def dbconnexion(self):
        """ Pop up a new window if the credentials are the right ones
        """
        if self.dbuser.get()=="beueradj" and  self.dbpassword.get()=="begueradj":
            # Pop up the new interface if credentials are OK
            self.item_insertion_window()
        else:
            # Loop over the login interface if not
            self.initialize_user_interface()

rawDescription: how to connect to mysql database from gui made in python

description: Connect to mysql database from gui made in python


code:
Name1
Name2
Name3

rawDescription: Pygame: Writing Multiple Lines

description: Pygame: Writing Multiple Lines


code:
self.X509_new = self._lib.X509_new
self.X509_new.restype = ctypes.POINTER(X509_st)
self.X509_new.argtypes = []

rawDescription: Wrapper for libeay32.dll: how to import macro?


code:
def AllCountries(request):
    country_names = list(Countries.objects.values_list('country_name', flat=True))
    data = {'country_names': country_names}
    return HttpResponse(json.dumps(data), content_type="application/json")

rawDescription: How to get the values in django model field?

description: Get the values in django model field


code:
from HTMLParser import HTMLParser
parser = HTMLParser()
html_decoded_string = parser.unescape(html_encoded_string)

rawDescription: How can I use Python to replace HTML escape characters?

description: Use Python to replace HTML escape characters


rawDescription: image stack population is slow in numpy

description: Image stack population is slow in numpy


code:
def angles(angle, steps):
    return [ i * angle * 2 / (steps - 1) - angle for i in xrange(steps)]

rawDescription: How to write this elegantly in Python?

description: Write this elegantly in Python


code:
current_dict = {'corse': 378, 'cielo': 209, 'mute': 16}
print(current_dict)
def replace_value_with_definition(key_to_find, definition):
    for key in current_dict.keys():
        if key == key_to_find:
            current_dict[key] = definition

replace_value_with_definition('corse', 'Definition of "corse"')
print(current_dict)

rawDescription: Python dictionary replace values

description: Python dictionary replace values


code: [getattr(myclass, x) for x in dir(myclass) if isinstance(getattr(myclass, x) , int)]

rawDescription: Python 2.7: How to get the list of static variables in a class?


code: comptest('This is a compression test of a short sentence.')

rawDescription: Python - Compress Ascii String

description: Python - Compress Ascii String


code: avg_instance = create_avg(a_instance, b_instance, c_instance)

rawDescription: Django easy way to create model from a multiple models


code:
def getImages(self, img_list) :
    nb_img = len(img_list)
    i = 1.0
    for img in img_list :
        self.progress_interface.set_progress(i, nb_img)
        gtk.gdk.threads_enter()
        get_maps.download_image(img)
        gtk.gdk.threads_leave()
        i += 1.0
        yield True
    self.progress_interface.progress_bar.set_text("Les images sont à jour")
    self.progress_interface.progress_bar.set_fraction(1)
    thread.start_new_thread(self.ShowMap, ()) # Here
    yield False

rawDescription: Is there a way to continue only when gobject.idle_add() function terminate?


rawDescription: Select values from a set of arrays according to an array of permutations


code:
elm = tree.xpath('//*[@id="inhalt_fewo"]/div[15]/table[1]/tr['+month+']/td')[0]

print(elm.text_content())
print(elm.attrib.get("class"))

rawDescription: how to return the class of an element in xpath?

description: Return the class of an element in xpath


code:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtNetwork import *

PORTS = (9998, 9999)
PORT = 9999
SIZEOF_UINT32 = 4

class Form(QDialog):

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

        # Ititialize socket
        self.socket = QTcpSocket()

        # Initialize data IO variables
        self.nextBlockSize = 0
        self.request = None

        # Create widgets/layout
        self.browser = QTextBrowser()
        self.lineedit = QLineEdit("Enter text here, dummy")
        self.lineedit.selectAll()
        self.connectButton = QPushButton("Connect")
        self.connectButton.setEnabled(True)
        layout = QVBoxLayout()
        layout.addWidget(self.browser)
        layout.addWidget(self.lineedit)
        layout.addWidget(self.connectButton)
        self.setLayout(layout)
        self.lineedit.setFocus()

        # Signals and slots for line edit and connect button
        self.lineedit.returnPressed.connect(self.issueRequest)
        self.connectButton.clicked.connect(self.connectToServer)

        self.setWindowTitle("Client")
        # Signals and slots for networking
        self.socket.readyRead.connect(self.readFromServer)
        self.socket.disconnected.connect(self.serverHasStopped)
        self.connect(self.socket,
                     SIGNAL("error(QAbstractSocket::SocketError)"),
                     self.serverHasError)

    # Update GUI
    def updateUi(self, text):
        self.browser.append(text)

    # Create connection to server
    def connectToServer(self):
        self.connectButton.setEnabled(False)
        self.socket.connectToHost("localhost", PORT)

    def issueRequest(self):
        self.request = QByteArray()
        stream = QDataStream(self.request, QIODevice.WriteOnly)
        stream.setVersion(QDataStream.Qt_4_2)
        stream.writeUInt32(0)
        stream.writeQString(self.lineedit.text())
        stream.device().seek(0)
        stream.writeUInt32(self.request.size() - SIZEOF_UINT32)
        self.socket.write(self.request)
        self.nextBlockSize = 0
        self.request = None
        self.lineedit.setText("")

    def readFromServer(self):
        stream = QDataStream(self.socket)
        stream.setVersion(QDataStream.Qt_4_2)

        while True:
            if self.nextBlockSize == 0:
                if self.socket.bytesAvailable() < SIZEOF_UINT32:
                    break
                self.nextBlockSize = stream.readUInt32()
            if self.socket.bytesAvailable() < self.nextBlockSize:
                break
            textFromServer = stream.readQString()
            self.updateUi(textFromServer)
            self.nextBlockSize = 0

    def serverHasStopped(self):
        self.socket.close()
        self.connectButton.setEnabled(True)

    def serverHasError(self):
        self.updateUi("Error: {}".format(
                self.socket.errorString()))
        self.socket.close()
        self.connectButton.setEnabled(True)

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

rawDescription: PyQt QTcpServer: How to return data to multiple clients?


code:
def getStatus ():
    data={"HOUR_ON": HOUR_ON,
          "MINUTE_ON": MINUTE_ON,
          "HOUR_OFF": HOUR_OFF,
          "MINUTE_OFF": MINUTE_OFF}
    return json.dumps(data)

rawDescription: Parsing JSON string from python to Javascript


code:
def product_list(request):
   return render(request, 'template.html', {'objects': Product.objects.all()})

rawDescription: django queryet to print product and it's models

description: Django queryet to print product and it's models


code:
from django.http import JsonResponse

def index(request):
    modellinks = model_link.objects.values_list('links', flat=True)
    link = random.choice(modellinks)

    data = {
       'link': link
    }
    return JsonResponse(data)

rawDescription: Django load model database values using ajax


code:
import theano.tensor as T
from theano import function

x = T.dmatrix('x')
linmax = function([x], T.maximum(x,0))

rawDescription: Theano element wise maximum

description: Theano element wise maximum


code:
df["DateTime"] = pd.to_datetime(df["DateTime"]) # just in case
df = df.sort(["PersonID", "DateTime"])
df["Order"] = df.groupby("PersonID").cumcount() + 1
df

rawDescription: How can I do a sequential count based on column value and timestamp in pandas?

description: Do a sequential count based on column value and timestamp in pandas


rawDescription: Best way to make many logical comparisons in the if clause of a list comprehension in Python?

description: Make many logical comparisons in the if clause of a list comprehension in Python


code:
def bisect_iter(pred, i):
    i1, i2 = itertools.tee(i)
    return itertools.takewhile(pred, i1), itertools.dropwhile(pred, i2)

i1, i2 = bisect_iter(str.isalpha, iter(l))
list(i1)
list(i2)

rawDescription: how to split a list in two at the point where predicate is first False

description: Split a list in two at the point where predicate is first False


code:
df.loc[idx, 'A'] += df.loc[idx, 'B'] * df.loc[idx, 'C']
print(df)

rawDescription: boolean indexing that can produce a view to a large pandas dataframe?

description: Boolean indexing that can produce a view to a large pandas dataframe


code:
def render(template, **vars):
    # In practice this would render a template
    print(vars)

def index():
    title = 'A title'
    subject = 'A subject'
    render("index.html", **locals())

if __name__ == '__main__':
    index()

rawDescription: Is there any way to pass all variables in the current scope to Mako as a context?


code:
import xml.dom.minidom as dom
import random

edgexml = dom.parse('sampledata/tmp.xml')
MSO = [8, 9, 10]

edge_groups = []
passed = []
for idx, edge in enumerate(edgexml.getElementsByTagName('edge')):
    if edge in passed:
        continue

    partners = []
    for partner in edgexml.getElementsByTagName('edge')[idx:]:
        if partner.getAttribute('from') == edge.getAttribute('to') \
        and partner.getAttribute('to') == edge.getAttribute('from'):
            partners.append(partner)

    edge_groups.append([edge] + partners)
    passed.extend([edge] + partners)

for e in edge_groups:
    print('NEW EDGE GROUP')
    x = random.choice(MSO)
    for p in e:
        p.setAttribute('speed', x)
        print('  E from "%s" to "%s" at "%s"' % (p.getAttribute('from'), p.getAttribute('to'), x))

rawDescription: Pair items of a list depending on value

description: Pair items of a list depending on value


code:
def dict_sub(d, text):
  """ Replace in 'text' non-overlapping occurences of REs whose patterns are keys
  in dictionary 'd' by corresponding values (which must be constant strings: may
  have named backreferences but not numeric ones). The keys must not contain
  anonymous matching-groups.
  Returns the new string."""

  # Create a regular expression  from the dictionary keys
  regex = re.compile("|".join("(%s)" % k for k in d))
  # Facilitate lookup from group number to value
  lookup = dict((i+1, v) for i, v in enumerate(d.itervalues()))

  # For each match, find which group matched and expand its value
  return regex.sub(lambda mo: mo.expand(lookup[mo.lastindex]), text)

rawDescription: Can you pass a dictionary when replacing strings in Python?

description: Pass a dictionary when replacing strings in Python


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

def kalman_xy(x, P, measurement, R,
              motion = np.matrix('0. 0. 0. 0.').T,
              Q = np.matrix(np.eye(4))):
    """
    Parameters:
    x: initial state 4-tuple of location and velocity: (x0, x1, x0_dot, x1_dot)
    P: initial uncertainty convariance matrix
    measurement: observed position
    R: measurement noise
    motion: external motion added to state vector x
    Q: motion noise (same shape as P)
    """
    return kalman(x, P, measurement, R, motion, Q,
                  F = np.matrix('''
                      1. 0. 1. 0.;
                      0. 1. 0. 1.;
                      0. 0. 1. 0.;
                      0. 0. 0. 1.
                      '''),
                  H = np.matrix('''
                      1. 0. 0. 0.;
                      0. 1. 0. 0.'''))

def kalman(x, P, measurement, R, motion, Q, F, H):
    '''
    Parameters:
    x: initial state
    P: initial uncertainty convariance matrix
    measurement: observed position (same shape as H*x)
    R: measurement noise (same shape as H)
    motion: external motion added to state vector x
    Q: motion noise (same shape as P)
    F: next state function: x_prime = F*x
    H: measurement function: position = H*x

    Return: the updated and predicted new values for (x, P)

    See also http://en.wikipedia.org/wiki/Kalman_filter

    This version of kalman can be applied to many different situations by
    appropriately defining F and H
    '''
    # UPDATE x, P based on measurement m
    # distance between measured and current position-belief
    y = np.matrix(measurement).T - H * x
    S = H * P * H.T + R  # residual convariance
    K = P * H.T * S.I    # Kalman gain
    x = x + K*y
    I = np.matrix(np.eye(F.shape[0])) # identity matrix
    P = (I - K*H)*P

    # PREDICT x, P based on motion
    x = F*x + motion
    P = F*P*F.T + Q

    return x, P

def demo_kalman_xy():
    x = np.matrix('0. 0. 0. 0.').T
    P = np.matrix(np.eye(4))*1000 # initial uncertainty

    N = 20
    true_x = np.linspace(0.0, 10.0, N)
    true_y = true_x**2
    observed_x = true_x + 0.05*np.random.random(N)*true_x
    observed_y = true_y + 0.05*np.random.random(N)*true_y
    plt.plot(observed_x, observed_y, 'ro')
    result = []
    R = 0.01**2
    for meas in zip(observed_x, observed_y):
        x, P = kalman_xy(x, P, meas, R)
        result.append((x[:2]).tolist())
    kalman_x, kalman_y = zip(*result)
    plt.plot(kalman_x, kalman_y, 'g-')
    plt.show()

demo_kalman_xy()

rawDescription: kalman 2d filter in python

description: Kalman 2d filter in python


code:
def get_stuff():
    return 'a string', [1,2,3,5]

all_stuff = [6,7]
all_stuff.extend(get_stuff()[1])

rawDescription: How to append the second return value, directly to a list, in Python

description: Append the second return value, directly to a list, in Python


code:
from django.db.models import Count, Min

winner = User.objects.annotate(vote_count=Count('vote_set'))\
    .annotate(first_vote_time=Min('vote_set__timestamp'))\
    .order_by('-vote_count', 'first_vote_time')[0]

rawDescription: Complex Django model query

description: Complex Django model query


rawDescription: Mocked Class & Asserting Method Calls

description: Mocked Class & Asserting Method Calls


code:
for i in words:
    if i in "ATGC":
        final1 = final1 + i
print(final1)

rawDescription: How do I loop through a list?

description: Loop through a list


code: dic = [[a, b, set(c)] for a, b, c in dic]

rawDescription: How to reduce the number of for-loops for shortening running time in Python programming?

description: Reduce the number of for-loops for shortening running time in Python programming


code:
pydict = {"name":"Chateau des Tours Brouilly","code":"chateau-des-tours-brouilly-2009-1","region":"France > Burgundy > Beaujolais > Brouilly","winery":"Chateau Des Tours","winery_id":"chateau-des-tours","varietal":"Gamay","price":"14.98","vintage":"2009","type":"Red Wine","link":"http://www.snooth.com/wine/chateau-des-tours-brouilly-2009-1/","tags":"colorful, mauve, intense, purple, floral, violet, lively, rich, raspberry, berry","image":"http://ei.isnooth.com/wine/b/7/8/wine_6316762_search.jpeg","snoothrank":3,"available":1,"num_merchants":10,"num_reviews":1}
from pprint import pprint
pprint(pydict)

rawDescription: formatting string output with linebreaks and tabs in Python?

description: Formatting string output with linebreaks and tabs in Python


code:
import functools

def callonce(func):

    result = []

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        if not result:
            result.append(func(*args, **kwargs))
        return result[0]

    return wrapper

rawDescription: Function which computes once, caches the result, and returns from cache infinitely (Python)


code:
def extract_text(node):
    return ' '.join(t.strip() for t in node(text=True, recursive=False))

rawDescription: Find All text within 1 level in HTML using Beautiful Soup - Python


code:
p1 = Page1(self)
p2 = Page2(self)
self.pages = [ p1, p2 ]

rawDescription: Still struggling with class scope

description: Still struggling with class scope


code:
@app.route('/search', methods=['POST', 'GET'])
def search():
    error = True
    if request.method == 'POST':
        return request.form['urlsearch']
    else:
        br = Browser()
        r = br.open(request.args.get('urlsearch'))

        if r.code == 200:
            return br.links()
        else:
            return "Error loading page"

rawDescription: How can I set a code for users when they enter a valud URL or not with PYTHON/Flask?

description: Set a code for users when they enter a valud URL or not with PYTHON/Flask


code:
def collectAbilityUpgrades(s):
    d = {}
    for k, v in s:
        if k not in d:
            d[k] = []
        d[k].append(v)
    return d

rawDescription: Python JSON parse duplicate records

description: Python JSON parse duplicate records


rawDescription: wxpython run when textctrl changes

description: Wxpython run when textctrl changes


rawDescription: Reading in datetime pandas without having to convert


rawDescription: Python 2.7 how should I parse a dict object?


code:
def numVowels(file):
    count = 0
    with open(file) as opened_file:
        for content in opened_file:
            for char in content:
                if char.lower() in 'aeiou':
                    count += 1
    return count

rawDescription: Check for letter in text file python

description: Check for letter in text file python


code:
from google.appengine.ext import webapp

register = webapp.template.create_template_register()

def hash(h,key):
    if key in h:
        return h[key]
    else:
        return None

register.filter(hash)

rawDescription: Django Templates and variable attributes


code:
df
df.groupby("Column with duplicates", as_index=False).sum()

rawDescription: Merging Pandas DataFrames with the same column name


code:
from scipy import stats
import matplotlib.pyplot as plt
import numpy as np

x = np.array([15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240])
y = np.array([1, 0.9, 0.8, 0.7, 0.6, 0.55, 0.5, 0.45, 0.4, 0.35, 0.33, 0.31, 0.29, 0.27, 0.25, 0.23])

# get slope of your data
dif = np.diff(y) / np.diff(x)

# determine the change of the slope
difdif = np.diff(dif)

# define a threshold for the allowed change of the slope
threshold = 0.001

# get indices where the diff returns value larger than a threshold
indNZ = np.where(abs(difdif) > threshold)[0]

# this makes plotting easier and avoids a couple of if clauses
indNZ += 1
indNZ = np.append(indNZ, len(x))
indNZ = np.insert(indNZ, 0, 0)

# plot the data
plt.scatter(x, y)

for indi, ind in enumerate(indNZ):

    if ind < len(x):
        slope, intercept, r_value, p_value, std_err = stats.linregress(x[ind:indNZ[indi+1]], y[ind:indNZ[indi+1]])
        plt.plot(x[ind:indNZ[indi+1]], slope * x[ind:indNZ[indi+1]] + intercept)

plt.show()

rawDescription: How to minimize chi squared for 3 linear fits

description: Minimize chi squared for 3 linear fits


code:
#!/usr/bin/env python2
import os, sys
import optparse, optcomplete
from cStringIO import StringIO

if __name__ == '__main__':
    parser = optparse.OptionParser()

    parser.add_option('-s', '--simple', action='store_true',
                      help="Simple really simple option without argument.")

    parser.add_option('-o', '--output', action='store',
                      help="Option that requires an argument.")

    opt = parser.add_option('-p', '--script', action='store',
                            help="Option that takes python scripts args only.")
    opt.completer = optcomplete.RegexCompleter('.*\.py')

    # debug env variables
    sys.stderr.write("\ncalled with args: %s\n" % repr(sys.argv))
    for k, v in sorted(os.environ.iteritems()):
        sys.stderr.write("  %s: %s\n" % (k, v))

    # setup capturing the actions of `optcomplete.autocomplete`
    def fake_exit(i):
      sys.stderr.write("autocomplete tried to exit with status %d\n" % i)
    sys.stdout = StringIO()
    sys.exit = fake_exit

    # Support completion for the command-line of this script.
    optcomplete.autocomplete(parser, ['.*\.tar.*'])

    sys.stderr.write("autocomplete tried to write to STDOUT:\n")
    sys.stderr.write(sys.stdout.getvalue())
    sys.stderr.write("\n")

    opts, args = parser.parse_args()

rawDescription: How does argparse (and the deprecated optparse) respond to 'tab' keypress after python program name, in bash?


code: lambda self, val=method_value: val

rawDescription: python setattr for dynamic method creator with decorator

description: Python setattr for dynamic method creator with decorator


code: time = grp['Datetime'].diff() / np.timedelta64(1, 's')

rawDescription: pandas - apply time and space functions to groupby

description: Pandas - apply time and space functions to groupby


code:
def sites(x):
    ln = len(x)
    r = [int(float(ele)) + (i + 1) for i,ele in enumerate(x)]
    modulo = [r[j] % ln for j in range(ln)]
    return modulo

rawDescription: how to check if two numbers in a list are the same in Python

description: Check if two numbers in a list are the same in Python


code:
infile = pd.read_csv('d:\\Data\\processed.cleveland.data', header=None)
outfile = infile[~infile.apply(lambda x: x== '?', axis=1).any(axis=1)]
dataset = np.asarray(outfile)

rawDescription: Lost data in pre-processing data using Python, numpy and panda


rawDescription: Python - Sum 4D Array

description: Python - Sum 4D Array


rawDescription: How to use optional named parameters and *mulit_vars in same function call?

description: Use optional named parameters and *mulit_vars in same function call


code:
array = [ [int(s) for s in line.split()] for line in open('file') ]
for line in array:
    print('%08i %3.1f %3i %3i' % (line[0], sum(line[1:])/len(line[1:]), min(line[1:]), max(line[1:])))

rawDescription: Need to read from a file and add the elements and get avg in python 3.4


code:
t = [list(filter(lambda x: len(x) > 2, string)) for string in s]
t

rawDescription: Converting this filter statement to list comprehension


code:
c = Component()
n = Node()

c.positive = n # at this point c.positive.connect(n) thus
               # n.add_terminal(c.positive) is called
del c.positive # at this point positive is "disconnected"
               # from the node and vise-versa.
# or disconnect explicitly
c.positive.disconnect()

rawDescription: Python - Making a reference to an objects attribute


code:
fig = pl.figure()
ax = pl.subplot(111)
ax.bar(range(len(dates)), values)

rawDescription: How to plot a very simple bar chart (Python, Matplotlib) using input *.txt file?

description: Plot a very simple bar chart (Python, Matplotlib) using input *.txt file


rawDescription: How do you clone a class in Python?

description: Clone a class in Python


code:
my_args = ["foo", "bar", "baz"]
"Hello %s" % ', '.join(my_args)

rawDescription: python - How to format variable number of arguments into a string?

description: Python - How to format variable number of arguments into a string


code: {'grab the rss': 1, 'approach this': 1, 'in': 5}

rawDescription: Python matching n-grams from a dictionary to a string of text


code:
x = np.random.random((100000,))
x.sort()
y = np.random.random((100000,))

rawDescription: Optimized function to separate arrays into bins


rawDescription: create nested json from the ouput of two queries django

description: Create nested json from the ouput of two queries django


code:
df

res = df.groupby(['category', 'sensitivity_level']).max()
res
res[res.val <= 30]

rawDescription: Assign values to the groups of a groupby in pandas


code:
width = 0.45
ind = np.arange(11) + 0.75

fig, ax = plt.subplots(1,1)
p0 = ax.bar(ind, data[0],  width, color = 'cyan')
p1 = ax.bar(ind, data[1], width, bottom = data[0], color = 'violet')
p2 = ax.bar(ind, data[2], width, bottom = data[0] + data[1], color = 'g')
p3 = ax.bar(ind, data[3], width, bottom = data[0] + data[1] + data[2], color = 'r')

ax.set_ylabel('kWh')
ax.set_xlabel('month')
ax.set_xticks (ind + width/2.)
ax.set_xticklabels( xt, rotation = 70 )

fig.legend( (p0[0], p1[0], p2[0], p3[0]), ('kitchen', 'laundry', 'aircon&heater', 'other') )
fig.tight_layout()
fig.show()

rawDescription: Creating bar chart with CSV data

description: Creating bar chart with CSV data


code:
import glob
module_files = glob.glob("*.py") #return a list of all names in folder ending '.py'

rawDescription: Dynamically load python modules

description: Dynamically load python modules


code: df.resample('BQS', how='sum').head()

rawDescription: Getting end of period change with different index


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

fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

line, = ax.plot([], [])

def init():
    line.set_data([], [])
    return line,

def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    y -= y % 0.3
    line.set_data(x, y)
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=100, interval=20, blit=True)

plt.show()

rawDescription: Matplotlib animation of a step

description: Matplotlib animation of a step


code: raw = fobj.buffer.raw

rawDescription: Re-open a file in binary mode with Python


code:
from operator import itemgetter
l = [(1,5,2),(7,1,4),(1,6,3)]
l.sort(key=itemgetter(2))
l

rawDescription: Sorting a list of tuples in python according to an element index


code:
class ShuffledPaginator(Paginator):
    def page(self, number):
        page = super(ShuffledPaginator, self).page(number)
        random.shuffle(page.object_list)
        return page

rawDescription: Django: Randomizing queryset with pagination in class based view


code: boolean_value = oneOf("True False").setParseAction(lambda t: t[0]=='True')

rawDescription: PyParsing a string representing a function


code:
class BaseClass(object):
    def foo(self): return 'BaseClass foo'

class Intermediary(BaseClass):
    def foo(self): return 'Intermediary foo'

class Derived(Intermediary):
    def foo(self): return 'Derived foo'

d = Derived()
d.foo()
super(Derived, d).foo
super(Derived, d).foo()
super(Intermediary, d).foo()
Derived.__mro__

rawDescription: What the difference btween super(type, object) and super(supertype, type) in python?


code:
for n, x in enumerate(List, 1):
    print(n, x)

rawDescription: Print lines with numbers infront

description: Print lines with numbers infront


code:
import datetime
import subprocess
import time

while True:
    start = datetime.datetime.now()
    with open('log.txt', 'a+b') as logfile:
        subprocess.call(['ping', '-c1', '-w4', 'www.example.com'], stdout=logfile)
    delta = datetime.datetime.now() - start
    time.sleep(delta.total_seconds())

rawDescription: Logging ping successes and failures in linux


rawDescription: Convert list to datetime in pandas

description: Convert list to datetime in pandas


code:
from datetime import timedelta, datetime

def helper(d):
    if d.weekday() == 5:
        d += timedelta(days=1)
    return d.replace(hour=0, minute=0, second=0, microsecond=0)

def diff(s, e):
    weekend = {5, 6}
    both = e.weekday() in weekend and s.weekday() in weekend
    is_weekend = e.weekday() in {5, 6} or s.weekday() in {5, 6}
    if e.weekday() in weekend:
        e = helper(e)
    if s.weekday() in weekend:
        s = helper(s)
    _diff = (e - s)
    wek = _diff.days / 7 * 2 + is_weekend - both
    if s.weekday() > e.weekday() and not is_weekend:
        wek += 2
    return timedelta(seconds=_diff.total_seconds()) - timedelta(wek)

rawDescription: Time between two dates except weekends

description: Time between two dates except weekends


code:
class Ride(models.Model):
    # ...
    passengers=models.ManyToMany(UserProfile, through='RideSeat', related_name="r_passengers")
    mid_destinations=models.ManyToMany(Destination, through='RideMidDestination', related_name="r_mid_destinations")

class RideSeat(models.Model):
    ride = models.ForeignKey(Ride)
    passenger = models.ForeignKey(UserProfile)
    seat_number = models.IntegerField()

class RideMidDestination(models.Model):
    ride = models.ForeignKey(Ride)
    destination = models.ForeignKey(Destination)
    arrival_time = models.DateTimeField()

rawDescription: django - Many To Many relationship issue

description: Django - Many To Many relationship issue


code: reduce(lambda r, d: r.update(d) or r, lst, {})

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


code:
from datetime import datetime
lst = ['Jan 01', 'Feb 02', 'Mar 05', 'Feb 04', 'Jan 05']
sorted(lst, key=lambda d: datetime.strptime(d, '%b %d'))

rawDescription: Python sort list of dates in [MMM dd, MMM dd...] format


code:
x = y
y = x + y

rawDescription: Why does this code only work when i use 'x,y = y,x + y format instead of 'x = y; y = x + y'?


rawDescription: Count occurrence of tuples with Python

description: Count occurrence of tuples with Python


code:
import datetime
s = 1236472051807 / 1000.0
datetime.datetime.fromtimestamp(s).strftime('%Y-%m-%d %H:%M:%S.%f')

rawDescription: converting epoch time with milliseconds to datetime

description: Converting epoch time with milliseconds to datetime


code:
def __metaclass__(name, base, dict):
    print('inside __metaclass__(%r, %r, %r)' % (name, base, dict))
    return type.__new__(__metaclass__, name, base, dict)
class Foo(object):
    __metaclass__ = __metaclass__

rawDescription: Why isn't my Python 2.7 metaclass function getting invoked for subclasses?


code:
True
True
False
False
False

rawDescription: Given a list of integers, determine if 70% of the values are within 20% of one of the values


code:
import matplotlib.pyplot as pp
pp.ion()
fig = pp.figure()

rawDescription: python and update figure in matplotlib

description: Python and update figure in matplotlib


code:
def concat(self):
    self = str(self)
    numstring.join(self)
    print(numstring)

rawDescription: How can I execute more a command based on the Button that called it? (Classes and tkinter)

description: Execute more a command based on the Button that called it? (Classes and tkinter)


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

x = np.linspace(0, 10, 100)

fig, ax = plt.subplots()
for i in range(1, 6):
    ax.plot(x, i*x + x, label='$y={i}x + {i}$'.format(i=i))
ax.legend(loc='upper left', borderpad=2)
plt.show()

rawDescription: How to adjust the size of matplotlib legend box?

description: Adjust the size of matplotlib legend box


code:
pd.to_timedelta(1, unit='M')
(pd.Timestamp('2015-01-01') + pd.to_timedelta(M, unit='M')) - pd.Timestamp('2015-01-01')

rawDescription: Python & Pandas: series to timedelta

description: Python & Pandas: series to timedelta


rawDescription: Pandas rename/transpose column using value from another column


code:
menl = MutableEfficientNamedList('foo', 'bar', 'baz')
menl
menl.field1
menl[0]
menl[1]
menl[1] = 'spam'
menl.field2

rawDescription: python - which data structure to use as an array of dicts?

description: Python - which data structure to use as an array of dicts


code:
x = ['temp1_a','temp2_b', None, 'temp3_c']
y, z  = [i if i is None else i.split('_')[0] for i in x ], [i if i is None else i.split('_')[1] for i in x ]
y
z

rawDescription: How do I split items in a list (with delimiter) within a list?

description: Split items in a list (with delimiter) within a list


code:
from StringIO import StringIO
import pandas as pd

data = """   obs  a  b  c  d
1   1-23-12  1  2  3
2  12-23-13  4  5  5
3  21-23-14  4  5  5"""

# Build dataframe from data
df = pd.read_table(StringIO(data), sep='  ')

# Reformat values for column a using an unnamed lambda function
df['a'] = df['a'].apply(lambda x: x.split('-')[0])

rawDescription: Pandas delete parts of string after specified character inside a dataframe


code:
for link in soup.findAll('div', {'class': 'article-entry text'}):
  title = link.string
  print(title)

rawDescription: Print article with a Web-Crawler on Python


code:
class Foo(object):
    def __init__(self, value):
        self._spam = value

    @property
    def spam(self):
        return self._spam

rawDescription: Python class which instances' atributes can't be edited


code:
di=[]
for pair in pairs(LoT):
   di.append({repr(list(pair)): [theader[i] for i, t in enumerate(zip(*pair)) if t[0]!=t[1]]})
di

rawDescription: Element by element comparison in tuple to return set of sets - python


code:
class EventEngine:

  def __init__(self, Camera_Class, Map_Class, Character_Class, Skill_Class,
               Interface_Class, Sprite_Class, Dialog_Class, Game_Class,
               Item_Class):
      self.ItemEngine = Item_Class
      self.GameEngine = Game_Class
      self.DialogEngine = Dialog_Class
      self.SpriteEngine = Sprite_Class
      self.SkillEngine = Skill_Class
      self.CameraEngine = Camera_Class
      self.MapEngine = Map_Class
      self.CharacterEngine = Character_Class
      self.IEngine = Interface_Class

rawDescription: How to Link Multiple Classes Together Efficiently


code:
def model(Dt=1, time_step = 50, w1 = 1, steepness_SS_a=1,
          speed_SS_a=1, threshold_SS_a=1, WS_a=1, SS_a=0,
          t=0, t_final=30):
    """This is my model of ...
       where
       Dt: ...
       time_step: ...
       w1: ...
       steepness_SS_a: ...
       speed_SS_a: ...
       threshold_SS_a: ...
       WS_a: ...
       SS_a: ...
       t: initial time
       t_final: final time
    """
    ...

rawDescription: Accessing a value from an np.array from a while loop


code: true_locs = np.where(input == True)[0]

rawDescription: Filling next N rows in pandas with False when True found


rawDescription: Matplotlib axes autoscale does not work with after twinx()


code:
from sqlalchemy import Column, Integer, Date, Text, String, Boolean, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Area(Base):
    __tablename__ = 'areas'
    id = Column(Integer, primary_key=True)
    name = Column(String(64), unique=True)

rawDescription: Trouble instantiating db SQLAlchemy when importing table class definitions


code:
import copy
def printtext(swefile):
    for row in swefile:
        print(row)
text = open("wordsv.txt","r",encoding='utf-8').readlines()
text2 = copy.copy(text)
printtext(text)
printtext(text2)

rawDescription: How to copy text file in python

description: Copy text file in python


code:
def walk_dict( d, e=None, l=None, cn=None ):
    if e==None: e={}
    if l==None: l=[]
    if cn==None: cn=[]
    for k,v in d.items():
        if k != 'data':
            if k not in cn: cn.append(k)
            e[k] = v
    if isinstance(d['data'], list):
        for dp in d['data']:
            walk_dict( dp, e, l, cn )
    else:
        for k,v in d['data'].items():
            if k not in cn: cn.append(k)
            e[k] = v
        l.append( e.copy() )
    return l, cn

rawDescription: Converting Arbitrarily Nested Dict to List of Lists


code:
urlpatterns = patterns('',

    url(r'^$',LandingPage.as_view(),name='home'),)+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

rawDescription: Bootstrap template with Django

description: Bootstrap template with Django


code:
XLSX_CONTENT_TYPE = ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

class ExportCampaignView(generic.View):

    def get(self, request, *args, **kwargs):
        xlsx_file = self.create_xlsx()
        response = HttpResponse(xlsx_file, content_type=XLSX_CONTENT_TYPE)
        response['Content-Disposition'] = (
            'attachment; filename=campaign.xlsx')
        return response

    @staticmethod
    def create_xlsx():
        """
        :return tablib.Dataset: Campaign data.
        """
        output = StringIO()
        workbook = xlsxwriter.Workbook(output)
        sheet = workbook.add_worksheet()
        bold_with_bg = workbook.add_format(
            {'bold': True, 'bg_color': 'silver'})
        date_format = workbook.add_format({'num_format': 'dd.mm.YYYY'})

        headers = [
            _(u'col1'), _(u'col2'), _(u'col3'),

        ]

        data = Model.objects.all()

        for row_idx, row in enumerate(data, start=1):
            sheet.write(row_idx, 0, row.col1)
            sheet.write(row_idx, 1, row.col2)
            sheet.write(row_idx, 1, row.col3)

        workbook.close()
        output.seek(0)
        return output

rawDescription: Triggering Custom Functionality in Django Admin


code:
text,rc = f()
text,_ = f()
_,rc = f()

rawDescription: How to discard part of function output in python?

description: Discard part of function output in python


rawDescription: How to extract substring with base part of path string

description: Extract substring with base part of path string


rawDescription: Reduce the decimal points in Python

description: Reduce the decimal points in Python


code:
db = SQLAlchemy()

def create_app():
    app = Flask(__name__)
    db.init_app(app)
    return app

rawDescription: Share sqlalchemy models between flask and other apps


code:
import datetime
str_date = '2/28/1987'
datetime.datetime.strptime(str_date, '%m/%d/%Y').date().isoformat()

rawDescription: how to format date in ISO using python?

description: Format date in ISO using python


rawDescription: Can I keep decimal precision while using integer division in Python?

description: Keep decimal precision while using integer division in Python


code:
from django.core.paginator import Paginator
objects = ['john', 'paul', 'george', 'ringo']
p = Paginator(objects, 2)
p.count
p.num_pages
p.page_range
page1 = p.page(1)
page1
page1.object_list
page2 = p.page(2)
page2.object_list
page2.has_next()
page2.has_previous()
page2.has_other_pages()
page2.next_page_number()
p.page(0)
p.page(3)

rawDescription: How can I display queryset values by x in group?

description: Display queryset values by x in group


code:
import textwrap
strs = "In my project, I have a bunch of strings that are read in from a file. Most of them, when printed in the command console, exceed 80 characters in length and wrap around, looking ugly."
print(textwrap.fill(strs, 20))

rawDescription: A good way to make long strings wrap to newline in Python? (3.x)


code:
import xml.etree.ElementTree
xml.etree.ElementTree.fromstring(
  '''<item><img src="cat.jpg" /> Picture of a cat</item>'''
).find('img').tail

rawDescription: Extracting text after tag in Python's ElementTree


code:
from collections import defaultdict

l = [('apple',4), ('carrot',2), ('apple',1), ('carrot',7)]

d = defaultdict(list)
for i, j in l:
    d[i].append(j)

result = sorted([tuple([x, tuple(sorted(y))]) for x, y in d.items()])

print(result)

rawDescription: Arranging elements of a list of tuples

description: Arranging elements of a list of tuples


rawDescription: Python look for a format that match specific format


code: subprocess.Popen(["C:\\Program Files\Microsoft Office\Office12\winword.exe", "P:\\docs\\daily checks.doc", "/mFilePrintDefault", "/mFileExit"]).communicate()

rawDescription: Printing a word Document within Python

description: Printing a word Document within Python


code:
import theano
import numpy
a = numpy.int64(2)
y = theano.tensor.scalar('y',dtype='int64')
z = theano.tensor.scalar('z',dtype='int64')
x = theano.shared(a)
plus = y + z
theano_sum = theano.function([y,z],plus)
# Using shared variable in a function
print(theano_sum(x.get_value(),3))
# Changing shared variable value using a function
x.set_value(theano_sum(2,2))
print(x.get_value())
# Update shared variable value
x.set_value(x.get_value(borrow=True)+1)
print(x.get_value())

rawDescription: Using a shared variable in a function

description: Using a shared variable in a function


rawDescription: How do I get the modified date/time of a file in Python?

description: Get the modified date/time of a file in Python


code:
class Tablerow(ndb.Model):
    col1: ndb.StringProperty()
    col2: ndb.StringProperty()

class Tablehandler(webapp2.RequestHandler):
    def get(self):
        data = [tr.to_dict() for tr in Tablerow.query().iter()]
        response.write(json.dumps(data))

app = webapp2.WSGIApplication([('/thetable', Tablehandler)])

rawDescription: How to refresh only part of page using GAE

description: Refresh only part of page using GAE


code:
resultDict = {}
cities = [' '.join(c[:2]) for c in places]
for startCity in places:
    for endCity in places:
        start = ' '.join(startCity[:2])
        end = ' '.join(endCity[:2])
        resultDict[start, end] = calc_distance(startCity, endCity)

rawDescription: Printing list to table

description: Printing list to table


code:
def NoneCheck(collegelist, attribute):
    return sum(getattr(coll, attribute) for coll in collegelist) != 0

rawDescription: Passing Attributes as Arguments

description: Passing Attributes as Arguments


code:
#!/usr/bin/env python3
import os
import sys
from select import select
from subprocess import Popen, PIPE

with Popen(['stdbuf', '-oL', '-e0', 'curl', 'www.google.com'],
           stdout=PIPE, stderr=PIPE) as p:
    readable = {
        p.stdout.fileno(): sys.stdout.buffer, # log separately
        p.stderr.fileno(): sys.stderr.buffer,
    }
    while readable:
        for fd in select(readable, [], [])[0]:
            data = os.read(fd, 1024) # read available
            if not data: # EOF
                del readable[fd]
            else:
                readable[fd].write(data)
                readable[fd].flush()

rawDescription: Run command and get its stdout, stderr separately in near real time like in a terminal


code:
def main(number):
    number = randomNumber(number)
    print(number)

rawDescription: python doesn't update int in main method

description: Python doesn't update int in main method


rawDescription: pandas count number of same element in column and erase if the number is small

description: Pandas count number of same element in column and erase if the number is small


code: [i for i, j in zip(a, b) if i == j]

rawDescription: How can I compare two lists in python and return matches

description: Compare two lists in python and return matches


code:
f = open('newtree.yaml', "w")
yaml.dump(dataMap, f)
f.close()

rawDescription: YAML parsing and Python?

description: YAML parsing and Python


rawDescription: Finding a list of all double-underscore variables?


code:
y_root = .0
for i in range(1000):
    y_root = sp.optimize.fsolve(g,y_root,i)

rawDescription: Finding the root of a multivariate function at different variable values with Python


code: res = df.groupby('grpno.', as_index=False)['count percent'].max().rename(columns={'count percent': 'MaxOfcount percent'})

rawDescription: find the max of column group by another column pandas

description: Find the max of column group by another column pandas


rawDescription: Pandas to_csv call is prepending a comma


code:
import mymodule

def dummy_urlopen(url):
    ...

mymodule.urllib.urlopen = dummy_urlopen

rawDescription: How can one mock/stub python module like urllib


rawDescription: Remove ' from python list

description: Remove ' from python list


code:
def getSrc():
    if not getSrc.source:
        getSrc.source = askdirectory()
    return getSrc.source
getSrc.source = None

rawDescription: Python Reuse a Variable in the Else Block of an If-Else Statement


code: bb = np.array([b])

rawDescription: SUM does not work

description: SUM does not work


rawDescription: Url pattern matching is confusing in django


code:
# ... 8< - -
# check area around agent (note: uses numpy)
sight = 1
left, right_sup = max(0, x - sight), min(x + sight + 1, width)
top, bottom_sup = max(0, y - sight), min(y + sight + 1, height)

known_section = known_environment[left:right_sup, top:bottom_sup]
actual_section = actual_environment[left:right_sup, top:bottom_sup]
if not np.array_equal(known_section, actual_section):
    known_environment[left:right_sup, top:bottom_sup] = actual_section
# - - >8 ...

rawDescription: Fastest method for comparing ranges of multidimensional lists


code:
import numpy as np
import pandas as pd
np.random.seed(2015)
df = pd.DataFrame(np.random.randint(10, size=(10,4)),
                  columns=['Min', 'Se', 'ID','Va'])

grouped = df.groupby(['Min', 'Se', 'ID'])
print(grouped.mean())

rawDescription: pandas or numpy for multi column conditional averaging

description: Pandas or numpy for multi column conditional averaging


rawDescription: Matrix triple product with theano

description: Matrix triple product with theano


code:
# at start add this
import os, sys

abspath = lambda *p: os.path.abspath(os.path.join(*p))

PROJECT_ROOT = abspath(os.path.dirname(__file__))
sys.path.insert(0, PROJECT_ROOT)

TEMPLATE_DIRS = (
    abspath(PROJECT_ROOT, 'templates'), # this will point to mysite/mysite/templates
)

rawDescription: Django - setting up urls.py and views.py


rawDescription: How to generate documentation from python code looking like "python shell output"

description: Generate documentation from python code looking like "python shell output"


code:
...
options = ['one', 'two', 'three', 'four']
option = OptionMenu(master, var, *options)
option.pack()
...

rawDescription: Tkinter option menu

description: Tkinter option menu


code: os.system('ulimit -s unlimited; some_executable')

rawDescription: Setting stacksize in a python script

description: Setting stacksize in a python script


rawDescription: How can I use django.auth.views.login?

description: Use django.auth.views.login


code: M1, M2 = map(list, zip(*M))

rawDescription: Separate lists of format M=[(a,x), (b,y)] into M1=[a,b] and M2=[x,y]


code:
path = "path/to/file/location/"
with open(path + 'result.txt', 'w') as result:
    files = [open(path + 'file%d.txt' % (n+1)) for n in range(16)]
    form = ", ".join('{}' for f in files) + '\n'
    for lines in zip(*files):
        result.write(form.format(*map(str.rstrip, lines)))
    for f in files:
        f.close()

rawDescription: Elegant way of combining files - Python

description: Elegant way of combining files - Python


code:
ftp.voidcmd('TYPE I')
# Do the transfer here

rawDescription: python ftplib transfercmd() BINARY Mode

description: Python ftplib transfercmd() BINARY Mode


code:
import numpy as np
import scipy.sparse as sparse

arr = np.array([[0, 1, 1],
                [0, 2, 1],
                [1, 2, 1],
                [1, 0, 1],
                [2, 1, 4]])
shape = tuple(arr.max(axis=0)[:2]+1)
coo = sparse.coo_matrix((arr[:, 2], (arr[:, 0], arr[:, 1])), shape=shape,
                        dtype=arr.dtype)

print(repr(coo))
# <3x3 sparse matrix of type '<type 'numpy.int64'>'
#   with 5 stored elements in COOrdinate format>

rawDescription: numpy/scipy build adjacency matrix from weighted edgelist

description: Numpy/scipy build adjacency matrix from weighted edgelist


rawDescription: Automatically writing headers using csv python


rawDescription: Disable autosave on update in SQLAlchemy


code:
import re
s_pat = r'''
\s*  # any amount of white space
([^( \t]  # start match group; match one char that is not a '(' or space or tab
[^(]*  # match any number of non '(' characters
[^( \t])  # match one char that is not a '(' or space or tab; close match group
\s*  # any amount of white space
\(  # match an actual required '(' char (not in any match group)
\s*  # any amount of white space
([^) \t]  # start match group; match one char that is not a ')' or space or tab
[^)]*  # match any number of non ')' characters
[^) \t])  # match one char that is not a ')' or space or tab; close match group
\s*  # any amount of white space
\) # match an actual required ')' char (not in any match group)
\s*  # any amount of white space
(?:,|$)  # non-match group: either a comma or the end of a line
'''
pat = re.compile(s_pat, re.VERBOSE)

lst = pat.findall(string)

rawDescription: Extracting contents of a string within parentheses


code:
type(dog.name())
name, pos, sid = dog.name().split('.')
name
pos
sid

rawDescription: While working on nltk, how to manipulate the nltk.corpus.reader.wordnet.Synset?


code:
#!/usr/bin/env python
from datetime import datetime
import parsedatetime # $ pip install parsedatetime

today = datetime(2015, 1, 1)
calendar= parsedatetime.Calendar()
for timestring in [
        "today at 4:00",
        "tomorrow at 10:00",
        "yesterday at 8:00"]:
    d, parsed_as = calendar.parseDT(timestring, today)
    assert parsed_as == 3 # as datetime
    print(d)

rawDescription: Relative Date Base with Dateutil

description: Relative Date Base with Dateutil


rawDescription: Summing across rows of Pandas Dataframe

description: Summing across rows of Pandas Dataframe


rawDescription: Python eig for generalized eigenvalue does not return correct eigenvectors


code:
currdiv = None
mydict = {}
for e in sel.xpath('//div[@class="asdf"]/*'):
    if bool(int(e.xpath('@class="button-left"').extract()[0])):
        currdiv = e.xpath('text()').extract()[0]
        mydict[currdiv] = []
    elif currdiv is not None:
        mydict[currdiv] += e.xpath('text()').extract()

rawDescription: How to scrape table with different xpath on the same level with Scrapy?

description: Scrape table with different xpath on the same level with Scrapy


code:
if totalq:
    print(totalq, "quarters", end=' ')
if totald:
    print(totalq, "dimes", end=' ')
if totaln:
    print(totalq, "nickels", end=' ')
if totalp:
    print(totalq, "pennies")

rawDescription: Printing specific parts of a function for Python


code:
nav_json = json.dumps(nav_data)
nav_b64=base64.b64encode(nav_json)
self.response.set_cookie('nav_data', nav_b64)

rawDescription: How can I send JSON-formatted cookie data using Python on Google App Engine?

description: Send JSON-formatted cookie data using Python on Google App Engine


code: circle = Geometry(1)

rawDescription: Python 2.7, defining a base class with attributes, id with init constructor


code:
from django.shortcuts import render

def user_page(request, user_id):
    page = userpagename.get(pk=user_id)
    links = page.newlinkpost_set.all()
    return render(
        request,
        "myapp/user_page.html",
        {
            page: page,
            links: links,
        }
    )

rawDescription: Django: How to return all models associated with a ForeignKey including all attributes of those models?


code:
def newton( ffp, x, nits):
    c = 0                   # initialize iteration counter
    for i in range(nits):
        c += 1              # increment counter for each iteration
        f, fp = ffp(x)
        x = x - f/fp
    return x, c             # return the counter when the function is called

rawDescription: How can I check to see the number of iterations Newton's method takes to run?

description: Check to see the number of iterations Newton's method takes to run


code:
import pytz
from dateutil import parser
parser.parse('2014/08/19 03:38:46 GMT-4')
dt = parser.parse('2014/08/19 03:38:46 GMT-4')
dt.astimezone (pytz.utc)

rawDescription: Convert GMT based time to UTC python

description: Convert GMT based time to UTC python


rawDescription: What is the difference between these two (Python) code samples?


code:
class bidict(dict):
    def key_with_value(self, value, default=None):
        for k, v in self.iteritems():
            if v == value:
                return v
        return default

    def keys_with_value(self, value, default=None):
        return [v for k, v in self.iteritems() if v == value]

rawDescription: Finding key from value in Python dictionary:


code: rdd = pairs.groupByKey().mapValues(lambda iter: ...)

rawDescription: How to return a dictionary in parallel processing in spark?

description: Return a dictionary in parallel processing in spark


code:
def on_mouse_click(self, widget, event):
    width = self.progresseventbox.get_allocated_width()
    percentage = event.x / width * 100
    percentage = str(percentage)
    output = (subprocess.check_output(["kodi-cli", "-g", percentage]))
    # Let's get some response from kodi-cli
    output = (subprocess.check_output(["kodi-cli", "-g"]))
    output = output.decode('ascii')
    # http://stackoverflow.com/questions/1798465/python-remove-last-3-characters-of-a-string
    self.output_text.set_text(output)

rawDescription: How can I receive a corresponding percentage when progressbar is clicked python3 gtk3

description: Receive a corresponding percentage when progressbar is clicked python3 gtk3


code:
def parse(self, response):
    hxs = HtmlXPathSelector(response)
    item = PrivacyItem()
    item['desc'] = hxs.select('//body//p/text()').extract()
    item['title'] = hxs.select('//title/text()').extract()
    return item

rawDescription: Scrapy spider not saving to csv

description: Scrapy spider not saving to csv


code: a, b = b, a

rawDescription: Python variable interchange not working properly?


code:
import argparse

def main(argv):
    parser = argparse.ArgumentParser()
    # ... Add args
    parser.parse_args(argv[1:])  # This is the default for argparse
    ns = parser.parse_args()

if __name__ == "__main__":
    import sys
    main(sys.argv)

rawDescription: Is there a way I can use mock() (or similar) to mock the date & time for a script I *invoke* from a unit test?


rawDescription: Math operations from string

description: Math operations from string


rawDescription: Manipulating data from a list of dictionaries (Google API Distance Matrix)


code:
def __get__(self, instance, owner):
    self.cls = owner
    self.obj = instance
    return self

rawDescription: Returning a modified docstring from an attribute wrapped method


code:
mask = pygame.Surface((320, 240), pygame.SRCALPHA)
for y in range(0, 240):
    for x in range(0, 320):
        if (x/16 + y/16) % 2 == 0:
            mask.set_at((x, y), Color("white"))

rawDescription: Pygame - is there any way to only blit or update in a mask


rawDescription: how to join element in list by index?

description: Join element in list by index


code:
import webapp2
import httplib2
from oauth2client.appengine import oauth2decorator_from_clientsecrets
from apiclient.discovery import build

import gdata.contacts.client

decorator = oauth2decorator_from_clientsecrets(
  "client_secrets.json",
  scope=["https://www.google.com/m8/feeds", "https://www.googleapis.com/auth/calendar.readonly"]
)

# Helper class to add headers to gdata
class TokenFromOAuth2Creds:
  def __init__(self, creds):
    self.creds = creds
  def modify_request(self, req):
    if self.creds.access_token_expired or not self.creds.access_token:
      self.creds.refresh(httplib2.Http())
    self.creds.apply(req.headers)

class MainHandler(webapp2.RequestHandler):
  @decorator.oauth_required
  def get(self):
    # This object is all we need for google-api-python-client access
    http = decorator.http()

    # Create a gdata client
    gd_client = gdata.contacts.client.ContactsClient(source='<var>YOUR_APPLICATION_NAME</var>')

    # And tell it to use the same credentials
    gd_client.auth_token = TokenFromOAuth2Creds(decorator.get_credentials())

    # Use Contacts API with gdata library
    feed = gd_client.GetContacts()
    for i, entry in enumerate(feed.entry):
      self.response.write('\n%s %s' % (i+1, entry.name.full_name.text if entry.name else ''))

    # Use Calendar API with google-api-python-client
    service = build("calendar", "v3")
    result = service.calendarList().list().execute(http=http)
    self.response.write(repr(result))

app = webapp2.WSGIApplication([
  ("/", MainHandler),
  (decorator.callback_path, decorator.callback_handler()),
], debug=True)

rawDescription: Unifying OAuth handling between gdata and newer Google APIs


code:
class C:
    @staticmethod
    def default_concrete_strategy():
        print("default")

    @staticmethod
    def other_concrete_strategy():
        print("other")

    def __init__(self, strategy=None):
        if strategy is None:
            strategy = self.default_concrete_strategy
        self.strategy = strategy

rawDescription: In Python, how can I use a static method as a default parameter for the strategy design pattern?


rawDescription: Set values between indexes to value - Python


code:
with open('file', 'w') as fout:
  for i in range(5):
    fout.write(10 * ('header %d ' % i) + '\n')
  for i in range(1234567):
    fout.write(10 * ('line %d ' % i) + '\n')

rawDescription: chunk a text database into N equal blocks and retain header

description: Chunk a text database into N equal blocks and retain header


rawDescription: Find span where condition is True using NumPy


code:
'-'.join([x for x in ('foo','bar') if x])
'-'.join([x for x in ('foo','') if x])
'-'.join([x for x in ('','bar') if x])
'-'.join([x for x in ('','') if x])

rawDescription: What better way to concatenate string in python?


code:
search
page
plus
signin
account
security
lol
about
someotherpage.html

rawDescription: How to know if a specific page in a website is down (Batch or Python)

description: Know if a specific page in a website is down (Batch or Python)


code: s = df.groupby(level='A').cumcount().div(df.groupby(level='A').size(), level='A')

rawDescription: Spliting multi-index indices around a given level


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

#-- Generate some data...
x, y = np.mgrid[-5:5:0.1, -5:5:0.1]
z = np.sqrt(x**2 + y**2) + np.sin(x**2 + y**2)

#-- Extract the line...
# Make a line with "num" points...
x0, y0 = 5, 4.5 # These are in _pixel_ coordinates!!
x1, y1 = 60, 75
num = 1000
x, y = np.linspace(x0, x1, num), np.linspace(y0, y1, num)

# Extract the values along the line, using cubic interpolation
zi = scipy.ndimage.map_coordinates(z, np.vstack((x,y)))

#-- Plot...
fig, axes = plt.subplots(nrows=2)
axes[0].imshow(z)
axes[0].plot([x0, x1], [y0, y1], 'ro-')
axes[0].axis('image')

axes[1].plot(zi)

plt.show()

rawDescription: How to extract an arbitrary line of values from a numpy array?

description: Extract an arbitrary line of values from a numpy array


code:
ba = bytearray()
ba.extend(i.to_bytes(length=2, byteorder='big'))
ba

rawDescription: Add part of an integer to bytearray

description: Add part of an integer to bytearray


code: c.executemany('''INSERT INTO my_table VALUES (?,?);''', my_data)

rawDescription: sqlite3 in Python extract entries that correspond to values in tuple

description: Sqlite3 in Python extract entries that correspond to values in tuple


code:
xc = ExpiringCache()
xc.add('apples', 1)
xc.add('bananas', 2)
xc.add('mangoes', 3)

rawDescription: Ideal data structure with fast lookup, fast update and easy comparison/sorting


code:
from datetime import date, datetime
import timeit
def ugly(input_date):
    a= 1000 * int(input_date[0])
    b=  100 * int(input_date[1])
    c=   10 * int(input_date[2])
    d=    1 * int(input_date[3])
    year = a+b+c+d
    c=   10 * int(input_date[5])
    d=    1 * int(input_date[6])
    month = c+d
    c=   10 * int(input_date[8])
    d=    1 * int(input_date[9])
    day = c+d
    try:
        my_date = date(year, month, day)
    except ValueError:
        my_date = None

def strptime(input_date):
    try:
        my_date = datetime.strptime(input_date, "%Y-%m-%d").date()
    except ValueError:
        my_date = None

timeit.timeit('f("2014-07-08")', 'from __main__ import ugly as f')
timeit.timeit('f("2014-07-08")', 'from __main__ import strptime as f')

rawDescription: Speeding up datetime.strptime

description: Speeding up datetime.strptime


code:
from django.utils import timezone

local_dt = timezone.localtime(item.created_at, pytz.timezone('Europe/Berlin'))

rawDescription: Django datetime field - convert to timezone in view


code:
with open(infile) as fin,open('outfile','w') as fout:
   for line in fin:
       #use line.split('\t'3) if the name of the field can contain spaces
       name,start,end,rest = line.split(None,3)
       #do something to change start and end here.
       #Note that `start` and `end` are strings, but they can easily be changed
       #using `int` or `float` builtins.
       fout.write('\t'.join((name,start,end,rest)))

rawDescription: Python -- how to read and change specific fields from file? (specifically, numbers)


code: macstr = addr.replace(':', '').decode('hex')

rawDescription: MAC address conversion into byte array in python


code:
n = {}
for i in k:
    n[i] = some_value

rawDescription: Declaring variable name with string and number?


code:
from sqlalchemy import create_engine, Column, Integer, DateTime, String, ForeignKey, Table
from sqlalchemy.orm import relation, scoped_session, sessionmaker, eagerload
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:///:memory:', echo=True)
session = scoped_session(sessionmaker(bind=engine, autoflush=True))
Base = declarative_base()

t_subscription = Table('subscription', Base.metadata,
    Column('userId', Integer, ForeignKey('user.id')),
    Column('channelId', Integer, ForeignKey('channel.id')),
)

class Channel(Base):
    __tablename__ = 'channel'

    id = Column(Integer, primary_key = True)
    title = Column(String)
    description = Column(String)
    link = Column(String)
    pubDate = Column(DateTime)

class User(Base):
    __tablename__ = 'user'

    id = Column(Integer, primary_key = True)
    username = Column(String)
    password = Column(String)
    sessionId = Column(String)

    channels = relation("Channel", secondary=t_subscription)

# NOTE: no need for this class
# class Subscription(Base):
    # ...

Base.metadata.create_all(engine)

# ######################
# Add test data
c1 = Channel()
c1.title = 'channel-1'
c2 = Channel()
c2.title = 'channel-2'
c3 = Channel()
c3.title = 'channel-3'
c4 = Channel()
c4.title = 'channel-4'
session.add(c1)
session.add(c2)
session.add(c3)
session.add(c4)
u1 = User()
u1.username ='user1'
session.add(u1)
u1.channels.append(c1)
u1.channels.append(c3)
u2 = User()
u2.username ='user2'
session.add(u2)
u2.channels.append(c2)
session.commit()

# ######################
# clean the session and test the code
session.expunge_all()

# retrieve all (I assume those are not that many)
channels = session.query(Channel).all()

# get subscription info for the user
#q = session.query(User)
# use eagerload(...) so that all 'subscription' table data is loaded with the user itself, and not as a separate query
q = session.query(User).options(eagerload(User.channels))
for u in q.all():
    for c in channels:
        print (c.id, c.title, (c in u.channels))

rawDescription: How do I join three tables with SQLalchemy and keeping all of the columns in one of the tables?

description: Join three tables with SQLalchemy and keeping all of the columns in one of the tables


code:
np.in1d(28, b)
Out[158]: array([ True], dtype=bool)

np.in1d(43, b)
Out[159]: array([False], dtype=bool)

rawDescription: check membership of a set in python

description: Check membership of a set in python


code: self.skills = [s for s in self.skills if s.id != skill_to_remove.id]

rawDescription: Python. How to efficiently remove custom object from array


code:
b = Bucket()
b["foo"] = 42
b["foo"]

rawDescription: Alternative to "assign to a function call" in a python


code:
bulletGroup = [i for i in bulletGroup if i.buly >= 0]
for shot in bulletGroup:
    shot.buly -= 3

rawDescription: Python. How to remove an object from list


code: dict((k, int(v)) for k, v in (e.split(' - ') for e in s.split(',')))

rawDescription: Creating a dictionary from a string

description: Creating a dictionary from a string


code: d = [['document1', "he's", 1], ['document1', "it's", 0], ['document1', "there's", 5], ['document1', "they're", 2], ['document2', "he's", 0], ['document2', "it's", 4], ['document2', "there's", 3], ['document2', "they're", 2], ['document3', "he's", 1], ['document3', "it's", 7], ['document3', "there's", 4], ['document3', "they're", 0]]

rawDescription: Iterating through multiple values in a dictionary?


code:
for generation in range(n):
    new_graphs = (graph.add_edge(e) for e in graph.possible_new_edges())

rawDescription: lookahead algorithm in Python

description: Lookahead algorithm in Python


code:
app = Flask(__name__)
app.config['SECRET_KEY'] = 'Insert_random_string_here'

rawDescription: Creating flask form with selects from more than one table


code:
show_more(df, 1000)
# <- Shows here the DF

rawDescription: Is there a concise way to show all rows in pandas for just the current command?


code: {'Last name': 'Miller', 'First name': 'Joe'}

rawDescription: Adding tuple to as keys dictionary

description: Adding tuple to as keys dictionary


code:
x1, z1 = np.meshgrid(LAT, Z[:,0])
plt.contourf(x1, Z, T)

rawDescription: Contouring non-uniform 2d data in python/matplotlib above terrain


code:
poisson.pmf(1, -1)
poisson._pmf(1, -1)

rawDescription: How to speed up Poisson pmf function?

description: Speed up Poisson pmf function


code: content_type = u"application/json"

rawDescription: Django Braces' "JSONResponseMixin" shows weird behavior


code: mx_2d = mx.view('<i4').reshape(mx.shape[0], -1)

rawDescription: Importing matrix data from CSV into a numpy array, Issue with slicing the result


code:
Xyzzy('some var')
Xyzzy.factory_foo()
Xyzzy('some var').factory_foo()
Yappy('some var')
Yappy.factory_foo()
Yappy('some var').factory_foo()

rawDescription: Python derived class intricacies

description: Python derived class intricacies


somename

description: desc

someothername

description: asdasd

rawDescription: Where to change the form of json response in django rest framework?


code: {}, {}.format(1, numbersInbetween(2, 3))

rawDescription: A more optimized code in recursion exercise


code:
#in `main` after getting the seating section
max_seats = {"A": 300, "B": 500, "C": 200}
cost_per_ticket = {"A": 20, "B": 15, "C": 10}
tickets = get_value("Enter the amount of tickets sold: ", 0, max_seats[section])
print("Your section revenue is", tickets * cost_per_ticket[section])

rawDescription: Values aren't stored in python program

description: Values aren't stored in python program


code:
x = df1.iloc[0]
x
pd.Series(1, pd.date_range(x['Start'], x['End'] - pd.offsets.Day(1), freq='D')).resample('M', how='sum')

rawDescription: Sum of days in intervals

description: Sum of days in intervals


rawDescription: What is the best way to split a line of numbers separated by a marker and spaces and store it into a tuple?


code:
anagram_list = []
for v in anagram_dict.iteritems():
    if len(v) > 1:
        anagram_list += v

rawDescription: Large anagram search not reading to end of set Python


code:
import os
file_exists = os.path.exists("points.csv")
with ... as csvfile:
    writer = ...
    if not file_exists:
        writer.writeheader()
    ...

rawDescription: python write to a csv file

description: Python write to a csv file


code:
class FooAdmin(admin.ModelAdmin):
    # regular stuff
    class Media:
        js = (
            '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', # jquery
            'js/myscript.js',       # project static folder
            'app/js/myscript.js',   # app static folder
        )

admin.site.register(Foo, FooAdmin)

rawDescription: Adding a jQuery script to the Django admin interface


code:
import wx
class MyCustomFrame(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id)
        self.Bind(wx.EVT_PAINT,self.OnPaint)
    def OnPaint(self,evt):
        self.dc = dc = wx.PaintDC(self)
        p1 = [0,0]
        p2 = [500,0]
        for i in range(100):
            dc.DrawLine(p1[0],p1[1],p2[0],p2[1])
            p1 = [p1[0],p1[1]+5]
            p2 = [p2[0],p2[1]+5]
        p1=[0,0]
        p2 = [0,500]
        for i in range(100):
            dc.DrawLine(p1[0],p1[1],p2[0],p2[1])
            p1 = [p1[0]+5,p1[1]]
            p2 = [p2[0]+5,p2[1]]
if __name__ == "__main__":
    a = wx.App(redirect=False)
    f = MyCustomFrame(None,-1)
    #f.OnPaint(None) --- dont do this!!!
    f.Show()
    a.MainLDop()

rawDescription: How to make a Grid in wxPython?

description: Make a Grid in wxPython


code:
with codecs.open('myfile.asc', encoding='utf-8-sig') as f:
    X = [[float(x) for x in line.split()] for line in f]

rawDescription: read an ascii file into a numpy array

description: Read an ascii file into a numpy array


code:
import multiprocessing

def my_function(x):
    """The function you want to compute in parallel."""
    x += 1
    return x

if __name__ == '__main__':
    pool = multiprocessing.Pool()
    results = pool.map(my_function, [1,2,3,4,5,6])
    print(results)

rawDescription: Can I run multiple Ipython qt console in the same time?

description: Run multiple Ipython qt console in the same time


code:
def switch(ls):
    copy_ls = ls[:]
    copy_ls[1:3], copy_ls[3:5] = copy_ls[3:5], copy_ls[1:3]
    return copy_ls

rawDescription: Python: Finding symmetries in list of lists


code:
import test #name of your file

test.SECdownload(2014, 5)
print(test.target_dir)

rawDescription: Python GUI cannot see global variable

description: Python GUI cannot see global variable


rawDescription: Count occurrences of number in two lists and return one list


rawDescription: How the ttest is calculated in numpy

description: How the ttest is calculated in numpy


code:
def match_all(the_list, to_match):
    return [sublist for sublist in the_list
                if all(item in sublist for item in to_match)]

rawDescription: Efficient Matching of Sublists In Python


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup('''\

''')

links = soup.find_all('a')
[(l, l.parent.get('id')) for l in links]

rawDescription: Grab the parent id tag with beautiful soup?


code:
Agg = Aggreegator(keys.items())

# And simply access to attributes
Agg.used_user

rawDescription: Pythonic way to aggregate object properties in memory efficient way?


code: product(range(0, 1), range(1, 5), range(2, 6))

rawDescription: Python, all possible combinations of numbers betweens 2 lists


code:
def dot_to_json(a):
    output = {}
    for key, value in a.iteritems():
        path = key.split('.')
        if path[0] == 'json':
            path = path[1:]
        target = reduce(lambda d, k: d.setdefault(k, {}), path[:-1], output)
        target[path[-1]] = value
    return output

rawDescription: Dot notation to Json in python

description: Dot notation to Json in python


rawDescription: Subtracting by Dictionary Values from Iterated Total in For Loop Python


code:
from django.db import models
from django.contrib.sites.models import Site
from django.db.models import signals
from notification import models as notification

def create_notice_types(app, created_models, verbosity, **kwargs):
    notification.create_notice_type("new_comment", "Comment posted", "A comment has been posted")
signals.post_syncdb.connect(create_notice_types, sender=notification)

def new_comment(sender, instance, created, **kwargs):
    # remove this if-block if you want notifications for comment edit too
    if not created:
        return None

    context = {
        'comment': instance,
        'site': Site.objects.get_current(),
    }
    recipients = []

    # add all users who commented the same object to recipients
    for comment in instance.__class__.objects.for_model(instance.content_object):
        if comment.user not in recipients and comment.user != instance.user:
            recipients.append(comment.user)

    # if the commented object is a user then notify him as well
    if isinstance(instance.content_object, models.get_model('auth', 'User')):
        # if he his the one who posts the comment then don't add him to recipients
        if instance.content_object != instance.user and instance.content_object not in recipients:
            recipients.append(instance.content_object)

    notification.send(recipients, 'new_comment', context)

signals.post_save.connect(new_comment, sender=models.get_model('comments', 'Comment'))

rawDescription: How to use django-notification to inform a user when somebody comments on their post

description: Use django-notification to inform a user when somebody comments on their post


rawDescription: Most efficient way to loop through multiple csv files and calculate NYSE tick


code: pan = df.to_panel()

rawDescription: how to load a long panel dataset in Pandas?

description: Load a long panel dataset in Pandas


code:
@numba.njit('f8[:,:,:](f8[:,:,:],f8[:,:,:])')
def ss5(A,output):
    for i in range(A.shape[0]):
        for j in range(A.shape[1]-1):
            for k in range(A.shape[2]):
                output[i,j,k] = A[i,j,k] + A[i,j+1,k]
    return output

rawDescription: Optimize NumPy sum of matrices iterated through every element


code:
class ParentOne():
    def __init__(self):
        super().__init__()
        print("Parent One says: Hello my child!")
        self.i = 1

    def methodOne(self):
        print(self.i)

class ParentTwo():
    def __init__(self):
        super().__init__()
        print("Parent Two says: Hello my child")

class Child(ParentOne, ParentTwo):
    def __init__(self):
        super().__init__()
        print("Child Says: hello")

A=Child()

rawDescription: Is Multiple Inheritance problematic in Python?


code:
myRenderView.ViewSize = [984, 962]
SaveScreenshot('image.png', magnification=2, quality=100, view=myRenderView)

rawDescription: In Python script for Paraview, can I specify the resolution when I do a SaveScreenshot()?


code: np.take(rec, inds, out=rec)

rawDescription: sorting numpy structured and record arrays is very slow

description: Sorting numpy structured and record arrays is very slow


code:
def Product_Category_OnChange(self,cr,uid,ids,categ1):
    product_obj = self.pool.get('product.product')
    product_ids = product_obj.search(cr, uid, [('cat_id','=',categ1)]) #maybe cat_id is wrong field name
    return {'domain':{'my_products':[('id','in',product_ids)]}}

rawDescription: How to use drop down field in OpenERP?

description: Use drop down field in OpenERP


code:
from operator import itemgetter
srt_key = [i for i, e in sorted(enumerate(d["d"]), key=itemgetter(1))]

new_d = {}

for k,v in d.items():
    new_d[k] = list(itemgetter(*srt_key)(v))

print(new_d)
{'c': ['a', '9', 'g', 'b'], 'a': [4, 7, 1, 6], 'b': [9, 8, 9, 9], 'd': [1, 2, 5, 10]}

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


code:
import requests
from bs4 import BeautifulSoup
import json
url = 'https://vine.co/tags/funny'
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, 'html.parser')
# js = json.loads(soup.find("script",type="application/ld+json").text)
js = json.loads(soup.select_one("script[type=application/ld+json]").text)
print(js)

rawDescription: Parsing html for specific script type

description: Parsing html for specific script type


code:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import RANSACRegressor

df = pd.read_excel('/home/Jian/Downloads/Data.xlsx').dropna()

a = df.a.values.reshape(len(df), 1)
d = df.d.values.reshape(len(df), 1)

fig, axes = plt.subplots(ncols=2, sharey=True)
axes[0].scatter(a, d, c='g')
axes[0].set_xlabel('a')
axes[0].set_ylabel('d')

robust_estimator = RANSACRegressor(random_state=0)
robust_estimator.fit(a, d)
d_pred = robust_estimator.predict(a)

# calculate mse
mse = (d - d_pred) ** 2

index = np.argsort(mse.ravel())

axes[1].scatter(a[index[:-50]], d[index[:-50]], c='b', label='inliers', alpha=0.2)
axes[1].scatter(a[index[-50:]], d[index[-50:]], c='r', label='outliers')
axes[1].set_xlabel('a')
axes[1].legend(loc=2)

rawDescription: Identifying chunks of outliers from a 1D and 2D data in Python


rawDescription: Insert a space between a non-alphanumeric and alphanumeric char except specific char (Python)


code:
def print_obj(obj):
    sys.stdout.write(str(obj))
    obj = obj.child
    while obj:
        sys.stdout.write('.%s' % obj)
        obj = obj.child

rawDescription: Parent child relationship in django

description: Parent child relationship in django


code:
def x(self, value1, value2=None):
    if value2 is None:
        self._x = [value1] * 4
    else:
        self._x[0] = value1
        self._x[3] = value2

rawDescription: overload setters decorator in python

description: Overload setters decorator in python


code:
for row in row.split('\n'):
    writer.writerow(row.split(','))

rawDescription: Write to CSV file after re.sub Python

description: Write to CSV file after re.sub Python


code:
class Cheese(object):
    def __init__(self, id):
        self.id = id
    def __eq__(self, other):
        if isinstance(other, Cheese):
            return self.id == other.id
        return NotImplemented

Cheese(1) == Cheese(1)
Cheese(1) == Cheese(2)
lst = [[Cheese(3), Cheese(2), Cheese(1)], []]
next((i, sublist.index(Cheese(1))) for i, sublist in enumerate(lst) if Cheese(1) in sublist)

rawDescription: Searching for index of object in a list

description: Searching for index of object in a list


code:
app = Flask(__name__)

@app.before_first_request
def do_something_only_once():
    app.logger.setLevel(logging.INFO)
    app.logger.info("Initialized Flask logger handler")

rawDescription: How to execute a block of code only once in flask?

description: Execute a block of code only once in flask


code:
def CleanFile(sourceFile):
    global thefile
    thefile=list(csv.reader(open(sourceFile, 'rb'), delimiter=',', quotechar='"'))
    for i, line in enumerate(thefile):
        if line[3]=='':
            del thefile[i]

rawDescription: delete a line from a list of lines

description: Delete a line from a list of lines


code:
from time import time
start_time = time()
# do stuff
if time() - start_time > 300: # 300 secs
    function_call()

rawDescription: Periodically execute function every n minutes while doing other stuffs


code:
[('things', KeyProperty('things', repeated=True, kind='Thing'))]
[('Thing', KeyProperty('Thing', repeated=True))]

rawDescription: Get properties of ndb model instance where Python name != datastore name


code:
def default_to_regular(d):
    if isinstance(d, defaultdict):
        d = {k: default_to_regular(v) for k, v in d.iteritems()}
    return d

rawDescription: How to convert defaultdict of defaultdicts [of defaultdicts] to dict of dicts [of dicts]?

description: Convert defaultdict of defaultdicts [of defaultdicts] to dict of dicts [of dicts]


code:
for i in range(len(n_wk)):
    if i == 0:
        continue
    if n_wk[i] < n_wk[i-1]:
        if t[i-1] == 'a': #changed from t[i]
            t[i] = 'b'
        elif t[i-1] == 'b': #changed from t[i]
            t[i] = 'c'
    if n_wk[i] > n_wk[i-1]:
       t[i] = t[i-1]

print(t)
# ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c']

rawDescription: Modifying List elements in a loop

description: Modifying List elements in a loop


code:
a='51545'
obj = []
for i in range(len(a)):
    obj.append({a[i]: i})

obj

rawDescription: Finding Index in string with recurring chars


code:
#NOTE it is an array of integers
array_valuesreject = np.array([[1, 2], [2, 2], [3, 1], [10, 2]])
i, j = array_valuesreject.T

mask = np.zeros(test_array.shape, bool)
mask[i,j] = True
m = np.ma.array(test_array, mask=mask)

rawDescription: Calculation on 2D array rejecting special values (knowing the indices)


code:
def myView(request):
    categories = Category.objects.all()
    return render(request, 'gallery/index.html', {'categories': categories})

rawDescription: How to use a ForeignKey field as filter in Django?

description: Use a ForeignKey field as filter in Django


code:
'$' in s        # found
'$' not in s    # not found

# original answer given, but less Pythonic than the above...
s.find('$')==-1 # not found
s.find('$')!=-1 # found

rawDescription: How to check a string for specific characters?

description: Check a string for specific characters


code:
A = pd.DataFrame(np.random.choice(list("abcdef"), (1000, 1000)))
B = pd.DataFrame({'Code': ['a', 'b', 'c', 'd', 'e'],
                  'Value': ["'House'", "'Bike'", "'Lamp'", "'Window'", "'Car'"]})
B = B.set_index("Code")["Value"]

rawDescription: Mapping values from one DataFrame to another


code:
options = parser.parse_args()
if option.x:
    f()

rawDescription: Framework argparse - check if flag is set


code:
import subprocess
with open('CONTENT','w') as f1:
  p1 = subprocess.Popen(["sort", "CONTENT1"], stdout=subprocess.PIPE)
  p2 = subprocess.Popen(["uniq"], stdin=p1.stdout, stdout=f1)
  p1.stdout.close()

rawDescription: Redirecting Output of PIPE to a FILE in python


code:
#!/usr/bin/env python3
import re
from mmap import ACCESS_READ, mmap

blocks = []
match_size = re.compile(br'(\d+):').match
with open('blocks.netstring', 'rb') as file, \
     mmap(file.fileno(), 0, access=ACCESS_READ) as mm:
    position = 0
    for m in iter(lambda: match_size(mm, position), None):
        i, size = m.end(), int(m.group(1))
        blocks.append(mm[i:i + size])
        position = i + size + 1 # shift to the next netstring
print(blocks)

rawDescription: Python: write and read blocks of binary data to a file


code:
np.random.seed(123)
x = np.random.randint(0, 4, size=(8, 10))
x
y = x.reshape(x.shape[0]/2, 2, x.shape[1]/2, 2)

rawDescription: How can I vectorize the averaging of 2x2 sub-arrays of numpy array?

description: Vectorize the averaging of 2x2 sub-arrays of numpy array


code:
class Book(models.Model):
    title = models.CharField(max_length=100)
    summary = models.TextField()

    def summary_history(self):
        return SummaryHistory.objects.filter(book=self).order_by('-version')

    def save(self, *args, **kwargs):
        super(Book, self).save(*args, **kwargs)
        # save summary history
        summary_history = self.summary_history()
        if not summary_history or self.summary != summary_history[0].text:
            newSummary = SummaryHistory(book=self, text=self.summary)
            newSummary.save()

rawDescription: Store versioned history of Field in a Django model


rawDescription: Beautiful Soup Using Regex to Find Tags?

description: Beautiful Soup Using Regex to Find Tags


code: result = np.einsum('ijk,i -> jk', data, vector)

rawDescription: Numpy - Dot Product of a Vector of Matrices with a Vector of Scalars


code: u"sauté"

rawDescription: MySQL special chars lost when queried in Python. (But works with Django.)


code:
def partitions(set_):
    if not set_:
        yield []
        return
    for i in range(int(2**len(set_)/2)):
        parts = [set(), set()]
        for item in set_:
            parts[i&1].add(item)
            i >>= 1
        for b in partitions(parts[1]):
            yield [parts[0]]+b

l = [1, 2, 3, 4]
for p in reversed(sorted(partitions(l))):
    print(p)
print('The Bell number is', len(list(partitions(l))))

rawDescription: partition of a set or all possible subgroups of a list

description: Partition of a set or all possible subgroups of a list


code: re.sub(r'src="([^"]+)"', r'src="{{ \'\1\' | asset_url }}"', test, flags=re.MULTILINE)

rawDescription: Replacing html script element sources with regular expression in Python


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

urlpatterns = patterns('',
    url(r'^get_data/', views.get_mydata, name='get_mydata')
)

rawDescription: Processing URL Queries (Django & JSON)

description: Processing URL Queries (Django & JSON)


code:
import cv2
import numpy as np
import subprocess

THRESHOLD = 0.7

# your reference image
needle = cv2.imread('/path/to/some/needle.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
needle_height, needle_width = needle.shape

width, height = (800, 600) # you could of course detect this

command = [ '/usr/bin/ffmpeg',
        '-f', 'x11grab',
        '-i', ':0.0+0,0',
        '-r', '3', # lower frame-rate for testing
        '-s', '%dx%d' % (width, height),
        '-f', 'rawvideo',
        '-vcodec', 'rawvideo',
        '-pix_fmt', 'bgr24',
        '-']

pipe = subprocess.Popen(command, stdout = subprocess.PIPE, bufsize=10**8)

while(True):
    # get a frame
    raw = pipe.stdout.read(width*height*3)
    # format it into a matrix which can be worked on by openCV
    original = np.fromstring(raw, dtype='uint8').reshape((height, width, 3))
    pipe.stdout.flush()

    # transform it to grayscale for the matching
    haystack = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)

    # work through the frame looking for matches above a certain THRESHOLD
    # and mark them with a green circle
    for pt in np.transpose(np.where(cv2.matchTemplate(haystack, needle, cv2.TM_CCOEFF_NORMED) >= THRESHOLD)):
        cv2.circle(original, (pt[1] + needle_width/2, pt[0] + needle_height/2), 10, (0,255,0))

    # display the altered frame
    cv2.imshow('matches', original)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

rawDescription: Capture screen and find reference image

description: Capture screen and find reference image


A1: G

A5: G

A10: G

A5: R

A10: Y

Author: joe

rawDescription: How to custom-sort a list of dict to use in json.dumps

description: Custom-sort a list of dict to use in json.dumps


rawDescription: Converting a RGB color tuple to a six digit code, in Python


code: python -B

rawDescription: Make python reload() function read from .py instead of .pyc


code:
from django.db.models import Count
tags = Tag.objects.filter(content__title__icontains='matematica').values('name').annotate(tag_count=Count('tag'))

rawDescription: Django: counting tags of contents that match a query