code:
engine = sqlalchemy.create_engine("sqlite:///:memory:")
engine.execute("select ?", 5).fetchall()
engine.execute("select ?", "; drop table users; --").fetchall()

rawDescription: Lightweight DBAL for python

description: Lightweight DBAL for python


rawDescription: adding list indexes in python

description: Adding list indexes in python


code:
@defer.inlineCallbacks
def procShutdownStuff(self):
    isWorking = yield deferToThread(self.stuff.isWorking)

    if isWorking:
        yield deferToThread(self.stuff.shutdown)

def cleanup(self):
    return self.procShutdownStuff()

rawDescription: twisted: how to communicate elegantly between reactor code and threaded code?

description: Twisted: how to communicate elegantly between reactor code and threaded code


code:
import itertools

getx, gety = lambda a: a['x'], lambda a: a['y'] # or use operator.itemgetter
groups = itertools.groupby(sorted(lst, key=getx), key=getx)
m = [max(b, key=gety) for a,b in groups]
copy = [l for l in lst if l in m]

rawDescription: Removing duplicates in lists of objects keeping the max and the order


code:
dtype = [('start', np.int32), ('ZZ', np.float32, (Ny1, Nx1)), ('end', np.int32)]
with open(fname1, 'rb') as fh:
    data = np.fromfile(fh, dtype)
print(data['ZZ'])

rawDescription: Reading several arrays in a binary file with numpy


code:
servers = []

for port in portlist:
    ds = ("0.0.0.0", port)

    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server.bind(ds)
    server.listen(1)

    servers.append(server)

while True:
    # Wait for any of the listening servers to get a client
    # connection attempt
    readable,_,_ = select.select(servers, [], [])
    ready_server = readable[0]

    connection, address = ready_server.accept()

    # Might want to spawn thread here to handle connection,
    # if it is long-lived

rawDescription: Python TCP several listen on several ports at once


code:
objA.pk = objB.pk
objA.save()

rawDescription: How to copy a object to an existing object in Django

description: Copy a object to an existing object in Django


code: return {wx_grad, wRec_grad}, grad_over_time

rawDescription: Lua function returning tuple of values

description: Lua function returning tuple of values


code:
def mypowerfunction(my_list, exponent=2):
    return [x ** exponent for x in my_list]

mypowerfunction([2,4,3])

rawDescription: How to create a power function

description: Create a power function


code:
x = df['pipestring'].apply(lambda x: pd.Series(x.split('|')))
x
df.join(x).set_index(['wibble'])

rawDescription: Add dimension to pandas DataFrame

description: Add dimension to pandas DataFrame


code:
input = numpy.array(range(10))
data = numpy.zeros((2,2), dtype=numpy.ndarray)
data[1][1] = input
data

rawDescription: How can i store a list within a numpy 2d array?

description: Store a list within a numpy 2d array


code:
counter = 0
for list in data:
    counter = counter + 1
    if counter == 1:
        value = 'Vrspng'
    else:
        value = counter
    list.append(value)

rawDescription: Calculate and add column in python

description: Calculate and add column in python


code:
import praw
r = praw.Reddit(user_agent="bot by /u/{0}".format("YOUR-USERNAME"))
submission = r.get_info(thing_id="t1_asdasd")
print(submission)

rawDescription: PRAW: How to get a reddit comment object with just the comment ID?


code:
df1
df2
df1.update(df2[['B']])  # subset of cols = ['B']
df1

rawDescription: How do I get panda's "update" function to overwrite numbers in one column but not another?

description: Get panda's "update" function to overwrite numbers in one column but not another


code:
import pandas as pd
d = {1 : {'we': 26, 'is': 112},
     2 : {'tp': 26, 'fp': 91},
     3 : {'pp': 23, 'kj': 74}}
# Replace the inner dicts with their string representations
for i in d:
    d[i] = str(d[i])
# Convert to dataframe
df = pd.DataFrame.from_dict(d, orient='index').reset_index()
# Clean up column names
df.rename(columns={'index': 'row', 0: 'col1'}, inplace=True)

rawDescription: converting dictionary to dataframe column in pandas

description: Converting dictionary to dataframe column in pandas


code: ','.join(str(i) for i in xrange(100) if i % 4 in (1,2))

rawDescription: Generate a sequence of numbers in Python


code:
def calc_state_series(entry,stay, max_time=5):
    reduce=(copy.deepcopy(entry)*0).fillna(0) #just for initalization
    reduce[(entry.shift(max_time)==1) & (pd.rolling_mean(stay,max_time)==1)]-=1
    entry=(entry+stay.shift(1)).fillna(0) #reduce state after max_time
    x=entry.values
    x = np.concatenate(([0], x))
    y=stay.values
    y=np.concatenate(([0], y))
    nans = y==0
    x = np.array(x)
    x[nans] = 0
    reset_idx = np.zeros(len(x), dtype=int)
    reset_idx[nans] = np.arange(len(x))[nans]
    reset_idx = np.maximum.accumulate(reset_idx)
    cumsum = np.cumsum(x)
    cumsum = cumsum - cumsum[reset_idx]
    return pd.Series(cumsum[1:], index=entry.index)

rawDescription: Pandas/Numpy: Calculate current state series based on binary signals


code:
def run(self):
    env = self.state.document.settings.env  # sphinx.environment.BuildEnvironment
    config = env.config                     # sphinx.config.Config
    folder = config["thumbnails_folder"]
    ...

rawDescription: How do I read conf.py setting in Sphinx extension node?

description: Read conf.py setting in Sphinx extension node


code:
import numpy, gdal, gdalconst
dataset = gdal.Open("multipage.tiff", gdalconst.GA_ReadOnly)
data = numpy.array([gdal.Open(name, gdalconst.GA_ReadOnly).ReadAsArray()
                    for name, descr in dataset.GetSubDatasets()])

rawDescription: Open tiff image stack in python with gdal


code:
arr = numpy.array(((2,2),(2,-2)))
tuple(map(tuple, arr))

rawDescription: Convert numpy array to tuple

description: Convert numpy array to tuple


code:
result = pd.merge(df,updt.rename(columns={'firstname' : 'name'}),on=['name'],suffixes=['_l','_r'],how='outer')
result
result['age'] = result['age_r'].combine_first(result['age_l'])
result

rawDescription: Pandas: update dataframe using rows as index but keeping original index safe


code:
import sys
from PyQt4 import QtCore, QtGui
class AppWindow (QtGui.QMainWindow):
    def __init__ (self):
        super(AppWindow, self).__init__()
        mainWidget = QtGui.QWidget(self)
        self.setCentralWidget(mainWidget)
        mainLayout = QtGui.QVBoxLayout()
        mainWidget.setLayout(mainLayout)
        frame = QtGui.QFrame()
        frame.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        frame.connect(frame, QtCore.SIGNAL("customContextMenuRequested(QPoint)" ), self.up)
        mainLayout.addWidget(frame)
        self.modal = QtGui.QInputDialog(self)
        self.modal.setWindowModality(QtCore.Qt.ApplicationModal)

    def up (self, QPos):
        self.modal.move(QtGui.QCursor.pos())
        self.modal.show()
        self.modal.raise_()

app = QtGui.QApplication([])
window = AppWindow()
window.show()
sys.exit(app.exec_())

rawDescription: How to make one window to block another without using .setModal(True)

description: Make one window to block another without using .setModal(True)


code:
def myview(request):
    if request.method == "POST":
        form = MotivatorForm(request.POST, request.FILE)
        if form.is_valid():
            instance = form.save()
            filepath = instance.image.path
    # etc...

rawDescription: Access renamed image name from ModelForm in views?


code:
text_file= open(filename,'r')
data=text_file.read()
listOfSentences = data.split(".")

rawDescription: Python Regex split text file into sentences and append to a list


code:
import timeit
# For the set
timeit.timeit("for i in s: pass", "s = set([1,4,7,10,13])")
# For the list
timeit.timeit("for i in l: pass", "l = [1,4,7,10,13]")

rawDescription: Which one is faster: iterating over a set and iterating over a list


code:
def splitWithPandas(self, data_file, split_by_column):
        values_to_split_by = pd.read_csv(data_file, delimiter="|", usecols=[split_by_column])
        values_to_split_by.drop_duplicates()
        values_to_split_by = pd.unique(values_to_split_by.values.ravel())

        for i in values_to_split_by:
            iter_csv = pd.read_csv(data_file, delimiter="|", chunksize=100000)
            df = pd.concat([chunk[chunk[split_by_column] == i] for chunk in iter_csv])
            df.to_csv("data_chunk_"+i, sep="|", index=False)

rawDescription: Split big csv file by the value of a column in python


code:
def wait(self, sleep=None):
    """Waits for any of the watched paths to generate an event,
    which is a ``(path, token)`` pair, where the first element
    is event path, i.e. the actual path that was modified and
    second element is a token, passed to the :meth:`watch`.
    :param float sleep: number of seconds to sleep between event
                        checks.
    """
    while True:
        if self.client.events:
            packet = self.client.events.popleft()
            return Event(*packet.payload.split("\x00")[:-1])

        # Executing a noop, hopefuly we'll get some events queued
        # in the meantime. Note: I know it sucks, but it seems like
        # there's no other way ...
        self.client.execute_command(Op.DEBUG, "")

        if sleep is not None: #sleep here is to provide tap gap between event check
            time.sleep(sleep)

rawDescription: Python daemon to monitor GPIO pins using pyxs Xenstore client


code:
for i, month in enumerate(months,1):
    line.replace(month, str(i))

rawDescription: Converting a month's name to its corresponding number


code:
from hashlib import sha1
from flask.sessions import session_json_serializer
from itsdangerous import URLSafeTimedSerializer

s = URLSafeTimedSerializer(
    'dev', salt='cookie-session',
    serializer=session_json_serializer,
    signer_kwargs={'key_derivation': 'hmac', 'digest_method': sha1}
)
session_data = s.loads('eyJoZWxsbyI6IndvcmxkIn0.BwEv5w.o3gYYutryNy7di1E3LbJZbCFGfY')
assert session_data['hello'] == 'world'  # True

rawDescription: How can a session cookie from Flask-0.10 be deserialized manually?


code:
import os.path
p1="path.dll"
print (os.path.abspath(p1))
p2="${env_var}/path.dll"  #Make sure you set env_var in the calling environment...Otherwise it won't be expanded...
print (os.path.expandvars(p2))

rawDescription: python: unable to specify relative path for dll import

description: Python: unable to specify relative path for dll import


rawDescription: Converting hex string representation to float in python


code:
def findNext(function):
    if function == 'y':
        return 'somestring',function[1:]
function = 'x'
print(findNext(function))
temp, function = findNext(function)

rawDescription: NoneType Error, Python

description: NoneType Error, Python


code:
import datetime
d = datetime.datetime(2012, 9, 23, 0, 0)
d.strftime('%Y%m%d')
int(d.strftime('%Y%m%d'))

rawDescription: convert python datetime to oracle number

description: Convert python datetime to oracle number


code: ak.sort(key=lambda x:x[0].lower())

rawDescription: case-insensitive alphabetical sorting of nested lists

description: Case-insensitive alphabetical sorting of nested lists


code:
class Player:
  def __init__(self, name, hp):
    self.name = name
    self.hp = hp

rawDescription: Changing renamed global variables from inside a function (python)


code:
def is_made_from(wordA,wordB):
    for l in wordA:
        if not wordA.count(l) <= wordB.count(l):
            return False
    return True

rawDescription: Struggling to find the error in my word-finding code?


code:
import pylab as pl
mycmap = pl.cm.jet # for example
for entry in pl.unique(raw_data):
    mycolor = mycmap(entry*255/(max(raw_data) - min(raw_data)))
    pl.plot(0, 0, "-", c=mycolor, label=mynames[entry])

pl.imshow(raw_data)
pl.legend()

rawDescription: How to add legend to imshow() in matplotlib

description: Add legend to imshow() in matplotlib


code: rand_list = [randrange(700, 1500) for _ in xrange(100)]

rawDescription: Generating List of N Random Numbers Between a Range of Numbers


code:
idx = df.resample('1Min').agg({'Acceleration': np.argmax})
df.ix[idx.Acceleration]

rawDescription: Getting index from resample in Pandas

description: Getting index from resample in Pandas


