code:
try:
    next
except NameError:       # doesn't show up until python 2.6
    def next(iter):
        return iter.next()

Empty = object()

class Boolean_Iterator(object):
    """Adds the abilities
    True/False tests:  True means there /may/ be items still remaining to be used
    """
    def __init__(self, iterator):
        self._iter = iter(iterator)
        self._alive = True
    def __iter__(self):
        return self
    def __next__(self):
        try:
            result = next(self._iter)
        except StopIteration:
            self._alive = False
            raise
        return result
    next = __next__                     # python 2.x
    def __bool__(self):
        return self._alive
    __nonzero__ = __bool__              # python 2.x

rawDescription: How can I get generators/iterators to evaluate as False when exhausted?

description: Get generators/iterators to evaluate as False when exhausted


code:
year = int(date_string[:4])
month = int(date_string[4:6])
day = int(date_string[6:8])
hour = int(date_string[8:10])
minute = int(date_string[10:12])
sec = int(date_string[12:14])
msec = int(date_string[14:] + '000')

float_days = date2num(datetime.datetime(year, month, day, hour, minute, sec, msec))

rawDescription: Converting date to float days format for Matplotlib


rawDescription: Floating point decimals in python

description: Floating point decimals in python


code:
import numpy as np

# create your matrix. If it's not yet a numpy array, make it one
ar = np.array(matrix)
indices = np.tril_indices_from(ar)

rawDescription: fast way to get the indices of a lower triangular matrix as 1 dimensional list in python

description: Fast way to get the indices of a lower triangular matrix as 1 dimensional list in python


rawDescription: Display a decimal in scientific notation


code: dict((x[0], x) for x in L).values()

rawDescription: Removing duplicates from list of lists in Python


code: best = deepcopy(board)

rawDescription: Python global variable changes itself.... Why?


code:
import random

multi_players = []
multi_players_pos = []

player = pygame.image.load("player_two.png").convert()
player.set_colorkey(white)

#create 10 players 0...9
for i in range(10):
    multi_players.append(player)
    pos = [random.randint(60,220+1), random.randint( 0, 475+1)]
    multi_players_pos.append(pos)

rawDescription: Pygame display multiple images randomly

description: Pygame display multiple images randomly


code: '  -- Checking connectivity from 1:2:3 to 4:5:6'

rawDescription: How can I print each list item, in a single line of code?

description: Print each list item, in a single line of code


rawDescription: How to calculate the intercept using numpy.linalg.lstsq

description: Calculate the intercept using numpy.linalg.lstsq


code:
print(politicians)
[(u'Courtney, John', u'Dem)'),
 (u'Clinton, Hilary', u'Dem)'),
 (u'Lee, Kevin', u'Rep)')]

rawDescription: web scraping using beautifulsoup: separating values

description: Web scraping using beautifulsoup: separating values


code:
cursor.execute("SELECT * FROM mytable")
rows = cursor.fetchall()
for row in rows:
   do_something_with(row)

rawDescription: Retrieving Data from MySQL in batches via Python


code:
import script1
import script2

if __name__ == '__main__':
    filename = script1.run(sys.args)
    script2.run(filename)

rawDescription: store return value of a Python script in a bash script

description: Store return value of a Python script in a bash script


code:
def determine_points(lst, new):
  # lst is a list of tuples with movies, ranked by how good the movie is
  # new is a tuple with new movie
  # the new movies position is to be determined

  new_title, new_genre = new
  at_most = len(lst)
  at_least = 0
  while at_least < at_most:
    center = (at_least + at_most) // 2
    title, genre = lst[center]
    os.system("clear")
    competition_strings = [new_title, new_genre, "\n" * 10, title, genre]
    print("\n".join(x.center(150) for x in competition_strings))
    c = getch()
    if c == "j": # new movie is worse than this
      at_most = center
    else: # new movie is better than this
      at_least = center + 1
  return at_least

rawDescription: Correct implementation of determining position in the list


code: [x for x in content if all(c in x for c in char)]

rawDescription: Python : Removing a List from List of List?


rawDescription: Python append list in loop

description: Python append list in loop


code:
def bbox_size(bbox):
    # return tuple with sizes (dx, dy, dz)
    return (bbox[1] - bbox[0], bbox[3] - bbox[2], bbox[5] - bbox[4])

def fits_bbox(bigbox, smallbox):
    bdx, bdy, bdz = bbox_size(bigbox)
    sdx, sdy, sdz = bbox_size(smallbox)
    # return True if small box fits in bigbox in all three dimensions
    return (sdx<bdx and sdy<bdy and sdz<bdz)

rawDescription: simple python algorithm needed in finding bounding box of meshes from an OBJ file

description: Simple python algorithm needed in finding bounding box of meshes from an OBJ file


code:
from sqlalchemy import inspect
from sqlalchemy.ext.declarative.clsregistry import _ModuleMarker
from sqlalchemy.orm import RelationshipProperty

logger = logging.getLogger(__name__)

def is_sane_database(Base, session):
    """Check whether the current database matches the models declared in model base.

    Currently we check that all tables exist with all columns. What is not checked

    * Column types are not verified

    * Relationships are not verified at all (TODO)

    :param Base: Declarative Base for SQLAlchemy models to check

    :param session: SQLAlchemy session bound to an engine

    :return: True if all declared models have corresponding tables and columns.
    """

    engine = session.get_bind()
    iengine = inspect(engine)

    errors = False

    tables = iengine.get_table_names()

    # Go through all SQLAlchemy models
    for name, klass in Base._decl_class_registry.items():

        if isinstance(klass, _ModuleMarker):
            # Not a model
            continue

        table = klass.__tablename__
        if table in tables:
            # Check all columns are found
            # Looks like [{'default': "nextval('sanity_check_test_id_seq'::regclass)", 'autoincrement': True, 'nullable': False, 'type': INTEGER(), 'name': 'id'}]

            columns = [c["name"] for c in iengine.get_columns(table)]
            mapper = inspect(klass)

            for column_prop in mapper.attrs:
                if isinstance(column_prop, RelationshipProperty):
                    # TODO: Add sanity checks for relations
                    pass
                else:
                    for column in column_prop.columns:
                        # Assume normal flat column
                        if not column.key in columns:
                            logger.error("Model %s declares column %s which does not exist in database %s", klass, column.key, engine)
                            errors = True
        else:
            logger.error("Model %s declares table %s which does not exist in database %s", klass, table, engine)
            errors = True

    return not errors

rawDescription: Check database schema matches SQLAlchemy models on application startup


code:
uid_to_foo = {foo.uid: foo for foo in foos}

# use 'uid_to_foo[some_foo.uid]' to find an instance fast

rawDescription: Find item in set via hash

description: Find item in set via hash


code:
urlpatterns = i18n_patterns('',
 url(r'^admin/', include(admin.site.urls)),
 url(r'^', include('cms.urls')),
)

rawDescription: How to remove language from the address?

description: Remove language from the address


code: return servers,msg

rawDescription: Can sphinx napoleon document function returning multiple arguments?


code:
def split_special(subject):
    return re.split(r"""
        \|           # Match |
        (?!          # only if it's not possible to match...
         (?:         # the following non-capturing group:
          (?!\[\[)   # that doesn't contain two square brackets
          .          # but may otherwise contain any character
         )*          # any number of times,
         \]\]        # followed by ]]
        )            # End of first loohahead. Now the same thing for braces:
        (?!(?:(?!\{\{).)*\}\})""",
        subject, flags=re.VERBOSE)

rawDescription: Split string into list if separator is not enclosed


code:
file = open('data.pkl', 'wb')

# Pickle dictionary using protocol 0.
pickle.dump(image, file)
file.close()

rawDescription: pickling a python image object

description: Pickling a python image object


code:
from pyspark.sql.functions import col, explode, sum as sum_

(df
  .select(explode(col("myMapColumn")))
  .groupBy(col("key").alias("id"))
  .agg(sum_("value._1").alias("count"), sum_("value._2").alias("revenue")))

rawDescription: PySpark Dataframe.groupBy MapType column


code: {'the': 'last', 'the': 'decrease'}

rawDescription: How to convert text into a dictionary in Python

description: Convert text into a dictionary in Python


rawDescription: Iterating over dictionary keys in python after a certain key in python


code:
import pandas as pd
df = pd.DataFrame(
    {'id': range(1, 9),
     'code': ['one', 'one', 'two', 'three',
              'two', 'three', 'one', 'two'],
     'colour': ['black', 'white', 'white', 'white',
                'black', 'black', 'white', 'white'],
     'texture': ['soft', 'soft', 'hard', 'soft', 'hard',
                 'hard', 'hard', 'hard'],
     'shape': ['round', 'triangular', 'triangular', 'triangular', 'square',
               'triangular', 'round', 'triangular']
     }, columns=['id', 'code', 'colour', 'texture', 'shape'])
multi_df = df.set_index(
    ['code', 'colour', 'texture', 'shape']).sort_index()['id']

# define the list of indices that I want to look up for in multi_df
look_up_list = [('two', 'white', 'hard', 'triangular'), (
    'five', 'black', 'hard', 'square'), ('four', 'black', 'hard', 'round')]

subdf = multi_df.loc[look_up_list].dropna()
print(subdf ** 2)

rawDescription: Optimising Pandas multi-index lookup

description: Optimising Pandas multi-index lookup


rawDescription: Initialize empty list with same shape as array


code:
def subtime(a,b):
    """ (datetime,int) -> datetime
    Subtract b hours from a datetime.datetime and return the new datetime object

    >>> subtime(datetime.datetime(2013,11,11,11,0),10)
    datetime.datetime(2013, 11, 11, 1, 0)

    >>> subtime(datetime.datetime(2013,11,11,11,0),24)
    datetime.datetime(2013, 11, 10, 11, 0)

    >>> subtime(datetime.datetime(2013,11,11,11,0),30)
    datetime.datetime(2013, 11, 10, 5, 0)

    >>> subtime(datetime.datetime(2013,11,11,11,0),0)
    datetime.datetime(2013, 11, 11, 11, 0)

    >>> subtime(datetime.datetime(2013,11,11,11,0),-5)
    datetime.datetime(2013, 11, 11, 16, 0)

    """
    subtract = datetime.timedelta(hours=b)
    difference = now - subtract
    return difference

rawDescription: Doctest with datetime

description: Doctest with datetime


code: {'path/to/list?date=05222014&type=cal': ['date=some_string', 'type=some_string'],    'path/to/somewhere?id=6&name=Test': ['id=some_string', 'name=some_string']}

rawDescription: Replace resulting values with python split method


code: my_scores.sort(key=lambda row: row[1:], reverse=True)

rawDescription: Sort a Matrix by row preserving the columns


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

gps_track = np.array([[0,0],[1.2,2.3],[1.9,3],[3.2,4.3],[4,2.9],[6.5,3.1]])
x = gps_track[:,0]
y = gps_track[:,1]

binsize = 1.0
xmin, xmax = x.min(), x.max()
ymin, ymax = y.min(), y.max()
xi = np.arange(xmin, xmax+binsize, binsize)
yi = np.arange(ymin, ymax+binsize, binsize)

N = 1000
tckp, u = interpolate.splprep([x, y], s=0, k=2, nest=-1)
xx, yy = interpolate.splev(np.linspace(0, 1, N), tckp)

# find integer indices where the xx fall between xi values
x_idx = np.searchsorted(xi, xx)
# find where the x_idx indices change. This is where the curve crosses a grid line
x_mask = np.diff(x_idx) != 0
# do the same for y
y_idx = np.searchsorted(yi, yy)
y_mask = np.diff(y_idx) != 0

