Skip to content

Instantly share code, notes, and snippets.

@brodul
Last active July 15, 2021 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brodul/82b179e473538d31e28ae5885ad754eb to your computer and use it in GitHub Desktop.
Save brodul/82b179e473538d31e28ae5885ad754eb to your computer and use it in GitHub Desktop.
closures presentation
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>@brodul</title>
<link rel="stylesheet" href="dist/reset.css">
<link rel="stylesheet" href="dist/reveal.css">
<link rel="stylesheet" href="dist/theme/black.css">
<!-- Theme used for syntax highlighted code -->
<link rel="stylesheet" href="plugin/highlight/monokai.css">
</head>
<body>
<div class="reveal">
<div class="slides">
<section data-markdown>
<textarea data-template>
# Practical Closures
Andraž Brodnik @brodul
</textarea>
</section>
<section data-auto-animate>
<h2 data-id="code-title">Scope 101</h2>
<pre data-id="code-animation"><code class="hlpy" data-trim data-line-numbers="|3,4|1|6,7"><script type="text/template">
a = 2 # outer scope
def sum2(b):
return a + b # local scope
sum2(3)
# returns: 5
</script></code></pre>
</section>
<section data-markdown>
<textarea data-template>
# Closures
A Closure is a function object that remembers values in enclosing scopes even if they are not present in memory.
</textarea>
</section>
<section data-auto-animate>
<h2 data-id="code-title">What are closures?</h2>
<pre data-id="code-animation"><code class="hlpy" data-trim data-line-numbers="|1,11|5-9|1,3,6,8|"><script type="text/template">
def get_max_value_validator(max_value):
# max_value is outer/enclosing scope
def inner_fun(value):
if value > max_value:
raise ValueError(
f"{value} is bigger than {max_value}")
return value
return inner_fun
</script></code></pre>
</section>
<section data-auto-animate>
<h2 data-id="code-title">What are closures?</h2>
<pre data-id="code-animation"><code class="hlpy" data-trim data-line-numbers="|1|"><script type="text/template">
shoe_size_validator = get_max_value_validator(42)
shoe_size_validator(40)
# returns: 40
shoe_size_validator(55)
# raises: ValueError: 55 is bigger than 42
</script></code></pre>
</section>
<section data-auto-animate>
<h2 data-id="code-title">Special case with "partial"</h2>
<pre data-id="code-animation"><code class="hlpy" data-trim data-line-numbers="|3-7|"><script type="text/template">
from functools import partial
def max_value_validator(value, max_value):
if value > max_value:
raise ValueError(
f"{value} is bigger than {max_value}")
return value
shoe_size_validator = partial(
max_value_validator, max_value=42)
</script></code></pre>
</section>
<section data-auto-animate>
<h4 data-id="code-title">Calling an external command</h4>
<pre data-id="code-animation"><code class="hlpy" data-trim data-line-numbers="|1-3|5-11"><script type="text/template">
LOG = logging.getLogger()
tempdir_ = tempfile.mkdtemp()
env = {"PATH": os.getenv("PATH")}
LOG.info("Running docker image ls")
result = subprocess.run(["docker", "image", "ls"],
capture_output=True, env=env, cwd=tempdir_)
LOG.info("'docker image ls' exited with %s",
result.returncode)
</script></code></pre>
</section>
<section data-auto-animate>
<!-- <h4 data-id="code-title">Problem</h4> -->
<pre data-id="code-animation"><code class="hlpy" data-trim data-line-numbers="|1,13|6-11|1|3-4|3-4,8-9"><script type="text/template">
def get_docker_command(env={}):
env = {"PATH": os.getenv("PATH")}.update(env)
tempdir_ = tempfile.mkdtemp()
def inner_fun(*args):
LOG.info("Running docker command ...")
result = subprocess.run(["docker"] + list(args),
capture_output=True, env=env, cwd=tempdir_)
LOG.info("Command output ... %s", result)
return result
return inner_fun
</script></code></pre>
</section>
<section data-auto-animate>
<!-- <h4 data-id="code-title">Problem</h4> -->
<pre data-id="code-animation"><code class="hlpy" data-trim data-line-numbers=""><script type="text/template">
docker_cmd = get_docker_command(env={"SOME_ENV": "biz"})
result = docker_cmd("image", "ls")
</script></code></pre>
</section>
<section data-markdown>
<textarea data-template>
# Practical Closures
Andraž Brodnik @brodul
</textarea>
</section>
</div>
</div>
<script src="dist/reveal.js"></script>
<script src="plugin/notes/notes.js"></script>
<script src="plugin/markdown/markdown.js"></script>
<script src="plugin/highlight/highlight.js"></script>
<script>
// More info about initialization & config:
// - https://revealjs.com/initialization/
// - https://revealjs.com/config/
Reveal.initialize({
hash: true,
// Learn about plugins: https://revealjs.com/plugins/
plugins: [RevealMarkdown, RevealHighlight, RevealNotes]
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment