analitics

Pages

Saturday, July 13, 2013

Selenium python module and cookies.

Today I will show how to deal with cookies and Firefox.

Selenium Python Client Driver is a Python language binding for Selenium Remote Control.

You can read more about this module here.

You can find some examples , but most of webpages working with cookies.So let's make one simple tutorial.

The next source code is very simple and most of the python users knows what means.


$ python 
Python 2.7.3 (default, Jan  2 2013, 16:53:07) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> from selenium.common.exceptions import NoSuchElementException
>>> from selenium.webdriver.common.keys import Keys
>>> browser = webdriver.Firefox()
>>> browser.get("http://facebook.com/")
>>> browser.get("http://facebook.com/home.php")

The next source codes will get your cookies from the webpage.

>>> for cookie in browser.get_cookies():
...     print(cookie['name'] + ' --> your cookie data ' + cookie['value'])
... 
datr --> your cookie data 
locale --> your cookie data 
xs --> your cookie data 
s --> your cookie data 
lu --> your cookie data 
fr --> your cookie data 
csm --> your cookie data 
c_user --> your cookie data 
act --> your cookie data 
x-src --> your cookie data 
sub --> your cookie data 
p --> your cookie data 
presence --> your cookie data 
>>> 

You can deal with all functions of selenium python module , see :

>>> dir(browser)
['NATIVE_EVENTS_ALLOWED', '__class__', '__delattr__', '__dict__', '__doc__', 
'__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__'
, '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__'
, '__subclasshook__', '__weakref__', '_is_remote', '_unwrap_value', '_wrap_value'
, 'add_cookie', 'application_cache', 'back', 'binary', 'capabilities', 'close', 
'command_executor', 'create_web_element', 'current_url', 'current_window_handle'
, 'delete_all_cookies', 'delete_cookie', 'desired_capabilities', 'error_handler'
, 'execute', 'execute_async_script', 'execute_script', 'find_element', 
'find_element_by_class_name', 'find_element_by_css_selector', 'find_element_by_id'
, 'find_element_by_link_text', 'find_element_by_name', 'find_element_by_partial_link_text'
, 'find_element_by_tag_name', 'find_element_by_xpath', 'find_elements', 
'find_elements_by_class_name', 'find_elements_by_css_selector', 'find_elements_by_id',
 'find_elements_by_link_text', 'find_elements_by_name', 'find_elements_by_partial_link_text'
 , 'find_elements_by_tag_name', 'find_elements_by_xpath', 'firefox_profile', 'forward',
  'get', 'get_cookie', 'get_cookies', 'get_screenshot_as_base64', 'get_screenshot_as_file'
  , 'get_window_position', 'get_window_size', 'implicitly_wait', 'is_online', 
  'maximize_window', 'name', 'orientation', 'page_source', 'profile', 'quit', 'refresh'
  , 'save_screenshot', 'session_id', 'set_page_load_timeout', 'set_script_timeout', 
  'set_window_position', 'set_window_size', 'start_client', 'start_session', 'stop_client'
  , 'switch_to_active_element', 'switch_to_alert', 'switch_to_default_content', 
  'switch_to_frame', 'switch_to_window', 'title', 'window_handles']

This module can be a way for you to testing your webpages , save cookie , restore and more...

Also , if you know networking development also you can use scapy module to test more.

I hope will like this simple tutorial about python.

Monday, July 8, 2013

Using findContours from OpenCV python module.

Today I will show something nice about OpenCV Analysis and Shape Descriptors.

This function finds contours in a binary image.

All detected contours is stored as a vector of points for each contour.


#!/usr/bin/python2.7
import cv2
im = cv2.imread('your_image.jpg')
img_gray = cv2.cvtColor(im,cv2.COLOR_RGB2GRAY)
ret,thresh = cv2.threshold(img_gray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im,contours,-1,(250,250,250),2)
cv2.imshow('your_image.jpg',im)
cv2.waitKey()
cv2.destroyAllWindows()

If you got this error:

findContours error 'support only 8uC1 images'

then the main reason it's findContours requires a monochrome image.

Let's see the result of the python script.


The contour it's draw with 250,250,250 color.

Friday, June 21, 2013

Simple way to find your script under linux.

Many python users have a lot of scripts.

They use some words for classes or some functions.

Sometime is hard to remember where it's this scripts.

So the easy way to do that is to find the script where is some words.

For example you need to find this : word_in_your_script

To do that just see next linux command:

$ find ~/ -type f -iname "*.py" -exec grep -l 'word_in_your_script' {} \;

Thursday, June 13, 2013

What you need : stdout or print ?

My question is much more complicated than intended and I will show you in this tutorial.

Most users use print or print() - if it used in python 3.

For example you can use this without import any python module.

$ python 
Python 2.6.8 (unknown, Apr 14 2013, 18:10:41) 
[GCC 4.3.2 20081105 (Red Hat 4.3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "my text"
my text
>>> 

This is simple to show some strings.

What the most people don't know about print function it's the interface of stdout.write.

Let's see another example with stdout.write .

>>> sys.stdout.write(str(my text) + '\n')   
  File "<stdin>", line 1
    sys.stdout.write(str(my text) + '\n')
                               ^
SyntaxError: invalid syntax
>>> sys.stdout.write(str("my text") + '\n')                  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined

First error tells us about function str.It's need just one arg not this my text.

The next error tells us about sys module is not import by python.

Note: This restriction can help us sometime.

Some example using stdout.write :

First is : my text example , see I add '\n' to go to the next row.

>>> import sys 
>>> sys.stdout.write("my text"+'\n')
my text

Let's try to change the output.

See the next image :

You can say : I can do this with print function.

>>> print "\033[0;32m"+"my text"+"\033[0m"
my text

Yes! Right. Can you do this ? (see the next video)


Wednesday, May 15, 2013

Use python to render scene in Blender 3D.

The python script is very simple.

I used my scene with Mickey Mouse (my boy like this funny cartoon).

This is the python script.

import bpy
bpy.context.scene.render.use_border = False
bpy.context.scene.render.use_crop_to_border = False
bpy.ops.render.render()
R="Render Result"
bpy.data.images[R].save_render("/home/your_username/test_render.png")

See the output image:


The next source code it's used for border and crop.

Only if you want to use it.

bpy.context.scene.render.use_border = False
bpy.context.scene.render.use_crop_to_border = False

Also you can use this to set resolution percentage.

For example if you want to render just 10% of resolution use this:

bpy.context.scene.render.resolution_percentage =10

Saturday, March 23, 2013

Using fnmatch python module ...

The module fnmatch provides support for Unix shell-style wildcards, which are not the same as regular expressions.

Why , because the special characters used are : * , ? , [seq] , [!seq] .

This is default example from fnmach website.


>>> for file in os.listdir('.'):
...  if fnmatch.fnmatch(file, '*.txt'):
...   print file
... 
tableta.txt
verf.txt
a.txt
python-modules.txt
untitled.txt
resetbios.txt
>>> 

Now I want to show you another way to use this module.

Using Blender with multiple objects and python script can be very hard way.

For example if you have many objects or one matrix of objects create manually or with some scripts the named object is like in the next image:


Just use the fnmatch to sort this objects.

>>> import bpy 
>>> import fnmatch

Now get all meshes objects.

>>> all_objects = bpy.data.objects

Put names of all meshes as a list of strings.

>>> list_all_objects= [all_objects[i].name for i in range(len(all_objects))]

Use the python module fnmatch to filter the name of objects.

>>> new_list_objects = fnmatch.filter(list_all_objects, 'Cube.*')]

Now you can use this new list to make some change. I use print to show test the list.

>>> print(new_list_objects)
['Cube.001', 'Cube.002', 'Cube.003'

The goal of fnmatch module in Blender 3D can be use one module to make list of objects and enables searching for files given a file name pattern.

It's two features in one module.

Also this python module can be used to get some

>>> regular_expression_txt = fnmatch.translate('*.txt')
>>> regular_expression_txt
'.*\\.txt\\Z(?ms)'
>>> print(regular_expression_txt)
.*\.txt\Z(?ms)

Just remove \Z(?ms) and use it.

I try to use some regular expression and seam working well.

Saturday, March 2, 2013

Simple way to reverse strings

Just see the next source code:

>>> text='0123456789'
>>> reverse_text=text[::-1]
>>> print reverse_text
9876543210
>>> 

Monday, February 25, 2013

Make Newton fractal with python

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:

Sunday, February 24, 2013

PP 1.6.4 is released ...

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.

Saturday, January 26, 2013

Using Flask to build websites in 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.

The sandboxed Python - development environments using pip and virtualenv .

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 <filename>             Log file where a complete (maximum verbosity)
                              record will be kept
 --proxy <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 <seconds>          Set the socket timeout (default 15 seconds)
 --exists-action <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.

Sunday, December 16, 2012

Simple way to create png image with an input text.

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.

Tuesday, December 11, 2012

Using pip for installing and managing Python packages.

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.