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

is_point #9

Open
calvinmetcalf opened this issue Aug 25, 2013 · 4 comments
Open

is_point #9

calvinmetcalf opened this issue Aug 25, 2013 · 4 comments

Comments

@calvinmetcalf
Copy link
Owner

@scardine your function

def is_point(p):
    try:
        float(p[0]), float(p[1])
    except (TypeError, IndexError):
        return False

is not equivalent to

is_point = lambda x : type(x)==type([]) and len(x)==2
@scardine
Copy link

No, it isn't because it will not return false if len(p) > 2.

Python programmers usually write isinstance(p, list) instead oftype(a) == type([])`, because type([]) will trigger memory allocation for an empty list. Better yet is not check for an specific type, but to use duck-typing/EAFP: just carry out the operation and catch relevant exceptions.

This will check if p is an iterable of numbers or numeric strings elements with length 2:

def is_point(p):
    try:
        return len([float(i) for i in p[:4]]) == 2
    except (TypeError, IndexError):
        return False

The slicing in p[:4] is to avoid checking if all members of p are numeric if p is larger than 3.

@calvinmetcalf
Copy link
Owner Author

what about

def is_point(p):
    try:
        return len(p)==2
    except (TypeError, IndexError):
        return False

@scardine
Copy link

>> is_point('Ha')
True

>> is_point(['something', 'else'])
True

Previous suggestion was checking:

  • if the argument is an iterable of length 2
  • if argument[0] and argument[1] are int, float or a numeric string

@calvinmetcalf
Copy link
Owner Author

I'm going to keep it as it is until we get tests working I think:

def is_point(p):
    try:
        return len([float(i) for i in p[:4]]) == 2
    except (TypeError, IndexError):
        return False

looks good, but i really want test coverage to make sure.

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

No branches or pull requests

2 participants