def split_by_base_count(infile, outfiles_prefix, max_bases, max_seqs=None):

seq_reader = sequences.file_reader(infile)<EOL>base_count = <NUM_LIT:0><EOL>file_count = <NUM_LIT:1><EOL>seq_count = <NUM_LIT:0><EOL>fout = None<EOL>if max_seqs is None:<EOL><INDENT>max_seqs = float('<STR_LIT>')<EOL><DEDENT>for seq in seq_reader:<EOL><INDENT>if base_count == <NUM_LIT:0>:<EOL><INDENT>fout = utils.open_file_write(outfiles_prefix + '<STR_LIT:.>' + str(file_count))<EOL>file_count += <NUM_LIT:1><EOL><DEDENT>if base_count + len(seq) > max_bases or seq_count >= max_seqs:<EOL><INDENT>if base_count == <NUM_LIT:0>:<EOL><INDENT>print(seq, file=fout)<EOL>utils.close(fout)<EOL><DEDENT>else:<EOL><INDENT>utils.close(fout)<EOL>fout = utils.open_file_write(outfiles_prefix + '<STR_LIT:.>' + str(file_count))<EOL>print(seq, file=fout)<EOL>base_count = len(seq)<EOL>file_count += <NUM_LIT:1><EOL>seq_count = <NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>base_count += len(seq)<EOL>seq_count += <NUM_LIT:1><EOL>print(seq, file=fout)<EOL><DEDENT><DEDENT>utils.close(fout)<EOL>

Splits a fasta/q file into separate files, file size determined by number of bases.

    Puts <= max_bases in each split file The exception is a single sequence >=max_bases
    is put in its own file.  This does not split sequences.


f = utils.open_file_write(outfile)<EOL>for seq in sequences.file_reader(infile):<EOL><INDENT>seq.replace_non_acgt()<EOL>print(seq, file=f)<EOL><DEDENT>utils.close(f)<EOL>

Replace every non-acgtn (case insensitve) character with an N


def interleave(infile_1, infile_2, outfile, suffix1=None, suffix2=None):

seq_reader_1 = sequences.file_reader(infile_1)<EOL>seq_reader_2 = sequences.file_reader(infile_2)<EOL>f_out = utils.open_file_write(outfile)<EOL>for seq_1 in seq_reader_1:<EOL><INDENT>try:<EOL><INDENT>seq_2 = next(seq_reader_2)<EOL><DEDENT>except:<EOL><INDENT>utils.close(f_out)<EOL>raise Error('<STR_LIT>', seq_1.id, '<STR_LIT>')<EOL><DEDENT>if suffix1 is not None and not seq_1.id.endswith(suffix1):<EOL><INDENT>seq_1.id += suffix1<EOL><DEDENT>if suffix2 is not None and not seq_2.id.endswith(suffix2):<EOL><INDENT>seq_2.id += suffix2<EOL><DEDENT>print(seq_1, file=f_out)<EOL>print(seq_2, file=f_out)<EOL><DEDENT>try:<EOL><INDENT>seq_2 = next(seq_reader_2)<EOL><DEDENT>except:<EOL><INDENT>seq_2 = None<EOL><DEDENT>if seq_2 is not None:<EOL><INDENT>utils.close(f_out)<EOL>raise Error('<STR_LIT>', seq_2.id, '<STR_LIT>')<EOL><DEDENT>utils.close(f_out)<EOL>

Makes interleaved file from two sequence files. If used, will append suffix1 onto end
    of every sequence name in infile_1, unless it already ends with suffix1. Similar for sufffix2.


total = <NUM_LIT:0><EOL>count = <NUM_LIT:0><EOL>seq_reader = sequences.file_reader(infile)<EOL>for seq in seq_reader:<EOL><INDENT>total += len(seq)<EOL>count += <NUM_LIT:1><EOL>if limit is not None and count >= limit:<EOL><INDENT>break<EOL><DEDENT><DEDENT>assert count > <NUM_LIT:0><EOL>return total / count<EOL>

Returns the mean length of the sequences in the input file. By default uses all sequences. To limit to the first N sequences, use limit=N


def sort_by_size(infile, outfile, smallest_first=False):

seqs = {}<EOL>file_to_dict(infile, seqs)<EOL>seqs = list(seqs.values())<EOL>seqs.sort(key=lambda x: len(x), reverse=not smallest_first)<EOL>fout = utils.open_file_write(outfile)<EOL>for seq in seqs:<EOL><INDENT>print(seq, file=fout)<EOL><DEDENT>utils.close(fout)<EOL>

Sorts input sequence file by biggest sequence first, writes sorted output file. Set smallest_first=True to have smallest first


seq_reader = sequences.file_reader(infile)<EOL>n = <NUM_LIT:0><EOL>for seq in seq_reader:<EOL><INDENT>n += <NUM_LIT:1><EOL><DEDENT>return n<EOL>

Returns the number of sequences in a file


def to_fastg(infile, outfile, circular=None):

if circular is None:<EOL><INDENT>to_circularise = set()<EOL><DEDENT>elif type(circular) is not set:<EOL><INDENT>f = utils.open_file_read(circular)<EOL>to_circularise = set([x.rstrip() for x in f.readlines()])<EOL>utils.close(f)<EOL><DEDENT>else:<EOL><INDENT>to_circularise = circular<EOL><DEDENT>seq_reader = sequences.file_reader(infile)<EOL>fout = utils.open_file_write(outfile)<EOL>nodes = <NUM_LIT:1><EOL>for seq in seq_reader:<EOL><INDENT>new_id = '<STR_LIT:_>'.join([<EOL>'<STR_LIT>', str(nodes),<EOL>'<STR_LIT>', str(len(seq)),<EOL>'<STR_LIT>', '<STR_LIT:1>',<EOL>'<STR_LIT>', seq.id<EOL>])<EOL>if seq.id in to_circularise:<EOL><INDENT>seq.id = new_id + '<STR_LIT::>' + new_id + '<STR_LIT:;>'<EOL>print(seq, file=fout)<EOL>seq.revcomp()<EOL>seq.id = new_id + "<STR_LIT>" + new_id + "<STR_LIT>"<EOL>print(seq, file=fout)<EOL><DEDENT>else:<EOL><INDENT>seq.id = new_id + '<STR_LIT:;>'<EOL>print(seq, file=fout)<EOL>seq.revcomp()<EOL>seq.id = new_id + "<STR_LIT>"<EOL>print(seq, file=fout)<EOL><DEDENT>nodes += <NUM_LIT:1><EOL><DEDENT>utils.close(fout)<EOL>

Writes a FASTG file in SPAdes format from input file. Currently only whether or not a sequence is circular is supported. Put circular=set of ids, or circular=filename to make those sequences circular in the output. Puts coverage=1 on all contigs


def split_by_fixed_size_onefile(infile, outfile, chunk_size, tolerance, skip_if_all_Ns=False):

seq_reader = sequences.file_reader(infile)<EOL>f_out = utils.open_file_write(outfile)<EOL>for seq in seq_reader:<EOL><INDENT>for i in range(<NUM_LIT:0>, len(seq), chunk_size):<EOL><INDENT>if i + chunk_size + tolerance >= len(seq):<EOL><INDENT>end = len(seq)<EOL><DEDENT>else:<EOL><INDENT>end = i + chunk_size<EOL><DEDENT>subseq = seq.subseq(i, end)<EOL>if not (skip_if_all_Ns and subseq.is_all_Ns()):<EOL><INDENT>subseq.id += '<STR_LIT:.>' + str(i+<NUM_LIT:1>) + '<STR_LIT:_>' + str(end)<EOL>print(subseq, file=f_out)<EOL><DEDENT>if end == len(seq):<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>utils.close(f_out)<EOL>

Splits each sequence in infile into chunks of fixed size, last chunk can be up to
       (chunk_size + tolerance) in length


def caf_to_fastq(infile, outfile, min_length=<NUM_LIT:0>, trim=False):

