Skip to content

Commit 98add33

Browse files
committed
random seed
1 parent 25bab71 commit 98add33

4 files changed

Lines changed: 100 additions & 37 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ I believe you can learn serious things through silly games.
44

55
I'd like to make this into a book or something, similar to the Python bioinformatic/data science (https://github.com/kyclark/practical_python_for_data_science) repo. I think you will learn best by *doing*, so I think I will write this as a loose collection of exercises that spell out the skills I aim to teach with each exercise. I will create descriptions for each exercise with examples of how the program should work along with a test suite. You will need to write the program that satisfies the test suite.
66

7+
I think I'm going to present this differently from other material in that I won't necessarily show you beforehand what you need to write a program. I'll describe what the program should do and provide some discussion about how to write it. I'll also create an appendix with short example of how to do things like read/write from/to a file, process all the files in a directory, extract k-mers from a string, etc. I'll provide some building blocks, but I want you to figure out how to put the pieces together!
8+
79
# new_py.py
810

911
I provide a program in the `bin` directory called `new_py.py` that will help you stub out new Python programs using the fabulous `argparse` module to parse the command line arguments and options for your programs. I highly recommend you start every new program with this. For example, if the `README` say "Write a Python program called `abc.py` that ...", then you should do this:

appendix/examples/random_seed.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : kyclark
4+
Date : 2019-05-17
5+
Purpose: Get/use a random seed from argparse
6+
"""
7+
8+
import argparse
9+
import random
10+
import sys
11+
12+
13+
# --------------------------------------------------
14+
def get_args():
15+
"""get command-line arguments"""
16+
parser = argparse.ArgumentParser(
17+
description='Get/use a random seed from argparse',
18+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
19+
20+
parser.add_argument('-s',
21+
'--seed',
22+
help='Random seed',
23+
metavar='int',
24+
type=int,
25+
default=None)
26+
27+
return parser.parse_args()
28+
29+
30+
# --------------------------------------------------
31+
def main():
32+
"""Make a jazz noise here"""
33+
args = get_args()
34+
random.seed(args.seed)
35+
36+
print('Random number is "{}"'.format(random.randint(1, 100)))
37+
38+
39+
# --------------------------------------------------
40+
if __name__ == '__main__':
41+
main()

appendix/howto.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,63 @@ bar
164164
baz
165165
````
166166

167+
## Accept/use a random seed argument
168+
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+
171+
````
172+
$ cat -n random_seed.py
173+
1 #!/usr/bin/env python3
174+
2 """
175+
3 Author : kyclark
176+
4 Date : 2019-05-17
177+
5 Purpose: Get/use a random seed from argparse
178+
6 """
179+
7
180+
8 import argparse
181+
9 import random
182+
10 import sys
183+
11
184+
12
185+
13 # --------------------------------------------------
186+
14 def get_args():
187+
15 """get command-line arguments"""
188+
16 parser = argparse.ArgumentParser(
189+
17 description='Get/use a random seed from argparse',
190+
18 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
191+
19
192+
20 parser.add_argument('-s',
193+
21 '--seed',
194+
22 help='Random seed',
195+
23 metavar='int',
196+
24 type=int,
197+
25 default=None)
198+
26
199+
27 return parser.parse_args()
200+
28
201+
29
202+
30 # --------------------------------------------------
203+
31 def main():
204+
32 """Make a jazz noise here"""
205+
33 args = get_args()
206+
34 random.seed(args.seed)
207+
35
208+
36 print('Random number is "{}"'.format(random.randint(1, 100)))
209+
37
210+
38
211+
39 # --------------------------------------------------
212+
40 if __name__ == '__main__':
213+
41 main()
214+
$ ./random_seed.py
215+
Random number is "31"
216+
$ ./random_seed.py
217+
Random number is "35"
218+
$ ./random_seed.py -s 1
219+
Random number is "18"
220+
$ ./random_seed.py -s 1
221+
Random number is "18"
222+
````
223+
167224
## Test if an argument is a directory and list the contents
168225

169226
````

insult_generator/insult.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)