Listening-Based Python at DLF Forum 2014

Unit 3: The Power of Functions

We'll cover...:

  • What functions are and why we care
  • Function syntax

...but we'll also cover:

  • What DRY means and why we care
  • Modifying code for fun and (non)profit

Function basics

  • What is a function?
  • How do you write a python function?
  • def function_name(argument1, argument2, ...):
        """
        docstring explaining what the function does (optional but recommended)
        """
        # function logic goes here
        return something  # optional
    
  • An example
  • def make_happier(base_word):
        return base_word + " :)"
    
  • A more complicated example
  • 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
    

Our first function

Let's work together to DRY out our handling of the 590 field.

Function practice

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 need practice with Python function fundamentals...

If you find unfamiliar material in here, you can use this python tutorial as a reference.

If you'd like to practice with more complex, real-world examples...

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?

If you were writing your own functions during Unit 2...

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:

  • The Record object is full of convenience functions like 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?
  • Reader has a map_records function. What does it do? Can you use it to make your earlier functions more elegant?
  • What's the benefit of having the code broken out into 8 separate files (plus __init__) instead of just pymarc.py?
  • Are the docstrings helpful to you? If so, what makes them good? If not, how could you improve them?
  • Why does exceptions.py exist? (If you're not familiar with exceptions, read the python documentation.)

Debrief

Let's share code, discuss what we've learned, and clarify things still in need of clarification.