code:
def _get_pos_and_group(s):
    words = ["(@sosiora\*+([1-5]*)(\W|)+(@[a-z]*)(\W|))",
             "((@[a-z]*)(\W|)@sosiora\*+([1-5]*)+(\W|))"
            ]

    for i, w in enumerate(words):
        if re.search(w, s):
            return i, re.search(w, s).group(0)
    return -1, None

print(_get_pos_and_group("@sosiora*4 @samsungID"))

(0, '@sosiora*4 @samsung')

rawDescription: How to detect if string is matched with some of many patterns in regex

description: Detect if string is matched with some of many patterns in regex


code:
import socket
s=socket.socket()
host=socket.gethostbyname('www.google.com')
port=80
s.connect((host,port))
s.sendall("GET /\r\n")
val = s.recv(10000)
# Split off the HTTP headers
val = val.split('\r\n\r\n',1)[1]

rawDescription: How to get html code using python sockets

description: Get html code using python sockets


code:
import win32com.client
object = win32com.client.Dispatch("Outlook.Application")

rawDescription: Python 3.0 Windows/COM

description: Python 3.0 Windows/COM


code:
c.InteractiveShellApp.exec_lines = [
'import sys,os; sys.path.append(os.getcwd())'
]

rawDescription: how to add directory to sys.path on ipython startup

description: Add directory to sys.path on ipython startup


code:
for word in filterKeywords:
    for r in db[word].find({'text': {'$exists': True}}, {'text': 1, '_id': False}):
        appleSentimentText.append(r['text'])

rawDescription: Code to extract text fields from a mongodb collection and append it to a list in python using pymongo


code: obj = PageModel.get_by_id(page_id)

rawDescription: App Engine, Python: how to filter query by ID?


code:
import random
n = 4

A = [i for i in xrange(n)]
B = [1,1,1,2]
NextGeneration = []
for i in xrange(n-1):
    last = n - i - 1
    actualChoice =  random.randint(0,last)
    NextGeneration.append(B[A[actualChoice]])
    auxSwap = actualChoice
    A[actualChoice] = A[last]

rawDescription: Population genetics modelling in python

description: Population genetics modelling in python


code:
k_out = ['word1', 'word2', 'some larger text']
c_lst = ['This is some text that contains no rejected word', 'This is some larger text which means this list should be rejected']
any(keyword in string for string in c_lst for keyword in k_out)

rawDescription: Find if any string element in list is contained in list of strings


code:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
drive = GoogleDrive(gauth)

f = drive.CreateFile({'parent': parent_id})
f.SetContentFile('cat.png') # Read local file
f.Upload() # Upload it

rawDescription: python upload file to google drive folder that is shared with me

description: Python upload file to google drive folder that is shared with me


code: x, y = get_point(foo)

rawDescription: How to decide when to introduce a new type instead of using list or tuple?

description: Decide when to introduce a new type instead of using list or tuple


code:
def XOR(x, y):
    return x ^ y

rawDescription: Basic XOR function

description: Basic XOR function


code:
df = DataFrame({'a': randint(3, size=10)})
df
s = df.a[:5]
dfa, sa = df.align(s, axis=0)
dfa
sa

rawDescription: add column with constant value to pandas dataframe

description: Add column with constant value to pandas dataframe


code:
redisClient.set('test_redis', 'Hello Python'.encode('utf-8'))
redisClient.get('test_redis').decode('utf-8')

rawDescription: About char b prefix in Python3.4.1 client connect to redis


code:
import numpy as np
arr = np.ma.array([0,np.nan,1])
print(np.ma.max(arr))
# >>>nan    # since there is no mask
marr = np.ma.masked_array([0,np.nan,1], np.isnan(arr))
print(np.ma.max(marr))
# >>>1.0    # since the mask tells mask to ignore the nan. The max of the rest (0,1) is 1.

rawDescription: computing with nan's with numpy's ma module

description: Computing with nan's with numpy's ma module


code:
def read_or_write_file(self, file, section, passed_option = None,
                      value = None, read = True):

    config = ConfigParser.RawConfigParser()
    with open(file) as configfile:
        config.readfp(configfile)

    if read:
        options = config.options(section)

        for option in options:
            file_settings[option] = config.get(section, option)

    else:
        config.set(section, passed_option, value)

        with open(file, 'w') as configfile:
            config.write(configfile)

rawDescription: How To Read and Write to a INI file in Python


code:
from colorsys import hls_to_rgb
import numpy as np

def get_distinct_colors(n):

    colors = []

    for i in np.arange(0., 360., 360. / n):
        h = i / 360.
        l = (50 + np.random.rand() * 10) / 100.
        s = (90 + np.random.rand() * 10) / 100.
        colors.append(hls_to_rgb(h, l, s))

    return colors

rawDescription: How to set a colormap which can give me over 20 distinct colors in matplotlib?

description: Set a colormap which can give me over 20 distinct colors in matplotlib


code:
from oauth2client.file import Storage
from oauth2client.tools import run

...

# Location of the credentials storage file
STORAGE = Storage('gmail.storage')

...

# Try to retrieve credentials from storage or run the flow to generate them
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
  credentials = run(flow, STORAGE, http=http)

rawDescription: Using refresh token with Google directory service API


code:
for p in tree.xpath("//p"):  # This gets all the p elements
    dfn = p.xpath('./dfn')[0]  # may want to check this exists first
    after_dfn = p.xpath("./dfn/following-sibling::node()")
    for x in after_dfn:
        pass  # do whatever you need to do with the stuff after dfn

rawDescription: How to print out all the text information from an html-file with lxml parsing?

description: Print out all the text information from an html-file with lxml parsing


code:
is_running = lambda: my_process.poll() is None

my_process = subprocess.Popen(' '.join(exclude),
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE,
                              shell=True)

# Grab all the output from stdout and stderr and log it
while is_running():
    rlist, wlist, xlist = select.select([my_process.stdout, my_process.stderr], [], [], 1)

# Log stdout, but don't spam the log
if my_process.stdout in rlist and verbose:
    # Adjust the number of bytes read however you like, 1024 seems to work
    # pretty well for me.
    Tracer.log.debug(my_process.stdout.read(1024))

# Log stderr, always
if my_process.stderr in rlist:
    # Same as with stdout, adjust the bytes read as needed.
    Tracer.log.error(my_process.stderr.read(1024))

rawDescription: Python subprocess call hangs

description: Python subprocess call hangs


code:
dfL
dfL1 = dfLeft.set_index('cuspin', append=True)
dfR1 = dfRight.set_index('idc_id', append=True)
dfL1
dfL1.join(dfR1)

rawDescription: Pandas Merge (pd.merge) How to set the index and join


code: message = message.replace(old, replacement)

rawDescription: Replace List in Json Object Function

description: Replace List in Json Object Function


code:
for row in rows:
     # Write rows to text file
     user = row[0]
     tag = row[1]
     writer.write(user + ' ' + tag + '\n')

rawDescription: Python postgres output formatting

description: Python postgres output formatting


code:
qsts_pks = QueryStringTag.objects.filter(tag__pk__in=['12', '14', '15']).values_list('id', flat=True)
queries = QueryString.objects.filter(qsquerystring__pk__in=qsts_pks)

rawDescription: Django filtering by multiple tags in many to many ORM object


code:
import shlex
shlex.split('arg arg1 "arg2 with quotes" arg4 arg5=1')

rawDescription: Python String Argument Parsing

description: Python String Argument Parsing


code:
import matplotlib as mpl
mpl.use('TkAgg')
import numpy as np
from  matplotlib import pyplot as plt

plt.close('all')

plt.rc('text', usetex = True)
font = {'family' : 'normal',
        'weight' : 'normal',
        'size'   : 25}
plt.rc('font', **font)

fig = plt.figure()
fig.set_size_inches(14.5, 10.5)
ax = fig.add_axes([0.1, 0.1, 0.85, 0.75])

#---- plot some data ----

ax.plot(np.arange(10), np.random.randn(10), 'rd', ms=15,
        label='The quick brown fox...')

ax.errorbar(np.arange(10), np.random.randn(10), yerr=0.5, fmt='o',
            color='g', ecolor='g', capthick=1,ls = '-', lw=2, elinewidth=1,
            label = "... jumps over the lazy dog.")

#---- plot legend ----

legend = plt.legend(bbox_to_anchor=(0., 1.02, 1., 0.), handletextpad=0.3,
                    loc='lower left', ncol=6, mode="expand", borderaxespad=0,
                    numpoints=1, handlelength=0.5)

#---- adjust fontsize and va ----

plt.setp(legend.get_texts(), fontsize='15', va='bottom')

#---- set legend's label vert. pos. manually ----

h = legend.legendHandles
t = legend.texts
renderer = fig.canvas.get_renderer()

for i in range(len(h)):
    hbbox = h[i].get_window_extent(renderer) # bounding box of handle
    tbbox = t[i].get_window_extent(renderer) # bounding box of text

    x = tbbox.x0 # keep default horizontal position
    y = (hbbox.height - tbbox.height) / 2. + hbbox.y0 # vertically center the
    # bbox of the text to the bbox of the handle.

    t[i].set_position((x, y)) # set new position of the text

plt.show(block=False)
plt.savefig('legend_text_va.png')

rawDescription: Alignment of text and the icon in the legend box (matplotlib)


code:
import pandas as pd
#open files as pandas datasets
file1 = pd.read_csv(filepath1, sep = '\t')
file2 = pd.read_csv(filepath2, sep = '\t')

#merge files by column 'Gene' using 'inner', so it comes up
#with the intersection of both datasets
file3 = pd.merge(file1, file2, how = 'inner', on = ['Gene'], suffixes = ['1','2'])
file3.to_csv(filepath3, sep = ',')

rawDescription: find matches in two file using python

description: Find matches in two file using python


code:
from __future__ import division
import scipy as sp
import mpl_toolkits.mplot3d as a3d
import pylab as pl

nan = sp.nan

def axesoff():
    box = [[-1,1,1,-1,-1,1,1,-1,-1,-1,nan,1,1,nan,1,1,nan,-1,-1],
           [-1,-1,-1,-1,1,1,1,1,-1,-1,nan,-1,1,nan,1,-1,nan,1,1],
           [-1,-1,1,1,1,1,-1,-1,-1,1,nan,-1,-1,nan,1,1,nan,-1,1]]
    ax3.plot(*box,color='k')
    for axis in (ax3.w_xaxis,ax3.w_yaxis,ax3.w_zaxis):
        for obj in axis.get_ticklines(): obj.set_visible(False)
        axis.set_ticklabels('')
        axis.line.set_visible(False)
        axis.pane.set_visible(False)
    ax3.grid(False)
    ax3.axis('equal')

#------here we go
x,y,z = sp.random.uniform(low=-1,high=1,size=(3,1000))
c = (x+1)+(y+1)+(z+1)
s = c*10
ax3 = a3d.Axes3D(pl.figure())
ax3.scatter(x,y,z,lw=0,s=s,c=c,alpha=0.5)

axesoff()

pl.show()

rawDescription: How to disable auto-swapping in axes labels in matplotlib?

description: Disable auto-swapping in axes labels in matplotlib


code:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

m = 256 #size of the matrix

X1, X2 = np.mgrid[:m, :m]

fig = plt.figure()
ax = fig.add_subplot(3,1,1, projection='3d')
jet = plt.get_cmap('jet')

#generation of the surface
F = 3
i = np.minimum(X1, m-X1-1)
j = np.minimum(X2, m-X2-1)
H = np.exp(-.5*(np.power(i, 2)  +  np.power(j, 2)   )/(F*F))
Y = np.real(  np.fft.ifft2   (H  *  np.fft.fft2(  np.random.randn(m, m))))
a = 0.0005; b = 0.0002; #parameters of the tilted plane
Y = Y + (a*X1 + b*X2); #adding the plane
Y = (Y - np.min(Y)) / (np.max(Y) - np.min(Y)) #data scaling

#plot the initial topological surface
ax.plot_surface(X1,X2,Y, rstride = 1, cstride = 1, cmap = jet, linewidth = 0)

#Regression
X = np.hstack(   ( np.reshape(X1, (m*m, 1)) , np.reshape(X2, (m*m, 1)) ) )
X = np.hstack(   ( np.ones((m*m, 1)) , X ))
YY = np.reshape(Y, (m*m, 1))

