@wraps(method)<EOL>def wrapper(self, val):<EOL><INDENT>try:<EOL><INDENT>return method(self, val)<EOL><DEDENT>except:<EOL><INDENT>exc_cls, inst, tb = sys.exc_info()<EOL>if hasattr(inst, '<STR_LIT>'):<EOL><INDENT>_, expr, _, inner_val = Q.__debug_info__<EOL>Q.__debug_info__ = QDebug(self, expr, val, inner_val)<EOL>raise<EOL><DEDENT>if issubclass(exc_cls, KeyError):  <EOL><INDENT>exc_cls = QKeyError<EOL><DEDENT>prettyval = repr(val)<EOL>if len(prettyval) > <NUM_LIT>:<EOL><INDENT>prettyval = "<STR_LIT>" % (type(val).__name__)<EOL><DEDENT>msg = "<STR_LIT>".format(<EOL>inst, prettyval, self)<EOL>new_exc = exc_cls(msg)<EOL>new_exc._RERAISE = True<EOL>Q.__debug_info__ = QDebug(self, self, val, val)<EOL>six.reraise(exc_cls, new_exc, tb)<EOL><DEDENT><DEDENT>return wrapper<EOL>

Decorator for eval_ that prints a helpful error message
if an exception is generated in a Q expression


if isinstance(value, six.binary_type):<EOL><INDENT>try:<EOL><INDENT>value = value.decode('<STR_LIT:utf-8>')<EOL><DEDENT>except UnicodeDecodeError:  <EOL><INDENT>value = six.text_type(_dequote(repr(value)))  <EOL><DEDENT><DEDENT>result = six.text_type(value)<EOL>if isinstance(value, six.text_type):<EOL><INDENT>result = "<STR_LIT>" % result<EOL><DEDENT>return result<EOL>

Convert to unicode, and add quotes if initially a string


return self<EOL>

Require that a node is not null

Null values will raise NullValueError, whereas nonnull
values return self.

useful for being strict about portions of queries.

Examples:

   node.find('a').nonnull().find('b').orelse(3)

   This will raise an error if find('a') doesn't match,
   but provides a fallback if find('b') doesn't match.


if isinstance(value, Wrapper):<EOL><INDENT>return value<EOL><DEDENT>if hasattr(value, '<STR_LIT>'):<EOL><INDENT>return Node(value)<EOL><DEDENT>return Scalar(value)<EOL>

Wrap value in the appropriate wrapper class,
based upon its type.


if args and kwargs:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if args:<EOL><INDENT>result = tuple(_unwrap(self.apply(func)) for func in args)<EOL><DEDENT>else:<EOL><INDENT>result = dict((name, _unwrap(self.apply(func)))<EOL>for name, func in kwargs.items())<EOL><DEDENT>return Wrapper.wrap(result)<EOL>

Extract derived values into a Scalar(tuple) or Scalar(dict)

The keyword names passed to this function become keys in
the resulting dictionary, while positional arguments passed to
this function become elements in the resulting tuple.

The positional arguments and keyword values are functions that
are called on this Node.

Notes:

    - The input functions are called on the Node, **not** the
      underlying BeautifulSoup element
    - If the function returns a wrapper, it will be unwrapped
    - Only either positional arguments or keyword arguments may
      be passed, not both.

Example:

    >>> soup = Soupy("<b>hi</b>").find('b')
    >>> data = soup.dump(name=Q.name, text=Q.text).val()
    >>> data == {'text': 'hi', 'name': 'b'}
    True

    >> name, text = soup.dump(Q.name, Q.text).val()
    >> (name, text) == ('hi', 'b')
    True


raise NullValueError()<EOL>


return Wrapper.wrap(value)<EOL>


return self<EOL>


def require(self, func, msg="<STR_LIT>"):

raise NullValueError()<EOL>


return Scalar(True)<EOL>

Return Scalar(True) if this item is a null value


return Wrapper.wrap(_make_callable(func)(self._value))<EOL>

Call a function on a wrapper's value, and wrap the result if necessary.

Parameters:

    func : function(val) -> val

Examples:

    >>> s = Scalar(3)
    >>> s.map(Q * 2)
    Scalar(6)


return Wrapper.wrap(_make_callable(func)(self))<EOL>

Call a function on a wrapper, and wrap the result if necessary.

Parameters:

    func: function(wrapper) -> val

Examples:

    >>> s = Scalar(5)
    >>> s.apply(lambda val: isinstance(val, Scalar))
    Scalar(True)


return self<EOL>

Provide a fallback value for failed matches.

Examples:

    >>> Scalar(5).orelse(10).val()
    5
    >>> Null().orelse(10).val()
    10


return self._value<EOL>

Return the value inside a wrapper.

Raises :class:`NullValueError` if called on a Null object


if self.apply(func):<EOL><INDENT>return self<EOL><DEDENT>raise NullValueError(msg)<EOL>

Assert that self.apply(func) is True.

Parameters:

    func : func(wrapper)
    msg : str
       The error message to display on failure

Returns:

    If self.apply(func) is True, returns self.
    Otherwise, raises NullValueError.


return Scalar(False)<EOL>


return list(self.iter_val())<EOL>

Unwraps each item in the collection, and returns as a list


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

Return the first element of the collection, or :class:`Null`


return (item.val() for item in self._items)<EOL>


funcs = list(map(_make_callable, funcs))<EOL>if len(funcs) == <NUM_LIT:1>:<EOL><INDENT>return Collection(map(funcs[<NUM_LIT:0>], self._items))<EOL><DEDENT>tupler = lambda item: Scalar(<EOL>tuple(_unwrap(func(item)) for func in funcs))<EOL>return Collection(map(tupler, self._items))<EOL>

Call `func` on each element in the collection.

If multiple functions are provided, each item
in the output will be a tuple of each
func(item) in self.

Returns a new Collection.

Example:

    >>> col = Collection([Scalar(1), Scalar(2)])
    >>> col.each(Q * 10)
    Collection([Scalar(10), Scalar(20)])
    >>> col.each(Q * 10, Q - 1)
    Collection([Scalar((10, 0)), Scalar((20, 1))])


func = _make_callable(func)<EOL>inverse = lambda x: not func(x)<EOL>return self.filter(inverse)<EOL>

Return a new Collection excluding some items

Parameters:

    func : function(Node) -> Scalar

        A function that, when called on each item
        in the collection, returns a boolean-like
        value. If no function is provided, then
        truthy items will be removed.

Returns:

    A new Collection consisting of the items
    where bool(func(item)) == False


func = _make_callable(func)<EOL>return Collection(filter(func, self._items))<EOL>

Return a new Collection with some items removed.

Parameters:

    func : function(Node) -> Scalar

        A function that, when called on each item
        in the collection, returns a boolean-like
        value. If no function is provided, then
        false-y items will be removed.

Returns:

    A new Collection consisting of the items
    where bool(func(item)) == True

Examples:

    node.find_all('a').filter(Q['href'].startswith('http'))


func = _make_callable(func)<EOL>return Collection(takewhile(func, self._items))<EOL>

Return a new Collection with the last few items removed.

Parameters:

    func : function(Node) -> Node

Returns:

    A new Collection, discarding all items
    at and after the first item where bool(func(item)) == False

Examples:

    node.find_all('tr').takewhile(Q.find_all('td').count() > 3)


func = _make_callable(func)<EOL>return Collection(dropwhile(func, self._items))<EOL>

Return a new Collection with the first few items removed.

Parameters:

    func : function(Node) -> Node

Returns:

    A new Collection, discarding all items
    before the first item where bool(func(item)) == True


return self.each(Q.dump(*args, **kwargs))<EOL>

Build a list of dicts, by calling :meth:`Node.dump`
on each item.

Each keyword provides a function that extracts a value
from a Node.

Examples:

    >>> c = Collection([Scalar(1), Scalar(2)])
    >>> c.dump(x2=Q*2, m1=Q-1).val()
    [{'x2': 2, 'm1': 0}, {'x2': 4, 'm1': 1}]


return Scalar(len(self))<EOL>

Return the number of items in the collection, as a :class:`Scalar`


args = [_unwrap(item) for item in (self,) + others]<EOL>ct = self.count()<EOL>if not all(len(arg) == ct for arg in args):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>return Collection(map(Wrapper.wrap, zip(*args)))<EOL>

Zip the items of this collection with one or more
other sequences, and wrap the result.

Unlike Python's zip, all sequences must be the same length.

Parameters:

    others: One or more iterables or Collections

Returns:

    A new collection.

Examples:

    >>> c1 = Collection([Scalar(1), Scalar(2)])
    >>> c2 = Collection([Scalar(3), Scalar(4)])
    >>> c1.zip(c2).val()
    [(1, 3), (2, 4)]


return Scalar(dict(zip(_unwrap(keys), self.val())))<EOL>

Turn this collection into a Scalar(dict), by zipping keys and items.

Parameters:

    keys: list or Collection of NavigableStrings
        The keys of the dictionary

Examples:

    >>> c = Collection([Scalar(1), Scalar(2)])
    >>> c.dictzip(['a', 'b']).val() == {'a': 1, 'b': 2}
    True


return self.map(all)<EOL>

Scalar(True) if all items are truthy, or collection is empty.


return self.map(any)<EOL>

Scalar(True) if any items are truthy. False if empty.


return self.map(lambda items: not any(items))<EOL>

Scalar(True) if no items are truthy, or collection is empty.


return self._wrap_multi(operator.attrgetter('<STR_LIT>'))<EOL>

A :class:`Collection` of the child elements.


A :class:`Collection` of the parents elements.


A :class:`Collection` of all elements nested inside this Node.


A :class:`Collection` of all siblings after this node


A :class:`Collection` of all siblings before this node


return self._wrap_node(operator.attrgetter('<STR_LIT>'))<EOL>

The parent :class:`Node`, or :class:`NullNode`


The :class:`Node` sibling after this, or :class:`NullNode`


The :class:`Node` sibling prior to this, or :class:`NullNode`


return self._wrap_scalar(operator.attrgetter('<STR_LIT>'))<EOL>

A :class:`Scalar` of this Node's attribute dictionary

Example:

    >>> Soupy("").find('a').attrs
    Scalar({u'val': u'3'})


return self._wrap_scalar(operator.attrgetter('<STR_LIT:text>'))<EOL>

A :class:`Scalar` of this Node's text.