fig, ax = plt.subplots()
line, = ax.plot(xx, yy)
ax.scatter(xx[x_mask], yy[x_mask], color='red')
ax.scatter(xx[y_mask], yy[y_mask], color='red')

# make sure the grid lines fall on xi and yi
ax.set_xticks(xi)
ax.set_yticks(yi)
ax.grid()
plt.show()

rawDescription: How to coarse grain xy coordinates

description: Coarse grain xy coordinates


code:
@contextlib.contextmanager
def make_temp_directory():
    temp_dir = tempfile.mkdtemp()
    yield temp_dir
    shutil.rmtree(temp_dir)

rawDescription: Right way to clean up a temporary folder in Python class


code: [['otis', 'milo', 'laurel', 'dog', 'hardy'], ['cat', 'bob', 'joe']]

rawDescription: How to check if a key/value is repeated elsewhere in a dictionary using Python

description: Check if a key/value is repeated elsewhere in a dictionary using Python


code:
correct = 0
for index in range(shortest_length):
    if correctanswer[index] == userinput[index]:
        correct += 1

rawDescription: How to compare words in two lists according to same order in python?(I have def a function)

description: Compare words in two lists according to same order in python?(I have def a function)


code:
def init_model(engine):
    sm = orm.sessionmaker(autoflush=False, autocommit=False, expire_on_commit=False, bind=engine)
    meta.engine = engine
    meta.Session = orm.scoped_session(sm)

def index(self):
    rows = Session.execute('SELECT ...')

rawDescription: Pylons: Sharing SQLAlchemy MySQL connection with external library


code:
people = [
    {"age": 22, "first": "John", "last": "Smith"},
    {"age": 22, "first": "Jane", "last": "Doe"},
    {"age": 41, "first": "Brian", "last": "Johnson"},
]
people_by_age = {}
for person in people:
    age = person.pop('age')
    people_by_age.setdefault(age, []).append(person)

people_by_age
from pprint import pprint
pprint(people_by_age)

rawDescription: Map a list of dicts into a dict of lists of dicts


rawDescription: Apostrophe within Python lookbehind assertion


code:
memberFolder = context.unrestrictedTraverse('/root/Members/' + myname)
contents = memberFolder.getFolderContents()

rawDescription: Plone - getting and listing objects from member folder in a view


code:
def decorator(setup, teardown):
    def decorate_function(test):
        def wrapper(self):
            setup(self)
            test(self)
            teardown(self)
        return wrapper
    return decorate_function

rawDescription: Python decorators calling functions in different classes


code:
def wrap(datum):
  # don't wrap strings
  if isinstance(datum, basestring):
    return datum
  # don't wrap numbers, either
  try: return datum + 0
  except TypeError: pass
  return Fourie(datum)

class Fourie(object):
  def __init__(self, data):
    self._data = data
  def __getattr__(self, n):
    return wrap(self._data[n])
  def __call__(self, n):
    return wrap(self._data[n])

rawDescription: PyYAML parse into arbitary object

description: PyYAML parse into arbitary object


code:
import kyotocabinet
import collections
import heapq

class IterableCursor(kyotocabinet.Cursor, collections.Iterator):
    def __init__(self, *args, **kwargs):
        kyotocabinet.Cursor.__init__(self, *args, **kwargs)
        collections.Iterator.__init__(self)

    def next():
        "Return (key,value) pair"
        res = self.get(True)
        if res is None:
            raise StopIteration
        else:
            return res

def orderedMerge(*iterables, **kwargs):
    """Take a list of ordered iterables; return as a single ordered generator.

    @param key:     function, for each item return key value
                    (Hint: to sort descending, return negated key value)

    @param unique:  boolean, return only first occurrence for each key value?
    """
    key     = kwargs.get('key', (lambda x: x))
    unique  = kwargs.get('unique', False)

    _heapify       = heapq.heapify
    _heapreplace   = heapq.heapreplace
    _heappop       = heapq.heappop
    _StopIteration = StopIteration

    # preprocess iterators as heapqueue
    h = []
    for itnum, it in enumerate(map(iter, iterables)):
        try:
            next  = it.next
            data   = next()
            keyval = key(data)
            h.append([keyval, itnum, data, next])
        except _StopIteration:
            pass
    _heapify(h)

    # process iterators in ascending key order
    oldkeyval = None
    while True:
        try:
            while True:
                keyval, itnum, data, next = s = h[0]  # get smallest-key value
                                                      # raises IndexError when h is empty
                # if unique, skip duplicate keys
                if unique and keyval==oldkeyval:
                    pass
                else:
                    yield data
                    oldkeyval = keyval

                # load replacement value from same iterator
                s[2] = data = next()        # raises StopIteration when exhausted
                s[0] = key(data)
                _heapreplace(h, s)          # restore heap condition
        except _StopIteration:
            _heappop(h)                     # remove empty iterator
        except IndexError:
            return

rawDescription: merge join two generators in python

description: Merge join two generators in python


rawDescription: Moving each value of a text file to its own line in Python


code:
def score(n):
    return 'Score from [%s] is %d' % (__name__, n)

rawDescription: How to import many modules to main and execute

description: Import many modules to main and execute


rawDescription: ReferenceProperty filter

description: ReferenceProperty filter


code:
import  wx

class Frame(wx.Frame):
  def __init__(self, parent, title):
    wx.Frame.__init__(self, parent=parent, id=wx.ID_ANY, title=title,
                style=(wx.DEFAULT_FRAME_STYLE | wx.WS_EX_CONTEXTHELP) ,
                pos=(20, 20))
    self.SetExtraStyle(wx.FRAME_EX_CONTEXTHELP)
    self.CreateStatusBar()
    self.createOtherStuffHere()
    self.Show()

  def createOtherStuffHere(self):
    panel = wx.Panel(self)
    panel.SetHelpText("This is a wx.Panel.")

    self.label = wx.StaticText(panel, style=wx.WS_EX_CONTEXTHELP, label="Click me I may provide some help?", size=(200,30))
    self.label.SetHelpText("This is the help though not so helpful!")

    self.edit = wx.TextCtrl(panel, pos=(20,50))
    self.edit.SetHelpText("i am a edit box")

    self.helpButton = wx.ContextHelpButton(panel, pos=(20,100))

provider = wx.SimpleHelpProvider()
wx.HelpProvider_Set(provider)

app = wx.PySimpleApp()
frame = Frame(None, "Test")
app.SetTopWindow(frame)
app.MainLoop()

rawDescription: wxPython: Context dialog popup

description: WxPython: Context dialog popup


rawDescription: Proper way to access items in FlexGridSizer in wxPython?


rawDescription: Python pandas group by aggregate statement print


code:
@memo
def memoized_func(some_arg):
    return some_arg ** 2

rawDescription: Nesting functions inside of functions

description: Nesting functions inside of functions


code:
def readFile(infilepath):
    answer = {}
    with open(infilepath) as infile:
        for lastname in infile:
            lastname = lastname.strip()
            firstname = infile.readline().strip()
            year = infile.readline().strip()
            medal = infile.readline().strip()

            if year not in answer:
                answer[year] = {}
            if lastname not in answer[year]:
                answer[year][lastname] = {}
            if firstname not in answer[year][lastname]:
                answer[year][lastname][firstname] = {}
            if medal not in answer[year][lastname][firstname]:
                answer[year][lastname][firstname][medal] = 0
            answer[year][lastname][firstname][medal] += 1
    return answer

def count(medalcounts):
    year = input("What year would you like to check: ").strip()
    answer = 0
    for lastname in medalcounts[year]:
        for firstname,medals in medalcounts[year][lastname].items():
            answer += sum(medals.values())
    print(answer, "medals were awarded in", year)

    firstname = input("Enter first name: ").strip()
    lastname = input("Enter last name: ").strip()
    for year,yeard in medalcounts.items():
        medals = yeard[lastname][firstname]
        for med,count in medals.items():
            print("In year", year, firstname, lastname, "won", count, '. gold silver bronze'.split()[med], "medals")

rawDescription: Python txt file- Reading and using information in file


code:
fin = open('filename')
tableau = ...
fin.close()

rawDescription: How to use "While()" in python

description: Use "While()" in python


code:
from datetime import datetime, timedelta
...

rawDescription: Check if file line sttime is older than 2 days - Python


code:
with open("/path/to/new/file", "w") as newfile:   # Create and open the new file
    with open("/path/to/old/file") as oldfile:    # Open the old file
        oldfile = oldfile.readlines()             # Read the lines of the old file into a list
        newfile.write(oldfile[0])                 # Write the column names to the new file
        for line in oldfile[1:]:                  # Iterate through the lines, skipping the first (which we already wrote to the new file)
            line = line.split(",")                # Split the line by commas
            line[3] = line[3].upper()             # Make the Varsity column value uppercase
            newfile.write(",".join(line))         # Put the line back together with .join and write it to the new file

rawDescription: Making changes to CSV and writing to a new file


code:
from matplotlib import animation
from tempfile import NamedTemporaryFile

VIDEO_TAG = """<video controls>
 <source src="data:video/x-webm;base64,{0}" type="video/webm">
 Your browser does not support the video tag.
</video>"""

def anim_to_html(anim):
 if not hasattr(anim, '_encoded_video'):
    with NamedTemporaryFile(suffix='.webm') as f:
        anim.save(f.name, fps=6, extra_args=['-vcodec', 'libvpx'])
        video = open(f.name, "rb").read()
    anim._encoded_video = video.encode("base64")

return VIDEO_TAG.format(anim._encoded_video)

from IPython.display import HTML

def display_animation(anim):
  plt.close(anim._fig)
  return HTML(anim_to_html(anim))

rawDescription: Saving Animation with matplotlib?

description: Saving Animation with matplotlib


code: df.apply(lambda row: func(row['name']), axis=1)

rawDescription: Pandas compare each row with all rows in data frame and save results in list for each row


code: cmd = ["matlab", "-nodisplay", "-nosplash", "-r", "run_image_alg"]

rawDescription: Running shell script on flask

description: Running shell script on flask


code:
from selenium import webdriver
from selenium.webdriver import ActionChains

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("http://www.livedata.com/")

wait = WebDriverWait(driver, 10)

healthcare = wait.until(EC.visibility_of_element_located((By.XPATH, "//li[@class='item' and a/span='Healthcare']")))
ActionChains(driver).move_to_element(healthcare).perform()

benefits = wait.until(EC.visibility_of_element_located((By.XPATH, "//li[@class='item']/a[span='Benefits']")))
ActionChains(driver).move_to_element(benefits).click().perform()

rawDescription: How to click on drop down menu in Python using Selenium and Chrome

description: Click on drop down menu in Python using Selenium and Chrome


code:
pop
popcorn
popular

rawDescription: Generating search term suggestions with Whoosh?


code:
# importee.py
import config
print(config.param)

rawDescription: Is there a way to get importer's variable in importee?


rawDescription: How do I store account information in python so that it can be accessed later?

description: Store account information in python so that it can be accessed later


code:
ITEM_PIPELINES = [
    'myproject.pipeline.DuplicatesPipeline',
]

rawDescription: Item caching in Scrapy

description: Item caching in Scrapy


code:
if 'text' in kwargs:
    initparms['text'] = kwargs['text']

rawDescription: how to pythonically pass arguments to methods of subclassed widgets?

description: Pythonically pass arguments to methods of subclassed widgets


code:
m = multiprocessing.Manager()
rnaSequences = m.list()

rawDescription: Python/Multiprocessing : Processes does not seem to start


code:
class Place(models.Model):
   map = models.ForeignKey('main.Map', related_name="places")
   name = models.CharField(max_length=100)
   slug = models.SlugField(max_length=200)

rawDescription: Django REST - How can I get a JSON with two models?


