code:
@pytest.fixture
def data():
    now = datetime.datetime.now()
    data = Data(now)
    return data

@pytest.fixture
def patch_db(monkeypatch):
    class FakeDbConnection:
        def __init__(*args, **kwargs):
            pass

        def fetchall(self):
            return fetch_result.values

    monkeypatch.setattr(DbConnection, 'execute', FakeDbConnection)

    class FetchResult:
        pass

    fetch_result = FetchResult()
    fetch_result.values = None
    return fetch_result

def test_get_somevalue(patch_db, data):
    patch_db.values = [1,2,3]
    userids = data.get_userids()
    assert userids == [1,2,3]

def test_get_something_else(patch_db, data):
    patch_db.values = [6,7,8]
    userids = data.get_userids()
    assert userids == [6,7,8]

rawDescription: pytest fixture params with monkeypatch

description: Pytest fixture params with monkeypatch


rawDescription: processing of list of list

description: Processing of list of list


code:
from mock import Mock
from any_valid import AnyValid, Int, OneOf
valid_bar = {
    'baz': AnyValid(Int(min=-3, max=14)),
    'qux': AnyValid(OneOf(['yes', 'no'])),
    }
mock = Mock(return_value=None)
mock('foo', bar={'baz': 4, 'qux': 'yes'})
mock.assert_called_once_with('foo', bar=valid_bar)

rawDescription: An easy way to mock loosely defined Python dict objects


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

N = 10000
mean = [0, 0]
cov = [[2, 2], [0, 2]]
x,y = np.random.multivariate_normal(mean, cov, N).T

plt.scatter(x, y, s=70, alpha=0.03)
plt.ylim((-5, 5))
plt.xlim((-5, 5))
plt.show()

rawDescription: Visualization of scatter plots with overlapping points in matplotlib


code:
from pyqtgraph.Qt import QtGui
import pyqtgraph as pg
from multiprocessing import Process, Array, Value, Lock
from serial_interface import EEG64Board
from collections import deque

def serialLoop(arr,idx,lock):
    eeg = EEG64Board(port='/dev/ttyACM0')
    eeg.openSerial()
    eeg.sendTest('1')        #Tells the eeg device to start sending data
    while True:
        data = eeg.readEEG() #Returns an array of the 8 latest values, one per channel
        if data != False:    #Returns False if bad checksum
            lock.acquire()
            for i in range(8):
                arr[i][idx.value] = data[i]
            idx.value += 1
            lock.release()
    eeg.sendTest('2')

arr = [Array('d',range(1024)) for i in range(8)]
idx = Value('i', 0)
q = [deque([],500) for i in range(8)]
iq = deque([],500)
lock = Lock()

lastUpdate = pg.ptime.time()
avgFps = 0.0

def graphLoop():
    global val,q,lock,arr,iq, lastUpdate, avgFps
    win = pg.GraphicsWindow()
    plt = list()
    for i in range(8):
        plt += [win.addPlot(row=(i+1), col=0, colspan=3)]
    #iplt = pg.plot(iq)
    counter = 0
    while True:
        lock.acquire()
        #time.sleep(.01)
        for i in range(idx.value):
            for j in range(8):
                q[j].append(arr[j][i])
        idx.value = 0
        lock.release()
        for i in range(8):
            plt[i].plot(q[i],clear=True)
        QtGui.QApplication.processEvents()
        counter += 1

        now = pg.ptime.time()
        fps = 1.0 / (now - lastUpdate)
        lastUpdate = now
        avgFps = avgFps * 0.8 + fps * 0.2

serial_proc = Process(target=serialLoop, args=(arr,idx,lock), name='serial_proc')
serial_proc.start()

graphLoop()

serial_proc.terminate()

rawDescription: What is most efficient Python IPC mechanism for plotting real-time serial data?


code:
p = ttk.Progressbar(parent, orient=HORIZONTAL, length=200, mode='determinate')
p['maximum'] = 100
for i in range(100):
    time.sleep(1)
    p['value'] = 1

rawDescription: Create a Progress bar that updates with a function [Python 3.4]


code: subprocess.Popen([sys.executable, "sub_script.py"], stdout=open("log.txt", "a"))

rawDescription: Directing script output to a file using subprocess?


code:
import linecache

filename = 'observations1.txt'

# Start at 44th line
curline = 44
lines = []

# Keep looping until no return string is found
# getline() never throws errors, but returns an empty string ''
# if the line wasn't found (if the line was actually empty, it would have
# returned the newline character '\n')
while linecache.getline(filename, curline):
    for i in xrange(75):
        lines.append(linecache.getline(filename, curline).rstrip())
        curline += 1

    # Perform work with the set of observation lines
    add_to_observation_log(lines)

    # Skip the unnecessary section and reset the lines list
    curline += 4
    lines = []

rawDescription: Quickest way of Importing 500GB Text File taking only the sections wanted


code:
def indent(elem, level=0):
    i = "\n" + level*"  "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

rawDescription: Changing the default indentation of etree.tostring in lxml


code:
import gzip
import cStringIO

def decompressStringToFile(value, outputFile):
  """
  decompress the given string value (which must be valid compressed gzip
  data) and write the result in the given open file.
  """
  stream = cStringIO.StringIO(value)
  decompressor = gzip.GzipFile(fileobj=stream, mode='r')
  while True:  # until EOF
    chunk = decompressor.read(8192)
    if not chunk:
      decompressor.close()
      outputFile.close()
      return
    outputFile.write(chunk)

def compressFileToString(inputFile):
  """
  read the given open file, compress the data and return it as string.
  """
  stream = cStringIO.StringIO()
  compressor = gzip.GzipFile(fileobj=stream, mode='w')
  while True:  # until EOF
    chunk = inputFile.read(8192)
    if not chunk:  # EOF?
      compressor.close()
      return stream.getvalue()
    compressor.write(chunk)

rawDescription: Which is the best way to compress json to store in a memory based store like redis or memcache?


code: [m.start() for m in re.finditer('(?={0})'.format(re.escape(p3)), p1)]

rawDescription: Python - Find all occurances of a substring (including overlap)?


code:
from itertools import compress

l = ['','7','','49','','173','158']
ret = compress(l, map(lambda x: bool(x), l))
print(list(ret))

rawDescription: Delete empty value from (sorted) dictionary


code:
s = 'badcfe'
''.join([ s[x:x+2][::-1] for x in range(0, len(s), 2) ])

rawDescription: What is the simplest way to swap char in a string with Python?


code:
def post(self):
  category = self.request.get('category')
  if category:
    user = users.get_current_user()
    if user:
      logout = users.create_logout_url(self.request.uri)
      global visits
      category = self.request.get('category')
      posts = db.GqlQuery("select * from Post where category=:1 order by created desc limit 30", category)
      self.render("home.html", posts=posts, user=user, visits=visits, logout=logout)
    else:
      self.redirect(users.create_login_url(self.request.uri))

rawDescription: POST method does the correct post and then rerenders the whole page


code:
text = "AED|784|2|United Arab Emirates dirham|United Arab Emirates"

spl = text.split("|")

var1, var2, var3, var4, var5 = spl
print(var1)
print (spl)
AED
['AED', '784', '2', 'United Arab Emirates dirham', 'United Arab Emirates']

rawDescription: Extract substrings from string in python


code:
from Tkinter import *
root = Tk()
l = Listbox(root, width = 15)
l.pack()
l.insert(END, "Hello")
l.insert(END, "world")
l.insert(END, "here")
l.insert(END, "is")
l.insert(END, "an")
l.insert(END, "example")

def close():
    global l, root
    items = l.get(0, END)
    print(items)
    root.destroy()

b = Button(root, text = "OK", command = close).pack()
root.mainloop()

rawDescription: Getting selected items from a Tkinter listbox without using a listbox bind


code:
import inspect
import decorator

def func(f1, kw='default'):
    pass
bare_argspec = inspect.getargspec(func)
print(bare_argspec)
# ArgSpec(args=['f1', 'kw'], varargs=None, keywords=None, defaults=('default',))

@decorator.decorator
def mydecorator(func,*args,**kw):
    result=func(*args,**kw)
    return result

@mydecorator
def func2(f1, kw='default'):
    pass
decorated_argspec = inspect.getargspec(func2)
print(decorated_argspec)
# ArgSpec(args=['f1', 'kw'], varargs=None, keywords=None, defaults=('default',))

assert(bare_argspec==decorated_argspec)

rawDescription: How can I programmatically change the argspec of a function in a python decorator?

description: Programmatically change the argspec of a function in a python decorator


code:
from multiprocessing import Process, Queue

def data(q):
    dosmth()
    processes = []
    for i in range(5):
        fetch_data()
        q.put('data has been added')
        processes.append(Process(target=analysis, args=(q,)))
    for p in processes:
        p.start()
    for p in processes:
        p.join()

def analysis(q):
    msg = q.get()
    print(msg)
    process_data()

if __name__ == '__main__':
    q = Queue()
    processes = [Process(target=data, args=(q,))]
    for p in processes:
        p.start()
    for p in processes:
        p.join()

rawDescription: Repeatedly spawning the same process

description: Repeatedly spawning the same process


code:
from Tkinter import *
root = Tk()

search_label = Label(root, text="Enter search here:")
search_entry = Entry(root)
search_label.pack()
search_entry.pack()

def answer_box(query):
    return query

def go():
    query=search_entry.get()
    bibliography.delete('1.0', END)
    bibliography.insert(INSERT, answer_box(query))

go_button = Button(root, text="Go!", width=10, command=go)
go_button.pack()
bibliography = Text(root)
bibliography.pack()

root.mainloop()

rawDescription: Tkinter only works once

description: Tkinter only works once


code:
class MyModelResource(Resource):
    class Meta:
        qs = MyModel.objects.all()

    def dehydrate(self, bundle):
        # remove unneeded point-field from the response data
        del bundle.data['point']
        # add required fields back to the response data in the form we need it
        bundle.data['lat'] = bundle.obj.point.y
        bundle.data['lng'] = bundle.obj.point.x
        return bundle

rawDescription: Return Latitude and Longitude values from a PointField using tastypie


code: newrow = row[:8] + row[-2:]

rawDescription: Python - CSV: Large file with rows of different lengths


code:
bits = urlparse.urlparse('http://www.example.com/foo?bar=zoo&a=b')
bits.query
urlparse.parse_qs(bits.query)

rawDescription: Inverse operation to Python's urllib.urlencode?


code:
prob_y = []
prob_x = []

for line in open(data_file_name):
    line = line.split(None, 1)
    # In case an instance with all zero features
    if len(line) == 1: line += ['']
    label, features = line
    xi = {}
    for e in features.split():
        ind, val = e.split(":")
        xi[int(ind)] = float(val)
    prob_y += [float(label)]
    prob_x += [xi]

return (prob_y, prob_x)

rawDescription: liblinear memory cost too much

description: Liblinear memory cost too much


code:
def last_occurence(s, char):
    found_at = -1
    index = 0
    for s_char in s:
        if s_char == char:
            found_at = index
        index += 1

    return found_at

rawDescription: Find last occurrence of character in string without built-in function


code:
from geventwebsocket.handler import WebSocketHandler
from gevent.pywsgi import WSGIServer
from flask import Flask, request
from time import sleep
from cStringIO import StringIO

import pygame
pygame.init()

import SimpleCV as scv

app = Flask(__name__)
cam = scv.Camera(0)

@app.route('/camera')
def camera():

    if request.environ.get('wsgi.websocket'):
        ws = request.environ['wsgi.websocket']

        while True:
            fp = StringIO()
            image = cam.getImage().flipHorizontal().getPIL()
            image.save(fp, 'JPEG')
            ws.send(fp.getvalue().encode("base64"))
            #fp.close() << benchmark and memory tests needed
            sleep(0.5)

if __name__ == '__main__':
    http_server = WSGIServer(('',5000), app, handler_class=WebSocketHandler)
    http_server.serve_forever()

rawDescription: How to send a pygame image over websockets?

description: Send a pygame image over websockets


code: "{foo}, {bar}".format(foo=2, bar=4)

rawDescription: Python Insert Variables into String

description: Python Insert Variables into String


code:
a = True
a+a
import numpy as np
np.array([a])
np.array([a]) + np.array([a])

