namedtuple

Serializing a Python namedtuple to json

人走茶凉 提交于 2019-12-17 07:12:18
问题 What is the recommended way of serializing a namedtuple to json with the field names retained? Serializing a namedtuple to json results in only the values being serialized and the field names being lost in translation. I would like the fields also to be retained when json-ized and hence did the following: class foobar(namedtuple('f', 'foo, bar')): __slots__ = () def __iter__(self): yield self._asdict() The above serializes to json as I expect and behaves as namedtuple in other places I use

Serializing a Python namedtuple to json

一笑奈何 提交于 2019-12-17 07:09:36
问题 What is the recommended way of serializing a namedtuple to json with the field names retained? Serializing a namedtuple to json results in only the values being serialized and the field names being lost in translation. I would like the fields also to be retained when json-ized and hence did the following: class foobar(namedtuple('f', 'foo, bar')): __slots__ = () def __iter__(self): yield self._asdict() The above serializes to json as I expect and behaves as namedtuple in other places I use

choosing right data structure to parse a file

半城伤御伤魂 提交于 2019-12-13 04:48:38
问题 I have a csv file with contents in the following format: CSE110, Mon, 1:00 PM, Fri, 1:00 PM CSE114, Mon, 8:00 AM, Wed, 8:00 AM, Fri, 8:00 AM which is basically course name followed by it's timings. what's the best data structure to parse and store this data? I tried using named tuples as follows: CourseTimes = namedtuple('CourseTimes', 'course_name, day, start_time ') But a single course can be scheduled on multiple days and time as shown for cse114 above. This can only be decided at run-time

How do you store the request.form to db through wtforms or error in sqlalchemy update?

假装没事ソ 提交于 2019-12-13 04:23:20
问题 This is following on from this question: SQLalchemy/wtforms update issue - 400 bad request I have a flask framework Issue When I submit the form the flash message comes up saying prediction added although when I query the db nothing has changed?? Can anyone spot where I'm going wrong? What am I trying to achieve Users are able to view their predictions making changes to current predictions. If there are no new predictions then they can submit new predictions. views # Predictor - User makes

Python unpack 2-dimensional list of named tuples

老子叫甜甜 提交于 2019-12-12 18:09:07
问题 I have a 2-dimensional list of named tuples (let's say that each tuple has N values), and I want to unpack them into N different 2-dimensional lists where each unpacked 2-D list is composed entirely of a single attribute from the original list. For example if I have this 2-D list: >>> combo = namedtuple('combo', 'i, f, s') >>> combo_mat = [[combo(i + 3*j, float(i + 3*j), str(i + 3*j)) for i in range(3)] for j in range(3)] >>> combo_mat [[combo(i=0, f=0.0, s='0'), combo(i=1, f=1.0, s='1'),

python collections.namedtuple() confusion

感情迁移 提交于 2019-12-12 14:56:29
问题 The documentation says that any python valid identifier can be a field_name, except those which start with underscore, that's fine. If the rename argument is true, it replaces invalid field_names with valid ones, but in the example specified there, it replaces it with _1 , or _3 , how is that? These start with underscore! The documentation also says: If verbose is true, the class definition is printed just before being built What does this really mean? 回答1: The reason you cannot use

Serializing namedtuples via PyYAML

柔情痞子 提交于 2019-12-12 09:58:31
问题 I'm looking for some reasonable way to serialize namedtuples in YAML using PyYAML. A few things I don't want to do: Rely on a dynamic call to add a constructor/representor/resolver upon instantiation of the namedtuple. These YAML files may be stored and re-loaded later, so I cannot rely on the same runtime environment existing when they are restored. Register the namedtuples in global. Rely on the namedtuples having unique names I was thinking of something along these lines: class namedtuple

Indexing a namedtuple nested in a dictionary

大憨熊 提交于 2019-12-11 17:59:51
问题 I'm looking to store a named tuple inside a dictionary. That parts easy. I don't know how to reference an individual bit in the namedtuple following that though. I know that I could just use a dictionary and make life easier, but in the case where you have values you know you don't want to change, it'd be nice to use a namedtuple here (more so just out of interest - I realize strings are immutable as well). from collections import namedtuple Rec = namedtuple('name', ['First', 'Middle', 'Last'

Passing String, integer and tuple information as key for python dictionary

守給你的承諾、 提交于 2019-12-11 11:36:17
问题 I'm trying to create a python dictionary and I would like to use a key that contains strings, numerics & a list/tuple entry. The key should ideally look like ("stringA", "stringB", "stringC", integer1, (integer2, integer3, integer4)) I tried to create a namedtuple based on this documentation as follows from collections import namedtuple dictKey = namedtuple('dictKey', 'stringA stringB stringC integer1 (integer2 integer3 integer4)') but it throws me a ValueError saying it can only contain

Check if namedtuple with value x exists in list

三世轮回 提交于 2019-12-10 22:54:41
问题 I want to see if a namedtuple exists in a list, similar to: numbers = [1, 2, 3, 4, 5] if 1 in numbers: do_stuff() is there a pythonic (or not) way to do this? Something like: namedtuples = [namedtuple_1, namedtuple_2, namedtuple3] if (namedtuple with value x = 1) in namedtuples: do stuff() 回答1: Use any: Demo: >>> from collections import namedtuple >>> A = namedtuple('A', 'x y') >>> lis = [A(100, 200), A(10, 20), A(1, 2)] >>> any(a.x==1 for a in lis) True >>> [getattr(a, 'x')==1 for a in lis]