Skip to content

Commit

Permalink
updates for computationally calculated dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
philtweir committed Sep 4, 2021
1 parent 21a00bf commit 99f4a5e
Show file tree
Hide file tree
Showing 9 changed files with 795 additions and 793 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ __pycache__
.mypy_cache
.venv
.vscode
env
1 change: 1 addition & 0 deletions 001-what-is-python/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ <h1>(Who am I?)</h1>
<li class='fragment'>Studied numeric modelling</li>
<li class='fragment'>Scientific programmer in industry (Dundalk)</li>
<li class='fragment'>Web, data and infrastructure consultant</li>
<li class='fragment'>Open Source as a Service</li>
</ul>
</section>
<section data-background="images/snake-side.png" data-background-color="black" data-state="img-right">
Expand Down
118 changes: 118 additions & 0 deletions 002-basic-control-structures/Answers.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "f70a0f3e",
"metadata": {},
"source": [
"# 002: Basic Control Structures"
]
},
{
"cell_type": "markdown",
"id": "aec9a83f",
"metadata": {},
"source": [
"## Zipping Up"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "b5663e8e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Set my_dictionary[ a ] to be 23\n",
"Set my_dictionary[ b ] to be 42\n",
"Set my_dictionary[ c ] to be 12\n",
"Set my_dictionary[ d ] to be 9\n",
"Set my_dictionary[ e ] to be 5\n",
"Set my_dictionary[ f ] to be 1\n",
"{'a': 23, 'b': 42, 'c': 12, 'd': 9, 'e': 5, 'f': 1}\n",
"Set my_dictionary[ 23 ] to be a\n",
"Set my_dictionary[ 42 ] to be b\n",
"Set my_dictionary[ 12 ] to be c\n",
"Set my_dictionary[ 9 ] to be d\n",
"Set my_dictionary[ 5 ] to be e\n",
"Set my_dictionary[ 1 ] to be f\n",
"{23: 'a', 42: 'b', 12: 'c', 9: 'd', 5: 'e', 1: 'f'}\n"
]
}
],
"source": [
"# Loop through them to make a dictionary, where the first list is the keys,\n",
"# and the second list is the values.\n",
"# You might want to make this into a function where the first argument is\n",
"# the first list, and the second argument is the second list\n",
"def zip_two_lists(key_list, value_list):\n",
" my_dictionary = {}\n",
" for i in range(len(key_list)):\n",
" my_key = key_list[i]\n",
" my_value = value_list[i]\n",
"\n",
" print('Set my_dictionary[', my_key, '] to be ', my_value)\n",
" my_dictionary[my_key] = my_value\n",
" return my_dictionary\n",
"\n",
"# Print out your new dictionary\n",
"my_list_of_keys = ['a', 'b', 'c', 'd', 'e', 'f']\n",
"my_list_of_values = [23, 42, 12, 9, 5, 1]\n",
"my_new_dictionary = zip_two_lists(my_list_of_keys, my_list_of_values)\n",
"print(my_new_dictionary)\n",
"my_new_dictionary = zip_two_lists(my_list_of_values, my_list_of_keys)\n",
"print(my_new_dictionary)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e131b7ca",
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"{'a': 23, 'b': 42, 'c': 12, 'd': 9, 'e': 5, 'f': 1}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"new_dct = {}\n",
"for key, value in zip(my_list_of_keys, my_list_of_values):\n",
" new_dct[key] = value\n",
"new_dct"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
132 changes: 110 additions & 22 deletions 002-basic-control-structures/Basic control structures II.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2281,6 +2281,115 @@
" return False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Decorators"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Decorators will be explored more as time permits, but to help you recognise them as we move through, a brief explanation: these are brief annotations directly above `def` (for functions and methods). They tell Python to wrap the function/method with a function-of-a-function. In another phrasing, a decorator takes a function and soups it up. This looks like:\n",
"\n",
"```python\n",
"@mydecorator\n",
"def afunction():\n",
" ...\n",
"``` \n",
"\n",
"In this case `mydecorator` will somehow modify the function's attributes or effects - examples are `@coroutine` for marking a function as async, `@staticmethod` for marking a class's method as static, and `@timer` for timing a function when it is called. You can even make your own - decorators are usually functions with a function as an argument, so you know all the syntax you need. It might look something like this:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def val2string(f):\n",
" def modified_f(val1, val2):\n",
" normal_result = f(val1, val2)\n",
" return str(normal_result)\n",
" return modified_f"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The above creates a new function called `modified_f` (note that function definitions can live anywhere, including in a function, just like any other variable definition). It is a two-argument function, which returns the stringified version of the original function, called with the same arguments. For instance:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'4'"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@val2string\n",
"def add(a, b):\n",
" return a + b\n",
"\n",
"@val2string\n",
"def mul(a, b):\n",
" return a * b\n",
"add(1, 3)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"['this', 'that']\""
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add(['this'], ['that'])"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'12'"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mul(6, 2)"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -2348,27 +2457,6 @@
"* comprehensions"
]
},
{
"cell_type": "markdown",
"metadata": {
"hideCode": false,
"hidePrompt": false,
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"We haven't talked about a number of structures, but a couple you may come across that we don't cover in this course, are asynchronous routines (`async`, `await`) that follow the coroutine paradigm; and decorators, which allow us to mark certain functions and methods to be wrapped by a function-of-a-function. This looks like:\n",
"\n",
"```python\n",
"@mydecorator\n",
"def afunction():\n",
" ...\n",
"``` \n",
"\n",
"In this case `mydecorator` will somehow modify the function's attributes or effects - examples are `@coroutine` for marking a function as async, `@staticmethod` for marking a class's method as static, and `@timer` for timing a function when it is called."
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -2803,7 +2891,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.7.11"
},
"livereveal": {
"theme": "beige",
Expand Down
Loading

0 comments on commit 99f4a5e

Please sign in to comment.