async def execute_method(self, method, **params):

url = self.url_builder(method, url_params=params)<EOL>logger.info('<STR_LIT>', method)<EOL>response = await aiohttp.get(url)<EOL>logger.info('<STR_LIT>', response.status)<EOL>if response.status == <NUM_LIT:200>:<EOL><INDENT>json = await response.json()<EOL>logger.debug('<STR_LIT>', json)<EOL>if json.get('<STR_LIT>'):<EOL><INDENT>return json<EOL><DEDENT>raise SlackApiError(json['<STR_LIT:error>'])<EOL><DEDENT>else:<EOL><INDENT>raise_for_status(response)<EOL><DEDENT>

Execute a specified Slack Web API method.

        Arguments:
          method (:py:class:`str`): The name of the method.
          **params (:py:class:`dict`): Any additional parameters
            required.

        Returns:
          :py:class:`dict`: The JSON data from the response.

        Raises:
          :py:class:`aiohttp.web_exceptions.HTTPException`: If the HTTP
            request returns a code other than 200 (OK).
          SlackApiError: If the Slack API is reached but the response
           contains an error message.


methods = cls.API_METHODS<EOL>for key in method.split('<STR_LIT:.>'):<EOL><INDENT>methods = methods.get(key)<EOL>if methods is None:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if isinstance(methods, str):<EOL><INDENT>logger.debug('<STR_LIT>', method, methods)<EOL>return True<EOL><DEDENT>return False<EOL>

Whether a given method exists in the known API.

        Arguments:
          method (:py:class:`str`): The name of the method.

        Returns:
          :py:class:`bool`: Whether the method is in the known API.


def url_builder(self, endpoint, *, root=None, params=None, url_params=None):

if root is None:<EOL><INDENT>root = self.ROOT<EOL><DEDENT>scheme, netloc, path, _, _ = urlsplit(root)<EOL>return urlunsplit((<EOL>scheme,<EOL>netloc,<EOL>urljoin(path, endpoint),<EOL>urlencode(url_params or {}),<EOL>'<STR_LIT>',<EOL>)).format(**params or {})<EOL>

Create a URL for the specified endpoint.

        Arguments:
          endpoint (:py:class:`str`): The API endpoint to access.
          root: (:py:class:`str`, optional): The root URL for the
            service API.
          params: (:py:class:`dict`, optional): The values for format
            into the created URL (defaults to ``None``).
          url_params: (:py:class:`dict`, optional): Parameters to add
            to the end of the URL (defaults to ``None``).

        Returns:
          :py:class:`str`: The resulting URL.


return {}<EOL>

Get the headers for the service requests.

        Returns:
          :py:class:`dict`: The header mapping.


def truncate(text, max_len=<NUM_LIT>, end='<STR_LIT>'):

if len(text) <= max_len:<EOL><INDENT>return text<EOL><DEDENT>return text[:max_len].rsplit('<STR_LIT:U+0020>', maxsplit=<NUM_LIT:1>)[<NUM_LIT:0>] + end<EOL>

Truncate the supplied text for display.

    Arguments:
      text (:py:class:`str`): The text to truncate.
      max_len (:py:class:`int`, optional): The maximum length of the
        text before truncation (defaults to 350 characters).
      end (:py:class:`str`, optional): The ending to use to show that
        the text was truncated (defaults to ``'...'``).

    Returns:
      :py:class:`str`: The truncated text.


for err_name in web_exceptions.__all__:<EOL><INDENT>err = getattr(web_exceptions, err_name)<EOL>if err.status_code == response.status:<EOL><INDENT>payload = dict(<EOL>headers=response.headers,<EOL>reason=response.reason,<EOL>)<EOL>if issubclass(err, web_exceptions._HTTPMove):  <EOL><INDENT>raise err(response.headers['<STR_LIT>'], **payload)<EOL><DEDENT>raise err(**payload)<EOL><DEDENT><DEDENT>

Raise an appropriate error for a given response.

    Arguments:
      response (:py:class:`aiohttp.ClientResponse`): The API response.

    Raises:
      :py:class:`aiohttp.web_exceptions.HTTPException`: The appropriate
        error for the response's status.


if not values:<EOL><INDENT>return values<EOL><DEDENT>values = sorted(values)<EOL>first_word = next(<EOL>(cnt for cnt, val in enumerate(values)<EOL>if val and not val[<NUM_LIT:0>].isdigit()),<EOL>None<EOL>)<EOL>if first_word is None:<EOL><INDENT>return values<EOL><DEDENT>words = values[first_word:]<EOL>numbers = values[:first_word]<EOL>return words + numbers<EOL>

Sort values, but put numbers after alphabetically sorted words.

This function is here to make outputs diff-compatible with Aleph.

Example::
    >>> sorted(["b", "1", "a"])
    ['1', 'a', 'b']
    >>> resorted(["b", "1", "a"])
    ['a', 'b', '1']

Args:
    values (iterable): any iterable object/list/tuple/whatever.

Returns:
    list of sorted values, but with numbers after words


def get_subfields(self, datafield, subfield, i1=None, i2=None,<EOL>exception=False):

if len(datafield) != <NUM_LIT:3>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if len(subfield) != <NUM_LIT:1>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if datafield not in self.datafields:<EOL><INDENT>if exception:<EOL><INDENT>raise KeyError(datafield + "<STR_LIT>")<EOL><DEDENT>return []<EOL><DEDENT>output = []<EOL>for datafield in self.datafields[datafield]:<EOL><INDENT>if subfield not in datafield:<EOL><INDENT>continue<EOL><DEDENT>for sfield in datafield[subfield]:<EOL><INDENT>if i1 and sfield.i1 != i1:<EOL><INDENT>continue<EOL><DEDENT>if i2 and sfield.i2 != i2:<EOL><INDENT>continue<EOL><DEDENT>output.append(sfield)<EOL><DEDENT><DEDENT>if not output and exception:<EOL><INDENT>raise KeyError(subfield + "<STR_LIT>")<EOL><DEDENT>return output<EOL>

Return content of given `subfield` in `datafield`.

Args:
    datafield (str): Section name (for example "001", "100", "700").
    subfield (str):  Subfield name (for example "a", "1", etc..).
    i1 (str, default None): Optional i1/ind1 parameter value, which
       will be used for search.
    i2 (str, default None): Optional i2/ind2 parameter value, which
       will be used for search.
    exception (bool): If ``True``, :exc:`~exceptions.KeyError` is
              raised when method couldn't found given `datafield` /
              `subfield`. If ``False``, blank array ``[]`` is returned.

Returns:
    list: of :class:`.MARCSubrecord`.

Raises:
    KeyError: If the subfield or datafield couldn't be found.

Note:
    MARCSubrecord is practically same thing as string, but has defined
    :meth:`.MARCSubrecord.i1` and :attr:`.MARCSubrecord.i2`
    methods.

    You may need to be able to get this, because MARC XML depends on
    i/ind parameters from time to time (names of authors for example).


def _parse_control_fields(self, fields, tag_id="<STR_LIT>"):

for field in fields:<EOL><INDENT>params = field.params<EOL>if tag_id not in params:<EOL><INDENT>continue<EOL><DEDENT>self.controlfields[params[tag_id]] = field.getContent().strip()<EOL><DEDENT>

Parse control fields.

Args:
    fields (list): list of HTMLElements
    tag_id (str):  parameter name, which holds the information, about
                   field name this is normally "tag", but in case of
                   oai_marc "id".


def add_data_field(self, name, i1, i2, subfields_dict):

if i1 not in self.valid_i_chars:<EOL><INDENT>raise ValueError("<STR_LIT>" + i1 + "<STR_LIT>")<EOL><DEDENT>if i2 not in self.valid_i_chars:<EOL><INDENT>raise ValueError("<STR_LIT>" + i2 + "<STR_LIT>")<EOL><DEDENT>if len(name) != <NUM_LIT:3>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if not subfields_dict:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if not isinstance(subfields_dict, dict):<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>subrecords = []<EOL>for key, val in subfields_dict.items():<EOL><INDENT>if len(key) > <NUM_LIT:1>:<EOL><INDENT>raise KeyError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if not isinstance(val, list):<EOL><INDENT>val = [val]<EOL><DEDENT>subfields = map(<EOL>lambda x: MARCSubrecord(x, i1, i2, None),<EOL>val<EOL>)<EOL>subfields_dict[key] = subfields<EOL>subrecords.extend(subfields)<EOL><DEDENT>subfields_dict[self.i1_name] = i1<EOL>subfields_dict[self.i2_name] = i2<EOL>if name in self.datafields:<EOL><INDENT>self.datafields[name].append(subfields_dict)<EOL><DEDENT>else:<EOL><INDENT>self.datafields[name] = [subfields_dict]<EOL><DEDENT>other_subfields = self.datafields[name]<EOL>for record in subrecords:<EOL><INDENT>record.other_subfields = other_subfields<EOL><DEDENT>

Add new datafield into :attr:`datafields` and take care of OAI MARC
differencies.

Args:
    name (str): Name of datafield.
    i1 (char): Value of i1/ind1 parameter.
    i2 (char): Value of i2/ind2 parameter.
    subfields_dict (dict): Dictionary containing subfields (as list).

`subfields_dict` is expected to be in this format::

    {
        "field_id": ["subfield data",],
        ...
        "z": ["X0456b"]
    }

Warning:
    For your own good, use OrderedDict for `subfields_dict`, or
    constructor's `resort` parameter set to ``True`` (it is by
    default).

Warning:
    ``field_id`` can be only one character long!


if not isinstance(xml, HTMLElement):<EOL><INDENT>xml = dhtmlparser.parseString(str(xml))<EOL><DEDENT>record = xml.find("<STR_LIT>")<EOL>if not record:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>record = record[<NUM_LIT:0>]<EOL>self.oai_marc = len(record.find("<STR_LIT>")) > <NUM_LIT:0><EOL>if not self.oai_marc:<EOL><INDENT>leader = record.find("<STR_LIT>")<EOL>if len(leader) >= <NUM_LIT:1>:<EOL><INDENT>self.leader = leader[<NUM_LIT:0>].getContent()<EOL><DEDENT><DEDENT>if self.oai_marc:<EOL><INDENT>self._parse_control_fields(record.find("<STR_LIT>"), "<STR_LIT:id>")<EOL>self._parse_data_fields(record.find("<STR_LIT>"), "<STR_LIT:id>", "<STR_LIT:label>")<EOL><DEDENT>else:<EOL><INDENT>self._parse_control_fields(record.find("<STR_LIT>"), "<STR_LIT>")<EOL>self._parse_data_fields(record.find("<STR_LIT>"), "<STR_LIT>", "<STR_LIT:code>")<EOL><DEDENT>if self.oai_marc and "<STR_LIT>" in self.controlfields:<EOL><INDENT>self.leader = self.controlfields["<STR_LIT>"]<EOL><DEDENT>

Parse MARC XML document to dicts, which are contained in
self.controlfields and self.datafields.

Args:
    xml (str or HTMLElement): input data

Also detect if this is oai marc format or not (see elf.oai_marc).


def get_ctl_field(self, controlfield, alt=None):

if not alt:<EOL><INDENT>return self.controlfields[controlfield]<EOL><DEDENT>return self.controlfields.get(controlfield, alt)<EOL>

Method wrapper over :attr:`.controlfields` dictionary.

Args:
    controlfield (str): Name of the controlfield.
    alt (object, default None): Alternative value of the `controlfield`
        when `controlfield` couldn't be found.

Returns:
    str: record from given `controlfield`


internal_urls = self.get_subfields("<STR_LIT>", "<STR_LIT:u>", i1="<STR_LIT:4>", i2="<STR_LIT:0>")<EOL>internal_urls.extend(self.get_subfields("<STR_LIT>", "<STR_LIT:a>"))<EOL>internal_urls.extend(self.get_subfields("<STR_LIT>", "<STR_LIT:u>"))<EOL>return map(lambda x: x.replace("<STR_LIT>", "<STR_LIT:&>"), internal_urls)<EOL>

URL's, which may point to edeposit, aleph, kramerius and so on.

Fields ``856u40``, ``998a`` and ``URLu``.

Returns:
    list: List of internal URLs.


authors = self._parse_persons("<STR_LIT:100>", "<STR_LIT:a>")<EOL>authors += self._parse_persons("<STR_LIT>", "<STR_LIT:a>")<EOL>authors += self._parse_persons("<STR_LIT>", "<STR_LIT:a>")<EOL>authors += self._parse_persons("<STR_LIT>", "<STR_LIT:a>")<EOL>return authors<EOL>

Returns:
    list: Authors represented as :class:`.Person` objects.


invalid_issns = set(self.get_invalid_ISSNs())<EOL>return [<EOL>self._clean_isbn(issn)<EOL>for issn in self["<STR_LIT>"]<EOL>if self._clean_isbn(issn) not in invalid_issns<EOL>]<EOL>

Get list of VALID ISSNs (``022a``).

Returns:
    list: List with *valid* ISSN strings.


dates = self["<STR_LIT>"] + self["<STR_LIT>"]<EOL>def clean_date(date):<EOL><INDENT>"""<STR_LIT>"""<EOL>out = "<STR_LIT>"<EOL>was_digit = False<EOL>for c in date:<EOL><INDENT>if c.isdigit() or (c == "<STR_LIT:->" and was_digit) or c == "<STR_LIT:U+0020>":<EOL><INDENT>out += c<EOL><DEDENT>was_digit = c.isdigit()<EOL><DEDENT>return out<EOL><DEDENT>dates = set([<EOL>clean_date(date)<EOL>for date in self["<STR_LIT>"] + self["<STR_LIT>"]<EOL>])<EOL>return _undefined_pattern(<EOL>"<STR_LIT:U+002CU+0020>".join(dates),<EOL>lambda x: x.strip() == "<STR_LIT>",<EOL>undefined<EOL>)<EOL>

Args:
    undefined (optional): Argument, which will be returned if the
              `pub_date` record is not found.

Returns:
    str: Date of publication (month and year usually) or `undefined` \
         if `pub_date` is not found.


return isbn.strip().split("<STR_LIT:U+0020>", <NUM_LIT:1>)[<NUM_LIT:0>]<EOL>

Clean ISBN from other information (binding).


def _undefined_pattern(value, fn, undefined):

if fn(value):<EOL><INDENT>return undefined<EOL><DEDENT>return value<EOL>

If ``fn(value) == True``, return `undefined`, else `value`.


return self.get_pub_type() == PublicationType.continuing<EOL>

Returns:
    bool: True if the record is continuing.


