if ftp is None:<EOL><INDENT>ftp = connect()<EOL>quit = True<EOL><DEDENT>else:<EOL><INDENT>quit = False<EOL><DEDENT>ftp.voidcmd('<STR_LIT>')<EOL>with contextlib.closing(ftp.transfercmd("<STR_LIT>" + TESTFN)) as conn:<EOL><INDENT>chunk = b'<STR_LIT:x>' * BUFFER_LEN<EOL>total_sent = <NUM_LIT:0><EOL>while True:<EOL><INDENT>sent = conn.send(chunk)<EOL>total_sent += sent<EOL>if total_sent >= FILE_SIZE:<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>ftp.voidresp()<EOL>if quit:<EOL><INDENT>ftp.quit()<EOL><DEDENT>return ftp<EOL>

Same as ftplib's storbinary() but just sends dummy data
    instead of reading it from a real file.


@contextlib.contextmanager<EOL>def benchmark():<EOL><INDENT>timer = time.clock if sys.platform == "<STR_LIT:win32>" else time.time<EOL>start = timer()<EOL>yield<EOL>stop = timer()<EOL>res = (stop - start)<EOL>print_bench(what, res, "<STR_LIT>")<EOL><DEDENT>if hasattr(what, "<STR_LIT>"):<EOL><INDENT>def timed(*args, **kwargs):<EOL><INDENT>with benchmark():<EOL><INDENT>return what(*args, **kwargs)<EOL><DEDENT><DEDENT>return timed<EOL><DEDENT>else:<EOL><INDENT>return benchmark()<EOL><DEDENT>

Utility function for making simple benchmarks (calculates time calls).
    It can be used either as a context manager or as a decorator.


ftp_class = ftplib.FTP if not SSL else ftplib.FTP_TLS<EOL>ftp = ftp_class(timeout=TIMEOUT)<EOL>ftp.connect(HOST, PORT)<EOL>ftp.login(USER, PASSWORD)<EOL>if SSL:<EOL><INDENT>ftp.prot_p()  <EOL><DEDENT>return ftp<EOL>

Connect to FTP server, login and return an ftplib.FTP instance.


<EOL>def get_mem(proc):<EOL><INDENT>if os.name == '<STR_LIT>':<EOL><INDENT>mem = proc.memory_info_ex()<EOL>counter = mem.rss<EOL>if '<STR_LIT>' in mem._fields:<EOL><INDENT>counter -= mem.shared<EOL><DEDENT>return counter<EOL><DEDENT>else:<EOL><INDENT>return proc.get_memory_info().rss<EOL><DEDENT><DEDENT>if SERVER_PROC is not None:<EOL><INDENT>mem = get_mem(SERVER_PROC)<EOL>for child in SERVER_PROC.children():<EOL><INDENT>mem += get_mem(child)<EOL><DEDENT>server_memory.append(bytes2human(mem))<EOL><DEDENT>

Register an approximation of memory used by FTP server process
    and all of its children.


symbols = ('<STR_LIT:B>', '<STR_LIT>', '<STR_LIT:M>', '<STR_LIT>', '<STR_LIT:T>', '<STR_LIT:P>', '<STR_LIT:E>', '<STR_LIT>', '<STR_LIT:Y>')<EOL>letter = s[-<NUM_LIT:1>:].strip().upper()<EOL>num = s[:-<NUM_LIT:1>]<EOL>assert num.isdigit() and letter in symbols, s<EOL>num = float(num)<EOL>prefix = {symbols[<NUM_LIT:0>]: <NUM_LIT:1>}<EOL>for i, s in enumerate(symbols[<NUM_LIT:1>:]):<EOL><INDENT>prefix[s] = <NUM_LIT:1> << (i + <NUM_LIT:1>) * <NUM_LIT:10><EOL><DEDENT>return int(num * prefix[letter])<EOL>


heap.append(item)<EOL>_siftdown_max(heap, <NUM_LIT:0>, len(heap) - <NUM_LIT:1>)<EOL>

Push item onto heap, maintaining the heap invariant.


return self.query("<STR_LIT>", rawl_id, commit=False)<EOL>


def query(self, sql_string, *args, **kwargs):

commit = None<EOL>columns = None<EOL>if kwargs.get('<STR_LIT>') is not None:<EOL><INDENT>commit = kwargs.pop('<STR_LIT>')<EOL><DEDENT>if kwargs.get('<STR_LIT>') is not None:<EOL><INDENT>columns = kwargs.pop('<STR_LIT>')<EOL><DEDENT>query = self._assemble_simple(sql_string, *args, **kwargs)<EOL>return self._execute(query, commit=commit, working_columns=columns)<EOL>

Execute a DML query

:sql_string:    An SQL string template
:*args:         Arguments to be passed for query parameters.
:commit:        Whether or not to commit the transaction after the query
:returns:       Psycopg2 result


def insert_dict(self, value_dict, commit=False):

