namedtuple

What is the advantage in using `exec` over `type()` when creating classes at runtime?

不羁的心 提交于 2021-02-17 19:31:12
问题 I want to dynamically create classes at runtime in python. For example, I want to replicate the code below: >>> class RefObj(object): ... def __init__(self, ParentClassName): ... print "Created RefObj with ties to %s" % ParentClassName ... class Foo1(object): ... ref_obj = RefObj("Foo1") ... class Foo2(object): ... ref_obj = RefObj("Foo2") ... Created RefObj with ties to Foo1 Created RefObj with ties to Foo2 >>> ... but I want the Foo1, Foo2, Foo classes to be created dynamically (ie: during

Using collections.namedtuple with ProcessPoolExecutor gets stuck in a few cases

北城余情 提交于 2021-02-17 05:56:45
问题 >>> import concurrent.futures >>> from collections import namedtuple >>> #1. Initialise namedtuple here >>> # tm = namedtuple("tm", ["pk"]) >>> class T: ... #2. Initialise named tuple here ... #tm = namedtuple("tm", ["pk"]) ... def __init__(self): ... #3: Initialise named tuple here ... tm = namedtuple("tm", ["pk"]) ... self.x = {'key': [tm('value')]} ... def test1(self): ... with concurrent.futures.ProcessPoolExecutor(max_workers=1) as executor: ... results = executor.map(self.test, ["key"])

how do I add fields to a namedtuple?

爱⌒轻易说出口 提交于 2021-02-07 11:16:57
问题 I am working with a list of namedtuples. I would like to add a field to each named tuple after it has already been created. It seems I can do that by just referencing it as an attribute (as in namedtuple.attribute = 'foo' ), but then it isn't added to the list of fields. Is there any reason why I shouldn't do it this way if I don't do anything with the fields list? Is there a better way to add a field? >>> from collections import namedtuple >>> result = namedtuple('Result',['x','y']) >>>

Python: Easy way to replace attribute on nested namedtuple?

冷暖自知 提交于 2021-01-28 07:07:53
问题 I'm creating a data structure with nested namedtuples (practicing my immutable functional programming skills), but am struggling to find an easy way to replace values in nested namedtuples. Let's say I have a data structure like this: from collections import namedtuple Root = namedtuple("Root", "inventory history") Inventory = namedtuple("Inventory", "item1 item2") Item = namedtuple("Item", "name num") Event = namedtuple("Event", "action item num") r = Root( inventory=Inventory( item1=Item

Dictionary of (named) tuples in Python and speed/RAM performance

强颜欢笑 提交于 2020-12-30 03:14:20
问题 I'm creating a dictionary d of one million of items which are tuples, and ideally I'd like to access them with: d[1634].id # or d[1634]['id'] d[1634].name # or d[1634]['name'] d[1634].isvalid # or d[1634]['isvalid'] rather than d[1634][0] , d[1634][1] , d[1634][2] which is less explicit. According to my test: import os, psutil, time, collections, typing Tri = collections.namedtuple('Tri', 'id,name,isvalid') Tri2 = typing.NamedTuple("Tri2", [('id', int), ('name', str), ('isvalid', bool)]) t0 =

Dictionary of (named) tuples in Python and speed/RAM performance

ⅰ亾dé卋堺 提交于 2020-12-30 03:10:46
问题 I'm creating a dictionary d of one million of items which are tuples, and ideally I'd like to access them with: d[1634].id # or d[1634]['id'] d[1634].name # or d[1634]['name'] d[1634].isvalid # or d[1634]['isvalid'] rather than d[1634][0] , d[1634][1] , d[1634][2] which is less explicit. According to my test: import os, psutil, time, collections, typing Tri = collections.namedtuple('Tri', 'id,name,isvalid') Tri2 = typing.NamedTuple("Tri2", [('id', int), ('name', str), ('isvalid', bool)]) t0 =

Equality overloading for namedtuple

蹲街弑〆低调 提交于 2020-07-20 07:32:32
问题 Is there a way to overload the equality operator __eq__(self, other) for a namedtuple in python? I know this is possible in classes and redefining the method, but is this possible for a namedtuple as well, and how would you implement this? 回答1: I think, given the public API of namedtuple, it is not possible to do that without overriding. The shortest solution would be: class Person(namedtuple('Person', ['ssn', 'name'])): def __eq__(self, other): return self.ssn == other.ssn -- >>> p1 = Person