Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Beware the new TTY code!


devel / comp.lang.python / Re: Keeping a list of records with named fields that can be updated

SubjectAuthor
* Keeping a list of records with named fields that can be updatedsongbird
+- Re: Keeping a list of records with named fields that can be updatedStefan Ram
+- Re: Keeping a list of records with named fields that can be updatedThomas Passin
+* Re: Keeping a list of records with named fields that can be updatedPeter Otten
|+- Re: Keeping a list of records with named fields that can be updatedsongbird
|`* Re: Keeping a list of records with named fields that can be updatedsongbird
| `* Re: Keeping a list of records with named fields that can be updatedPeter Otten
|  `- Re: Keeping a list of records with named fields that can be updatedsongbird
+- Re: Keeping a list of records with named fields that can be updatedWeatherby,Gerard
`- Re: Keeping a list of records with named fields that can be updatedGilmeh Serda

1
Keeping a list of records with named fields that can be updated

<6gip6j-698.ln1@anthive.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=25096&group=comp.lang.python#25096

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: songbird@anthive.com (songbird)
Newsgroups: comp.lang.python
Subject: Keeping a list of records with named fields that can be updated
Date: Wed, 14 Dec 2022 13:50:14 -0500
Organization: the little wild kingdom
Lines: 25
Message-ID: <6gip6j-698.ln1@anthive.com>
Reply-To: songbird <songbird@anthive.com>
Injection-Info: reader01.eternal-september.org; posting-host="f217ac5282fb1edfbb39c596b4779cc0";
logging-data="3004211"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/N+SehmmhNca7TBvPrbYjPPb9MJQJJIxM="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:l6GH5PHmisS9SORCjpthKkWntoA=
 by: songbird - Wed, 14 Dec 2022 18:50 UTC

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

Re: Keeping a list of records with named fields that can be updated

<lists-20221214201437@ram.dialup.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=25098&group=comp.lang.python#25098

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!rocksolid2!news.neodome.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Keeping a list of records with named fields that can be updated
Date: 14 Dec 2022 19:23:11 GMT
Organization: Stefan Ram
Lines: 35
Expires: 1 Sep 2023 11:59:58 GMT
Message-ID: <lists-20221214201437@ram.dialup.fu-berlin.de>
References: <6gip6j-698.ln1@anthive.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de X+jp3TNxB32ChCr0OlvOSgMDU2LIJ/dLErvoDtEk7w46n6
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Wed, 14 Dec 2022 19:23 UTC

songbird <songbird@anthive.com> writes:
>I started using named tuples (it works for reading in the
>files and accessing the fields) but I cannot update those

Instead of named tuples, you could use dictionaries, regular
classes, or data classes. Some like the library "attrs" to
reduce boilerplate code in classes, some like "Pydantic" or
"chili".

Some books that I deem to be ok:

"Object-Oriented Programming in Python Documentation" - a PDF file,
Introduction to Programming Using Python - Y Daniel Liang (2013),
How to Think Like a Computer Scientist - Peter Wentworth (2012-08-12),
The Coder's Apprentice - Pieter Spronck (2016-09-21), and
Python Programming - John Zelle (2009).

For advanced learners:

Fluent Python - Luciano Ramalho (2015)
Pro Python - James Browning (2014)
The Python Journeyman - Robert Smallshire (2018-01-02)
Python Applications Programming - Wesley Chun (2012-03)
Mastering Object-Oriented Python - Steven F. Lott (2014)

, python.org has:

Python Tutorial
The Python Library Reference
The Python Language Reference
(and more ...)

which I like to download as PDF files.

Re: Keeping a list of records with named fields that can be updated