code:
fig, (ax1, ax2) = plt.subplots(nrows=2)
ax1.plot(ts.index, ts['Value1'], 'k-')
ax1.fill_between(ts.index, ts['Value1'], y2=-6, where=ts['OnOff'])

ax2.plot(ts.index, ts['Value2'], 'k-')
ax2.fill_between(ts.index, ts['Value2'], y2=-6, where=ts['OnOff'])
fig.tight_layout()

rawDescription: Matplotlib axvspan shading for pandas DataFrame subplots based on one of the columns


code:
t1=("string1","string2","string3","string4","string5","string6","string7")
t2=("another string1","another string2",3,None,"another string5",6,7)

rawDescription: Merge two tuples into one

description: Merge two tuples into one


code:
with open('/path/to/some/file/you/want/to/read') as file_1, \
     open('/path/to/some/file/being/written', 'w') as file_2:
    file_2.write(file_1.read())

rawDescription: How to split multiple commands according to PEP8?

description: Split multiple commands according to PEP8


code:
def playlist_changed(self):
        num = self.playlist.currentIndex()  # gets current playlist index
        self.dialog.setWindowTitle('MLB Replay - {}'.format(self.replay_window.item(num).text()))

rawDescription: PyQt5 sending a signal to QMediaPlayer

description: PyQt5 sending a signal to QMediaPlayer


code:
answer = []
for r,row in zip(b,a):
    big = max(enumerate(row), key=operator.itemgetter(1))
    answer.append(r[big[0]])

rawDescription: How to extract data in one array according to the values in another array in Python

description: Extract data in one array according to the values in another array in Python


code:
from sqlalchemy.orm import validates

class Vertex(Base):
    # ...

    @validates('successors')
    def validate_successors(self, key, value):
        check_value = True
        # @todo: insert the validation logic here
        if type(self) == B and type(value) == A:
            check_value = False
        assert check_value, 'Invalid Edge: not allowed to link from [{fr}] to [{to}]'.format(fr=type(self).__name__, to=type(value).__name__)
        return value

rawDescription: type dependant edge creation in polymorphic graph structure with sqlalchemy

description: Type dependant edge creation in polymorphic graph structure with sqlalchemy


code:
import numpy as np

# array of reference float values, equivalent to a[0]
refs = np.random.uniform(0, 100, 10000)

def fast_samp_1(refs, lim=10000, blocksize=10000):

    # sample uniformally from refs
    samp = np.random.choice(refs, size=blocksize, replace=True)
    samp_sum = np.cumsum(samp)

    # find where the cumsum first exceeds your limit
    last = np.searchsorted(samp_sum, lim, side='right')
    return samp[:last + 1]

    # # if it's ok to be just under lim rather than just over then this might
    # # be quicker
    # return samp[samp_sum <= lim]

rawDescription: Speed up loop to fill an array with closest values from another array


code:
import csv
from collections import defaultdict

d_entries = defaultdict(list)

with open('fileTwo.csv', 'r') as f_fileTwo:
    csv_fileTwo = csv.reader(f_fileTwo)
    header_fileTwo = next(csv_fileTwo)
    for cols in csv_fileTwo:
        d_entries[(cols[0], cols[1])].append([cols[0], ''] + cols[1:])

with open('fileOne.csv', 'r') as f_fileOne, open('result.csv', 'w', newline='') as f_result:
    csv_fileOne = csv.reader(f_fileOne)
    csv_result = csv.writer(f_result)
    header_fileOne = next(csv_fileOne)
    csv_result.writerow(header_fileOne)

    for cols in csv_fileOne:
        if (cols[0], cols[2]) in d_entries:
            csv_result.writerow(cols)
            csv_result.writerows(d_entries.pop((cols[0], cols[2])))

rawDescription: How to merge two CSV files by Python based on the common information in both files?

description: Merge two CSV files by Python based on the common information in both files


code:
import numpy as np

# Generate some test data
data = np.arange(200).reshape((4,5,10))

# Write the array to disk
with file('test.txt', 'w') as outfile:
    # I'm writing a header here just for the sake of readability
    # Any line starting with "#" will be ignored by numpy.loadtxt
    outfile.write('# Array shape: {0}\n'.format(data.shape))

    # Iterating through a ndimensional array produces slices along
    # the last axis. This is equivalent to data[i,:,:] in this case
    for data_slice in data:

        # The formatting string indicates that I'm writing out
        # the values in left-justified columns 7 characters in width
        # with 2 decimal places.
        np.savetxt(outfile, data_slice, fmt='%-7.2f')

        # Writing out a break to indicate different slices...
        outfile.write('# New slice\n')

rawDescription: How to write a multidimensional array to a text file?

description: Write a multidimensional array to a text file


rawDescription: Making a dict of a list with more then 1 value


code:
import numpy as np
a = np.zeros(3)
b = np.zeros(3)
a[1] = 1
a == b

rawDescription: Numpy: how to get the items that are different of 2 matrices?


code: vector = np.array( [1 for ele in long_list if condition(ele)] )

rawDescription: better way to vectorize using numpy

description: Better way to vectorize using numpy


code:
def changePage(self,change):
    pages = [self.pageOne,self.pageTwo,self.pageThree]
    new_position = self.position + change
    if (new_postion < 0) or (new_postion <= len(pages)):
        show_frame(BlankPage)
        #not sure how you would handle the new position here
    else:
        pages[new_position].tkraise()
        self.position = new_position

rawDescription: tkinter - Going Back and Forth Between Frames Using Buttons

description: Tkinter - Going Back and Forth Between Frames Using Buttons


code:
from typing import TypeVar
import numpy as np
import array
Ar = TypeVar('Ar', np.ndarray, list, array.array)

rawDescription: function annotations in python

description: Function annotations in python


code:
import pandas as pd
import csv

#preprocessing
def get_names():
    with open('test/file.txt', 'r') as csvfile:
        reader = csv.reader(csvfile)
        num = []
        for i, row in enumerate(reader):
            if i ==0:
                firstline = ''.join(row).split()
                lenfirstline = len(firstline)
                #print firstline, lenfirstline
            num.append(len(''.join(row).split()))
        m = max(num)
        rng = range(1, m - lenfirstline + 2)
        #remove )
        rng = firstline[:-1] + rng
        return rng

#names is list return from function
df = pd.read_csv('test/file.txt', sep="\s+", names=get_names(), index_col=[0], skiprows=1)

rawDescription: How to read unstructured csv in pandas

description: Read unstructured csv in pandas


code: [(x[0],y[0]) for x in enumerate(lst) for y in enumerate(x[1]) if y[1] == find]

rawDescription: Find which row/sublist a value is in

description: Find which row/sublist a value is in


code:
items = [
    "abcde",
    "fghij",
    "fghde"
]

items.sort(key = make_sort_key("gfiabdcej"))

rawDescription: Sort a list based on a string- Python

description: Sort a list based on a string- Python


code: df = df.reset_index()

rawDescription: How to generate continuous record from incomplete data in Pandas Dataframe

description: Generate continuous record from incomplete data in Pandas Dataframe


code:
from operator import getitem
lis = ['car', 'ford', 'mustang']

rawDescription: How can I change the value of a node in a python dictionary by following a list of keys?

description: Change the value of a node in a python dictionary by following a list of keys


code:
def find_name(package, name):
    if hasattr(package, name):
        return package
    for modulename, submodule in inspect.getmembers(package, inspect.ismodule):
        result = find_name(submodule, name)
        if result:
            return result

rawDescription: How to locate a function in a Python module tree?

description: Locate a function in a Python module tree


code:
headers = {}
headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"
r = requests.get('http://www.google.com/searchbyimage?image_url=http://i.imgur.com/j3shP.jpg', headers=headers)

rawDescription: Sending GET requests for a Google reverse image search


code: directors = map(str.strip, directors.split(',')) if directors else []

rawDescription: DRY version of similar string processing


code:
cdfs = defaultdict()
for id1,id2,val in d:
    prevtotal = cdfs[id1][-1][0]
    newtotal = prevtotal + val
    cdfs[id1].append( (newtotal,id2) )

rawDescription: Random weighted choice

description: Random weighted choice


rawDescription: Replace key values in a python csv Dictreader object


code:
import os
os.path.abspath("C:/Documents")
from os import path
path.abspath("C:/documents")
from path import abspath
from os.path import abspath
abspath("C:/documents")

rawDescription: Imporing a function from a module in a package in python


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

# Read CSV file, get author names and counts.
df = pd.read_csv("books.csv", index_col="id")
counter = Counter(df['author'])
author_names = counter.keys()
author_counts = counter.values()

# Plot histogram using matplotlib bar().
indexes = np.arange(len(author_names))
width = 0.7
plt.bar(indexes, author_counts, width)
plt.xticks(indexes + width * 0.5, author_names)
plt.show()

rawDescription: words frequency using pandas and matplotlib

description: Words frequency using pandas and matplotlib


code:
pre("global", "glossary")
pre("global", "global")
pre("global", "")

rawDescription: How to create a new string with the prefix of 2 given strings?

description: Create a new string with the prefix of 2 given strings


code:
for v in d.itervalues(): # values in python 3
    print(v[0]["body_src"])

rawDescription: json dict - python how to access

description: Json dict - python how to access


code: map(sum, zipped)

rawDescription: How to add elements from a dictonary of lists in python

description: Add elements from a dictonary of lists in python


code:
fig = plt.gcf()
size = fig.get_size_inches()*fig.dpi # size in pixels

rawDescription: How to get matplotlib figure size

description: Get matplotlib figure size


code:
dic={'Tim':3, 'Kate':2}
[{'Name':i, 'Age':j} for i,j in dic.items()]

rawDescription: A dict and a list of dicts: conversion and when to use which


code:
class State( object ):
    def transitionRule( self, input ):
        return eval(self.map[input])()

class S1( State ):
    map = { "input": "S2", "other": "S3" }
    pass # Overrides to state-specific methods

class S2( State ):
    map = { "foo": "S1", "bar": "S2" }

class S3( State ):
    map = { "quux": "S1" }

rawDescription: Python state-machine design

description: Python state-machine design


code:
...

scan = table.scan()

#outer-most dict for each row
dict0 = {}

# enumerate so each row can be appended to dict0 using index
for index, row in enumerate(scan):
    # inner-most dict for each key/value pair
    dict1 = {}
    for key, value in row.items():
        dict1[str(key)] = str(value)
    dict0[index] = dict1

rawDescription: Unpack uncertain number of attributes from iterator object in Boto DynamoDB Scan


rawDescription: reversing list using slice notation

description: Reversing list using slice notation


code:
x = root.winfo_x()
y = root.winfo_y()

rawDescription: How to position topLevel() widget relative to root window?

description: Position topLevel() widget relative to root window


code:
from collections import defaultdict
data = defaultdict(lambda: defaultdict(dict))

data['Name34']['IntemList']['Item1']=10
data['Name34']['IntemList']['Item6']=100

print(data['Name34']['IntemList'])

rawDescription: python list tree indexing

description: Python list tree indexing


code:
import re
p = re.compile(r"""^                    # At the start of the string, ...
                   (?!                  # check if next characters are not...
                      www\.             # URLs starting with www.
                     |
                      (?:http|ftp)s?:// # URLs starting with http, https, ftp, ftps
                     |
                      [A-Za-z]:\\       # Local full paths starting with [drive_letter]:\
                     |
                      //                # UNC locations starting with //
                   )                    # End of look-ahead check
                   .*                   # Martch up to the end of string""", re.X)
print(p.search("./about.html"));          # => There is a match
print(p.search("//dub-server1/mynode"));  # => No match

rawDescription: Regex check if given string is relative URL


