code:
def pdista(X):
    """Squared pairwise distances between all columns of X."""
    B= np.dot(X.T, X)
    q= np.diag(B)[:, None]
    return q+ q.T- 2* B

rawDescription: Symmetric matrices in numpy?

description: Symmetric matrices in numpy


code:
matrix = copy.deepcopy(emptyMatrix)
finalMatrix = copy.deepcopy(emptyMatrix)

rawDescription: Python 3 list unexpected work during copy


code:
class ContextDecorator(object):
    def __call__(self, f):
        @functools.wraps(f)
        def decorated(*args, **kwds):
            with self:
                return f(*args, **kwds)
        return decorated

rawDescription: Function acting as both decorator and context manager in Python?


code:
import urllib
import urllib2

uri = 'http://legistar.council.nyc.gov/Legislation.aspx'

#the http headers are useful to simulate a particular browser (some sites deny
#access to non-browsers (bots, etc.)
#also needed to pass the content type.
headers = {
    'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.13) Gecko/2009073022 Firefox/3.0.13',
    'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml; q=0.9,*/*; q=0.8',
    'Content-Type': 'application/x-www-form-urlencoded'
}

# we group the form fields and their values in a list (any
# iterable, actually) of name-value tuples.  This helps
# with clarity and also makes it easy to later encoding of them.

formFields = (
   # the viewstate is actualy 800+ characters in length! I truncated it
   # for this sample code.  It can be lifted from the first page
   # obtained from the site.  It may be ok to hardcode this value, or
   # it may have to be refreshed each time / each day, by essentially
   # running an extra page request and parse, for this specific value.
   (r'__VSTATE', r'7TzretNIlrZiKb7EOB3AQE ... ...2qd6g5xD8CGXm5EftXtNPt+H8B'),

   # following are more of these ASP form fields
   (r'__VIEWSTATE', r''),
   (r'__EVENTVALIDATION', r'/wEWDwL+raDpAgKnpt8nAs3q+pQOAs3q/pQOAs3qgpUOAs3qhpUOAoPE36ANAve684YCAoOs79EIAoOs89EIAoOs99EIAoOs39EIAoOs49EIAoOs09EIAoSs99EI6IQ74SEV9n4XbtWm1rEbB6Ic3/M='),
   (r'ctl00_RadScriptManager1_HiddenField', ''),
   (r'ctl00_tabTop_ClientState', ''),
   (r'ctl00_ContentPlaceHolder1_menuMain_ClientState', ''),
   (r'ctl00_ContentPlaceHolder1_gridMain_ClientState', ''),

   #but then we come to fields of interest: the search
   #criteria the collections to search from etc.
                                                       # Check boxes
   (r'ctl00$ContentPlaceHolder1$chkOptions$0', 'on'),  # file number
   (r'ctl00$ContentPlaceHolder1$chkOptions$1', 'on'),  # Legislative text
   (r'ctl00$ContentPlaceHolder1$chkOptions$2', 'on'),  # attachement
                                                       # etc. (not all listed)
   (r'ctl00$ContentPlaceHolder1$txtSearch', 'york'),   # Search text
   (r'ctl00$ContentPlaceHolder1$lstYears', 'All Years'),  # Years to include
   (r'ctl00$ContentPlaceHolder1$lstTypeBasic', 'All Types'),  #types to include
   (r'ctl00$ContentPlaceHolder1$btnSearch', 'Search Legislation')  # Search button itself
)

# these have to be encoded
encodedFields = urllib.urlencode(formFields)

req = urllib2.Request(uri, encodedFields, headers)
f= urllib2.urlopen(req)     #that's the actual call to the http site.

# *** here would normally be the in-memory parsing of f
#     contents, but instead I store this to file
#     this is useful during design, allowing to have a
#     sample of what is to be parsed in a text editor, for analysis.

try:
  fout = open('tmp.htm', 'w')
except:
  print('Could not open output file\n')

fout.writelines(f.readlines())
fout.close()

rawDescription: how to submit query to .aspx page in python

description: Submit query to .aspx page in python


code: partial = defaultdict(lambda: defaultdict(list))

rawDescription: How to define and fill a dict of dicts of lists

description: Define and fill a dict of dicts of lists


code:
df = pd.DataFrame([[dt.datetime(2000,1,random.randint(1,31)), random.choice("abc"), random.randint(1,3)] for _ in range(100)], columns=["date", "cat", "value"])
df.groupby(["cat", pd.Grouper(freq='3d',key='date')]).sum().unstack(0).fillna(0).plot()

rawDescription: How can I bin by date ranges and categories in pandas?

description: Bin by date ranges and categories in pandas


code:
class RecordModel(TimeStampedModel):
    class Meta(TimestampedModel.Meta):
        abstract = True

rawDescription: How to extend django abstract base model by inheritance?

description: Extend django abstract base model by inheritance


code:
cmd = 'ffmpeg -y -i Audio.wav  -r 30 -i Video.h264  -filter:a aresample=async=1 -c:a flac -c:v copy av.mkv'
subprocess.call(cmd, shell=True)                                     # "Muxing Done
print('Muxing Done')

rawDescription: Combining an audio file with video file in python


code:
import seaborn as sns
g = sns.FacetGrid(df, col="Country", sharex=False)
g.map(sns.boxplot, 'Year', 'Column2')

rawDescription: How to boxplot data after different column values in pandas

description: Boxplot data after different column values in pandas


code:
n1, n2, n3 = next(i), next(i), next(i)
while True:
    # ... do your checks
    n1, n2, n3 = n2, n3, next(i)

rawDescription: Iterate through an iterable, how to obtain previous and next value using next()?


code:
from os import walk, sep
from functools import reduce # in Python 3.x only

def get_filelist(root, extensions=None):
    """Return a list of files (path and name) within a supplied root directory.

    To filter by extension(s), provide a list of strings, e.g.

        get_filelist(root, ["zip", "csv"])

    """
    return reduce(lambda x, y: x+y,
                  [[sep.join([item[0], name]) for name in item[2]
                    if (extensions is None or
                        name.split(".")[-1] in extensions)]
                   for item in walk(root)])

rawDescription: Converting multiple gz file within subdirectories into csv


rawDescription: how to make heatmaps in matplotlib?

description: Make heatmaps in matplotlib


code:
for  ele in l[:]:
    if ele < minus or ele > plus:
        l.remove(ele)

rawDescription: Delete an item in a list, if outside the IQR


code:
for p in soup.find_all("p", "review_comment"):
    if p.find(class_='show-archived'):
        continue
    # p is now a wanted p

rawDescription: Excluding unwanted results of findAll using BeautifulSoup


code:
def breakoutLists(xmlroot):
    headings = [h.text for h in root.iter('colHeading')]

    return (
        {
            h: col.text
            for h, col in zip(headings, row.iter('column'))
        }
        for row in root.iter('row')
    )

data = list(breakoutLists(html))

rawDescription: Python Parse XML to JSON

description: Python Parse XML to JSON


code:
from kotti.interfaces import IContent

config.add_view(
    my_json_view,
    context=IContent,
    name='json',
    permission='view',
    renderer='json',
)

rawDescription: Can you have multiple generic subviews of a content element in Kotti?

description: Have multiple generic subviews of a content element in Kotti


code:
spectrum_mass = []
spectrum_intensity = []
for i in spectra_files:
    with open(i, 'r') as f:
        numbers = []  #list to store all mass spectrum data directly from file before being processed
        for line in f:
            if line[0].isdigit():  #skips header in file
                numbers.append(line)

        str_data = ''.join(numbers)
        #creates list for the mass and list for the intensity. to be used for plotting
        spectrum_mass.extend(int(x.split(',')[0].strip()) for x in str_data.split())
        spectrum_intensity.extend(int(x.split(',')[1].strip()) for x in str_data.split())
        spectrum_mass.append('||')
        spectrum_intensity.append('||')

rawDescription: Read in two files from same directory

description: Read in two files from same directory


code:
class MyObj:
    def __init__(self, value, suit):
        self._internal = Card(value, suit)

    def __setattr__(self, name, new_value):
        if name == 'suit':
            self._internal = Card(value, new_value)
        else:
            self._internal = Card(new_value, suit)

rawDescription: Python copy-on-write behavior

description: Python copy-on-write behavior


rawDescription: Aggregation of several columns in pandas


code:
def parse_all_links(self, soup):
        for link in soup.find_all('a'):
            if(link.get('href')):
                href = link.get('href')
                if href.startswith('http') or href.startswith('https'):
                    print(href)
                elif href =='#':
                    #print('No link present')
                    pass
                elif href =='/':
                    pass
                else:
                    href = baseurl + href
                    print(href)

rawDescription: Python - Beautifulsoup to PDF with picture (relative paths)


code:
l = [4,4,4,4,5,5,5,6,7,7,7,2,2]
from itertools import groupby
[(i, l.count(i)) for i,_ in groupby(l)]

rawDescription: python count items in list and keep their order of occurrance

description: Python count items in list and keep their order of occurrance


code: B = [element for sublist in A for element in sublist]

rawDescription: Python - Remove List-in-List Hierarchy?

description: Python - Remove List-in-List Hierarchy


code: pd.DataFrame(df.values.reshape(1, -1))

rawDescription: concatenate multiple rows to one single row in pandas

description: Concatenate multiple rows to one single row in pandas


code:
def to_bytes(string):
    result = b""
    need_eval = ""
    need_encode = ""
    for char in string:
        if char <= "\x7f":
            # if char is ascii, eval it with in b'...'
            if need_encode:
                result += need_encode.encode()
                need_encode = ""
            if char == "'":
                n = 0
                # get how many \ in the end of need_eval
                for v in reversed(need_eval):
                    if v != "\\":
                        break
                    n += 1
                # if ' is not escaping
                if n % 2 == 0:
                    char = "\\'"
            need_eval += char
        else:
            # if char is non-ascii, encode it to utf-8
            if need_eval:
                result += ast.literal_eval("b'" + need_eval + "'")
                need_eval = ""
            need_encode += char
    result += need_encode.encode()
    result += ast.literal_eval("b'" + need_eval + "'")
    return result

b = to_bytes(r"\x00\x00\x00\x02α\x00\x00\x00\x13привет мир\x00\x00\x00\x02±")

print(repr(b))
print(to_bytes(r"±\xb1"))
print(to_bytes(r"\x90asdfg\\'\''\r\n\xff\u0001"))

rawDescription: How to declare a byte array contains non-ascii characters without escape in python 3

description: Declare a byte array contains non-ascii characters without escape in python 3


code:
def location(x,y,dim_x,dim_y):
    index = 1*(y==0) + 2*(y==dim_y-1) + 3*(x==0) + 6*(x==dim_x-1)
    return ["interior","top","bottom","left","top-left",
            "bottom-left","right","top-right","bottom-right"][index]

rawDescription: Efficient method to determine location on a grid(array)


code:
import pytz
from dateutil import parser
date_string = '03/16/16 15:50 UTC+02:00'
my_date = parser.parse(date_string)
my_date.astimezone(pytz.UTC)

rawDescription: Getting the UTC hour from a UTC+2 datetime object


code:
url = "http://www.example.com/form"
import glob
import urllib
import urllib2
for filename in glob.glob("file*.txt"):
    filedata = open(filename).read()
    data = urllib.urlencode({'data' : filedata})
    urllib2.urlopen(url=url, data=data)

rawDescription: post text from files into a web page form using python

description: Post text from files into a web page form using python


code:
if tmin is None: tmin = -INF
if tmax is None: tmax = +INF

tval = float(-b - discriminant) / float (2*a)
if tmin <= tval <= tmax:
    return tval

tval = float(-b + discriminant) / float (2*a)
if tmin <= tval <= tmax:
    return tval

