python-dataclasses

Dictionary to dataclasses with inheritance of classes

萝らか妹 提交于 2020-05-17 06:06:21
问题 I have the following class @dataclass_json @dataclass class Input: sources: List[Sources] =None Transformations: List[str] =None As well as: @dataclass_json @dataclass class Source: type: str =None label: str =None path: str = None and the two subclasses: @dataclass_json @dataclass class Csv(Source): csv_path: str=None delimiter: str=';' and @dataclass_json @dataclass class Parquet(Source): parquet_path: str=None Given now the dictionary: parquet={type: 'Parquet', label: 'events', path: '/...

Dataclass-style object with mutable and immutable properties?

可紊 提交于 2020-05-16 07:47:25
问题 I have been playing around with dataclasses dynamically loaded with property names from a file and I am unable to find a way to create both 'frozen' and 'non-frozen' properties. I believe dataclasses only allow you to set all properites to frozen or non-frozen. As of now, I create a frozen dataclass and add a mutable class as one of the properties which I can change as I go but I am not very happy with the readability of this approach. Is there another pythonic dataclass people would

Dataclass-style object with mutable and immutable properties?

倾然丶 夕夏残阳落幕 提交于 2020-05-16 07:47:05
问题 I have been playing around with dataclasses dynamically loaded with property names from a file and I am unable to find a way to create both 'frozen' and 'non-frozen' properties. I believe dataclasses only allow you to set all properites to frozen or non-frozen. As of now, I create a frozen dataclass and add a mutable class as one of the properties which I can change as I go but I am not very happy with the readability of this approach. Is there another pythonic dataclass people would

How to make “keyword-only” fields with dataclasses?

允我心安 提交于 2020-05-12 11:10:01
问题 Since 3.0 there is support to make an argument keyword only: class S3Obj: def __init__(self, bucket, key, *, storage_class='Standard'): self.bucket = bucket self.key = key self.storage_class = storage_class How to get that kind of signature using dataclasses? Something like this, but preferably without the SyntaxError : @dataclass class S3Obj: bucket: str key: str * storage_class: str = 'Standard' Ideally declarative, but using the __post_init__ hook and/or a replacement class decorator is

How to make “keyword-only” fields with dataclasses?

烈酒焚心 提交于 2020-05-12 11:09:11
问题 Since 3.0 there is support to make an argument keyword only: class S3Obj: def __init__(self, bucket, key, *, storage_class='Standard'): self.bucket = bucket self.key = key self.storage_class = storage_class How to get that kind of signature using dataclasses? Something like this, but preferably without the SyntaxError : @dataclass class S3Obj: bucket: str key: str * storage_class: str = 'Standard' Ideally declarative, but using the __post_init__ hook and/or a replacement class decorator is

dict attribute 'type' to select Subclass of dataclass

霸气de小男生 提交于 2020-04-30 06:32:09
问题 I have the following class @dataclass_json @dataclass class Source: type: str =None label: str =None path: str = None and the two subclasses: @dataclass_json @dataclass class Csv(Source): csv_path: str=None delimiter: str=';' and @dataclass_json @dataclass class Parquet(Source): parquet_path: str=None Given now the dictionary: parquet={type: 'Parquet', label: 'events', path: '/.../test.parquet', parquet_path: '../../result.parquet'} csv={type: 'Csv', label: 'events', path: '/.../test.csv',

dict attribute 'type' to select Subclass of dataclass

浪子不回头ぞ 提交于 2020-04-30 06:32:05
问题 I have the following class @dataclass_json @dataclass class Source: type: str =None label: str =None path: str = None and the two subclasses: @dataclass_json @dataclass class Csv(Source): csv_path: str=None delimiter: str=';' and @dataclass_json @dataclass class Parquet(Source): parquet_path: str=None Given now the dictionary: parquet={type: 'Parquet', label: 'events', path: '/.../test.parquet', parquet_path: '../../result.parquet'} csv={type: 'Csv', label: 'events', path: '/.../test.csv',

Make the Python json encoder support Python's new dataclasses

烂漫一生 提交于 2020-04-08 08:39:23
问题 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 >>> foo = Foo(x="bar") >>> json.dumps(foo) TypeError: Object of type Foo is not JSON serializable How can I make json.dumps() encode instances of Foo into json objects? 回答1: Much like you can add support to the JSON encoder for datetime objects or Decimals, you can also provide a custom encoder subclass to serialize

Make the Python json encoder support Python's new dataclasses

蓝咒 提交于 2020-04-08 08:37:31
问题 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 >>> foo = Foo(x="bar") >>> json.dumps(foo) TypeError: Object of type Foo is not JSON serializable How can I make json.dumps() encode instances of Foo into json objects? 回答1: Much like you can add support to the JSON encoder for datetime objects or Decimals, you can also provide a custom encoder subclass to serialize

Make the Python json encoder support Python's new dataclasses

狂风中的少年 提交于 2020-04-08 08:37:10
问题 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 >>> foo = Foo(x="bar") >>> json.dumps(foo) TypeError: Object of type Foo is not JSON serializable How can I make json.dumps() encode instances of Foo into json objects? 回答1: Much like you can add support to the JSON encoder for datetime objects or Decimals, you can also provide a custom encoder subclass to serialize