def function_name(argument1, argument2, ...): """ docstring explaining what the function does (optional but recommended) """ # function logic goes here return something # optional
def make_happier(base_word):
return base_word + " :)"
def get_isbn_13(isbn): """ Given an ISBN, returns the 13-digit version of that ISBN. Does not check to see if it's a valid ISBN except by confirming length; returns None if length is invalid. It's the caller's responsibility to confirm validity if needed. """ if len(isbn) == 13: return isbn elif len(isbn) == 10: return "978" + isbn else: # Something is wrong; ISBNs should only be 10 or 13 digits. return None
Let's work together to DRY out our handling of the 590 field.
There are more practice functions here than anyone is likely to finish. That's intentional! I want to be sure that no one runs out of things to do, and that there are options at varying levels of difficulty since our prior experience with programming varies. Please start wherever makes sense for you, and continue at whatever pace is productive for you.
If you find unfamiliar material in here, you can use this python tutorial as a reference.
There are sample functions in the practice_functions directory of our lesson. I've provided a skeleton for you; replace the comments with your own logic.
When you'd like to see if it works, make sure you're in the
practice_functions
directory on your own machine and
simply run python practice_1.py
(or whatever function
you've been working on). It will automatically run your code on all the MARC records in the practice_functions/example_records
directory.
It's very important that you sanity-check this. Look at the example MARC records in that directory (I've provided XML versions to make it easier) and see if you are getting the results you expect. If you don't have a tool on your computer to format XML for easy viewing, you can copy-paste the XML code into this site to get an easier-to-read version. (I use the file:// protocol in Chrome.)
You may find it helpful to look at the source code for pymarc, particularly the code for the Record object, to see what options pymarc provides.
You can also try out the examples suggested by pymarc. Using their sample file, can you get the same list of titles that they give in #1? Following their example in #2, can you write a more sophisticated record? If you can do that, how about a record adequate to your preferred minimum level of cataloging standards?
Let's talk about how to debug them, improve style, and/or abstract them to cover a wider range of circumstances.
If you prefer to mentor your classmates or work through the real-world practice functions, those are also great options.
If you'd like to read pymarc source, that's a great option as well. Here are some questions to consider:
title
, isbn
, and author
. What errors are they trying to defend against with their various if
conditions? What types of objects do they return? When and how is this different from what you'd get performing similar queries with get_fields
or similar?
map_records
function. What does it do? Can you use it to make your earlier functions more elegant?
__init__
) instead of just pymarc.py?
exceptions.py
exist? (If you're not familiar with exceptions, read the python documentation.)
Let's share code, discuss what we've learned, and clarify things still in need of clarification.