<mailman.1168.1671077464.20444.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=25102&group=comp.lang.python#25102

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mixmin.net!news2.arglkargh.de!news.karotte.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: list1@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: Keeping a list of records with named fields that can be updated
Date: Wed, 14 Dec 2022 22:54:04 -0500
Lines: 29
Message-ID: <mailman.1168.1671077464.20444.python-list@python.org>
References: <6gip6j-698.ln1@anthive.com>
<49bbb11a-a33d-b883-b3e4-7418840dbb99@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de hbTOGhQJ8a2mn25Ci7Gl0Qr62QnwhvqTosTeCtVqzQmQ==
Return-Path: <list1@tompassin.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=tompassin.net header.i=@tompassin.net header.b=gPs1EJm5;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.007
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'csv': 0.03; 'learning.':
0.04; 'is.': 0.05; 'matches': 0.07; 'much,': 0.09;
'received:23.83.212': 0.09; 'received:elm.relay.mailchannels.net':
0.09; 'subject:list': 0.11; '(it': 0.16; 'coherent': 0.16;
'date,': 0.16; 'determines': 0.16; 'general.': 0.16;
'received:10.0.0': 0.16; 'received:64.90': 0.16;
'received:64.90.62': 0.16; 'received:64.90.62.162': 0.16;
'received:dreamhost.com': 0.16; 'referencing': 0.16; 'relatively':
0.16; 'songbird': 0.16; 'stocks,': 0.16; 'subject:named': 0.16;
'suggestions?': 0.16; 'symbol': 0.16; 'tuples': 0.16; 'wrote:':
0.16; 'python': 0.16; 'pm,': 0.19; 'to:addr:python-list': 0.20;
'written': 0.22; "i've": 0.22; 'code': 0.23; 'lines': 0.23;
'thanks!': 0.24; 'cannot': 0.25; 'programming': 0.25; "isn't":
0.27; 'else': 0.27; 'fact': 0.28; 'keeping': 0.28; 'header:User-
Agent:1': 0.30; 'default': 0.31; 'program': 0.31; 'amounts': 0.32;
'assume': 0.32; 'gotten': 0.32; 'received:10.0': 0.32;
'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'but': 0.32; "i'm": 0.33;
'there': 0.33; 'header:In-Reply-To:1': 0.34; 'subject:that': 0.35;
'track': 0.35; 'files': 0.36; 'change': 0.36; 'those': 0.36;
'using': 0.37; 'file': 0.38; 'added': 0.39; 'list': 0.39; 'use':
0.39; 'fee': 0.39; 'still': 0.40; 'cash': 0.40; 'files.': 0.40;
'happen': 0.40; 'something': 0.40; 'want': 0.40; 'here.': 0.61;
'seen': 0.62; 'simply': 0.63; 'key': 0.64; 'accessing': 0.64;
'your': 0.64; 'upon': 0.64; 'parts': 0.65; 'named': 0.65;
'header:Received:6': 0.67; 'received:64': 0.67; 'manually': 0.69;
'order.': 0.69; 'stock': 0.69; 'within': 0.69; 'domain': 0.77;
'field': 0.78; 'accounts,': 0.81; 'happens': 0.84; 'funds,': 0.84;
'referenced': 0.84; 'subject:updated': 0.84; 'friends': 0.96
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1671076453; a=rsa-sha256;
cv=none;
b=AO6ufcZI5YUen4JQSxSEHHFNWxc3nisR4kWkVB5C8c1vh+ZwdHf8LATz2YTTw8gqASCMLW
cZeHlACRWS9uPdEbOZqoVKwB9wXTdlhLDT0Yt4NYW5WIk0IdFYr86eoQbuoLPQ7Km4Bn/W
jz124QINaRSlAH3Zd2L8ppAjM6PXffMAH5rpfwatbGJ2zOr0ex3VvxWSSDhCpt1ZtZy96B
d2pvecqGo/DCNudHuegjdEl0jeham21lKjnEQFo2h40MGiBgBKDYIhso3eoSGn49FaiXD9
eaAGhKYQ/m8OObdcr7Zi4j2zUg6QP6VGaWRMybhF2NhQ3hIaEOG4R9vD5o2t/Q==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1671076453;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=dvU6+4YVRJT9hOSUSejQOVL9RbeHuXia7bTyZwd0i0g=;
b=b+qWk7YhuNeExW8750ySmR9sCUAF6gaNWQbrCwvvuR28fwRiTP9YREznF8jkmALHWxi0vz
PXFPJhSV5kmIpu9d1EQgxYvprofHElb2I9MwSlhPY2PkMNqqDeFIMBxvNaqraI6B1KdG1F
kbFwF7u5KhHQpJckWzfmxS/LkZdiL+HWzQaV1YGzaqW9AcA4DeDeHRFjEmo9drA/V6V6iM
L5pAauuzD8qJ5zgXkpUKPXb9Y0CMcSP3+ezEOPxxSV7paiLFBmpIZC37BscJQJaDBLhMxB
KWx37vDs8cbcjvpo9pa7oayMV52Wo3C0Al1yidElEPmzaMDgtEdiD7EjFMo5tw==
ARC-Authentication-Results: i=1; rspamd-747d4f8b9f-5nsd2;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Harbor-Battle: 2d3804cf45daa3ab_1671076453990_2779011821
X-MC-Loop-Signature: 1671076453990:2655048292
X-MC-Ingress-Time: 1671076453990
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1671076453;
bh=dvU6+4YVRJT9hOSUSejQOVL9RbeHuXia7bTyZwd0i0g=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=gPs1EJm5LaxK31XgFwF1k2b4nBHBOUjd7leOlmWACT3aJbYYwiSjsKfLLKSoqKYb2
gBtUtTv4FW15Ak8cXOuJGwbhDk8Hq0Pb243aUZ179PorRIaRmqzKCKMQeJNWtx3QCt
lPq69FWHAnFOQZuRPzKgfyWl9er1gGqTpyWJCyHbrx1ZW/kSjvT/NQ5qFyuHdMerjt
wxibn0XLePkVxGMHTjbZUvKgG7k0wxEUqKffC9+3jfz8Cm7uw5X91z/u2zyzzPiVfP
co/SN+f+xPf8PGeq7+CI4dapCPFM1TEe0udSarSmm3wmWspkUIGnmn8cW5Oh3nhO6U
ONcfkKNxbr3Yg==
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.5.1
Content-Language: en-US
In-Reply-To: <6gip6j-698.ln1@anthive.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <49bbb11a-a33d-b883-b3e4-7418840dbb99@tompassin.net>
X-Mailman-Original-References: <6gip6j-698.ln1@anthive.com>
 by: Thomas Passin - Thu, 15 Dec 2022 03:54 UTC

Dictionaries and sets are your friends here.

On 12/14/2022 1:50 PM, 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! :)
>
>
> songbird

Re: Keeping a list of records with named fields that can be updated

