!pwd/home/jupyter-anand/book
October 23, 2023
A lot of times, we may want to:
There two modules in Python standard library that allows us to do this - os and subprocess.
The os module is very primitive and not that safe. The subprocess module is safe and very nice!
0 indicates successful termination of the process. Any non-zero status indicates error.
The previous command terminated with non-zero exit status, indicating that it was not successful.
Let’s see what happens if we use the os.system.
The os.system passes the command to shell and shell executes it. When we say echo foo > bar, shell interprets that as output redirection to file bar.
When we call subprocess.call, we specify each argument explicitly instead leaving that for the shell to parse.
You can even make subprocess unsafe, by forcing it to use shell to run the command. But that is not the default behavior. With subprocess, we are safe by default.
1
/bin/sh: 1: /bin/sh: 1: 2: not found
3: not found
/bin/sh: 1: 4: not found
127
We can read the output by specifying stdout as a pipe.
By default, the output is bytes. We can force it to be text, by passing text=True.
cat: bad-file: No such file or directory
We can also tell subprocess to send stderr to stdout.
_ _ _
| |__ ___| | | ___
| '_ \ / _ \ | |/ _ \
| | | | __/ | | (_) |
|_| |_|\___|_|_|\___/
Let’s implement a function figlet, that calls the figlet command and returns the output of the command.
" _ _ _ \n| |__ ___| | | ___ \n| '_ \\ / _ \\ | |/ _ \\ \n| | | | __/ | | (_) |\n|_| |_|\\___|_|_|\\___/ \n \n"
_ _ _
| |__ ___| | | ___
| '_ \ / _ \ | |/ _ \
| | | | __/ | | (_) |
|_| |_|\___|_|_|\___/
There is command cowsay to print a text as if a cow is saying it.
____________________
< python is awesome! >
--------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Problem: Write a function cowsay that takes a text as argument, calls the command cowsay with that text and returns the output of that command.
>>> x = cowsay("python is awesome")
>> print(x)
___________________
< python is awesome >
-------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Regular Expression is a mini-language for pattern matching.
How many fruits are there in that sentence?
Let’s see how to do that with regular expressions.
Suppose we want to mask the numbers.
A regular expression can have an ordinary or special characters.
. matches any characterIf we want to match a literal ., we need escape it.
| is used to match either of the two patternsA character group matches any of the characters in thr group.
We can also specify a range.
We can also negate a character group using ^.
? - matches 0 or 1 occurances* - matches 0 or more occurances+ - matches 1 or more occurancesLets say we want to match hexadecimal numbers like 0x12fe.
<re.Match object; span=(11, 16), match='0x12f'>
\d - same as [0-9]\s - any white space\w - any identifier (something like [a-zA-Z0-9_]+)[('10', 'apples'), ('20', 'mangoes')]
[('10', 'apples'), ('20', 'mangoes'), ('30', 'bananas')]
The special characters ^ and $ match the begin and the end of the string respectively.
Remove trailing space.
findall
finds all occurances.
match
matches a regular expression at the beginning of a string.
search
similar to match, but find anywhere in the string.
sub
split
If we put the pattern in a group, that is also included in the result.
Write a function squeeze to replace multiple continuous space characters with a single space.
>>> squeeze("a b c d")
'a b c d'
You can verify your solution using:
%verify_problem squeeze
%%file git-log.txt
commit b27f92644e44657533671155ce92f597ffdc2b03
Author: Anand Chitipothu <anandology@gmail.com>
Date: Thu Jul 7 08:22:38 2022 +0530
Added gitignore
commit 7ed3348503ccd2f234d62f848428646469625ee5
Author: Anand Chitipothu <anandology@gmail.com>
Date: Thu Jul 7 08:21:50 2022 +0530
Added version checks to verify commit script
commit cb86214516da82c619417e91df485ae1c739e645
Author: Anand Chitipothu <anandology@gmail.com>
Date: Wed Jul 6 23:16:54 2022 +0530
Verify your python workspace Writing git-log.txt
Can we convert this into oneline log of git?
b27f926 Added gitignore
7ed3348 Added version checks to verify commit script
cb86214 Verify your python workspace
commit b27f92644e44657533671155ce92f597ffdc2b03
Author: Anand Chitipothu <anandology@gmail.com>
Date: Thu Jul 7 08:22:38 2022 +0530
Added gitignore
commit 7ed3348503ccd2f234d62f848428646469625ee5
Author: Anand Chitipothu <anandology@gmail.com>
Date: Thu Jul 7 08:21:50 2022 +0530
Added version checks to verify commit script
commit cb86214516da82c619417e91df485ae1c739e645
Author: Anand Chitipothu <anandology@gmail.com>
Date: Wed Jul 6 23:16:54 2022 +0530
Verify your python workspace
['',
'b27f92644e44657533671155ce92f597ffdc2b03',
'\nAuthor: Anand Chitipothu <anandology@gmail.com>\nDate: Thu Jul 7 08:22:38 2022 +0530\n\n Added gitignore\n\n',
'7ed3348503ccd2f234d62f848428646469625ee5',
'\nAuthor: Anand Chitipothu <anandology@gmail.com>\nDate: Thu Jul 7 08:21:50 2022 +0530\n\n Added version checks to verify commit script\n\n',
'cb86214516da82c619417e91df485ae1c739e645',
'\nAuthor: Anand Chitipothu <anandology@gmail.com>\nDate: Wed Jul 6 23:16:54 2022 +0530\n\n Verify your python workspace \n']
['',
'b27f92644e44657533671155ce92f597ffdc2b03',
'\nAuthor: Anand Chitipothu <anandology@gmail.com>\nDate: Thu Jul 7 08:22:38 2022 +0530\n\n Added gitignore\n\n',
'7ed3348503ccd2f234d62f848428646469625ee5',
'\nAuthor: Anand Chitipothu <anandology@gmail.com>\nDate: Thu Jul 7 08:21:50 2022 +0530\n\n Added version checks to verify commit script\n\n',
'cb86214516da82c619417e91df485ae1c739e645',
'\nAuthor: Anand Chitipothu <anandology@gmail.com>\nDate: Wed Jul 6 23:16:54 2022 +0530\n\n Verify your python workspace \n']