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 :
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 :
from insurance.models import InsuranceUser
user_list = InsuranceUser.objects.all()
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 :
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 :
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.
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.
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 ?
This single asterisk form is used to pass non-keyworded, variable-length argument list. For instance :
# 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 :
Basic arg : 1
single asterisk form arg : testing
single asterisk form arg : 30