caf_reader = caf.file_reader(infile)<EOL>fout = utils.open_file_write(outfile)<EOL>for c in caf_reader:<EOL><INDENT>if trim:<EOL><INDENT>if c.clip_start is not None and c.clip_end is not None:<EOL><INDENT>c.seq.seq = c.seq.seq[c.clip_start:c.clip_end + <NUM_LIT:1>]<EOL>c.seq.qual = c.seq.qual[c.clip_start:c.clip_end + <NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>print('<STR_LIT>', c.id, file=sys.stderr)<EOL><DEDENT><DEDENT>if len(c.seq) >= min_length:<EOL><INDENT>print(c.seq, file=fout)<EOL><DEDENT><DEDENT>utils.close(fout)<EOL>

Convert a CAF file to fastq. Reads shorter than min_length are not output. If clipping information is in the CAF file (with a line Clipping QUAL ...) and trim=True, then trim the reads


if self.intersects(i):<EOL><INDENT>return Interval(max(self.start, i.start), min(self.end, i.end))<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>

If intervals intersect, returns their intersection, otherwise returns None


return Interval(min(self.start, i.start), max(self.end, i.end))<EOL>

Like union, but ignores whether the two intervals intersect or not


i = <NUM_LIT:0><EOL>l.sort()<EOL>while i < len(l) - <NUM_LIT:1>:<EOL><INDENT>if l[i+<NUM_LIT:1>].contains(l[i]):<EOL><INDENT>l.pop(i)<EOL><DEDENT>elif l[i].contains(l[i+<NUM_LIT:1>]):<EOL><INDENT>l.pop(i+<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>i += <NUM_LIT:1><EOL><DEDENT><DEDENT>

Sorts list in place, then removes any intervals that are completely
       contained inside another interval


if len(l1) == <NUM_LIT:0> or len(l2) == <NUM_LIT:0>:<EOL><INDENT>return []<EOL><DEDENT>out = []<EOL>l2_pos = <NUM_LIT:0><EOL>for l in l1:<EOL><INDENT>while l2_pos < len(l2) and l2[l2_pos].end < l.start:<EOL><INDENT>l2_pos += <NUM_LIT:1><EOL><DEDENT>if l2_pos == len(l2):<EOL><INDENT>break<EOL><DEDENT>while l2_pos < len(l2) and l.intersects(l2[l2_pos]):<EOL><INDENT>out.append(l.intersection(l2[l2_pos]))<EOL>l2_pos += <NUM_LIT:1><EOL><DEDENT>l2_pos = max(<NUM_LIT:0>, l2_pos - <NUM_LIT:1>)<EOL><DEDENT>return out<EOL>

Returns intersection of two lists.  Assumes the lists are sorted by start positions


return sum([len(x) for x in l])<EOL>

Returns total length of intervals from a list


return self.start <= i.start and i.end <= self.end<EOL>

Returns true iff this interval contains the interval i


return self.start <= i.end and i.start <= self.end<EOL>

Returns true iff this interval intersects the interval i


if self.intersects(i) or self.end + <NUM_LIT:1> == i.start or i.end + <NUM_LIT:1> == self.start:<EOL><INDENT>return Interval(min(self.start, i.start), max(self.end, i.end))<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>

If intervals intersect, returns their union, otherwise returns None


if not isinstance(instance, OctaveUserClass) or not instance._attrs:<EOL><INDENT>return dict()<EOL><DEDENT>dtype = []<EOL>values = []<EOL>for attr in instance._attrs:<EOL><INDENT>dtype.append((str(attr), object))<EOL>values.append(getattr(instance, attr))<EOL><DEDENT>struct = np.array([tuple(values)], dtype)<EOL>return MatlabObject(struct, instance._name)<EOL>


instance = OctaveUserClass.__new__(cls)<EOL>instance._address = '<STR_LIT>' % (instance._name, id(instance))<EOL>instance._ref().push(instance._address, value)<EOL>return instance<EOL>

This is how an instance is created when we read a
           MatlabObject from a MAT file.


attrs = session.eval('<STR_LIT>' % name, nout=<NUM_LIT:1>).ravel().tolist()<EOL>methods = session.eval('<STR_LIT>' % name, nout=<NUM_LIT:1>).ravel().tolist()<EOL>ref = weakref.ref(session)<EOL>doc = _DocDescriptor(ref, name)<EOL>values = dict(__doc__=doc, _name=name, _ref=ref, _attrs=attrs,<EOL>__module__='<STR_LIT>')<EOL>for method in methods:<EOL><INDENT>doc = _MethodDocDescriptor(ref, name, method)<EOL>cls_name = '<STR_LIT>' % (name, method)<EOL>method_values = dict(__doc__=doc)<EOL>method_cls = type(str(cls_name),<EOL>(OctaveUserClassMethod,), method_values)<EOL>values[method] = method_cls(ref, method, name)<EOL><DEDENT>for attr in attrs:<EOL><INDENT>values[attr] = OctaveUserClassAttr(ref, attr, attr)<EOL><DEDENT>return type(str(name), (OctaveUserClass,), values)<EOL>

Make an Octave class for a given class name


return OctavePtr(instance._ref, instance._name, instance._address)<EOL>


script = """<STR_LIT>"""<EOL>if not PY2:<EOL><INDENT>script = script.replace('<STR_LIT>', '<STR_LIT:input>')<EOL><DEDENT>for line in script.strip().split('<STR_LIT:\n>'):<EOL><INDENT>line = line.strip()<EOL>if not '<STR_LIT>' in line:<EOL><INDENT>time.sleep(delay)<EOL>print("<STR_LIT>".format(line))<EOL>time.sleep(delay)<EOL><DEDENT>if not interactive:<EOL><INDENT>if '<STR_LIT>' in line or '<STR_LIT>' in line or '<STR_LIT>' in line:<EOL><INDENT>line = '<STR_LIT>'<EOL><DEDENT><DEDENT>exec(line)<EOL><DEDENT>

Play a demo script showing most of the oct2py api features.

Parameters
==========
delay : float
    Time between each command in seconds.


for key, type_ in zip(keys, types):<EOL><INDENT>if not type(base[key]) == type_:<EOL><INDENT>try:<EOL><INDENT>assert type_(base[key]) == base[key], key<EOL><DEDENT>except ValueError:<EOL><INDENT>assert np.allclose(type_(base[key]), base[key])<EOL><DEDENT><DEDENT><DEDENT>

Perform type checking of the values

Parameters
==========
base : dict
    Sub-dictionary we are accessing.
keys : array-like
    List of keys to test in base.
types : array-like
    List of expected return types for the keys.


super(OctaveMagics, self).__init__(shell)<EOL>self._oct = oct2py.octave<EOL>self._display = display<EOL>

Parameters
----------
shell : IPython shell


octave = Oct2Py()<EOL>octave.push('<STR_LIT:name>', self.getName())<EOL>name = octave.pull('<STR_LIT:name>')<EOL>now = datetime.datetime.now()<EOL>print("<STR_LIT>".format(self.getName(), name, now))<EOL>octave.exit()<EOL>try:<EOL><INDENT>assert self.getName() == name<EOL><DEDENT>except AssertionError:  <EOL><INDENT>raise Oct2PyError('<STR_LIT>')<EOL><DEDENT>return<EOL>

Create a unique instance of Octave and verify namespace uniqueness.

Raises
======
Oct2PyError
    If the thread does not sucessfully demonstrate independence


if exist in [<NUM_LIT:2>, <NUM_LIT:5>]:<EOL><INDENT>return False<EOL><DEDENT>cmd = '<STR_LIT>' % name<EOL>resp = self._engine.eval(cmd, silent=True).strip()<EOL>return resp == '<STR_LIT>'<EOL>


func = _make_function_ptr_instance<EOL>self._function_ptrs.setdefault(name, func(self, name))<EOL>return self._function_ptrs[name]<EOL>

Get or create a function pointer of the given name.


def pull(self, var, timeout=None, verbose=True):

if isinstance(var, (str, unicode)):<EOL><INDENT>var = [var]<EOL><DEDENT>outputs = []<EOL>for name in var:<EOL><INDENT>exist = self._exist(name)<EOL>if exist == <NUM_LIT:1>:<EOL><INDENT>outputs.append(self.feval('<STR_LIT>', '<STR_LIT>', name,<EOL>timeout=timeout, verbose=verbose))<EOL><DEDENT>else:<EOL><INDENT>outputs.append(self.get_pointer(name, timeout=timeout))<EOL><DEDENT><DEDENT>if len(outputs) == <NUM_LIT:1>:<EOL><INDENT>return outputs[<NUM_LIT:0>]<EOL><DEDENT>return outputs<EOL>

Retrieve a value or values from the Octave session.

Parameters
----------
var : str or list
    Name of the variable(s) to retrieve.
timeout : float, optional.
    Time to wait for response from Octave (per line).
**kwargs: Deprecated kwargs, ignored.

Returns
-------
out : object
    Object returned by Octave.

Raises
------
Oct2PyError
    If the variable does not exist in the Octave session.

Examples
--------
  >>> from oct2py import octave
  >>> y = [1, 2]
  >>> octave.push('y', y)
  >>> octave.pull('y')
  array([[ 1.,  2.]])
  >>> octave.push(['x', 'y'], ['spam', [1, 2, 3, 4]])
  >>> octave.pull(['x', 'y'])  # doctest: +SKIP
  [u'spam', array([[1, 2, 3, 4]])]


def get_pointer(self, name, timeout=None):

exist = self._exist(name)<EOL>isobject = self._isobject(name, exist)<EOL>if exist == <NUM_LIT:0>:<EOL><INDENT>raise Oct2PyError('<STR_LIT>' % name)<EOL><DEDENT>elif exist == <NUM_LIT:1>:<EOL><INDENT>return _make_variable_ptr_instance(self, name)<EOL><DEDENT>elif isobject:<EOL><INDENT>return self._get_user_class(name)<EOL><DEDENT>elif exist in [<NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:5>]:<EOL><INDENT>return self._get_function_ptr(name)<EOL><DEDENT>raise Oct2PyError('<STR_LIT>' % name)<EOL>

Get a pointer to a named object in the Octave workspace.

        Parameters
        ----------
        name: str
            The name of the object in the Octave workspace.
        timemout: float, optional.
            Time to wait for response from Octave (per line).

        Examples
        --------
        >>> from oct2py import octave
        >>> octave.eval('foo = [1, 2];')
        >>> ptr = octave.get_pointer('foo')
        >>> ptr.value
        array([[ 1.,  2.]])
        >>> ptr.address
        'foo'
        >>> # Can be passed as an argument
        >>> octave.disp(ptr)  # doctest: +SKIP
        1  2

        >>> from oct2py import octave
        >>> sin = octave.get_pointer('sin')  # equivalent to `octave.sin`
        >>> sin.address
        '@sin'
        >>> x = octave.quad(sin, 0, octave.pi())
        >>> x
        2.0

        Notes
        -----
        Pointers can be passed to `feval` or dynamic functions as function arguments.  A pointer passed as a nested value will be passed by value instead.

        Raises
        ------
        Oct2PyError
            If the variable does not exist in the Octave session or is of
            unknown type.

        Returns
        -------
        A variable, object, user class, or function pointer as appropriate.


self.logger.debug(err)<EOL>stack = err.get('<STR_LIT>', [])<EOL>if not err['<STR_LIT:message>'].startswith('<STR_LIT>'):<EOL><INDENT>err['<STR_LIT:message>'] = '<STR_LIT>' + err['<STR_LIT:message>']<EOL><DEDENT>errmsg = '<STR_LIT>' % err['<STR_LIT:message>']<EOL>if not isinstance(stack, StructArray):<EOL><INDENT>return errmsg<EOL><DEDENT>errmsg += '<STR_LIT>'<EOL>for item in stack[:-<NUM_LIT:1>]:<EOL><INDENT>errmsg += '<STR_LIT>' % item<EOL>try:<EOL><INDENT>errmsg += '<STR_LIT>' % item<EOL><DEDENT>except Exception:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>return errmsg<EOL>

Create a traceback for an Octave evaluation error.


def _feval(self, func_name, func_args=(), dname='<STR_LIT>', nout=<NUM_LIT:0>,<EOL>timeout=None, stream_handler=None, store_as='<STR_LIT>', plot_dir=None):

engine = self._engine<EOL>if engine is None:<EOL><INDENT>raise Oct2PyError('<STR_LIT>')<EOL><DEDENT>out_file = osp.join(self.temp_dir, '<STR_LIT>')<EOL>out_file = out_file.replace(osp.sep, '<STR_LIT:/>')<EOL>in_file = osp.join(self.temp_dir, '<STR_LIT>')<EOL>in_file = in_file.replace(osp.sep, '<STR_LIT:/>')<EOL>func_args = list(func_args)<EOL>ref_indices = []<EOL>for (i, value) in enumerate(func_args):<EOL><INDENT>if isinstance(value, OctavePtr):<EOL><INDENT>ref_indices.append(i + <NUM_LIT:1>)<EOL>func_args[i] = value.address<EOL><DEDENT><DEDENT>ref_indices = np.array(ref_indices)<EOL>req = dict(func_name=func_name, func_args=tuple(func_args),<EOL>dname=dname or '<STR_LIT>', nout=nout,<EOL>store_as=store_as or '<STR_LIT>',<EOL>ref_indices=ref_indices)<EOL>write_file(req, out_file, oned_as=self._oned_as,<EOL>convert_to_float=self.convert_to_float)<EOL>engine.stream_handler = stream_handler or self.logger.info<EOL>if timeout is None:<EOL><INDENT>timeout = self.timeout<EOL><DEDENT>try:<EOL><INDENT>engine.eval('<STR_LIT>' % (out_file, in_file),<EOL>timeout=timeout)<EOL><DEDENT>except KeyboardInterrupt as e:<EOL><INDENT>stream_handler(engine.repl.interrupt())<EOL>raise<EOL><DEDENT>except TIMEOUT:<EOL><INDENT>stream_handler(engine.repl.interrupt())<EOL>raise Oct2PyError('<STR_LIT>')<EOL><DEDENT>except EOF:<EOL><INDENT>stream_handler(engine.repl.child.before)<EOL>self.restart()<EOL>raise Oct2PyError('<STR_LIT>')<EOL><DEDENT>resp = read_file(in_file, self)<EOL>if resp['<STR_LIT>']:<EOL><INDENT>msg = self._parse_error(resp['<STR_LIT>'])<EOL>raise Oct2PyError(msg)<EOL><DEDENT>result = resp['<STR_LIT:result>'].ravel().tolist()<EOL>if isinstance(result, list) and len(result) == <NUM_LIT:1>:<EOL><INDENT>result = result[<NUM_LIT:0>]<EOL><DEDENT>if (isinstance(result, Cell) and<EOL>result.size == <NUM_LIT:1> and<EOL>isinstance(result[<NUM_LIT:0>], string_types) and<EOL>result[<NUM_LIT:0>] == '<STR_LIT>'):<EOL><INDENT>result = None<EOL><DEDENT>if plot_dir:<EOL><INDENT>self._engine.make_figures(plot_dir)<EOL><DEDENT>return result<EOL>

Run the given function with the given args.


self._user_classes.setdefault(name, _make_user_class(self, name))<EOL>return self._user_classes[name]<EOL>

Get or create a user class of the given type.


def eval(self, cmds, verbose=True, timeout=None, stream_handler=None,<EOL>temp_dir=None, plot_dir=None, plot_name='<STR_LIT>', plot_format='<STR_LIT>',<EOL>plot_width=None, plot_height=None, plot_res=None,<EOL>nout=<NUM_LIT:0>, **kwargs):

if isinstance(cmds, (str, unicode)):<EOL><INDENT>cmds = [cmds]<EOL><DEDENT>prev_temp_dir = self.temp_dir<EOL>self.temp_dir = temp_dir or self.temp_dir<EOL>prev_log_level = self.logger.level<EOL>if kwargs.get('<STR_LIT>') is False:<EOL><INDENT>self.logger.setLevel(logging.WARN)<EOL><DEDENT>for name in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>if name not in kwargs:<EOL><INDENT>continue<EOL><DEDENT>msg = '<STR_LIT>'<EOL>warnings.warn(msg % name, stacklevel=<NUM_LIT:2>)<EOL><DEDENT>return_both = kwargs.pop('<STR_LIT>', False)<EOL>lines = []<EOL>if return_both and not stream_handler:<EOL><INDENT>stream_handler = lines.append<EOL><DEDENT>ans = None<EOL>for cmd in cmds:<EOL><INDENT>resp = self.feval('<STR_LIT>', '<STR_LIT>', cmd,<EOL>nout=nout, timeout=timeout,<EOL>stream_handler=stream_handler,<EOL>verbose=verbose, plot_dir=plot_dir,<EOL>plot_name=plot_name, plot_format=plot_format,<EOL>plot_width=plot_width, plot_height=plot_height,<EOL>plot_res=plot_res)<EOL>if resp is not None:<EOL><INDENT>ans = resp<EOL><DEDENT><DEDENT>self.temp_dir = prev_temp_dir<EOL>self.logger.setLevel(prev_log_level)<EOL>if return_both:<EOL><INDENT>return '<STR_LIT:\n>'.join(lines), ans<EOL><DEDENT>return ans<EOL>

Evaluate an Octave command or commands.

Parameters
----------
cmds : str or list
    Commands(s) to pass to Octave.
verbose : bool, optional
     Log Octave output at INFO level.  If False, log at DEBUG level.
stream_handler: callable, optional
    A function that is called for each line of output from the
    evaluation.
timeout : float, optional
    Time to wait for response from Octave (per line).  If not given,
    the instance `timeout` is used.
nout : int, optional.
    The desired number of returned values, defaults to 0.  If nout
    is 0, the `ans` will be returned as the return value.
temp_dir: str, optional
    If specified, the session's MAT files will be created in the
    directory, otherwise a the instance `temp_dir` is used.
    a shared memory (tmpfs) path.
plot_dir: str, optional
    If specificed, save the session's plot figures to the plot
    directory instead of displaying the plot window.
plot_name : str, optional
    Saved plots will start with `plot_name` and
    end with "_%%.xxx' where %% is the plot number and
    xxx is the `plot_format`.
plot_format: str, optional
    The format in which to save the plot (PNG by default).
plot_width: int, optional
    The plot with in pixels.
plot_height: int, optional
    The plot height in pixels.
plot_res: int, optional
    The plot resolution in pixels per inch.
**kwargs Deprectated kwargs.

Examples
--------
>>> from oct2py import octave
>>> octave.eval('disp("hello")') # doctest: +SKIP
hello
>>> x = octave.eval('round(quad(@sin, 0, pi/2));')
>>> x
1.0

>>> a = octave.eval('disp("hello");1;')  # doctest: +SKIP
hello
>>> a = octave.eval('disp("hello");1;', verbose=False)
>>> a
1.0

>>> from oct2py import octave
>>> lines = []
>>> octave.eval('for i = 1:3; disp(i);end', \
                stream_handler=lines.append)
>>> lines  # doctest: +SKIP
[' 1', ' 2', ' 3']

Returns
-------
out : object
    Octave "ans" variable, or None.

Notes
-----
The deprecated `log` kwarg will temporarily set the `logger` level to
`WARN`.  Using the `logger` settings directly is preferred.
The deprecated `return_both` kwarg will still work, but the preferred
method is to use the `stream_handler`.  If `stream_handler` is given,
the `return_both` kwarg will be honored but will give an empty string
as the reponse.

Raises
------
Oct2PyError
    If the command(s) fail.


def feval(self, func_path, *func_args, **kwargs):

if not self._engine:<EOL><INDENT>raise Oct2PyError('<STR_LIT>')<EOL><DEDENT>nout = kwargs.get('<STR_LIT>', None)<EOL>if nout is None:<EOL><INDENT>nout = <NUM_LIT:1><EOL><DEDENT>plot_dir = kwargs.get('<STR_LIT>')<EOL>settings = dict(backend='<STR_LIT>' if plot_dir else self.backend,<EOL>format=kwargs.get('<STR_LIT>'),<EOL>name=kwargs.get('<STR_LIT>'),<EOL>width=kwargs.get('<STR_LIT>'),<EOL>height=kwargs.get('<STR_LIT>'),<EOL>resolution=kwargs.get('<STR_LIT>'))<EOL>self._engine.plot_settings = settings<EOL>dname = osp.dirname(func_path)<EOL>fname = osp.basename(func_path)<EOL>func_name, ext = osp.splitext(fname)<EOL>if ext and not ext == '<STR_LIT>':<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if func_name == '<STR_LIT>':<EOL><INDENT>raise Oct2PyError('<STR_LIT>' +<EOL>'<STR_LIT>')<EOL><DEDENT>stream_handler = kwargs.get('<STR_LIT>')<EOL>verbose = kwargs.get('<STR_LIT>', True)<EOL>store_as = kwargs.get('<STR_LIT>', '<STR_LIT>')<EOL>timeout = kwargs.get('<STR_LIT>', self.timeout)<EOL>if not stream_handler:<EOL><INDENT>stream_handler = self.logger.info if verbose else self.logger.debug<EOL><DEDENT>return self._feval(func_name, func_args, dname=dname, nout=nout,<EOL>timeout=timeout, stream_handler=stream_handler,<EOL>store_as=store_as, plot_dir=plot_dir)<EOL>

Run a function in Octave and return the result.

        Parameters
        ----------
        func_path: str
            Name of function to run or a path to an m-file.
        func_args: object, optional
            Args to send to the function.
        nout: int, optional
            Desired number of return arguments, defaults to 1.
        store_as: str, optional
            If given, saves the result to the given Octave variable name
            instead of returning it.
        verbose : bool, optional
            Log Octave output at INFO level.  If False, log at DEBUG level.
        stream_handler: callable, optional
            A function that is called for each line of output from the
            evaluation.
        timeout: float, optional
            The timeout in seconds for the call.
        plot_dir: str, optional
            If specificed, save the session's plot figures to the plot
            directory instead of displaying the plot window.
        plot_name : str, optional
            Saved plots will start with `plot_name` and
            end with "_%%.xxx' where %% is the plot number and
            xxx is the `plot_format`.
        plot_format: str, optional
            The format in which to save the plot.
        plot_width: int, optional
            The plot with in pixels.
        plot_height: int, optional
            The plot height in pixels.

        Notes
        -----
        The function arguments passed follow Octave calling convention, not
        Python. That is, all values must be passed as a comma separated list,
        not using `x=foo` assignment.

        Examples
        --------
        >>> from oct2py import octave
        >>> cell = octave.feval('cell', 10, 10, 10)
        >>> cell.shape
        (10, 10, 10)

        >>> from oct2py import octave
        >>> x = octave.feval('linspace', 0, octave.pi() / 2)
        >>> x.shape
        (1, 100)

        >>> from oct2py import octave
        >>> x = octave.feval('svd', octave.hilb(3))
        >>> x
        array([[ 1.40831893],
               [ 0.12232707],
               [ 0.00268734]])
        >>> # specify three return values
        >>> (u, v, d) = octave.feval('svd', octave.hilb(3), nout=3)
        >>> u.shape
        (3, 3)

        Returns
        -------
        The Python value(s) returned by the Octave function call.


cmd = '<STR_LIT>' % name<EOL>resp = self._engine.eval(cmd, silent=True).strip()<EOL>exist = int(resp.split()[-<NUM_LIT:1>])<EOL>if exist == <NUM_LIT:0>:<EOL><INDENT>msg = '<STR_LIT>'<EOL>raise Oct2PyError(msg % name)<EOL><DEDENT>return exist<EOL>

Test whether a name exists and return the name code.

        Raises an error when the name does not exist.


if name is None:<EOL><INDENT>name = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>name = '<STR_LIT>' + name<EOL><DEDENT>log = logging.getLogger(name)<EOL>log.setLevel(logging.INFO)<EOL>return log<EOL>

Return a console logger.

    Output may be sent to the logger using the `debug`, `info`, `warning`,
    `error` and `critical` methods.

    Parameters
    ----------
    name : str
        Name of the log.

    References
    ----------
    .. [1] Logging facility for Python,
           http://docs.python.org/library/logging.html


self.octave.push('<STR_LIT:x>', self.array)<EOL>

Create a large matrix and load it into the octave session.


Run a fast Octave command and see how long it takes.


value = np.asarray(value)<EOL>if (value.shape[value.ndim - <NUM_LIT:1>] == <NUM_LIT:1>):<EOL><INDENT>value = value.squeeze(axis=value.ndim - <NUM_LIT:1>)<EOL><DEDENT>value = np.atleast_1d(value)<EOL>if not session:<EOL><INDENT>return value.view(cls)<EOL><DEDENT>obj = np.empty(value.size, dtype=value.dtype).view(cls)<EOL>for (i, item) in enumerate(value.ravel()):<EOL><INDENT>for name in value.dtype.names:<EOL><INDENT>obj[i][name] = _extract(item[name], session)<EOL><DEDENT><DEDENT>return obj.reshape(value.shape)<EOL>

Create a struct array from a value and optional Octave session.


item = np.recarray.__getitem__(self, item)<EOL>if isinstance(item, np.ndarray) and item.dtype.kind == '<STR_LIT:O>':<EOL><INDENT>return Cell(item)<EOL><DEDENT>return item<EOL>

Return object arrays as cells and all other values unchanged.


value = np.asarray(value, dtype=object)<EOL>if (value.shape[value.ndim - <NUM_LIT:1>] == <NUM_LIT:1>):<EOL><INDENT>value = value.squeeze(axis=value.ndim - <NUM_LIT:1>)<EOL><DEDENT>value = np.atleast_1d(value)<EOL>if not session:<EOL><INDENT>return value.view(cls)<EOL><DEDENT>obj = np.empty(value.size, dtype=object).view(cls)<EOL>for (i, item) in enumerate(value.ravel()):<EOL><INDENT>obj[i] = _extract(item, session)<EOL><DEDENT>return obj.reshape(value.shape)<EOL>

Create a cell array from a value and optional Octave session.


if "<STR_LIT:user>" in context:<EOL><INDENT>user = context["<STR_LIT:user>"]<EOL>if user.is_authenticated():<EOL><INDENT>qs = Inbox.objects.select_related("<STR_LIT:message>").filter(user=user)<EOL>return {<EOL>"<STR_LIT>": qs[:num_elements],<EOL>"<STR_LIT:count>": qs.count(),<EOL>}<EOL><DEDENT><DEDENT>

Renders a list of unread stored messages for the current user


from .settings import stored_messages_settings<EOL>backend = stored_messages_settings.STORAGE_BACKEND()<EOL>backend.inbox_purge(request.user)<EOL>return Response({"<STR_LIT:message>": "<STR_LIT>"})<EOL>

Mark all messages as read (i.e. delete from inbox) for current logged in user


for message in messages:<EOL><INDENT>if not self.backend.can_handle(message):<EOL><INDENT>message._prepare()<EOL><DEDENT><DEDENT>

Like the base class method, prepares a list of messages for storage
but avoid to do this for `models.Message` instances.


def _store(self, messages, response, *args, **kwargs):

contrib_messages = []<EOL>if self.user.is_authenticated():<EOL><INDENT>if not messages:<EOL><INDENT>self.backend.inbox_purge(self.user)<EOL><DEDENT>else:<EOL><INDENT>for m in messages:<EOL><INDENT>try:<EOL><INDENT>self.backend.inbox_store([self.user], m)<EOL><DEDENT>except MessageTypeNotSupported:<EOL><INDENT>contrib_messages.append(m)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>super(StorageMixin, self)._store(contrib_messages, response, *args, **kwargs)<EOL>

persistent messages are already in the database inside the 'archive',
so we can say they're already "stored".
Here we put them in the inbox, or remove from the inbox in case the
messages were iterated.

messages contains only new msgs if self.used==True
else contains both new and unread messages


messages, all_retrieved = super(StorageMixin, self)._get(*args, **kwargs)<EOL>if self.user.is_authenticated():<EOL><INDENT>inbox_messages = self.backend.inbox_list(self.user)<EOL><DEDENT>else:<EOL><INDENT>inbox_messages = []<EOL><DEDENT>return messages + inbox_messages, all_retrieved<EOL>

Retrieve unread messages for current user, both from the inbox and
from other storages


try:<EOL><INDENT>parts = val.split('<STR_LIT:.>')<EOL>module_path, class_name = '<STR_LIT:.>'.join(parts[:-<NUM_LIT:1>]), parts[-<NUM_LIT:1>]<EOL>module = importlib.import_module(module_path)<EOL>return getattr(module, class_name)<EOL><DEDENT>except ImportError as e:<EOL><INDENT>msg = "<STR_LIT>" % (val, setting_name,<EOL>e.__class__.__name__, e)<EOL>raise ImportError(msg)<EOL><DEDENT>

Attempt to import a class from a string representation.


if isinstance(val, six.string_types):<EOL><INDENT>return import_from_string(val, setting_name)<EOL><DEDENT>elif isinstance(val, (list, tuple)):<EOL><INDENT>return [import_from_string(item, setting_name) for item in val]<EOL><DEDENT>return val<EOL>

If the given setting is a string import notation,
then perform the necessary import or imports.


return Message(**json.loads(force_text(json_msg)))<EOL>

Return a Message instance built from data contained in a JSON string


def create_message(self, level, msg_text, extra_tags, date=None):

raise NotImplementedError()<EOL>

Create and return a `Message` instance.
Instance types depend on backends implementation.

Params:
    `level`: message level (see django.contrib.messages)
    `msg_text`: what you think it is
    `extra_tags`: see django.contrib.messages
    `date`: a DateTime (optional)

Return:
    `Message` instance


raise NotImplementedError()<EOL>

Clear all backend data.
Warning: heavily destructive! Here for convenience, not used by the API anyway.

Params:
    None

Return:
    None


raise NotImplementedError()<EOL>

Remove messages that have been expired.

Params:
    None

Return:
   None


raise NotImplementedError()<EOL>

Retrieve all the messages in `user`'s Inbox.

Params:
    `user`: Django User instance

Return:
    An iterable containing `Message` instances


BackendClass = stored_messages_settings.STORAGE_BACKEND<EOL>backend = BackendClass()<EOL>backend.inbox_delete(user, message)<EOL>

Mark message instance as read for user.
Returns True if the message was `unread` and thus actually marked as `read` or False in case
it is already `read` or it does not exist at all.

:param user: user instance for the recipient
:param message: a Message instance to mark as read


init_py = open(os.path.join(package, '<STR_LIT>')).read()<EOL>return re.search("<STR_LIT>", init_py).group(<NUM_LIT:1>)<EOL>

Return package version as listed in `__version__` in `init.py`.


from notification.models import NoticeSetting<EOL>return NoticeSetting.for_user(user, notice_type, self.medium_id).send<EOL>

Determines whether this backend is allowed to send a notification to
the given user and notice_type.


def deliver(self, recipient, sender, notice_type, extra_context):

raise NotImplementedError()<EOL>

Deliver a notification to the given recipient.


raise NotImplementedError("<STR_LIT>")<EOL>

Return True if this object is locking the file.


Acquire the lock.

* If timeout is omitted (or None), wait forever trying to lock the
  file.

* If timeout > 0, try to acquire the lock for that many seconds.  If
  the lock period expires and the file is still locked, raise
  LockTimeout.

* If timeout <= 0, raise AlreadyLocked immediately if the file is
  already locked.


self.release()<EOL>


Remove a lock.  Useful if a locking thread failed to unlock.


def send_now(users, label, extra_context=None, sender=None):

sent = False<EOL>if extra_context is None:<EOL><INDENT>extra_context = {}<EOL><DEDENT>notice_type = NoticeType.objects.get(label=label)<EOL>current_language = get_language()<EOL>for user in users:<EOL><INDENT>try:<EOL><INDENT>language = get_notification_language(user)<EOL><DEDENT>except LanguageStoreNotAvailable:<EOL><INDENT>language = None<EOL><DEDENT>if language is not None:<EOL><INDENT>activate(language)<EOL><DEDENT>for backend in NOTIFICATION_BACKENDS.values():<EOL><INDENT>if backend.can_send(user, notice_type):<EOL><INDENT>backend.deliver(user, sender, notice_type, extra_context)<EOL>sent = True<EOL><DEDENT><DEDENT><DEDENT>activate(current_language)<EOL>return sent<EOL>

Creates a new notice.

This is intended to be how other apps create new notices.

notification.send(user, "friends_invite_sent", {
    "spam": "eggs",
    "foo": "bar",
)


def divrank_scipy(G, alpha=<NUM_LIT>, d=<NUM_LIT>, personalization=None,<EOL>max_iter=<NUM_LIT:100>, tol=<NUM_LIT>, nstart=None, weight='<STR_LIT>',<EOL>dangling=None):

import scipy.sparse<EOL>N = len(G)<EOL>if N == <NUM_LIT:0>:<EOL><INDENT>return {}<EOL><DEDENT>nodelist = G.nodes()<EOL>M = nx.to_scipy_sparse_matrix(G, nodelist=nodelist, weight=weight,<EOL>dtype=float)<EOL>S = scipy.array(M.sum(axis=<NUM_LIT:1>)).flatten()<EOL>S[S != <NUM_LIT:0>] = <NUM_LIT:1.0> / S[S != <NUM_LIT:0>]<EOL>Q = scipy.sparse.spdiags(S.T, <NUM_LIT:0>, *M.shape, format='<STR_LIT>')<EOL>M = Q * M<EOL>M = scipy.sparse.lil_matrix(M)<EOL>M.setdiag(<NUM_LIT:0.0>)<EOL>M = alpha * M<EOL>M.setdiag(<NUM_LIT:1.0> - alpha)<EOL>x = scipy.repeat(<NUM_LIT:1.0> / N, N)<EOL>if personalization is None:<EOL><INDENT>p = scipy.repeat(<NUM_LIT:1.0> / N, N)<EOL><DEDENT>else:<EOL><INDENT>missing = set(nodelist) - set(personalization)<EOL>if missing:<EOL><INDENT>raise NetworkXError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % missing)<EOL><DEDENT>p = scipy.array([personalization[n] for n in nodelist],<EOL>dtype=float)<EOL>p = p / p.sum()<EOL><DEDENT>if dangling is None:<EOL><INDENT>dangling_weights = p<EOL><DEDENT>else:<EOL><INDENT>missing = set(nodelist) - set(dangling)<EOL>if missing:<EOL><INDENT>raise NetworkXError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % missing)<EOL><DEDENT>dangling_weights = scipy.array([dangling[n] for n in nodelist],<EOL>dtype=float)<EOL>dangling_weights /= dangling_weights.sum()<EOL><DEDENT>is_dangling = scipy.where(S == <NUM_LIT:0>)[<NUM_LIT:0>]<EOL>for _ in range(max_iter):<EOL><INDENT>xlast = x<EOL>D_t =  M * x<EOL>x = (<EOL>d * (x / D_t * M * x + sum(x[is_dangling]) * dangling_weights)<EOL>+ (<NUM_LIT:1.0> - d) * p<EOL>)<EOL>err = scipy.absolute(x - xlast).sum()<EOL>if err < N * tol:<EOL><INDENT>return dict(list(zip(nodelist, list(map(float, x)))))<EOL><DEDENT><DEDENT>raise NetworkXError('<STR_LIT>'<EOL>'<STR_LIT>' % max_iter)<EOL>

Returns the DivRank (Diverse Rank) of the nodes in the graph.
This code is based on networkx.pagerank_scipy


@not_implemented_for('<STR_LIT>')<EOL>def divrank(G, alpha=<NUM_LIT>, d=<NUM_LIT>, personalization=None,<EOL>max_iter=<NUM_LIT:100>, tol=<NUM_LIT>, nstart=None, weight='<STR_LIT>',<EOL>dangling=None):

if len(G) == <NUM_LIT:0>:<EOL><INDENT>return {}<EOL><DEDENT>if not G.is_directed():<EOL><INDENT>D = G.to_directed()<EOL><DEDENT>else:<EOL><INDENT>D = G<EOL><DEDENT>W = nx.stochastic_graph(D, weight=weight)<EOL>N = W.number_of_nodes()<EOL>for n in W.nodes_iter():<EOL><INDENT>for n_ in W.nodes_iter():<EOL><INDENT>if n != n_ :<EOL><INDENT>if n_ in W[n]:<EOL><INDENT>W[n][n_][weight] *= alpha<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if n_ not in W[n]:<EOL><INDENT>W.add_edge(n, n_)<EOL><DEDENT>W[n][n_][weight] = <NUM_LIT:1.0> - alpha<EOL><DEDENT><DEDENT><DEDENT>if nstart is None:<EOL><INDENT>x = dict.fromkeys(W, <NUM_LIT:1.0> / N)<EOL><DEDENT>else:<EOL><INDENT>s = float(sum(nstart.values()))<EOL>x = dict((k, v / s) for k, v in list(nstart.items()))<EOL><DEDENT>if personalization is None:<EOL><INDENT>p = dict.fromkeys(W, <NUM_LIT:1.0> / N)<EOL><DEDENT>else:<EOL><INDENT>missing = set(G) - set(personalization)<EOL>if missing:<EOL><INDENT>raise NetworkXError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % missing)<EOL><DEDENT>s = float(sum(personalization.values()))<EOL>p = dict((k, v / s) for k, v in list(personalization.items()))<EOL><DEDENT>if dangling is None:<EOL><INDENT>dangling_weights = p<EOL><DEDENT>else:<EOL><INDENT>missing = set(G) - set(dangling)<EOL>if missing:<EOL><INDENT>raise NetworkXError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % missing)<EOL><DEDENT>s = float(sum(dangling.values()))<EOL>dangling_weights = dict((k, v/s) for k, v in list(dangling.items()))<EOL><DEDENT>dangling_nodes = [n for n in W if W.out_degree(n, weight=weight) == <NUM_LIT:0.0>]<EOL>for _ in range(max_iter):<EOL><INDENT>xlast = x<EOL>x = dict.fromkeys(list(xlast.keys()), <NUM_LIT:0>)<EOL>danglesum = d * sum(xlast[n] for n in dangling_nodes)<EOL>for n in x:<EOL><INDENT>D_t = sum(W[n][nbr][weight] * xlast[nbr] for nbr in W[n])<EOL>for nbr in W[n]:<EOL><INDENT>x[nbr] += (<EOL>d * (W[n][nbr][weight] * xlast[nbr] / D_t) * xlast[n]<EOL>)<EOL><DEDENT>x[n] += danglesum * dangling_weights[n] + (<NUM_LIT:1.0> - d) * p[n]<EOL><DEDENT>err = sum([abs(x[n] - xlast[n]) for n in x])<EOL>if err < N*tol:<EOL><INDENT>return x<EOL><DEDENT><DEDENT>raise NetworkXError('<STR_LIT>'<EOL>'<STR_LIT>' % max_iter)<EOL>