rawDescription: Pandas: Sum of two boolean series

description: Pandas: Sum of two boolean series


code: lista.replace("/" + dia + "/", "/" + dict_months[int(dia)] + "/")

rawDescription: Search a key in a dict and assing the value of that key to a variable Python


code:
delete_memo = []
for i in xrange(len(names)):
    if CONDITION_FOR_DELETION:
        delete_memo.append(i)
delete_memo.reverse()
for i in delete_memo:
    del names[i]
    del data1[i]
    del data2[i]

rawDescription: Iteratively removing list items without altering indices, is using a 'DELETEME' string a sound approach?


code:
import matplotlib.pyplot as plt
import random
dictionary = {i: random.choice([0] * 100 + list(range(100))) for i in range(100)}
new_dict = {i: j for i, j in dictionary.items() if j}
keys = list(new_dict.keys())
values = list(new_dict.values())

plt.stem(keys, values, markerfmt='ro', linefmt='-r')
plt.show()

rawDescription: using marker in condition in matplotlib

description: Using marker in condition in matplotlib


code: foo_list = request.GET.getlist('foo[]')

rawDescription: Python/Django: How to deserialize foo[]=1&foo[]=2&


code:
from multiprocessing import Pool

def func(fn):
    st = readWaveform(fn, bpfreq=[5.,30.])
    coinc = CoincidenceTimes(st)
    triggerlist += coinc.getCoincTimes()
    events += cutWithList(wfpath=fn, trigg=coinc.getCoincTimes(),
    station=self.getStation())

if __name__ == '__main__':
    p = Pool(4) # if you have 4 cores in your processor
    p.map(func, wfpathlist)

rawDescription: Can someone help me with parallel processing for Python?


code:
mock_class = MagicMock(spec=[u'a'])
mock_class.a = MagicMock(spec=[u'a'])
hasattr(mock_class.a, u'c')  # returns False

rawDescription: How to Mock a missing attribute

description: Mock a missing attribute


code:
def replacer_factory1(dictionary):
    def replacing(match):
        if len(dictionary) > 0:
            word = match.group()[:-1]
            exchange = dictionary.get(word, word)
            spintax = '{' + exchange + '}'
            create_place_holder = spintax.replace(' ', '#!#')
            return create_place_holder + match.group()[-1]
        else:
            return ""
    return replacing

def replacing1(text):
    regex_patt_list = r'\b(?:' + '|'.join(patt_list) + r')\W'
    replacer = replacer_factory1(dictionary)
    return re.sub(regex_patt_list, replacer, text)

rawDescription: Python: Why is re.sub not replacing dict key with dict value when there is ',' in dict key


code:
from Tkinter import *
import sys

class popupWindow(object):
    def __init__(self,master):
        top=self.top=Toplevel(master)
        self.l=Label(top,text="Hello World")
        self.l.pack()
        self.e=Entry(top)
        self.e.pack()
        self.b=Button(top,text='Ok',command=self.cleanup)
        self.b.pack()
    def cleanup(self):
        self.value=self.e.get()
        self.top.destroy()

class mainWindow(object):
    def __init__(self,master):
        self.master=master
        self.b=Button(master,text="click me!",command=self.popup)
        self.b.pack()
        self.b2=Button(master,text="print value",command=lambda: sys.stdout.write(self.entryValue()+'\n'))
        self.b2.pack()

    def popup(self):
        self.w=popupWindow(self.master)
        self.master.wait_window(self.w.top)

    def entryValue(self):
        return self.w.value

if __name__ == "__main__":
    root=Tk()
    m=mainWindow(root)
    root.mainloop()

rawDescription: Creating a popup message box with an Entry field


code:
import numpy as np

# randomly initialize the RNG from some platform-dependent source of entropy
np.random.seed(None)

# get the initial state of the RNG
st0 = np.random.get_state()

# draw some random numbers
print(np.random.randint(0, 100, 10))
# [ 8 76 76 33 77 26  3  1 68 21]

# set the state back to what it was originally
np.random.set_state(st0)

# draw again
print(np.random.randint(0, 100, 10))
# [ 8 76 76 33 77 26  3  1 68 21]

rawDescription: How can I retrieve the current seed of NumPy's random number generator?

description: Retrieve the current seed of NumPy's random number generator


code:
t = [[[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]],
     [((3,), (2,), (2, 2), (2,)),
      ((2,), (1, 2), (2,), (0,))]]

board_size = len(t[1][0])
cells = t[0]
hnums = t[1][0] # These are the horizontal numbers
vnums = t[1][1] # These are the vertical numbers

# Here I'm building new lists using list comprehension.
# I've noticed that some of the numbers are missing the second element.
# For example here ((2,), (1, 2), (2,), (0,)), only the second element (1, 2) has two members. So I had to make sure this condition is taken care of by adding this x[1] if len(x) > 1 which means only take the second element if the length of the total tuple is more than 1.
# If you haven't seen list comperhension before, you need to look them up because they are a huge topic.
# at the end of these lines we will have something like this
# h1 = [' ', ' ', 2, ' ']
# h2 = [3, 2, 2, 2]

h1 = [x[1] if len(x) > 1 else " " for x in hnums]
h2 = [x[0] for x in hnums]
v1 = [x[1] if len(x) > 1 else " " for x in vnums]
v2 = [x[0] for x in vnums]

# Here I'm building the two horizontal lines
# heading_string will look like this '  {}    {}    {}    {}  ' because the board size is 4. Notice that this will work with other sizes if you changed the t variable.
heading_string = "  {}  " * board_size
# Here I'm just calling the format function and passing in the h1 variable.
# The * will unpack the list.
# The end result will be '  {}    {}    {}    {}  '.format(' ', ' ', 2, ' ')
# So each parameter of the format function will be replaced with the {} placeholder
print(heading_string.format(*h1))
print(heading_string.format(*h2))

# Here I'm just defining a function to take care of the logic of the cell text
def cell_text(c):
    if c == [0]:
        return "?"
    elif c == [1]:
        return "."
    else:
        return "X"

