You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: appendix/howto.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# How To Do Common Operations in Python
2
2
3
-
##Get positional command-line arguments
3
+
# Get positional command-line arguments
4
4
5
5
You can get the command-line arguments using `sys.argv` (argument vector), but it's annoying that the name of the Python program itself is in the first position (`sys.argv[0]`). To skip over this, take a slice of the argument vector starting at the second position (index `1`) which will succeed even if there are no arguments -- you'll get an empty list, which is safe.
6
6
@@ -22,7 +22,7 @@ There are 1 arg
22
22
$ ./args.py foo bar
23
23
There are 2 args
24
24
````
25
-
##Put positional arguments into named variables
25
+
# Put positional arguments into named variables
26
26
27
27
If you use `sys.argv[1]` and `sys.argv[2]` throughout your program, it degrades readability. It's better to copy the values into variables that have meaningful names like "file" or "num_lines".
28
28
@@ -51,7 +51,7 @@ $ ./name_args.py nobody.txt 10
51
51
FILE is "nobody.txt", NUM is "10"
52
52
````
53
53
54
-
##Set defaults for optional arguments
54
+
# Set defaults for optional arguments
55
55
56
56
````
57
57
$ cat -n default_arg.py
@@ -79,7 +79,7 @@ $ ./default_arg.py nobody.txt 5
79
79
FILE is "nobody.txt", NUM is "5"
80
80
````
81
81
82
-
##Test argument is file and read
82
+
# Test argument is file and read
83
83
84
84
This program takes an argument, tests that it is a file, and then reads it. It's basically `cat`.
85
85
@@ -121,7 +121,7 @@ To an admiring Bog!
121
121
Emily Dickinson
122
122
````
123
123
124
-
##Write data to a file
124
+
# Write data to a file
125
125
126
126
To write a file, you need to `open` some filename with a second argument of the "mode" where
127
127
@@ -164,7 +164,7 @@ bar
164
164
baz
165
165
````
166
166
167
-
##Accept/use a random seed argument
167
+
# Accept/use a random seed argument
168
168
169
169
Here is how I would define and use a random seed argument using `argparse`. If the `--seed` is defined with `default=None`, then you can pass it directly to `random.seed`. When it is defined by the user, it will be used; otherwise it is ignored.
170
170
@@ -221,7 +221,7 @@ $ ./random_seed.py -s 1
221
221
Random number is "18"
222
222
````
223
223
224
-
##Test if an argument is a directory and list the contents
224
+
# Test if an argument is a directory and list the contents
225
225
226
226
````
227
227
$ cat -n list_dir.py
@@ -266,7 +266,7 @@ codons.py
266
266
default_arg.py
267
267
````
268
268
269
-
##Loop N Times
269
+
# Loop N Times
270
270
271
271
If you want to go through a loop some defined number of times, maybe combine `for` and `range`:
272
272
@@ -311,7 +311,7 @@ $ ./looping_n_times.py 3
311
311
3 times
312
312
````
313
313
314
-
##Skip an iteration of a loop
314
+
# Skip an iteration of a loop
315
315
316
316
Sometimes in a loop (`for` or `while`) you want to skip immediately to the top of the loop. You can use `continue` to do this. In this example, we skip the even-numbered lines by using the modulus `%` operator to find those line numbers which have a remainder of 0 after dividing by 2. We can use the `enumerate` function to provide both the array index and value of any list.
317
317
@@ -351,7 +351,7 @@ $ ./skip_loop.py nobody.txt
351
351
11 Emily Dickinson
352
352
````
353
353
354
-
##Create a directory if it does not exist
354
+
# Create a directory if it does not exist
355
355
356
356
This program takes a directory name and looks to see if it already exists or needs to be created.
357
357
@@ -476,7 +476,7 @@ $ ./unpack_dict2.py
476
476
1982 Signals
477
477
````
478
478
479
-
##Sort a dictionary by keys
479
+
# Sort a dictionary by keys
480
480
481
481
To sort a dictionary by the keys, you have to understand that the `.sort()` method of an list mutates the list *in-place*. We get the keys of a dictionary with the `.keys()` method which does not support the `.sort()` method:
482
482
@@ -596,7 +596,7 @@ Power Windows 1985
596
596
Signals 1982
597
597
````
598
598
599
-
##Sort a dictionary by values
599
+
# Sort a dictionary by values
600
600
601
601
To sort a dictionary by the values rather than the keys, we need to reverse the tuples which is what happens on line 24. Notice that in years when two albums were released, the `sorted` first sorts by the first tuple member (the year) and then the second (album name):
0 commit comments