Namedtuples: some unexpected inconveniences

Deborah Swanson python at deborahswanson.net
Fri Apr 14 15:34:52 EDT 2017


Peter,

Retracing my steps to rewrite the getattr(row, label) code, this is what
sent me down the rabbit hole in the first place. (I changed your 'rows'
to 'records' just to use the same name everywhere, but all else is the
same as you gave me.) I'd like you to look at it and see if you still
think complete(group, label) should work. Perhaps seeing why it fails
will clarify some of the difficulties I'm having.

I ran into problems with values and has_empty. values has a problem
because 
row[label] gets a TypeError. has_empty has a problem because a list of
field values will be shorter with missing values than a full list, but a
namedtuple with missing values will be the same length as a full
namedtuple since missing values have '' placeholders.  Two more
unexpected inconveniences. 

A short test csv is at the end, for you to read in and attempt to
execute the following code, and I'm still working on reconstructing the
lost getattr(row, label) code.

import csv
from collections import namedtuple, defaultdict

def get_title(row):
    return row.title

def complete(group, label):
    values = {row[label] for row in group}  
    # get "TypeError: tuple indices must be integers, not str"
    has_empty = not min(values, key=len)
    if len(values) - has_empty != 1:
        # no value or multiple values; manual intervention needed
        return False
    elif has_empty:
        for row in group:
            row[label] = max(values, key=len)
    return True

