analitics

Pages

Tuesday, August 3, 2010

Random module - another example

Random module is a module used in everyday programming.
Even if it is not used in the final software, he helps us to test various types of random data.
We present you a simple example - play with cards.
We chose a list of cards of one color for not having a large number of elements.
We exemplify the use of random module by analogy with a game of cards.
That it means: shuffle cards , select just one from cards and a choice selection of a defined number of cards.
Below you see the code used as an example.

>>> import random
>>> choice=random.choice
>>> shuffle=random.shuffle
>>> for i in range (9):
...     print choice(cards)
... 
Q
K
10
Q
2
2
10
K
6
>>> shuffle(cards)
>>> cards
[4, 7, 10, 'J', 3, 'Q', 6, 2, 'K', 9, 'A', 8, 5]
>>> sample=random.sample
>>> sample(cards,5)
[8, 'J', 2, 'Q', 10]
>>> sample(cards,5)
['A', 9, 4, 'K', 'Q']
>>> 
The random module docs

Saturday, July 3, 2010

The module matplotlib-0.99.3

The module matplotlib is a python 2D plotting library with a variety of hardcopy formats and interactive environments across platforms.
With just a few lines of code we can generate plots, histograms, power spectra, bar charts, errorcharts, scatterplots, etc...
See images bellow or visit the gallery.

This module is version 0.99.3 for Python 2.5 and 2.6. We have modules for installation on operating systems like MacOS, Windows and Linux.
To use this module you must have install numpy module.
Now download module from here.
$python setup.py build
Use super user:
#python setup.py install
Try to load module:
$python
Python 2.5.1 (r251:54863, Jun 15 2008, 18:24:51) 
[GCC 4.3.0 20080428 (Red Hat 4.3.0-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> 
It's working fine.