📚 The CoCalc Library - books, templates and other resources
License: OTHER
This notebook was prepared by Donne Martin. Source and license info is on GitHub.
Functions
Functions as Objects
Lambda Functions
Closures
*args, **kwargs
Currying
Generators
Generator Expressions
itertools
Functions as Objects
Python treats functions as objects which can simplify data cleaning. The following contains a transform utility class with two functions to clean strings:
Below are nose tests that exercises the utility functions:
Execute the nose tests in verbose mode:
core.tests.test_transform_util.TestTransformUtil.test_clean_strings ... ok
core.tests.test_transform_util.TestTransformUtil.test_map_remove_punctuation ... ok
core.tests.test_transform_util.TestTransformUtil.test_remove_punctuation ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK
Lambda Functions
Lambda functions are anonymous functions and are convenient for data analysis, as data transformation functions take functions as arguments.
Sort a sequence of strings by the number of letters:
Closures
Closures are dynamically-genearated functions returned by another function. The returned function has access to the variables in the local namespace where it was created.
Closures are often used to implement decorators. Decorators are useful to transparently wrap something with additional functionality:
Each time the following closure() is called, it generates the same output:
Keep track of arguments passed:
*args, **kwargs
*args and **kwargs are useful when you don't know how many arguments might be passed to your function or when you want to handle named arguments that you have not defined in advance.
Print arguments and call the input function on *args:
Currying
Currying means to derive new functions from existing ones by partial argument appilcation. Currying is used in pandas to create specialized functions for transforming time series data.
The argument y in add_numbers is curried:
The built-in functools can simplify currying with partial:
Generators
A generator is a simple way to construct a new iterable object. Generators return a sequence lazily. When you call the generator, no code is immediately executed until you request elements from the generator.
Find all the unique ways to make change for $1:
Generator Expressions
A generator expression is analogous to a comprehension. A list comprehension is enclosed by [], a generator expression is enclosed by ():
itertools
The library itertools has a collection of generators useful for data analysis.
Function groupby takes a sequence and a key function, grouping consecutive elements in the sequence by the input function's return value (the key). groupby returns the function's return value (the key) and a generator.
itertools contains many other useful functions:
Function | Description |
---|---|
imap | Generator version of map |
ifilter | Generator version of filter |
combinations | Generates a sequence of all possible k-tuples of elements in the iterable, ignoring order |
permutations | Generates a sequence of all possible k-tuples of elements in the iterable, respecting order |
groupby | Generates (key, sub-iterator) for each unique key |