code:
from operator import itemgetter

# first get index from first row
min_index = min(enumerate(foo[0]), key=lambda x: x[1][1])[0]

# then apply to all rows
foo = [item[min_index:] + item[:min_index] for item in foo]

rawDescription: lists: index of lowest value and reorder

description: Lists: index of lowest value and reorder


code:
# Read data from the first file
with open("file1.txt", "rt") as f:
    d1 = f.read()

# Read data from the second file
with open("file2.txt", "rt") as f:
    d2 = f.read()

# Make sure that both sizes are equal
assert len(d1) == len(d2)

# Calculate OR-ed data
d3 = "".join(chr(ord(d1[i]) | ord(d2[i])) for i in range(len(d1)))

# Write the output data
with open("file3.txt", "wt") as f:
    f.write(d3)

rawDescription: logical "OR" between bin files

description: Logical "OR" between bin files


code: plt.xlim(0, len(counter_trim))

rawDescription: Matplotlib skips data -

description: Matplotlib skips data -


code:
import matplotlib.pyplot as plt
freq = {1: 1000, 2: 980, 4: 560, 40: 3, 41: 1, 43: 1}
fig, ax = plt.subplots()
ax.bar(freq.keys(), freq.values())
fig.savefig("bar.png")

rawDescription: Creating a simple python bar chart / histogram using data in dictionary


code:
val_dict = {}

for row in creader:
    try:
        value = val_dict[row[0]]
        value += row[1] + ","
    except KeyError:
        value = row[1] + ","
    val_dict[row[0]] = value

for row in val_dict.items():
    to_db = [unicode(row[1][:-1], "utf8"), unicode(row[0], "utf8")]
    cursor.execute("UPDATE 'people' SET 'description'= ? WHERE name= ?", to_db)

rawDescription: Best way to loop through .csv file that has two columns and duplicate entries in column 1?

description: Loop through .csv file that has two columns and duplicate entries in column 1


code:
if __name__ == '__main__':

    # Pyinstaller fix
    multiprocessing.freeze_support()

    main()

rawDescription: pyInstaller loads script multiple times

description: PyInstaller loads script multiple times


rawDescription: Is there a built in python method for double slashing file paths?


code:
#ax = plt.gca()
ttl = ax.title
ttl.set_position([.5, 1.05])
#plt.draw()

rawDescription: Increase distance between title and plot in matplolib?


code:
from itertools import chain
a = ['1'], ['3'], ['3']
list(chain(*a))

rawDescription: Python merge multiple list inside one variable into one list


code:
def sort_char_list(lst):
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    # Here, create the 26 buckets
    new_list = [''] * len(alphabet)

    for letter in lst:
        # This is the bucket index
        # You could use `ord(letter) - ord('a')` in this specific case, but it is not mandatory
        index = alphabet.index(letter)
        new_list[index] += letter

    # Assemble the buckets
    return ''.join(new_list)

rawDescription: Sorting a list using an alphabet string

description: Sorting a list using an alphabet string


code:
from BeautifulSoup import BeautifulSoup
html = """
<title>Title</title>
first
no-href

second

third

no-bold-tag
text
other-stuff

... """
soup = BeautifulSoup(html)
[a['href'] for a in soup('a', href=True) if a.b and len(a) == 1]

rawDescription: Matching HTML subtree with BeautifulSoup


code:
from collections import Counter

numbers = {
    word: iter([""] if count == 1 else xrange(1, count + 1))
    for word, count in Counter(sample).items()
}

result = [
    word + str(next(numbers[word]))
    for word in sample
]

rawDescription: Find and replace duplicates in Array, but replace each nth instance with a different string


code:
class codeCheck(FancyValidator):
    def to_python(self, value, state=None):
        if value==None:
            raise Invalid('missing a value', value, state)
        return super(codeCheck,self).to_python(value,state)

    def _validate_python(self, value, state):
        codes = DBSession.query(Code).all()
        if value not in codes:
            raise Invalid('wrong code',value, state)
        return value

class itemsEditSchema(Schema):
    code = codeCheck ()
    allow_extra_fields = True

rawDescription: Formencode OneOf validator with dynamic list to test against


code:
from IPy import IP
ip = IP('127.0.0.0/30')
ip.iptype()

rawDescription: How do you determine if an IP address is private, in Python?

description: Determine if an IP address is private, in Python


code:
string = "abcd\n"
print(repr(string))

rawDescription: Print "\n" or newline characters as part of the output on terminal


code:
class FilterDeleteListSerializer(serializers.ListSerializer):
    def to_representation(self, data):
        data = data.filter(delete=False)
        return super(FilterDeleteListSerializer, self).to_representation(data)

class NestedSampleSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = api_models.NestedSample
        list_serializer_class = FilterDeleteListSerializer

class NestedSample2Serializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = api_models.NestedSample2
        list_serializer_class = FilterDeleteListSerializer

class SampleSerializer(serializers.HyperlinkedModelSerializer):
    nested = NestedSampleSerializer() # HERE filter delete=false
    nested2 = NestedSample2Serializer() # HERE filter deletefalse

    class Meta:
        model = api_models.Sample

rawDescription: How can i filter queryset in nested serializer in django

description: Filter queryset in nested serializer in django


code:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(5,2))
ax = fig.add_subplot(111, position=[0.01, 0.01, 0.98, 0.98])

rawDescription: Changing Chart Size in Matplotlib

description: Changing Chart Size in Matplotlib


code:
from django.db import models
from django.contrib.auth.models import User

class MyUser(models.Model):
    user = models.OneToOneField(User)
    friends = models.ManyToManyField(User) # you can also define this relationship to MyUser

rawDescription: create friends list between users in django web application

description: Create friends list between users in django web application


code:
def run_powershell(scriptblock, cwd=os.getcwd()):
    log.debug("Running PowerShell Block:\r\n%s", scriptblock)
    log.debug("Current Directory: %s\r\n" % cwd)
    psProc = subprocess.Popen([r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe',
                                '-ExecutionPolicy', 'Bypass',
                                '-noprofile',
                                '-c', '-',],
                                cwd=cwd,
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
    stdoutdata, stderrdata = psProc.communicate(scriptblock)

    if stdoutdata:
        log.debug("Script Output:\r\n%s" % stdoutdata)
    elif not stderrdata:
        log.debug("Script completed succssfully (no stdout/stderr).")
    if stderrdata:
        log.error("Script Error:\r\n%s" % stderrdata)

    return stdoutdata, stderrdata

def get_certificates(server_list, location="LocalMachine", store="My"):
    cmd = '''
$sb = { ls Cert:\%s\%s | Select Subject,ThumbPrint }
$Servers = '%s' | ConvertFrom-Json

Invoke-Command -ComputerName $Servers -ScriptBlock $sb -Authentication Negotiate | ConvertTo-Json -Depth 999
    ''' % (location, store, json.dumps(server_list))
    stdoutdata, stderrdata = run_powershell(cmd)
    return json.loads(stdoutdata)

rawDescription: How to get installed certificates on a windows machine remotely with Python?

description: Get installed certificates on a windows machine remotely with Python


code:
or_filters = [getattr(Projects, k).like('%{}%'.format(term)) for term in v]
projects = Projects.query.filter(or_(*or_filters))

rawDescription: Flask-SQLAlchemy 'OR' query with variable number of parameters


code:
mutabledict = dict(zip('abc', range(3)))
immutable = frozenset(mutabledict.items())
read_frozen = dict(immutable)
read_frozen['a'] # => 1

rawDescription: Immutable dictionary, only use as a key for another dictionary


code:
with open("results.txt", "w") as out:
    xiter = iter(xvariables)
    for idx in results:
        print >>out, idx, next(xiter), next(xiter), next(xiter)

rawDescription: Python lists, pairing and writting

description: Python lists, pairing and writting


code:
import re
cleaned = re.sub(r'& ?(ld|rd)quo ?[;\]]', '\"', raw)
cleaned = re.sub(r'& ?(ls|rs)quo ?;', '\'', cleaned)
cleaned = re.sub(r'& ?ndash ?;', '-', cleaned)

rawDescription: Text Cleaning python

description: Text Cleaning python


code:
#!/usr/bin/env python
#your script here

rawDescription: How to give python command line arguments from command line mode

description: Give python command line arguments from command line mode


code:
class MyModelRetieveApiView(generics.RetrieveAPIView):
    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer
    permission_classes = (IsAuthenticated,)
    authentication_classes = (TokenAuthentication,)

rawDescription: How is the 'obj' parameter passed to Permission class function in Django Rest Framework?


code:
seq = [["bar","baz","foo","bar"],["foo","bar","baz","foo","bar"]]
omit = [(0,"foo"),(4,"bar")]
for x in seq:
    if not any(i < len(x) and x[i] == v for i,v in omit):
        print(x)

rawDescription: Elegant way to test list of if statements in Python


code:
def g():
  import numpy as np

def h():
  import np.ma as ma # note: np is not defined!

dis.dis(g)
dis.dis(h)

rawDescription: Why can I not import from a module in memory


code: end_result = [[(w, y), (x, z)] for [(w, x), (y, z)] in c]

rawDescription: Re-group 2D coordinates of pairs in Python


code:
class BaseForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(SomeForm, self).__init__(*args, **kwargs)

        self.base_fields['st_name'].widget = CustomWidget()
        self.base_fields['st_key'].widget = CustomWidget()
        self.base_fields['st_meta'].widget = CustomWidget()

class NumberOneForm(BaseForm):
    class Meta:
        model = NumberOneModel

class NumberTwoForm(BaseForm):
    class Meta:
        model = NumberTwoModel

rawDescription: Django - Change default widgets of fields in MultiColumnField


code:
@app.route('/', methods=['GET', 'POST'])
def index():
    form = NameForm()

    if form.validate_on_submit():
        session['name'] = form.name.data
        return redirect(url_for('index'))

    return render_template('index.html', form=form, name=session.get('name'))

rawDescription: Email field not clearing on successful form submit


code:
model = MultinomialNB(0.5).fit(X, y)
# or
model = LogisticRegression().fit(X, y)

rawDescription: Python scikit-learn Predictionfail

description: Python scikit-learn Predictionfail


code:
A
B
bkeys=set([i[0] for i in B])
[i for i,v in enumerate(A) if v[0] in bkeys]

rawDescription: How to find a list of elements in an array using python

description: Find a list of elements in an array using python


code:
df = pd.concat([a, b.set_index('date')], axis=1)
df.interpolate(method='time').plot()  # interpolate(method='time').ffill()

rawDescription: Plotting dataframes on same plot

description: Plotting dataframes on same plot


code:
import sys
data = sys.stdin.read()

rawDescription: How to parse the "request body" using python CGI?

description: Parse the "request body" using python CGI


code:
#=========================================================================
def find_windows(class_name = None,
                class_name_re = None,
                parent = None,
                process = None,
                title = None,
                title_re = None,
                top_level_only = True,
                visible_only = True,
                enabled_only = False,
                best_match = None,
                handle = None,
                ctrl_index = None,
                predicate_func = None,
                active_only = False,
                control_id = None,
    ):
    """Find windows based on criteria passed in

    Possible values are:

    * **class_name**  Windows with this window class
    * **class_name_re**  Windows whose class match this regular expression
    * **parent**    Windows that are children of this
    * **process**   Windows running in this process
    * **title**     Windows with this Text
    * **title_re**  Windows whose Text match this regular expression
    * **top_level_only** Top level windows only (default=True)
    * **visible_only**   Visible windows only (default=True)
    * **enabled_only**   Enabled windows only (default=True)
    * **best_match**  Windows with a title similar to this
    * **handle**      The handle of the window to return
    * **ctrl_index**  The index of the child window to return
    * **active_only**  Active windows only (default=False)
    * **control_id**  Windows with this control id
   """

rawDescription: Python pywinauto search windows with partial title


code:
def my_find(text, pattern):
    '''Find the start index of a pattern string in a text.
    Return -1 if not found, and assume that pattern is not empty'''

    found = False
    current_start_index = text.find(pattern[0])
    index_text = current_start_index
    index_pattern = 0

    while not found and index_text + len(pattern) - 1 < len(text) and \
            current_start_index != -1:

        index_text += 1
        index_pattern += 1

        while index_text < len(text) and \
                index_pattern < len(pattern) and \
                text[index_text] == pattern[index_pattern]:

            if index_pattern == len(pattern) - 1:
                found = True
                break
            else:
                index_text += 1
                index_pattern += 1

        if not found:
            current_start_index = text.find(pattern[0],current_start_index + 1)
            index_text = current_start_index

    if found:
        return current_start_index
    else:
        -1

rawDescription: Pattern Matching Python

description: Pattern Matching Python


code:
flux = img.read() # From your request

image_ref = Image(image_data=flux)
w = image_ref.width
h = image_ref.height

rawDescription: How to know the width and the height of image using app-engine image API wit python?

description: Know the width and the height of image using app-engine image API wit python


code:
def perform_bubble_sort(blist):
    cmpcount, swapcount = 0, 0
    for j in range(len(blist)):
        for i in range(1, len(blist)-j):
            cmpcount += 1
            if blist[i-1] > blist[i]:
                swapcount += 1
                blist[i-1], blist[i] = blist[i], blist[i-1]
    return cmpcount, swapcount

rawDescription: Python Bubble Sort list with swap count

description: Python Bubble Sort list with swap count


code:
class MySpinBox(QtGui.QSpinBox):
    def __init__(self, *args):
       QtGui.QSpinBox.__init__(self, *args)

       self.setRange(0,9999)

    def textFromValue(self, value):
       return "%04d" % value

rawDescription: PyQt4 QSpinBox value format

description: PyQt4 QSpinBox value format


code:
import urllib2
import time
url = 'Roblox url'
data = urllib2.urlopen(url)
time.sleep(10)
data = data.read()

rawDescription: Python - Urllib2 Wait for page to load to scrape data


code: property_names=[p for p in dir(SomeClass) if isinstance(getattr(SomeClass,p),property)]

rawDescription: How to list all class properties

description: List all class properties


code: td = timedelta(**{my_var: 5})

rawDescription: Can I use a string as a named argument in python

description: Use a string as a named argument in python


rawDescription: Python Selenium: Can't find element by link text


code:
import wx

class GUI(wx.Frame):
    def __init__(self, parent, title=""):
        wx.Frame.__init__(self, parent=parent, title=title, size=(340,380))
        self.SetMinSize((140,180))

        self.creating_widgets = False

        self.panel = wx.Panel(id=wx.ID_ANY, parent=self)

        self.startButton = wx.Button(self.panel, wx.ID_ANY, 'Start')
        self.stopButton = wx.Button(self.panel, wx.ID_ANY, 'Stop')
        self.messageBox = wx.TextCtrl(self.panel, wx.ID_ANY, '', size=(75, 20))

        self.Bind(wx.EVT_BUTTON,  self.onStart, self.startButton)
        self.Bind(wx.EVT_BUTTON,  self.onStop, self.stopButton)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.startButton, 0, wx.ALL, 10)
        self.sizer.Add(self.stopButton, 0, wx.ALL, 10)
        self.sizer.Add(self.messageBox, 0, wx.ALL, 10)

        self.panel.SetSizerAndFit(self.sizer)

    def onStart(self, event):
        self.creating_widgets = True
        count = 0
        self.startButton.Disable()
        while self.creating_widgets:
            count += 1
            #Create your widgets here

            #just for simulations sake...
            wx.MilliSleep(100)
            self.messageBox.SetLabel(str(count))

            #Allow the window to update,
            #You must call wx.yield() frequently to update your window
            wx.Yield()

    def onStop(self, message):
        self.startButton.Enable()
        self.creating_widgets = False