<mailman.1175.1671096336.20444.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=25110&group=comp.lang.python#25110

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: __peter__@web.de (Peter Otten)
Newsgroups: comp.lang.python
Subject: Re: Keeping a list of records with named fields that can be updated
Date: Thu, 15 Dec 2022 10:21:14 +0100
Lines: 64
Message-ID: <mailman.1175.1671096336.20444.python-list@python.org>
References: <6gip6j-698.ln1@anthive.com>
<9158537c-f561-e666-f60c-34d50f61d7b7@web.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de 6KcGnGx07UPvFvgyt4alBA3i1g2j1mVvuZFBzctcaOtQ==
Return-Path: <__peter__@web.de>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=web.de header.i=@web.de header.b=L/1SZKI/; dkim-adsp=pass;
dkim-atps=neutral
X-Spam-Status: OK 0.006
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'csv': 0.03; 'learning.':
0.04; 'is.': 0.05; 'row': 0.05; 'usage': 0.05; 'matches': 0.07;
'much,': 0.09; 'subject:list': 0.11; 'import': 0.15; '(it': 0.16;
'coherent': 0.16; 'date,': 0.16; 'determines': 0.16;
'from:addr:web.de': 0.16; 'general.': 0.16; 'message-id:@web.de':
0.16; 'received:mout.web.de': 0.16; 'received:web.de': 0.16;
'referencing': 0.16; 'relatively': 0.16; 'songbird': 0.16;
'stocks,': 0.16; 'subject:named': 0.16; 'suggestions?': 0.16;
'symbol': 0.16; 'tuples': 0.16; 'wrote:': 0.16; 'python': 0.16;
'instead': 0.17; 'to:addr:python-list': 0.20; 'written': 0.22;
"i've": 0.22; 'code': 0.23; 'lines': 0.23; 'received:de': 0.23;
'thanks!': 0.24; 'cannot': 0.25; 'programming': 0.25; "isn't":
0.27; 'else': 0.27; '>>>': 0.28; 'fact': 0.28; 'keeping': 0.28;
'header:User-Agent:1': 0.30; 'default': 0.31; 'program': 0.31;
'think': 0.32; 'amounts': 0.32; 'assume': 0.32; 'gotten': 0.32;
'but': 0.32; "i'm": 0.33; 'there': 0.33; 'header:In-Reply-To:1':
0.34; 'bar': 0.35; 'subject:that': 0.35; 'track': 0.35; 'files':
0.36; 'change': 0.36; 'those': 0.36; 'using': 0.37;
'received:192.168': 0.37; 'file': 0.38; 'way': 0.38; 'added':
0.39; 'list': 0.39; 'use': 0.39; 'fee': 0.39; 'still': 0.40;
'cash': 0.40; 'files.': 0.40; 'happen': 0.40; 'something': 0.40;
'want': 0.40; 'seen': 0.62; 'simply': 0.63; 'skip:m 20': 0.63;
'skip:b 10': 0.63; 'key': 0.64; 'skip:r 20': 0.64; 'accessing':
0.64; 'upon': 0.64; 'parts': 0.65; 'named': 0.65; 'received:217':
0.67; 'manually': 0.69; 'order.': 0.69; 'stock': 0.69; 'within':
0.69; 'domain': 0.77; 'field': 0.78; 'database': 0.80;
'accounts,': 0.81; 'happens': 0.84; 'funds,': 0.84; 'one:': 0.84;
'referenced': 0.84; 'subject:updated': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=s29768273;
t=1671096328; bh=hinZx9ReekED+wesN26Fwi1XSipDQ8FFPl8X/If8daw=;
h=X-UI-Sender-Class:Date:Subject:To:References:From:In-Reply-To;
b=L/1SZKI/wS/pr6rS4FWOB7en2Fh03LhRPLrcNjFrwjwIqHuPonfEiYeLj1Y5KYjjc
xhfRmCsYkMpqhnyHnHwzsHpdI/3bQu2Q5XC2U69jLUKQwGJSeLLpW0zdHbQgIXcPMx
zcGLFHNLkqY/jZ1Vr8/Q+1iDn77rb9Jn80uM5MJreAyEmQZk/vPrL2lvqD1SOKy8Fg
lu7TnD0XoXDTihCJshQKjN/+g9q2YRk4nhg3HwKfdXCrphmlgztdDRNkgEcreM+TQ5
0gY6eWoLX6krHnVXYZX80gbkZCxZpoobk8kTn32HBWmEXmLEr8FXnZ7tLqrryWbWdq
EKnuYep95e/Iw==
X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6
User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101
Thunderbird/102.6.0
Content-Language: en-US
In-Reply-To: <6gip6j-698.ln1@anthive.com>
X-Provags-ID: V03:K1:F85PGd+bMp7N3NOc1vZ8PohyPAexfhpwul2uVJXpCs0vUJNKqAO
X5ZvJ7hTu/dgK5i6axulAbsDV1M/Ixpj968XY+XrqsVZXu3316z8coCEi2qRbFmqYTRm9tw
TIRBrMvUJr79u+d37wqkB3jPmjsGxoYmb63CF3O4lHNnOc8JBT6bk3l05O/jY5SqzPtXoQc
8Upzqn8R2wUSEYYIxFB/g==
X-Spam-Flag: NO
UI-OutboundReport: notjunk:1;M01:P0:8HO6NU4W490=;fPA7MXBEJs6T9NjEuZNtjMs3wWD
LIXlmfiCcSvFrmzLXYVCuBCDhcmVprxqteAD9V5ETFPogXsOlx5t384GA0Xvk5LykwKU6pUbI
/TvKyLpVxjidtCQeIac5SHkNwNOBkjtXK8gn98GdmTvnVxba0HX5jJJR6FDxZqHNXPjc0j9N1
lw6epTJc0vlamchRdK7PWyZV8Id+owNjFe8PUW07tQYj4tj/XNc6ReXI1FFG7cE8q4tXkY4YA
QAK4pZ1a1/tSLZcGRDUDUWRCAQ6CX6UnCbXQfuW88yz39ZWAbyd9wdwsyf00mkBjQFiJXw1SV
BG7fITlqCZT/3SqPhcpu4Mj+xMUaOIfVFYJ+Vor0LO5H5YXyvYbjldwb3c1c0XDp47ZknEseV
SxQshTe1QPIPbVVMap5jrpx2MqdZl7Rlr9yeFfJ8Y4Oue8u+MX5QXCCSww3EK1zUf3SY4EOoA
HdzG67lqWPd2z2FPAU41VqNpLMLAULnsNTQOcnYTKuXB5cO7lBkrRIzGRa7TIBbo+KQ0Z7L8o
V+DKF60i4s8gGHUB+94Achs3dkew3I/dLzKLNnw6aznDt3afBDio13PvidZ5NjaNSk/0gDqKC
k0hUGz7GMYNtWoFSn/7Yw2HWQezBIIppXrRjeI+jQOad+4FT8DGnP5IAKXg3RrwKIeCn2IcZ2
eKQLFyp/9FU84a+Gc5cg3U/z+7mORvGJgRO+6MzoGTss1dFVvXP1RwWjztJFH5VeizpLJpUtG
F//+o2sk2T77DaogGUHHOxDj85bLl0zepUdg0tuzSvCacKdcUcS7lC51XSrwwbPjtAVOG94u6
MQg2zP76zlcW6vJhdkhEVHHXHLHI54iXr+10eUk5xgSSUB+UhW5TbM7KPY7TvQ0vfy9JF1ym+
XZ2gaxfu0Y05y1O6FZCkSYApTZI1SszpzZFmQsVN72Y9pnuNISnYVCeKYFlF8n5n48mhnaJDa
CqAQ8MI92djm6q0AkWZqzULdyV8=
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <9158537c-f561-e666-f60c-34d50f61d7b7@web.de>
X-Mailman-Original-References: <6gip6j-698.ln1@anthive.com>
 by: Peter Otten - Thu, 15 Dec 2022 09:21 UTC

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)

Re: Keeping a list of records with named fields that can be updated

