Today I’m going through exercises 14-18 of http://learnpythonthehardway.org/book/! Ooooooooh, what craziness will I learn today?! Exciting craziness, that’s what!
Exercise 14: Prompting and Passing
- -so here we look at using argv and raw_input together
- -it’s like a text adventure!!
- -So custom prompts can be given by creating a variable. Neat-o
- -this is pretty straight forward
- -something to note is that if I use tab to autocomplete it inserts the . for the file path, but this is totally not necessary by the interpreter… just the windows power shell
- -I should be sure to try
“Run pydoc file and scroll down until you see the read() command (method/function). See all the other ones you can use? Skip the ones that have __ (two underscores) in front because those are junk. Try some of the other commands.”
later
Exercise 16: Reading And Writing Files
- -close — Closes the file. Like File->Save.. in your editor.
- -read — Reads the contents of the file. You can assign the result to a variable.
- -readline — Reads just one line of a text file.
- -truncate — Empties the file. Watch out if you care about the file.
- -write(stuff) — Writes stuff to the file.
- -so the “w” tag is used by the open() function to indicate that we’ll be able to write once the file is open. use “r” if it’s read only.
- – search for open(name[, mode[, buffering]]) if you want to find the info about it in docs.python.org
- -If I want to both read and write, I’m not sure of the best way to do it… opening it with r doesn’t let me write, opening with w doesn’t let me read… if I use r+ it doesn’t seem to allow the truncate, but it lets me append new things to it… so I can read, and then print, and then write… but, if I read and write as two separate variables, and then print… the read variable is blank now… not sure why this is…
- – opening with w seems to truncate regardless of whether we pass a truncate() in it…
- -Python (the language) does not promise that it will close the files for you. The operating system does, when the program exits. In this trivial program, that’s right away. But if your program does something else for a while, or repeats this sequence of steps dozens of times, you could run out of resources, or overwrite something.
- –
#in_file = open(from_file)
#indata = in_file.read()
can be replaced withindata = open(from_file).read()
- –
indata = open(from_file).read()
means that the script executes and then python will close, therefore we don’t need to close again at the bottom
Exercise 18: Names, Variables, Code, Functions
- -First we tell Python we want to make a function using def for “define”.
- -On the same line as def we then give the function a name. In this case we just called it “print_two” but it could be “peanuts” too. It doesn’t matter, except that your function should have a short name that says what it does.
- -Then we tell it we want *args (asterisk args) which is a lot like your argv parameter but for functions. This has to go inside () parentheses to work.
- -Then we end this line with a : colon, and start indenting.
- -After the colon all the lines that are indented four spaces will become attached to this name, print_two. Our first indented line is one that unpacks the arguments the same as with your scripts.
- -To demonstrate how it works we print these arguments out, just like we would in a script.
- -It took me a while to understand wtf he was talking about above. He’s just talking about how the definition is on one line, and then it’s contents are indented. Makes sense now that I think about it. I’ve always done that with html code too, but it’s a legibility thing there, not a mandatory practice.
- -I’ve got a mean habbit of using $ instead of % thanks to HScript :-/