Returns the DivRank (Diverse Rank) of the nodes in the graph.
This code is based on networkx.pagerank.

Args: (diff from pagerank)
  alpha: controls strength of self-link [0.0-1.0]
  d: the damping factor

Reference:
  Qiaozhu Mei and Jian Guo and Dragomir Radev,
  DivRank: the Interplay of Prestige and Diversity in Information Networks,
  http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.174.7982


def lexrank(sentences, continuous=False, sim_threshold=<NUM_LIT:0.1>, alpha=<NUM_LIT>,<EOL>use_divrank=False, divrank_alpha=<NUM_LIT>):

<EOL>ranker_params = {'<STR_LIT>': <NUM_LIT:1000>}<EOL>if use_divrank:<EOL><INDENT>ranker = divrank_scipy<EOL>ranker_params['<STR_LIT>'] = divrank_alpha<EOL>ranker_params['<STR_LIT:d>'] = alpha<EOL><DEDENT>else:<EOL><INDENT>ranker = networkx.pagerank_scipy<EOL>ranker_params['<STR_LIT>'] = alpha<EOL><DEDENT>graph = networkx.DiGraph()<EOL>sent_tf_list = []<EOL>for sent in sentences:<EOL><INDENT>words = tools.word_segmenter_ja(sent)<EOL>tf = collections.Counter(words)<EOL>sent_tf_list.append(tf)<EOL><DEDENT>sent_vectorizer = DictVectorizer(sparse=True)<EOL>sent_vecs = sent_vectorizer.fit_transform(sent_tf_list)<EOL>sim_mat = <NUM_LIT:1> - pairwise_distances(sent_vecs, sent_vecs, metric='<STR_LIT>')<EOL>if continuous:<EOL><INDENT>linked_rows, linked_cols = numpy.where(sim_mat > <NUM_LIT:0>)<EOL><DEDENT>else:<EOL><INDENT>linked_rows, linked_cols = numpy.where(sim_mat >= sim_threshold)<EOL><DEDENT>graph.add_nodes_from(list(range(sent_vecs.shape[<NUM_LIT:0>])))<EOL>for i, j in zip(linked_rows, linked_cols):<EOL><INDENT>if i == j:<EOL><INDENT>continue<EOL><DEDENT>weight = sim_mat[i,j] if continuous else <NUM_LIT:1.0><EOL>graph.add_edge(i, j, {'<STR_LIT>': weight})<EOL><DEDENT>scores = ranker(graph, **ranker_params)<EOL>return scores, sim_mat<EOL>