INFO_CHAR_INDEX = <NUM_LIT:6><EOL>SECOND_INFO_CHAR_I = <NUM_LIT><EOL>if not len(self.leader) >= INFO_CHAR_INDEX + <NUM_LIT:1>:<EOL><INDENT>return PublicationType.monographic<EOL><DEDENT>if self.controlfields.get("<STR_LIT>") == "<STR_LIT>":<EOL><INDENT>return PublicationType.continuing<EOL><DEDENT>info_char = self.leader[INFO_CHAR_INDEX]<EOL>multipart_n = self.get_subfields("<STR_LIT>", "<STR_LIT:n>", exception=False)<EOL>multipart_p = self.get_subfields("<STR_LIT>", "<STR_LIT:p>", exception=False)<EOL>if info_char in "<STR_LIT>":<EOL><INDENT>return PublicationType.monographic<EOL><DEDENT>elif info_char in "<STR_LIT>":<EOL><INDENT>return PublicationType.continuing<EOL><DEDENT>elif info_char == "<STR_LIT:m>" and (multipart_n or multipart_p):<EOL><INDENT>return PublicationType.multipart_monograph<EOL><DEDENT>elif info_char == "<STR_LIT:m>" and len(self.leader) >= SECOND_INFO_CHAR_I + <NUM_LIT:1>:<EOL><INDENT>if self.leader[SECOND_INFO_CHAR_I] == "<STR_LIT:a>":<EOL><INDENT>return PublicationType.multipart_monograph<EOL><DEDENT>elif self.leader[SECOND_INFO_CHAR_I] == "<STR_LIT:U+0020>":<EOL><INDENT>return PublicationType.single_unit<EOL><DEDENT><DEDENT>return PublicationType.monographic<EOL>

Returns:
    PublicationType: :class:`.PublicationType` enum **value**.


return _undefined_pattern(<EOL>"<STR_LIT>".join(self.get_subfields("<STR_LIT>", "<STR_LIT:f>")),<EOL>lambda x: x.strip() == "<STR_LIT>",<EOL>undefined<EOL>)<EOL>

Args:
    undefined (optional): Argument, which will be returned if the
              `pub_order` record is not found.

Returns:
    str: Information about order in which was the book published or \
         `undefined` if `pub_order` is not found.


return self.get_corporations(roles=["<STR_LIT>"])<EOL>

Returns:
    list: Distributors represented as :class:`.Corporation` object.


try:<EOL><INDENT>val = self[item]<EOL><DEDENT>except ValueError:<EOL><INDENT>return alt<EOL><DEDENT>return val if val is not None else alt<EOL>

Standard dict-like .get() method.

Args:
    item (str): See :meth:`.__getitem__` for details.
    alt (default None): Alternative value, if item is not found.

Returns:
    obj: `item` or `alt`, if item is not found.


invalid_isbns = set(self.get_invalid_ISBNs())<EOL>valid_isbns = [<EOL>self._clean_isbn(isbn)<EOL>for isbn in self["<STR_LIT>"]<EOL>if self._clean_isbn(isbn) not in invalid_isbns<EOL>]<EOL>if valid_isbns:<EOL><INDENT>return valid_isbns<EOL><DEDENT>return [<EOL>self._clean_isbn(isbn)<EOL>for isbn in self["<STR_LIT>"]<EOL>]<EOL>

Get list of VALID ISBN.

Returns:
    list: List with *valid* ISBN strings.


def _parse_persons(self, datafield, subfield, roles=["<STR_LIT>"]):