<mailman.1176.1671109229.20444.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=25112&group=comp.lang.python#25112

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: gweatherby@uchc.edu (Weatherby,Gerard)
Newsgroups: comp.lang.python
Subject: Re: Keeping a list of records with named fields that can be updated
Date: Thu, 15 Dec 2022 13:00:15 +0000
Lines: 82
Message-ID: <mailman.1176.1671109229.20444.python-list@python.org>
References: <6gip6j-698.ln1@anthive.com>
<SA1PR14MB5855FCF33DDDBA034167958CB9E19@SA1PR14MB5855.namprd14.prod.outlook.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de A/UOHxbYjsZtrjipvzJURASxCZpYqtZkE0lF1Jd6Kkvg==
Return-Path: <prvs=03486db407=gweatherby@uchc.edu>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=uchc.edu header.i=@uchc.edu header.b=M2AiB6nF;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '3.7': 0.03; 'csv': 0.03;
'learning.': 0.04; '2022': 0.05; 'description:': 0.05; 'is.':
0.05; 'matches': 0.07; 'much,': 0.09;
'received:namprd14.prod.outlook.com': 0.09; 'url-
ip:13.107.213.67/32': 0.09; 'url-ip:13.107.213/24': 0.09; 'url-
ip:13.107.246.67/32': 0.09; 'url-ip:13.107.246/24': 0.09;
'subject:list': 0.11; 'import': 0.15; 'url:mailman': 0.15; '(it':
0.16; '***': 0.16; 'codebase,': 0.16; 'coherent': 0.16; 'date,':
0.16; 'determines': 0.16; 'general.': 0.16; 'ledger': 0.16;
'referencing': 0.16; 'relatively': 0.16; 'songbird': 0.16;
'stocks,': 0.16; 'subject:named': 0.16; 'suggestions?': 0.16;
'symbol': 0.16; 'tuples': 0.16; 'url:urldefense': 0.16; 'url:v3':
0.16; 'python': 0.16; 'to:addr:python-list': 0.20; 'option': 0.20;
'written': 0.22; "i've": 0.22; 'code': 0.23; 'lines': 0.23;
'thanks!': 0.24; 'to:name:python-list@python.org': 0.24;
'url:listinfo': 0.25; 'cannot': 0.25; 'programming': 0.25;
'opening': 0.26; 'received:edu': 0.26; 'wednesday,': 0.26;
"isn't": 0.27; 'else': 0.27; 'fact': 0.28; 'email
addr:python.org&gt;': 0.28; 'keeping': 0.28; 'default': 0.31;
'program': 0.31; 'amounts': 0.32; 'assume': 0.32; 'gotten': 0.32;
'python-list': 0.32; 'but': 0.32; "i'm": 0.33; 'there': 0.33;
'header:In-Reply-To:1': 0.34; 'subject:that': 0.35; 'track': 0.35;
'files': 0.36; 'header:Received:8': 0.36; 'change': 0.36; 'those':
0.36; 'received:filterd': 0.37; 'received:pps.filterd': 0.37;
'using': 0.37; 'class': 0.37; 'url-ip:13.107/16': 0.38; 'file':
0.38; 'added': 0.39; 'date:': 0.39; 'list': 0.39; 'use': 0.39;
'fee': 0.39; 'still': 0.40; 'cash': 0.40; 'files.': 0.40;
'happen': 0.40; 'something': 0.40; 'want': 0.40; 'email.': 0.61;
'from:': 0.62; 'to:': 0.62; 'seen': 0.62; 'simply': 0.63; 'key':
0.64; 'accessing': 0.64; 'your': 0.64; 'upon': 0.64; 'parts':
0.65; 'named': 0.65; 'url-ip:104.18/16': 0.67; 'manually': 0.69;
'order.': 0.69; 'stock': 0.69; 'url-ip:52.6/16': 0.69; 'within':
0.69; 'records': 0.75; 'clicking': 0.76; 'combination': 0.76;
'domain': 0.77; 'field': 0.78; 'accounts,': 0.81; 'links.': 0.81;
'happens': 0.84; 'attention:': 0.84; 'email name:&lt;python-list':
0.84; 'funds,': 0.84; 'referenced': 0.84; 'skip:& 50': 0.84;
'subject:updated': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uchc.edu;
h=from : to : subject :
date : message-id : references : in-reply-to : content-type :
mime-version; s=UCHC03162020;
bh=5ZnmehtFm31aZ1Xl0ftAgFpi4pDXto7mIq0cfUwwSEU=;
b=M2AiB6nFICN1AONd/KerFQfeev6qk81i4HDrf5QA/avtafwYtdZfRZInFE3xehXc2oHm
A3v2ToHGgyq/KxUfPKzpz33qB6c9vIgYkl8+L0NbvBfxMyFbNeeblqxdeEZWjqZqNIXU
Kq9834tLyeUnlP81MeNz6RAvJJaxHjXepED7a0cbJ0ZHfEV4UcJLgrDolUAcO1785cXX
YTYI0vjazXEOsurISdnLgWb7PCkg/ulBO4/bo7UHoy4Al+Ky3ltfcy/Yo5RhlzGUhxr9
1A6/eOX95UqjRQ2d2tAsMF8L37IbhmYNNCmakp37IF7xa1+FcvUd6Z2MjnT2hsDM+6nz 9A==
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
b=Z6Ou4biiIRC7oDWnD5PeV573OjCj2IrT5UuDtj/nuaSDoT9DBwC2fuyEF68sziVR87DfmY4RaLSA/GM/gDdke/WHBVTxJJYy9gopmSeHtt2AbChYQlFgMffLIrhRgvOxyUhwD5bnsCLbWtV3MzE9t8Zp88Al84khMROhSg8DWj8j7eH6J8rFVUEYKSZrObO9eYgBT7wP8PlJ65Npj7KOEueL2PqlZ0ErEvamaj96p0KrKCqL5ACpavlWNemkrJDDc7jVtdvwYRfRklfl1qQdAQohGTLsy2zMEsUhuTOsgzzezdUAqlMHI5HZ9ktC8B4biCo1oTKg9l3YbDOz0u5bmA==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
s=arcselector9901;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1;
bh=5ZnmehtFm31aZ1Xl0ftAgFpi4pDXto7mIq0cfUwwSEU=;
b=Hw/csUuWIoaDbqR2cOlY0zyNskHEgQcwa8BTEDrUmxWChRa0TsLWc6zZRj6Il2dT5DOpzcrT/2r/mzHSQzyjsqrXb80FXZqrxAIPaX8n3Lpgy3cDzygKJ5qHOIlR23OmHJUUGPLKdF97F/xsDU6ymTVX5U6KPL3vHfXaM3kQHORa14/hHQJIKKW6FG0Y2PYb4fb+k0DsopC1oJ5mg9kN4OTBWhABGmsUT4ShB0wAEXyiAxOma9NGq8d+lD3A3dVXTBr/fswXvoJY3mxcrHJKcKopKU2KjuHZTUbk7rOfJbNDY26Otne3EQ904Yf7K9SHZxoJgcXWMad9QNpeSS6i+g==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass
smtp.mailfrom=uchc.edu; dmarc=pass action=none header.from=uchc.edu;
dkim=pass header.d=uchc.edu; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uchc.onmicrosoft.com;
s=selector2-uchc-onmicrosoft-com;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
bh=5ZnmehtFm31aZ1Xl0ftAgFpi4pDXto7mIq0cfUwwSEU=;
b=MdPVXgfGS5V1cnUbgDDZTtydC9RHFmwM4cl+TR0hM6C+BCZzzRY6Aq/4E1G1H1BH48hKgHGnwF9RcUtJaFQ999qutx6cuql6+PB1qbmwmzo9kR4nWQEn52G6DsKey/tp6rVp5YtV5yL5DXmx2YLl9Kh2RP2pQS2ph9P2FQgxIZQ=
Thread-Topic: Keeping a list of records with named fields that can be updated
Thread-Index: AQHZEDajb4Dk4xRPDUe2p6M2VyxGjK5u5/iN
In-Reply-To: <6gip6j-698.ln1@anthive.com>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
x-ms-publictraffictype: Email
x-ms-traffictypediagnostic: SA1PR14MB5855:EE_|CH2PR14MB3562:EE_
x-ms-office365-filtering-correlation-id: 35f8abde-5cbb-4856-1f53-08dade9c4fa2
x-ms-exchange-senderadcheck: 1
x-ms-exchange-antispam-relay: 0
x-microsoft-antispam: BCL:0;
x-microsoft-antispam-message-info: 9FTHgZfZEmPZ1EXVYqZ4ZnLsLMbAn4w64Irg6W5dwjz2ykBhyDrmWcyCTIVBkk2baYDkBefa6p99zqIMiB24HtpdSKrY+kRz40953dq+oeltVIR0k5fSoqxc7cBccSt+OWPjx/Mjkhk4gQUvZgaNtLP3tdc2hi2Tt/2hRvy0OaYmSWGOOmxbYorbM4Pj8laP+R5uVWcJps6QNP8gWcX3up4jdOpcPiLx/pWsGnLFAhQnxc5rZvaGPJ8lUgmSmfbyGeiT3flcLo4oRilvPb3o2q5yFDMKS/eOey5kCgoh+BLMi9oOMAzxN9EFa6J/0RkBQ5GpIwo3zcAxlrZxH5Mmgf6xW0l6nMui5BgyDIPPwj9g6JyW5M/GxEun7qA1AJL/xGPUp7q8oPrK2s7Vn5Kvlr5ugsEzZr9iaZ17Vc5PFP/6EJlvuDH4qBJOYbTIVmhDo6eGL4DdC+XkHkVhACqIO38eCcNLX/0xxWTYUZwnMS5YwCY5jEUz3mjGdXfN3pzANUvpQw1SjMIRgAjk95CnRinzopLDnclDQ1kPCUhWwX0+grpel4X/HxRPyWI7HbGD9xFonyF4T0K+ZP82MY5Px1AwIDtHoSNaxBrfj/D6kcd8S9GFTYHV/gjaSr+Wze98aLaeY7SinYdcZu0LgIdAnPp2RE1++UwF0SEQEkhvHbFNTiGoH01cMVmWgZZYbfHqwmBlPSmIUhD4qNBAeRvxMGwTARQQYQPU1PrTHxAE/vVz7rtGg51ceL4oqnhk9Cuo2Jsa6aveT0dlC2CpJNsbyQ==
x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:;
IPV:NLI; SFV:NSPM; H:SA1PR14MB5855.namprd14.prod.outlook.com; PTR:; CAT:NONE;
SFS:(13230022)(4636009)(366004)(376002)(346002)(396003)(136003)(39850400004)(451199015)(9686003)(83380400001)(186003)(7696005)(71200400001)(478600001)(966005)(26005)(6506007)(53546011)(55016003)(33656002)(166002)(38070700005)(86362001)(41320700001)(75432002)(122000001)(38100700002)(15650500001)(2906002)(110136005)(316002)(786003)(8676002)(66946007)(76116006)(5660300002)(66476007)(64756008)(66446008)(66556008)(41300700001)(91956017)(8936002)(52536014)(11970500018);
DIR:OUT; SFP:1101;
x-ms-exchange-antispam-messagedata-chunkcount: 1
x-ms-exchange-antispam-messagedata-0: kHjhOQYvOS3HUiGcoI1cozsX8mPe6tni9RF9qwXSyi1JNu0aqb5741Rne3fj
yDlcrgiL/m85gDWGRyDAMhdsjJiAoHSVCYk0mnTU7S26Mh3Qu+w4mPrwbZIH
BCPd9m8KAO2P67FK5WWwPdPZstxeCK7HNiwnQQGZK2pGMl/M/wAa3vzAu4QV
F3C1b7QfnhGLxeqjDO9bWFW7ao8zTeK3CqmIKFA1xn677QFW9ECRnx55hHQ/
AmDU8VOfmdrRz6n1gjlt7WJcieAkKdMQAOl3q/zmoqcsm0nCJtDQPvnOYtcJ
7LVuhhyM7UPIchwX1d4Z349bZjZ2dCoh18cvxMTqZPUyKE4vVP5PI+IUUu33
SFTOu/xq6+NF7IOqO40fCYWVqNMDFr/Ir4y+P7z8dmx5ZLGn8g12ZAd7qxi4
kVWXMipX81wZ0eYVmL2wPFp47n8pcnqPSfpgTFSMu9eDojfDmHZAUVxv5aC+
v/K/ogei5OyZQXpAqYQgtRip2FWi5TBP6N7ITcClYYVmgBYL3SzHvB2QZnuC
jOBzyTjKdvjiiqlY0hzKPQWDSKLkRU1xqyxZcCnGyF50eXLI42iQVoND7OHy
E7dAa/nik7+G69oeD7mOsTXkSiko0UERzEaJndXPDIUbx6AJ/BMAArlYFbpv
GGbQVR4NR8qBUYpk48E8RhIzylTXV5kx0Vk3LX7eOJKpGMi0g8VBf01Kh59c
JALvN5AfQN4isY33s1b+dpzMzOTlDpSHCS5H3xiFltNh8CP75SyoGCS1GZn6
bWYc09zwyMe2w1YnRyMdEvaB7iBRFDMSzvFn9XdnlXy40K9p/lxjSy1T0bXt
Jw8DytDqwLeKcv9XfWd3Cfy1h+P1w9w2O2gf6IqUXyOgmoiivpKyMI43XO8F
la6LzIDxdBlg7yuJeJESd9Ba1Fzn3qmROxJTdR7xhgnkYdhFZ5pLTael57c4
LtuCZ44YXjK2A6+ICNc4H4x9m7vmj+ON2vppRLeXYq2Dso5RAAwEy6Laks/4
FwlOmkZ+ovT3TCvrlFHQ6whsJbu2Snzc7WkQRhOhKFJEyI+u+QB1eCCzkVvC
5DBS21cyN+nUNo6WjYG1Oj1FvZUN/xJ1duonA5xHaby66M/LPre+J4Jhz1dz
uxz2nTR9v4ucpu1mimvZNC0vSWl8mQXfEKto66RMeg3kp1eoaGO/AcVUUGM0
C0s5tb3v7Q/HFbOcPG319VJghVFEc2k6SPMamjrE88RU4RPyf+mSEyfr/xMz
w2DaoEk3oUqs+7/5lzOfxYU/Yk0FOS11rUTT17XHRWttXz7GqkE7yPApXvPP
bA0u9BRoo9czgYvpqhOpardXRHEhTNWcmTpF5h1DY2Dh14/JRivJqrDJPDhC
N/jYUfIcqXZFUCfEPglbACgrWLn0dYcFcgerW+1kYFoZyDAwOljSqz7ssbkE
sBQJ8ZER6pKliyTJkXdpWziDzj8CILbbHQZP4jJ62NH3MYlDmHqkYhrDzt1P
WIhMEfIuX585Qp+yqcsokdzmKyN4ytrRAvJocdq027mhvbQQ+DEpzX7fC9/i
4AbdmnKQyAM/X7V7Chy6bz57wFylTKeKaydE/fFP+fZjgqDu+ZtLQyptGmrC
TA==
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-AuthSource: SA1PR14MB5855.namprd14.prod.outlook.com
X-MS-Exchange-CrossTenant-Network-Message-Id: 35f8abde-5cbb-4856-1f53-08dade9c4fa2
X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Dec 2022 13:00:15.8479 (UTC)
X-MS-Exchange-CrossTenant-fromentityheader: Hosted
X-MS-Exchange-CrossTenant-id: 5c82d83a-818a-4c16-b540-ded2344a7ad3
X-MS-Exchange-CrossTenant-mailboxtype: HOSTED
X-MS-Exchange-CrossTenant-userprincipalname: MQIOeR/TiTubRm7pPXsq/BLSFwPgjQJN6YvAW8U0WOe1tjuWWDCKld2Qe4/gmyKZYxG70x98AE/M2D+DoWU3NA==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR14MB3562
X-Proofpoint-GUID: _8aTR1GAJdnyyt61lz-Wwm9ZtdMKN7_X
X-Proofpoint-ORIG-GUID: _8aTR1GAJdnyyt61lz-Wwm9ZtdMKN7_X
X-Proofpoint-Virus-Version: vendor=baseguard
engine=ICAP:2.0.205,Aquarius:18.0.923,Hydra:6.0.545,FMLib:17.11.122.1
definitions=2022-12-15_07,2022-12-15_02,2022-06-22_01
X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0
malwarescore=0
lowpriorityscore=0 phishscore=0 priorityscore=1501 impostorscore=0
adultscore=0 bulkscore=0 suspectscore=0 clxscore=1011 mlxscore=0
spamscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx
scancount=1 engine=8.12.0-2212070000 definitions=main-2212150106
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <SA1PR14MB5855FCF33DDDBA034167958CB9E19@SA1PR14MB5855.namprd14.prod.outlook.com>
X-Mailman-Original-References: <6gip6j-698.ln1@anthive.com>
 by: Weatherby,Gerard - Thu, 15 Dec 2022 13:00 UTC

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.edu@python.org> on behalf of songbird <songbird@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$>