compute centrality score of sentences.

Args:
  sentences: [u'こんにちは．', u'私の名前は飯沼です．', ... ]
  continuous: if True, apply continuous LexRank. (see reference)
  sim_threshold: if continuous is False and smilarity is greater or
    equal to sim_threshold, link the sentences.
  alpha: the damping factor of PageRank and DivRank
  divrank: if True, apply DivRank instead of PageRank
  divrank_alpha: strength of self-link [0.0-1.0]
    (it's not the damping factor, see divrank.py)

Returns: tuple
  (
    {
      # sentence index -> score
      0: 0.003,
      1: 0.002,
      ...
    },
    similarity_matrix
  )

Reference:
  Günes Erkan and Dragomir R. Radev.
  LexRank: graph-based lexical centrality as salience in text
  summarization. (section 3)
  http://www.cs.cmu.edu/afs/cs/project/jair/pub/volume22/erkan04a-html/erkan04a.html


def __init__(self, i2c, device_address, *, debug=False):

while not i2c.try_lock():<EOL><INDENT>pass<EOL><DEDENT>try:<EOL><INDENT>i2c.writeto(device_address, b'<STR_LIT>')<EOL><DEDENT>except OSError:<EOL><INDENT>try:<EOL><INDENT>result = bytearray(<NUM_LIT:1>)<EOL>i2c.readfrom_into(device_address, result)<EOL><DEDENT>except OSError:<EOL><INDENT>raise ValueError("<STR_LIT>" % device_address)<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>i2c.unlock()<EOL><DEDENT>self.i2c = i2c<EOL>self.device_address = device_address<EOL>self._debug = debug<EOL>

Try to read a byte from an address,
if you get an OSError it means the device is not there


def write_then_readinto(self, out_buffer, in_buffer, *,<EOL>out_start=<NUM_LIT:0>, out_end=None, in_start=<NUM_LIT:0>, in_end=None, stop=True):

if out_end is None:<EOL><INDENT>out_end = len(out_buffer)<EOL><DEDENT>if in_end is None:<EOL><INDENT>in_end = len(in_buffer)<EOL><DEDENT>if hasattr(self.i2c, '<STR_LIT>'):<EOL><INDENT>if self._debug:<EOL><INDENT>print("<STR_LIT>",<EOL>[hex(i) for i in out_buffer[out_start:out_end]])<EOL><DEDENT>self.i2c.writeto_then_readfrom(self.device_address, out_buffer, in_buffer,<EOL>out_start=out_start, out_end=out_end,<EOL>in_start=in_start, in_end=in_end, stop=stop)<EOL>if self._debug:<EOL><INDENT>print("<STR_LIT>",<EOL>[hex(i) for i in in_buffer[in_start:in_end]])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>self.write(out_buffer, start=out_start, end=out_end, stop=stop)<EOL>if self._debug:<EOL><INDENT>print("<STR_LIT>",<EOL>[hex(i) for i in out_buffer[out_start:out_end]])<EOL><DEDENT>self.readinto(in_buffer, start=in_start, end=in_end)<EOL>if self._debug:<EOL><INDENT>print("<STR_LIT>",<EOL>[hex(i) for i in in_buffer[in_start:in_end]])<EOL><DEDENT><DEDENT>

Write the bytes from ``out_buffer`` to the device, then immediately
reads into ``in_buffer`` from the device. The number of bytes read
will be the length of ``in_buffer``.
Transmits a stop bit after the write, if ``stop`` is set.

If ``out_start`` or ``out_end`` is provided, then the output buffer
will be sliced as if ``out_buffer[out_start:out_end]``. This will
not cause an allocation like ``buffer[out_start:out_end]`` will so
it saves memory.

If ``in_start`` or ``in_end`` is provided, then the input buffer
will be sliced as if ``in_buffer[in_start:in_end]``. This will not
cause an allocation like ``in_buffer[in_start:in_end]`` will so
it saves memory.

:param bytearray out_buffer: buffer containing the bytes to write
:param bytearray in_buffer: buffer containing the bytes to read into
:param int out_start: Index to start writing from
:param int out_end: Index to read up to but not include
:param int in_start: Index to start writing at
:param int in_end: Index to write up to but not include
:param bool stop: If true, output an I2C stop condition after the buffer is written


for id_ in self._items:<EOL><INDENT>yield self[id_]<EOL><DEDENT>

allows you to iterate and use for-loops

        The objects in the iterator have the order in which they were appended.


return bool(self._items)<EOL>

:return: whether there is anything in the collection.
        :rtype: bool


self._items = OrderedDict()<EOL>

Create a new :class:`IdCollection` with no arguments.

        You can add objects later using the method :meth:`append`.


return self._items[id_]<EOL>

Get the object with the :paramref:`id`

        .. code:: python

            ic = IdCollection()
            ic.append(object_1)
            ic.append(object_2)
            assert ic[object_1.id] == object_1
            assert ic[object_2.id] == object_1

        :param id_: the id of an object
        :return: the object with the :paramref:`id`
        :raises KeyError: if no object with :paramref:`id` was found


return len(self._items)<EOL>

:return: the number of objects in this collection


def __init__(self, process=identity, chooses_path=true):

self._process = process<EOL>self._chooses_path = chooses_path<EOL>

Create a PathLoader object.

        :param process: ``process(path)`` is called with the `path` to load.
          The result of :paramref:`process` is returned to the caller. The
          default value is :func:`identity`, so the paths are returned when
          loaded.
        :param chooses_path: ``chooses_path(path)`` is called before
          :paramref:`process` and returns :obj:`True` or :obj:`False`
          depending on whether a specific path should be loaded and passed to
          :paramref:`process`.


example_path = os.path.join("<STR_LIT>", relative_path)<EOL>return self.relative_file(__file__, example_path)<EOL>

Load an example from the knitting pattern examples.

        :param str relative_path: the path to load
        :return: the result of the processing

        You can use :meth:`knittingpattern.Loader.PathLoader.examples`
        to find out the paths of all examples.


object_ = json.loads(string)<EOL>return self.object(object_)<EOL>

Load an object from a string and return the processed JSON content

        :return: the result of the processing step
        :param str string: the string to load the JSON from


def relative_folder(self, module, folder):

folder = self._relative_to_absolute(module, folder)<EOL>return self.folder(folder)<EOL>

Load a folder located relative to a module and return the processed
        result.

        :param str module: can be

          - a path to a folder
          - a path to a file
          - a module name

        :param str folder: the path of a folder relative to :paramref:`module`
        :return: a list of the results of the processing
        :rtype: list

        Depending on :meth:`chooses_path` some paths may not be loaded.
        Every loaded path is processed and returned part of the returned list.
        You can use :meth:`choose_paths` to find out which paths are chosen to
        load.


return [path for path in paths if self._chooses_path(path)]<EOL>

:return: the paths that are chosen by :meth:`chooses_path`
        :rtype: list


path = self._relative_to_absolute(module, file)<EOL>return self.path(path)<EOL>

Load a file relative to a module.

        :param str module: can be

          - a path to a folder
          - a path to a file
          - a module name

        :param str folder: the path of a folder relative to :paramref:`module`
        :return: the result of the processing


return object_<EOL>

:return: the argument
    :param object_: the object to be returned


return walk(self)<EOL>

Return the rows in the order that they should be knit.

        :rtype: list
        :return: the :attr:`rows` in the order that they should be knit

        .. seealso:: :mod:`knittingpattern.walk`


return self._name<EOL>


row = self._parser.new_row(id_)<EOL>self._rows.append(row)<EOL>return row<EOL>

Add a new row to the pattern.

        :param id_: the id of the row


return self._rows<EOL>

a collection of rows that this pattern is made of

        Usually this should be a
        :class:`knittingpattern.IdCollection.IdCollection` of
        :class:`knittingpattern.Row.Row`.


return self.as_instruction({TYPE: instruction_type})<EOL>

:return: the specification for :paramref:`instruction_type`

        .. seealso:: :meth:`as_instruction`


return Instruction<EOL>

:return: the class for the specifications


super().__init__()<EOL>self.load.relative_folder(__file__, self.INSTRUCTIONS_FOLDER)<EOL>

Create the default instruction library without arguments.

        The default specifications are loaded automatically form this package.


for instruction in obj:<EOL><INDENT>self.add_instruction(instruction)<EOL><DEDENT>return self<EOL>

add the loaded instructions from :attr:`load`


return JSONLoader<EOL>

:return: the class for loading the specifications with
          :attr:`load`


return charlotte.patterns["<STR_LIT>"]<EOL>

:return: the pattern ``"A.1"`` in charlotte


return _charlotte().patterns["<STR_LIT>"]<EOL>


return single_instruction_pattern_set.patterns["<STR_LIT>"]<EOL>

The pattern which has only one instruction.


return pattern.rows[<NUM_LIT:1>]<EOL>


return load_from_relative_file(HERE, "<STR_LIT>")<EOL>

Load the pattern set with only one instruction.


return "<STR_LIT>".format(cls.__module__, cls.__qualname__)<EOL>

The string representation of the object.

        :return: the string representation
        :rtype: str


self.new_loader = new_loader<EOL>self.new_parser = new_parser<EOL>self.new_parsing_error = new_parsing_error<EOL>self.new_pattern_set = new_pattern_set<EOL>self.new_pattern_collection = new_pattern_collection<EOL>self.new_row_collection = new_row_collection<EOL>self.new_pattern = new_pattern<EOL>self.new_row = new_row<EOL>self.new_default_instructions = new_default_instructions<EOL>self.new_instruction_in_row = new_instruction_in_row<EOL>


parser = specification.new_parser(specification)<EOL>loader = specification.new_loader(parser.knitting_pattern_set)<EOL>return loader<EOL>

Create a loader for a knitting pattern set.

    :param specification: a :class:`specification
      <knittingpattern.ParsingSpecification.ParsingSpecification>`
      for the knitting pattern set, default
      :class:`DefaultSpecification`


return load_from().file(file)<EOL>

Load a knitting pattern from a file-like object.

    :rtype: knittingpattern.KnittingPatternSet.KnittingPatternSet


from .ParsingSpecification import new_knitting_pattern_set_loader<EOL>return new_knitting_pattern_set_loader()<EOL>

Create a loader to load knitting patterns with.

    :return: the loader to load objects with
    :rtype: knittingpattern.Loader.JSONLoader

    Example:

    .. code:: python

       import knittingpattern, webbrowser
       k = knittingpattern.load_from().example("Cafe.json")
       webbrowser.open(k.to_svg(25).temporary_path(".svg"))


return load_from().object(object_)<EOL>

Load a knitting pattern from an object.

    :rtype: knittingpattern.KnittingPatternSet.KnittingPatternSet


from .convert.image_to_knittingpattern importconvert_image_to_knitting_pattern<EOL>return convert_image_to_knitting_pattern(colors=colors)<EOL>

Convert and image to a knitting pattern.

    :return: a loader
    :rtype: knittingpattern.Loader.PathLoader
    :param tuple colors: the colors to convert to

    .. code:: python

        convert_from_image().path("pattern.png").path("pattern.json")
        convert_from_image().path("pattern.png").knitting_pattern()

    .. seealso:: :mod:`knittingoattern.convert.image_to_knitting_pattern`


return load_from().path(path)<EOL>

Load a knitting pattern from a file behind located at `path`.

    :rtype: knittingpattern.KnittingPatternSet.KnittingPatternSet


for instruction in reversed(self.instructions):<EOL><INDENT>if instruction.produces_meshes():<EOL><INDENT>return instruction.last_produced_mesh<EOL><DEDENT><DEDENT>raise IndexError("<STR_LIT>".format(self))<EOL>

The last produced mesh.

        :return: the last produced mesh
        :rtype: knittingpattern.Mesh.Mesh
        :raises IndexError: if no mesh is produced

        .. seealso:: :attr:`number_of_produced_meshes`


return self.get(COLOR)<EOL>

The color of the row.

        :return: the color of the row as specified or :obj:`None`


return unique(instruction.colors for instruction in self.instructions)<EOL>

The colors of the instructions in the row in the order tehy appear.

        :return: a list of colors of the knitting pattern in the order that
          they appear in
        :rtype: list


return self._instructions<EOL>

The instructions in this row.

        :return: a collection of :class:`instructions inside the row
          <knittingpattern.Instruction.InstructionInRow>`
        :rtype: ObservableList.ObservableList


return "<STR_LIT>".format(self.__class__.__qualname__, self.id)<EOL>

The string representation of this row.

        :return: a string representation of this row
        :rtype: str


for instruction in self.instructions:<EOL><INDENT>if instruction.produces_meshes():<EOL><INDENT>return instruction.first_produced_mesh<EOL><DEDENT><DEDENT>raise IndexError("<STR_LIT>".format(self))<EOL>

The first produced mesh.

        :return: the first produced mesh
        :rtype: knittingpattern.Mesh.Mesh
        :raises IndexError: if no mesh is produced

        .. seealso:: :attr:`number_of_produced_meshes`


rows_before = []<EOL>for mesh in self.consumed_meshes:<EOL><INDENT>if mesh.is_produced():<EOL><INDENT>row = mesh.producing_row<EOL>if rows_before not in rows_before:<EOL><INDENT>rows_before.append(row)<EOL><DEDENT><DEDENT><DEDENT>return rows_before<EOL>

The rows that produce meshes for this row.

        :rtype: list
        :return: a list of rows that produce meshes for this row. Each row
          occurs only once. They are sorted by the first occurrence in the
          instructions.


return self.instructions[<NUM_LIT:0>]<EOL>

The first instruction of the rows instructions.

        :rtype: knittingpattern.Instruction.InstructionInRow
        :return: the first instruction in this row's :attr:`instructions`


return sum(instruction.number_of_produced_meshes<EOL>for instruction in self.instructions)<EOL>

The number of meshes that this row produces.

        :return: the number of meshes that this row produces
        :rtype: int

        .. seealso::
          :meth:`Instruction.number_of_produced_meshes()
          <knittingpattern.Instruction.Instruction.number_of_produced_meshes>`,
          :meth:`number_of_consumed_meshes`


rows_after = []<EOL>for mesh in self.produced_meshes:<EOL><INDENT>if mesh.is_consumed():<EOL><INDENT>row = mesh.consuming_row<EOL>if rows_after not in rows_after:<EOL><INDENT>rows_after.append(row)<EOL><DEDENT><DEDENT><DEDENT>return rows_after<EOL>

The rows that consume meshes from this row.

        :rtype: list
        :return: a list of rows that consume meshes from this row. Each row
          occurs only once. They are sorted by the first occurrence in the
          instructions.


return self.instructions[-<NUM_LIT:1>]<EOL>

The last instruction of the rows instructions.

        :rtype: knittingpattern.Instruction.InstructionInRow
        :return: the last instruction in this row's :attr:`instructions`


def __init__(self, knittingpattern, layout, instruction_to_svg, builder,<EOL>zoom):

self._knittingpattern = knittingpattern<EOL>self._layout = layout<EOL>self._instruction_to_svg = instruction_to_svg<EOL>self._builder = builder<EOL>self._zoom = zoom<EOL>self._instruction_type_color_to_symbol = OrderedDict()<EOL>self._symbol_id_to_scale = {}<EOL>

:param knittingpattern.KnittingPattern.KnittingPattern knittingpattern:
  a knitting pattern
:param knittingpattern.convert.Layout.GridLayout layout:
:param instruction_to_svg: an
  :class:`~knittingpattern.convert.InstructionToSVG.InstructionToSVG`
  :class:`
  ~knittingpattern.convert.InstructionToSVGCache.InstructionSVGCache`,
  both with instructions already loaded.
:param knittingpattern.convert.SVGBuilder.SVGBuilder builder:
:param float zoom: the height and width of a knit instruction


def place_svg_use(self, symbol_id, layer_id, group=None):

self.place_svg_use_coords(<NUM_LIT:0>, <NUM_LIT:0>, symbol_id, layer_id, group)<EOL>

Same as :meth:`place_svg_use_coords`.

        With implicit `x`  and `y` which are set to `0` in this method and then
        :meth:`place_svg_use_coords` is called.


return (self._min_x, self._min_y, self._max_x, self._max_y)<EOL>

the bounding box of this SVG
        ``(min_x, min_y, max_x, max_y)``.

        .. code:: python

            svg_builder10x10.bounding_box = (0, 0, 10, 10)
            assert svg_builder10x10.bounding_box == (0, 0, 10, 10)

        ``viewBox``, ``width`` and ``height`` are computed from this.

        If the bounding box was never set, the result is a tuple of four
        :obj:`None`.


if layer_id not in self._layer_id_to_layer:<EOL><INDENT>self._svg.setdefault("<STR_LIT:g>", [])<EOL>layer = {<EOL>"<STR_LIT:g>": [],<EOL>"<STR_LIT>": layer_id,<EOL>"<STR_LIT>": layer_id,<EOL>"<STR_LIT>": "<STR_LIT>",<EOL>"<STR_LIT>": "<STR_LIT>"<EOL>}<EOL>self._layer_id_to_layer[layer_id] = layer<EOL>self._svg["<STR_LIT:g>"].append(layer)<EOL><DEDENT>return self._layer_id_to_layer[layer_id]<EOL>

:return: the layer with the :paramref:`layer_id`. If the layer
  does not exist, it is created.
:param str layer_id: the id of the layer


return self._structure<EOL>


return lambda func: load_and_dump(create_loader, create_dumper, func)<EOL>

Same as :func:`load_and_dump` but returns a function to enable decorator
    syntax.

    Examples:

    .. code:: Python

        @decorate_load_and_dump(ContentLoader, JSONDumper)
        def convert_from_loader_to_dumper(loaded_stuff, other="arguments"):
            # convert
            return converted_stuff

        @decorate_load_and_dump(PathLoader, lambda dump: ContentDumper(dump,
            encoding=None))
        def convert_from_loader_to_dumper(loaded_stuff, to_file):
            # convert
            to_file.write(converted_stuff)


def load_and_dump(create_loader, create_dumper, load_and_dump_):

@wraps(load_and_dump_)<EOL>def load_and_dump__(*args1, **kw):<EOL><INDENT>"""<STR_LIT>"""<EOL>def load(*args2):<EOL><INDENT>"""<STR_LIT>"""<EOL>def dump(*args3):<EOL><INDENT>"""<STR_LIT>"""<EOL>return load_and_dump_(*(args2 + args3 + args1), **kw)<EOL><DEDENT>return create_dumper(dump)<EOL><DEDENT>return create_loader(load)<EOL><DEDENT>return load_and_dump__<EOL>

:return: a function that has the doc string of
      :paramref:`load_and_dump_`
      additional arguments to this function are passed on to
      :paramref:`load_and_dump_`.

    :param create_loader: a loader, e.g.
      :class:`knittingpattern.Loader.PathLoader`
    :param create_dumper: a dumper, e.g.
      :class:`knittingpattern.Dumper.ContentDumper`
    :param load_and_dump_: a function to call with the loaded content.
      The arguments to both, :paramref:`create_dumper` and,
      :paramref:`create_loader`
      will be passed to :paramref:`load_and_dump_`.
      Any additional arguments to the return value are also passed to
      :paramref:`load_and_dump_`.
      The return value of :paramref:`load_and_dump_` is passed back to the
      :paramref:`Dumper`.

    .. seealso:: :func:`decorate_load_and_dump`


if isinstance(content, str):<EOL><INDENT>return re.findall("<STR_LIT>", content)[-<NUM_LIT:1>]<EOL><DEDENT>return content.title.cdata<EOL>


return list(layout.walk_connections(lambda c: (c.start.xy, c.stop.xy)))<EOL>

The connections between the rows of the leyout.


return list(layout.walk_instructions(lambda p: (p.width, p.height)))<EOL>

The sizes of the instructions of the layout.


return GridLayout(pattern)<EOL>


def __init__(self, min_x, min_y, max_x, max_y,<EOL>default_color="<STR_LIT>"):

self._min_x = min_x<EOL>self._min_y = min_y<EOL>self._max_x = max_x<EOL>self._max_y = max_y<EOL>self._default_color = default_color<EOL>self._image = PIL.Image.new(<EOL>"<STR_LIT>", (max_x - min_x, max_y - min_y),<EOL>self._convert_to_image_color(default_color))<EOL>

Initialize the builder with the bounding box and a default color.

        .. _png-builder-bounds:

        ``min_x <= x < max_x`` and ``min_y <= y < max_y`` are the bounds of the
        instructions.
        Instructions outside the bounds are not rendered.
        Any Pixel that is not set has the :paramref:`default_color`.

        :param int min_x: the lower bound of the x coordinates
        :param int max_x: the upper bound of the x coordinates
        :param int min_y: the lower bound of the y coordinates
        :param int max_y: the upper bound of the y coordinates
        :param default_color: a valid :ref:`color <png-color>`


self._set_pixel_and_convert_color(x, y, color)<EOL>

set the pixel at ``(x, y)`` position to :paramref:`color`

        If ``(x, y)`` is out of the :ref:`bounds <png-builder-bounds>`
        this does not change the image.

        .. seealso:: :meth:`set_color_in_grid`


return webcolors.hex_to_rgb(rrggbb)<EOL>

:return: the color that is used by the image


return self._default_color<EOL>

:return: the :ref:`color <png-color>` of the pixels that are not set

        You can set this color by passing it to the :meth:`constructor
        <__init__>`.


def _set_pixel_and_convert_color(self, x, y, color):

if color is None:<EOL><INDENT>return<EOL><DEDENT>color = self._convert_color_to_rrggbb(color)<EOL>self._set_pixel(x, y, color)<EOL>

set the pixel but convert the color before.


super().__init__(self._dump_knitting_pattern,<EOL>text_is_expected=False, encoding=None)<EOL>self.__on_dump = function_that_returns_a_knitting_pattern_set<EOL>

Initialize the Dumper with a
        :paramref:`function_that_returns_a_knitting_pattern_set`.

        :param function_that_returns_a_knitting_pattern_set: a function that
          takes no arguments but returns a
          :class:`knittinpattern.KnittingPatternSet.KnittingPatternSet`

        When a dump is requested, the
        :paramref:`function_that_returns_a_knitting_pattern_set`
        is called and the knitting pattern set is converted and saved to the
        specified location.


for start in self.walk_instructions():<EOL><INDENT>for stop_instruction in start.instruction.consuming_instructions:<EOL><INDENT>if stop_instruction is None:<EOL><INDENT>continue<EOL><DEDENT>stop = self._walk.instruction_in_grid(stop_instruction)<EOL>connection = Connection(start, stop)<EOL>if connection.is_visible():<EOL><INDENT>yield mapping(connection)<EOL><DEDENT><DEDENT><DEDENT>

Iterate over connections between instructions.

        :return: an iterator over :class:`connections <Connection>` between
          :class:`instructions in grid <InstructionInGrid>`
        :param mapping: funcion to map the result, see
          :meth:`walk_instructions` for an example usage


return self._position.y<EOL>

:return: y coordinate in the grid
        :rtype: float


instructions = chain(*self.walk_rows(lambda row: row.instructions))<EOL>return map(mapping, instructions)<EOL>

Iterate over instructions.

        :return: an iterator over :class:`instructions in grid
          <InstructionInGrid>`
        :param mapping: funcion to map the result

        .. code:: python

            for pos, c in layout.walk_instructions(lambda i: (i.xy, i.color)):
                print("color {} at {}".format(c, pos))


while self._todo:<EOL><INDENT>args = self._todo.pop(<NUM_LIT:0>)<EOL>self._step(*args)<EOL><DEDENT>

Loop through all the instructions that are `_todo`.


return self._start<EOL>

:return: the start of the connection
        :rtype: InstructionInGrid


x = self.x<EOL>y = self.y<EOL>result = []<EOL>for instruction in self._row.instructions:<EOL><INDENT>instruction_in_grid = InstructionInGrid(instruction, Point(x, y))<EOL>x += instruction_in_grid.width<EOL>result.append(instruction_in_grid)<EOL><DEDENT>return result<EOL>

The instructions in a grid.

        :return: the :class:`instructions in a grid <InstructionInGrid>` of
          this row
        :rtype: list


return self._rows_in_grid[row]<EOL>

Returns an `RowInGrid` object for the `row`


super().__init__(position)<EOL>self._row = row<EOL>


def _expand(self, row, consumed_position, passed):

self._todo.append((row, consumed_position, passed))<EOL>

Add the arguments `(args, kw)` to `_walk` to the todo list.


def _row_should_be_placed(self, row, position):

placed_row = self._rows_in_grid.get(row)<EOL>return placed_row is None or placed_row.y < position.y<EOL>

:return: whether to place this instruction


self._rows_in_grid[row] = RowInGrid(row, position)<EOL>


return self._width<EOL>

:return: width of the object on the grid
        :rtype: float


self._start = start<EOL>self._stop = stop<EOL>

:param InstructionInGrid start: the start of the connection
:param InstructionInGrid stop: the end of the connection


return self._walk.row_in_grid(row)<EOL>

The a RowInGrid for the row with position information.

        :return: a row in the grid
        :rtype: RowInGrid


return self._instruction.color<EOL>

The color of the instruction.

        :return: the color of the :attr:`instruction`


min_x, min_y, max_x, max_y = zip(*list(self.walk_rows(<EOL>lambda row: row.bounding_box)))<EOL>return min(min_x), min(min_y), max(max_x), max(max_y)<EOL>

The minimum and maximum bounds of this layout.

        :return: ``(min_x, min_y, max_x, max_y)`` the bounding box
          of this layout
        :rtype: tuple


return self._bounding_box<EOL>

The bounding box of this object.

        :return: (min x, min y, max x, max y)
        :rtype: tuple


self._pattern = pattern<EOL>self._rows = list(pattern.rows)<EOL>self._walk = _RecursiveWalk(self._rows[<NUM_LIT:0>].instructions[<NUM_LIT:0>])<EOL>self._rows.sort(key=lambda row: self._walk.row_in_grid(row).yx)<EOL>

:param knittingpattern.KnittingPattern.KnittingPattern pattern: the
  pattern to layout


return self._position.y, self._position.x<EOL>

:return: ``(y, x)`` coordinate in the grid
        :rtype: tuple


return self._instruction<EOL>

The instruction.

        :return: instruction that is placed on the grid
        :rtype: knittingpattern.Instruction.InstructionInRow


return INSTRUCTION_HEIGHT<EOL>

:return: height of the object on the grid
        :rtype: float


self._rows_in_grid = {}<EOL>self._todo = []<EOL>self._expand(first_instruction.row, Point(<NUM_LIT:0>, <NUM_LIT:0>), [])<EOL>self._walk()<EOL>

Start walking the knitting pattern starting from first_instruction.


row_position = self._rows_in_grid[instruction.row].xy<EOL>x = instruction.index_of_first_consumed_mesh_in_row<EOL>position = Point(row_position.x + x, row_position.y)<EOL>return InstructionInGrid(instruction, position)<EOL>

Returns an `InstructionInGrid` object for the `instruction`


return SVGDumper(on_dump)<EOL>

Create a new SVGDumper with the function ``on_dump``.

        :rtype: knittingpattern.Dumper.SVGDumper


def instruction_to_svg_dict(self, instruction_or_id, copy_result=True):

instruction_id = self.get_instruction_id(instruction_or_id)<EOL>if instruction_id in self._cache:<EOL><INDENT>result = self._cache[instruction_id]<EOL><DEDENT>else:<EOL><INDENT>result = self._instruction_to_svg_dict(instruction_id)<EOL>self._cache[instruction_id] = result<EOL><DEDENT>if copy_result:<EOL><INDENT>result = deepcopy(result)<EOL><DEDENT>return result<EOL>

Return the SVG dict for the SVGBuilder.

        :param instruction_or_id: the instruction or id, see
          :meth:`get_instruction_id`
        :param bool copy_result: whether to copy the result
        :rtype: dict

        The result is cached.


return self._new_svg_dumper(lambda: self.instruction_to_svg_dict(<EOL>instruction_or_id, not i_promise_not_to_change_the_result))<EOL>

Return the SVG for an instruction.

        :param instruction_or_id: either an
          :class:`~knittingpattern.Instruction.Instruction` or an id
          returned by :meth:`get_instruction_id`
        :param bool i_promise_not_to_change_the_result:

          - :obj:`False`: the result is copied, you can alter it.
          - :obj:`True`: the result is directly from the cache. If you change
            the result, other calls of this function get the changed result.

        :return: an SVGDumper
        :rtype: knittingpattern.Dumper.SVGDumper


instruction_to_svg = InstructionToSVG()<EOL>instruction_to_svg.load.relative_folder(__name__, DEFAULT_SVG_FOLDER)<EOL>return instruction_to_svg<EOL>

load the default set of svg files for instructions

    :return: the default svg files for the instructions in this package
    :rtype: knittingpattern.InstructionToSVG.InstructionToSVG


svg_dict = self.default_instruction_to_svg_dict(instruction)<EOL>return xmltodict.unparse(svg_dict)<EOL>

As :meth:`instruction_to_svg` but it only takes the ``default.svg``
        file into account.

        In case no file is found for an instruction in
        :meth:`instruction_to_svg`,
        this method is used to determine the default svg for it.

        The content is created by replacing the text ``{instruction.type}`` in
        the whole svg file named ``default.svg``.

        If no file ``default.svg`` was loaded, an empty string is returned.


index = self.index_of_first_consumed_mesh_in_row<EOL>return index + self.number_of_consumed_meshes - <NUM_LIT:1><EOL>

The index of the last consumed mesh of this instruction in its row.

        Same as :attr:`index_of_last_produced_mesh_in_row`
        but for the last consumed mesh.


return [(mesh.consuming_instruction if mesh.is_consumed() else None)<EOL>for mesh in self.produced_meshes]<EOL>

Instructions that consume the meshes that this instruction produces.

        :return: a list of :class:`instructions
          <knittingpattern.Instruction.InstructionInRow>`
        :rtype: list

        .. seealso:: :attr:`producing_instructions`, :attr:`produced_meshes`


if self.has_color():<EOL><INDENT>return convert_color_to_rrggbb(self.color)<EOL><DEDENT>return None<EOL>

The color in "#RRGGBB" format.

        :return: the :attr:`color` in "#RRGGBB" format or none if no color is
          given


return self.number_of_produced_meshes != <NUM_LIT:0><EOL>

Whether this institution produces meshes.

        :return: whether this instruction produces any meshes
        :rtype: bool

        .. seealso:: :attr:`number_of_produced_meshes`


return ProducedMesh<EOL>

:return: the class of the produced meshes.


return self.number_of_consumed_meshes != <NUM_LIT:0><EOL>

Whether this instruction consumes meshes.

        :return: whether this instruction consumes any meshes
        :rtype: bool

        .. seealso:: :attr:`number_of_consumed_meshes`


return self.type == KNIT_TYPE<EOL>

Whether this instruction is a knit instruction.

        :return: whether this instruction is a knit instruction
        :rtype: bool


raise InstructionNotFoundInRow(self._instruction_not_found_message)<EOL>

Raise an error that this instruction is in its row no longer.

        :raises knittingpattern.Instruction.InstructionNotFoundInRow:
          the instruction was not found

        .. warning: private, do not use


return [self.color]<EOL>

All the colors that an instruction has.

        :return: a list of colors of the instruction. If the instruction has
          no color, this is ``[None]``.
        :rtype: list


return self.get(TYPE, DEFAULT_TYPE)<EOL>

The type of the instruction.

        :return: the :data:`type <TYPE>` of the instruction or
          :data:`DEFAULT_TYPE` if none is specified.
        :rtype: str

        The type should be a string.
        Depending on the type, the instruction can receive additional
        attributes.

        .. seealso:: :mod:`knittingpattern.InstructionLibrary`


return self.color is not None<EOL>

Whether this instruction has a color.

        :return: whether a :data:`color <COLOR>` is specified
        :rtype: bool


index = self.index_of_first_produced_mesh_in_row<EOL>return index + self.number_of_produced_meshes - <NUM_LIT:1><EOL>

Index of the last mesh produced by this instruction in its row.

        :return: an index of the last produced mesh of rows produced meshes
        :rtype: int

        .. note:: If this instruction :meth:`produces meshes
          <Instruction.produces_meshes>`, this is the index of
          its last produces mesh in the row. However, if this instruction does
          not produce meshes, this is the index **before** the first mesh of
          the instruction if it produced meshes.

        .. seealso:: :attr:`index_of_first_produced_mesh_in_row`


return ConsumedMesh<EOL>

:return: the class of the consumed meshes.


included_elements = set()<EOL>def included(element):<EOL><INDENT>result = element in included_elements<EOL>included_elements.add(element)<EOL>return result<EOL><DEDENT>return [element for elements in iterables for element in elements<EOL>if not included(element)]<EOL>

Create an iterable from the iterables that contains each element once.

    :return: an iterable over the iterables. Each element of the result
      appeared only once in the result. They are ordered by the first
      occurrence in the iterables.


def new_pattern(self, id_, name, rows=None):

if rows is None:<EOL><INDENT>rows = self.new_row_collection()<EOL><DEDENT>return self._spec.new_pattern(id_, name, rows, self)<EOL>

Create a new knitting pattern.

        If rows is :obj:`None` it is replaced with the
        :meth:`new_row_collection`.


self._start()<EOL>pattern_collection = self._new_pattern_collection()<EOL>self._fill_pattern_collection(pattern_collection, values)<EOL>self._create_pattern_set(pattern_collection, values)<EOL>return self._pattern_set<EOL>

Parse a knitting pattern set.

        :param dict value: the specification of the knitting pattern set
        :rtype: knittingpattern.KnittingPatternSet.KnittingPatternSet
        :raises knittingpattern.KnittingPatternSet.ParsingError: if
          :paramref:`value` does not fulfill the :ref:`specification
          <FileFormatSpecification>`.


return values[VERSION]<EOL>

:return: the version of :paramref:`values`.


self._instruction_todos.append(row)<EOL>

Add a deleyed inheritance that is ti be resolved later.

        When calling :meth:`_finish_instructions` this inheritance chain shall
        be resolved.


def instruction_in_row(self, row, specification):

whole_instruction_ = self._as_instruction(specification)<EOL>return self._spec.new_instruction_in_row(row, whole_instruction_)<EOL>

Parse an instruction.

        :param row: the row of the instruction
        :param specification: the specification of the instruction
        :return: the instruction in the row


row_id = self._to_id(values[ID])<EOL>row = self._spec.new_row(row_id, values, self)<EOL>if SAME_AS in values:<EOL><INDENT>self._delay_inheritance(row, self._to_id(values[SAME_AS]))<EOL><DEDENT>self._delay_instructions(row)<EOL>self._id_cache[row_id] = row<EOL>return row<EOL>


self._spec = specification<EOL>self._start()<EOL>

Create a parser with a specification.

        :param specification: the types and classes to use for the resulting
          object structure, preferably a
          :class:`knittingpattern.ParsingSpecification.ParsingSpecification`


def _fill_pattern_collection(self, pattern_collection, values):

pattern = values.get(PATTERNS, [])<EOL>for pattern_to_parse in pattern:<EOL><INDENT>parsed_pattern = self._pattern(pattern_to_parse)<EOL>pattern_collection.append(parsed_pattern)<EOL><DEDENT>


if TYPE not in values:<EOL><INDENT>self._error("<STR_LIT>"<EOL>"<STR_LIT>".format(KNITTING_PATTERN_TYPE))<EOL><DEDENT>type_ = values[TYPE]<EOL>if type_ != KNITTING_PATTERN_TYPE:<EOL><INDENT>self._error("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(type_, KNITTING_PATTERN_TYPE))<EOL><DEDENT>return type_<EOL>

:return: the type of a knitting pattern set.


def _delay_inheritance(self, prototype, parent_id):

self._inheritance_todos.append((prototype, parent_id))<EOL>

Add a deleyed inheritance that is ti be resolved later.

        When calling :meth:`_finish_inheritance` this inheritance chain shall
        be resolved.


return tuple(id_) if isinstance(id_, list) else id_<EOL>

Converts the argument to a object suitable as an identifier.

        :return: a hashable object


return self._spec.new_pattern_collection()<EOL>

Create a new pattern collection.

        :return: a new specified pattern collection for
          :meth:`knitting_pattern_set`


self._instruction_library = self._spec.new_default_instructions()<EOL>self._as_instruction = self._instruction_library.as_instruction<EOL>self._id_cache = {}<EOL>self._pattern_set = None<EOL>self._inheritance_todos = []<EOL>self._instruction_todos = []<EOL>


while self._inheritance_todos:<EOL><INDENT>prototype, parent_id = self._inheritance_todos.pop()<EOL>parent = self._id_cache[parent_id]<EOL>prototype.inherit_from(parent)<EOL><DEDENT>


return self._spec.new_row_collection()<EOL>

Create a new row collection.

        :return: a new specified row collection for the
          :meth:`knitting pattern <new_pattern>`


if self.is_connected():<EOL><INDENT>self._disconnect()<EOL><DEDENT>

Remove the connection between two rows through this mesh.

        After disconnecting this mesh, it can be connected anew.


assert other.is_mesh()<EOL>disconnected = not other.is_connected() and not self.is_connected()<EOL>types_differ = self._is_consumed_mesh() != other._is_consumed_mesh()<EOL>return disconnected and types_differ<EOL>

Whether a connection can be established between those two meshes.


self._assert_is_consumed()<EOL>return self._consuming_instruction_and_index()[<NUM_LIT:1>]<EOL>

Index in instruction as consumed mesh.

        :return: the index of the mesh in the list of meshes that
          :attr:`consuming_instruction` consumes
        :rtype: int

        .. code:: python

            instruction = mesh.consuming_instruction
            index = mesh.index_in_consuming_instruction
            assert instruction.consumed_meshes[index] == mesh

        .. seealso:: :attr:`consuming_instruction`,
          :attr:`index_in_consuming_instruction`

        .. warning:: Check with :meth:`is_consumed` before!


self._assert_is_consumed()<EOL>return self._consuming_row_and_index()[<NUM_LIT:1>]<EOL>

Index in row as consumed mesh.

        :return: the index of the mesh in the list of meshes that
          :attr:`consuming_row` consumes
        :rtype: int

        .. code:: python

            row = mesh.consuming_row
            index = mesh.index_in_consuming_row
            assert row.consumed_meshes[index] == mesh

        .. seealso:: :attr:`consuming_row`, :attr:`index_in_producing_row`

        .. warning:: Check with :meth:`is_consumed` before!


self.__consuming_instruction_and_index = (<EOL>consuming_instruction,<EOL>index_in_consuming_instruction<EOL>)<EOL>self._produced_part = None<EOL>

:param consuming_instruction: the
  :class:`instruction <knittingpattern.Instruction.InstructionInRow>`
  that consumes the mesh
:param int index_in_consuming_instruction: the index of the mesh
  in the list of meshes that :attr:`consuming_instruction`
  consumes

.. note:: There should be no necessity to create instances of this
  directly. You should be able to use
  ``instruction.produced_meshes`` or ``instruction.consumed_meshes``
  to access the :class:`meshes <knittingpattern.Mesh.Mesh>`.


return self._is_consumed() and self._is_produced()<EOL>

Returns whether this mesh is already connected.

        :return: whether this mesh is connected to an other.
        :rtype: bool


self._assert_is_consumed()<EOL>return self._consuming_row_and_index()[<NUM_LIT:0>]<EOL>

Row which consumes this mesh.

        :return: the row that consumes this mesh
        :rtype: knittingpattern.Row.Row

        .. seealso:: :attr:`index_in_consuming_row`,
          :attr:`consuming_instruction`, :attr:`producing_row`

        .. warning:: Check with :meth:`is_consumed` before!


return self._is_consumed()<EOL>

Whether the mesh has an instruction that consumed it.

        :return: whether the mesh is consumed by an instruction
        :rtype: bool

        If you get this mesh from
        :attr:`knittingpattern.Instruction.InstructionInRow.consumed_meshes` or
        :attr:`knittingpattern.Row.Row.consumed_meshes`,
        this should be :obj:`True`.

        .. warning:: Before you use any methods on how the mesh is consumed,
          you should check with ``mesh.is_consumed()``.


self._assert_is_produced()<EOL>return self._producing_instruction_and_index()[<NUM_LIT:1>]<EOL>

Index in instruction as a produced mesh.

        :return: the index of the mesh in the list of meshes that
          :attr:`producing_instruction` produces
        :rtype: int

        .. code:: python

            instruction = mesh.producing_instruction
            index = mesh.index_in_producing_instruction
            assert instruction.produced_meshes[index] == mesh

        .. seealso:: :attr:`producing_instruction`,
          :attr:`index_in_consuming_instruction`

        .. warning:: Check with :meth:`is_produced` before!


self._assert_is_produced()<EOL>return self._producing_row_and_index()[<NUM_LIT:1>]<EOL>

Index in row as produced mesh.

        :return: the index of the mesh in the :attr:`producing_row`
        :rtype: int

        .. code:: python

            row = mesh.producing_row
            index = mesh.index_in_producing_row
            assert row[index] == mesh

        .. seealso:: :attr:`producing_row`, :attr:`index_in_consuming_row`

        .. warning:: Check with :meth:`is_produced` before!


if self.__text_is_expected:<EOL><INDENT>return self._string()<EOL><DEDENT>else:<EOL><INDENT>return self._bytes().decode(self.__encoding)<EOL><DEDENT>


if file is None:<EOL><INDENT>file = BytesIO()<EOL><DEDENT>self._binary_file(file)<EOL>return file<EOL>

Same as :meth:`file` but for binary content.


return self.__encoding<EOL>

:return: the encoding for byte to string conversion
        :rtype: str


if self.__text_is_expected:<EOL><INDENT>return self.string().encode(self.__encoding)<EOL><DEDENT>else:<EOL><INDENT>return self._bytes()<EOL><DEDENT>


return self._temporary_file(delete_when_closed)<EOL>

Saves the dump in a temporary file and returns the open file object.

        :param bool delete_when_closed: whether to delete the temporary file
                                        when it is closed.
        :return: a file-like object

        If :paramref:`delete_when_closed` is :obj:`True` (default) the file
        on the hard drive will be deleted if it is closed or not referenced
        any more.

        If :paramref:`delete_when_closed` is :obj:`False` the returned
        temporary file is not deleted when closed or unreferenced.
        The user of this method has then the responsibility to free the
        space on the host system.

        The returned file-like object has an attribute ``name`` that holds
        the location of the file.


Saves the dump in a file named :paramref:`path`.

        :param str path: a valid path to a file location. The file can exist.


return "<STR_LIT>".format(<EOL>self.__class__.__name__,<EOL>self.__encoding<EOL>)<EOL>

the string representation for people to read

        :return: the string representation of this object
        :rtype: str


file = StringIO()<EOL>self.__dump_to_file(file)<EOL>file.seek(<NUM_LIT:0>)<EOL>return file.read()<EOL>

:return: the string from a :class:`io.StringIO`


super().__init__(self._dump_to_file)<EOL>self.__dump_object = on_dump<EOL>

Create a new JSONDumper object with the callable `on_dump`.

        `on_dump` takes no arguments and returns the object that should be
        serialized to JSON.


json.dump(self.object(), file)<EOL>


bytes_ = string.encode(self._encoding)<EOL>self._file.write(bytes_)<EOL>


string = bytes_.decode(self._encoding)<EOL>self._file.write(string)<EOL>


self.__specification.insert(<NUM_LIT:1>, new_specification)<EOL>

Inherit from a :paramref:`new_specification`

        :param new_specification: a specification as passed to :meth:`__init__`

        The :paramref:`new_specification` is inserted before the first
        :paramref:`inherited value <__init__.inherited_values>`.

        If the order is

        1. :paramref:`~__init__.specification`
        2. :paramref:`~__init__.inherited_values`

        after calling ``prototype.inherit_from(new_specification)`` the lookup
        order is

        1. :paramref:`~__init__.specification`
        2. :paramref:`new_specification`
        3. :paramref:`~__init__.inherited_values`


def __init__(self, specification, inherited_values=()):

self.__specification = [specification] + list(inherited_values)<EOL>

create a new prototype

        :param specification: the specification of the prototype.
          This specification can be inherited by other prototypes.
          It can be a :class:`dict` or an other
          :class:`knittingpattern.Prototype.Prototype` or anything else that
          supports :meth:`__contains__` and :meth:`__getitem__`

        To look up a key in the specification it will be walked through

        1. :paramref:`specification`
        2. :paramref:`inherited_values` in order

        However, new lookups can be inserted at before
        :paramref:`inherited_values`, by calling :meth:`inherit_from`.


def __init__(self, type_, version, patterns, parser, comment=None):

self._version = version<EOL>self._type = type_<EOL>self._patterns = patterns<EOL>self._comment = comment<EOL>self._parser = parser<EOL>

Create a new knitting pattern set.

        This is the class for a set of :class:`knitting patterns
        <knittingpattern.KnittingPattern.KnittingPattern>`.

        :param str type: the type of the knitting pattern set, see the
          :ref:`specification <FileFormatSpecification>`.
        :param str version: the version of the knitting pattern set.
          This is not the version of the library but the version of the
          :ref:`specification <FileFormatSpecification>`.
        :param patterns: a collection of patterns. This should be a
          :class:`~knittingpattern.IdCollection.IdCollection` of
          :class:`KnittingPatterns
          <knittingpattern.KnittingPattern.KnittingPattern>`.
        :param comment: a comment about the knitting pattern


return self._comment<EOL>

The comment about the knitting pattern.

        :return: the comment for the knitting pattern set or None,
          see :meth:`__init__`.


def add_new_pattern(self, id_, name=None):

if name is None:<EOL><INDENT>name = id_<EOL><DEDENT>pattern = self._parser.new_pattern(id_, name)<EOL>self._patterns.append(pattern)<EOL>return pattern<EOL>

Add a new, empty knitting pattern to the set.

        :param id_: the id of the pattern
        :param name: the name of the pattern to add or if :obj:`None`, the
          :paramref:`id_` is used
        :return: a new, empty knitting pattern
        :rtype: knittingpattern.KnittingPattern.KnittingPattern


return os.path.abspath(os.path.join(*args))<EOL>

:return: an absolute path to the joined arguments
:param args: the parts of the path to join


return re.findall(WARNING_PATTERN, sphinx_build)<EOL>

:return: the warnings during the build process.


try:<EOL><INDENT>print(bytes_.decode())<EOL><DEDENT>except UnicodeDecodeError:<EOL><INDENT>print(bytes_)<EOL><DEDENT>


if os.path.exists(BUILD_DIRECTORY):<EOL><INDENT>shutil.rmtree(BUILD_DIRECTORY)<EOL><DEDENT>output = subprocess.check_output(<EOL>"<STR_LIT>", shell=True, cwd=DOCS_DIRECTORY,<EOL>stderr=subprocess.STDOUT<EOL>)<EOL>output += subprocess.check_output(<EOL>"<STR_LIT>", shell=True, cwd=DOCS_DIRECTORY,<EOL>stderr=subprocess.STDOUT<EOL>)<EOL>print(output.decode())<EOL>return output<EOL>

Build the documentation with sphinx and return the output.


value = json.dumps(value)<EOL>with warnings.catch_warnings():<EOL><INDENT>warnings.simplefilter("<STR_LIT:ignore>")<EOL>encrypted_value = self.cipher.encrypt(value.encode('<STR_LIT:utf8>'))<EOL><DEDENT>hexified_value = binascii.hexlify(encrypted_value).decode('<STR_LIT:ascii>')<EOL>return hexified_value<EOL>

Turn a json serializable value into an jsonified, encrypted,
        hexa string.


self._assert_valid_stash()<EOL>if key_name == '<STR_LIT>':<EOL><INDENT>raise GhostError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>if not self.get(key_name):<EOL><INDENT>raise GhostError('<STR_LIT>'.format(key_name))<EOL><DEDENT>key = self._storage.get(key_name)<EOL>if key.get('<STR_LIT>'):<EOL><INDENT>raise GhostError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(key_name))<EOL><DEDENT>deleted = self._storage.delete(key_name)<EOL>audit(<EOL>storage=self._storage.db_path,<EOL>action='<STR_LIT>',<EOL>message=json.dumps(dict(key_name=key_name)))<EOL>if not deleted:<EOL><INDENT>raise GhostError('<STR_LIT>'.format(key_name))<EOL><DEDENT>


def assert_key_is_unlocked(existing_key):<EOL><INDENT>if existing_key and existing_key.get('<STR_LIT>'):<EOL><INDENT>raise GhostError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(name))<EOL><DEDENT><DEDENT>def assert_value_provided_for_new_key(value, existing_key):<EOL><INDENT>if not value and not existing_key.get('<STR_LIT:value>'):<EOL><INDENT>raise GhostError('<STR_LIT>')<EOL><DEDENT><DEDENT>self._assert_valid_stash()<EOL>self._validate_key_schema(value, key_type)<EOL>if value and encrypt and not isinstance(value, dict):<EOL><INDENT>raise GhostError('<STR_LIT>')<EOL><DEDENT>key = self._handle_existing_key(name, modify or add)<EOL>assert_key_is_unlocked(key)<EOL>assert_value_provided_for_new_key(value, key)<EOL>new_key = dict(name=name, lock=lock)<EOL>if value:<EOL><INDENT>if add:<EOL><INDENT>value = self._update_existing_key(key, value)<EOL><DEDENT>new_key['<STR_LIT:value>'] = self._encrypt(value) if encrypt else value<EOL><DEDENT>else:<EOL><INDENT>new_key['<STR_LIT:value>'] = key.get('<STR_LIT:value>')<EOL><DEDENT>new_key['<STR_LIT:description>'] = description or key.get('<STR_LIT:description>')<EOL>new_key['<STR_LIT>'] = key.get('<STR_LIT>') or _get_current_time()<EOL>new_key['<STR_LIT>'] = _get_current_time()<EOL>new_key['<STR_LIT>'] = metadata or key.get('<STR_LIT>')<EOL>new_key['<STR_LIT>'] = key.get('<STR_LIT>') or str(uuid.uuid4())<EOL>new_key['<STR_LIT:type>'] = key.get('<STR_LIT:type>') or key_type<EOL>key_id = self._storage.put(new_key)<EOL>audit(<EOL>storage=self._storage.db_path,<EOL>action='<STR_LIT>' if (modify or add) else '<STR_LIT>',<EOL>message=json.dumps(dict(<EOL>key_name=new_key['<STR_LIT:name>'],<EOL>value='<STR_LIT>',<EOL>description=new_key['<STR_LIT:description>'],<EOL>uid=new_key['<STR_LIT>'],<EOL>metadata=json.dumps(new_key['<STR_LIT>']),<EOL>lock=new_key['<STR_LIT>'],<EOL>type=new_key['<STR_LIT:type>'])))<EOL>return key_id<EOL>

