Skip to content

Commit

Permalink
Add functions and logging units
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Copeland <[email protected]>
  • Loading branch information
rick446 committed Oct 7, 2012
1 parent bdf1e75 commit dbad17b
Show file tree
Hide file tree
Showing 32 changed files with 950 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"metadata": {
"name": "01 - Python Basic Syntax"
"name": "01-BasicPythonSyntax"
},
"nbformat": 2,
"worksheets": [
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion 02 - Useful Builtin Functions.ipynb → 02-Builtins.ipynb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"metadata": {
"name": "02 - Useful Builtin Functions"
"name": "02-Builtins"
},
"nbformat": 2,
"worksheets": [
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion 03 - File IO.ipynb → 03-FileIO.ipynb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"metadata": {
"name": "03 - File IO"
"name": "03-FileIO"
},
"nbformat": 2,
"worksheets": [
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion 04 - Using Modules.ipynb → 04-UsingModules.ipynb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"metadata": {
"name": "04 - Using Modules"
"name": "04-UsingModules"
},
"nbformat": 2,
"worksheets": [
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion 05 - String Processing.ipynb → 05-Strings.ipynb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"metadata": {
"name": "05 - String Processing"
"name": "05-Strings"
},
"nbformat": 2,
"worksheets": [
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion 06 - Regular Expressions.ipynb → 06-Regex.ipynb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"metadata": {
"name": "06 - Regular Expressions"
"name": "06-Regex"
},
"nbformat": 2,
"worksheets": [
Expand Down
File renamed without changes.
342 changes: 342 additions & 0 deletions 07-Functions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,342 @@
{
"metadata": {
"name": "07-Functions"
},
"nbformat": 2,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Functions in Python",
"",
"Functions without arguments are straighforward. You can use the `def` keyword to define a",
"function:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def myfunction():",
" print 'Called myfunction'",
"myfunction()"
],
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Called myfunction"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"source": [
"We can also define functions with arguments:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def myfunction2(a, b):",
" print 'Called myfunction2(%r,%r)' % (a,b)",
"myfunction2('avalue', 'bvalue')"
],
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Called myfunction2('avalue','bvalue')"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"source": [
"When calling a function with arguments, you may also pass the arguments *by name* rather than ",
"*positionally.* This is often useful when there are a large number of arguments and you ",
"don't want to remember the order in which they appear."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"myfunction2(b='bvalue first', a='avalue second')"
],
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Called myfunction2('avalue second','bvalue first')"
]
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"source": [
"## `lambda` functions",
"",
"Python provides a special form of defining functions that consist of nothing more than a single",
"expression using the `lambda` keyword:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"lambda_adder = lambda a,b: a+b",
"lambda_adder(1, 2)"
],
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 19,
"text": [
"3"
]
}
],
"prompt_number": 19
},
{
"cell_type": "markdown",
"source": [
"Note the fact that `lambda` returns the function object itself. This form is often used",
"when a function needs to be passed as an argument to another function, as in a callback. Also",
"note the fact that the `return` statement is implicit.",
"",
"The equivalent function defined with `def` would be the following"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def lambda_adder_equiv(a,b):",
" return a+b",
"lambda_adder_equiv(1,2)"
],
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 21,
"text": [
"3"
]
}
],
"prompt_number": 21
},
{
"cell_type": "markdown",
"source": [
"## Default arguments",
"",
"You can define a function with default argument values. If a value is not passed for an argument",
"with a default value, the default will be used instead:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def myfunction3(a, b='default value'):",
" print 'Called myfunction3(%r,%r)' % (a,b)",
"myfunction3('avalue')"
],
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Called myfunction3('avalue','default value')"
]
}
],
"prompt_number": 17
},
{
"cell_type": "markdown",
"source": [
"You can, of course, override the default:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"myfunction3('avalue', 'bvalue')"
],
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Called myfunction3('avalue','bvalue')"
]
}
],
"prompt_number": 18
},
{
"cell_type": "markdown",
"source": [
"## Variable arguments",
"",
"We can define a function that takes any number of arguments using the `*args` syntax. The",
"value of the `args` parameter below is any *positional* arguments that remain after ",
"accounting for other arguments:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def va_adder(prompt, *args):",
" print 'Called va_adder(%r, *%r)' % (prompt, args)",
" return sum(args)",
"va_adder('ThePrompt>', 1,2,3)"
],
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Called va_adder('ThePrompt>', *(1, 2, 3))"
]
},
{
"output_type": "pyout",
"prompt_number": 26,
"text": [
"6"
]
}
],
"prompt_number": 26
},
{
"cell_type": "markdown",
"source": [
"Likewise, we can call a function with a `tuple` of arguments using a similar syntax:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def normal_function(a,b):",
" print 'Called normal_function(%r, %r)' % (a,b)",
"argument_tuple = ('avalue', 'bvalue')",
"normal_function(*argument_tuple)"
],
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Called normal_function('avalue', 'bvalue')"
]
}
],
"prompt_number": 25
},
{
"cell_type": "markdown",
"source": [
"If you want to define a function with variable *keyword* arguments, you can do that as well",
"with the `**kwargs` syntax. In this case, the keyword arguments are passed as a `dict`:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def kw_function(**kwargs):",
" print kwargs",
"kw_function(a='avalue', b='bvalue')"
],
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{'a': 'avalue', 'b': 'bvalue'}"
]
}
],
"prompt_number": 27
},
{
"cell_type": "markdown",
"source": [
"Of course, we can also pass a dictionary as the keyword arguments of a function:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"arguments = { 'a': 'avalue', 'b': 'bvalue' }",
"normal_function(**arguments)"
],
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Called normal_function('avalue', 'bvalue')"
]
}
],
"prompt_number": 28
},
{
"cell_type": "markdown",
"source": [
"### Exercise",
"",
"Write a function with the signature `def log(format, *args, **kwargs):` which prints a ",
"line, formatted according to the format string. Some sample results are below:",
"",
" >>> log('The pair is (%r,%r)', 1, 2)",
" The pair is (1,2)",
" >>> log('The value of a is %(a)r', a='foo')",
" The value of a is 'foo'"
]
},
{
"cell_type": "code",
"collapsed": true,
"input": [],
"language": "python",
"outputs": []
}
]
}
]
}
Loading

0 comments on commit dbad17b

Please sign in to comment.