All Questions
Tagged with namedtuple python
480
questions
1
vote
0
answers
36
views
Setting the return type of a custom namedtuple
I have a sample_config.json file with potentially an arbitrary number of the parameters. An example of the sample_config.json is as follows:
{
"tableA_ID": "tableA.csv",
"...
1
vote
1
answer
37
views
How can I access the name of namedtuple attribute from inside a method if it wasn't passed as a string?
This is a followup to How can I pass a namedtuple attribute to a method without using a string?.
Given this solution to the above:
from typing import NamedTuple
class Record(NamedTuple):
id: int
...
0
votes
1
answer
71
views
Trying to understand behavior of `_field_defaults` of `namedtuple` in Python 3.9 [closed]
I am new to Python's namedtuple, particularly, _field_defaults.
According to Python 3.12.3 official doc,
some namedtuple._field_defaults
Dictionary mapping field names to default values.
Would ...
-1
votes
1
answer
55
views
Get the total count of 1 field of a list of namedtuple objects
In Python, what is the easiest way to get the total count of one field from a set of named tuples?
In this example, I'm looking for the total count of TestResults.failed, which should be 4. It's ...
0
votes
2
answers
52
views
Conversion of Dataframe to namedtuple of numpy arrays
I need to convert my pandas dataframe to tuple of numpy arrays. My code is like,
import pandas as pd
data = {'Class':[10., 11., 9., 8.],
'Age':[27., 24., 22., 32.],
'Mark':[76., 56., ...
0
votes
1
answer
126
views
_pickle.PicklingError: Can't pickle <class '__main___.{CONST} '>: attribute lookupfaileddir_namesmain_on __main__ failed
firstly dir_names works well when pickling
dir_names = collections.namedtuple('dir_names', ['mark', 'category'])
pickle.dump(dir_names, open('tmp.bin', 'wb'))
I want make dir_name a const
DIR_NAMES = ...
0
votes
1
answer
54
views
define the args of a function outside the actual call
I have a function that has to call other functions, with a not-fixed number and type of arguments, that I want to set in a dictionary.
Something like this:
def main_function(...
0
votes
1
answer
78
views
How is Python able to distinct type names of namedtuples? [duplicate]
My model class has to provide lots of queries and I would like to return query results as namedtuple. My solution is working, but I don't know why :) - so I would like to get some clarification ...
0
votes
1
answer
83
views
Why does namedtuple + namedtuple return a regular tuple?
from collections import namedtuple
foo_a = namedtuple("foo_a", ["a1", "a2", "a3"])
foo_b = namedtuple("foo_b", ["b1", "b2", "...
2
votes
1
answer
350
views
Using a cached property on a named tuple
from typing import NamedTuple
from functools import cached_property
class Rectangle(NamedTuple):
x: int
y: int
@cached_property
def area(self):
return self.x * self.y
I ...
0
votes
1
answer
129
views
Print named tuple value in a list
A function is returning me the below list of named tuples:
[popenfile(path='/home/giampaolo/monit.py', fd=3, position=0, mode='r', flags=32768), popenfile(path='/var/log/monit.log', fd=4, position=...
0
votes
1
answer
62
views
modifiy one element of namedtuple of list
I have written script to extract some information from pdf file.
Each page is read as blocks.
if [V2G has been found, then it will saved it as well as the title ,subtitle and the bulleted list.
My ...
0
votes
1
answer
37
views
How can I make a data structure containing a variable so that it updates when the variable is re-defined?
I am making a text adventure game. I'm using a named tuple to define locations.
Location = namedtuple('Location', ['desc', 'ldesc', 'func', 'dirloc'])
entrance = foyer = None
entrance = Location('...
2
votes
1
answer
329
views
How to alias class attributes when creating NamedTuple?
I want to alias project_version with init_version, but since NamedTuple is a factory method I'm having difficulty in doing this.
from typing import NamedTuple
class ProjectMetadata(NamedTuple):
&...
1
vote
1
answer
506
views
PyCharm: namedtuple: unexpected argument, unfilled parameter
In PyCharm, you can declare a named tuple.
from collections import namedtuple
InstTyp = namedtuple(
typename='InstTyp',
field_names='''
instance_type
memory
num_cpus ...