I started using named tuples (it works for reading in the
files and accessing the fields) but I cannot update those
I'm relatively new to python but not new to programming in general.they all must contain a description.
The program domain is accounting and keeping track of stock trades and other related information (dates, cash accounts, interest, dividends, transfers of funds, etc.)
Assume that all data is CSV format. There are multiple files.
Assume there is a coherent starting point and that all data is in order.
Assume each line contains a description. The description determines what the line is. The number of fields in the line does not change within the data file but it may happen that later lines in other files may be different other than the fact that
All descriptions are deterministic (none are recursive or referencing things from the future). All things referenced in the description which do not already exist are added to a list (or perhaps more than one in a few cases) and may contain somebasic information (the date, how many and for how much, or a total amount or a fee or ...) If the field of the line isn't a number it is either a symbol or a description.
A default action is simply to keep most parts of the line and to adjust any totals of a previously seen description that matches by whatever amounts are on the line. The key is the description.changes.
I've already written one program based upon the files I already have which works but what happens is that new descriptions are added (new accounts, new stocks, etc.) and I don't want to have to write new code manually every time a description
I started using named tuples (it works for reading in the files and accessing the fields) but I cannot update those so I need to use something else to give me the list of unique descriptions and fields that I need to update. I've not gotten beyondthat yet as I'm still learning.
Suggestions?
Thanks! :)
songbird
I'm relatively new to python but not new to programming in general.they all must contain a description.
The program domain is accounting and keeping track of stock trades and other related information (dates, cash accounts, interest, dividends, transfers of funds, etc.)
Assume that all data is CSV format. There are multiple files.
Assume there is a coherent starting point and that all data is in order.
Assume each line contains a description. The description determines what the line is. The number of fields in the line does not change within the data file but it may happen that later lines in other files may be different other than the fact that
All descriptions are deterministic (none are recursive or referencing things from the future). All things referenced in the description which do not already exist are added to a list (or perhaps more than one in a few cases) and may contain somebasic information (the date, how many and for how much, or a total amount or a fee or ...) If the field of the line isn't a number it is either a symbol or a description.
A default action is simply to keep most parts of the line and to adjust any totals of a previously seen description that matches by whatever amounts are on the line. The key is the description.changes.
I've already written one program based upon the files I already have which works but what happens is that new descriptions are added (new accounts, new stocks, etc.) and I don't want to have to write new code manually every time a description
I started using named tuples (it works for reading in the files and accessing the fields) but I cannot update those so I need to use something else to give me the list of unique descriptions and fields that I need to update. I've not gotten beyondthat yet as I'm still learning.
Suggestions?
Thanks! :)
Row(foo=1, bar=42, baz=3)from collections import namedtuple
Row = namedtuple("Row", "foo bar baz")
row = Row(1, 2, 3)
row._replace(bar=42)
Row(foo=1, bar=2, baz=3)from dataclasses import make_dataclass
Row = make_dataclass("Row", "foo bar baz".split())
row = Row(1, 2, 3)
row
Row(foo=1, bar=42, baz=3)row.bar = 42
row
While I think what you need is a database instead of the collection of
csv files the way to alter namedtuples is to create a new one:
Row(foo=1, bar=42, baz=3)from collections import namedtuple
Row = namedtuple("Row", "foo bar baz")
row = Row(1, 2, 3)
row._replace(bar=42)
An alternative would be dataclasses where basic usage is just as easy:
Row(foo=1, bar=2, baz=3)from dataclasses import make_dataclass
Row = make_dataclass("Row", "foo bar baz".split())
row = Row(1, 2, 3)
row
Row(foo=1, bar=42, baz=3)row.bar = 42
row
Suggestions?
While I think what you need is a database instead of the collection of
csv files the way to alter namedtuples is to create a new one:
Row(foo=1, bar=42, baz=3)from collections import namedtuple
Row = namedtuple("Row", "foo bar baz")
row = Row(1, 2, 3)
row._replace(bar=42)
An alternative would be dataclasses where basic usage is just as easy:
Row(foo=1, bar=2, baz=3)from dataclasses import make_dataclass
Row = make_dataclass("Row", "foo bar baz".split())
row = Row(1, 2, 3)
row
Row(foo=1, bar=42, baz=3)row.bar = 42
row
On Dec 15, 2022 10:21, Peter Otten <__peter__@web.de> wrote:
>>> from collections import namedtuple
>>> Row = namedtuple("Row", "foo bar baz")
>>> row = Row(1, 2, 3)
>>> row._replace(bar=42)
Row(foo=1, bar=42, baz=3)
====
Ahh, I always thought these are undocumented methods, but: "In addition to
the methods inherited from tuples, named tuples support three additional
methods and two attributes. To prevent conflicts with field names, the
method and attribute names start with an underscore."
https://docs.python.org/3/library/collections.html#collections.somenamedtuple._make
Peter Otten wrote:
...
While I think what you need is a database instead of the collection of
csv files the way to alter namedtuples is to create a new one:
Row(foo=1, bar=42, baz=3)from collections import namedtuple
Row = namedtuple("Row", "foo bar baz")
row = Row(1, 2, 3)
row._replace(bar=42)
namedtuple is easier to use as that will use the csv and
csvreader and create the records without me having to do any
conversion or direct handling myself. it's all automagically
done. my initial version works, but i'd like it to be a bit
more elegant and handle descriptions it hasn't seen before
in a more robust manner.
An alternative would be dataclasses where basic usage is just as easy:
Row(foo=1, bar=2, baz=3)from dataclasses import make_dataclass
Row = make_dataclass("Row", "foo bar baz".split())
row = Row(1, 2, 3)
row
Row(foo=1, bar=42, baz=3)row.bar = 42
row
i do like that i can directly reference each field in a
dataclass and not have to specify a _replace for each change.
is there an easy way to convert from namedtuple to dataclass?
i can see there is a _asdict converter, but don't really like
how that turns out as then i have to do a bunch of:
rec['fieldname'] = blah
rec.fieldname is much easier to understand.
NTRow = namedtuple("NTRow", ["alpha", "beta", "gamma"])
DCRow = make_dataclass("DCRow", NTRow._fields)
NTRow(alpha=1, beta='two', gamma=3.0)ntrow = NTRow(1, "two", 3.0)
ntrow
DCRow(alpha=1, beta='two', gamma=3.0)dcrow = DCRow(*ntrow)
dcrow
I recommend that you use a dataclass /instead/ of a namedtuple, not
both. However, for a dataclass with the same fields in the same order as
in your namedtuple the conversion is trivial:
Create compatible namedtuple and dataclass types:
NTRow = namedtuple("NTRow", ["alpha", "beta", "gamma"])
DCRow = make_dataclass("DCRow", NTRow._fields)
Build the namedtuple:
NTRow(alpha=1, beta='two', gamma=3.0)ntrow = NTRow(1, "two", 3.0)
ntrow
Convert to dataclass:
DCRow(alpha=1, beta='two', gamma=3.0)dcrow = DCRow(*ntrow)
dcrow
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 78:30:02 |
Calls: | 6,716 |
Files: | 12,247 |
Messages: | 5,357,830 |