code:
Median.append( float(row[1]) )
Upper.append( float(row[2]) )
Lower.append( float(row[3]) )

rawDescription: Filling in the space between lines with matplotlib


code:
vec=np.arange(2*2*2*3).reshape(2,2,2,3)
it=np.ndindex(2,2,2)
for i in it:
    print(vec[i])

rawDescription: How to iterate over initial dimensions of a Numpy array?

description: Iterate over initial dimensions of a Numpy array


code:
def translate(lettered_code):
    new_answer = "" # set variable outside the for loop to an empty string
    for letters in lettered_code:
        new_answer += translation[letters] # add each translated letter to new_answer
    return new_answer # return all joined letters

rawDescription: Caesar Cipher: How to print from dictionary *all* letters from input?


code: list(a) == list(b)

rawDescription: Elegant way to compare sequences

description: Elegant way to compare sequences


code:
trades.update(config, join = 'left', overwrite = False)
print(trades)

#                    cusip
# ticker date
# IBM    2000-01-01      1
# MSFT   2000-01-02      2
# GOOG   2000-01-03    100 # If overwrite = True, then 100 is overwritten by 3.
# AAPL   2000-01-04    NaN

rawDescription: Updating pandas DataFrame by key

description: Updating pandas DataFrame by key


rawDescription: Update and create a multi-dimensional dictionary in Python


code:
import json
outfile = open('test.json', 'w')
json.dump(s, outfile)
outfile.close()

rawDescription: Pandas Dataframe to JSON with Custom Encoding


rawDescription: How to calculate ts1 - ts2.interpolate(index=ts1.index)

description: Calculate ts1 - ts2.interpolate(index=ts1.index)


code: {'somethingb': ['z'], 'somethinga': ['x', 'y']}

rawDescription: attibutes of subclassed classes in registry of subclassed classes

description: Attibutes of subclassed classes in registry of subclassed classes


code:
import tokenize
import token
import io
import collections

class Token(collections.namedtuple('Token', 'num val start end line')):
    @property
    def name(self):
        return token.tok_name[self.num]

def check(codestr):
    lastname = None
    for tok in tokenize.generate_tokens(io.BytesIO(codestr).readline):
        tok = Token(*tok)
        if lastname == 'STRING' and lastname == tok.name:
            print('ADJACENT STRINGS: {}'.format(tok.line.rstrip()))
        else:
            lastname = tok.name

codestr = '''
'hello'\
'world'

for z in ('foo' 'bar', 'baz'):
    x = ["abc" "def"]
    y = [1, 2, 3]
'''

check(codestr)

rawDescription: is there any way to stop python to concatanate space delimited strings?

description: Is there any way to stop python to concatanate space delimited strings


code:
from twisted.internet import reactor
from twisted.application.service import Service

class PortReporter(Service, object):
    def __init__(self, factory, reportPort):
        self.factory = factory
        self.reportPort = reportPort

    def privilegedStartService(self):
        self.listeningPort = reactor.listenTCP(0, self.factory)
        self.reportPort(self.listeningPort.getHost().port)
        return super(PortReporter, self).privilegedStartService()

    def stopService(self):
        self.listeningPort.stopListening()
        return super(PortReporter, self).stopService()

rawDescription: twisted - get OS-chosen listen port

description: Twisted - get OS-chosen listen port


code:
def _getattr(obj, query):
    """
    Very similar to _setattr. Instead of setting attributes they will be
    returned. As expected, an error will be raised if a __getitem__ call
    fails.
    """
    instructions = build_instructions(obj, query)
    for i, (_, arg) in enumerate(instructions[:-1]):
        _type = instructions[i + 1][0]
        obj = _get(obj, _type, arg)

    _type, arg = instructions[-1]
    return _get(obj, _type, arg)

def _get(obj, _type, arg):
    """
    Helper function for calling obj.__getitem__(arg).
    """
    if isinstance(obj, dict):
        obj = obj[arg]
    elif isinstance(obj, list):
        arg = int(arg)
        obj = obj[arg]
    return obj

rawDescription: Python recursive setattr()-like function for working with nested dictionaries


code:
class ProductView(ListView):

    context_object_name = 'product_list'
    template_name = 'product_extend/_productlist.html'
    # queryset = ProductExtend.objects.filter(id=1)
    model = Product

    def get_context_data(self, **kwargs):
        context = super(ProductView, self).get_context_data(**kwargs)
        return context

    def get_queryset(self):
        qs = super(ProductView, self).get_queryset()
        return qs.filter(website__website_slug__exact=self.kwargs['website_slug'])

rawDescription: Django 1.6: Displaying a particular models Objects in another template


rawDescription: Python print formatted byte(s)

description: Python print formatted byte(s)


rawDescription: IPython.parallel client is hanging while waiting for result of map_async


code:
class Test(object):
    num = Num()

rawDescription: getting pythons __set__ working

description: Getting pythons __set__ working


rawDescription: Ordering lost when returning self.get_queryset() in GET method of ListAPIView in Django REST Framework


rawDescription: Sort python dictionary by Keys formatted as strings


code:
import os
r, w = os.pipe()
cmd = 'gpg --batch --decrypt --passphrase-fd {}'.format(r)
p = Popen(shlex.split(cmd), stdout=PIPE, stdin=PIPE, stderr=PIPE, pass_fds=(r,))
os.close(r)    # closes fd in parent, child needs it
open(w, 'w').write(passphrase)
decrypted_data, stderr = p.communicate(encrypted_data)

# check that data was successfully roundtripped
assert open(source_filename).read() == decrypted_data.decode()

rawDescription: Encrypt file with GPG in python 3 with subprocess


rawDescription: Python parse dataframe element

description: Python parse dataframe element


code:
def normal(sent):
    sent = sent.split(".")
    result = sent[:1]
    for item in sent[1:]:
        if item[0].isalpha():
            item = " " + item
        result.append(item)
    return ".".join(result)

rawDescription: operating over strings, python

description: Operating over strings, python


code: bool(set(L) & set(M))

rawDescription: efficiently knowing if intersection of two list is empty or not, in python

description: Efficiently knowing if intersection of two list is empty or not, in python


code:
def make_lazy(*funcs):
    def wrapper(x):
        return [lazy_func(f)(x) for f in funcs]
    return wrapper

rawDescription: Avoiding unnecessary key evaluations when sorting a list


code:
if userClickedAddEntryButton: # <-- I made this up
    date=raw_input('date')
    amount=raw_input('amount')
    desc=raw_input('desc')
    account=raw_input('account')
    addEntry(date, amount, desc, account)

rawDescription: How to delay input

description: Delay input


code: degrees = np.arange(len(coeffs))

rawDescription: Calculating "generating functions" with numpy


code:
from django.core.paginator import Paginator, Page

class DSEPaginator(Paginator):
    """
    Override Django's built-in Paginator class to take in a count/total number of items;
    Elasticsearch provides the total as a part of the query results, so we can minimize hits.
    """
    def __init__(self, *args, **kwargs):
        super(DSEPaginator, self).__init__(*args, **kwargs)
        self._count = self.object_list.hits.total

    def page(self, number):
        # this is overridden to prevent any slicing of the object_list - Elasticsearch has
        # returned the sliced data already.
        number = self.validate_number(number)
        return Page(self.object_list, number, self)

rawDescription: Python elasticsearch-dsl django pagination


code:
class MyBase(object):
    def myValue(self): return 42

# Assume that myAppObjDict is already initialized.
myAppObj=type("myAppClass", (MyBase,),myAppObjDict)

rawDescription: Adding methods in type() generated objects in python


rawDescription: dictionary to JSON?

description: Dictionary to JSON


code: ultimate_pattern = '|'.join(patterns)

rawDescription: Regular expressions in python (multiple variables)


code:
import threading, time

def hello():
    for i in range(1,5):
        time.sleep(1)
        print("Hello")

def world():
    for i in range(1,5):
        time.sleep(1)
        print("World")

newthread = threading.Thread(target=hello)
newthread.start()
otherthread = threading.Thread(target=world)
otherthread.start()

# Just for clarity
newthread.join()
otherthread.join()

print("end")

rawDescription: How does multi-threading work in Python 3.4?


code:
class Point(object):
    def __init__(self, x = 0, y = 0):
        self.x = x
        self.y = y

class Square(object):
    def __init__(self, ul, ll, ur, lr):
        self.ul = Point(ul[0], ul[1])
        self.ll = Point(ll[0], ll[1])
        self.ur = Point(ur[0], ur[1])
        self.lr = Point(lr[0], lr[1])

def main():
    square_one = Square((1, 2), (1, 1), (2, 2), (2, 1))
    square_two = Square((3, 3), (3, 2), (4, 3), (4, 2))

rawDescription: Constructor overwrites previous values of already created object


rawDescription: pamie How to access variables in framset

description: Pamie How to access variables in framset


code:
alphabet = {}
for line in input_file:
    line = line.strip()
    starts_with = line[0]
    if line[0] in alphabet:
        alphabet[line[0]].append(line)
    else:
        alphabet[line[0]] = [line]
for key in alphabet:
    alphabet[key] = len(alphabet[key])

rawDescription: count the words starts with a-z character using dictionary in python

description: Count the words starts with a-z character using dictionary in python


code:
from requests_futures.sessions import FuturesSession

def my_parse_function(html):
    """Use this function to parse each page"""
    soup = BeautifulSoup(html)
    all_paragraphs = soup.find_all('p')
    return all_paragraphs

session = FuturesSession(max_workers=5)
futures = [session.get(url) for url in my_list]

page_results = [my_parse_function(future.result()) for future in results]

rawDescription: Web crawler - following links

description: Web crawler - following links


code:
from PySide import QtGui
from gen.ui_MainView import Ui_MainView

class MainView(QtGui.QMainWindow):

    # properties to read/write widget value
    @property
    def running(self):
        return self.ui.pushButton_running.isChecked()
    @running.setter
    def running(self, value):
        self.ui.pushButton_running.setChecked(value)

    def __init__(self, model, main_ctrl):
        self.model = model
        self.main_ctrl = main_ctrl
        super(MainView, self).__init__()
        self.build_ui()
        # register func with model for future model update announcements
        self.model.subscribe_update_func(self.update_ui_from_model)

    def build_ui(self):
        self.ui = Ui_MainView()
        self.ui.setupUi(self)
        # connect signal to method
        self.ui.pushButton_running.clicked.connect(self.on_running)

    def on_running(self):
        self.main_ctrl.change_running(self.running)

    def update_ui_from_model(self):
        self.running = self.model.running

rawDescription: MVC design with QtDesigner and PySide

description: MVC design with QtDesigner and PySide


rawDescription: django-post_office - Using file-based instead of database-based email templates

description: Django-post_office - Using file-based instead of database-based email templates


code:
import urllib2
url = 'http://espn.go.com/nba/team/roster/_/name/bos/boston-celtics'
response = urllib2.urlopen(url)
with open('/tmp/test.html', 'wb') as f:
    f.write(response.read())

rawDescription: XPath syntax with Python

description: XPath syntax with Python


code:
i = {}
for k, v in d.items():
    for k2, v2 in v.items():
        i[v2] = (k, k2)

rawDescription: python query keys in a dictionary based on values

description: Python query keys in a dictionary based on values


rawDescription: Implementing Chain of responsibility pattern in python using coroutines


code: "" is None

rawDescription: store null as default value againt Null boolean field

description: Store null as default value againt Null boolean field


code:
def VisitNode(self, node, target):
    # Base case. If we found the target, return target in a list
    if node == target:
        return [node]
    # recursively iterate over children
    for i in self.neighbors_iter(node):
        tail = self.VisitNode(i, target)
        if tail: # is not None
            return [node] + tail # prepend node to path back from target
    return None # leaf node or none of the child contains target

