Cleaning up conditionals

Deborah Swanson python at deborahswanson.net
Tue Jan 3 02:35:31 EST 2017


Gregory Ewing wrote, on January 02, 2017 7:58 PM
> 
> Deborah Swanson wrote:
> > Unless you know of, and are suggesting, a way to index a 
> sequence with 
> > strings instead of integers, so the code could remain 
> untouched with 
> > string indices when changes to the columns are made.
> 
> I'm talking about this:
> 
>https://docs.python.org/3/library/collections.html#collections.namedtup
le

>It's like a tuple, but the fields also have names, so
>you can access them like attributes using dot-notation.

> Also, for such a scheme to be superior to what I already have, it 
> needs to be very nearly self-maintaining.

>If you derive the attribute names from the header row,
>it would be extremely self-maintaining. You would be able
>to reorder columns without touching the code at all.

>-- 
>Greg

I've never worked with collections before, and you're the second person
on this thread to suggest using them. I'm very intrigued by both your
use of collections and Peter Otten's.

I think I'm right that core python sequences can't be indexed in any
fashion by strings, because strings aren't iterable. I suppose it might
be possible for strings to be iterable in some sort of ascii char code
order, but that seems like it could get real whacky very fast, and not
terribly useful.

But a library of functions designed to augment core python certainly
would be capable of many things.  So I will be checking out collections
and your suggested use of namedtuples very soon. I may not need to fix
this part of my code, but I would like to learn a new way to do it. And
it would be nice to replace the long string of 2-letter variables I
currently have with a data structure. They're not really in the way, but
they do clutter up the variable list in PyCharm. And using the header
row titles as indices would automatically make my code a lot more
readable.

Thanks!  ;)
D.




More information about the Python-list mailing list