I have a lot of NamedTuples in my codebase, and I now add new ones never. They 
were a good option prior to Python 3.7 but dataclasses are much easier to work 
with and are almost a drop-in substitute.

A combination of a default dictionary and a dataclass might meet your needs:



import collections
from dataclasses import dataclass


@dataclass
class AccountingEntry:
    description: str
    # other fields


ledger = collections.defaultdict(list)

for ae in get_accounting_entries():
    ledger[ae.description] = ae



From: Python-list <python-list-bounces+gweatherby=uchc....@python.org> on 
behalf of songbird <songb...@anthive.com>
Date: Wednesday, December 14, 2022 at 10:38 PM
To: python-list@python.org <python-list@python.org>
Subject: Keeping a list of records with named fields that can be updated
*** Attention: This is an external email. Use caution responding, opening 
attachments or clicking on links. ***

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


  songbird
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!ndkoYjlClLoELvhzTpXFZEtJ70fXjdFllo-ce0fJ4f0AdRLQXvryO11ZSJ16tf-Ke-pko3kmBxW1cesvrQAQUQ$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!ndkoYjlClLoELvhzTpXFZEtJ70fXjdFllo-ce0fJ4f0AdRLQXvryO11ZSJ16tf-Ke-pko3kmBxW1cesvrQAQUQ$>
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to