theta = np.dot(np.dot( np.linalg.pinv(np.dot(X.transpose(), X)), X.transpose()), YY)

plane = np.reshape(np.dot(X, theta), (m, m));

ax = fig.add_subplot(3,1,2, projection='3d')
ax.plot_surface(X1,X2,plane)
ax.plot_surface(X1,X2,Y, rstride = 1, cstride = 1, cmap = jet, linewidth = 0)

#Subtraction
Y_sub = Y - plane
ax = fig.add_subplot(3,1,3, projection='3d')
ax.plot_surface(X1,X2,Y_sub, rstride = 1, cstride = 1, cmap = jet, linewidth = 0)

plt.show()

rawDescription: Fitting a plane to a 2D array

description: Fitting a plane to a 2D array


code:
first_with_id_or_none = \
    next((value for value in dictionary['items'] if value['id'] == 1), None)

rawDescription: Reaching a dictionary inside a list of dictionaries by key


rawDescription: Python: changing value in a tuple

description: Python: changing value in a tuple


code:
def obj_create(self, bundle, **kwargs):
    # Get tech for this user
    site_user = SiteUser.objects.get(user=bundle.request.user)

    bundle.obj = self._meta.object_class()

    for key, value in kwargs.items():
        setattr(bundle.obj, key, value)

    # Override the tech and operator fields
    setattr(bundle.obj, 'tech', site_user)
    setattr(bundle.obj, 'operator', site_user.operator)

    self.authorized_create_detail(self.get_object_list(bundle.request), bundle)
    bundle = self.full_hydrate(bundle)
    return self.save(bundle);

rawDescription: Overriding model fields with Tastypie

description: Overriding model fields with Tastypie


code:
import os
import numpy as np

class Data(object):
    def __init__(self):
        super(Data, self).__init__()
        self.time = None
        self.pressure = None

    def load(self, filename):

        #Data Properties
        cols = 7
        lines_per_page = 59
        header_lines = 4

        #Initialize Data
        data = np.empty([0, cols])

        #Open the file
        with open(filename, 'r') as f:

            #Traverse the file's lines
            for i, line in enumerate(f.readlines()):

                #Check whether this is a header line
                if i % lines_per_page >= header_lines:

                    #Split line into columns, convert to float, convert to numpy array, shape as a row
                    linedata = np.array(map(float, line.split())).reshape(1, cols)

                    #Add the row to the bottom of the data
                    data = np.vstack([data, linedata])

        self.time = data[:, 0]
        self.pressure = data[:, 1]

rawDescription: Best way to import delimited and paginated data with Python

description: Import delimited and paginated data with Python


code: os.system("open -a iTunes")

rawDescription: python open applications through your program

description: Python open applications through your program


code:
htmlFile = open('web.html','w')
htmlFile.write("\n")
htmlFile.write("\n")
htmlFile.write("\n")
# add the above line in
htmlFile.write("<title> Web page </title>\n")
htmlFile.write("\n")
htmlFile.write("\n")
htmlFile.write('  abcd > <\a>
\n')
htmlFile.write("\n")
htmlFile.write("\n")
htmlFile.close()

rawDescription: How to disable link color change in html created in python

description: Disable link color change in html created in python


code:
import autocomplete_light.shortcuts as al
from models import Node,Line

# This will generate a LineAutocomplete class
al.register(Line,
    # Just like in ModelAdmin.search_fields
    search_fields=['node'],
    attrs={
        # This will set the input placeholder attribute:
        'placeholder': '',
        # This will set the yourlabs.Autocomplete.minimumCharacters
        # options, the naming conversion is handled by jQuery
        'data-autocomplete-minimum-characters': 1,
    },
    # This will set the data-widget-maximum-values attribute on the
    # widget container element, and will be set to
    # yourlabs.Widget.maximumValues (jQuery handles the naming
    # conversion).
    widget_attrs={
        'data-widget-maximum-values': 4,
        # Enable modern-style widget !
        'class':    'modern-style',
    },
)

al.register(Node,
    # Just like in ModelAdmin.search_fields
    search_fields=['node_name'],
    attrs={
        # This will set the input placeholder attribute:
        'placeholder': '',
        # This will set the yourlabs.Autocomplete.minimumCharacters
        # options, the naming conversion is handled by jQuery
        'data-autocomplete-minimum-characters': 1,
    },
    # This will set the data-widget-maximum-values attribute on the
    # widget container element, and will be set to
    # yourlabs.Widget.maximumValues (jQuery handles the naming
    # conversion).
    widget_attrs={
        'data-widget-maximum-values': 4,
        # Enable modern-style widget !
        'class': 'modern-style',
    },
)

rawDescription: How to use the django-autocomplete-light to return the foreign-key with a valid value in the form?

description: Use the django-autocomplete-light to return the foreign-key with a valid value in the form


code: d = {(i,j):f(i,j) for i in xrange(A) for j in xrange(B)}

rawDescription: Replace two nested loops with a dictionary comprehension


code:
def tag_list(request, slug=None):
  instance = get_object_or_404(Tag, slug=slug)
  ins = instance.post_set.all()
  queryset_list = Post.objects.active()
  query = request.GET.get("q")
  if query:
      queryset_list = queryset_list.filter(
              Q(title__icontains=query) |
              Q(content__icontains=query) |
              Q(user__first_name__icontains=query) |
              Q(user__last_name__icontains=query)
      ).distinct()
  paginator = Paginator(queryset_list, 1)
  page_request_var = "tags"
  page = request.GET.get(page_request_var)
  try:
      queryset = paginator.page(page)
  except PageNotAnInteger:
      queryset = paginator.page(1)
  except EmptyPage:
      queryset = paginator.page(paginator.num_pages)
  context = {
      "queryset": queryset,
      "paginator": paginator,
      "page_request_var": page_request_var,
  }
  return render(request, "posts/tag_list.html", context)

rawDescription: My Django apps search function wont work from the tags page


code:
from suds.client import Client
client = Client(url_to_wsdl_file)
auth = client.service.login(username, password)

client.service.addBase64EncodedAttachmentsToIssue(auth, issue_key, [filename.encode("utf-8")], [open(full_path_and_filename, "rb").read().encode('base64')])

rawDescription: Python+suds : xsd_base64Binary type?

description: Python+suds : xsd_base64Binary type


code:
import time
timeout = time.time() + 60*5   # 5 minutes from now
while True:
    test = 0
    if test == 5 or time.time() > timeout:
        break
    test = test - 1

rawDescription: How would I stop a while loop after n amount of time?


code: indexVector = [empAvg[i] + upperBound(t, pullCount[i]) for i in range(10)]

rawDescription: How do I make this snippet Pythonic?

description: Make this snippet Pythonic


rawDescription: numpy split doesn't work on float array

description: Numpy split doesn't work on float array


code: new_list = map(str.capitalize, old_list)

rawDescription: What is the best way to capitalize every element in an array of strings in Python?


code:
import threading, doctest, cProfile, time, random
result = [2]
counter = 1

def get_int(num):
    for i in range(3, num):
        yield i

def is_prime(num):
    for j in range(2,int(num)):
        if (num % j) == 0:
            return False
    result.append(num)
    return True

def prime_calculator(nth):
    lock = threading.Lock()
    global result, counter, integer
    while counter < (nth):
        if is_prime(next(integer)):
            lock.acquire()
            try:
                counter += 1
            finally:
                lock.release()
        time.sleep(random.random()/1000)

def prime(nth):
    """Returns the nth prime number
    >>> prime(1)
    2
    >>> prime(2)
    3
    >>> prime(4)
    7
    >>> prime(1000)
    7919
    """
    global integer, counter, result
    integer = get_int(99999999)
    threads = [threading.Thread(daemon=True, target=prime_calculator, args=(nth,)) for i in range(2)]
    [thread.start() for thread in threads]
    [thread.join() for thread in threads]
    counter = 1
    return result[-1]

if __name__ == "__main__":
    doctest.testmod()
    cProfile.run('print(prime(1000))')

rawDescription: Use 2 Threads to calculate the nth prime number in Python


code:
with open("powersearch.txt") as fileIn:
    for line in fileIn:
        n1, n2 = (int(s) for s in line.split())
        print(n1, n2)

rawDescription: What are Python implementation of nextInt(), hasNext() from Java?


code:
dic = {'a':'111', 'b':'222', 'c':'333'}

def create_connect(x):
    return lambda: doit(x)

for key in dic:
    btn = QPushButton(key, self)
    btn.clicked.connect(create_connect(dic[key]))
    vbox.addWidget(btn)

rawDescription: PyQt - creating buttons from dictionary

description: PyQt - creating buttons from dictionary


code:
def human_format(num):
    magnitude = 0
    while abs(num) >= 1000:
        magnitude += 1
        num /= 1000.0
    # add more suffixes if you need them
    return '%.2f%s' % (num, ['', 'K', 'M', 'G', 'T', 'P'][magnitude])

print('the answer is %s' % human_format(7436313))  # prints 'the answer is 7.44M'

rawDescription: formatting long numbers as strings in python

description: Formatting long numbers as strings in python


code: [key.split(',') for key in muncounty.keys()]

rawDescription: Python split dictionary key at comma

description: Python split dictionary key at comma


code:
s = """<number="4" word="start" sentence="I said, "start!"" />"""

re.sub(r'(?<=sentence=")(.*)(?=" />)', lambda m: m.group().replace('"',"'"), s)
Out[179]: '<number="4" word="start" sentence="I said, \'start!\'" />'

rawDescription: Search and replace some text in strings in Python?


rawDescription: python regex for extracting number in string

description: Python regex for extracting number in string


code:
game = str(input('if you want to start a game press y, if you want to back    out press n\n'))
if game == 'y':
    # Since you are after one value i.e. cherry why not use choice function
    result1 = random.choice(option1)
    result2 = random.choice(option2)
    result3 = random.choice(option3)
    print([result1, result2, result3])  # You can print them all together

    # Or use sample to get all 3 results at once
    options = ['cherry', 'lemon', 'orange', 'plum', 'bell', 'bar', '7']
    results = random.sample(options, 3)
    print(results)
    print(results[0])  # will print result1

rawDescription: Issue with my program (Python 3.5)

description: Issue with my program (Python 3.5)


code:
class TrafficListView(ListView):
    model = Traffic
    template_name = 'dashboard/pages/traffic.html'

    def get_context_data(self, **kwargs):
        context = super(TrafficListView, self).get_context_data(**kwargs)
        days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday'] # days of the week
        total_sales = {} # create an empty dictionary
        for day in days:
            # get the sales for that particular day
            total_sales[day] = Traffic.objects.filter(day_of_week=day).aggregate(Sum('sales')).get('sales__sum', 0.00)
        context['total_sales'] = total_sales # pass 'total_sales' dictionary in context
        return context

rawDescription: Writing Conditional Django Views

description: Writing Conditional Django Views


code:
import curses
from contextlib import contextmanager

@contextmanager
def open_screen():
    scr = curses.initscr()
    curses.noecho()
    curses.cbreak()
    scr.keypad(1)
    try:
        yield scr
    finally:
        curses.nocbreak()
        scr.keypad(0)
        curses.echo()
        curses.endwin()

curses_mouse_states = {
    curses.BUTTON1_PRESSED: 'Button 1 Pressed',
    curses.BUTTON1_RELEASED: 'Button 1 Released',
    curses.BUTTON1_CLICKED: 'Button 1 Clicked',
    curses.BUTTON1_DOUBLE_CLICKED: 'Button 1 Double-Clicked',
    curses.BUTTON1_TRIPLE_CLICKED: 'Button 1 Triple-Clicked',

    curses.BUTTON2_PRESSED: 'Button 2 Pressed',
    curses.BUTTON2_RELEASED: 'Button 2 Released',
    curses.BUTTON2_CLICKED: 'Button 2 Clicked',
    curses.BUTTON2_DOUBLE_CLICKED: 'Button 2 Double-Clicked',
    curses.BUTTON2_TRIPLE_CLICKED: 'Button 2 Triple-Clicked',

    curses.BUTTON3_PRESSED: 'Button 3 Pressed',
    curses.BUTTON3_RELEASED: 'Button 3 Released',
    curses.BUTTON3_CLICKED: 'Button 3 Clicked',
    curses.BUTTON3_DOUBLE_CLICKED: 'Button 3 Double-Clicked',
    curses.BUTTON3_TRIPLE_CLICKED: 'Button 3 Triple-Clicked',

    curses.BUTTON4_PRESSED: 'Button 4 Pressed',
    curses.BUTTON4_RELEASED: 'Button 4 Released',
    curses.BUTTON4_CLICKED: 'Button 4 Clicked',
    curses.BUTTON4_DOUBLE_CLICKED: 'Button 4 Double-Clicked',
    curses.BUTTON4_TRIPLE_CLICKED: 'Button 4 Triple-Clicked',

    curses.BUTTON_SHIFT: 'Button Shift',
    curses.BUTTON_CTRL: 'Button Ctrl',
    curses.BUTTON_ALT: 'Button Alt'
}

with open_screen() as scr:
    curses.mousemask(-1)
    while True:
        c = scr.getch()
        if c == curses.KEY_MOUSE:
            mouse_state = curses.getmouse()[4]
            states = '; '.join(state_string for state, state_string
                               in curses_mouse_states.viewitems()
                               if mouse_state & state)
            scr.addstr(0, 0, states)
            scr.clrtoeol()
            scr.refresh()
        elif c == ord('q'):
            break

rawDescription: Mouse wheel in python curses

description: Mouse wheel in python curses


code:
#########################Chapters#######################

import requests
from bs4 import BeautifulSoup, SoupStrainer
import re

def leveltwo(item_url):
    r = requests.get(item_url)
    soup = BeautifulSoup((r.content),"html.parser")
    section = soup.find('div', {'class': 'primary-content' })
    for sublinks in section.find_all('a'):
        sectionlinks = sublinks.get('href')
        print (sectionlinks)

base_url = "http://law.justia.com/codes/alabama/2015/title-{title:01d}/"

for title in range (1,4):
    url = base_url.format(title=title)
    r = requests.get(url)

for link in BeautifulSoup((r.content),"html.parser",parse_only=SoupStrainer('a')):
    try:
        if 'chapt' in link['href']:
            href = "http://law.justia.com" + link['href']
            leveltwo(href)
        else:
            continue
    except KeyError:
        continue
#########################Sections#######################

rawDescription: Python web crawler using BeautifulSoup, trouble getting URLs


rawDescription: dot products on sparse matrices

description: Dot products on sparse matrices


code:
conditional = [df.groupby(col_name) for col_name in df.columns]
mean        = [cond.mean() for cond in conditional]
var         = [cond.var() for cond in conditional]

rawDescription: Python method for calculating conditional means and variances?


code: img = misc.imread('mypic.jpg', flatten=True)

rawDescription: change a colored image represented as a numpy ndarray to greyscale

description: Change a colored image represented as a numpy ndarray to greyscale


code: result = sys.stdin.read()

rawDescription: Read a File from redirected stdin with python


code:
#!/usr/bin/env python3
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
root = Tk()
tx = Text(root, background='white', font='Courier 12')
tx.grid(column=0, row=0, sticky=(N, W, E, S))
ybar = ttk.Scrollbar(root, orient=VERTICAL, command=tx.yview)
ybar.grid(column=1, row=0, sticky=(N, W, E, S))
xbar = ttk.Scrollbar(root, orient=HORIZONTAL, command=tx.xview)
xbar.grid(column=0, row=1, columnspan=2, sticky=(N, W, E, S))
tx["yscrollcommand"] = ybar.set
tx["xscrollcommand"] = xbar.set
tx.tag_config('hide', elide=1)
tx.tag_config('bold', font='Courier 12 bold')
lastplace=tx.index('1.0')
def boldit():
    global tx, lastplace
    nextplace = tx.search('[B]', lastplace, 'end')
    if nextplace:
        boldon = nextplace + ' +3c'
        tx.tag_add('hide', nextplace, boldon)
        boldoff = tx.search('[/B]', boldon, 'end')
        if boldoff:
            tx.tag_add('bold', boldon, boldoff)
            codoff = boldoff + ' +4c'
            tx.tag_add('hide', boldoff, codoff)
        lastplace = codoff
        boldit()
    else:
        return
tx.insert('1.0', """When, in the course of [B]human events,[/B] it becomes [B]necessary[/B] for one people to [B]dissolve[/B] the political bands ...""")
boldit()
root.mainloop()

rawDescription: Tkinter Text Highlighting With Markers Like: [B] and [\B]


rawDescription: Stripping characters from a Python string


code: sigma = self.config.get("sigma")

rawDescription: How to access command-line arguments from different sub-modules

description: Access command-line arguments from different sub-modules


code:
admin.py
from .models import Question

class QuestionAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("naslov",)}