rawDescription: Finding the (guaranteed unique) path between two nodes in a tree


code: row = df.iloc[0]

rawDescription: plot entire row on pandas

description: Plot entire row on pandas


rawDescription: How to sort and select pandas data

description: Sort and select pandas data


rawDescription: Python Pandas counting and summing specific conditions


code:
plt.figure()   # figure 1
plt.plot(stuff)

plt.figure()   # figure 2
plt.scatter(more_stuff_x, more_stuff_y)

# Wait, I want to add something to the first:
plt.figure(1)
plt.scatter(new_stuff)

plt.show()

rawDescription: Create multiple plots using pyplot and gridspec on separate 'windows'?


rawDescription: How to recursively check all combinations of list python

description: Recursively check all combinations of list python


code:
#read from a file
with open("path/to/file", "rb") as read_file:
    contents = read_file.read()
#write to a file
with open("path/to/file", "wb") as write_file:
    write_file.write(contents)

rawDescription: Convert file into string then Rebuild that String and make it a file again


code:
def checkEqual2(iterator):
   return len(set(iterator)) <= 1

rawDescription: check if all elements in a list are identical

description: Check if all elements in a list are identical


code:
class Migration(SchemaMigration):

    def forwards(self, orm):
        #original changes generated by South:
        # Deleting field 'Cat.id'
        #db.delete_column(u'animals_cat', u'id')

        # Adding field 'Cat.feline'
        #db.add_column(u'animals_cat', 'feline',
        #              self.gf('django.db.models.fields.related.OneToOneField')(default=None, to=orm['animals.Feline'], unique=True, primary_key=True),
        #              keep_default=False)

        #instead of doing the above, just rename Cat.id to Cat.feline_id
        #and drop the default numbering sequence for the Cat.feline_id field
        db.rename_column('animals_cat', 'id', 'feline_id')
        db.execute("ALTER TABLE animals_cat ALTER COLUMN feline_id DROP DEFAULT")

    def backwards(self, orm):
        #original changes generated by South:
        # Adding field 'Cat.id'
        #db.add_column('animals_cat', u'id',
        #              self.gf('django.db.models.fields.AutoField')(default=None, primary_key=True),
        #              keep_default=False)

        # Deleting field 'Cat.feline_id'
        #db.delete_column(u'animals_cat', 'feline_id')

        #instead of doing the above, rename Cat.feline_id to Cat.id
        #and reinstate the default numbering sequence for the Cat.id field
        db.rename_column('animals_cat', 'feline_id', 'id')
        db.execute("ALTER TABLE animals_cat ALTER COLUMN id SET DEFAULT nextval('u'animals_cat_id_seq'::regclass)")

        if orm['Cat'].objects.count():
            #if there are any Cat objects, make sure that new ids are allocated after the largest current ID
            last_id = orm['Cat'].objects.latest('id').id
            db.execute('alter sequence animals_cat_id_seq restart with %s;' % (last_id + 1))

rawDescription: How to create a superclass (for an existing model) with Django and South

description: Create a superclass (for an existing model) with Django and South


code:
import re

rnum = re.compile('\s*TM\s+([^\s:]+):.*')
ruser = re.compile('.*Owner_Info.User_ref\s*=\s*(.*)')
rmodel = re.compile('.*CarModel\s*=\s*(.*)')

def display(out, num, user, model):
    print(num, user, model)
    out.write('insert_owner_car.pl -id %s -o owner_user="%s",car_model="%s",priority="Unknown"\n' % (num, user, model))

inFile = open("/tmp/20141202.194812_carStatus")
outFile = open("result.txt", "w")
firstOfBlock = False
carnum = None
for line in inFile:
    if line.startswith("--------------------------------"):
        firstOfBlock = True
        if carnum is not None:
            display(outFile, carnum, user, model)
            carnum = None
    else:
        if firstOfBlock:
            m = rnum.match(line)
            if m is not None:
                carnum = m.group(1)
                firstOfBlock = False
        else:
            line = line.strip()
            m = ruser.match(line)
            if m is not None:
                user = m.group(1)
            else:
                m = rmodel.match(line)
                if m is not None:
                    model = m.group(1)

if carnum is not None:
    display(outFile, carnum, user, model)
    carnum = None

inFile.close()
outFile.close()

rawDescription: python read blocks of lines from file

description: Python read blocks of lines from file


code: context['form_media'] = my_form.media + my_other_form.media

rawDescription: How to include js and css only once

description: Include js and css only once


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

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
hist, bins = np.histogram(x, bins=50)
width = 0.7 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist, align='center', width=width)
plt.show()

rawDescription: Histogram Matplotlib

description: Histogram Matplotlib


code: names = list(filter(f, names))

rawDescription: How to extract a common name from multipal filenames and delete something I don't want

description: Extract a common name from multipal filenames and delete something I don't want


code:
import fileinput
import sys
for line in fileinput.input("test.txt",inplace=True):
    # split on the last whitespace and write everything up to that
    sys.stdout.write("{}\n".format(line.rsplit(None, 1)[0]))

rawDescription: Overwritting characters from a file in Python


code: result = [myfunction(*item) for item in itertools.product(params_list1, params_list2)]

rawDescription: Iterate over parameters without a loop

description: Iterate over parameters without a loop


code:
colours = get_colours(4, {"red", "green", "blue", "yellow", "magenta", "cyan"})
colours

rawDescription: how can i modify my code so that the user can input the same colour four times but not anything outside my list range?

description: Modify my code so that the user can input the same colour four times but not anything outside my list range


code: files = ['data1.txt', 'data2.txt',...,'dataN.txt']

rawDescription: Name of a list changing as I go through a loop


code:
for batch in grouper(data, 1000, ''):
    print('\n'.join(batch))

rawDescription: Python3, a lazy print

description: Python3, a lazy print


rawDescription: Resolve FB GraphAPI picture call to the final URL


code:
import struct
ninebits = [0b110011001,0b000000000,0b111111111,0b010101010]
fourbit = 0b1010
pack = 0
for i,v in enumerate(ninebits):
    pack <<= 9
    pack |= v
pack <<= 4
pack |= fourbit
print('{:040b}'.format(pack))
print('{:010X}'.format(pack))
print(struct.pack('>LB',pack >> 8, pack & 0xFF))

rawDescription: how to pack 4 9-bit and 1 4-bit int variable into 5 8-bit variable in python?

description: Pack 4 9-bit and 1 4-bit int variable into 5 8-bit variable in python


code:
def specialCharsToHtmlHex(self, rawString, jsfViewState=False):
    parsedString = (rawString.replace('+', '%2B')
                             .replace('/', '%2F')
                             .replace(':', '%3A')
                             .replace('?', '%3F'))

    if jsfViewState:
        parsedString = parsedString.replace('=', '%3D')

    return parsedString

rawDescription: Download "disguised" file using a PyQT scraper


rawDescription: Fastest Way to Populate a Pandas DataFrame When Order Matters


code:
def job(value):
    return lambda f: f

rawDescription: How to get arguments to a decorator?

description: Get arguments to a decorator


code: d2 = {k: f(v) for k, v in d1.items()}

rawDescription: Applying a function to values in dict

description: Applying a function to values in dict


rawDescription: How to replace prefix and postfix with regex in python

description: Replace prefix and postfix with regex in python


code:
for fh in output.values():
    fh.seek(0)

rawDescription: Sorting csv by number of columns in each row


code:
from itertools import chain

result = list(chain.from_iterable(test))

rawDescription: More "pythonic" way of expanding list of lists to one joined list


rawDescription: Why is hardcoding this list slower than calculating it?


code:
Example
file = f('prog.py')  # use as ordinary function
print(repr(file.read(20)))
file.seek(0)
for line in file:
    print(repr(line))
    break
file.close()

rawDescription: functools.wraps won't let me wrap a function with a class in Python 3

description: Functools.wraps won't let me wrap a function with a class in Python 3


code:
from itertools import groupby
from operator import itemgetter

data = [ 1,  4,5,6, 10, 15,16,17,18, 22, 25,26,27,28,29]
# Find runs of consecutive numbers using groupby.  The key to the solution
# is differencing with a range so that consecutive numbers all appear in
# same group.
for k, g in groupby(enumerate(data), lambda x: x[0]-x[1]):
    seq = list(map(itemgetter(1), g))
    # if you don't want the actual items then:
    # sum(1 for _ in g) >= 5: print('yes')
    if len(seq) >= 5:
        print (seq)

rawDescription: How to check existence of a sequence in a sorted list?

description: Check existence of a sequence in a sorted list


code:
with open(file) as f:
    for line in f:
        while True:
            try:
                jfile = json.loads(line)
                break
            except ValueError:
                # Not yet a complete JSON value
                line += next(f)

        # do something with jfile

rawDescription: Python: Trying to Deserialize Multiple JSON objects in a file with each object spanning multiple but consistently spaced number of lines


code:
from w3lib.html import remove_tags
map(remove_tags, sel.xpath('//b').extract())

rawDescription: Get xpath() to return empty values

description: Get xpath() to return empty values


rawDescription: Comparing List to Dictionary in Python

description: Comparing List to Dictionary in Python


code:
Session.query(distinct(Transaction.user_id).label('user_id'),
                         extreme_last(Transaction.remains,
                                      partition_by=Transaction.user_id,
                                      order_by=Transaction.id).label('remains'))

rawDescription: How can i combine two queries in sqlalchemy - raw and orm?

description: Combine two queries in sqlalchemy - raw and orm


code: latitude, longitude = geo-point # Where geo point is (23.3,38.3) or [12.2,83.33]

rawDescription: Use parentheses or not for multiple variable assignment in Python?


code:
import itertools

def _odd_iter():
    n = 1
    while True:
        n = n + 2
        yield n

def primes():
    yield 2
    it = _odd_iter()
    while True:
        n = next(it)
        yield n
        it = itertools.ifilter(lambda x, n=n: x%n > 0, it)

for n in primes():
    if n < 100:
        print(n)
    else:
        break

rawDescription: Ifilter together with lambda expression

description: Ifilter together with lambda expression


code: K += max(l, key=lambda x: x[0])

rawDescription: Python: List is a int, TypeError

description: Python: List is a int, TypeError


code: ipython.magic("timeit abs(-42)")

rawDescription: How to run an IPython magic from a script (or timing a Python script)

description: Run an IPython magic from a script (or timing a Python script)


rawDescription: Latin to English alphabet hashing

description: Latin to English alphabet hashing


code: l = r.json()['results']

rawDescription: how to access key values in a json files dictionaries with python

description: Access key values in a json files dictionaries with python


code:
regex = re.compile(r"(?<=\()\d+(?=\))")
value = int(re.search(regex, fname).group(0))

rawDescription: Extracting a number from a string using regular expressions


code:
myDict = {}
myDict.setdefault(myKey, []).append(value)

rawDescription: How can duplicate values be allowed in dictionary when using set


code:
int_keys = rgb2int(np.array(color2ind.keys(), dtype='uint8'))
int_keys

rawDescription: Convert a 3D array to 2D array based on dictionary


code:
from gdata.youtube import service

USERNAME = 'username@gmail.com'
PASSWORD = 'a_very_long_password'
VIDEO_ID = 'wf_IIbT8HGk'

def comments_generator(client, video_id):
    comment_feed = client.GetYouTubeVideoCommentFeed(video_id=video_id)
    while comment_feed is not None:
        for comment in comment_feed.entry:
             yield comment
        next_link = comment_feed.GetNextLink()
        if next_link is None:
             comment_feed = None
        else:
             comment_feed = client.GetYouTubeVideoCommentFeed(next_link.href)

