From b0f701701c0d675e153f2f603d73a93197983dce Mon Sep 17 00:00:00 2001 From: Kushal Das Date: Tue, 7 May 2013 19:31:13 +0530 Subject: [PATCH] a lot changes now I can not track :( --- .tx/config | 3 +- en-US/classes.xml | 3 +- en-US/functions.xml | 15 +++++ en-US/looping.xml | 1 + en-US/modules.xml | 67 ++++++++++++++--------- en-US/strings.xml | 127 +++++++++++++++++++------------------------ en-US/virtualenv.xml | 4 +- 7 files changed, 118 insertions(+), 102 deletions(-) diff --git a/.tx/config b/.tx/config index 97e239f..f3c144c 100644 --- a/.tx/config +++ b/.tx/config @@ -1,6 +1,7 @@ [main] -host = https://www.transifex.net +host = https://www.transifex.com lang_map = aln:aln-AL, ar:ar-SA, as:as-IN, bal:bal-PK, bg:bg-BG, bn:bn-BD, bn_IN:bn-IN, bs:bs-BA, ca:ca-ES, cs:cs-CZ, da:da-DK, de_CH:de-CH, de:de-DE, el:el-GR, en_GB:en-GB, es:es-ES, et:et-EE, fa:fa-IR, fi:fi-FI, fr:fr-FR, gu:gu-IN, he:he-IL, hi:hi-IN, hr:hr-HR, hu:hu-HU, id:id-ID, is:is-IS, it:it-IT, ja:ja-JP, kn:kn-IN, ko:ko-KR, lt:lt-LT, lv:lv-LV, mai:mai-IN, ml:ml-IN, mr:mr-IN, ms:ms-MY, nb:nb-NO, nds:nds-DE, nl:nl-NL, nn:nn-NO, or:or-IN, pa:pa-IN, pl:pl-PL, pt_BR:pt-BR, pt:pt-PT, ro:ro-RO, ru:ru-RU, si:si-LK, sk:sk-SK, sl:sl-SI, sq:sq-AL, sr:sr-RS, sr@latin:sr-Latn-RS, sv:sv-SE, ta:ta-IN, te:te-IN, tr:tr-TR, uk:uk-UA, ur:ur-PK, vi:vi-VN, zh_CN:zh-CN, zh_HK:zh-HK, zh_TW:zh-TW +type = PO [pym.acknowledgment] file_filter = /acknowledgment.po diff --git a/en-US/classes.xml b/en-US/classes.xml index 2acd2a2..008f8e5 100644 --- a/en-US/classes.xml +++ b/en-US/classes.xml @@ -5,10 +5,9 @@ Class - What is a class? In our daily life, we come across many objects which are similar kind or in words which are basically same. For example we can think about a car. There are different brands , color, look, style, parts, but basically they all are cars. All them of are made by different companies but with similar kind of components. - We can say all of them are different instances of the class car. They all belongs to the car class. Every instance is known as an object in computer world. A class can contain variables or methods to access those variables.In Python everything is an object. Even if you create an integer, that is an object of the Integer class. In C++ this is different. +
Your first class diff --git a/en-US/functions.xml b/en-US/functions.xml index b30a2f7..cfa786c 100644 --- a/en-US/functions.xml +++ b/en-US/functions.xml @@ -133,6 +133,20 @@ True]]> >>> print f(3) [1, 2, 3]]]> + + To avoid this you can write more idiomatic Python, like the following + + >> def f(a, data=None): + if data is None: + data = [] + data.append(a) + return data + +>>> print f(1) +[1] +>>> print f(2) +[2]]]> +
@@ -159,6 +173,7 @@ SyntaxError: non-default argument follows default argument]]>
Docstrings + DocstringsDocstrings in Python In Python we use docstrings to explain how to use the code, it will be useful in interactive mode and to create auto-documentation. Below we see an example of the docstring for a function called longest_side. diff --git a/en-US/looping.xml b/en-US/looping.xml index eba722d..1e3e326 100644 --- a/en-US/looping.xml +++ b/en-US/looping.xml @@ -243,6 +243,7 @@ Enter the number of rows: 5
Lists + ListList datastructure We are going to learn a data structure called list before we go ahead to learn more on looping. Lists an be written as a list of comma-separated values (items) between square brackets. diff --git a/en-US/modules.xml b/en-US/modules.xml index e754860..fb9745a 100644 --- a/en-US/modules.xml +++ b/en-US/modules.xml @@ -9,7 +9,8 @@
Introduction - + ModulePython modules + Still now when ever we wrote code in the python interpreter, after we came out of it, the code was lost. But in when one writes a larger program, people breaks their codes into different files and reuse then as required. In python we do this by modules. Modules are nothing files with Python definitions and statements. The module name is same as the file name without the .py extension. @@ -120,6 +121,11 @@ help> modules The above example shows how to get the list of all installed modules in your system. I am not pasting them here as it is a big list in my system :) + + You can also use help() function in the interpeter to find documentation about any module/classes. Say you want to know all available methods in strings, you can use the following method + + >> help(str)]]> +
Submodules @@ -135,8 +141,10 @@ mymodule ]]> - In this example mymodule is the module name and bars and utils are two submodules in it. + In this example mymodule is the module name and bars and utils are two submodules in it. You can create an empty __init__.py using touch command. + +
Module os @@ -144,42 +152,30 @@ mymodule os module provides operating system dependent functionality. You can import it using the following import statement. ->> import os - -]]> +>> import os]]> getuid() function returns the current process's effective user's id. ->> os.getuid() -500 - -]]> +>> os.getuid() +500]]> getpid() returns the current process's id. getppid() returns the parent process's id. ->> os.getpid() +>> os.getpid() 16150 >>> os.getppid() -14847 - - -]]> +14847]]> - uname() returns different information identifying the operating system, in Linux it returns details you can get from the uname command. The returned object is a tuple, (sysname, nodename, release, version, machine) + uname() returns different information identifying the operating system, in Linux it returns details you can get from the uname command. The returned object is a tuple, (sysname, nodename, release, version, machine) ->> os.uname() -('Linux', 'd80', '2.6.34.7-56.fc13.i686.PAE', '#1 SMP Wed Sep 15 03:27:15 UTC 2010', 'i686') - -]]> +>> os.uname() +('Linux', 'd80', '2.6.34.7-56.fc13.i686.PAE', '#1 SMP Wed Sep 15 03:27:15 UTC 2010', 'i686')]]> getcwd()returns the current working directory. chdir(path) changes the current working directory to path. In the example we first see the current directory which is my home directory and change the current directory to /tmp and then again checking the current directory. @@ -189,10 +185,31 @@ mymodule '/home/kushal' >>> os.chdir('/tmp') >>> os.getcwd() -'/tmp' -]]> +'/tmp']]> + + + So let us use another function provided by the os module and create our own function to list all files and directories in any given directory. + + + + + +>> view_dir('/') +.readahead bin boot dev etc home junk lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var]]> + + +
- + diff --git a/en-US/strings.xml b/en-US/strings.xml index 2761a95..58cc00d 100644 --- a/en-US/strings.xml +++ b/en-US/strings.xml @@ -7,8 +7,9 @@ Strings are nothing but simple text. In python we declare strings in between "" or '' or ''' ''' or """ """. The examples below will help you to understand sting in a better way. - ->> s = "I am Indian" + + + >> s = "I am Indian" >>> s 'I am Indian' >>> s = 'I am Indian' @@ -21,81 +22,74 @@ 'Here is a line \n splitted in two lines' >>> print s Here is a line - splitted in two lines -]]> - + splitted in two lines]]> + Now if you want to multiline strings you have to use triple single/double quotes. - + >> s = """ This is a ... multiline string, so you can ... write many lines""" >>> print s This is a multiline string, so you can -write many lines -]]> - +write many lines]]> +
Different methods available for Strings Every string object is having couple of buildin methods available, we already saw some of them like s.split(" "). - + >> s = "kushal das" >>> s.title() -'Kushal Das' -]]> - +'Kushal Das']]> + title() method returns a titlecased version of the string, words start with uppercase characters, all remaining cased characters are lowercase. - + >> z = s.upper() >>> z 'KUSHAL DAS' >>> z.lower() -'kushal das' -]]> - +'kushal das']]> + upper() returns a total uppercase version whereas lower() returns a lower case version of the string. - + >> s = "I am A pRoGraMMer" >> s.swapcase() -'i AM a PrOgRAmmER' -]]> - +'i AM a PrOgRAmmER']]> + swapcase() returns the string with case swapped :) - + >> s = "jdwb 2323bjb" >>> s.isalnum() False >>> s = "jdwb2323bjb" >>> s.isalnum() -True -]]> - +True]]> + Because of the space in the first line isalnum() returned False , it checks for all charecters are alpha numeric or not. - + >> s = "SankarshanSir" >>> s.isalpha() True >>> s = "Sankarshan Sir" >>> s.isalpha() -False -]]> - +False]]> + isalpha() checkes for only alphabets. - + >> s = "1234" >>> s.isdigit() #To check if all the characters are digits or not True @@ -107,29 +101,26 @@ False True >>> s = "INDIA" >>> s.isupper() # To check if characters are in upper case or not -True -]]> - +True]]> + To split any string we have split(). It takes a string as an argument , depending on that it will split the main string and returns a list containing splitted strings. - + >> s = "We all love Python" >>> s.split(" ") ['We', 'all', 'love', 'Python'] >>> x = "Nishant:is:waiting" >>> x.split(':') -['Nishant', 'is', 'waiting'] -]]> - +['Nishant', 'is', 'waiting']]]> + The opposite method for split() is join(). It takes a list contains strings as input and join them. - + >> "-".join("GNU/Linux is great".split(" ")) -'GNU/Linux-is-great' -]]> - +'GNU/Linux-is-great']]> + In the above example first we are splitting the string "GNU/Linux is great" based on the white space, then joining them with "-". @@ -140,23 +131,21 @@ True Strings do have few methods to do striping. The simplest one is strip(chars). If you provide the chars argument then it will strip any combination of them. By default it strips only whitespace or newline characters. - + >> s = " abc\n " >>> s.strip() -'abc' -]]> - +'abc']]> + You can particularly strip from the left hand or right hand side also using lstrip(chars) or rstrip(chars). - + >> s = "www.foss.in" >>> s.lstrip("cwsd.") 'foss.in' >>> s.rstrip("cnwdi.") -'www.foss' -]]> - +'www.foss']]> +
@@ -164,7 +153,7 @@ True Stings have some methods which will help you in finding text/substring in a string. Examples are given below: - + >> s.find("for") 7 >>> s.find("fora") @@ -172,9 +161,8 @@ True >>> s.startswith("fa") #To check if the string startswith fa or not True >>> s.endswith("reason") # -True -]]> - +True]]> + find() helps to find the first occurrence of the substring given, if not found it returns -1. @@ -186,48 +174,43 @@ True Palindrome are the kind of strings which are same from left or right whichever way you read them. Example "madam". In this example we will take the word as input from the user and say if it is palindrome or not. - + - + print "The string is not a palindrome"]]> + The output - + - +The string is a palindrome]]> +
Number of words In this example we will count the number of words in a given line - + - +print "The number of words in the line are %d" % (len(s.split(" ")))]]> + The output - + - +The number of words in the line are 5 ]]> +
diff --git a/en-US/virtualenv.xml b/en-US/virtualenv.xml index ac24b5c..8e13dfc 100644 --- a/en-US/virtualenv.xml +++ b/en-US/virtualenv.xml @@ -36,7 +36,7 @@ Installing setuptools............done. Installing pip...............done. - Now we can go inside virt1 directory and then enable virt1 environment. + Now we can virt1 environment. [user@host]$ source virt1/bin/activate @@ -81,7 +81,7 @@ wsgiref - 0.1.2 - active development (/usr/lib64/python2.7) yolk - 0.4.3 - active - Now we will create another virtual environment virt2 where we will install same redis module but an old version 2.4 + Now we will create another virtual environment virt2 where we will install same redis module but an old 2.4 version. [user@host]$ virtualenv virt2