python-dataclasses Questions

3

Is it possible to specify that a single field is frozen in a non-frozen dataclass? Something like this: @dataclass class Data: fixed: int = field(frozen=True) mutable: int = field(frozen=False) ...
Dionne asked 9/9, 2020 at 2:7

2

Solved

I wrote a Flask-SQLAlchemy model class that looks like this (from this reference): from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) db = SQLAlchemy(app) class...
Treharne asked 26/5, 2021 at 20:12

5

Solved

I want to populate an attribute of a dataclass using the default_factory method. However, since the factory method is only meaningful in the context of this specific class, I want to keep it inside...
Castra asked 23/7, 2020 at 9:38

2

Solved

I am using Python 3.6 and the dataclasses backport package from ericvsmith. It seems that calling dataclasses.asdict(my_dataclass) is ~10x slower than calling my_dataclass.__dict__: In [172]: @da...
Numerable asked 7/9, 2018 at 20:53

2

Today I installed python 3.7 from apt-get to try out the new dataclasses module. I installed it seperately because python3.6 wasn't upgradeable to 3.7. When I type: python3.7 --version, it gives m...

3

How can I check if a class is a dataclass in Python? I found out that I can check the existance of __dataclass_fields__ and __dataclass_params__ attributes, but I'd love to find a more elegant way...
Demulsify asked 13/5, 2019 at 5:17

2

Solved

I have a dataclass, which looks like this: @dataclass class myClass: id: str mode: str value: float This results in: dataclasses.asdict(myClass) {"id": id, "mode": mode, &qu...
Shindig asked 19/10, 2020 at 6:47

3

Solved

How can I update the fields of a dataclass using a dict? Example: @dataclass class Sample: field1: str field2: str field3: str field4: str sample = Sample('field1_value1', 'field2_value1', '...
Ebonyeboracum asked 25/4, 2020 at 13:16

17

Solved

What's the proper way to declare custom exception classes in modern Python? My primary goal is to follow whatever standard other exception classes have, so that (for instance) any extra string I in...
Thaothapa asked 23/8, 2009 at 21:29

7

Solved

I'd like to create a config dataclass in order to simplify whitelisting of and access to specific environment variables (typing os.environ['VAR_NAME'] is tedious relative to config.VAR_NAME). I the...
Salvatore asked 13/2, 2019 at 19:51

6

Solved

I'm in the process of converting existing dataclasses in my project to pydantic-dataclasses, I'm using these dataclasses to represent models I need to both encode-to and parse-from json. Here's an ...
Bookkeeping asked 20/5, 2021 at 13:13

1

Solved

Is it possible to benefit from dataclasses.field, especially for default values, but using a custom constuctor? I know the @dataclass annotation sets default values in the generated __init__, and w...
Castellated asked 24/2, 2022 at 10:19

1

I have a dataclass let's say: from dataclasses import dataclass @dataclass class Foo: bar: int baz: int I have a function that is called from an API receiving json and loading it as a Dict: def...
Bemoan asked 12/10, 2022 at 12:50

2

I have a class like this: from __future__ import annotations import os from dataclasses import dataclass @dataclass(frozen=True) class Config: name: str age: str @staticmethod def init() -&...
Wallis asked 11/5, 2021 at 19:53

17

Solved

I'm currently trying my hands on the new dataclass constructions introduced in Python 3.7. I am currently stuck on trying to do some inheritance of a parent class. It looks like the order of the ar...
Merrygoround asked 28/7, 2018 at 23:8

1

I have a code like below with multiple functions inside for various calculations. I am using python fire to pass arguments instead of defining argparse and call the functions from cli. Every time I...

9

Solved

I have the following very simple dataclass: import dataclasses @dataclasses.dataclass class Test: value: int I create an instance of the class but instead of an integer I use a string: >&g...
Brambling asked 25/2, 2019 at 9:52

4

Below DataFrame of pandas is not validated by pydantic. How to handle this? from pydantic.dataclasses import dataclass @dataclass class DataFrames: dataframe1: pd.DataFrame = None dataframe2: pd...

6

I have two dataclasses, Route and Factors. Route contains a value and three copies of Factors. Route does not know how many variables Factors contains. I want to get the name of these variables, an...
Baryta asked 16/3, 2020 at 21:54

3

Solved

I am trying to write a function to log dataclasses I would like to get the name of all fields in the dataclass and print the value to each (similar to how you might write a function to print a dict...
Gmt asked 28/7, 2022 at 9:39

4

Solved

I have a dataclass whose instances I want to hash and order, using the id member as a key. from dataclasses import dataclass, field @dataclass(eq=True, order=True) class Category: id: str = field...
Enrich asked 18/9, 2018 at 16:3

2

Solved

If I create a Python dataclass containing a Numpy ndarray, I can no longer use the automatically generated __eq__ anymore. import numpy as np @dataclass class Instr: foo: np.ndarray bar: np.nda...
Sherrie asked 8/8, 2018 at 9:58

13

Solved

Starting with Python 3.7, there is something called a dataclass: from dataclasses import dataclass @dataclass class Foo: x: str However, the following fails: >>> import json >>...
Ounce asked 11/7, 2018 at 13:29

4

Solved

I'm trying to port our namedtuple classes into dataclass in Python 3.6 using the backport package. However, I noticed when mocking dataclass classes, you cannot use the "spec" keyword anymore. I as...
Mollymollycoddle asked 1/8, 2018 at 18:57

2

I want to initialize python dataclass object even if no instance variables are passed into it and we have not added default values to the param @dataclass class TestClass: paramA: str paramB: f...
Melgar asked 6/3, 2022 at 8:44

© 2022 - 2024 — McMap. All rights reserved.