File: autonamedtuple_demo.py

package info (click to toggle)
sphinx-toolbox 3.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,924 kB
  • sloc: python: 11,636; sh: 26; javascript: 25; makefile: 16
file content (97 lines) | stat: -rw-r--r-- 1,474 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Examples from
# https://docs.python.org/3/library/typing.html#typing.NamedTuple
# https://www.python.org/dev/peps/pep-0589/#totality
# https://github.com/python/typing/pull/700

# stdlib
import collections
from typing import NamedTuple

__all__ = ("Animal", "Employee", "Movie")


class Animal(NamedTuple):
	"""
	An animal.

	:param name: The name of the animal.
	:param voice: The animal's voice.
	"""

	name: str
	voice: str


class Employee(NamedTuple):
	"""
	Represents an employee.

	:param id: The employee's ID number
	"""

	#: The employee's name
	name: str

	id: int = 3

	def __repr__(self) -> str:
		return f'<Employee {self.name}, id={self.id}>'

	def is_executive(self) -> bool:
		"""
		Returns whether the employee is an executive.

		Executives have ID numbers < 10.
		"""


class Movie(NamedTuple):
	"""
	Represents a movie.
	"""

	#: The name of the movie.
	name: str

	#: The movie's release year.
	year: int

	based_on: str


class Foo(NamedTuple):
	"""
	A Namedtuple

	:param a: An integer
	:param str b: A string
	:param str c:
	"""

	#: An integer (another doc)
	a: int

	b: str

	#: C's doc
	c: str


class NoDocstring(NamedTuple):
	#: An integer
	a: int

	b: str

	#: C's doc
	c: str


Traditional = collections.namedtuple("Traditional", "a, b, c")
Traditional.__doc__ = "A traditional Namedtuple"


class CustomisedNew(collections.namedtuple("CustomisedNew", "a, b, c")):

	def __new__(cls, values: str):
		return super().__new__(*values.split())