Showing posts with label core python. Show all posts
Showing posts with label core python. Show all posts

Jan 30, 2012

Logging

import logging
import logging.handlers

# Sets up a basic textfile log, with formatting
logging.basicConfig(level=logging.DEBUG,
   format='%(asctime)s %(levelname)-8s %(message)s',
   datefmt='%m/%d/%y %H:%M:%S',
   filename=r'C:\temp\mylog.log',
   filemode='a')

# Log a few different events
logging.info('Just testing the water.')
logging.warning('Hmm, something is not right here')
logging.error("Oh no, now you're in for it")
The resulting text log:
02/14/08 22:19:03 INFO     Just testing the water.
02/14/08 22:19:03 WARNING  Hmm, something is not right here
02/14/08 22:19:03 ERROR    Oh no, now you're in for it
Add a few more lines and it sends you an email for any logs that are level "ERROR" or above:
email = logging.handlers.SMTPHandler('smtp.foo.com',
   'script@foo.com',('techart@bar.com'),'Error Report')
email.setLevel(logging.ERROR)

logging.getLogger('').addHandler(email)

Python String Templates

import string
template = string.Template("The $speed $color $thing1")
template.substitute(speed='quick', color='brown', thing1='fox')
# 'The quick brown fox'

Jan 19, 2012

Python Standard Library: Collections

Counter

Dict subclass for counting hashable items. Sometimes called a bag or multiset. Elements are stored as dictionary keys and their counts are stored as dictionary values.
Counter(['a', 'b', 'c', 'a'])
# Counter({'a': 2, 'c': 1, 'b': 1})

namedtuple

Returns a new subclass of tuple with named fields.
Point = namedtuple('Point', 'x y z')
Point.__doc__
# 'Point(x, y, z)'
p = Point(x=1, y=2, z=3)
print p.x, p.y, p.z
# 1 2 3

Python Standard Library: Functools

partial

New function with partial application of the given arguments and keywords
from functools import partial
def f(a, b=2):
    print a, b

f1 = partial(f, 'fixed_a')
f1(b=777)
# fixed_a 777

f2 = partial(f, b='fixed_b')
f2(777)
# 777 fixed_b

Jan 18, 2012

Python Standard Library: Itertools

chain

Return a chain object whose .next() method returns elements from the first iterable until it is exhausted, then elements from the next iterable, until all of the iterables are exhausted.
[x for x in chain([1, 2],[3, 4])]
# [1, 2, 3, 4]

izip

Works like the zip() function but consumes less memory by returning an iterator instead of a list.
[x for x in izip([1, 2],[3, 4])]
# [(1, 3), (2, 4)]

[x for x in izip([1, 2, 4],[3, 4])]
# [(1, 3), (2, 4)]

islice

Works like the slice() function but consumes less memory by returning an iterator instead of a list.
[x for x in islice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 4)] # first n-elements
# [0, 1, 2, 3]

[x for x in islice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 2, 5)]
# [2, 3, 4]

[x for x in islice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 0, 10, 3)]
# [0, 3, 6, 9]

#same with count()
from itertools import islice, count
[x for x in islice(count(), 0, 10, 3)]
# [0, 3, 6, 9]

imap

Like map() except that it returns an iterator instead of a list and that it stops when the shortest iterable is exhausted instead of filling in None for shorter iterables.
[x for x in imap(lambda x: 2*x, [1,2,3])]
# [2, 4, 6]

combination

Return successive r-length combinations of elements in the iterable.
[x for x in combinations([1, 2, 3], 1)]
# [(1,), (2,), (3,)]

[x for x in combinations([1, 2, 3], 2)]
# [(1, 2), (1, 3), (2, 3)]

[x for x in combinations([1, 2, 3], 3)]
# [(1, 2, 3)]

[x for x in combinations([1, 2, 3], 4)]
# []

cycle

Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely.
for x in cycle(['one', 'two', 'three']):
    print x
  
one
two
three
one
two
three
one
two
......

repeat

Create an iterator which returns the element for the specified number of times. If not specified, returns the element endlessly.
for x in repeat(['one', 'two', 'three'], 2):
    print x
['one', 'two', 'three']
['one', 'two', 'three']
Much faster then
for x in ['one', 'two', 'three'] * 2:
    print x

Dec 18, 2011

Negative round

>>> str(round(1234.5678, -2))
'1200.0'

Operator overloading for the set builtin

>>> a = set([1,2,3,4])
>>> b = set([3,4,5,6])
>>> a | b # Union
{1, 2, 3, 4, 5, 6}
>>> a & b # Intersection
{3, 4}
>>> a < b # Subset
False
>>> a - b # Difference
{1, 2}
>>> a ^ b # Symmetric Difference
{1, 2, 5, 6}

Dec 13, 2011

Date, Time

Pythonic way to add date.timedate and datetime.time objects


>>> datetime.datetime.combine(datetime.date(2011, 01, 01), datetime.time(10, 23))
datetime.datetime(2011, 1, 1, 10, 23)

Aug 16, 2011

Python

>>> dir('s')
>>> help('s'.rfind)

>>> a={1, 2, 3}
>>> b={2, 3, 4}
>>> a & b
set([2, 3])
>>> a | b
set([1, 2, 3, 4])
>>> a - b
set([1])

# Конкантенация
>>>  "a" 'b'
'ab'

# Третий предел
>>> s="asdfghjklqwerty"
>>> s[::3]
'afjqr'

# Удалять можно сразу срез
l = [0, 1, 2, 3, 4, 5, 6]
>>> del l[2:]
или
>>> del l[::2]

# Генерация словаря
>>> v = [1, 2, 3]
>>> k = ['a', 'b', 'c']
>>> dict(zip(k,v))
{'a': 1, 'b': 2, 'c': 3}
>>> dict.fromkeys(k)
{'a': None, 'b': None, 'c': None}
>>> dict.fromkeys(k, 0)
{'a': None, 'b': 0, 'c': 0}
>>> dict(a = 1, b = 2)
{'a': 1, 'b': 2}

>>> {'a': 1, 'b': 2, 'c': 3}.items()
[('a', 1), ('c', 3), ('b', 2)]

Jul 6, 2011

Notes

'a' + 'b' # slow
'%s%s' % ('a', 'b') # faster

>> 'an' in 'Django'
True

>> ['a', 'b', 'c'] * 2
['a', 'b', 'c', 'a', 'b', 'c']

>> [x for x in range(10) if x % 2 == 0]
[0, 2, 4, 6, 8]

{'a' : 123, 'b' : '345'}['c'] # KeyError

{'a' : 123, 'b' : '345'}.get('c', 'N/A') # {'a' : 123, 'b' : '345', 'c' : 'N/A'}
'N/A'

Jun 20, 2011

Удаление элементов из списка

a=range(10);
for item in a:
    if item < 5:
        a.remove(item)
print a 

# Вернет [1, 3, 5, 6, 7, 8, 9]
Почему так происходит? Потому что при удалении элемента из списка, индекс не уменьшается. А значит, следующий элемент списка будет пропущен. Отчаявшись, люди идут на такие ухищрения:
i = 0
while i < len(a):
    if i < 5:
        del a[i]
    else:
        i += 1
Нам на помощь приходит такая замечательная функция как filter(func, a). Она создает новый список из элементов списка, для которым функция func(item) вернет истину.
filter(lambda x: x <= 5, a)
# Вернет [6, 7, 8, 9]
[i for i in a if i >= 5]
# Также вернет[6, 7, 8, 9], да и выглядит красивее.
print a # Список a остался неизменным

Итерация по спискам

for i in range(len(a)):
    print "Под номером %d находится элемент %s" % (i, a[i])
Это работает, но что мы сделали лишнего: посчитали длину списка и создали еще один список, с длиной равной длине списка a. Нас немного спасет xrange, но правильнее от этого не станет. Если вам и правда необходимы индексы элементов, используйте enumerate.
for i, item in enumerate(a):
    print "Под номером %d находится элемент %s" % (i, item)
enumerate(a) не создает лишних списков, он возвращает поочередно элементы списка в виде (<индекс>, <элемент>). Да и выглядит такая конструкция гораздо понятнее.

Encription (len=128)

import hashlib
o = hashlib.sha512("22222@aaa.com" + str(datetime.datetime.now()))
o.hexdigest()