admin.site.register(Question, QuestionAdmin)

rawDescription: How to make a slug field autocomplete in a form?

description: Make a slug field autocomplete in a form


code:
from django.db import connection
# ...
cursor = connection.cursor()
out_arg1 = ""
args = [in_arg1, in_arg2, out_arg1]
result = cursor.callproc('some_procedure', args)

cursor.execute('SELECT @some_procedure_2')
print(cursor.fetchall())

#print(args[2], result[2])
cursor.close()
# ...

rawDescription: Can't retrieve OUT parameters from stored procedures (MySQL)


code:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib as mpl
import random

def fun(x, y):
  return 0.063*x**2 + 0.0628*x*y - 0.15015876*x + 96.1659*y**2 - 74.05284306*y  +      14.319143466051

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = y = np.arange(-1.0, 1.0, 0.05)
X, Y = np.meshgrid(x, y)
zs = np.array([fun(x,y) for x,y in zip(np.ravel(X), np.ravel(Y))])
Z = zs.reshape(X.shape)

ax.plot_surface(X, Y, Z)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
fake2Dline = mpl.lines.Line2D([0],[0], linestyle="none", c='b', marker = 'o')
ax.legend([fake2Dline], ['Lyapunov function on XY plane'], numpoints = 1)
plt.show()

rawDescription: Adding legend to a surface plot

description: Adding legend to a surface plot


code:
queryresults = serializers.serialize("python", queryset, fields=('jobmst_id', 'jobmst_prntid', 'jobmst_name', 'jobdtl_cmd'))
#                                                                                here--^

rawDescription: Django - Invalid Syntax with my view

description: Django - Invalid Syntax with my view


code: data.to_csv("out_file.csv", header=["column1 title", "column2 title",...])

rawDescription: Converting from fixed width columns to csv


code:
with open(output_file, 'w') as f:
    for word, count in word_list:
        f.write("{0}\t{1}\n".format(word, count))

rawDescription: Python collections.Counter How to print element and number of counts


code:
urllib.quote('this is my string')
urllib.quote_plus('this is my string')

rawDescription: URL-encode a string

description: URL-encode a string


code:
@app.route("/api/<projectTitle>/<path:urlSuffix>", methods=['GET'])
def projectTitlePage(projectTitle, urlSuffix):

    # ...

    return r.text, r.status_code, r.headers

rawDescription: FLASK: Serving file to browser behind API proxy


rawDescription: How to display full (non-truncated) dataframe information in html when converting from pandas dataframe to html?

description: Display full (non-truncated) dataframe information in html when converting from pandas dataframe to html


code:
text = "0x15000000"
text = text[2:]  # remove literal type prefix
text = text.zfill(len(text) + len(text) % 2)  # pad with zeros for even digits
' '.join(text[i: i+2] for i in range(0, len(text), 2))  # split into 2-digit chunks

rawDescription: Splitting a hexadecimal string into chunks in Python


code:
import struct, base64
base64.urlsafe_b64encode(struct.pack("ffff", 123.545,56.234,-4534.234,544.64))

rawDescription: How can I compress four floats into a string?

description: Compress four floats into a string


code:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from pandas.tools.plotting import scatter_matrix

df = pd.DataFrame(np.random.randn(1000, 4), columns=['long label', 'testing', 'another label', 'something else'])

sm = scatter_matrix(df, alpha=0.2, figsize=(6, 6), diagonal='kde')

#Change label rotation
[s.xaxis.label.set_rotation(45) for s in sm.reshape(-1)]
[s.yaxis.label.set_rotation(0) for s in sm.reshape(-1)]

#May need to offset label when rotating to prevent overlap of figure
[s.get_yaxis().set_label_coords(-0.3,0.5) for s in sm.reshape(-1)]

#Hide all ticks
[s.set_xticks(()) for s in sm.reshape(-1)]
[s.set_yticks(()) for s in sm.reshape(-1)]

plt.show()

rawDescription: How to customize a scatter matrix to see all titles?

description: Customize a scatter matrix to see all titles


code:
dt = datetime.now()
d = DateTime(*dt.timetuple()[:6])

# For UTC times, you need to pass 'kind' as a kwarg
# because of Python's rules around using * unpacking
udt = datetime.now()
ud = DateTime(*udt.timetuple()[:6], kind=DateTimeKind.Utc)

rawDescription: How do I convert from an IronPython datetime to a .NET DateTime?

description: Convert from an IronPython datetime to a .NET DateTime


code:
def PrintCommand():
    the_number = int(Entry1.get())
    print(the_number)

rawDescription: Integer Gui input for button command in Python 2.7


code:
import scipy.ndimage
import scipy.misc

data = scipy.misc.imread(...)
assert data.ndim == 2, "Image must be monochromatic"

# finds and number all disjoint white regions of the image
is_white = data > 128
labels, n = scipy.ndimage.measurements.label(is_white)

# get a set of all the region ids which are on the edge - we should not fill these
on_border = set(labels[:,0]) | set(labels[:,-1]) | set(labels[0,:]) | set(labels[-1,:])

for label in range(1, n+1):  # label 0 is all the black pixels
    if label not in on_border:
        # turn every pixel with that label to black
        data[labels == label] = 0

rawDescription: Coloring only the inside of a shape

description: Coloring only the inside of a shape


rawDescription: python dictionary and default values

description: Python dictionary and default values


code:
from kivy.app import App
from kivy.uix.slider import Slider
from kivy.lang import Builder
from kivy.graphics import Color

kv = """
#:kivy 1.9.1
BoxLayout:
    orientation: 'vertical'
    Widget:
        id: w_canvas
        my_color: (0, 1, 1)
        canvas:
            Color:
                rgb: self.my_color
            Rectangle:
                pos: self.pos
                size: (self.width/2, self.height/2)
            Color:
                group: 'b'
                rgb: (0, .8, 0)
            Ellipse:
                group: 'a'
                pos: (self.pos[0], self.pos[1] + self.height/2)
                size: (self.width/4, self.height/4)
            Ellipse:
                group: 'b'
                pos: (self.pos[0]+ self.width/2, self.pos[1] + self.height/2)
                size: (self.width/4, self.height/4)
    Button:
        text: 'Click me'
        on_release: app.handle_button()
"""
class Test(App):
    def build(self):
        return Builder.load_string(kv)
    def handle_button(self):
        # binding Canvas instruction property to Widget property
        self.root.ids.w_canvas.my_color = (.5, .2, 0)
        # Access single item of canvas instruction group
        an_ellipse = self.root.ids.w_canvas.canvas.get_group('a')[0]
        an_ellipse.pos = (an_ellipse.pos[0] + 10, an_ellipse.pos[1])
        # loop through all elements of canvas instruction group
        for gitem in self.root.ids.w_canvas.canvas.get_group('b'):
            if isinstance(gitem, Color):
                gitem.rgb = (0, .5, 1)
            try:
                gitem.size = (gitem.size[0] / 2.0, gitem.size[1])
            except:
                pass

Test().run()

rawDescription: How do I reference id's of children within a canvas in Kivy?

description: Reference id's of children within a canvas in Kivy


code:
import pandas as pd
import io
import matplotlib
import matplotlib.pyplot as plt

test=u"""Cell,Value
Bcells,0.056304
DendriticCells,0.3155388
Macrophages,0.131430
"""

tableau10=[ "#17BECF", "#BCBD22", "#7F7F7F", ]

toplot_df = pd.read_csv(io.StringIO(test))
toplot_df.set_index('Cell',inplace=True)
xlabels =  toplot_df.index.values
barplot   = toplot_df.plot(kind="bar", figsize=(17,17), \
                          color = tableau10, \
                          width=0.7,\
                          fontsize = 30,\
                          legend=False,
                          ylim = (0,0.5),
                          subplots=False,
                          zorder=2)                         ##### This is all I added

