-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataclass.py
More file actions
71 lines (56 loc) · 2.39 KB
/
dataclass.py
File metadata and controls
71 lines (56 loc) · 2.39 KB
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
# Why use the dataclasses?
# We can set default values for particular fields to ensure that each time we use a dataclass those fields are preset.
# Dataclasses also provide a default representation for print, log and other outputs.
# If we need to convert our dataclass to a dictionary or a tuple, dataclasses have functions that can perform that conversion for us.
# We can also make custom properties that do more than just store a value.
# It's also possible to make frozen instances of a dataclass that doesn't allow any edits to the properties after the dataclass has been created.
from dataclasses import dataclass # Import dataclass from the dataclasses module
@dataclass
class Amino_acid:
name: str
function: str = None
First_aa = Amino_acid('Cysteine', 'Help prevent side effects due to drug reactions and toxic chemicals')
print(First_aa.name)
# Cysteine
print(First_aa.function)
# Help prevent side effects due to drug reactions and toxic chemicals
from dataclasses import asdict, astuple
First_aa = Amino_acid('Cysteine', 'Help prevent side effects due to drug reactions and toxic chemicals')
First_aa_dict = asdict(First_aa)
print(First_aa_dict)
# {'name': 'Cysteine', 'function': 'Help prevent side effects due to drug reactions and toxic chemicals'}
First_aa_tuple = astuple(First_aa)
print(First_aa_tuple)
# ('Cysteine', 'Help prevent side effects due to drug reactions and toxic chemicals')
from dataclasses import dataclass
from decimal import Decimal
@dataclass
class Price_entry:
# Define the fields on the class
food: str
quantity: int
unit_price: Decimal
# Define a property that returns the total_price
@property
def Total_price(self):
return int(self.quantity) * self.unit_price
Grazed_donut = Price_entry('Grazed_donut', 3, Decimal('2.5'))
Grazed_donut.Total_price # @property decorator do not need us to add parentheses to call Total_price
# Decimal('7.5')
# Make a frozen class
from dataclasses import dataclass # Import dataclass from the dataclasses module
@dataclass
class Amino_acid:
name: str
molecular_weight: float
cys = Amino_acid('Cysteine', 121.16)
cys.molecular_weight = 130
cys.molecular_weight
# 130
@dataclass(frozen=True)
class Amino_acid:
name: str
molecular_weight: float
cys = Amino_acid('Cysteine', 121.16)
cys.molecular_weight = 130
# dataclasses.FrozenInstanceError: cannot assign to field 'molecular_weight'