Re: Keeping a list of records with named fields that can be updated

<5qkr6j-dk2.ln1@anthive.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=25115&group=comp.lang.python#25115

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: songbird@anthive.com (songbird)
Newsgroups: comp.lang.python
Subject: Re: Keeping a list of records with named fields that can be updated
Date: Thu, 15 Dec 2022 08:41:57 -0500
Organization: the little wild kingdom
Lines: 38
Message-ID: <5qkr6j-dk2.ln1@anthive.com>
References: <6gip6j-698.ln1@anthive.com>
<9158537c-f561-e666-f60c-34d50f61d7b7@web.de>
<mailman.1175.1671096336.20444.python-list@python.org>
Reply-To: songbird <songbird@anthive.com>
Injection-Info: reader01.eternal-september.org; posting-host="8eb19f924c22c8e7ed114fa8931bc585";
logging-data="3265167"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+x1u0gAn041Go/2YqoHXqkkEkY11LkT/U="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:tLXMeMWM6pTQPXhvI+2uHZtVGZE=
 by: songbird - Thu, 15 Dec 2022 13:41 UTC

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:

the files are coming from the web site that stores the
accounts. i already have manually created files from many
years ago with some of the same information but to go back
and reformat all of those would be a lot of work. it is
much easier to just take the files as supplied and process
them if i can do that instead.

