python-attrs

How to deserialise enumeration with string representation?

落爺英雄遲暮 提交于 2021-02-04 07:31:30
问题 I would like to create a class, which has an enumeration as an attribute. This enumeration should have a string representation that shows up as human-readable value when dumping the instance of the class that uses the enum attribute as a JSON string. In the minimal working example below, I created three enumerations in three different ways. After the deserialization, each attribute shows us that it comes from an enumeration except the enumeration with a string representation. It is just a

How to deserialise enumeration with string representation?

我的未来我决定 提交于 2021-02-04 07:30:25
问题 I would like to create a class, which has an enumeration as an attribute. This enumeration should have a string representation that shows up as human-readable value when dumping the instance of the class that uses the enum attribute as a JSON string. In the minimal working example below, I created three enumerations in three different ways. After the deserialization, each attribute shows us that it comes from an enumeration except the enumeration with a string representation. It is just a

How to get @property methods in asdict?

孤人 提交于 2020-12-09 06:58:12
问题 I have something like: from attr import attrs, attrib @attrs class Foo(): max_count = attrib() @property def get_max_plus_one(self): return self.max_count + 1 Now when I do: f = Foo(max_count=2) f.get_max_plus_one =>3 I want to convert this to dict: {'max_count':2, 'get_max_plus_one': 3} When I used attr.asdict(f) I do not get the @property . I get only {'max_count':2} . What is the cleanest way to achieve the above? 回答1: For this case, you can use dir on the object, and get only the

How to get @property methods in asdict?

喜夏-厌秋 提交于 2020-12-09 06:55:38
问题 I have something like: from attr import attrs, attrib @attrs class Foo(): max_count = attrib() @property def get_max_plus_one(self): return self.max_count + 1 Now when I do: f = Foo(max_count=2) f.get_max_plus_one =>3 I want to convert this to dict: {'max_count':2, 'get_max_plus_one': 3} When I used attr.asdict(f) I do not get the @property . I get only {'max_count':2} . What is the cleanest way to achieve the above? 回答1: For this case, you can use dir on the object, and get only the

How to get @property methods in asdict?

风格不统一 提交于 2020-12-09 06:55:12
问题 I have something like: from attr import attrs, attrib @attrs class Foo(): max_count = attrib() @property def get_max_plus_one(self): return self.max_count + 1 Now when I do: f = Foo(max_count=2) f.get_max_plus_one =>3 I want to convert this to dict: {'max_count':2, 'get_max_plus_one': 3} When I used attr.asdict(f) I do not get the @property . I get only {'max_count':2} . What is the cleanest way to achieve the above? 回答1: For this case, you can use dir on the object, and get only the

How to get @property methods in asdict?

懵懂的女人 提交于 2020-12-09 06:54:46
问题 I have something like: from attr import attrs, attrib @attrs class Foo(): max_count = attrib() @property def get_max_plus_one(self): return self.max_count + 1 Now when I do: f = Foo(max_count=2) f.get_max_plus_one =>3 I want to convert this to dict: {'max_count':2, 'get_max_plus_one': 3} When I used attr.asdict(f) I do not get the @property . I get only {'max_count':2} . What is the cleanest way to achieve the above? 回答1: For this case, you can use dir on the object, and get only the

Error while launching Jupyter Notebook - @attr.s(hash=True) AttributeError: module 'attr' has no attribute 's'

ぐ巨炮叔叔 提交于 2020-04-16 05:48:50
问题 I am not able to launch or open Jupyter Notebook. I have uninstalled and installed it multiple times, still get the same error which I have pasted below! It's giving some 'Attribute Error'. Traceback (most recent call last): File "C:\Users\Ramya\anaconda3\Scripts\jupyter-notebook-script.py", line 6, in from notebook.notebookapp import main File "C:\Users\Ramya\anaconda3\lib\site-packages\notebook\notebookapp.py", line 80, in from .services.contents.manager import ContentsManager File "C:

Perfect forwarding - in Python

蹲街弑〆低调 提交于 2019-12-30 06:15:28
问题 I am a maintainer of a Python project that makes heavy use of inheritance. There's an anti-pattern that has caused us a couple of issues and makes reading difficult, and I am looking for a good way to fix it. The problem is forwarding very long argument lists from derived classes to base classes - mostly but not always in constructors. Consider this artificial example: class Base(object): def __init__(self, a=1, b=2, c=3, d=4, e=5, f=6, g=7): self.a = a # etc class DerivedA(Base): def __init_

How do I avoid the “self.x = x; self.y = y; self.z = z” pattern in __init__?

旧时模样 提交于 2019-12-17 21:38:27
问题 I see patterns like def __init__(self, x, y, z): ... self.x = x self.y = y self.z = z ... quite frequently, often with a lot more parameters. Is there a good way to avoid this type of tedious repetitiveness? Should the class inherit from namedtuple instead? 回答1: Edit: If you have python 3.7+ just use dataclasses A decorator solution that keeps the signature: import decorator import inspect import sys @decorator.decorator def simple_init(func, self, *args, **kws): """ @simple_init def __init__

How to specify that an attribute must be a list of (say) integers, not just a list?

蓝咒 提交于 2019-12-01 17:30:59
Using the attrs libary and Python 3.6, I thought the following would allow me to specify that x and y can only contain integers: import attr @attr.s class C: x : List[int] = attr.ib() # not working y = attr.ib(type=List[int]) # not working either Both of the commented lines throw a NameError: name 'List' is not defined . The reasons I expected that to work are these: (1) The types section of the attr documentation includes the following passage: " attrs also allows you to associate a type with an attribute using either the type argument to attr.ib() or – as of Python 3.6 – using PEP 526