Toblerity / Fiona

Fiona reads and writes geographic data files
https://fiona.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.14k stars 201 forks source link

Give model classes a nice repr again #1380

Closed sgillies closed 2 months ago

sgillies commented 3 months ago

Resolves #1379

sgillies commented 3 months ago

@loicdtx @mwtoews what do you think of this?

sgillies commented 3 months ago

@mwtoews thank you! I'd like to keep the fiona package name in there for the sake of disambiguation, thinking that in some cases the people that see these will not have intentionally imported fiona, but are getting it second-hand.

As to moving the use of reprlib.repr() to the base class, I found that it, by default, shortens representations too much. I'm finding it useful for shortening representation of geometry coordinates. It might be useful for shortening very long mappings of properties... I'll try that out.

mwtoews commented 3 months ago

@sgillies it's fine to keep fiona. in the repr, so my suggestion would only need to adjust with return "fiona.{}({})".format(self.__class__.__name__, ", ".join(kvs)).

As for shortening other instances of Object, I wasn't suggesting this. But I suppose there could be instances of datasets with many fields or with individual fields with "large" blobs or text attributes.

sgillies commented 3 months ago

@mwtoews thank you for the suggestions!

loicdtx commented 3 months ago

Thanks @sgillies , that's definitely an improvement. I tested it on tests/data/gre.shp and it looks great overall. Two minor observations:

>>> import fiona
>>> from pprint import pprint
>>> fc = fiona.open('tests/data/gre.shp')
>>> fc[0]
fiona.Feature(geometry=fiona.Geometry(coordinates=[[(-61.173214300000005, 12.516654800000001), ...]], type='Polygon'), id='0', properties=fiona.Properties(flag='http://upload.wikimedia.org/wikipedia/commons/b/bc/Flag_of_Grenada.svg', name='Grenada', name_cs='Grenada', name_de='Grenada', name_en='Grenada', name_eo='Grenado', name_fr='Grenade', name_fy='Grenada', name_hr='Grenada', name_nl='Grenada', name_ru='Гренада', name_sl='Grenada', name_ta='கிரெனடா', name_uk='Гренада', boundary='administrative', name_tzl='Grenada', timezone='America/Grenada', wikidata='Q769', ISO3166-1='GD', wikipedia='en:Grenada', admin_leve='2', is_in_cont='North America', ISO3166-1_='GD', ISO3166-_1='GRD', ISO3166-_2='308'))

>>> pprint(fc[0])
fiona.Feature(geometry=fiona.Geometry(coordinates=[[(-61.173214300000005, 12.516654800000001), ...]], type='Polygon'), id='0', properties=fiona.Properties(flag='http://upload.wikimedia.org/wikipedia/commons/b/bc/Flag_of_Grenada.svg', name='Grenada', name_cs='Grenada', name_de='Grenada', name_en='Grenada', name_eo='Grenado', name_fr='Grenade', name_fy='Grenada', name_hr='Grenada', name_nl='Grenada', name_ru='Гренада', name_sl='Grenada', name_ta='கிரெனடா', name_uk='Гренада', boundary='administrative', name_tzl='Grenada', timezone='America/Grenada', wikidata='Q769', ISO3166-1='GD', wikipedia='en:Grenada', admin_leve='2', is_in_cont='North America', ISO3166-1_='GD', ISO3166-_1='GRD', ISO3166-_2='308'))

>>> pprint(dict(fc[0]['properties']))
{'ISO3166-1': 'GD',
 'ISO3166-1_': 'GD',
 'ISO3166-_1': 'GRD',
 'ISO3166-_2': '308',
 'admin_leve': '2',
 'boundary': 'administrative',
 'flag': 'http://upload.wikimedia.org/wikipedia/commons/b/bc/Flag_of_Grenada.svg',
 'is_in_cont': 'North America',
 'name': 'Grenada',
 'name_cs': 'Grenada',
 'name_de': 'Grenada',
 'name_en': 'Grenada',
 'name_eo': 'Grenado',
 'name_fr': 'Grenade',
 'name_fy': 'Grenada',
 'name_hr': 'Grenada',
 'name_nl': 'Grenada',
 'name_ru': 'Гренада',
 'name_sl': 'Grenada',
 'name_ta': 'கிரெனடா',
 'name_tzl': 'Grenada',
 'name_uk': 'Гренада',
 'timezone': 'America/Grenada',
 'wikidata': 'Q769',
 'wikipedia': 'en:Grenada'}

>>> pprint(dict(fc[0]))
{'geometry': fiona.Geometry(coordinates=[[(-61.173214300000005, 12.516654800000001), ...]], type='Polygon'),
 'id': '0',
 'properties': fiona.Properties(flag='http://upload.wikimedia.org/wikipedia/commons/b/bc/Flag_of_Grenada.svg', name='Grenada', name_cs='Grenada', name_de='Grenada', name_en='Grenada', name_eo='Grenado', name_fr='Grenade', name_fy='Grenada', name_hr='Grenada', name_nl='Grenada', name_ru='Гренада', name_sl='Grenada', name_ta='கிரெனடா', name_uk='Гренада', boundary='administrative', name_tzl='Grenada', timezone='America/Grenada', wikidata='Q769', ISO3166-1='GD', wikipedia='en:Grenada', admin_leve='2', is_in_cont='North America', ISO3166-1_='GD', ISO3166-_1='GRD', ISO3166-_2='308')}
sgillies commented 2 months ago

@loicdtx thank you for pointing out the issue with dict(). I've addressed that in the latest commits, you'll now get dicts all the way down.