Skip to content

Instantly share code, notes, and snippets.

@jtpio
Last active March 29, 2019 10:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtpio/d0a5fb727a0c934e311a6ffbe3c743d8 to your computer and use it in GitHub Desktop.
Save jtpio/d0a5fb727a0c934e311a6ffbe3c743d8 to your computer and use it in GitHub Desktop.
PyBerlin Meetup - Coding Slam - 2019-04-28

PyBerlin Meetup - Coding Slam

2019-04-28

Featuring 5 picks from the Python standard library.

Try it online

The presentation is available online with Binder:

Binder

Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](./skip.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 🐍 🐻 PyBerlin Meetup 🐻 🐍 - 2019-04-28\n",
"\n",
"## Coding Slam - 5 Picks from the Python Standard Library\n",
"\n",
"### Jeremy Tuloup - [github.com/jtpio](https://github.com/jtpio)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](./skip.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. `sys`\n",
"\n",
"### https://docs.python.org/3/library/sys.html"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sys.version"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sys.prefix"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sys.executable"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sys.path"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](./skip.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. `textwrap.dedent`\n",
"\n",
"### https://docs.python.org/3.7/library/textwrap.html#textwrap.dedent"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from textwrap import dedent"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def func():\n",
" multiline = \"\"\"\n",
" Lorem ipsum dolor sit amet.\n",
" Integer nec odio.\n",
" Praesent libero.\n",
" Sed cursus ante dapibus diam.\n",
" Sed nisi. \n",
" \"\"\"\n",
" return multiline\n",
"\n",
"res = func()\n",
"print(res)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"res"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def func():\n",
" multiline = dedent(\"\"\"\n",
" Lorem ipsum dolor sit amet.\n",
" Integer nec odio.\n",
" Praesent libero.\n",
" Sed cursus ante dapibus diam.\n",
" Sed nisi. \n",
" \"\"\")\n",
" return multiline\n",
"\n",
"res = func()\n",
"print(res)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"res"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"res.strip()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](./skip.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. `textwrap.wrap`\n",
"\n",
"### https://docs.python.org/3.7/library/textwrap.html#textwrap.wrap"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"long_line = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Diam maecenas sed enim ut sem viverra aliquet eget. Massa tempor nec feugiat nisl pretium. Eu tincidunt tortor aliquam nulla facilisi cras fermentum. Mauris rhoncus aenean vel elit scelerisque mauris pellentesque pulvinar pellentesque. Scelerisque varius morbi enim nunc. Facilisis sed odio morbi quis commodo odio. Aliquet sagittis id consectetur purus. Quis ipsum suspendisse ultrices gravida. Rhoncus aenean vel elit scelerisque mauris.\"\n",
"long_line"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from textwrap import wrap\n",
"wrap(long_line) # width=70"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"wrap(long_line, width=10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import interact, fixed\n",
"\n",
"interact(wrap, text=fixed(long_line), width=(70, 120));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](./skip.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. `io.StringIO`\n",
"\n",
"### https://docs.python.org/3/library/io.html#io.StringIO"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from io import StringIO"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"out = StringIO()\n",
"\n",
"out.write('First line\\n')\n",
"out.write('Second line\\n')\n",
"\n",
"print(out.getvalue())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"from contextlib import contextmanager\n",
"\n",
"@contextmanager\n",
"def capture_stdout(output):\n",
" stdout = sys.stdout\n",
" sys.stdout = output\n",
" try:\n",
" yield\n",
" finally:\n",
" sys.stdout = stdout\n",
"\n",
"out = StringIO()\n",
"with capture_stdout(out):\n",
" print('hello world')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"out.getvalue()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('hello world')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](./skip.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5. `keyword`\n",
"\n",
"\n",
"### https://docs.python.org/3.7/library/keyword.html"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import keyword"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"keyword.iskeyword('def')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"keyword.iskeyword('define')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"keyword.kwlist"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# longest keywords?\n",
"sorted(keyword.kwlist, key=len)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](./skip.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Thanks!\n",
"## [github.com/jtpio](https://github.com/jtpio)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](./skip.png)"
]
}
],
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
channels:
- conda-forge
dependencies:
- jupyterlab=0.35
- ipywidgets=7.4
jupyter labextension install @jupyter-widgets/jupyterlab-manager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment