Skip to content

Instantly share code, notes, and snippets.

# recursive_dictionary.py
# Created 2009-05-20 by Jannis Andrija Schnitzer.
#
# Copyright (c) 2009 Jannis Andrija Schnitzer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# "Understanding Python's closures".
#
# Tested in Python 3.1.2
#
# General points:
#
# 1. Closured lexical environments are stored
# in the property __closure__ of a function
#
# 2. If a function does not use free variables
def namedlist(typename, field_names):
"""Returns a new subclass of list with named fields.
>>> Point = namedlist('Point', ('x', 'y'))
>>> Point.__doc__ # docstring for the new class
'Point(x, y)'
>>> p = Point(11, y=22) # instantiate with positional args or keywords
>>> p[0] + p[1] # indexable like a plain list
33
>>> x, y = p # unpack like a regular list
@sapamja
sapamja / py_faq.md
Last active August 29, 2015 13:57
PYthon FAQ forum.

Dear all,

I am trying to start a FAQ kind of forum for python related questions.

All questions are mostly related to python version 2.7. Some question may not be applicable to 3.x and above.

  1. Why can't you delete a dictionary key while doing dict.iteritems() ?
  2. And why is it allow you to delete dictionary key while doing dict.items() ?
  3. Difference between a name startswith single underscore vs double underscore ?
  4. What is first class object in python ?
@sapamja
sapamja / duplicate_counts.py
Last active August 29, 2015 13:57
Profiling various way of counting repeated item from python list
#!/usr/bin/python
import cProfile
from faker import Faker
from timeit import Timer
from collections import Counter
from collections import defaultdict
# old style using dictionary:
def get_count1(sequence):
@sapamja
sapamja / word_lookup.py
Last active August 29, 2015 13:58
Profiling words lookup in long sentence.
#!/usr/bin/python
import cProfile
from timeit import Timer
from faker import Faker
def func1(sentence, words):
return any(word in sentence for word in words)
@sapamja
sapamja / func_return_compare.py
Created April 6, 2014 08:40
comparing function return assigning variable vs not.
#!/usr/bin/python
from memory_profiler import profile
@profile
def func1(a):
if a > 0:
num = 1
else:
num = -1
return num
@sapamja
sapamja / seive_prime.py
Created April 7, 2014 06:36
Seive_Prime in python
#!/usr/bin/python
# inserting 1 for every prime and 0 of non prime within a given range
def prime_list(n):
pr = [-1] * n
ind = 2
pr[0] = pr[1] = 0
pr[2] = 1
@sapamja
sapamja / merge_list.py
Created April 14, 2014 14:04
Merging list
Output:
f1 102011 function calls in 0.701 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.701 0.701 <string>:1(<module>)
1000 0.040 0.000 0.495 0.000 merge.py:3(f1)
1000 0.001 0.000 0.495 0.000 merge.py:42(<lambda>)
@sapamja
sapamja / concantenate_string_timeit.py
Last active August 29, 2015 14:00
Fastest way to concatenate string in python
#!/usr/bin/python
import cProfile
from faker import Faker
from timeit import Timer
from UserString import MutableString
from cStringIO import StringIO
#from memory_profiler import profile
def func1(List):
"""Naive appending"""