On 14/12/2022 19:50, songbird wrote:

   I'm relatively new to python but not new to programming in general.

   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 they all must contain a description.

   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 some basic 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.

   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 changes.

   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 beyond that yet as I'm still learning.

   Suggestions?

   Thanks!  :)

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:

>>> 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)

An alternative would be dataclasses where basic usage is just as easy:

>>> from dataclasses import make_dataclass
>>> Row = make_dataclass("Row", "foo bar baz".split())
>>> row = Row(1, 2, 3)
>>> row
Row(foo=1, bar=2, baz=3)
>>> row.bar = 42
>>> row
Row(foo=1, bar=42, baz=3)


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to