<EOL>for key in value_dict.keys():<EOL><INDENT>if key not in self.columns:<EOL><INDENT>raise ValueError("<STR_LIT>" % key)<EOL><DEDENT><DEDENT>insert_cols = []<EOL>value_set = []<EOL>for col in self.columns:<EOL><INDENT>if col in value_dict:<EOL><INDENT>insert_cols.append(col)<EOL>value_set.append(value_dict[col])<EOL><DEDENT><DEDENT>placeholders = '<STR_LIT:U+002CU+0020>'.join(["<STR_LIT>" % x for x in range(<NUM_LIT:1>, len(value_set) + <NUM_LIT:1>)])<EOL>query = self._assemble_with_columns('''<STR_LIT>''' + self.table + '''<STR_LIT>''' + placeholders + '''<STR_LIT>''' + self.pk + '''<STR_LIT:U+0020>''', insert_cols, *value_set)<EOL>result = self._execute(query, commit=commit)<EOL>if len(result) > <NUM_LIT:0>:<EOL><INDENT>if hasattr(result[<NUM_LIT:0>], self.pk):<EOL><INDENT>return getattr(result[<NUM_LIT:0>], self.pk)<EOL><DEDENT>else:<EOL><INDENT>return result[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>

Execute an INSERT statement using a python dict

:value_dict:    A dictionary representing all the columns(keys) and
    values that should be part of the INSERT statement
:commit:        Whether to automatically commit the transaction
:returns:       Psycopg2 result


return self.select("<STR_LIT>" + self.table + "<STR_LIT:;>", <EOL>self.columns)<EOL>

Retreive all single record from the table.  Should be implemented but not
required.
:returns:       List of results


def _assemble_simple(self, sql_str, *args, **kwargs):

query_string = sql.SQL(sql_str).format(<EOL>*[sql.Literal(a) for a in args]<EOL>)<EOL>return query_string<EOL>

Format a select statement with specific columns

:sql_str:   An SQL string template
:*args:     Arguments to use as query parameters.
:returns:   Psycopg2 compiled query


def _assemble_with_columns(self, sql_str, columns, *args, **kwargs):

<EOL>qcols = []<EOL>for col in columns:<EOL><INDENT>if '<STR_LIT:.>' in col:<EOL><INDENT>wlist = col.split('<STR_LIT:.>')<EOL>qcols.append(sql.SQL('<STR_LIT:.>').join([sql.Identifier(x) for x in wlist]))<EOL><DEDENT>else:<EOL><INDENT>qcols.append(sql.Identifier(col))<EOL><DEDENT><DEDENT>query_string = sql.SQL(sql_str).format(<EOL>sql.SQL('<STR_LIT:U+002CU+0020>').join(qcols),<EOL>*[sql.Literal(a) for a in args]<EOL>)<EOL>return query_string<EOL>

Format a select statement with specific columns

:sql_str:   An SQL string template
:columns:   The columns to be selected and put into {0}
:*args:     Arguments to use as query parameters.
:returns:   Psycopg2 compiled query


def retry(<EOL>exceptions=(Exception,), interval=<NUM_LIT:0>, max_retries=<NUM_LIT:10>, success=None,<EOL>timeout=-<NUM_LIT:1>):

if not exceptions and success is None:<EOL><INDENT>raise TypeError(<EOL>'<STR_LIT>')<EOL><DEDENT>exceptions = exceptions or (_DummyException,)<EOL>_retries_error_msg = ('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>_timeout_error_msg = '<STR_LIT>'<EOL>@decorator<EOL>def wrapper(func, *args, **kwargs):<EOL><INDENT>signal.signal(<EOL>signal.SIGALRM, _timeout(<EOL>_timeout_error_msg.format(timeout, func.__name__)))<EOL>run_func = functools.partial(func, *args, **kwargs)<EOL>logger = logging.getLogger(func.__module__)<EOL>if max_retries < <NUM_LIT:0>:<EOL><INDENT>iterator = itertools.count()<EOL><DEDENT>else:<EOL><INDENT>iterator = range(max_retries)<EOL><DEDENT>if timeout > <NUM_LIT:0>:<EOL><INDENT>signal.alarm(timeout)<EOL><DEDENT>for num, _ in enumerate(iterator, <NUM_LIT:1>):<EOL><INDENT>try:<EOL><INDENT>result = run_func()<EOL>if success is None or success(result):<EOL><INDENT>signal.alarm(<NUM_LIT:0>)<EOL>return result<EOL><DEDENT><DEDENT>except exceptions:<EOL><INDENT>logger.exception(<EOL>'<STR_LIT>'.format(<EOL>func.__name__))<EOL>if num == max_retries:<EOL><INDENT>raise<EOL><DEDENT><DEDENT>logger.warning(<EOL>'<STR_LIT>'.format(<EOL>func.__name__, interval))<EOL>time.sleep(interval)<EOL><DEDENT>else:<EOL><INDENT>raise MaximumRetriesExceeded(<EOL>_retries_error_msg.format(<EOL>max_retries, interval, func.__name__))<EOL><DEDENT><DEDENT>return wrapper<EOL>

Decorator to retry a function 'max_retries' amount of times

    :param tuple exceptions: Exceptions to be caught for retries
    :param int interval: Interval between retries in seconds
    :param int max_retries: Maximum number of retries to have, if
        set to -1 the decorator will loop forever
    :param function success: Function to indicate success criteria
    :param int timeout: Timeout interval in seconds, if -1 will retry forever
    :raises MaximumRetriesExceeded: Maximum number of retries hit without
        reaching the success criteria
    :raises TypeError: Both exceptions and success were left None causing the
        decorator to have no valid exit criteria.

    Example:
        Use it to decorate a function!

        .. sourcecode:: python

            from retry import retry

            @retry(exceptions=(ArithmeticError,), success=lambda x: x > 0)
            def foo(bar):
                if bar < 0:
                    raise ArithmeticError('testing this')
                return bar
            foo(5)
            # Should return 5
            foo(-1)
            # Should raise ArithmeticError
            foo(0)
            # Should raise MaximumRetriesExceeded


for model in get_activatable_models():<EOL><INDENT>activatable_field = next((<EOL>f for f in model._meta.fields<EOL>if f.__class__ == models.BooleanField and f.name == model.ACTIVATABLE_FIELD_NAME<EOL>), None)<EOL>if activatable_field is None:<EOL><INDENT>raise ValidationError((<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(model)<EOL>))<EOL><DEDENT>if not model.ALLOW_CASCADE_DELETE:<EOL><INDENT>for field in model._meta.fields:<EOL><INDENT>if field.__class__ in (models.ForeignKey, models.OneToOneField):<EOL><INDENT>if field.remote_field.on_delete == models.CASCADE:<EOL><INDENT>raise ValidationError((<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>).format(model))<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>

Raises a ValidationError for any ActivatableModel that has ForeignKeys or OneToOneFields that will
cause cascading deletions to occur. This function also raises a ValidationError if the activatable
model has not defined a Boolean field with the field name defined by the ACTIVATABLE_FIELD_NAME variable
on the model.


current_activable_value = getattr(self, self.ACTIVATABLE_FIELD_NAME)<EOL>is_active_changed = self.id is None or self.__original_activatable_value != current_activable_value<EOL>self.__original_activatable_value = current_activable_value<EOL>ret_val = super(BaseActivatableModel, self).save(*args, **kwargs)<EOL>if is_active_changed:<EOL><INDENT>model_activations_changed.send(self.__class__, instance_ids=[self.id], is_active=current_activable_value)<EOL><DEDENT>if self.activatable_field_updated:<EOL><INDENT>model_activations_updated.send(self.__class__, instance_ids=[self.id], is_active=current_activable_value)<EOL><DEDENT>return ret_val<EOL>

A custom save method that handles figuring out when something is activated or deactivated.


if not isinstance(self.val, str_types):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if os.path.exists(self.val):<EOL><INDENT>self._err('<STR_LIT>' % self.val)<EOL><DEDENT>return self<EOL>

Asserts that val is a path and that it does not exist.


def does_not_contain_entry(self, *args, **kwargs):

self._check_dict_like(self.val, check_values=False)<EOL>entries = list(args) + [{k:v} for k,v in kwargs.items()]<EOL>if len(entries) == <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>found = []<EOL>for e in entries:<EOL><INDENT>if type(e) is not dict:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if len(e) != <NUM_LIT:1>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>k = next(iter(e))<EOL>if k in self.val and e[k] == self.val[k]:<EOL><INDENT>found.append(e)<EOL><DEDENT><DEDENT>if found:<EOL><INDENT>self._err('<STR_LIT>' % (self.val, self._fmt_items(entries), self._fmt_items(found)))<EOL><DEDENT>return self<EOL>

Asserts that val is a dict and does not contain the given entry or entries.


if not isinstance(self.val, str_types):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if len(self.val) == <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if self.val != self.val.upper():<EOL><INDENT>self._err('<STR_LIT>' % self.val)<EOL><DEDENT>return self<EOL>

Asserts that val is non-empty string and all characters are uppercase.


self._validate_number()<EOL>return self.is_equal_to(<NUM_LIT:0>)<EOL>

Asserts that val is numeric and equal to zero.


if len(items) == <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>elif len(items) == <NUM_LIT:1>:<EOL><INDENT>if items[<NUM_LIT:0>] in self.val:<EOL><INDENT>self._err('<STR_LIT>' % (self.val, items[<NUM_LIT:0>]))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>found = []<EOL>for i in items:<EOL><INDENT>if i in self.val:<EOL><INDENT>found.append(i)<EOL><DEDENT><DEDENT>if found:<EOL><INDENT>self._err('<STR_LIT>' % (self.val, self._fmt_items(items), self._fmt_items(found)))<EOL><DEDENT><DEDENT>return self<EOL>

Asserts that val does not contain the given item or items.


if self._check_dict_like(self.val, check_values=False, return_as_bool=True) andself._check_dict_like(other, check_values=False, return_as_bool=True):<EOL><INDENT>if self._dict_not_equal(self.val, other, ignore=kwargs.get('<STR_LIT:ignore>'), include=kwargs.get('<STR_LIT>')):<EOL><INDENT>self._dict_err(self.val, other, ignore=kwargs.get('<STR_LIT:ignore>'), include=kwargs.get('<STR_LIT>'))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if self.val != other:<EOL><INDENT>self._err('<STR_LIT>' % (self.val, other))<EOL><DEDENT><DEDENT>return self<EOL>


def when_called_with(self, *some_args, **some_kwargs):

if not self.expected:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>try:<EOL><INDENT>self.val(*some_args, **some_kwargs)<EOL><DEDENT>except BaseException as e:<EOL><INDENT>if issubclass(type(e), self.expected):<EOL><INDENT>return AssertionBuilder(str(e), self.description, self.kind)<EOL><DEDENT>else:<EOL><INDENT>self._err('<STR_LIT>' % (<EOL>self.val.__name__,<EOL>self.expected.__name__,<EOL>self._fmt_args_kwargs(*some_args, **some_kwargs),<EOL>type(e).__name__))<EOL><DEDENT><DEDENT>self._err('<STR_LIT>' % (<EOL>self.val.__name__,<EOL>self.expected.__name__,<EOL>self._fmt_args_kwargs(*some_args, **some_kwargs)))<EOL>

Asserts the val callable when invoked with the given args and kwargs raises the expected exception.


if len(items) == <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if isinstance(self.val, str_types):<EOL><INDENT>if len(items) == <NUM_LIT:1>:<EOL><INDENT>if not isinstance(items[<NUM_LIT:0>], str_types):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if items[<NUM_LIT:0>].lower() not in self.val.lower():<EOL><INDENT>self._err('<STR_LIT>' % (self.val, items[<NUM_LIT:0>]))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>missing = []<EOL>for i in items:<EOL><INDENT>if not isinstance(i, str_types):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if i.lower() not in self.val.lower():<EOL><INDENT>missing.append(i)<EOL><DEDENT><DEDENT>if missing:<EOL><INDENT>self._err('<STR_LIT>' % (self.val, self._fmt_items(items), self._fmt_items(missing)))<EOL><DEDENT><DEDENT><DEDENT>elif isinstance(self.val, Iterable):<EOL><INDENT>missing = []<EOL>for i in items:<EOL><INDENT>if not isinstance(i, str_types):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>found = False<EOL>for v in self.val:<EOL><INDENT>if not isinstance(v, str_types):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if i.lower() == v.lower():<EOL><INDENT>found = True<EOL>break<EOL><DEDENT><DEDENT>if not found:<EOL><INDENT>missing.append(i)<EOL><DEDENT><DEDENT>if missing:<EOL><INDENT>self._err('<STR_LIT>' % (self.val, self._fmt_items(items), self._fmt_items(missing)))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>return self<EOL>

Asserts that val is string and contains the given item or items.


if not isinstance(self.val, str_types):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if not isinstance(other, str_types):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if self.val.lower() != other.lower():<EOL><INDENT>self._err('<STR_LIT>' % (self.val, other))<EOL><DEDENT>return self<EOL>

Asserts that val is case-insensitive equal to other.


global _soft_ctx<EOL>if _soft_ctx:<EOL><INDENT>global _soft_err<EOL>_soft_err.append('<STR_LIT>' % msg if msg else '<STR_LIT>')<EOL>return<EOL><DEDENT>fail(msg)<EOL>

Adds error message to soft errors list if within soft assertions context.
       Either just force test failure with the given message.


try:<EOL><INDENT>if len(self.val) == len(set(self.val)):<EOL><INDENT>return self<EOL><DEDENT><DEDENT>except TypeError:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>self._err('<STR_LIT>' % self.val)<EOL>

Asserts that val is iterable and does not contain any duplicate items.


return self.is_less_than(<NUM_LIT:0>)<EOL>

Asserts that val is numeric and less than zero.


global _soft_ctx<EOL>if _soft_ctx:<EOL><INDENT>return AssertionBuilder(val, description, '<STR_LIT>')<EOL><DEDENT>return AssertionBuilder(val, description)<EOL>

Factory method for the assertion builder with value to be tested and optional description.


if not isinstance(self.val, Iterable):<EOL><INDENT>self._err('<STR_LIT>')<EOL><DEDENT>return self<EOL>

Asserts that val is iterable collection.


self._check_dict_like(self.val, check_values=False, check_getitem=False)<EOL>return self.does_not_contain(*keys)<EOL>

Asserts the val is a dict and does not contain the given key or keys.  Alias for does_not_contain().


if type(self.val) is not unicode:<EOL><INDENT>self._err('<STR_LIT>' % (self.val, type(self.val).__name__))<EOL><DEDENT>return self<EOL>


if len(items) == <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>for i in items:<EOL><INDENT>if self.val == i:<EOL><INDENT>self._err('<STR_LIT>' % (self.val, self._fmt_items(items)))<EOL><DEDENT><DEDENT><DEDENT>return self<EOL>

Asserts that val is not equal to one of the given items.


def __init__(self, val, description='<STR_LIT>', kind=None, expected=None):

self.val = val<EOL>self.description = description<EOL>self.kind = kind<EOL>self.expected = expected<EOL>


self._validate_number()<EOL>self._validate_real()<EOL>if not math.isinf(self.val):<EOL><INDENT>self._err('<STR_LIT>' % self.val)<EOL><DEDENT>return self<EOL>

Asserts that val is real number and Inf (infinity).


def is_not_close_to(self, other, tolerance):

self._validate_close_to_args(self.val, other, tolerance)<EOL>if self.val >= (other-tolerance) and self.val <= (other+tolerance):<EOL><INDENT>if type(self.val) is datetime.datetime:<EOL><INDENT>tolerance_seconds = tolerance.days * <NUM_LIT> + tolerance.seconds + tolerance.microseconds / <NUM_LIT><EOL>h, rem = divmod(tolerance_seconds, <NUM_LIT>)<EOL>m, s = divmod(rem, <NUM_LIT>)<EOL>self._err('<STR_LIT>' % (self.val.strftime('<STR_LIT>'), other.strftime('<STR_LIT>'), h, m, s))<EOL><DEDENT>else:<EOL><INDENT>self._err('<STR_LIT>' % (self.val, other, tolerance))<EOL><DEDENT><DEDENT>return self<EOL>

Asserts that val is numeric and is not close to other within tolerance.


if not isinstance(self.val, str_types):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if not isinstance(pattern, str_types):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if len(pattern) == <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if re.search(pattern, self.val) is not None:<EOL><INDENT>self._err('<STR_LIT>' % (self.val, pattern))<EOL><DEDENT>return self<EOL>

Asserts that val is string and does not match regex pattern.


self._validate_compareable(other)<EOL>if self.val <= other:<EOL><INDENT>if type(self.val) is datetime.datetime:<EOL><INDENT>self._err('<STR_LIT>' % (self.val.strftime('<STR_LIT>'), other.strftime('<STR_LIT>')))<EOL><DEDENT>else:<EOL><INDENT>self._err('<STR_LIT>' % (self.val, other))<EOL><DEDENT><DEDENT>return self<EOL>

Asserts that val is numeric and is greater than other.


if type(some_type) is not type andnot issubclass(type(some_type), type):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if type(self.val) is not some_type:<EOL><INDENT>if hasattr(self.val, '<STR_LIT>'):<EOL><INDENT>t = self.val.__name__<EOL><DEDENT>elif hasattr(self.val, '<STR_LIT>'):<EOL><INDENT>t = self.val.__class__.__name__<EOL><DEDENT>else:<EOL><INDENT>t = '<STR_LIT>'<EOL><DEDENT>self._err('<STR_LIT>' % (self.val, t, some_type.__name__))<EOL><DEDENT>return self<EOL>


if prefix is None:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if isinstance(self.val, str_types):<EOL><INDENT>if not isinstance(prefix, str_types):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if len(prefix) == <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if not self.val.startswith(prefix):<EOL><INDENT>self._err('<STR_LIT>' % (self.val, prefix))<EOL><DEDENT><DEDENT>elif isinstance(self.val, Iterable):<EOL><INDENT>if len(self.val) == <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>first = next(iter(self.val))<EOL>if first != prefix:<EOL><INDENT>self._err('<STR_LIT>' % (self.val, prefix))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>return self<EOL>

Asserts that val is string or iterable and starts with prefix.


self.is_file()<EOL>if not isinstance(parent, str_types):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>val_abspath = os.path.abspath(self.val)<EOL>parent_abspath = os.path.abspath(parent)<EOL>if not val_abspath.startswith(parent_abspath):<EOL><INDENT>self._err('<STR_LIT>' % (val_abspath, parent_abspath))<EOL><DEDENT>return self<EOL>

Asserts that val is an existing path to a file and that file is a child of parent.


if not isinstance(self.val, str_types):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if len(self.val) == <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if not self.val.isdigit():<EOL><INDENT>self._err('<STR_LIT>' % self.val)<EOL><DEDENT>return self<EOL>

Asserts that val is non-empty string and all characters are digits.


if isinstance(self.val, numbers.Real) is False:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>

Raise TypeError if val is not real number.


if not self.val:<EOL><INDENT>self._err('<STR_LIT>')<EOL><DEDENT>return self<EOL>


if type(self.val) is not datetime.datetime:<EOL><INDENT>raise TypeError('<STR_LIT>' % type(self.val).__name__)<EOL><DEDENT>if type(other) is not datetime.datetime:<EOL><INDENT>raise TypeError('<STR_LIT>' % type(other).__name__)<EOL><DEDENT>if self.val <= other:<EOL><INDENT>self._err('<STR_LIT>' % (self.val.strftime('<STR_LIT>'), other.strftime('<STR_LIT>')))<EOL><DEDENT>return self<EOL>

Asserts that val is a date and is after other date.


self._check_dict_like(self.val, check_getitem=False)<EOL>if len(values) == <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>missing = []<EOL>for v in values:<EOL><INDENT>if v not in self.val.values():<EOL><INDENT>missing.append(v)<EOL><DEDENT><DEDENT>if missing:<EOL><INDENT>self._err('<STR_LIT>' % (self.val, self._fmt_items(values), self._fmt_items(missing)))<EOL><DEDENT>return self<EOL>

Asserts that val is a dict and contains the given value or values.


if self.val is None:<EOL><INDENT>self._err('<STR_LIT>')<EOL><DEDENT>return self<EOL>


if self.val == other:<EOL><INDENT>self._err('<STR_LIT>' % (self.val, other))<EOL><DEDENT>return self<EOL>


self.analog_mapping_query_results = data<EOL>

This method handles an analog mapping query response message and stores the results to be retrieved
via get_analog_mapping_request_results() in pymata.py

:param data: raw analog mapping data


with self.pymata.data_lock:<EOL><INDENT>data = self.analog_response_table<EOL><DEDENT>return data<EOL>

This method returns the entire analog response table to the caller
:return: The analog response table.


<EOL>self.command_dispatch.update({self.REPORT_VERSION: [self.report_version, <NUM_LIT:2>]})<EOL>self.command_dispatch.update({self.REPORT_FIRMWARE: [self.report_firmware, <NUM_LIT:1>]})<EOL>self.command_dispatch.update({self.ANALOG_MESSAGE: [self.analog_message, <NUM_LIT:2>]})<EOL>self.command_dispatch.update({self.DIGITAL_MESSAGE: [self.digital_message, <NUM_LIT:2>]})<EOL>self.command_dispatch.update({self.ENCODER_DATA: [self.encoder_data, <NUM_LIT:3>]})<EOL>self.command_dispatch.update({self.SONAR_DATA: [self.sonar_data, <NUM_LIT:3>]})<EOL>self.command_dispatch.update({self.STRING_DATA: [self._string_data, <NUM_LIT:2>]})<EOL>self.command_dispatch.update({self.I2C_REPLY: [self.i2c_reply, <NUM_LIT:2>]})<EOL>self.command_dispatch.update({self.CAPABILITY_RESPONSE: [self.capability_response, <NUM_LIT:2>]})<EOL>self.command_dispatch.update({self.PIN_STATE_RESPONSE: [self.pin_state_response, <NUM_LIT:2>]})<EOL>self.command_dispatch.update({self.ANALOG_MAPPING_RESPONSE: [self.analog_mapping_response, <NUM_LIT:2>]})<EOL>self.command_dispatch.update({self.STEPPER_DATA: [self.stepper_version_response, <NUM_LIT:2>]})<EOL>while not self.is_stopped():<EOL><INDENT>if len(self.pymata.command_deque):<EOL><INDENT>data = self.pymata.command_deque.popleft()<EOL>command_data = []<EOL>if data == self.START_SYSEX:<EOL><INDENT>while len(self.pymata.command_deque) == <NUM_LIT:0>:<EOL><INDENT>pass<EOL><DEDENT>sysex_command = self.pymata.command_deque.popleft()<EOL>dispatch_entry = self.command_dispatch.get(sysex_command)<EOL>method = dispatch_entry[<NUM_LIT:0>]<EOL>end_of_sysex = False<EOL>while not end_of_sysex:<EOL><INDENT>while len(self.pymata.command_deque) == <NUM_LIT:0>:<EOL><INDENT>pass<EOL><DEDENT>data = self.pymata.command_deque.popleft()<EOL>if data != self.END_SYSEX:<EOL><INDENT>command_data.append(data)<EOL><DEDENT>else:<EOL><INDENT>end_of_sysex = True<EOL>method(command_data)<EOL><DEDENT><DEDENT>continue<EOL><DEDENT>elif <NUM_LIT> <= data <= <NUM_LIT>:<EOL><INDENT>if <NUM_LIT> <= data <= <NUM_LIT>:<EOL><INDENT>port = data & <NUM_LIT><EOL>command_data.append(port)<EOL>data = <NUM_LIT><EOL><DEDENT>elif <NUM_LIT> <= data <= <NUM_LIT>:<EOL><INDENT>pin = data & <NUM_LIT><EOL>command_data.append(pin)<EOL>data = <NUM_LIT><EOL><DEDENT>else:<EOL><INDENT>pass<EOL><DEDENT>dispatch_entry = self.command_dispatch.get(data)<EOL>method = dispatch_entry[<NUM_LIT:0>]<EOL>num_args = dispatch_entry[<NUM_LIT:1>]<EOL>for i in range(num_args):<EOL><INDENT>while len(self.pymata.command_deque) == <NUM_LIT:0>:<EOL><INDENT>pass<EOL><DEDENT>data = self.pymata.command_deque.popleft()<EOL>command_data.append(data)<EOL><DEDENT>method(command_data)<EOL>continue<EOL><DEDENT><DEDENT>else:<EOL><INDENT>time.sleep(<NUM_LIT>)<EOL><DEDENT><DEDENT>

This method starts the thread that continuously runs to receive and interpret
messages coming from Firmata. This must be the last method in this file
It also checks the deque for messages to be sent to Firmata.


port = data[<NUM_LIT:0>]<EOL>port_data = (data[self.MSB] << <NUM_LIT:7>) + data[self.LSB]<EOL>pin = port * <NUM_LIT:8><EOL>for pin in range(pin, min(pin + <NUM_LIT:8>, self.total_pins_discovered)):<EOL><INDENT>with self.pymata.data_lock:<EOL><INDENT>prev_data = self.digital_response_table[pin][self.RESPONSE_TABLE_PIN_DATA_VALUE]<EOL>self.digital_response_table[pin][self.RESPONSE_TABLE_PIN_DATA_VALUE] = port_data & <NUM_LIT><EOL>if prev_data != port_data & <NUM_LIT>:<EOL><INDENT>callback = self.digital_response_table[pin][self.RESPONSE_TABLE_CALLBACK]<EOL>if callback:<EOL><INDENT>callback([self.pymata.DIGITAL, pin,<EOL>self.digital_response_table[pin][self.RESPONSE_TABLE_PIN_DATA_VALUE]])<EOL><DEDENT><DEDENT>latching_entry = self.digital_latch_table[pin]<EOL>if latching_entry[self.LATCH_STATE] == self.LATCH_ARMED:<EOL><INDENT>if latching_entry[self.LATCHED_THRESHOLD_TYPE] == self.DIGITAL_LATCH_LOW:<EOL><INDENT>if (port_data & <NUM_LIT>) == <NUM_LIT:0>:<EOL><INDENT>if latching_entry[self.DIGITAL_LATCH_CALLBACK] is not None:<EOL><INDENT>self.digital_latch_table[pin] = [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>, None]<EOL>latching_entry[self.DIGITAL_LATCH_CALLBACK](<EOL>[self.pymata.OUTPUT | self.pymata.LATCH_MODE,<EOL>pin, <NUM_LIT:0>, time.time()])<EOL><DEDENT>else:<EOL><INDENT>updated_latch_entry = latching_entry<EOL>updated_latch_entry[self.LATCH_STATE] = self.LATCH_LATCHED<EOL>updated_latch_entry[self.DIGITAL_LATCHED_DATA] = self.DIGITAL_LATCH_LOW<EOL>updated_latch_entry[self.DIGITAL_TIME_STAMP] = time.time()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>elif latching_entry[self.LATCHED_THRESHOLD_TYPE] == self.DIGITAL_LATCH_HIGH:<EOL><INDENT>if port_data & <NUM_LIT>:<EOL><INDENT>if latching_entry[self.DIGITAL_LATCH_CALLBACK] is not None:<EOL><INDENT>self.digital_latch_table[pin] = [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>, None]<EOL>latching_entry[self.DIGITAL_LATCH_CALLBACK](<EOL>[self.pymata.OUTPUT | self.pymata.LATCH_MODE,<EOL>pin, <NUM_LIT:1>, time.time()])<EOL><DEDENT>else:<EOL><INDENT>updated_latch_entry = latching_entry<EOL>updated_latch_entry[self.LATCH_STATE] = self.LATCH_LATCHED<EOL>updated_latch_entry[self.DIGITAL_LATCHED_DATA] = self.DIGITAL_LATCH_HIGH<EOL>updated_latch_entry[self.DIGITAL_TIME_STAMP] = time.time()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>port_data >>= <NUM_LIT:1><EOL><DEDENT>

This method handles the incoming digital message.
It stores the data values in the digital response table.
Data is stored for all 8 bits of a  digital port

:param data: Message data from Firmata

:return: No return value.


prev_val = self.digital_response_table[data[self.RESPONSE_TABLE_MODE]][self.RESPONSE_TABLE_PIN_DATA_VALUE]<EOL>val = int((data[self.MSB] << <NUM_LIT:7>) + data[self.LSB])<EOL>if val > <NUM_LIT>:<EOL><INDENT>val -= <NUM_LIT><EOL><DEDENT>pin = data[<NUM_LIT:0>]<EOL>with self.pymata.data_lock:<EOL><INDENT>self.digital_response_table[data[self.RESPONSE_TABLE_MODE]][self.RESPONSE_TABLE_PIN_DATA_VALUE] = val<EOL>if prev_val != val:<EOL><INDENT>callback = self.digital_response_table[pin][self.RESPONSE_TABLE_CALLBACK]<EOL>if callback is not None:<EOL><INDENT>callback([self.pymata.ENCODER, pin,<EOL>self.digital_response_table[pin][self.RESPONSE_TABLE_PIN_DATA_VALUE]])<EOL><DEDENT><DEDENT><DEDENT>

This method handles the incoming encoder data message and stores
the data in the digital response table.

:param data: Message data from Firmata

:return: No return value.


with self.pymata.data_lock:<EOL><INDENT>pin_data = self.digital_latch_table[pin]<EOL>current_latch_data = [pin,<EOL>pin_data[self.LATCH_STATE],<EOL>pin_data[self.DIGITAL_LATCHED_DATA],<EOL>pin_data[self.DIGITAL_TIME_STAMP],<EOL>pin_data[self.DIGITAL_LATCH_CALLBACK]]<EOL>if pin_data[self.LATCH_STATE] == self.LATCH_LATCHED:<EOL><INDENT>self.digital_latch_table[pin] = [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>, None]<EOL><DEDENT><DEDENT>return current_latch_data<EOL>

This method reads the digital latch table for the specified pin and returns a list that contains:
[latch_state, latched_data, and time_stamp].
If the latch state is latched, the entry in the table is cleared

:param pin:  pin number

:return: [latch_state, latched_data, and time_stamp]


<EOL>self.pymata = pymata<EOL>self.last_pin_query_results = []<EOL>self.capability_query_results = []<EOL>self.analog_mapping_query_results = []<EOL>self.total_pins_discovered = <NUM_LIT:0><EOL>self.number_of_analog_pins_discovered = <NUM_LIT:0><EOL>threading.Thread.__init__(self)<EOL>self.daemon = True<EOL>self.stop_event = threading.Event()<EOL>

constructor for CommandHandler class

:param pymata: A reference to the pymata instance.


send_message = "<STR_LIT>"<EOL>for i in command:<EOL><INDENT>send_message += chr(i)<EOL><DEDENT>for data in send_message:<EOL><INDENT>self.pymata.transport.write(data)<EOL><DEDENT>

This method is used to transmit a non-sysex command.

:param command: Command to send to firmata includes command + data formatted by caller

:return : No return value.


def set_analog_latch(self, pin, threshold_type, threshold_value, cb):

with self.pymata.data_lock:<EOL><INDENT>self.analog_latch_table[pin] = [self.LATCH_ARMED, threshold_type, threshold_value, <NUM_LIT:0>, <NUM_LIT:0>, cb]<EOL><DEDENT>

This method "arms" a pin to allow data latching for the pin.

:param pin: Analog pin number (value following an 'A' designator, i.e. A5 = 5

:param threshold_type: ANALOG_LATCH_GT | ANALOG_LATCH_LT  | ANALOG_LATCH_GTE | ANALOG_LATCH_LTE

:param threshold_value: numerical value

:param cb: User provided callback function


with self.pymata.data_lock:<EOL><INDENT>pin_data = self.analog_latch_table[pin]<EOL>current_latch_data = [pin,<EOL>pin_data[self.LATCH_STATE],<EOL>pin_data[self.ANALOG_LATCHED_DATA],<EOL>pin_data[self.ANALOG_TIME_STAMP],<EOL>pin_data[self.ANALOG_LATCH_CALLBACK]]<EOL>if pin_data[self.LATCH_STATE] == self.LATCH_LATCHED:<EOL><INDENT>self.analog_latch_table[pin] = [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>, None]<EOL><DEDENT><DEDENT>return current_latch_data<EOL>

This method reads the analog latch table for the specified pin and returns a list that contains:
[latch_state, latched_data, and time_stamp].
If the latch state is latched, the entry in the table is cleared

:param pin:  pin number

:return: [latch_state, latched_data, and time_stamp]


<EOL>start_time = time.time()<EOL>while self._command_handler.stepper_library_version <= <NUM_LIT:0>:<EOL><INDENT>if time.time() - start_time > timeout:<EOL><INDENT>if self.verbose is True:<EOL><INDENT>print("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>return<EOL><DEDENT>else:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>return self._command_handler.stepper_library_version<EOL>

Get the stepper library version number.

:param timeout: specify a time to allow arduino to process and return a version

:return: the stepper version number if it was set.


return ['<STR_LIT:2>', '<STR_LIT>']<EOL>

Returns the PyMata version number in a list: [Major Number, Minor Number]

:return:


def servo_config(self, pin, min_pulse=<NUM_LIT>, max_pulse=<NUM_LIT>):

self.set_pin_mode(pin, self.SERVO, self.OUTPUT)<EOL>command = [pin, min_pulse & <NUM_LIT>, (min_pulse >> <NUM_LIT:7>) & <NUM_LIT>,<EOL>max_pulse & <NUM_LIT>, (max_pulse >> <NUM_LIT:7>) & <NUM_LIT>]<EOL>self._command_handler.send_sysex(self._command_handler.SERVO_CONFIG, command)<EOL>

Configure a pin as a servo pin. Set pulse min, max in ms.

:param pin: Servo Pin.

:param min_pulse: Min pulse width in ms.

:param max_pulse: Max pulse width in ms.

:return: No return value


def i2c_config(self, read_delay_time=<NUM_LIT:0>, pin_type=None, clk_pin=<NUM_LIT:0>, data_pin=<NUM_LIT:0>):

data = [read_delay_time & <NUM_LIT>, (read_delay_time >> <NUM_LIT:7>) & <NUM_LIT>]<EOL>self._command_handler.send_sysex(self._command_handler.I2C_CONFIG, data)<EOL>if pin_type:<EOL><INDENT>if pin_type == self.DIGITAL:<EOL><INDENT>self._command_handler.digital_response_table[clk_pin][self._command_handler.RESPONSE_TABLE_MODE]= self.I2C<EOL>self._command_handler.digital_response_table[data_pin][self._command_handler.RESPONSE_TABLE_MODE]= self.I2C<EOL><DEDENT>else:<EOL><INDENT>self._command_handler.analog_response_table[clk_pin][self._command_handler.RESPONSE_TABLE_MODE]= self.I2C<EOL>self._command_handler.analog_response_table[data_pin][self._command_handler.RESPONSE_TABLE_MODE]= self.I2C<EOL><DEDENT><DEDENT>

NOTE: THIS METHOD MUST BE CALLED BEFORE ANY I2C REQUEST IS MADE
This method initializes Firmata for I2c operations.
It allows setting of a read time delay amount, and to optionally track
the pins as I2C in the appropriate response table.
To track pins: Set the pin_type to ANALOG or DIGITAL and provide the pin numbers.
If using ANALOG, pin numbers use the analog number, for example A4: use 4.

:param read_delay_time: an optional parameter, default is 0

:param pin_type: ANALOG or DIGITAL to select response table type to track pin numbers

:param clk_pin: pin number (see comment above).

:param data_pin: pin number (see comment above).

:return: No Return Value


def encoder_config(self, pin_a, pin_b, cb=None):

This command enables the rotary encoder (2 pin + ground) and will
enable encoder reporting.

NOTE: This command is not currently part of standard arduino firmata, but is provided for legacy
support of CodeShield on an Arduino UNO.

Encoder data is retrieved by performing a digital_read from pin a (encoder pin 1)

:param pin_a: Encoder pin 1.

:param pin_b: Encoder pin 2.

:param cb: callback function to report encoder changes

:return: No return value


def stepper_step(self, motor_speed, number_of_steps):

if number_of_steps > <NUM_LIT:0>:<EOL><INDENT>direction = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>direction = <NUM_LIT:0><EOL><DEDENT>abs_number_of_steps = abs(number_of_steps)<EOL>data = [self.STEPPER_STEP, motor_speed & <NUM_LIT>, (motor_speed >> <NUM_LIT:7>) & <NUM_LIT>, (motor_speed >> <NUM_LIT>) & <NUM_LIT>,<EOL>abs_number_of_steps & <NUM_LIT>, (abs_number_of_steps >> <NUM_LIT:7>) & <NUM_LIT>, direction]<EOL>self._command_handler.send_sysex(self._command_handler.STEPPER_DATA, data)<EOL>

Move a stepper motor for the number of steps at the specified speed

:param motor_speed: 21 bits of data to set motor speed

:param number_of_steps: 14 bits for number of steps & direction
                        positive is forward, negative is reverse


self._command_handler.send_sysex(self._command_handler.PIN_STATE_QUERY, [pin])<EOL>

This method issues a pin state query command. Data returned is retrieved via
a call to get_pin_state_query_results()
:param pin: pin number


data = [address, self.I2C_STOP_READING]<EOL>self._command_handler.send_sysex(self._command_handler.I2C_REQUEST, data)<EOL>

This method stops an I2C_READ_CONTINUOUSLY operation for the i2c device address specified.

:param address: address of i2c device


data = [address, self.I2C_WRITE]<EOL>for item in args:<EOL><INDENT>data.append(item & <NUM_LIT>)<EOL>data.append((item >> <NUM_LIT:7>) & <NUM_LIT>)<EOL><DEDENT>self._command_handler.send_sysex(self._command_handler.I2C_REQUEST, data)<EOL>

Write data to an i2c device.

:param address: i2c device address

:param args: A variable number of bytes to be sent to the device


def stepper_config(self, steps_per_revolution, stepper_pins):

data = [self.STEPPER_CONFIGURE, steps_per_revolution & <NUM_LIT>, (steps_per_revolution >> <NUM_LIT:7>) & <NUM_LIT>]<EOL>for pin in range(len(stepper_pins)):<EOL><INDENT>data.append(stepper_pins[pin])<EOL><DEDENT>self._command_handler.send_sysex(self._command_handler.STEPPER_DATA, data)<EOL>

Configure stepper motor prior to operation.

:param steps_per_revolution: number of steps per motor revolution

:param stepper_pins: a list of control pin numbers - either 4 or 2


def set_digital_latch(self, pin, threshold_type, cb=None):

if <NUM_LIT:0> <= threshold_type <= <NUM_LIT:1>:<EOL><INDENT>self._command_handler.set_digital_latch(pin, threshold_type, cb)<EOL>return True<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT>

This method "arms" a digital pin for its data to be latched and saved in the latching table
If a callback method is provided, when latching criteria is achieved, the callback function is called
with latching data notification. In that case, the latching table is not updated.

:param pin: Digital pin number

:param threshold_type: DIGITAL_LATCH_HIGH | DIGITAL_LATCH_LOW

:param cb: callback function

:return: True if successful, False if parameter data is invalid


command = [self._command_handler.REPORT_VERSION]<EOL>self._command_handler.send_command(command)<EOL>

This method will query firmata for the report version.
Retrieve the report version via a call to get_firmata_version()


port = pin // <NUM_LIT:8><EOL>command = [self._command_handler.REPORT_DIGITAL + port, self.REPORTING_DISABLE]<EOL>self._command_handler.send_command(command)<EOL>

Disables digital reporting. By turning reporting off for this pin, reporting
is disabled for all 8 bits in the "port" -

:param pin: Pin and all pins for this port

:return: No return value


return self._command_handler.capability_query_results<EOL>

Retrieve the data returned by a previous call to capability_query()
:return: Raw capability data returned by firmata


port = pin // <NUM_LIT:8><EOL>command = [self._command_handler.REPORT_DIGITAL + port, self.REPORTING_ENABLE]<EOL>self._command_handler.send_command(command)<EOL>

Enables digital reporting. By turning reporting on for all 8 bits in the "port" -
this is part of Firmata's protocol specification.

:param pin: Pin and all pins for this port

:return: No return value


r_data = self._command_handler.last_pin_query_results<EOL>self._command_handler.last_pin_query_results = []<EOL>return r_data<EOL>

This method returns the results of a previous call to pin_state_query() and then resets
the pin state query data to None

:return: Raw pin state query data


def play_tone(self, pin, tone_command, frequency, duration):

<EOL>if tone_command == self.TONE_TONE:<EOL><INDENT>if duration:<EOL><INDENT>data = [tone_command, pin, frequency & <NUM_LIT>, (frequency >> <NUM_LIT:7>) & <NUM_LIT>, duration & <NUM_LIT>, (duration >> <NUM_LIT:7>) & <NUM_LIT>]<EOL><DEDENT>else:<EOL><INDENT>data = [tone_command, pin, frequency & <NUM_LIT>, (frequency >> <NUM_LIT:7>) & <NUM_LIT>, <NUM_LIT:0>, <NUM_LIT:0>]<EOL><DEDENT>self._command_handler.digital_response_table[pin][self._command_handler.RESPONSE_TABLE_MODE] =self.TONE<EOL><DEDENT>else:<EOL><INDENT>data = [tone_command, pin]<EOL><DEDENT>self._command_handler.send_sysex(self._command_handler.TONE_PLAY, data)<EOL>

This method will call the Tone library for the selected pin.
If the tone command is set to TONE_TONE, then the specified tone will be played.
Else, if the tone command is TONE_NO_TONE, then any currently playing tone will be disabled.
It is intended for a future release of Arduino Firmata

:param pin: Pin number

:param tone_command: Either TONE_TONE, or TONE_NO_TONE

:param frequency: Frequency of tone in hz

:param duration: Duration of tone in milliseconds

:return: No return value


analog_data = [pin, data & <NUM_LIT>, (data >> <NUM_LIT:7>) & <NUM_LIT>, (data >> <NUM_LIT>) & <NUM_LIT>]<EOL>self._command_handler.send_sysex(self._command_handler.EXTENDED_ANALOG, analog_data)<EOL>

This method will send an extended data analog output command to the selected pin

:param pin: 0 - 127

:param data: 0 - 0xfffff


<EOL>for pin in range(<NUM_LIT:0>, self._command_handler.total_pins_discovered):<EOL><INDENT>if self._command_handler.digital_response_table[self._command_handler.RESPONSE_TABLE_MODE]== self.PWM:<EOL><INDENT>self.analog_write(pin, <NUM_LIT:0>)<EOL><DEDENT>elif self._command_handler.digital_response_table[self._command_handler.RESPONSE_TABLE_MODE]== self.SERVO:<EOL><INDENT>self.analog_write(pin, <NUM_LIT:0>)<EOL><DEDENT>elif self._command_handler.digital_response_table[self._command_handler.RESPONSE_TABLE_MODE]== self.TONE:<EOL><INDENT>data = [self.TONE_NO_TONE, pin]<EOL>self._command_handler.send_sysex(self._command_handler.TONE_PLAY, data)<EOL><DEDENT>else:<EOL><INDENT>self.digital_write(pin, <NUM_LIT:0>)<EOL><DEDENT><DEDENT>self._command_handler.system_reset()<EOL>

This command sends a reset message to the Arduino. The response tables will be reinitialized
:return: No return value.


self._command_handler.send_sysex(self._command_handler.REPORT_FIRMWARE, None)<EOL>

This method will query firmata to report firmware. Retrieve the report via a
call to get_firmata_firmware_version()


with self.data_lock:<EOL><INDENT>data =self._command_handler.digital_response_table[pin][self._command_handler.RESPONSE_TABLE_PIN_DATA_VALUE]<EOL><DEDENT>return data<EOL>

Retrieve the last digital data value received for the specified pin.
NOTE: This command will return values for digital, pwm, etc,  pin types

:param pin: Selected pin

:return: The last value entered into the digital response table.


def set_analog_latch(self, pin, threshold_type, threshold_value, cb=None):

if self.ANALOG_LATCH_GT <= threshold_type <= self.ANALOG_LATCH_LTE:<EOL><INDENT>if <NUM_LIT:0> <= threshold_value <= <NUM_LIT>:<EOL><INDENT>self._command_handler.set_analog_latch(pin, threshold_type, threshold_value, cb)<EOL>return True<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT>

This method "arms" an analog pin for its data to be latched and saved in the latching table
If a callback method is provided, when latching criteria is achieved, the callback function is called
with latching data notification. In that case, the latching table is not updated.

:param pin: Analog pin number (value following an 'A' designator, i.e. A5 = 5

:param threshold_type: ANALOG_LATCH_GT | ANALOG_LATCH_LT  | ANALOG_LATCH_GTE | ANALOG_LATCH_LTE

:param threshold_value: numerical value - between 0 and 1023

:param cb: callback method

:return: True if successful, False if parameter data is invalid


while not self.is_stopped():<EOL><INDENT>try:<EOL><INDENT>if self.arduino.inWaiting():<EOL><INDENT>c = self.arduino.read()<EOL>self.command_deque.append(ord(c))<EOL><DEDENT>else:<EOL><INDENT>time.sleep(<NUM_LIT>)<EOL><DEDENT><DEDENT>except OSError:<EOL><INDENT>pass<EOL><DEDENT>except IOError:<EOL><INDENT>self.stop()<EOL><DEDENT><DEDENT>self.close()<EOL>

This method continually runs. If an incoming character is available on the serial port
it is read and placed on the _command_deque
@return: Never Returns


def __init__(self, port_id, command_deque, baud_rate):

self.port_id = port_id<EOL>self.command_deque = command_deque<EOL>self.baud_rate = baud_rate<EOL>threading.Thread.__init__(self)<EOL>self.daemon = True<EOL>self.arduino = serial.Serial(self.port_id, self.baud_rate,<EOL>timeout=int(self.timeout), writeTimeout=<NUM_LIT:0>)<EOL>self.stop_event = threading.Event()<EOL>if sys.platform == '<STR_LIT>':<EOL><INDENT>self.arduino.nonblocking()<EOL><DEDENT>

Constructor:

:param command_deque: A reference to the deque shared with the _command_handler

:param baud_rate: must match that of Arduino Sketch


if brightness > <NUM_LIT:15>:<EOL><INDENT>brightness = <NUM_LIT:15><EOL><DEDENT>brightness |= <NUM_LIT><EOL>self.brightness = brightness<EOL>self.firmata.i2c_write(<NUM_LIT>, brightness)<EOL>

Set the brightness level for the entire display
@param brightness: brightness level (0 -15)


green = <NUM_LIT:0><EOL>red = <NUM_LIT:0><EOL>for row in range(<NUM_LIT:0>, <NUM_LIT:8>):<EOL><INDENT>for col in range(<NUM_LIT:0>, <NUM_LIT:8>):<EOL><INDENT>if self.display_buffer[row][col] == self.LED_GREEN:<EOL><INDENT>green |= <NUM_LIT:1> << col<EOL><DEDENT>elif self.display_buffer[row][col] == self.LED_RED:<EOL><INDENT>red |= <NUM_LIT:1> << col<EOL><DEDENT>elif self.display_buffer[row][col] == self.LED_YELLOW:<EOL><INDENT>green |= <NUM_LIT:1> << col<EOL>red |= <NUM_LIT:1> << col<EOL><DEDENT>elif self.display_buffer[row][col] == self.LED_OFF:<EOL><INDENT>green &= ~(<NUM_LIT:1> << col)<EOL>red &= ~(<NUM_LIT:1> << col)<EOL><DEDENT><DEDENT>self.firmata.i2c_write(<NUM_LIT>, row * <NUM_LIT:2>, <NUM_LIT:0>, green)<EOL>self.firmata.i2c_write(<NUM_LIT>, row * <NUM_LIT:2> + <NUM_LIT:1>, <NUM_LIT:0>, red)<EOL><DEDENT>


if b > <NUM_LIT:3>:<EOL><INDENT>b = <NUM_LIT:0>  <EOL><DEDENT>self.firmata.i2c_write(self.board_address,<EOL>(self.HT16K33_BLINK_CMD | self.HT16K33_BLINK_DISPLAYON | (b << <NUM_LIT:1>)))<EOL>

Set the user's desired blink rate (0 - 3)
@param b: blink rate


for row in range(<NUM_LIT:0>, <NUM_LIT:8>):<EOL><INDENT>self.firmata.i2c_write(<NUM_LIT>, row * <NUM_LIT:2>, <NUM_LIT:0>, <NUM_LIT:0>)<EOL>self.firmata.i2c_write(<NUM_LIT>, (row * <NUM_LIT:2>) + <NUM_LIT:1>, <NUM_LIT:0>, <NUM_LIT:0>)<EOL>for column in range(<NUM_LIT:0>, <NUM_LIT:8>):<EOL><INDENT>self.display_buffer[row][column] = <NUM_LIT:0><EOL><DEDENT><DEDENT>


def __init__(self, address, blink_rate, brightness):<EOL>

<EOL>self.firmata = PyMata("<STR_LIT>")<EOL>self.board_address = address<EOL>self.blink_rate = blink_rate<EOL>self.brightness = brightness<EOL>self.clear_display_buffer()<EOL>self.firmata.i2c_config(<NUM_LIT:0>, self.firmata.ANALOG, <NUM_LIT:4>, <NUM_LIT:5>)<EOL>self.oscillator_set(self.OSCILLATOR_ON)<EOL>self.set_blink_rate(self.blink_rate)<EOL>self.set_brightness(self.brightness)<EOL>

@param address: I2C address of the device
@param blink_rate: desired blink rate
@param brightness: brightness level for the display


def set_pixel(self, row, column, color, suppress_write):<EOL>

if (row < <NUM_LIT:0>) or (row >= <NUM_LIT:8>):<EOL><INDENT>print("<STR_LIT>")<EOL>return<EOL><DEDENT>if (column < <NUM_LIT:0>) or (column >= <NUM_LIT:8>):<EOL><INDENT>print("<STR_LIT>")<EOL>return<EOL><DEDENT>self.display_buffer[row][column] = color<EOL>green = <NUM_LIT:0><EOL>red = <NUM_LIT:0><EOL>for col in range(<NUM_LIT:0>, <NUM_LIT:8>):<EOL><INDENT>if self.display_buffer[row][col] == self.LED_GREEN:<EOL><INDENT>green |= <NUM_LIT:1> << col<EOL><DEDENT>elif self.display_buffer[row][col] == self.LED_RED:<EOL><INDENT>red |= <NUM_LIT:1> << col<EOL><DEDENT>elif self.display_buffer[row][col] == self.LED_YELLOW:<EOL><INDENT>green |= <NUM_LIT:1> << col<EOL>red |= <NUM_LIT:1> << col<EOL><DEDENT>elif self.display_buffer[row][col] == self.LED_OFF:<EOL><INDENT>green &= ~(<NUM_LIT:1> << col)<EOL>red &= ~(<NUM_LIT:1> << col)<EOL><DEDENT><DEDENT>if not suppress_write:<EOL><INDENT>self.firmata.i2c_write(<NUM_LIT>, row * <NUM_LIT:2>, <NUM_LIT:0>, green)<EOL>self.firmata.i2c_write(<NUM_LIT>, row * <NUM_LIT:2> + <NUM_LIT:1>, <NUM_LIT:0>, red)<EOL><DEDENT>

@param row: pixel row number
@param column: pix column number
@param color: pixel color (yellow is both red and green both on)
@param suppress_write: if true, just sets the internal data structure, else writes out the pixel to the display


for row in range(<NUM_LIT:0>, <NUM_LIT:8>):<EOL><INDENT>data = shape[row]<EOL>bit_mask = <NUM_LIT><EOL>for column in range(<NUM_LIT:0>, <NUM_LIT:8>):<EOL><INDENT>if data & bit_mask:<EOL><INDENT>self.set_pixel(row, column, color, True)<EOL><DEDENT>bit_mask >>= <NUM_LIT:1><EOL><DEDENT><DEDENT>self.output_entire_buffer()<EOL>

Populate the bit map with the supplied "shape" and color
and then write the entire bitmap to the display
@param shape: pattern to display
@param color: color for the pattern


del mapped['<STR_LIT>']<EOL>del mapped['<STR_LIT>']<EOL>

Remove definitions of the built functions


def _get_catalysts_in_reaction(reaction: Reaction) -> Set[BaseAbundance]:

return {<EOL>reactant<EOL>for reactant in reaction.reactants<EOL>if reactant in reaction.products<EOL>}<EOL>

Return nodes that are both in reactants and reactions in a reaction.


def list_abundance_cartesian_expansion(graph: BELGraph) -> None:

for u, v, k, d in list(graph.edges(keys=True, data=True)):<EOL><INDENT>if CITATION not in d:<EOL><INDENT>continue<EOL><DEDENT>if isinstance(u, ListAbundance) and isinstance(v, ListAbundance):<EOL><INDENT>for u_member, v_member in itt.product(u.members, v.members):<EOL><INDENT>graph.add_qualified_edge(<EOL>u_member, v_member,<EOL>relation=d[RELATION],<EOL>citation=d.get(CITATION),<EOL>evidence=d.get(EVIDENCE),<EOL>annotations=d.get(ANNOTATIONS),<EOL>)<EOL><DEDENT><DEDENT>elif isinstance(u, ListAbundance):<EOL><INDENT>for member in u.members:<EOL><INDENT>graph.add_qualified_edge(<EOL>member, v,<EOL>relation=d[RELATION],<EOL>citation=d.get(CITATION),<EOL>evidence=d.get(EVIDENCE),<EOL>annotations=d.get(ANNOTATIONS),<EOL>)<EOL><DEDENT><DEDENT>elif isinstance(v, ListAbundance):<EOL><INDENT>for member in v.members:<EOL><INDENT>graph.add_qualified_edge(<EOL>u, member,<EOL>relation=d[RELATION],<EOL>citation=d.get(CITATION),<EOL>evidence=d.get(EVIDENCE),<EOL>annotations=d.get(ANNOTATIONS),<EOL>)<EOL><DEDENT><DEDENT><DEDENT>_remove_list_abundance_nodes(graph)<EOL>

Expand all list abundances to simple subject-predicate-object networks.


def flatten_list_abundance(node: ListAbundance) -> ListAbundance:

return node.__class__(list(chain.from_iterable(<EOL>(<EOL>flatten_list_abundance(member).members<EOL>if isinstance(member, ListAbundance) else<EOL>[member]<EOL>)<EOL>for member in node.members<EOL>)))<EOL>

Flattens the complex or composite abundance.


def _reaction_cartesion_expansion_unqualified_helper(<EOL>graph: BELGraph,<EOL>u: BaseEntity,<EOL>v: BaseEntity,<EOL>d: dict,<EOL>) -> None:

if isinstance(u, Reaction) and isinstance(v, Reaction):<EOL><INDENT>enzymes = _get_catalysts_in_reaction(u) | _get_catalysts_in_reaction(v)<EOL>for reactant, product in chain(itt.product(u.reactants, u.products),<EOL>itt.product(v.reactants, v.products)):<EOL><INDENT>if reactant in enzymes or product in enzymes:<EOL><INDENT>continue<EOL><DEDENT>graph.add_unqualified_edge(<EOL>reactant, product, INCREASES<EOL>)<EOL><DEDENT>for product, reactant in itt.product(u.products, u.reactants):<EOL><INDENT>if reactant in enzymes or product in enzymes:<EOL><INDENT>continue<EOL><DEDENT>graph.add_unqualified_edge(<EOL>product, reactant, d[RELATION],<EOL>)<EOL><DEDENT><DEDENT>elif isinstance(u, Reaction):<EOL><INDENT>enzymes = _get_catalysts_in_reaction(u)<EOL>for product in u.products:<EOL><INDENT>if product in enzymes:<EOL><INDENT>continue<EOL><DEDENT>if v not in u.products and v not in u.reactants:<EOL><INDENT>graph.add_unqualified_edge(<EOL>product, v, INCREASES<EOL>)<EOL><DEDENT>for reactant in u.reactants:<EOL><INDENT>graph.add_unqualified_edge(<EOL>reactant, product, INCREASES<EOL>)<EOL><DEDENT><DEDENT><DEDENT>elif isinstance(v, Reaction):<EOL><INDENT>enzymes = _get_catalysts_in_reaction(v)<EOL>for reactant in v.reactants:<EOL><INDENT>if reactant in enzymes:<EOL><INDENT>continue<EOL><DEDENT>if u not in v.products and u not in v.reactants:<EOL><INDENT>graph.add_unqualified_edge(<EOL>u, reactant, INCREASES<EOL>)<EOL><DEDENT>for product in v.products:<EOL><INDENT>graph.add_unqualified_edge(<EOL>reactant, product, INCREASES<EOL>)<EOL><DEDENT><DEDENT><DEDENT>

Helper to deal with cartension expansion in unqualified edges.


def remove_reified_nodes(graph: BELGraph) -> None:

_remove_list_abundance_nodes(graph)<EOL>_remove_reaction_nodes(graph)<EOL>


def make_pubmed_abstract_group(pmids: Iterable[Union[str, int]]) -> Iterable[str]:

for pmid in set(pmids):<EOL><INDENT>yield '<STR_LIT>'<EOL>res = requests.get(title_url_fmt.format(pmid))<EOL>title = res.content.decode('<STR_LIT:utf-8>').strip()<EOL>yield '<STR_LIT>'.format(title, pmid)<EOL>res = requests.get(abstract_url_fmt.format(pmid))<EOL>abstract = res.content.decode('<STR_LIT:utf-8>').strip()<EOL>yield '<STR_LIT>'.format(abstract)<EOL>yield '<STR_LIT>'<EOL><DEDENT>

Build a skeleton for the citations' statements.

    :param pmids: A list of PubMed identifiers
    :return: An iterator over the lines of the citation section


def get_entrez_gene_data(entrez_ids: Iterable[Union[str, int]]):

url = PUBMED_GENE_QUERY_URL.format('<STR_LIT:U+002C>'.join(str(x).strip() for x in entrez_ids))<EOL>response = requests.get(url)<EOL>tree = ElementTree.fromstring(response.content)<EOL>return {<EOL>element.attrib['<STR_LIT>']: {<EOL>'<STR_LIT>': _sanitize(element.find('<STR_LIT>').text),<EOL>'<STR_LIT:description>': element.find('<STR_LIT>').text<EOL>}<EOL>for element in tree.findall('<STR_LIT>')<EOL>}<EOL>


def make_pubmed_gene_group(entrez_ids: Iterable[Union[str, int]]) -> Iterable[str]:

url = PUBMED_GENE_QUERY_URL.format('<STR_LIT:U+002C>'.join(str(x).strip() for x in entrez_ids))<EOL>response = requests.get(url)<EOL>tree = ElementTree.fromstring(response.content)<EOL>for x in tree.findall('<STR_LIT>'):<EOL><INDENT>yield '<STR_LIT>'.format(x.find('<STR_LIT>').text)<EOL>yield '<STR_LIT>'.format(x.attrib['<STR_LIT>'])<EOL>yield '<STR_LIT>'.format(x.find('<STR_LIT>').text.strip().replace('<STR_LIT:\n>', '<STR_LIT>'))<EOL>yield '<STR_LIT>'<EOL><DEDENT>

Builds a skeleton for gene summaries

    :param entrez_ids: A list of Entrez Gene identifiers to query the PubMed service
    :return: An iterator over statement lines for NCBI Entrez Gene summaries


for line in in_file:<EOL><INDENT>print(line.strip(), file=out_file)<EOL><DEDENT>

Helps remove extraneous whitespace from the lines of a file

    :param file in_file: A readable file or file-like
    :param file out_file: A writable file or file-like


@in_place_transformation<EOL>def remove_unweighted_leaves(graph: BELGraph, key: Optional[str] = None) -> None:

unweighted_leaves = list(get_unweighted_upstream_leaves(graph, key=key))<EOL>graph.remove_nodes_from(unweighted_leaves)<EOL>

Remove nodes that are leaves and that don't have a weight (or other key) attribute set.

    :param graph: A BEL graph
    :param key: The key in the node data dictionary representing the experimental data. Defaults to
     :data:`pybel_tools.constants.WEIGHT`.


def get_unweighted_upstream_leaves(graph: BELGraph, key: Optional[str] = None) -> Iterable[BaseEntity]:

if key is None:<EOL><INDENT>key = WEIGHT<EOL><DEDENT>return filter_nodes(graph, [node_is_upstream_leaf, data_missing_key_builder(key)])<EOL>

Get nodes with no incoming edges, one outgoing edge, and without the given key in its data dictionary.

    .. seealso :: :func:`data_does_not_contain_key_builder`

    :param graph: A BEL graph
    :param key: The key in the node data dictionary representing the experimental data. Defaults to
     :data:`pybel_tools.constants.WEIGHT`.
    :return: An iterable over leaves (nodes with an in-degree of 0) that don't have the given annotation


@in_place_transformation<EOL>def remove_unweighted_sources(graph: BELGraph, key: Optional[str] = None) -> None:

nodes = list(get_unweighted_sources(graph, key=key))<EOL>graph.remove_nodes_from(nodes)<EOL>

Prune unannotated nodes on the periphery of the sub-graph.

    :param graph: A BEL graph
    :param key: The key in the node data dictionary representing the experimental data. Defaults to
     :data:`pybel_tools.constants.WEIGHT`.


def get_pair_tuple(a: BaseEntity, b: BaseEntity) -> Tuple[str, str, str, str]:

return a.as_bel(), a.sha512, b.as_bel(), b.sha512<EOL>


def get_network_summary_dict(graph: BELGraph) -> Mapping:

return dict(<EOL>function_count=count_functions(graph),<EOL>modifications_count=get_modifications_count(graph),<EOL>relation_count=count_relations(graph),<EOL>authors_count=count_authors(graph).most_common(<NUM_LIT:15>),<EOL>variants_count=count_variants(graph),<EOL>namespaces_count=count_namespaces(graph),<EOL>hub_data={<EOL>(<EOL>node.name or node.identifier<EOL>if NAME in node or IDENTIFIER in node else<EOL>str(node)<EOL>): degree<EOL>for node, degree in get_top_hubs(graph, n=<NUM_LIT:15>)<EOL>},<EOL>disease_data={<EOL>(<EOL>node.name or node.identifier<EOL>if NAME in node or IDENTIFIER in node else<EOL>str(node)<EOL>): count<EOL>for node, count in get_top_pathologies(graph, n=<NUM_LIT:15>)<EOL>},<EOL>regulatory_pairs=[<EOL>get_pair_tuple(u, v)<EOL>for u, v in get_regulatory_pairs(graph)<EOL>],<EOL>unstable_pairs=list(itt.chain(<EOL>(get_pair_tuple(u, v) + ('<STR_LIT>',) for u, v, in get_chaotic_pairs(graph)),<EOL>(get_pair_tuple(u, v) + ('<STR_LIT>',) for u, v, in get_dampened_pairs(graph)),<EOL>)),<EOL>contradictory_pairs=[<EOL>get_pair_tuple(u, v) + (relation,)<EOL>for u, v, relation in get_contradiction_summary(graph)<EOL>],<EOL>contradictory_triplets=list(itt.chain(<EOL>(get_triplet_tuple(a, b, c) + ('<STR_LIT>',) for a, b, c in<EOL>get_separate_unstable_correlation_triples(graph)),<EOL>(get_triplet_tuple(a, b, c) + ('<STR_LIT>',) for a, b, c in get_mutually_unstable_correlation_triples(graph)),<EOL>(get_triplet_tuple(a, b, c) + ('<STR_LIT>',) for a, b, c in get_jens_unstable(graph)),<EOL>(get_triplet_tuple(a, b, c) + ('<STR_LIT>',) for a, b, c in get_increase_mismatch_triplets(graph)),<EOL>(get_triplet_tuple(a, b, c) + ('<STR_LIT>',) for a, b, c in get_decrease_mismatch_triplets(graph)),<EOL>)),<EOL>unstable_triplets=list(itt.chain(<EOL>(get_triplet_tuple(a, b, c) + ('<STR_LIT>',) for a, b, c in get_chaotic_triplets(graph)),<EOL>(get_triplet_tuple(a, b, c) + ('<STR_LIT>',) for a, b, c in get_dampened_triplets(graph)),<EOL>)),<EOL>causal_pathologies=sorted({<EOL>get_pair_tuple(u, v) + (graph[u][v][k][RELATION],)<EOL>for u, v, k in filter_edges(graph, has_pathology_causal)<EOL>}),<EOL>undefined_namespaces=get_undefined_namespaces(graph),<EOL>undefined_annotations=get_undefined_annotations(graph),<EOL>namespaces_with_incorrect_names=get_namespaces_with_incorrect_names(graph),<EOL>unused_namespaces=get_unused_namespaces(graph),<EOL>unused_annotations=get_unused_annotations(graph),<EOL>unused_list_annotation_values=get_unused_list_annotation_values(graph),<EOL>naked_names=get_naked_names(graph),<EOL>error_count=count_error_types(graph),<EOL>error_groups=get_most_common_errors(graph),<EOL>syntax_errors=get_syntax_errors(graph),<EOL>citation_years=get_citation_years(graph),<EOL>confidence_count=count_confidences(graph),<EOL>)<EOL>


context = get_network_summary_dict(graph)<EOL>summary_dict = graph.summary_dict()<EOL>citation_years = context['<STR_LIT>']<EOL>function_count = context['<STR_LIT>']<EOL>relation_count = context['<STR_LIT>']<EOL>error_count = context['<STR_LIT>']<EOL>transformations_count = context['<STR_LIT>']<EOL>hub_data = context['<STR_LIT>']<EOL>disease_data = context['<STR_LIT>']<EOL>authors_count = context['<STR_LIT>']<EOL>variants_count = context['<STR_LIT>']<EOL>namespaces_count = context['<STR_LIT>']<EOL>confidence_count = context['<STR_LIT>']<EOL>confidence_data = [<EOL>(label, confidence_count.get(label, <NUM_LIT:0>))<EOL>for label in ('<STR_LIT:None>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>]<EOL>template = environment.get_template('<STR_LIT>')<EOL>return template.render(<EOL>graph=graph,<EOL>chart_1_data=prepare_c3(function_count, '<STR_LIT>'),<EOL>chart_6_data=prepare_c3(namespaces_count, '<STR_LIT>'),<EOL>chart_5_data=prepare_c3(variants_count, '<STR_LIT>'),<EOL>number_variants=sum(variants_count.values()),<EOL>number_namespaces=len(namespaces_count),<EOL>chart_2_data=prepare_c3(relation_count, '<STR_LIT>'),<EOL>chart_4_data=prepare_c3(transformations_count, '<STR_LIT>') if transformations_count else None,<EOL>number_transformations=sum(transformations_count.values()),<EOL>chart_3_data=prepare_c3(error_count, '<STR_LIT>') if error_count else None,<EOL>chart_7_data=prepare_c3(hub_data, '<STR_LIT>'),<EOL>chart_9_data=prepare_c3(disease_data, '<STR_LIT>') if disease_data else None,<EOL>chart_authors_count=prepare_c3(authors_count, '<STR_LIT>'),<EOL>chart_10_data=prepare_c3_time_series(citation_years, '<STR_LIT>') if citation_years else None,<EOL>chart_confidence_count=prepare_c3(confidence_data, '<STR_LIT>'),<EOL>summary_dict=summary_dict,<EOL>**context<EOL>)<EOL>

Render the graph as an HTML string.

    Common usage may involve writing to a file like:

    >>> from pybel.examples import sialic_acid_graph
    >>> with open('html_output.html', 'w') as file:
    ...     print(to_html(sialic_acid_graph), file=file)


def to_html(graph: BELGraph, chart: Optional[str] = None) -> str:

with open(os.path.join(HERE, '<STR_LIT>'), '<STR_LIT>') as f:<EOL><INDENT>html_template = Template(f.read())<EOL><DEDENT>return html_template.render(**_get_context(graph, chart=chart))<EOL>

Render the graph as an HTML string.

    Common usage may involve writing to a file like:

    >>> from pybel.examples import sialic_acid_graph
    >>> with open('ideogram_output.html', 'w') as file:
    ...     print(to_html(sialic_acid_graph), file=file)


def to_jupyter(graph: BELGraph, chart: Optional[str] = None) -> Javascript:

with open(os.path.join(HERE, '<STR_LIT>'), '<STR_LIT>') as f:<EOL><INDENT>js_template = Template(f.read())<EOL><DEDENT>return Javascript(js_template.render(**_get_context(graph, chart=chart)))<EOL>

Render the graph as JavaScript in a Jupyter Notebook.


return '<STR_LIT>'.join(random.sample('<STR_LIT>', <NUM_LIT:16>))<EOL>


@transformation<EOL>def get_subgraph_by_node_search(graph: BELGraph, query: Strings) -> BELGraph:

nodes = search_node_names(graph, query)<EOL>return get_subgraph_by_induction(graph, nodes)<EOL>

Get a sub-graph induced over all nodes matching the query string.

    :param graph: A BEL Graph
    :param query: A query string or iterable of query strings for node names

    Thinly wraps :func:`search_node_names` and :func:`get_subgraph_by_induction`.


@transformation<EOL>def get_largest_component(graph: BELGraph) -> BELGraph:

biggest_component_nodes = max(nx.weakly_connected_components(graph), key=len)<EOL>return subgraph(graph, biggest_component_nodes)<EOL>


return search_node_namespace_names(graph, query, namespace='<STR_LIT>')<EOL>

Search for nodes with the HGNC namespace and whose names containing a given string(s).

    :param pybel.BELGraph graph: A BEL graph
    :param query: The search query
    :type query: str or iter[str]
    :return: An iterator over nodes whose names match the search query
    :rtype: iter


path_graph = graph.subgraph(path_nodes)<EOL>node_in_degree_tuple = sorted([(n, d) for n, d in path_graph.in_degree().items()], key=itemgetter(<NUM_LIT:1>))<EOL>node_out_degree_tuple = sorted([(n, d) for n, d in path_graph.out_degree().items()], key=itemgetter(<NUM_LIT:1>),<EOL>reverse=True)<EOL>tied_root_index = <NUM_LIT:0><EOL>for i in range(<NUM_LIT:0>, (len(node_in_degree_tuple) - <NUM_LIT:1>)):<EOL><INDENT>if node_in_degree_tuple[i][<NUM_LIT:1>] < node_in_degree_tuple[i + <NUM_LIT:1>][<NUM_LIT:1>]:<EOL><INDENT>tied_root_index = i<EOL>break<EOL><DEDENT><DEDENT>if tied_root_index != <NUM_LIT:0>:<EOL><INDENT>root_tuple = max(node_out_degree_tuple[:tied_root_index], key=itemgetter(<NUM_LIT:1>))<EOL><DEDENT>else:<EOL><INDENT>root_tuple = node_in_degree_tuple[<NUM_LIT:0>]<EOL><DEDENT>return path_graph, root_tuple[<NUM_LIT:0>]<EOL>

Find the 'root' of the path -> The node with the lowest out degree, if multiple:
         root is the one with the highest out degree among those with lowest out degree

    :param pybel.BELGraph graph: A BEL Graph
    :param list[tuple] path_nodes: A list of nodes in their order in a path
    :return: A pair of the graph: graph of the path and the root node
    :rtype: tuple[pybel.BELGraph,tuple]


@lru_cache(maxsize=None)<EOL>def get_walks_exhaustive(graph, node, length):

if <NUM_LIT:0> == length:<EOL><INDENT>return (node,),<EOL><DEDENT>return tuple(<EOL>(node, key) + path<EOL>for neighbor in graph.edge[node]<EOL>for path in get_walks_exhaustive(graph, neighbor, length - <NUM_LIT:1>)<EOL>if node not in path<EOL>for key in graph.edge[node][neighbor]<EOL>)<EOL>

Gets all walks under a given length starting at a given node

    :param networkx.Graph graph: A graph
    :param node: Starting node
    :param int length: The length of walks to get
    :return: A list of paths
    :rtype: list[tuple]


def match_complex_metapath(graph, node, complex_metapath):

raise NotImplementedError<EOL>

Matches a complex metapath starting at the given node

    :param pybel.BELGraph graph: A BEL graph
    :param tuple node: A BEL node
    :param list[str] complex_metapath: An iterable of alternating BEL nodes and relations
    :return: An iterable over paths from the node matching the metapath
    :rtype: iter[tuple]


def match_simple_metapath(graph, node, simple_metapath):

if <NUM_LIT:0> == len(simple_metapath):<EOL><INDENT>yield node,<EOL><DEDENT>else:<EOL><INDENT>for neighbor in graph.edges[node]:<EOL><INDENT>if graph.nodes[neighbor][FUNCTION] == simple_metapath[<NUM_LIT:0>]:<EOL><INDENT>for path in match_simple_metapath(graph, neighbor, simple_metapath[<NUM_LIT:1>:]):<EOL><INDENT>if node not in path:<EOL><INDENT>yield (node,) + path<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>

Matches a simple metapath starting at the given node

    :param pybel.BELGraph graph: A BEL graph
    :param tuple node: A BEL node
    :param list[str] simple_metapath: A list of BEL Functions
    :return: An iterable over paths from the node matching the metapath
    :rtype: iter[tuple]


return [<EOL>graph.node[node][FUNCTION]<EOL>for node in nodes<EOL>]<EOL>

Converts a list of nodes to their corresponding functions

    :param list[tuple] nodes: A list of BEL node tuples
    :rtype: list[str]


return [<EOL>graph.nodes[node][FUNCTION]<EOL>for node in simple_walk<EOL>]<EOL>

Converts a walk into a sequence of BEL functions

    :param pybel.BELGraph graph: A BEL graph
    :param iter[tuple] simple_walk: An iterable of BEL nodes
    :return: A list of BEL functions of the walk
    :rtype: list[str]


def group_nodes_by_annotation_filtered(graph: BELGraph,<EOL>node_predicates: NodePredicates = None,<EOL>annotation: str = '<STR_LIT>',<EOL>) -> Mapping[str, Set[BaseEntity]]:

node_filter = concatenate_node_predicates(node_predicates)<EOL>return {<EOL>key: {<EOL>node<EOL>for node in nodes<EOL>if node_filter(graph, node)<EOL>}<EOL>for key, nodes in group_nodes_by_annotation(graph, annotation).items()<EOL>}<EOL>

Group the nodes occurring in edges by the given annotation, with a node filter applied.

    :param graph: A BEL graph
    :param node_predicates: A predicate or list of predicates (graph, node) -> bool
    :param annotation: The annotation to use for grouping
    :return: A dictionary of {annotation value: set of nodes}


def average_node_annotation(graph: BELGraph,<EOL>key: str,<EOL>annotation: str = '<STR_LIT>',<EOL>aggregator: Optional[Callable[[Iterable[X]], X]] = None,<EOL>) -> Mapping[str, X]:

if aggregator is None:<EOL><INDENT>def aggregator(x):<EOL><INDENT>"""<STR_LIT>"""<EOL>return sum(x) / len(x)<EOL><DEDENT><DEDENT>result = {}<EOL>for subgraph, nodes in group_nodes_by_annotation(graph, annotation).items():<EOL><INDENT>values = [graph.nodes[node][key] for node in nodes if key in graph.nodes[node]]<EOL>result[subgraph] = aggregator(values)<EOL><DEDENT>return result<EOL>

Groups graph into subgraphs and assigns each subgraph a score based on the average of all nodes values
    for the given node key

    :param pybel.BELGraph graph: A BEL graph
    :param key: The key in the node data dictionary representing the experimental data
    :param annotation: A BEL annotation to use to group nodes
    :param aggregator: A function from list of values -> aggregate value. Defaults to taking the average of a list of
                       floats.
    :type aggregator: lambda


return all(<EOL>is_edge_consistent(graph, u, v)<EOL>for u, v in graph.edges()<EOL>)<EOL>

Return if all edges are consistent in a graph. Wraps :func:`pybel_tools.utils.is_edge_consistent`.

    :param pybel.BELGraph graph: A BEL graph
    :return: Are all edges consistent
    :rtype: bool


if not graph.has_edge(u, v):<EOL><INDENT>raise ValueError('<STR_LIT>'.format(graph, u, v))<EOL><DEDENT>return <NUM_LIT:0> == len(set(d[RELATION] for d in graph.edge[u][v].values()))<EOL>

Check if all edges between two nodes have the same relation.

    :param pybel.BELGraph graph: A BEL Graph
    :param tuple u: The source BEL node
    :param tuple v: The target BEL node
    :return: If all edges from the source to target node have the same relation
    :rtype: bool


def export_namespace(graph, namespace, directory=None, cacheable=False):

directory = os.getcwd() if directory is None else directory<EOL>path = os.path.join(directory, '<STR_LIT>'.format(namespace))<EOL>with open(path, '<STR_LIT:w>') as file:<EOL><INDENT>log.info('<STR_LIT>', path)<EOL>right_names = get_names_by_namespace(graph, namespace)<EOL>log.info('<STR_LIT>', len(right_names), namespace)<EOL>wrong_names = get_incorrect_names_by_namespace(graph, namespace)<EOL>log.info('<STR_LIT>', len(right_names), namespace)<EOL>undefined_ns_names = get_undefined_namespace_names(graph, namespace)<EOL>log.info('<STR_LIT>', len(right_names), namespace)<EOL>names = (right_names | wrong_names | undefined_ns_names)<EOL>if <NUM_LIT:0> == len(names):<EOL><INDENT>log.warning('<STR_LIT>', namespace)<EOL><DEDENT>write_namespace(<EOL>namespace_name=namespace,<EOL>namespace_keyword=namespace,<EOL>namespace_domain='<STR_LIT>',<EOL>author_name=graph.authors,<EOL>author_contact=graph.contact,<EOL>citation_name=graph.name,<EOL>values=names,<EOL>cacheable=cacheable,<EOL>file=file<EOL>)<EOL><DEDENT>

Exports all names and missing names from the given namespace to its own BEL Namespace files in the given
    directory.

    Could be useful during quick and dirty curation, where planned namespace building is not a priority.

    :param pybel.BELGraph graph: A BEL graph
    :param str namespace: The namespace to process
    :param str directory: The path to the directory where to output the namespace. Defaults to the current working
                      directory returned by :func:`os.getcwd`
    :param bool cacheable: Should the namespace be cacheable? Defaults to ``False`` because, in general, this operation
                        will probably be used for evil, and users won't want to reload their entire cache after each
                        iteration of curation.


enrich_complexes(graph)<EOL>enrich_composites(graph)<EOL>enrich_reactions(graph)<EOL>enrich_variants(graph)<EOL>

Enrich the sub-graph with the unqualified edges from the graph.

    The reason you might want to do this is you induce a sub-graph from the original graph based on an annotation
    filter, but the unqualified edges that don't have annotations that most likely connect elements within your graph
    are not included.

    .. seealso::

        This function thinly wraps the successive application of the following functions:

        - :func:`enrich_complexes`
        - :func:`enrich_composites`
        - :func:`enrich_reactions`
        - :func:`enrich_variants`

    Equivalent to:

    >>> enrich_complexes(graph)
    >>> enrich_composites(graph)
    >>> enrich_reactions(graph)
    >>> enrich_variants(graph)


@uni_in_place_transformation<EOL>def enrich_complexes(graph: BELGraph) -> None:

nodes = list(get_nodes_by_function(graph, COMPLEX))<EOL>for u in nodes:<EOL><INDENT>for v in u.members:<EOL><INDENT>graph.add_has_component(u, v)<EOL><DEDENT><DEDENT>

Add all of the members of the complex abundances to the graph.


def count_targets(edge_iter: EdgeIterator) -> Counter:

return Counter(v for _, v, _ in edge_iter)<EOL>

Count the target nodes in an edge iterator with keys and data.

    :return: A counter of target nodes in the iterable


def get_subgraph_edges(graph: BELGraph,<EOL>annotation: str,<EOL>value: str,<EOL>source_filter=None,<EOL>target_filter=None,<EOL>):

if source_filter is None:<EOL><INDENT>source_filter = keep_node_permissive<EOL><DEDENT>if target_filter is None:<EOL><INDENT>target_filter = keep_node_permissive<EOL><DEDENT>for u, v, k, data in graph.edges(keys=True, data=True):<EOL><INDENT>if not edge_has_annotation(data, annotation):<EOL><INDENT>continue<EOL><DEDENT>if data[ANNOTATIONS][annotation] == value and source_filter(graph, u) and target_filter(graph, v):<EOL><INDENT>yield u, v, k, data<EOL><DEDENT><DEDENT>

Gets all edges from a given subgraph whose source and target nodes pass all of the given filters

    :param pybel.BELGraph graph: A BEL graph
    :param str annotation:  The annotation to search
    :param str value: The annotation value to search by
    :param source_filter: Optional filter for source nodes (graph, node) -> bool
    :param target_filter: Optional filter for target nodes (graph, node) -> bool
    :return: An iterable of (source node, target node, key, data) for all edges that match the annotation/value and
             node filters
    :rtype: iter[tuple]


def get_peripheral_successor_edges(graph: BELGraph, subgraph: BELGraph) -> EdgeIterator:

for u in subgraph:<EOL><INDENT>for _, v, k in graph.out_edges(u, keys=True):<EOL><INDENT>if v not in subgraph:<EOL><INDENT>yield u, v, k<EOL><DEDENT><DEDENT><DEDENT>

Get the set of possible successor edges peripheral to the sub-graph.

    The source nodes in this iterable are all inside the sub-graph, while the targets are outside.


@uni_in_place_transformation<EOL>def expand_periphery(universe: BELGraph,<EOL>graph: BELGraph,<EOL>node_predicates: NodePredicates = None,<EOL>edge_predicates: EdgePredicates = None,<EOL>threshold: int = <NUM_LIT:2>,<EOL>) -> None:

nd = get_subgraph_peripheral_nodes(universe, graph, node_predicates=node_predicates,<EOL>edge_predicates=edge_predicates)<EOL>for node, dd in nd.items():<EOL><INDENT>pred_d = dd['<STR_LIT>']<EOL>succ_d = dd['<STR_LIT>']<EOL>in_subgraph_connections = set(pred_d) | set(succ_d)<EOL>if threshold > len(in_subgraph_connections):<EOL><INDENT>continue<EOL><DEDENT>graph.add_node(node, attr_dict=universe[node])<EOL>for u, edges in pred_d.items():<EOL><INDENT>for k, d in edges:<EOL><INDENT>safe_add_edge(graph, u, node, k, d)<EOL><DEDENT><DEDENT>for v, edges in succ_d.items():<EOL><INDENT>for k, d in edges:<EOL><INDENT>safe_add_edge(graph, node, v, k, d)<EOL><DEDENT><DEDENT><DEDENT>

Iterates over all possible edges, peripheral to a given subgraph, that could be added from the given graph.
    Edges could be added if they go to nodes that are involved in relationships that occur with more than the
    threshold (default 2) number of nodes in the subgraph.

    :param universe: The universe of BEL knowledge
    :param graph: The (sub)graph to expand
    :param threshold: Minimum frequency of betweenness occurrence to add a gap node

    A reasonable edge filter to use is :func:`pybel_tools.filters.keep_causal_edges` because this function can allow
    for huge expansions if there happen to be hub nodes.


@uni_in_place_transformation<EOL>def expand_internal_causal(universe: BELGraph, graph: BELGraph) -> None:

expand_internal(universe, graph, edge_predicates=is_causal_relation)<EOL>

Add causal edges between entities in the sub-graph.

    Is an extremely thin wrapper around :func:`expand_internal`.

    :param universe: A BEL graph representing the universe of all knowledge
    :param graph: The target BEL graph to enrich with causal relations between contained nodes

    Equivalent to:

    >>> from pybel_tools.mutation import expand_internal
    >>> from pybel.struct.filters.edge_predicates import is_causal_relation
    >>> expand_internal(universe, graph, edge_predicates=is_causal_relation)


@uni_in_place_transformation<EOL>def expand_internal(universe: BELGraph, graph: BELGraph, edge_predicates: EdgePredicates = None) -> None:

edge_filter = and_edge_predicates(edge_predicates)<EOL>for u, v in itt.product(graph, repeat=<NUM_LIT:2>):<EOL><INDENT>if graph.has_edge(u, v) or not universe.has_edge(u, v):<EOL><INDENT>continue<EOL><DEDENT>rs = defaultdict(list)<EOL>for key, data in universe[u][v].items():<EOL><INDENT>if not edge_filter(universe, u, v, key):<EOL><INDENT>continue<EOL><DEDENT>rs[data[RELATION]].append((key, data))<EOL><DEDENT>if <NUM_LIT:1> == len(rs):<EOL><INDENT>relation = list(rs)[<NUM_LIT:0>]<EOL>for key, data in rs[relation]:<EOL><INDENT>graph.add_edge(u, v, key=key, **data)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>log.debug('<STR_LIT>', u, v)<EOL><DEDENT><DEDENT>

Edges between entities in the sub-graph that pass the given filters.

    :param universe: The full graph
    :param graph: A sub-graph to find the upstream information
    :param edge_predicates: Optional list of edge filter functions (graph, node, node, key, data) -> bool


def count_sources(edge_iter: EdgeIterator) -> Counter:

return Counter(u for u, _, _ in edge_iter)<EOL>

Count the source nodes in an edge iterator with keys and data.

    :return: A counter of source nodes in the iterable


def build_delete_node_by_hash(manager: Manager) -> Callable[[BELGraph, str], None]:

@in_place_transformation<EOL>def delete_node_by_hash(graph: BELGraph, node_hash: str) -> None:<EOL><INDENT>"""<STR_LIT>"""<EOL>node = manager.get_dsl_by_hash(node_hash)<EOL>graph.remove_node(node)<EOL><DEDENT>return delete_node_by_hash<EOL>

Make a delete function that's bound to the manager.


def build_expand_node_neighborhood_by_hash(manager: Manager) -> Callable[[BELGraph, BELGraph, str], None]:

@uni_in_place_transformation<EOL>def expand_node_neighborhood_by_hash(universe: BELGraph, graph: BELGraph, node_hash: str) -> None:<EOL><INDENT>"""<STR_LIT>"""<EOL>node = manager.get_dsl_by_hash(node_hash)<EOL>return expand_node_neighborhood(universe, graph, node)<EOL><DEDENT>return expand_node_neighborhood_by_hash<EOL>

Make an expand function that's bound to the manager.


@in_place_transformation<EOL>def remove_inconsistent_edges(graph: BELGraph) -> None:

for u, v in get_inconsistent_edges(graph):<EOL><INDENT>edges = [(u, v, k) for k in graph[u][v]]<EOL>graph.remove_edges_from(edges)<EOL><DEDENT>

Remove all edges between node pairs with inconsistent edges.

    This is the all-or-nothing approach. It would be better to do more careful investigation of the evidences during
    curation.


def _collapse_edge_by_namespace(graph: BELGraph,<EOL>victim_namespaces: Strings,<EOL>survivor_namespaces: str,<EOL>relations: Strings) -> None:

relation_filter = build_relation_predicate(relations)<EOL>source_namespace_filter = build_source_namespace_filter(victim_namespaces)<EOL>target_namespace_filter = build_target_namespace_filter(survivor_namespaces)<EOL>edge_predicates = [<EOL>relation_filter,<EOL>source_namespace_filter,<EOL>target_namespace_filter<EOL>]<EOL>_collapse_edge_passing_predicates(graph, edge_predicates=edge_predicates)<EOL>

Collapse pairs of nodes with the given namespaces that have the given relationship.

    :param graph: A BEL Graph
    :param victim_namespaces: The namespace(s) of the node to collapse
    :param survivor_namespaces: The namespace of the node to keep
    :param relations: The relation(s) to search


@in_place_transformation<EOL>def collapse_protein_variants(graph: BELGraph) -> None:

_collapse_variants_by_function(graph, PROTEIN)<EOL>

Collapse all protein's variants' edges to their parents, in-place.


@in_place_transformation<EOL>def collapse_gene_variants(graph: BELGraph) -> None:

_collapse_variants_by_function(graph, GENE)<EOL>

Collapse all gene's variants' edges to their parents, in-place.


@in_place_transformation<EOL>def collapse_equivalencies_by_namespace(graph: BELGraph, victim_namespace: Strings, survivor_namespace: str) -> None:

_collapse_edge_by_namespace(graph, victim_namespace, survivor_namespace, EQUIVALENT_TO)<EOL>

Collapse pairs of nodes with the given namespaces that have equivalence relationships.

    :param graph: A BEL graph
    :param victim_namespace: The namespace(s) of the node to collapse
    :param survivor_namespace: The namespace of the node to keep

    To convert all ChEBI names to InChI keys, assuming there are appropriate equivalence relations between nodes with
    those namespaces:

    >>> collapse_equivalencies_by_namespace(graph, 'CHEBI', 'CHEBIID')
    >>> collapse_equivalencies_by_namespace(graph, 'CHEBIID', 'INCHI')


@in_place_transformation<EOL>def collapse_nodes_with_same_names(graph: BELGraph) -> None:

survivor_mapping = defaultdict(set) <EOL>victims = set() <EOL>it = tqdm(itt.combinations(graph, r=<NUM_LIT:2>), total=graph.number_of_nodes() * (graph.number_of_nodes() - <NUM_LIT:1>) / <NUM_LIT:2>)<EOL>for a, b in it:<EOL><INDENT>if b in victims:<EOL><INDENT>continue<EOL><DEDENT>a_name, b_name = a.get(NAME), b.get(NAME)<EOL>if not a_name or not b_name or a_name.lower() != b_name.lower():<EOL><INDENT>continue<EOL><DEDENT>if a.keys() != b.keys():  <EOL><INDENT>continue<EOL><DEDENT>for k in set(a.keys()) - {NAME, NAMESPACE}:<EOL><INDENT>if a[k] != b[k]:  <EOL><INDENT>continue<EOL><DEDENT><DEDENT>survivor_mapping[a].add(b)<EOL>victims.add(b)<EOL><DEDENT>collapse_nodes(graph, survivor_mapping)<EOL>

Collapse all nodes with the same name, merging namespaces by picking first alphabetical one.


@transformation<EOL>def collapse_to_protein_interactions(graph: BELGraph) -> BELGraph:

rv: BELGraph = graph.copy()<EOL>collapse_to_genes(rv)<EOL>def is_edge_ppi(_: BELGraph, u: BaseEntity, v: BaseEntity, __: str) -> bool:<EOL><INDENT>"""<STR_LIT>"""<EOL>return isinstance(u, Gene) and isinstance(v, Gene)<EOL><DEDENT>return get_subgraph_by_edge_filter(rv, edge_predicates=[has_polarity, is_edge_ppi])<EOL>

Collapse to a graph made of only causal gene/protein edges.


collapse_orthologies_by_namespace(graph, '<STR_LIT>', '<STR_LIT>')<EOL>


collapse_orthologies_by_namespace(graph, ['<STR_LIT>', '<STR_LIT>'], '<STR_LIT>')<EOL>


@in_place_transformation<EOL>def enrich_pubmed_citations(graph: BELGraph, manager: Manager) -> Set[str]:

pmids = get_pubmed_identifiers(graph)<EOL>pmid_data, errors = get_citations_by_pmids(manager=manager, pmids=pmids)<EOL>for u, v, k in filter_edges(graph, has_pubmed):<EOL><INDENT>pmid = graph[u][v][k][CITATION][CITATION_REFERENCE].strip()<EOL>if pmid not in pmid_data:<EOL><INDENT>log.warning('<STR_LIT>', pmid)<EOL>errors.add(pmid)<EOL>continue<EOL><DEDENT>graph[u][v][k][CITATION].update(pmid_data[pmid])<EOL><DEDENT>return errors<EOL>

Overwrite all PubMed citations with values from NCBI's eUtils lookup service.

    :return: A set of PMIDs for which the eUtils service crashed


@uni_in_place_transformation<EOL>def update_context(universe: BELGraph, graph: BELGraph):

for namespace in get_namespaces(graph):<EOL><INDENT>if namespace in universe.namespace_url:<EOL><INDENT>graph.namespace_url[namespace] = universe.namespace_url[namespace]<EOL><DEDENT>elif namespace in universe.namespace_pattern:<EOL><INDENT>graph.namespace_pattern[namespace] = universe.namespace_pattern[namespace]<EOL><DEDENT>else:<EOL><INDENT>log.warning('<STR_LIT>', namespace)<EOL><DEDENT><DEDENT>for annotation in get_annotations(graph):<EOL><INDENT>if annotation in universe.annotation_url:<EOL><INDENT>graph.annotation_url[annotation] = universe.annotation_url[annotation]<EOL><DEDENT>elif annotation in universe.annotation_pattern:<EOL><INDENT>graph.annotation_pattern[annotation] = universe.annotation_pattern[annotation]<EOL><DEDENT>elif annotation in universe.annotation_list:<EOL><INDENT>graph.annotation_list[annotation] = universe.annotation_list[annotation]<EOL><DEDENT>else:<EOL><INDENT>log.warning('<STR_LIT>', annotation)<EOL><DEDENT><DEDENT>

Update the context of a subgraph from the universe of all knowledge.


@in_place_transformation<EOL>def remove_highlight_edges(graph: BELGraph, edges=None):

for u, v, k, _ in graph.edges(keys=True, data=True) if edges is None else edges:<EOL><INDENT>if is_edge_highlighted(graph, u, v, k):<EOL><INDENT>del graph[u][v][k][EDGE_HIGHLIGHT]<EOL><DEDENT><DEDENT>

Remove the highlight from the given edges, or all edges if none given.

    :param graph: A BEL graph
    :param edges: The edges (4-tuple of u,v,k,d) to remove the highlight from)
    :type edges: iter[tuple]


@in_place_transformation<EOL>def remove_highlight_nodes(graph: BELGraph, nodes: Optional[Iterable[BaseEntity]]=None) -> None:

for node in graph if nodes is None else nodes:<EOL><INDENT>if is_node_highlighted(graph, node):<EOL><INDENT>del graph.node[node][NODE_HIGHLIGHT]<EOL><DEDENT><DEDENT>

Removes the highlight from the given nodes, or all nodes if none given.

    :param graph: A BEL graph
    :param nodes: The list of nodes to un-highlight


@in_place_transformation<EOL>def highlight_edges(graph: BELGraph, edges=None, color: Optional[str]=None) -> None:

color = color or EDGE_HIGHLIGHT_DEFAULT_COLOR<EOL>for u, v, k, d in edges if edges is not None else graph.edges(keys=True, data=True):<EOL><INDENT>graph[u][v][k][EDGE_HIGHLIGHT] = color<EOL><DEDENT>

Adds a highlight tag to the given edges.

    :param graph: A BEL graph
    :param edges: The edges (4-tuples of u, v, k, d) to add a highlight tag on
    :type edges: iter[tuple]
    :param str color: The color to highlight (use something that works with CSS)


@in_place_transformation<EOL>def remove_highlight_subgraph(graph: BELGraph, subgraph: BELGraph):

remove_highlight_nodes(graph, subgraph.nodes())<EOL>remove_highlight_edges(graph, subgraph.edges())<EOL>

Remove the highlight from all nodes/edges in the graph that are in the subgraph.

    :param graph: The BEL graph to mutate
    :param subgraph: The subgraph from which to remove the highlighting


@in_place_transformation<EOL>def highlight_nodes(graph: BELGraph, nodes: Optional[Iterable[BaseEntity]] = None, color: Optional[str]=None):

color = color or NODE_HIGHLIGHT_DEFAULT_COLOR<EOL>for node in nodes if nodes is not None else graph:<EOL><INDENT>graph.node[node][NODE_HIGHLIGHT] = color<EOL><DEDENT>

Adds a highlight tag to the given nodes.

    :param graph: A BEL graph
    :param nodes: The nodes to add a highlight tag on
    :param color: The color to highlight (use something that works with CSS)


def is_edge_highlighted(graph: BELGraph, u, v, k) -> bool:

return EDGE_HIGHLIGHT in graph[u][v][k]<EOL>

Returns if the given edge is highlighted.

    :param graph: A BEL graph
    :return: Does the edge contain highlight information?
    :rtype: bool


@transformation<EOL>def random_by_edges(graph: BELGraph, percentage: Optional[float] = None) -> BELGraph:

percentage = percentage or <NUM_LIT><EOL>assert <NUM_LIT:0> < percentage <= <NUM_LIT:1><EOL>edges = graph.edges(keys=True)<EOL>n = int(graph.number_of_edges() * percentage)<EOL>subedges = random.sample(edges, n)<EOL>rv = graph.fresh_copy()<EOL>for u, v, k in subedges:<EOL><INDENT>safe_add_edge(rv, u, v, k, graph[u][v][k])<EOL><DEDENT>update_node_helper(graph, rv)<EOL>return rv<EOL>

Get a random graph by keeping a certain percentage of original edges.

    :param graph: A BEL graph
    :param percentage: What percentage of eges to take


@transformation<EOL>def random_by_nodes(graph: BELGraph, percentage: Optional[float] = None) -> BELGraph:

percentage = percentage or <NUM_LIT><EOL>assert <NUM_LIT:0> < percentage <= <NUM_LIT:1><EOL>nodes = graph.nodes()<EOL>n = int(len(nodes) * percentage)<EOL>subnodes = random.sample(nodes, n)<EOL>result = graph.subgraph(subnodes)<EOL>update_node_helper(graph, result)<EOL>return result<EOL>

Get a random graph by inducing over a percentage of the original nodes.

    :param graph: A BEL graph
    :param percentage: The percentage of edges to keep


@document.command()<EOL>@click.argument('<STR_LIT>', nargs=-<NUM_LIT:1>)<EOL>@connection_option<EOL>@click.option('<STR_LIT>', '<STR_LIT>', type=click.File('<STR_LIT:r>'), default=sys.stdin, help='<STR_LIT>')<EOL>@click.option('<STR_LIT>', '<STR_LIT>', help='<STR_LIT>'.format(os.getcwd()))<EOL>def serialize_namespaces(namespaces, connection: str, path, directory):

from .definition_utils import export_namespaces<EOL>graph = from_lines(path, manager=connection)<EOL>export_namespaces(namespaces, graph, directory)<EOL>

Parse a BEL document then serializes the given namespaces (errors and all) to the given directory.


@namespace.command()<EOL>@click.option('<STR_LIT>', '<STR_LIT>', type=click.File('<STR_LIT:r>'), default=sys.stdin, help="<STR_LIT>")<EOL>@click.option('<STR_LIT>', '<STR_LIT>', type=click.File('<STR_LIT:w>'), default=sys.stdout,<EOL>help="<STR_LIT>")<EOL>def convert_to_annotation(file, output):

resource = parse_bel_resource(file)<EOL>write_annotation(<EOL>keyword=resource['<STR_LIT>']['<STR_LIT>'],<EOL>values={k: '<STR_LIT>' for k in resource['<STR_LIT>']},<EOL>citation_name=resource['<STR_LIT>']['<STR_LIT>'],<EOL>description=resource['<STR_LIT>']['<STR_LIT>'],<EOL>file=output,<EOL>)<EOL>

Convert a namespace file to an annotation file.


@in_place_transformation<EOL>def overlay_type_data(graph: BELGraph,<EOL>data: Mapping[str, float],<EOL>func: str,<EOL>namespace: str,<EOL>label: Optional[str] = None,<EOL>overwrite: bool = False,<EOL>impute: Optional[float] = None,<EOL>) -> None:

new_data = {<EOL>node: data.get(node[NAME], impute)<EOL>for node in filter_nodes(graph, function_namespace_inclusion_builder(func, namespace))<EOL>}<EOL>overlay_data(graph, new_data, label=label, overwrite=overwrite)<EOL>

Overlay tabular data on the network for data that comes from an data set with identifiers that lack
    namespaces.

    For example, if you want to overlay differential gene expression data from a table, that table
    probably has HGNC identifiers, but no specific annotations that they are in the HGNC namespace or
    that the entities to which they refer are RNA.

    :param graph: A BEL Graph
    :param dict data: A dictionary of {name: data}
    :param func: The function of the keys in the data dictionary
    :param namespace: The namespace of the keys in the data dictionary
    :param label: The annotation label to put in the node dictionary
    :param overwrite: Should old annotations be overwritten?
    :param impute: The value to use for missing data


@in_place_transformation<EOL>def overlay_data(graph: BELGraph,<EOL>data: Mapping[BaseEntity, Any],<EOL>label: Optional[str] = None,<EOL>overwrite: bool = False,<EOL>) -> None:

if label is None:<EOL><INDENT>label = WEIGHT<EOL><DEDENT>for node, value in data.items():<EOL><INDENT>if node not in graph:<EOL><INDENT>log.debug('<STR_LIT>', node)<EOL>continue<EOL><DEDENT>if label in graph.nodes[node] and not overwrite:<EOL><INDENT>log.debug('<STR_LIT>', label, node)<EOL>continue<EOL><DEDENT>graph.nodes[node][label] = value<EOL><DEDENT>

Overlays tabular data on the network

    :param graph: A BEL Graph
    :param data: A dictionary of {tuple node: data for that node}
    :param label: The annotation label to put in the node dictionary
    :param overwrite: Should old annotations be overwritten?


def build_edge_data_filter(annotations: Mapping, partial_match: bool = True) -> EdgePredicate:

@edge_predicate<EOL>def annotation_dict_filter(data: EdgeData) -> bool:<EOL><INDENT>"""<STR_LIT>"""<EOL>return subdict_matches(data, annotations, partial_match=partial_match)<EOL><DEDENT>return annotation_dict_filter<EOL>

Build a filter that keeps edges whose data dictionaries are super-dictionaries to the given dictionary.

    :param annotations: The annotation query dict to match
    :param partial_match: Should the query values be used as partial or exact matches? Defaults to :code:`True`.


def build_target_namespace_filter(namespaces: Strings) -> EdgePredicate:

if isinstance(namespaces, str):<EOL><INDENT>def target_namespace_filter(_, __, v: BaseEntity, ___) -> bool:<EOL><INDENT>return node_has_namespace(v, namespaces)<EOL><DEDENT><DEDENT>elif isinstance(namespaces, Iterable):<EOL><INDENT>namespaces = set(namespaces)<EOL>def target_namespace_filter(_, __, v: BaseEntity, ___) -> bool:<EOL><INDENT>return node_has_namespaces(v, namespaces)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise TypeError<EOL><DEDENT>return target_namespace_filter<EOL>

Only passes for edges whose target nodes have the given namespace or one of the given namespaces

    :param namespaces: The namespace or namespaces to filter by


def node_has_namespace(node: BaseEntity, namespace: str) -> bool:

ns = node.get(NAMESPACE)<EOL>return ns is not None and ns == namespace<EOL>

Pass for nodes that have the given namespace.


def build_author_inclusion_filter(authors: Strings) -> EdgePredicate:

if isinstance(authors, str):<EOL><INDENT>@edge_predicate<EOL>def author_filter(data: EdgeData) -> bool:<EOL><INDENT>"""<STR_LIT>"""<EOL>return has_authors(data) and authors in data[CITATION][CITATION_AUTHORS]<EOL><DEDENT><DEDENT>elif isinstance(authors, Iterable):<EOL><INDENT>authors = set(authors)<EOL>@edge_predicate<EOL>def author_filter(data: EdgeData) -> bool:<EOL><INDENT>"""<STR_LIT>"""<EOL>return has_authors(data) and any(<EOL>author in data[CITATION][CITATION_AUTHORS]<EOL>for author in authors<EOL>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise TypeError<EOL><DEDENT>return author_filter<EOL>

Pass only for edges with author information that matches one of the given authors.

    :param authors: The author or list of authors to filter by


@register_deprecated('<STR_LIT>')<EOL>@in_place_transformation<EOL>def remove_mouse_nodes(graph: BELGraph) -> None:

remove_nodes_by_namespace(graph, ['<STR_LIT>', '<STR_LIT>'])<EOL>

Remove nodes using the MGI and MGIID namespaces.


@in_place_transformation<EOL>def remove_nodes_by_function_namespace(graph: BELGraph, func: str, namespace: Strings) -> None:

remove_filtered_nodes(graph, function_namespace_inclusion_builder(func, namespace))<EOL>

Remove nodes with the given function and namespace.

    This might be useful to exclude information learned about distant species, such as excluding all information
    from MGI and RGD in diseases where mice and rats don't give much insight to the human disease mechanism.


@register_deprecated('<STR_LIT>')<EOL>@in_place_transformation<EOL>def remove_rat_nodes(graph: BELGraph) -> None:

Remove nodes using the RGD and RGDID namespaces.


def function_exclusion_filter_builder(func: Strings) -> NodePredicate:

if isinstance(func, str):<EOL><INDENT>def function_exclusion_filter(_: BELGraph, node: BaseEntity) -> bool:<EOL><INDENT>"""<STR_LIT>"""<EOL>return node[FUNCTION] != func<EOL><DEDENT>return function_exclusion_filter<EOL><DEDENT>elif isinstance(func, Iterable):<EOL><INDENT>functions = set(func)<EOL>def functions_exclusion_filter(_: BELGraph, node: BaseEntity) -> bool:<EOL><INDENT>"""<STR_LIT>"""<EOL>return node[FUNCTION] not in functions<EOL><DEDENT>return functions_exclusion_filter<EOL><DEDENT>raise ValueError('<STR_LIT>'.format(func))<EOL>

Build a filter that fails on nodes of the given function(s).

    :param func: A BEL Function or list/set/tuple of BEL functions


def summarize_node_filter(graph: BELGraph, node_filters: NodePredicates) -> None:

passed = count_passed_node_filter(graph, node_filters)<EOL>print('<STR_LIT>'.format(passed, graph.number_of_nodes()))<EOL>

Print a summary of the number of nodes passing a given set of filters.

    :param graph: A BEL graph
    :param node_filters: A node filter or list/tuple of node filters


def node_exclusion_filter_builder(nodes: Iterable[BaseEntity]) -> NodePredicate:

node_set = set(nodes)<EOL>def exclusion_filter(_: BELGraph, node: BaseEntity) -> bool:<EOL><INDENT>"""<STR_LIT>"""<EOL>return node not in node_set<EOL><DEDENT>return exclusion_filter<EOL>

Build a filter that fails on nodes in the given list.


def node_inclusion_filter_builder(nodes: Iterable[BaseEntity]) -> NodePredicate:

node_set = set(nodes)<EOL>def inclusion_filter(_: BELGraph, node: BaseEntity) -> bool:<EOL><INDENT>"""<STR_LIT>"""<EOL>return node in node_set<EOL><DEDENT>return inclusion_filter<EOL>

Build a filter that only passes on nodes in the given list.

    :param nodes: An iterable of BEL nodes


def function_inclusion_filter_builder(func: Strings) -> NodePredicate:

if isinstance(func, str):<EOL><INDENT>return _single_function_inclusion_filter_builder(func)<EOL><DEDENT>elif isinstance(func, Iterable):<EOL><INDENT>return _collection_function_inclusion_builder(func)<EOL><DEDENT>raise ValueError('<STR_LIT>'.format(func))<EOL>

Build a filter that only passes on nodes of the given function(s).

    :param func: A BEL Function or list/set/tuple of BEL functions


def get_graph_by_id(self, network_id: int) -> BELGraph:

return self.networks[network_id]<EOL>


def insert_graph(self, graph: BELGraph, **_kwargs) -> Network:

result = _Namespace()<EOL>result.id = len(self.networks)<EOL>self.networks[result.id] = graph<EOL>return result<EOL>

Insert a graph and return the resulting ORM object (mocked).


def get_graphs_by_ids(self, network_ids: Iterable[int]) -> List[BELGraph]:

return [<EOL>self.networks[network_id]<EOL>for network_id in network_ids<EOL>]<EOL>

Get several graphs by their identifiers.


def count_unique_relations(graph: BELGraph) -> Counter:

return Counter(itt.chain.from_iterable(get_edge_relations(graph).values()))<EOL>

Return a histogram of the different types of relations present in a graph.

    Note: this operation only counts each type of edge once for each pair of nodes


def count_annotation_values(graph: BELGraph, annotation: str) -> Counter:

return Counter(iter_annotation_values(graph, annotation))<EOL>

Count in how many edges each annotation appears in a graph

    :param graph: A BEL graph
    :param annotation: The annotation to count
    :return: A Counter from {annotation value: frequency}