row = [] # a row list that will represent the each row of cells
# for loop using enumerate and starting from 1. So i will equal to 1 in the first loop
for i, cell in enumerate(cells, 1):
    row.append(cell_text(cell)) # appending cells to the row list

    # if i in the current loop is 4, 8, 12, 16 this condition will be true and we need to break the row to create a new one for the next row
    if i % board_size == 0:
        row_string = "[ {} ]" * board_size # Same thing as heading_string
        # Here I'm building the vertical numbers string
        # The numbers are stored in v1 and v2
        # and they both have 4 members indexed 0, 1, 2, 3
        # That's why I'm subtracting 1 from i because i starts from 1 not from 0 because of enumerate(cells, 1).
        # This will work fine for the first four elements
        # but in the fifth loop, i = 4 and we actually need v1[0]
        # and that's why I'm dividing by the board size to get the correct index
        vnum_string = " " + str(v2[(i-1) // board_size]) + " " \
                          + str(v1[(i-1) // board_size]) + " |"
        print(row_string.format(*row) + vnum_string) # Same as before except that I'm adding the vertical numbers string
        row = [] # Reset the row list

rawDescription: python 3 printing a picross board

description: Python 3 printing a picross board


code:
Dlg = wx.TextEntryDialog(panel, 'Enter your word:',"New word","",
                style=wx.OK|wx.CANCEL|wx.CENTRE,pos=(100,200))

if Dlg.ShowModal() == wx.OK:
    test = Dlg.GetValue()
del Dlg

rawDescription: How to make a text dialogue appear in wxpython

description: Make a text dialogue appear in wxpython


code:
from tkinter import *

root = Tk()
menu_bar = Menu(root)

def clicked(menu):
    menu.entryconfigure(1, label="Clicked!")

file_menu = Menu(menu_bar, tearoff=False)
file_menu.add_command(label="An example item", command=lambda: clicked(file_menu))
menu_bar.add_cascade(label="File", menu=file_menu)

root.config(menu=menu_bar)
root.mainloop()

rawDescription: Update label of tkinter menubar item?

description: Update label of tkinter menubar item


code:
import re
s = 'ABCa4.daf<<tag1>>permission : wiadsfth.accedsafsds.INTERNET<<tag2>>'
re.findall('<<tag1>>.*?([A-Z]+)<<tag2>>', s)

rawDescription: how to output uppercases with regex using python

description: Output uppercases with regex using python


code:
from urllib2 import urlopen
from bs4 import BeautifulSoup

url = 'http://www.sec.gov/Archives/edgar/data/70858/000119312507058027/0001193125-07-058027.txt'
soup = BeautifulSoup(urlopen(url))
print(soup.document)

rawDescription: how to delete a part of a text in python

description: Delete a part of a text in python


rawDescription: What is a pandas approach to counting text that matches condition?


code:
def get_listening_socket():
    sock = socket.socket()
    sock.setblocking(0)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind(('0.0.0.0', 5555))
    sock.listen(0)
    return sock

sock = get_listening_socket()

LIMIT = 1
conns = {sock}
while True:
    readable, writable, exceptional = select.select(conns, [], [])
    if sock in readable: # new connection on the listening socket
        conn, caddr = sock.accept()
        conns.add(conn)
        if len(conns) > LIMIT: # ">" because "sock" is also in there
            conns.remove(sock)
            sock.close()
    else: # reading from an established connection
        for c in readable:
            buf = c.recv(4096)
            if not buf:
                conns.remove(c)
                sock = get_listening_socket()
                conns.add(sock)
            else:
                print("received: %s" % buf)

rawDescription: How to limit the number of connections to a socket and trigger timeout on client (Python)

description: Limit the number of connections to a socket and trigger timeout on client (Python)


code: re.findall("[A-Za-z]+", sentence)

rawDescription: Regular expression to capture all groups in a Kleene Plus closure


code:
with open(file_1) as infile, open(file_2, 'w') as outfile:
    for line in infile:
        if line.startswith('PROFILE'):
            # found the start of a PROFILE, continue reading with a nested loop
            for line in infile:
                if not line.startswith(' '):
                    # line no longer starts with a space, found the end
                    # of the section
                    break
                number = line.split()[0]
                outfile.write(number + '\n')

rawDescription: how to recognize certain character and save strings after it

description: Recognize certain character and save strings after it


rawDescription: python: how to get numbers after decimal point?

description: Python: how to get numbers after decimal point


code:
import calendar
import re
import time
from urllib.request import urlopen

import lxml.html

# define base url with placeholders for dynamic parameters
BASE_URL = "http://f10.eastmoney.com/f10_v2/BackOffice.aspx?timetip=%s000&command=RptF10AssetStatement&paramReportTime=Year&paramCode=%s&paramNum=%s"
PATTERN = re.compile("\('(\d+)','(\d+)',")

# parse initial page
url = "http://f10.eastmoney.com/f10_v2/FinanceAnalysis.aspx?code=sz300059"
root = lxml.html.parse(urlopen(url))

# extract js function arguments from `onclick` attribute value
li = root.xpath('//div[@class="content"][4]/div/ul/li')[1]
code, num = PATTERN.search(li.attrib['onclick']).groups()

# get the current time
seconds_since_epoch = calendar.timegm(time.gmtime())

# parse second page
url = BASE_URL % (seconds_since_epoch, code, num)
root = lxml.html.parse(urlopen(url))

# get table titles (just for example)
for row in root.xpath('//tr/th/span'):
    print(row.text)

rawDescription: how to extract the designated div table data in lxml?

description: Extract the designated div table data in lxml


code:
import re
...
p.extend(re.split('[ ,]', lines))

rawDescription: Can two conditions be given for python's string.split function?


code:
media_list = [
    'A00{}'.format(i) for i in range (1,10)
]

nth_item = 4

str_list = [
    '{}\tOR\n'.format(m)
    if(((media_list.index(m)+1) % nth_item) == 0)
    else
    '{}\tOR'.format(m)
    for m in media_list
]

print('\n'.join(str_list))

rawDescription: Python: Printing a list with a line break after every nth item


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

x = np.random.rand(100)
y = np.random.rand(100)
target = np.random.randint(1,9, size=100)

df = pd.DataFrame(dict(points1=x, points2=y, target=target))
colors = {1: 'green', 2:'red', 3:'blue', 4:'yellow', 5:'orange', 6:'pink', \
              7:'brown', 8:'black', 9:'white'}
fig, ax = plt.subplots()

for k,v in colors.items():
    series = df[df['target'] == k]
    scat = ax.scatter(series['points1'], series['points2'], c=v, label=k)

plt.legend()

rawDescription: Legend in matplotlib

description: Legend in matplotlib


code:
from datetime import date, timedelta

def get_month(year, week, day):
    january_first = date(year, 1, 1)
    monday_date = january_first + timedelta(days=week * 7 - january_first.weekday())

    for d in range(7):
        weekday_date = monday_date + timedelta(days=d)
        if weekday_date.day == day:
            return weekday_date.month

rawDescription: Calculating month-number in python

description: Calculating month-number in python


code:
import numpy as np
from scipy.optimize import fmin_bfgs
import io
data = np.loadtxt('ex2data1.txt',delimiter=",")
m,n = data.shape
X = np.array(np.column_stack((np.ones(m),data[:,:-1])))
y = np.array(data[:,2].reshape(m,1))
theta = np.array(np.zeros(n).reshape(n,1))

def sigmoid(z):
    return 1/(1+np.exp(-z))

def hypothesis(X,theta):
    return sigmoid( X.dot(theta) )

def cost(theta):
    h = hypothesis(X,theta)
    cost = (-y.T.dot(np.log(h))-(1-y).T.dot(np.log(1-h)))/m
    r = cost[0]
    if np.isnan(r):
        return np.inf
    return r

def gradient(theta):
    theta = theta.reshape(-1, 1)
    h = hypothesis(X,theta)
    grad = ((h-y).T.dot(X)).T/m
    return grad.flatten()

def fmin():
    initial_theta=np.zeros(n)
    theta=fmin_bfgs(cost,initial_theta,fprime=gradient)
    return theta

theta = fmin()

rawDescription: Logistic regression: objects are not aligned


code:
def enum(cls):
  names = getattr(cls, 'names', None)
  if names is None:
    raise TypeError('%r must have a class field `names` to be an `enum`!',
                    cls.__name__)
  for i, n in enumerate(names):
    setattr(cls, n, i)
  return cls

@enum
class Color(object):
  names = 'red green blue'.split()

rawDescription: Decorators applied to class definition with Python


code:
res_all = []
for res in results:
    low, upp = res.confint().T   # unpack columns
    res_all.append(numpy.concatenate(([res.llf], res.params, res.tvalues, res.pvalues,
                   low, upp)))

rawDescription: Python 2.7 - statsmodels - formatting and writing summary output


code:
root = tree.invisibleRootItem()
for item in tree.selectedItems():
    (item.parent() or root).removeChild(item)

rawDescription: Delete QTreeWidgetItem in PyQt?

description: Delete QTreeWidgetItem in PyQt


code:
import urllib
urllib.unquote(urllib.unquote("what%2527s%2bthe%2btime%252c%2bnow%253f") )

rawDescription: Python decode text to ascii

description: Python decode text to ascii


code: [list(x) for x in zip(lsta, lstb, lstc)]

rawDescription: How to turn multiple lists into a list of sublists where each sublist is made up of the same index items across all lists?

description: Turn multiple lists into a list of sublists where each sublist is made up of the same index items across all lists


rawDescription: Pylab plotting with a table

description: Pylab plotting with a table


code:
from pylab import figure, show, rand
from matplotlib.patches import Ellipse
import numpy as np
import matplotlib.pyplot as plt
N = 25

# ellipse centers
xy = np.random.rand(N, 2)*10
# ellipse eccentrities
eccs = np.random.rand(N) * 0.8 + 0.1

fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')

A = 0.1
for pos, e in zip(xy, eccs):
    # semi-minor, semi-major axes, b and a:
    b = np.sqrt(A/np.pi  * np.sqrt(1-e**2))
    a = A / np.pi / b
    ellipse = Ellipse(xy=pos, width=2*a, height=2*b)
    ax.add_artist(ellipse)

ax.set_xlim(0, 10)
ax.set_ylim(0, 10)

show()

rawDescription: matplotlib scatterplot: adding 4th dimension by the marker shape

description: Matplotlib scatterplot: adding 4th dimension by the marker shape


code:
while True:
    input_A = GPIO.input(26)
    input_B = GPIO.input(19)
    input_C = GPIO.input(13)
    input_D = GPIO.input(6)

    if input_A == True:
            print('A was pushed')

    if input_B == True:
            print('B was pushed')

    if input_C == True:
            print('C was pushed')

    if input_D == True:
            print('D was pushed')

    sleep(1.5);

rawDescription: My While True loop is getting stuck in python


rawDescription: django queryset from a list or set

description: Django queryset from a list or set


code:
import os

def same_fs(file1, file2):
    dev1 = os.stat(file1).st_dev
    dev2 = os.stat(file2).st_dev
    return dev1 == dev2

rawDescription: Is a file on the same filesystem as another file in python?


code:
import datetime

#parse json to get offset(import json; json.load(resonse); etc.)
#offset is gmtoffset + dstoffset
datetime.datetime.utcnow() + datetime.timedelta(seconds = offset)

rawDescription: how to calculate the local time from gmtoffset

description: Calculate the local time from gmtoffset


code:
url ="http://www.amazon.com/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords=digital+camera"
import urllib2
from bs4 import BeautifulSoup
req = urllib2.Request(url, headers={ 'User-Agent': 'Mozilla/5.0' })
html = urllib2.urlopen(req).read()
soup = BeautifulSoup(html)
links = soup.find_all('a')
for l in links:
    href = l.get('href')
    title = l.get('title', '')
    if 'Sony W800/B 20.1 MP Digital' in title:
        print(href)  # prints: http://www.amazon.com/Sony-W800-Digital-Camera-Black/dp/B00I8BIBCW/ref=sr_1_2/183-0842534-8993425?s=photo&ie=UTF8&qid=1421400650&sr=1-2&keywords=digital+camera

rawDescription: scrape href of each product from website using python

description: Scrape href of each product from website using python


code:
from netaddr import *

allnets = IPSet(['0.0.0.0/0'])
ip = IPSet(['8.8.8.8', '8.8.8.4'])
filtered = allnets - ip

assert '8.8.8.8' not in filtered
assert '8.8.8.4' not in filtered
assert '8.8.8.7' in filtered
assert '192.0.2.17' in filtered
assert '203.0.113.1' in filtered

rawDescription: Find subtract IPSet

description: Find subtract IPSet


code:
with open("./the_file.txt", 'r') as f:
    answer = [ast.literal_eval(line.strip()) for line in f]

rawDescription: Python read a txt file into a list of lists of numbers


code:
def print_monthly_totals(input_csv_filename):
    with open(input_csv_filename) as csv_file:
        data = csv_file.readlines()
        print('Rainfall totals for each month')
        for line in data:
            manage_line(line)

def manage_line(line):
        columns = line.split(',')
        month = int(columns[0])
        num_days = int(columns[1])
        trf = rainfall_total(columns, num_days)
        print('Month {:2}: {:.1f}'.format(month, trf))

def rainfall_total(columns, num_days):
    total_rainfall = 0
    for col in columns[2 : 2 + num_days]:
        total_rainfall += float(col)
    return total_rainfall

print_monthly_totals('rainfalls2011.csv')

rawDescription: Breaking down a function into more manageable segments


code:
def loadFile(self):
    selFile = QtGui.QFileDialog.getOpenFileName()
    if self.sender() == self.qFileButton:
        self.qFileTF.setText(selFile)
    elif self.sender() == self.lhFileTF:
        self.lhFileTF.setText(selFile)
    else:
        self.oldQTF.setText(selFile)

rawDescription: PyQt4 - How to connect different signals to the same callable, but be able to differntiate where the signal came from and act accordingly


rawDescription: Numpy index, get bands of width 2

description: Numpy index, get bands of width 2


code:
import win32com.client, pythoncom, time

def word(wordfile):
    pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED)
    myWord = win32com.client.DispatchEx('Word.Application')
    myDoc = myWord.Documents.Open(wordfile, False, False, False)

    ...

    myDoc.Close()
    myWord.Quit()
    del myDoc
    del myWord
    pythoncom.CoUninitialize()

rawDescription: Opening word document with read mode using python


code:
if '__name__' == '__main__':
    from mainmodule import main
    main()

rawDescription: Preventing pool processes from importing __main__ and globals


code:
an_re = r"(\d+[A-Z])|([A-Z]+\d)[\dA-Z]*"
re.search(an_re, '12345', re.I) # not acceptable string
re.search(an_re, 'abcd', re.I) # not acceptable string
re.search(an_re, 'abc1', re.I) # acceptable string
re.search(an_re, '1abc', re.I)

rawDescription: Python Alphanumeric Regex

description: Python Alphanumeric Regex


code: df1.join(df2).select("person", "dog", (df1.score * df2.score).alias("product"))

rawDescription: Pyspark: multiplying columns from different tables


code:
book = db.get(bookkey)
q = book.updatepoint_set
q.order('date')
ups = q.fetch(50)

rawDescription: Datastore access optimization

description: Datastore access optimization


code:
commons = set(dict1).intersection(set(dict2))
list1 = [dict1[k] for k in commons]
list2 = [dict2[k] for k in commons]

rawDescription: Compare two large dictionaries and create lists of values for keys they have in common


code:
def __getstate__(self):
    state = self.__dict__.copy()
    del state['_sa_instance_state']
    return state

def __setstate__(self, state):
    self.__dict__.update(state)

rawDescription: How to exclude specific fields on serialization with jsonpickle?

description: Exclude specific fields on serialization with jsonpickle


code:
from lxml.html import parse
import urllib

SOURCE = 'http://stackoverflow.com/unanswered'

htmlpage = urllib.urlopen(SOURCE)
tree = parse(htmlpage).getroot()

print (tree.xpath('//div[1]'))

htmlpage.close()

rawDescription: libxml2.parseFile for online files?

description: Libxml2.parseFile for online files


code:
from contextlib import closing
from zipfile import ZipFile, ZIP_DEFLATED

from google.appengine.ext import webapp
from google.appengine.api import urlfetch

def addResource(zfile, url, fname):
    # get the contents
    contents = urlfetch.fetch(url).content
    # write the contents to the zip file
    zfile.writestr(fname, contents)

class OutZipfile(webapp.RequestHandler):
    def get(self):
        # Set up headers for browser to correctly recognize ZIP file
        self.response.headers['Content-Type'] ='application/zip'
        self.response.headers['Content-Disposition'] = \
            'attachment; filename="outfile.zip"'

        # compress files and emit them directly to HTTP response stream
        with closing(ZipFile(self.response.out, "w", ZIP_DEFLATED)) as outfile:
            # repeat this for every URL that should be added to the zipfile
            addResource(outfile,
                'https://www.google.com/intl/en/policies/privacy/',
                'privacy.html')
            addResource(outfile,
                'https://www.google.com/intl/en/policies/terms/',
                'terms.html')

rawDescription: Is it possible to generate and return a ZIP file with App Engine?


code:
import os
FILE = os.path.splitext(FILE)[0]

rawDescription: Python: A way to detect a filetype attached to a string?


code:
def reduce(function, iterable, initializer=None):
    it = iter(iterable)
    if initializer is None:
        value = next(it)
    else:
        value = initializer
    for element in it:
        value = function(value, element)
    return value

rawDescription: Reduce function with three parameters

description: Reduce function with three parameters


code:
class Meta(type):
    def __delattr__(cls, attr):
        if attr=='b':
            raise TypeError("Bad boy/girl, you shouldn't delete attribute b")
        elif attr=='a':
            super(Meta, cls).__delattr__(attr)

class klass(metaclass=Meta):
    a = 1
    b = 2

print(hasattr(klass, 'a'))  # True
del klass.a
print(hasattr(klass, 'a'))  # False
del klass.b  # throws TypeError exception

rawDescription: How to filter which attributes I want to delete in Python class?

description: Filter which attributes I want to delete in Python class


code:
import gdb
import threading
import pysigset, signal # Import these packages!

def plot_thread():
    import time
    while True:
        print('Placeholder for a window event loop.')
        time.sleep(1)
        pass
    pass

class PlotterCommand(gdb.Command):
    def __init__(self):
        super(PlotterCommand, self).__init__("plot",
                                            gdb.COMMAND_DATA,
                                            gdb.COMPLETE_SYMBOL)
        self.dont_repeat()
        pass

    def invoke(self, arg, from_tty):
        with pysigset.suspended_signals(signal.SIGCHLD): # Disable signals here!
            plot_thread_instance=threading.Thread(target=plot_thread)
            plot_thread_instance.daemon=True
            plot_thread_instance.start()
            pass
        pass

    pass

PlotterCommand()

rawDescription: GDB hangs when multi-threaded python extension is used to debug multi threaded program


code:
from PyQt4.QtGui import *
from PyQt4.QtCore import QUrl
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest

app = QApplication([])
url = "http://www.google.com/favicon.ico"
lbl = QLabel("loading...")
nam = QNetworkAccessManager()

def finishRequest(reply):
    img = QImage()
    img.loadFromData(reply.readAll())
    lbl.setPixmap(QPixmap(img))

nam.finished.connect(finishRequest)
nam.get(QNetworkRequest(QUrl(url)))
lbl.show()
app.exec_()

rawDescription: Setting QIcon() pixmap from URL

description: Setting QIcon() pixmap from URL


code:
CommentInfo = namedtuple('CommentInfo', ["stt", "gid", "uid"])
x = CommentInfo(stt=1,gid=12,uid=222)
x._asdict()

rawDescription: How to convert nametuple to dict in python

description: Convert nametuple to dict in python


rawDescription: wxpython: small square panel in corner

description: Wxpython: small square panel in corner


code:
def day(d, m):
    day = (sum((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)[:m-1]) + d + 3) % 7
#           ^                                                     ^      ^   ^  ^ ^
#           '---- adding up the days in the months                |      |   |  | |
#                   up to but not including the current month ----'      |   |  | |
#                                  plus the current day of the month ----'   |  | |
#                                  and the day of the week on 12/31/2013 ----'  | |
#                    modulus (%) is what's left over after integer division ----' |
#                                                        seven days in a week ----'

rawDescription: How to calculate day of the week provided the day and month

description: Calculate day of the week provided the day and month


code:
import colander

def values_are_singularly_typed_arrays(node, mapping):
    for val in mapping.values():
        if not isinstance(val, list):
            raise colander.Invalid(node, "one or more value(s) is not a list")
        if not len(set(map(type, val))) == 1:
            raise colander.Invalid(node, "one or more value(s) is a list with mixed types")

class MySchema(colander.MappingSchema):
    data = colander.SchemaNode(
        colander.Mapping(unknown='preserve'),
        validator=values_are_singularly_typed_arrays
    )

def main():
    valid_data = {
        'data' : {
            'numbers' : [1,2,3],
            'reals' : [1.2,3.4,5.6],
        }
    }
    not_list = {
        'data' : {
            'numbers' : [1,2,3],
            'error_here' : 123
        }
    }
    mixed_type = {
        'data' : {
            'numbers' : [1,2,3],
            'error_here' : [123, 'for the watch']
        }
    }

    schema = MySchema()
    schema.deserialize(valid_data)

    try:
        schema.deserialize(not_list)
    except colander.Invalid as e:
        print(e.asdict())

    try:
        schema.deserialize(mixed_type)
    except colander.Invalid as e:
        print(e.asdict())

if __name__ == '__main__':
    main()

rawDescription: colander schema for mapping where keys are variable but value are arrays

description: Colander schema for mapping where keys are variable but value are arrays


code:
with open("file1.txt","r") as fin, open("file2.txt","w") as fout:
    string = raw_input("Enter the String:")
    for line in fin:
        if string in line:
            fout.write(line)
            try:
                while ')' not in line:
                    line = next(fin)
                    fout.write(line)
            except StopIteration:
                pass  # ran out of file to read

rawDescription: How to copy the specific lines from file in python

description: Copy the specific lines from file in python


code:
from yourscrapyprojecthere import settings

settings.PROJECT_ROOT

rawDescription: Determine the scrapy root directory

description: Determine the scrapy root directory


code: zipped = zip(mylist[0::2], mylist[1::2])

rawDescription: Collect every pair of elements from a list into tuples in Python


code:
if __name__ == "__main__":
    firstFunctionEver()

rawDescription: Run python file -- what function is main?


rawDescription: Gridllines on only the left axis

description: Gridllines on only the left axis


rawDescription: How to draw this window?

description: Draw this window


code:
from multiprocessing import Pool
def f(x):
     return x*x

p = Pool(5)
p.map(f, [1,2,3])

rawDescription: multiprocess a function with a element of list as argument

description: Multiprocess a function with a element of list as argument


code:
# create dummy data
import pandas as pd
rng = pd.date_range('1/1/2011', periods=90, freq='h')
# shift a copy of the date column and subtract from the original date
df = pd.DataFrame({'value':range(1,91),'date':rng})
df['time_gap'] = df['date']- df['date'].shift(1)

rawDescription: How to find the difference between each subsequent pair of DataFrame.index values in pandas?

description: Find the difference between each subsequent pair of DataFrame.index values in pandas


code:
# returns a surface with the circle drawn onto it
def render_transparent_circle(color, radius, width=0):
    size = radius * 2
    temp_surf = pygame.Surface((size, size), SRCALPHA)
    temp_surf.fill(Color(0, 0, 0, 0))
    pygame.draw.circle(temp_surf, color, (radius, radius), radius, width)
    return temp_surf

# draw a half-opaque blue circle of radius 30 to the display at point (50, 100)
def test():
    transp_blue = Color(0, 0, 255, 128)
    screen = pygame.display.get_surface()
    circle = render_transparent_circle(transp_blue, 30)
    screen.blit(circle, (50, 100))

rawDescription: Overlapping partially transparent shapes


code:
from sphinx.ext import autodoc

class DocsonlyMethodDocumenter(autodoc.MethodDocumenter):
  def format_args(self):
    return None

autodoc.add_documenter(DocsonlyMethodDocumenter)

rawDescription: show *only* docstring in Sphinx documentation

description: Show *only* docstring in Sphinx documentation


code:
A = type('A', (object,), {'s': 'i am a member', 'double_s': lambda self: self.s * 2})
a = A()
a
a.s
a.double_s()

rawDescription: What is the least-bad way to create Python classes at runtime?


code:
import signal
dict((k, v) for v, k in reversed(sorted(signal.__dict__.items()))
     if v.startswith('SIG') and not v.startswith('SIG_'))

rawDescription: Get signal names from numbers in Python

description: Get signal names from numbers in Python


code:
@classmethod
def get_channel_list(cls, account):
    ret = cls.objects.extra(select={'name_lower': 'lower(name)'})
    ret = ret.order_by('-name_lower')
    ret = ret.filter(accountid=account).values_list('name', 'channelid')
    return channels

rawDescription: Django Query sort case-insensitive using Model method with PostgreSQL


code:
import sys
arg1 = str(sys.argv[1])
print(arg1)

rawDescription: How to open a python script with a constructor variable

description: Open a python script with a constructor variable


code:
s
s.str.replace('rs|in|ec', '')
pd.to_timedelta(s.str.replace('rs|in|ec', ''))

rawDescription: Extract hour/min/seconds from Series

description: Extract hour/min/seconds from Series


code:
import wx

class MainWindow(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Python Test App',size=(600,400))
        panel=wx.Panel(self)
        wx.Frame.CenterOnScreen(self)

        status=self.CreateStatusBar()
        menubar=wx.MenuBar()
        file_menu=wx.Menu()
        edit_menu=wx.Menu()

        ID_FILE_NEW = 1
        ID_FILE_OPEN = 2

        ID_EDIT_UNDO = 3
        ID_EDIT_REDO = 4

        file_menu.Append(ID_FILE_NEW,"New Window","This is a new window")
        file_menu.Append(ID_FILE_OPEN,"Open...","This will open a new window")

        edit_menu.Append(ID_EDIT_UNDO,"Undo","This will undo your last action")
        edit_menu.Append(ID_EDIT_REDO,"Redo","This will redo your last undo")

        menubar.Append(file_menu,"File")
        menubar.Append(edit_menu,"Edit")
        self.SetMenuBar(menubar)

        self.Bind(wx.EVT_MENU, self.test, None, 1)

    def test(self, event):
        self.new = NewWindow(parent=None, id=-1)
        self.new.Show()

class NewWindow(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self, parent, id, 'New Window', size=(400,300))
        wx.Frame.CenterOnScreen(self)
        #self.new.Show(False)

if __name__=='__main__':
        app=wx.PySimpleApp()
        frame=MainWindow(parent=None,id=-1)
        frame.Show()
        app.MainLoop()

rawDescription: showing another window/frame in wxPython

description: Showing another window/frame in wxPython


code:
functions = {
    1: lambda: api.PostDirectMessage(user,'Trebuie sa-mi spui si marca pe care o cauti'),
    2: lambda: postmarket(user,tval),
    3: lambda: api.PostDirectMessage(user,'Imi pare rau, dar nu stiu unde poti gasi aceste tipuri de smantana: %s' % tval)}
func = functions.get(i)
if func:
    return func()
else:
    raise ValueError("Bad index: %d!" % i)

rawDescription: "case sequence" in python implemented with dictionaries


rawDescription: Find backslash (\) in a string --Python

description: Find backslash (\) in a string --Python


code:
self.bind('<Shift-MouseWheel>', self.scrollHorizontally)
...

def scrollHorizontally(self, event):
    self.log.xview_scroll((event.delta/120), "units")

rawDescription: Tkinter bind <Shift-MouseWheel> to horizontal scroll


code:
import csv
from pprint import pprint

from bs4 import BeautifulSoup
import requests

url = "http://www.nse.com.ng/Issuers-section/corporate-disclosures/corporate-actions/closure-of-register"
soup = BeautifulSoup(requests.get(url).content, "html.parser")

rows = soup.select("table.ms-rteTable-default tr")
headers = [header.get_text(strip=True).encode("utf-8") for header in rows[0].find_all("td")]

data = [dict(zip(headers, [cell.get_text(strip=True).encode("utf-8") for cell in row.find_all("td")]))
        for row in rows[1:]]

# see what the data looks like at this point
pprint(data)

with open('sara.csv', 'wb') as csvfile:
    spamwriter = csv.DictWriter(csvfile, headers, delimiter='\t', quotechar="\n")

    for row in data:
        spamwriter.writerow(row)

rawDescription: Get content of table in BeautifulSoup

description: Get content of table in BeautifulSoup


code:
data_dict = {}
for key, value in list:
    if key not in data_dict:
        data_dict.update({key: [value]})
    else:
        data_dict[key].append(value)

rawDescription: Python - how to reduce a list and keep values?


rawDescription: Python - Subtracting elements in lists of tuples


code:
file = request.files['file']

#file.read() is the same as file.stream.read()
img_key = hashlib.md5(file.read()).hexdigest()

rawDescription: Calculate md5 from werkzeug.datastructures.FileStorage without saving the object as file


code:
print ("Welcome teacher")
while True:
    Class = input("Which class (1, 2 or 3) would you like to look at?")
    if Class.lower() not in ('1', '2', '3'):
        print("Not an appropriate choice.")
    else:
        break

while True:
    print("Which method would you like to view by?")
    Method = input("Type '1' for average, '2' for highest score or '3' for alphabetical")
    if Class.lower() not in ('1', '2', '3'):
        print("Not an appropriate choice.")
    else:
        break

if Method == '1':
    print("You have chosen Class" + Class + " average")
elif Method == '2':
    print("You have chosen Class" + Class + " highest score")
elif Method == '3':
    print("You have chosen Class" + Class + " alphabetical")

rawDescription: basic Python if statements not loading

description: Basic Python if statements not loading


code:
def __init__(self, *args, **kwargs):
    super(FormClass, self).__init__(*args, **kwargs)
    if self.object: # in case of ModelForm
        self.fields['extra_field'] = forms.CharField(required=False)
        self.fields['other_field'].required = True
        del(self.fields['non_editable_field'])

rawDescription: What to put inside __init__ method in django form class?


code:
it = iter(lists)
the_len = len(next(it))
if not all(len(l) == the_len for l in it):
    raise ValueError('not all lists have same length!')

rawDescription: Better way to check if all lists in a list are the same length?


code:
import decimal
decimal.Decimal("1e400")
int(decimal.Decimal("1e400"))

rawDescription: why is 1e400 not an int?

description: Why is 1e400 not an int


rawDescription: Removing brackets and comma's in list

description: Removing brackets and comma's in list


code:
from random import shuffle

f = open('q2.txt')

lines = f.readlines()

shuffle(lines)

for line in lines:
    qnum, question, *choices, answer= line.split('$')
    user_input = input("{} ({}) ".format(question, ", ".join(choices)))
    answer = answer.rstrip()
    if user_input == answer:
        print('your answer is right')
    else:
        print('your answer is wrong the answer is', answer)

f.close()

rawDescription: python 3 random picking of statement from given file

description: Python 3 random picking of statement from given file


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

t = np.linspace(0, 5, 100)
assert np.allclose(signal.detrend(t), signal.detrend(2*t))

rawDescription: Reconstructing the original data from detrended data -- Python


rawDescription: How to vectorize np.dot(vector_a, vector_b[:,i]) for i in range?

description: Vectorize np.dot(vector_a, vector_b[:,i]) for i in range


code: data = [line.strip().split(':') for line in data.split('\n') if line.strip()]

rawDescription: How can I parse a line by new lines and colons?

description: Parse a line by new lines and colons


code:
import xlsxwriter

workbook = xlsxwriter.Workbook('chart.xlsx')
worksheet = workbook.add_worksheet()
chart = workbook.add_chart({'type': 'line'})

worksheet.write_column('A1', [5, 4, 9, 12, 7])

chart.add_series({'values': '=Sheet1!$A$1:$A$5'})

chart.set_x_axis({
    'major_gridlines': {
        'visible': True,
        'line': {'width': 1.25, 'dash_type': 'dash', 'color' : 'red'}
    },
})

worksheet.insert_chart('A7', chart)

workbook.close()

rawDescription: XlsxWriter - setting gridlines color

description: XlsxWriter - setting gridlines color


code:
plt.plot(series1_x, series1_y, zorder=1)
plt.plot(series2_x, series2_y, zorder=2)
plt.scatter(series2_x, series2_y, zorder=3)

rawDescription: Matplotlib control which plot is on top

description: Matplotlib control which plot is on top


code:
class Thing(Base):
    id = ...

    def get_children(self, childrentype, invalidate=False):
        if invalidate:
            region_invalidate(_get_children, None, self.id, childrentype)

        @cache_region('short_term', 'get_children')
        def _get_children(id, childrentype):
            ...
            return query

        return _get_children(self.id, childrentype)

rawDescription: beaker cache regions for class methods - use it not as a decorator?

description: Beaker cache regions for class methods - use it not as a decorator


code: image.blit(spritesheet, (0,0), rect)

rawDescription: a list of pygame sprites loses its first element, and gains a duplicate of the last

description: A list of pygame sprites loses its first element, and gains a duplicate of the last


code:
def f():
   while ok==0:
     if q<1 and z>10:
       ok=0
       u=u+1
       k=0
       dv=10
     elif a<100 and b>10:
       ok=1
dis(f)

rawDescription: JUMP_FORWARD or JUMP_ABSOLUTE with IF statement ? Python 2.5


code:
from peewee import *
database_proxy = Proxy()

class BaseModel(Model):
    class Meta:
        database = database_proxy  # Use proxy for our DB.

class main_table(BaseModel):
    file_name = CharField(primary_key = True)
    year = CharField()

import string
db_names = string.ascii_lowercase
# print(db_names) # abcdefghijklmnopqrstuvwxyz

'''
Loop over all the characters in the string and create databases in the current folder of this script
'''
for db_name in db_names:
    database = SqliteDatabase('{}.db'.format(db_name), threadlocals=True, pragmas=(("synchronous", "off"),))
    database_proxy.initialize(database)
    database_proxy.connect()
    database_proxy.create_tables([main_table])

rawDescription: Creating multiple sql databases of the same structure using Peewee


code:
from Tkinter import *

COUNT = 0
def add_one():
    global COUNT
    COUNT += 1
    listbox.insert(END, str(COUNT))
    master.after(1000, add_one)

master = Tk()

scrollbar = Scrollbar(master)
scrollbar.pack(side=RIGHT, fill=Y)

listbox = Listbox(master, yscrollcommand=scrollbar.set)

add_one()
listbox.pack(side=LEFT, fill=BOTH)

scrollbar.config(command=listbox.yview)

mainloop()

rawDescription: Python Tkinter append the output to a list


code:
def calc():
    firstnumber=int(input('Firstnumber'))
    secondnumber=int(input('Secondnumber'))
    addorsubtract=input('Add or subtact?')
    add=(firstnumber+secondnumber)
    minus=(firstnumber-secondnumber)
    if addorsubtract==("+"):
        print (add)
    if addorsubtract==("-"):
        print (minus)

while True:
    c = input("continue?(Y/N)")
    if c == 'Y' or c == 'y':
        calc()
    else:
        break

rawDescription: How do I add a Run command into my python code?

description: Add a Run command into my python code


code:
from collections import OrderedDict
diction ={'z': {'golf': 3, 'bowling': 9}, 'a': {'fed': 5, 'alvin': 10}, 'r': {'yell': 7, 'shout': 11}}
a = OrderedDict(sorted(diction.items()))
for key, subdict in a.items():
    a[key] = OrderedDict(sorted(subdict.items()))

rawDescription: how to sort dictionary within a dictionary python

description: Sort dictionary within a dictionary python


code: x2 = [abs(k) for k in x]

rawDescription: Invert negative values in a list

description: Invert negative values in a list


code:
from random import randint

mcworks = []

for n in xrange(NUM_ITERATIONS):
    mctest = [randint(0, 100) for i in xrange(5)]
    if sum(mctest[:3])/3 == mcavg[2]:
        mcworks.append(mctest) # mcavg is real data

rawDescription: Storing multiple arrays in Python

description: Storing multiple arrays in Python


code:
object_list = [aa, ab, ... ]
for n, k in enumerate( object_list, start=1):
     ...

rawDescription: Rewrite this loop function with range

description: Rewrite this loop function with range


code:
model = self.listView.model()
for index in range(model.rowCount()):
    item = model.item(index)
    if item.isCheckable() and item.checkState() == QtCore.Qt.Unchecked:
        item.setCheckState(QtCore.Qt.Checked)

rawDescription: Iterating all items inside QListView using python


code:
from django.db import models

class Person(models.Model):
  first_name = models.CharField(max_length=50)
  last_name = models.CharField(max_length=50)
  birth_date = models.DateField()

  def baby_boomer_status(self):
    "Returns the person's baby-boomer status."
    import datetime
    if self.birth_date < datetime.date(1945, 8, 1):
        return "Pre-boomer"
    elif self.birth_date < datetime.date(1965, 1, 1):
        return "Baby boomer"
    else:
        return "Post-boomer"

  def _get_full_name(self):
    "Returns the person's full name."
    return '%s %s' % (self.first_name, self.last_name)
  full_name = property(_get_full_name)

rawDescription: Django - what is best practice - Calculating field values


code:
class Something:
   def foo(self): pass

for name, fn in inspect.getmembers(Something):
    if isinstance(fn, types.UnboundMethodType):
        setattr(Something, name, decorator(fn))

rawDescription: Attaching a decorator to all functions within a class


code:
import subprocess

args = ""
while True:
     args += "A"
     result = subprocess.call(["./program", "{args}".format(args=args)])
     if result == 'TRUE':
         break

rawDescription: How to run several times the same program with different inputs via Python script?

description: Run several times the same program with different inputs via Python script


rawDescription: Am I misunderstanding Python regex?

description: Am I misunderstanding Python regex


code:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

from kivy.lang import Builder

Builder.load_string("""
#:import Clock kivy.clock.Clock

<MyWidget>:
    Button:
        text: "Print events"
        on_press: print(Clock._events)
    Button:
        text: "Add event"
        on_press: Clock.schedule_once(root.my_callback, 5)
""")

class MyWidget(BoxLayout):
    def my_callback(self, arg):
        print("my_callback")

class MyApp(App):
    def build(self):
        return MyWidget()

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

rawDescription: Kivy: Check if a function is already scheduled


code:
crm_ids = ",".join(customers)
select_customer = "SELECT UNIQUE id FROM customers WHERE CRM_id IN (%s);" % crm_ids

rawDescription: Most python(3)esque way to repeatedly SELECT from MySQL database


code:
from numpy import cos,sin,arccos
import numpy as np

def parametric_circle(t,xc,yc,R):
    x = xc + R*cos(t)
    y = yc + R*sin(t)
    return x,y

def inv_parametric_circle(x,xc,R):
    t = arccos((x-xc)/R)
    return t

N = 30
R = 3
xc = 1.0
yc = 3.0

start_point = (xc + R*cos(.3), yc + R*sin(.3))
end_point   = (xc + R*cos(2.2), yc + R*sin(2.2))

start_t = inv_parametric_circle(start_point[0], xc, R)
end_t   = inv_parametric_circle(end_point[0], xc, R)

arc_T = np.linspace(start_t, end_t, N)

from pylab import *
X,Y = parametric_circle(arc_T, xc, yc, R)
plot(X,Y)
scatter(X,Y)
scatter([xc],[yc],color='r',s=100)
axis('equal')
show()

rawDescription: How can I generate an arc in numpy?

description: Generate an arc in numpy


code:
from multiprocessing import Pool

def test(a):
    print(a+10)

def main():
    result=range(1,5)
    print('start')
    pool = Pool(processes=2)
    pool.map(test,result)

if __name__=="__main__":
    main()

rawDescription: multiple call body multicoreprocessing python

description: Multiple call body multicoreprocessing python


code:
import functools

def assert_index_datetime(f):
    @functools.wraps(f)
    def wrapper(df):
        assert df.index.dtype == pd.to_datetime(['2013']).dtype
        return f(df)
    return wrapper

@assert_index_datetime
def fn(df):
    pass

df = pd.DataFrame({'a': [1]}, index=pd.to_datetime(['2013']))
fn(df)

rawDescription: Asserting that pandas dataframe has a datetime index through decorator


code:
df
_, i = np.unique(df.columns, return_index=True)
df.iloc[:, i]

rawDescription: Fast method for removing duplicate columns in pandas.Dataframe


code:
import xml.etree.ElementTree as ET
import arcpy

xmlfile = 'D:/Working/Test/Test.xml'
element_tree = ET.parse(xmlfile)
root = element_tree.getroot()
agreement = root.find(".//agreementid").text
arcpy.AddMessage(agreement)

rawDescription: Python extract data from xml and save it to excel


code:
def makeFlake(length,depth,isRoot=True):
    """
    This function draws the flakes. To draw the smaller flakes, this function is called recursively
    :param length: the length of the biggest flake's branch
    :param depth: The number of smaller flakes to draw
    :param isRoot: Draw an extra branch if the value is true. Note that true is the default value.
    """
    if depth>0:
        forward(length)
        makeFlake(length // 3, depth - 1,False)
        backward(length)
        left(60)

        forward(length)
        makeFlake(length // 3, depth - 1,False)
        backward(length)
        left(60)

        forward(length)
        makeFlake(length // 3, depth - 1,False)
        backward(length)
        left(60)

        forward(length)
        if isRoot == True:
         makeFlake(length // 3, depth - 1,False)
        backward(length)
        left(60)

        forward(length)
        makeFlake(length // 3, depth - 1,False)
        backward(length)
        left(60)

        forward(length)
        makeFlake(length // 3, depth - 1,False)
        backward(length)
        left(60)

rawDescription: Drawing a snowflake using recursion

description: Drawing a snowflake using recursion


code: 'read this short text'.translate(None, 'aeiou')

rawDescription: Remove specific characters from a string


code:
class MyColumns(object):
    id = Column(Integer)
    foo = Column(Integer)
    bar = Column(Integer)

class MyMainTable(MyColumns, Base):
    __tablename__ = 'main_table'
    __table_args__ = (
        PrimaryKeyConstraint("id", name="pk_id"),
    )

rawDescription: SQLAlchemy: Possible to declare a column as primary key declaratively?


code:
#!/usr/bin/env python
import os
from datetime import datetime
from tzlocal import get_localzone # $ pip install tzlocal

local_timezone = get_localzone()
aware_dt = datetime.fromtimestamp(os.path.getctime(path), local_timezone)

rawDescription: How can I get the ctime and/or mtime of a file in Python including timezone?

description: Get the ctime and/or mtime of a file in Python including timezone


code:
import glob
from itertools import izip

def extract_meaningful_info(line):
    return line.rstrip('\n').split('[')[0]

MA_files = glob.glob("MA*continuous*2")

with open("MA_continuous_results.csv", "wb") as outfile:
    outfile.write("\t".join(MA_files) + '\n')
    for fields in izip(*(open(f) for f in MA_files)):
        fields = [extract_meaningful_info(f) for f in fields]
        outfile.write('\t'.join(fields) + '\n')

rawDescription: combining one-column text files into one multi-column file, separated by tabs

description: Combining one-column text files into one multi-column file, separated by tabs


code:
def nPk(n,k):
    return int( factorial(n)/(factorial(n- k) * factorial(k)))

rawDescription: Python challenge: distinct combination of prime numbers in a given range


code:
matrix = np.random.randint(2, size=(3,4))
np.save('test', matrix)
a = np.load('test.npy')
matrix
a

rawDescription: How to YAML save numpy array

description: YAML save numpy array


rawDescription: Changing log mode

description: Changing log mode


rawDescription: P-value from Chi sq test statistic in Python


code:
adder_lambda = lambda parameter1,parameter2: parameter1+parameter2
def adder_regular(parameter1, parameter2): return parameter1+parameter2

rawDescription: Syntax behind sorted(key=lambda :)

description: Syntax behind sorted(key=lambda :)


code: checksum = zlib.adler32("S89234IX".encode('utf-8')) & 0xffffffff

rawDescription: Deterministic hashing of Python 3 strings with adler32


code:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib import transforms
import numpy as np
from numpy import cos, sin
plt.close()
plt.plot([1,2],[0,4], 'w')
ax = plt.gcf().axes[0]

def patchesAB(styleA, styleB, orig, target,
                widthA, lengthA, widthB, lengthB,
                kwargsA, kwargsB, shrinkA=0., shrinkB=0.):
    '''
    Select 'styleA' and 'styleB' from the dictionary 'polygons'
    widthA, lengthA, widthB, lenghtB, shrinkA, shrinkB are defined in points
    kwargsA and kwargsB are dictionaries
    '''
    polygons = {\
        '|':np.array([[0,0],[0,1],[0.1,1],[0.1,-1],[0,-1],[0,0]], dtype=float),
        'arrow1':np.array([[0,0],[0,1],[-1,2],[3,0],[-1,-2],[0,-1],[0,0]], dtype=float),
        'arrow2':np.array([[0,0],[-1,1],[0,2],[3,0],[0,-2],[-1,-1],[0,0]], dtype=float),
               }
    xyA = polygons.get( styleA )
    xyB = polygons.get( styleB )
    #
    fig = plt.gcf()
    ax = fig.axes[0]
    trans = ax.transData
    pixPunit = trans.transform([(1,0),(0,1)])-ax.transData.transform((0,0))
    unitPpix = pixPunit
    unitPpix[0,0] = 1/unitPpix[0,0]
    unitPpix[1,1] = 1/unitPpix[1,1]
    #
    orig = np.array(orig)
    target = np.array(target)
    vec = target-orig
    angle = np.arctan2( vec[1], vec[0] )
    #
    lengthA *= unitPpix[0,0]
    lengthB *= unitPpix[0,0]
    widthA  *= unitPpix[1,1]
    widthB  *= unitPpix[1,1]
    orig   += (unitPpix[1,1]*sin(angle)+unitPpix[0,0]*cos(angle))*vec*shrinkA
    target -= (unitPpix[1,1]*sin(angle)+unitPpix[0,0]*cos(angle))*vec*shrinkB
    #TODO improve shrinking... another attempt:
    #orig   +=  unitPpix.dot(vec) * shrinkA
    #target -=  unitPpix.dot(vec) * shrinkB
    # polA
    if xyA != None:
        a = transforms.Affine2D()
        tA = a.rotate_around( orig[0], orig[1], angle+np.pi ) + trans
        xyA = np.float_(xyA)
        xyA[:,0] *= lengthA/(xyA[:,0].max()-xyA[:,0].min())
        xyA[:,1] *=  widthA/(xyA[:,1].max()-xyA[:,1].min())
        xyA += orig
        polA = patches.Polygon( xyA, **kwargsA )
        polA.set_transform( tA )
    else:
        polA = None
    # polB
    if xyB != None:
        a = transforms.Affine2D()
        tB = a.rotate_around( target[0], target[1], angle ) + trans
        xyB = np.float_(xyB)
        xyB[:,0] *= lengthB/(xyB[:,0].max()-xyB[:,0].min())
        xyB[:,1] *=  widthB/(xyB[:,1].max()-xyB[:,1].min())
        xyB += target
        polB = patches.Polygon( xyB, **kwargsB )
        polB.set_transform( tB )
    else:
        polB = None
    return polA, polB

# ARROW 0
plt.annotate('arrow0',xy=(2,1.5),xycoords='data',
             xytext=(1.1,1), textcoords='data',
             arrowprops=dict(frac=0.1,headwidth=10., width=2.))
#
kwargsA = dict( lw=1., ec='k', fc='gray' )
kwargsB = dict( lw=1., ec='k', fc='b' )
# ARROW 1
orig = (1.1,2.)
target = (2.,2.5)
shrinkA = 0.
shrinkB = 0.
polA, polB = patchesAB( '|', 'arrow1', orig, target, 20.,1.,60.,60.,
                        kwargsA, kwargsB, shrinkA, shrinkB )
ax.add_patch(polA)
ax.add_patch(polB)

ax.annotate('arrow1', xy=target, xycoords='data',
             xytext=orig, textcoords='data',
             arrowprops=dict(arrowstyle='-', patchA=polA, patchB=polB,
                 lw=1., shrinkA=shrinkA, shrinkB=shrinkB, relpos=(0.,0.),
                 mutation_scale=1.))
# ARROW 2
orig = (1.1,3.)
target = (2.,3.5)
polA, polB = patchesAB( '|', 'arrow2', orig, target, 20.,1.,60.,60.,
                        kwargsA, kwargsB, shrinkA, shrinkB )
ax.add_patch(polA)
ax.add_patch(polB)

ax.annotate('arrow2', xy=target, xycoords='data',
             xytext=orig, textcoords='data',
             arrowprops=dict(arrowstyle='-', patchA=polA, patchB=polB,
                 lw=1., shrinkA=shrinkA, shrinkB=shrinkB, relpos=(0.,0.),
                 mutation_scale=1.))
plt.autoscale()
plt.xlim(1.,2.2)
plt.ylim(0.5,4)
plt.show()

rawDescription: Custom arrow style for matplotlib, pyplot.annotate


code:
class GalleryExtended(models.Model):

    # Link back to Photologue's Gallery model.
    gallery = models.OneToOneField(Gallery, related_name='extended')

    # Now your extra field.
    extra_url = models.URLField(max_length=200, blank=True, null=True)

rawDescription: How to display extended fields in Django Photologue

description: Display extended fields in Django Photologue


code:
...
def parse(self, response):
    jsonresponse = json.loads(response.body)
    my_count = None
    for platform in jsonresponse['attributes']['facets']['platform']:
        if 'PS4' in platform['name']:
            my_count = platform['count']

    yield dict(count=my_count)
...

rawDescription: Using Scrapy to scrape nested JSON data?

description: Using Scrapy to scrape nested JSON data


code:
import psutil,os
self_process = psutil.Process(os.getpid())
self_process.get_cpu_affinity()
# [0,1] - i have 2 core cpu and process can load both cores
self_process.set_cpu_affinity([1])
# now this process can load only one core #1

rawDescription: Python multiprocessing processing name of processes


code:
playsPerWeek=dict()
for row in data:
    week, artist = row['week'],row['artist']
    playsPerWeek[(week, artist)] = playsPerWeek.get((week, artist),0) + 1

rawDescription: Optimal way to count big data in pandas

description: Optimal way to count big data in pandas


code:
import glob, codecs
from bs4 import BeautifulSoup

inside_location = 0
location_string = ''

with codecs.open("washington_locations.txt","w","utf-8") as out:
    for i in glob.glob("/afs/crc.nd.edu/user/d/dduhaime/java/stanford-corenlp-full-2015-01-29/processed_washington_correspondence/*.xml"):
        locations = []

        with codecs.open(i,'r','utf-8') as f:
            soup   = BeautifulSoup(f.read())
            tokens = soup.findAll('token')
            for token in tokens:
                if token.ner.string     == "LOCATION":
                    inside_location = 1
                    location_string += token.word.string + u" "
                else:
                    if location_string:
                        locations.append( location_string )
                        location_string = ''

        out.write( i + "\t" + "\t".join(l for l in locations) + "\n" )

rawDescription: How to Modify Python Code in Order to Print Multiple Adjacent "Location" Tokens to Single Line of Output


rawDescription: Elegantly generating a datetime from a URL in Django?


code:
data = { "minShift":shiftData[0],"maxShift":shiftData[1],"stepShift":shiftData[2],
          "feeds":providerAliases,
           "instruments":[ {"instrument_old":pair[0],"instrument_new":pair[1]}
                            for pair in instrumentPairList ]}
data_string = json.dumps(data, indent=4)

file = open(dir+r"\config.json","w")
file.write(data_string)
file.close()

rawDescription: Serializing data to JSON in python

description: Serializing data to JSON in python


rawDescription: How can I avoid keyboard conflict in python Tkinter?

description: Avoid keyboard conflict in python Tkinter


code:
files = [fn for fn in glob('somepath/*.txt')
         if not os.path.basename(fn).startswith('eph')]

rawDescription: glob exclude pattern

description: Glob exclude pattern


code: diff = tuple(x-y for x,y in zip(a,b))

rawDescription: What is the proper to do the difference between two tuple in python


code:
for v, p, t in itertools.product(verbs, persons, tenses):
    ...

rawDescription: Itertools equivalent of nested loop "for x in xs: for y in ys..."


code: filename = files.blobstore.create(mime_type='text/plain', _blobinfo_uploaded_filename='filename.txt')

rawDescription: appengine unnamed files in blob viewer

description: Appengine unnamed files in blob viewer


rawDescription: Removing duplicates using set

description: Removing duplicates using set


code:
import datetime

class MyDatetime(datetime.datetime):
    def __init__(self, *args, **kwargs):
        super().__init__()
        if self < datetime.datetime.now():
            raise ValueError("Date must be in the future")

MyDatetime(2017,10,10)  # Ok

MyDatetime(2010,10,10)  # Not ok

rawDescription: Set now as minimum datetime in Python 3.4


code:
class User(models.Model):
  twitter_id = models.CharField(max_length=21, unique=True)
  twitter_name = models.CharField(max_length=55, unique=True)
  # ... other fields characterizing a Twitter account
  legislator = models.ForeignKey('Legislator', blank=True, null=True)

class Legislator(models.Model):
  last_name = models.CharField(max_length=17, blank=True)
  first_name = models.CharField(max_length=11, blank=True)
  # ... other fields characterizing a legislator

rawDescription: Django model with many-to-one or none relationship vs many-to-many through?


code:
norm = numpy.fmax(1.0, numpy.linalg.norm(var, axis=2))
res = var / norm[:, :, numpy.newaxis]

rawDescription: Normalize vector field by NumPy

description: Normalize vector field by NumPy


code:
parents  = []
children = []
mu = []
sigma = []
emitting = False
identifier = [0 for x in range(3)]

rawDescription: Unable to create new instances of node objects python


code:
from django.core.files.base import ContentFile

if form.is_valid():
    new_player = form.save()

    temp_image = new_player.profile_image2
    # duplicate the image for "profile_image2"
    dup_file = ContentFile(temp_image.read())
    dup_file.name = temp_image.name
    new_player.profile_image = dup_file
    new_player.save()

rawDescription: save form imagefield to another image field in view in django

description: Save form imagefield to another image field in view in django


rawDescription: Python socket doesn't close connection properly


code:
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import Gridlayout
from kivy.app import App

layout = Gridlayout(cols = 1)
mybtn = Button(on_press = myfunction)
layout.add_widget(mybtn)

def myfunction():
    mylabel = Label(text = "This is a label")
    layout.add_widget(mylabel)

class myApp(App):
    def build(self):
        return mylayout

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

rawDescription: How can I have a button in Kivy that when pressed, creates a label?

description: Have a button in Kivy that when pressed, creates a label


code: "http://awebsite.me/main?%s" % (urllib.urlencode({'param': cipherText}))

rawDescription: Replacing backslashes in Python strings

description: Replacing backslashes in Python strings


code:
import telnetlib

t = telnetlib.Telnet()
t.open('horizons.jpl.nasa.gov', 6775)

expect = ( ( r'Horizons>', 'DES=C/2012 X1\n' ),
           ( r'Continue.*:', 'y\n' ),
           ( r'Select.*E.phemeris.*:', 'E\n'),
           ( r'Observe.*:', 'o\n' ),
           ( r'Coordinate center.*:', 'H06\n' ),
           ( r'Confirm selected station.*>', 'y\n'),
           ( r'Accept default output.*:', 'y\n'),
           ( r'Starting *UT.* :', '2013-Nov-7 09:00\n' ),
           ( r'Ending *UT.* :', '2013-Nov-17 09:00\n' ),
           ( r'Output interval.*:', '1d\n' ),
           ( r'Select table quant.* :', '1,4,9,19,20,24\n' ),
           ( r'Scroll . Page: .*%', ' '),
           ( r'Select\.\.\. .A.gain.* :', 'X\n' )
)

with open('results.txt', 'w') as fp:
    while True:
        try:
            answer = t.expect(list(i[0] for i in expect), 10)
        except EOFError:
            break
        fp.write(answer[2])
        fp.flush()
        t.write(expect[answer[0]][1])

rawDescription: Accessing a telnet session in python

description: Accessing a telnet session in python


code:
import numpy as np

from bokeh.plotting import figure,output_notebook, show, gridplot
from bokeh.models import ColumnDataSource
output_notebook()
N = 100
max = 100
x1 = [0,10,20,30]
x2 = [50,20,10,70]
y1 = [10,10, 20, 20]
y2 = [30,0,30,0]

source = ColumnDataSource(data=dict(x1=x1, x2=x2, y1=y1, y2=y2))

TOOLS="box_select"

left = figure(width=400, height=400, tools=TOOLS, x_range=(0,100), y_range=(0,100))
left.circle("x1", "y1", source=source, size=10, fill_color="black", line_color=None)

right = figure(width=400, height=400, tools=TOOLS, x_range=(0,100), y_range=(0,100))
right.circle("x2", "y2", source=source, size=10, fill_color="black", line_color=None)

p = gridplot([[left, right]])
show(p)

rawDescription: How to link scatter plots by a custom attribute with Bokeh python library?

description: Link scatter plots by a custom attribute with Bokeh python library


code:
#add .distinct() if you get duplicate users.
users = User.objects.filter(channels__manager=request.user)

rawDescription: How do you extract unique queryset from queryset of objects with ManyToManyField

description: Extract unique queryset from queryset of objects with ManyToManyField


code:
# Initialize output array
res = np.zeros(r.shape)

# Get mask1 and compute values for all elements and use the mask to set only
# TRUE positions with the computed values
mask1 = (r >= 0.5*dr) & (r <= 1.5*dr)
V1 = (5-3*np.abs(r*mask1)/dr - np.sqrt(-3*(1-np.abs(r*mask1)/dr)**2+1))/(6*dr)
res[mask1] = V1[mask1]

# Similarly for mask2 and the computations with that mask
mask2 = np.logical_not(mask1) & (r <= 0.5*dr)
V2 = (1+np.sqrt(-3*(r*mask2/dr)**2+1))/(3*dr)
res[mask2] = V2[mask2]

rawDescription: Little change in a function. Mask. Python


code:
v = np.ones([4,5,6])
new_v = np.zeros((v.shape[0]*10,)+v.shape[1:])
new_v.shape

rawDescription: python zeros array based on another : same shape but longer along one dimension

description: Python zeros array based on another : same shape but longer along one dimension


code:
import getopt
import re
import sys

class Parser(object):
    def __init__(self, input_file):
        self.input_file = input_file

    def get_number_of_nodes(self):
        """
        :return: the number of nodes according to the name of the file
        """
        return int(re.match('Logs/Firefly-\w\w-(\d+).+', self.input_file).group(1))

    @staticmethod
    def get_time(line):
        """
        Function get_time
        :param line: line of the log containing a timestamp of the form HH:MM:SS.NNNN
        :return: Time in seconds
        """
        time = re.match('(\d+):(\d+):(\d+)\.(\d+) \(\d+\)  \w+', line)
        return int(time.group(1)) * 3600 + int(time.group(2)) * 60 + int(time.group(3)) + int(time.group(4)) / 10 ** (
            len(time.group(4)))

    def get_initial_time_and_id(self):
        """
        Function get_initial_time: reads input_file until it finds the first flash
        :rtype: object
        :return: time (in seconds) of the first flash
        """
        with open(self.input_file, 'r') as f:
            for line in f:
                current_line = re.match('(\d+):(\d+):(\d+)\.\d+ \(\d+\)  Node (\d+) emitted a flash.', line)
                if current_line:
                    return self.get_time(line), int(current_line.group(4))

    # Need to define the thing to create all the tables and stuff
    def get_timestamps(self):
        """
        get_timestamps: reads the input_file (log) and every time a node emits a flash, the time at which the flash was
                        emitted is stored in the i-th array of lists_time (where i is the id of the node).
        :return: the lists containing the time at which nodes emitted light.
        """
        init_time, _ = self.get_initial_time_and_id()
        lists_time = [[] for i in range(self.get_number_of_nodes())]
        with open(self.input_file, 'r') as ifile:
            for line in ifile:
                current_line = re.match('(\d+):(\d+):(\d+)\.(\d)\d* \(\d+\)  Node (\d+) emitted a flash.', line)
                if current_line:
                    lists_time[int(current_line.group(5)) - 1].append(self.get_time(line) - init_time)
        return lists_time

def get_paths():
    """
    Generate the output files by getting the input file (passed as an argument)
    :return: input file, output file for cycle lengths, output file for emission window length
    """
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'hi:', ['ifile='])
    except getopt.GetoptError:
        print('python3 Parser-synchronization.py -i <input file>')
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            print('python3 Parser-synchronization.py -i <input file>')
            print('Ex: \"python3 Parser-synchronization.py -i foo\" will get the file Logs/foo.txt')
            sys.exit()
        elif opt in ('-i', '--ifile'):
            input_file = 'Logs/' + str(arg) + '.txt'
            output_file_cl = 'Parsed-logs/' + str(arg) + '-cl-parsed.txt'
            output_file_ewl = 'Parsed-logs/' + str(arg) + '-ewl-parsed.txt'

    return input_file, output_file_cl, output_file_ewl

def cycle_lengths(lists, ofile):
    """
    This function calculates the cycle lengths for each nodes.
    :param lists: list containing the times at which every node flashed
    :param ofile: output file to write the cycle lengths to.
    """
    with open(ofile, 'w') as output_file:
        for elem in lists:
            for i in range(1, len(elem)):
                output_file.write(str(elem[i]) + '\t' + str(elem[i] - elem[i - 1]) + '\n')

def get_closests(lists, time):
    # This is not the best thing to do, but radius = Delta/2
    # I did not want to create another parameter, especially that I only used Delta = 5 in my test.
    """
    This is the second step of the algorithm described in emission_window_length.
    It reads every time list (for each node) until it finds a timestamp within 2.5sec
    :param lists: list containing the times at which every node flashed
    :param time: time of the reference node to look for
    :return: list of all the time within 2.5 sec of the ref time (at most one per node)
    """
    radius = 2.5
    closests = []
    for l in lists:
        i = 0
        while i < len(l)-1 and abs(l[i] - time) > radius:
            i += 1
        # Now we found an element in the ball of center time and radius 2.5.
        # We need to check which of this element or the successor is closest to time.
        if abs(l[i] - time) <= radius:
            if i < len(l) - 2 and abs(l[i] - time) > abs(l[i + 1] - time):
                closests.append(l[i + 1])
            else:
                closests.append(l[i])
    return closests

def emission_window_length(lists, ofile, ref_id):
    """
    Explanation of how the function works:
      1. We get the id of the node that emitted the first flash. This will be our ref_id (passed in parameter)
      2. Every time this node emit a flash, we look for all the nodes that emitted a flash less than 2.5 seconds
         before or after the node. We consider that it is in the same emission.

      node id      node 3 = ref node, At first node 2 is not in the emission window.
         ^      -----------
         |   3  |    o     |        o              o
         |      |<-- 5 --->|
         |   2  |          |       o               o
         |      |<2.5>     |
         |   1  |     o    |         o             o
         |      -----------
         --------------------------------------------------------> time

      3. Once we have this list, we look for the min and max, and max-min is our emission window length for the
         current emission.
      4. Do this for every time ref_id flashed.
    WARNING: As the nodes do not start at the same time, we do NOT consider the flashes after ref_id node stops.
    :param lists: list containing the times at which every node flashed
    :param ofile: output file
    :param ref_id: reference id of the node that first flashed
    """
    with open(ofile, 'w') as output_file:
        for time in lists[ref_id]:
            lists_closests = get_closests(lists, time)
            # to get min and max of list, we use the built in functions in python
            min_time = min(lists_closests)
            max_time = max(lists_closests)
            length = max_time - min_time
            output_file.write('{0}\t{1}\n'.format(str(time), str(length)))

def main():
    ifile, ofile_cl, ofile_ewl = get_paths()

    parser = Parser(ifile)

    _, init_id = parser.get_initial_time_and_id()

    lists_time = parser.get_timestamps()

    cycle_lengths(lists_time, ofile_cl)

    emission_window_length(lists_time, ofile_ewl, init_id)

if __name__ == '__main__':
    main()

rawDescription: Python emission window length parser

description: Python emission window length parser


code:
original_list = [1, 2, 3, 4, 5]
new_list = [2, 3, 4]

original_list[3:3] = new_list

rawDescription: Unwrapping a list from the inside

description: Unwrapping a list from the inside


code: df.apply(lambda x: pd.factorize(x)[0])

rawDescription: Vectorizing / Contrasting a Dataframe with Categorical Variables


code: MyModel.objects.extra(where=['thetime < INTERVAL 1 HOUR'])

rawDescription: How do I write this query in Django?

description: Write this query in Django


caption: SublimeREPL

mnemonic: r

caption: Python

caption: Python (Homebrew)

mnemonic: p

type: subprocess

cmd: /usr/local/bin/python -i -u

external_id: python

rawDescription: trouble with Sublime REPL not finding packages

description: Trouble with Sublime REPL not finding packages


code:
for i in range(0,17):
    it = incomes[i]
    p = paths[i]
    if it > 20000:
        color_class = 2
    elif it > 25000:
        color_class = 1
    elif it > 30000:
        color_class = 3
    elif it > 35000:
        color_class = 4

    color = colors[color_class]
    path_style = "font-size:12px;fill:#%s;fill-rule:nonzero;stroke:#000000;stroke-opacity:1;stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;marker-start:none;stroke-linejoin:bevel" % color
    p['style'] = path_style

rawDescription: Working with SVG paths in BeautifulSoup & Python


code:
MC01 = 'WKST01*.bat'
MC02 = 'WKST02*.bat'

def collect_files(folder, pattern):
    for basedir, dirs, files in os.walk(folder):
        for file in fnmatch.filter(files, pattern):
                yield os.path.join(folder, basedir, file)

MC01_CMD = collect_files(FOLDER, MC01)
MC01_CSV = "MC01.csv"

command = "reporter.exe /rptcsv "+ MC01_CSV + " " + " ".join(MC01_CMD)
os.chdir("C:\Program Files (x86)\Reporter")
os.system(command)

rawDescription: How to pass variables from Python to CMD in Windows

description: Pass variables from Python to CMD in Windows


rawDescription: Python: how to bitmask and compare mac address?


rawDescription: How to print decimal notation rather than scientific notation in Python?

description: Print decimal notation rather than scientific notation in Python


code:
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

rawDescription: Python condition variable timeout

description: Python condition variable timeout


code:
a = 0x7b
b = 0x80000

def append_hex(a, b):
    sizeof_b = 0

    # get size of b in bits
    while((b >> sizeof_b) > 0):
        sizeof_b += 1

    # align answer to nearest 4 bits (hex digit)
    sizeof_b += sizeof_b % 4

    return (a << sizeof_b) | b

print(hex(append_hex(a, b)))

rawDescription: append 2 hex values in python

description: Append 2 hex values in python


code:
from django.utils import translation
from django.shortcuts import redirect
from django.conf import settings

def set_language(request):
    language = request.POST.get('language', settings.LANGUAGE_CODE)
    translation.activate(language)
    request.session[translation.LANGUAGE_SESSION_KEY] = language
    return redirect('root')

rawDescription: Can't change language in django

description: Can't change language in django


code:
client = zeep.Client(
    wsdl='http://www.webservicex.net/ConvertSpeed.asmx?WSDL')
doc = client.get_port().binding.create_message(
    'ConvertSpeed', 100, 'kilometersPerhour', 'milesPerhour')
print(etree.tostring(doc, pretty_print=True))

rawDescription: Programmatically invoke RPC methods for a SOAP endpoint in python


code:
text = '    Y   M   D     PDSW      RSPC      NPPC       NEE'
cols = re.findall('\s+\S+', text)
[len(col) for col in cols]

rawDescription: Getting column widths from string in python


code:
repl = r"foo\1bar\2zot"
re.sub(r".*", repl, "frobozz")
re.sub(r".*", repl.replace("\\", "\\\\"), "frobozz")
re.sub(r".*", lambda z: repl, "frobozz")

rawDescription: python equivalent of java Matcher.quoteReplacement()

description: Python equivalent of java Matcher.quoteReplacement()


code:
import os
import paramiko
from stat import S_ISDIR

server ="any.sftpserver"
username = "uname"
password = "***"
path_to_hosts_file = os.path.join("~", ".ssh", "known_hosts")

ssh = paramiko.SSHClient()
ssh.load_host_keys(os.path.expanduser(path_to_hosts_file))
ssh.connect(server, username=username, password=password)

def isdir(path):
    try:
        return S_ISDIR(sftp.stat(path).st_mode)
    except IOError:
        return False

def rm(path):
    files = sftp.listdir(path=path)

    for f in files:
        filepath = os.path.join(path, f)
        if isdir(filepath):
            rm(filepath)
        else:
            sftp.remove(filepath)

    sftp.rmdir(path)

if __name__ == "__main__":
    rm("/")

rawDescription: Recursive remove directory using SFTP

description: Recursive remove directory using SFTP


code:
# from for loop onward...
for filename in filelist:
    tmpdf = pd.read_csv(os.path.join(targetdir, filename),sep=' ',header=None)
    tmpdf[len(tmpdf.columns)] = filename # add column with filename
    bigdf = pd.concat([bigdf,tmpdf],ignore_index=True)

rawDescription: Append multiple CSVs with the name of the name of each CSV in all rows - Python


code: result = number & 0x1FFFFFFF

rawDescription: bitwise numbers python

description: Bitwise numbers python


code:
class myObj:
    def __init__(self):
        self.a = []
        self.b = False

rawDescription: How can Python lists within objects be freed?


code:
def get_class_by_tablename(tablename):
  """Return class reference mapped to table.

  :param tablename: String with name of table.
  :return: Class reference or None.
  """
  for c in Base._decl_class_registry.values():
    if hasattr(c, '__tablename__') and c.__tablename__ == tablename:
      return c

rawDescription: SQLAlchemy: get Model from table name. This may imply appending some function to a metaclass constructor as far as I can see


code:
from collections import defaultdict
d = defaultdict(list)
items = [['food','fish'],['food','meat'],['food','veg'],['sports','football']]
for key, value in items:
    d[key].append(value)

[[key] + values for key, values in d.items()]

rawDescription: Python a List of lists Merge

description: Python a List of lists Merge


code: df['Body-Collocation'] = df.Body.apply(lambda x: BigramCollocationFinder.from_words(x))

rawDescription: How to apply a function ( BigramCollocationFinder) to Pandas DataFrame

description: Apply a function ( BigramCollocationFinder) to Pandas DataFrame


code:
import matplotlib
import matplotlib.pyplot as plt
from Bio import Phylo
from cStringIO import StringIO

def plot_tree(treedata, output_file):
    handle = StringIO(treedata)  # parse the newick string
    tree = Phylo.read(handle, "newick")
    matplotlib.rc('font', size=6)
    # set the size of the figure
    fig = plt.figure(figsize=(10, 20), dpi=100)
    # alternatively
    # fig.set_size_inches(10, 20)
    axes = fig.add_subplot(1, 1, 1)
    Phylo.draw(tree, axes=axes)
    plt.savefig(output_file, dpi=100)

    return

rawDescription: In python, how can I change the font size of leaf nodes when generating phylogenetic trees using Bio.Phylo.draw()?


code:
with open('database.txt','a+') as myfile:
    json.dump(lst, myfile)
    myfile.write('\n')

rawDescription: Using json with list of lists. How to append lists in loop in file and later load it as list of lists


code:
a["merge"] = 1
b["merge"] = 1
c = a.merge(b, on="merge")

rawDescription: Pandas DataFrame merge between two values instead of matching one


rawDescription: Removing values that repeat more than 5 times in Pandas DataFrame


code:
from __future__ import print_function

def countSubStringMatchRecursive(target, key):
    match = target.find(key)

    if match != -1:
        next_target = target[match+1:]
        return 1 + countSubStringMatchRecursive(next_target, key)
    else:
        return 0

print(countSubStringMatchRecursive('asd asd asd', 'sd'))  # 3
print(countSubStringMatchRecursive('ababa', 'ab'))  # 2

rawDescription: Recursive Python Simple Function

description: Recursive Python Simple Function


code: [[0 for i in ...] for j in ...]

rawDescription: How can I write a 2D array in python using for loop

description: Write a 2D array in python using for loop


code:
from __future__ import print_function

class Map(object):
    def __init__(self):
        self._points = []
    def add_point(self, coordinates):
        self._points.append(coordinates)
    def __str__(self):
        centerLat = sum(( x[0] for x in self._points )) / len(self._points)
        centerLon = sum(( x[1] for x in self._points )) / len(self._points)
        markersCode = "\n".join(
            [ """new google.maps.Marker({{
                position: new google.maps.LatLng({lat}, {lon}),
                map: map
                }});""".format(lat=x[0], lon=x[1]) for x in self._points
            ])
        return """

        """.format(centerLat=centerLat, centerLon=centerLon,
                   markersCode=markersCode)

if __name__ == "__main__":
        map = Map()
        # Add Beijing, you'll want to use your geocoded points here:
        map.add_point((39.908715, 116.397389))
        with open("output.html", "w") as out:
            print(map, file=out)

rawDescription: Is it possible to create a google map from python?


code:
lines = 0
shortfile = None
with open('longfile.txt') as longfile:
   for lineno, line in enumerate(longfile):
       new_lines = line.split(' ')[0]
       if lines != new_lines:
           lines = new_lines
           if shortfile:
               shortfile.close()
               short_filename = 'small_file_{}.txt'.format(lines)
           shortfile = open(short_filename, "w")
       shortfile.write(line)
   if shortfile:
       shortfile.close()

rawDescription: Dividing file by first column of the file PYTHON


rawDescription: comparing and sorting array

description: Comparing and sorting array


code: l.add_value('url', 'http://domain.com/' + l.get_output_value('scu'))

rawDescription: How to use loaded data to add a new value in an ItemLoader?

description: Use loaded data to add a new value in an ItemLoader


code:
# Obvs add arguments so you can pass in your data and plot choices.
def make_img():
    pylab.ioff()

    import matplotlib as plt
    fig = plt.pyplot.figure(figsize=(20,20))
    im2 = plt.pyplot.imread('/some/dir/fancy-map.png')

    # Magic to calculate points, x_min etc.

    fig.clf()
    ax = fig.gca(xlim=(x_min, x_max), ylim=(y_min, y_max))
    ax.imshow(im2, extent=(4, 4.5746, 42.5448, 43.3791), aspect=1.5)
    raster = ax.imshow(points,
                       vmin = 0,
                       vmax=maxval,
                       extent=(x_min, x_max, y_min, y_max),
                       aspect=1.5,
                       origin='lower')
    fig.colorbar(raster)
    ax.set_title('coordinates plot')
    fig.savefig("fancy-annotated-map.png", bbox_inches=0)

    pylab.ion()

rawDescription: iPython notebook: How to prevent from outputing image on imshow()?

description: IPython notebook: How to prevent from outputing image on imshow()


code:
a_slider = widgets.IntSliderWidget(min=-5, max=5, step=1, value=0)
b_slider = widgets.FloatSliderWidget(min=-5, max=5, step=0.3, value=0)
w=widgets.interactive(sigmoid_demo,a=a_slider,b=b_slider)
display(w)

rawDescription: IPython notebook interactive function: how to set the slider range


rawDescription: How to plot bar graph in python

description: Plot bar graph in python


code:
query = """
    INSERT INTO
        favs
    VALUES
        (null, %s, %s, %s, %s)
"""
cursor.execute(query, (item1, item2, item3, item4))

rawDescription: Passing special characters into mysql using python


code:
def escape_quote(value):
  return value.replace("'", "\\'")

query = '''SELECT * FROM redshift_table
           LEFT JOIN
           (SELECT DISTINCT * FROM redshift_view) v
           ON redshift_table.account_number = v.card_no
           WHERE timestamp < date_trunc('day', CURRENT_DATE)
           AND timestamp >=  (CURRENT_DATE - INTERVAL '1 days')'''

unload = '''UNLOAD ('%s') to '%s'
            credentials 'aws_access_key_id=%s;aws_secret_access_key=%s'
            delimiter as '%s'parallel off ALLOWOVERWRITE''' % (escape_quote(query), s3_path, access_key, aws_secret, file_delimiter)

rawDescription: Escaping quotes in Redshift query

description: Escaping quotes in Redshift query


code:
import datetime
created_time = datetime.datetime.now() - datetime.timedelta(minutes=30)
old_objects = MyModel.objects.filter(created__lte=created_time)

rawDescription: figuring out objects created 30 min ago in django

description: Figuring out objects created 30 min ago in django


code:
from django.core.urlresolvers import reverse_lazy

class EditarUsuario(UpdateView):
    model=User
    form_class=EditUserForm
    template_name = "editarUsuario.html"

    def get_success_url(self):
        return reverse_lazy('editar_perfil', args = (self.object.id,))

rawDescription: How can I get a post attribute in a view class in Django?

description: Get a post attribute in a view class in Django


rawDescription: How to get values from a "cell" of a "groupby" object?

description: Get values from a "cell" of a "groupby" object


code: df_clean = df.drop_duplicates(cols=['timestamp', 'user_id'])

rawDescription: How to remove efficiently all duplicates in dataframe or csv file in python?

description: Remove efficiently all duplicates in dataframe or csv file in python


code:
list = dictionarize1(list) #Calling the function
print(list) #your result is ok

rawDescription: Python: How to traverse a list of lists by columns, as if it was a regular 2D array (matrix)?


code:
import glob

print(glob.glob('delta*[0-9].txt'))

rawDescription: Find a file from current directory

description: Find a file from current directory


code:
import unittest
from tempfile import mkdtemp

class Test(unittest.TestCase):

    def setUp(self):
        self.tempdir = mkdtemp()
        os.chdir(self.tempdir)

    def tearDown(self):
        os.rmdir(self.tempdir)

    def testName(self):
        #Do the actual testing
        pass

if __name__ == "__main__":
    unittest.main()

rawDescription: Python unit-testing in multiple threads

description: Python unit-testing in multiple threads


code:
import math
print (math.floor(5.6))

rawDescription: Adding a round down feature on a program