incompatible interface with python dictionary.

263 views
Skip to first unread message

korr...@gmail.com

unread,
May 15, 2013, 3:53:45 AM5/15/13
to python...@googlegroups.com
Debian wheezy package python-dbf 0.88.16-1.

I tried to load dbf to database and convert field types like this:

def row_converter(row, fields):
d = {}
for name in fields:
v = row.get(name, None)
#futher converting field data
...

return d

dbf throws FieldMissing if field doesn't exists in DBF-file.

Traceback (most recent call last):
File "./load_rests.py", line 66, in row_converter
v = row.get(name, None)
File "/usr/lib/pymodules/python2.7/dbf/tables.py", line 212, in __getattr__
raise FieldMissing(name)

It's incompatible behaviour of get with python dictionaries.
IMHO it's not good.
Is it already fixed?
If not, can you fix this in next versions, please?

Ethan Furman

unread,
May 20, 2013, 7:40:47 AM5/20/13
to python...@googlegroups.com
On 05/15/2013 12:53 AM, korr...@gmail.com wrote:
> Debian wheezy package python-dbf 0.88.16-1.
>
> I tried to load dbf to database and convert field types like this:
>
> def row_converter(row, fields):
> d = {}
> for name in fields:
> v = row.get(name, None)

Apologies for the delay in responding.

The problem is that a row is not a dictionary, and has none of the dictionary helper methods. It does allow for item
access, like a dict does, but it also allows for index access, like a list does (it's also not a list, though).

The bigger issue is that any helper method (get, setdefault, copy, etc.) is a potential field name, and if the method
exists the field name cannot.

So, no, I won't be adding get, or any of the other methods, to rows.

If you know the fields exist in the row, you can do this:

--> def row_converter(row, fields):
... d = {}
... for name in fields:
... v = row[name]

and if you're not sure the field exists:

--> def row_converter(row, fields):
... d = {}
... existing_fields = dbf.field_names(row)
... fields = [fld for fld in fields if fld in existing_fields]
... for name in fields:
... v = row[name]


You may want to upgrade to the latest version, but be aware there are significant, backwards-incompatible, changes in
the current version.

--
~Ethan~
Reply all
Reply to author
Forward
0 new messages