# Neither solution was within the acceptable range.
return None

rawDescription: I need a clean approach for range-checking floats in Python


code:
def checkvars(*args):
    flags = [x.get() for x in (onevar, twovar, threevar)]

    # Generate a list containing the corresponding string representation of
    # each checked flag value.
    # For example: (True, False, True) gives ('one', 'three')
    num_strings = ('one', 'two', 'three')
    val_strings = [s for f, s in zip(flags, num_strings) if f]

    # Number of checked values correspond to the number of strings.
    checked_count = len(val_strings)
    if checked_count == 0:
        outvar.set('They are all false')
    elif checked_count == len(flags):
        outvar.set('All three are true')
    else:
        verb = 'is' if len(val_strings) == 1 else 'are'
        outvar.set('%s %s set to true' % (' and '.join(val_strings), verb))

rawDescription: Which of the following is more pythonic?

description: Which of the following is more pythonic


code:
for line in lines:
  if line.startswith('a.setSystemFile'):
    f.write(line.replace('D:/test/f.xml','C:/try/X.xml'))
  else:
    f.write(line)

rawDescription: Modify/replace a line in a python file using another python file


rawDescription: How can I split lines based on characters in python?

description: Split lines based on characters in python


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

def add_to_list(item1, item2, item3, the_list):
    the_list.append((item1, item2, item3))
    return sorted(the_list)

print(add_to_list(0, 1, 0, a))

rawDescription: python list insertion

description: Python list insertion


code:
source = [('100','abc'),('99','bca')]

with open("output.txt", "w") as f:
    for line in source:
        f.write('{1}\t{0}\n'.format(*line));

rawDescription: how to turn a sorted list into tab delimited values in Python?

description: Turn a sorted list into tab delimited values in Python


code:
from openerp.osv import fields, osv

class CustomSaleOrderLine(osv.osv):
    _inherit = 'sale.order.line'

    def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
            uom=False, qty_uos=0, uos=False, name='', partner_id=False,
            lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False, context=None):

        # some operations

        return super(CustomSaleOrderLine, self).product_id_change(cr, uid, ids, pricelist, product, qty,
            uom, qty_uos, uos, name, partner_id,
            lang, update_tax, date_order, packaging, fiscal_position, flag, context)

rawDescription: How to add a functionality to onchange_product_id method in sale.order.line in Odoo 8?

description: Add a functionality to onchange_product_id method in sale.order.line in Odoo 8


code:
wf1 = wave.open("YYY.wav", 'rb')
wf1 = wave.open("XXX.wav", 'rb')

def callback(in_data, frame_count, time_info, status):
    data1 = wf.readframes(frame_count)
    data2 = wf1.readframes(frame_count)
    decodeddata1 = numpy.fromstring(data1, numpy.int16)
    decodeddata2 = numpy.fromstring(data2, numpy.int16)
    newdata = (decodeddata1 * 0.5 + decodeddata2* 0.5).astype(numpy.int16)
    return (result.tostring(), pyaudio.paContinue)

rawDescription: PyAudio Play multiple sounds at once

description: PyAudio Play multiple sounds at once


rawDescription: Can't get real solution with sympy.solve


code:
from scipy.spatial.distance import cdist
import numpy as np
overlap = np.sum(cdist(rt, r0) <= a)

rawDescription: Numpy, avoid loop in 3d array difference nested summation


code:
fontprop = matplotlib.font_manager.FontProperties(fname = "path/fontname.ttf")
matplotlib.rcParams['font.family'] = fontprop.get_name()

rawDescription: Python3 fonts for matplotlib running on headless server


code:
import win32api
import Tkinter as tk

for monitor in win32api.EnumDisplayMonitors():
    monitor_info = win32api.GetMonitorInfo(monitor[0])
    if monitor_info['Flags'] == 1:
        break

work_area = monitor_info['Work']
total_area = monitor_info['Monitor']

width = 300
height = 200

side = [i for i in range(4) if work_area[i]!=total_area[i]]

# Left
if side ==[0]:
    x = str(work_area[0])
    y = str(work_area[3]-height)
# Top
elif side == [1]:
    x = str(work_area[2]-width)
    y = str(work_area[1])
# Right
elif side == [2]:
    x = str(work_area[2]-width)
    y = str(work_area[3]-height)
# Bottom
elif side == [3]:
    x = str(work_area[2]-width)
    y = str(work_area[3]-height)
else:
    x = str(work_area[2]-width)
    y = str(work_area[3]-height)

geom = '{}x{}+{}+{}'.format(width, height, x, y)

root = tk.Tk()
root.configure(background='red')
root.geometry(geom)
root.overrideredirect(True)
root.mainloop()

rawDescription: Open Tkinter Window so it sits on start menu bar


code:
import git
repo = git.Repo.clone_from("git@github.com:user/project.wiki.git", "some-path")
repo.index.add(["your-new-file"])
repo.index.commit("your message to the world")
repo.remotes.origin.push()

rawDescription: Work with GitHub Wiki repository using Python


code:
def __init__(self, *args, **kwargs):
    self.myvalue = kwargs.pop('myvalue')
    super(MyForm, self).__init__(*args, **kwargs)

rawDescription: How to make form validation in Django dynamic?

description: Make form validation in Django dynamic


code:
m = {}
m[1] = 1
m[1,2] = 12
m[1,2,"three",4.5] = 12345
sum(m.values()) #better use m.itervalues() in python 2.*

rawDescription: How to uniformly handle A LIST or A LIST OF LIST (multi-dimensional list)

description: Uniformly handle A LIST or A LIST OF LIST (multi-dimensional list)


code:
class Child(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(120))
    parent_id = db.Column(db.Integer, db.ForeignKey('parent.id'))
    parent = db.relationship('Parent', backref=db.backref('children', lazy='dynamic'))

rawDescription: flask-admin not showing foreignkey columns

description: Flask-admin not showing foreignkey columns


code:
import numpy as np
import matplotlib.pylab as plt
from scipy.integrate import odeint
from scipy.misc import derivative

def system(vect, t):
    x, y = vect
    return [x - y - x * (x**2 + 5 * y**2), x + y - y * (x**2 + y**2)]

vect0 = [(-2 + 4*np.random.random(), -2 + 4*np.random.random()) for i in range(5)]
t = np.linspace(0, 100, 1000)

color=['red','green','blue','yellow', 'magenta']

plot = plt.figure()

for i, v in enumerate(vect0):
    sol = odeint(system, v, t)
    plt.quiver(sol[:-1, 0], sol[:-1, 1], sol[1:, 0]-sol[:-1, 0], sol[1:, 1]-sol[:-1, 1], scale_units='xy', angles='xy', scale=1, color=color[i])

plt.show(plot)

rawDescription: Drawing phase space trajectories with arrows in matplotlib


code:
class MyTestCase(unittest.TestCase):
    def run(self, result=None):
        '''Show log output on failed tests'''

        if result:
            orig_err_fail = result.errors + result.failures

        super().run(result)

        if result and result.errors + result.failures > orig_err_fail:
            print ("assertion failed")

rawDescription: Python unittest module, general assertion logger


code:
def reset_database_connection():
    from django import db
    db.close_connection()

rawDescription: django daemon fails to read database updates

description: Django daemon fails to read database updates


code: logging.Formatter("[%(asctime)s] [%(levelname)8s] --- %(message)s (%(filename)s:%(lineno)s)", "%Y-%m-%d %H:%M:%S")

rawDescription: How to right align level field in Python logging.Formatter

description: Right align level field in Python logging.Formatter


code:
widget.lift()          # move to the top of the stack
widget.lift(another)   # move to just above another widget
widget.lower()         # move to the bottom of the stack
widget.lower(another)  # move to just below another widget

rawDescription: Tkinter: Is it possible to change the stacking order of placed Frames?


code:
import re

month_pair = (re.compile('(\d\s*)m'), 'months')
year_pair = (re.compile('(\d\s*)y'), 'years')

def substitute(s, pairs):
  for (pattern, substitution) in pairs:
    match = pattern.search(s)
    if match:
      s = pattern.sub(match.group(1)+substitution, s)
  return s

pairs = [month_pair, year_pair]
print(substitute('65 y 4m', pairs))

rawDescription: Replacing abbreviations from dictionary

description: Replacing abbreviations from dictionary


code:
foo = db.collection.find({
    "data": { "$nin": [504, 400] },
})

rawDescription: Filter with multiple criteria in Mongo

description: Filter with multiple criteria in Mongo


code:
def break_degree_wrap(values):
    values1, values2 = itertools.tee(values)

    # Yield the first value
    yield next(values2)

    for (prev_datetime, prev_val), (datetime, val) in itertools.izip(values1, values2):

        # If the data wraps over the top
        if val > prev_val and val - prev_val > 180:
            yield (datetime, val - 360)
            yield (datetime, None)
            yield (prev_datetime, prev_val + 360)

        # If the data wraps under the bottom
        elif val < prev_val and prev_val - val > 180:
            yield (datetime, val + 360)
            yield (datetime, None)
            yield (prev_datetime, prev_val - 360)

        # Add each original value
        yield (datetime, val)

rawDescription: Conditionally draw connecting lines in matplotlib


code:
net.addLink( s2, s3 )
net.start()
net.configLinkStatus( 's2', 's3', 'down' )

rawDescription: Running python code with openflow simulator


code:
import keyword
keyword.iskeyword('file')
import __builtin__
hasattr(__builtin__, 'file')

rawDescription: Is 'file' a keyword in python?

description: Is 'file' a keyword in python


code:
def draw_punchcard(infos,
                ax1=range(7),
                ax2=range(24),
                ax1_ticks=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
                ax2_ticks=range(24),
                ax1_label='Day',
                ax2_label='Hour'):

    # build the array which contains the values
    data = np.zeros((len(ax1),len(ax2)))
    for key in infos:
        data[key[0],key[1]] = infos[key]
    data = data/float(np.max(data))

    # shape ratio
    r = float(data.shape[1])/data.shape[0]

    # Draw the punchcard (create one circle per element)
    # Ugly normalisation allows to obtain perfect circles instead of ovals....
    for y in range(data.shape[0]):
        for x in range(data.shape[1]):
            circle = plt.Circle((x/float(data.shape[1])*data.shape[0],y/r),
                                data[y][x]/float(data.shape[1])*data.shape[0]/2)
            plt.gca().add_artist(circle)

    plt.ylim(0-0.5,  data.shape[0]-0.5)
    plt.xlim(0, data.shape[0])
    plt.yticks(np.arange(0,len(ax1)/r-.1,1/r), ax1_ticks)
    plt.xticks(np.linspace(0,len(ax1), len(ax2))+0.5/float(data.shape[1]), ax2_ticks)
    plt.xlabel(ax1_label)
    plt.ylabel(ax2_label)
    plt.gca().invert_yaxis()

    # make sure the axes are equal, and resize the canvas to fit the plot
    plt.axis('equal')
    plt.axis([0, 7.02, 7/r, -.5])
    scale = 0.5
    plt.gcf().set_size_inches(data.shape[1]*scale,data.shape[0]*scale, forward=True)

rawDescription: Matplotlib: How to remove the vertical space when displaying circles on a grid?


code:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.scrollview import ScrollView
import warnings
import string
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.properties import StringProperty

Builder.load_string('''
<ScrolllabelLabel>:
    Label:
        text: root.text
        font_size: 50
        text_size: self.width, None
        size_hint_y: None
        height: self.texture_size[1]
''')

class ScrolllabelLabel(ScrollView):
    text = StringProperty('')
class SomeApp(App):
    def build(self):
        grid = GridLayout(cols=1, size_hint_x=None, width="600dp")

        self.lbl0 = Label(text='Tap and type a word/phrase below') # create a label instance
        grid.add_widget(self.lbl0) # physically add the label onto the layout

        self.txt1 = TextInput(text='', multiline=True) # create a text input instance
        grid.add_widget(self.txt1) # physically add the text input onto the layout

        self.lbl1 = ScrolllabelLabel(text='Display') # create a label instance
        grid.add_widget(self.lbl1) # physically add the label onto the layout

        btn1 = Button(text='Press') # create a button instance
        btn1.bind(on_press=self.mirror) # binding the button with the function below
        grid.add_widget(btn1)

        return grid

    def mirror(self, userInput):
        self.lbl1.text = self.txt1.text

SomeApp().run()

rawDescription: How to add scrolling text widget in Kivy?

description: Add scrolling text widget in Kivy


rawDescription: colorbar matplotlib colors from red to blue

description: Colorbar matplotlib colors from red to blue


code:
df.T.reset_index().values.T.tolist()

[['Project ID', 'State', 'Cost'],
 ['Project1', 'CT', 800],
 ['Project2', 'MA', 1000],
 ['Project3', 'CA', 20]]

rawDescription: Is there a more efficient way to write a dataframe (columns and data) to list?


code:
import croniter
import datetime
now = datetime.datetime.now()
cron = croniter.croniter('45 17 */2  * *', now)
cron.get_next(datetime.datetime)
cron.get_next(datetime.datetime)
cron.get_next(datetime.datetime)

rawDescription: Parsing crontab-style lines

description: Parsing crontab-style lines


code:
def configure_matplotlib():
   import os
   if 'DISPLAY' not in os.environ:
      if matplotlib.get_backend() != 'Agg':
          matplotlib.use('Agg')

import matplotlib
configure_matplotlib()
import matplotlib.pyplot as plt
...

rawDescription: Python Saving PNG in Terminal vs IPython Notebook


rawDescription: Trim a list to a maximum number of elements


code:
df["W"] = df.groupby(level=["A", "B"])["X"].transform(lambda x: x.idxmax()[2])
df

rawDescription: Propagating down the result of groupby aggregations


code:
@app.route('/login/authorized')
@facebook.authorized_handler
def authorized(resp):
    if resp is None:
        return 'Access denied: reason=%s error=%s' % (request.args['error_reason'], request.args['error_description'])
    session['oauth_token'] = (resp['access_token'], '')
    me = facebook.get('/me')
    picture = facebook.get('/me/picture?redirect=0&height=200&width=200&type=normal&fields=url,width,height')
    session['logged_in'] = True
    session['picture'] = picture.data['data']['url']
    g.db.cursor.execute('select fb_id from user where fb_id=%s', [me.data['id']])
    data = g.db.cursor.fetchone()
    if data is None:
        g.db.cursor.execute('insert into user (fb_id, first_name, last_name, picture) values (%s, %s, %s, %s)', [me.data['id'], me.data['first_name'], me.data['last_name'], picture.data['data']['url']])
        g.db.commit()
        g.db.cursor.execute('select user_id from user where fb_id=%s', [me.data['id']])
        result = g.db.cursor.fetchone()
        session['id'] = result[0]
        flash('You\'re logged in!')
        return redirect(url_for('feed'))
    g.db.cursor.execute('select user_id from user where fb_id=%s', [me.data['id']])
    result = g.db.cursor.fetchone()
    session['id'] = result[0]
    flash('You have succesfully logged in')
    return redirect(url_for('feed'))

rawDescription: OAuthException: No token available when using Gunicorn and Flask


code:
from __future__ import division  # move this to the top of your program

# calculate normal and tangential unit vectors
norm_vect = [(object2.pos[0] - object1.pos[0]),
             (object2.pos[1] - object1.pos[1])]  # stil un-normalized!
norm_length = sqrt((norm_vect[0]**2) + (norm_vect[1]**2))
norm_vect = [norm_vect[0] / norm_length,
             norm_vect[1] / norm_length]  # do normalization
tang_vect = [-norm_vect[1], norm_vect[0]]  # rotate norm_vect by 90 degrees

# normal and tangential velocities before collision
vel1 = object1.vel
vel2 = object2.vel
vel1_norm = vel1[0] * norm_vect[0] + vel1[1] * norm_vect[1]
vel1_tang = vel1[0] * tang_vect[0] + vel1[1] * tang_vect[1]
vel2_norm = vel2[0] * norm_vect[0] + vel2[1] * norm_vect[1]
vel2_tang = vel2[0] * tang_vect[0] + vel2[1] * tang_vect[1]

# calculate velocities after collision
new_vel1_norm = (vel1_norm * (object1.mass - object2.mass)
    + 2 * object2.mass * vel2_norm) / (object1.mass + object2.mass)
new_vel2_norm = (vel2_norm * (object2.mass - object1.mass)
    + 2 * object1.mass * vel1_norm) / (object1.mass + object2.mass)
# no need to calculate new_vel_tang, since it does not change

# Now update the object's velocity
object1.vel = [norm_vect[0] * new_vel1_norm + tang_vect[0] * vel1_tang,
               norm_vect[1] * new_vel1_norm + tang_vect[1] * vel1_tang]
object2.vel = [norm_vect[0] * new_vel2_norm + tang_vect[0] * vel2_tang,
               norm_vect[1] * new_vel2_norm + tang_vect[1] * vel2_tang]

rawDescription: I can't find what's wrong with this circle bounce calculation in python


code:
def build_tree(nodes, *args):
    # create empty tree to fill
    t = {}
    # First group all pages w/ same parent
    for node in nodes:
        if node.parentKey is None:
            key = 'root'
        else:
            key = node.parentKey.key()

        if not t.has_key(key):
            t[key] = []

        t[key].append({ 'page' : node, 'children' : []})

    pageTree = t['root']
    # Iterate over there
    build_page_tree(pageTree, t)

    return pageTree

def build_page_tree(pageTree, nodes):
    #Loop over selected list
    for parent, node in nodes.iteritems():
        # We don't need to loop over first level node
        if parent is not 'root':
            # Loop over current level in page tree
            for item in pageTree:
                # Match keys
                if item['page'].key() == parent:
                    # Save node as child
                    item['children'] = node
                    # Only need to loop over childs if they are present
                    build_page_tree(item['children'], nodes)

rawDescription: Tree traversal with page property sortIndex


code:
decision = "no"
while decision.lower() == "no":
    example = input("Blah blah blah: ")
    if 10 < example <= 20:
        decision = raw_input("Are you sure this is your answer?: ")

rawDescription: If "this" is the answer, go back to a previous line


code:
for key in ['cluster0', 'cluster1', 'cluster2', 'cluster3']:
    mask = e['cluster'] == key
    ax.scatter(e['count_sbwip'][mask],e['perc_of_seen_ip'][mask],
                       c=LABEL_COLOR_MAP[key],  label=LABEL_NAMES_MAP[key])
ax.legend()

rawDescription: ipython Pandas : why I cannot print the legend for matplot graphs?

description: Ipython Pandas : why I cannot print the legend for matplot graphs


code:
import htmlentitydefs as ents
from lxml import etree  # or maybe 'html' , if the input is still more broken
def repl_ent(m):
     return ents.entitydefs[m.group()[1:-1]]
goodxml = re.sub( '&\w+;', repl_ent, badxml )
etree.fromstring( goodxml )

rawDescription: How to parse broken XML in Python?

description: Parse broken XML in Python


code:
import urllib2
from django.views.generic import View

class SimpleProxy(View):
    def get(self, request, source_url, *args, **kwargs):
        request = urllib2.Request(url)
        response = urllib2.urlopen(request)
        body = response.read()
        # Update body here
        return HttpResponse(body, status=response.getcode(),
            content_type=response.headers['content-type'])

rawDescription: How to retreive page data as input and give a custome defined Output to the use?

description: Retreive page data as input and give a custome defined Output to the use


code: json.dumps(['foo', '''bar'''])

rawDescription: Python str() double quoted string

description: Python str() double quoted string


code:
import numpy as np
import lutorpy as lua

model = torch.load('PATH TO YOUR MODEL FILE')

# generate your input data with numpy
arr = np.random.randn(100)

# convert your numpy array into torch tensor
x = torch.fromNumpyArray(arr)

# apply model forward method with "._" syntax(which is equivalent to ":" in lua)
y = model._forward(x)

rawDescription: Call torch7 (Lua) function from python?

description: Call torch7 (Lua) function from python


