Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Long time Pythoneer Tim Peters succinctly channels the BDFL’s guiding principles for Python’s design into 20 aphorisms, only 19 of which have been written down.
>>>importthisTheZenofPython,byTimPetersBeautifulisbetterthanugly.Explicitisbetterthanimplicit.Simpleisbetterthancomplex.Complexisbetterthancomplicated.Flatisbetterthannested.Sparseisbetterthandense.Readabilitycounts.Specialcasesaren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you'reDutch.Nowisbetterthannever.Althoughneverisoftenbetterthan*right*now.Iftheimplementationishardtoexplain,it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let'sdomoreofthose!
>>>print('What is your name?')# ask for their name
>>>myName=input()>>>print('It is good to meet you, {}'.format(myName))Whatisyourname?AlItisgoodtomeetyou,Al
When the program execution reaches a continue statement, the program execution immediately jumps back to the start of the loop.
1
2
3
4
5
6
7
8
9
10
whileTrue:print('Who are you?')name=input()ifname!='Joe':continueprint('Hello, Joe. What is the password? (It is a fish.)')password=input()ifpassword=='swordfish':breakprint('Access granted.')
>>>print('My name is')>>>foriinrange(5):>>>print('Jimmy Five Times ({})'.format(str(i)))MynameisJimmyFiveTimes(0)JimmyFiveTimes(1)JimmyFiveTimes(2)JimmyFiveTimes(3)JimmyFiveTimes(4)
The range() function can also be called with three arguments. The first two arguments will be the start and stop values, and the third will be the step argument. The step is the amount that the variable is increased by after each iteration.
1
2
3
4
5
6
7
>>>foriinrange(0,10,2):>>>print(i)02468
You can even use a negative number for the step argument to make the for loop count down instead of up.
1
2
3
4
5
6
7
8
>>>foriinrange(5,-1,-1):>>>print(i)543210
For else statement
This allows to specify a statement to execute in case of the full loop has been executed. Only
useful when a break condition can occur in the loop:
1
2
3
4
5
>>>foriin[1,2,3,4,5]:>>>ifi==3:>>>break>>>else:>>>print("only executed when no item of the list is equal to 3")
When creating a function using the def statement, you can specify what the return value should be with a return statement. A return statement consists of the following:
The return keyword.
The value or expression that the function should return.
importrandomdefgetAnswer(answerNumber):ifanswerNumber==1:return'It is certain'elifanswerNumber==2:return'It is decidedly so'elifanswerNumber==3:return'Yes'elifanswerNumber==4:return'Reply hazy try again'elifanswerNumber==5:return'Ask again later'elifanswerNumber==6:return'Concentrate and ask again'elifanswerNumber==7:return'My reply is no'elifanswerNumber==8:return'Outlook not so good'elifanswerNumber==9:return'Very doubtful'r=random.randint(1,9)fortune=getAnswer(r)print(fortune)
Code in the global scope cannot use any local variables.
However, a local scope can access global variables.
Code in a function’s local scope cannot use variables in any other local scope.
You can use the same name for different variables if they are in different scopes. That is, there can be a local variable named spam and a global variable also named spam.
>>>supplies=['pens','staplers','flame-throwers','binders']>>>fori,supplyinenumerate(supplies):>>>print('Index {} in supplies is: {}'.format(str(i),supply))Index0insuppliesis:pensIndex1insuppliesis:staplersIndex2insuppliesis:flame-throwersIndex3insuppliesis:binders
>>>name=['Pete','John','Elizabeth']>>>age=[6,23,44]>>>forn,ainzip(name,age):>>>print('{} is {} years old'.format(n,a))Peteis6yearsoldJohnis23yearsoldElizabethis44yearsold
The multiple assignment trick is a shortcut that lets you assign multiple variables with the values in a list in one line of code. So instead of doing this:
>>>importpprint>>>>>>message='It was a bright cold day in April, and the clocks were striking
>>> thirteen.'>>>count={}>>>>>>forcharacterinmessage:>>>count.setdefault(character,0)>>>count[character]=count[character]+1>>>>>>pprint.pprint(count){' ':13,',':1,'.':1,'A':1,'I':1,'a':4,'b':1,'c':3,'d':3,'e':5,'g':2,'h':3,'i':6,'k':2,'l':3,'n':4,'o':2,'p':1,'r':5,'s':3,'t':6,'w':2,'y':1}
A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
Initializing a set
There are two ways to create sets: using curly braces {} and the built-in function set()
1
2
>>>s={1,2,3}>>>s=set([1,2,3])
When creating an empty set, be sure to not use the curly braces {} or you will get an empty dictionary instead.
1
2
3
>>>s={}>>>type(s)<class'dict'>
sets: unordered collections of unique elements
A set automatically remove all the duplicate values.
1
2
3
>>>s={1,2,3,2,3,4}>>>s{1,2,3,4}
And as an unordered data type, they can’t be indexed.
The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an “iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python.
The itertools module comes in the standard library and must be imported.
The operator module will also be used. This module is not necessary when using itertools, but needed for some of the examples below.
Makes an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted.
>>>print('''Dear Alice,
>>>
>>> Eve's cat has been arrested for catnapping, cat burglary, and extortion.
>>>
>>> Sincerely,
>>> Bob''')DearAlice,Eve's cat has been arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob
To keep a nicer flow in your code, you can use the dedent function from the textwrap standard package.
1
2
3
4
5
6
7
8
9
10
11
>>>fromtextwrapimportdedent>>>>>>defmy_function():>>>print('''
>>> Dear Alice,
>>>
>>> Eve's cat has been arrested for catnapping, cat burglary, and extortion.
>>>
>>> Sincerely,
>>> Bob
>>> ''').strip()
Justifying Text with rjust(), ljust(), and center()
rjust() and ljust():
1
2
>>>'Hello'.rjust(10)' Hello'
1
2
>>>'Hello'.rjust(20)' Hello'
1
2
>>>'Hello World'.rjust(20)' Hello World'
1
2
>>>'Hello'.ljust(10)'Hello '
An optional second argument to rjust() and ljust() will specify a fill character other than a space character. Enter the following into the interactive shell:
Python 3 introduced a new way to do string formatting that was later back-ported to Python 2.7. This makes the syntax for string formatting more regular.
1
2
3
4
5
>>>name='John'>>>age=20'
>>> "Hello I'm{},myageis{}".format(name, age)
"HelloI'm John, my age is 20"
1
2
>>>"Hello I'm {0}, my age is {1}".format(name,age)"Hello I'm John, my age is 20"
The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals or the str.format() interface helps avoid these errors. These alternatives also provide more powerful, flexible and extensible approaches to formatting text.
A simpler and less powerful mechanism, but it is recommended when handling format strings generated by users. Due to their reduced complexity template strings are a safer choice.
>>>phone_num_regex=re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')>>>mo=phone_num_regex.search('My number is 415-555-4242.')>>>print('Phone number found: {}'.format(mo.group()))Phonenumberfound:415-555-4242
>>>phone_num_regex=re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')>>>mo=phone_num_regex.search('My number is 415-555-4242.')>>>mo.group(1)'415'>>>mo.group(2)'555-4242'>>>mo.group(0)'415-555-4242'>>>mo.group()'415-555-4242'
To retrieve all the groups at once: use the groups() method—note the plural form for the name.
character is called a pipe. You can use it anywhere you want to match one of many expressions. For example, the regular expression r’Batman
Tina Fey’ will match either ‘Batman’ or ‘Tina Fey’.
1
2
3
4
5
6
7
8
9
10
11
>>>hero_regex=re.compile(r'Batman|Tina Fey')>>>mo1=hero_regex.search('Batman and Tina Fey.')>>>mo1.group()'Batman'>>>mo2=hero_regex.search('Tina Fey and Batman.')>>>mo2.group()'Tina Fey'
You can also use the pipe to match one of several patterns as part of your regex:
1
2
3
4
5
6
7
8
9
>>>bat_regex=re.compile(r'Bat(man|mobile|copter|bat)')>>>mo=bat_regex.search('Batmobile lost a wheel')>>>mo.group()'Batmobile'>>>mo.group(1)'mobile'
The ? character flags the group that precedes it as an optional part of the pattern.
1
2
3
4
5
6
7
8
>>>bat_regex=re.compile(r'Bat(wo)?man')>>>mo1=bat_regex.search('The Adventures of Batman')>>>mo1.group()'Batman'>>>mo2=bat_regex.search('The Adventures of Batwoman')>>>mo2.group()'Batwoman'
The * (called the star or asterisk) means “match zero or more”—the group that precedes the star can occur any number of times in the text.
1
2
3
4
5
6
7
8
9
10
11
12
>>>bat_regex=re.compile(r'Bat(wo)*man')>>>mo1=bat_regex.search('The Adventures of Batman')>>>mo1.group()'Batman'>>>mo2=bat_regex.search('The Adventures of Batwoman')>>>mo2.group()'Batwoman'>>>mo3=bat_regex.search('The Adventures of Batwowowowoman')>>>mo3.group()'Batwowowowoman'
While * means “match zero or more,” the + (or plus) means “match one or more”. The group preceding a plus must appear at least once. It is not optional:
1
2
3
4
>>>bat_regex=re.compile(r'Bat(wo)+man')>>>mo1=bat_regex.search('The Adventures of Batwoman')>>>mo1.group()'Batwoman'
1
2
3
>>>mo2=bat_regex.search('The Adventures of Batwowowowoman')>>>mo2.group()'Batwowowowoman'
1
2
3
>>>mo3=bat_regex.search('The Adventures of Batman')>>>mo3isNoneTrue
If you have a group that you want to repeat a specific number of times, follow the group in your regex with a number in curly brackets. For example, the regex (Ha){3} will match the string ‘HaHaHa’, but it will not match ‘HaHa’, since the latter has only two repeats of the (Ha) group.
Instead of one number, you can specify a range by writing a minimum, a comma, and a maximum in between the curly brackets. For example, the regex (Ha){3,5} will match ‘HaHaHa’, ‘HaHaHaHa’, and ‘HaHaHaHaHa’.
Python’s regular expressions are greedy by default, which means that in ambiguous situations they will match the longest string possible. The non-greedy version of the curly brackets, which matches the shortest string possible, has the closing curly bracket followed by a question mark.
In addition to the search() method, Regex objects also have a findall() method. While search() will return a Match object of the first matched text in the searched string, the findall() method will return the strings of every match in the searched string.
1
2
3
4
>>>phone_num_regex=re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')# has no groups
>>>phone_num_regex.findall('Cell: 415-555-9999 Work: 212-555-0000')['415-555-9999','212-555-0000']
To summarize what the findall() method returns, remember the following:
When called on a regex with no groups, such as \d-\d\d\d-\d\d\d\d, the method findall() returns a list of ng matches, such as [‘415-555-9999’, ‘212-555-0000’].
When called on a regex that has groups, such as (\d\d\d)-(d\d)-(\d\d\d\d), the method findall() returns a list of es of strings (one string for each group), such as [(‘415’, ‘555’, ‘9999’), (‘212’, ‘555’, ‘0000’)].
There are times when you want to match a set of characters but the shorthand character classes (\d, \w, \s, and so on) are too broad. You can define your own character class using square brackets. For example, the character class [aeiouAEIOU] will match any vowel, both lowercase and uppercase.
You can also include ranges of letters or numbers by using a hyphen. For example, the character class [a-zA-Z0-9] will match all lowercase letters, uppercase letters, and numbers.
By placing a caret character (^) just after the character class’s opening bracket, you can make a negative character class. A negative character class will match all the characters that are not in the character class. For example, enter the following into the interactive shell:
You can also use the caret symbol (^) at the start of a regex to indicate that a match must occur at the beginning of the searched text.
Likewise, you can put a dollar sign ($) at the end of the regex to indicate the string must end with this regex pattern.
And you can use the ^ and $ together to indicate that the entire string must match the regex—that is, it’s not enough for a match to be made on some subset of the string.
The r’^Hello’ regular expression string matches strings that begin with ‘Hello’:
1
2
3
4
5
6
7
>>>begins_with_hello=re.compile(r'^Hello')>>>begins_with_hello.search('Hello world!')<_sre.SRE_Matchobject;span=(0,5),match='Hello'>>>>begins_with_hello.search('He said hello.')isNoneTrue
The r’\d$’ regular expression string matches strings that end with a numeric character from 0 to 9:
>>>name_regex=re.compile(r'First Name: (.*) Last Name: (.*)')>>>mo=name_regex.search('First Name: Al Last Name: Sweigart')>>>mo.group(1)'Al'
1
2
>>>mo.group(2)'Sweigart'
The dot-star uses greedy mode: It will always try to match as much text as possible. To match any and all text in a nongreedy fashion, use the dot, star, and question mark (.*?). The question mark tells Python to match in a nongreedy way:
1
2
3
4
>>>nongreedy_regex=re.compile(r'<.*?>')>>>mo=nongreedy_regex.search('<To serve man> for dinner.>')>>>mo.group()'<To serve man>'
1
2
3
4
>>>greedy_regex=re.compile(r'<.*>')>>>mo=greedy_regex.search('<To serve man> for dinner.>')>>>mo.group()'<To serve man> for dinner.>'
The dot-star will match everything except a newline. By passing re.DOTALL as the second argument to re.compile(), you can make the dot character match all characters, including the newline character:
1
2
3
>>>no_newline_regex=re.compile('.*')>>>no_newline_regex.search('Serve the public trust.\nProtect the innocent.\nUphold the law.').group()'Serve the public trust.'
1
2
3
>>>newline_regex=re.compile('.*',re.DOTALL)>>>newline_regex.search('Serve the public trust.\nProtect the innocent.\nUphold the law.').group()'Serve the public trust.\nProtect the innocent.\nUphold the law.'
The sub() method for Regex objects is passed two arguments:
The first argument is a string to replace any matches.
The second is the string for the regular expression.
The sub() method returns a string with the substitutions applied:
1
2
3
4
>>>names_regex=re.compile(r'Agent \w+')>>>names_regex.sub('CENSORED','Agent Alice gave the secret documents to Agent Bob.')'CENSORED gave the secret documents to CENSORED.'
Another example:
1
2
3
4
>>>agent_names_regex=re.compile(r'Agent (\w)\w*')>>>agent_names_regex.sub(r'\1****','Agent Alice told Agent Carol that Agent Eve knew Agent Bob was a double agent.')A****toldC****thatE****knewB****wasadoubleagent.'
To tell the re.compile() function to ignore whitespace and comments inside the regular expression string, “verbose mode” can be enabled by passing the variable re.VERBOSE as the second argument to re.compile().
Now instead of a hard-to-read regular expression like this:
There are two main modules in Python that deals with path manipulation.
One is the os.path module and the other is the pathlib module.
The pathlib module was added in Python 3.4, offering an object-oriented way
to handle file system paths.
Backslash on Windows and Forward Slash on OS X and Linux
On Windows, paths are written using backslashes (\) as the separator between
folder names. On Unix based operating system such as macOS, Linux, and BSDs,
the forward slash (/) is used as the path separator. Joining paths can be
a headache if your code needs to work on different platforms.
Fortunately, Python provides easy ways to handle this. We will showcase
how to deal with this with both os.path.join and pathlib.Path.joinpath
Notice the path separator is different between Windows and Unix based operating
system, that’s why you want to use one of the above methods instead of
adding strings together to join paths together.
Joining paths is helpful if you need to create different file paths under
the same directory.
Oh no, we got a nasty error! The reason is that the ‘delicious’ directory does
not exist, so we cannot make the ‘walnut’ and the ‘waffles’ directories under
it. To fix this, do:
An absolute path, which always begins with the root folder
A relative path, which is relative to the program’s current working directory
There are also the dot (.) and dot-dot (..) folders. These are not real folders but special names that can be used in a path. A single period (“dot”) for a folder name is shorthand for “this directory.” Two periods (“dot-dot”) means “the parent folder.”
>>>frompathlibimportPath>>>stat=Path('/bin/python3.6').stat()>>>print(stat)# stat contains some other information about the file as well
os.stat_result(st_mode=33261,st_ino=141087,st_dev=2051,st_nlink=2,st_uid=0,--snip--st_gid=0,st_size=10024,st_atime=1517725562,st_mtime=1515119809,st_ctime=1517261276)>>>print(stat.st_size)# size in bytes
10024
Listing directory contents using os.listdir on Windows:
To find the total size of all the files in this directory:
WARNING: Directories themselves also have a size! So you might want to
check for whether a path is a file or directory using the methods in the methods discussed in the above section!
Using os.path.getsize() and os.listdir() together on Windows:
You can install this module by running pip install send2trash from a Terminal window.
1
2
3
4
5
6
7
>>>importsend2trash>>>withopen('bacon.txt','a')asbacon_file:# creates the file
...bacon_file.write('Bacon is not a vegetable.')25>>>send2trash.send2trash('bacon.txt')
>>>importos>>>>>>forfolder_name,subfolders,filenamesinos.walk('C:\\delicious'):>>>print('The current folder is {}'.format(folder_name))>>>>>>forsubfolderinsubfolders:>>>print('SUBFOLDER OF {}: {}'.format(folder_name,subfolder))>>>forfilenameinfilenames:>>>print('FILE INSIDE {}: {}'.format(folder_name,filename))>>>>>>print('')ThecurrentfolderisC:\deliciousSUBFOLDEROFC:\delicious:catsSUBFOLDEROFC:\delicious:walnutFILEINSIDEC:\delicious:spam.txtThecurrentfolderisC:\delicious\catsFILEINSIDEC:\delicious\cats:catnames.txtFILEINSIDEC:\delicious\cats:zophie.jpgThecurrentfolderisC:\delicious\walnutSUBFOLDEROFC:\delicious\walnut:wafflesThecurrentfolderisC:\delicious\walnut\wafflesFILEINSIDEC:\delicious\walnut\waffles:butter.txt
pathlib provides a lot more functionality than the ones listed above,
like getting file name, getting file extension, reading/writing a file without
manually opening it, etc. Check out the
official documentation
if you want to know more!
Reading and Writing Files
The File Reading/Writing Process
To read/write to a file in Python, you will want to use the with
statement, which will close the file for you after you are done.
>>>withopen('C:\\Users\\your_home_folder\\hello.txt')ashello_file:...hello_content=hello_file.read()>>>hello_content'Hello World!'>>># Alternatively, you can use the *readlines()* method to get a list of string values from the file, one string for each line of text:
>>>withopen('sonnet29.txt')assonnet_file:...sonnet_file.readlines()[When,indisgracewithfortuneandmen's eyes,\n',' I all alone beweep my
outcast state,\n',Andtroubledeafheavenwithmybootlesscries,\n', And
look upon myself and curse my fate,']>>># You can also iterate through the file line by line:
>>>withopen('sonnet29.txt')assonnet_file:...forlineinsonnet_file:# note the new line character will be included in the line
...print(line,end='')When,indisgracewithfortuneandmen's eyes,
I all alone beweep my outcast state,
And trouble deaf heaven with my bootless cries,
And look upon myself and curse my fate,
>>>withopen('bacon.txt','w')asbacon_file:...bacon_file.write('Hello world!\n')13>>>withopen('bacon.txt','a')asbacon_file:...bacon_file.write('Bacon is not a vegetable.')25>>>withopen('bacon.txt')asbacon_file:...content=bacon_file.read()>>>print(content)Helloworld!Baconisnotavegetable.
Just like dictionaries, shelf values have keys() and values() methods that will return list-like values of the keys and values in the shelf. Since these methods return list-like values instead of true lists, you should pass them to the list() function to get them in list form.
>>>importzipfile,os>>>os.chdir('C:\\')# move to the folder with example.zip
>>>withzipfile.ZipFile('example.zip')asexample_zip:...print(example_zip.namelist())...spam_info=example_zip.getinfo('spam.txt')...print(spam_info.file_size)...print(spam_info.compress_size)...print('Compressed file is %sx smaller!'%(round(spam_info.file_size/spam_info.compress_size,2)))['spam.txt','cats/','cats/catnames.txt','cats/zophie.jpg']139083828'Compressed file is 3.63x smaller!'
The extractall() method for ZipFile objects extracts all the files and folders from a ZIP file into the current working directory.
1
2
3
4
5
6
>>>importzipfile,os>>>os.chdir('C:\\')# move to the folder with example.zip
>>>withzipfile.ZipFile('example.zip')asexample_zip:...example_zip.extractall()
The extract() method for ZipFile objects will extract a single file from the ZIP file. Continue the interactive shell example:
Compared to JSON, YAML allows for much better human maintainability and gives you the option to add comments.
It is a convenient choice for configuration files where humans will have to edit it.
There are two main libraries allowing to access to YAML files:
Install them using pipinstall in your virtual environment.
The first one it easier to use but the second one, Ruamel, implements much better the YAML
specification, and allow for example to modify a YAML content without altering comments.
Anyconfig is a very handy package allowing to abstract completely the underlying configuration file format. It allows to load a Python dictionary from JSON, YAML, TOML, and so on.
Exceptions are raised with a raise statement. In code, a raise statement consists of the following:
The raise keyword
A call to the Exception() function
A string with a helpful error message passed to the Exception() function
1
2
3
4
5
>>>raiseException('This is the error message.')Traceback(mostrecentcalllast):File"<pyshell#191>",line1,in<module>raiseException('This is the error message.')Exception:Thisistheerrormessage.
Often it’s the code that calls the function, not the function itself, that knows how to handle an exception. So you will commonly see a raise statement inside a function and the try and except statements in the code calling the function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
defbox_print(symbol,width,height):iflen(symbol)!=1:raiseException('Symbol must be a single character string.')ifwidth<=2:raiseException('Width must be greater than 2.')ifheight<=2:raiseException('Height must be greater than 2.')print(symbol*width)foriinrange(height-2):print(symbol+(' '*(width-2))+symbol)print(symbol*width)forsym,w,hin(('*',4,4),('O',20,5),('x',1,3),('ZZ',3,3)):try:box_print(sym,w,h)exceptExceptionaserr:print('An exception happened: '+str(err))
The traceback is displayed by Python whenever a raised exception goes unhandled. But can also obtain it as a string by calling traceback.format_exc(). This function is useful if you want the information from an exception’s traceback but also want an except statement to gracefully handle the exception. You will need to import Python’s traceback module before calling this function.
1
2
3
4
5
6
7
8
9
10
>>>importtraceback>>>try:>>>raiseException('This is the error message.')>>>except:>>>withopen('errorInfo.txt','w')aserror_file:>>>error_file.write(traceback.format_exc())>>>print('The traceback info was written to errorInfo.txt.')116ThetracebackinfowaswrittentoerrorInfo.txt.
The 116 is the return value from the write() method, since 116 characters were written to the file. The traceback text was written to errorInfo.txt.
An assertion is a sanity check to make sure your code isn’t doing something obviously wrong. These sanity checks are performed by assert statements. If the sanity check fails, then an AssertionError exception is raised. In code, an assert statement consists of the following:
The assert keyword
A condition (that is, an expression that evaluates to True or False)
A comma
A string to display when the condition is False
1
2
3
4
5
6
7
8
9
10
11
12
>>>pod_bay_door_status='open'>>>assertpod_bay_door_status=='open','The pod bay doors need to be "open".'>>>pod_bay_door_status='I\'m sorry, Dave. I\'m afraid I can\'t do that.'>>>assertpod_bay_door_status=='open','The pod bay doors need to be "open".'Traceback(mostrecentcalllast):File"<pyshell#10>",line1,in<module>assertpod_bay_door_status=='open','The pod bay doors need to be "open".'AssertionError:Thepodbaydoorsneedtobe"open".
In plain English, an assert statement says, “I assert that this condition holds true, and if not, there is a bug somewhere in the program.” Unlike exceptions, your code should not handle assert statements with try and except; if an assert fails, your program should crash. By failing fast like this, you shorten the time between the original cause of the bug and when you first notice the bug. This will reduce the amount of code you will have to check before finding the code that’s causing the bug.
Disabling Assertions
Assertions can be disabled by passing the -O option when running Python.
To enable the logging module to display log messages on your screen as your program runs, copy the following to the top of your program (but under the #! python shebang line):
Say you wrote a function to calculate the factorial of a number. In mathematics, factorial 4 is 1 × 2 × 3 × 4, or 24. Factorial 7 is 1 × 2 × 3 × 4 × 5 × 6 × 7, or 5,040. Open a new file editor window and enter the following code. It has a bug in it, but you will also enter several log messages to help yourself figure out what is going wrong. Save the program as factorialLog.py.
>>>importlogging>>>>>>logging.basicConfig(level=logging.DEBUG,format=' %(asctime)s - %(levelname)s- %(message)s')>>>>>>logging.debug('Start of program')>>>>>>deffactorial(n):>>>>>>logging.debug('Start of factorial(%s)'%(n))>>>total=1>>>>>>foriinrange(1,n+1):>>>total*=i>>>logging.debug('i is '+str(i)+', total is '+str(total))>>>>>>logging.debug('End of factorial(%s)'%(n))>>>>>>returntotal>>>>>>print(factorial(5))>>>logging.debug('End of program')2015-05-2316:20:12,664-DEBUG-Startofprogram2015-05-2316:20:12,664-DEBUG-Startoffactorial(5)2015-05-2316:20:12,665-DEBUG-iis0,totalis02015-05-2316:20:12,668-DEBUG-iis1,totalis02015-05-2316:20:12,670-DEBUG-iis2,totalis02015-05-2316:20:12,673-DEBUG-iis3,totalis02015-05-2316:20:12,675-DEBUG-iis4,totalis02015-05-2316:20:12,678-DEBUG-iis5,totalis02015-05-2316:20:12,680-DEBUG-Endoffactorial(5)02015-05-2316:20:12,684-DEBUG-Endofprogram
Logging levels provide a way to categorize your log messages by importance. There are five logging levels, described in Table 10-1 from least to most important. Messages can be logged at each level using a different logging function.
Level
Logging Function
Description
DEBUG
logging.debug()
The lowest level. Used for small details. Usually you care about these messages only when diagnosing problems.
INFO
logging.info()
Used to record information on general events in your program or confirm that things are working at their point in the program.
WARNING
logging.warning()
Used to indicate a potential problem that doesn’t prevent the program from working but might do so in the future.
ERROR
logging.error()
Used to record an error that caused the program to fail to do something.
CRITICAL
logging.critical()
The highest level. Used to indicate a fatal error that has caused or is about to cause the program to stop running entirely.
After you’ve debugged your program, you probably don’t want all these log messages cluttering the screen. The logging.disable() function disables these so that you don’t have to go into your program and remove all the logging calls by hand.
Instead of displaying the log messages to the screen, you can write them to a text file. The logging.basicConfig() function takes a filename keyword argument, like so:
Many programming languages have a ternary operator, which define a conditional expression. The most common usage is to make a terse simple conditional assignment statement. In other words, it offers one-line code to evaluate the first expression if the condition is true, otherwise it evaluates the second expression.
The names argsandkwargs are arbitrary - the important thing are the * and ** operators. They can mean:
In a function declaration, * means “pack all remaining positional arguments into a tuple named <name>”, while ** is the same for keyword arguments (except it uses a dictionary, not a tuple).
In a function call, * means “unpack tuple or list named <name> to positional arguments at this position”, while ** is the same for keyword arguments.
For example you can make a function that you can use to call any other function, no matter what parameters it has:
Inside forward, args is a tuple (of all positional arguments except the first one, because we specified it - the f), kwargs is a dict. Then we call f and unpack them so they become normal arguments to f.
You use *args when you have an indefinite amount of positional arguments.
>>>defshow(arg1,arg2,*args,kwarg1=None,kwarg2=None,**kwargs):>>>print(arg1)>>>print(arg2)>>>print(args)>>>print(kwarg1)>>>print(kwarg2)>>>print(kwargs)>>>data1=[1,2,3]>>>data2=[4,5,6]>>>data3={'a':7,'b':8,'c':9}>>>show(*data1,*data2,kwarg1="python",kwarg2="cheatsheet",**data3)12(3,4,5,6)pythoncheatsheet{'a':7,'b':8,'c':9}>>>show(*data1,*data2,**data3)12(3,4,5,6)NoneNone{'a':7,'b':8,'c':9}# If you do not specify ** for kwargs
>>>show(*data1,*data2,*data3)12(3,4,5,6,"a","b","c")NoneNone{}
Things to Remember(args)
Functions can accept a variable number of positional arguments by using *args in the def statement.
You can use the items from a sequence as the positional arguments for a function with the * operator.
Using the * operator with a generator may cause your program to run out of memory and crash.
Adding new positional parameters to functions that accept *args can introduce hard-to-find bugs.
Things to Remember(kwargs)
Function arguments can be specified by position or by keyword.
Keywords make it clear what the purpose of each argument is when it would be confusing with only positional arguments.
Keyword arguments with default values make it easy to add new behaviors to a function, especially when the function has existing callers.
Optional keyword arguments should always be passed by keyword instead of by position.
While Python’s context managers are widely used, few understand the purpose behind their use. These statements, commonly used with reading and writing files, assist the application in conserving system memory and improve resource management by ensuring specific resources are only in use for certain processes.
with statement
A context manager is an object that is notified when a context (a block of code) starts and ends. You commonly use one with the with statement. It takes care of the notifying.
For example, file objects are context managers. When a context ends, the file object is closed automatically:
1
2
3
4
>>>withopen(filename)asf:>>>file_contents=f.read()# the open_file object has automatically been closed.
Anything that ends execution of the block causes the context manager’s exit method to be called. This includes exceptions, and can be useful when an error causes you to prematurely exit from an open file or connection. Exiting a script without properly closing files/connections is a bad idea, that may cause data loss or other problems. By using a context manager you can ensure that precautions are always taken to prevent damage or loss in this way.
Writing your own contextmanager using generator syntax
It is also possible to write a context manager using generator syntax thanks to the contextlib.contextmanager decorator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
>>>importcontextlib>>>@contextlib.contextmanager...defcontext_manager(num):...print('Enter')...yieldnum+1...print('Exit')>>>withcontext_manager(2)ascm:...# the following instructions are run when the 'yield' point of the context
...# manager is reached.
...# 'cm' will have the value that was yielded
...print('Right in the middle with cm = {}'.format(cm))EnterRightinthemiddlewithcm=3Exit>>>
__main__ is the name of the scope in which top-level code executes.
A module’s name is set equal to __main__ when read from standard input, a script, or from an interactive prompt.
A module can discover whether or not it is running in the main scope by checking its own __name__, which allows a common idiom for conditionally executing code in a module when it is run as a script or with python-m but not when it is imported:
1
2
3
>>>if__name__=="__main__":...# execute only if run as a script
...main()
For a package, the same effect can be achieved by including a main.py module, the contents of which will be executed when the module is run with -m
For example we are developing script which is designed to be used as module, we should do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>># Python program to execute function directly
>>>defadd(a,b):...returna+b...>>>add(10,20)# we can test it by calling the function save it as calculate.py
30>>># Now if we want to use that module by importing we have to comment out our call,
>>># Instead we can write like this in calculate.py
>>>if__name__=="__main__":...add(3,5)...>>>importcalculate>>>calculate.add(3,5)8
Advantages
Every Python module has it’s __name__ defined and if this is __main__, it implies that the module is being run standalone by the user and we can do corresponding appropriate actions.
If you import this script as a module in another script, the name is set to the name of the script/module.
Python files can act as either reusable modules, or as standalone programs.
if __name__==“main”: is used to execute some code only if the file was run directly, and not imported.
The setup script is the centre of all activity in building, distributing, and installing modules using the Distutils. The main purpose of the setup script is to describe your module distribution to the Distutils, so that the various commands that operate on your modules do the right thing.
The setup.py file is at the heart of a Python project. It describes all of the metadata about your project. There a quite a few fields you can add to a project to give it a rich set of metadata describing the project. However, there are only three required fields: name, version, and packages. The name field must be unique if you wish to publish your package on the Python Package Index (PyPI). The version field keeps track of different releases of the project. The packages field describes where you’ve put the Python source code within your project.
This allows you to easily install Python packages. Often it’s enough to write:
1
python setup.py install
and module will install itself.
Our initial setup.py will also include information about the license and will re-use the README.txt file for the long_description field. This will look like:
Dataclasses are python classes but are suited for storing data objects.
This module provides a decorator and functions for automatically adding generated special methods such as __init__() and __repr__() to user-defined classes.
Features
They store data and represent a certain data type. Ex: A number. For people familiar with ORMs, a model instance is a data object. It represents a specific kind of entity. It holds attributes that define or represent the entity.
They can be compared to other objects of the same type. Ex: A number can be greater than, less than, or equal to another number.
Python 3.7 provides a decorator dataclass that is used to convert a class into a dataclass.
The use of a Virtual Environment is to test python code in encapsulated environments and to also avoid filling the base Python installation with libraries we might use for only one project.
Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
The pyproject.toml file will orchestrate your project and its dependencies:
1
2
3
4
5
6
7
8
9
10
11
[tool.poetry]name="my-project"version="0.1.0"description=""authors=["your name <your@mail.com>"][tool.poetry.dependencies]python="*"[tool.poetry.dev-dependencies]pytest="^3.4"
Packages
To add dependencies to your project, you can specify them in the tool.poetry.dependencies section:
1
2
[tool.poetry.dependencies]pendulum="^1.4"
Also, instead of modifying the pyproject.toml file by hand, you can use the add command and it will automatically find a suitable version constraint.
1
$poetryaddpendulum
To install the dependencies listed in the pyproject.toml:
Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. Windows is a first-class citizen, in our world.
Install pipenv
1
pipinstallpipenv
Enter your Project directory and install the Packages for your project
1
2
cdmy_projectpipenvinstall<package>
Pipenv will install your package and create a Pipfile for you in your project’s directory. The Pipfile is used to track which dependencies your project needs in case you need to re-install them.
Uninstall Packages
1
pipenvuninstall<package>
Activate the Virtual Environment associated with your Python project