if __name__ == "__main__":

    app = wx.PySimpleApp()
    frame = GUI(None)
    frame.Show()
    app.MainLoop()

rawDescription: Get object created in child thread back in main thread


code:
def __setitem__(self, key, value):
    key = self.encodekey(key)
    value = self.encodevalue(value)
    self.putenv(key, value)
    self._data[key] = value

rawDescription: Using copy.deepcopy on os.environ in python appears broken


code:
with open('data.txt', 'r') as myfile:
    data=myfile.read().replace('\n', '')

rawDescription: How do I read a text file into a string variable in Python

description: Read a text file into a string variable in Python


rawDescription: python math, numpy modules different results?

description: Python math, numpy modules different results


code:
the_string = raw_input()
name, age = the_string.split()

rawDescription: Reading Space separated input in python

description: Reading Space separated input in python


code:
for line in ActiveFileR:
    newline = line
    for ReChr1 in CapAlphabet:
        for ReChr2 in CapAlphabet:
            newline = newline.replace(ReChr1 + ReChr2, ReChr1 + " " + ReChr2)
    ActiveFileW.write(newline)

rawDescription: find and replace with multiple for loops in Python

description: Find and replace with multiple for loops in Python


code:
def percentage(rd):
    apl = [10,10,10,5,5,20,10,10,10,10]
    if age > 15:
        t=sum(apl)
    if age < 15:
        t=sum(apl) + 10
    com=rd/t*100 #this overwrites whatever the passed argument com was.
    com = round(com,0) #assign the rounded value back to com
    return com

rawDescription: Rounding in Python.

description: Rounding in Python.


code:
for phrase in l:
    if all(word in phrase for word in q):
        print(phrase)

rawDescription: Python - search a list for items in another list


rawDescription: python conditional storing a value

description: Python conditional storing a value


code:
static = 'http://www.example.com/'
end = ['abc','def']

{ k:'{}{}'.format(static, v) for k,v in enumerate(end) }

rawDescription: populate dictionary with for loop

description: Populate dictionary with for loop


code:
from Tkinter import *
import ttk

root = Tk()

def calculate(*args) :
    value = float(feet.get())
    meters.set((0.305 * value * 10000. + .5)/10000.)

def callback_function(*args) :
    meters.set('blue')

mf = ttk.Frame(root, padding="3 3 12 12")
mf.grid(column=0, row=0, sticky=(N, W, E, S))
mf.columnconfigure(0, weight=1)
mf.rowconfigure(0, weight=1)

feet = StringVar()
meters = StringVar()

feet_entry = ttk.Entry(mf, width=7, textvariable=feet)
feet_entry.grid(column=2, row=1, sticky=(W, E))

ttk.Label(mf, textvariable=meters, background='#E9D66B').grid(column=2,
          row=2, sticky=(W, E))

ttk.Button(mf, text="Calculate", command=calculate).grid(column=2,row=3,
          sticky=W)

ttk.Label(mf, text="feet").grid(column=3, row=1, sticky=W)
ttk.Label(mf, text="is equivalent to").grid(column=1, row=2, sticky=E)
ttk.Label(mf, text="meters").grid(column=3, row=2, sticky=W)

for child in mf.winfo_children():
   child.grid_configure(padx=5, pady=5)

feet_entry.focus()
root.bind('<Return>', calculate)

# this is the key line
root.bind('red', callback_function)

root.mainloop()

rawDescription: Python Tkinter listener in text box

description: Python Tkinter listener in text box


rawDescription: Read FORTRAN formatted numbers with Python


code: 3>> print(mystery)

rawDescription: The unicode can not be demonstrated in python


code:
fields = ('mykey1', 'mykey2', ...)
with open('output.csv', 'wb') as f:
    writer = csv.DictWriter(f, fields)
    writer.writerow({f: f for f in fields})  # write a header row
    for entry in myList:
        write.writerow(entry)

rawDescription: Get values from a lists of hashmaps and put into one list?


code:
time1 = datetime.datetime.strptime(timestring1, '%H:%M:%S')
time2 = datetime.datetime.strptime(timestring2, '%H:%M:%S')
difference = time2 - time1

rawDescription: How to get difference in seconds between two strings?

description: Get difference in seconds between two strings


code:
string = "_GRP_Group||s1||s2||Group2||"
word = re.findall(r"(?:_GRP_)?([^|]+)", string)

rawDescription: Extract data from string, leaving out a pattern


code:
@property
def path_from_root(self):
    if self.parent:
        return self.parent.path_from_root + [self]
    return [self]

rawDescription: Tree Path from Root to Selected Node - The Pythonic Way


code:
test = {'a' : None, 'b' : None}
b = (','.join([k for k in test if test[k]])
     or 'hello')

rawDescription: List comprehension and or spacing with a new-line


code:
from PyQt4 import QtGui as gui

app = gui.QApplication([])

pm = gui.QPixmap("icon.png")

scene = gui.QGraphicsScene()
scene.setSceneRect(0, 0, 200, 100) #set the size of the scene

view = gui.QGraphicsView()
view.setScene(scene)

item1 = scene.addPixmap(pm) #you get a reference of the item just added
item1.setPos(0,100-item1.boundingRect().height()) #now sets the position

view.show()
app.exec_()

rawDescription: PyQt/PySide How to access/move QGraphicsItem after having added it to QGraphicsScene


code:
import array
import fcntl
import os

FAT_IOCTL_GET_ATTRIBUTES = 0x80047210
FATATTR_BITS = 'rhsvda67'

def get_fat_attrs(fn):
    fd = os.open(fn, os.O_RDONLY)
    try:
        buf = array.array('L', [0])
        try:
            fcntl.ioctl(fd, FAT_IOCTL_GET_ATTRIBUTES, buf, True)
        except IOError as ioe:
            if ioe.errno == 25: # Not a FAT volume
                return None
            else:
                raise

        return buf[0]
    finally:
        os.close(fd)

if __name__ == '__main__':
    import sys
    for fn in sys.argv[1:]:
        attrv = get_fat_attrs(fn)
        if attrv is None:
            print(fn + ': Not on a FAT volume')
            continue
        s = ''.join((fb if (1 << idx) & attrv else ' ')
                    for idx,fb in enumerate(FATATTR_BITS))
        print(fn + ': ' + s)

rawDescription: Get fat32 attributes with python

description: Get fat32 attributes with python


code:
import itertools
import operator

def main():
  for let, gen in itertools.groupby(big_gen(), key=operator.itemgetter(0)):
    secgen = itertools.imap(operator.itemgetter(1), gen)
    printer(let, secgen)

rawDescription: Can a Python function take a generator and return generators to subsets of its generated output?


code:
def shellquote(s):
    return "'" + s.replace("'", "'\\''") + "'"

rawDescription: How to escape os.system() calls in Python?

description: Escape os.system() calls in Python


code:
from ConfigParser import ConfigParser, NoOptionError, NoSectionError
from boto.s3.connection import S3Connection

try:
   config = ConfigParser()
   config.readpf(open('/etc/boto.cfg', 'rb'))
   aws_seckey = config.get('Credentials', 'aws_secret_access_key')
   aws_keyid = config.get('Credentials', 'aws_access_key_id')
   conn = S3Connection(aws_keyid, aws_seckey)

except (IOError, NoSectionError, NoOptionError):
   conn = S3Connection()  # try env if /etc/boto.cfg missing

except:
  raise

rawDescription: Connection with boto to AWS hangs when running with crond


code:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
from matplotlib.colors import colorConverter
import matplotlib.pyplot as plt
import numpy as np

zs = []
fig = plt.figure()
ax = fig.gca(projection='3d')
verts = []

# XY data (i.e. "normal line plots")
count = 4
for i in range(count):
    xs, ys = [800.0, 900.0, 1000.0, 1100.],  [0., 1., 1., 0.]
    verts.append(list(zip(xs, ys)))

# Z position (i.e. depth at which the XY plot is drawn)
zs = [0,1,2,3]

colours = plt.cm.Blues(np.linspace(0.2, 0.8, len(zs)))
poly = PolyCollection(verts, facecolors = colours )

for path in poly.get_paths() :  # There is the fix :
  path.codes[-1] = 0        # we have to manually switch the last point in a path to STOP (code = 0)

