[英]Can you modify an object's field every time another field is modified?
我有一個看起來像這樣的數據類from dataclasses import dataclass, field @dataclass class Data: name: str | None = None file_friendly_name: str | None = field ...
[英]Can you modify an object's field every time another field is modified?
我有一個看起來像這樣的數據類from dataclasses import dataclass, field @dataclass class Data: name: str | None = None file_friendly_name: str | None = field ...
[英]Python SQLAlchemy 2.0 non required field types using dataclass_transform
我剛剛在一個新項目上安裝了 SQLAlchemy 2.0,我正試圖讓我的模型盡可能的類型安全。 通過使用@typing_extensions.dataclass_transform ,我已經能夠在類型檢查方面實現我想要實現的大部分目標,但是所有字段目前都被標記為不需要。 例如: 在上面的示例中,如何 ...
[英]What type of 'field' for a time value
如果提供的值采用時間格式,您會在數據類中使用哪種字段類型? 我正在解析的 JSON 文件返回如下值: 2.00:00:15.0830000 像下面這樣一個class,應該設置什么類型呢? date類型似乎不在列表中。 我在這里需要非str的原因是計算該值是否早於 x 天/小時然后執行某些操作。 ...
[英]dataclasses.asdict doesn't work on Python 3.11?
我嘗試了以下代碼:from dataclasses import asdict, dataclass @dataclass class DC: a: int b: int c = DC(a=10, b=5) dc = asdict(c) print(dc) 在 Python ...
[英]Create class and prep data once that all objects can then use
請原諒我的術語,我還在學習 Python 和 OOP。 我有 2 個 py 文件: 數據.py 主程序在 main.py 中,我想使用類似以下內容創建 n 個對象: 以上適用於美元,但也可能適用於 n 種其他貨幣和不同的利率曲線——可能有 15-20 個單獨的對象。 目標是使用(例如)返回一個插值: ...
[英]Calling a dataclass constructor with decorator given only a type object
我有一個數據類,它繼承了一個抽象的 class,它實現了一些樣板,並且還使用@validate_arguments裝飾器在創建 object 時立即將字符串轉換回數字。 數據類是一系列數字,其中一些是在__post_init__中計算的。 report.py : 然后我有另一個 class,它用R ...
[英]Category and subcategory classes. Need code example
我有像[{id, category, parent_id},]這樣的字典類型數據集。 我需要創建 class 來解析這個數據集並創建簡單的界面來獲取父母和孩子。 我不想發明輪子,有人可以分享可以幫助我實現此任務的代碼嗎? 我試圖創建數據類,然后創建類來實現與它們一起工作的方法,但我厭倦了思考一個有能 ...
[英]Python: initiated Logger with dataclass field param
這是我的Logger class: 這是我的class接受用戶argument ,我希望我的log文件在file name中使用我的用戶名創建: 所以目前我在Logger class 中的用戶field的類型是dataclasses.Field而不是string 。 我也嘗試使用default_fa ...
[英]Python typing for class that having methods from decorator
dataclasses-json允許這樣的代碼:from dataclasses import dataclass from dataclasses_json import dataclass_json @dataclass_json @dataclass class Person: n ...
[英]function from_dict() failing for unknown reason in python
我使用https://json2csharp.com/code-converters/json-to-python將以下 JSON 轉換為數據類: 它在數據類下方生成 - 由於某種原因,它顯示錯誤 at.from_dict() 方法,我無法弄清楚。 請指教 錯誤: ...
[英]A constructor for dataclasses with randomized attribute values
嗨,有人可以解釋一下這里發生的事情嗎:我想用隨機值實例化對象。 印刷: (123, 586) (123, 586) (123, 586) 預期行為將是具有不同值的三個元組。 這里發生了什么事?? (當我使用普通的 class 時,它按預期工作) ...
[英]How do I implement polymorphic dataclass models with default values in the base model in SQLAlchemy 2.0 / Declarative?
我正在遷移到 SQLAlchemy 2.0 並采用帶有MappedAsDataclass的新聲明語法。 以前,我已經為我的模型實現了連接表 inheritance 。 (簡化的)代碼如下所示:from sqlalchemy import ForeignKey, String from sqlal ...
[英]How to specify the type of a callable python dataclass member to accept subclass instances
我希望能夠指定一個可調用的數據 class 成員,並采用派生 class 的實例。如果我使用抽象成員函數,這將很容易。 我嘗試過如下代碼,但不知道為 Callable 類型規范提供什么參數。 我正在尋找一種方法來注釋handler屬性,以便派生類可以將自己的實例作為handler (在本例中為Spe ...
[英]Python3 kwarg starting with dunder not possible in classes & private attributes generated by dataclasses
我對以下行為感到有些困惑(在 Python 3.9 中):class Dunder: def __init__(self, __a_kwarg="hello"): self.__a_kwarg = __a_kwarg print(self.__a_kwarg ...
[英]Python dataclasses: is it safe to use an id-based hash instead of `unsafe_hash=True`?
我有一個代表二維點的數據類: 我希望Point class 具有以下行為,以便可以根據值比較不同的點對象,但也可以單獨存儲在字典中: 我可以使用unsafe_hash=True ,例如 但是當點存儲在字典中時,這會導致問題。 例如 有什么理由我不應該實施__hash__方法來根據 object 的 ...
[英]Converting a dataclass with a field of the dictionary type, where the other dataclass is the keof dicty
如何將數據類轉換為 python 字典? 在這種情況下,數據class的其中一個字段包含一個字典,其鍵是另一個數據class。似乎有必要以某種方式將嵌套數據類轉換為元組@dataclass(frozen=True) class Two: a:str = 'World' @dataclass ...
[英]How can I restrict this dataclass serialization to only attributes on the base class?
這是一些代碼:from dataclasses import dataclass from dataclasses_json import dataclass_json @dataclass_json @dataclass class Foo: f: str @dataclass_js ...
[英]Allow multiple data types in a dataclass variable
有沒有辦法允許數據類變量具有多種數據類型之一? (它也應該適用於 Python 3.7) 我希望 index_range 具有默認值字符串"all" ,但也可以將元組作為值。 (稍后在函數中我定義"all"意味着使用所有索引,但如果給出一個元組,則只使用這些數字之間的元素。) Pylance ...
[英]How to best convert json or dict to object and back?
假設我有一些數據(可能是json或dict ),它們以某種相對復雜的方式任意構造,例如下面示例代碼中的example字典。from dataclasses import asdict, dataclass, field from typing import Any, List @datacla ...
[英]A pythonic way to init inherited dataclass from an object of parent type
給定一個數據類結構: 將種子對象字段淺復制到新的子對象中的正確方法是什么? 我什至不介意將種子轉換為子類型,因為初始化后父對象沒有用處。 我為什么要那樣做? 我想避免每次 RichParent 發生更改時更改 Child 類,只要父級保持普通數據類即可。 ...