infile = open("E:\\Coding projects\\Pycharm\\Moving\\Moving 2017 in -
test.csv")
rows = csv.reader(infile)
fieldnames = next(rows)
Record = namedtuple("Record", fieldnames)
records = [Record._make(fieldnames)]
records.extend(Record._make(row) for row in rows)

# group rows by title
groups = defaultdict(list)
for row in records:
    groups[get_title(row)].append(row)

LABELS = ['Location', 'Kind', 'Notes']

# add missing values
for group in groups.values():
    for label in LABELS:
        complete(group, label)

Moving 2017 in - test.csv:
(If this doesn't come through the mail system correctly, I've also
uploaded the file to
http://deborahswanson.net/python/Moving%202017%20in%20-%20test.csv.
Permissions should be set correctly, but let me know if you run into
problems downloading the file.)


CLDesc,url,title,Description,Location,ST,co,miles,Kind,Rent,Date,first,b
r,Notes,yesno,mark,arc
Jan 3 1 bedroom/1 bath mobile $700 1br - (Mount
Vernon),http://skagit.craigslist.org/apa/5943737902.html,1 bedroom/1
bath mobile $700 1br - (Mount Vernon) - (Mount Vernon),1 bedroom/1 bath
mobile $700 1br,Mount Vernon,WA,sk,,trailer,700,1/3/2017,1/3/2017,1,no
smoking,,,deleted by its author
Jan 6 1 bedroom/1 bath mobile $700 1br - (Mount
Vernon),http://skagit.craigslist.org/apa/5943737902.html,1 bedroom/1
bath mobile $700 1br - (Mount Vernon) - (Mount Vernon),1 bedroom/1 bath
mobile $700 1br,,WA,,,trailer,700,1/6/2017,1/3/2017,1,no smoking,,,
Jan 10 1 bedroom/1 bath mobile $700 1br - (Mount
Vernon),http://skagit.craigslist.org/apa/5943737902.html,1 bedroom/1
bath mobile $700 1br - (Mount Vernon) - (Mount Vernon),1 bedroom/1 bath
mobile $700 1br,,,,,,700,1/10/2017,1/3/2017,1,,,,
Jan 17 1 bedroom/1 bath mobile $700 1br - (Mount
Vernon),http://skagit.craigslist.org/apa/5943737902.html,1 bedroom/1
bath mobile $700 1br - (Mount Vernon) - (Mount Vernon),1 bedroom/1 bath
mobile $700 1br,Mount Vernon,WA,,,trailer,700,1/17/2017,1/3/2017,1,no
smoking,,,
Jan 19 1 bedroom/1 bath mobile $700 1br - (Mount
Vernon),http://skagit.craigslist.org/apa/5943737902.html,1 bedroom/1
bath mobile $700 1br - (Mount Vernon) - (Mount Vernon),1 bedroom/1 bath
mobile $700 1br,Mount Vernon,WA,,,trailer,700,1/19/2017,1/3/2017,1,no
smoking,,,
Jan 26 1240 8th Avenue $725 2br - 676ft2 -
(Longview),http://portland.craigslist.org/clk/apa/5976442500.html,1240
8th Avenue $725 2br - 676ft2 - (Longview),1240 8th Avenue $725 2br -
676ft2,,,,,,725,1/26/2017,1/16/2017,2,,,,
Jan 16 1240 8th Avenue $725 2br - 676ft2 -
(Longview),http://portland.craigslist.org/clk/apa/5961794305.html,1240
8th Avenue $725 2br - 676ft2 - (Longview) - (Longview),1240 8th Avenue
$725 2br -
676ft2,Longview,WA,,,house,725,1/16/2017,1/16/2017,2,"detached garage,
w/d hookups",,,
Jan 6 1424 California Avenue $750 2br - 1113ft2 - (Klamath
Falls),http://klamath.craigslist.org/apa/5947977083.html,1424 California
Avenue $750 2br - 1113ft2 - (Klamath Falls) - (Klamath Falls),1424
California Avenue $750 2br - 1113ft2,Klamath
Falls,OR,kl,,house,750,1/6/2017,1/6/2017,2,no smoking,,,
Jan 11 1424 California Avenue $750 2br - 1113ft2 - (Klamath
Falls),http://klamath.craigslist.org/apa/5947977083.html,1424 California
Avenue $750 2br - 1113ft2 - (Klamath Falls) - (Klamath Falls),1424
California Avenue $750 2br -
1113ft2,,OR,kl,,house,750,1/11/2017,1/6/2017,2,no smoking,,,
"Jan 3 1838 Alma Drive Kelso, WA 98626 $550 1br - 600ft2 - (1838 Alma
Drive Kelso,
WA)",http://portland.craigslist.org/clk/apa/5937961608.html,"1838 Alma
Drive Kelso, WA 98626 $550 1br - 600ft2","1838 Alma Drive Kelso, WA
98626 $550 1br - 600ft2",Kelso,WA,ck,,house,550,1/3/2017,12/29/2016,1,no
smoking,,,
"Jan 5 1838 Alma Drive Kelso, WA 98626 $550 1br - 600ft2 - (1838 Alma
Drive Kelso,
WA",http://portland.craigslist.org/clk/apa/5937961608.html,"1838 Alma
Drive Kelso, WA 98626 $550 1br - 600ft2","1838 Alma Drive Kelso, WA
98626 $550 1br - 600ft2",,,,,,550,1/5/2017,12/29/2016,1,,,,
"Jan 9 1838 Alma Drive Kelso, WA 98626 $550 1br - 600ft2 - (1838 Alma
Drive Kelso,
WA)",http://portland.craigslist.org/clk/apa/5937961608.html,"1838 Alma
Drive Kelso, WA 98626 $550 1br - 600ft2","1838 Alma Drive Kelso, WA
98626 $550 1br - 600ft2",,,,house,550,1/9/2017,12/29/2016,1,no
smoking,,,,
"Jan 12 1838 Alma Drive Kelso, WA 98626 $550 1br - 600ft2 - (1838 Alma
Drive Kelso,
WA)",http://portland.craigslist.org/clk/apa/5937961608.html,"1838 Alma
Drive Kelso, WA 98626 $550 1br - 600ft2","1838 Alma Drive Kelso, WA
98626 $550 1br - 600ft2",Kelso,WA,,,house,550,1/12/2017,12/29/2016,1,no
smoking,,,
"Jan 17 1838 Alma Drive Kelso, WA 98626 $550 1br - 600ft2 - (1838 Alma
Drive Kelso, WA) pic
map",http://portland.craigslist.org/clk/apa/5937961608.html,"1838 Alma
Drive Kelso, WA 98626 $550 1br - 600ft2","1838 Alma Drive Kelso, WA
98626 $550 1br - 600ft2",Kelso,WA,,,house,550,1/17/2017,12/29/2016,1,no
smoking,,,
"Jan 19 1838 Alma Drive Kelso, WA 98626 $550 1br - 600ft2 - (1838 Alma
Drive Kelso, WA) pic
map",http://portland.craigslist.org/clk/apa/5937961608.html,"1838 Alma
Drive Kelso, WA 98626 $550 1br - 600ft2","1838 Alma Drive Kelso, WA
98626 $550 1br - 600ft2",,,,house,550,1/19/2017,12/29/2016,1,no
smoking,,,,
Jan 9 1907 Olympic Hwy N #22 - Shelton Home for Rent $750 2br -
(Shelton),http://seattle.craigslist.org/oly/apa/5916473104.html,1907
Olymwy N #22 - Shelton Home for Rent $750 2br,1907 Olymwy N #22 -
Shelton Home for Rent $750 2br,,,,mfgd,750,1/9/2017,12/5/2016,2,carport
and storage shed,,,,
Jan 16 1907 Olympic Hwy N #22 - Shelton Home for Rent $750 2br -
(Shelton) pic map
,http://seattle.craigslist.org/oly/apa/5961587289.html,1907 Olymwy N #22
- Shelton Home for Rent $750 2br,1907 Olymwy N #22 - Shelton Home for
Rent $750 2br,Shelton,WA,ma,,mfgd,750,1/16/2017,12/5/2016,2,carport and
storage shed,,,



More information about the Python-list mailing list