function

Regex match / search list of word by separate spaces, number, words with spaces using Python

9 Feb , 2012  

This is example of using re module for building regex. Searching match words that contains spaces or separate with spaces or more than one spaces and number. This is most basic things that may faced in daily development. First, let’s create sample text, eg :
More…

,

function

How to called Class instance from given string variable in Python

4 Feb , 2012  

This is pretty interesting. You have give string list like “InsuranceUser”, “InsuranceUserProfile”, etc. That are string. Now, you should called class that have name same with the given string. I will show in Django models as more real explanation.

To fetch InsuranceUser models, usually we do :

1
2
3
from insurance.models import InsuranceUser

user_list = InsuranceUser.objects.all()

More…

,

function

How to make pep8 for long attribute / method chain in Python

2 Feb , 2012  

When we use 80 lines rules, sometimes we facing some difficult cases like long attributes. Usually this also happen in Django which need nested method chain for calling models. For instance :

1
user_list = User.objects.filter(created__year=2012).order_by('user').values('user').distinct()

We should shorten this long attributes. We should know that blackslash is “evil” in python. But, at this cases we can solve this problem using 3 way :
More…

,

function

Deep Dive into Python Path Construction

15 Dec , 2011  

Today I found great tutorial that explain how sys.path is working. Follow this link to see through :

If a script is executed, the interpreter sets the first entry of sys.path to that script’s directory. If Python is launched interactively, the first entry is the empty string (“”), meaning Python will scan the present working directory first. The next entries of sys.path are the contents of the PYTHONPATH environment variable, if it exists. Then, installation-dependent entries are appended (example below).

When initializing, the interpreter normally imports the site module automatically. The module, on import, executes code to find .pth files in known site-packages directory locations, which themselves contain entries which are either paths to add to sys.path, or import calls. If we really want to trace what’s going on, we can launch a Python interpreter with -S to prevent loading the site module automatically, and instead trace the import.

http://blog.adamdklein.com/?p=492 More…

,

function

Understanding about namespaces, attributes and scopes in Python

8 Dec , 2011  

I assume you don’t understand about namespaces, attributes and scopes in Python. Because I will explain about this three things that mostly missed by people who start learning with Python. Actually, this things will help us in the future about understanding Python even for advanced programmer. So, let begin with namespaces.

Namespaces according to Wikipedia it’s meaning for :

A namespace (sometimes also called a name scope) is an abstract container or environment created to hold a logical grouping of unique identifiers or symbols (i.e., names). An identifier defined in a namespace is associated only with that namespace. The same identifier can be independently defined in multiple namespaces. That is, the meaning associated with an identifier defined in one namespace may or may not have the same meaning as the same identifier defined in another namespace. Languages that support namespaces specify the rules that determine to which namespace an identifier (not its definition) belongs.[1]

More…

,

function

Understanding arguments *args and **kwargs in Python

2 Dec , 2011  

In Python, we usually need passing variable through arguments to functions. There are 2 things that commonly need to know about passing arguments into function which called *args and **kwargs. What this is ?

*args
This single asterisk form is used to pass non-keyworded, variable-length argument list. For instance :

1
2
3
4
5
6
7
# Single asterisk form
def someFunction(farg, *args):
    print "Basic arg : ", farg
    for arg in args:
        print "single asterisk form arg : ", arg

someFunction(1, "testing", 30)

Will give result :

1
2
3
Basic arg :  1
single asterisk form arg :  testing
single asterisk form arg :  30

More…

,