|
@@ -0,0 +1,78 @@
|
|
|
+from dataclasses import dataclass, make_dataclass, is_dataclass, field
|
|
|
+from typing import List, Any
|
|
|
+
|
|
|
+@dataclass
|
|
|
+class Book:
|
|
|
+ title: str
|
|
|
+ author: str
|
|
|
+
|
|
|
+
|
|
|
+@dataclass
|
|
|
+class InventoryItem:
|
|
|
+ name: str
|
|
|
+ quantity: int = field(default=1)
|
|
|
+ price: float = field(default=9.99)
|
|
|
+
|
|
|
+@dataclass
|
|
|
+class ProgramStaff:
|
|
|
+ items: List[InventoryItem]
|
|
|
+ # Или после python 3.10
|
|
|
+ # items: list[InventoryItem]
|
|
|
+
|
|
|
+
|
|
|
+@dataclass
|
|
|
+class A:
|
|
|
+ name: Any
|
|
|
+ value: Any = 42
|
|
|
+
|
|
|
+
|
|
|
+# Создание класса через make_dataclass
|
|
|
+def test_1():
|
|
|
+ Person = make_dataclass('Person', ['firs_name', 'last_name', 'age'])
|
|
|
+ artem = Person('Artem', 'Egorov', 33)
|
|
|
+ print(artem)
|
|
|
+
|
|
|
+
|
|
|
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
+
|
|
|
+@dataclass
|
|
|
+class Student:
|
|
|
+ name: str
|
|
|
+ surname: str
|
|
|
+ student_id: int
|
|
|
+ faculty: str
|
|
|
+ specialty: str
|
|
|
+
|
|
|
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
+
|
|
|
+@dataclass
|
|
|
+class Point:
|
|
|
+ x: int
|
|
|
+ y: int
|
|
|
+
|
|
|
+def test_2():
|
|
|
+ point1 = Point(5, 7)
|
|
|
+ point2 = Point(-10, 12)
|
|
|
+ print(point1)
|
|
|
+ print(point2)
|
|
|
+
|
|
|
+ assert is_dataclass(Point), 'Point is not dataclass'
|
|
|
+ assert isinstance(point1, Point)
|
|
|
+ assert isinstance(point2, Point)
|
|
|
+
|
|
|
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def main():
|
|
|
+
|
|
|
+ # book = Book('asdf', 'asdfas')
|
|
|
+ # print(book)
|
|
|
+
|
|
|
+ # test_1()
|
|
|
+ test_2()
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ main()
|