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

Apr 29, 2014

click is a Python package for creating beautiful command line interfaces

click is a Python package for creating beautiful command line interfaces in a composable way with as little amount of code as necessary. It’s the “Command Line Interface Creation Kit”.

import click

@click.command()
@click.option('--count', default=1, help='number of greetings')
@click.option('--name', prompt='Your name',
              help='the person to greet', required=True)
def hello(count, name):
    for x in range(count):
        print('Hello %s!' % name)

if __name__ == '__main__':
    hello()
python click_test.py --help
Usage: click_test.py [OPTIONS] NAME

Options:
  --count=COUNT  number of greetings
  --help         Show this message and exit.

Jan 10, 2013

Python shell command

Execute code:
python -c "print 'hi.'"
# or
echo "print 'hi.'" | python '-'
# equal to
echo "print 'hi.'" | python -- -
Execute multiline code:
python '-' <<"EOF"
lines=2
print "\nThis script is %i lines long.\n" %(lines,)
EOF
Using json.tool to validate and pretty-print:
curl https://app01.nutshell.com/api/v1/json | python -m json.tool

Aug 6, 2012

Tox to test across Python versions

Tox as is a generic virtualenv management and test command line tool you can use for:
  • checking your package installs correctly with different Python versions and interpreters
  • running your tests in each of the environments, configuring your test tool of choice
  • acting as a frontend to Continuous Integration servers, greatly reducing boilerplate and merging CI and shell-based testing.

Jun 27, 2012

Install multiple Python versions locally

https://github.com/utahta/pythonbrew - Pythonbrew is a program to automate the building and installation of Python in the users $HOME.
https://github.com/akheron/multipy - Install multiple Python versions locally

Feb 20, 2012

Online Python Tutor, IDE & Debugging Tool

http://people.csail.mit.edu/pgbovine/python/ - learn Python by writing code and visualizing execution
ideone.com - online
repl.it - online, with source code

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

Nov 11, 2011

Python Enum

class Color(Enum):
    RED = EnumValue('R', 'Red')
    GREEN = EnumValue('G', 'Green')
    BLUE = EnumValue('B', 'Blue')
Where Enum from here django-stdfields models.py

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) не создает лишних списков, он возвращает поочередно элементы списка в виде (<индекс>, <элемент>). Да и выглядит такая конструкция гораздо понятнее.