-
Notifications
You must be signed in to change notification settings - Fork 213
Give model classes a nice repr again #1380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
fiona/model.py
Outdated
| from fiona.errors import FionaDeprecationWarning | ||
|
|
||
| _coord_repr = reprlib.Repr() | ||
| _coord_repr.maxlist = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For real-world features, the coordinates are likely to make up most of the representation. Showing 20+ can easily overflow displays. One is enough to see where the feature is in space, roughly. 6 would be reprlib's default, but that's still a lot of characters if we're dealing with small scale, projected features.
| geometry=Geometry(type="LineString", coordinates=[(0, 0)] * 100), | ||
| properties=Properties(a=1, foo="bar"), | ||
| ) | ||
| assert repr(feat) == "fiona.Feature(geometry=fiona.Geometry(coordinates=[(0, 0), ...], type='LineString'), id='1', properties=fiona.Properties(a=1, foo='bar'))" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of Fiona 1.8's GeoJSON representation, I propose this. It shows which classes are involved, which is nice. And in a form that could be used to reproduce the objects, modulo the coordinates truncation.
mwtoews
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Useful reprs are welcome! (And reprlib has evaded my attention!)
As a suggestion, you could code less by adding this once to Object around L179:
def __repr__(self):
kvs = [f"{k}={v!r}" for k, v in self.items()]
return "{}({})".format(self.__class__.__name__, ", ".join(kvs))but keep Geometry.__repr__ for the _coord_repr difference.
Also also suggest to see the repr class name with the shorter Geometry rather than fiona.Geometry. A counterpoint to this suggestion is that it may confuse users with other library object names also called "Geometry" (I actually can't name any right now, but certainly they exist).
|
@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. |
|
@sgillies it's fine to keep 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. |
|
@mwtoews thank you for the suggestions! |
|
Thanks @sgillies , that's definitely an improvement. I tested it on
>>> 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')} |
|
@loicdtx thank you for pointing out the issue with |
Resolves #1379