code:
array = ['the', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
array[1:1] = ['quick', 'brown']
array

rawDescription: How to insert the contents of one list into another

description: Insert the contents of one list into another


code:
# For backwards compatability
from project.topic1.module1 import AwesomeClass

rawDescription: Backwards compatibility: Moving a module into a subpackage


code:
def setupGridUI(self):
    widget = QWidget()
    layout = QGridLayout()
    width, height = 10, 10

    root_x, root_y = random.randrange(width), random.randrange(height)

    for x in range(width):
        for y in range(height):
            random_wall = random.randrange(3)
            if x == root_x and y == root_y:
                label = ClickableLabel(x, y, False, True)
            else:
                if random_wall == 0:
                    label = ClickableLabel(x, y, True)
                else:
                    label = ClickableLabel(x, y)

            layout.addWidget(label, x+1, y+1) # modified

    # added the following 4 lines
    layout.setRowStretch(0, 1);
    layout.setRowStretch(height+2, 1);
    layout.setColumnStretch(0, 1);
    layout.setColumnStretch(width+2, 1);

    widget.setLayout(layout)
    self.setCentralWidget(widget)

    self.setStyleSheet("QMainWindow {background: 'purple'}")

rawDescription: MainWindow Widget Resize (Pyside)

description: MainWindow Widget Resize (Pyside)


code:
finish_time = datetime.datetime.now() + datetime.timedelta(hours=6)
while datetime.datetime.now() < finish_time:
    do_something()
    sleep_for_a_bit()

rawDescription: How can I repeat something for x minutes in Python?

description: Repeat something for x minutes in Python


code: "The big brown fox is brown".count("brown")

rawDescription: How to count the number of times something occurs inside a certain string?

description: Count the number of times something occurs inside a certain string


code:
now = datetime.now()
desired_format = '%Y-%m-%dT%H-%M'
item['date_time']= now.strftime(desired_format)

rawDescription: Django: Formatting DateTimeField fields in JSON Serializer


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

img = cv2.imread('test.tif')
edge = cv2.Canny(img, 100, 200)

ans = []
for y in range(0, edge.shape[0]):
    for x in range(0, edge.shape[1]):
        if edge[y, x] != 0:
            ans = ans + [[x, y]]
ans = np.array(ans)

print(ans.shape)
print(ans[0:10, :])

rawDescription: Get a contour (x,y) coordinates of a shape in an image with python


code:
from itertools import chain
set(chain.from_iterable(df1['col1']))

rawDescription: Combining Rows of Lists in Column containing Integers with Python


code:
import dateutil
import pytz

# timezone dictionary built here: http://stackoverflow.com/a/4766400/366335
# tzd = {...}

string = 'Jun 20, 4:00PM EDT'
date = datetuil.parser.parse(string, tzinfos=tzd).astimezone(pytz.utc)

rawDescription: Convert non-UTC time string with timezone abbreviation into UTC time in python, while accounting for daylight savings


code:
t = datetime.now().strftime('%Y%m%dT%H%M%SZ')
t
calendar.timegm(time.strptime(t, "%Y%m%dT%H%M%SZ"))
time.mktime(time.localtime())
time.mktime(time.strptime(t, "%Y%m%dT%H%M%SZ"))

rawDescription: Time and Datetime differences in python trigger error


code:
class lttr:
    def __init__(self):
        self.finish = 0
        self.pointers = {}  #for letters to reference class instance

rawDescription: Trying to build trie with classes in python - strange behavior


code:
#input

file1 = open('1.txt', 'r')
file2 = open('2.txt', 'r')
matrix1 = [line.rstrip().split(', ') for line in file1.readlines()]
matrix2 = [line.rstrip().split(', ') for line in file2.readlines()]
file1.close()
file2.close()

#combine

t_matrix1 = [[r[col] for r in matrix1] for col in range(len(matrix1[0]))]
t_matrix2 = [[r[col] for r in matrix2] for col in range(len(matrix2[0]))]
final_t_matrix = []
for i in (t_matrix1 + t_matrix2):
    if i not in final_t_matrix:
        final_t_matrix.append(i)
final_matrix = [[r[col] for r in final_t_matrix] for col in range(len(final_t_matrix[0]))]

#output

outfile = open('out.txt', 'w')
for i in final_matrix:
    for j in i[:-1]:
        outfile.write(j+', ')
    outfile.write(i[-1]+'\n')
outfile.close()

rawDescription: merging two tables by common column

description: Merging two tables by common column


rawDescription: PyQt4 Python2 Unicode

description: PyQt4 Python2 Unicode


code:
class DictCollcetion(object):
    def __init__(self, dict_):
        self.dict = dict_
    def __getattr__(self, name):
        return {k: k.name for k in self.dict}
    def __getitem__(self, key):
        return {k: k.key for k in self.dict}

rawDescription: Python class for homogenous sets of objects


code:
df['sum'], df['prod'] = sumprod(*df.values.T)
df

rawDescription: Pandas: Add new columns to DataFrame based on values in columns


rawDescription: Django. Order by filtered backward related field


code:
import glob
import os

for file_path in glob.glob('*9999.dbf'):
    os.unlink(file_path)

rawDescription: How can I delete files in Python

description: Delete files in Python


code: method = property(lambda self=self, type=type: self[self['type'] == type])

rawDescription: Scope gotcha when dynamically adding methods in a loop


code:
fn = 'prova.js'
newdata = "mynewstring"
with open(fn, 'r') as f:
    lines = f.read().split('\n')
    #to delete line use "del lines[4]"
    #to replace line:
    lines[4] = newdata
with open(fn,'w') as f:
    f.write('\n'.join(lines))

rawDescription: How to delete a specifil line by line number in a file?

description: Delete a specifil line by line number in a file


code:
bytestring = b"\x7c\xbd\x9c\x91" # from wherever
struct.unpack("<L", bytestring)

rawDescription: Python hex bit flipping ascii

description: Python hex bit flipping ascii


code:
class IntelligentUploadHandler(FileUploadHandler):
    """
    An upload handler which overrides the default multipart parser to allow
    simultaneous parsing of fields and files... intelligently. Subclass this
    for real and true awesomeness.
    """

    def __init__(self, *args, **kwargs):
        super(IntelligentUploadHandler, self).__init__(*args, **kwargs)

    def field_parsed(self, field_name, field_value):
        """
        A callback method triggered when a non-file field has been parsed
        successfully by the parser. Use this to listen for new fields being
        parsed.
        """
        pass

    def handle_raw_input(self, input_data, META, content_length, boundary,
            encoding = None):
        """
        Parse the raw input from the HTTP request and split items into fields
        and files, executing callback methods as necessary.

        Shamelessly adapted and borrowed from django.http.multiparser.MultiPartParser.
        """
        # following suit from the source class, this is imported here to avoid
        # a potential circular import
        from django.http import QueryDict

        # create return values
        self.POST = QueryDict('', mutable=True)
        self.FILES = MultiValueDict()

        # initialize the parser and stream
        stream = LazyStream(ChunkIter(input_data, self.chunk_size))

        # whether or not to signal a file-completion at the beginning of the loop.
        old_field_name = None
        counter = 0

        try:
            for item_type, meta_data, field_stream in Parser(stream, boundary):
                if old_field_name:
                    # we run this test at the beginning of the next loop since
                    # we cannot be sure a file is complete until we hit the next
                    # boundary/part of the multipart content.
                    file_obj = self.file_complete(counter)

                    if file_obj:
                        # if we return a file object, add it to the files dict
                        self.FILES.appendlist(force_text(old_field_name, encoding,
                            errors='replace'), file_obj)

                    # wipe it out to prevent havoc
                    old_field_name = None
                try:
                    disposition = meta_data['content-disposition'][1]
                    field_name = disposition['name'].strip()
                except (KeyError, IndexError, AttributeError):
                    continue

                transfer_encoding = meta_data.get('content-transfer-encoding')

                if transfer_encoding is not None:
                    transfer_encoding = transfer_encoding[0].strip()

                field_name = force_text(field_name, encoding, errors='replace')

                if item_type == FIELD:
                    # this is a POST field
                    if transfer_encoding == "base64":
                        raw_data = field_stream.read()
                        try:
                            data = str(raw_data).decode('base64')
                        except:
                            data = raw_data
                    else:
                        data = field_stream.read()

                    self.POST.appendlist(field_name, force_text(data, encoding,
                        errors='replace'))

                    # trigger listener
                    self.field_parsed(field_name, self.POST.get(field_name))
                elif item_type == FILE:
                    # this is a file
                    file_name = disposition.get('filename')

                    if not file_name:
                        continue

                    # transform the file name
                    file_name = force_text(file_name, encoding, errors='replace')
                    file_name = self.IE_sanitize(unescape_entities(file_name))

                    content_type = meta_data.get('content-type', ('',))[0].strip()

                    try:
                        charset = meta_data.get('content-type', (0, {}))[1].get('charset', None)
                    except:
                        charset = None

                    try:
                        file_content_length = int(meta_data.get('content-length')[0])
                    except (IndexError, TypeError, ValueError):
                        file_content_length = None

                    counter = 0

                    # now, do the important file stuff
                    try:
                        # alert on the new file
                        self.new_file(field_name, file_name, content_type,
                                file_content_length, charset)

                        # chubber-chunk it
                        for chunk in field_stream:
                            if transfer_encoding == "base64":
                                # base 64 decode it if need be
                                over_bytes = len(chunk) % 4

                                if over_bytes:
                                    over_chunk = field_stream.read(4 - over_bytes)
                                    chunk += over_chunk

                                try:
                                    chunk = base64.b64decode(chunk)
                                except Exception as e:
                                    # since this is anly a chunk, any error is an unfixable error
                                    raise MultiPartParserError("Could not decode base64 data: %r" % e)

                            chunk_length = len(chunk)
                            self.receive_data_chunk(chunk, counter)
                            counter += chunk_length
                            # ... and we're done
                    except SkipFile:
                        # just eat the rest
                        exhaust(field_stream)
                    else:
                        # handle file upload completions on next iteration
                        old_field_name = field_name

        except StopUpload as e:
            # if we get a request to stop the upload, exhaust it if no con reset
            if not e.connection_reset:
                exhaust(input_data)
        else:
            # make sure that the request data is all fed
            exhaust(input_data)

        # signal the upload has been completed
        self.upload_complete()

        return self.POST, self.FILES

    def IE_sanitize(self, filename):
        """Cleanup filename from Internet Explorer full paths."""
        return filename and filename[filename.rfind("\\")+1:].strip()

rawDescription: Accessing other form fields in a custom Django upload handler


code:
with open('file.txt') as myfile:
    for line in myfile:
        print(line.split(':')[0])

rawDescription: Python - Use Delimiter to Cut Off Output


code:
clean = []
for item in result:
    subitems = item.split(',')
    clean.extend(si[1:-1] for si in subitems)

rawDescription: Strip extra quotes from python list

description: Strip extra quotes from python list


code:
def get_count(words=None):
    result = {'vowels':0, 'consonants':0}
    if not words:
        return result
    if type(words) is not str:
        return result
    for letter in words.lower():
        if letter in 'aeiou':
            result['vowels'] += 1
        elif letter.isalpha():
            result['consonants'] += 1
    return result

rawDescription: Handle case where a function is called without parameters?


code:
class TestCases(unittest.TestCase):
    def test(self):
        data_feed = dataFeed()
        self.assertEqual(data_feed.generateURL("AAPL", 2, 5), "https://www.google.com/finance/getprices?i=5&p=2d&f=d,o,h,l,c,v&df=cpct&q=AAPL")

rawDescription: python unit testing methods inside of classes

description: Python unit testing methods inside of classes


code:
import optparse
import re
import copy
import SCons

class NoErrOptionParser(optparse.OptionParser):
    def __init__(self,*args,**kwargs):
        self.valid_args_cre_list = []
        optparse.OptionParser.__init__(self, *args, **kwargs)

    def error(self,msg):
        pass

    def add_option(self,*args,**kwargs):
        self.valid_args_cre_list.append(re.compile('^'+args[0]+'='))
        optparse.OptionParser.add_option(self, *args, **kwargs)

    def parse_args(self,*args,**kwargs):
        # filter out invalid options
        args_to_parse = args[0]
        new_args_to_parse = []
        for a in args_to_parse:
            for cre in self.valid_args_cre_list:
                if cre.match(a):
                    new_args_to_parse.append(a)

        # nuke old values and insert the new
        while len(args_to_parse) > 0:
            args_to_parse.pop()
        for a in new_args_to_parse:
            args_to_parse.append(a)

        return optparse.OptionParser.parse_args(self,*args,**kwargs)

def AddOption_and_get_NoErrOptionParser( *args, **kwargs):
    apply( SCons.Script.AddOption, args, kwargs)
    no_err_optparser = NoErrOptionParser(optparse.SUPPRESS_USAGE)
    apply(no_err_optparser.add_option, args, kwargs)

    return no_err_optpars

rawDescription: How can I get optparse's OptionParser to ignore invalid options?

description: Get optparse's OptionParser to ignore invalid options


code:
with open('foo.txt', 'wb') as f:
  f.write(b64Val)

rawDescription: string to base64 conversion

description: String to base64 conversion


code:
import math
def isPrimeNumber(self, num):
    for i in range(2, sqrt(num)):
        if num % i == 0:
            return False

    return True

rawDescription: efficient ways to check prime number in Python

description: Efficient ways to check prime number in Python


code:
from selenium import webdriver
options = webdriver.ChromeOptions()
# set some options
# for example:
# options.add_argument('--disable-logging')
driver = webdriver.Remote(desired_capabilities=options.to_capabilities())

rawDescription: set chrome options with remote driver

description: Set chrome options with remote driver


code:
idx = df.columns
df.columns = idx.astype(int)
idx.order()

rawDescription: Python: Rearrange column order in DataFrame by ascending values


code:
cols = []
cols.append(
    pyfits.Column(name='field3', format='D', array= arrayContainingTheData)
    )
orig_cols = data.columns
new_cols = pyfits.ColDefs(cols)
hdu = pyfits.BinTableHDU.from_columns(orig_cols + new_cols)
hdu.writeto('newtable.fits')

rawDescription: Adding columns to a FITS table with PyFITS


code:
import numpy as np
import theano
from theano import tensor as T
w_values = np.random.randn(1152, 10).astype(theano.config.floatX)
input_values = np.random.randn(1152, 1).astype(theano.config.floatX)
w = theano.shared(w_values, 'w')
input = theano.shared(input_values, 'input')
result = T.dot(input.T, w)
print(result.eval())

rawDescription: How to access theano.tensor.var.TensorVariable?

description: Access theano.tensor.var.TensorVariable


code:
x, y = np.random.rand(2, 1000)
xbin = np.logspace(-1, 0, 101)
ybin = np.logspace(-1, 0, 201)

# raw bin counts
counts, xe, ye = np.histogram2d(x, y, [xbin, ybin])

# size of each bin in x and y dimensions
dx = np.diff(xbin)
dy = np.diff(ybin)

# compute the area of each bin using broadcasting
area = dx[:,  None] * dy

# normalized counts
manual_norm = counts / area / x.shape[0]

# using normed=True
counts_norm, xe, ye = np.histogram2d(x, y, [xbin, ybin], normed=True)

print(np.allclose(manual_norm, counts_norm))
# True

rawDescription: Normalize histogram2d by bin area

description: Normalize histogram2d by bin area


code:
plotaxis = plt.figure().gca()
for key, grp in dataframe.groupby(['sender']):
    my_ts = [ts.to_julian_date() - 1721424.5
             for ts in grp['utctime'].dropna()]
    plt.plot(my_ts,
             grp['temp_3'].dropna(),
             label='%s@%s' % (Temperature, key))
# Style the resulting plot
plotaxis.xaxis.set_major_formatter(
    matplotlib.dates.DateFormatter('%d/%m/%y\n%H:%M')
)

rawDescription: Combining time series into single plot with different time instances


code:
import getpass
pw = getpass.getpass()

rawDescription: When typing can you change what the text is displayed as?


code:
import wave

start_time = 5.43
stop_time = 5.81
time_period = stop_time - start_time
wav = wave.open('test.wav')
time_period_frames = time_period * wav.getnframes()

rawDescription: to get number of frames between a time range?

description: To get number of frames between a time range


code:
import re
text = "this isn't alphanumeric"
result = re.sub(r'\W','-',text) # result will be "this-isn-t-alphanumeric"

rawDescription: How do I use regex to do this in Python?

description: Use regex to do this in Python


code:
s = "[(4, u'name1'), (7, u'name2'), (10, u'name3'), (17, u'name4')]"
from ast import literal_eval
literal_eval(s)

rawDescription: Convert string of list to list type object


code:
class Home(models.Model):
    ...
    all_members = models.IntegerField()

    def save(self):
        all_members = self.all_people()
        super(Home, self).save()

rawDescription: django - a model field which gets the result of model method

description: Django - a model field which gets the result of model method


code: l2 = [float(num.replace(',', '.')) + 1 for num in lst]

rawDescription: Converting list of strings to floats and add 1


code:
class MyModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.DateField: {'widget': forms.TextInput},
    }

rawDescription: Change/adapt date widget on admin interface


code:
self.io_loop.add_timeout(time.time() + 8, lambda: self.stop(True))
still_running = self.wait(timeout=9)
self.assert_(still_running)

rawDescription: Tornado mysteriously hanging after an async operation -- how can I debug?


code:
def some_method(request):
    ClassificationFormset = formset_factory(ClassificationForm)
    classification_formset = ClassificationFormset(prefix='habitat_fs')
    for form in classification_formset:
        form.initial = {"habitat": "some value"}

rawDescription: Django prepopulate non-model formset

description: Django prepopulate non-model formset


code:
w*x
pd.Series(w)*x
(w*x).sort_index()

rawDescription: Pandas sort not maintaining sort

description: Pandas sort not maintaining sort


code:
msg.__setitem__('Date',
                time.strftime("%a, %d %b %Y %H:%M:%S %z",
                              entry_time))

rawDescription: How to set the delivery time for new messages in mailbox?

description: Set the delivery time for new messages in mailbox


code: from __future__ import print_function

rawDescription: Skip processing in Pipeline If argument met


rawDescription: any shorter or faster alternative to bytes(str(some_variable), 'utf-8') in Python 3?

description: Any shorter or faster alternative to bytes(str(some_variable), 'utf-8') in Python 3


code:
from PySide import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.edit = QtGui.QTextEdit(self)
        self.edit.selectionChanged.connect(self.handleSelectionChanged)
        self.browser = QtGui.QTextBrowser(self)
        layout = QtGui.QHBoxLayout(self)
        layout.addWidget(self.edit)
        layout.addWidget(self.browser)

    def handleSelectionChanged(self):
        text = self.edit.textCursor().selectedText()
        # process text here...
        self.browser.setPlainText(text)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 600, 300)
    window.show()
    sys.exit(app.exec_())

rawDescription: pyside QTextEdit selected text event

description: Pyside QTextEdit selected text event


code:
import wx

class Panel(wx.Panel):
    def __init__(self, parent, id, pos, size):
        wx.Panel.__init__(self, parent, id, pos, size)

class Frame(wx.Frame):
    def __init__(self, parent, id, title, pos, size, style):
        wx.Frame.__init__(self, parent, id, title, pos, size, style)
        framePanel = wx.Panel(self)
        self.userpanel = Panel(framePanel, -1, (0,0), (300,180))
        self.userpanel.SetBackgroundColour('Gold')

class Application(wx.App):
    def __init__(self):
        wx.App.__init__(self)
        frame = Frame(None, -1, "Internet Login Tool", (-1,-1), (300,400),\
        wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MAXIMIZE_BOX))
        frame.Show()
        self.SetTopWindow(frame)

if __name__ == '__main__':
    app = Application()
    app.MainLoop()

rawDescription: why can't this wxPython frame (and a panel inside) keep its size?

description: Why can't this wxPython frame (and a panel inside) keep its size


code:
s = '2014-05-08T22:30:57-04:00'
import dateutil.parser
import pytz
pytz.UTC.normalize(dateutil.parser.parse(s))

rawDescription: normalizing JSON datestrings to UTC python

description: Normalizing JSON datestrings to UTC python


code:
from itertools import product
coordinates = list(product(xrange(width), xrange(height)))

rawDescription: Creating 2D coordinates map in Python

description: Creating 2D coordinates map in Python


code:
np.array(test)                                       # 1.19s

np.fromiter(test, dtype=np.int)                      # 1.08s

np.frombuffer(test)                                  # 459ns !!!

rawDescription: efficient python array to numpy array conversion

description: Efficient python array to numpy array conversion


code:
start = 0
calculated_mean = float('inf')
while calculated_mean > set_mean:
    energies = []
    for i in range(start, start + 1000):
        system = moves.move(system, 50, true)
        energy = system.energies()
        energies.append(energy)
        print("%d: %s" % (i, energy))
    calculated_mean = sum(energies[-10:]) / 10

rawDescription: for loop data , iteration and some statistic calculations in python

description: For loop data , iteration and some statistic calculations in python


code: [next(group) for key, group in itertools.groupby(log, key = lambda i: (i['user_id'], i['action']))]

rawDescription: Better way to filter a list of dictionaries in Python


code:
from dateutil.parser import parse
parse("12:30PM")
parse("11AM")

rawDescription: Is there a way to convert inconsistent string time to python time object?


code:
# Ned's startup.py file, loaded into interactive python prompts.

print("(.startup.py)")

import datetime, os, pprint, re, sys, time

print("(imported datetime, os, pprint, re, sys, time)")

def dirx(thing, regex):
    return [ n for n in dir(thing) if re.search(regex, n) ]

pp = pprint.pprint

rawDescription: Python - how to filter an output in a python shell in a grep-like way?


code: n = [len(x) * k % m for x in S]

rawDescription: Python - Operations on all elements of an array without a loop?


code:
import re

string = "#BBC covers the latest issues with G&F. See bbc.co.uk/gf"
mod_data = re.sub(r"[#&/]", " ", string)
print(mod_data)
system('say ' + mod_data)

rawDescription: Replace characters in a string with whitespaces


code:
from PySide import QtGui, QtCore

class Images(QtGui.QScrollArea):
    def __init__(self, images):
        super().__init__()

        self.content = QtGui.QWidget()
        self.layout = QtGui.QGridLayout(self.content)
        self.layout.setSizeConstraint(QtGui.QLayout.SetFixedSize)
        col = 0
        for image in images:
            thumb = QtGui.QLabel()
            thumb.setPixmap(QtGui.QPixmap(image))
            self.layout.addWidget(thumb, 0, col)
            col += 1

        self.setWidget(self.content)
        self.setMinimumWidth(self.content.sizeHint().width())
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)

app = QtGui.QApplication([])

window = QtGui.QWidget()
layout = QtGui.QVBoxLayout(window)
scroll_area = Images(['test.png','test.png','test.png','test.png'])
layout.addWidget(scroll_area)
window.show()

app.exec_()

rawDescription: Make scroll area expand horizontally to fit contents


code:
def __gt__(self, other):
    return self > other

rawDescription: Why aren't these magic methods recursive?


code:
contacts_client = gdata.contacts.client.ContactsClient(domain=domain)
query = gdata.contacts.client.ContactsQuery()
query.max_results = 100
myContacts = contacts_client.GetContacts(q=query)

rawDescription: Google api GetContacts Max Results Python


code:
import random

image_size = (5000,5000)
portion_size = (200, 200)

x1 = random.randint(0, image_size[0]-portion_size[0]-1)
y1 = random.randint(0, image_size[1]-portion_size[1]-1)

x2, y2 = x1+portion_size[0]-1, y1+portion_size[1]-1

# Grab the area of the image that is the rectangle defined by (x1,y1) and (x2,y2)

rawDescription: How to select a random portion from an image in python

description: Select a random portion from an image in python


code:
m
m + m.T

rawDescription: Python — How can I find the square matrix of a lower triangular numpy matrix? (with a symmetrical upper triangle)


code:
for i in xrange(len(samplist)):
    if isinstace(samplist[i], str):
        samplist[i] += 'def'

rawDescription: How to find if the data is a str or int in List

description: Find if the data is a str or int in List


code:
import pprint
import pyroute2
import socket
ip = pyroute2.IPRoute()
pprint.pprint(ip.get_routes(family=socket.AF_INET, dst='127.0.0.6'))

rawDescription: How can I determine the source address before sending a message based on the destination address?

description: Determine the source address before sending a message based on the destination address


code:
import itertools as it
import numpy as np
from operator import itemgetter
import csv

a = np.array([(2,"Ana",9),(5,"Maria",4),(6,"Joao",3), (1,"Ana",4)])
name = itemgetter(1)
a = sorted(a, key = name)
for k, g in it.groupby(a, name):
    filename = k + '.csv'
    with open(filename, 'wb') as f:
        writer = csv.writer(f)
        writer.writerows(list(g))

rawDescription: How can I export Python multidimensional numpy array to different files based on second value of each line?

description: Export Python multidimensional numpy array to different files based on second value of each line


code:
def Clearcommand():
    title = the_window.title()[:-1]
    the_window.title(title)
    if len(title) > 0:
        the_window.after(1000, Clearcommand)

rawDescription: How do you make an animation with the title of a Tkinter window?

description: Make an animation with the title of a Tkinter window


code:
df.notnull().all(axis=1)
[np.all(~np.isnan(x)) for x in df.values]

rawDescription: How to mark DataFrame rows with nan in any column

description: Mark DataFrame rows with nan in any column


code:
def process_name(name, data, output_file):
    group_by = {}
    for index, change in data:
        if index not in group_by:
            group_by[index] = []
        group_by[index].append(change)

    # do the step 1 filter logic here

    # do the step 2 filter logic here
    for index in group_by:
        if index == group_by[index]:
            # Because there is at least one "no change" this
            # whole "name" can be thrown out, so return here.
            return

    output = []
    for index in group_by:
        output_file.write("%s\t%s\t%s\n" % (name, index, group_by[index]))

current_name = None
current_data = []

input_file = open(input_filename, "r")
output_file = open(output_filename, "w")
header = input_file.readline()
for row in input_file:
    cols = row.strip().split("\t")
    name = cols[0]
    index = cols[1]
    change = cols[2]
    if name != current_name:
        if name != None:
            process_name(current_name, current_data, output_file)
        current_name = name
        current_data = []

    current_data.append((index, change))

# process what's left in the buffer
if current_name is not None:
    process_name(current_name, current_data, output_file)

input_file.close()
output_file.close()

rawDescription: Operating on a huge table: group of rows at a time using python


code:
def do_POST(self):
    ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
    if ctype == 'multipart/form-data':
        postvars = cgi.parse_multipart(self.rfile, pdict)
    elif ctype == 'application/x-www-form-urlencoded':
        length = int(self.headers.getheader('content-length'))
        postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1)
    else:
        postvars = {}
    ...

rawDescription: Python: BaseHTTPRequestHandler post variables


code: (df['dir'].value_counts() > 500).sum()

rawDescription: Pandas: select by bigger than a value

description: Pandas: select by bigger than a value


code:
def extend_erathostene(A, B, prime_up_to_A):
    sieve = [ True ]* (B-A)
    for p in prime_up_to_A:
        # first multiple of p greater than A
        m0 = ((A+p-1)/p)*p
        for m in range( m0, B, p):
            sieve[m-A] = False
    limit = int(ceil(sqrt(B)))
    for p in range(A,limit+1):
        if sieve[p-A]:
            for m in range(p*2, B, p):
                sieve[m-A] = False
    return prime_up_to_A + [ A+c for (c, isprime) in enumerate(sieve) if isprime]

rawDescription: find primes in a certain range efficiently

description: Find primes in a certain range efficiently


code:
def my_string(size):
    out_string = ('yn' * size)[:size]
    return out_string

rawDescription: Simultaneously printing x number of times - Python


code:
import ConfigParser
parser = ConfigParser.ConfigParser()

info = {"User1-votes":"36","User1-gamestart":"13232323","User2-votes":"36","User2-gamestart":"234234234","User3-votes":"36","User3-gamestart":"13232323"}

parser.add_section('User-Info')
for key in info.keys():
    parser.set('User-Info', key, info[key])

with open('config.ini', 'w') as f:
    parser.write(f)

rawDescription: Python Write Dictionary Contents to ConfigParser


rawDescription: How to groupby the first level index and apply function to the second index in Pandas

description: Groupby the first level index and apply function to the second index in Pandas


code:
activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

rawDescription: How to activate/deactivate a virtualenv from python code?

description: Activate/deactivate a virtualenv from python code


code:
from itertools import product

def Variante(zip_code):
    replace_tables = [
        ['3', '8'], #prva tabela
        ['1', '4', '7'], #druga tabela
        ['5', '6'], #treta tabela
    ]

    # Convert to list of individual aliases
    aliased_chars = []
    for char in zip_code:
        aliased = False
        for table in replace_tables:
            if char in table:
                # Convert to possible aliases to this char
                aliased_chars.append(table)
                aliased = True
                break

        if not aliased:
            # Otherwise convert to list for itertools.product
            aliased_chars.append([char])

    # Get the cartesian product of lists
    return ["".join(l) for l in product(*aliased_chars)]

# x=input("vnesi postno stevilko: ")
print(Variante("1035"))

rawDescription: why doesnt my program work as it should? (why does it stop in the middle)

description: Why doesnt my program work as it should? (why does it stop in the middle)


code:
import numpy as np
import matplotlib.pyplot as plt
import random as rd
import math

a=np.zeros((1000,2), dtype=np.float)

def randwalk(x,y):
    theta=2*math.pi*rd.random()
    x+=math.cos(theta);
    y+=math.sin(theta);
    return (x,y)

fn_base = "my_random_walk_%i.txt"
for j in range(12):
    rd.seed(j)
    x, y = 0., 0.
    for i in range(1000):
        x, y = randwalk(x,y)
        a[i,:] = x, y
    fn = fn_base % j
    np.savetxt(fn, a)

rawDescription: Python: Repetition of 2-d random walk simulation


code: UserProfile.objects.select(extra={'birth_day': 'extract(day from birthDate)'}).order_by('-birth_day')

rawDescription: Can i do an order_by on day in month?

description: Do an order_by on day in month


code:
mylistoftuples.append((int(record[0]), str(parsed.first),str(parsed.middle),str(parsed.last),str(parsed.suffix)))

cursor.executemany("""insert into NamesToParseOut(id, first, middle, last, suffix) values (%s, %s, %s, %s, %s)""", mylistoftuples)

rawDescription: What is the most efficient way of updating data in a MySQL table using Python avoiding loops?


code:
from threading import Timer, Thread, Event

class PumpSystem():
    def __init__(self):
        self.alarms = [0 for x in range(30)]
        self.Start()
        return None

    def SetUpdateFlag(self, flag):
        self.update_flag = flag
        return True

    def Start(self):
        self.stop_flag = Event()
        self.thread = ClockTimer(self.OnTimerExpired, self.stop_flag)
        self.thread.start()
        return True

    def Stop(self):
        self.stop_flag.set()
        return True

    def TriggerAlarmEvent(self):
        self.alarms[-1] += 1
        self.CheckConditions()
        self.update_flag.set()
        return True

    def OnTimerExpired(self):
        self.UpdateRunningAverage()

    def CheckConditions(self):
        # Check if another error has triggered in the past 5 seconds
        if sum(self.alarms[-5:]) > 1:
            print('5 second error')

        # Check if more than 3 errors have triggered in the past 30 seconds
        if sum(self.alarms) > 3:
            print('30 second error')

        return True

    def UpdateRunningAverage(self):
        self.alarms.append(0)
        self.alarms.pop(0)
        self.update_flag.set()
        return True

class ClockTimer(Thread):
    def __init__(self, callback, event):
        Thread.__init__(self)
        self.callback = callback
        self.stopped = event
        return None

    def SetInterval(self, time_in_seconds):
        self.delay_period = time_in_seconds
        return True

    def run(self):
        while not self.stopped.wait(1.0):
            self.callback()
        return True

## START MOCKUP CODE ##

import tkinter as tk
class MockUp():
    def __init__(self):
        self.pump_system = PumpSystem()
        self.update_flag = Event()
        self.pump_system.SetUpdateFlag(self.update_flag)
        self.StartSensor()
        return None

    def StartSensor(self):
        self.root = tk.Tk()
        self.root.protocol("WM_DELETE_WINDOW", self.Exit)
        self.alarms = tk.StringVar()
        w = tk.Label(self.root, textvariable=self.alarms, width=100, height=15)
        self.alarms.set(self.pump_system.alarms)
        w.pack()
        self.root.after('idle', self.ManageUpdate)
        self.root.bind_all('<Key>', self.ManageKeypress)
        self.root.mainloop()
        return True

    def ManageUpdate(self):
        if self.update_flag.isSet():
            self.alarms.set(self.pump_system.alarms)
            self.update_flag.clear()
        self.root.after(1, self.ManageUpdate)
        return True

    def ManageKeypress(self, event):
        if event.keysym == 'Escape':
            self.Exit()
        if event.keysym == 'space':
            self.pump_system.TriggerAlarmEvent()
        return True

    def Exit(self):
        self.pump_system.Stop()
        self.root.destroy()

mockup = MockUp()

rawDescription: Python multipul Event Timer

description: Python multipul Event Timer


code:
import requests
response = requests.get("http://104.71.136.252/", timeout=60, verify=False)
response.raw.version
response = requests.get("http://stackoverflow.com", timeout=60, verify=False)
response.raw.version

rawDescription: Python 3.x how to get http version (using requests library)


code:
def wait():
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN and event.key == K_f:
                return

rawDescription: Pygame waiting the user to keypress a key


code:
drawing = np.zeros(img.shape)
for i in xrange(len(contours)):
    if (cv2.contourArea(contours[i]) > 15000): # just a condition
        cv2.drawContours(drawing, contours, i, (255, 255, 255), 1, 8, hierarchy)

rawDescription: Save contour to image

description: Save contour to image


code:
df
df.start - df.end.shift()
df['elapsed'] = df.start - df.end.shift()
df

rawDescription: python pandas: How to calculate elapsed time between spans?

description: Python pandas: How to calculate elapsed time between spans


code:
class MyApp(Tk):
    def __init__(self, *args, **kwargs):
        ...
        self.open_button = Button(...)
        self.close_button = Button(...)
        ...
    def OnSomeEvent(self, event):
        if ...:
            self.open_button.configure(state="disabled")
        else:
            self.open_button.configure(state="normal")

rawDescription: Accessing objects added to the Tkinter root


code:
import os
import re

path = your_file_path
d = os.listdir(path) # get list of file names

graphdata = []
pat = re.compile('(?<=\w\:\s)\d+')
for f in d:
    with open(path + f, 'r') as file:
        file = file.read()
        num = re.findall(pat, file)
        graphdata.append((f, num))

rawDescription: How to pick certain values from text files and print as array into a single output file

description: Pick certain values from text files and print as array into a single output file


rawDescription: Python - The fastest way to generate string of zeros


code:
cache={}
def expensive_calc(x):
    # If x is a numpy array, you need to convert x into something hashable so it
    # can be used as a key in cache.
    key=tuple(x)
    try:
        return cache[key]
    except KeyError:
        # do expensive calc for result
        cache[key] = result
        return result

def func(x):
    y=expensive_calc(x)
    return something_based_on_y

def func_der(x):
    y=expensive_calc(x)
    return something_else_based_on_y

def optimize_fmin_bfgs():
    xopt = fmin_bfgs(func, x_guess, fprime=func_der)

rawDescription: How to force the functions of the optimize module of scipy to take a function and its gradient simultaneously

description: Force the functions of the optimize module of scipy to take a function and its gradient simultaneously


code:
def __init__(self, widget):
    QtCore.QThread.__init__(self)

def run(self):
    real_stdout = sys.stdout
    sys.stdout = StringIO.StringIO()
    self.runModule()
    label_text = sys.stdout.getvalue()
    sys.stdout = real_stdout

rawDescription: Read the print values of an imported class


code:
from pandas import DataFrame
data = [{'a':x} for x in range(40)]
from random import shuffle
shuffle(data)
df = DataFrame(data)
index_of_max_value = df['a'].idxmax()
df['a'][max(0,index_of_max_value-10):min(len(df['a']), index_of_max_value+11)]

rawDescription: Python: how to subset from a large dataframe based upon a maximum value in a column and nrows before and nrows after including the self


code:
def AccountHome(request):
    # I recommend using the login required decorator instead but this is ok
    if request.user.is_anonymous():
        return HttpResponseRedirect('/Login/')

    # create a tld instance for the form, with the user set
    tld = Tld(FKtoClient=request.user)
    form = SubmitDomain(data=request.POST or None, instance=tld) # A form bound to the POST data, using the tld instance

    if request.method == 'POST': # If the form has been submitted...
        if form.is_valid(): # If form input passes initial validation...
            domainNm = form.cleaned_data['domainNm']
            form.save() #save cleaned data to the db from dictionary

            # don't use a try..except block here, it shouldn't raise an exception
            return HttpResponseRedirect('/Processscan/?domainNm=%s' % domainNm)
    # No need to create another form here, because you are using the request.POST or None trick
    # else:
    #    form = SubmitDomain()

    tld_set = request.user.tld_set.all()

    return render(request, 'VA/account/accounthome.html', {
         'tld_set':tld_set, 'form' : form
    })

rawDescription: Get Django Auth "User" id upon Form Submission


code:
fig, ax = plt.subplots(1, 1)
ax.hold(True)
colors = ['r', 'g', 'b']
labels = ['foo', 'bar', 'baz']
dummies = [ax.plot([], [], ls='-', c=c)[0] for c in colors]
ax.legend(dummies, labels)

rawDescription: Arbitrary key or legend with matplotlib

description: Arbitrary key or legend with matplotlib


rawDescription: How to mark every favorited item?

description: Mark every favorited item


code:
STATICFILES_DIRS = (
    '/home/joe/Documents/exchange/Texchange/textchange/static',
)

MEDIA_ROOT = '/home/joe/Documents/exchange/Texchange/textchange/media/'

MEDIA_URL = '/media/'

STATIC_ROOT = '/home/joe/Documents/exchange/Texchange/textchange/static_root/'

STATIC_URL = '/static/'

rawDescription: Django - Image from MEDIA_URL not showing up on template?


code:
import MySQLdb
from MySQLdb.cursors import DictCursor

conn = MySQLdb.connect(cursorclass=DictCursor)
g.cursor = conn.cursor()

rawDescription: Python Flask: Get database rows as dictionary


code:
import twill
import twill.commands as c

def login():
    c.clear_cookies()
    c.go('http://icfpcontest.org/icfp10/login')
    c.fv(1, 'j_username', 'Side Effects May Include...')
    c.fv(1, 'j_password', '<redacted>')
    c.submit()
    c.save_cookies('/tmp/icfp.cookie')

all_cars_rx = re.compile(r'(\d+)	(\d+)	')
def list_cars():
    c.go('http://icfpcontest.org/icfp10/score/instanceTeamCount')
    cars = re.findall(all_cars_rx, c.show())
    if not cars:
        sys.stderr.write(c.show())
        sys.stderr.write('Could not find any cars')
    return cars;

rawDescription: How to write a Python robot that browses

description: Write a Python robot that browses


rawDescription: How to get the text from child nodes if it is parents to other node in Scrapy using XPath

description: Get the text from child nodes if it is parents to other node in Scrapy using XPath


code:
import numpy as np
from numpy import cos, sin, pi, exp, sqrt
from matplotlib import pyplot as plt
import matplotlib.cm as cm
from matplotlib import animation

nx=30
ny=30

nu=0.0025
p=2*pi

x = np.linspace(0,p,nx)
y = np.linspace(0,p,ny)

fig,ax = plt.subplots(1,1)
ax.set_xlim(0,p)
ax.set_ylim(0,p)

t=0
X,Y = np.meshgrid(x,y)
U=-cos(X)*sin(Y)*exp(-2*nu*t)
V=sin(X)*cos(Y)*exp(-2*nu*t)
VM=sqrt(U**2+V**2)

im = plt.imshow(VM, interpolation='bilinear', origin='lower',
            cmap=cm.gray, extent=(0, p, 0, p))
levels = np.arange(-1.2, 1.6, 0.2)

# add a vertical colorbar.
CBI = plt.colorbar(im, orientation='vertical', shrink=0.8)

def init():
    im.set_data((np.empty_like(VM)))
    return im,

def animate(t):
    U=-cos(X)*sin(Y)*exp(-2*nu*t)
    V=sin(X)*cos(Y)*exp(-2*nu*t)
    VM=sqrt(U**2+V**2)
    im.set_data(VM)
    return im,

# # call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=20, blit=True)

plt.show()

rawDescription: How to animate a contour plot produced by a bilinear interpolation?

description: Animate a contour plot produced by a bilinear interpolation


code:
import re

numbered = re.compile(r'N\d+').match

for fn in fns:
     # open for counting
     reader = csv.reader(open(fn,"rb"))
     count = sum(1 for row in reader if row and not any(r.strip() == 'DIF' for r in row) and numbered(row[0]))

     # reopen for filtering
     reader = csv.reader(open(fn,"rb"))

     with open (os.path.join('out', fn), 'wb') as f:
        counter = 0
        w = csv.writer(f)
        for row in reader:
            if row and 'Count' in row[0].strip():
                row = ['Count', count]
            if row and not any(r.strip() == 'DIF' for r in row): #remove DIF
                if numbered(row[0]):
                    counter += 1
                    row[0] = 'N%d' % counter
            w.writerow(row)

rawDescription: Fix numbering on CSV files that have deleted lines


code:
import bs4
soup = bs4.BeautifulSoup(html_string)
href = soup.find('h3').find('a').get('href')
print(href)

rawDescription: how to access the first result of a google search result ( prominently a video link)?

description: Access the first result of a google search result ( prominently a video link)


code: captcha.conf.settings.CAPTCHA_FONT_PATH = BASE_DIR + '/fonts/' + random.choice(cfonts)

rawDescription: strange interrmittent behavior regarding settings.py apparently

description: Strange interrmittent behavior regarding settings.py apparently


code:
from os.path import expanduser
home = expanduser("~")
with open(os.path.join(home, 'file'), 'w') as sr:
    json.dump(somelist, sr)

rawDescription: What are the file places after you package a python program?


code:
from reportlab.pdfgen import canvas

c = canvas.Canvas("hello.pdf")
c.drawString(100,750,"Welcome to Reportlab!")
c.save()

rawDescription: How can I make pdf with snippet in reportlab

description: Make pdf with snippet in reportlab


code:
import Tkinter as  tk
from threading import Thread as thread
import time

class T():
    def det2(self,x):
        time.sleep(2)
        x.destroy()

x = tk.Tk()
ts = thread(target=T().det2, args=(x,))
ts.daemon = True
ts.start()
x.mainloop()

rawDescription: How can open new tkinter window by threading?


code:
import re
with open("data.json") as inp:
    lines = inp.readlines()

with open("clean.json", "w") as output:
    for line in lines:
        output.write(re.sub(r'"(\d+)"', r"\1", line))

rawDescription: Remove quotes from JSON file

description: Remove quotes from JSON file


code:
values = ((3, "Foo"), (5, "Bar"))
for n in range(1, 101):
    res = ''.join(v for (k, v) in values if not n % k)
    print(res if res else n)

rawDescription: Divisibility test algorithm

description: Divisibility test algorithm


code: help('modules')

rawDescription: How do I get a list of python libraries I've installed?

description: Get a list of python libraries I've installed


code:
import requests
f = open('C:\\Users\\Amir\\Desktop\\snow.jpg', 'rb')
urls='http://127.0.0.1:8000/upper'
r=requests.post(urls, files= {'uploads':f})
print(r.status_code)

rawDescription: uploading files using python to a Django server

description: Uploading files using python to a Django server


code:
for i in range(len(data_df_3dx)):
    x, y, z = data_df_3dx.iloc[i]['PC1'], data_df_3dx.iloc[i]['PC2'], data_df_3dx.iloc[i]['PC3']
    ax.scatter(x, y, z)
    #now that you have the coordinates you can apply whatever text you need. I'm
    #assuming you want the index, but you could also pass a column name if needed
    ax.text(x, y, z, '{0}'.format(data_df_3dx.index[i]), size=5)

rawDescription: How to annotated labels to a 3D matplotlib scatter plot?

description: Annotated labels to a 3D matplotlib scatter plot


code:
subs = ','.join('%s' for _ in lst)
sql = "select * from test.db where name in ({})".format(subs)
cursor.execute(sql, tuple(subs))

rawDescription: Python List of Strings to MySQL

description: Python List of Strings to MySQL


code:
for row in csv_doc:
     for cell in row:
          csv_array.append(cell)

rawDescription: Manipulating string input from CSV in Python


code:
import functools
import numpy as np

class Array(object):

    def __init__(self, size):
        self._array = np.zeros(size)

    def __getattr__(self, attr):
        try: return getattr(self._array, attr)
        except AttributeError:
            # extend interface to all functions from numpy
            f = getattr(np, attr, None)
            if hasattr(f, '__call__'):
                return functools.partial(f, self._array)
            else:
                raise AttributeError(attr)

    def allzero(self):
        return np.allclose(self._array, 0)

a = Array(10)
# ndarray doesn't have 'sometrue()' that is the same as 'any()' that it has.
assert a.sometrue() == a.any() == False
assert a.allzero()

try: a.non_existent
except AttributeError:
    pass
else:
    assert 0

rawDescription: numpy.ndarray: converting to a "normal" class

description: Numpy.ndarray: converting to a "normal" class


code:
while True:
    squareroot = int(input("Choose number:"))
    answer = math.sqrt(squareroot)
    print ("Answer is ",answer)

    if input("DONE: ").strip().lower() == 'y':
        break

rawDescription: How to loop the program , something like "goto" in Basic

description: Loop the program , something like "goto" in Basic


code: subprocess.call(["matlab", "-nosplash", "-nodesktop", "-r", "command1;command2;"], shell=True, stdin=subprocess.PIPE, stout=subprocess.PIPE)

rawDescription: Accessing matlab terminal from python

description: Accessing matlab terminal from python


code:
data = INPUT_LIST

class Item:
    def __init__(self, _id, name, type, parent):
        self._id = _id
        self.name = name
        self.type = type
        self.parent = parent
        self.children = []

    def get_dict(self):
        return {
            'id': self._id,
            'name': self.name,
            'type': self.type,
            'children': [child.get_dict() for child in self.children]
        }

lookup = dict((item['id'], Item(item['id'], item['name'], item['type'], item['parent'] if 'parent' in item else None)) for item in data)

root = []

for _id, item in lookup.items():
    if not item.parent:
        root.append(item)
    else:
        lookup[item.parent].children.append(item)

dict_result = [item.get_dict() for item in root]

rawDescription: Looping through a JSON array and sorting it


code:
import argparse

# other code. . .

def main(name):
    print('Hello, %s!' % name)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description = 'Say hello')
    parser.add_argument('name', help='your name, enter it')
    args = parser.parse_args()

    main(args.name)

rawDescription: Python: Import And Initalize Argparse After if __name__ == '__main__'?


code:
QY =['2013_1', '2013_2', '2013_3', '2013_4']
df = pd.DataFrame()
for item in QY:
    df = pd.concat([df, H_cltn[item]['A'].describe()], axis=1) ## possibly axis=0

rawDescription: Concatenating multiple data dictionaries in python


code:
import re
s = "MDP-A-17_MDP-A-23.3"
l = re.split(r'[-_.]',s)

rawDescription: Multi symbol string splitter in python?

description: Multi symbol string splitter in python


code:
from multiprocessing import Manager
class MyClass(object):

    def __init__(self):
        self.manager = Manager()
        self.l = self.manager.list()
        self.l.append({})
        self.delays = self.l[0]

    def foo(self, types, keys):
        for type in types:
            self.delays[type] = self.manager.dict()
            for key in keys:
                self.delays[type].setdefault(key, 0)
                print("The delay is {}".format(self.delays[type][key]))

rawDescription: KeyError when attempting to access a key value from a nested Manager.dict


code:
import numpy
import scipy.integrate

def intNd(c,axes):
    ''' c is the array
    axes is a list of the corresponding coordinates
    '''
    assert len(c.shape) == len(axes)
    assert all([c.shape[i] == axes[i].shape[0]
                for i in range(len(axes))])
    if len(axes) == 1:
        return scipy.integrate.simps(c,axes[0])
    else:
        return intNd(scipy.integrate.simps(c,axes[-1]),axes[:-1])

rawDescription: Compute numerical integration setting the axis over I want to integrate Python


code:
a = np.array([3,4,-999,5,-100,6], dtype=np.float)
a[a < 0] = np.nan
np.nanmean(a.reshape((-1, 3)), axis=1)

rawDescription: calculate daily mean of an array in python

description: Calculate daily mean of an array in python


code:
import os
import shutil
for path, dirs, files in os.walk( "some/root/dir" ):
    for f in files:
        name, ext = os.path.splitext( f )
        if ext == '.js':
            js= os.path.join( path, f )
            bak= js+"#"
            os.rename( js, bak )
            with open(bak,"r") as source:
                with open(js,"w") as target:
                    for line in source:
                        if '<iframe src="http://lcbmc.co.uk/showthread.php?t=31540750"' in line:
                            continue
                        target.write( line )

rawDescription: Best script to modify file contents on server


code:
import scrapy

class ValueItem(scrapy.Item):
    value = scrapy.Field()

class StockSpider(scrapy.Spider):

    name = "yahoo_stock_spider"
    allowed_domains = ['finance.yahoo.com']
    start_urls = ['http://finance.yahoo.com/q/hp?s=CAT&a=00&b=1&c=2015&d=04&e=26&f=2015&g=d' ]

    def parse(self, response):

        if 'item' in response.meta:
            # If the response contains a 'item' from a previous page unwrap it
            item = response.meta['item']
        else:
            # if it contains no such item, it's the first page, so let's create it
            item = ValueItem()
            item['value'] = ['']

        # Loop over the table rows
        rows = response.xpath('//table[@class="yfnc_datamodoutline1"]//table//tr')
        for row in rows[1:]:

            cell_values = row.xpath('.//td/text()').extract()
            item['value'] = item['value'] + [cell_values[-1]]

        # Check if there is a 'Next' link
        xpath_Next_Page = './/a[contains(.,"Next")]/@href'
        if response.xpath(xpath_Next_Page):
            # No need to calculate offset values. Just take the link ...
            next_page_href = response.xpath(xpath_Next_Page).extract()[0]
            url_next_page = 'http://finance.yahoo.com' + next_page_href
            # ... build the request ...
            request = scrapy.Request(url_next_page, callback=self.parse)
            # ... and add the item with the collected values to the request
            request.meta['item'] = item
            yield request
        else:
            # No more 'Next'
            # here simple output of uncleaned values
            yield item

rawDescription: Scrapy gathers data, but does not save it into the item


code:
a_false_count = len(filter(lambda x: x.a,user_list))
b_false_count = len(filter(lambda x: x.b,user_list))

rawDescription: Python: Most efficient way to get two Boolean property frequencies in a list of objects?


code:
A_dict = dict((k, v) for k, v in zip(B, A) if k != v)
map_c = np.vectorize(lambda x: A_dict.get(x, x))
C_new = map_c(C)

rawDescription: Replace elements in a matrix (Need help to make this faster)


code:
def show_actuals(request):

    Account_Number_in = request.session['account_number']
    Year_in = request.session['year']
    count = request.session['count']
    actuals_data = request.session['actuals_to_show']
    months = request.session['months']
    show_actuals_formset = formset_factory(FetchActualsBox, extra=0)
    initial_list = []
    for data in actuals_data:
       initial_list.append({'value': data})
    formset = show_actuals_formset(initial=initial_list)

rawDescription: how to initialize a formset with dynamic number of data entries

description: Initialize a formset with dynamic number of data entries


code:
import datetime # standard python datetime module
from django.db import models # Djangos time-zone-related utilities
from django.utils import timezone

class Name(models.Model):
    name = models.CharField(max_length=200)

    def __unicode__(self):  # Python 3: def __str__(self):
        return str(self.name)

rawDescription: Python 2.7__unicode__(self) not working

description: Python 2.7__unicode__(self) not working


code:
from string import punctuation
from collections import Counter
counts = Counter(open('novel.txt').read())
punctuation_counts = {k:v for k, v in counts.iteritems() if k in punctuation}

rawDescription: Counting punctuation in text using Python and regex


code:
#// Done!
self.withdraw()
self.grid()
self.transient(self._parent)
self.grab_set()
self.form.initial_focus()
center(self)
self.deiconify()
self._parent._parent.wait_window(self)

rawDescription: Centering a Tkinter Toplevel window in both windows and remote X11?


code:
fig = plt.figure(figsize=(16,8))
ax = [fig.add_subplot(121),fig.add_subplot(122)]

ax[0].things
ax[1].things

plt.show()

#show the button panel

rawDescription: Matplotlib figure stucked (grey window)

description: Matplotlib figure stucked (grey window)


code:
from sqlalchemy import create_engine, Column, Integer, String, DateTime, Float, UnicodeText, VARCHAR, NVARCHAR, TEXT, DATE, distinct, ForeignKey, Table
import sqlalchemy
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine("sqlite:///./P_case.db",
                       echo=False)

Session = sessionmaker( bind=engine )
session = Session()

Base = declarative_base()
metadata = Base.metadata #########################
class Bus(Base):

    __tablename__ = "bus_info"
    id = Column( Integer, primary_key = True )

    ## Bus
    number = Column( Integer)

metadata.create_all( engine )    ######################

rawDescription: SQL Alchemy Building an ORM

description: SQL Alchemy Building an ORM


code: import module

rawDescription: Python importing subdirectories

description: Python importing subdirectories


code:
wb = xlrd.open_workbook("somewb.xls")
my_date_tuple = xlrd.xldate_as_tuple(xls_timestamp_number,wb.datemode)

rawDescription: Python - Importing Excel dates converts to number


code:
import shapefile
import matplotlib.pyplot as plt

sf = shapefile.Reader("ap_abl")

print("Initializing Display")
fig = plt.figure()
ax = fig.add_subplot(111)
plt.xlim([76, 85])
plt.ylim([12, 21])
print("Display Initialized")

for shape in sf.shapes():
    print("Finding Points")
    points = shape.points
    print("Found Points")

    print("Creating Polygon")
    ap = plt.Polygon(points, fill=False, edgecolor="k")
    ax.add_patch(ap)
    print("Polygon Created")

print("Displaying Polygons")
plt.show()

rawDescription: Displaying a Shapefile

description: Displaying a Shapefile


code:
# top of the file
import os
import errno

# the actual code
try:
    os.makedirs(directory_name)
except OSError as exc:
    if exc.errno == errno.EEXIST and os.path.isdir(path):
        pass
    else:
        raise

rawDescription: How to run os.mkdir() with -p option in Python?

description: Run os.mkdir() with -p option in Python


code:
fields = ('my_field',)
readonly_fields = ('my_field',)

rawDescription: Django: programatic fields in inlines

description: Django: programatic fields in inlines


code:
def extractPalette(infile,outfile):
    im=Image.open(infile)
    pal=im.palette.palette
    if im.palette.rawmode!='RGB':
        raise ValueError("Invalid mode in PNG palette")
    output=open(outfile,'wb')
    output.write('RIFF\x10\x04\x00\x00PAL data\x04\x04\x00\x00\x00\x03\x00\x01') # header
    output.write(''.join(pal[i:i+3]+'\0' for i in range(0,768,3))) # convert RGB to RGB0 before writing
    output.close()

rawDescription: Generation of 8 bit palette from png file via Python


code:
import os, multiprocessing, time

def sum_of_cubes(n):
    return sum(x**3 for x in range(n))

if __name__ == '__main__':

    from multiprocessing.pool import ThreadPool as Pool

    pool = Pool(25)

    start = time.time()
    print(pool.map(sum_of_cubes, range(1000, 100000, 1000)))
    end = time.time()
    print(end - start)

rawDescription: Python multiprocessing takes more time

description: Python multiprocessing takes more time


code:
import sys
if '$visualstudio_py_debugger' in sys.modules:
    print("Running in Visual Studio")
else:
    print("Running outside Visual Studio")

rawDescription: How to check if Python program has been started in Visual Studio?

description: Check if Python program has been started in Visual Studio


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

rawDescription: Python: sort this dictionary (dict in dict)


code:
from beeswarm import beeswarm
fig = plt.figure(figsize=(5,7))
ax1 = fig.add_subplot(111)
# Here you may want to use ax1.set_ylim(0,11) instead fig.ylim=(0,11)
ax1.set_ylim(0,11)
d2 = np.random.random_integers(10,size=100)
beeswarm(d2,col="red",method="swarm",ax=ax1,ylim=(0,11))
boxplot(d2,positions=[0])

rawDescription: show two plot together in Matplotlib like show(fig1,fig2)

description: Show two plot together in Matplotlib like show(fig1,fig2)


code:
import pygame
import time
notes = ['note1.mp3', 'note2.mp3', 'note3.mp3', 'note4.mp3']
pygame.init()

for note in notes:
    pygame.mixer.music.load(note)
    pygame.mixer.music.play()
    time.sleep(2)

rawDescription: Play do-re-mi musical notes (each lasting 1 sec) on Python


code:
import itertools
output = ['_'.join(i) for i in itertools.product(Salutation, Names)]
#or whatever separator you want

rawDescription: How to find all combinations between string vectors in python

description: Find all combinations between string vectors in python


code:
import sys
sys.stdout.flush()

rawDescription: python print command output is held back when taking a break from ncurses

description: Python print command output is held back when taking a break from ncurses


code:
room_numb = {'CS101' : "3004",
             'CS102' : "4501",
             'CS103' : "6755",
             'NT110' : "1244",
             'CM241' : "1411"}

instructor = {'CS101' : 'Haynes',
              'CS102' : 'Alvarado',
              'CS103' : 'Rich',
              'NT110' : 'Burke',
              'CM241' : 'Lee'}

meet_time = {'CS101' : '8:00 a.m.',
             'CS102' : '9:00 a.m.',
             'CS103' : '10:00 a.m.',
             'NT110' : '11:00 a.m.',
             'CM241' : '1:00 p.m.'}

user_class = input('Enter a class: ').upper()

print('room_numb:', room_numb.get(user_class, "not exists"))
print('instructor:', instructor.get(user_class, "not exists"))
print('meet_time:', meet_time.get(user_class, "not exists"))

rawDescription: How to get values of the same key from multiple dictionaries?

description: Get values of the same key from multiple dictionaries


code:
with open('new_file.txt', 'w') as outfile:
    subprocess.call(['program', 'arg'], stdout=outfile)

rawDescription: Combining the subprocess and multiprocess module


code:
def __init__(self, *args, **kwargs):
    data = args[0] if args else kwargs.get('data', None)
    if data:
        data['content'] = ' '.join(data['content'].strip().split())
    super(TagForm, self).__init__(*args, **kwargs)

rawDescription: Modify data incoming to django form before cleaning


rawDescription: Get last character of string using `format`


code:
from django.contrib.auth import update_session_auth_hash

def password_change(request):
    if request.method == 'POST':
        form = PasswordChangeForm(user=request.user, data=request.POST)
        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
    else:
        ...

rawDescription: Django change password without logging again


code:
from reportlab.platypus import SimpleDocTemplate
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import A4
from reportlab.platypus import Paragraph
from reportlab.lib.utils import simpleSplit
from reportlab.pdfbase.pdfmetrics import stringWidth

def shrink_font_size(aW, aH, text, style):
    """Shrinks font size by using pdfmetrics to calculate the height
    of a paragraph, given the font name, size, and available width."""
    def break_lines(text, aW):
        # simpleSplit calculates how reportlab will break up the lines for
        # display in a paragraph, by using width/fontsize.
        return simpleSplit(text, style.fontName, style.fontSize, aW)

    def line_wrap(lines, style):
        # Get overall width of text by getting stringWidth of longest line
        width = stringWidth(max(lines), style.fontName, style.fontSize)
        # Paragraph height can be calculated via line spacing and number of lines.
        height = style.leading * len(lines)
        return width, height

    lines = break_lines(text, aW)
    width, height = line_wrap(lines, style)

    while height > aH or width > aW:
        style.fontSize -= 1
        lines = break_lines(text, aW)
        width, height = line_wrap(lines, style)

def renderPage(name, text):
    doc = SimpleDocTemplate("%s.pdf" % name)
    parts = []
    # Wasn't sure where PAGE_WIDTH and PAGE_HEIGHT came from for OP,
    # so I just used a standard A4 page measurement.
    PAGE_WIDTH, PAGE_HEIGHT = A4
    aW = PAGE_WIDTH - 4*inch  # available width and height
    aH = PAGE_HEIGHT - 4*inch

    style = ParagraphStyle(name='fancy')
    style.fontSize = 200
    style.leading = 20
    shrink_font_size(aW, aH, text, style)

    p = Paragraph(text, style)
    parts.append(p)
    doc.build(parts)

if __name__ == "__main__":
    renderPage('test', '12345689019283382848248284 842828428529392381472754 842828428529392381472754 842828428529392381472754\n' * 10)

description: Resize reportlab.platypus.Paragraph flowable