Just see the next source code:
>>> text='0123456789'
>>> reverse_text=text[::-1]
>>> print reverse_text
9876543210
>>>
Is a blog about python programming language. You can see my work with python programming language, tutorials and news.
Just see the next source code:
>>> text='0123456789'
>>> reverse_text=text[::-1]
>>> print reverse_text
9876543210
>>>
A fractal is a mathematical set that has a fractal dimension that usually exceeds its topological dimension , see Fractal wikipedia.
Today I used my mind and also my python skills to make one fractal image.
I use Newton's method to make all points and PIL python module to save the result.
Let's see the source code and comments.
from PIL import Image
#size of image
imgx = 600
imgy = 400
#make image buffer
image = Image.new("RGB", (imgx, imgy))
# area of fractal
xa = -2.0
xb = 2.0
ya = -2.0
yb = 2.0
#define constants
max_iterations = 10 # max iterations allowed
step_derivat = 0.002e-1 # step size for numerical derivative
error = 5e-19 # max error allowed
# function will generate the newton fractal
def f(z): return z * z + complex(-0.31,0.031)
# draw derivate fractal for each y and x
for y in range(imgy):
zy = y * (yb - ya)/(imgy - 1) + ya
for x in range(imgx):
zx = x * (xb - xa)/(imgx - 1) + xa
z = complex(zx, zy)
for i in range(max_iterations):
# make complex numerical derivative
dz = (f(z + complex(step_derivat, step_derivat)) - f(z))/complex(step_derivat,step_derivat)
# Newton iteration see wikipedia
z0 = z - f(z)/dz
# stop to the error
if abs(z0 - z) < error:
break
z = z0
#I use modulo operation expression to do RGB colors of the pixels
image.putpixel((x, y), (i % 8 * 16, i % 4 * 32,i % 2 * 64))
#save the result
image.save("fractal.png", "PNG")
This is the final result of Newton fractal image:
The new version : PP 1.6.4 is released and working well.
What is PP python module?
PP is a python module which provides mechanism for parallel execution of python code on SMP and clusters.
SMP - systems with multiple processors or cores;
clusters - computers connected via network;
Read more Parallel Python.
The python module Flask is a small web framework.
Install the module using pip.
(my_new_env)[free-tutorials@free-tutorials ~]$ pip install Flask
Downloading/unpacking Flask
Downloading Flask-0.9.tar.gz (481kB): 481kB downloaded
Running setup.py egg_info for package Flask
warning: no files found matching '*' under directory 'tests'
...
...
Successfully installed Flask Werkzeug Jinja2
Cleaning up...
Create the next python script and save as : flask-web.py .
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
Run the python script:
(my_new_env)[free-tutorials@free-tutorials ~]$ python flask-web.py
* Running on http://127.0.0.1:5000/
127.0.0.1 - - [25/Jan/2013 00:10:35] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [25/Jan/2013 00:10:36] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [25/Jan/2013 00:10:36] "GET /robots.txt HTTP/1.1" 404 -
127.0.0.1 - - [25/Jan/2013 00:10:36] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [25/Jan/2013 00:10:36] "GET /robots.txt HTTP/1.1" 404 -
You will see something like this:
Also you can try the quickstart tutorial here.
Use pip and virtualenv and you can make sandboxed Python development environments.
With this tools such as pip and virtualenv you have total control over the development environment.
Why? Because if your project is developed by a team with mutiple developers then they prefer having exactly replicated environments.
Let's try some simple example commands:
1. Start your environment ( in my case is: my_new_env ).
[free-tutorials@free-tutorials ~]$ python virtualenv.py my_new_env
New python executable in my_new_env/bin/python
Installing setuptools..................done.
Installing pip.............done.
Activate your environment ( in my case is: my_new_env ).
[free-tutorials@free-tutorials ~]$ . my_new_env/bin/activate
Let's see the pip --help command :
(my_new_env)[free-tutorials@free-tutorials ~]$ pip --help
Usage: pip COMMAND [OPTIONS]
--version show program's version number and exit
-h, --help Show help
-v, --verbose Give more output
-q, --quiet Give less output
--log Log file where a complete (maximum verbosity)
record will be kept
--proxy Specify a proxy in the form
user:passwd@proxy.server:port. Note that the
user:password@ is optional and required only if
you are behind an authenticated proxy. If you
provide user@proxy.server:port then you will be
prompted for a password.
--timeout Set the socket timeout (default 15 seconds)
--exists-action
Default action when a path already exists. Use
this option more than one time to specify
another action if a certain option is not
available. Choices: (s)witch, (i)gnore, (w)ipe,
(b)ackup
Commands available:
bundle: Create pybundles (archives containing multiple packages)
freeze: Output all currently installed packages (exact versions) to stdout
help: Show available commands
install: Install packages
search: Search PyPI
uninstall: Uninstall packages
unzip: Unzip individual packages
zip: Zip individual packages
Now we will use freeze and install.
I will list all the pip packages used in my virtual environment.
(my_new_env)[free-tutorials@free-tutorials ~]$ pip freeze -l
PyOpenGL==3.0.2
PyOpenGL-accelerate==3.0.2
Put all the output in my_packages.txt file.
(my_new_env)[free-tutorials@free-tutorials ~]$ pip freeze -l > my_packages.txt
Install my packages from my_packages.txt .
(my_new_env)[free-tutorials@free-tutorials ~]$ pip install -r my_packages.txt
Requirement already satisfied (use --upgrade to upgrade): PyOpenGL==3.0.2 in
./my_new_env/lib/python2.7/site-packages (from -r my_packages.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): PyOpenGL-accelerate==3.0.2 in
./my_new_env/lib/python2.7/site-packages (from -r my_packages.txt (line 2))
Cleaning up...
Let's try now to find one python module : opencv .
(my_new_env)[free-tutorials@free-tutorials ~]$ pip search opencv
remotecv - remotecv is an OpenCV server for facial and
feature recognition
ctypes-opencv - ctypes-opencv - A Python wrapper for OpenCV using
ctypes
pyopencv - PyOpenCV - A Python wrapper for OpenCV 2.x using
Boost.Python and NumPy
opencv-cython - An alternative OpenCV wrapper
CVtypes - Python OpenCV wrapper using ctypes
Tippy - another Toolbox for Image Processing in PYthon,
based on OpenCV
You can see where the version of python you are using installs to by running it the next python code.
>>> import sys
>>> sys.prefix
'/home/free-tutorials/my_new_env'
>>> sys.exec_prefix
'/home/free-tutorials/my_new_env'
To leave your environment just type next command: $ deactivate.
I will come with new tutorials about pip and virtualenv .
See you later.
First you need to have the PIL python module and import this.
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from PIL import ImageFilter
Next will be this function.Basically will make and return one RGBA image data type.
def make_img(image, textColor, backgroundColor):
img = image.convert("RGBA")
img.putdata([textColor if value == 0 else backgroundColor
for value in image.getdata()])
return img
The next step is to set the text , font and the new image.
I use Arggotsc.ttf. You can use any TrueType font.
text = " Inca un script in python! "
font = ImageFont.truetype('Arggotsc.ttf', 55)
image = Image.new("1", font.getsize(text), '#FFF')
Now we can draw, add text, resize, bur the text and finally save the image.
draw = ImageDraw.Draw(image)
draw.text((0, 0), text, font=font)
image = image.resize([i for i in image.size], Image.NEAREST)
imgText = make_img(image, (200, 200, 200), (0, 0, 0, 0))
blur_img = make_img(image, (0, 0, 0), (0, 0, 0, 0))
for i in range(3):
blur_img = blur_img.filter(ImageFilter.BLUR)
blur_img.paste(imgText, (0, 0), imgText)
blur_img.save("text-img.png")
The result is:
See you later with another tutorial.
An easy_install replacement is pip.
So pip installs packages and managing Python packages.
Let's try to install packages.
$ pip install PyOpenGL_accelerate
bash: pip: command not found
Install the pip package.
The recommended way to use pip is within virtualenv, since every virtualenv has pip installed in it automatically.
[free-tutorials@free-tutorials ~]$
curl -O https://raw.github.com/pypa/virtualenv/master/virtualenv.py
...
[free-tutorials@free-tutorials ~]$ python virtualenv.py my_new_env
New python executable in my_new_env/bin/python
Installing setuptools............................done.
Installing pip.....................done.
[free-tutorials@free-tutorials ~]$ . my_new_env/bin/activate
(my_new_env)[free-tutorials@free-tutorials ~]$ pip --help
Let's try again with PyOpenGL and PyOpenGL_accelerate packages.
(my_new_env)[free-tutorials@free-tutorials ~]$
pip install PyOpenGL PyOpenGL_accelerate
Downloading/unpacking PyOpenGL
Downloading PyOpenGL-3.0.2.tar.gz (891kB): 891kB downloaded
Running setup.py egg_info for package PyOpenGL
....
Now we can test this two packages.First is PyOpenGL.
(my_new_env)[free-tutorials@free-tutorials ~]$ python
Python 2.7.3 (default, Dec 6 2012, 03:02:26)
[GCC 4.3.2 20081105 (Red Hat 4.3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import OpenGL
>>> dir(OpenGL)
...and PyOpenGL_accelerate:
(my_new_env)[free-tutorials@free-tutorials ~]$ python
Python 2.7.3 (default, Dec 6 2012, 03:02:26)
[GCC 4.3.2 20081105 (Red Hat 4.3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import OpenGL_accelerate
>>> dir(OpenGL_accelerate)
This is working only on my_new_env.
That is all for now. I will try to do some examples with this packages.
The new Python 3.3.0 was released on September 29th, 2012.
It's been two weeks since it was launched last version of python and I don't have found complaints about this release.
We can read more about updates and changes made by developers here.
What I think it's more significantly to this version:
Let's see how to working the new Python 3.3.0
First download it , unzip and install it. See next:
/Python-3.3.0 $ ./configure
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking for --enable-universalsdk... no
checking for --with-universal-archs... 32-bit
checking MACHDEP... linux
checking for --without-gcc...
The next step is ...
$ make all
And finally ...
# su
# ./python setup.py build
running build
running build_ext
INFO: Can't locate Tcl/Tk libs and/or headers
Python build finished, but the necessary bits to build these modules were not found:
_bz2 _dbm _gdbm
_lzma _sqlite3 _ssl
_tkinter readline
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
I will use it without this modules, until I find a way to fix it.
$ ./python
Python 3.3.0 (default, Oct 14 2012, 21:42:00)
[GCC 4.4.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import ipaddress
They are two examples of sequence data types in python.
Today I will tell about tuples.
A tuple consists of a number of values separated by commas.
The biggest problem is when we do not know the number of these values or type.
Let me illustrate with a function that returns a tuple.
This is a known tuple but we will use like an unknown tuple.
Let's see the python code and some errors:
>>> import sys
>>> if hasattr(sys, 'version_info'):
... print sys.version_info()
...
Traceback (most recent call last):
File "", line 2, in
TypeError: 'tuple' object is not callable
Is very correct to got this error.
And the result using the print function:
>>> if hasattr(sys, 'version_info'):
... print "%s" % str(sys.version_info)
...
(2, 6, 4, 'final', 0)
>>> if hasattr(sys, 'version_info'):
... sys.stderr.write("%s" % str(sys.version_info))
...
(2, 6, 4, 'final', 0)>>>
or if you can use the repr function.
>>> if hasattr(sys, 'version_info'):
... sys.stderr.write("%s" % repr(sys.version_info))
...
(2, 6, 4, 'final', 0)>>>
The official Python documentation says __repr__ is used to compute the “official” string representation of an object and __str__ is used to compute the “informal” string representation of an object.
In my opinion , the correct way it's to use repr. For example:
>>> import datetime
>>> today = datetime.datetime.now()
>>> str(today)
'2012-10-02 22:45:57.634977'
>>> repr(today)
'datetime.datetime(2012, 10, 2, 22, 45, 57, 634977)'
As we see all values from tuples it's show.
You can customize Vim for editing a specific file.
In this case is our python script files.
To do that you can use modelines, or auto commands, or filetype plugins.
For example , if you want to see the number on rows then just set the boolean number option to true.
set number
The keys PageUp and PageDown are mapped to Ctr+U and Ctr+D.
The next changes of the vimrc file will move cursor only by half of screen.
map
map
imap
imap
To move the cursor to the end of the screen they have to be triggered twice:
map
map
imap
imap
Then you can add next line to prevent the cursor from changing the current column when jumping to other lines within the window.
set nostartofline
To add some lines on your python file.
For example many python files start with this:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
Just add the next line in your vimrc file.
:iabbrev newpy #!/usr/bin/env python #-*- coding: utf-8 -*-
On your first line from your python file in the insert mode , write newpy and press Enter key.
Also will be a good idea to take a look at the official .vimrc for following PEP 7 & 8 conventions.
...and the result of this changes.
You need to use mercurial.
Mercurial is a free, distributed source control management tool.
It is mainly implemented using the Python programming language and C.
To use it , just see the next command.
# hg clone https://vim.googlecode.com/hg/ vim
requesting all changes
adding changesets
adding manifests
adding file changes
added 3831 changesets with 24526 changes to 2566 files (+2 heads)
updating working directory
2385 files updated, 0 files merged, 0 files removed, 0 files unresolved
Go to vim folder.
# cd vim/src/
Use configure to builds a Makefile file.
src # ./configure --enable-pythoninterp --with-features=huge --prefix=$HOME/opt/vim
configure: creating cache auto/config.cache
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking whether the C compiler works... yes
...
The next commands make builds the program and make install to install the program.
src # make && make install
mkdir objects
...
Now let's try to make working well.
src # mkdir -p $HOME/bin
src # cd $HOME/bin
bin # ls
vim
bin # ln -s $HOME/opt/vim/bin/vim
bin # ls
vim
bin # which vim
/usr/bin/vim
bin # vim --version
VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Sep 21 2009 11:22:49)
Included patches: 1-245
If you read all the output , will see something like this:
+profile +python +quickfix
The main goal is to use vimrc.
For example you cand do this.
" Wrapping and tabs spaces.
set tw=78 ts=4 sw=4 sta et sts=4 ai
" Update syntax highlighting.
let python_highlight_all = 1
" Create smart indenting
set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class
" Auto completion using ctrl-space
set omnifunc=pythoncomplete#Complete
inoremap
With just few lines can change and improve your work.
There're situations when we want to compile the python script.
In this case , we have a python script named your_script.py.
The next script code will make one new your_script.pyc file.
import py_compile
py_compile.compile('your_script.py')
Note: this will hide the python code, but some strings can be view.
For example , if you use this in your_script.py.
print 'This is a string'
The string This is a string can be show in pyc file.
Today I show you how to make Voronoi diagram using python.
I use this to make textures for underwater.
This is just one example. But you can improve to control all cells of voronoi diagram.
The theory say:
In mathematics, a Voronoi diagram is a special kind of decomposition of a metric space, determined by distances to a specified family of objects (subsets) in the space. These objects are usually called the sites or the generators...Source : wikipedia.
I used the euclidean distance to make the Voronoi diagram because it's the most familiar case.
About wikipedia - Euclidean_distance: In mathematics, the Euclidean distance or Euclidean metric is the "ordinary" distance between two points that one would measure with a ruler, and is given by the Pythagorean formula...
My python script use the next python modules:
PIL - this allow me to use image functions.
random - this module give me... random numbers.
math - some math functions.
Let's see the source code :
from PIL import Image
import random
import math
Now I make the function named gen_voronoi.
This take the height and width of the output image and the number of cells.
The function has some random variables for red , green , blue - nr,ng,nb.
The function hypot is not accessible directly so we need to import math module and using math static object.
The return value is the Euclidean norm : sqrt(x*x + y*y).
def gen_voronoi(w, h, cells):
image = Image.new("RGB", (w, h))
putpixel = image.putpixel
img_x, img_y = image.size
nx = []
ny = []
nr = []
ng = []
nb = []
for i in range(cells):
nx.append(random.randrange(img_x))
ny.append(random.randrange(img_y))
nr.append(random.randrange(256))
ng.append(random.randrange(256))
nb.append(random.randrange(256))
for y in range(img_y):
for x in range(img_x):
dmin = math.hypot(img_x-1, img_y-1)
j = -1
for i in range(cells):
d = math.hypot(nx[i]-x, ny[i]-y)
if d < dmin:
dmin = d
j = i
putpixel((x, y), (nr[j], ng[j], nb[j]))
image.save("output.png", "PNG")
image.show()
Use the function to make the output.png image.
gen_voronoi(200, 200, 55)
The result is :