ax.add_collection3d(poly, zs=zs, zdir='y')

ax.set_xlabel('X')
ax.set_xlim3d(800,1150)
ax.set_ylabel('Y')
ax.set_ylim3d(0, 4)
ax.set_zlabel('Z')
ax.set_zlim3d(0, 1)
plt.show()

rawDescription: Odd line artefacts appearing in matplotlib mplot3d plot using PolyCollection


code:
@gen.coroutine
def moveForward():
    while True:
        do_something()
        # Allow other processing while we wait 1 sec.
        yield gen.sleep(1)

rawDescription: Running Background task with Python and Tornado


code:
# declare file names and necessary lists
file1 = "file1.txt"
file2 = "file2.txt"
file1_new = "file1.txt.EDIT"
clrs = {}

# read clrs to update
with open(file1, "r") as f:
    # skip header line
    f.next()
    for line in f:
        clrs[line[:3]] = []

# read the new codes
with open(file2, "r") as f:
    # skip header
    f.next()
    for line in f:
        current = line.strip().split(",")
        key = current[0].lower()
        if key in clrs:
            clrs[key].append(current[1])

# write the new lines (old codes replaced with the new ones) to new file
with open(file1, "r") as f_in:
    with open(file1_new, "w") as f_out:
        # writes header
        f_out.write(f_in.next())
        for line in f_in:
            line_new = list(line)
            key = line[:3]
            # checks if new codes were found for that key
            if key in clrs.keys():
                # replaces old keys by the new keys
                line_new[3:15] = "".join(clrs[key])
            f_out.write("".join(line_new))

rawDescription: update records in file2 with data found in file1

description: Update records in file2 with data found in file1


code:
for filename in os.listdir('./json/'):
    sources = None
    with open('./json/'+filename) as data_file:
        sources = json.load(data_file)
        sourcelist = sources['sources']
        for i, s in enumerate(sourcelist):
            sources['sources'][i]['collectorName'] = 'Collector' + str(i)
    with open('./json/'+filename, 'w') as data_file:
        data_file.write(json.dumps(sources))

rawDescription: Add "entry" to JSON File with Python

description: Add "entry" to JSON File with Python


code:
from wand.image import Image
from wand.display import display
from wand.drawing import Drawing
from wand.color import Color
img=Image(filename='../../rmli_fr/rmli_fr.ave.ras')

color = Color('red')
draw = Drawing()
draw.fill_color = color
for i in pts_comp:
    draw.line((i[0], i[1]), (i[0], i[1]))
    draw(img)

img.save(filename='rmli.ras')

rawDescription: Wand (imagemagick) draw colored points on top of greyscale sun raster image


rawDescription: Find the Most Nested (), Solving with SymPy, and Work Outwards?


code:
for val in comparison_list:
    for a in args:
        val = val[a]
    # Do something with val

rawDescription: Python: use *args as dictionary key

description: Python: use *args as dictionary key


code:
import sys
if sys.version_info < (3, 0):
    from Tkinter import *
else:
    from tkinter import *

class btn_demo:
    def __init__(self):
        self.click_count = 0
        self.window = Tk()
        self.btn_txt = 'Change label!'
        self.btn = Button(self.window, text=self.btn_txt, command=self.update_label)
        self.btn.pack()
        self.lab = Label(self.window, text="")
        self.lab.pack()
        mainloop()

    def update_label(self):
        texts = ['the first', 'the second', 'the third']
        self.click_count = (self.click_count + 1) % len(texts)
        self.lab["text"] = texts[self.click_count]

demo = btn_demo()

rawDescription: Tkinter label text changes with each button press


rawDescription: How to filter weekday of datetime in model filter?

description: Filter weekday of datetime in model filter


code:
import asyncio
import time
from concurrent.futures import ProcessPoolExecutor

@asyncio.coroutine
def coro(loop):
    ex = ProcessPoolExecutor(2)
    yield from loop.run_in_executor(ex, time.sleep, 10)  # This can be interrupted.

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait_for(coro(loop), 1))

rawDescription: Python asyncio force timeout

description: Python asyncio force timeout


code: map(d.get, keys)

rawDescription: An efficient way of replacing list of keys with dict values?


code:
with open('filename.txt', 'r') as f:
    numbers = [float(x.strip()) for x in f]

rawDescription: converting input file to list

description: Converting input file to list


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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x, y = np.random.rand(2, 100) * 4
hist, xedges, yedges = np.histogram2d(x, y, bins=4)

elements = (len(xedges) - 1) * (len(yedges) - 1)
xpos, ypos = np.meshgrid(xedges[:-1]+0.25, yedges[:-1]+0.25)

xpos = xpos.flatten()
ypos = ypos.flatten()
zpos = np.zeros(elements)
dx = 0.5 * np.ones_like(zpos)
dy = dx.copy()
dz = hist.flatten()

ax.bar3d(xpos[:8], ypos[:8], zpos[:8], dx, dy, dz, color='b', zsort='average')
blue_proxy = plt.Rectangle((0, 0), 1, 1, fc="b")
ax.bar3d(xpos[8:], ypos[8:], zpos[8:], dx, dy, dz, color='r', zsort='average')
red_proxy = plt.Rectangle((0, 0), 1, 1, fc="r")
ax.legend([blue_proxy,red_proxy],['cars','bikes'])

plt.show()

rawDescription: How to create a legend for 3D bar in matplotlib?

description: Create a legend for 3D bar in matplotlib


code:
from itertools import product

pro = product(A,B)
C = map(lambda x: func(*x), pro)

rawDescription: Iterate two or more lists / numpy arrays... and compare each item with each other and avoid loops in python


code:
x, y = (image > limit).nonzero()
vals = image[x, y]

rawDescription: Faster way to loop through every pixel of an image in Python?


code:
coll = {1:{'a','b'}, 2:{'b','e'}, 3:{'a','c'}, 4:{'b','f'}}
print(coll)

d=dict()
for subs in coll.values():
  for s in subs:
    d[s]=d.setdefault(s, 0)+1

m=max(d.values())
print(m)

rawDescription: Finding n largest intersections of collection of sets in Python


code: print("String to print %s %s\n" % (var1, var2), file=sys.stdout)

rawDescription: Python3 Print Function

description: Python3 Print Function


code:
import pymunk
b = pymunk.Body()
b2 = pymunk.Body()
j = pymunk.PivotJoint(b,b2,(0,0))
s = pymunk.Circle(b,3)
b.constraints
b.shapes

rawDescription: Pymunk -- Finding a body's associated shapes and constraints


code:
def get(self):
    deck = range(1, 101)
    shuffle(deck)
    items = db.GqlQuery("SELECT * FROM joke WHERE jokeID IN :1", deck[:3])
    template_values = {'items' : items}
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path,template_values))

rawDescription: I can't get the entities with my query

description: I can't get the entities with my query


rawDescription: Python - calling wrapper function after naming function it wraps


code:
def addToFile(file, what):
    f = open(file, 'a').write(what)

rawDescription: How to write to CSV and not overwrite past text

description: Write to CSV and not overwrite past text


code:
import wx

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.panel = wx.Panel(self)

        self.box1 = wx.StaticBox(self.panel, label="One")
        self.box2 = wx.StaticBox(self.panel, label="Two")

        self.box_sizer = wx.StaticBoxSizer(self.box1, wx.HORIZONTAL)
        self.box_sizer.Add(self.box2, 1, wx.ALL | wx.EXPAND, 5)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.box_sizer, 1, wx.ALL | wx.EXPAND, 5)
        self.panel.SetSizerAndFit(self.sizer)

        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    win = MainWindow(None)
    app.MainLoop()

rawDescription: Static box in another static box on wxpython


code: tfidf_vect= TfidfVectorizer(use_idf=False,binary=True, norm=False, ngram_range=(2, 2))

rawDescription: How to binarize textual data with scikit learn?

description: Binarize textual data with scikit learn


code:
import matplotlib.pyplot as plt

channel,percentages, mass = "ab",[0.2,0.2,0.1,0.5], 10

# build a rectangle in axes coords
left, width = .25, .5
bottom, height = .25, .5
right = left + width
top = bottom + height