i do know database stuff well enough but this is fairly
simple math and i'd like to avoid creating yet another
copy in yet another format to have to deal with.

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

thanks, i'll give these a try. :)

songbird

Re: Keeping a list of records with named fields that can be updated

<27pnL.1963315$WRz3.1232514@fx03.ams4>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=25150&group=comp.lang.python#25150

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx03.ams4.POSTED!not-for-mail
From: gilmeh.serda@nothing.here.invalid (Gilmeh Serda)
Subject: Re: Keeping a list of records with named fields that can be updated
Newsgroups: comp.lang.python
References: <6gip6j-698.ln1@anthive.com>
MIME-Version: 1.0
x-no-archive: yes
User-Agent: Pan/0.153 (Mariupol; c5405f5)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Lines: 15
Message-ID: <27pnL.1963315$WRz3.1232514@fx03.ams4>
X-Complaints-To: abuse@easynews.com
Organization: Easynews - www.easynews.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Sat, 17 Dec 2022 19:53:34 GMT
X-Received-Bytes: 1241
 by: Gilmeh Serda - Sat, 17 Dec 2022 19:53 UTC

On Wed, 14 Dec 2022 13:50:14 -0500, songbird wrote:

> Suggestions?

Move it to SQLite. Most likely easier to deal with table integrity, like
differences between types. And it's probably faster, too.

