Questions tagged [namedtuple]

namedtuple is a data structure provided by the Python collections module. It enables the creation of tuples with named elements (e.g., a Student tuple with the values (name, school, age) rather than a tuple with just two strings and an integer).

namedtuple is a data structure provided by the Python collections module. It enables the creation of tuples with named elements (e.g., a Student tuple with the values (name, school, age) rather than a tuple with just two strings and an integer).

Documentation: collections module in Python 3.

Recently, a declarative API for namedtuples is also provided by the typing module.

545 questions
0
votes
2 answers

Are pickle-able tuple-factories (with names) possible?

There are several questions about pickling namedtuples already, however none of the ones I found [1] [2] [3] [4] deals with the case of pickling a namedtuple that is bound on an object instance. Consider the following example import pickle from…
Hyperplane
  • 1,422
  • 1
  • 14
  • 28
0
votes
1 answer

Mypy does not detect errors regarding the use of NamedTuple inside functions

In the following code the defintions of x2 and x3 are obviously wrong, yet mypy does not complain. from typing import NamedTuple class X(NamedTuple): a: float b: float c: float def foo(): x1 = X(1, 2, 3) x2 = X(1, 2) x3 =…
Friedrich Gretz
  • 535
  • 4
  • 14
0
votes
0 answers

How to type a generic function with namedtuples?

I have a wrapper class for a database cursor like this: namedtuple_def = TypeVar("namedtuple_def") class Wrapper: def fetchall(defn: Optional[namedtuple_def] = None) -> List[namedtuple_def]: return self.cursor.fetchall() The underlying…
ldrg
  • 4,150
  • 4
  • 43
  • 52
0
votes
1 answer

Iterate through list of NamedTuples and add random value

I have a list of NamedTuples (short extract below) and wrote some code to add the same random value to each of the a and b values in the tuple. The list will vary in length. This works ok but I now need to add a different random value to each of the…
Robsmith
  • 339
  • 1
  • 8
0
votes
2 answers

Search substring in list of dictionaries of namedtuples keyed with an event type

I have created a list of dictionaries of named tuples, keyed with an event type. [{'EVENT_DELETE': DeleteRequestDetails(rid=53421, user='user1', type='EVENT_DELETE', reviewed=1, approved=1, completed=0)},{'EVENT_DELETE':…
gosaultech
  • 89
  • 1
  • 8
0
votes
0 answers

Python proccess pool executor and named tuple leads to unit test error -> "cannot pickle '_io.TextIOWrapper' object"

I am trying to use namedtuple along with process pool executor and when writing a unit test , i keep getting an error below The dut in the code represent the python file where i have the 2 functions stored and i am calling it from the unit…
0
votes
1 answer

UrlSplitResult: can't _replace fields

I'm trying to inject a basic-authentication into an url by split-inject-join: url = urllib.parse.urlsplit(url) new_url = url._replace(username=user, password=password) But I'm surprised about the behavior of the SplitResult I get from the…
xtofl
  • 40,723
  • 12
  • 105
  • 192
0
votes
2 answers

How to store a collection of constants that can be called by Collection.variable?

I'm searching for a collection that can be used to store multiple constants and also can be used to get a list of them? EDIT: It does not have to be constants, it can be variables. I want to get a value this way: Constants.first_one But also get a…
Milano
  • 18,048
  • 37
  • 153
  • 353
0
votes
1 answer

Inheriting from Generic and NamedTuple fails with "missing required positional argument"

I've encountered a problem when trying to define a generic (typing/mypy Generic) NamedTuple. I've managed to reduce it to smallest possible working example: a.py: from typing import NamedTuple from typing import Generic from typing import TypeVar T…
Jan Spurny
  • 5,219
  • 1
  • 33
  • 47
0
votes
1 answer

Unable to compare types of identical but redeclared namedtuples in Python

While working on a difference engine to identify differences in very large data structures, I noticed that a type comparison between identical-but-redeclared namedtuples misbehaves. Redeclaring the namedtuples is unavoidable*. Here is a minimal…
Drakes
  • 23,254
  • 3
  • 51
  • 94
0
votes
1 answer

Replace a element in namedtupled of a nested list

I would like to edit the vote, but I cannot find how to do it. Please help this_list = [Mascot(mascot_name='Peter', species='Anteater', school_name='UC Irvine', number_of_votes=137988), Mascot(mascot_name='Victor E.', species='Bulldog',…
sala123
  • 11
0
votes
1 answer

Convert txt to JSON with Python using namedtuple

I'm trying to convert a txt file to JSON using this: import json import collections def main(): file = open("file.txt") #structure the output using namedtuple UserBase = collections.namedtuple( "UserBase", [ …
Maddax
  • 3
  • 2
0
votes
1 answer

How to construct circular referencing instances of a frozen class in python

I have instances of a dataclass that reference each other. from dataclasses import dataclass @dataclass() class Foo: id: int neighbor: 'Foo' foo = Foo(1, None) bar = Foo(2, foo) foo.neighbor = bar I really want a frozen class,…
Durtal
  • 1,063
  • 3
  • 11
0
votes
1 answer

Nested data structures in Python

I am trying to create a data structure that will be used for geospatial decomposition - basically take a top 10x10 grid and be able to decompose each grid within it. I am using namedtuples for the structure. And to replicate the structure for the…
Bryon
  • 939
  • 13
  • 25
0
votes
1 answer

How to pass namedtuple key as a variable

I want to call different namedtuple values where my namedtuple key is passed from a variable. Here I am trying to get the value of namedtuple qa where the key qa will be gotten from variable environment. from collections import…
Epilogue
  • 3
  • 5