analitics

Pages

Saturday, August 17, 2013

Working with PEP 8 conventions ...

I am wrote my own python scripts to long time ago and now I saw many of this scripts are a mess  in terms of PEP criteria.
What is the pep criteria? The PEP describing style guidelines for the C code in the C implementation of Python.
If you using Blender 3D/Python development  then you need to follow the PEP 8.
The PEP 8 and can be found here and the authors are: Guido van Rossum , Barry Warsaw and Nick Coghlan.
Today I will show you a brief listing of pep8 criteria:
-indentation of 4 spaces not use tabs
-camel caps for class names: MyClass , AnotherClass
-all lower case underscore separated module names: my_module, another_module
-use explicit imports, not importing '*'
-imports should usually be on separate lines, not import os,sys
-adding whitespace around the operators , not 1+2 or 7+ 3
-don't use spaces around the = sign , not a = b +1
-use single quotes for enums, and double quotes for strings
-each line of a block comment starts with a # and a single space
-don't make comments that contradict the code
-if you want to writing good documentation strings then follow the PEP 257 conventions.
-run checks for pep8 compliance
# <pep8 compliant=""></pep8>
-and enable line length checks use this instead
# <pep8-80 compliant=""></pep8-80>
-don't use multiple statements on the same line, like:
if foo == 'test': do_test_thing()
one_test(); two_test()
... this is the correct way:
if foo == 'test':
    do_test_thing()
one_test()
two_test()
Also you can read about Blender 3D/Python and PEP 8 best practice here.

Saturday, August 10, 2013

Fix python error: SyntaxError: Non-ASCII character.

This can be fix easy ... just add the below line at top of the python file:
# coding: utf-8
You will see now something like this error:
SyntaxError: invalid syntax
... but also will be point to the bad encoding character with : ^
For example I had this:
usertest@home:~$ python sunet.py 
  File "sunet.py", line 22
    def  __init__ ( self , a , b , c ) :
                                 ^
SyntaxError: invalid syntax

How to deal with environment variables using python script.

Any OS like Linux, Unix, Windows has environment variables.

Also any variables which must not be committed on a public code can be used in this way.

You can give this variables to your application.

Let's see one simple example about how to do that:

import os
ENV_VAR = os.environ.get("ENV_VAR", None)
if not ENV_VAR:
        print "not ENV_VAR"
if ENV_VAR:
        print "yes ! is ENV_VAR"

Now you can give this ENV_VAR to the script or not. See next...

usertest@home:~$ ENV_VAR=true python demo-env.py 
yes ! is ENV_VAR
usertest@home:~$ python demo-env.py 
not ENV_VAR

With Python 3 on Unix, environment variables are decoded using the file system encoding.

Tuesday, July 30, 2013

The new pygobject come with version 3.9.5 .

GObject is a object system used by GTK+, GStreamer and other libraries.

The new PyGObject 3.9.5 provides a convenient wrapper for use in Python programs when
accessing GObject libraries.

The news come from Simon Feltman - pygtk digest: I am pleased to announce version 3.9.5 of the Python bindings for
GObject. This is the third release of the 3.9.x series which will
eventually result in the stable 3.10 release for GNOME 3.10.

The new release is available from ftp.gnome.org.

Wednesday, July 24, 2013

Using python module webkit and gtk to make one simple webbrowser.

Today I will show you a simple example with webkit python module.

I will make one simple browser using this two python modules.

Read the full tutorial here.

The result can be see in the next image:

Sunday, July 14, 2013

Selenium python module and links.

Just see my old tutorial to run the selenium module.
NOTE: If your script not run when you try to login then use python shell :P
Now add the next source code to see the links and some infos.
links = browser.find_elements_by_partial_link_text('')
for link in links:
    print link.get_attribute("href")