You can always export to csv later.

--
Gilmeh

The most important design issue... is the fact that Linux is supposed to
be fun... -- Linus Torvalds at the First Dutch International Symposium on
Linux

Re: Keeping a list of records with named fields that can be updated

<i3p37j-5c2.ln1@anthive.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=25172&group=comp.lang.python#25172

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: songbird@anthive.com (songbird)
Newsgroups: comp.lang.python
Subject: Re: Keeping a list of records with named fields that can be updated
Date: Sun, 18 Dec 2022 10:44:18 -0500
Organization: the little wild kingdom
Lines: 42
Message-ID: <i3p37j-5c2.ln1@anthive.com>
References: <6gip6j-698.ln1@anthive.com>
<9158537c-f561-e666-f60c-34d50f61d7b7@web.de>
<mailman.1175.1671096336.20444.python-list@python.org>
Reply-To: songbird <songbird@anthive.com>
Injection-Info: reader01.eternal-september.org; posting-host="74ffa83ad2b8eba2ecc654e79a3fa103";
logging-data="17196"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19/n+CpvRteuqwh9ZvffdtXoi/feafPjuM="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:ugxdh6FQXr1h6kHI9VtELDWd1cg=
 by: songbird - Sun, 18 Dec 2022 15:44 UTC

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

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

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.

songbird

Re: Keeping a list of records with named fields that can be updated

