Skip to content
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

rewrite of validation mechanism #98

Merged
merged 6 commits into from Aug 12, 2017
Merged

Conversation

zoranbosnjak
Copy link
Contributor

There was a conceptual problem with validation function, since it assumes "valid" for all unknown instances. This in turn generates valid status for some otherwise false imputs (false positives). This implementation instead assumes "not implemented" unless "errors" method is implemented on all subitems.

This change introduces small incompatibility with previous version.

  • is_valid function is removed
  • obj.is_valid property is introduced instead
  • obj.errors() method is introduced to get error details

Test cases and README file is changed to reflect the change.

I have only small set of geojson data available. I suggest to run validation tests on bigger dataset if you have it available.

Use obj.is_valid to get object validation status (Bool).

Use obj.errors() to get an error or collection of errors during validation
The return type depends on the object, but in any case,
bool(obj.errors()) must evaluate to:
    True in case of error(s)
    False if no errors

This implementation either checks for all subitems or raises NotImplementedError.
geojson/base.py Outdated
def checkListErrors(self, checkFunc, lst):
"""Validation helper function."""
# check for errors on each subitem, filter only subitems with errors
results = [checkFunc(i) for i in lst]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could benefit from being a generator:

results = (checkFunc(i) for i in lst)

geojson/base.py Outdated
def is_valid(self):
return not self.errors()

def checkListErrors(self, checkFunc, lst):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call this check_list_errors? We're not very consistent right now, but it'd be great to have everything be snake case

CHANGELOG.rst Outdated
@@ -1,6 +1,11 @@
Changes
=======

1.4.0 (2017-07-28)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change, so we should bump to 2.0.0 🎊 🎉 2️⃣ .0️⃣



class Polygon(Geometry):
pass
def checkPolygon(coord):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

snake case

class Polygon(Geometry):
pass
def checkPolygon(coord):
lengths = all([len(elem) >= 4 for elem in coord])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all([len(elem) >= 4 for elem in coord])

@zoranbosnjak
Copy link
Contributor Author

I have fixed the comments, except I did not understand last remark:

-class Polygon(Geometry):
-    pass
+def checkPolygon(coord):
+    lengths = all([len(elem) >= 4 for elem in coord])

all([len(elem) >= 4 for elem in coord])

What needs to be changed here?

@frewsxcv
Copy link
Collaborator

frewsxcv commented Aug 1, 2017

What needs to be changed here?

woops, meant to write:

all(len(elem) >= 4 for elem in coord])

Also, do you happen to know if your changes fix #99 ?

@zoranbosnjak
Copy link
Contributor Author

I have converted from list to generator.

Regarding the issue #99, the validation now raises attribute error.
It turns out that the features attribute is a "dict" in this partucilar case, not GeoJSON instance.

Check this:

import geojson

fc = geojson.loads("""{
    "type": "FeatureCollection",
    "features": [{}]
    }
""")

print type(fc.features[0])
print fc.errors()

>>> <type 'dict'>

I am not sure, but it might be better to fix #99 in the init, so that features list is a list of Feature instances. The validation would in this case turn False on the Feature instance and also on toplevel FeatureCollection object.

Copy link
Collaborator

@frewsxcv frewsxcv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

everything looks good to me here, thanks for the pr! before merging, you'll need to address some style/flake8 things:

https://travis-ci.org/frewsxcv/python-geojson/jobs/260921306

if lengths is False:
return 'LinearRing must contain with 4 or more positions'

isring = all([elem[0] == elem[-1] for elem in coord])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isring = all(elem[0] == elem[-1] for elem in coord)

@codecov-io
Copy link

codecov-io commented Aug 6, 2017

Codecov Report

Merging #98 into master will decrease coverage by 2.81%.
The diff coverage is 84.09%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #98      +/-   ##
==========================================
- Coverage   81.23%   78.41%   -2.82%     
==========================================
  Files          12       11       -1     
  Lines         325      315      -10     
  Branches       60       52       -8     
==========================================
- Hits          264      247      -17     
- Misses         51       58       +7     
  Partials       10       10
Impacted Files Coverage Δ
geojson/__init__.py 100% <ø> (ø) ⬆️
geojson/_version.py 100% <100%> (ø) ⬆️
geojson/geometry.py 96.61% <100%> (+1.48%) ⬆️
geojson/feature.py 82.35% <40%> (-17.65%) ⬇️
geojson/base.py 78.33% <60%> (-3.67%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 112e457...10c9713. Read the comment docs.

@frewsxcv
Copy link
Collaborator

thanks!

@frewsxcv frewsxcv merged commit 0f782f7 into jazzband:master Aug 12, 2017
@frewsxcv
Copy link
Collaborator

just published 2.0.0

pbugnion added a commit to pbugnion/gmaps that referenced this pull request Aug 20, 2017
This involved some breaking changes, especially during the validation.

See jazzband/geojson#98 for details.
p-leger added a commit to p-leger/gtfspy that referenced this pull request Jul 17, 2019
p-leger added a commit to p-leger/gtfspy that referenced this pull request Jul 17, 2019
p-leger added a commit to p-leger/gtfspy that referenced this pull request Jul 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants