Tiny Python Projects - Book Summary
Contents
The following post contains a summary of the book titled Tiny Python Projects by Ken Youens-Clark
Chapter 1
- The use of shebang at the top of the file is meant to indicate the type of program that can be used to run the code in the file. Most of the Programming languages - the program is written in a text file and relevant executable is used to invoke the program. The use of
#!is a hint to the type of executable to be used for running the script #!/usr/bin/env python3is a way of telling OS to use environment command to find out the location ofpython3and then use the relevant executable on the script$PATH$is a way of telling OS to find the relevant places where executables are available- The basic ideas behind creating an executable are two fold. First is to make sure that the OS recognizes the program as an executable. Second is to make sure that the executable is in a place that is mentioned in the
$PATH$variable parser.parse_argsasks the parser to parse any arguments to the program
Chapter 2
- Have learned the way to write test-cases for simple programs
- Learned about positional and optional arguments that can be specified via
argparselibrary - Every feature of a program must have tests. Writing and running test suites should be an integral part of writing programs
- Template that creates a basic python program with some skeleton. One can further work on this skeleton to fill up flesh
Chapter 3
reversedon a list gives a generator- If you want to take in a list of items representing a specific parameter, it is helpful to use
nargsattribute in theparser.add_argumentfunction - I haven’t learn any thing specific to Python
lists. It is more about ways to capture the command line arguments, that I seem to be learning in all the first three chapters
Chapter 4
- PEP guide says the variables need to be named with lowercase names separated by underscores
printtakes an additional argumentendwhich you can use to print a value to the screen without a newline
Chapter 5
- The author’s solution was extremely elegant as compared to my solution
- Misinterpreted the input to the program as a list instead of string
- Was unnecessarily trying to use
typein File argument parser - If you define multiple positional arguments, then it is important to define it in the correct order. The order of optional arguments is not important. They can be defined before or after the positional arguments
- One can treat a regular string value as if it were a generator of values similar to a file handle. One can use the return value of
io.IOStreamas a “mock” file handle so that the code can handle the input seamlessly across file input and string input - To write a file in text mode, one can use the mode
rt - STDOUT is always available via
sys.stdoutfile handle - Never knew that one could simulate
printfunctionality via
|
|
Chapter 6
- To specify standard input as the default value, one needs to specify the
sys.stdinin a list - I have spent quite a number of hours working through this chapter. Even though the code was simple, there was a quite a bit of learning on the way
argparsemodule handlesfile input - Never knew that
argparsemodule automatically handles the files and createsfile handlers - Use of
nargsfor specifying one or more/ zero or more inputs argparse.FileType('rt')will validate the user has provided a readable text file and will make the value available in your code as an open file handle
Chapter 7
- Dictionary comprehension can be used to quickly create dictionaries
- Defining file input arguments using
argparse.FileTypesaves you time and code
Chapter 8
- use of
choicesin the argparse field will automatically create the relevant error messages transobject that is a result of applyingmaketransfunction gives a dict- I had never know that one could write more than one conditional in a list comprehension
|
|
In the above code the function applies to each element of the list comprehension is the result of applying if, elif and else logic. However one never needs to use elif
maptakes a function and an iterable. The result is another iterable where each element is the result of applying the function to each element of the iterable- The author presents eight different solutions and personally I liked the
regexandlist comprehensionapproaches - In
regexyou specify what you want to achieve and the rest is done by the regex engine. This is an example of declarative programming mapis called higher order function(HOF) because it takes another function as an argument- The chapter also introduces the concept of
closures
Chapter 9
- Learned about receiving
intarguments viaargparse - There are certain aspects that
argparsecannot check. In all such cases, one needs to manually write logic that usesparser.errorfunction to display message indicating erroneous arguments
Chapter 10
- Learned to use a float argument to command line so that
argparsercan pick up the relevant input random.seed()can acceptNoneas input
Chapter 11
- Had completely forgotten the option to define a reversed list of numbers using
stepargument
Chapter 12
- Learned about using
mapreducefor a very simple example - Learned about various ways of solving the problem posed in the chapter
Chapter 13
- Spent one hour to get this right. Most importantly I have got all the tests right and have never looked at the solution
- Why did it take one hour ? I think I was struggling to get the verse endings right with trailing newline characters
- Also i realized that i was making a typo with
twelfth - Learned about
endargument in theprintstatement - When one mentions a file input with
typeattribute,argparsewill automatically open the file inreadmode orwritemode sys.stdoutfile handle is always open for writing- shadowing a variable or function means reusing an existing variable or function
Chapter 14
- Learned about the use of using groups in pattern in regex
- Use regex to match various patterns or figure out the absence of patterns
- use of
re.matchto find out patterns in the form of a dictionary - use of
re.findallto figure out missing patterns in the word re.matchstarts from the beginning of the textre.searchwill match the string anywhere in the text- ran
pylintand learned a few aspects of code formating
Chapter 15
- Was struggling to split the text that had whitespace in the beginning. Never realized that one can use
regexto split the text - If capturing parenthesis are used as a part of regex pattern, then the text of all groups are returned as part of the resulting list
- The biggest learning from this chapter is
re.split()function
Chapter 16
random.shuffledoes in place sortingsplitlinesmethod for file handler objects
Chapter 17
- Regular expressions are almost like functions where we describe what we want as functions
- Regex patterns with
*and+are greedy patterns in that they match as many characters as possible. Adding?after them makes it non-greedy re.findallwill find all the matching patterns in a stringre.subwill substitute the relevant matching patterns in a string with a replacement string- One can halt a program with a
sys.exitcall
Chapter 18
- use of
ordfunction
Chapter 19
- Was a straightforward exercise using
pandas - came across
tabulatepackage to print tabular contents
Chapter 20
- Spent at least 90 minutes on this exercise and managed to worked through it
- cleaning text and extracting words cannot be accomplished with just splitting on white space. One must use
regexto split the word that takes care of punctuation
Chapter 21
- Took me two hours to work through all the tasks mentioned in the project
- This was also the first time that I ran pytest on an intermediate file that wasn’t the master test file
- spent sometime in understanding the board structure and ways to codify it
- wrote four functions in
utils.pyso that i could reuse them at many places - once the helper functions were written, it did not take me a long time to code the main function that put all the pieces together
- this chapter made me realize the importance of writing test scripts. Whatever be your proficiency in python, it is foolhardy to assume that the code that you have written will run without any problems. There will be edge cases and one will have to use a testing library to run through the tests and make sure that the code passes all the tests
- used XOR operator in the code for the first time
- author’s
format_boardfunction was much more elegant than my approach - author’s input validation code was also more elegant as it uses regular expression instead of manually checking the length of input arguments and using sets to check for the existence of a pattern
- taught me a lot of stuff relating to various aspects of python as well as touch typing
- got to use
choicesargument inargparselibrary
Chapter 22
- motivation behind a named tuple- It would be nice to combine the safety of an immutable tuple with named fields
collectionsmodule hasnamedtuplethat gives this functionality- one can use
namedtupleto store all the states of the game and associated error messages and game messages that might crop up as the interactive game progresses - TypedDict got introduced in Python 3.8 that has a similar functionality to NamedTuple. However it looks like the former is the preferred option for the author
- Avoid using global variable unless it is really needed
NamedTupleandTypedDictallow you to create a novel data type with defined fields and types that you can use as type hints to your own functions- Documentation is a love letter that you write to your future self
Using argparse
- this module is like a bouncer that allows the right kind of input in to the program
- the module automatically generates help prompt documentation
metavaris the variable that is visible in the documentation- the lack of
-makes the argument a positional argument instead of a optional argument - one can specify a
typeargument in defining the argument in the parser. Thistypeis a Python data type that the input must be converted to - big advantage of argparse
typeis taking input from command line that are essentially used as numeric values in the program flagsare usually optional arguments that do not have value. They can be represented in the parser usingstore_trueorstore_false- If there is any incorrect input to the program, the
parser.parse_argswill throw error - one can restrict the input values to the program by using
choicesargument - if one needs to specify more than one of a positional argument, one can use
nargsargument argparse.FileType('r')is used to specify that the input argument is a file
Fantastic appendix that gave me a good understanding of argparse module
Takeaway
I have been working through this book for the last six days and have had a great learning experience in the process. If I have to list down some of the takeaways, they are
pytestmodule and its relevance to writing any production level code- various features of
argparsemodule to validate user input - powerful use of
regexfor searching , splitting, replacing operations on a text - confidence that i can code in python and get things done
- understood the importance of
pylint. Every time i run apylintcommand on a python program, I will be able to immediately get feedback on the code formatting and style that I have followed