<mailman.1264.1671474780.20444.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=25223&group=comp.lang.python#25223

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: __peter__@web.de (Peter Otten)
Newsgroups: comp.lang.python
Subject: Re: Keeping a list of records with named fields that can be updated
Date: Mon, 19 Dec 2022 19:29:20 +0100
Lines: 62
Message-ID: <mailman.1264.1671474780.20444.python-list@python.org>
References: <6gip6j-698.ln1@anthive.com>
<9158537c-f561-e666-f60c-34d50f61d7b7@web.de>
<mailman.1175.1671096336.20444.python-list@python.org>
<i3p37j-5c2.ln1@anthive.com>
<2ede4582-0bd9-fec3-421d-24239e2eb144@web.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de gfIArv2y8HsFOafdW9TrAQPsuW0a8dO1deSMDnMyse/g==
Return-Path: <__peter__@web.de>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=web.de header.i=@web.de header.b=WRhsW/wd; dkim-adsp=pass;
dkim-atps=neutral
X-Spam-Status: OK 0.006
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'csv': 0.03; 'bunch': 0.05;
'row': 0.05; 'usage': 0.05; 'received:212.227': 0.07; "hasn't":
0.09; 'myself.': 0.09; 'subject:list': 0.11; 'import': 0.15;
'>>>>>': 0.16; 'both.': 0.16; 'conversion': 0.16;
'from:addr:web.de': 0.16; 'message-id:@web.de': 0.16;
'received:212.227.15': 0.16; 'received:mout.web.de': 0.16;
'received:web.de': 0.16; 'songbird': 0.16; 'specify': 0.16;
'subject:named': 0.16; 'turns': 0.16; 'understand.': 0.16;
'wrote:': 0.16; 'instead': 0.17; 'to:addr:python-list': 0.20;
'version': 0.23; 'received:de': 0.23; "i'd": 0.24; 'bit': 0.27;
'>>>': 0.28; 'header:User-Agent:1': 0.30; 'think': 0.32;
'manner.': 0.32; 'but': 0.32; 'there': 0.33; 'same': 0.34; 'header
:In-Reply-To:1': 0.34; 'bar': 0.35; 'handling': 0.35;
'subject:that': 0.35; 'files': 0.36; 'really': 0.37; "it's": 0.37;
'received:192.168': 0.37; 'way': 0.38; 'handle': 0.39; 'use':
0.39; 'done.': 0.40; 'reference': 0.60; 'initial': 0.61; 'seen':
0.62; 'received:212': 0.62; 'skip:m 20': 0.63; 'skip:b 10': 0.63;
'skip:r 20': 0.64; 'your': 0.64; 'order': 0.69; 'change.': 0.69;
'direct': 0.73; 'easy': 0.74; 'records': 0.75; 'field': 0.78;
'database': 0.80; 'one:': 0.84; 'subject:updated': 0.84; 'of:':
0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=s29768273;
t=1671474779; bh=W2RXPIgGcAr360NbL6HKDq51CPVriJGD9nyln+ApR64=;
h=X-UI-Sender-Class:Date:Subject:To:References:From:In-Reply-To;
b=WRhsW/wdHd8At4EAhe/XK2XBEntr8w6Z0C8B5INH1jTBqlGg2/p8NCFH5A/md6l/+
Qfc6h7vhNXCS5QjmCVzsZuX2Zs4y3QebTGf3ntViK+vz0c4gSmYqcHtkkFsRSzpo+s
RZPmomBusxIr51Wie6yPENPewF+SBcas3x4GY9PFZ9SH1jt/iQIPwS0lZF5B5+W38K
u1KLBeK8QWAQbYBoZcmpKyH+igahJbfKn9s/ul3Hej5oN1GX8dfw3vJZCqtFgVfX7F
xqqYKeg2Dp1c7KT1MEjyOePhFnjUNQoBKpaBVEgPBb8u0OsqXgQcXGPXYXImFiQce5
KTYtimURqTgrw==
X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6
User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101
Thunderbird/102.6.0
Content-Language: en-US
In-Reply-To: <i3p37j-5c2.ln1@anthive.com>
X-Provags-ID: V03:K1:MU3ctodFxXV3/k5IdVon/nCtUI5/yYY9K8LoheiKfcxYmUGXivM
pzJvJcNZkrEiv+nZ2H1XfrZlEf/vOlfSLW09TOG6hOW5x5BNNx0yN3eExpojbyAL3MqeUm9
hXc1P7wqdLwUFPwvQ0ZP2hfTfOkMCL4FluCg1oSZ/q/ncbZsVBffuTU0RgBOdaZ3ya2FFax
dGmMoOY7vBUlA7eqMIlBw==
X-Spam-Flag: NO
UI-OutboundReport: notjunk:1;M01:P0:MZCLSZPNFJ0=;CTkG8Tcd/ordBOutlrsePt6oYUG
iXq0/2HMQEitM5ogxXrU6OgepYYkob6suwKhVXwb6ea7ebv0CBbUW8iRzzWUNXtkEKnitUuPS
6q86V9AGRnNSteoCHl7eMvQTpLAwxUgHzVFUkc15lsrSnyKBYznP8xabRcIfUNMm80SVTnZkr
RGnZyjyWPEZor/i+cawkZ2hXfAQVK0JvOeklRaVhsgNJeIYaxh5BhfYSgMrRQhmJWTesUhQNh
ma3PPXABVypOLBpnrWl04c/c+mh0tpoVTM8jLT7ydJVZskMMbiRUHeDZL8Wnz6MkajtpcnpHd
j/9YfM1KuykoJ71LzuOD4i/ztth8xv5T2jeueKeCqThjybeN3UY8Cha8RZ+Ki8PmxHS4005ld
7SfJPQpxsgdNO62a78wEzcwo0rXPCD+DHNrWMzwWup1Gk3gyUi05ccV8Fonyik/VTe74mS53x
RObUn/04Vto1CLwoy27RTIyGo55slW0y4WPXqk1nuMxn0jVtMXxtweZ/jjy4q7230zWN4pGCp
Za+x/WD2NWDDyTXqyPRgshyoyiO8CbsfNK/VCXZXHhiN6PvTp5f5IkBSt1gzhgjlUQmaxhEJY
a031fTGYqTU0K1g+UPuocrkfI0vzWRJrfPHgXVX2iok/CDT5KXlTGg79nRz7/xG3zrg+wx8KJ
aSIhBquTz2mnzEbvg7QOD3mgz9nq9sImHSq6k6OrjuI659q3B8JvLu1ZSxi2pxANt4bvccuQe
3WdF4ZniwEJRLNRPRinKyC12ftKy23FwnqSEYRczVmHx0XgKw7XYXXOmVgJ/hO5n1yTwbXEJ0
eMk+ODtzRsgE6rUGKpqIAwxhS8fIHUq8zb5RSSfI73qnqFsACOZO5uy5rUj2wRZAEJ5FGgP3g
kVk2HxUXZJo+cXPVnKTYoJx9pejy0EFO/3GkA5NUTruUkTeXx4bKyvFeaRwzIBAJLPzDh+VnS
o456UpDW+/hgPTu5FVbCTADHynI=
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <2ede4582-0bd9-fec3-421d-24239e2eb144@web.de>
X-Mailman-Original-References: <6gip6j-698.ln1@anthive.com>
<9158537c-f561-e666-f60c-34d50f61d7b7@web.de>
<mailman.1175.1671096336.20444.python-list@python.org>
<i3p37j-5c2.ln1@anthive.com>
 by: Peter Otten - Mon, 19 Dec 2022 18:29 UTC

On 18/12/2022 16:44, songbird wrote:
> 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:
>>
>>>>> 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)
>
> 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:
>>
>>>>> 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)
>
> 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.

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 = NTRow(1, "two", 3.0)
>>> ntrow
NTRow(alpha=1, beta='two', gamma=3.0)

Convert to dataclass:

>>> dcrow = DCRow(*ntrow)
>>> dcrow
DCRow(alpha=1, beta='two', gamma=3.0)

Re: Keeping a list of records with named fields that can be updated

<q8q67j-uob.ln1@anthive.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=25226&group=comp.lang.python#25226

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: songbird@anthive.com (songbird)
Newsgroups: comp.lang.python
Subject: Re: Keeping a list of records with named fields that can be updated
Date: Mon, 19 Dec 2022 14:22:34 -0500
Organization: the little wild kingdom
Lines: 32
Message-ID: <q8q67j-uob.ln1@anthive.com>
References: <6gip6j-698.ln1@anthive.com>
<9158537c-f561-e666-f60c-34d50f61d7b7@web.de>
<mailman.1175.1671096336.20444.python-list@python.org>
<i3p37j-5c2.ln1@anthive.com> <2ede4582-0bd9-fec3-421d-24239e2eb144@web.de>
<mailman.1264.1671474780.20444.python-list@python.org>
Reply-To: songbird <songbird@anthive.com>
Injection-Info: reader01.eternal-september.org; posting-host="df2f97b2a2360b3fc7313356371ad6af";
logging-data="459874"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/KjilUOMwQohukAzseR8y/X88tCSQZgSY="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:t/An2JVSScKMqJ+8WscVxDiONs8=
 by: songbird - Mon, 19 Dec 2022 19:22 UTC

Peter Otten wrote:
....
> 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 = NTRow(1, "two", 3.0)
> >>> ntrow
> NTRow(alpha=1, beta='two', gamma=3.0)
>
> Convert to dataclass:
>
> >>> dcrow = DCRow(*ntrow)
> >>> dcrow
> DCRow(alpha=1, beta='two', gamma=3.0)

thanks, once i get the data in from the file i only have
to reference it, but for the rest of the code i can use
the dataclass instead and that will be easier to read than
dicts. :)

your help is appreciated. :)

songbird


devel / comp.lang.python / Re: Keeping a list of records with named fields that can be updated

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor