Python http://www.trypython.org/
http://www.aleax.it/python_mat_en.html Alex Martelli
http://ru.wikiversity.org/wiki/Программирование_и_научные_вычисления_на_языке_Python
http://habrahabr.ru/blogs/python/84330/#habracut
http://faassen.n--tree.net/blog/view/weblog/2005/08/06/0
http://panela.blog-city.com/dear_lazyweb_best_of_breed_flow_basedpipeline_programming_.htm
Pipeline programming and workflow in Python
IDE
http://ipython.scipy.org/moin/
http://bpython-interpreter.org/
http://dreampie.sourceforge.net/
http://code.google.com/p/spyderlib/
http://code.google.com/p/ulipad/
http://pythonide.blogspot.com/
http://code.google.com/p/pyscripter/
IntelliJ PyPe Komodo NetBeans
http://github.com/xolox/vim-pyref#readme VIM plagin to call python doc on F1
http://sebsauvage.net/python/snyppets/
http://aspn.activestate.com/ASPN/Cookbook/Python/
http://www.programmersheaven.com/zone40/index.htm
http://aima.cs.berkeley.edu/python/readme.html
http://pleac.sourceforge.net/pleac_python/ Perl Cookbook in Python
http://code.google.com/p/aima-python/ Norvig. Artificial Intelligence
Python /C++ integration: http://wiki.cython.org/WrappingCorCpp
http://sayspy.blogspot.com/2010/03/various-ways-of-distributing-python.html
http://hashedbits.com/how-to-distribute-commercial-python-applications/
http://aymanh.com/python-debugging-techniques
http://pythonconquerstheuniverse.wordpress.com/2009/09/10/debugging-in-python/
http://jblevins.org/computing/python/scipy-overview SciPy,SAGE,SimPy,NumPy
http://code.google.com/p/python-statlib/ Statlib for python
http://amath.colorado.edu/faculty/fperez/python/scicomp/
http://www.python.org/doc/essays/graphs Graphs in Python
http://buzhug.sourceforge.net pure-Python database engine,
http://www.secnetix.de/~olli/Python/block_indentation.hawk IntendationExplained
http://www.ibm.com/developerworks/linux/library/l-python-mechanize-beautiful-soup/index.html
http://www.example-code.com/python/pythonspider.asp
http://dev.scrapy.org/
http://theanti9.wordpress.com/2009/02/14/python-web-crawler-in-less-than-50-lines/
http://code.activestate.com/recipes/576551/
Serve the current directory at http://localhost:8000/
$python -m SimpleHTTPServer
This one-liner starts a web server on port 8000 with the contents
of current directory on all the interfaces (address 0.0.0.0), not just localhost.
If you have “index.html” or “index.htm” files, it will serve those,
otherwise it will list the contents of the currently working directory.
It works because python comes with a standard module called SimpleHTTPServer.
The -m argument makes python to search for a module named SimpleHTTPServer.py
in all the possible system locations
(listed in sys.path and $PYTHONPATH shell variable).
Once found, it executes it as a script.
If you look at the source code of this module, you’ll find that this module tests if it’s run as a script if __name__ == '__main__', and if it is, it runs the test() method that makes it run a web server in the current directory.
To use a different port, specify it as the next argument:
$ python -m SimpleHTTPServer 8080
This command runs a HTTP server on all local interfaces on port 8080.
http://wiki.sympy.org/wiki/Main_Page http://code.google.com/p/sympy/
sed 's/\t/ /g' oldcode.py >newcode.py
Simply using the vim command,
:retab
from subprocess import Popen,PIPE
p = Popen(("yourProgram","arg1","arg2"),stdout=PIPE)
print "exit code",p.wait()
print "stdout:"
print p.stdout.read()
http://www.fantascienza.net/leonardo/ar/python_best_practices.html
lambda
def add(x,y):
return x+y
inc = lambda x: add(x,1)
def incrementer(n):
return lambda x: add(x,n)
inc(3) # выдаст 4
f = incrementer(3)
f(5) # выдаст 8.
Другие
типичные конструкции, позаимствованные из функциональных языков - это
стандартные функции map,filter,zip и специальная форма, называемая
списочным сокращением (list comprehension).
Функция map(f,list) возвращает список значений, полученный применением функции f к каждому элементу списка list. Пример:
>>> def sqr(x):
... return x*x
...
>>> map(sqr,[1,2,3])
[1, 4, 9]
Функция filter(p,list) возвращает список из только тех элементов списка list, для которых функция p возвращает истину. Пример:
>>> def even(n):
... return n%2 == 0
...
>>> filter(even,[1,2,3,4,5,6])
[2, 4, 6]
Функция zip принимает два (или больше) списка и возвращает список пар
(кортежей), составленных из соответствующих элементов списков:
>>> zip([1,2,3,4],["A","B","C","D"],['x','y','z','t'])
[(1, 'A', 'x'), (2, 'B', 'y'), (3, 'C', 'z'), (4, 'D', 't')]
List comprehensions позволяют одной строкой создать списки по какому-нибудь правилу:
>>> [x*x for x in [1,2,3,4,5]]
[1, 4, 9, 16, 25]
Можно тут же отбирать нужные элементы списка:
>>> [x*x for x in [1,2,3,4,5] if even(x)]
[4, 16]
Можно перебирать одновременно несколько последовательностей:
>>> l = [1,2,3,4,5]
>>> [x*y for x in l for y in l]
[1, 2, 3, 4, 5, 2, 4, 6, 8, 10, 3, 6, 9, 12, 15, 4, 8, 12, 16, 20, 5, 10, 15, 20, 25]
Pylint/PyChecker/PyFlakes PythonTidy virtualenv pydb
http://code.google.com/p/python-nose/ nose - for testing
http://code.google.com/p/pythontracer/ Pythontracer
http://source.pythonxy.com/spyder/doc/index.html IDE
Don’t use the tab character when editing Python code. Have your editor fill the tab with spaces to keep everything uniform.
%run myscript.py args...
This is like just running it at the system shell as
python myscript.py args...
except that you get much faster cycling if modules have long import times , much better exception tracebacks, the ability to fire up the debugger after an exception just by typing %debug, etc. Furthermore, all variables defined in the script are now in the interactive namespace, where they can be further explored, manipulated, etc.
In summary, all 'real code' lives always in scripts, and the interactive session is the flexible experimentation playground.
If you ever end up writing in ipython a bit of 'real code', you just type
%hist -n
to get your history printed without line numbers, and you can just copy and paste it back into an editor.
In the most recent/svn ipython you can do
%doctest_mode
and ipython flips its prompts to '>>>' ones so you can develop your doctests in a real, running session to then paste them into your code. Furthermore, you can actually paste real doctests *input* (without the output) at the ipython prompt, and it will strip all the leading '>>>' and execute it correctly. This lets you do interactive development of doctests conveniently.
Finally, you can also do (feel free to put this into your init file):
from IPython.dtutils import *
and then type
idoctest()
This function will then let you paste entire doctests (input *and* outptut) at the terminal, and will run them for you with the real doctest runner.
graphTK barTK sorting fileHandling timing
http://www.forum.script-coding.info/viewforum.php?id=6
#Синхронный запуск консольной команды с получением её объединенного потока вывода и ошибок, с установкой текущего каталога
import subprocess
stdOutErr = subprocess.Popen(['cmd', 'cmd', '/C', 'dir', '/Q'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd='C:\\Program Files').stdout
for line in stdOutErr:
print line
#Асинхронный запуск нового независимого процесса, например, GUI-программы, с параметрами:
import os
params = ['notepad', 'C:\\Temp\\test.txt']
os.spawnv(os.P_NOWAIT, os.environ['SystemRoot'] + '\\system32\\notepad.exe', params)
print 'Process is started.'
# случайные числа
import random
# случайное целое в указанном диапазоне
print random.randint(1, 10)
# случайный символ из указанных
print random.choice('ABC')
# проверка на "пустоту" переменной
myvar = None
if not myvar:
print "variable \"myvar\" is empty"
# аргументы функций со значениями по умолчанию
def myfunc(myvar = "j"):
return myvar + "s"
print(myfunc())
print(myfunc("q"))
# аргументы функций со значениями по умолчанию,
# особенности работы с mutable-объектами (списки, словари)
def myfunc(myvar=[]):
myvar.append("f")
return myvar
print myfunc(), myfunc(), myfunc()
def myfunc(myvar = None):
if myvar is None:
myvar = []
myvar.append("f")
return myvar
print myfunc(), myfunc(), myfunc()
# присвоение "истинного" значения
b = 0; c = "text"
myvar = b or c
print "myvar=",myvar
# использование переменной, только если она определена
try:
myvar = NotDefinedVariable
except NameError:
myvar = "exception happened because we do not know that is NotDefinedVariable"
print "myvar=",myvar
# определение текущего пользователя for any OS
import getpass
user = getpass.getuser()
print "user=",user
import sys
def process(...):
.....
def main(args):
.....
try:
process(...)
except EnvironmentError, e:
die("OS problem: "+str(e))
except MyError, e:
die(str(e))
....
if __name__ == "__main__":
main(sys.argv[1:])
#! /usr/bin/env python
"""
this canonicalcat is an example of writing a python program that perhaps
you want to distribute or will be maintained over time. As such one
should add documentation and this is an example of module level
documentation. If one were to import canonicalcat and type
`help(canonicalcat)` it would spit this out
"""
# These are the imports from the python stdlib
import sys
import logging
import optparse
# Some like to separate additional libraries from the standard ones
# file meta data
__version__ = "0.1"
__author__ = "matt harrison"
__license__ = "psf"
# GLOBAL ARGS/CONSTS
# Since python doesn't have constants, we can emulate them
# using (naming) conventions. Put them here
# GLOBAL INIT
logging.basicConfig(filename=".concat.log", level=logging.DEBUG)
def cat_file(fin, outfile=None):
"""
Main logic to cat file
>>> import StringIO
>>> fout = StringIO.StringIO()
>>> cat_file("small.txt", fout)
>>> lines = fout.getvalue()
>>> lines
'foo\nbar\n'
"""
logging.log(logging.DEBUG, "CONCAT: %s"% fin)
if outfile is None:
fout = sys.stdout
elif isinstance(outfile, str):
fout = open(outfile, 'w')
else:
fout = outfile
for line in open(fin):
fout.write(line)
def _test():
import doctest
doctest.testmod()
def main(prog_args=None):
"""
A main function. Rather than putting this logic into the the if
__name__ statement below, creating a main function allows other
programs to use main logic. It also allows for testing, by
passing in args rather than monkey patching sys.argv (which won't
be thread safe).
>>> main(["canonicalcat.py", "small.txt"])
foo
bar
"""
if prog_args is None:
prog_args = sys.argv
parser = optparse.OptionParser()
parser.usage = """A python implementation of 'cat', default use is to
provide a filename to cat"""
parser.add_option("-o", "--output-file", dest="fileout",
help="specify file to cat to (default is stdout)")
parser.add_option("-t", "--test", dest="test", action="store_true",
help="run doctests")
opt, args = parser.parse_args(prog_args)
if args[1:]:
cat_file(args[1], opt.fileout)
if opt.test:
_test()
else:
parser.print_help()
if __name__ == "__main__":
# when one "exectutes" a python program, it's __name__ is
# __main__, otherwise it's name will be the module name
main()
http://pypi.python.org/pypi http://www.pyobject.ru/blog/
collections.defaultdict versus dict
list in Python is like an array (@)in Perl or ArrayList class in Java
dictionary in Python is like a hash (%) in Perl or HashTable class in Java
dir(myObj) will give all information about myObj; another way is: myObj.__dict__
FreeBSD: portinstall py25-имя-пакета.
Ubuntu: sudo apt-get install python-имя-пакета.
Fedora: это yum search/ yum install.
Если там есть setup.py, то setup.py install.
http://www.pyobject.ru/blog/post/cooking-python-eggs
Package will be installed in /usr/lib/python2.4/site-packages
Если setpu.py is missing, то его надо положить куда--нибудь в [path for path in sys.path if path.endswith('site-packages'].
В линуксе это если есть /usr/local/lib/pythonXX/site-packages, иначе /usr/lib/pythonXX/site-packages.
Если таким образом поставить setuptools, то появится easy_install, c которым вообще все ставится вот так:
easy_install SQLAlchemy
Для установки некоторых пакетов достаточно скопировать их в папку site-packages (она используется Питоном по умолчанию).
Для установки в другую папку добавьте переменную окружения
PYTHONPATH и добавьте в нее путь к пакету.
#!/usr/bin/env python
import subprocess
subprocess.call("ls -l", shell=True)
Python allows you to add static methods to a class. Just decorate your method using the ’staticmethod’ decorator!
Python has no switch/case construct. There’s a very neat way to implement this though by simply using a dict and function objects (or lambda functions). An example:
def handle_one():
return 'one'
def handle_two():
return 'two'
def handle_default():
return 'unknown'
cases = {
'one': handle_one,
'two': handle_two,
'three': lambda: 'three',
}
for i in ('one', 'two', 'three', 'four', ):
handler = cases.get(i, handle_default)
print handler()
We’re using the dict.get method here, which can take an optional ‘default’ argument.
File: example.c
---------------------
#include <stdio.h>
#include <stdlib.h>
int cube( int n )
{
return n * n * n;
}
File: example.i
----------------------
%module example
%{
extern int cube( int n );
%}
%include example.c
------------------------
swig -python example.i
After the command above 2 files will be created: example_wrap.c or example_wrap.cxx and a Python source file example.py
gcc -c example.c example_wrap.c -I/usr/include/python2.4 -L/usr/lib/python2.4
ld -shared -o _example.so example.o example_wrap.o
python
>>> import example
>>> example.cube(3)
27
http://www.swig.org/Doc1.3/Python.html
http://sourceforge.net/mailarchive/forum.php?forum_name=swig-user
File: stl_example.i
----------------------
%module stl_example
%{
#define SWIG_FILE_WITH_INIT
#include "stl_example.h"
%}
%include "std_vector.i"
// Instantiate templates used by example
namespace std {
%template(IntVector) vector<int>;
%template(DoubleVector) vector<double>;
}
// Include the header file with above prototypes
%include "stl_example.h"
-------------------------
The #define SWIG_FILE_WITH_INIT line above inserts a macro that specifies that the resulting C file should be built as a python extension, inserting the module init code
File: stl_example.h
----------------
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
double average(std::vector<int> v) {
return std::accumulate(v.begin(),v.end(),0.0)/v.size();
}
std::vector<double> half(const std::vector<double>& v) {
std::vector<double> w(v);
for (unsigned int i=0; i<w.size(); i++)
w[i] /= 2.0;
return w;
}
void halve_in_place(std::vector<double>& v) {
std::transform(v.begin(),v.end(),v.begin(),
std::bind2nd(std::divides<double>(),2.0));
}
------
swig -python -c++ stl_example.i
g++ -c stl_example.cpp stl_example_wrap.cxx -I/usr/include/python2.4 -L/usr/lib/python2.4
g++ -shared stl_example.o -o _stl_example.so
import Region
r=Region.Region()
r.append(1,3)
r._print()
-------------
Large files sorting -Unix way:
sort inputfile -o outputfile
The first thing I would try is just doing a
for line in file:
pass
to see how much time is consumed merely by iterating over the file. This should give you a baseline from which you can base your timings
Split the file into chunks that are small enough to fit in memory, sort each chunk
and write it to a file and then interleave the chunks. Below is a
cheap and cheesy outline of code to do this, from which you can start.
For files which are hugely larger than your available memory you can
do this recursively but for files which are 10 to 100 times too big
the single-pass code below will probably work just fine. The
complexity is technically O(n.(log(c)+(n/c))) where n is the size of
input and c is the chunk size; once n/c (the number of chunks) exceeds
log(c) the cost of merging the chunks will start to dominate, though a
recursive version would be slowed by needing a lot more disc access.
#!/usr/bin/env python
from itertools import islice
from tempfile import TemporaryFile
import sys
# Tweak this number to fill your memory
lines_per_chunk = 100000
chunkfiles = []
mergechunks = []
while True:
chunk = list(islice(sys.stdin, lines_per_chunk))
if not chunk:
break
chunk.sort()
f = TemporaryFile()
f.writelines(chunk)
f.seek(0)
mergechunks.append((chunk[0], len(chunkfiles)))
chunkfiles.append(f)
while mergechunks:
# The next line is order O(n) in the number of chunks
(line, fileindex) = min(mergechunks)
mergechunks.remove((line, fileindex))
sys.stdout.write(line)
nextline = chunkfiles[fileindex].readline()
if nextline == "":
chunkfiles[fileindex].close()
else:
mergechunks.append((nextline, fileindex))
http://refactormycode.com/codes/176-count-word-occurrences-in-a-string
# -- Julian Tibble
import re
# "Word -> Number of occurences" mapping
wordcount = {}
# Regular expression to match a word
re_word = re.compile('[a-z]+')
# Read in whole file and split into words
try:
while 1:
line = raw_input().lower()
line = re_word.findall(line)
for word in line:
if wordcount.has_key(word):
wordcount[word] += 1
else:
wordcount[word] = 1
except EOFError:
pass
# Order by descending number of occurences
sortable = zip(wordcount.values(), wordcount.keys())
sortable.sort()
sortable.reverse()
for n,w in sortable:
print w,n
# tested with Python2.4.3 HAB
import re# this one in honor of 4th July, or pick text file you have!!!!!!!
filename = 'NationalAnthemUSA.txt'
# create list of lower case words, \s+ --> match any whitespace(s)
# you can replace file(filename).read() with given string
word_list = re.split('\s+', file(filename).read().lower())
print 'Words in text:', len(word_list)
# create dictionary of word:frequency pairs
freq_dic = {}
# punctuation marks to be removed
punctuation = re.compile(r'[.?!,":;]')
for word in word_list:
# remove punctuation marks
word = punctuation.sub("", word)
# form dictionary
try:
freq_dic[word] += 1
except:
freq_dic[word] = 1 print 'Unique words:', len(freq_dic) # create list of (key, val) tuple pairs freq_list = freq_dic.items() # sort by key or word freq_list.sort() # display result for word, freq in freq_list:
print word, freq
# set all the counters to zero
lines, blanklines, sentences, words = 0, 0, 0, 0
print '-' * 50
try:
# use a text file you have, or google for this one ...
filename = 'GettysburgAddress.txt'
textf = open(filename, 'r')
except IOError:
print 'Cannot open file %s for reading' % filename
import sys
sys.exit(0)
# reads one line at a time
for line in textf:
print line, # test
lines += 1
if line.startswith('\n'):
blanklines += 1
else:
# assume that each sentence ends with . or ! or ?
# so simply count these characters
sentences += line.count('.') + line.count('!') + line.count('?')
# create a list of words
# use None to split at any whitespace regardless of length
# so for instance double space counts as one space
tempwords = line.split(None)
print tempwords # test
# word total count
words += len(tempwords)
textf.close()
print '-' * 50
print "Lines : ", lines
print "Blank lines: ", blanklines
print "Sentences : ", sentences
print "Words : ", words
# optional console wait for keypress
from msvcrt import getch
getch()
words = [line.rstrip() for line in open("WORD.LST")]
words_set = set(words)
letters = "abcdefghijklmnopqrstuvwxyz"
repl = dict(zip(letters, letters[13:]+letters[:13]))
def rot13(word):
return "".join(repl.get(c, c) for c in word)
found = []
for word in words:
rotword = rot13(word)
if rotword in words_set:
found.append( (word, rotword) )
words_set.discard(word)
found.sort( key=lambda words:(len(words[0]), words[0]) )
for word, rotword in found:
print "%s\t%s"%(word, rotword)