python-dataclasses

create dataclass with optional attribute

create dataclass with optional attribute Question: I’m trying create dataclass with optional attribute is_complete: from dataclasses import dataclass from typing import Optional @dataclass(frozen=True) class MyHistoricCandle: open: float high: float low: float close: float volume: int time: datetime is_complete: Optional[bool] But when i init MyHistoricCandle object without is_complete attribute: MyHistoricCandle(open=1, high=1, low=1, close=1, volume=1, time=datetime.now()) Getting …

Total answers: 1

Python typing for class that having methods from decorator

Python typing for class that having methods from decorator Question: The dataclasses-json allows code such as: from dataclasses import dataclass from dataclasses_json import dataclass_json @dataclass_json @dataclass class Person: name: str lidatong = Person(‘lidatong’) # Encoding to JSON lidatong.to_json() # ‘{"name": "lidatong"}’ # Decoding from JSON Person.from_json(‘{"name": "lidatong"}’) # Person(name=’lidatong’) How could I use typing to …

Total answers: 1

function from_dict() failing for unknown reason in python

function from_dict() failing for unknown reason in python Question: I converted below JSON using https://json2csharp.com/code-converters/json-to-python to a dataclass: { "bypassCd": [ "Duis sint ipsum in", "consequat" ] } It generaged below dataclass – for some reason, it is showing error at .from_dict() method and I’m unable to figure it out. Please advise from typing import …

Total answers: 2

dataclass constructor – same values for instance variables

A constructor for dataclasses with randomized attribute values Question: Hi could someone explain whats happening here: i want to instantiate objects with random values. @dataclass class Particle: pos = (random.randint(0, 800), random.randint(0, 800)) for _ in range(3): p = Particle() print(p.pos) prints: (123, 586) (123, 586) (123, 586) expected behaviour wold be three tuples with …

Total answers: 1

How can I restrict this dataclass serialization to only attributes on the base class?

How can I restrict this dataclass serialization to only attributes on the base class? Question: Here’s some code: from dataclasses import dataclass from dataclasses_json import dataclass_json @dataclass_json @dataclass class Foo: f: str @dataclass_json @dataclass class Baz(Foo): b: str def full(self): return self.to_dict() # as expected, returns {"f":"f", "b":"b"} def partial(self): return Foo.to_dict(self) # also returns …

Total answers: 1

How to update a property for all dataclass objects in a list?

How to update a property for all dataclass objects in a list? Question: I have a list of objects of the following type: @dataclass class Feature: name: str active: bool and my list is: features = [Feature("name1",False), Feature("name2",False), Feature("name3",True)] I want to get back a list with all the features but switch their active property …

Total answers: 6

What other types of classes are there in Python?

What other types of classes are there in Python? Question: I just found out about dataclasses and was reading some tutorials on how to use them. One tutorial described data classes as: A data class is a class typically containing mainly data, although there aren’t really any restrictions. What other types of classes are there …

Total answers: 2

Python dataclass: Forcing a dictionary field to be a deep copy

Python dataclass: Forcing a dictionary field to be a deep copy Question: I am working with a dataclass that contains a dict. I want the dict to be a deepcopy, without having to rely on a post_init call, which would basically void to interest of a dataclass What would be a nice solution ? from …

Total answers: 1

How to handle typing of member variable that is initialized during __post_init__() of dataclass

How to handle typing of member variable that is initialized during __post_init__() of dataclass Question: The variable below is initialized as none, but during __post_init__ it is replaced with an instance of outlook client. @dataclass class Config: """Outlook configuration""" mailbox: str inbox: str mailbox_obj: Union["Mailbox", None] = None However, static type analysis correctly informs that …

Total answers: 1