Put a key inside the stash

        if key exists and modify true: delete and create
        if key exists and modify false: fail
        if key doesn't exist and modify true: fail
        if key doesn't exist and modify false: create

        `name` is unique and cannot be changed.

        `value` must be provided if the key didn't already exist, otherwise,
        the previous value will be retained.

        `created_at` will be left unmodified if the key
        already existed. Otherwise, the current time will be used.

        `modified_at` will be changed to the current time
        if the field is being modified.

        `metadata` will be updated if provided. If it wasn't
        provided the field from the existing key will be used and the
        same goes for the `uid` which will be generated if it didn't
        previously exist.

        `lock` will lock the key to prevent it from being modified or deleted

        `add` allows to add values to an existing key instead of overwriting.

        Returns the id of the key in the database


self.client.write(self._key_path(key['<STR_LIT:name>']), **key)<EOL>return self._key_path(key['<STR_LIT:name>'])<EOL>

Put and return the only unique identifier possible, its path


return self.db.insert(key)<EOL>

Insert the key and return its database id


self.client.delete_object(<EOL>Bucket=self.db_path,<EOL>Key=key_name)<EOL>return self.get(key_name) == {}<EOL>

Delete the key.
        :return: True if it was deleted, False otherwise


Insert a key to the stash

    `KEY_NAME` is the name of the key to insert

    `VALUE` is a key=value argument which can be provided multiple times.
    it is the encrypted value of your key


def load(self, origin_passphrase, keys=None, key_file=None):

<EOL>self._assert_valid_stash()<EOL>if not (bool(keys) ^ bool(key_file)):<EOL><INDENT>raise GhostError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>if key_file:<EOL><INDENT>with open(key_file) as stash_file:<EOL><INDENT>keys = json.loads(stash_file.read())<EOL><DEDENT><DEDENT>decrypt = origin_passphrase != self.passphrase<EOL>if decrypt:<EOL><INDENT>stub = Stash(TinyDBStorage('<STR_LIT>'), origin_passphrase)<EOL><DEDENT>for key in keys:<EOL><INDENT>self.put(<EOL>name=key['<STR_LIT:name>'],<EOL>value=stub._decrypt(key['<STR_LIT:value>']) if decrypt else key['<STR_LIT:value>'],<EOL>metadata=key['<STR_LIT>'],<EOL>description=key['<STR_LIT:description>'],<EOL>lock=key.get('<STR_LIT>'),<EOL>key_type=key.get('<STR_LIT:type>'),<EOL>encrypt=decrypt)<EOL><DEDENT>

Import keys to the stash from either a list of keys or a file

        `keys` is a list of dictionaries created by `self.export`
        `stash_path` is a path to a file created by `self.export`


Ghost generates a secret-store in which you can
    keep your secrets encrypted. Ghost isn't real. It's just in your head.


<EOL>return self.db.search(Query().name.matches('<STR_LIT>'))<EOL>

Return a list of all keys (not just key names, but rather the keys
        themselves).

        e.g.
         {u'created_at': u'2016-10-10 08:31:53',
          u'description': None,
          u'metadata': None,
          u'modified_at': u'2016-10-10 08:31:53',
          u'name': u'aws',
          u'uid': u'459f12c0-f341-413e-9d7e-7410f912fb74',
          u'value': u'the_value'},
         {u'created_at': u'2016-10-10 08:32:29',
          u'description': u'my gcp token',
          u'metadata': {u'owner': u'nir'},
          u'modified_at': u'2016-10-10 08:32:29',
          u'name': u'gcp',
          u'uid': u'a51a0043-f241-4d52-93c1-266a3c5de15e',
          u'value': u'the_value'}]


return json.loads(base64.b64decode(data['<STR_LIT>']).decode('<STR_LIT:utf-8>'))<EOL>

Decode one key as returned by consul.

        The format of the data returned is [{'Value': base-64-encoded-json,
        'Key': keyname}]. We need to decode and return just the values.


self._assert_valid_stash()<EOL>key = self._storage.get(key_name).copy()<EOL>if not key.get('<STR_LIT:value>'):<EOL><INDENT>return None<EOL><DEDENT>if decrypt:<EOL><INDENT>key['<STR_LIT:value>'] = self._decrypt(key['<STR_LIT:value>'])<EOL><DEDENT>audit(<EOL>storage=self._storage.db_path,<EOL>action='<STR_LIT:GET>',<EOL>message=json.dumps(dict(key_name=key_name)))<EOL>return key<EOL>