client = service.YouTubeService()
client.ClientLogin(USERNAME, PASSWORD)

for comment in comments_generator(client, VIDEO_ID):
    author_name = comment.author[0].name.text
    text = comment.content.text
    print("{}: {}".format(author_name, text))

rawDescription: How to get all YouTube comments with Python's gdata module?

description: Get all YouTube comments with Python's gdata module


code:
transmap = str.maketrans(
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ?!.-()<>%$',:_/\\",
    "ɐqɔpǝɟƃɥᴉɾʞ˥ɯuodbɹsʇnʌʍxʎz∀qƆpƎℲפHIſʞ˥WNOԀΌɹS┴∩ΛMX⅄Z0ƖᄅƐㄣϛ9ㄥ86 ¿¡˙-)(><%$,':‾\\/")
'The quick brown fox jumps over the lazy dog, 42 times!'.translate(transmap)

rawDescription: How to replace every character from input? (python3.x)

description: Replace every character from input? (python3.x)


rawDescription: numpy array - replace certain values

description: Numpy array - replace certain values


code:
import requests # pip install requests

url = 'https://api.twilio.com/2010-04-01/Accounts/abc/SMS/Messages.json'
r = requests.post(url, dict(
        From='+17035551212',
        To='+17035551212',
        Body='This is a test'), auth=('foo', 'bar'))

print(r.headers)
print(r.text) # or r.json()

rawDescription: Convert this curl cmd to Python 3

description: Convert this curl cmd to Python 3


code:
gnome_panel_procs = []
for process in psutil.process_iter():
    # I assume the gnome-panel processes correctly set their name
    # eventually you could use process.cmdline instead
    if process.name == 'gnome-panel':
        gnome_panel_procs.append(process)

for proc in gnome_panel_procs:
    for _ in range(60):
        # check cpu percentage over 1 second
        if proc.get_cpu_percent(1) < 80 or not proc.is_running():
            # less than 80% of cpu or process terminated
            break
    else:
        # process used 80% of cpu for over 1 minute
        proc.kill()

rawDescription: how to kill specific process using %cpu over given time in python on linux?

description: Kill specific process using %cpu over given time in python on linux


code:
from msvcrt import getch
while True:
    print(ord(getch()))

rawDescription: Python method for reading keypress?

description: Python method for reading keypress


code: data = [df, df, df, df]

rawDescription: Merge a list of DataFrame's on a column?

description: Merge a list of DataFrame's on a column


code:
f_ = A(f)
inspect.ismethod(f_.call)
f_.call()

rawDescription: <method-wrapper '__call__' of functools.partial object at 0x1356e10> is not a Python function


code:
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from yourapp.forms import ContactForm

def email(request):
    if request.method == 'GET':
        form = ContactForm()
    else:
        form = ContactForm(request.POST)
        if form.is_valid():
            subject = form.cleaned_data['subject']
            from_email = form.cleaned_data['from_email']
            message = form.cleaned_data['message']
            try:
                send_mail(subject, message, from_email, ['admin@example.com'])
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('thanks')
    return render(request, "yourapp/email.html", {'form': form})

def thanks(request):
    return HttpResponse('Thank you for your message.')

rawDescription: Python Django e-mail form example

description: Python Django e-mail form example


code:
df = pd.read_csv('qs.d.ieutio1m.csv', sep=';', header=4, na_values=['ND'])
df = df.dropna()

rawDescription: pandas: skip strings when loading a csv

description: Pandas: skip strings when loading a csv


code:
banned_users = User.select().where(User.is_banned == True)
Message.delete().where(Message.user.in_(banned_users)).execute()

rawDescription: Deleting multiple records in a table using join in Peewee?


code:
answer = 0
for i,elem in enumerate(L):
    mult = [1,-1][i%2]
    answer += elem * mult

rawDescription: Alternating sum of integers in Python using lambda


code:
import os #for renaming the output file

def ripple(file_loc='/typical/location/while/developing.txt'):
    outfile = "output.".join(os.path.basename(file_loc).split('.'))

    with open(outfile, 'w') as w:
        lines = open(file_loc, 'r').readlines() #everything is one giant list
        w.write('\n'.join([line.split(':')[-1] for line in lines]))

ripple()

rawDescription: Write strings to another file

description: Write strings to another file


code:
df['weight'] = df['dist'] / df.groupby('ind')['dist'].transform('sum')
df['weight']

rawDescription: Calculate weighted average with pandas dataframe


code: url = 'http://www.tripadvisor.co.uk/' + link[0].attrib['href']

rawDescription: Python scraper (Scraperwiki) only getting half of the table


code:
def askPowerTread():
        while True:
            choice = input(...)
            if choice in [...]:
                break
            else:
                print('{choice} is an invalid choice'.format(choice=choice))

rawDescription: Looking for help designing a While loop in Python


code:
class Foo(object):
    objs = []  # registrar

    def __init__(self, num):
        # register the new object with the class
        Foo.objs.append(self)
        self.my_value = num

    @classmethod
    def crunch_all(cls):
        for obj in cls.objs:
            obj.new_value = obj.my_value + 1

rawDescription: Apply a function to all instances of a class


rawDescription: Zeroing-out array values outside of a centered slice


code:
for x, y in zip(g1, g2):
    print(x[0] + y[0])

rawDescription: How to join/merge two generators output using python

description: Join/merge two generators output using python


code:
from multiprocessing import *
import time

def main():
    array, loops = list(range(1000)), 1000
    with Manager() as manager:
        variable = manager.Value('I', 0)
        p = Process(target=sample, args=(array, loops, variable))
        p.start()
        while variable.value < 1000:
            print('Still less than 1000')
            time.sleep(0.005)
        print('Must be at least 1000')
        p.join()
        print('Value is', variable.value)

def sample(array, loops, variable):
    for number in range(loops):
        variable.value = number * 10
    print('Sample is done')

if __name__ == '__main__':
    main()

rawDescription: Return value while executing paralel processes with python


code:
class Sniffer(threading.Thread):

    def __init__(self, port):
        ...
        threading.Thread.__init__(self)
        #Packets will be stored here
        self.result = []
        #Not worry about closing the sniffer thread
        self.daemon = True

    #Invoked when I start my thread
    def run(self):
        max_bytes = 16000
        #Important to capture broken packets as I need them
        promiscuous = True
        read_timeout = 100
        pc = pcapy.open_live(str(self.port), max_bytes, promiscuous, read_timeout)
        pc.loop(-1, self.recv_pkts)

    #Do when the packet arrives
    def recv_pkts(self, hdr, packet):
        packetHex = binascii.hexlify(packet)
        self.result.append(packetHex)
        return self.result

rawDescription: Efficient way to capture packets in multithreading Python program


rawDescription: Сut zeros from the end of the list

description: Сut zeros from the end of the list


code:
# models.py
class MovieCategory(models.Model):
    category = models.ForeignKey(Category)
    movie = models.ForeignKey(Movie)
    prefix = models.ForeignKey('Prefix', blank=True, null=True)
    number = models.DecimalField(verbose_name='Movie Number', max_digits=2,
                                 blank=True, null=True, decimal_places=0)

rawDescription: Django ForeignKey limit_choices_to a different ForeignKey id


code: [f(x) for x in iterable]

rawDescription: Understanding the map function

description: Understanding the map function


code:
import numpy
from matplotlib import pyplot as plt

if __name__ == '__main__':
    x = [1, 2, 3]
    plt.ion() # turn on interactive mode
    for loop in range(0,3):
        y = numpy.dot(x, loop)
        plt.figure()
        plt.plot(x,y)
        plt.show()
        _ = raw_input("Press [enter] to continue.")

rawDescription: Can I generate and show a different image during each loop with Matplotlib?

description: Generate and show a different image during each loop with Matplotlib


code:
mask = np.zeros(df.shape[0], dtype=bool)
evt_id = np.zeros(df.shape[0], dtype=int)
for i, (s, e) in enumerate(zip(start_idx, end_idx)):
    mask[s:e] = True
    evt_id[s:e] = i

rawDescription: Filtering a panda dataframe based on value and time


code:
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import sys

name = 'ball_glut'

def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
    glutInitWindowSize(400,400)
    glutCreateWindow(name)

    glClearColor(0.,0.,1.,1.)
    glShadeModel(GL_SMOOTH)
    glEnable(GL_CULL_FACE)
    glEnable(GL_DEPTH_TEST)
    glEnable(GL_LIGHTING)
    lightZeroPosition = [10.,4.,10.,1.]
    lightZeroColor = [1.0,1.0,1.0,1.0]
    glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition)
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor)
    glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1)
    glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05)
    glEnable(GL_LIGHT0)
    glutDisplayFunc(display)
    glMatrixMode(GL_PROJECTION)
    gluPerspective(40.,1.,1.,40.)
    glMatrixMode(GL_MODELVIEW)
    gluLookAt(0,0,10,
              0,0,0,
              0,1,0)
    glPushMatrix()
    glutMainLoop()
    return

def display():
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glMatrixMode(GL_MODELVIEW)

    # Render sphere normally
    glPushMatrix()
    color = [1.0,0.,0.,1.]
    glMaterialfv(GL_FRONT,GL_DIFFUSE,color)
    glCullFace(GL_BACK)
    glEnable(GL_LIGHTING)
    glutSolidSphere(2,20,20)

    # Render silhouette around object
    glPushMatrix()
    glCullFace(GL_FRONT) # Front face culling makes us render only the inside of the sphere, which gives the illusion of a silhouette
    glScale(1.04, 1.04, 1.04) # This makes the silhouette show up around the object
    glDisable(GL_LIGHTING) # We only want a plain fixed colour silhouette
    glColor(0., 1., 0.) # Silhouette colour
    glutSolidSphere(2,20,20) # This can be any object, not limited to spheres (even non-convex objects)
    glPopMatrix()

    glPopMatrix()
    glutSwapBuffers() # Display results.
    return

if __name__ == '__main__': main()

rawDescription: how to defining the contour of a shape PyOpenGL

description: Defining the contour of a shape PyOpenGL


code:
foo = Foo()
foo.y = 1

rawDescription: Metaclasses and __slots__?

description: Metaclasses and __slots__


code: result = np.einsum('ij,ij->i', df1.values, df2.values)

rawDescription: Applying np.dot to each row of two DataFrames


code:
form = crud.create(db.mytable)
form.element('#mytable_myfield__row .w2p_fw').append(A('Add', _onclick='add()'))
form.element('#mytable_myfield__row .w2p_fw').append(A('Remove', _onclick='remove()'))

rawDescription: web2py CRUD.create() field represenation in forms

description: Web2py CRUD.create() field represenation in forms


code:
from collections import Counter

y_counts = Counter(obj.y for obj in items)
test = [obj for obj in items if y_counts[obj.y] > 1]

rawDescription: Count with respect to a particular attribute


code:
#!/usr/bin/env python
"""Start process; wait 2 seconds; kill the process; print all process output."""
import errno
import os
import pty
import select
from subprocess import Popen, STDOUT
try:
    from time import monotonic as timer
except ImportError:
    from time import time as timer

output = []
master_fd, slave_fd = pty.openpty() #XXX add cleanup on exception
p = Popen(["top"], stdin=slave_fd, stdout=slave_fd, stderr=STDOUT,
          close_fds=True)
os.close(slave_fd)
endtime = timer() + 2 # stop in 2 seconds
while True:
    delay = endtime - timer()
    if delay <= 0: # timeout
        break
    if select.select([master_fd], [], [], delay)[0]:
        try:
            data = os.read(master_fd, 1024)
        except OSError as e: #NOTE: no need for IOError here
            if e.errno != errno.EIO:
                raise
            break # EIO means EOF on some systems
        else:
            if not data: # EOF
                break
            output.append(data)
