Skip to content

Instantly share code, notes, and snippets.

@metasim
Created July 31, 2019 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metasim/6c487e5cc9f85d3821ef738fa445aa0e to your computer and use it in GitHub Desktop.
Save metasim/6c487e5cc9f85d3821ef738fa445aa0e to your computer and use it in GitHub Desktop.
def set_dims(parts):
assert(len(parts) is 2, "Expected dimensions specification to have exactly two components")
assert(all([isinstance(p, int) and p > 0 for p in parts]),
"Expected all components in dimensions to be positive integers")
options.update({
"imageWidth": parts[0],
"imageHeight": parts[1]
})
if raster_dimensions is not None:
if isinstance(raster_dimensions, tuple):
set_dims(raster_dimensions)
elif isinstance(raster_dimensions, str):
p = list(map(lambda s: int(s), raster_dimensions.split(',')))
set_dims(p)
@vpipkt
Copy link

vpipkt commented Jul 31, 2019

At line 2 I would probably prefer an equality test rather than is. Not exactly sure why.

at line 3 we could also attempt coercion to int.

try:
    parts = [int(p) for p in parts]
    assert(all([p > 0 for p in parts]), 'nice message')
except ValueError:
    raise
    # or some nicer errror handling?

At line 10 also check for list : if isinstance(raster_dimensions, (list, tuple))

If you take my suggestion above about doing int coercion within set_dims function then you don't need to do it at 14. If you still want to do it, i think more readable to do anotehr list comprehension at 14 instead of list(map(...) . say [int(s) for s in raster_dimensions.split(',')]

@metasim
Copy link
Author

metasim commented Jul 31, 2019

At line 2 I would probably prefer an equality test rather than is. Not exactly sure why.

I honestly don't know the difference.

@vpipkt
Copy link

vpipkt commented Aug 2, 2019

IDK but maybe this seems correct

is will return True if two variables point to the same object, == if the objects referred to by the variables are equal.

And as I recall there is some interesting things like if i say a=2 and b=2 then a is b is true because both objects will point ot the same underlying object. I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment