_idents module

exception pyflyby._idents.BadDottedIdentifierError
class pyflyby._idents.DottedIdentifier(arg, scope_info=None)
classmethod _from_name(name, scope_info=None)
name: str
property parent
parts: Tuple[str, ...]
property prefixes
scope_info: Optional[Dict]
startswith(o)
pyflyby._idents.brace_identifiers(text)

Parse a string and yield all tokens of the form “{some_token}”.

>>> list(brace_identifiers("{salutation}, {your_name}."))
['salutation', 'your_name']
pyflyby._idents.dotted_prefixes(dotted_name, reverse=False)

Return the prefixes of a dotted name.

>>> dotted_prefixes("aa.bb.cc")
['aa', 'aa.bb', 'aa.bb.cc']
>>> dotted_prefixes("aa.bb.cc", reverse=True)
['aa.bb.cc', 'aa.bb', 'aa']
Parameters:

reverse – If False (default), return shortest to longest. If True, return longest to shortest.

Return type:

list of str

pyflyby._idents.is_identifier(s, dotted=False, prefix=False)

Return whether s is a valid Python identifier name.

>>> is_identifier("foo")
True
>>> is_identifier("foo+bar")
False
>>> is_identifier("from")
False

By default, we check whether s is a single valid identifier, meaning dots are not allowed. If dotted=True, then we check each dotted component:

>>> is_identifier("foo.bar")
False

>>> is_identifier("foo.bar", dotted=True)
True

>>> is_identifier("foo..bar", dotted=True)
False

>>> is_identifier("foo.from", dotted=True)
False

By default, the string must comprise a valid identifier. If prefix=True, then allow strings that are prefixes of valid identifiers. Prefix=False excludes the empty string, strings with a trailing dot, and strings with a trailing keyword component, but prefix=True does not exclude these.

>>> is_identifier("foo.bar.", dotted=True)
False
>>> is_identifier("foo.bar.", dotted=True, prefix=True)
True
>>> is_identifier("foo.or", dotted=True)
False
>>> is_identifier("foo.or", dotted=True, prefix=True)
True
Parameters:
  • dotted (bool) – If False (default), then the input must be a single name such as “foo”. If True, then the input can be a single name or a dotted name such as “foo.bar.baz”.

  • prefix (bool) – If False (Default), then the input must be a valid identifier. If True, then the input can be a valid identifier or the prefix of a valid identifier.

Return type:

bool