ax = plt.gca()
ax.set_xticklabels(xlabels, rotation=30, ha='right')

# How to to make this to the back
plt.axhline(y=0.1, linewidth=1, color='r',zorder=1)
plt.xlabel("")
plt.ylabel("Score", fontsize=30, fontweight="bold")

plt.show()

rawDescription: How to send axhline to back of Matplotlib's barplot

description: Send axhline to back of Matplotlib's barplot


code: from django.utils.translation import ugettext_lazy as _

rawDescription: How to restart the django server from views.py for localization changes in python code to take affect

description: Restart the django server from views.py for localization changes in python code to take affect


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

np.random.seed(1)
x, y = np.random.random((2,500))

fig, ax = plt.subplots()
ax.plot(x, y, 'bo')

# The key option here is `bbox`. I'm just going a bit crazy with it.
ax.annotate('Something', xy=(x[0], y[0]), xytext=(-20,20),
            textcoords='offset points', ha='center', va='bottom',
            bbox=dict(boxstyle='round,pad=0.2', fc='yellow', alpha=0.3),
            arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5',
                            color='red'))

plt.show()

rawDescription: matplotlib: how to annotate point on a scatter automatically placed arrow?

description: Matplotlib: how to annotate point on a scatter automatically placed arrow


code: baseobj.derived    # note: small 'd'

rawDescription: Type casting in python

description: Type casting in python


code:
import pandas as pd
import numpy as np
from cStringIO import StringIO

PI_KWARGS = dict(freq='M', periods=7)

tseries_data = '''2013-01-01      55.0
2013-02-01      57.0
2013-03-01      52.0
2013-04-01      56.0
2013-05-01      59.0
2013-06-01      61.0
2013-07-01      63.0
2013-08-01      66.0
2013-09-01      67.0
2013-10-01      67.0
2013-11-01      69.0
2013-12-01      70.0'''

bool_col = '''2013-01-01    False
 2013-02-01    True
 2013-03-01    False
 2013-04-01    False
 2013-05-01    False
 2013-06-01    False
 2013-07-01    True
 2013-08-01    True
 2013-09-01    False
 2013-10-01    False
 2013-11-01    True
 2013-12-01    False'''

df = pd.read_csv(StringIO(tseries_data), index_col=0, parse_dates=True, sep='\s+', header=None, names=['Date', 'Data'])
bools = pd.read_csv(StringIO(bool_col), index_col=0, parse_dates=True, sep='\s+', header=None, names=['Date', 'Data'])
dates = bools.where(bools).dropna().index

def make_back_datetime_index(time, nmonths=3, **pi_kwargs):

    month = time.month - nmonths % 12
    is_month = month > 0

    nyears = int(nmonths) / 12 + (not is_month)
    month = (is_month and month) or 12 + month
    start = pd.datetime(time.year - nyears, month, time.day)

    return pd.PeriodIndex(start=start, **pi_kwargs).to_datetime()

data_dict = {date : df.ix[make_back_datetime_index(date, **PI_KWARGS)] for date in dates}

rawDescription: Padding pandas columns with NaNs if offset is too large


code:
from os import path
import sys

PACKAGE_NAMESPACE = ["project_name", "ext"]
VIRTUAL_PACKAGE = '.'.join(PACKAGE_NAMESPACE)
__import__(VIRTUAL_PACKAGE)
local_package = path.abspath(path.join(*PACKAGE_NAMESPACE))
sys.modules[VIRTUAL_PACKAGE].__dict__["__path__"].insert(0, local_package)

rawDescription: Developing with Python Namespaced Packages


code:
class Address(Document):
    street_address = StringField()
    city = StringField()
    state_province = StringField()
    country = StringField()

class User(Document):

    name = StringField()
    email = StringField()

    address = DocumentField(Address)

user = User()
user.name = "tony"

user.address = Address()
user.address.city = "London"

rawDescription: MongoAlchemy embedded documents

description: MongoAlchemy embedded documents


code: emails = [r.email for u in db.session.query(my_table.c.email).filter_by(name=name).distinct()]

rawDescription: Get a list of values of one column from the results of a query


code:
lim1 = ax1.get_ylim()
lim2 = (lim1[0]*2, lim1[1] *2)
ax2.set_ylim(lim2)

rawDescription: Trouble with aligning two y-axis ticks with matplotlib


code:
# Initialize 3D array to random counts
import random
cbucket = {}
for y in range(0,16):
    for cb in range(0,16):
        for cr in range(0,16):
            cbucket[ y,cb,cr ] = float(round(random.uniform(0,10)))

# Find total count
tcount = 0
for y in range(0,16):
    for cb in range(0,16):
        for cr in range(0,16):
            tcount += cbucket[ y,cb,cr ]

print("tcount: %6.2f " % tcount)

# now normalize
for y in range(0,16):
    for cb in range(0,16):
        for cr in range(0,16):
            cbucket[ y,cb,cr ] = cbucket[ y,cb,cr ]/tcount

rawDescription: Creating a normalized histogram

description: Creating a normalized histogram


code:
for k in topology:
    for j in topology:
        if k == j:
            continue
        else:
            # compare values at key k and key j
            my_compare_function(topology[k], topology[j])

rawDescription: comparing each value in a dictionary to each other value

description: Comparing each value in a dictionary to each other value


code:
tgt='createNode transform -n "bar1_1" -p "bar11";'
re.search(r'bar1',tgt)
# matches first 'bar1'...
re.search(r'bar11',tgt)
# matches 'bar11'...
re.search(r'bar1\b',tgt)
# no match

rawDescription: strict match anywhere in line regex python

description: Strict match anywhere in line regex python


code:
def test():
    def makefunc(i):
        return lambda x: p(i, x)
    a = []
    for i in range(10):
        a.append(makefunc(i))
    return a

rawDescription: What's going on with the lambda expression in this python function?


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

KEu = 100.
KEm = 50.
KEl = 5.
X   = np.linspace(1,10,10)

fig = plt.figure()
ax = plt.subplot(111)
ax.plot(X,KEu*X,label=r"Maximum Energy = {0:6.2f} MeV".format(KEu))
ax.plot(X,KEm*X,label=r"Median  Energy = {0:6.2f} MeV".format(KEm))
ax.plot(X,KEl*X,label=r"Minimum Energy = {0:6.2f} MeV".format(KEl))
ax.legend(loc='upper left', prop={'family': 'monospace'})
plt.show()

rawDescription: Adding internal spaces in pyplot legend

description: Adding internal spaces in pyplot legend


rawDescription: Split the list into lines

description: Split the list into lines


code: result = array([subarray[index] for subarray, index in zip(m, select)])

rawDescription: Indexing NumPy 2D array with another 2D array


code: portalocker.lock(file, flags)

rawDescription: What is the best way to open a file for exclusive access in Python?


code:
subprocess.call(["esxcli",
                 "--server="+settings.vserver,
                 "--username="+settings.user,
                 "--password="+settings.pwd,
                 "--vihost="+settings.host,
                 "network","vswitch","standard","portgroup","add",
                 "-p",settings.newpgname,
                 "-v",settings.newpgswitch])

rawDescription: Subprocess fails to print esxcli namespace correctly


code:
from PIL import Image
import Image

def crop(Path,input,height,width,i,k,page,area):
    im = Image.open(input)
    imgwidth, imgheight = im.size
    for i in range(0,imgheight,height):
        for j in range(0,imgwidth,width):
            box = (j, i, j+width, i+height)
            a = im.crop(box)
            try:
                o = a.crop(area)
                o.save(os.path.join(Path,"PNG","%s" % page,"IMG-%s.png" % k))
            except:
                pass
            k +=1

rawDescription: How to Split Image Into Multiple Pieces in Python


code: list3 = map(lambda x: str(row[x]).strip(), list2);

rawDescription: python get a list of expressions

description: Python get a list of expressions


code:
import os
import time

dirpath = '/path/to/dir'
past = time.time() - 2*60*60 # 2 hours
result = []
for p, ds, fs in os.walk(dirpath):
    for fn in fs:
        filepath = os.path.join(p, fn)
        if os.path.getmtime(filepath) >= past:
            result.append(filepath)

rawDescription: Listing files timestamped within last two hours in Python


code:
from ggplot import *
import pandas as pd
df = pd.DataFrame({"x":[1,2,3,4], "y":[1,3,4,2]})
ggplot(aes(x="x", weight="y"), df) + geom_bar()

rawDescription: How do I create a bar chart in python ggplot?

description: Create a bar chart in python ggplot


rawDescription: Most efficient way in Python to iterate over a large file (10GB+)


code:
with open('file.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        time.append(row['Time'])
        energy.append(row['Energy'])
        ondeIG.append(row['OndeIG'])
        envelope.append(row['envelope'])

rawDescription: How to make this process work faster?

description: Make this process work faster


code:
import mimetypes

import gio
import gtk

def get_icon_path(extension, size=32):
    type_, encoding = mimetypes.guess_type('x.' + extension)
    if type_:
        icon = gio.content_type_get_icon(type_)
        theme = gtk.icon_theme_get_default()
        info = theme.choose_icon(icon.get_names(), size, 0)
        if info:
            return info.get_filename()

rawDescription: Get the default icon for specific file type in Ubuntu OS by python


code:
ee = deepcopy(e)
ee.id=None
ee.save()
ee.connexion.add(*e.connexion.all())

rawDescription: Duplicate django objects with ManyToManyFields


code:
import BaseHTTPServer, SimpleHTTPServer
import ssl

handler = BaseHTTPServer.HTTPServer(("", LISTEN_PORT), simpleHandler)
handler.socket = ssl.wrap_socket (handler.socket, certfile='path/to/combined/PKCS12/container', server_side=True)
handler.serve_forever()

rawDescription: Python simple HTTPS forwarder

description: Python simple HTTPS forwarder


code:
from collections import Counter
bigList = [['A','B','A','A'],['C','C','B','B'],['A','C','B','B']]
for index,subList in enumerate(bigList):
   print(index)
   print(Counter(subList))
   print('---')

rawDescription: Number of elements in each nested list

description: Number of elements in each nested list


code:
import pylab as plt

X = [1,2,3,4]
Y = [1,4,3,2]

fig, axes = plt.subplots(2, 1)
ax1, ax2 = axes

ax1.bar(X,Y,alpha=.5)
ax1.set_xticks(X)

ax2.bar(X,Y,align="center",alpha=.5)
ax2.set_xticks(X)

plt.show()

rawDescription: matplotlib xtick labels not aligned

description: Matplotlib xtick labels not aligned


code:
print("That is " + formatCurrency(rare) + " for rare choice for " + str(noAdult) +
     (" adult" if noAdult==1 else " adults") + " and " + str(noChild) +
     (" child" if noChild==1 else " children") + ". Enjoy!")

rawDescription: Plurals, if statement python

description: Plurals, if statement python


code:
import audiolab
import scipy
x = scipy.cos((2*scipy.pi*f/fs)*scipy.arange(fs*T))
audiolab.play(x, fs)

rawDescription: beep sound in python audiolab

description: Beep sound in python audiolab


code:
f = open('filename.txt', 'r')
li = [line.split() for line in f]

rawDescription: python creating a list from lines in a file

description: Python creating a list from lines in a file


code:
pipe = [
    {'$match': {'$timeout': False}},
    {'$group': {'_id': '$created_time',
                'comments': {'$push': '$comments'}}},
]
db.comments.aggregate(pipeline=pipe)

rawDescription: Python - pymongo list inside dict

description: Python - pymongo list inside dict


code:
import resource
info = resource.getrusage(resource.RUSAGE_CHILDREN)

rawDescription: get how much time python subprocess spends

description: Get how much time python subprocess spends


code:
import wx.lib.agw.buttonpanel as BP
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx

#---------------------------- plot panel -----------------------------
class PanelWithCanvas(wx.Panel):
    """
    This panel contains plot figure and other things...
    """
    def __init__(self, parent, frame):
        wx.Panel.__init__(self, parent)

        self.frame = frame

        ...

        # create figure
        self.figure = plt.Figure()
        self.axes  = self.figure.add_subplot(111)
        self.axes.grid()
        # attach a canvas to the figure
        self.canvas = FigureCanvas(self, wx.ID_ANY, self.figure)

        # create the default matplotlib toolbar and attach it to the canvas
        self.toolbar = NavigationToolbar2Wx(self.canvas)
        # realize and hide it, since it will be replaced by a better one
        self.toolbar.Realize()
        self.toolbar.Hide()

        # create a new plot toolbar to replace the default one
        self.ptb = plotToolbar(self)

        ...

#---------------------------- plot toolbar -----------------------------
class plotToolbar(BP.ButtonPanel):
    """
    Create small toolbar which is added to the main panel
    par:  parent
    """
    def __init__(self, par):
        BP.ButtonPanel.__init__(self, par)

        # PanelWithCanvas
        self.pwc = par

        self.ID_FIT = wx.NewId()
        self.ID_ZOOM = wx.NewId()
        self.ID_PAN = wx.NewId()
        self.ID_SAVE = wx.NewId()

        self.AddSpacer()

        btn1 = BP.ButtonInfo(self, self.ID_FIT, bitmaps['zoomfit'].GetBitmap(),
                         shortHelp='Fit plot extents')
        self.AddButton(btn1)
        self.Bind(wx.EVT_BUTTON, self.ZoomFit, btn1)

        self.btnzoom = BP.ButtonInfo(self, self.ID_ZOOM, bitmaps['zoom'].GetBitmap(),
                         kind=wx.ITEM_CHECK, shortHelp='Zoom in/out')
        self.AddButton(self.btnzoom)
        self.Bind(wx.EVT_BUTTON, self.Zoom, self.btnzoom)

        self.btnpan = BP.ButtonInfo(self, self.ID_PAN, bitmaps['pan'].GetBitmap(),
                         kind=wx.ITEM_CHECK, shortHelp='Move plot')
        self.AddButton(self.btnpan)
        self.Bind(wx.EVT_BUTTON, self.Pan, self.btnpan)

        btnsav = BP.ButtonInfo(self, self.ID_SAVE, bitmaps['saveimage'].GetBitmap(),
                         shortHelp='Save image')
        self.AddButton(btnsav)
        self.Bind(wx.EVT_BUTTON, self.SaveImage, btnsav)

        self.AddSpacer()

        self.DoLayout()

    def ZoomFit(self, event):
        self.pwc.toolbar.home()
        event.Skip()

    def Zoom(self, event):
        self.pwc.toolbar.zoom()
        event.Skip()

    def Pan(self, event):
        self.pwc.toolbar.pan()
        event.Skip()

    def SaveImage(self, event):
        self.pwc.toolbar.save_figure(None)
        event.Skip()

rawDescription: Embed NavigationToolbar in separate frame than FigureCanvas with wxPython


code:
dbNavigator = frmDua['TDBNavigator']
dbNavigator.SetFocus()
SendKeys.SendKeys("""{LEFT 10}""") #Move Cursor To Most LeftButton
SendKeys.SendKeys("""{RIGHT 4}""") #Move Cursor To InsertButton
SendKeys.SendKeys("""{SPACE}""")   #Perform Click On Insert Button

rawDescription: How to Acces Button In TDBNavigator (exe build with Delphi) using pyWinAuto


code:
import itertools
list(itertools.chain(*[(0, 1), (1, 2), (2, 3)]))

rawDescription: Python: Obtain edge end points of the graph


code:
import os
import glob
import re

inputPath = '.' # the directory where your files are stored
num = 5         # the fixed parameter, n

# first obtain all the data*.txt files in the directory
for inputName in glob.glob(os.path.join(inputPath,'data*.txt')):

  # attempt to extract the file number from the input name
  fileNum = re.findall(r'data([0-9]+)\.txt',inputName)
  # if not successful, skip this file
  if not fileNum: continue

  # create the output filename using the fle number and the fixed parameter
  outputName = 'output_data{0}_{1}.txt'.format(fileNum[0],num)
  # add the input path to the filename, or use a different path if necessary
  outputName = os.path.join(inputPath,outputName)

  # process the file
  x,y = genfromtxt(inputName).T
  with open(outputName,'wb') as file:
    # do the rest of your code here
    pass

rawDescription: How to read JUST filenames of a directory in python and then do the same job for all?

description: Read JUST filenames of a directory in python and then do the same job for all


rawDescription: Python regex split function multiple delimiter


code:
pipeline = mapreduce.mapreduce_pipeline.MapreducePipeline( ... )
pipeline.with_params(target="version-dot-module")
pipeline.start()

rawDescription: How to run appengine mapreduce jobs in a module?

description: Run appengine mapreduce jobs in a module


code:
import random
from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3D

mypoints = []
for _ in range(100):
    mypoints.append([random.random(),    #x
                    random.random(),     #y
                    random.random(),     #z
                    random.randint(10,100)]) #scalar

data = zip(*mypoints)

fig = pyplot.figure()
ax = fig.add_subplot(111, projection='3d')

ax.scatter(data[0], data[1], data[2], c=data[3])
pyplot.show()

rawDescription: How to plot a data cube in python

description: Plot a data cube in python


code:
mylist = ['ab=2','bc=5','ab=1','cd=6','ab=7']
[i for i in mylist if 'ab' in i]

rawDescription: whats the best way to find multiple appearance of string within a list in python?

description: Whats the best way to find multiple appearance of string within a list in python


code:
import tensorflow as tf
import numpy as np

# coord read from binary file
x = tf.constant([1])
y = tf.constant([1])

# shape is taken from another image read from binary file
shape = tf.constant([3, 3])

# Little modification to get the right shape for the indices
sparse_indices = tf.transpose(tf.pack([x, y]))
mask = tf.sparse_to_dense(sparse_indices, shape, 1.)

rawDescription: TF: Create mask from binary data read (item assignment)


code:
attr = getattr(instance, key)
if isinstance(attr, Decimal):
    value = Decimal(str(value))
if attr != value:
    ...

rawDescription: Test django model attribute values - e.g. Decimal


rawDescription: Redirection url using urllib in Python 3


code:
def f():
    a, b = self.mytuple

import dis
dis.dis(f)

rawDescription: Is Python unpacking thread safe?

description: Is Python unpacking thread safe


code:
for i in range (m,n+1):
    sum += i
    print(i)

print(sum)

rawDescription: sum of all integer between m and n in a loop Python 3

description: Sum of all integer between m and n in a loop Python 3


rawDescription: PyGtk3 ~ Allowing a signal from the user, but not the programmatically


code:
cursor.execute("""insert into track_table
                    (tid,pid,usid,address,status,phno,email,ord_date,del_date)
                  values (%s,%s,%s,%s,%s,%s,%s,%s,%s)""",
               (tid,pid,usid,address,status,phno,email,ord_date,del_date))
cursor.execute("""insert into user_table
                    (tid,usid)
                  values (%s,%s)""",(tid,usid))

rawDescription: unable to insert data that are stored in variables in to an MYSQL database table using python

description: Unable to insert data that are stored in variables in to an MYSQL database table using python


code:
import code
def start_shell():
    import sys, os
    code.interact(local=locals())
start_shell()
dir()

rawDescription: python: load thrift classes and run interactive shell

description: Python: load thrift classes and run interactive shell


code: np.savetxt('output.txt', array, header=str(dimension), comments='')

rawDescription: Python savetxt header

description: Python savetxt header


code:
with open('a') as A, open('b') as B:
    while True:
        try:
            lineA1, lineA2, lineB1, lineB2 = next(A), next(A), next(B), next(B)
            # compare lines
            # ...
        except StopIteration:
            break

rawDescription: Reading two lines at a time from two different files in Python


code:
subprocess.check_call('npm --help', shell=True)

subprocess.check_call('yuidoc --help', shell=True)

rawDescription: Run npm commands using Python subprocess


rawDescription: Python - Threading with PyQt

description: Python - Threading with PyQt


code:
#!/usr/bin/env python3

def make_map(list_child_parent):
    has_parent = set()
    all_items = {}
    for child, parent in list_child_parent:
        if parent not in all_items:
            all_items[parent] = {}
        if child not in all_items:
            all_items[child] = {}
        all_items[parent][child] = all_items[child]
        has_parent.add(child)

    result = {}
    for key, value in all_items.items():
        if key not in has_parent:
            result[key] = value
    return result

if __name__ == '__main__':
    list_child_parent = [
        #first value is child, second is parent
        (0, 1),
        (1, 3),
        (8, 7),
        (3, 6),
        (4, 3),
        (5, 3)
    ]

    actual = make_map(list_child_parent)

    expected = {
        6: {
            3: {
                1: {
                    0: {}
                },
                4: {},
                5: {}
            }
        },
        7: {
            8: {}
        }
    }
    print('OK' if expected == actual else 'FAIL')

rawDescription: Python: create a nested dictionary from a list of parent child values


code:
class ChangeAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        return Change.objects.exclude(status='closed')

class ClosedChangeAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        return ClosedChange.objects.filter(status='closed')

admin.site.register(Change, ChangeAdmin)
admin.site.register(ClosedChange, ClosedChangeAdmin)

rawDescription: Using proxy model in django admin to customize change list view


code:
def random_path(origin, nsteps, choices, choice_probs, choice_map):
    directions = np.random.choice(choices, size=(15,), p=choice_probs)
    return directions

dir_map = np.array([[0,1], [0,-1], [1,0], [-1,0]])
# Everything else is the same as defined by OP

path_directions = random_path(origin, 15, np.arange(4), dir_probabilities, dir_map)
path = dir_map[path_directions]

rawDescription: Can you broadcast dictionary definitions over numpy arrays?

description: Broadcast dictionary definitions over numpy arrays


code:
#this is the one that returns the first dictionary
def dictionarize1(array):
    dict1 = {}
    count = 0
    for x in range(len(array[0]) - 1) :
        for y in range(len(array[0]) - 1):
            dict1[count] = [array[x][y], array[x][y+1], array[x+1][y], array[x + 1][y+1]]
            count = count + 1
    return dict1

def dictionarize2(array):
    dict2 = {}
    counter = 0
    for a in range(len(array[0]) - 1) :
        for b in range(len(array[0]) - 1):
            dict2[counter] = (array[a][b] + array[a][b+1] + array[a+1][b] + array[a + 1][b+1])/4
            counter = counter + 1
    return dict2

#here's a little trial code to see them working

eighties = [[2.0, 2.2, 2.6, 5.7, 4.7], [2.1, 2.3, 2.3, 5.8, 1.6], [2.0, 2.2, 2.6, 5.7, 4.7],[2.0, 2.2, 2.6, 5.7, 4.7],[2.0, 2.2, 2.6, 5.7, 4.7]]
print("Dictionarize1: \n")
print(dictionarize1(eighties))
print("\n\n")
print("Dictionarize2: \n")
print(dictionarize2(eighties))
print("\n\n")

rawDescription: How to sample a huge 2D array in Python using 2x2 arrays to create a dictionary? (Stencil Algorithm for Python)

description: Sample a huge 2D array in Python using 2x2 arrays to create a dictionary? (Stencil Algorithm for Python)


code:
d = """
Some text here(div,span,img tags)
<!-- 3GP||**Some link**|| -->
Some text here(div,span,img tags)
"""
import re
re.findall(r'\<!-- 3GP\|\|([^|]+)\|\| --\>',d)

rawDescription: Extract text between pattern using REGEX


code:
with open("f1.txt") as file1:
    file1Lines = file1.readlines()
with open("f2.txt") as file2:
    countriesToKeep = file2.readlines()
file1Lines = [i.strip() for i in file1Lines]
countriesToKeep = [i.strip() for i in countriesToKeep]
newFileLines = []
for line in file1Lines:
    if line.startswith(">"):
        newFileLines.append(line)
    elif line in countriesToKeep:
        newFileLines.append(" "+line)

with open("output.txt", "w") as output:
    for line in newFileLines:
        output.write(line+"\n")

rawDescription: remove uncommon lines in two files, but keeping file structure

description: Remove uncommon lines in two files, but keeping file structure


code: ";".join([','.join(x) for x in a])

rawDescription: python nested list comprehension string concatenation

description: Python nested list comprehension string concatenation


code:
import matplotlib.pyplot as plt

#Create an 11x5 figure
fig = plt.figure(figsize=(11,5))

#Create subplots[0]
subplts = []
subplt = fig.add_subplot(121)
subplts.append(subplt)

#Create subplots[1:20]
for xind in range(4,8):
    for yind in range(0,5):
        subplt = fig.add_subplot(5,8,(yind*8+xind))
        subplts.append(subplt)

plt.show()

rawDescription: python Matplotlib -- Multiple Chart Objects?

description: Python Matplotlib -- Multiple Chart Objects


code:
import csv
import glob
import os
import itertools

def get_data(fname):
    """
    Load a .csv file
    Returns a dict of {'exchange':float(price)}
    """
    with open(fname, 'rb') as inf:
        items = (row.split() for row in csv.reader(inf))
        return {item[0]:float(item[1]) for item in items}

def do_compare(a_name, a_data, b_name, b_data):
    """
    Compare two data files of {'key': float(value)}

    Returns a list of
      - the name of the first file
      - the name of the second file
      - the number of keys in A which are not in B
      - the number of keys in B which are not in A
      - the number of values in A less than the corresponding value in B
      - the number of values in A equal to the corresponding value in B
      - the number of values in A greater than the corresponding value in B
    """
    a_keys = set(a_data.iterkeys())
    b_keys = set(b_data.iterkeys())

    unique_to_a = len(a_keys - b_keys)
    unique_to_b = len(b_keys - a_keys)

    lt,eq,gt = 0,0,0
    pairs = ((a_data[key], b_data[key]) for key in a_keys & b_keys)
    for ai,bi in pairs:
        if ai < bi:
            lt +=1
        elif ai == bi:
            eq += 1
        else:
            gt += 1

    return [a_name, b_name, unique_to_a, unique_to_b, lt, eq, gt]

def main():
    os.chdir('d:/tariff_compare')

    # load data from csv files
    data = {}
    for fname in glob.glob("*.csv"):
        data[fname] = get_data(fname)

    # do comparison
    files = data.keys()
    files.sort()
    with open('summary.csv', 'wb') as outf:
        outcsv = csv.writer(outf)
        outcsv.writerow(["File A", "File B", "Unique to A", "Unique to B", "A<B", "A==B", "A>B"])
        for a,b in itertools.combinations(files, 2):
            outcsv.writerow(do_compare(a, data[a], b, data[b]))

if __name__=="__main__":
    main()

rawDescription: Compare multiple CSV files with Python

description: Compare multiple CSV files with Python


rawDescription: Python/Django Check if object is in 2 lists


code:
# 1. Backing index
table = {}

# 2. Each State is empty.
table[(0, 0)] = State(0, 0)
table[(0, 1)] = State(0, 1)
table[(1, 0)] = State(1, 0)
table[(1, 1)] = State(1, 1)
# ...

# 3. Initialize each State.
table[(0, 0)].initialize()
table[(0, 1)].initialize()
# ...

class State:
  def initialize(self):
    if ...:
      # self.neighbours is a list of States.
      self.neighbours = [table[(x, y)], table[(a, b)], table[(p, q)]]

rawDescription: User Defined Types as Dictionary Keys

description: User Defined Types as Dictionary Keys


code: signed_angle = atan2(b.y,b.x) - atan2(a.y,a.x)

rawDescription: Finding Signed Angle Between Vectors

description: Finding Signed Angle Between Vectors


code:
from ZSI import ServiceProxy
import BabelTypes
import sys

dbgfile = open('dbgfile', 'w')   # to log trace to a file, or
dbgfile = sys.stdout             # to log trace to stdout
service = ServiceProxy('http://www.xmethods.net/sd/BabelFishService.wsdl',
                       tracefile=dbgfile,
                       typesmodule=BabelTypes)
value = service.BabelFish('en_de', 'This is a test!')

dbgfile.close()

rawDescription: How to display outcoming and incoming SOAP message for ZSI.ServiceProxy in Python?

description: Display outcoming and incoming SOAP message for ZSI.ServiceProxy in Python


code:
def post(self):
  img_data = self.request.get('extract1')
  # then strip off the prefix and convert from base64

rawDescription: cgi get submitted image data-url

description: Cgi get submitted image data-url


code:
import os
cwd = os.getcwd()

rawDescription: How do I find directory of the Python running script from inside the script?

description: Find directory of the Python running script from inside the script


code:
if selectedc >= 0 and selectedc < len(questions):
    print(questions[selectedc]['question'])
    ...
else:
    print('Invalid index number')

rawDescription: Display an “Invalid Index Number” message if the user enters an index number that doesn’t exist in the questions list


code:
from django import forms
class MyForm(forms.Form):
    some_field=forms.CharField()
    other_field=forms.CharField()

    def __init__(self, my_criteria, *args,**kwrds):
        super(MyForm,self).__init__(*args,**kwrds)
        if my_criteria == 'this':
            self.fields['some_field'].widget = forms.HiddenInput(required=False)
        elif my_criteria == 'that':
             self.fields['other_field'].widget=forms.HiddenInput(required=False)
        #else: pass - leave it the way it is.

rawDescription: Is it possible to specify specific form fields when initialising a form in a view?


code:
pd.factorize(x)
pd.factorize(x)[0] + 1

rawDescription: python pandas\numpy encode unique by integers

description: Python pandas\numpy encode unique by integers


code: df.groupby([pd.cut(df.A, 3), pd.cut(df.B, 3)]).C.mean().unstack()

rawDescription: bin DataFrame in pandas for heatmap

description: Bin DataFrame in pandas for heatmap


rawDescription: Equivalent of 'in' for comparing two Numpy arrays


code:
date_in_the_future = date.today() + timedelta(50)
next_friday_in_the_future = get_next_friday(base_date=date_in_the_future)

with patch('module_under_test.get_next_friday') as mocked_function:
    mocked_function.return_value = next_friday_in_the_future

    # call refresh_settlement_date

rawDescription: mock function arguments in python

description: Mock function arguments in python


code: execfile(filename)

rawDescription: How do I execute (not import) a python script from a python prompt?

description: Execute (not import) a python script from a python prompt


code:
# Monkey-patch functools.wraps
import functools

def no_op_wraps(func):
    """Replaces functools.wraps in order to undo wrapping.

    Can be used to preserve the decorated function's signature
    in the documentation generated by Sphinx.

    """
    def wrapper(decorator):
        return func
    return wrapper

functools.wraps = no_op_wraps

rawDescription: Preserve default arguments of wrapped/decorated Python function in Sphinx documentation


code:
import subprocess,os
from Tkinter import *

def text_click_callback(event):
    # an event to highlight a line when single click is done
    line_no = event.widget.index("@%s,%s linestart" % (event.x, event.y))
    #print(line_no)
    line_end = event.widget.index("%s lineend" % line_no)
    event.widget.tag_remove("highlight", 1.0, "end")
    event.widget.tag_add("highlight", line_no, line_end)
    event.widget.tag_configure("highlight", background="yellow")

def viewFile():
    tex.delete('1.0', END)

    for f in os.listdir(path):
        #if f.endswith('.h264'):
        linkname="link-" + f
        tex.insert(END,f + "\n", linkname)
        tex.tag_configure(linkname, foreground="blue", underline=True)
        tex.tag_bind(linkname, "<Button-1>", text_click_callback )    # highlight a line
        tex.tag_bind(linkname, "<Double-Button-1>", lambda event, filename =path+'/'+f: subprocess.call(['vlc',filename]) )    # Video play on VLC Player

if __name__ == '__main__':

    root = Tk()
    #step= root.attributes('-fullscreen', True)

    step = LabelFrame(root,text="FILE MANAGER", font = "Arial 20 bold   italic")
    step.grid(row=1, columnspan=7, sticky='W',padx=100, pady=5, ipadx=130, ipady=25)

    Button(step,    text="ViewFile",        font = "Arial 8 bold    italic",    activebackground="turquoise",   width=30, height=5, command=viewFile).grid      (row= 6, column =3)
    Button(step,    text="Exit",            font = "Arial 8 bold    italic",    activebackground="turquoise",   width=20, height=5, command=root.quit).grid     (row= 6, column =5)

    tex = Text(master=root)                                      # TextBox For Displaying File Information
    scr=Scrollbar(root,orient =VERTICAL,command=tex.yview)
    scr.grid(row=8, column=2, rowspan=15, columnspan=1, sticky=NS)
    tex.grid(row=8, column=1, sticky=E)
    tex.config(yscrollcommand=scr.set,font=('Arial', 8, 'bold', 'italic'))

    global process
    path = os.path.expanduser("/tmp")                   # Define path To play, delete, or rename video

    root.mainloop()

rawDescription: Highlight text when clicked in tkinter

description: Highlight text when clicked in tkinter


code:
import somescript
args = ['one','two']
somescript.main(args)

rawDescription: Call a python main from another python script


code:
INPUT = '/root/Desktop/data'

def deobfuscate(data):
    r = []
    for c in data:
        c = ord(c)
        c = (c + ord('y')) & 0xff
        c = (c ^ ord('Z')) & 0xff
        c = (c - ord('e')) & 0xff
        r.append(c)
    tmp = None
    i = len(r) - 1
    while i >= 0:
        if i == len(r) - 1:
            x = r[i]
            tmp = ((x & 7) << 5) & 0xff

        if i == 0:
            assert tmp is not None
            x = r[0]
            x = (x >> 3) & 0xff
            x = (x + tmp) & 0xff
            r[0] = x
        else:
            c1 = r[i-1]
            c2 = r[i]
            c1 = c1 & 0x7
            c2 = (c2 >> 3) & 0xff
            c1 = (c1 << 5) & 0xff
            c2 = (c2 + c1) & 0xff
            r[i] = c2
        i = i - 1

    r = "".join([chr(x) for x in r])

    s = ""
    assert (len(r) % 2) == 0
    for i in range(len(r)/2):
        s += r[i+(len(r)/2)] + r[i]

    return s

def main():
    with open(INPUT) as inf:
        data = inf.read()

    print(deobfuscate(data))

if __name__=="__main__":
    main()

rawDescription: Python script not printing string

description: Python script not printing string


code:
def __init__(self, data):
    for name, func in data.items():
        setattr(self, name, func)

rawDescription: Assigning a function to a string with the function's name in Python


code:
minima = np.array([[0], [1], [2], [1], [0]])
minima == arange(minima.max() + 1)
(minima == arange(minima.max() + 1)).astype(int)

rawDescription: Array of 1's in indexed positions

description: Array of 1's in indexed positions


code:
# Use a private key
spur.SshShell(
    hostname="localhost",
    username="bob",
    private_key_file="path/to/private.key"
)

rawDescription: Unable to run access EC2 server via cron job


code: string = ' ' * howmany + string

rawDescription: Python: programatically add a specific number of spaces to the beginning of a string


rawDescription: Find all consecutive sub-sequences of length n in a sequence


code: sum(1 for s in soup.find_all('date') if s.text)

rawDescription: Check if tags are empty

description: Check if tags are empty


code:
import multiprocessing

def query_with_timeout(dbc, timeout, query, *a, **k):
  conn1, conn2 = multiprocessing.Pipe(False)
  subproc = multiprocessing.Process(target=do_query,
                                    args=(dbc, query, conn2)+a,
                                    kwargs=k)
  subproc.start()
  subproc.join(timeout)
  if conn1.poll():
    return conn1.recv()
  subproc.terminate()
  raise TimeoutError("Query %r ran for >%r" % (query, timeout))

def do_query(dbc, query, conn, *a, **k):
  cu = dbc.cursor()
  cu.execute(query, *a, **k)
  return cu.fetchall()

rawDescription: python MySQLDB query timeout

description: Python MySQLDB query timeout


rawDescription: Flatten a dictionary to an argument list of its elements


code:
all_dates = set(e['created'] for e in dates) # gets a list of all dates that exist in `dates`
elts_dates = set(e['created'] for e in elts) # same for elts

missing_dates = all_dates - elts_dates

for entry in missing_dates:
    elts.append(dict(created=entry))

rawDescription: Intersect a list of dicts based on a common key


code:
processed = set()
while set(dictionary) - processed:
    for k in set(dictionary) - processed:
        processed.add(k)
        # Do something with k

rawDescription: Iterating over a changing dictionary

description: Iterating over a changing dictionary


code:
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            # insert your TEMPLATE_DIRS here
            os.path.join(BASE_DIR, 'templates'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
                # list if you haven't customized them:
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

rawDescription: Looking for template in a wrong directory


code:
from django.db.models import IntegerField, Case, When, Count

queryset = Model1.objects.all()

queryset = queryset.annotate(
    declined=Count(
        Case(When(model1_participation__status=0, then=1),
             output_field=IntegerField())
    ),
    not_sure=Count(
        Case(When(model1_participation__status=1, then=1),
             output_field=IntegerField())
    ),
    accepted=Count(
        Case(When(model1_participation__status=2, then=1),
             output_field=IntegerField())
    )
)

rawDescription: Django annotate queryset on a specific value of relational model attribute


code:
for item in items:
    tree = item_data
    for i,attr in enumerate(item.get_attr()):
        if attr not in tree.keys():
            if i<len(item.get_attr())-1:
                tree[attr] = {}
            else:
                tree[attr] = []
        else:
            tree = tree[attr]
    # do something to the tree

rawDescription: How can I dynamically create a tree using Python dict

description: Dynamically create a tree using Python dict


code:
def lis_iterative(seq):
    n = len(seq)
    dp = [(0, -1)]*n
    # dp contains (best, idx) tuples, where best is the length of the longest
    # increasing sequence starting from that element, and idx is the index of the
    # next element in that sequence
    for i in range(n-1, -1, -1):
        best = 0
        idx = -1
        for j in range(i+1, n):
            if seq[i] < seq[j] and dp[j][0] + 1 > best:
                best = dp[j][0] + 1
                idx = j
        dp[i] = (best, idx)

    # find longest increasing sequence from dp, then follow idx chain for result
    result = []
    idx = max(range(n), key=lambda i: dp[i][0])
    while idx != -1:
        result.append(seq[idx])
        _, idx = dp[idx]
    return result

rawDescription: Iterative solution to finding Longest Increasing Subsequence using Python


code:
import pwd
import grp
import os

uid = pwd.getpwnam("nobody").pw_uid
gid = grp.getgrnam("nogroup").gr_gid
path = '/tmp/f.txt'
os.chown(path, uid, gid)

rawDescription: How to change the user and group permissions for a directory, by name?

description: Change the user and group permissions for a directory, by name


code:
from xml.dom.minidom import parse
def make_xml(filename, elements):
    dom = parse(filename)
    top_element = dom.documentElement

    for elmnt in elements:
        parent = doc.createElement('word-data')
        top_element.appendChild(parent)
        parent.appendChild(doc.createTextNode(elmnt))
    return dom

new_dom = add_to_xml_file('old_file.xml', ['foo','bar','baz'])

# save file
new_dom.writexml(open('new-bigger-file.xml','w'))

rawDescription: Add to an XML file in python

description: Add to an XML file in python


code: keys = ['name', 'display_name', 'displayName', 'displayname']

rawDescription: Extracting value from dictionary based on matching term from a list


rawDescription: python generate random list starting with

description: Python generate random list starting with


code:
for s in self.level.sprites:
  spritelayer.fill((0), s.rect)

rawDescription: How do I clear a pygame alpha layer efficiently?

description: Clear a pygame alpha layer efficiently


code:
# Use the faster _pickle if possible
try:
    from _pickle import *
except ImportError:
    Pickler, Unpickler = _Pickler, _Unpickler

rawDescription: What difference between pickle and _pickle in python 3?


code:
DOWNLOAD_DELAY
RANDOMIZE_DOWNLOAD_DELAY

rawDescription: Being a good citizen and web-scraping

description: Being a good citizen and web-scraping


code:
click_element(get_element_by_xpath('//*[@id="tracker"]/table[4]/tbody/tr/td[1]/a[1]'),wait=False)
accept_alert('Do you wish to delete?')

rawDescription: NoAlertPresentException when attempting to accept alert using SST Python


code:
import CGAL

min_sphere = CGAL.Geometric_Optimisation.Min_sphere_3()

for pt in verts:
    min_sphere.insert(CGAL.Point_3(pt[0], pt[1], pt[2]))

# Sphere properties: min_sphere.center(), min_sphere.squared_radius().

rawDescription: Where is vtkSphere::ComputeBoundingSphere in in VTK's Python bindings


code:
class Shop(models.Model):
    ...
    code = models.CharField(max_length=4, default=generate_random_code)

rawDescription: Random on model fields generates infinite migration


code:
line = reader.next()
for next_line in reader:
    # Do your processing
    line = next_line
# Now do whatever needs to be done with the very last line

rawDescription: reading .csv file.

description: Reading .csv file.


code:
#if Subscript is an instance of a type
if type(Subscript) in map(type,l):
    return True

# or if Subscript is a type such as a class
if Subscript in map(type,l):
    return True

rawDescription: How to see if an object of a particular type is in a Python list?

description: See if an object of a particular type is in a Python list


code:
from astroquery.simbad import Simbad
Simbad.add_votable_fields('measurements')
acen = Simbad.query_object('alpha centauri')

rawDescription: Astroquery SIMBAD : Obtaining data for diameter, mass, distance, luminosity and age of celestial object


rawDescription: Python regex search only finding the first pattern


code:
class Preferences(ndb.Model):
    gcm = ndb.StringProperty()

user = users.get_current_user()
Preferences(
    gcm='',
    id=user.user_id(),
).put()
prefs = Preferences.get_by_id(user.user_id())

rawDescription: google app engine python: how to extend ndb User class

description: Google app engine python: how to extend ndb User class


code:
if subpath[-1] != "/":
    subpath += "/"

if not isinstance(root_path, bytes):
    root_path = root_path.encode(sys.getfilesystemencoding())

for entry in object_store.iter_tree_contents(tree_id):
    if not validate_path(entry.path, validate_path_element_default):
        continue
    # new lines added here:
    if not entry.path.startswith(subpath):
        continue
    full_path = _tree_to_fs_path(root_path, entry.path[len(subpath):])

    if not os.path.exists(os.path.dirname(full_path)):
        os.makedirs(os.path.dirname(full_path))

    obj = object_store[entry.sha]
    build_file_from_blob(obj, entry.mode, full_path)

rawDescription: How to checkout specific files or directories with dulwich

description: Checkout specific files or directories with dulwich


code:
doc = html5lib.parse('<!doctype html><html><x /></html>', treebuilder='lxml')
doc.xpath('.//*')
doc.xpath('.//html:x', namespaces={'html':'http://www.w3.org/1999/xhtml'})

rawDescription: Differences with element tree and xpath

description: Differences with element tree and xpath


rawDescription: Is there a way to return same length arrays in numpy.hist?


code:
import xarray as xr

# xarray will open your file but doesn't load in any data until you ask for it
# dask handles the chunking and memory management for you
# chunk size can be optimized for your specific dataset.
ds = xr.open_dataset('Somebigfile.nc', chunks={'time': 100})

# mask out values below the threshold
da_thresh = ds['Wind'].where(ds['Wind'] > Somethreshold)

# Now just operate on the values greater than your threshold
do_something(da_thresh)

rawDescription: NetCDF Big data

description: NetCDF Big data


code:
code = ast.parse(open("someones_class.py").read())
code.body.pop(1)
code.body.pop(1)
eval(compile(code, '', 'exec'))
test = A(4)
test

rawDescription: Python: import module without executing script


code:
def isqrt(n):
    x = n
    y = (x + 1) // 2
    while y < x:
        x = y
        y = (x + n // x) // 2
    return x

rawDescription: Integer square root in python

description: Integer square root in python


code:
def test_copysign_hit(candidate):
    # one run per permutation
    def copysign(a1, a2):
        return candidate(a2, a1)
    # original test
    assert copysign(2, 2) == 2
    assert copysign(-3, 2) == 3
    assert copysign(4, -2) == -4
    assert copysign(-5, -2) == -5

rawDescription: pytest-nodev: automatic permutation of arguments

description: Pytest-nodev: automatic permutation of arguments


code:
import random
from twisted.internet import reactor

INTERVAL = xxxx
def callRandomTime():
    youFunc()
    reactor.callLater(INTERVAL * random.random(), callRandomTime)

rawDescription: How to set random time interval in scrapy/python?

description: Set random time interval in scrapy/python


code:
import subprocess
import shlex
import os

# relative dir seems to work for me, no /'s or ~'s in front though
dir = 'Desktop/Git_Repo'

# I did get fetch (but not pull) to work
cmd = shlex.split('git pull Git_Repo')

# you need to give it a path to find git, this lets you do that.
env = os.environ

subprocess.Popen(cmd, cwd=dir, env=env)

rawDescription: Python scripts for writing UNIX commands on terminal


code:
from BeautifulSoup import BeautifulSoup

#Or retrieve it from the web, etc.
html_data = open('/yourwebsite/page.html','r').read()

#Create the soup object from the HTML data
soup = BeautifulSoup(html_data)
fooId = soup.find('input',name='fooId',type='hidden') #Find the proper tag
value = fooId.attrs[2][1] #The value of the third attribute of the desired tag
                          #or index it directly via fooId['value']

rawDescription: Python regular expression for HTML parsing (BeautifulSoup)


code:
def element_to_string(element):
    s = element.text or ""
    for sub_element in element:
        s += etree.tostring(sub_element)
    s += element.tail
    return s

rawDescription: How do I get the whole text of an element using ElementTree?

description: Get the whole text of an element using ElementTree


code:
from Tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append('i can has clipboardz?')
r.destroy()

rawDescription: How do I copy a string to the clipboard on Windows using Python?

description: Copy a string to the clipboard on Windows using Python


code:
from xml.etree import ElementTree
example = ElementTree.fromstring('''\
<a>
hello
<B>
there
</B>
How was your day.
</a>
'''
)
example
example.text
example.find('B')
example.find('B').text
example.find('B').tail

rawDescription: How to extract text in nested xml after closing of one tag in python using xml.etree.ElementTree

description: Extract text in nested xml after closing of one tag in python using xml.etree.ElementTree


code:
i = len(x) - 1
while i > 0:
    j = randint(0, i)
    x[i], x[j] = x[j], x[i]
    i = i - 1

rawDescription: Avoiding Exact Repeats for Mersenne Twister in Python


rawDescription: return max value from a list that has multiple elements python

description: Return max value from a list that has multiple elements python


rawDescription: Given a big list of urls, what is a way to check which are active/inactive?


code:
import cPickle
f = cPickle.load(open('mnist.pkl', 'rb'))
type(f)
len(f)
type(f[0])
len(f[0])
type(f[0][0])
len(f[0][0])
f[0][0].shape

rawDescription: Python Terminology of "pairs"

description: Python Terminology of "pairs"


code: pd.rolling_mean(df.stack(), window=12)

rawDescription: Moving average for months over years

description: Moving average for months over years


rawDescription: re.match one from a set of strings, with result as variable

description: Re.match one from a set of strings, with result as variable


code: lst.append(lambda i=i: i)

rawDescription: Capturing Value instead of Reference in Lambdas


rawDescription: Sum the total line length inside grid using Python


code:
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [125, 32, 54, 253, 67, 87, 233, 56, 67]

color = [item / 255.0 for item in y]
grayscale_map = plt.get_cmap('gray')

plt.figure()
plt.scatter(x, y, s = 500, c = color, cmap = grayscale_map)
plt.colorbar()
plt.show()

rawDescription: Python matplotlib: Add legend for a colormap


code:
class TestAnyImplementation:
    def testThis(self):
        obj = getImpl()  # call to factory method
        self.assertTrue(obj.isEmpty())

class TestFooImplementation(TestAnyImplementation,TestCase):
    def getImpl(self):
        return Foo()

class TestBarImplementation(TestAnyImplementation,TestCase):
    def getImpl(self):
        return Bar()

rawDescription: How to reuse the same tests to test different implementations?

description: Reuse the same tests to test different implementations


code:
import urllib2
url = "http://charlesngo.com/wp-content/uploads/2015/11/rat-race-full-res-1030x728.jpg"
req = urllib2.Request(url)
resp = urllib2.urlopen(req)
req = urllib2.Request(url)
req.add_header('user-agent', "Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11")
resp = urllib2.urlopen(req)
resp.read()[:10]

rawDescription: urllib.retrieve and urllib2.urlopen never returns after downloading the image

description: Urllib.retrieve and urllib2.urlopen never returns after downloading the image