os.close(master_fd)
p.terminate()
returncode = p.wait()
print([returncode, b''.join(output)])

rawDescription: How do I obtain the output from a program that uses screen redrawing for use in a terminal screen scraper?

description: Obtain the output from a program that uses screen redrawing for use in a terminal screen scraper


code:
import numpy as np
dates = np.intersect1d(df1['Date'], df2['Date'])
mask1 = df1['Date'].isin(dates)
mask2 = df2['Date'].isin(dates)
result = pd.merge(df1.loc[mask1], df2.loc[mask2], on=['Date', 'count'], how='outer')

rawDescription: Pandas join/merge 2 dataframes using date as index


code:
login_url = "http://forum.bazicenter.com/login.php?do=login"
login_data = {
    'vb_login_password_hint': 'your_password',
    'vb_login_username': 'username'
}

s.post(login_url, login_data)

rawDescription: Crawling a members-only page with BeautifulSoup


code:
def run(self):
    while(not self._stop.isSet()):
        self.awake.clear()
        self._stop.wait(self._timeout)  # instead of sleeping
        if self._stop.isSet():
            continue
        self.awake.set()
        self._target()

rawDescription: Python terminate a thread when it is sleeping


code: array2d = spread(array1d,2,2)

rawDescription: broadcast an array to different shape (adding "fake" dimensions)

description: Broadcast an array to different shape (adding "fake" dimensions)


code: a[list(np.ogrid[[slice(x) for x in a.shape]][:-1])+[i]]

rawDescription: Numpy: Sorting a multidimensional array by a multidimensional array


code:
[sum([n*(10**i) for i,n in enumerate(([x]+list(ys)+[z]+list(ys)[::-1]+[x]) if k%2
                                else ([x]+list(ys)+list(ys)[::-1]+[x]))])
            for x in range(1,10)
            for ys in itertools.permutations(range(10), k/2-1)
            for z in (range(10) if k%2 else (None,))]

rawDescription: Generate list of all palindromic numbers of 3 digits in python


rawDescription: Numpy: Multiple Outer Products

description: Numpy: Multiple Outer Products


code: ['palabras\n']

rawDescription: Running many python threads and grabbing the returns values of each


code:
def reverse(self, pos):
    """Gets the world coordinates by screen coordinates"""
    return (pos[0] - self.state.left, pos[1] - self.state.top)

rawDescription: Getting World Coordinates with mouse in pygame


code:
foo = Foo(a, b)
barparam = (c, len(d))
foo.bar(barparam)

rawDescription: Inline Tuple Creation - Which is More Readable?


code:
if box_plot:
    plt.subplot(1, 1, 1)
    plt.boxplot(data.data)
    plt.subplot(1, 2, 2)

rawDescription: Matplotlib boxplot + imageshow (subplots)


code:
import string
map(Counter(s).__getitem__, string.lowercase)

rawDescription: Match letter frequency within a word against 26 letters in R (or python)


code:
import heapq
x0 = 45
n = 3
mylist = [12,32,432,43,54,234,23,543,2]
heapq.nsmallest(n, mylist, key=lambda x: abs(x-x0))

rawDescription: Top n closest numbers from a python list


code:
def autofetch_value(self, sql, args=None):
    """ return a single value from a single row or None if there is no row
    """
    self.execute(sql, args)
    returned_val = None

    row = self.fetchone()
    if row is not None:
        returned_val = row[0]

    return returned_val

rawDescription: Python - automating MySQL query: passing parameter


rawDescription: How to convert a date string to different format

description: Convert a date string to different format


code:
def debug(fn, *args, **kw):
    if __debug__:
        print(fn(*args, **kw))
    return True

rawDescription: Options to avoid executing code on __debug__


code:
lib_typelib = bld.new_task_gen(
  name = 'versionfile',
  source = 'versionfile.ver',
  target = 'versionfile.out',
  rule='/path/to/your/python/script ${SRC} -o ${TGT}')

rawDescription: Waf: How to output a generated file?

description: Waf: How to output a generated file


code:
'001 abcdef 001'.split(None, 1)   # `1` to split at most once
'001 abcdef 001'.split(None, 1)[1]

rawDescription: Different cut method python 2.6 to 2.7

description: Different cut method python 2.6 to 2.7


code:
import shelve
myDict = {"a" : 1, "b" : 2}
myShelvedDict = shelve.open("my_shelved_dictionary.db")
myShelvedDict["myDict"] = myDict

rawDescription: How to populate shelf with existing dictionary

description: Populate shelf with existing dictionary


code: key, value = dict.popitem()

rawDescription: Python access to first element in dictionary


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

labels = ['D', 'F', 'A', 'G', 'DG', 'R', 'I', 'BS', 'COA', 'W', 'T']
data = np.random.random((len(labels), len(labels)))

fig, ax = plt.subplots()
im = ax.matshow(data, cmap='Pastel1')
fig.colorbar(im)

i = range(len(labels))
ax.set(xticks=i, xticklabels=labels, yticks=i, yticklabels=labels)

for (i, j), z in np.ndenumerate(data):
    ax.text(j, i, '{:0.1f}'.format(z), ha='center', va='center')

ax.set(ylabel='True label', xlabel='Predicted label', title='Confusion Matrix')
plt.show()

rawDescription: Issues with the labels in image in matplotlib


code:
from selenium import webdriver

fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", "/home/jill/Downloads/Dinamalar")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,application/x-pdf")

fp.set_preference("pdfjs.disabled", "true")  # < KEY PART HERE

browser = webdriver.Firefox(firefox_profile=fp)
browser.get("http://epaper.dinamalar.com/PUBLICATIONS/DM/MADHURAI/2015/05/26/PagePrint//26_05_2015_001_b2b69fda315301809dda359a6d3d9689.pdf");

rawDescription: Selenium pdf automatic download not working


code:
from bs4 import BeautifulSoup

with open('mpg.xml') as f:
    contents=f.read()

mpgs = BeautifulSoup(contents)

def find_nearest_vehicle(elem):
    for sibling in elem.next_siblings:
        if sibling.name == 'yourmpgvehicle':
            return sibling

def find_avg_mpg(elem):
    for child in elem.children:
        if child.name == 'avgmpg':
            return child

year_2013 = [year for year in mpgs.find_all('year')
             if year.string == '2013']

avgmpg = [find_avg_mpg(find_nearest_vehicle(elem)).string
          for elem in year_2013]

print(avgmpg)

rawDescription: BeautifulSoup: How to get the nearest tag


rawDescription: Removing element in a list

description: Removing element in a list


rawDescription: Adding a line to a matplotlib scatterplot based on a slope


code: #!/home/user/virtualenv/bin/python

rawDescription: Calling python script from crontab with activate


code:
output = [row._asdict() for row in DBSession.query(Client.id, Client.name)
                                            .filter(q in Client.name)]

rawDescription: Create dictionary of a sqlalchemy query object in Pyramid


rawDescription: Convert multichar %xx escapes to unicode


code:
class UserCellPhone(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)

class SentMessage(models.Model):
    to_number = models. ManyToManyField(UserCellPhone, through='MessagesDelivery')

rawDescription: Send messages to multiple users

description: Send messages to multiple users


code:
def paypal_payment_successful(sender, **kwargs):
    #Called when the payment is sucessful
    ipn_obj = sender
    user_id = ipn_obj.custom

    if ipn_obj.payment_status == "Completed":
        try:
            user_profile = models.UserProfile.objects.get(user=user_id)
            user_profile.account_balance += float(ipn_obj.mc_gross)
            user_profile.save()

        except models.UserProfile.DoesNotExist:
            pass # TODO email admin

rawDescription: How to use django-paypal to update a users account balance

description: Use django-paypal to update a users account balance


code:
def sub(name_of_array):
    return name_of_array[0, 0]

def main():
    name_of_array = np.zeros((2, 2))
    sub(name_of_array)

rawDescription: Numpy array defined in main not recognized in function


rawDescription: How to quickly invert a power function in Sympy

description: Quickly invert a power function in Sympy


code:
ip_subtasks = group(
        [resolve_hostname.s(hostname, resolver=resolver)
         for hostname in required_hosts.REQUIRED_HOSTS.keys()]
    )()

rawDescription: Celery: One subtask in group always timesout


code:
self.aspan_list = []
self.aspan_list.append(self.axes.axvspan(x0,event.xdata, facecolor='y', alpha=0.5))
#...
for aspan in self.aspan_list:
    aspan.remove()

rawDescription: How can I delet an axvspan line?

description: Delet an axvspan line


code: from library import DCOM_ERROR_CONSTANTS

rawDescription: In python, can a whole file be just a variable?


code:
l = lambda: None
l
l.__name__

rawDescription: What is islambda function in python

description: What is islambda function in python


code:
from collections import defaultdict
res = defaultdict(list)

for d in books:
    res[d.pop('Serial Number')].append(d)

print({k: v for k, v in res.items() if len(v) > 1})

rawDescription: Extracting duplicate from array of dictionaries


code:
from collections import Counter
Counter(value for values in d.itervalues() for value in values)

rawDescription: Count items in defaultdict

description: Count items in defaultdict


rawDescription: How should I take the max of 2 columns in a dataframe and make it another column?


code:
import json

def persist_to_file(file_name):

    def decorator(original_func):

        try:
            cache = json.load(open(file_name, 'r'))
        except (IOError, ValueError):
            cache = {}

        def new_func(param):
            if param not in cache:
                cache[param] = original_func(param)
                json.dump(cache, open(file_name, 'w'))
            return cache[param]

        return new_func

    return decorator

rawDescription: memoize to disk - python - persistent memoization

description: Memoize to disk - python - persistent memoization


code:
df = pd.DataFrame([["a"]], columns=["A"])
df.to_csv()
df.to_csv(quoting=1)

rawDescription: python/pandas:need help adding double quotes to columns

description: Python/pandas:need help adding double quotes to columns


code: gems = pygame.sprite.Group()

rawDescription: How to use sprite groups in pygame

description: Use sprite groups in pygame


rawDescription: Merging csv files using python and pandas (overlapping rows)


rawDescription: Arranging columns in pivot_table, Pandas


code: sorted("aa2, ab2, bb1, bc1, ab3, aa3, ba2, ba1, aa1, ac1, bb2, bb3, ab1".split(', '))

rawDescription: How to do a "Filing cabinet" sort on alphanumeric strings

description: Do a "Filing cabinet" sort on alphanumeric strings


rawDescription: loading a file, posting that to a textbox

description: Loading a file, posting that to a textbox


code: servqual not in ("great", "good", "lacking", "poor")

rawDescription: Python: Repeating a function when raw_input is not valid


code:
text_list = generate_text_list() # or whatever you do to get a text sequence
extensions = ['.doc', '.pdf', '.xls', '.jpg']
words = ['some', 'words', 'to', 'filter']
text_list = [text for text in text_list if not text.endswith(tuple(extensions))]
text_list = [text for text in text_list if not any(word in text for word in words)]

rawDescription: Python Logic in searching String

description: Python Logic in searching String


code:
import live_settings
from django.core.management  import setup_environ
setup_environ(live_settings)

rawDescription: Using live_settings in python shell

description: Using live_settings in python shell


code:
xml_file = "{}/../text.xml".format(BASE_DIR)
fin = open(xml_file, 'r+')

rawDescription: Python: Check if data file exists relative to source code file


code:
import os
import sys
from time import sleep

from subprocess import check_call, CalledProcessError

try:
    check_call(["lockfile-create", "-q","-p", "-r", "0", "-l", "my.lock"])
except CalledProcessError as e:
    print("{} is already running".format(sys.argv[0]))
    print(e.returncode)
    exit(1)

