属性 在数据类中

Property in dataclass

描述


我正在尝试实现一个只包含几个参数的简单数据class。

@dataclass
class ReconstructionParameters:

    img_size: int
    CR: int
    denoise: bool
    epochs: int
    learning_rate: float
    step_size: int
    gamma: float
    batch_size: int
    regularization: float
    N0: float
    sig: float

    arch_name: InitVar[str]

    net_arch: int = field(init=False)


    def __post_init__(self, arch_name):
        self.net_arch = arch_name

    @property
    def net_arch(self):
        return str(netType(self.net_arch))

    @net_arch.setter
    def net_arch(self, arch_name):
        self.net_arch = int(netType[arch_name])
    

用户应该能够在 class 的初始化期间传递包含字符串 arch_name 的字符串,但是 class 应该保留以下 [=] 中定义的等效整数16=]:

class netType(IntEnum):
    
    c0mp = 0
    comp = 1
    pinv = 2
    free = 3

但是,如果用户想要获得包含在先前创建的 class 中的 net_arch,他们应该能够访问用于初始化的相同字符串而不是整数表示。我正在尝试使用 @property 装饰器和 setter。理想情况下,我想使用 __post_init__() 方法来初始化 net_arch,它使用其 setter.

错误


当运行下面的代码我得到一个错误:

a = ReconstructionParameters(
    img_size=64, 
    CR=1024,
    denoise=True,
    epochs=20, 
    learning_rate=1e-6,
    step_size=10,
    gamma=1e-7,
    batch_size=256,
    regularization=1e-6, 
    N0=2500,
    sig=0.5,
    arch_name='c0mp')
Traceback (most recent call last):

  File "C:\spas\Programs\Python\test.py", line 51, in <module>
    a = ReconstructionParameters(

  File "<string>", line 14, in __init__

  File "C:\spas\Programs\Python\test.py", line 48, in net_arch
    self.net_arch = int(netType[arch_name])

  File "C:\Users\user\Anaconda3\envs\singlepixelenv\lib\enum.py", line 349, in __getitem__
    return cls._member_map_[name]

KeyError: <property object at 0x000001CF7C26BA40>

数据类字段和属性不能同名。但是您可以在该字段中添加前导下划线,然后 属性 将起作用。

from dataclasses import InitVar, dataclass, field
from enum import IntEnum


@dataclass
class ReconstructionParameters:
    img_size: int
    CR: int
    denoise: bool
    epochs: int
    learning_rate: float
    step_size: int
    gamma: float
    batch_size: int
    regularization: float
    N0: float
    sig: float

    arch_name: InitVar[str]

    _net_arch: int = field(init=False)

    def __post_init__(self, arch_name):
        self.net_arch = arch_name

    @property
    def net_arch(self):
        return str(netType(self._net_arch))

    @net_arch.setter
    def net_arch(self, arch_name):
        self._net_arch = int(netType[arch_name])


class netType(IntEnum):
    c0mp = 0
    comp = 1
    pinv = 2
    free = 3


a = ReconstructionParameters(
    img_size=64,
    CR=1024,
    denoise=True,
    epochs=20,
    learning_rate=1e-6,
    step_size=10,
    gamma=1e-7,
    batch_size=256,
    regularization=1e-6,
    N0=2500,
    sig=0.5,
    arch_name='c0mp')

print(a)
# ReconstructionParameters(img_size=64, CR=1024, denoise=True, epochs=20, learning_rate=1e-06, step_size=10, gamma=1e-07, batch_size=256, regularization=1e-06, N0=2500, sig=0.5, _net_arch=0)
print(a.net_arch)
# netType.c0mp

我可能误解了什么,但这不是你想要的,不需要属性吗?

from dataclasses import dataclass, field


@dataclass
class ReconstructionParameters:

    img_size: int
    CR: int
    denoise: bool
    epochs: int
    learning_rate: float
    step_size: int
    gamma: float
    batch_size: int
    regularization: float
    N0: float
    sig: float
    arch_name: str

    net_arch: int = field(init=False)

    def __post_init__(self):
        self.net_arch = getattr(netType, arch_name)