channel = ''.join(i for i in channel if i in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
nu_energy , d_plus_p_energy, e_energy, gamma_energy  = percentages

# The slices will be ordered and plotted counter-clockwise.
labels = [r'$E_{\nu} / E_{tot}$ = %.3f' % nu_energy,
          r'$E_{d+p} / E_{tot}$ = %.3f' % d_plus_p_energy,
          r'$E_{e} / E_{tot}$ = %.3f' % e_energy,
          r'$E_{\gamma} / E_{tot}$ = %.3f' %gamma_energy]
sizes = [nu_energy , d_plus_p_energy, e_energy, gamma_energy]
colors = ['gold','red','green', 'lightskyblue']
explode = (0.1, 0,0,0)
patches, texts = plt.pie(sizes, colors=colors)#, startangle=90) ** not working for some reason
plt.legend(patches, labels, loc = "best")
E_gamma_e = e_energy + gamma_energy
#plt.pie(sizes, explode=explode, labels=labels, colors=colors,
        #autopct='%1.1f%%', shadow=True)
# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')
plt.title(r'DM DM $\rightarrow$ $%s$ + $%s$'%(channel,channel),position=(0.5,1),bbox=dict(facecolor='0.8',), fontsize=30)
plt.text(-1,-0.98, r'$E_{\gamma + e} / E_{tot}$ = %.3f'%E_gamma_e, bbox=dict(facecolor='white', alpha=0.5), fontsize=14)
plt.tight_layout()

plt.show()

rawDescription: Organizing text on pie charts at matplotlib


code:
from multiprocessing.pool import ThreadPool
from random import randint
import threading
import time

MAX_THREADS = 5
print_lock = threading.Lock()  # prevent overlapped printing from threads

def query_data(trade):
    trade_id = trade[0][1][:6]
    time.sleep(randint(1, 3))  # simulate variable working time for testing
    with print_lock:
        print(trade_id)

def process_trades(trade_list):
    pool = ThreadPool(processes=MAX_THREADS)
    results = []
    while(trade_list):
        trade = trade_list.pop()
        results.append(pool.apply_async(query_data, (trade,)))

    # wait until all threads have finished
    while not(all(a_thread.ready() for a_thread in results)):
        pass

def test():
    trade_list = [[['abc', ('%06d' % id) + 'defghi']] for id in range(1, 101)]
    process_trades(trade_list)
    print('done')

if __name__ == "__main__":
    test()

rawDescription: Python start a new thread when old one finishes


code:
def mousePressEvent(self , e):
    self.endx = e.x()
    self.endy = e.y()
    if self.sender == 1:
        self.LineChange(self.endx , self.endy)
    # let the base implementation do its thing
    super(Palette, self).mousePressEvent(e)

rawDescription: MousePressEvent catches signal unnecessary- PyQt4


code:
task_inputs = [
        'input1',
        'input2',
        ]

class Task(object):
    def __init__(self, task_input):
        self.task_input = task_input
    def __call__(self):
        """Do your thing..."""

task_threads = []
for task_input in task_inputs:
    task = Task(task_input)
    thr = Thread(target=task)
    thr.start()
    task_threads.append((thr, task))

# wait for timeout

naughty_tasks = []
for thr, task in task_threads:
    if thr.is_alive():
        # still running!
        """Do your termination thing..."""
        naughty_tasks.append(task)

# now you have a list of Task object with their original input ( naughty_tasks[i].task_input )

rawDescription: Python - Running several OS command in parallel, using timeouts, and checking return codes


code:
worklist = [...]
batchsize = 500

for i in xrange(0, len(worklist), batchsize):
    batch = worklist[i:i+batchsize] # the result might be shorter than batchsize at the end
    # do stuff with batch

rawDescription: How to execute a for loop in batches?

description: Execute a for loop in batches


code:
df
df['id'] = pd.factorize(df['id'])[0]
df

rawDescription: Pandas DataFrame column value remapping

description: Pandas DataFrame column value remapping


code:
from itertools import compress
l = ['a', 'b', 'c']
b = [True, False, False]
list(compress(l, b))

rawDescription: Filter list using Boolean index arrays

description: Filter list using Boolean index arrays


code:
d = {}
with open("dn0501.txt", "r") as f:
    for line in f:
       key, val = line.split()
       d[key] = int(val)

rawDescription: Put the read file in directory

description: Put the read file in directory


code:
def bimodal( low1, high1, mode1, low2, high2, mode2 ):
    toss = random.choice( (1, 2) )
    if toss == 1:
        return random.triangular( low1, high1, mode1 )
    else:
        return random.triangular( low2, high2, mode2 )

rawDescription: Bimodal distribution in C or Python

description: Bimodal distribution in C or Python


code:
def addwatchfiles(*paths):
    for p in paths:
        autoreload.watch(os.path.abspath(p))
addwatchfiles('config.xml')

rawDescription: how to make tornado auto restart when certain files changes

description: Make tornado auto restart when certain files changes


code:
for row in x:
    row[1] = list(itertools.combinations(row[1], 2))

rawDescription: How to do a Cartesian product for words in a list using Python

description: Do a Cartesian product for words in a list using Python


code:
try:
    return x.Evaluate()
except AttributeError:
    return x

rawDescription: How to implement a function that can both accept built-in types and custom classes on Python?

description: Implement a function that can both accept built-in types and custom classes on Python


code:
import arrow
import simpy

start = arrow.get('2015-01-01T00:00:00')
env = simpy.Environment()

# do some simulation ...

current_date = start.replace(seconds=env.now)
print('Curret weekday:', current_date.weekday())

rawDescription: Linking Simpy simulation time to Python Calendar for week day specific actions


code:
from nltk.corpus import brown
from nltk import UnigramTagger as ut
brown_sents = brown.tagged_sents()
train = int(len(brown_sents)*90/100) # use 90% for training
uni_tag = ut(brown_sents[:train]) # this will take some time, ~1-2 mins
uni_tag.tag ("this is a foo bar sentence .".split())
uni_tag.evaluate(brown_sents[train+1:]) # evaluate on 10%, will also take ~1-2 mins

rawDescription: Part-of-speech tag without context using nltk


code:
import re
[m.start() for m in re.finditer(r'f(?=f)', 'fff')]

rawDescription: Match regex multiple times

description: Match regex multiple times


code:
def dCostFunction(self, theta, in_dim, hidden_dim, num_labels, X, y):
        #compute gradient
        t1, t2 = self.uncat(theta, in_dim, hidden_dim)

        a1, z2, a2, z3, a3 = self._forward(X, t1, t2) # p x s matrix

        # t1 = t1[1:, :] # remove bias term
        # t2 = t2[1:, :]
        sigma3 = -(y - a3) * self.dactivation(z3) # do not apply dsigmode here? should I
        sigma2 = np.dot(t2, sigma3)
        term = np.ones((1,num_labels))
        sigma2 = sigma2 * np.concatenate((term, self.dactivation(z2)),axis=0)

        theta2_grad = np.dot(sigma3, a2.T)
        theta1_grad = np.dot(sigma2[1:,:], a1.T)

        theta1_grad = theta1_grad / num_labels
        theta2_grad = theta2_grad / num_labels

        return self.cat(theta1_grad.T, theta2_grad.T)

rawDescription: Wrong values for partial derivatives in neural network python


code:
def split_and_convert(s):
    bits = s[1:-1].split(',')
    return (
        int(bits[0]), bits[1], float(bits[2]),
        float(bits[3]), float(bits[4]), float(bits[5])
    )

data_to_dump = [split_and_convert(s) for s in data]
json.dumps(data_to_dump)

rawDescription: Python: remove double quotes from JSON dumps


code:
from collections import Counter
from random import randint

def roll(ndice,nsides=9):
    return [randint(1,nsides) for _ in range(ndice)]

def count_it():
    c = Counter(roll(5))
    return c.most_common(1)[0][1] >= 3

ntries = 100000
print (sum(1 for _ in range(ntries) if count_it())/ntries)

rawDescription: Using Monte Carlo method to find a specific probability of a certain dice output in Python 3.2


code:
other_news = NewsPost.objects.exclude(id=post.id).filter(live=True,
    categories__in=post.categories.all).distinct().order_by("-posted")[:3]

rawDescription: Filtering a django news post

description: Filtering a django news post


code:
for i,tax in enumerate(othertaxes):
    basecost += tax.calculate(basecost,othertaxes[:i])

rawDescription: Is using __add__ in Python on an int a bad idea?


code:
class MyClass(object):
    pass

if __name__ == '__main__':
    print("This will only run if you run the script explicitly, not import it")

rawDescription: Equivalent to Perl Modulino for Ruby, Python?


code:
sp = subprocess.Popen(..., stdin=subprocess.PIPE)
sp.stdin.close()

rawDescription: Keyboard input between select() in Python


code:
import string
list(vars(string))

rawDescription: globals() works only for the current module

description: Globals() works only for the current module


code: os.kill(pid, signal.SIGINT)

rawDescription: How to send control C to Mac Terminal using python?

description: Send control C to Mac Terminal using python


code:
def main():
    NumberofHouseholds = 0
    HouseIncome = []
    avgincome = 0

    for idNumber, row in enumerate(open('program9.txt', 'r')): # While there are more records
        words = row.split()        # Split the records into substrings
        acctNum = int(words[0])         # Convert first substring to integer
        annualIncome = float(words[1])  # Convert second substring to float
        members = int(words[2])         # Convert third substring to integer
        print(acctNum, format(annualIncome, '.2f'), members, sep=' ')
        if povertycalc(annualIncome, idNumber + 1):
            HouseIncome.append(annualIncome)
        NumberofHouseholds = NumberofHouseholds + 1
        avgincome = (avgincome+annualIncome)/NumberofHouseholds

    # Close the file.
    print(avgincome)
    print(HouseIncome)

def povertycalc(annualIncome, members):
    povertyLevel = 15930.00 + 4160.00 * (members-2)

    if annualIncome < povertyLevel:
        return True
    else: return False

# Call the main function.
main()

rawDescription: How to find the index of last element on list and add it to another

description: Find the index of last element on list and add it to another


code:
def sumdigits(value):
    return value and (value % 10 + sumdigits(value // 10))

rawDescription: Python3 Recursivley Sum Digits of an Integer


code:
class Multiplier(object):
   def __init__(self, num):
       self.num = num
   def __call__(self, other):
       return self.num*other
mul5 = Multiplier(5)
mul5(6)

rawDescription: Overloading standard parantheses ("()") in Python


code:
User.coords = classmethod(lambda s: (s.latitude, s.longitude))

def calc_distance(latlong1, latlong2):
    return func.sqrt(func.pow(69.1 * (latlong1[0] - latlong2[0]),2)
                   + func.pow(53.0 * (latlong1[1] - latlong2[1]),2))

meta.Session.query(User).filter(calc_distance(User.coords(), (my_lat, my_long)) < 5)

rawDescription: How to Use Mathematic Equations as Filters in SQLAlchemy


code:
print('Name Age')
for name, age in dic.items():
    print('{} {}'.format(name, age))

rawDescription: Print a dictionary into a table

description: Print a dictionary into a table


code:
from os import path

with open ('full.txt', 'r') as f:
    lines_full = f.read().splitlines()

with open ('partial.txt', 'r') as f:
    lines_partial = set(f.read().splitlines())  # create set for faster checking

lines_new = [x + '\n' for x in lines_full if path.split(x)[1] not in lines_partial]

with open('remaining.txt', 'w') as f:
    f.writelines(lines_new)

rawDescription: make list from text file and compare the lists

description: Make list from text file and compare the lists


code: client.get('/tracks', tags = "rock, jazz")

rawDescription: Getting tracks with specific tags with python soundcloud module


rawDescription: comparing a string 1-d numpy array elementwise

description: Comparing a string 1-d numpy array elementwise


code:
from sqlalchemy.sql import select, func
# assuming you have imported table from somewhere!!

s = select([func.ifnull(table.c.int1, 0), func.ifnull(table.c.string1, '')])

rawDescription: Using IFNULL in sqlalchemy core

description: Using IFNULL in sqlalchemy core


code:
#!/usr/bin/env python

from __future__ import division

import colorsys
import turtle

def mvmt(original, length):
    if length >= 10:
        fraction = length / 3
        h, s, v = colorsys.rgb_to_hsv(*original.fillcolor())

        clone = original.clone()
        clone.fillcolor(colorsys.hsv_to_rgb(h + 1/6, s, v))
        clone.begin_fill()
        mvmt(clone, fraction)
        clone.right(60)
        mvmt(clone, fraction)
        clone.left(120)
        mvmt(clone, fraction)
        clone.right(60)
        mvmt(clone, fraction)
        clone.end_fill()

    original.forward(length)

def whole(length = 300):
    voldemort = turtle.Turtle()
    voldemort.speed(0)
    voldemort.hideturtle()
    voldemort.penup()
    voldemort.goto(-200,-255)
    voldemort.pendown()

    voldemort.fillcolor((1.0, 0.5, 0.5))
    voldemort.begin_fill()
    mvmt(voldemort, length)
    voldemort.left(60)
    mvmt(voldemort, length)
    voldemort.left(60)
    mvmt(voldemort, length)
    voldemort.left(60)
    mvmt(voldemort, length)
    voldemort.left(60)
    mvmt(voldemort, length)
    voldemort.left(60)
    mvmt(voldemort, length)
    voldemort.end_fill()

turtle.delay(0)
turtle.speed(0)

whole()
turtle.exitonclick()

rawDescription: How would I go about making it so that each new generation of triangles in this koch snowflake is a different color? (Python + Turtle)


code:
import copy as cp

DCT = {...}  # You already know what goes here.

FOUND_PATHS = []            # In case of more than one path to GOAL.
FOUND_REVERSE_PATHS = []

COUNTER = len(DCT)

def back_track(root, target_path = [], counter=COUNTER):
    """
    @param root: DCT key.
    @type root: str.

    @param target_path: Reference to the path we are constructing.
    @type target_path: list.

    """
    global FOUND_PATHS

    # Avoiding cycles.
    if counter == 0:
        return

    # Some nodes aren't full generated.
    try:
        DCT[root]
    except KeyError:
        return

    # End condition.
    if DCT[root] == ['DEADEND']:
        return
    # Path found.
    if DCT[root] == ['GOAL']:
        FOUND_PATHS.append(target_path)             # The normal path.
        reverse_path = cp.copy(target_path)
        reverse_path.reverse()
        FOUND_REVERSE_PATHS.append(reverse_path)     # The path you want.
        return

    for node in DCT[root]:
        # Makes copy of target parh and add the node.
        path_copy = cp.copy(target_path)
        path_copy.append(node)
        # Call back_track with current node and the copy
        # of target_path.
        back_track(node, path_copy, counter=(counter - 1))

if __name__ == '__main__':
    back_track('4345092')
    print(FOUND_PATHS)
    print(FOUND_REVERSE_PATHS)

rawDescription: shortest path from goal to root in directed graph with cycles python

description: Shortest path from goal to root in directed graph with cycles python


code:
cfg_parser = ConfigParser.ConfigParser() # Python 2.x
if cfg_parser.read('config_file_name.ini'):
    path = cfg_parser.get('SECTION_NAME', 'path')
else:
    print("No config file found")

rawDescription: Best way to accept permanent input from a user?

description: Accept permanent input from a user


rawDescription: Python: writing integers to a single byte


code:
def equalize_lengths(l):
    length = len(max(l, key=len))
    return [e.ljust(length) for e in l]

rawDescription: Adding spaces in elements of list(PY)

description: Adding spaces in elements of list(PY)


code:
class Parent(ndb.Model):
    lastname = ndb.StringProperty()

class Child(Parent):
    name = ndb.StringProperty()

Child(lastname='Doe', name='John').put()

rawDescription: Understanding classes in ndb model - GAE


code:
span = driver.find_element_by_xpath('//span[. = "Product 1"]')
product_id = span.get_attribute('id')

rawDescription: Scraping HTML info with Python

description: Scraping HTML info with Python


code:
import pygame
import math

# some simple vector helper functions, stolen from http://stackoverflow.com/a/4114962/142637
def magnitude(v):
    return math.sqrt(sum(v[i]*v[i] for i in range(len(v))))

def add(u, v):
    return [ u[i]+v[i] for i in range(len(u)) ]

def sub(u, v):
    return [ u[i]-v[i] for i in range(len(u)) ]

def dot(u, v):
    return sum(u[i]*v[i] for i in range(len(u)))

def normalize(v):
    vmag = magnitude(v)
    return [ v[i]/vmag  for i in range(len(v)) ]

class Ship(object):
    def __init__(self):
        self.x, self.y = (0, 0)
        self.set_target((0, 0))
        self.speed = 0.7

    @property
    def pos(self):
        return self.x, self.y

    # for drawing, we need the position as tuple of ints
    # so lets create a helper property
    @property
    def int_pos(self):
        return map(int, self.pos)

    @property
    def target(self):
        return self.t_x, self.t_y

    @property
    def int_target(self):
        return map(int, self.target)

    def set_target(self, pos):
        self.t_x, self.t_y = pos

    def update(self):
        # if we won't move, don't calculate new vectors
        if self.int_pos == self.int_target:
            return

        target_vector = sub(self.target, self.pos)

        # a threshold to stop moving if the distance is to small.
        # it prevents a 'flickering' between two points
        if magnitude(target_vector) < 2:
            return

        # apply the ship's speed to the vector
        move_vector = [c * self.speed for c in normalize(target_vector)]

        # update position
        self.x, self.y = add(self.pos, move_vector)

    def draw(self, s):
        pygame.draw.circle(s, (255, 0 ,0), self.int_pos, 2)

pygame.init()
quit = False
s = pygame.display.set_mode((300, 300))
c = pygame.time.Clock()
ship = Ship()

while not quit:
    quit = pygame.event.get(pygame.QUIT)
    if pygame.event.get(pygame.MOUSEBUTTONDOWN):
        ship.set_target(pygame.mouse.get_pos())
    pygame.event.poll()
    ship.update()
    s.fill((0, 0, 255))
    ship.draw(s)
    pygame.display.flip()
    c.tick(60)

rawDescription: Make a sprite move to the mouse click position step by step


code:
s = """A' B F2 E' B2 A2 C' D2 C D' E2 F"""

L = s.split(' ')

for i in xrange(len(L)):
  if len(L[i]) == 1:
    L[i] += 'x'

str_out = ' '.join(L)

rawDescription: String manipulation in Python

description: String manipulation in Python


code:
from copy import copy
from pickle import Pickler, MARK, DICT
from types import DictionaryType

class CanonicalizingPickler(Pickler):
    dispatch = copy(Pickler.dispatch)

    def save_set(self, obj):
        rv = obj.__reduce_ex__(0)
        rv = (rv[0], (sorted(rv[1][0]),), rv[2])
        self.save_reduce(obj=obj, *rv)

    dispatch[set] = save_set

    def save_dict(self, obj):
        write = self.write
        write(MARK + DICT)

        self.memoize(obj)
        self._batch_setitems(sorted(obj.iteritems()))

    dispatch[DictionaryType] = save_dict

rawDescription: Create Hash for Arbitrary Objects?

description: Create Hash for Arbitrary Objects


code:
sentence = input("Please input a sentence: ")
word = input("Please input a word: ")
sentence = sentence.lower()
word = word.lower()
wordlist = sentence.split(' ')
print ("Your word '%s' is in these positions:" % word)
for position,w in enumerate(wordlist):
    if w == word:
        print("%d" % position + 1)

rawDescription: How do i find the position of MORE THAN ONE substring in a string (Python 3.4.3 shell)

description: Find the position of MORE THAN ONE substring in a string (Python 3.4.3 shell)


code:
import mechanize
from bs4 import BeautifulSoup

br = mechanize.Browser()

url = "http://ceojk.nic.in/ElectionPDF/Main.aspx"
response = br.open(url)

br.select_form(name="Form1")
br["RadioButtonList1"] = ["PS Wise Report"]
br["RadioButtonList2"] = ["English"]
br.submit()

# getting ACs
br.select_form(name="Form1")
br["DistlistP"] = ["1"]
br.submit(name="BtnPs")

# getting PSes
br.select_form(name="Form1")
br["AclistP"] = ["1"]
br.submit(name="BtnPs")

# getting report
br.select_form(name="Form1")
br["PslistP"] = ["1"]
response = br.submit(name="BtnPs")

soup = BeautifulSoup(response)
print(soup.find(id="Pnlfile"))

rawDescription: how to change multiple options of webpage using mechanize and bs4

description: Change multiple options of webpage using mechanize and bs4


code:
import Tkinter, tkFileDialog

root = Tkinter.Tk()

x = tkFileDialog.askopenfilename() # Can pass optional arguments for this...

root.destroy()

rawDescription: In python is there a way to use the windows explorer to create a file path which can then be returned as a string?


code: u"\N{NO-BREAK SPACE}"

rawDescription: How can I determine a Unicode character from its name in Python, even if that character is a control character?

description: Determine a Unicode character from its name in Python, even if that character is a control character


code:
def parse_page(self, response):
    xxs = XmlXPathSelector(response)
    for row in xxs.select('//row'):
        detail_page_link = row.select('.//cell[1]/@href')[0].extract()
        yield Request(urlparse.urljoin(response.url, detail_page_link), callback=self.parse_page)

rawDescription: convert lxml to scrapy xxs selector

description: Convert lxml to scrapy xxs selector


code:
q1 = session.query(table_name_1.column_name_1.label("column_name_1")).filter(table_name_1.column_name_2=='Y')
q2 = session.query(table_name_2.column_name_1.label("column_name_1")).filter(table_name_2.column_name_2=='Y')

q3 = union_all(q1, q2)
q3 = select([q3.c.column_name_1]).where(q3.c.column_name_1 == '1234')

rawDescription: SQLAlchemy : Column name on union_all

description: SQLAlchemy : Column name on union_all


code:
filename = str(input("what is the new file going to be called?")) #Enter "test"
file = open(filename + '.txt','w')                                #This will add .txt
file.close()

rawDescription: Making a text file in python

description: Making a text file in python


code:
from Spotfire.Dxp.Data import AddRowsSettings
import System
from System import DateTime
from System.IO import StringReader, StreamReader, StreamWriter, MemoryStream, SeekOrigin
from Spotfire.Dxp.Data import DataType, DataTableSaveSettings
from Spotfire.Dxp.Data.Import import TextFileDataSource, TextDataReaderSettings

#First we need to create your data with some columns.
#Here I have a comma separated miniature table built up with
#a commented option for your variable itself. \r\n used for newline
textData = "name,values,category\r\ncountry,100,color\r\n"
#textData = "col1,col2,col3\r\n" + string_in + "\r\n"

#Memory Stream stuff. Simply just writing our variable
#into a place we can access to make a data source
stream = MemoryStream()
writer = StreamWriter(stream)
writer.Write(textData)
writer.Flush()
stream.Seek(0, SeekOrigin.Begin)

#you need settings to tell the system what stuff you're importing.
#here we define it is comma separated and what data types our columns are.
readerSettings = TextDataReaderSettings()
readerSettings.Separator = ","
readerSettings.AddColumnNameRow(0)
readerSettings.SetDataType(0, DataType.String)
readerSettings.SetDataType(1, DataType.Integer)
readerSettings.SetDataType(2, DataType.String)
textDataSource = TextFileDataSource(stream,readerSettings)

#We create some settings here automatically having the system match
#column names for us between the data table and our data source.
settings = AddRowsSettings(datTab,textDataSource)
#And finally we add the rows from our datasource with our settings.
datTab.AddRows(textDataSource,settings)

rawDescription: spotfire ironpython : Append new row to a data-table

description: Spotfire ironpython : Append new row to a data-table


rawDescription: Finding current location of editors in traitsui


code: has_capital = lambda s:s[0].isupper() or has_capital(s[1:]) if s else False

rawDescription: find a capital in a string. No loops. Only if statements and string methods python

description: Find a capital in a string. No loops. Only if statements and string methods python


code: ykr_.restype = c_float

rawDescription: Call fortran function from Python with ctypes


rawDescription: Convert a byte array to string

description: Convert a byte array to string


code:
def set_function(self, external_function):
    self.F = external_function

rawDescription: can i list methods in python?

description: List methods in python


code:
for action in my_toolbar.actions():
    widget = my_toolbar.widgetForAction(action)
    widget.setFixedSize(width, height)

rawDescription: PyQt4 Toolbar Button Alignment

description: PyQt4 Toolbar Button Alignment


code:
from boto.sqs.message import RawMessage

queue = SQS_CONNECTION.get_queue(QUEUE_NAME)
queue.set_message_class(RawMessage)
messages = queue.get_messages()

rawDescription: Amazon SQS messages coming through boto garbled


code:
def counting(A, m):
    n = len(A)
    count = [0] * (m + 1)
    for k in xrange(n):
        count[A[k]] += 1
    return count

rawDescription: codility counting lesson 2 : swap elements from arrays

description: Codility counting lesson 2 : swap elements from arrays


code:
import glob
import os

list_of_folders = glob.glob('path_to_folder/*')
list_of_folders.sort(key=lambda d: len(os.listdir(d)), reverse=True)

rawDescription: Sort list of folders on most items in folder


code:
import threading
import struct

data =[]
file = open("/dev/input/mouse0", "rb")
e=threading.Event()

def getMouseEvent():
  buf = file.read(3);
  #python 2 & 3 compatibility
  button = buf[0] if isinstance(buf[0], int) else ord(buf[0])
  bLeft = button & 0x1;
  bMiddle = ( button & 0x4 ) > 0;
  bRight = ( button & 0x2 ) > 0;
  x,y = struct.unpack( "bb", buf[1:] );
  return "L:%d, M: %d, R: %d, x: %d, y: %d\n" % (bLeft,bMiddle,bRight, x, y)

def mouseCollect():
    global e
    #this will wait while e is False (without breaking the loop)
    #and loop while e is True
    while e.wait():
        #do something with MouseEvent data, like append to an array, or redirect to pipe etc.
        data.append(getMouseEvent())
mouseCollectThread = threading.Thread(target=mouseCollect)
mouseCollectThread.start()
#toggle mouseCollect with any keyboard input
#type "q" or "quit" to quit.
while True:
    x = input()
    if x.lower() in ['quit', 'q', 'exit']:
        mouseCollectThread._stop()
        file.close()
        break
    elif x:
        e.clear() if e.isSet() else e.set()

rawDescription: Python pause or stop realtime data

description: Python pause or stop realtime data


code:
import matplotlib.pyplot as plt
plt.plot([0,1],[0,1],ls="--",c="b")
plt.fill_between([0,1],[0,1], color="none", hatch="X", edgecolor="b", linewidth=0.0)
plt.show()

rawDescription: matplotlib hatched fill_between without edges?

description: Matplotlib hatched fill_between without edges


code:
clock = pygame.time.Clock()

time_counter = 0

while True:
    time_counter = clock.tick()
    if time_counter > 3000:
        enemy.move()
        time_counter = 0

rawDescription: In PyGame, how to move an image every 3 seconds without using the sleep function?


rawDescription: Zip Python Array?

description: Zip Python Array


code:
solrURL = "http://mySolr:8983/solr/solrdatabase/update/csv?commit=true"
subprocess.call(['curl', '-v', solrURL,  '--data-binary', '@+"fileName"', '-H', 'Content-type:text/xml; charset=utf-8'])

rawDescription: Python curl usage inconsistency

description: Python curl usage inconsistency


code:
import wx

########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        bfont = wx.Font(10,wx.DEFAULT,wx.NORMAL,wx.BOLD)

        vbox = wx.BoxSizer(wx.VERTICAL)
        fgs = wx.FlexGridSizer(rows=2, cols=3, vgap=5, hgap=5)

        # add two rows of widgets to the sizer
        widgets = []
        for i in range(2):
            angle = wx.StaticText(self,label="Angle")
            angle.SetFont(bfont)
            widgets.append(angle)

            count_c = wx.StaticText(self,label="Counts (C)")
            count_c.SetFont(bfont)
            widgets.append(count_c)

            count_u = wx.StaticText(self,label="Counts (U)")
            count_u.SetFont(bfont)
            widgets.append(count_u)

        fgs.AddMany(widgets)
        vbox.Add(fgs, proportion=1,flag=wx.ALL|wx.EXPAND,border=5)
        self.SetSizer(vbox)

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Test")
        panel = MyPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

rawDescription: wxpython use the same static text twice in FlexGridSizer.AddMany

description: Wxpython use the same static text twice in FlexGridSizer.AddMany


code: im.save('/path/to/cover-2.jpg', format='JPEG', subsampling=0, quality=100)

rawDescription: Why is the quality of JPEG images produced by PIL so poor?


rawDescription: Combine negative lookahead and behind regex


code: cidrs = netaddr.iprange_to_cidrs(startip, endip)

rawDescription: IP Range to CIDR conversion in Python?

description: IP Range to CIDR conversion in Python


code:
import imaplib
obj = imaplib.IMAP4_SSL('imap.gmail.com','993')
obj.login('username','password')
obj.select()
obj.search(None,'UnSeen')

rawDescription: Check unread count of Gmail messages with Python


code: canvas.after(time, dosomething) #the first parameter is how many milliseconds it will wait before it will call the second parameter.

rawDescription: Tkinter process not shown durring run

description: Tkinter process not shown durring run


code:
import time
from subprocess import Popen, PIPE

start = time.time()

p1 = Popen(["my_linux_cmd"], stdout=PIPE)
print(p1.communicate()) # sdout

end = time.time()
exec_time = end - start
print(exec_time) # exeution time

rawDescription: executing a command from a string and timing it

description: Executing a command from a string and timing it


code:
import sys
sys.path.insert(0, 'libs')

import webapp2
import datetime
import csv
from models import DomainStateData, Domain

class ExportAllDomainsToCsvHandler(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/csv'
        self.response.headers['Content-Disposition'] = 'attachment; filename="All Domains [{0}].csv"'.format(str(datetime.date.today()))
        writer = csv.writer(self.response.out)
        writer.writerow(["Domain", "Current state", "Plan expiration date", "Trial expiration date", "Current oauth user"])
        all_domain_state_data_dict = dict()
        all_domain_state_data = DomainStateData.all().fetch(1000000)
        all_domains = Domain.all().fetch(1000000)
        for domain_state_data in all_domain_state_data:
            all_domain_state_data_dict[domain_state_data.key().name()] = domain_state_data
        for domain in all_domains:
            if (domain.key().name() in all_domain_state_data_dict):
                domain.__state_data = all_domain_state_data_dict[domain.key().name()]
        all_domains.sort(key=lambda domain: (0 if domain.__state_data.plan_expiration is None else 1, domain.__state_data.plan_expiration, 0 if domain.__state_data.trial_expiration is None else 1, domain.__state_data.trial_expiration, domain.domain))
        for domain in all_domains:
            if (domain.__state_data.plan_expiration is None):
                domain_plan_expiration = "No plan expiration date"
            else:
                domain_plan_expiration = domain.__state_data.plan_expiration.strftime('%Y-%m-%d')
            if (domain.__state_data.trial_expiration is None):
                domain_trial_expiration = "No trial expiration date"
            else:
                domain_trial_expiration = domain.__state_data.trial_expiration.strftime('%Y-%m-%d')
            writer.writerow([domain.domain, domain.cur_state.name, domain_plan_expiration, domain_trial_expiration, domain.admin])

app = webapp2.WSGIApplication([
    ("/csv/export_all_domains_to_csv", ExportAllDomainsToCsvHandler)
], debug=True)

rawDescription: Is it possible to fetch many objects together from Google App Engine?


code:
page = urlopen(url)
html = page.read()    # Here I get the xml data

with open('output.xml', 'w', encoding='utf-8') as f:
    f.write(html.decode(page.headers.get_content_charset()))

rawDescription: Newline character is ignored by Python

description: Newline character is ignored by Python


rawDescription: Python -- Decimal to Hex, Reverse byte order, Hex to Decimal


code:
def rst(p):
    ans = IP(src=p[IP].dst, dst=p[IP].src)/TCP(
        flags='RA',
        sport=p[TCP].dport,
        dport=p[TCP].sport,
        seq = 0,
        ack = p[TCP].seq + 1,
    )
    send(ans, verbose=False)
    return "%s\n => %s" % (p[IP].summary(), ans.summary())

sniff(iface="eth0", filter="tcp and tcp[tcpflags] & tcp-syn == tcp-syn",
      prn=rst)

rawDescription: Scapy, sending, receiving and responding


rawDescription: Querying with Flask-SQLAlchemy

description: Querying with Flask-SQLAlchemy


code:
from bson.json_util import loads, dumps

...

c = Course.objects.get_or_404(id=course_id)

json = loads(c.to_json())

return dumps(json['assignments'])

rawDescription: Mongoengine - JSONify EmbeddedDocumentListField


code:
index = 2
for table in soup.find_all('table'):
    for row in soup.find_all('tr'):
        try:
            td = row.find_all('td')[index]
        except IndexError:
            continue
        for link in td.find_all('a'):
            print(link.contents)

rawDescription: BeautifulSoup Specify table column by number?


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup(open(path_to_html_file))

name_span = soup.find(id="sobi2Details_field_name")

# First way: split text over ':'
# This only works because there's always a ':' before the target field
name = name_span.text.split(':')[1]

# Second way: iterate over the span strings
# The element you look for is always the last one
name = list(name_span.strings)[-1]

# Third way: iterate over 'next' elements
name = name_span.next.next.next # you can create a function to do that, it looks ugly :)

rawDescription: extract data from html content

description: Extract data from html content


code:
import concurrent.futures
import multiprocessing
import requests
import time
import json

cpu_start_time = time.process_time()
clock_start_time = time.time()

queue = multiprocessing.Queue()
uri = "http://localhost:5000/data.json"
users = [str(user) for user in range(1, 50)]

with concurrent.futures.ThreadPoolExecutor(multiprocessing.cpu_count()) as executor:
    for user_id, result in zip(
            [str(user) for user in range(1, 50)]
            , executor.map(lambda x: requests.get(uri, params={id: x}).content, users)
    ):
        queue.put((user_id, result))

while not queue.empty():

    user_id, rs = queue.get()
    print("User ", user_id, json.loads(rs.decode()))

cpu_end_time = time.process_time()
clock_end_time = time.time()

print("Took {0:.03}s [{1:.03}s]".format(cpu_end_time-cpu_start_time,   clock_end_time-clock_start_time))

rawDescription: Python Multiple requests

description: Python Multiple requests


code:
with futures.ProcessPoolExecutor(max_workers=6) as executor:
    # A dictionary which will contain a list the future info in the key, and the filename in the value
    jobs = {}

    # Loop through the files, and run the parse function for each file, sending the file-name to it.
    # The results of can come back in any order.
    files_left = len(files_list) #<----
    files_iter = iter(files_list) #<------

    while files_left:
        for this_file in files_iter:
            job = executor.submit(parse_function, this_file, **parser_variables)
            jobs[job] = this_file
            if len(jobs) > MAX_JOBS_IN_QUEUE:
                break #limit the job submission for now job

        # Get the completed jobs whenever they are done
        for job in futures.as_completed(jobs):

            files_left -= 1 #one down - many to go...   <---

            # Send the result of the file the job is based on (jobs[job]) and the job (job.result)
            results_list = job.result()
            this_file = jobs[job]

            # delete the result from the dict as we don't need to store it.
            del jobs[job]

            # post-processing (putting the results into a database)
            post_process(this_file, results_list)
            break; #give a chance to add more jobs <-----

rawDescription: Using Concurrent Futures without running out of RAM


code:
s1 = 'test'
s2 = ''
s3 = ''
' '.join(s for s in (s1,s2,s3) if s)

rawDescription: how to check multiple strings for emptyness in python

description: Check multiple strings for emptyness in python


code:
from multiprocessing.pool import ThreadPool
import subprocess

def work(sample):
    my_tool_subprocess = subprocess.Popen('mytool {}'.format(sample),shell=True, stdout=subprocess.PIPE)
    line = True
    while line:
        myline = my_tool_subprocess.stdout.readline()
        #here I parse stdout..

num = None  # set to the number of workers you want (it defaults to the cpu count of your machine)
tp = ThreadPool(num)
for sample in all_samples:
    tp.apply_async(work, (sample,))

tp.close()
tp.join()

rawDescription: Python multiple subprocess with a pool/queue recover output as soon as one finishes and launch next job in queue


code:
from django.db.models import Count
FoodTruck.objects.values_list('name').annotate(truck_count=Count('name')).order_by('-truck_count')

rawDescription: Django order by most frequent value

description: Django order by most frequent value


code:
import Tkinter as tk
from ScrolledText import ScrolledText

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        t = ScrolledText(self, wrap="word")
        t.insert("end", "Hello\nworld")
        t.configure(state="disabled")
        t.pack(side="top", fill="both", expand=True)

        # make sure the widget gets focus when clicked
        # on, to enable highlighting and copying to the
        # clipboard.
        t.bind("<1>", lambda event: t.focus_set())

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

rawDescription: Readonly tkinter text widget

description: Readonly tkinter text widget


code:
import csv
myHeaders = ["Header1", "Header2"]
csvData = []
with open( 'myfile.csv','r+b' ) as csvfile:
    # Read the data
    reader = csv.reader(csvfile, quoting=csv.QUOTE_NONE)
    writer = csv.writer(csvfile)
    writer.writerow(myHeaders)
    writer.writerows([row for row in reader])

rawDescription: How to include headers into a csv file

description: Include headers into a csv file


code: TData_Pre_Out = map(itemgetter(0, 7, 8), HDU_DATA)

rawDescription: Removing Indices from a list in Python

description: Removing Indices from a list in Python


code:
import win32gui
thetitle = win32gui.GetWindowText( hwnd )

rawDescription: how can I get the window name? [Python]

description: Get the window name? [Python]


rawDescription: Python List ordering

description: Python List ordering


code:
import ast

with open('file.txt') as f:
    data = ast.literal_eval(f.read())

rawDescription: Read the written list of dictionaries from file in Python


code:
import csv

matching20 = []

for fname in ('file1.csv', 'file2.csv', 'file3.csv'):
    with open(fname) as fin:
        csvin = csv.reader(fin)
        next(csvin) # <--- if you want to skip header row
        for row in csvin:
            if row[20] == 'value':
                matching20.append(row) # or do something with it here

rawDescription: Python: General CSV file parsing and manipulation


code:
APPNAME = "MyApp"
import sys
from os import path, environ
if sys.platform == 'darwin':
    from AppKit import NSSearchPathForDirectoriesInDomains
    # http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSSearchPathForDirectoriesInDomains
    # NSApplicationSupportDirectory = 14
    # NSUserDomainMask = 1
    # True for expanding the tilde into a fully qualified path
    appdata = path.join(NSSearchPathForDirectoriesInDomains(14, 1, True)[0], APPNAME)
elif sys.platform == 'win32':
    appdata = path.join(environ['APPDATA'], APPNAME)
else:
    appdata = path.expanduser(path.join("~", "." + APPNAME))

rawDescription: How do I store desktop application data in a cross platform way for python?

description: Store desktop application data in a cross platform way for python


code:
from datetime import datetime as dt
from datetime import timedelta as td
# using os.popen() to simulate the results of a HPUX shell 'date'...
# substitute the real HPUX shell date string in hpux_date
hpux_date = os.popen('date').read().strip()
hpux_dt = dt.strptime(hpux_date, '%a %b %d %H:%M:%S %Z %Y')
# Rounding to the nearest hour because there *will* be slight delay
# between shell string capture and python processing
offset_seconds = ((dt.utcnow() - hpux_dt).seconds//3600)*3600
hpux_gmt = hpux_dt + td(0,offset_seconds)
# Sanity checking to ensure we are correct...
hpux_gmt
hpux_dt
hpux_date

rawDescription: python - remote hosts timezone vs local

description: Python - remote hosts timezone vs local


rawDescription: Correctly reading text from Windows-1252(cp1252) file in python


code:
"""This part turned into a string to ease commenting out
if ...:
    # 500 lines

""" # end of block string.

rawDescription: Is there any way around Python's lack of block commenting?


code:
week_length = len(data_Week.keys())
if week_length > 1:
    tempAverage_Week = sum([x for x,y in data_Week.values()])/weekLength
    humAverage_Week = sum([y for x,y in data_Week.values()])/weekLength

rawDescription: Fastest way to get mean value from dictionary


code:
from subprocess import check_output

command = 'pdfkt %s dump_data_fields' % path
output = check_output(command, shell=True)
# do some processing here on output

rawDescription: How can i use PDFKt with django python

description: Use PDFKt with django python


code:
import os
import numpy as np

BASE_PATH = "/path/to/base"

for i in xrange(100):
    x = np.random.normal(0.0, 1.0, 100) #make the array to save
    file_name = "{0}.npz".format(i)
    np.savez( os.path.join(BASE_PATH, file_name), x )

rawDescription: How to choose where numpy.savez saves the files?

description: Choose where numpy.savez saves the files


code:
for j in range(set_count):
    tmp_accum = numpy.zeros(len(props[0]))
    for k in range(len(props)):
        ax1.bar(ind + j*b_width, props[k][set_label==j], width=b_width, color=color_sets[j][k], bottom=tmp_accum[set_label==j],label=labels[k])
        ax1.errorbar(ind + j*b_width +(1+k)*0.01, props[k][set_label==j]+tmp_accum[set_label==j],yerr= numpy.vstack([[0]*1, std_devs[k][j]]), ecolor="black",elinewidth =3)
        tmp_accum += props[k]

rawDescription: Place Matplotlib errorbars NOT in center of bar


rawDescription: Python creating an array from hex bytes

description: Python creating an array from hex bytes


code:
log('{}FUNC: {} ARGS: {}'.format('PID: {} '.format(args[0].pid)
                                 if args and hasattr(args[0], "pid") else '',
                                 func.__name__, args))

rawDescription: Check whether method is a class method and call attribute


rawDescription: Python dynamic class name creation

description: Python dynamic class name creation


code:
from datetime import datetime, timedelta

def parse_iso8601_datetime(dtstr, loose=False):
    """
    Convert ISO8601 datetime string and return Python datetime.datetime.
    Specify loose=True for more relaxed parsing accepting eg "YYYY-MM-DD" format.

    Raise ValueError on malformed input.
    """
    dt = None
    if len(dtstr) == 19:    # (eg '2010-05-07T23:12:51')
        dt = datetime.strptime(dtstr, "%Y-%m-%dT%H:%M:%S")
    elif len(dtstr) == 20:  # (eg '2010-05-07T23:12:51Z')
        dt = datetime.strptime(dtstr, "%Y-%m-%dT%H:%M:%SZ")
    elif len(dtstr) == 25:  # (eg '2010-05-07T23:12:51-08:00')
        dt = datetime.strptime(dtstr[0:19], "%Y-%m-%dT%H:%M:%S")
        tzofs = int(dtstr[19:22])
        dt = dt - timedelta(hours=tzofs)
    else:
        if loose:
            if len(dtstr) == 10:  # (eg '2010-05-07')
                dt = datetime.strptime(dtstr, "%Y-%m-%d")
        if not dt:
            raise ValueError("Invalid ISO8601 format: '%s'" % dtstr)
    return dt

rawDescription: Storing RSS pubDate in App Engine Datastore


code:
def list_diff_2(lst):
  return [(lst[i - 1], lst[i]) for i in range(1,len(lst)) if abs(lst[i] - lst[i-1]) == 2]

rawDescription: Traversing through a list and compare 2 elements in it


code:
import turtle

turtle.ht()

width = 800
height = 800
turtle.screensize(width, height)

##Definitions
def text(text, size, color, pos1, pos2):
     turtle.penup()
     turtle.goto(pos1, pos2)
     turtle.color(color)
     turtle.begin_fill()
     turtle.write(text, font=('Arial', size, 'normal'))
     turtle.end_fill()

def onTextClick(event):
    x, y = event.x, event.y
    print('x={}, y={}'.format(x, y))
    if (x >= 600 and x <= 800) and (  y >= 280 and y <= 300):
        turtle.onscreenclick(lambda x, y: turtle.bgcolor('red'))

##Screen
turtle.bgcolor('purple')
text('This is an example', 20, 'orange', 100, 100)

canvas = turtle.getcanvas()
canvas.bind('<Motion>', onTextClick)

turtle.done()

rawDescription: Python Turtle - Click Events

description: Python Turtle - Click Events


code:
def foo():
    a = 1
    b = 2
    return (a, b)

x, y = foo()  # x is now '1', and y is now '2'

rawDescription: Setting variable to only equal even numbers, and the other to odds


code:
from datetime import datetime
a = datetime.strptime(convdate, "%b %d %H:%M:%S %Y %Z")
b = datetime(1970, 1, 1)
(a-b).total_seconds()

rawDescription: Convert date to seconds

description: Convert date to seconds


code:
df
df.sortlevel(["Block", "conc"], ascending=[True, False], sort_remaining=False)

rawDescription: pandas pivot table descending order python

description: Pandas pivot table descending order python


code:
_here = os.path.dirname(os.path.abspath(__file__))

def create_key():
    filename = os.path.join(_here, 'passfile.txt')
    if os.path.isfile(filename):
        # exists
        with open(filename) as passfile:
            secret = passfile.read().strip()
    else:
        # create a new key
        secret = ''.join([str(randint(1, i + 8)) for i in range(8)])
        with open(filename, 'w') as passfile:
            secret = passfile.write(secret)

rawDescription: Python: Need a Function that Runs Only at First Time Startup


code:
def pull_marbles(sample, population=100):
  red = population / 2
  green = (population+1) / 2  # round up just to ensure red+green == population
  for i in range(sample):
    choice = random.randint(1, red + green)
    if choice <= red:  # red pulled
      red -= 1
    else:
      green -= 1
  return (red, green)

rawDescription: simulate pulling marbles from a bag without replacement (efficiently)

description: Simulate pulling marbles from a bag without replacement (efficiently)


code:
j=0
x=[]
for j in range(9):
   x = x + [str(j)]

rawDescription: problem with lists?

description: Problem with lists


code:
from skimage import io, color, morphology
from scipy.signal import convolve2d
import numpy as np
import matplotlib.pyplot as plt

img = color.rgb2gray(io.imread('6EnOn.png'))

# Reduce all lines to one pixel thickness
snakes = morphology.skeletonize(img < 1)

# Find pixels with only one neighbor
corners = convolve2d(snakes, [[1, 1, 1],
                              [1, 0, 1],
                              [1, 1, 1]], mode='same') == 1
corners = corners & snakes

# Those are the start and end positions of the segments
y, x = np.where(corners)

plt.imshow(img, cmap=plt.cm.gray, interpolation='nearest')
plt.scatter(x, y)
plt.axis('off')
plt.show()

rawDescription: Python image processing : Help needed for corner detection in preferably PIL or any relevant module


code:
def get_context_data(self, **kwargs):
    context = super(PointView, self).get_context_data(**kwargs)
    context['all_areas'] = Area.objects.exclude(title=self.kwargs['area_title'])
    return context

rawDescription: Get all objects again after passing a filtered list to template in django


code:
with open('data.txt') as f,open('out.txt') as f2:
    for x in f:
        if 'are' not in x:
            f2.write(x.strip()+'\n')  #strip the line first and then add a '\n',
                                      #so now you'll not get a empty line between two lines

rawDescription: how do i delete a line that contains a certain string?

description: Delete a line that contains a certain string


code:
js_code = '''
          <script type="text/javascript">
          google.load("visualization", "1", {packages:["corechart"]});
          google.setOnLoadCallback(drawChart);
          function drawChart() {
          var data = google.visualization.arrayToDataTable(''' + json.dumps(array1) + ''');
          var chart = new google.visualization.LineChart(document.getElementById("casestrend"));chart.draw(data);}
          </script>
          '''

rawDescription: How to insert array variable from Python to Javascript

description: Insert array variable from Python to Javascript


code:
@login_manager.request_loader
def loader(request):
    identifier = get_identifier_from_request(request)
    exist = check_on_cas_server(identifier)
    if not exist:
        return None
    user = UserMixin()
    user.id = get_specified_or_random_id(identifier, exist)
    return user

rawDescription: Flask logins without persistence

description: Flask logins without persistence


code:
password = '123'
with settings(prompts={
                  'Enter new UNIX password: ': password,
                  'Retype new UNIX password: ': password
              }):
    run('passwd {}'.format(username))

rawDescription: Python fabric capture interactive dialog output


rawDescription: Maximum and Minimum Value of a Dictonary Using Python


code:
import csv
from collections import defaultdict

threshold = 10E-20

data = defaultdict(dict)
with open('path/to/file') as infile:
    for contig, _ignore, e, _id, nuc in csv.reader(infile, delimiter='\t'):
        contig = int(contig.split('-')[1])
        e = float(e)
        if e < threshold: continue
        data[contig][e] = nuc
        if len(data[contig]) > 3: data[contig].pop(min(data[contig]))

for contig,d in data.items():
    for e in sorted(d):
        print(contig, e, d[e])

rawDescription: Filtering tsv file to get top 3 occurrences based on a column value


rawDescription: dot dashed line touch yaxis min and max in matplotlib

description: Dot dashed line touch yaxis min and max in matplotlib


code:
def login():
    if current_user.is_authenticated():
        return redirect(url_for('index'))
    # process login for anon user

rawDescription: Skip Login Page if Session Exists

description: Skip Login Page if Session Exists


code:
class Yoga():
    def __init__(self,weekday,name,teacher,description):
        self.weekday=weekday
        self.name=name
        self.teacher=teacher
        self.description=description
    def __str__(self):
        return "%s yoga with %s %s" % (self.name,self.teacher,self.description)
    def __repr__(self):
        return str(self)

# just make up some data to work with
yoga_classes = (Yoga(1,"dynamic","mary","6pm"),
                Yoga(1,"soft","susie","8pm"),
                Yoga(3,"hatha","bob","5pm"),
                Yoga(3,"hot","alice","7pm"))

daylookup = (None,"Monday","Tuesday","Wednesday",
            "Thursday","Friday","Saturday","Sunday")

import itertools
def get_classes():
    weekly_classes = []
    ycsorted = sorted(yoga_classes,key=lambda x: x.weekday)
    for k,g in itertools.groupby(ycsorted,key=lambda x: x.weekday):
        weekly_classes.append((daylookup[k],tuple(g)))
    return weekly_classes

import pprint
pprint.pprint(get_classes())

rawDescription: Issue with creating nested lists

description: Issue with creating nested lists


code:
import matplotlib.path as mplPath
def is_in_zone(quadri,point):
    bbPath = mplPath.Path(quadri)
    return bbPath.contains_point(point)

def get_zone_nbr(dictio,trip):
    start_zone=-1
    end_zone=-1
    trip_ref=-1
    for key,coordinates in dictio.iteritems():
        if is_in_zone(coordinates,trip[0]):
            start_zone=key
        if is_in_zone(coordinates,trip[1]):
            end_zone=key
        if start_zone>-1 and end_zone>-1:
            trip_ref=len(dictio)*start_zone+end_zone
            break
    return (start_zone,end_zone,trip_ref)

dictionary={0:((0,0),(0,1),(1,1),(1,0)),1:((3,3),(3,4),(4,4),(4,3))}
trips=(((0.5,0.5),(3.5,3.5)),((-1,-1),(-2,-2)))

from pathos.pools import ThreadPool
pool = ThreadPool()

results = pool.map(lambda x: get_zone_nbr(dictionary, x), trips)
results

rawDescription: Multiprocessing in for loop

description: Multiprocessing in for loop


code:
import os
import subprocess

cwd = os.getcwd()

for i in os.listdir(cwd):
    if i.endswith('.ttp'):
        o = i + "-out"
        p = subprocess.call(["program_name", "-input", i, "-output", o])

rawDescription: Calling a non python program from python?


code:
for cell in row:
    cell_value = cell.value
    # etc

rawDescription: Extracting values only from the value of excel row recived using xlrd -python


rawDescription: How to create small dict from another dict with array of keys?

description: Create small dict from another dict with array of keys


code:
MyFile1 = open("A-L.txt", "wt")
MyFile2 = open("M-Z.txt", "wt")

Surname = input("What is your surname");
if Surname[0] in ["A","B","C","D","E","F","G","H","I","J","K","L"]:
    MyFile1.write(Surname + "\n")
if Surname[0] in ["M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]:
    MyFile2.write(Surname + "\n")

rawDescription: How do i save text into 2 different files?

description: Save text into 2 different files


code: del os.environ['MYVAR']

rawDescription: What is the correct way to unset a linux environment variable in python?


code:
import os
f = open('/root/names.txt')
line = f.readline()
f.close()
words= line.split()

for word in words:
    cmd1 = 'svn up ' +word
    if os.system(cmd1) == 0:
        print('All files updated')

rawDescription: Commit file in svn with python3

description: Commit file in svn with python3


code:
d_teams=pd.DataFrame( [[int(y) for y in x.split('_')[1:]] \
            for x in df1.match_up], columns=('team_A', 'team_B') )
merged=pd.concat((df1,d_teams),axis=1)
df2i=df2.set_index('team')
merged['win_A']=df2i.ix[merged.team_A].reset_index().win_A_B
merged['win_B']=df2i.ix[merged.team_B].reset_index().win_A_B

rawDescription: How to merge 2 complicated data frames in python pandas?

description: Merge 2 complicated data frames in python pandas


code:
with open('data.txt', mode="r") as data_file:
    fieldnames = ('character', 'string', 'value1', 'value2')
    reader = csv.DictReader(data_file, fieldnames=fieldnames, delimiter="\t")

    complete_dict = {}
    for row in reader:
        char_dict = complete_dict.setdefault(row['character'], {})
        values_list = char_dict.setdefault(row['string'], [])
        values = [int(row['value1']), float(row['value2'])]
        values_list.append(values)

pprint.pprint(complete_dict)

rawDescription: How to create complex dictionary structure in Python?

description: Create complex dictionary structure in Python


code:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from tutorial.items import CraigslistSampleItem

class MySpider(CrawlSpider):
    name = "craigs"
    allowed_domains = ["sfbay.craigslist.org"]
    start_urls = ["http://sfbay.craigslist.org/"]

    rules = (
        Rule(SgmlLinkExtractor(allow=("index\d00\.html")), callback="parse_items_2", follow= True),
        Rule(SgmlLinkExtractor(allow=(r'sfbay.craigslist.org/npo')), callback="parse_items_1", follow= True),
        )

    def parse_items_1(self, response):
        items = []
        hxs = HtmlXPathSelector(response)
        titles = hxs.select("//div")
        for title in titles:
            item = CraigslistSampleItem()
            item ["title"] = title.select("//li/a/text()").extract()
            item ["link"] = title.select("//li/a/@href").extract()
            print ('**parse-items_1:', item["title"])
            items.append(item)
        return items

    def parse_items_2(self, response):
        hxs = HtmlXPathSelector(response)
        titles = hxs.select("//p")
        items = []
        for title in titles:
            item = CraigslistSampleItem()
            item ["title"] = title.select("a/text()").extract()
            item ["link"] = title.select("a/@href").extract()
            print ('**parse_items_2:', item["title"], item["link"])
            items.append(item)
        return items

rawDescription: Scrapy: Crawled but not scraped

description: Scrapy: Crawled but not scraped


code:
from email.header import decode_header
bytes, encoding = decode_header('=?ISO-8859-1?Q?Certificado=5FZonificaci=F3n=5F2010=2Epdf?=')[0]
bytes.decode(encoding)

rawDescription: ISO 8859-1 filename not decoding

description: ISO 8859-1 filename not decoding


code:
df = pd.DataFrame(np.random.rand(100000),columns=['A'],
                  index=pd.date_range("Jan 1 2013", freq="H", periods=100000))
df.iloc[1:99999:3] = np.nan

upper_np = df.resample('1A',how=lambda x: np.percentile(x,q=75))
upper_np.describe()
upper_pd = df.resample('1A',how=lambda x: x.quantile(0.75))
upper_pd.describe()

rawDescription: pandas.DataFrame.describe() vs numpy.percentile() NaN handling

description: Pandas.DataFrame.describe() vs numpy.percentile() NaN handling


code:
import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        canvas = tk.Canvas(self, width=800, height=500)
        canvas.pack(side="top", fill="both", expand=True)
        canvas_id = canvas.create_text(10, 10, anchor="nw")

        canvas.itemconfig(canvas_id, text="this is the text")
        canvas.insert(canvas_id, 12, "new ")

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

rawDescription: Python, Tkinter - Inserting text into canvas windows


code:
import os
import zipfile
import StringIO

from django.http import HttpResponse

def getfiles(request):
    # Files (local path) to put in the .zip
    # FIXME: Change this (get paths from DB etc)
    filenames = ["/tmp/file1.txt", "/tmp/file2.txt"]

    # Folder name in ZIP archive which contains the above files
    # E.g [thearchive.zip]/somefiles/file2.txt
    # FIXME: Set this to something better
    zip_subdir = "somefiles"
    zip_filename = "%s.zip" % zip_subdir

    # Open StringIO to grab in-memory ZIP contents
    s = StringIO.StringIO()

    # The zip compressor
    zf = zipfile.ZipFile(s, "w")

    for fpath in filenames:
        # Calculate path for file in zip
        fdir, fname = os.path.split(fpath)
        zip_path = os.path.join(zip_subdir, fname)

        # Add file, at correct path
        zf.write(fpath, zip_path)

    # Must close zip for all contents to be written
    zf.close()

    # Grab ZIP file from in-memory, make response with correct MIME-type
    resp = HttpResponse(s.getvalue(), mimetype = "application/x-zip-compressed")
    # ..and correct content-disposition
    resp['Content-Disposition'] = 'attachment; filename=%s' % zip_filename

    return resp

rawDescription: Django - Create A Zip of Multiple Files and Make It Downloadable


code:
import urllib.parse
word = 'می‌باشد'
urllib.parse.quote(word, encoding='utf-8')

rawDescription: Converting non English words to % separated string in Python


code:
lengths = {}
with open(mfile) as csvfile:
    test = csv.reader(csvfile, dialect='excel-tab')
    for row in test:
        for colno, col in enumerate(row):
            lengths[colno] = max(len(col), lengths.get(colno, 0))

rawDescription: Python3.4 - enumeration through columns then rows in csv to obtain Max lengths


code:
class MyView(CreateView):
    def get(self, request, *args, **kwargs):
        self.request.session.set_test_cookie()
        super(MyView, self).get(request, *args, **kwargs)

    def form_valid(self, form):
        if not self.request.session.test_cookie_worked():
            pass
        else:
            pass

rawDescription: Setting test cookie in generic view in Django


code: plot_date(df.index, df.data)

rawDescription: Pandas scatter plot

description: Pandas scatter plot


code:
import numpy as np
np.logspace(-10, -14, 50)

rawDescription: Error using numpy.logspace() : how to generate numbers spaced evenly on a log-scale


code:
list1 = ['a', 'y', 'k', 'x', 'd', 'l']
sorted_list = sorted(list1)
new_list = sorted_list[0:2] + sorted_list[-2:]
new_list

rawDescription: Python: How to create new list with certain alphabetized items from original list?


code:
from itertools import count
counter = lambda c=count(): next(c)

rawDescription: Simple number generator

description: Simple number generator


code:
def parent_to_child(parent):
    newchild= child()
    newchild.property= parent.property
    #...

list_of_children= map(parent_to_child, list_of_parents)

rawDescription: Convert all objects in a list to a child class.


code:
for row in table:
    row[1:] = [int(c) for c in row[1:]]

rawDescription: Most elegant way to modify elements of nested lists in place


code:
from Crypto.Protocol.KDF import PBKDF2
from Crypto.PublicKey import RSA

password = "swordfish"   # for testing
salt = "yourAppName"     # replace with random salt if you can store one

master_key = PBKDF2(password, salt, count=10000)  # bigger count = better

def my_rand(n):
    # kluge: use PBKDF2 with count=1 and incrementing salt as deterministic PRNG
    my_rand.counter += 1
    return PBKDF2(master_key, "my_rand:%d" % my_rand.counter, dkLen=n, count=1)

my_rand.counter = 0
RSA_key = RSA.generate(2048, randfunc=my_rand)

rawDescription: Making RSA keys from a password in python


code:
class MyModelA(models.Model):
    my_int = models.IntegerField()
    created_ts = models.DateTimeField()

    # A method that will fetch the most recent related MyModelC
    def get_latest_c(self):
        from mymodelc.models import MyModelC
        return MyModelC.objects.filter(my_b__my_a=self).latest("created_ts")

rawDescription: How can I follow a Django reverse-foreign-key and then another reverse-foreign-key?

description: Follow a Django reverse-foreign-key and then another reverse-foreign-key


code:
s = "[virus 1 [isolated from china]]"
s.partition('[')[-1].rpartition(']')[0]

rawDescription: How do I find the string between two special characters?

description: Find the string between two special characters


code:
def shell_script(appname, *commands):
        workspace = tempfile.mkdtemp(prefix=appname + '-')
        installer = open(workspace + "/installer.sh", 'w')

        installer.write("#!/bin/bash\n")
        installer.write("cd " + workspace + "\n")
        for line in commands:
                installer.write(line + "\n")
        ShellCommand("chmod u+x " + installer.name)
        installer.close()
        ShellCommand(installer.name)

rawDescription: Maintaining environment state between subprocess.Popen commands?


rawDescription: related to list and file handling?

description: Related to list and file handling


code:
fig = plt.figure()
plt.xlim(0,54)
plt.ylim(0,8)
plt.axes().set_aspect('equal')
plt.show()

rawDescription: Create plot in matplotlib with appropriately sized axis


code:
n = 100000
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

x = np.random.standard_normal(n)
img = plt.imread("soccer.jpg")
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)

red_high = ((0., 0., 0.),
         (.3, .5, 0.5),
         (1., 1., 1.))

blue_middle = ((0., .2, .2),
         (.3, .5, .5),
         (.8, .2, .2),
         (1., .1, .1))

green_none = ((0,0,0),(1,0,0))

cdict3 = {'red':  red_high,

     'green': green_none,

     'blue': blue_middle,

     'alpha': ((0.0, 0.0, 0.0),
               (0.3, 0.5, 0.5),
               (1.0, 1.0, 1.0))
    }

dropout_high = LinearSegmentedColormap('Dropout', cdict3)
plt.register_cmap(cmap = dropout_high)

plt.hexbin(x,y, bins='log', cmap=dropout_high)
plt.imshow(img,zorder=0, extent=[-10, 10, -20, 20])
plt.show()
#plt.savefig('map.png')

rawDescription: How to fade color to transparent using hexbin and matplotlib?

description: Fade color to transparent using hexbin and matplotlib