def _infer_length(iterable):
try:<EOL><INDENT>return len(iterable)<EOL><DEDENT>except (AttributeError, TypeError):  <EOL><INDENT>try:<EOL><INDENT>get_hint = type(iterable).__length_hint__<EOL><DEDENT>except AttributeError:<EOL><INDENT>return None<EOL><DEDENT>try:<EOL><INDENT>hint = get_hint(iterable)<EOL><DEDENT>except TypeError:<EOL><INDENT>return None<EOL><DEDENT>if (hint is NotImplemented or<EOL>not isinstance(hint, int) or<EOL>hint < <NUM_LIT:0>):<EOL><INDENT>return None<EOL><DEDENT>return hint<EOL><DEDENT>
Try and infer the length using the PEP 424 length hint if available.

adapted from click implementation