One small piece of a friend's larger project, in which I learned about working on the command line, parsing YAML and JSON, writing tests, and using Git and GitHub. Good times! Below are my notes from the process.
Tried to set TextMate as the default editor for git, using git config --global core.editor mate -w (the 3rd answer here: http://stackoverflow.com/questions/3539594/change-the-default-editor-for-files-opened-in-the-terminal-e-g-set-it-to-texte )
When I typed "git commit" it would open TextMate, but it would automatically abort the commit.
Using nano instead, it worked fine. (git config --global core.editor nano)
Installed Swampy and started working on Think Python chap 9 exercises. NEXT TASK: finish Ex. 9.2.
NEXT: finish Ex. 9.6, p. 103
NEXT: finish Ex. 11.3, p. 126
Copied cpangloss directory to dropbox using the command line. Might need to change path to git repository?
NEXT: try to make something of a META.yml file.
Downloaded and installed pyYAML. Both Terminal and Idle can find the yaml module, but TextMate can't. WHY?
Useful stuff in pyYAML:
- yaml.load(fin) converts a YAML document into a Python object. See also load_all() and safe_load()
- yaml.dump() accepts a Python object and produces a YAML document.
- optional 2nd argument: an open text or binary file in which to write the document (which is otherwise returned by the function)
Wrote "extract_requirements" function:
- takes a yaml file
- converts it to a python dict
- searches the dict for keys containing "requires" and stores them in a new dict
- converts the new dict into JSON and returns it.
NEXT: figure out how to commit it to git?
Created cpanglossProject dir to put git stuff in (NEXT: move files there)
Researched tar format
JEFF SEZ:
"Error handling:
try:
json.loads('blah blah')
except ValueError:
print("Wasn't json!")
try:
open('blah blah')
except IOError:
print("Wasn't a file!")
Except do something useful instead of printing an error message.
Also: write a test involving a fake yaml file that doesn't include requirements - make sure the reqextractor can handle this situation.
Also: rewrite the function to output the "expected_json_dict" in the test. BUT FIRST, write the test to check for the things that aren't there yet: name and version. Check whether the value of 'data' is of type dict; check for the number of keys, etc."
TO DO: learn about the following python libraries:
- ipdb
- unittest (not covered in Think Python)
- cStringIO
- to assert that dicts are equal, use a for loop to compare each key.
- if a string/file without the necessary data is passed to extract_requirements, the function should return {success : false, errors : [ 'error message', 'more different error message' ] }
- 5 is a magic number. Instead, use len(useful_data)
- add optional argument to allow for different data types
NEXT: look at the additions Jeff made to last test
QUESTION FOR JEFF: does it still make sense to split extract_requirements into multiple functions?
- GitHub for Mac requires a 64-bit processor. (Raebot has a 32.)
- Pushed my local repository to GitHub.