Return a key with its parameters if it was found.


stash = _get_stash(backend, stash, passphrase)<EOL>click.echo('<STR_LIT>'.format(key_file))<EOL>stash.load(origin_passphrase, key_file=key_file)<EOL>click.echo('<STR_LIT>')<EOL>

Load all keys from an exported key file to the stash

    `KEY_FILE` is the exported stash file to load keys from


self._change_lock_state(key_name, lock=True)<EOL>

Lock a key to prevent it from being deleted, purged and modified


stash = _get_stash(backend, stash, passphrase)<EOL>try:<EOL><INDENT>click.echo('<STR_LIT>'.format(output_path))<EOL>stash.export(output_path=output_path)<EOL>click.echo('<STR_LIT>')<EOL><DEDENT>except GhostError as ex:<EOL><INDENT>sys.exit(ex)<EOL><DEDENT>


command = ['<STR_LIT>', '<STR_LIT>', conn_info['<STR_LIT>'], conn_info['<STR_LIT>']]<EOL>if conn_info.get('<STR_LIT>') and not no_tunnel:<EOL><INDENT>command.insert(<NUM_LIT:1>, conn_info.get('<STR_LIT>'))<EOL>command.insert(<NUM_LIT:1>, '<STR_LIT>')<EOL>command.insert(<NUM_LIT:1>, '<STR_LIT>')<EOL><DEDENT>if conn_info.get('<STR_LIT>'):<EOL><INDENT>command.extend(_build_proxy_command(conn_info))<EOL><DEDENT>if conn_info.get('<STR_LIT>'):<EOL><INDENT>command.append(conn_info.get('<STR_LIT>'))<EOL><DEDENT>return command<EOL>

# TODO: Document clearly
IndetityFile="~/.ssh/id_rsa"
ProxyCommand="ssh -i ~/.ssh/id_rsa proxy_IP nc HOST_IP HOST_PORT"


stash = _get_stash(backend, stash, passphrase)<EOL>try:<EOL><INDENT>click.echo('<STR_LIT>')<EOL>stash.lock(key_name=key_name)<EOL>click.echo('<STR_LIT>')<EOL><DEDENT>except GhostError as ex:<EOL><INDENT>sys.exit(ex)<EOL><DEDENT>

Lock a key to prevent it from being deleted, purged or modified

    `KEY_NAME` is the name of the key to lock


return True<EOL>

...and therefore, this should always return true


<EOL>self.es.indices.create(index=self.params['<STR_LIT:index>'], ignore=<NUM_LIT>)<EOL>

Create an Elasticsearch index if necessary


return '<STR_LIT>' + self._stash_name + '<STR_LIT:/>' + key_name<EOL>

Return a valid vault path

        Note that we don't use os.path.join as the path is read by vault using
        slashes even on Windows.


Consul creates directories on the fly, so no init is required.


Migrate all keys from a source stash to a destination stash.

    `SOURCE_STASH_PATH` and `DESTINATION_STASH_PATH` are the paths
    to the stashs you wish to perform the migration on.


chars = string.ascii_lowercase + string.ascii_uppercase + string.digits<EOL>return str('<STR_LIT>'.join(random.choice(chars) for _ in range(size)))<EOL>

Return a generate string `size` long based on lowercase, uppercase,
    and digit chars


try:<EOL><INDENT>self.client.create_bucket(<EOL>Bucket=self.db_path,<EOL>CreateBucketConfiguration=self.bucket_configuration)<EOL><DEDENT>except botocore.exceptions.ClientError as e:<EOL><INDENT>if '<STR_LIT>' not in str(<EOL>e.response['<STR_LIT>']['<STR_LIT>']):<EOL><INDENT>raise e<EOL><DEDENT><DEDENT>


def search(self, body, filter_path, **kwargs):

if '<STR_LIT>' in body['<STR_LIT>']:<EOL><INDENT>items = list(self.store.items())<EOL>for name, key in items:<EOL><INDENT>return self.store[name]<EOL><DEDENT>else:<EOL><INDENT>return {'<STR_LIT>': {'<STR_LIT>': []}}<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return self.store.get(body['<STR_LIT>']['<STR_LIT>']['<STR_LIT:name>'])<EOL><DEDENT>

{
    u'hits': {
        u'hits': [
            {
                u'_id': u'AVewADAWUnUKEMeMQ4QB',
                u'_source': {
                    u'description': None,
                    u'created_at':
                    u'2016-10-10 22:09:44',
                    u'modified_at':
                    u'2016-10-10 22:09:44',
                    u'value': u'the_value',
                    u'name': u'aws',
                    u'uid': u'7a1caa7d-14d4-4045-842c-66adf22190b5',
                    u'metadata': None
                }
            },
        ]
    }
}