# main body

for i in range(10):
    sleep(2)
    print(1)

check_call(["rm","-f","my.lock"])

rawDescription: Checking running python script within the python script


rawDescription: How to get values with any depth in dict via *args parameter? (python)

description: Get values with any depth in dict via *args parameter? (python)


rawDescription: Losing DST information using pytz and UTC conversions


rawDescription: Rotate photo via python

description: Rotate photo via python


code:
import redis
redis_instance = redis.Redis()
p = subprocess.Popen(shlex.split("tail -f /tmp/foo"), stdout=subprocess.PIPE)

while True:
    line = p.stdout.readline()
    if line:
        redis_instance.publish('process log', line)
    else:
        break

rawDescription: Stream results in celery

description: Stream results in celery


code: t2=[n for n,nbrs in G.succ.items() if len(nbrs)==2]

rawDescription: What is the fastest iteration over a large network instance of DiGraph networkx?


rawDescription: Python Hash: Function to determine longest probe sequence


code: home, away = [int(e) for e in re.findall(r'\d+', lp)]

rawDescription: Most Pyhthonic way to convert element of list get with re


code:
import timeit
import time

def _template_func(setup, func):
    """Create a timer function. Used if the "statement" is a callable."""
    def inner(_it, _timer, _func=func):
        setup()
        _t0 = _timer()
        for _i in _it:
            retval = _func()
        _t1 = _timer()
        return _t1 - _t0, retval
    return inner

timeit._template_func = _template_func

def foo():
    time.sleep(1)
    return 42

t = timeit.Timer(foo)
print(t.timeit(number=1))

rawDescription: How can I capture return value with Python timeit module?

description: Capture return value with Python timeit module


code:
from itertools import product
list(product([1, 0], repeat=2))

rawDescription: How to generate list combinations in Python

description: Generate list combinations in Python


code:
def agent_loc(a_maze):
    matches = (str(row) + ";" + str(column)
               for row, i in enumerate(a_maze)
               for column, j in enumerate(i)
               if j == "E")
    print("|".join(matches) + "|")

rawDescription: (Python) Index stops after finding one element in the row


code:
with open("/etc/os-release") as f:
    d = {}
    for line in f:
        k,v = line.rstrip().split("=")
        # .strip('"') will remove if there or else do nothing
        d[k] = v.strip('"')
print(d)

{'VERSION': '14.04.2 LTS, Trusty Tahr', 'NAME': 'Ubuntu', 'HOME_URL': 'http://www.ubuntu.com/', 'ID': 'ubuntu', 'VERSION_ID': '14.04', 'SUPPORT_URL': 'http://help.ubuntu.com/', 'PRETTY_NAME': 'Ubuntu 14.04.2 LTS', 'BUG_REPORT_URL': 'http://bugs.launchpad.net/ubuntu/', 'ID_LIKE': 'debian'}
{'BUG_REPORT_URL': 'http://bugs.launchpad.net/ubuntu/',
 'HOME_URL': 'http://www.ubuntu.com/',
 'ID': 'ubuntu',
 'ID_LIKE': 'debian',
 'NAME': 'Ubuntu',
 'PRETTY_NAME': 'Ubuntu 14.04.2 LTS',
 'SUPPORT_URL': 'http://help.ubuntu.com/',
 'VERSION': '14.04.2 LTS, Trusty Tahr',
 'VERSION_ID': '14.04'}

rawDescription: module to parse a simple syntax in python

description: Module to parse a simple syntax in python


code:
import matplotlib.pyplot as plt

fig = plt.figure()

for n in range(0,10):
    plt.plot(list(range(0,n)))
    fig.savefig('test%d.png' % n)
    plt.show()
    plt.clf()

rawDescription: Matplotlib: Saved files in a loop aren't the same as in show()


rawDescription: Deciphering My Cipher: Python Wizardry

description: Deciphering My Cipher: Python Wizardry


code: coord1, coord2 = zip(*itertools.product(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution)))

rawDescription: Scikit-learn and data visusalisation: Why do I have to use ravel when I use predict?


code:
import datetime

date = datetime.datetime.strptime(inputvalue, '%d/%b/%Y').date()

rawDescription: Extract date ("05/Jan/2014") from string


code:
import multiprocessing

def process_big_file(big_file):
    print("Process {0}: Got big file {1}".format(multiprocessing.current_process(), big_file))
    return "done with {0}".format(big_file)

def get_big_file_list():
    return ['bf{0}'.format(i) for i in range(20)]  # Just a dummy list

if __name__ == "__main__":
    pool = multiprocessing.Pool(5)  # 5 worker processes in the pool
    big_file_list = get_big_file_list()
    results = pool.map(process_big_file, big_file_list)
    print(results)

rawDescription: Setting up a manager in python in a cluster environment


code:
master = list(daily)
random.shuffle(master)

rawDescription: Random list with rules

description: Random list with rules


code:
1 - fou4r
2 - fileeee3
3 - fi2le
4 - file1file
5 -  donttouch
6 -  donttouch5either
script.pl

rawDescription: Batch rename directories in reverse order


rawDescription: Break out of double while loops?

description: Break out of double while loops


code:
def transpose_matrix(matrix):
    matrix = list(map(list, zip(*matrix))) # assign result
    return matrix # return transposed matrix

rawDescription: Transpose this matrix(python3)

description: Transpose this matrix(python3)


code:
import locale
locale.setlocale(locale.LC_ALL, 'de_DE.utf-8')
locale.format('%d', 1000000, 1)

rawDescription: Add thousands separators to a number

description: Add thousands separators to a number


code: re.split(r'([a-zA-Z]+)', inputstring)

rawDescription: python split string into strings with same language characters

description: Python split string into strings with same language characters


code:
class MyWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
        self.setWindowOpacity(.9)
        text_widget = text(self)
        self.setCentralWidget(text_widget)
        self.setStyleSheet("QMainWindow { background: 'black'}");

rawDescription: PyQt - Widget in QtGui.QtMainWindow

description: PyQt - Widget in QtGui.QtMainWindow


code:
newlist = []
for i in range(len(mylist)/size_x):
    newlist.append(''.join(mylist[i*size_x:(i+1)*size_x]))

rawDescription: Printing list with set width in python

description: Printing list with set width in python


code: [c_dir + "\\" if not c_dir.endswith("\\") else c_dir for c_dir in (met_dir, out_dir)]

rawDescription: Update elements of a list

description: Update elements of a list


code:
Username0:Password0

Username1:Password1

Username2:Password2

Username3:Password3

rawDescription: Python - Writing to a new file from another file


code: value |= flag

rawDescription: Python equivalent of C# Enum.HasFlag

description: Python equivalent of C# Enum.HasFlag


code: droid.eventPost("echo", "hello world")

rawDescription: send events from python to javascript using sl4a

description: Send events from python to javascript using sl4a


code:
x = df2.swaplevel('variable1','variable3',axis=1)
x['y'] = x['y'].ffill()
x.swaplevel('variable3','variable1',axis=1)

rawDescription: pandas - selecting a lower level in a DataFrame to do a ffill

description: Pandas - selecting a lower level in a DataFrame to do a ffill


code:
import itertools
map(''.join, itertools.product('ABC', repeat=3))

rawDescription: How to generate all possible strings in python?

description: Generate all possible strings in python


code:
l = symbols('x1 x2 x3')
Matrix(3, 3, lambda i,j: l[i] if i==j else 1)

rawDescription: How to replace the the diagonal elements of a matrix by a vector in SymPy?

description: Replace the the diagonal elements of a matrix by a vector in SymPy


code:
from sqlalchemy.orm.attributes import manager_of_class
from sqlalchemy.orm.properties import ColumnProperty

def get_state_dict(instance):
    cls = type(instance)
    mgr = manager_of_class(cls)
    return dict((key, getattr(instance, key))
                for key, attr in mgr.iteritems()
                if isinstance(attr.property, ColumnProperty))

rawDescription: Recreate some sqlalchemy object

description: Recreate some sqlalchemy object


code:
#SciPy Sparse linear algebra takes care of sparse matrix computations
#http://docs.scipy.org/doc/scipy/reference/sparse.linalg.html
import scipy.sparse as sp
import scipy.sparse.linalg  as la

import numpy as np
import matplotlib.pyplot as plt

#discretize x-axis
N = 1100
x = np.linspace(-100,100,N)
V = x * x / 2.
h = len(x)/(N)
hi2 = 1./(h**2)

#discretize Schroedinger Equation, i.e. build
#banded matrix from difference equation
C0 = np.ones(N)*30. + V
C1 = np.ones(N) * -16.
C2 = np.ones(N) * 1.

H = sp.spdiags([C2, C1, C0, C1, C2],[-2,-1,0,1,2], N, N)
H *= hi2 * (- 1./12.) * (- 1. / 2.)
H = sp.csr_matrix(H)
H[0,0] = 0
H[0,1] = 0
H[0,2] = 0

#check structure of H
plt.figure()
plt.spy(H)
plt.show()

EV = la.eigsh(H,return_eigenvectors = False)

rawDescription: How to change elements in sparse matrix in Python's SciPy?

description: Change elements in sparse matrix in Python's SciPy


code: cdefefghi

rawDescription: python replace records in variable

description: Python replace records in variable


code:
def set_pyqtproperties(klass, properties, proxy='user'):
    def make_prop(prop):
        def property_(self):
            return getattr(getattr(self, proxy), 'get_' + prop)
        property_.__name__ = prop
        return property_

    if isinstance(properties, basestring):
       properties = properties.split()
    for prop in properties:
         setattr(klass, prop, pyqtProperty(QVariant, make_prop(prop)))

rawDescription: Can anyone help condense this Python code?


code:
filter(None, l)
filter(bool, l)

rawDescription: Removing empty elements from an array in Python


code:
clicmd = "/usr/local/bin/cli -s 10.1.213.226 -n Administrator -p password"
cliargs = shlex.split(clicmd)

rawDescription: Interactively writing/reading from an external program


code:
NUMBERS = [4, 8, 6, 2, 15, 50]
TARGET = 590

initial = set(map(Expr, NUMBERS))
SearchTrees(initial, TARGET)

rawDescription: Generate equation with the result value closest to the requested one, have speed problems


code:
room = "|                     |"
chest = "[Chest]"
door = "Door"
wall = "---------------------"
trap = "-"
you = "You"
table = "[Table]"

def print_room():
    print(insert_object(wall,1))
    print(insert_object(chest,3))
    print(insert_object(trap,20))
    print(insert_object(door,18))
    print(insert_object(trap,21))
    print(insert_object(table,4))
    print(insert_object(you,3))
    print(insert_object(wall,1))

rawDescription: How do I format code to print with the same width every time?

description: Format code to print with the same width every time


code:
def italic(predecessor):
    x = predecessor
    x2 = x
    x3 = x2
    def successor():
        return "<italic/>" + x3() + "</italic>"
    return successor

rawDescription: A better way to write python closures?

description: A better way to write python closures


code:
urlpatterns += patterns('',
    (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)

rawDescription: Django Http404 and DEBUG mode catch 22

description: Django Http404 and DEBUG mode catch 22


code:
l = list(filter(lambda x: x > 3, n))

len(l)  # <--

rawDescription: How to find the length of a "filter" object in python

description: Find the length of a "filter" object in python


code:
def my_function(c, r):
    lines = []
    if c and r:
        lines.append("*" * c)

        for i in range(r - 2):
            if c == 1:
                lines.append("*")
            else:
                lines.append("*" + " " * (c - 2) + "*")

        if r > 1:
            lines.append("*" * c)

    return '\n'.join(lines)

rawDescription: How to return multiple values in Python?

description: Return multiple values in Python