Example:

    >>> node = Soupy('
hi there
').find('p')
    >>> node
    Node(
hi there
)
    >>> node.text
    Scalar(u'hi there')


return self._wrap_scalar(operator.attrgetter('<STR_LIT:name>'))<EOL>

A :class:`Scalar` of this Node's tag name.

Example:

    >>> node = Soupy('
hi there
').find('p')
    >>> node
    Node(
hi there
)
    >>> node.name
    Scalar(u'p')


op = operator.methodcaller('<STR_LIT>', *args, **kwargs)<EOL>return self._wrap_node(op)<EOL>

Find a single Node among this Node's descendants.

Returns :class:`NullNode` if nothing matches.

This inputs to this function follow the same semantics
as BeautifulSoup. See http://bit.ly/bs4doc for more info.

Examples:

 - node.find('a')  # look for `a` tags
 - node.find('a', 'foo') # look for `a` tags with class=`foo`
 - node.find(func) # find tag where func(tag) is True
 - node.find(val=3)  # look for tag like <a, val=3>


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

Like :meth:`find`, but searches through :attr:`next_siblings`


Like :meth:`find`, but searches through :attr:`parents`


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

Like :meth:`find`, but searches through :attr:`previous_siblings`


op = operator.methodcaller('<STR_LIT>', *args, **kwargs)<EOL>return self._wrap_multi(op)<EOL>

Like :meth:`find`, but selects all matches (not just the first one).

Returns a :class:`Collection`.

If no elements match, this returns a Collection with no items.


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

Like :meth:`find_all`, but searches through :attr:`next_siblings`


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

Like :meth:`find_all`, but searches through :attr:`parents`


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

Like :meth:`find_all`, but searches through :attr:`previous_siblings`


op = operator.methodcaller('<STR_LIT>', selector)<EOL>return self._wrap_multi(op)<EOL>

Like :meth:`find_all`, but takes a CSS selector string as input.


return Scalar({})<EOL>


return Scalar(self._value.string)<EOL>


return Scalar('<STR_LIT>')<EOL>


return Collection([])<EOL>


return NullNode()<EOL>


return NullCollection()<EOL>


return Null()<EOL>


return val<EOL>

Pass the argument ``val`` to the function, and return the result.

This special method is necessary because the ``__call__`` method
builds a new function stead of evaluating the current one.


result = self.__debug_info__<EOL>if isinstance(result, QDebug):<EOL><INDENT>return result<EOL><DEDENT>return QDebug(None, None, None, None)<EOL>

Returns debugging information for the previous error raised
during expression evaluation.

Returns a QDebug namedtuple with four fields:

  - expr is the last full expression to have raised an exception
  - inner_expr is the specific sub-expression that raised the exception
  - val is the value that expr tried to evaluate.
  - inner_val is the value that inner_expr tried to evaluate

If no exceptions have been triggered from expression evaluation,
then each field is None.

Examples:

    >>> Scalar('test').map(Q.upper().foo)
    Traceback (most recent call last):
    ...
    AttributeError: 'str' object has no attribute 'foo'
    ...
    >>> dbg = Q.debug_()
    >>> dbg.expr
    Q.upper().foo
    >>> dbg.inner_expr
    .foo
    >>> dbg.val
    'test'
    >>> dbg.inner_val
    'TEST'


def __init__(self, name, min_val, max_val):

self.name = name<EOL>self.min_val = min_val<EOL>self.max_val = max_val<EOL>if type(min_val) != type(max_val):<EOL><INDENT>raise ValueError('<STR_LIT>'.format(<EOL>type(min_val),<EOL>type(max_val))<EOL>)<EOL><DEDENT>self.dtype = type(min_val + max_val)<EOL>if self.dtype not in SUPPORTED_DTYPES:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>.format(SUPPORTED_DTYPES))<EOL><DEDENT>

Parameter object

        Args:
            name (str): name of the parameter
            min_val (int or float): minimum allowed value for the parameter
            max_val (int or float): maximum allowed value for the parameter


def __init__(self, parameters, cost_fn_val):

self.parameters = parameters<EOL>self.cost_fn_val = cost_fn_val<EOL>self.fitness_score = self.__calc_fitness_score(cost_fn_val)<EOL>

Member object

        Args:
            parameters (dictionary): dictionary of parameter names and values
            cost_fn_val (float): value returned by cost function using params


if cost_fn_val >= <NUM_LIT:0>:<EOL><INDENT>return <NUM_LIT:1> / (cost_fn_val + <NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>return <NUM_LIT:1> + abs(cost_fn_val)<EOL><DEDENT>

Derives fitness score from cost function return value

        Args:
            cost_fn_val (float): value returned by cost function

        Returns:
            float: standardized fitness score


def __init__(self, size, cost_fn, cost_fn_args=None, num_processes=<NUM_LIT:1>,<EOL>select_fn=minimize_best_n):

if size <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>self.__pop_size = size<EOL>if not callable(cost_fn):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>self.__cost_fn = cost_fn<EOL>if not callable(select_fn):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>self.__cost_fn_args = cost_fn_args<EOL>self.__select_fn = select_fn<EOL>self.__parameters = []<EOL>self.__members = []<EOL>self.__num_processes = num_processes<EOL>self.__best_fitness = None<EOL>self.__best_cost_fn_val = None<EOL>self.__best_parameters = None<EOL>

Population object: tunes specified parameters by measuring the
        performance of population members

        Args:
            size (int): number of Members in the population
            cost_fn (callable): function used to evaluate member fitness
            cost_fn_args (iterable or dict): additional user-specified
                arguments to pass to cost_fn
            num_processes (int): number of concurrent processes to run for
                Member generation/evaluation
            select_fn (callable): function used to sort members for parent
                based on member fitness score


return self.__pop_size<EOL>


if len(self.__members) != <NUM_LIT:0>:<EOL><INDENT>if self.__num_processes > <NUM_LIT:1>:<EOL><INDENT>members = [m.get() for m in self.__members]<EOL><DEDENT>else:<EOL><INDENT>members = self.__members<EOL><DEDENT>return sum(m.fitness_score for m in members) / len(members)<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>

Population fitness == average member fitness score


return self.__best_fitness<EOL>

Fitness score of best performing population member so far


if len(self.__members) != <NUM_LIT:0>:<EOL><INDENT>if self.__num_processes > <NUM_LIT:1>:<EOL><INDENT>members = [m.get() for m in self.__members]<EOL><DEDENT>else:<EOL><INDENT>members = self.__members<EOL><DEDENT>return sum(m.cost_fn_val for m in members) / len(members)<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>

Returns average cost function return value for all members


if len(self.__members) != <NUM_LIT:0>:<EOL><INDENT>if self.__num_processes > <NUM_LIT:1>:<EOL><INDENT>members = [m.get() for m in self.__members]<EOL><DEDENT>else:<EOL><INDENT>members = self.__members<EOL><DEDENT>return median([m.cost_fn_val for m in members])<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>

Returns median cost function return value for all members


if len(self.__members) != <NUM_LIT:0>:<EOL><INDENT>if self.__num_processes > <NUM_LIT:1>:<EOL><INDENT>members = [m.get() for m in self.__members]<EOL><DEDENT>else:<EOL><INDENT>members = self.__members<EOL><DEDENT>params = {}<EOL>for p in self.__parameters:<EOL><INDENT>params[p.name] = sum(<EOL>m.parameters[p.name] for m in members<EOL>) / len(members)<EOL><DEDENT>return params<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>

Population parameter vals == average member parameter vals


return self.__best_parameters<EOL>

Parameter values of best performing population member so far


if self.__num_processes > <NUM_LIT:1>:<EOL><INDENT>return [m.get() for m in self.__members]<EOL><DEDENT>else:<EOL><INDENT>return self.__members<EOL><DEDENT>


def add_parameter(self, name, min_val, max_val):

self.__parameters.append(Parameter(name, min_val, max_val))<EOL>

Adds a paramber to the Population

        Args:
            name (str): name of the parameter
            min_val (int or float): minimum value for the parameter
            max_val (int or float): maximum value for the parameter


if self.__num_processes > <NUM_LIT:1>:<EOL><INDENT>process_pool = Pool(processes=self.__num_processes)<EOL><DEDENT>self.__members = []<EOL>for _ in range(self.__pop_size):<EOL><INDENT>feed_dict = {}<EOL>for param in self.__parameters:<EOL><INDENT>feed_dict[param.name] = self.__random_param_val(<EOL>param.min_val,<EOL>param.max_val,<EOL>param.dtype<EOL>)<EOL><DEDENT>if self.__num_processes > <NUM_LIT:1>:<EOL><INDENT>self.__members.append(process_pool.apply_async(<EOL>self._start_process,<EOL>[self.__cost_fn, feed_dict, self.__cost_fn_args])<EOL>)<EOL><DEDENT>else:<EOL><INDENT>self.__members.append(<EOL>Member(<EOL>feed_dict,<EOL>self.__cost_fn(feed_dict, self.__cost_fn_args)<EOL>)<EOL>)<EOL><DEDENT><DEDENT>if self.__num_processes > <NUM_LIT:1>:<EOL><INDENT>process_pool.close()<EOL>process_pool.join()<EOL><DEDENT>self.__determine_best_member()<EOL>

Generates self.__pop_size Members with randomly initialized values
        for each parameter added with add_parameter(), evaluates their fitness


def next_generation(self, mut_rate=<NUM_LIT:0>, max_mut_amt=<NUM_LIT:0>, log_base=<NUM_LIT:10>):

if self.__num_processes > <NUM_LIT:1>:<EOL><INDENT>process_pool = Pool(processes=self.__num_processes)<EOL>members = [m.get() for m in self.__members]<EOL><DEDENT>else:<EOL><INDENT>members = self.__members<EOL><DEDENT>if len(members) == <NUM_LIT:0>:<EOL><INDENT>raise Exception(<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>selected_members = self.__select_fn(members)<EOL>reproduction_probs = list(reversed(logspace(<NUM_LIT:0.0>, <NUM_LIT:1.0>,<EOL>num=len(selected_members), base=log_base)))<EOL>reproduction_probs = reproduction_probs / sum(reproduction_probs)<EOL>self.__members = []<EOL>for _ in range(self.__pop_size):<EOL><INDENT>parent_1 = nrandom.choice(selected_members, p=reproduction_probs)<EOL>parent_2 = nrandom.choice(selected_members, p=reproduction_probs)<EOL>feed_dict = {}<EOL>for param in self.__parameters:<EOL><INDENT>which_parent = uniform(<NUM_LIT:0>, <NUM_LIT:1>)<EOL>if which_parent < <NUM_LIT:0.5>:<EOL><INDENT>feed_dict[param.name] = parent_1.parameters[param.name]<EOL><DEDENT>else:<EOL><INDENT>feed_dict[param.name] = parent_2.parameters[param.name]<EOL><DEDENT>feed_dict[param.name] = self.__mutate_parameter(<EOL>feed_dict[param.name], param, mut_rate, max_mut_amt<EOL>)<EOL><DEDENT>if self.__num_processes > <NUM_LIT:1>:<EOL><INDENT>self.__members.append(process_pool.apply_async(<EOL>self._start_process,<EOL>[self.__cost_fn, feed_dict, self.__cost_fn_args])<EOL>)<EOL><DEDENT>else:<EOL><INDENT>self.__members.append(<EOL>Member(<EOL>feed_dict,<EOL>self.__cost_fn(feed_dict, self.__cost_fn_args)<EOL>)<EOL>)<EOL><DEDENT><DEDENT>if self.__num_processes > <NUM_LIT:1>:<EOL><INDENT>process_pool.close()<EOL>process_pool.join()<EOL><DEDENT>self.__determine_best_member()<EOL>

Generates the next population from a previously evaluated generation

        Args:
            mut_rate (float): mutation rate for new members (0.0 - 1.0)
            max_mut_amt (float): how much the member is allowed to mutate
                (0.0 - 1.0, proportion change of mutated parameter)
            log_base (int): the higher this number, the more likely the first
                Members (chosen with supplied selection function) are chosen
                as parents for the next generation


@staticmethod<EOL><INDENT>def _start_process(cost_fn, feed_dict, cost_fn_args):<DEDENT>

return Member(feed_dict, cost_fn(feed_dict, cost_fn_args))<EOL>

Static method: starts a process to generate (evaluate) a new Member

        Args:
            cost_fn (function): cost function supplied to the Population
            feed_dict (dictionary): dictionary of Parameter objects
            cost_fn_args (iterable or dict): user-supplied args for cost_fn

        Returns:
            Evaluated Member object


@staticmethod<EOL><INDENT>def __random_param_val(min_val, max_val, dtype):<DEDENT>

return SUPPORTED_DTYPES[dtype](min_val, max_val)<EOL>

Private, static method: returns a random value

Args:
    min_val (int or float): minimum value for random value
    max_Val (int or float): maximum value for random vlaue
    dtype (type): type of random value, int or float

Returns:
    int or float: random value


@staticmethod<EOL><INDENT>def __mutate_parameter(value, param, mut_rate, max_mut_amt):<DEDENT>

if uniform(<NUM_LIT:0>, <NUM_LIT:1>) < mut_rate:<EOL><INDENT>mut_amt = uniform(<NUM_LIT:0>, max_mut_amt)<EOL>op = choice((add, sub))<EOL>new_val = op(value, param.dtype(<EOL>(param.max_val - param.min_val) * mut_amt<EOL>))<EOL>if new_val > param.max_val:<EOL><INDENT>return param.max_val<EOL><DEDENT>elif new_val < param.min_val:<EOL><INDENT>return param.min_val<EOL><DEDENT>else:<EOL><INDENT>return new_val<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return value<EOL><DEDENT>

Private, static method: mutates parameter

        Args:
            value (int or float): current value for Member's parameter
            param (Parameter): parameter object
            mut_rate (float): mutation rate of the value
            max_mut_amt (float): maximum mutation amount of the value

        Returns:
            int or float: mutated value


if self.__num_processes > <NUM_LIT:1>:<EOL><INDENT>members = [m.get() for m in self.__members]<EOL><DEDENT>else:<EOL><INDENT>members = self.__members<EOL><DEDENT>if self.__best_fitness is None:<EOL><INDENT>self.__best_fitness = members[<NUM_LIT:0>].fitness_score<EOL>self.__best_cost_fn_val = members[<NUM_LIT:0>].cost_fn_val<EOL>self.__best_parameters = {}<EOL>for p in self.__parameters:<EOL><INDENT>self.__best_parameters[p.name] = members[<NUM_LIT:0>].parameters[p.name]<EOL><DEDENT><DEDENT>for m_id, member in enumerate(members):<EOL><INDENT>if member.fitness_score > self.__best_fitness:<EOL><INDENT>self.__best_fitness = member.fitness_score<EOL>self.__best_cost_fn_val = member.cost_fn_val<EOL>self.__best_parameters = {}<EOL>for p in self.__parameters:<EOL><INDENT>self.__best_parameters[p.name] = member.parameters[p.name]<EOL><DEDENT><DEDENT><DEDENT>

Private method: determines if any current population members have a
        fitness score better than the current best


return(list(reversed(sorted(<EOL>Members, key=lambda Member: Member.fitness_score<EOL>))))<EOL>

Orders population members from lowest fitness to highest fitness

Args:
    Members (list): list of PyGenetics Member objects

Returns:
    lsit: ordered lsit of Members, from highest fitness to lowest fitness


def monitored(total: int, name=None, message=None):

def decorator(f):<EOL><INDENT>nonlocal name<EOL>monitor_index = list(inspect.signature(f).parameters.keys()).index('<STR_LIT>')<EOL>if name is None:<EOL><INDENT>name=f.__name__<EOL><DEDENT>@wraps(f)<EOL>def wrapper(*args, **kargs):<EOL><INDENT>if len(args) > monitor_index:<EOL><INDENT>monitor = args[monitor_index]<EOL><DEDENT>elif '<STR_LIT>' in kargs:<EOL><INDENT>monitor = kargs['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>monitor = kargs['<STR_LIT>'] = NullMonitor()<EOL><DEDENT>with monitor.task(total, name, message):<EOL><INDENT>f(*args, **kargs)<EOL><DEDENT><DEDENT>return wrapper<EOL><DEDENT>return decorator<EOL>

Decorate a function to automatically begin and end a task on the progressmonitor.
The function must have a parameter called 'monitor'


def begin(self, total: int, name=None, message=None):

self.total = total<EOL>message = message or name or "<STR_LIT>"<EOL>self.name = name or "<STR_LIT>"<EOL>self.update(<NUM_LIT:0>, message)<EOL>

Call before starting work on a monitor, specifying name and amount of work


@contextmanager<EOL><INDENT>def task(self, total: int, name=None, message=None):<DEDENT>

self.begin(total, name, message)<EOL>try:<EOL><INDENT>yield self<EOL><DEDENT>finally:<EOL><INDENT>self.done()<EOL><DEDENT>

Wrap code into a begin and end call on this monitor


@contextmanager<EOL><INDENT>def subtask(self, units: int):<DEDENT>

sm = self.submonitor(units)<EOL>try:<EOL><INDENT>yield sm<EOL><DEDENT>finally:<EOL><INDENT>if sm.total is None:<EOL><INDENT>self.update(units)<EOL><DEDENT>else:<EOL><INDENT>sm.done()<EOL><DEDENT><DEDENT>

Create a submonitor with the given units


if self.total is None:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>my_progress = self.worked<EOL>my_progress += sum(s.progress * weight<EOL>for (s, weight) in self.sub_monitors.items())<EOL>return min(<NUM_LIT:1>, my_progress / self.total)<EOL>

What percentage (range 0-1) of work is done (including submonitors)


def update(self, units: int=<NUM_LIT:1>, message: str=None):

if self.total is None:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>self.worked = min(self.total, self.worked+units)<EOL>if message:<EOL><INDENT>self.message = message<EOL><DEDENT>for listener in self.listeners:<EOL><INDENT>listener(self)<EOL><DEDENT>

Increment the monitor with N units worked and an optional message


self.listeners.add(func)<EOL>


self.listeners.remove(func)<EOL>


def submonitor(self, units: int, *args, **kargs) -> '<STR_LIT>':

submonitor = ProgressMonitor(*args, **kargs)<EOL>self.sub_monitors[submonitor] = units<EOL>submonitor.add_listener(self._submonitor_update)<EOL>return submonitor<EOL>

Create a sub monitor that stands for N units of work in this monitor
The sub task should call .begin (or use @monitored / with .task) before calling updates


if message is None:<EOL><INDENT>message = "<STR_LIT>".format(**locals()) if self.name else "<STR_LIT>"<EOL><DEDENT>self.update(units=self.total - self.worked, message=message)<EOL>

Signal that this task is done.
This is completely optional and will just call .update with the remaining work.


if log is None:<EOL><INDENT>log = logging.getLogger("<STR_LIT>")<EOL><DEDENT>def listen(monitor):<EOL><INDENT>name = "<STR_LIT>".format(monitor.name) if monitor.name is not None else "<STR_LIT>"<EOL>perc = int(monitor.progress * <NUM_LIT:100>)<EOL>msg = "<STR_LIT>".format(**locals())<EOL>log.log(level, msg)<EOL><DEDENT>return listen<EOL>

Progress Monitor listener that logs all updates to the given logger


regex = "<STR_LIT>".format(meta_name)<EOL>return re.search(regex, package_file).group(<NUM_LIT:1>)<EOL>

Return value of variable set in the package where said variable is
    named in the Python meta format `__<meta_name>__`.


transformations = parse(instruction)<EOL>return reduce(lambda dt, transformation: transformation.apply_to(dt), transformations, dttm)<EOL>

Args:
    instruction (string): a string that encodes 0 to n transformations of a time, i.e. "-1h@h", "@mon+2d+4h", ...
    dttm (datetime):
Returns:
    datetime: The datetime resulting from applying all transformations to the input datetime.

Example:
    >>> snap(datetime(2016, 1, 1, 15, 30), "-1h@h")
    datetime(2016, 1, 1, 14)


def snap_tz(dttm, instruction, timezone):

transformations = parse(instruction)<EOL>return reduce(lambda dt, transformation: transformation.apply_to_with_tz(dt, timezone), transformations, dttm)<EOL>

This function handles timezone aware datetimes.
    Sometimes it is necessary to keep daylight saving time switches in mind.

    Args:
        instruction (string): a string that encodes 0 to n transformations of a time, i.e. "-1h@h", "@mon+2d+4h", ...
        dttm (datetime): a datetime with timezone
        timezone: a pytz timezone
    Returns:
        datetime: The datetime resulting from applying all transformations to the input datetime.

    Example:
        >>> import pytz
        >>> CET = pytz.timezone("Europe/Berlin")
        >>> dttm = CET.localize(datetime(2017, 3, 26, 3, 44)
        >>> dttm
        datetime.datetime(2017, 3, 26, 3, 44, tzinfo=<DstTzInfo 'Europe/Berlin' CEST+2:00:00 DST>)

        >>> snap_tz(dttm, "-2h@h", CET)
        datetime.datetime(2017, 3, 26, 0, 0, tzinfo=<DstTzInfo 'Europe/Berlin' CET+1:00:00 STD>)
        >>> # switch from winter to summer time!


def apply_to_with_tz(self, dttm, timezone):

result = self.apply_to(dttm)<EOL>if self.unit in [DAYS, WEEKS, MONTHS, YEARS]:<EOL><INDENT>naive_dttm = datetime(result.year, result.month, result.day)<EOL>result = timezone.localize(naive_dttm)<EOL><DEDENT>return result<EOL>

We make sure that after truncating we use the correct timezone,
        even if we 'jump' over a daylight saving time switch.

        I.e. if we apply "@d" to `Sun Oct 30 04:30:00 CET 2016` (1477798200)
        we want to have `Sun Oct 30 00:00:00 CEST 2016` (1477778400)
        but not `Sun Oct 30 00:00:00 CET 2016` (1477782000)


def get_token(s, pos, brackets_are_chars=True, environments=True, **parse_flags):

return LatexWalker(s, **parse_flags).get_token(pos=pos,<EOL>brackets_are_chars=brackets_are_chars,<EOL>environments=environments)<EOL>

Parse the next token in the stream.

Returns a `LatexToken`. Raises `LatexWalkerEndOfStream` if end of stream reached.

.. deprecated:: 1.0
   Please use :py:meth:`LatexWalker.get_token()` instead.


def get_latex_expression(s, pos, **parse_flags):

return LatexWalker(s, **parse_flags).get_latex_expression(pos=pos)<EOL>

Reads a latex expression, e.g. macro argument. This may be a single char, an escape
sequence, or a expression placed in braces.

Returns a tuple `(<LatexNode instance>, pos, len)`. `pos` is the first char of the
expression, and `len` is its length.

.. deprecated:: 1.0
   Please use :py:meth:`LatexWalker.get_latex_expression()` instead.


def get_latex_maybe_optional_arg(s, pos, **parse_flags):

return LatexWalker(s, **parse_flags).get_latex_maybe_optional_arg(pos=pos)<EOL>

Attempts to parse an optional argument. Returns a tuple `(groupnode, pos, len)` if
success, otherwise returns None.

.. deprecated:: 1.0
   Please use :py:meth:`LatexWalker.get_latex_maybe_optional_arg()` instead.


def get_latex_braced_group(s, pos, brace_type='<STR_LIT:{>', **parse_flags):

return LatexWalker(s, **parse_flags).get_latex_braced_group(pos=pos, brace_type=brace_type)<EOL>

Reads a latex expression enclosed in braces {...}. The first token of `s[pos:]` must
be an opening brace.

Returns a tuple `(node, pos, len)`. `pos` is the first char of the
expression (which has to be an opening brace), and `len` is its length,
including the closing brace.

.. deprecated:: 1.0
   Please use :py:meth:`LatexWalker.get_latex_braced_group()` instead.


def get_latex_environment(s, pos, environmentname=None, **parse_flags):

return LatexWalker(s, **parse_flags).get_latex_environment(pos=pos, environmentname=environmentname)<EOL>

Reads a latex expression enclosed in a \\begin{environment}...\\end{environment}. The first
token in the stream must be the \\begin{environment}.

Returns a tuple (node, pos, len) with node being a :py:class:`LatexEnvironmentNode`.

.. deprecated:: 1.0
   Please use :py:meth:`LatexWalker.get_latex_environment()` instead.


def get_latex_nodes(s, pos=<NUM_LIT:0>, stop_upon_closing_brace=None, stop_upon_end_environment=None,<EOL>stop_upon_closing_mathmode=None, **parse_flags):

Parses latex content `s`.

Returns a tuple `(nodelist, pos, len)` where nodelist is a list of `LatexNode` 's.

If `stop_upon_closing_brace` is given, then `len` includes the closing brace, but the
closing brace is not included in any of the nodes in the `nodelist`.

.. deprecated:: 1.0
   Please use :py:meth:`LatexWalker.get_latex_nodes()` instead.


super(LatexNode, self).__init__(**kwargs)<EOL>

Important: subclasses must set `self._fields` to a list (or tuple or iterable) of the
fields stored in this object.


return LatexNode<EOL>

Returns the class which corresponds to the type of this node.  This is a Python class
object, that is one of :py:class:`~pylatexenc.latexwalker.LatexCharsNode`,
:py:class:`~pylatexenc.latexwalker.LatexGroupNode`, etc.


return isinstance(self, t)<EOL>

Returns `True` if the current node is of the given type.  The argument `t` must be a
Python class such as, e.g. :py:class:`~pylatexenc.latexwalker.LatexGroupNode`.


super(LatexCharsNode, self).__init__(**kwargs)<EOL>self._fields = ('<STR_LIT>',)<EOL>self.chars = chars<EOL>

r"""
        Constructor arguments:

            - `chars`: the actual characters.


return {<EOL>'<STR_LIT>': self.keep_inline_math,<EOL>'<STR_LIT>': self.tolerant_parsing,<EOL>'<STR_LIT>': self.strict_braces,<EOL>}<EOL>

The parse flags currently set on this object.  Returns a dictionary with keys
'keep_inline_math', 'tolerant_parsing' and 'strict_braces'.


def get_token(self, pos, brackets_are_chars=True, environments=True, keep_inline_math=None):

s = self.s <EOL>with _PushPropOverride(self, '<STR_LIT>', keep_inline_math):<EOL><INDENT>space = '<STR_LIT>'<EOL>while (pos < len(s) and s[pos].isspace()):<EOL><INDENT>space += s[pos]<EOL>pos += <NUM_LIT:1><EOL>if (space.endswith('<STR_LIT>')):  <EOL><INDENT>return LatexToken(tok='<STR_LIT>', arg='<STR_LIT>', pos=pos-<NUM_LIT:2>, len=<NUM_LIT:2>, pre_space='<STR_LIT>')<EOL><DEDENT><DEDENT>if (pos >= len(s)):<EOL><INDENT>raise LatexWalkerEndOfStream()<EOL><DEDENT>if (s[pos] == '<STR_LIT:\\>'):<EOL><INDENT>i = <NUM_LIT:2><EOL>macro = s[pos+<NUM_LIT:1>] <EOL>isalphamacro = False<EOL>if (s[pos+<NUM_LIT:1>].isalpha()):<EOL><INDENT>isalphamacro = True<EOL>while pos+i<len(s) and s[pos+i].isalpha():<EOL><INDENT>macro += s[pos+i]<EOL>i += <NUM_LIT:1><EOL><DEDENT><DEDENT>if (pos+i<len(s) and s[pos+i] == '<STR_LIT:*>'):<EOL><INDENT>macro += '<STR_LIT:*>'<EOL>i += <NUM_LIT:1><EOL><DEDENT>if (environments and (macro == '<STR_LIT>' or macro == '<STR_LIT:end>')):<EOL><INDENT>envmatch = re.match(r'<STR_LIT>', s[pos+i:])<EOL>if (envmatch is None):<EOL><INDENT>raise LatexWalkerParseError(<EOL>s=s,<EOL>pos=pos,<EOL>msg="<STR_LIT>" %(macro)<EOL>)<EOL><DEDENT>return LatexToken(<EOL>tok=('<STR_LIT>' if macro == '<STR_LIT>' else  '<STR_LIT>'),<EOL>arg=envmatch.group(<NUM_LIT:1>),<EOL>pos=pos,<EOL>len=i+envmatch.end(), <EOL>pre_space=space<EOL>)<EOL><DEDENT>post_space = '<STR_LIT>'<EOL>if isalphamacro:<EOL><INDENT>while pos+i<len(s) and s[pos+i].isspace():<EOL><INDENT>post_space += s[pos+i]<EOL>i += <NUM_LIT:1><EOL><DEDENT><DEDENT>return LatexToken(tok='<STR_LIT>', arg=macro, pos=pos, len=i,<EOL>pre_space=space, post_space=post_space)<EOL><DEDENT>if (s[pos] == '<STR_LIT:%>'):<EOL><INDENT>m = re.search(r'<STR_LIT>', s[pos:])<EOL>mlen = None<EOL>if m is not None:<EOL><INDENT>arglen = m.start() <EOL>mlen = m.end() <EOL>mspace = m.group()<EOL><DEDENT>else:<EOL><INDENT>arglen = len(s)-pos<EOL>mlen = arglen<EOL>mspace = '<STR_LIT>'<EOL><DEDENT>return LatexToken(tok='<STR_LIT>', arg=s[pos+<NUM_LIT:1>:pos+arglen], pos=pos, len=mlen,<EOL>pre_space=space, post_space=mspace)<EOL><DEDENT>openbracechars = '<STR_LIT:{>'<EOL>closebracechars = '<STR_LIT:}>'<EOL>if not brackets_are_chars:<EOL><INDENT>openbracechars += '<STR_LIT:[>'<EOL>closebracechars += '<STR_LIT:]>'<EOL><DEDENT>if s[pos] in openbracechars:<EOL><INDENT>return LatexToken(tok='<STR_LIT>', arg=s[pos], pos=pos, len=<NUM_LIT:1>, pre_space=space)<EOL><DEDENT>if s[pos] in closebracechars:<EOL><INDENT>return LatexToken(tok='<STR_LIT>', arg=s[pos], pos=pos, len=<NUM_LIT:1>, pre_space=space)<EOL><DEDENT>if (s[pos] == '<STR_LIT:$>' and self.keep_inline_math):<EOL><INDENT>if not (pos+<NUM_LIT:1> < len(s) and s[pos+<NUM_LIT:1>] == '<STR_LIT:$>'):<EOL><INDENT>return LatexToken(tok='<STR_LIT>', arg=s[pos], pos=pos, len=<NUM_LIT:1>, pre_space=space)<EOL><DEDENT><DEDENT>return LatexToken(tok='<STR_LIT>', arg=s[pos], pos=pos, len=<NUM_LIT:1>, pre_space=space)<EOL><DEDENT>

Parses the latex content given to the constructor (and stored in `self.s`),
starting at position `pos`, to parse a single "token", as defined by
:py:class:`LatexToken`.

Parse the token in the stream pointed to at position `pos`.

Returns a :py:class:`LatexToken`. Raises :py:exc:`LatexWalkerEndOfStream` if end
of stream reached.

If `brackets_are_chars=False`, then square bracket characters count as
'brace_open' and 'brace_close' token types (see :py:class:`LatexToken`); otherwise
(the default) they are considered just like other normal characters.

If `environments=False`, then '\\begin' and '\\end' tokens count as regular
'macro' tokens (see :py:class:`LatexToken`); otherwise (the default) they are
considered as the token types 'begin_environment' and 'end_environment'.

If `keep_inline_math` is not `None`, then that value overrides that of
`self.keep_inline_math` for the duration of this method call.


def get_latex_expression(self, pos, strict_braces=None):

with _PushPropOverride(self, '<STR_LIT>', strict_braces):<EOL><INDENT>tok = self.get_token(pos, environments=False, keep_inline_math=False)<EOL>if (tok.tok == '<STR_LIT>'):<EOL><INDENT>if (tok.arg == '<STR_LIT:end>'):<EOL><INDENT>if not self.tolerant_parsing:<EOL><INDENT>raise LatexWalkerParseError("<STR_LIT>", self.s, pos)<EOL><DEDENT>else:<EOL><INDENT>return (LatexCharsNode(chars='<STR_LIT>'), tok.pos, <NUM_LIT:0>)<EOL><DEDENT><DEDENT>return (LatexMacroNode(macroname=tok.arg, nodeoptarg=None, nodeargs=[],<EOL>macro_post_space=tok.post_space),<EOL>tok.pos, tok.len)<EOL><DEDENT>if (tok.tok == '<STR_LIT>'):<EOL><INDENT>return self.get_latex_expression(pos+tok.len)<EOL><DEDENT>if (tok.tok == '<STR_LIT>'):<EOL><INDENT>return self.get_latex_braced_group(tok.pos)<EOL><DEDENT>if (tok.tok == '<STR_LIT>'):<EOL><INDENT>if (self.strict_braces and not self.tolerant_parsing):<EOL><INDENT>raise LatexWalkerParseError("<STR_LIT>", self.s, pos)<EOL><DEDENT>return (LatexCharsNode(chars='<STR_LIT>'), tok.pos, <NUM_LIT:0>)<EOL><DEDENT>if (tok.tok == '<STR_LIT>'):<EOL><INDENT>return (LatexCharsNode(chars=tok.arg), tok.pos, tok.len)<EOL><DEDENT>raise LatexWalkerParseError("<STR_LIT>" %(tok.tok), self.s, pos)<EOL><DEDENT>

Parses the latex content given to the constructor (and stored in `self.s`),
starting at position `pos`, to parse a single LaTeX expression.

Reads a latex expression, e.g. macro argument. This may be a single char, an escape
sequence, or a expression placed in braces.  This is what TeX calls a "token" (and
not what we call a token... anyway).

Returns a tuple `(node, pos, len)`, where `pos` is the position of the
first char of the expression and `len` the length of the expression.


tok = self.get_token(pos, brackets_are_chars=False, environments=False)<EOL>if (tok.tok == '<STR_LIT>' and tok.arg == '<STR_LIT:[>'):<EOL><INDENT>return self.get_latex_braced_group(pos, brace_type='<STR_LIT:[>')<EOL><DEDENT>return None<EOL>

Parses the latex content given to the constructor (and stored in `self.s`),
starting at position `pos`, to attempt to parse an optional argument.

Attempts to parse an optional argument. If this is successful, we return
a tuple `(node, pos, len)` if success where `node` is a
:py:class:`LatexGroupNode`.  Otherwise, this method returns None.


def get_latex_braced_group(self, pos, brace_type='<STR_LIT:{>'):

closing_brace = None<EOL>if (brace_type == '<STR_LIT:{>'):<EOL><INDENT>closing_brace = '<STR_LIT:}>'<EOL><DEDENT>elif (brace_type == '<STR_LIT:[>'):<EOL><INDENT>closing_brace = '<STR_LIT:]>'<EOL><DEDENT>else:<EOL><INDENT>raise LatexWalkerParseError(s=self.s, pos=pos, msg="<STR_LIT>" %(brace_type))<EOL><DEDENT>brackets_are_chars = (brace_type != '<STR_LIT:[>')<EOL>firsttok = self.get_token(pos, brackets_are_chars=brackets_are_chars)<EOL>if (firsttok.tok != '<STR_LIT>'  or  firsttok.arg != brace_type):<EOL><INDENT>raise LatexWalkerParseError(<EOL>s=self.s,<EOL>pos=pos,<EOL>msg='<STR_LIT>' %(self.s[pos])<EOL>)<EOL><DEDENT>(nodelist, npos, nlen) = self.get_latex_nodes(firsttok.pos + firsttok.len,<EOL>stop_upon_closing_brace=closing_brace)<EOL>return (LatexGroupNode(nodelist=nodelist), firsttok.pos, npos + nlen - firsttok.pos)<EOL>

Parses the latex content given to the constructor (and stored in `self.s`),
starting at position `pos`, to read a latex group delimited by braces.

Reads a latex expression enclosed in braces ``{ ... }``. The first token of
`s[pos:]` must be an opening brace.

Returns a tuple `(node, pos, len)`, where `node` is a
:py:class:`LatexGroupNode` instance, `pos` is the position of the first
char of the expression (which has to be an opening brace), and `len` is
the length of the group, including the closing brace (relative to the
starting position).


def get_latex_environment(self, pos, environmentname=None):

startpos = pos<EOL>firsttok = self.get_token(pos)<EOL>if (firsttok.tok != '<STR_LIT>'  or<EOL>(environmentname is not None and firsttok.arg != environmentname)):<EOL><INDENT>raise LatexWalkerParseError(s=self.s, pos=pos,<EOL>msg=r'<STR_LIT>' %(<EOL>environmentname if environmentname is not None else '<STR_LIT>',<EOL>firsttok.arg<EOL>))<EOL><DEDENT>if (environmentname is None):<EOL><INDENT>environmentname = firsttok.arg<EOL><DEDENT>pos = firsttok.pos + firsttok.len<EOL>optargs = []<EOL>args = []<EOL>optargtuple = None<EOL>if (self.s[pos] == '<STR_LIT:[>'):<EOL><INDENT>optargtuple = self.get_latex_maybe_optional_arg(pos)<EOL><DEDENT>if (optargtuple is not None):<EOL><INDENT>optargs.append(optargtuple[<NUM_LIT:0>])<EOL>pos = optargtuple[<NUM_LIT:1>]+optargtuple[<NUM_LIT:2>]<EOL><DEDENT>else:<EOL><INDENT>if self.s[pos] == '<STR_LIT:{>':<EOL><INDENT>(argnode, apos, alen) = self.get_latex_braced_group(pos)<EOL>args.append(argnode)<EOL>pos = apos+alen<EOL><DEDENT><DEDENT>(nodelist, npos, nlen) = self.get_latex_nodes(pos, stop_upon_end_environment=environmentname)<EOL>return (LatexEnvironmentNode(envname=environmentname,<EOL>nodelist=nodelist,<EOL>optargs=optargs,<EOL>args=args),<EOL>startpos, npos+nlen-startpos)<EOL>

r"""
        Parses the latex content given to the constructor (and stored in `self.s`),
        starting at position `pos`, to read a latex environment.

        Reads a latex expression enclosed in a
        ``\begin{environment}...\end{environment}``.  The first token in the
        stream must be the ``\begin{environment}``.

        If `environmentname` is given and nonempty, then additionally a
        :py:exc:`LatexWalkerParseError` is raised if the environment in the
        input stream does not match the provided name.

        This function will attempt to heuristically parse an optional argument,
        and possibly a mandatory argument given to the environment.  No space is
        allowed between ``\begin{environment}`` and an opening square bracket or
        opening brace.

        Returns a tuple (node, pos, len) with node being a
        :py:class:`LatexEnvironmentNode`.


def get_latex_nodes(self, pos=<NUM_LIT:0>, stop_upon_closing_brace=None, stop_upon_end_environment=None,<EOL>stop_upon_closing_mathmode=None):

nodelist = []<EOL>brackets_are_chars = True<EOL>if (stop_upon_closing_brace == '<STR_LIT:]>'):<EOL><INDENT>brackets_are_chars = False<EOL><DEDENT>origpos = pos<EOL>class PosPointer:<EOL><INDENT>def __init__(self, pos=<NUM_LIT:0>, lastchars='<STR_LIT>'):<EOL><INDENT>self.pos = pos<EOL>self.lastchars = lastchars<EOL><DEDENT><DEDENT>p = PosPointer(pos)<EOL>def do_read(nodelist, p):<EOL><INDENT>"""<STR_LIT>"""<EOL>try:<EOL><INDENT>tok = self.get_token(p.pos, brackets_are_chars=brackets_are_chars)<EOL><DEDENT>except LatexWalkerEndOfStream:<EOL><INDENT>if self.tolerant_parsing:<EOL><INDENT>return True<EOL><DEDENT>raise <EOL><DEDENT>p.pos = tok.pos + tok.len<EOL>if (tok.tok == '<STR_LIT>'):<EOL><INDENT>p.lastchars += tok.pre_space + tok.arg<EOL>return False<EOL><DEDENT>if len(p.lastchars):<EOL><INDENT>strnode = LatexCharsNode(chars=p.lastchars+tok.pre_space)<EOL>nodelist.append(strnode)<EOL>p.lastchars = '<STR_LIT>'<EOL><DEDENT>elif len(tok.pre_space):<EOL><INDENT>spacestrnode = LatexCharsNode(chars=tok.pre_space)<EOL>nodelist.append(spacestrnode)<EOL><DEDENT>if (tok.tok == '<STR_LIT>'):<EOL><INDENT>if (tok.arg != stop_upon_closing_brace):<EOL><INDENT>if (not self.tolerant_parsing):<EOL><INDENT>raise LatexWalkerParseError(<EOL>s=self.s,<EOL>pos=tok.pos,<EOL>msg='<STR_LIT>' %(tok.arg)<EOL>)<EOL><DEDENT>return False<EOL><DEDENT>return True<EOL><DEDENT>if (tok.tok == '<STR_LIT>'):<EOL><INDENT>if (tok.arg != stop_upon_end_environment):<EOL><INDENT>if (not self.tolerant_parsing):<EOL><INDENT>raise LatexWalkerParseError(<EOL>s=self.s,<EOL>pos=tok.pos,<EOL>msg=('<STR_LIT>'<EOL>'<STR_LIT>' %(tok.arg, stop_upon_end_environment))<EOL>)<EOL><DEDENT>return False<EOL><DEDENT>return True<EOL><DEDENT>if (tok.tok == '<STR_LIT>'):<EOL><INDENT>if stop_upon_closing_mathmode is not None:<EOL><INDENT>if stop_upon_closing_mathmode != '<STR_LIT:$>':<EOL><INDENT>raise LatexWalkerParseError(<EOL>s=self.s,<EOL>pos=tok.pos,<EOL>msg='<STR_LIT>'<EOL>)<EOL><DEDENT>return True<EOL><DEDENT>(mathinline_nodelist, mpos, mlen) = self.get_latex_nodes(p.pos, stop_upon_closing_mathmode='<STR_LIT:$>')<EOL>p.pos = mpos + mlen<EOL>nodelist.append(LatexMathNode(displaytype='<STR_LIT>', nodelist=mathinline_nodelist))<EOL>return<EOL><DEDENT>if (tok.tok == '<STR_LIT>'):<EOL><INDENT>commentnode = LatexCommentNode(comment=tok.arg, comment_post_space=tok.post_space)<EOL>nodelist.append(commentnode)<EOL>return<EOL><DEDENT>if (tok.tok == '<STR_LIT>'):<EOL><INDENT>(groupnode, bpos, blen) = self.get_latex_braced_group(tok.pos)<EOL>p.pos = bpos + blen<EOL>nodelist.append(groupnode)<EOL>return<EOL><DEDENT>if (tok.tok == '<STR_LIT>'):<EOL><INDENT>(envnode, epos, elen) = self.get_latex_environment(tok.pos, environmentname=tok.arg)<EOL>p.pos = epos + elen<EOL>nodelist.append(envnode)<EOL>return<EOL><DEDENT>if (tok.tok == '<STR_LIT>'):<EOL><INDENT>nodeoptarg = None<EOL>nodeargs = []<EOL>macname = tok.arg.rstrip('<STR_LIT:*>') <EOL>if macname in self.macro_dict:<EOL><INDENT>mac = self.macro_dict[macname]<EOL>def getoptarg(pos):<EOL><INDENT>"""<STR_LIT>"""<EOL>optarginfotuple = self.get_latex_maybe_optional_arg(pos)<EOL>if optarginfotuple is not None:<EOL><INDENT>(nodeoptarg, optargpos, optarglen) = optarginfotuple<EOL>return (nodeoptarg, optargpos+optarglen)<EOL><DEDENT>return (None, pos)<EOL><DEDENT>def getarg(pos):<EOL><INDENT>"""<STR_LIT>"""<EOL>(nodearg, npos, nlen) = self.get_latex_expression(pos, strict_braces=False)<EOL>return (nodearg, npos + nlen)<EOL><DEDENT>if mac.optarg:<EOL><INDENT>(nodeoptarg, p.pos) = getoptarg(p.pos)<EOL><DEDENT>if isinstance(mac.numargs, _basestring):<EOL><INDENT>for arg in mac.numargs:<EOL><INDENT>if arg == '<STR_LIT:{>':<EOL><INDENT>(node, p.pos) = getarg(p.pos)<EOL>nodeargs.append(node)<EOL><DEDENT>elif arg == '<STR_LIT:[>':<EOL><INDENT>(node, p.pos) = getoptarg(p.pos)<EOL>nodeargs.append(node)<EOL><DEDENT>else:<EOL><INDENT>raise LatexWalkerError(<EOL>"<STR_LIT>"<EOL>% (mac.macroname, arg)<EOL>)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>for n in range(mac.numargs):<EOL><INDENT>(nodearg, p.pos) = getarg(p.pos)<EOL>nodeargs.append(nodearg)<EOL><DEDENT><DEDENT><DEDENT>nodelist.append(LatexMacroNode(macroname=tok.arg,<EOL>nodeoptarg=nodeoptarg,<EOL>nodeargs=nodeargs,<EOL>macro_post_space=tok.post_space))<EOL>return None<EOL><DEDENT>raise LatexWalkerParseError(s=self.s, pos=p.pos, msg="<STR_LIT>" %(tok))<EOL><DEDENT>while True:<EOL><INDENT>try:<EOL><INDENT>r_endnow = do_read(nodelist, p)<EOL><DEDENT>except LatexWalkerEndOfStream:<EOL><INDENT>if stop_upon_closing_brace or stop_upon_end_environment:<EOL><INDENT>if (not self.tolerant_parsing):<EOL><INDENT>raise LatexWalkerError("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>r_endnow = False<EOL><DEDENT><DEDENT>else:<EOL><INDENT>r_endnow = True<EOL><DEDENT><DEDENT>if (r_endnow):<EOL><INDENT>if (p.lastchars):<EOL><INDENT>strnode = LatexCharsNode(chars=p.lastchars)<EOL>nodelist.append(strnode)<EOL><DEDENT>return (nodelist, origpos, p.pos - origpos)<EOL><DEDENT><DEDENT>raise LatexWalkerError(                <EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL>

Parses the latex content given to the constructor (and stored in `self.s`)
into a list of nodes.

Returns a tuple `(nodelist, pos, len)` where nodelist is a list of
:py:class:`LatexNode`\ 's.

If `stop_upon_closing_brace` is given and set to a character, then parsing stops
once the given closing brace is encountered (but not inside a subgroup).  The
brace is given as a character, ']' or '}'.  The returned `len` includes the
closing brace, but the closing brace is not included in any of the nodes in the
`nodelist`.

If `stop_upon_end_environment` is provided, then parsing stops once the given
environment was closed.  If there is an environment mismatch, then a
`LatexWalkerParseError` is raised except in tolerant parsing mode (see
py:meth:`parse_flags()`).  Again, the closing environment is included in the
length count but not the nodes.

If `stop_upon_closing_mathmode` is specified, then the parsing stops
once the corresponding math mode (assumed already open) is closed.
Currently, only inline math modes delimited by ``$`` are supported.
I.e., currently, if set, only the value
``stop_upon_closing_mathmode='$'`` is valid.


def latex2text(content, tolerant_parsing=False, keep_inline_math=False, keep_comments=False):

(nodelist, tpos, tlen) = latexwalker.get_latex_nodes(content, keep_inline_math=keep_inline_math,<EOL>tolerant_parsing=tolerant_parsing)<EOL>return latexnodes2text(nodelist, keep_inline_math=keep_inline_math, keep_comments=keep_comments)<EOL>

Extracts text from `content` meant for database indexing. `content` is
some LaTeX code.

.. deprecated:: 1.0
   Please use :py:class:`LatexNodes2Text` instead.


def latexnodes2text(nodelist, keep_inline_math=False, keep_comments=False):

return LatexNodes2Text(<EOL>keep_inline_math=keep_inline_math,<EOL>keep_comments=keep_comments<EOL>).nodelist_to_text(nodelist)<EOL>

Extracts text from a node list. `nodelist` is a list of nodes as returned by
:py:func:`pylatexenc.latexwalker.get_latex_nodes()`.

.. deprecated:: 1.0
   Please use :py:class:`LatexNodes2Text` instead.


def set_tex_input_directory(self, tex_input_directory, latex_walker_init_args=None, strict_input=True):

self.tex_input_directory = tex_input_directory<EOL>self.latex_walker_init_args = latex_walker_init_args if latex_walker_init_args else {}<EOL>self.strict_input = strict_input<EOL>if tex_input_directory:<EOL><INDENT>self.macro_dict['<STR_LIT:input>'] = MacroDef('<STR_LIT:input>', lambda n: self._callback_input(n))<EOL>self.macro_dict['<STR_LIT>'] = MacroDef('<STR_LIT>', lambda n: self._callback_input(n))<EOL><DEDENT>else:<EOL><INDENT>self.macro_dict['<STR_LIT:input>'] = MacroDef('<STR_LIT:input>', discard=True)<EOL>self.macro_dict['<STR_LIT>'] = MacroDef('<STR_LIT>', discard=True)<EOL><DEDENT>

Set where to look for input files when encountering the ``\\input`` or
``\\include`` macro.

Alternatively, you may also override :py:meth:`read_input_file()` to
implement a custom file lookup mechanism.

The argument `tex_input_directory` is the directory relative to which to
search for input files.

If `strict_input` is set to `True`, then we always check that the
referenced file lies within the subtree of `tex_input_directory`,
prohibiting for instance hacks with '..' in filenames or using symbolic
links to refer to files out of the directory tree.

The argument `latex_walker_init_args` allows you to specify the parse
flags passed to the constructor of
:py:class:`pylatexenc.latexwalker.LatexWalker` when parsing the input
file.


fnfull = os.path.realpath(os.path.join(self.tex_input_directory, fn))<EOL>if self.strict_input:<EOL><INDENT>dirfull = os.path.realpath(self.tex_input_directory)<EOL>if not fnfull.startswith(dirfull):<EOL><INDENT>logger.warning(<EOL>"<STR_LIT>",<EOL>fn<EOL>)<EOL>return '<STR_LIT>'<EOL><DEDENT><DEDENT>if not os.path.exists(fnfull) and os.path.exists(fnfull + '<STR_LIT>'):<EOL><INDENT>fnfull = fnfull + '<STR_LIT>'<EOL><DEDENT>if not os.path.exists(fnfull) and os.path.exists(fnfull + '<STR_LIT>'):<EOL><INDENT>fnfull = fnfull + '<STR_LIT>'<EOL><DEDENT>if not os.path.isfile(fnfull):<EOL><INDENT>logger.warning(u"<STR_LIT>", fn)<EOL>return '<STR_LIT>'<EOL><DEDENT>logger.debug("<STR_LIT>", fnfull)<EOL>try:<EOL><INDENT>with open(fnfull) as f:<EOL><INDENT>return f.read()<EOL><DEDENT><DEDENT>except IOError as e:<EOL><INDENT>logger.warning(u"<STR_LIT>", fn, e)<EOL>return '<STR_LIT>'<EOL><DEDENT>

This method may be overridden to implement a custom lookup mechanism when
encountering ``\\input`` or ``\\include`` directives.

The default implementation looks for a file of the given name relative
to the directory set by :py:meth:`set_tex_input_directory()`.  If
`strict_input=True` was set, we ensure strictly that the file resides in
a subtree of the reference input directory (after canonicalizing the
paths and resolving all symlinks).

You may override this method to obtain the input data in however way you
see fit.  (In that case, a call to `set_tex_input_directory()` may not
be needed as that function simply sets properties which are used by the
default implementation of `read_input_file()`.)

This function accepts the referred filename as argument (the argument to
the ``\\input`` macro), and should return a string with the file
contents (or generate a warning or raise an error).


def latex_to_text(self, latex, **parse_flags):

return self.nodelist_to_text(latexwalker.LatexWalker(latex, **parse_flags).get_latex_nodes()[<NUM_LIT:0>])<EOL>

Parses the given `latex` code and returns its textual representation.

The `parse_flags` are the flags to give on to the
:py:class:`pylatexenc.latexwalker.LatexWalker` constructor.


s = self._nodelistcontents_to_text(nodelist)<EOL>for pattern, replacement in self.text_replacements:<EOL><INDENT>if (hasattr(pattern, '<STR_LIT>')):<EOL><INDENT>s = pattern.sub(replacement, s)<EOL><DEDENT>else:<EOL><INDENT>s = s.replace(pattern, replacement)<EOL><DEDENT><DEDENT>if not self.keep_inline_math:<EOL><INDENT>s = s.replace('<STR_LIT:$>', '<STR_LIT>'); <EOL><DEDENT>return s<EOL>

Extracts text from a node list. `nodelist` is a list of nodes as returned by
:py:meth:`pylatexenc.latexwalker.LatexWalker.get_latex_nodes()`.

In addition to converting each node in the list to text using
`node_to_text()`, we apply some global replacements and fine-tuning to
the resulting text to account for `text_replacements` (e.g., to fix
quotes, tab alignment ``&`` chars, etc.)


s = '<STR_LIT>'<EOL>prev_node = None<EOL>for node in nodelist:<EOL><INDENT>if self._is_bare_macro_node(prev_node) and node.isNodeType(latexwalker.LatexCharsNode):<EOL><INDENT>if not self.strict_latex_spaces['<STR_LIT>']:<EOL><INDENT>s += prev_node.macro_post_space<EOL><DEDENT><DEDENT>s += self.node_to_text(node)<EOL>prev_node = node<EOL><DEDENT>return s<EOL>

Turn the node list to text representations of each node.  Basically apply
`node_to_text()` to each node.  (But not quite actually, since we take
some care as to where we add whitespace.)


def node_to_text(self, node, prev_node_hint=None):

if node is None:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>if node.isNodeType(latexwalker.LatexCharsNode):<EOL><INDENT>if not self.strict_latex_spaces['<STR_LIT>'] and len(node.chars.strip()) == <NUM_LIT:0>:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>return node.chars<EOL><DEDENT>if node.isNodeType(latexwalker.LatexCommentNode):<EOL><INDENT>if self.keep_comments:<EOL><INDENT>if self.strict_latex_spaces['<STR_LIT>']:<EOL><INDENT>return '<STR_LIT:%>' + node.comment + '<STR_LIT:\n>'<EOL><DEDENT>else:<EOL><INDENT>return '<STR_LIT:%>' + node.comment + node.comment_post_space<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if self.strict_latex_spaces['<STR_LIT>']:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>return node.comment_post_space<EOL><DEDENT><DEDENT><DEDENT>if node.isNodeType(latexwalker.LatexGroupNode):<EOL><INDENT>contents = self._groupnodecontents_to_text(node)<EOL>if self.keep_braced_groups and len(contents) >= self.keep_braced_groups_minlen:<EOL><INDENT>return "<STR_LIT:{>" + contents + "<STR_LIT:}>"<EOL><DEDENT>return contents<EOL><DEDENT>def apply_simplify_repl(node, simplify_repl, nodelistargs, what):<EOL><INDENT>if callable(simplify_repl):<EOL><INDENT>if '<STR_LIT>' in getfullargspec(simplify_repl)[<NUM_LIT:0>]:<EOL><INDENT>return simplify_repl(node, l2tobj=self)<EOL><DEDENT>return simplify_repl(node)<EOL><DEDENT>if '<STR_LIT:%>' in simplify_repl:<EOL><INDENT>try:<EOL><INDENT>return simplify_repl % tuple([self._groupnodecontents_to_text(nn)<EOL>for nn in nodelistargs])<EOL><DEDENT>except (TypeError, ValueError):<EOL><INDENT>logger.warning(<EOL>"<STR_LIT>".format(what)<EOL>)<EOL>return simplify_repl <EOL><DEDENT><DEDENT>return simplify_repl<EOL><DEDENT>if node.isNodeType(latexwalker.LatexMacroNode):<EOL><INDENT>macroname = node.macroname.rstrip('<STR_LIT:*>')<EOL>if macroname in self.macro_dict:<EOL><INDENT>mac = self.macro_dict[macroname]<EOL><DEDENT>else:<EOL><INDENT>mac = self.macro_dict['<STR_LIT>']<EOL><DEDENT>def get_macro_str_repl(node, macroname, mac):<EOL><INDENT>if mac.simplify_repl:<EOL><INDENT>return apply_simplify_repl(node, mac.simplify_repl, node.nodeargs,<EOL>what="<STR_LIT>"%(macroname))<EOL><DEDENT>if mac.discard:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>a = node.nodeargs<EOL>if (node.nodeoptarg):<EOL><INDENT>a.prepend(node.nodeoptarg)<EOL><DEDENT>return "<STR_LIT>".join([self._groupnodecontents_to_text(n) for n in a])<EOL><DEDENT>macrostr = get_macro_str_repl(node, macroname, mac)<EOL>return macrostr<EOL><DEDENT>if node.isNodeType(latexwalker.LatexEnvironmentNode):<EOL><INDENT>envname = node.envname.rstrip('<STR_LIT:*>')<EOL>if (envname in self.env_dict):<EOL><INDENT>envdef = self.env_dict[envname]<EOL><DEDENT>else:<EOL><INDENT>envdef = self.env_dict['<STR_LIT>']<EOL><DEDENT>if envdef.simplify_repl:<EOL><INDENT>return apply_simplify_repl(node, envdef.simplify_repl, node.nodelist,<EOL>what="<STR_LIT>"%(envname))<EOL><DEDENT>if envdef.discard:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>return self._nodelistcontents_to_text(node.nodelist)<EOL><DEDENT>if node.isNodeType(latexwalker.LatexMathNode):<EOL><INDENT>if self.keep_inline_math:<EOL><INDENT>return latexwalker.math_node_to_latex(node)<EOL><DEDENT>else:<EOL><INDENT>with _PushEquationContext(self):<EOL><INDENT>return self._nodelistcontents_to_text(node.nodelist)<EOL><DEDENT><DEDENT><DEDENT>logger.warning("<STR_LIT>", node)<EOL>return "<STR_LIT>"<EOL>

Return the textual representation of the given `node`.

If `prev_node_hint` is specified, then the current node is formatted
suitably as following the node given in `prev_node_hint`.  This might
affect how much space we keep/discard, etc.


def utf8tolatex(s, non_ascii_only=False, brackets=True, substitute_bad_chars=False, fail_bad_chars=False):

s = unicode(s) <EOL>s = unicodedata.normalize('<STR_LIT>', s)<EOL>if not s:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT>result = u"<STR_LIT>"<EOL>for ch in s:<EOL><INDENT>if (non_ascii_only and ord(ch) < <NUM_LIT>):<EOL><INDENT>result += ch<EOL><DEDENT>else:<EOL><INDENT>lch = utf82latex.get(ord(ch), None)<EOL>if (lch is not None):<EOL><INDENT>result += (  '<STR_LIT:{>'+lch+'<STR_LIT:}>' if brackets and lch[<NUM_LIT:0>:<NUM_LIT:1>] == '<STR_LIT:\\>' else<EOL>lch  )<EOL><DEDENT>elif ((ord(ch) >= <NUM_LIT:32> and ord(ch) <= <NUM_LIT>) or<EOL>(ch in "<STR_LIT>")):<EOL><INDENT>result += ch<EOL><DEDENT>else:<EOL><INDENT>msg = u"<STR_LIT>" % (ord(ch), ch)<EOL>if fail_bad_chars:<EOL><INDENT>raise ValueError(msg)<EOL><DEDENT>log.warning(msg)<EOL>if substitute_bad_chars:<EOL><INDENT>result += r'<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>result += ch<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return result<EOL>

u"""
    Encode a UTF-8 string to a LaTeX snippet.

    If `non_ascii_only` is set to `True`, then usual (ascii) characters such as ``#``,
    ``{``, ``}`` etc. will not be escaped.  If set to `False` (the default), they are
    escaped to their respective LaTeX escape sequences.

    If `brackets` is set to `True` (the default), then LaTeX macros are enclosed in
    brackets.  For example, ``sant\N{LATIN SMALL LETTER E WITH ACUTE}`` is replaced by
    ``sant{\\'e}`` if `brackets=True` and by ``sant\\'e`` if `brackets=False`.

    .. warning::
        Using `brackets=False` might give you an invalid LaTeX string, so avoid
        it! (for instance, ``ma\N{LATIN SMALL LETTER I WITH CIRCUMFLEX}tre`` will be
        replaced incorrectly by ``ma\\^\\itre`` resulting in an unknown macro ``\\itre``).

    If `substitute_bad_chars=True`, then any non-ascii character for which no LaTeX escape
    sequence is known is replaced by a question mark in boldface. Otherwise (by default),
    the character is left as it is.

    If `fail_bad_chars=True`, then a `ValueError` is raised if we cannot find a
    character substitution for any non-ascii character.

    .. versionchanged:: 1.3

        Added `fail_bad_chars` switch


with open(os.path.join(*paths), '<STR_LIT:r>') as f:<EOL><INDENT>return f.read()<EOL><DEDENT>

Build a file path from *paths* and return the contents.


req = context.get('<STR_LIT>')<EOL>qs = SliderItem.objects.published(req).order_by('<STR_LIT>')<EOL>if amount:<EOL><INDENT>qs = qs[:amount]<EOL><DEDENT>return qs<EOL>


req = context.get('<STR_LIT>')<EOL>qs = SliderItem.objects.published(req).order_by('<STR_LIT>')<EOL>return {<EOL>'<STR_LIT>': qs,<EOL>}<EOL>


<EOL>for title in orm['<STR_LIT>'].objects.all():<EOL><INDENT>title.is_published = True<EOL>title.save()<EOL><DEDENT>


try:<EOL><INDENT>return self.content_object.get_absolute_url()<EOL><DEDENT>except AttributeError:<EOL><INDENT>return self.external_url<EOL><DEDENT>


return c.mro()[<NUM_LIT:1>:]<EOL>

return a tuple of all base classes the class c has as a parent.
>>> object in all_bases(list)
True


return c.mro()<EOL>

return a tuple of all classes to which c belongs
>>> list in all_classes(list)
True


if not isinstance(cls, type):<EOL><INDENT>raise TypeError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % cls<EOL>)<EOL><DEDENT>if _seen is None:<EOL><INDENT>_seen = set()<EOL><DEDENT>try:<EOL><INDENT>subs = cls.__subclasses__()<EOL><DEDENT>except TypeError:  <EOL><INDENT>subs = cls.__subclasses__(cls)<EOL><DEDENT>for sub in subs:<EOL><INDENT>if sub in _seen:<EOL><INDENT>continue<EOL><DEDENT>_seen.add(sub)<EOL>yield sub<EOL>for sub in iter_subclasses(sub, _seen):<EOL><INDENT>yield sub<EOL><DEDENT><DEDENT>

Generator over all subclasses of a given class, in depth-first order.

>>> bool in list(iter_subclasses(int))
True
>>> class A(object): pass
>>> class B(A): pass
>>> class C(A): pass
>>> class D(B,C): pass
>>> class E(D): pass
>>>
>>> for cls in iter_subclasses(A):
...		print(cls.__name__)
B
D
E
C
>>> # get ALL (new-style) classes currently defined
>>> res = [cls.__name__ for cls in iter_subclasses(object)]
>>> 'type' in res
True
>>> 'tuple' in res
True
>>> len(res) > 100
True


parser = etree.XMLParser(target=RpmParser())<EOL>return etree.parse(fp.read(), parser)<EOL>


return Package.from_url(self.uri())<EOL>


package_data = HTTPClient().http_request(url=url, decode=None)<EOL>return Package(raw_data=package_data)<EOL>

Given a URL, return a package
:param url:
:return:


cpio = self.rpm.gzip_file.read()<EOL>content = cpio.read()<EOL>return []<EOL>

Read the contents of the rpm itself
:return:


self.rpm.__exit__(*excinfo)<EOL>if self.mode == self.MODE_RAW_DATA:<EOL><INDENT>self.fp.close()<EOL><DEDENT>

Make sure we close the file after we finish the 'with'
:param excinfo:
:return:


for repo_path in self.path.glob('<STR_LIT>'):<EOL><INDENT>for id, repository in self._get_repo_file(repo_path).repositories:<EOL><INDENT>yield id, repository<EOL><DEDENT><DEDENT>

Return a list of all repository objects in the repofiles in the repo folder specified
:return:


if repo_path not in self._repo_files:<EOL><INDENT>self._repo_files[repo_path] = RepoFile(repo_path)<EOL><DEDENT>return self._repo_files[repo_path]<EOL>

Lazy load RepoFile objects on demand.
:param repo_path:
:return:


super(RepoFile, self).__init__()<EOL>self._path_to_config = str(path_to_config)<EOL>self.read(self._path_to_config)<EOL>self._yum_variables = {}<EOL>

Path on disk to the ini file containing the repo description
:param str path_to_config:
:return:


def set_yum_variables(self, releasever, basearch):

self.yum_variables = {<EOL>'<STR_LIT>': str(releasever),<EOL>'<STR_LIT>': basearch<EOL>}<EOL>

Variables which are usually injected from an OS level.
:param str releasever: OS major version
:param str basearch: One of either 'i386' or 'x86_64'.
:return:


self._yum_variables = {<EOL>'<STR_LIT>': str(releasever),<EOL>'<STR_LIT>': basearch<EOL>}<EOL>

Variables which are usually injected from an OS level.
:param int releasever: OS major version
:param str basearch: One of either 'i386' or 'x86_64'.
:return:


parser = etree.XMLParser(target=cls.xml_parse())<EOL>return etree.parse(xml_path, parser)<EOL>

Parses an xml_path with the inherited xml parser
:param xml_path:
:return:


self._attributes = kwargs<EOL>self.repo_url = None<EOL>

Ingest all attributes
:param kwargs:
:return:


if item not in self._attributes:<EOL><INDENT>raise KeyError("<STR_LIT>".format(item))<EOL><DEDENT>else:<EOL><INDENT>return self._attributes[item]<EOL><DEDENT>

Forward attribute requests to self._attributes, or try to get them normally.
:param item:
:return:


self.repo_url = new_url<EOL>

Setter function for the repo URL
:param new_url:
:return:


return "<STR_LIT>".format(self.repo_url.replace('<STR_LIT>', '<STR_LIT>'), self._attributes['<STR_LIT:location>'])<EOL>

Predefined format for the location. This should be an @property
:return:


data = self.http_request(self.location())<EOL>self._parse(data)<EOL>return self<EOL>

Load the repo database from the remote source, and then parse it.
:return:


if six.PY2 and isinstance(unicode_str, __unicode__):<EOL><INDENT>return unicode_str.encode('<STR_LIT:utf-8>')<EOL><DEDENT>return unicode_str<EOL>

Return a utf-8 encoded string from a valid unicode string.

:param unicode_str: Unicode string.

:return: str


if not instruction.endswith(INST_TERM):<EOL><INDENT>raise InvalidInstruction('<STR_LIT>')<EOL><DEDENT>args = cls.decode_instruction(instruction)<EOL>return cls(args[<NUM_LIT:0>], *args[<NUM_LIT:1>:])<EOL>

Loads a new GuacamoleInstruction from encoded instruction string.

:param instruction: Instruction string.

:return: GuacamoleInstruction()


if not instruction.endswith(INST_TERM):<EOL><INDENT>raise InvalidInstruction('<STR_LIT>')<EOL><DEDENT>instruction = utf8(instruction)<EOL>elems = instruction.split(ELEM_SEP, <NUM_LIT:1>)<EOL>try:<EOL><INDENT>arg_size = int(elems[<NUM_LIT:0>])<EOL><DEDENT>except Exception:<EOL><INDENT>raise InvalidInstruction(<EOL>'<STR_LIT>' +<EOL>'<STR_LIT>')<EOL><DEDENT>arg_str = elems[<NUM_LIT:1>][:arg_size]<EOL>remaining = elems[<NUM_LIT:1>][arg_size:]<EOL>args = [arg_str]<EOL>if remaining.startswith(ARG_SEP):<EOL><INDENT>remaining = remaining[<NUM_LIT:1>:]<EOL><DEDENT>elif remaining == INST_TERM:<EOL><INDENT>return args<EOL><DEDENT>else:<EOL><INDENT>raise InvalidInstruction(<EOL>'<STR_LIT>' % arg_str)<EOL><DEDENT>next_args = GuacamoleInstruction.decode_instruction(remaining)<EOL>if next_args:<EOL><INDENT>args = args + next_args<EOL><DEDENT>return args<EOL>

Decode whole instruction and return list of args.
Usually, returned arg[0] is the instruction opcode.

example:
>> args = decode_instruction('4.size,4.1024;')
>> args == ['size', '1024']
>> True

:param instruction: Instruction string.

:return: list


arg_utf8 = utf8(arg)<EOL>return ELEM_SEP.join([str(len(str(arg_utf8))), str(arg_utf8)])<EOL>

Encode argument to be sent in a valid GuacamoleInstruction.

example:
>> arg = encode_arg('size')
>> arg == '4.size'
>> True

:param arg: arg string.

:return: str


instruction_iter = itertools.chain([self.opcode], self.args)<EOL>elems = ARG_SEP.join(self.encode_arg(arg) for arg in instruction_iter)<EOL>return elems + INST_TERM<EOL>

Prepare the instruction to be sent over the wire.

:return: str


def __init__(self, host, port, timeout=<NUM_LIT:20>, debug=False, logger=None):

self.host = host<EOL>self.port = port<EOL>self.timeout = timeout<EOL>self._client = None<EOL>self.connected = False<EOL>self._buffer = bytearray()<EOL>self._id = None<EOL>self.logger = guac_logger<EOL>if logger:<EOL><INDENT>self.logger = logger<EOL><DEDENT>if debug:<EOL><INDENT>self.logger.setLevel(logging.DEBUG)<EOL><DEDENT>

Guacamole Client class. This class can handle communication with guacd
server.

:param host: guacd server host.

:param port: guacd server port.

:param timeout: socket connection timeout.

:param debug: if True, default logger will switch to Debug level.


if not self._client:<EOL><INDENT>self._client = socket.create_connection(<EOL>(self.host, self.port), self.timeout)<EOL>self.logger.debug('<STR_LIT>'<EOL>% (self.host, self.port, self.timeout))<EOL><DEDENT>return self._client<EOL>


return self._id<EOL>


self.client.close()<EOL>self._client = None<EOL>self.connected = False<EOL>self.logger.debug('<STR_LIT>')<EOL>

Terminate connection with Guacamole guacd server.


start = <NUM_LIT:0><EOL>while True:<EOL><INDENT>idx = self._buffer.find(INST_TERM.encode(), start)<EOL>if idx != -<NUM_LIT:1>:<EOL><INDENT>line = self._buffer[:idx + <NUM_LIT:1>].decode()<EOL>self._buffer = self._buffer[idx + <NUM_LIT:1>:]<EOL>self.logger.debug('<STR_LIT>' % line)<EOL>return line<EOL><DEDENT>else:<EOL><INDENT>start = len(self._buffer)<EOL>buf = self.client.recv(BUF_LEN)<EOL>if not buf:<EOL><INDENT>self.close()<EOL>self.logger.debug(<EOL>'<STR_LIT>')<EOL>return None<EOL><DEDENT>self._buffer.extend(buf)<EOL><DEDENT><DEDENT>

Receive instructions from Guacamole guacd server.


self.logger.debug('<STR_LIT>' % data)<EOL>self.client.sendall(data.encode())<EOL>

Send encoded instructions to Guacamole guacd server.


self.logger.debug('<STR_LIT>')<EOL>return Instruction.load(self.receive())<EOL>


self.logger.debug('<STR_LIT>' % str(instruction))<EOL>return self.send(instruction.encode())<EOL>


def handshake(self, protocol='<STR_LIT>', width=<NUM_LIT>, height=<NUM_LIT>, dpi=<NUM_LIT>,<EOL>audio=None, video=None, image=None, **kwargs):

if protocol not in PROTOCOLS:<EOL><INDENT>self.logger.debug('<STR_LIT>' % protocol)<EOL>raise GuacamoleError('<STR_LIT>')<EOL><DEDENT>if audio is None:<EOL><INDENT>audio = list()<EOL><DEDENT>if video is None:<EOL><INDENT>video = list()<EOL><DEDENT>if image is None:<EOL><INDENT>image = list()<EOL><DEDENT>self.logger.debug('<STR_LIT>')<EOL>self.send_instruction(Instruction('<STR_LIT>', protocol))<EOL>instruction = self.read_instruction()<EOL>self.logger.debug('<STR_LIT>'<EOL>% str(instruction))<EOL>if not instruction:<EOL><INDENT>self.close()<EOL>raise GuacamoleError(<EOL>'<STR_LIT>')<EOL><DEDENT>if instruction.opcode != '<STR_LIT:args>':<EOL><INDENT>self.close()<EOL>raise GuacamoleError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % instruction.opcode)<EOL><DEDENT>self.logger.debug('<STR_LIT>'<EOL>% (width, height, dpi))<EOL>self.send_instruction(Instruction('<STR_LIT:size>', width, height, dpi))<EOL>self.logger.debug('<STR_LIT>' % audio)<EOL>self.send_instruction(Instruction('<STR_LIT>', *audio))<EOL>self.logger.debug('<STR_LIT>' % video)<EOL>self.send_instruction(Instruction('<STR_LIT>', *video))<EOL>self.logger.debug('<STR_LIT>' % image)<EOL>self.send_instruction(Instruction('<STR_LIT:image>', *image))<EOL>connection_args = [<EOL>kwargs.get(arg.replace('<STR_LIT:->', '<STR_LIT:_>'), '<STR_LIT>') for arg in instruction.args<EOL>]<EOL>self.logger.debug('<STR_LIT>' % connection_args)<EOL>self.send_instruction(Instruction('<STR_LIT>', *connection_args))<EOL>instruction = self.read_instruction()<EOL>self.logger.debug('<STR_LIT>'<EOL>% str(instruction))<EOL>if instruction.opcode != '<STR_LIT>':<EOL><INDENT>self.logger.warning(<EOL>'<STR_LIT>')<EOL><DEDENT>if instruction.args:<EOL><INDENT>self._id = instruction.args[<NUM_LIT:0>]<EOL>self.logger.debug(<EOL>'<STR_LIT>' % self.id)<EOL><DEDENT>self.logger.debug('<STR_LIT>')<EOL>self.connected = True<EOL>

Establish connection with Guacamole guacd server via handshake.


args = self.parser.parse_args(args)<EOL>if not args.token:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>sys.path.insert(<NUM_LIT:0>, "<STR_LIT:.>")<EOL>module_path, object_path = args.channel_layer.split('<STR_LIT::>', <NUM_LIT:1>)<EOL>channel_layer = importlib.import_module(module_path)<EOL>for part in object_path.split('<STR_LIT:.>'):<EOL><INDENT>channel_layer = getattr(channel_layer, part)<EOL><DEDENT>Client(<EOL>channel_layer=channel_layer,<EOL>token=args.token,<EOL>).run()<EOL>

Pass in raw arguments, instantiate Slack API and begin client.


return json.dumps(message).encode('<STR_LIT:utf8>')<EOL>

Serialize the message into something for sending
across the wire.

Args:
    message: {dict} dictionary that full specifies a slack message


return json.loads(text.decode('<STR_LIT:utf8>'))<EOL>

Decode & Load

Args:
    text: {str} JSON blob


self._message_id += <NUM_LIT:1><EOL>return self._message_id<EOL>


try:<EOL><INDENT>channel_id = self.slack.channel_from_name(channel)['<STR_LIT:id>']<EOL><DEDENT>except ValueError:<EOL><INDENT>channel_id = channel<EOL><DEDENT>return pack({<EOL>'<STR_LIT:text>': text,<EOL>'<STR_LIT:type>': '<STR_LIT:message>',<EOL>'<STR_LIT>': channel_id,<EOL>'<STR_LIT:id>': self.message_id,<EOL>})<EOL>

High-level function for creating messages. Return packed bytes.

Args:
    text: {str}
    channel: {str} Either name or ID


<EOL>try:<EOL><INDENT>user_id = message.pop('<STR_LIT:user>')<EOL>user = self.slack.user_from_id(user_id)<EOL>message[u'<STR_LIT:user>'] = user['<STR_LIT:name>']<EOL><DEDENT>except (KeyError, IndexError, ValueError):<EOL><INDENT>pass<EOL><DEDENT>try:<EOL><INDENT>if type(message['<STR_LIT>']) == str:<EOL><INDENT>channel_id = message.pop('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>channel_id = message.pop('<STR_LIT>')['<STR_LIT:id>']<EOL>self.slack.reload_channels()<EOL><DEDENT>channel = self.slack.channel_from_id(channel_id)<EOL>message[u'<STR_LIT>'] = channel['<STR_LIT:name>']<EOL><DEDENT>except (KeyError, IndexError, ValueError):<EOL><INDENT>pass<EOL><DEDENT>return message<EOL>

Translate machine identifiers into human-readable


self.factory.protocols.append(self)<EOL>

Store this protocol instance in the factory and wave hello.


msg = self.translate(unpack(payload))<EOL>if '<STR_LIT:type>' in msg:<EOL><INDENT>channel_name = '<STR_LIT>'.format(msg['<STR_LIT:type>'])<EOL>print('<STR_LIT>'.format(channel_name))<EOL>channels.Channel(channel_name).send({'<STR_LIT:text>': pack(msg)})<EOL><DEDENT>

Send the payload onto the {slack.[payload['type]'} channel.
The message is transalated from IDs to human-readable identifiers.

Note: The slack API only sends JSON, isBinary will always be false.


channel = message.get('<STR_LIT>', '<STR_LIT>')<EOL>self.sendMessage(self.make_message(message['<STR_LIT:text>'], channel))<EOL>


channel, message = self.protocol.channel_layer.receive_many([u'<STR_LIT>'], block=False)<EOL>delay = <NUM_LIT:0.1><EOL>if channel:<EOL><INDENT>self.protocols[<NUM_LIT:0>].sendSlack(message)<EOL><DEDENT>reactor.callLater(delay, self.read_channel)<EOL>

Get available messages and send through to the protocol


def __init__(self, channel_layer, token, channel_name=u'<STR_LIT>'):

self.channel_layer = channel_layer<EOL>self.token = token<EOL>self.channel_name = channel_name<EOL>

Args:
    channel_layer: channel layer on which this client will communicate to Django
    token: {str} Slack token
    channel_name: {str} channel name to send messages that will come back to slack


slack = SlackAPI(token=self.token)<EOL>rtm = slack.rtm_start()<EOL>factory = SlackClientFactory(rtm['<STR_LIT:url>'])<EOL>factory.protocol = SlackClientProtocol<EOL>factory.protocol.slack = slack<EOL>factory.protocol.channel_layer = self.channel_layer<EOL>factory.channel_name = self.channel_name<EOL>factory.run()<EOL>

Main interface. Instantiate the SlackAPI, connect to RTM
and start the client.


def _call_api(self, method, params=None):

url = self.url.format(method=method)<EOL>if not params:<EOL><INDENT>params = {'<STR_LIT>': self.token}<EOL><DEDENT>else:<EOL><INDENT>params['<STR_LIT>'] = self.token<EOL><DEDENT>logger.debug('<STR_LIT>', url)<EOL>response = requests.get(url, params=params).json()<EOL>if self.verify:<EOL><INDENT>if not response['<STR_LIT>']:<EOL><INDENT>msg = '<STR_LIT>'<EOL>raise Exception(msg.format(url=url, response=response))<EOL><DEDENT><DEDENT>return response<EOL>

Low-level method to call the Slack API.

Args:
    method: {str} method name to call
    params: {dict} GET parameters
        The token will always be added


if not self._channels:<EOL><INDENT>self._channels = self._call_api('<STR_LIT>')['<STR_LIT>']<EOL><DEDENT>return self._channels<EOL>


if not self._users:<EOL><INDENT>self._users = self._call_api('<STR_LIT>')['<STR_LIT>']<EOL><DEDENT>return self._users<EOL>


return self._call_api('<STR_LIT>')<EOL>


try:<EOL><INDENT>channel = [channel for channel in self.channels<EOL>if channel['<STR_LIT:name>'] == name][<NUM_LIT:0>]<EOL><DEDENT>except IndexError:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(name))<EOL><DEDENT>return channel<EOL>

Return the channel dict given by human-readable {name}


ignore = ("<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>")<EOL>for glob in ignore:<EOL><INDENT>if fnmatch.fnmatchcase(filename.upper(), glob.upper()):<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL>

We do not support multiple signatures in XPI signing because the client
side code makes some pretty reasonable assumptions about a single signature
on any given JAR.  This function returns True if the file name given is one
that we dispose of to prevent multiple signatures.


prio = <NUM_LIT:4><EOL>if filename == '<STR_LIT>':<EOL><INDENT>prio = <NUM_LIT:1><EOL><DEDENT>elif filename in ["<STR_LIT>", "<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>prio = <NUM_LIT:2><EOL><DEDENT>elif filename in ["<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>",<EOL>"<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>prio = <NUM_LIT:5><EOL><DEDENT>return (prio, os.path.split(filename.lower()))<EOL>

Sort keys for xpi files

    The filenames in a manifest are ordered so that files not in a
    directory come before files in any directory, ordered
    alphabetically but ignoring case, with a few exceptions
    (install.rdf, chrome.manifest, icon.png and icon64.png come at the
    beginning; licenses come at the end).

    This order does not appear to affect anything in any way, but it
    looks nicer.


return file_key(zinfo.filename)<EOL>

Same as file_key, but for a ZipInfo entry.


return "<STR_LIT>".format(<EOL>type_name.title(),<EOL>version,<EOL>)<EOL>

Returns a header, suitable for use in a manifest.

    >>> manifest_header("signature")
    "Signature-Version: 1.0"

    :param type_name: The kind of manifest which needs a header:
        "manifest", "signature".
    :param version: The manifest version to encode in the header
        (default: '1.0')


server=_shared.get('<STR_LIT>')<EOL>if server is None:<EOL><INDENT>server=launch_pipeserver()<EOL><DEDENT>return server<EOL>


def extract_hbs(fileobj, keywords, comment_tags, options):

server = get_pipeserver()<EOL>server.sendline(COMMAND+u'<STR_LIT>'+fileobj.name)<EOL>server.expect(RESPONSE+'<STR_LIT>')<EOL>server.expect(RESPONSE+'<STR_LIT>')<EOL>trans_strings = server.before<EOL>for item in json.loads(trans_strings):<EOL><INDENT>messages = [item['<STR_LIT:content>']]<EOL>if item['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>messages.append(item['<STR_LIT>'])<EOL><DEDENT>yield item['<STR_LIT>'],item['<STR_LIT>'],tuple(messages),[]<EOL><DEDENT>

Extract messages from Handlebars templates.

    It returns an iterator yielding tuples in the following form ``(lineno,
    funcname, message, comments)``.

    TODO: Things to improve:
    --- Return comments


if self.color == BLACK:<EOL><INDENT>return PIECE_SYMBOLS[self.piece_type].upper()<EOL><DEDENT>else:<EOL><INDENT>return PIECE_SYMBOLS[self.piece_type]<EOL><DEDENT>


if symbol.lower() == symbol:<EOL><INDENT>return cls(PIECE_SYMBOLS.index(symbol), WHITE)<EOL><DEDENT>else:<EOL><INDENT>return cls(PIECE_SYMBOLS.index(symbol.lower()), BLACK)<EOL><DEDENT>

Creates a piece instance from a piece symbol.
Raises `ValueError` if the symbol is invalid.


self.piece_bb = [<EOL>BB_VOID,                       <EOL>BB_RANK_C | BB_RANK_G,         <EOL>BB_A1 | BB_I1 | BB_A9 | BB_I9, <EOL>BB_A2 | BB_A8 | BB_I2 | BB_I8, <EOL>BB_A3 | BB_A7 | BB_I3 | BB_I7, <EOL>BB_A4 | BB_A6 | BB_I4 | BB_I6, <EOL>BB_B2 | BB_H8,                 <EOL>BB_B8 | BB_H2,                 <EOL>BB_A5 | BB_I5,                 <EOL>BB_VOID,                       <EOL>BB_VOID,                       <EOL>BB_VOID,                       <EOL>BB_VOID,                       <EOL>BB_VOID,                       <EOL>BB_VOID,                       <EOL>]<EOL>self.pieces_in_hand = [collections.Counter(), collections.Counter()]<EOL>self.occupied = Occupied(BB_RANK_G | BB_H2 | BB_H8 | BB_RANK_I, BB_RANK_A | BB_B2 | BB_B8 | BB_RANK_C)<EOL>self.king_squares = [I5, A5]<EOL>self.pieces = [NONE for i in SQUARES]<EOL>for i in SQUARES:<EOL><INDENT>mask = BB_SQUARES[i]<EOL>for piece_type in PIECE_TYPES:<EOL><INDENT>if mask & self.piece_bb[piece_type]:<EOL><INDENT>self.pieces[i] = piece_type<EOL><DEDENT><DEDENT><DEDENT>self.turn = BLACK<EOL>self.move_number = <NUM_LIT:1><EOL>self.captured_piece_stack = collections.deque()<EOL>self.move_stack = collections.deque()<EOL>self.incremental_zobrist_hash = self.board_zobrist_hash(DEFAULT_RANDOM_ARRAY)<EOL>self.transpositions = collections.Counter((self.zobrist_hash(), ))<EOL>


mask = BB_SQUARES[square]<EOL>color = int(bool(self.occupied[WHITE] & mask))<EOL>piece_type = self.piece_type_at(square)<EOL>if piece_type:<EOL><INDENT>return Piece(piece_type, color)<EOL><DEDENT>


return self.pieces[square]<EOL>

Gets the piece type at the given square.


def remove_piece_at(self, square, into_hand=False):

piece_type = self.piece_type_at(square)<EOL>if piece_type == NONE:<EOL><INDENT>return<EOL><DEDENT>if into_hand:<EOL><INDENT>self.add_piece_into_hand(piece_type, self.turn)<EOL><DEDENT>mask = BB_SQUARES[square]<EOL>self.piece_bb[piece_type] ^= mask<EOL>color = int(bool(self.occupied[WHITE] & mask))<EOL>self.pieces[square] = NONE<EOL>self.occupied.ixor(mask, color, square)<EOL>if color == BLACK:<EOL><INDENT>piece_index = (piece_type - <NUM_LIT:1>) * <NUM_LIT:2><EOL><DEDENT>else:<EOL><INDENT>piece_index = (piece_type - <NUM_LIT:1>) * <NUM_LIT:2> + <NUM_LIT:1><EOL><DEDENT>self.incremental_zobrist_hash ^= DEFAULT_RANDOM_ARRAY[<NUM_LIT> * piece_index + <NUM_LIT:9> * rank_index(square) + file_index(square)]<EOL>

Removes a piece from the given square if present.


def set_piece_at(self, square, piece, from_hand=False, into_hand=False):

if from_hand:<EOL><INDENT>self.remove_piece_from_hand(piece.piece_type, self.turn)<EOL><DEDENT>self.remove_piece_at(square, into_hand)<EOL>self.pieces[square] = piece.piece_type<EOL>mask = BB_SQUARES[square]<EOL>piece_type = piece.piece_type<EOL>self.piece_bb[piece_type] |= mask<EOL>if piece_type == KING:<EOL><INDENT>self.king_squares[piece.color] = square<EOL><DEDENT>self.occupied.ixor(mask, piece.color, square)<EOL>if piece.color == BLACK:<EOL><INDENT>piece_index = (piece.piece_type - <NUM_LIT:1>) * <NUM_LIT:2><EOL><DEDENT>else:<EOL><INDENT>piece_index = (piece.piece_type - <NUM_LIT:1>) * <NUM_LIT:2> + <NUM_LIT:1><EOL><DEDENT>self.incremental_zobrist_hash ^= DEFAULT_RANDOM_ARRAY[<NUM_LIT> * piece_index + <NUM_LIT:9> * rank_index(square) + file_index(square)]<EOL>

Sets a piece at the given square. An existing piece is replaced.


self.push(move)<EOL>is_suicide = self.was_suicide()<EOL>is_check_by_dropping_pawn = self.was_check_by_dropping_pawn(move)<EOL>self.pop()<EOL>return is_suicide or is_check_by_dropping_pawn<EOL>

Checks if the given move would move would leave the king in check or
put it into check.


return self.is_attacked_by(self.turn, self.king_squares[self.turn ^ <NUM_LIT:1>])<EOL>

Checks if the king of the other side is attacked. Such a position is not
valid and could only be reached by an illegal move.


<EOL>try:<EOL><INDENT>next(self.generate_legal_moves().__iter__())<EOL><DEDENT>except StopIteration:<EOL><INDENT>return True<EOL><DEDENT>if self.is_fourfold_repetition():<EOL><INDENT>return True<EOL><DEDENT>return False<EOL>

Checks if the game is over due to checkmate, stalemate or
fourfold repetition.


if not self.is_check():<EOL><INDENT>return False<EOL><DEDENT>try:<EOL><INDENT>next(self.generate_legal_moves().__iter__())<EOL>return False<EOL><DEDENT>except StopIteration:<EOL><INDENT>return True<EOL><DEDENT>

Checks if the current position is a checkmate.


if self.is_check():<EOL><INDENT>return False<EOL><DEDENT>try:<EOL><INDENT>next(self.generate_legal_moves().__iter__())<EOL>return False<EOL><DEDENT>except StopIteration:<EOL><INDENT>return True<EOL><DEDENT>

Checks if the current position is a stalemate.


zobrist_hash = self.zobrist_hash()<EOL>if self.transpositions[zobrist_hash] < <NUM_LIT:4>:<EOL><INDENT>return False<EOL><DEDENT>return True<EOL>

a game is ended if a position occurs for the fourth time
on consecutive alternating moves.


<EOL>self.move_number += <NUM_LIT:1><EOL>captured_piece = self.piece_type_at(move.to_square) if move else NONE<EOL>self.captured_piece_stack.append(captured_piece)<EOL>self.move_stack.append(move)<EOL>if not move:<EOL><INDENT>self.turn ^= <NUM_LIT:1><EOL>return<EOL><DEDENT>if move.drop_piece_type:<EOL><INDENT>piece_type = move.drop_piece_type<EOL>from_hand = True<EOL><DEDENT>else:<EOL><INDENT>piece_type = self.piece_type_at(move.from_square)<EOL>from_hand = False<EOL>if move.promotion:<EOL><INDENT>piece_type = PIECE_PROMOTED[piece_type]<EOL><DEDENT>self.remove_piece_at(move.from_square, False)<EOL><DEDENT>self.set_piece_at(move.to_square, Piece(piece_type, self.turn), from_hand, True)<EOL>self.turn ^= <NUM_LIT:1><EOL>self.transpositions.update((self.zobrist_hash(), ))<EOL>

Updates the position with the given move and puts it onto a stack.
Null moves just increment the move counters, switch turns and forfeit
en passant capturing.
No validation is performed. For performance moves are assumed to be at
least pseudo legal. Otherwise there is no guarantee that the previous
board state can be restored. To check it yourself you can use:
>>> move in board.pseudo_legal_moves
True


move = self.move_stack.pop()<EOL>self.transpositions.subtract((self.zobrist_hash(), ))<EOL>self.move_number -= <NUM_LIT:1><EOL>captured_piece_type = self.captured_piece_stack.pop()<EOL>captured_piece_color = self.turn<EOL>if not move:<EOL><INDENT>self.turn ^= <NUM_LIT:1><EOL>return move<EOL><DEDENT>piece_type = self.piece_type_at(move.to_square)<EOL>if move.promotion:<EOL><INDENT>piece_type = PIECE_PROMOTED.index(piece_type)<EOL><DEDENT>if move.from_square is None:<EOL><INDENT>self.add_piece_into_hand(piece_type, self.turn ^ <NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>self.set_piece_at(move.from_square, Piece(piece_type, self.turn ^ <NUM_LIT:1>))<EOL><DEDENT>if captured_piece_type:<EOL><INDENT>self.remove_piece_from_hand(captured_piece_type, captured_piece_color ^ <NUM_LIT:1>)<EOL>self.set_piece_at(move.to_square, Piece(captured_piece_type, captured_piece_color))<EOL><DEDENT>else:<EOL><INDENT>self.remove_piece_at(move.to_square)<EOL><DEDENT>self.turn ^= <NUM_LIT:1><EOL>return move<EOL>

Restores the previous position and returns the last move from the stack.


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


sfen = []<EOL>empty = <NUM_LIT:0><EOL>for square in SQUARES:<EOL><INDENT>piece = self.piece_at(square)<EOL>if not piece:<EOL><INDENT>empty += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>if empty:<EOL><INDENT>sfen.append(str(empty))<EOL>empty = <NUM_LIT:0><EOL><DEDENT>sfen.append(piece.symbol())<EOL><DEDENT>if BB_SQUARES[square] & BB_FILE_1:<EOL><INDENT>if empty:<EOL><INDENT>sfen.append(str(empty))<EOL>empty = <NUM_LIT:0><EOL><DEDENT>if square != I1:<EOL><INDENT>sfen.append('<STR_LIT:/>')<EOL><DEDENT><DEDENT><DEDENT>sfen.append('<STR_LIT:U+0020>')<EOL>if self.turn == WHITE:<EOL><INDENT>sfen.append('<STR_LIT:w>')<EOL><DEDENT>else:<EOL><INDENT>sfen.append('<STR_LIT:b>')<EOL><DEDENT>sfen.append('<STR_LIT:U+0020>')<EOL>pih_len = <NUM_LIT:0><EOL>for color in COLORS:<EOL><INDENT>p = self.pieces_in_hand[color]<EOL>pih_len += len(p)<EOL>for piece_type in sorted(p.keys(), reverse=True):<EOL><INDENT>if p[piece_type] >= <NUM_LIT:1>:<EOL><INDENT>if p[piece_type] > <NUM_LIT:1>:<EOL><INDENT>sfen.append(str(p[piece_type]))<EOL><DEDENT>piece = Piece(piece_type, color)<EOL>sfen.append(piece.symbol())<EOL><DEDENT><DEDENT><DEDENT>if pih_len == <NUM_LIT:0>:<EOL><INDENT>sfen.append('<STR_LIT:->')<EOL><DEDENT>sfen.append('<STR_LIT:U+0020>')<EOL>sfen.append(str(self.move_number))<EOL>return '<STR_LIT>'.join(sfen)<EOL>

Gets an SFEN representation of the current position.


<EOL>parts = sfen.split()<EOL>if len(parts) != <NUM_LIT:4>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr(sfen)))<EOL><DEDENT>rows = parts[<NUM_LIT:0>].split('<STR_LIT:/>')<EOL>if len(rows) != <NUM_LIT:9>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr(sfen)))<EOL><DEDENT>for row in rows:<EOL><INDENT>field_sum = <NUM_LIT:0><EOL>previous_was_digit = False<EOL>previous_was_plus = False<EOL>for c in row:<EOL><INDENT>if c in ['<STR_LIT:1>', '<STR_LIT:2>', '<STR_LIT:3>', '<STR_LIT:4>', '<STR_LIT:5>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>if previous_was_digit:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr(sfen)))<EOL><DEDENT>if previous_was_plus:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr(sfen)))<EOL><DEDENT>field_sum += int(c)<EOL>previous_was_digit = True<EOL>previous_was_plus = False<EOL><DEDENT>elif c == '<STR_LIT:+>':<EOL><INDENT>if previous_was_plus:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr(sfen)))<EOL><DEDENT>previous_was_digit = False<EOL>previous_was_plus = True<EOL><DEDENT>elif c.lower() in ['<STR_LIT:p>', '<STR_LIT:l>', '<STR_LIT:n>', '<STR_LIT:s>', '<STR_LIT:g>', '<STR_LIT:b>', '<STR_LIT:r>', '<STR_LIT:k>']:<EOL><INDENT>field_sum += <NUM_LIT:1><EOL>if previous_was_plus and (c.lower() == '<STR_LIT:g>' or c.lower() == '<STR_LIT:k>'):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>previous_was_digit = False<EOL>previous_was_plus = False<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr(sfen)))<EOL><DEDENT><DEDENT>if field_sum != <NUM_LIT:9>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr(sfen)))<EOL><DEDENT><DEDENT>if not parts[<NUM_LIT:1>] in ['<STR_LIT:b>', '<STR_LIT:w>']:<EOL><INDENT>raise ValueError("<STR_LIT>".format(repr(sfen)))<EOL><DEDENT>if int(parts[<NUM_LIT:3>]) < <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr(sfen)))<EOL><DEDENT>self.clear()<EOL>square_index = <NUM_LIT:0><EOL>previous_was_plus = False<EOL>for c in parts[<NUM_LIT:0>]:<EOL><INDENT>if c in ['<STR_LIT:1>', '<STR_LIT:2>', '<STR_LIT:3>', '<STR_LIT:4>', '<STR_LIT:5>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>square_index += int(c)<EOL><DEDENT>elif c == '<STR_LIT:+>':<EOL><INDENT>previous_was_plus = True<EOL><DEDENT>elif c == '<STR_LIT:/>':<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>piece_symbol = c<EOL>if previous_was_plus:<EOL><INDENT>piece_symbol = '<STR_LIT:+>' + piece_symbol<EOL><DEDENT>self.set_piece_at(square_index, Piece.from_symbol(piece_symbol))<EOL>square_index += <NUM_LIT:1><EOL>previous_was_plus = False<EOL><DEDENT><DEDENT>if parts[<NUM_LIT:1>] == '<STR_LIT:w>':<EOL><INDENT>self.turn = WHITE<EOL><DEDENT>else:<EOL><INDENT>self.turn = BLACK<EOL><DEDENT>self.pieces_in_hand = [collections.Counter(), collections.Counter()]<EOL>if parts[<NUM_LIT:2>] != '<STR_LIT:->':<EOL><INDENT>piece_count = <NUM_LIT:0><EOL>for c in parts[<NUM_LIT:2>]:<EOL><INDENT>if c in ['<STR_LIT:0>', '<STR_LIT:1>', '<STR_LIT:2>', '<STR_LIT:3>', '<STR_LIT:4>', '<STR_LIT:5>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>piece_count *= <NUM_LIT:10><EOL>piece_count += int(c)<EOL><DEDENT>else:<EOL><INDENT>piece = Piece.from_symbol(c)<EOL>if piece_count == <NUM_LIT:0>:<EOL><INDENT>piece_count = <NUM_LIT:1><EOL><DEDENT>self.add_piece_into_hand(piece.piece_type, piece.color, piece_count)<EOL>piece_count = <NUM_LIT:0><EOL><DEDENT><DEDENT><DEDENT>self.move_number = int(parts[<NUM_LIT:3>]) or <NUM_LIT:1><EOL>self.transpositions = collections.Counter((self.zobrist_hash(), ))<EOL>

Parses a SFEN and sets the position from it.
Rasies `ValueError` if the SFEN string is invalid.


move = Move.from_usi(usi)<EOL>self.push(move)<EOL>return move<EOL>

Parses a move in standard coordinate notation, makes the move and puts
it on the the move stack.
Raises `ValueError` if neither legal nor a null move.
Returns the move.


<EOL>zobrist_hash = self.board_zobrist_hash(array)<EOL>if array is None:<EOL><INDENT>array = DEFAULT_RANDOM_ARRAY<EOL><DEDENT>if self.turn == WHITE:<EOL><INDENT>zobrist_hash ^= array[<NUM_LIT>]<EOL><DEDENT>i = (<EOL>self.pieces_in_hand[BLACK][ROOK] * <NUM_LIT> +<EOL>self.pieces_in_hand[BLACK][BISHOP] * <NUM_LIT> +<EOL>self.pieces_in_hand[BLACK][GOLD] * <NUM_LIT> +<EOL>self.pieces_in_hand[BLACK][SILVER] * <NUM_LIT> +<EOL>self.pieces_in_hand[BLACK][KNIGHT] * <NUM_LIT> +<EOL>self.pieces_in_hand[BLACK][LANCE] * <NUM_LIT> +<EOL>self.pieces_in_hand[BLACK][PAWN])<EOL>bit = bit_scan(i)<EOL>while bit != -<NUM_LIT:1> and bit is not None:<EOL><INDENT>zobrist_hash ^= array[<NUM_LIT> + bit]<EOL>bit = bit_scan(i, bit + <NUM_LIT:1>)<EOL><DEDENT>return zobrist_hash<EOL>

Returns a Zobrist hash of the current position.


if self:<EOL><INDENT>if self.drop_piece_type:<EOL><INDENT>return '<STR_LIT>'.format(PIECE_SYMBOLS[self.drop_piece_type].upper(), SQUARE_NAMES[self.to_square])<EOL><DEDENT>else:<EOL><INDENT>return SQUARE_NAMES[self.from_square] + SQUARE_NAMES[self.to_square] +('<STR_LIT:+>' if self.promotion else '<STR_LIT>')<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>

Gets an USI string for the move.
For example a move from 7A to 8A would be `7a8a` or `7a8a+` if it is
a promotion.


if usi == '<STR_LIT>':<EOL><INDENT>return cls.null()<EOL><DEDENT>elif len(usi) == <NUM_LIT:4>:<EOL><INDENT>if usi[<NUM_LIT:1>] == '<STR_LIT:*>':<EOL><INDENT>piece = Piece.from_symbol(usi[<NUM_LIT:0>])<EOL>return cls(None, SQUARE_NAMES.index(usi[<NUM_LIT:2>:<NUM_LIT:4>]), False, piece.piece_type)<EOL><DEDENT>else:<EOL><INDENT>return cls(SQUARE_NAMES.index(usi[<NUM_LIT:0>:<NUM_LIT:2>]), SQUARE_NAMES.index(usi[<NUM_LIT:2>:<NUM_LIT:4>]))<EOL><DEDENT><DEDENT>elif len(usi) == <NUM_LIT:5> and usi[<NUM_LIT:4>] == '<STR_LIT:+>':<EOL><INDENT>return cls(SQUARE_NAMES.index(usi[<NUM_LIT:0>:<NUM_LIT:2>]), SQUARE_NAMES.index(usi[<NUM_LIT:2>:<NUM_LIT:4>]), True)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>

Parses an USI string.
Raises `ValueError` if the USI string is invalid.


return cls(None, None, NONE)<EOL>

Gets a null move.
A null move just passes the turn to the other side (and possibly
forfeits en-passant capturing). Null moves evaluate to `False` in
boolean contexts.
>>> bool(shogi.Move.null())
False


if cfgfile is not None:<EOL><INDENT>return cfgfile<EOL><DEDENT>cfg = '<STR_LIT>'<EOL>if not os.path.isfile(cfg):<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>return cfg<EOL>

Find the best match for the configuration file.


configobj.DEFAULT_INTERPOLATION = '<STR_LIT>'<EOL>cfgfile = configuration_file(cfgfile)<EOL>cfg = configobj.ConfigObj(cfgfile, configspec=cfgspec, encoding='<STR_LIT:utf-8>')<EOL>validator = Validator()<EOL>val = cfg.validate(validator)<EOL>if val is not True:<EOL><INDENT>raise ValueError('<STR_LIT>' % val)<EOL><DEDENT>if len(cfg['<STR_LIT>']['<STR_LIT>']) != len(cfg['<STR_LIT>']['<STR_LIT>']):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>globals()['<STR_LIT>'] = cfg<EOL>logger_init()<EOL>if cfg['<STR_LIT>'].get('<STR_LIT:url>', '<STR_LIT>').endswith('<STR_LIT:/>'):<EOL><INDENT>logger.warning('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>logger.info('<STR_LIT>' % cfgfile)<EOL>check()<EOL>return cfg<EOL>

Update configuration from file.

    :param cfgfile: Configuration file to load.


handlers = []<EOL>logconf = config('<STR_LIT>')<EOL>if logconf['<STR_LIT>']:<EOL><INDENT>handlers.append(logging.handlers.SysLogHandler(address='<STR_LIT>'))<EOL><DEDENT>if logconf['<STR_LIT>']:<EOL><INDENT>handlers.append(logging.StreamHandler(sys.stderr))<EOL><DEDENT>if logconf['<STR_LIT:file>']:<EOL><INDENT>handlers.append(logging.handlers.WatchedFileHandler(logconf['<STR_LIT:file>']))<EOL><DEDENT>for handler in handlers:<EOL><INDENT>handler.setFormatter(logging.Formatter(logconf['<STR_LIT>']))<EOL>logging.root.addHandler(handler)<EOL><DEDENT>logging.root.setLevel(logconf['<STR_LIT>'].upper())<EOL>logger.info('<STR_LIT>' % logconf['<STR_LIT>'])<EOL>

Initialize logger based on configuration


set_service_status(Service.AGENTSTATE, ServiceStatus.BUSY)<EOL>notify.notify('<STR_LIT>')<EOL>notify.notify('<STR_LIT>')<EOL>while not terminate():<EOL><INDENT>notify.notify('<STR_LIT>')<EOL>update_agent_state()<EOL>next_update = timestamp() + config()['<STR_LIT>']['<STR_LIT>']<EOL>while not terminate() and timestamp() < next_update:<EOL><INDENT>time.sleep(<NUM_LIT:0.1>)<EOL><DEDENT><DEDENT>logger.info('<STR_LIT>')<EOL>set_service_status(Service.AGENTSTATE, ServiceStatus.STOPPED)<EOL>

Main loop, updating the capture agent state.


print(USAGE % sys.argv[<NUM_LIT:0>])<EOL>sys.exit(retval)<EOL>

Print usage information to stdout and exit.


utils.terminate(True)<EOL>

Intercept sigint and terminate services gracefully.


sigint_handler(signum, frame)<EOL>for process in multiprocessing.active_children():<EOL><INDENT>process.terminate()<EOL><DEDENT>sys.exit(<NUM_LIT:0>)<EOL>

Intercept sigterm and terminate all processes.


processes = [multiprocessing.Process(target=mod.run) for mod in modules]<EOL>for p in processes:<EOL><INDENT>p.start()<EOL><DEDENT>for p in processes:<EOL><INDENT>p.join()<EOL><DEDENT>