Skip to content

Instantly share code, notes, and snippets.

@santoshpy
Created May 15, 2019 01:52
Show Gist options
  • Save santoshpy/a74e90774d537d2b8894769e203a4a0a to your computer and use it in GitHub Desktop.
Save santoshpy/a74e90774d537d2b8894769e203a4a0a to your computer and use it in GitHub Desktop.
things_you_are_probably_not_using_in_python_3_but_should/python 3 examples.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Things you're probably not using in Python 3 - but should\n",
"\n",
"Code examples from the [DataWhatNow](https://datawhatnow.com) blog on a [blog post abot Python 3](https://datawhatnow.com/things-you-are-probably-not-using-in-python-3-but-should).\n",
"\n",
"## All the code examples are executed in python 3.7"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.7.1 (default, Dec 14 2018, 19:28:38) \n",
"[GCC 7.3.0]\n"
]
}
],
"source": [
"import sys\n",
"\n",
"print(sys.version)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Interpolation f-strings"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"User Jane Doe has logged in and did an action buy.\n"
]
}
],
"source": [
"user = \"Jane Doe\"\n",
"action = \"buy\"\n",
"\n",
"log_message = 'User {} has logged in and did an action {}.'.format(user, action)\n",
"print(log_message)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"User Jane Doe has logged in and did an action buy.\n"
]
}
],
"source": [
"user = \"Jane Doe\"\n",
"action = \"buy\"\n",
"\n",
"log_message = f'User {user} has logged in and did an action {action}.'\n",
"print(log_message)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Path Library"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"post_sub_folder\n"
]
}
],
"source": [
"from pathlib import Path\n",
"\n",
"root = Path('post_sub_folder')\n",
"print(root)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/home/weenkus/Workspace/Projects/DataWhatNow-Codes/things_you_are_probably_not_using_in_python_3_but_should/post_sub_folder/happy_user\n"
]
}
],
"source": [
"path = root / 'happy_user'\n",
"\n",
"# Make the path absolute\n",
"print(path.resolve())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Type hinting"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def sentence_has_animal(sentence: str) -> bool:\n",
" return \"animal\" in sentence\n",
"\n",
"sentence_has_animal(\"Donald had a farm without animals\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Enumerations"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Monster.ZOMBIE\n"
]
}
],
"source": [
"from enum import Enum, auto\n",
"\n",
"class Monster(Enum):\n",
" ZOMBIE = auto()\n",
" WARRIOR = auto()\n",
" BEAR = auto()\n",
" \n",
"print(Monster.ZOMBIE)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Monster.ZOMBIE\n",
"Monster.WARRIOR\n",
"Monster.BEAR\n"
]
}
],
"source": [
"for monster in Monster:\n",
" print(monster)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Built-in LRU cache"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"\n",
"def fib(number: int) -> int:\n",
" if number == 0: return 0\n",
" if number == 1: return 1\n",
" \n",
" return fib(number-1) + fib(number-2)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Duration: 29.93897843360901s\n"
]
}
],
"source": [
"start = time.time()\n",
"fib(40)\n",
"print(f'Duration: {time.time() - start}s')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"from functools import lru_cache\n",
"\n",
"@lru_cache(maxsize=512)\n",
"def fib_memoization(number: int) -> int:\n",
" if number == 0: return 0\n",
" if number == 1: return 1\n",
" \n",
" return fib_memoization(number-1) + fib_memoization(number-2)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Duration: 6.580352783203125e-05s\n"
]
}
],
"source": [
"start = time.time()\n",
"fib_memoization(40)\n",
"print(f'Duration: {time.time() - start}s')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unpacking"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 [1, 2, 3] 4\n"
]
}
],
"source": [
"head, *body, tail = range(5)\n",
"print(head, body, tail)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"python3.7\n",
"script.py\n",
"['-n', '5', '-l', '15']\n"
]
}
],
"source": [
"py, filename, *cmds = \"python3.7 script.py -n 5 -l 15\".split()\n",
"print(py)\n",
"print(filename)\n",
"print(cmds)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 2\n"
]
}
],
"source": [
"first, _, third, *_ = range(10)\n",
"\n",
"print(first, third)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Data classes"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10.4"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class Armor:\n",
" \n",
" def __init__(self, armor: float, description: str, level: int = 1):\n",
" self.armor = armor\n",
" self.level = level\n",
" self.description = description\n",
" \n",
" def power(self) -> float:\n",
" return self.armor * self.level\n",
" \n",
"armor = Armor(5.2, \"Common armor.\", 2)\n",
"armor.power()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<__main__.Armor object at 0x7f09d83f8ac8>\n"
]
}
],
"source": [
"print(armor)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10.4"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from dataclasses import dataclass\n",
"\n",
"@dataclass\n",
"class Armor:\n",
" armor: float\n",
" description: str\n",
" level: int = 1\n",
" \n",
"\n",
" def power(self) -> float:\n",
" return self.armor * self.level\n",
" \n",
"armor = Armor(5.2, \"Common armor.\", 2)\n",
"armor.power()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Armor(armor=5.2, description='Common armor.', level=2)\n"
]
}
],
"source": [
"print(armor)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment