# Python
Python is a programming language.
# Snippets
# *args
& **kwargs
?
>>> def hello(*args, **kwargs):
... gretting = kwargs.pop('gretting', 'Hello')
...
... print(f"""{gretting} {' '.join(args)}!""")
...
>>>
>>> hello("Laura", "Dang", gretting="Hi")
Hi Laura Dang!
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
- How To Use *args and **kwargs in Python 3 (opens new window) by Lisa Tagliaferri, 20 November 2017.
# Bare asterisk (*
) in function argument
In Python 3 you can specify *
in the argument list:
Parameters after "*" or "*identifier" are keyword-only parameters and may only be passed used keyword arguments.
>>> def is_birthday(*, birthday):
... today = datetime.date.today()
... if birthday.month == today.month and birthday.day == today.day:
... print(f"Yes it's their birthday.")
... else:
... print(f"No it's not their birthday.")
...
>>>
>>> is_birthday(datetime.date(1986, 9, 19))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: is_birthday() takes exactly 1 positional argument (1 given)
>>>
>>> is_birthday(birthday=datetime.date(1986, 9, 19))
No it's not their birthday.
>>>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- Reference: Forced naming of parameters in Python (opens new window) by Eli Bendersky, 12 January 2013.
# Coerce to NamedTuple
from typing import Any, Dict, NamedTuple, Type
class AlbumTuple(NamedTuple):
"""Album Tuple Class."""
name: str
artist: str
length: float
def coerce_to_namedtuple(d: Dict[str, Any], T: Type):
"""Create a NamedTuple from a dict, ignoring extra keys in the dict"""
return T(**{k: v for k, v in d.items() if k in T._fields})
album = dict(name="Liar", artist="Fake Shark - Real Zombie!", length=47.15)
print(coerce_to_namedtuple(d, AlbumTuple))
# output: AlbumTuple(name="Liar", artist="Fake Shark - Real Zombie!", length=47.15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Libraries
- Camelot (opens new window) - PDF Table Extraction for Humans - :megaphone: (opens new window) 🐙 (opens new window) 🐍 (opens new window)
- cleanco (opens new window) - Cleans companies names by stripping away terms indicating organization type - 🐍 (opens new window)
- Datafiles (opens new window) - a file-based ORM for dataclasses - 🐙 (opens new window) 🐍 (opens new window)
- dataset (opens new window) - Databases for lazy people. - 🐙 (opens new window) 🐍 (opens new window)
- lab_getting_started.py (opens new window) - Lets you organize TensorFlow machine learning projects 🐍 (opens new window)
- py-spy (opens new window) - sampling profiler for Python programs
- tartiflette (opens new window) - Tartiflette is a GraphQL Engine, built on top of Python 3.6 and above. Focused on building GraphQL APIs using the awesome Schema Definition Language. - 🐙 (opens new window) 🐍 (opens new window)
- yaps (opens new window) - a surface language for programming Stan models using python syntax - 🐙 (opens new window)
- Typer (opens new window) - Typer is library to build CLI applications that users will love using and developers will love creating. - 🐙 (opens new window)
# Data Science
- datacompy (opens new window) - Pandas and Spark DataFrame comparison for humans - 🐙 (opens new window) 🐍 (opens new window)
- intake (opens new window) - A plugin system for loading your data and making data catalogs - 🐙 (opens new window) 🐍 (opens new window)
- locopy (opens new window) - Loading/Unloading to Amazon Redshift - 🐙 (opens new window) 🐍 (opens new window)
- Luigi (opens new window) - a Python module that helps you build complex pipelines of batch jobs - 🐙 (opens new window) 🐍 (opens new window)
- Vaex (opens new window) - visualize and explore big tabular data at a billion rows per second - 📑 (opens new window) 🐙 (opens new window) 🐍 (opens new window) :megaphone: (opens new window)
# Templates
- Python Packages Project Generator (opens new window) - cookiecutter template for the most state-of-the-art libraries and best development practices for Python.
# Links
- Detecting SQL injections in Python code using AST (opens new window) by Artem Golubin, 29 April 2019
- Dive into Machine Learning (opens new window)
- Einstein Summation in Numpy (opens new window) by Olexa Bilaniuk, 4 February 2016
- My Python Development Environment (opens new window) by Jacob Kaplan-Moss, 21 February 2018.
- Named Entity Recognition with NLTK and SpaCy (opens new window) by Susan Li, 17 August 2018.
- Python at Netflix (opens new window), 29 April 2019
- Text Preprocessing in Python: Steps, Tools, and Examples (opens new window) by Olga Davydova, 15 October 2018
- Working efficiently with JupyterLab Notebooks (opens new window) by Florian Wilhelm, 8 November 2018.
- wtfpython (opens new window) - A collection of surprising Python snippets and lesser-known features.
- Understanding the asterisk(*) of Python (opens new window) by mingrammer, 20 March 2017.