<EOL>parsed_persons = []<EOL>raw_persons = self.get_subfields(datafield, subfield)<EOL>for person in raw_persons:<EOL><INDENT>other_subfields = person.other_subfields<EOL>if "<STR_LIT:4>" in other_subfields and roles != ["<STR_LIT>"]:<EOL><INDENT>person_roles = other_subfields["<STR_LIT:4>"]  <EOL>relevant = any(map(lambda role: role in roles, person_roles))<EOL>if not relevant:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>ind1 = person.i1<EOL>ind2 = person.i2<EOL>person = person.strip()<EOL>name = "<STR_LIT>"<EOL>second_name = "<STR_LIT>"<EOL>surname = "<STR_LIT>"<EOL>title = "<STR_LIT>"<EOL>if ind1 == "<STR_LIT:1>" and ind2 == "<STR_LIT:U+0020>":<EOL><INDENT>if "<STR_LIT:U+002C>" in person:<EOL><INDENT>surname, name = person.split("<STR_LIT:U+002C>", <NUM_LIT:1>)<EOL><DEDENT>elif "<STR_LIT:U+0020>" in person:<EOL><INDENT>surname, name = person.split("<STR_LIT:U+0020>", <NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>surname = person<EOL><DEDENT>if "<STR_LIT:c>" in other_subfields:<EOL><INDENT>title = "<STR_LIT:U+002C>".join(other_subfields["<STR_LIT:c>"])<EOL><DEDENT><DEDENT>elif ind1 == "<STR_LIT:0>" and ind2 == "<STR_LIT:U+0020>":<EOL><INDENT>name = person.strip()<EOL>if "<STR_LIT:b>" in other_subfields:<EOL><INDENT>second_name = "<STR_LIT:U+002C>".join(other_subfields["<STR_LIT:b>"])<EOL><DEDENT>if "<STR_LIT:c>" in other_subfields:<EOL><INDENT>surname = "<STR_LIT:U+002C>".join(other_subfields["<STR_LIT:c>"])<EOL><DEDENT><DEDENT>elif ind1 == "<STR_LIT:1>" and ind2 == "<STR_LIT:0>" or ind1 == "<STR_LIT:0>" and ind2 == "<STR_LIT:0>":<EOL><INDENT>name = person.strip()<EOL>if "<STR_LIT:c>" in other_subfields:<EOL><INDENT>title = "<STR_LIT:U+002C>".join(other_subfields["<STR_LIT:c>"])<EOL><DEDENT><DEDENT>parsed_persons.append(<EOL>Person(<EOL>name.strip(),<EOL>second_name.strip(),<EOL>surname.strip(),<EOL>title.strip()<EOL>)<EOL>)<EOL><DEDENT>return parsed_persons<EOL>

Parse persons from given datafield.

Args:
    datafield (str): code of datafield ("010", "730", etc..)
    subfield (char):  code of subfield ("a", "z", "4", etc..)
    role (list of str): set to ["any"] for any role, ["aut"] for
         authors, etc.. For details see
         http://www.loc.gov/marc/relators/relaterm.html

Main records for persons are: "100", "600" and "700", subrecords "c".

Returns:
    list: Person objects.


publishers = set([<EOL>remove_hairs_fn(publisher)<EOL>for publisher in self["<STR_LIT>"] + self["<STR_LIT>"]<EOL>])<EOL>return _undefined_pattern(<EOL>"<STR_LIT:U+002CU+0020>".join(publishers),<EOL>lambda x: x.strip() == "<STR_LIT>",<EOL>undefined<EOL>)<EOL>

Args:
    undefined (optional): Argument, which will be returned if the
              `publisher` record is not found.

Returns:
    str: Name of the publisher ("``Grada``" for example) or \
         `undefined` if `publisher` is not found.


def _parse_corporations(self, datafield, subfield, roles=["<STR_LIT>"]):

if len(datafield) != <NUM_LIT:3>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if len(subfield) != <NUM_LIT:1>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>parsed_corporations = []<EOL>for corporation in self.get_subfields(datafield, subfield):<EOL><INDENT>other_subfields = corporation.other_subfields<EOL>if "<STR_LIT:4>" in other_subfields and roles != ["<STR_LIT>"]:<EOL><INDENT>corp_roles = other_subfields["<STR_LIT:4>"]  <EOL>relevant = any(map(lambda role: role in roles, corp_roles))<EOL>if not relevant:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>name = "<STR_LIT>"<EOL>place = "<STR_LIT>"<EOL>date = "<STR_LIT>"<EOL>name = corporation<EOL>if "<STR_LIT:c>" in other_subfields:<EOL><INDENT>place = "<STR_LIT:U+002C>".join(other_subfields["<STR_LIT:c>"])<EOL><DEDENT>if "<STR_LIT:d>" in other_subfields:<EOL><INDENT>date = "<STR_LIT:U+002C>".join(other_subfields["<STR_LIT:d>"])<EOL><DEDENT>parsed_corporations.append(Corporation(name, place, date))<EOL><DEDENT>return parsed_corporations<EOL>

Parse informations about corporations from given field identified
by `datafield` parameter.

Args:
    datafield (str): MARC field ID ("``110``", "``610``", etc..)
    subfield (str):  MARC subfield ID with name, which is typically
                     stored in "``a``" subfield.
    roles (str): specify which roles you need. Set to ``["any"]`` for
                 any role, ``["dst"]`` for distributors, etc.. For
                 details, see
                 http://www.loc.gov/marc/relators/relaterm.html

Returns:
    list: :class:`Corporation` objects.


return self.get_pub_type() == PublicationType.single_unit<EOL>

Returns:
    bool: True if the record is single unit.


return self.get_pub_type() == PublicationType.multipart_monograph<EOL>

Returns:
    bool: True if the record is multi_mono.


return _undefined_pattern(<EOL>"<STR_LIT>".join(self.get_subfields("<STR_LIT>", "<STR_LIT:n>")),<EOL>lambda x: x.strip() == "<STR_LIT>",<EOL>undefined<EOL>)<EOL>

Args:
    undefined (optional): Argument, which will be returned if the
              `part_name` record is not found.

Returns:
    str: Name of the part of the series. or `undefined` if `part_name`\
         is not found.


return [<EOL>self._clean_isbn(issn)<EOL>for issn in self["<STR_LIT>"] + self["<STR_LIT>"]<EOL>]<EOL>

Get list of invalid ISSNs (``022z`` + ``022y``).

Returns:
    list: List with INVALID ISSN strings.


if self._potential is not None and self._potential:<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT>

Getter for 'potential' property

Returns:
        bool: potential is required?


self._details = value<EOL>

Setter for 'details' property

Args:
        value (str): Issue's details


self._checker_name = value<EOL>

Setter for 'checker' property

Args:
        value (str): Issue's checker


return {<EOL>"<STR_LIT:name>": self.name,<EOL>"<STR_LIT:file>": self.file,<EOL>"<STR_LIT>": self.details,<EOL>"<STR_LIT>": self.severity,<EOL>"<STR_LIT>": self.potential,<EOL>"<STR_LIT>": self.checker<EOL>}<EOL>

Returns a dictionary with the class representation

Returns:
        dict: class representarion


return self._command<EOL>

Getter for 'command' property

Returns:
        str: Command to execute


process = Popen(self.command, stdout=PIPE, stderr=PIPE, shell=shell)<EOL>self.output, self.errors = process.communicate()<EOL>

Executes the command setted into class

Args:
        shell (boolean): Set True if command is a shell command. Default: True


return self._errors<EOL>

Getter for 'errors' property

Returns:
        str: Stderr content


self._output = None<EOL>self._errors = None<EOL>self._command = None<EOL>self.command = command<EOL>

Class constructor.

Args:
        command (str): Command to execute


if '<STR_LIT>' in value:<EOL><INDENT>value['<STR_LIT>'] = bool(value['<STR_LIT>'])<EOL><DEDENT>if '<STR_LIT>' in value:<EOL><INDENT>value['<STR_LIT>'] = [n.strip() for n in ast.literal_eval(value['<STR_LIT>'])]<EOL><DEDENT>return value<EOL>

Parse the config values

Args:
        value (dict): Dictionary which contains the checker config

Returns:
        dict: The checker config with parsed values


pass<EOL>

Abstract method. This method will be executed for subclass which not implemented his own method


return self._dao<EOL>

Getter for 'dao' property

Returns:
        atomshield.helpers.DAO: Instance of DAO class


return self._issues<EOL>

Getter for 'issues' property

Returns:
        list<atomshields.helpers.Issue>: List of instances of Issue class


return self._path<EOL>

Getter for 'path' property

Returns:
        str: Absolute path to scan


self._project = value<EOL>

Setter for 'project' property

Args:
        value (str): Project's name


function = """<STR_LIT>"""bash -c '<STR_LIT>'

Check if a software is installed into machine.

Args:
        value (str): Software's name

Returns:
        bool: True if the software is installed. False else


def execute(self, *args, **kwargs):<EOL><INDENT>try:<EOL><INDENT>print("<STR_LIT>".format(n=self.__class__.NAME))<EOL>if hasattr(self, '<STR_LIT:test>'):<EOL><INDENT>if self.test():<EOL><INDENT>return func(self, *args, **kwargs)<EOL><DEDENT>else:<EOL><INDENT>print(colored("<STR_LIT>".format(c=self.__class__.__name__), "<STR_LIT>"))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return func(self, *args, **kwargs)<EOL><DEDENT><DEDENT>except Exception as e:<EOL><INDENT>print(colored("<STR_LIT>".format(n=self.__class__.NAME, e = e), "<STR_LIT>"))<EOL><DEDENT><DEDENT>return execute<EOL>

Decorator for method run. This method will be execute before the execution
from the method with this decorator.


if self._issues is None:<EOL><INDENT>return []<EOL><DEDENT>return self._issues<EOL>

Getter for 'issues' property

Returns:
        list: List of `Issue` instances


self._issues = value<EOL>

Setter for 'issues' property

Args:
        value (list): List of `Issue` instances


return self._config<EOL>

Getter for 'config' property

Returns:
        dict: Dictionary which contains the current values for this report config


<EOL>if not os.path.isdir(AtomShieldsScanner.CHECKERS_DIR):<EOL><INDENT>os.makedirs(AtomShieldsScanner.CHECKERS_DIR)<EOL><DEDENT>if not os.path.isdir(AtomShieldsScanner.REPORTS_DIR):<EOL><INDENT>os.makedirs(AtomShieldsScanner.REPORTS_DIR)<EOL><DEDENT>for f in AtomShieldsScanner._getFiles(os.path.join(os.path.dirname(os.path.realpath(__file__)), "<STR_LIT>"), "<STR_LIT>"):<EOL><INDENT>AtomShieldsScanner.installChecker(f)<EOL><DEDENT>for f in AtomShieldsScanner._getFiles(os.path.join(os.path.dirname(os.path.realpath(__file__)), "<STR_LIT>"), "<STR_LIT>"):<EOL><INDENT>AtomShieldsScanner.installReport(f)<EOL><DEDENT>AtomShieldsScanner._executeMassiveMethod(path=AtomShieldsScanner.CHECKERS_DIR, method="<STR_LIT>", args={})<EOL>config_dir = os.path.dirname(AtomShieldsScanner.CONFIG_PATH)<EOL>if not os.path.isdir(config_dir):<EOL><INDENT>os.makedirs(config_dir)<EOL><DEDENT>

Creates required directories and copy checkers and reports.


self._config_file = os.path.abspath(value)<EOL>

Setter for 'configFile' property

Args:
        value (str): Path to config file


return self._issues<EOL>

Getter for 'issues' property

Returns:
        list: List of Issue instances


self._config = value<EOL>

Setter for 'config' property

Args:
        value (dict): Dictionary which contains the config


@staticmethod<EOL><INDENT>def _addConfig(instance, config, parent_section):<DEDENT>

try:<EOL><INDENT>section_name = "<STR_LIT>".format(p = parent_section, n=instance.NAME.lower())<EOL>config.add_section(section_name)<EOL>for k in list(instance.CONFIG.keys()):<EOL><INDENT>config.set(section_name, k, instance.CONFIG[k])<EOL><DEDENT><DEDENT>except Exception as e:<EOL><INDENT>print("<STR_LIT>" % e)<EOL><DEDENT>

Writes a section for a plugin.

Args:
        instance (object): Class instance for plugin
        config (object): Object (ConfigParser) which the current config
        parent_section (str): Parent section for plugin. Usually 'checkers' or 'reports'


self._issues = value<EOL>

Setter for 'issues' property

Args:
        value (list): List of Issue instances


self._path = os.path.abspath(value)<EOL>

Setter for 'path' property

Args:
        value (str): Path to target directory


@staticmethod<EOL><INDENT>def _debug(message, color=None, attrs=None):<DEDENT>

if attrs is None:<EOL><INDENT>attrs = []<EOL><DEDENT>if color is not None:<EOL><INDENT>print(colored(message, color, attrs=attrs))<EOL><DEDENT>else:<EOL><INDENT>if len(attrs) > <NUM_LIT:0>:<EOL><INDENT>print(colored(message, "<STR_LIT>", attrs=attrs))<EOL><DEDENT>else:<EOL><INDENT>print(message)<EOL><DEDENT><DEDENT>

Print a message if the class attribute 'verbose' is enabled

Args:
        message (str): Message to print


return self._config_file<EOL>

Getter for 'configFile' property

Returns:
        str: Path to config file


data = {}<EOL>if section is None:<EOL><INDENT>for s in self.config.sections():<EOL><INDENT>if '<STR_LIT:/>' in s:<EOL><INDENT>parent, _s = s.split('<STR_LIT:/>')<EOL>data[parent][_s] = dict(self.config.items(s))<EOL><DEDENT>else:<EOL><INDENT>data[s] = dict(self.config.items(s))<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>data = dict(self.config.items(section))<EOL><DEDENT>return data<EOL>

Returns a dictionary which contains the current config. If a section is setted,
only will returns the section config

Args:
        section (str): (Optional) Section name.

Returns:
        dict: Representation of current config


def initialize(self, templates_path, global_data):

self.env = Environment(loader=FileSystemLoader(templates_path))<EOL>self.env.trim_blocks = True<EOL>self.global_data = global_data<EOL>

initialize with templates' path
        parameters
          templates_path    str    the position of templates directory
          global_data       dict   globa data can be got in any templates


def render_to(self, path, template, **data):

html = self.render(template, **data)<EOL>with open(path, '<STR_LIT:w>') as f:<EOL><INDENT>f.write(html.encode(charset))<EOL><DEDENT>

Render data with template and then write to path


if not lang:<EOL><INDENT>return self._code_no_lexer(text)<EOL><DEDENT>try:<EOL><INDENT>lexer = get_lexer_by_name(lang, stripall=True)<EOL><DEDENT>except ClassNotFound:  <EOL><INDENT>return self._code_no_lexer(text)<EOL><DEDENT>formatter = HtmlFormatter()<EOL>return highlight(text, lexer, formatter)<EOL>


rt, title, title_pic, markdown = libparser.parse(source)<EOL>if rt == -<NUM_LIT:1>:<EOL><INDENT>raise SeparatorNotFound<EOL><DEDENT>elif rt == -<NUM_LIT:2>:<EOL><INDENT>raise PostTitleNotFound<EOL><DEDENT>title, title_pic, markdown = map(to_unicode, (title, title_pic,<EOL>markdown))<EOL>html = self.markdown.render(markdown)<EOL>summary = self.markdown.render(markdown[:<NUM_LIT:200>])<EOL>return {<EOL>'<STR_LIT:title>': title,<EOL>'<STR_LIT>': markdown,<EOL>'<STR_LIT:html>': html,<EOL>'<STR_LIT>': summary,<EOL>'<STR_LIT>': title_pic<EOL>}<EOL>


self.watcher.start()<EOL>self.run_server(port)<EOL>


if not exists(Post.src_dir):<EOL><INDENT>logger.error(SourceDirectoryNotFound.__doc__)<EOL>sys.exit(SourceDirectoryNotFound.exit_code)<EOL><DEDENT>paths = []<EOL>for fn in ls(Post.src_dir):<EOL><INDENT>if fn.endswith(src_ext):<EOL><INDENT>paths.append(join(Post.src_dir, fn))<EOL><DEDENT><DEDENT>if exists(config.filepath):<EOL><INDENT>paths.append(config.filepath)<EOL><DEDENT>files = dict((p, stat(p).st_mtime) for p in paths)<EOL>return files<EOL>


try:<EOL><INDENT>while <NUM_LIT:1>:<EOL><INDENT>sleep(<NUM_LIT:1>)  <EOL>try:<EOL><INDENT>files_stat = self.get_files_stat()<EOL><DEDENT>except SystemExit:<EOL><INDENT>logger.error("<STR_LIT>")<EOL>self.shutdown_server()<EOL><DEDENT>if self.files_stat != files_stat:<EOL><INDENT>logger.info("<STR_LIT>")<EOL>try:<EOL><INDENT>generator.re_generate()<EOL>global _root<EOL>_root = generator.root<EOL><DEDENT>except SystemExit:  <EOL><INDENT>logger.error("<STR_LIT>")<EOL>self.shutdown_server()<EOL><DEDENT>self.files_stat = files_stat  <EOL><DEDENT><DEDENT><DEDENT>except KeyboardInterrupt:<EOL><INDENT>logger.info("<STR_LIT>")<EOL>self.shutdown_watcher()<EOL><DEDENT>

watch files for changes, if changed, rebuild blog. this thread
        will quit if the main process ends


try:<EOL><INDENT>self.server = MultiThreadedHTTPServer(('<STR_LIT>', port), Handler)<EOL><DEDENT>except socket.error as e:  <EOL><INDENT>logger.error(str(e))<EOL>sys.exit(<NUM_LIT:1>)<EOL><DEDENT>logger.info("<STR_LIT>"<EOL>% port)<EOL>try:<EOL><INDENT>self.server.serve_forever()<EOL><DEDENT>except KeyboardInterrupt:<EOL><INDENT>logger.info("<STR_LIT>")<EOL>self.shutdown_server()<EOL><DEDENT>


<EOL>try:<EOL><INDENT>conf = config.parse()<EOL><DEDENT>except ConfigSyntaxError as e:<EOL><INDENT>logger.error(e.__doc__)<EOL>sys.exit(e.exit_code)<EOL><DEDENT>update_nested_dict(self.config, conf)<EOL>self.blog.__dict__.update(self.config['<STR_LIT>'])<EOL>self.author.__dict__.update(self.config['<STR_LIT>'])<EOL>self.root = self.config['<STR_LIT:root>']<EOL>templates = join(self.blog.theme, '<STR_LIT>')  <EOL>jinja2_global_data = {<EOL>'<STR_LIT>': self.blog,<EOL>'<STR_LIT>': self.author,<EOL>'<STR_LIT>': self.config,<EOL>'<STR_LIT:root>': self.root<EOL>}<EOL>renderer.initialize(templates, jinja2_global_data)<EOL>logger.success('<STR_LIT>')<EOL>

Initialize configuration and renderer environment


return os.path.normpath(os.path.join(*p))<EOL>


rt = libparser.parse(byref(post), src)<EOL>return (<EOL>rt,<EOL>string_at(post.title, post.tsz),<EOL>string_at(post.tpic, post.tpsz),<EOL>post.body<EOL>)<EOL>


p =  os.path.join(os.getcwd(), '<STR_LIT>', '<STR_LIT>')<EOL>return content.replace('<STR_LIT>', p)<EOL>

Replace '../' leaded url with absolute uri.


try:<EOL><INDENT>return renderer.render(template, **data)<EOL><DEDENT>except JinjaTemplateNotFound as e:<EOL><INDENT>logger.error(e.__doc__ + '<STR_LIT>' % template)<EOL>sys.exit(e.exit_code)<EOL><DEDENT>

shortcut to render data with `template`. Just add exception
    catch to `renderer.render`


path = os.path.join(os.path.dirname(__file__), "<STR_LIT>", filename)<EOL>with open(path) as json_data:<EOL><INDENT>return json.load(json_data)<EOL><DEDENT>


return int(round_half(((fahrenheit - <NUM_LIT>) * <NUM_LIT>) + <NUM_LIT>))<EOL>

Convert Fahrenheit to a temperature value that NuHeat understands
Formula f(x) = ((x - 33) * 56) + 33

:param fahrenheit: The temperature to convert to NuHeat


fahrenheit = nuheat_to_fahrenheit(nuheat_temperature)<EOL>return fahrenheit_to_celsius(fahrenheit)<EOL>

Convert the NuHeat temp value to Celsius

:param nuheat_temperature: The temperature to convert to Celsius


fahrenheit = celsius_to_fahrenheit(celsius)<EOL>return int(round_half(((fahrenheit - <NUM_LIT>) * <NUM_LIT>) + <NUM_LIT>))<EOL>

Convert Celsius to a temperature value that NuHeat understands
Formula f(x) = ((x - 33) * 56) + 33

:param celsius: The temperature to convert to NuHeat


return int(round_half(celsius * <NUM_LIT> + <NUM_LIT:32>))<EOL>

Convert Celsius to Fahrenheit

:param celsius: The temperature to convert to Fahrenheit


return self._schedule_mode<EOL>

Return the mode that the thermostat is currently using


if not self.max_temperature:<EOL><INDENT>return None<EOL><DEDENT>return nuheat_to_fahrenheit(self.max_temperature)<EOL>

Return the thermostat's maximum temperature in Fahrenheit


if not self.temperature:<EOL><INDENT>return None<EOL><DEDENT>return nuheat_to_fahrenheit(self.temperature)<EOL>

Return the current temperature in Fahrenheit


def set_target_celsius(self, celsius, mode=config.SCHEDULE_HOLD):

temperature = celsius_to_nuheat(celsius)<EOL>self.set_target_temperature(temperature, mode)<EOL>

Set the target temperature to the desired celsius, with more granular control of the hold
mode

:param celsius: The desired temperature in C
:param mode: The desired mode to operate in


params = {<EOL>"<STR_LIT>": self.serial_number<EOL>}<EOL>self._session.request(config.THERMOSTAT_URL, method="<STR_LIT:POST>", data=post_data, params=params)<EOL>

Update (patch) the current instance's data on the NuHeat API


modes = [config.SCHEDULE_RUN, config.SCHEDULE_TEMPORARY_HOLD, config.SCHEDULE_HOLD]<EOL>if mode not in modes:<EOL><INDENT>raise Exception("<STR_LIT>".format(modes))<EOL><DEDENT>self.set_data({"<STR_LIT>": mode})<EOL>

Set the thermostat mode

:param mode: The desired mode integer value.
             Auto = 1
             Temporary hold = 2
             Permanent hold = 3


if not self.temperature:<EOL><INDENT>return None<EOL><DEDENT>return nuheat_to_celsius(self.temperature)<EOL>

Return the current temperature in Celsius


if not self.min_temperature:<EOL><INDENT>return None<EOL><DEDENT>return nuheat_to_celsius(self.min_temperature)<EOL>

Return the thermostat's minimum temperature in Celsius


self.set_target_fahrenheit(fahrenheit)<EOL>

Helper to set and HOLD the target temperature to the desired fahrenheit

:param fahrenheit: The desired temperature in F


def set_target_fahrenheit(self, fahrenheit, mode=config.SCHEDULE_HOLD):

temperature = fahrenheit_to_nuheat(fahrenheit)<EOL>self.set_target_temperature(temperature, mode)<EOL>

Set the target temperature to the desired fahrenheit, with more granular control of the
hold mode

:param fahrenheit: The desired temperature in F
:param mode: The desired mode to operate in


return NuHeatThermostat(self, serial_number)<EOL>

Get a thermostat object by serial number

:param serial_number: The serial number / ID of the desired thermostat


def request(self, url, method="<STR_LIT:GET>", data=None, params=None, retry=True):

headers = config.REQUEST_HEADERS<EOL>if params and self._session_id:<EOL><INDENT>params['<STR_LIT>'] = self._session_id<EOL><DEDENT>if method == "<STR_LIT:GET>":<EOL><INDENT>response = requests.get(url, headers=headers, params=params)<EOL><DEDENT>elif method == "<STR_LIT:POST>":<EOL><INDENT>response = requests.post(url, headers=headers, params=params, data=data)<EOL><DEDENT>if response.status_code == <NUM_LIT> and retry:<EOL><INDENT>_LOGGER.warn("<STR_LIT>")<EOL>self._session_id = None<EOL>self.authenticate()<EOL>return self.request(url, method=method, data=data, params=params, retry=False)<EOL><DEDENT>response.raise_for_status()<EOL>try:<EOL><INDENT>return response.json()<EOL><DEDENT>except ValueError:<EOL><INDENT>return response<EOL><DEDENT>

Make a request to the NuHeat API

:param url: The URL to request
:param method: The type of request to make (GET, POST)
:param data: Data to be sent along with POST requests
:param params: Querystring parameters
:param retry: Attempt to re-authenticate and retry request if necessary


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

View, only not looked between yesterday and tomorrow.


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

View, locked by the decorator with extra context.


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

View, locked by the decorator with access for known users only.


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


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

View, locked except for the configured IP-address.


if self.cleaned_data['<STR_LIT>'] == <NUM_LIT>:<EOL><INDENT>return <NUM_LIT><EOL><DEDENT>raise forms.ValidationError('<STR_LIT>')<EOL>

Clean the answer field, by checking its value.


super(MiddlewareTests, self).setUp()<EOL>self._old_middleware_classes = django_settings.MIDDLEWARE<EOL>django_settings.MIDDLEWARE.append(<EOL>'<STR_LIT>',<EOL>)<EOL>


cleaned_data = super(AuthForm, self).clean()<EOL>user = self.get_user()<EOL>if self.staff_only and (not user or not user.is_staff):<EOL><INDENT>raise forms.ValidationError('<STR_LIT>')<EOL><DEDENT>if self.superusers_only and (not user or not user.is_superuser):<EOL><INDENT>raise forms.ValidationError('<STR_LIT>')<EOL><DEDENT>return cleaned_data<EOL>

When receiving the filled out form, check for valid access.


def __init__(self, passwords=None, *args, **kwargs):

super(LockdownForm, self).__init__(*args, **kwargs)<EOL>if passwords is None:<EOL><INDENT>passwords = settings.PASSWORDS<EOL><DEDENT>self.valid_passwords = passwords<EOL>

Initialize the form by setting the valid passwords.


value = self.cleaned_data.get('<STR_LIT:password>')<EOL>if value not in self.valid_passwords:<EOL><INDENT>raise forms.ValidationError('<STR_LIT>')<EOL><DEDENT>return value<EOL>


return bool(self.valid_passwords)<EOL>

Show the form if there are any valid passwords.


return self.cleaned_data['<STR_LIT:password>']<EOL>

Save the password as the authentication token.

        It's acceptable to store the password raw, as it is stored server-side
        in the user's session.


url = request.path<EOL>querystring = request.GET.copy()<EOL>if self.logout_key and self.logout_key in request.GET:<EOL><INDENT>del querystring[self.logout_key]<EOL><DEDENT>if querystring:<EOL><INDENT>url = '<STR_LIT>' % (url, querystring.urlencode())<EOL><DEDENT>return HttpResponseRedirect(url)<EOL>


def __init__(self, get_response=None, form=None, until_date=None,<EOL>after_date=None, logout_key=None, session_key=None,<EOL>url_exceptions=None, view_exceptions=None,<EOL>remote_addr_exceptions=None, trusted_proxies=None,<EOL>extra_context=None, **form_kwargs):

if logout_key is None:<EOL><INDENT>logout_key = settings.LOGOUT_KEY<EOL><DEDENT>if session_key is None:<EOL><INDENT>session_key = settings.SESSION_KEY<EOL><DEDENT>self.get_response = get_response<EOL>self.form = form<EOL>self.form_kwargs = form_kwargs<EOL>self.until_date = until_date<EOL>self.after_date = after_date<EOL>self.logout_key = logout_key<EOL>self.session_key = session_key<EOL>self.url_exceptions = url_exceptions<EOL>self.remote_addr_exceptions = remote_addr_exceptions<EOL>self.trusted_proxies = trusted_proxies<EOL>self.extra_context = extra_context<EOL>

Initialize the middleware, by setting the configuration values.


try:<EOL><INDENT>session = request.session<EOL><DEDENT>except AttributeError:<EOL><INDENT>raise ImproperlyConfigured('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>if settings.ENABLED is False:<EOL><INDENT>return None<EOL><DEDENT>if self.remote_addr_exceptions:<EOL><INDENT>remote_addr_exceptions = self.remote_addr_exceptions<EOL><DEDENT>else:<EOL><INDENT>remote_addr_exceptions = settings.REMOTE_ADDR_EXCEPTIONS<EOL><DEDENT>if remote_addr_exceptions:<EOL><INDENT>trusted_proxies = self.trusted_proxies or settings.TRUSTED_PROXIES<EOL>remote_addr = request.META.get('<STR_LIT>')<EOL>if remote_addr in remote_addr_exceptions:<EOL><INDENT>return None<EOL><DEDENT>if remote_addr in trusted_proxies:<EOL><INDENT>x_forwarded_for = request.META.get('<STR_LIT>')<EOL>if x_forwarded_for:<EOL><INDENT>remote_addr = x_forwarded_for.split('<STR_LIT:U+002C>')[-<NUM_LIT:1>].strip()<EOL>if remote_addr in remote_addr_exceptions:<EOL><INDENT>return None<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if self.url_exceptions:<EOL><INDENT>url_exceptions = compile_url_exceptions(self.url_exceptions)<EOL><DEDENT>else:<EOL><INDENT>url_exceptions = compile_url_exceptions(settings.URL_EXCEPTIONS)<EOL><DEDENT>for pattern in url_exceptions:<EOL><INDENT>if pattern.search(request.path):<EOL><INDENT>return None<EOL><DEDENT><DEDENT>try:<EOL><INDENT>resolved_path = resolve(request.path)<EOL><DEDENT>except Resolver404:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>if resolved_path.func in settings.VIEW_EXCEPTIONS:<EOL><INDENT>return None<EOL><DEDENT><DEDENT>if self.until_date:<EOL><INDENT>until_date = self.until_date<EOL><DEDENT>else:<EOL><INDENT>until_date = settings.UNTIL_DATE<EOL><DEDENT>if self.after_date:<EOL><INDENT>after_date = self.after_date<EOL><DEDENT>else:<EOL><INDENT>after_date = settings.AFTER_DATE<EOL><DEDENT>if until_date or after_date:<EOL><INDENT>locked_date = False<EOL>if until_date and datetime.datetime.now() < until_date:<EOL><INDENT>locked_date = True<EOL><DEDENT>if after_date and datetime.datetime.now() > after_date:<EOL><INDENT>locked_date = True<EOL><DEDENT>if not locked_date:<EOL><INDENT>return None<EOL><DEDENT><DEDENT>form_data = request.POST if request.method == '<STR_LIT:POST>' else None<EOL>if self.form:<EOL><INDENT>form_class = self.form<EOL><DEDENT>else:<EOL><INDENT>form_class = get_lockdown_form(settings.FORM)<EOL><DEDENT>form = form_class(data=form_data, **self.form_kwargs)<EOL>authorized = False<EOL>token = session.get(self.session_key)<EOL>if hasattr(form, '<STR_LIT>'):<EOL><INDENT>if form.authenticate(token):<EOL><INDENT>authorized = True<EOL><DEDENT><DEDENT>elif token is True:<EOL><INDENT>authorized = True<EOL><DEDENT>if authorized and self.logout_key and self.logout_key in request.GET:<EOL><INDENT>if self.session_key in session:<EOL><INDENT>del session[self.session_key]<EOL><DEDENT>querystring = request.GET.copy()<EOL>del querystring[self.logout_key]<EOL>return self.redirect(request)<EOL><DEDENT>if authorized:<EOL><INDENT>return None<EOL><DEDENT>if form.is_valid():<EOL><INDENT>if hasattr(form, '<STR_LIT>'):<EOL><INDENT>token = form.generate_token()<EOL><DEDENT>else:<EOL><INDENT>token = True<EOL><DEDENT>session[self.session_key] = token<EOL>return self.redirect(request)<EOL><DEDENT>page_data = {'<STR_LIT>': until_date, '<STR_LIT>': after_date}<EOL>if not hasattr(form, '<STR_LIT>') or form.show_form():<EOL><INDENT>page_data['<STR_LIT>'] = form<EOL><DEDENT>if self.extra_context:<EOL><INDENT>page_data.update(self.extra_context)<EOL><DEDENT>return render(request, '<STR_LIT>', page_data)<EOL>

Check if each request is allowed to access the current resource.


return decorator_from_middleware_with_args(LockdownMiddleware)(*args,<EOL>**kwargs)<EOL>

Define a decorator based on the LockdownMiddleware.

    This decorator takes the same arguments as the middleware, but allows a
    more granular locking than the middleware.


Add metadata to a file in the proto dataset.


Edit / show readme content.

    The readme content is descriptive metadata describing the dataset.


Add items and item metadata to a proto dataset.


if new_name != "<STR_LIT>":<EOL><INDENT>_validate_name(new_name)<EOL>try:<EOL><INDENT>dataset = dtoolcore.ProtoDataSet.from_uri(<EOL>uri=dataset_uri,<EOL>config_path=CONFIG_PATH<EOL>)<EOL><DEDENT>except dtoolcore.DtoolCoreTypeError:<EOL><INDENT>dataset = dtoolcore.DataSet.from_uri(<EOL>uri=dataset_uri,<EOL>config_path=CONFIG_PATH<EOL>)<EOL><DEDENT>dataset.update_name(new_name)<EOL><DEDENT>admin_metadata = dtoolcore._admin_metadata_from_uri(<EOL>uri=dataset_uri,<EOL>config_path=CONFIG_PATH<EOL>)<EOL>click.secho(admin_metadata["<STR_LIT:name>"])<EOL>

Report / update the name of the dataset.

It is only possible to update the name of a proto dataset,
i.e. a dataset that has not yet been frozen.


@click.command()<EOL>@click.option("<STR_LIT>", is_flag=True, help="<STR_LIT>")<EOL>@click.option("<STR_LIT>", "<STR_LIT>", is_flag=True, help="<STR_LIT>")<EOL>@dataset_uri_argument<EOL>@click.argument("<STR_LIT>")<EOL>def copy(resume, quiet, dataset_uri, dest_base_uri):

click.secho(<EOL>"<STR_LIT>",<EOL>fg="<STR_LIT>",<EOL>err=True<EOL>)<EOL>click.secho(<EOL>"<STR_LIT>",<EOL>fg="<STR_LIT>",<EOL>err=True<EOL>)<EOL>_copy(resume, quiet, dataset_uri, dest_base_uri)<EOL>

DEPRECATED: Copy a dataset to a different location.


proto_dataset = dtoolcore.ProtoDataSet.from_uri(<EOL>uri=proto_dataset_uri<EOL>)<EOL>_validate_and_put_readme(proto_dataset, input.read())<EOL>

Use YAML from a file or stdin to populate the readme.

    To stream content from stdin use "-", e.g.

    echo "desc: my data" | dtool readme write <DS_URI> -


@click.command()<EOL>@click.option("<STR_LIT>", "<STR_LIT>", is_flag=True, help="<STR_LIT>")<EOL>@click.argument("<STR_LIT:name>")<EOL>@click.argument("<STR_LIT>", default="<STR_LIT>")<EOL>@click.option("<STR_LIT>", "<STR_LIT>", type=click.Path(exists=True))<EOL>def create(quiet, name, base_uri, symlink_path):

_validate_name(name)<EOL>admin_metadata = dtoolcore.generate_admin_metadata(name)<EOL>parsed_base_uri = dtoolcore.utils.generous_parse_uri(base_uri)<EOL>if parsed_base_uri.scheme == "<STR_LIT>":<EOL><INDENT>if symlink_path is None:<EOL><INDENT>raise click.UsageError("<STR_LIT>")  <EOL><DEDENT><DEDENT>if symlink_path:<EOL><INDENT>base_uri = dtoolcore.utils.sanitise_uri(<EOL>"<STR_LIT>" + parsed_base_uri.path<EOL>)<EOL>parsed_base_uri = dtoolcore.utils.generous_parse_uri(base_uri)<EOL><DEDENT>proto_dataset = dtoolcore.generate_proto_dataset(<EOL>admin_metadata=admin_metadata,<EOL>base_uri=dtoolcore.utils.urlunparse(parsed_base_uri),<EOL>config_path=CONFIG_PATH)<EOL>if symlink_path:<EOL><INDENT>symlink_abspath = os.path.abspath(symlink_path)<EOL>proto_dataset._storage_broker.symlink_path = symlink_abspath<EOL><DEDENT>try:<EOL><INDENT>proto_dataset.create()<EOL><DEDENT>except dtoolcore.storagebroker.StorageBrokerOSError as err:<EOL><INDENT>raise click.UsageError(str(err))<EOL><DEDENT>proto_dataset.put_readme("<STR_LIT>")<EOL>if quiet:<EOL><INDENT>click.secho(proto_dataset.uri)<EOL><DEDENT>else:<EOL><INDENT>click.secho("<STR_LIT>", nl=False, fg="<STR_LIT>")<EOL>click.secho(proto_dataset.uri)<EOL>click.secho("<STR_LIT>")<EOL>step = <NUM_LIT:1><EOL>if parsed_base_uri.scheme != "<STR_LIT>":<EOL><INDENT>click.secho("<STR_LIT>".format(step))<EOL>click.secho(<EOL>"<STR_LIT>".format(proto_dataset.uri),<EOL>fg="<STR_LIT>")<EOL>if parsed_base_uri.scheme == "<STR_LIT:file>":<EOL><INDENT>data_path = proto_dataset._storage_broker._data_abspath<EOL>click.secho("<STR_LIT>")<EOL>click.secho(<EOL>"<STR_LIT>".format(data_path),<EOL>fg="<STR_LIT>"<EOL>)<EOL><DEDENT>step = step + <NUM_LIT:1><EOL><DEDENT>click.secho("<STR_LIT>".format(step))<EOL>click.secho(<EOL>"<STR_LIT>".format(proto_dataset.uri),<EOL>fg="<STR_LIT>")<EOL>step = step + <NUM_LIT:1><EOL>click.secho(<EOL>"<STR_LIT>".format(step)<EOL>)<EOL>click.secho("<STR_LIT>".format(proto_dataset.uri), fg="<STR_LIT>")<EOL><DEDENT>


if handle.find("<STR_LIT:\n>") != -<NUM_LIT:1>:<EOL><INDENT>return False<EOL><DEDENT>return True<EOL>

Return false if the handle is invalid.

    For example if the handle contains a newline.


return -tf.nn.softplus(-a)<EOL>


<EOL>with tf.device('<STR_LIT>'):<EOL><INDENT>def removeNone(tensor_list):<EOL><INDENT>local_list = []<EOL>for item in tensor_list:<EOL><INDENT>if item is not None:<EOL><INDENT>local_list.append(item)<EOL><DEDENT><DEDENT>return local_list<EOL><DEDENT>def copyStats(var_list):<EOL><INDENT>print("<STR_LIT>")<EOL>redundant_stats = {}<EOL>copied_list = []<EOL>for item in var_list:<EOL><INDENT>if item is not None:<EOL><INDENT>if item not in redundant_stats:<EOL><INDENT>if self._use_float64:<EOL><INDENT>redundant_stats[item] = tf.cast(<EOL>tf.identity(item), tf.float64)<EOL><DEDENT>else:<EOL><INDENT>redundant_stats[item] = tf.identity(item)<EOL><DEDENT><DEDENT>copied_list.append(redundant_stats[item])<EOL><DEDENT>else:<EOL><INDENT>copied_list.append(None)<EOL><DEDENT><DEDENT>return copied_list<EOL><DEDENT>stats_eigen = self.stats_eigen<EOL>computedEigen = {}<EOL>eigen_reverse_lookup = {}<EOL>updateOps = []<EOL>with tf.control_dependencies([]):<EOL><INDENT>for stats_var in stats_eigen:<EOL><INDENT>if stats_var not in computedEigen:<EOL><INDENT>eigens = tf.self_adjoint_eig(stats_var)<EOL>e = eigens[<NUM_LIT:0>]<EOL>Q = eigens[<NUM_LIT:1>]<EOL>if self._use_float64:<EOL><INDENT>e = tf.cast(e, tf.float32)<EOL>Q = tf.cast(Q, tf.float32)<EOL><DEDENT>updateOps.append(e)<EOL>updateOps.append(Q)<EOL>computedEigen[stats_var] = {'<STR_LIT:e>': e, '<STR_LIT>': Q}<EOL>eigen_reverse_lookup[e] = stats_eigen[stats_var]['<STR_LIT:e>']<EOL>eigen_reverse_lookup[Q] = stats_eigen[stats_var]['<STR_LIT>']<EOL><DEDENT><DEDENT><DEDENT>self.eigen_reverse_lookup = eigen_reverse_lookup<EOL>self.eigen_update_list = updateOps<EOL>if KFAC_DEBUG:<EOL><INDENT>self.eigen_update_list = [item for item in updateOps]<EOL>with tf.control_dependencies(updateOps):<EOL><INDENT>updateOps.append(tf.Print(tf.constant(<EOL><NUM_LIT:0.>), [tf.convert_to_tensor('<STR_LIT>')]))<EOL><DEDENT><DEDENT><DEDENT>return updateOps<EOL>

compute the eigen decomp using copied var stats to avoid concurrent read/write from other queue


episode_batch = {}<EOL>for key in episode.keys():<EOL><INDENT>val = np.array(episode[key]).copy()<EOL>episode_batch[key] = val.swapaxes(<NUM_LIT:0>, <NUM_LIT:1>)<EOL><DEDENT>return episode_batch<EOL>

Converts an episode to have the batch dimension in the major (first)
    dimension.


argspec = inspect.getfullargspec(method)<EOL>defaults = {}<EOL>if argspec.defaults is not None:<EOL><INDENT>defaults = dict(<EOL>zip(argspec.args[-len(argspec.defaults):], argspec.defaults))<EOL><DEDENT>if argspec.kwonlydefaults is not None:<EOL><INDENT>defaults.update(argspec.kwonlydefaults)<EOL><DEDENT>arg_names = argspec.args[<NUM_LIT:1>:]<EOL>@functools.wraps(method)<EOL>def wrapper(*positional_args, **keyword_args):<EOL><INDENT>self = positional_args[<NUM_LIT:0>]<EOL>args = defaults.copy()<EOL>for name, value in zip(arg_names, positional_args[<NUM_LIT:1>:]):<EOL><INDENT>args[name] = value<EOL><DEDENT>args.update(keyword_args)<EOL>self.__dict__.update(args)<EOL>return method(*positional_args, **keyword_args)<EOL><DEDENT>return wrapper<EOL>

Stores provided method args as instance attributes.


shape = episode_batch['<STR_LIT:u>'].shape<EOL>return shape[<NUM_LIT:0>] * shape[<NUM_LIT:1>]<EOL>

Number of transitions in a given episode batch.


mod_name, fn_name = spec.split('<STR_LIT::>')<EOL>module = importlib.import_module(mod_name)<EOL>fn = getattr(module, fn_name)<EOL>return fn<EOL>

Import a function identified by a string like "pkg.module:fn_name".


def __init__(self, buffer_shapes, size_in_transitions, T, sample_transitions):

self.buffer_shapes = buffer_shapes<EOL>self.size = size_in_transitions // T<EOL>self.T = T<EOL>self.sample_transitions = sample_transitions<EOL>self.buffers = {key: np.empty([self.size, *shape])<EOL>for key, shape in buffer_shapes.items()}<EOL>self.current_size = <NUM_LIT:0><EOL>self.n_transitions_stored = <NUM_LIT:0><EOL>self.lock = threading.Lock()<EOL>

Creates a replay buffer.

        Args:
            buffer_shapes (dict of ints): the shape for all buffers that are used in the replay
                buffer
            size_in_transitions (int): the size of the buffer, measured in transitions
            T (int): the time horizon for episodes
            sample_transitions (function): a function that samples from the replay buffer


buffers = {}<EOL>with self.lock:<EOL><INDENT>assert self.current_size > <NUM_LIT:0><EOL>for key in self.buffers.keys():<EOL><INDENT>buffers[key] = self.buffers[key][:self.current_size]<EOL><DEDENT><DEDENT>buffers['<STR_LIT>'] = buffers['<STR_LIT:o>'][:, <NUM_LIT:1>:, :]<EOL>buffers['<STR_LIT>'] = buffers['<STR_LIT>'][:, <NUM_LIT:1>:, :]<EOL>transitions = self.sample_transitions(buffers, batch_size)<EOL>for key in (['<STR_LIT:r>', '<STR_LIT>', '<STR_LIT>'] + list(self.buffers.keys())):<EOL><INDENT>assert key in transitions, "<STR_LIT>" % key<EOL><DEDENT>return transitions<EOL>

Returns a dict {key: array(batch_size x shapes[key])}


def __init__(self, size, eps=<NUM_LIT>, default_clip_range=np.inf, sess=None):

self.size = size<EOL>self.eps = eps<EOL>self.default_clip_range = default_clip_range<EOL>self.sess = sess if sess is not None else tf.get_default_session()<EOL>self.local_sum = np.zeros(self.size, np.float32)<EOL>self.local_sumsq = np.zeros(self.size, np.float32)<EOL>self.local_count = np.zeros(<NUM_LIT:1>, np.float32)<EOL>self.sum_tf = tf.get_variable(<EOL>initializer=tf.zeros_initializer(), shape=self.local_sum.shape, name='<STR_LIT>',<EOL>trainable=False, dtype=tf.float32)<EOL>self.sumsq_tf = tf.get_variable(<EOL>initializer=tf.zeros_initializer(), shape=self.local_sumsq.shape, name='<STR_LIT>',<EOL>trainable=False, dtype=tf.float32)<EOL>self.count_tf = tf.get_variable(<EOL>initializer=tf.ones_initializer(), shape=self.local_count.shape, name='<STR_LIT:count>',<EOL>trainable=False, dtype=tf.float32)<EOL>self.mean = tf.get_variable(<EOL>initializer=tf.zeros_initializer(), shape=(self.size,), name='<STR_LIT>',<EOL>trainable=False, dtype=tf.float32)<EOL>self.std = tf.get_variable(<EOL>initializer=tf.ones_initializer(), shape=(self.size,), name='<STR_LIT>',<EOL>trainable=False, dtype=tf.float32)<EOL>self.count_pl = tf.placeholder(name='<STR_LIT>', shape=(<NUM_LIT:1>,), dtype=tf.float32)<EOL>self.sum_pl = tf.placeholder(name='<STR_LIT>', shape=(self.size,), dtype=tf.float32)<EOL>self.sumsq_pl = tf.placeholder(name='<STR_LIT>', shape=(self.size,), dtype=tf.float32)<EOL>self.update_op = tf.group(<EOL>self.count_tf.assign_add(self.count_pl),<EOL>self.sum_tf.assign_add(self.sum_pl),<EOL>self.sumsq_tf.assign_add(self.sumsq_pl)<EOL>)<EOL>self.recompute_op = tf.group(<EOL>tf.assign(self.mean, self.sum_tf / self.count_tf),<EOL>tf.assign(self.std, tf.sqrt(tf.maximum(<EOL>tf.square(self.eps),<EOL>self.sumsq_tf / self.count_tf - tf.square(self.sum_tf / self.count_tf)<EOL>))),<EOL>)<EOL>self.lock = threading.Lock()<EOL>

A normalizer that ensures that observations are approximately distributed according to
        a standard Normal distribution (i.e. have mean zero and variance one).

        Args:
            size (int): the size of the observation to be normalized
            eps (float): a small constant that avoids underflows
            default_clip_range (float): normalized observations are clipped to be in
                [-default_clip_range, default_clip_range]
            sess (object): the TensorFlow session to be used


@store_args<EOL><INDENT>def __init__(self, venv, policy, dims, logger, T, rollout_batch_size=<NUM_LIT:1>,<EOL>exploit=False, use_target_net=False, compute_Q=False, noise_eps=<NUM_LIT:0>,<EOL>random_eps=<NUM_LIT:0>, history_len=<NUM_LIT:100>, render=False, monitor=False, **kwargs):<DEDENT>

assert self.T > <NUM_LIT:0><EOL>self.info_keys = [key.replace('<STR_LIT>', '<STR_LIT>') for key in dims.keys() if key.startswith('<STR_LIT>')]<EOL>self.success_history = deque(maxlen=history_len)<EOL>self.Q_history = deque(maxlen=history_len)<EOL>self.n_episodes = <NUM_LIT:0><EOL>self.reset_all_rollouts()<EOL>self.clear_history()<EOL>

Rollout worker generates experience by interacting with one or many environments.

        Args:
            make_env (function): a factory function that creates a new instance of the environment
                when called
            policy (object): the policy that is used to act
            dims (dict of ints): the dimensions for observations (o), goals (g), and actions (u)
            logger (object): the logger that is used by the rollout worker
            rollout_batch_size (int): the number of parallel rollouts that should be used
            exploit (boolean): whether or not to exploit, i.e. to act optimally according to the
                current policy without any exploration
            use_target_net (boolean): whether or not to use the target net for rollouts
            compute_Q (boolean): whether or not to compute the Q values alongside the actions
            noise_eps (float): scale of the additive Gaussian noise
            random_eps (float): probability of selecting a completely random action
            history_len (int): length of history for statistics smoothing
            render (boolean): whether or not to render the rollouts


self.success_history.clear()<EOL>self.Q_history.clear()<EOL>

Clears all histories that are used for statistics


self.reset_all_rollouts()<EOL>o = np.empty((self.rollout_batch_size, self.dims['<STR_LIT:o>']), np.float32)  <EOL>ag = np.empty((self.rollout_batch_size, self.dims['<STR_LIT:g>']), np.float32)  <EOL>o[:] = self.initial_o<EOL>ag[:] = self.initial_ag<EOL>obs, achieved_goals, acts, goals, successes = [], [], [], [], []<EOL>dones = []<EOL>info_values = [np.empty((self.T - <NUM_LIT:1>, self.rollout_batch_size, self.dims['<STR_LIT>' + key]), np.float32) for key in self.info_keys]<EOL>Qs = []<EOL>for t in range(self.T):<EOL><INDENT>policy_output = self.policy.get_actions(<EOL>o, ag, self.g,<EOL>compute_Q=self.compute_Q,<EOL>noise_eps=self.noise_eps if not self.exploit else <NUM_LIT:0.>,<EOL>random_eps=self.random_eps if not self.exploit else <NUM_LIT:0.>,<EOL>use_target_net=self.use_target_net)<EOL>if self.compute_Q:<EOL><INDENT>u, Q = policy_output<EOL>Qs.append(Q)<EOL><DEDENT>else:<EOL><INDENT>u = policy_output<EOL><DEDENT>if u.ndim == <NUM_LIT:1>:<EOL><INDENT>u = u.reshape(<NUM_LIT:1>, -<NUM_LIT:1>)<EOL><DEDENT>o_new = np.empty((self.rollout_batch_size, self.dims['<STR_LIT:o>']))<EOL>ag_new = np.empty((self.rollout_batch_size, self.dims['<STR_LIT:g>']))<EOL>success = np.zeros(self.rollout_batch_size)<EOL>obs_dict_new, _, done, info = self.venv.step(u)<EOL>o_new = obs_dict_new['<STR_LIT>']<EOL>ag_new = obs_dict_new['<STR_LIT>']<EOL>success = np.array([i.get('<STR_LIT>', <NUM_LIT:0.0>) for i in info])<EOL>if any(done):<EOL><INDENT>break<EOL><DEDENT>for i, info_dict in enumerate(info):<EOL><INDENT>for idx, key in enumerate(self.info_keys):<EOL><INDENT>info_values[idx][t, i] = info[i][key]<EOL><DEDENT><DEDENT>if np.isnan(o_new).any():<EOL><INDENT>self.logger.warn('<STR_LIT>')<EOL>self.reset_all_rollouts()<EOL>return self.generate_rollouts()<EOL><DEDENT>dones.append(done)<EOL>obs.append(o.copy())<EOL>achieved_goals.append(ag.copy())<EOL>successes.append(success.copy())<EOL>acts.append(u.copy())<EOL>goals.append(self.g.copy())<EOL>o[...] = o_new<EOL>ag[...] = ag_new<EOL><DEDENT>obs.append(o.copy())<EOL>achieved_goals.append(ag.copy())<EOL>episode = dict(o=obs,<EOL>u=acts,<EOL>g=goals,<EOL>ag=achieved_goals)<EOL>for key, value in zip(self.info_keys, info_values):<EOL><INDENT>episode['<STR_LIT>'.format(key)] = value<EOL><DEDENT>successful = np.array(successes)[-<NUM_LIT:1>, :]<EOL>assert successful.shape == (self.rollout_batch_size,)<EOL>success_rate = np.mean(successful)<EOL>self.success_history.append(success_rate)<EOL>if self.compute_Q:<EOL><INDENT>self.Q_history.append(np.mean(Qs))<EOL><DEDENT>self.n_episodes += self.rollout_batch_size<EOL>return convert_episode_to_batch_major(episode)<EOL>

Performs `rollout_batch_size` rollouts in parallel for time horizon `T` with the current
        policy acting on it accordingly.


logs = []<EOL>logs += [('<STR_LIT>', np.mean(self.success_history))]<EOL>if self.compute_Q:<EOL><INDENT>logs += [('<STR_LIT>', np.mean(self.Q_history))]<EOL><DEDENT>logs += [('<STR_LIT>', self.n_episodes)]<EOL>if prefix != '<STR_LIT>' and not prefix.endswith('<STR_LIT:/>'):<EOL><INDENT>return [(prefix + '<STR_LIT:/>' + key, val) for key, val in logs]<EOL><DEDENT>else:<EOL><INDENT>return logs<EOL><DEDENT>

Generates a dictionary that contains all collected statistics.


with open(path, '<STR_LIT:wb>') as f:<EOL><INDENT>pickle.dump(self.policy, f)<EOL><DEDENT>

Pickles the current policy for later inspection.


super(PrioritizedReplayBuffer, self).__init__(size)<EOL>assert alpha >= <NUM_LIT:0><EOL>self._alpha = alpha<EOL>it_capacity = <NUM_LIT:1><EOL>while it_capacity < size:<EOL><INDENT>it_capacity *= <NUM_LIT:2><EOL><DEDENT>self._it_sum = SumSegmentTree(it_capacity)<EOL>self._it_min = MinSegmentTree(it_capacity)<EOL>self._max_priority = <NUM_LIT:1.0><EOL>

Create Prioritized Replay buffer.

        Parameters
        ----------
        size: int
            Max number of transitions to store in the buffer. When the buffer
            overflows the old memories are dropped.
        alpha: float
            how much prioritization is used
            (0 - no prioritization, 1 - full prioritization)

        See Also
        --------
        ReplayBuffer.__init__


self._storage = []<EOL>self._maxsize = size<EOL>self._next_idx = <NUM_LIT:0><EOL>

Create Replay buffer.

        Parameters
        ----------
        size: int
            Max number of transitions to store in the buffer. When the buffer
            overflows the old memories are dropped.


def update_priorities(self, idxes, priorities):

assert len(idxes) == len(priorities)<EOL>for idx, priority in zip(idxes, priorities):<EOL><INDENT>assert priority > <NUM_LIT:0><EOL>assert <NUM_LIT:0> <= idx < len(self._storage)<EOL>self._it_sum[idx] = priority ** self._alpha<EOL>self._it_min[idx] = priority ** self._alpha<EOL>self._max_priority = max(self._max_priority, priority)<EOL><DEDENT>

Update priorities of sampled transitions.

        sets priority of transition at index idxes[i] in buffer
        to priorities[i].

        Parameters
        ----------
        idxes: [int]
            List of idxes of sampled transitions
        priorities: [float]
            List of updated priorities corresponding to
            transitions at the sampled idxes denoted by
            variable `idxes`.


raise NotImplementedError<EOL>

Given data input it to the placeholder(s).


super().__init__(placeholder.name)<EOL>self._placeholder = placeholder<EOL>

Wrapper for regular tensorflow placeholder.


def build_train(make_obs_ph, q_func, num_actions, optimizer, grad_norm_clipping=None, gamma=<NUM_LIT:1.0>,<EOL>double_q=True, scope="<STR_LIT>", reuse=None, param_noise=False, param_noise_filter_func=None):

if param_noise:<EOL><INDENT>act_f = build_act_with_param_noise(make_obs_ph, q_func, num_actions, scope=scope, reuse=reuse,<EOL>param_noise_filter_func=param_noise_filter_func)<EOL><DEDENT>else:<EOL><INDENT>act_f = build_act(make_obs_ph, q_func, num_actions, scope=scope, reuse=reuse)<EOL><DEDENT>with tf.variable_scope(scope, reuse=reuse):<EOL><INDENT>obs_t_input = make_obs_ph("<STR_LIT>")<EOL>act_t_ph = tf.placeholder(tf.int32, [None], name="<STR_LIT:action>")<EOL>rew_t_ph = tf.placeholder(tf.float32, [None], name="<STR_LIT>")<EOL>obs_tp1_input = make_obs_ph("<STR_LIT>")<EOL>done_mask_ph = tf.placeholder(tf.float32, [None], name="<STR_LIT>")<EOL>importance_weights_ph = tf.placeholder(tf.float32, [None], name="<STR_LIT>")<EOL>q_t = q_func(obs_t_input.get(), num_actions, scope="<STR_LIT>", reuse=True)  <EOL>q_func_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=tf.get_variable_scope().name + "<STR_LIT>")<EOL>q_tp1 = q_func(obs_tp1_input.get(), num_actions, scope="<STR_LIT>")<EOL>target_q_func_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=tf.get_variable_scope().name + "<STR_LIT>")<EOL>q_t_selected = tf.reduce_sum(q_t * tf.one_hot(act_t_ph, num_actions), <NUM_LIT:1>)<EOL>if double_q:<EOL><INDENT>q_tp1_using_online_net = q_func(obs_tp1_input.get(), num_actions, scope="<STR_LIT>", reuse=True)<EOL>q_tp1_best_using_online_net = tf.argmax(q_tp1_using_online_net, <NUM_LIT:1>)<EOL>q_tp1_best = tf.reduce_sum(q_tp1 * tf.one_hot(q_tp1_best_using_online_net, num_actions), <NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>q_tp1_best = tf.reduce_max(q_tp1, <NUM_LIT:1>)<EOL><DEDENT>q_tp1_best_masked = (<NUM_LIT:1.0> - done_mask_ph) * q_tp1_best<EOL>q_t_selected_target = rew_t_ph + gamma * q_tp1_best_masked<EOL>td_error = q_t_selected - tf.stop_gradient(q_t_selected_target)<EOL>errors = U.huber_loss(td_error)<EOL>weighted_error = tf.reduce_mean(importance_weights_ph * errors)<EOL>if grad_norm_clipping is not None:<EOL><INDENT>gradients = optimizer.compute_gradients(weighted_error, var_list=q_func_vars)<EOL>for i, (grad, var) in enumerate(gradients):<EOL><INDENT>if grad is not None:<EOL><INDENT>gradients[i] = (tf.clip_by_norm(grad, grad_norm_clipping), var)<EOL><DEDENT><DEDENT>optimize_expr = optimizer.apply_gradients(gradients)<EOL><DEDENT>else:<EOL><INDENT>optimize_expr = optimizer.minimize(weighted_error, var_list=q_func_vars)<EOL><DEDENT>update_target_expr = []<EOL>for var, var_target in zip(sorted(q_func_vars, key=lambda v: v.name),<EOL>sorted(target_q_func_vars, key=lambda v: v.name)):<EOL><INDENT>update_target_expr.append(var_target.assign(var))<EOL><DEDENT>update_target_expr = tf.group(*update_target_expr)<EOL>train = U.function(<EOL>inputs=[<EOL>obs_t_input,<EOL>act_t_ph,<EOL>rew_t_ph,<EOL>obs_tp1_input,<EOL>done_mask_ph,<EOL>importance_weights_ph<EOL>],<EOL>outputs=td_error,<EOL>updates=[optimize_expr]<EOL>)<EOL>update_target = U.function([], [], updates=[update_target_expr])<EOL>q_values = U.function([obs_t_input], q_t)<EOL>return act_f, train, update_target, {'<STR_LIT>': q_values}<EOL><DEDENT>

Creates the train function:

    Parameters
    ----------
    make_obs_ph: str -> tf.placeholder or TfInput
        a function that takes a name and creates a placeholder of input with that name
    q_func: (tf.Variable, int, str, bool) -> tf.Variable
        the model that takes the following inputs:
            observation_in: object
                the output of observation placeholder
            num_actions: int
                number of actions
            scope: str
            reuse: bool
                should be passed to outer variable scope
        and returns a tensor of shape (batch_size, num_actions) with values of every action.
    num_actions: int
        number of actions
    reuse: bool
        whether or not to reuse the graph variables
    optimizer: tf.train.Optimizer
        optimizer to use for the Q-learning objective.
    grad_norm_clipping: float or None
        clip gradient norms to this value. If None no clipping is performed.
    gamma: float
        discount rate.
    double_q: bool
        if true will use Double Q Learning (https://arxiv.org/abs/1509.06461).
        In general it is a good idea to keep it enabled.
    scope: str or VariableScope
        optional scope for variable_scope.
    reuse: bool or None
        whether or not the variables should be reused. To be able to reuse the scope must be given.
    param_noise: bool
        whether or not to use parameter space noise (https://arxiv.org/abs/1706.01905)
    param_noise_filter_func: tf.Variable -> bool
        function that decides whether or not a variable should be perturbed. Only applicable
        if param_noise is True. If set to None, default_param_noise_filter is used by default.

    Returns
    -------
    act: (tf.Variable, bool, float) -> tf.Variable
        function to select and action given observation.
`       See the top of the file for details.
    train: (object, np.array, np.array, object, np.array, np.array) -> np.array
        optimize the error in Bellman's equation.
`       See the top of the file for details.
    update_target: () -> ()
        copy the parameters from optimized Q function to the target Q function.
`       See the top of the file for details.
    debug: {str: function}
        a bunch of functions to print debug data like q_values.


return tf.get_variable_scope().name<EOL>

Returns the name of current scope as a string, e.g. deepq/q_func


def build_act_with_param_noise(make_obs_ph, q_func, num_actions, scope="<STR_LIT>", reuse=None, param_noise_filter_func=None):

if param_noise_filter_func is None:<EOL><INDENT>param_noise_filter_func = default_param_noise_filter<EOL><DEDENT>with tf.variable_scope(scope, reuse=reuse):<EOL><INDENT>observations_ph = make_obs_ph("<STR_LIT>")<EOL>stochastic_ph = tf.placeholder(tf.bool, (), name="<STR_LIT>")<EOL>update_eps_ph = tf.placeholder(tf.float32, (), name="<STR_LIT>")<EOL>update_param_noise_threshold_ph = tf.placeholder(tf.float32, (), name="<STR_LIT>")<EOL>update_param_noise_scale_ph = tf.placeholder(tf.bool, (), name="<STR_LIT>")<EOL>reset_ph = tf.placeholder(tf.bool, (), name="<STR_LIT>")<EOL>eps = tf.get_variable("<STR_LIT>", (), initializer=tf.constant_initializer(<NUM_LIT:0>))<EOL>param_noise_scale = tf.get_variable("<STR_LIT>", (), initializer=tf.constant_initializer(<NUM_LIT>), trainable=False)<EOL>param_noise_threshold = tf.get_variable("<STR_LIT>", (), initializer=tf.constant_initializer(<NUM_LIT>), trainable=False)<EOL>q_values = q_func(observations_ph.get(), num_actions, scope="<STR_LIT>")<EOL>q_values_perturbed = q_func(observations_ph.get(), num_actions, scope="<STR_LIT>")<EOL>def perturb_vars(original_scope, perturbed_scope):<EOL><INDENT>all_vars = scope_vars(absolute_scope_name(original_scope))<EOL>all_perturbed_vars = scope_vars(absolute_scope_name(perturbed_scope))<EOL>assert len(all_vars) == len(all_perturbed_vars)<EOL>perturb_ops = []<EOL>for var, perturbed_var in zip(all_vars, all_perturbed_vars):<EOL><INDENT>if param_noise_filter_func(perturbed_var):<EOL><INDENT>op = tf.assign(perturbed_var, var + tf.random_normal(shape=tf.shape(var), mean=<NUM_LIT:0.>, stddev=param_noise_scale))<EOL><DEDENT>else:<EOL><INDENT>op = tf.assign(perturbed_var, var)<EOL><DEDENT>perturb_ops.append(op)<EOL><DEDENT>assert len(perturb_ops) == len(all_vars)<EOL>return tf.group(*perturb_ops)<EOL><DEDENT>q_values_adaptive = q_func(observations_ph.get(), num_actions, scope="<STR_LIT>")<EOL>perturb_for_adaption = perturb_vars(original_scope="<STR_LIT>", perturbed_scope="<STR_LIT>")<EOL>kl = tf.reduce_sum(tf.nn.softmax(q_values) * (tf.log(tf.nn.softmax(q_values)) - tf.log(tf.nn.softmax(q_values_adaptive))), axis=-<NUM_LIT:1>)<EOL>mean_kl = tf.reduce_mean(kl)<EOL>def update_scale():<EOL><INDENT>with tf.control_dependencies([perturb_for_adaption]):<EOL><INDENT>update_scale_expr = tf.cond(mean_kl < param_noise_threshold,<EOL>lambda: param_noise_scale.assign(param_noise_scale * <NUM_LIT>),<EOL>lambda: param_noise_scale.assign(param_noise_scale / <NUM_LIT>),<EOL>)<EOL><DEDENT>return update_scale_expr<EOL><DEDENT>update_param_noise_threshold_expr = param_noise_threshold.assign(tf.cond(update_param_noise_threshold_ph >= <NUM_LIT:0>,<EOL>lambda: update_param_noise_threshold_ph, lambda: param_noise_threshold))<EOL>deterministic_actions = tf.argmax(q_values_perturbed, axis=<NUM_LIT:1>)<EOL>batch_size = tf.shape(observations_ph.get())[<NUM_LIT:0>]<EOL>random_actions = tf.random_uniform(tf.stack([batch_size]), minval=<NUM_LIT:0>, maxval=num_actions, dtype=tf.int64)<EOL>chose_random = tf.random_uniform(tf.stack([batch_size]), minval=<NUM_LIT:0>, maxval=<NUM_LIT:1>, dtype=tf.float32) < eps<EOL>stochastic_actions = tf.where(chose_random, random_actions, deterministic_actions)<EOL>output_actions = tf.cond(stochastic_ph, lambda: stochastic_actions, lambda: deterministic_actions)<EOL>update_eps_expr = eps.assign(tf.cond(update_eps_ph >= <NUM_LIT:0>, lambda: update_eps_ph, lambda: eps))<EOL>updates = [<EOL>update_eps_expr,<EOL>tf.cond(reset_ph, lambda: perturb_vars(original_scope="<STR_LIT>", perturbed_scope="<STR_LIT>"), lambda: tf.group(*[])),<EOL>tf.cond(update_param_noise_scale_ph, lambda: update_scale(), lambda: tf.Variable(<NUM_LIT:0.>, trainable=False)),<EOL>update_param_noise_threshold_expr,<EOL>]<EOL>_act = U.function(inputs=[observations_ph, stochastic_ph, update_eps_ph, reset_ph, update_param_noise_threshold_ph, update_param_noise_scale_ph],<EOL>outputs=output_actions,<EOL>givens={update_eps_ph: -<NUM_LIT:1.0>, stochastic_ph: True, reset_ph: False, update_param_noise_threshold_ph: False, update_param_noise_scale_ph: False},<EOL>updates=updates)<EOL>def act(ob, reset=False, update_param_noise_threshold=False, update_param_noise_scale=False, stochastic=True, update_eps=-<NUM_LIT:1>):<EOL><INDENT>return _act(ob, stochastic, update_eps, reset, update_param_noise_threshold, update_param_noise_scale)<EOL><DEDENT>return act<EOL><DEDENT>

Creates the act function with support for parameter space noise exploration (https://arxiv.org/abs/1706.01905):

    Parameters
    ----------
    make_obs_ph: str -> tf.placeholder or TfInput
        a function that take a name and creates a placeholder of input with that name
    q_func: (tf.Variable, int, str, bool) -> tf.Variable
        the model that takes the following inputs:
            observation_in: object
                the output of observation placeholder
            num_actions: int
                number of actions
            scope: str
            reuse: bool
                should be passed to outer variable scope
        and returns a tensor of shape (batch_size, num_actions) with values of every action.
    num_actions: int
        number of actions.
    scope: str or VariableScope
        optional scope for variable_scope.
    reuse: bool or None
        whether or not the variables should be reused. To be able to reuse the scope must be given.
    param_noise_filter_func: tf.Variable -> bool
        function that decides whether or not a variable should be perturbed. Only applicable
        if param_noise is True. If set to None, default_param_noise_filter is used by default.

    Returns
    -------
    act: (tf.Variable, bool, float, bool, float, bool) -> tf.Variable
        function to select and action given observation.
`       See the top of the file for details.


def build_act(make_obs_ph, q_func, num_actions, scope="<STR_LIT>", reuse=None):

with tf.variable_scope(scope, reuse=reuse):<EOL><INDENT>observations_ph = make_obs_ph("<STR_LIT>")<EOL>stochastic_ph = tf.placeholder(tf.bool, (), name="<STR_LIT>")<EOL>update_eps_ph = tf.placeholder(tf.float32, (), name="<STR_LIT>")<EOL>eps = tf.get_variable("<STR_LIT>", (), initializer=tf.constant_initializer(<NUM_LIT:0>))<EOL>q_values = q_func(observations_ph.get(), num_actions, scope="<STR_LIT>")<EOL>deterministic_actions = tf.argmax(q_values, axis=<NUM_LIT:1>)<EOL>batch_size = tf.shape(observations_ph.get())[<NUM_LIT:0>]<EOL>random_actions = tf.random_uniform(tf.stack([batch_size]), minval=<NUM_LIT:0>, maxval=num_actions, dtype=tf.int64)<EOL>chose_random = tf.random_uniform(tf.stack([batch_size]), minval=<NUM_LIT:0>, maxval=<NUM_LIT:1>, dtype=tf.float32) < eps<EOL>stochastic_actions = tf.where(chose_random, random_actions, deterministic_actions)<EOL>output_actions = tf.cond(stochastic_ph, lambda: stochastic_actions, lambda: deterministic_actions)<EOL>update_eps_expr = eps.assign(tf.cond(update_eps_ph >= <NUM_LIT:0>, lambda: update_eps_ph, lambda: eps))<EOL>_act = U.function(inputs=[observations_ph, stochastic_ph, update_eps_ph],<EOL>outputs=output_actions,<EOL>givens={update_eps_ph: -<NUM_LIT:1.0>, stochastic_ph: True},<EOL>updates=[update_eps_expr])<EOL>def act(ob, stochastic=True, update_eps=-<NUM_LIT:1>):<EOL><INDENT>return _act(ob, stochastic, update_eps)<EOL><DEDENT>return act<EOL><DEDENT>

Creates the act function:

    Parameters
    ----------
    make_obs_ph: str -> tf.placeholder or TfInput
        a function that take a name and creates a placeholder of input with that name
    q_func: (tf.Variable, int, str, bool) -> tf.Variable
        the model that takes the following inputs:
            observation_in: object
                the output of observation placeholder
            num_actions: int
                number of actions
            scope: str
            reuse: bool
                should be passed to outer variable scope
        and returns a tensor of shape (batch_size, num_actions) with values of every action.
    num_actions: int
        number of actions.
    scope: str or VariableScope
        optional scope for variable_scope.
    reuse: bool or None
        whether or not the variables should be reused. To be able to reuse the scope must be given.

    Returns
    -------
    act: (tf.Variable, bool, float) -> tf.Variable
        function to select and action given observation.
`       See the top of the file for details.


return lambda *args, **kwargs: _mlp(hiddens, layer_norm=layer_norm, *args, **kwargs)<EOL>

This model takes as input an observation and returns values of all actions.

    Parameters
    ----------
    hiddens: [int]
        list of sizes of hidden layers
    layer_norm: bool
        if true applies layer normalization for every layer
        as described in https://arxiv.org/abs/1607.06450

    Returns
    -------
    q_func: function
        q_function for DQN algorithm.


return next(filter(lambda task: task['<STR_LIT>'] == env_id, benchmark['<STR_LIT>']), None)<EOL>

Get a task by env_id. Return None if the benchmark doesn't have the env


if MPI is not None:<EOL><INDENT>nworkers = MPI.COMM_WORLD.Get_size()<EOL>rank = MPI.COMM_WORLD.Get_rank()<EOL><DEDENT>else:<EOL><INDENT>nworkers = <NUM_LIT:1><EOL>rank = <NUM_LIT:0><EOL><DEDENT>cpus_per_worker = <NUM_LIT:1><EOL>U.get_session(config=tf.ConfigProto(<EOL>allow_soft_placement=True,<EOL>inter_op_parallelism_threads=cpus_per_worker,<EOL>intra_op_parallelism_threads=cpus_per_worker<EOL>))<EOL>policy = build_policy(env, network, value_network='<STR_LIT>', **network_kwargs)<EOL>set_global_seeds(seed)<EOL>np.set_printoptions(precision=<NUM_LIT:3>)<EOL>ob_space = env.observation_space<EOL>ac_space = env.action_space<EOL>ob = observation_placeholder(ob_space)<EOL>with tf.variable_scope("<STR_LIT>"):<EOL><INDENT>pi = policy(observ_placeholder=ob)<EOL><DEDENT>with tf.variable_scope("<STR_LIT>"):<EOL><INDENT>oldpi = policy(observ_placeholder=ob)<EOL><DEDENT>atarg = tf.placeholder(dtype=tf.float32, shape=[None]) <EOL>ret = tf.placeholder(dtype=tf.float32, shape=[None]) <EOL>ac = pi.pdtype.sample_placeholder([None])<EOL>kloldnew = oldpi.pd.kl(pi.pd)<EOL>ent = pi.pd.entropy()<EOL>meankl = tf.reduce_mean(kloldnew)<EOL>meanent = tf.reduce_mean(ent)<EOL>entbonus = ent_coef * meanent<EOL>vferr = tf.reduce_mean(tf.square(pi.vf - ret))<EOL>ratio = tf.exp(pi.pd.logp(ac) - oldpi.pd.logp(ac)) <EOL>surrgain = tf.reduce_mean(ratio * atarg)<EOL>optimgain = surrgain + entbonus<EOL>losses = [optimgain, meankl, entbonus, surrgain, meanent]<EOL>loss_names = ["<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>"]<EOL>dist = meankl<EOL>all_var_list = get_trainable_variables("<STR_LIT>")<EOL>var_list = get_pi_trainable_variables("<STR_LIT>")<EOL>vf_var_list = get_vf_trainable_variables("<STR_LIT>")<EOL>vfadam = MpiAdam(vf_var_list)<EOL>get_flat = U.GetFlat(var_list)<EOL>set_from_flat = U.SetFromFlat(var_list)<EOL>klgrads = tf.gradients(dist, var_list)<EOL>flat_tangent = tf.placeholder(dtype=tf.float32, shape=[None], name="<STR_LIT>")<EOL>shapes = [var.get_shape().as_list() for var in var_list]<EOL>start = <NUM_LIT:0><EOL>tangents = []<EOL>for shape in shapes:<EOL><INDENT>sz = U.intprod(shape)<EOL>tangents.append(tf.reshape(flat_tangent[start:start+sz], shape))<EOL>start += sz<EOL><DEDENT>gvp = tf.add_n([tf.reduce_sum(g*tangent) for (g, tangent) in zipsame(klgrads, tangents)]) <EOL>fvp = U.flatgrad(gvp, var_list)<EOL>assign_old_eq_new = U.function([],[], updates=[tf.assign(oldv, newv)<EOL>for (oldv, newv) in zipsame(get_variables("<STR_LIT>"), get_variables("<STR_LIT>"))])<EOL>compute_losses = U.function([ob, ac, atarg], losses)<EOL>compute_lossandgrad = U.function([ob, ac, atarg], losses + [U.flatgrad(optimgain, var_list)])<EOL>compute_fvp = U.function([flat_tangent, ob, ac, atarg], fvp)<EOL>compute_vflossandgrad = U.function([ob, ret], U.flatgrad(vferr, vf_var_list))<EOL>@contextmanager<EOL>def timed(msg):<EOL><INDENT>if rank == <NUM_LIT:0>:<EOL><INDENT>print(colorize(msg, color='<STR_LIT>'))<EOL>tstart = time.time()<EOL>yield<EOL>print(colorize("<STR_LIT>"%(time.time() - tstart), color='<STR_LIT>'))<EOL><DEDENT>else:<EOL><INDENT>yield<EOL><DEDENT><DEDENT>def allmean(x):<EOL><INDENT>assert isinstance(x, np.ndarray)<EOL>if MPI is not None:<EOL><INDENT>out = np.empty_like(x)<EOL>MPI.COMM_WORLD.Allreduce(x, out, op=MPI.SUM)<EOL>out /= nworkers<EOL><DEDENT>else:<EOL><INDENT>out = np.copy(x)<EOL><DEDENT>return out<EOL><DEDENT>U.initialize()<EOL>if load_path is not None:<EOL><INDENT>pi.load(load_path)<EOL><DEDENT>th_init = get_flat()<EOL>if MPI is not None:<EOL><INDENT>MPI.COMM_WORLD.Bcast(th_init, root=<NUM_LIT:0>)<EOL><DEDENT>set_from_flat(th_init)<EOL>vfadam.sync()<EOL>print("<STR_LIT>", th_init.sum(), flush=True)<EOL>seg_gen = traj_segment_generator(pi, env, timesteps_per_batch, stochastic=True)<EOL>episodes_so_far = <NUM_LIT:0><EOL>timesteps_so_far = <NUM_LIT:0><EOL>iters_so_far = <NUM_LIT:0><EOL>tstart = time.time()<EOL>lenbuffer = deque(maxlen=<NUM_LIT>) <EOL>rewbuffer = deque(maxlen=<NUM_LIT>) <EOL>if sum([max_iters><NUM_LIT:0>, total_timesteps><NUM_LIT:0>, max_episodes><NUM_LIT:0>])==<NUM_LIT:0>:<EOL><INDENT>return pi<EOL><DEDENT>assert sum([max_iters><NUM_LIT:0>, total_timesteps><NUM_LIT:0>, max_episodes><NUM_LIT:0>]) < <NUM_LIT:2>,'<STR_LIT>'<EOL>while True:<EOL><INDENT>if callback: callback(locals(), globals())<EOL>if total_timesteps and timesteps_so_far >= total_timesteps:<EOL><INDENT>break<EOL><DEDENT>elif max_episodes and episodes_so_far >= max_episodes:<EOL><INDENT>break<EOL><DEDENT>elif max_iters and iters_so_far >= max_iters:<EOL><INDENT>break<EOL><DEDENT>logger.log("<STR_LIT>"%iters_so_far)<EOL>with timed("<STR_LIT>"):<EOL><INDENT>seg = seg_gen.__next__()<EOL><DEDENT>add_vtarg_and_adv(seg, gamma, lam)<EOL>ob, ac, atarg, tdlamret = seg["<STR_LIT>"], seg["<STR_LIT>"], seg["<STR_LIT>"], seg["<STR_LIT>"]<EOL>vpredbefore = seg["<STR_LIT>"] <EOL>atarg = (atarg - atarg.mean()) / atarg.std() <EOL>if hasattr(pi, "<STR_LIT>"): pi.ret_rms.update(tdlamret)<EOL>if hasattr(pi, "<STR_LIT>"): pi.ob_rms.update(ob) <EOL>args = seg["<STR_LIT>"], seg["<STR_LIT>"], atarg<EOL>fvpargs = [arr[::<NUM_LIT:5>] for arr in args]<EOL>def fisher_vector_product(p):<EOL><INDENT>return allmean(compute_fvp(p, *fvpargs)) + cg_damping * p<EOL><DEDENT>assign_old_eq_new() <EOL>with timed("<STR_LIT>"):<EOL><INDENT>*lossbefore, g = compute_lossandgrad(*args)<EOL><DEDENT>lossbefore = allmean(np.array(lossbefore))<EOL>g = allmean(g)<EOL>if np.allclose(g, <NUM_LIT:0>):<EOL><INDENT>logger.log("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>with timed("<STR_LIT>"):<EOL><INDENT>stepdir = cg(fisher_vector_product, g, cg_iters=cg_iters, verbose=rank==<NUM_LIT:0>)<EOL><DEDENT>assert np.isfinite(stepdir).all()<EOL>shs = <NUM_LIT>*stepdir.dot(fisher_vector_product(stepdir))<EOL>lm = np.sqrt(shs / max_kl)<EOL>fullstep = stepdir / lm<EOL>expectedimprove = g.dot(fullstep)<EOL>surrbefore = lossbefore[<NUM_LIT:0>]<EOL>stepsize = <NUM_LIT:1.0><EOL>thbefore = get_flat()<EOL>for _ in range(<NUM_LIT:10>):<EOL><INDENT>thnew = thbefore + fullstep * stepsize<EOL>set_from_flat(thnew)<EOL>meanlosses = surr, kl, *_ = allmean(np.array(compute_losses(*args)))<EOL>improve = surr - surrbefore<EOL>logger.log("<STR_LIT>"%(expectedimprove, improve))<EOL>if not np.isfinite(meanlosses).all():<EOL><INDENT>logger.log("<STR_LIT>")<EOL><DEDENT>elif kl > max_kl * <NUM_LIT>:<EOL><INDENT>logger.log("<STR_LIT>")<EOL><DEDENT>elif improve < <NUM_LIT:0>:<EOL><INDENT>logger.log("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>logger.log("<STR_LIT>")<EOL>break<EOL><DEDENT>stepsize *= <NUM_LIT><EOL><DEDENT>else:<EOL><INDENT>logger.log("<STR_LIT>")<EOL>set_from_flat(thbefore)<EOL><DEDENT>if nworkers > <NUM_LIT:1> and iters_so_far % <NUM_LIT:20> == <NUM_LIT:0>:<EOL><INDENT>paramsums = MPI.COMM_WORLD.allgather((thnew.sum(), vfadam.getflat().sum())) <EOL>assert all(np.allclose(ps, paramsums[<NUM_LIT:0>]) for ps in paramsums[<NUM_LIT:1>:])<EOL><DEDENT><DEDENT>for (lossname, lossval) in zip(loss_names, meanlosses):<EOL><INDENT>logger.record_tabular(lossname, lossval)<EOL><DEDENT>with timed("<STR_LIT>"):<EOL><INDENT>for _ in range(vf_iters):<EOL><INDENT>for (mbob, mbret) in dataset.iterbatches((seg["<STR_LIT>"], seg["<STR_LIT>"]),<EOL>include_final_partial_batch=False, batch_size=<NUM_LIT:64>):<EOL><INDENT>g = allmean(compute_vflossandgrad(mbob, mbret))<EOL>vfadam.update(g, vf_stepsize)<EOL><DEDENT><DEDENT><DEDENT>logger.record_tabular("<STR_LIT>", explained_variance(vpredbefore, tdlamret))<EOL>lrlocal = (seg["<STR_LIT>"], seg["<STR_LIT>"]) <EOL>if MPI is not None:<EOL><INDENT>listoflrpairs = MPI.COMM_WORLD.allgather(lrlocal) <EOL><DEDENT>else:<EOL><INDENT>listoflrpairs = [lrlocal]<EOL><DEDENT>lens, rews = map(flatten_lists, zip(*listoflrpairs))<EOL>lenbuffer.extend(lens)<EOL>rewbuffer.extend(rews)<EOL>logger.record_tabular("<STR_LIT>", np.mean(lenbuffer))<EOL>logger.record_tabular("<STR_LIT>", np.mean(rewbuffer))<EOL>logger.record_tabular("<STR_LIT>", len(lens))<EOL>episodes_so_far += len(lens)<EOL>timesteps_so_far += sum(lens)<EOL>iters_so_far += <NUM_LIT:1><EOL>logger.record_tabular("<STR_LIT>", episodes_so_far)<EOL>logger.record_tabular("<STR_LIT>", timesteps_so_far)<EOL>logger.record_tabular("<STR_LIT>", time.time() - tstart)<EOL>if rank==<NUM_LIT:0>:<EOL><INDENT>logger.dump_tabular()<EOL><DEDENT><DEDENT>return pi<EOL>

learn a policy function with TRPO algorithm

Parameters:
----------

network                 neural network to learn. Can be either string ('mlp', 'cnn', 'lstm', 'lnlstm' for basic types)
                        or function that takes input placeholder and returns tuple (output, None) for feedforward nets
                        or (output, (state_placeholder, state_output, mask_placeholder)) for recurrent nets

env                     environment (one of the gym environments or wrapped via baselines.common.vec_env.VecEnv-type class

timesteps_per_batch     timesteps per gradient estimation batch

max_kl                  max KL divergence between old policy and new policy ( KL(pi_old || pi) )

ent_coef                coefficient of policy entropy term in the optimization objective

cg_iters                number of iterations of conjugate gradient algorithm

cg_damping              conjugate gradient damping

vf_stepsize             learning rate for adam optimizer used to optimie value function loss

vf_iters                number of iterations of value function optimization iterations per each policy optimization step

total_timesteps           max number of timesteps

max_episodes            max number of episodes

max_iters               maximum number of policy optimization iterations

callback                function to be called with (locals(), globals()) each policy optimization step

load_path               str, path to load the model from (default: None, i.e. no model is loaded)

**network_kwargs        keyword arguments to the policy / network builder. See baselines.common/policies.py/build_policy and arguments to a particular type of network

Returns:
-------

learnt model


set_global_seeds(seed)<EOL>nenvs = env.num_envs<EOL>policy = build_policy(env, network, **network_kwargs)<EOL>model = Model(policy=policy, env=env, nsteps=nsteps, ent_coef=ent_coef, vf_coef=vf_coef,<EOL>max_grad_norm=max_grad_norm, lr=lr, alpha=alpha, epsilon=epsilon, total_timesteps=total_timesteps, lrschedule=lrschedule)<EOL>if load_path is not None:<EOL><INDENT>model.load(load_path)<EOL><DEDENT>runner = Runner(env, model, nsteps=nsteps, gamma=gamma)<EOL>epinfobuf = deque(maxlen=<NUM_LIT:100>)<EOL>nbatch = nenvs*nsteps<EOL>tstart = time.time()<EOL>for update in range(<NUM_LIT:1>, total_timesteps//nbatch+<NUM_LIT:1>):<EOL><INDENT>obs, states, rewards, masks, actions, values, epinfos = runner.run()<EOL>epinfobuf.extend(epinfos)<EOL>policy_loss, value_loss, policy_entropy = model.train(obs, states, rewards, masks, actions, values)<EOL>nseconds = time.time()-tstart<EOL>fps = int((update*nbatch)/nseconds)<EOL>if update % log_interval == <NUM_LIT:0> or update == <NUM_LIT:1>:<EOL><INDENT>ev = explained_variance(values, rewards)<EOL>logger.record_tabular("<STR_LIT>", update)<EOL>logger.record_tabular("<STR_LIT>", update*nbatch)<EOL>logger.record_tabular("<STR_LIT>", fps)<EOL>logger.record_tabular("<STR_LIT>", float(policy_entropy))<EOL>logger.record_tabular("<STR_LIT>", float(value_loss))<EOL>logger.record_tabular("<STR_LIT>", float(ev))<EOL>logger.record_tabular("<STR_LIT>", safemean([epinfo['<STR_LIT:r>'] for epinfo in epinfobuf]))<EOL>logger.record_tabular("<STR_LIT>", safemean([epinfo['<STR_LIT:l>'] for epinfo in epinfobuf]))<EOL>logger.dump_tabular()<EOL><DEDENT><DEDENT>return model<EOL>

Main entrypoint for A2C algorithm. Train a policy with given network architecture on a given environment using a2c algorithm.

Parameters:
-----------

network:            policy network architecture. Either string (mlp, lstm, lnlstm, cnn_lstm, cnn, cnn_small, conv_only - see baselines.common/models.py for full list)
                    specifying the standard network architecture, or a function that takes tensorflow tensor as input and returns
                    tuple (output_tensor, extra_feed) where output tensor is the last network layer output, extra_feed is None for feed-forward
                    neural nets, and extra_feed is a dictionary describing how to feed state into the network for recurrent neural nets.
                    See baselines.common/policies.py/lstm for more details on using recurrent nets in policies

env:                RL environment. Should implement interface similar to VecEnv (baselines.common/vec_env) or be wrapped with DummyVecEnv (baselines.common/vec_env/dummy_vec_env.py)

seed:               seed to make random number sequence in the alorightm reproducible. By default is None which means seed from system noise generator (not reproducible)

nsteps:             int, number of steps of the vectorized environment per update (i.e. batch size is nsteps * nenv where
                    nenv is number of environment copies simulated in parallel)

total_timesteps:    int, total number of timesteps to train on (default: 80M)

vf_coef:            float, coefficient in front of value function loss in the total loss function (default: 0.5)

ent_coef:           float, coeffictiant in front of the policy entropy in the total loss function (default: 0.01)

max_gradient_norm:  float, gradient is clipped to have global L2 norm no more than this value (default: 0.5)

lr:                 float, learning rate for RMSProp (current implementation has RMSProp hardcoded in) (default: 7e-4)

lrschedule:         schedule of learning rate. Can be 'linear', 'constant', or a function [0..1] -> [0..1] that takes fraction of the training progress as input and
                    returns fraction of the learning rate (specified as lr) as output

epsilon:            float, RMSProp epsilon (stabilizes square root computation in denominator of RMSProp update) (default: 1e-5)

alpha:              float, RMSProp decay parameter (default: 0.99)

gamma:              float, reward discounting parameter (default: 0.99)

log_interval:       int, specifies how frequently the logs are printed out (default: 100)

**network_kwargs:   keyword arguments to the policy / network builder. See baselines.common/policies.py/build_policy and arguments to a particular type of network
                    For instance, 'mlp' network architecture has arguments num_hidden and num_layers.


gym.Wrapper.__init__(self, env)<EOL>self.lives = <NUM_LIT:0><EOL>self.was_real_done  = True<EOL>

Make end-of-life == end-of-episode, but only reset on true game over.
        Done by DeepMind for the DQN and co. since it helps value estimation.


self.env.reset(**kwargs)<EOL>if self.override_num_noops is not None:<EOL><INDENT>noops = self.override_num_noops<EOL><DEDENT>else:<EOL><INDENT>noops = self.unwrapped.np_random.randint(<NUM_LIT:1>, self.noop_max + <NUM_LIT:1>) <EOL><DEDENT>assert noops > <NUM_LIT:0><EOL>obs = None<EOL>for _ in range(noops):<EOL><INDENT>obs, _, done, _ = self.env.step(self.noop_action)<EOL>if done:<EOL><INDENT>obs = self.env.reset(**kwargs)<EOL><DEDENT><DEDENT>return obs<EOL>

Do no-op action for a number of steps in [1, noop_max].


gym.Wrapper.__init__(self, env)<EOL>self.k = k<EOL>self.frames = deque([], maxlen=k)<EOL>shp = env.observation_space.shape<EOL>self.observation_space = spaces.Box(low=<NUM_LIT:0>, high=<NUM_LIT:255>, shape=(shp[:-<NUM_LIT:1>] + (shp[-<NUM_LIT:1>] * k,)), dtype=env.observation_space.dtype)<EOL>

Stack k last frames.

        Returns lazy array, which is much more memory efficient.

        See Also
        --------
        baselines.common.atari_wrappers.LazyFrames


gym.Wrapper.__init__(self, env)<EOL>assert env.unwrapped.get_action_meanings()[<NUM_LIT:1>] == '<STR_LIT>'<EOL>assert len(env.unwrapped.get_action_meanings()) >= <NUM_LIT:3><EOL>

Take action on reset for environments that are fixed until firing.


assert y.ndim == <NUM_LIT:1> and ypred.ndim == <NUM_LIT:1><EOL>vary = np.var(y)<EOL>return np.nan if vary==<NUM_LIT:0> else <NUM_LIT:1> - np.var(y-ypred)/vary<EOL>

Computes fraction of variance that ypred explains about y.
Returns 1 - Var[y-ypred] / Var[y]

interpretation:
    ev=0  =>  might as well have predicted zero
    ev=1  =>  perfect prediction
    ev<0  =>  worse than just predicting zero


def make_robotics_env(env_id, seed, rank=<NUM_LIT:0>):

set_global_seeds(seed)<EOL>env = gym.make(env_id)<EOL>env = FlattenDictWrapper(env, ['<STR_LIT>', '<STR_LIT>'])<EOL>env = Monitor(<EOL>env, logger.get_dir() and os.path.join(logger.get_dir(), str(rank)),<EOL>info_keywords=('<STR_LIT>',))<EOL>env.seed(seed)<EOL>return env<EOL>

Create a wrapped, monitored gym.Env for MuJoCo.


parser = arg_parser()<EOL>parser.add_argument('<STR_LIT>', help='<STR_LIT>', type=str, default='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', help='<STR_LIT>', type=int, default=None)<EOL>parser.add_argument('<STR_LIT>', type=int, default=int(<NUM_LIT>))<EOL>return parser<EOL>

Create an argparse.ArgumentParser for run_mujoco.py.


retval = {}<EOL>preceded_by_key = False<EOL>for arg in args:<EOL><INDENT>if arg.startswith('<STR_LIT>'):<EOL><INDENT>if '<STR_LIT:=>' in arg:<EOL><INDENT>key = arg.split('<STR_LIT:=>')[<NUM_LIT:0>][<NUM_LIT:2>:]<EOL>value = arg.split('<STR_LIT:=>')[<NUM_LIT:1>]<EOL>retval[key] = value<EOL><DEDENT>else:<EOL><INDENT>key = arg[<NUM_LIT:2>:]<EOL>preceded_by_key = True<EOL><DEDENT><DEDENT>elif preceded_by_key:<EOL><INDENT>retval[key] = arg<EOL>preceded_by_key = False<EOL><DEDENT><DEDENT>return retval<EOL>

Parse arguments not consumed by arg parser into a dicitonary


wrapper_kwargs = wrapper_kwargs or {}<EOL>mpi_rank = MPI.COMM_WORLD.Get_rank() if MPI else <NUM_LIT:0><EOL>seed = seed + <NUM_LIT> * mpi_rank if seed is not None else None<EOL>logger_dir = logger.get_dir()<EOL>def make_thunk(rank):<EOL><INDENT>return lambda: make_env(<EOL>env_id=env_id,<EOL>env_type=env_type,<EOL>mpi_rank=mpi_rank,<EOL>subrank=rank,<EOL>seed=seed,<EOL>reward_scale=reward_scale,<EOL>gamestate=gamestate,<EOL>flatten_dict_observations=flatten_dict_observations,<EOL>wrapper_kwargs=wrapper_kwargs,<EOL>logger_dir=logger_dir<EOL>)<EOL><DEDENT>set_global_seeds(seed)<EOL>if num_env > <NUM_LIT:1>:<EOL><INDENT>return SubprocVecEnv([make_thunk(i + start_index) for i in range(num_env)])<EOL><DEDENT>else:<EOL><INDENT>return DummyVecEnv([make_thunk(start_index)])<EOL><DEDENT>

Create a wrapped, monitored SubprocVecEnv for Atari and MuJoCo.


print('<STR_LIT>')<EOL>return common_arg_parser()<EOL>

Create an argparse.ArgumentParser for run_atari.py.


parser = arg_parser()<EOL>parser.add_argument('<STR_LIT>', help='<STR_LIT>', type=str, default='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', help='<STR_LIT>', type=str)<EOL>parser.add_argument('<STR_LIT>', help='<STR_LIT>', type=int, default=None)<EOL>parser.add_argument('<STR_LIT>', help='<STR_LIT>', type=str, default='<STR_LIT>')<EOL>parser.add_argument('<STR_LIT>', type=float, default=<NUM_LIT>),<EOL>parser.add_argument('<STR_LIT>', help='<STR_LIT>', default=None)<EOL>parser.add_argument('<STR_LIT>', help='<STR_LIT>', default=None)<EOL>parser.add_argument('<STR_LIT>', help='<STR_LIT>', default=None, type=int)<EOL>parser.add_argument('<STR_LIT>', help='<STR_LIT>', default=<NUM_LIT:1.0>, type=float)<EOL>parser.add_argument('<STR_LIT>', help='<STR_LIT>', default=None, type=str)<EOL>parser.add_argument('<STR_LIT>', help='<STR_LIT>', default=<NUM_LIT:0>, type=int)<EOL>parser.add_argument('<STR_LIT>', help='<STR_LIT>', default=<NUM_LIT:200>, type=int)<EOL>parser.add_argument('<STR_LIT>', default=False, action='<STR_LIT:store_true>')<EOL>return parser<EOL>


def make_mujoco_env(env_id, seed, reward_scale=<NUM_LIT:1.0>):

rank = MPI.COMM_WORLD.Get_rank()<EOL>myseed = seed  + <NUM_LIT:1000> * rank if seed is not None else None<EOL>set_global_seeds(myseed)<EOL>env = gym.make(env_id)<EOL>logger_path = None if logger.get_dir() is None else os.path.join(logger.get_dir(), str(rank))<EOL>env = Monitor(env, logger_path, allow_early_resets=True)<EOL>env.seed(seed)<EOL>if reward_scale != <NUM_LIT:1.0>:<EOL><INDENT>from baselines.common.retro_wrappers import RewardScaler<EOL>env = RewardScaler(env, reward_scale)<EOL><DEDENT>return env<EOL>


import argparse<EOL>return argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)<EOL>

Create an empty argparse.ArgumentParser.


if isinstance(obs, dict):<EOL><INDENT>return obs<EOL><DEDENT>return {None: obs}<EOL>


return {k: np.copy(v) for k, v in obs.items()}<EOL>


def __init__(self, env_fns, spaces=None, context='<STR_LIT>'):

self.waiting = False<EOL>self.closed = False<EOL>nenvs = len(env_fns)<EOL>ctx = mp.get_context(context)<EOL>self.remotes, self.work_remotes = zip(*[ctx.Pipe() for _ in range(nenvs)])<EOL>self.ps = [ctx.Process(target=worker, args=(work_remote, remote, CloudpickleWrapper(env_fn)))<EOL>for (work_remote, remote, env_fn) in zip(self.work_remotes, self.remotes, env_fns)]<EOL>for p in self.ps:<EOL><INDENT>p.daemon = True  <EOL>with clear_mpi_env_vars():<EOL><INDENT>p.start()<EOL><DEDENT><DEDENT>for remote in self.work_remotes:<EOL><INDENT>remote.close()<EOL><DEDENT>self.remotes[<NUM_LIT:0>].send(('<STR_LIT>', None))<EOL>observation_space, action_space, self.spec = self.remotes[<NUM_LIT:0>].recv()<EOL>self.viewer = None<EOL>VecEnv.__init__(self, len(env_fns), observation_space, action_space)<EOL>

Arguments:

env_fns: iterable of callables -  functions that create environments to run in subprocesses. Need to be cloud-pickleable


pass<EOL>

Clean up the  extra resources, beyond what's in this base class.
Only runs when not self.closed.


raise NotImplementedError<EOL>


self.step_async(actions)<EOL>return self.step_wait()<EOL>

Step the environments synchronously.

This is available for backwards compatibility.