lock = asyncio.Lock()<EOL>def locked_coro(coro):<EOL><INDENT>@asyncio.coroutine<EOL>@wraps(coro)<EOL>def wrapper(*args, **kwargs):<EOL><INDENT>with (yield from lock):<EOL><INDENT>return (yield from coro(*args, **kwargs))<EOL><DEDENT><DEDENT>return wrapper<EOL><DEDENT>class MonopriceAsync(Monoprice):<EOL><INDENT>def __init__(self, monoprice_protocol):<EOL><INDENT>self._protocol = monoprice_protocol<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def zone_status(self, zone: int):<EOL><INDENT>string = yield from self._protocol.send(_format_zone_status_request(zone), skip=<NUM_LIT:6>)<EOL>return ZoneStatus.from_string(string)<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def set_power(self, zone: int, power: bool):<EOL><INDENT>yield from self._protocol.send(_format_set_power(zone, power))<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def set_mute(self, zone: int, mute: bool):<EOL><INDENT>yield from self._protocol.send(_format_set_mute(zone, mute))<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def set_volume(self, zone: int, volume: int):<EOL><INDENT>yield from self._protocol.send(_format_set_volume(zone, volume))<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def set_treble(self, zone: int, treble: int):<EOL><INDENT>yield from self._protocol.send(_format_set_treble(zone, treble))<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def set_bass(self, zone: int, bass: int):<EOL><INDENT>yield from self._protocol.send(_format_set_bass(zone, bass))<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def set_balance(self, zone: int, balance: int):<EOL><INDENT>yield from self._protocol.send(_format_set_balance(zone, balance))<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def set_source(self, zone: int, source: int):<EOL><INDENT>yield from self._protocol.send(_format_set_source(zone, source))<EOL><DEDENT>@locked_coro<EOL>@asyncio.coroutine<EOL>def restore_zone(self, status: ZoneStatus):<EOL><INDENT>yield from self._protocol.send(_format_set_power(status.zone, status.power))<EOL>yield from self._protocol.send(_format_set_mute(status.zone, status.mute))<EOL>yield from self._protocol.send(_format_set_volume(status.zone, status.volume))<EOL>yield from self._protocol.send(_format_set_treble(status.zone, status.treble))<EOL>yield from self._protocol.send(_format_set_bass(status.zone, status.bass))<EOL>yield from self._protocol.send(_format_set_balance(status.zone, status.balance))<EOL>yield from self._protocol.send(_format_set_source(status.zone, status.source))<EOL><DEDENT><DEDENT>class MonopriceProtocol(asyncio.Protocol):<EOL><INDENT>def __init__(self, loop):<EOL><INDENT>super().__init__()<EOL>self._loop = loop<EOL>self._lock = asyncio.Lock()<EOL>self._transport = None<EOL>self._connected = asyncio.Event(loop=loop)<EOL>self.q = asyncio.Queue(loop=loop)<EOL><DEDENT>def connection_made(self, transport):<EOL><INDENT>self._transport = transport<EOL>self._connected.set()<EOL>_LOGGER.debug('<STR_LIT>', self._transport)<EOL><DEDENT>def data_received(self, data):<EOL><INDENT>asyncio.ensure_future(self.q.put(data), loop=self._loop)<EOL><DEDENT>@asyncio.coroutine<EOL>def send(self, request: bytes, skip=<NUM_LIT:0>):<EOL><INDENT>yield from self._connected.wait()<EOL>result = bytearray()<EOL>with (yield from self._lock):<EOL><INDENT>self._transport.serial.reset_output_buffer()<EOL>self._transport.serial.reset_input_buffer()<EOL>while not self.q.empty():<EOL><INDENT>self.q.get_nowait()<EOL><DEDENT>self._transport.write(request)<EOL>try:<EOL><INDENT>while True:<EOL><INDENT>result += yield from asyncio.wait_for(self.q.get(), TIMEOUT, loop=self._loop)<EOL>if len(result) > skip and result[-LEN_EOL:] == EOL:<EOL><INDENT>ret = bytes(result)<EOL>_LOGGER.debug('<STR_LIT>', ret)<EOL>return ret.decode('<STR_LIT:ascii>')<EOL><DEDENT><DEDENT><DEDENT>except asyncio.TimeoutError:<EOL><INDENT>_LOGGER.error("<STR_LIT>", request, result)<EOL>raise<EOL><DEDENT><DEDENT><DEDENT><DEDENT>_, protocol = yield from create_serial_connection(loop, functools.partial(MonopriceProtocol, loop),<EOL>port_url, baudrate=<NUM_LIT>)<EOL>return MonopriceAsync(protocol)<EOL>

Return asynchronous version of Monoprice interface
:param port_url: serial port, i.e. '/dev/ttyUSB0'
:return: asynchronous implementation of Monoprice interface


raise NotImplemented()<EOL>

Get the structure representing the status of the zone
:param zone: zone 11..16, 21..26, 31..36
:return: status of the zone or None


def set_power(self, zone: int, power: bool):

raise NotImplemented()<EOL>

Turn zone on or off
:param zone: zone 11..16, 21..26, 31..36
:param power: True to turn on, False to turn off


def set_mute(self, zone: int, mute: bool):

raise NotImplemented()<EOL>

Mute zone on or off
:param zone: zone 11..16, 21..26, 31..36
:param mute: True to mute, False to unmute


def set_volume(self, zone: int, volume: int):

raise NotImplemented()<EOL>

Set volume for zone
:param zone: zone 11..16, 21..26, 31..36
:param volume: integer from 0 to 38 inclusive


def set_treble(self, zone: int, treble: int):

raise NotImplemented()<EOL>

Set treble for zone
:param zone: zone 11..16, 21..26, 31..36
:param treble: integer from 0 to 14 inclusive, where 0 is -7 treble and 14 is +7


def set_bass(self, zone: int, bass: int):

raise NotImplemented()<EOL>

Set bass for zone
:param zone: zone 11..16, 21..26, 31..36
:param bass: integer from 0 to 14 inclusive, where 0 is -7 bass and 14 is +7


def set_balance(self, zone: int, balance: int):

raise NotImplemented()<EOL>

Set balance for zone
:param zone: zone 11..16, 21..26, 31..36
:param balance: integer from 0 to 20 inclusive, where 0 is -10(left), 0 is center and 20 is +10 (right)


def set_source(self, zone: int, source: int):

raise NotImplemented()<EOL>

Set source for zone
:param zone: zone 11..16, 21..26, 31..36
:param source: integer from 0 to 6 inclusive


def restore_zone(self, status: ZoneStatus):

raise NotImplemented()<EOL>

Restores zone to it's previous state
:param status: zone state to restore


if isinstance(songid, list):<EOL><INDENT>songid = '<STR_LIT:U+002C>'.join(songid)<EOL><DEDENT>data = {<EOL>"<STR_LIT>": <NUM_LIT:1>,<EOL>"<STR_LIT>": songid<EOL>}<EOL>res = requests.post(MUSIC_INFO_URL, data=data)<EOL>info = res.json()<EOL>music_data = info["<STR_LIT:data>"]<EOL>songs = []<EOL>for song in music_data["<STR_LIT>"]:<EOL><INDENT>song_link, size = _song_link(song, music_data["<STR_LIT>"])<EOL>songs.append({<EOL>"<STR_LIT:name>": song["<STR_LIT>"],<EOL>"<STR_LIT>": song["<STR_LIT>"],<EOL>"<STR_LIT>": song["<STR_LIT>"],<EOL>"<STR_LIT>": song_link,<EOL>"<STR_LIT:size>": size<EOL>})<EOL><DEDENT>return songs<EOL>


filename = "<STR_LIT>".format(song["<STR_LIT:name>"])<EOL>if os.path.exists(filename):<EOL><INDENT>os.remove(filename)<EOL><DEDENT>part = int(song["<STR_LIT:size>"] / thread_num)<EOL>if part <= <NUM_LIT>:<EOL><INDENT>thread_num = <NUM_LIT:1><EOL><DEDENT>_id = uuid.uuid4().hex<EOL>logger.info("<STR_LIT>".format(song["<STR_LIT:name>"]))<EOL>threads = []<EOL>for i in range(thread_num):<EOL><INDENT>if i == thread_num - <NUM_LIT:1>:<EOL><INDENT>end = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>end = (i + <NUM_LIT:1>) * part - <NUM_LIT:1><EOL><DEDENT>thread = Worker((i * part, end), song, _id)<EOL>thread.start()<EOL>threads.append(thread)<EOL><DEDENT>for t in threads:<EOL><INDENT>t.join()<EOL><DEDENT>fileParts = glob.glob("<STR_LIT>".format(_id))<EOL>fileParts.sort(key=lambda e: e.split('<STR_LIT:->')[-<NUM_LIT:1>])<EOL>logger.info("<STR_LIT>".format(song["<STR_LIT:name>"]))<EOL>with open(filename, "<STR_LIT>") as f:<EOL><INDENT>for part in fileParts:<EOL><INDENT>with open(part, "<STR_LIT:rb>") as d:<EOL><INDENT>shutil.copyfileobj(d, f)<EOL><DEDENT>os.remove(part)<EOL><DEDENT><DEDENT>logger.info("<STR_LIT>".format(song["<STR_LIT:name>"]))<EOL>

process for downing music with multiple threads


<EOL>lon1, lat1, lon2, lat2 = map(math.radians, [lon1, lat1, lon2, lat2])<EOL>dlon = lon2 - lon1<EOL>dlat = lat2 - lat1<EOL>a = math.sin(dlat / <NUM_LIT:2>) ** <NUM_LIT:2> + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / <NUM_LIT:2>) ** <NUM_LIT:2><EOL>c = <NUM_LIT:2> * math.asin(math.sqrt(a))<EOL>dis = E.R * c<EOL>return dis<EOL>

Calculate the great circle distance between two points
on the earth (specified in decimal degrees)


pattern_list = []<EOL>for t in args:<EOL><INDENT>if isinstance(t, (list, tuple)):<EOL><INDENT>t = url(prefix=prefix, *t)<EOL><DEDENT>elif isinstance(t, RegexURLPattern):<EOL><INDENT>t.add_prefix(prefix)<EOL><DEDENT>pattern_list.append(t)<EOL><DEDENT>return pattern_list<EOL>


def url(regex, view, kwargs=None, name=None, prefix='<STR_LIT>'):

if isinstance(view, (list, tuple)):<EOL><INDENT>urlconf_module, app_name, namespace = view<EOL>return URLResolver(regex, urlconf_module, kwargs, app_name=app_name, namespace=namespace)<EOL><DEDENT>else:<EOL><INDENT>if isinstance(view, six.string_types):<EOL><INDENT>if not view:<EOL><INDENT>raise ImproperlyConfigured('<STR_LIT>' % regex)<EOL><DEDENT>if prefix:<EOL><INDENT>view = prefix + '<STR_LIT:.>' + view<EOL><DEDENT>view = get_callable(view)<EOL><DEDENT>return CBVRegexURLPattern(regex, view, kwargs, name)<EOL><DEDENT>


def setup_platform(hass, config, add_entities, discovery_info=None):

host = config.get(CONF_HOST)<EOL>token = config.get(CONF_ACCESS_TOKEN)<EOL>name = config.get(CONF_NAME)<EOL>volume_step = config.get(CONF_VOLUME_STEP)<EOL>device_type = config.get(CONF_DEVICE_CLASS)<EOL>device = VizioDevice(host, token, name, volume_step, device_type)<EOL>if device.validate_setup() is False:<EOL><INDENT>_LOGGER.error("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>return<EOL><DEDENT>elif (token is None or token == "<STR_LIT>") and device_type == "<STR_LIT>":<EOL><INDENT>_LOGGER.error("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL>return<EOL><DEDENT>if config.get(CONF_SUPPRESS_WARNING):<EOL><INDENT>from requests.packages import urllib3<EOL>_LOGGER.warning("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)<EOL><DEDENT>add_entities([device], True)<EOL>


def __init__(self, host, token, name, volume_step, device_type):

import pyvizio<EOL>self._name = name<EOL>self._state = None<EOL>self._volume_level = None<EOL>self._volume_step = volume_step<EOL>self._current_input = None<EOL>self._available_inputs = None<EOL>self._device_type = device_type<EOL>self._supported_commands = SUPPORTED_COMMANDS[device_type]<EOL>self._device = pyvizio.Vizio(DEVICE_ID, host, DEFAULT_NAME, token,<EOL>device_type)<EOL>self._max_volume = float(self._device.get_max_volume())<EOL>


is_on = self._device.get_power_state()<EOL>if is_on:<EOL><INDENT>self._state = STATE_ON<EOL>volume = self._device.get_current_volume()<EOL>if volume is not None:<EOL><INDENT>self._volume_level = float(volume) / self._max_volume<EOL><DEDENT>input_ = self._device.get_current_input()<EOL>if input_ is not None:<EOL><INDENT>self._current_input = input_.meta_name<EOL><DEDENT>inputs = self._device.get_inputs()<EOL>if inputs is not None:<EOL><INDENT>self._available_inputs = [input_.name for input_ in inputs]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if is_on is None:<EOL><INDENT>self._state = None<EOL><DEDENT>else:<EOL><INDENT>self._state = STATE_OFF<EOL><DEDENT>self._volume_level = None<EOL>self._current_input = None<EOL>self._available_inputs = None<EOL><DEDENT>


return self._state<EOL>


return self._name<EOL>


return self._volume_level<EOL>


return self._current_input<EOL>


return self._available_inputs<EOL>

Return list of available inputs of the device.


return self._supported_commands<EOL>

Flag device features that are supported.


if mute:<EOL><INDENT>self._device.mute_on()<EOL><DEDENT>else:<EOL><INDENT>self._device.mute_off()<EOL><DEDENT>


self._volume_level += self._volume_step / self._max_volume<EOL>self._device.vol_up(num=self._volume_step)<EOL>


self._volume_level -= self._volume_step / self._max_volume<EOL>self._device.vol_down(num=self._volume_step)<EOL>


return self._device.get_current_volume() is not None<EOL>

Validate if host is available and key is correct.


if self._volume_level is not None:<EOL><INDENT>if volume > self._volume_level:<EOL><INDENT>num = int(self._max_volume * (volume - self._volume_level))<EOL>self._volume_level = volume<EOL>self._device.vol_up(num=num)<EOL><DEDENT>elif volume < self._volume_level:<EOL><INDENT>num = int(self._max_volume * (self._volume_level - volume))<EOL>self._volume_level = volume<EOL>self._device.vol_down(num=num)<EOL><DEDENT><DEDENT>


return NotImplementedError<EOL>

The permittivty of the desired material.

Each new material needs this method implemented.

Args:
    wavelength (float, list, None): The wavelength the permittivty
        will be evaluated at.

Returns:
    float, list: The permittivty at the target wavelength.


return self._eps(wavelength)<EOL>

The permittivty of the desired material.

Args:
    wavelength (float, list, None): The wavelength the permittivty
        will be evaluated at.

Returns:
    float, list: The permittivty at the target wavelength.


return self.eps(wavelength)**<NUM_LIT:0.5><EOL>

The refractive index of the desired material.

Args:
    wavelength (float, list, None): The wavelength the refractive index
        will be evaluated at.

Returns:
    float, list: The refractive index at the target wavelength.


return self._nDer1(wavelength)<EOL>

The first derivative of the refractive index with respect to
wavelength.

Args:
    wavelength (float, list, None): The wavelength(s) the derivative
        will be evaluated at.

Returns:
    float, list: The refractive index at the target wavelength(s).


return self._nDer2(wavelength)<EOL>

The second derivative of the refractive index with respect to
wavelength.

Args:
    wavelength (float, list, None): The wavelength(s) the derivative
        will be evaluated at.

Returns:
    float, list: The refractive index at the target wavelength(s).


return self.n(wavelength) - (wavelength*<NUM_LIT>)*self.nDer1(wavelength)<EOL>

The group index with respect to wavelength.

Args:
    wavelength (float, list, None): The wavelength(s) the group
        index will be evaluated at.

Returns:
    float, list: The group index at the target wavelength(s).


return spc.c / self.ng(wavelength)<EOL>

The group velocities with respect to wavelength.

Args:
    wavelength (float, list, None): The wavelength(s) the group
        velocities will be evaluated at.

Returns:
    float, list: The group velocities at the target wavelength(s).


g = (wavelength*<NUM_LIT>)**<NUM_LIT>/(<NUM_LIT>*spc.pi*spc.c**<NUM_LIT>) * self.nDer2(wavelength)<EOL>return g<EOL>

The group velocity dispersion (GVD) with respect to wavelength.

Args:
    wavelength (float, list, None): The wavelength(s) the GVD will
        be evaluated at.

Returns:
    float, list: The GVD at the target wavelength(s).


return <NUM_LIT>*spc.pi*self.n(wavelength)/(wavelength*<NUM_LIT>)<EOL>

The propagation constant with respect to wavelength.

Args:
    wavelength (float, list, None): The wavelength(s) the
        propagation constant will be evaluated at.

Returns:
    float, list: The propagation constant at the target wavelength(s).


return <NUM_LIT:1.>/self.vg(wavelength)<EOL>

The derivative of the propagation constant with respect to wavelength.

Args:
    wavelength (float, list, None): The wavelength(s) the
        propagation constant will be evaluated at.

Returns:
    float, list: The propagation constant at the target wavelength(s).


return self.gvd(wavelength)<EOL>

The second derivative of the propagation constant with respect to wavelength.

Args:
    wavelength (float, list, None): The wavelength(s) the
        propagation constant will be evaluated at.

Returns:
    float, list: The propagation constant at the target wavelength(s).


return <NUM_LIT>*np.pi / self.n(wavelength)<EOL>

The wave impedance assuming the material is dielectric (not
lossy or magnetic).

Args:
    wavelength (float, list, None): The wavelength(s) the
        propagation constant will be evaluated at.

Returns:
    float, list: The impedance of the material.


n = <NUM_LIT:0.><EOL>for i, c in enumerate(coefficients):<EOL><INDENT>exponent  = <NUM_LIT:2>*i<EOL>n += c / wavelength**exponent<EOL><DEDENT>return n<EOL>

Helpful function to evaluate Cauchy equations.

Args:
    wavelength (float, list, None): The wavelength(s) the
        Cauchy equation will be evaluated at.
    coefficients (list): A list of the coefficients of
        the Cauchy equation.

Returns:
    float, list: The refractive index at the target wavelength(s).


with io.open(os.path.join(PATH_BASE, fpath)) as f:<EOL><INDENT>return f.read()<EOL><DEDENT>

Reads a file within package directories.


contents = read_file(os.path.join('<STR_LIT>', '<STR_LIT>'))<EOL>version = re.search('<STR_LIT>', contents)<EOL>version = version.group(<NUM_LIT:1>).replace('<STR_LIT:U+002CU+0020>', '<STR_LIT:.>').strip()<EOL>return version<EOL>

Returns version number, without module import (which can lead to ImportError
    if some dependencies are unavailable before install.


def get_gravatar_url(obj, size=<NUM_LIT>, default='<STR_LIT>'):

if isinstance(obj, USER_MODEL):<EOL><INDENT>email = obj.email or obj.username<EOL><DEDENT>else:<EOL><INDENT>email = obj<EOL><DEDENT>if email:<EOL><INDENT>return ('<STR_LIT>' %<EOL>(hashlib.md5(email.encode()).hexdigest(), urlencode({'<STR_LIT:size>': size, '<STR_LIT:d>': default})))<EOL><DEDENT>return '<STR_LIT>'<EOL>

See http://ru.gravatar.com/site/implement/images/ for details on options.

:param UserModel, str obj:
:param int size:
:param str default: 404, mm (mystery-man), identicon, monsterid, wavatar, retro, blank
:return:


@register.simple_tag<EOL>def gravatar_get_url(obj, size=<NUM_LIT>, default='<STR_LIT>'):

return get_gravatar_url(obj, size=size, default=default)<EOL>

Returns Gravatar image URL for a given string or UserModel.

    Example:

        {% load gravatar %}
        {% gravatar_get_url user_model %}

    :param UserModel, str obj:
    :param int size:
    :param str default:
    :return:


@register.simple_tag<EOL>def gravatar_get_img(obj, size=<NUM_LIT>, default='<STR_LIT>'):

url = get_gravatar_url(obj, size=size, default=default)<EOL>if url:<EOL><INDENT>return safe('<STR_LIT>' % url)<EOL><DEDENT>return '<STR_LIT>'<EOL>

Returns Gravatar image HTML tag for a given string or UserModel.

    Example:

        {% load gravatar %}
        {% gravatar_get_img user_model %}

    :param UserModel, str obj:
    :param int size:
    :param str default:
    :return:


return model._meta.verbose_name<EOL>

Returns model verbose name singular.

    Example:

        {% load model_meta %}
        {% model_meta_verbose_name my_model %}

    :param Model model:


return model._meta.verbose_name_plural<EOL>

Returns model verbose name plural.

    Example:

        {% load model_meta %}
        {% model_meta_verbose_name_plural my_model %}

    :param Model model:
    :return:


return get_site_url(request=context.get('<STR_LIT>', None))<EOL>

Tries to get a site URL from environment and settings.

    See toolbox.get_site_url() for description.

    Example:

        {% load etc_misc %}
        {% site_url %}


bits = token.split_contents()<EOL>dynamic = False<EOL>if len(bits) >= <NUM_LIT:2>:<EOL><INDENT>dynamic = '<STR_LIT>' in bits[<NUM_LIT:1>]<EOL>if dynamic:<EOL><INDENT>fallback = None<EOL>bits_new = []<EOL>for bit in bits:<EOL><INDENT>if fallback is True:<EOL><INDENT>fallback = bit<EOL>continue<EOL><DEDENT>if bit == '<STR_LIT>':<EOL><INDENT>fallback = True<EOL><DEDENT>else:<EOL><INDENT>bits_new.append(bit)<EOL><DEDENT><DEDENT>if fallback:<EOL><INDENT>fallback = parser.compile_filter(construct_relative_path_(parser, fallback))<EOL><DEDENT>token.contents = '<STR_LIT:U+0020>'.join(bits_new)<EOL><DEDENT><DEDENT>token.contents = token.contents.replace('<STR_LIT>', '<STR_LIT>')<EOL>include_node = do_include(parser, token)<EOL>if dynamic:<EOL><INDENT>include_node = DynamicIncludeNode(<EOL>include_node.template,<EOL>extra_context=include_node.extra_context,<EOL>isolated_context=include_node.isolated_context,<EOL>fallback=fallback or None,<EOL>)<EOL><DEDENT>return include_node<EOL>

Similar to built-in ``include`` template tag, but allowing
    template variables to be used in template name and a fallback template,
    thus making the tag more dynamic.

    .. warning:: Requires Django 1.8+

    Example:

        {% load etc_misc %}
        {% include_ "sub_{{ postfix_var }}.html" fallback "default.html" %}


return _get_model_field_attr('<STR_LIT>', '<STR_LIT>', token)<EOL>

Returns model field verbose name.
    Two notations are acceptable:

        1. Two arguments:
           {% model_field_verbose_name from mymodel.myfield %}

           Is used to render verbose name of `myfield`.

        2. Four arguments:
           {% model_field_verbose_name from mymodel.myfield as myvar %}

           Is used to put `myfield` verbose name into `myvar` template variable.


Returns model field help text.
    Two notations are acceptable:

        1. Two arguments:
           {% model_field_help_text from mymodel.myfield %}

           Is used to render verbose name of `myfield`.

        2. Four arguments:
           {% model_field_help_text from mymodel.myfield as myvar %}

           Is used to put `myfield` verbose name into `myvar` template variable.


return OrderedDict(choices)<EOL>

Helps to define choices for models, that could be addressed
    later as dictionaries.

    To be used in conjunction with `get_choices()`.

    Example:

        class MyModel(models.Model):

            TYPE_ONE = 1
            TYPE_TWO = 2

            TYPES = choices_list(
                (TYPE_ONE, 'Type one title'),
                (TYPE_TWO, 'Type two title'),
            )

            type = models.PositiveIntegerField('My type', choices=get_choices(TYPES), default=TYPE_TWO)

            def get_display_type(self):
                return self.TYPES[self.type]

    :param set|list|tuple choices:
    :rtype: OrderedDict
    :return: Choices ordered dictionary


return tuple((key, val) for key, val in choices_list.items())<EOL>

Returns model field choices from a given choices list.
    Choices list is defined with `choices_list()`.

    :param OrderedDict choices_list:
    :rtype: tuple
    :return: Choices tuple


for _, field in form.fields.items():<EOL><INDENT>attrs_ = dict(attrs)<EOL>for name, val in attrs.items():<EOL><INDENT>if hasattr(val, '<STR_LIT>'):<EOL><INDENT>attrs_[name] = val(field)<EOL><DEDENT><DEDENT>field.widget.attrs = field.widget.build_attrs(attrs_)<EOL><DEDENT>

Applies a given HTML attributes to each field widget of a given form.

    Example:

        set_form_widgets_attrs(my_form, {'class': 'clickable'})


try:<EOL><INDENT>app_name, model_name = model_path.split('<STR_LIT:.>')<EOL><DEDENT>except ValueError:<EOL><INDENT>raise ImproperlyConfigured('<STR_LIT>' % model_path)<EOL><DEDENT>if apps_get_model is None:<EOL><INDENT>model = get_model(app_name, model_name)<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>model = apps_get_model(app_name, model_name)<EOL><DEDENT>except (LookupError, ValueError):<EOL><INDENT>model = None<EOL><DEDENT><DEDENT>if model is None:<EOL><INDENT>raise ImproperlyConfigured('<STR_LIT>' % (model_path, model_name))<EOL><DEDENT>return model<EOL>

Returns a certain model as defined in a string formatted `<app_name>.<model_name>`.

    Example:

        model = get_model_class_from_string('myapp.MyModel')


return get_model_class_from_string(getattr(settings_module, settings_entry_name))<EOL>

Returns a certain model as defined in a given settings module.

    Example:

        myapp/settings.py

            from django.conf import settings

            MY_MODEL = getattr(settings, 'MYAPP_MY_MODEL', 'myapp.MyModel')

        myapp/some.py

            from myapp import settings

            model = get_model_class_from_settings(settings, 'MY_MODEL')


env = partial(environ.get)<EOL>settings_ = partial(getattr, settings)<EOL>domain = None<EOL>scheme = None<EOL>url = None<EOL>for src in (env, settings_):<EOL><INDENT>if url is None:<EOL><INDENT>url = src('<STR_LIT>', None)<EOL><DEDENT>if domain is None:<EOL><INDENT>domain = src('<STR_LIT>', None)<EOL><DEDENT>if scheme is None:<EOL><INDENT>scheme = src('<STR_LIT>', src('<STR_LIT>', None))<EOL><DEDENT><DEDENT>if domain is None and url is not None:<EOL><INDENT>scheme, domain = url.split('<STR_LIT>')[:<NUM_LIT:2>]<EOL><DEDENT>if domain is None:<EOL><INDENT>site = get_current_site(request or DomainGetter(domain))<EOL>domain = site.domain<EOL><DEDENT>if scheme is None and request:<EOL><INDENT>scheme = request.scheme<EOL><DEDENT>if domain is None:<EOL><INDENT>domain = '<STR_LIT>'<EOL><DEDENT>if scheme is None:<EOL><INDENT>scheme = '<STR_LIT:http>'<EOL><DEDENT>domain = domain.rstrip('<STR_LIT:/>')<EOL>return '<STR_LIT>' % (scheme, domain)<EOL>

Tries to get a site URL from environment and settings
    in the following order:

    1. (SITE_PROTO / SITE_SCHEME) + SITE_DOMAIN
    2. SITE_URL
    3. Django Sites contrib
    4. Request object

    :param HttpRequest request: Request object to deduce URL from.
    :rtype: str


name_split = app_name.split('<STR_LIT:.>')<EOL>if name_split[-<NUM_LIT:1>][<NUM_LIT:0>].isupper():  <EOL><INDENT>app_name = '<STR_LIT:.>'.join(name_split[:-<NUM_LIT:2>])<EOL><DEDENT>module = import_module(app_name)<EOL>try:<EOL><INDENT>sub_module = import_module('<STR_LIT>' % (app_name, module_name))<EOL>return sub_module<EOL><DEDENT>except:<EOL><INDENT>if module_has_submodule(module, module_name):  <EOL><INDENT>raise<EOL><DEDENT>return None<EOL><DEDENT>

Returns a module from a given app by its name.

    :param str app_name:
    :param str module_name:
    :rtype: module or None


from django.conf import settings<EOL>submodules = []<EOL>for app in settings.INSTALLED_APPS:<EOL><INDENT>module = import_app_module(app, module_name)<EOL>if module is not None:<EOL><INDENT>submodules.append(module)<EOL><DEDENT><DEDENT>return submodules<EOL>

Imports modules from registered apps using given module name
    and returns them as a list.

    :param str module_name:
    :rtype: list


<EOL>if t.value.upper() in reserved:<EOL><INDENT>t.type = t.value.upper()<EOL>t.value = t.value.upper()<EOL><DEDENT>return t<EOL>


t.value = int(t.value)<EOL>return t<EOL>


t.value = t.value.replace(r'<STR_LIT:\\>', chr(<NUM_LIT>)).replace(r"<STR_LIT>", r"<STR_LIT:'>")[<NUM_LIT:1>:-<NUM_LIT:1>]<EOL>return t<EOL>


t.lexer.lineno += t.value.count("<STR_LIT:\n>")<EOL>


pass<EOL>


p[<NUM_LIT:0>] = {**p[<NUM_LIT:1>], **p[<NUM_LIT:2>]}<EOL>


if len(p) > <NUM_LIT:2>:<EOL><INDENT>if p[<NUM_LIT:1>] == "<STR_LIT>":<EOL><INDENT>postposition = {<EOL>"<STR_LIT>": p[<NUM_LIT:2>]<EOL>}<EOL>rest = p[<NUM_LIT:3>] if p[<NUM_LIT:3>] else {}<EOL><DEDENT>elif p[<NUM_LIT:1>:<NUM_LIT:3>] == ["<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>postposition = {<EOL>"<STR_LIT>": p[<NUM_LIT:3>]<EOL>}<EOL>rest = p[<NUM_LIT:4>] if p[<NUM_LIT:4>] else {}<EOL><DEDENT>else:<EOL><INDENT>breakpoint()<EOL><DEDENT>p[<NUM_LIT:0>] = {**postposition, **rest}<EOL><DEDENT>else:<EOL><INDENT>p[<NUM_LIT:0>] = {}<EOL><DEDENT>

postpositions : LIMIT NUMBER postpositions
              | ORDER BY colspec postpositions
              | empty


p[<NUM_LIT:0>] = {<EOL>"<STR_LIT:type>": "<STR_LIT>",<EOL>"<STR_LIT>": p[<NUM_LIT:2>],<EOL>"<STR_LIT>": p[<NUM_LIT:4>],<EOL>}<EOL>if p[<NUM_LIT:5>]:<EOL><INDENT>p[<NUM_LIT:0>]["<STR_LIT>"] = p[<NUM_LIT:5>]<EOL><DEDENT>

select : SELECT colspec FROM value condition


rest = p[<NUM_LIT:3>] if len(p) > <NUM_LIT:3> else []<EOL>if p[<NUM_LIT:1>] == "<STR_LIT:*>":<EOL><INDENT>p[<NUM_LIT:0>] = [{"<STR_LIT:type>": "<STR_LIT>"}]<EOL><DEDENT>elif isinstance(p[<NUM_LIT:1>], dict) and p[<NUM_LIT:1>].get("<STR_LIT:type>") == "<STR_LIT>":<EOL><INDENT>p[<NUM_LIT:0>] = [p[<NUM_LIT:1>], *rest]<EOL><DEDENT>elif p[<NUM_LIT:1>]:<EOL><INDENT>p[<NUM_LIT:0>] = [<EOL>{<EOL>"<STR_LIT:type>": "<STR_LIT:name>",<EOL>"<STR_LIT:value>": p[<NUM_LIT:1>],<EOL>},<EOL>*rest,<EOL>]<EOL><DEDENT>else:<EOL><INDENT>p[<NUM_LIT:0>] = []<EOL><DEDENT>

colspec : STAR
        | NAME
        | function
        | NAME COMMA colspec
        | function COMMA colspec


if len(p) > <NUM_LIT:2>:<EOL><INDENT>p[<NUM_LIT:0>] = p[<NUM_LIT:2>]<EOL><DEDENT>else:<EOL><INDENT>p[<NUM_LIT:0>] = None<EOL><DEDENT>

condition : WHERE expression
          | empty


p[<NUM_LIT:0>] = {<EOL>"<STR_LIT:type>": "<STR_LIT>",<EOL>"<STR_LIT:value>": {<EOL>"<STR_LIT:name>": p[<NUM_LIT:1>],<EOL>"<STR_LIT>": p[<NUM_LIT:3>],<EOL>},<EOL>}<EOL>


if len(p) < <NUM_LIT:3>:<EOL><INDENT>p[<NUM_LIT:0>] = p[<NUM_LIT:1>]<EOL><DEDENT>elif len(p) == <NUM_LIT:3>:  <EOL><INDENT>p[<NUM_LIT:0>] = {<EOL>"<STR_LIT>": "<STR_LIT>",<EOL>"<STR_LIT:args>": [p[<NUM_LIT:2>]],<EOL>}<EOL><DEDENT>elif p[<NUM_LIT:1>] == "<STR_LIT:(>":<EOL><INDENT>p[<NUM_LIT:0>] = p[<NUM_LIT:2>]<EOL><DEDENT>else:<EOL><INDENT>p[<NUM_LIT:0>] = {<EOL>"<STR_LIT>": p[<NUM_LIT:2>].lower(),<EOL>"<STR_LIT:args>": [p[<NUM_LIT:1>], p[<NUM_LIT:3>]],<EOL>}<EOL><DEDENT>

expression : value
           | expression AND expression
           | expression OR expression
           | expression EQUALS expression
           | NOT expression
           | LPAREN expression RPAREN


p[<NUM_LIT:0>] = {<EOL>"<STR_LIT:type>": "<STR_LIT:name>" if p.slice[<NUM_LIT:1>].type == "<STR_LIT>" else "<STR_LIT>",<EOL>"<STR_LIT:value>": p[<NUM_LIT:1>],<EOL>}<EOL>

value : NUMBER
      | STRING
      | NAME


return force_text(related_instance)<EOL>

Return the label to show in the "main form" for the
        newly created object.

        Overwrite this to customize the label that is being shown.


def build_attrs(self, extra_attrs=None, **kwargs):

self.attrs = self.widget.build_attrs(extra_attrs=None, **kwargs)<EOL>return self.attrs<EOL>

Helper function for building an attribute dictionary.


def _add_team(session_browser, field, team_name):

session_browser.find_by_id('<STR_LIT>' % field).click()<EOL>session_browser.windows.current = session_browser.windows[<NUM_LIT:1>]<EOL>session_browser.fill_form({'<STR_LIT:name>': team_name})<EOL>session_browser.find_by_css('<STR_LIT>').click()<EOL>session_browser.windows.current = session_browser.windows[<NUM_LIT:0>]<EOL>

Click the add-another button to add a new related Team object.


return session_browser.evaluate_script('<STR_LIT>' % field)<EOL>


def _get_value_label(session_browser, field, value):

return session_browser.evaluate_script(<EOL>'<STR_LIT>' % (field, value)<EOL>)<EOL>

Get the label of an input field's value.


def invoke(published_func, *args, **kwargs):

for i in xrange(<NUM_LIT:100>):<EOL><INDENT>time.sleep(<NUM_LIT:5>)<EOL>try:<EOL><INDENT>return published_func(*args, **kwargs)<EOL>break<EOL><DEDENT>except Exception as e:<EOL><INDENT>traceback.print_exc()<EOL>print(e)<EOL><DEDENT><DEDENT>

helper to repeatedly invoke the function until it becomes available...


for i in xrange(<NUM_LIT:100>):<EOL><INDENT>time.sleep(<NUM_LIT:5>)<EOL>try:<EOL><INDENT>return published_func.map(*args)<EOL>break<EOL><DEDENT>except Exception as e:<EOL><INDENT>traceback.print_exc()<EOL>print(e)<EOL><DEDENT><DEDENT>


def _dataframe_to_csv(writer, dataframe, delimiter, with_header):

encoding_writer = codecs.getwriter('<STR_LIT:utf-8>')(writer)<EOL>dataframe.to_csv(<EOL>path_or_buf=encoding_writer,<EOL>sep=delimiter,<EOL>header=with_header,<EOL>index=False<EOL>)<EOL>

serialize the dataframe with different delimiters


def _dataframe_from_csv(reader, delimiter, with_header, skipspace):

sep = delimiter<EOL>header = <NUM_LIT:0><EOL>if not with_header:<EOL><INDENT>header = None<EOL><DEDENT>return pd.read_csv(<EOL>reader,<EOL>header=header,<EOL>sep=sep,<EOL>skipinitialspace=skipspace,<EOL>encoding='<STR_LIT>'<EOL>)<EOL>

Returns csv data as a pandas Dataframe object


return pd.read_csv(reader, header=None, sep="<STR_LIT:\n>", encoding='<STR_LIT>')<EOL>

Returns PlainText data as a pandas Dataframe object


def serialize_dataframe(writer, data_type_id, dataframe):

_not_none('<STR_LIT>', writer)<EOL>_not_none_or_empty('<STR_LIT>', data_type_id)<EOL>_not_none('<STR_LIT>', dataframe)<EOL>serializer = _SERIALIZERS.get(data_type_id)<EOL>if serializer is None:<EOL><INDENT>raise UnsupportedDatasetTypeError(data_type_id)<EOL><DEDENT>serializer[<NUM_LIT:0>](writer=writer, dataframe=dataframe)<EOL>

Serialize a dataframe.

Parameters
----------
writer : file
    File-like object to write to. Must be opened in binary mode.
data_type_id : dict
    Serialization format to use.
    See the azureml.DataTypeIds class for constants.
dataframe: pandas.DataFrame
    Dataframe to serialize.


_not_none('<STR_LIT>', reader)<EOL>_not_none_or_empty('<STR_LIT>', data_type_id)<EOL>serializer = _SERIALIZERS.get(data_type_id)<EOL>if serializer is None:<EOL><INDENT>raise UnsupportedDatasetTypeError(data_type_id)<EOL><DEDENT>return serializer[<NUM_LIT:1>](reader=reader)<EOL>

Deserialize a dataframe.

Parameters
----------
reader : file
    File-like object to read from. Must be opened in binary mode.
data_type_id : dict
    Serialization format of the raw data.
    See the azureml.DataTypeIds class for constants.

Returns
-------
pandas.DataFrame
    Dataframe object.


_not_none_or_empty('<STR_LIT>', data_type_id)<EOL>return _SERIALIZERS.get(data_type_id) is not None<EOL>

Return if a serializer is available for the specified format.


def __init__(self, workspace=None, metadata=None):

_not_none('<STR_LIT>', metadata)<EOL>_not_none('<STR_LIT>', workspace)<EOL>self.workspace = workspace<EOL>self._metadata = metadata<EOL>if is_supported(self.data_type_id):<EOL><INDENT>self.to_dataframe = self._to_dataframe<EOL><DEDENT>if not self.is_example:<EOL><INDENT>self.update_from_raw_data = self._update_from_raw_data<EOL>self.update_from_dataframe = self._update_from_dataframe<EOL><DEDENT>

INTERNAL USE ONLY. Initialize a dataset.

Parameters
----------
workspace : Workspace
    Parent workspace of the dataset.
metadata : dict
    Dictionary of dataset metadata as returned by the REST API.


return self.workspace._rest.open_dataset_contents(self.contents_url)<EOL>

Open and return a stream for the dataset contents.


return self.workspace._rest.read_dataset_contents_binary(self.contents_url)<EOL>

Read and return the dataset contents as binary.


return self.workspace._rest.read_dataset_contents_text(self.contents_url)<EOL>

Read and return the dataset contents as text.


with self.open() as reader:<EOL><INDENT>return deserialize_dataframe(reader, self.data_type_id)<EOL><DEDENT>

Read and return the dataset contents as a pandas DataFrame.


def _update_from_dataframe(self, dataframe, data_type_id=None, name=None,<EOL>description=None):

_not_none('<STR_LIT>', dataframe)<EOL>if data_type_id is None:<EOL><INDENT>data_type_id = self.data_type_id<EOL><DEDENT>if name is None:<EOL><INDENT>name = self.name<EOL><DEDENT>if description is None:<EOL><INDENT>description = self.description<EOL><DEDENT>try:<EOL><INDENT>output = BytesIO()<EOL>serialize_dataframe(output, data_type_id, dataframe)<EOL>raw_data = output.getvalue()<EOL><DEDENT>finally:<EOL><INDENT>output.close()<EOL><DEDENT>self._upload_and_refresh(raw_data, data_type_id, name, description)<EOL>

Serialize the specified DataFrame and replace the existing dataset.

Parameters
----------
dataframe : pandas.DataFrame
    Data to serialize.
data_type_id : str, optional
    Format to serialize to.
    If None, the existing format is preserved.
    Supported formats are:
        'PlainText'
        'GenericCSV'
        'GenericTSV'
        'GenericCSVNoHeader'
        'GenericTSVNoHeader'
    See the azureml.DataTypeIds class for constants.
name : str, optional
    Name for the dataset.
    If None, the name of the existing dataset is used.
description : str, optional
    Description for the dataset.
    If None, the name of the existing dataset is used.


def _update_from_raw_data(self, raw_data, data_type_id=None, name=None,<EOL>description=None):

_not_none('<STR_LIT>', raw_data)<EOL>if data_type_id is None:<EOL><INDENT>data_type_id = self.data_type_id<EOL><DEDENT>if name is None:<EOL><INDENT>name = self.name<EOL><DEDENT>if description is None:<EOL><INDENT>description = self.description<EOL><DEDENT>self._upload_and_refresh(raw_data, data_type_id, name, description)<EOL>

Upload already serialized raw data and replace the existing dataset.

Parameters
----------
raw_data: bytes
    Dataset contents to upload.
data_type_id : str
    Serialization format of the raw data.
    If None, the format of the existing dataset is used.
    Supported formats are:
        'PlainText'
        'GenericCSV'
        'GenericTSV'
        'GenericCSVNoHeader'
        'GenericTSVNoHeader'
        'ARFF'
    See the azureml.DataTypeIds class for constants.
name : str, optional
    Name for the dataset.
    If None, the name of the existing dataset is used.
description : str, optional
    Description for the dataset.
    If None, the name of the existing dataset is used.


return SourceDataset.Location(self._metadata['<STR_LIT>'])<EOL>


return self._metadata['<STR_LIT>']<EOL>


return self._metadata['<STR_LIT:Name>']<EOL>


return self._metadata['<STR_LIT>']<EOL>

Serialization format for the dataset.
See the azureml.DataTypeIds class for constants.


return self._metadata['<STR_LIT>']<EOL>

Size in bytes of the serialized dataset contents.


loc = self.download_location<EOL>return loc.base_uri + loc.location + loc.access_credential<EOL>


return self.dataset_id.startswith(_GLOBAL_WORKSPACE_ID)<EOL>

True for an example dataset, False for user created.


def __init__(self, workspace, example_filter=None):

_not_none('<STR_LIT>', workspace)<EOL>self.workspace = workspace<EOL>self._example_filter = example_filter<EOL>

INTERNAL USE ONLY. Initialize a dataset collection.

Parameters
----------
workspace : Workspace
    Parent workspace of the datasets.
example_filter : bool
    True to include only examples.
    False to include only user-created.
    None to include all.


_not_none('<STR_LIT:index>', index)<EOL>datasets = self._get_datasets()<EOL>if isinstance(index, numbers.Integral):<EOL><INDENT>return self._create_dataset(list(datasets)[index])<EOL><DEDENT>else:<EOL><INDENT>for dataset in datasets:<EOL><INDENT>if dataset['<STR_LIT:Name>'] == index:<EOL><INDENT>return self._create_dataset(dataset)<EOL><DEDENT><DEDENT><DEDENT>raise IndexError('<STR_LIT>'.format(index))<EOL>

Retrieve a dataset by index or by name (case-sensitive).


def add_from_dataframe(self, dataframe, data_type_id, name, description):

_not_none('<STR_LIT>', dataframe)<EOL>_not_none_or_empty('<STR_LIT>', data_type_id)<EOL>_not_none_or_empty('<STR_LIT:name>', name)<EOL>_not_none_or_empty('<STR_LIT:description>', description)<EOL>try:<EOL><INDENT>output = BytesIO()<EOL>serialize_dataframe(output, data_type_id, dataframe)<EOL>raw_data = output.getvalue()<EOL><DEDENT>finally:<EOL><INDENT>output.close()<EOL><DEDENT>return self._upload(raw_data, data_type_id, name, description)<EOL>

Serialize the specified DataFrame and upload it as a new dataset.

Parameters
----------
dataframe : pandas.DataFrame
    Data to serialize.
data_type_id : str
    Format to serialize to.
    Supported formats are:
        'PlainText'
        'GenericCSV'
        'GenericTSV'
        'GenericCSVNoHeader'
        'GenericTSVNoHeader'
    See the azureml.DataTypeIds class for constants.
name : str
    Name for the new dataset.
description : str
    Description for the new dataset.

Returns
-------
SourceDataset
    Dataset that was just created.
    Use open(), read_as_binary(), read_as_text() or to_dataframe() on
    the dataset object to get its contents as a stream, bytes, str or
    pandas DataFrame.


def add_from_raw_data(self, raw_data, data_type_id, name, description):

_not_none('<STR_LIT>', raw_data)<EOL>_not_none_or_empty('<STR_LIT>', data_type_id)<EOL>_not_none_or_empty('<STR_LIT:name>', name)<EOL>_not_none_or_empty('<STR_LIT:description>', description)<EOL>return self._upload(raw_data, data_type_id, name, description)<EOL>

Upload already serialized raw data as a new dataset.

Parameters
----------
raw_data: bytes
    Dataset contents to upload.
data_type_id : str
    Serialization format of the raw data.
    Supported formats are:
        'PlainText'
        'GenericCSV'
        'GenericTSV'
        'GenericCSVNoHeader'
        'GenericTSVNoHeader'
        'ARFF'
    See the azureml.DataTypeIds class for constants.
name : str
    Name for the new dataset.
description : str
    Description for the new dataset.

Returns
-------
SourceDataset
    Dataset that was just created.
    Use open(), read_as_binary(), read_as_text() or to_dataframe() on
    the dataset object to get its contents as a stream, bytes, str or
    pandas DataFrame.


def __init__(self, workspace, experiment, node_id, port_name, data_type_id):

_not_none('<STR_LIT>', workspace)<EOL>_not_none('<STR_LIT>', experiment)<EOL>_not_none_or_empty('<STR_LIT>', node_id)<EOL>_not_none_or_empty('<STR_LIT>', port_name)<EOL>_not_none_or_empty('<STR_LIT>', data_type_id)<EOL>self.workspace = workspace<EOL>self.experiment = experiment<EOL>self.node_id = node_id<EOL>self.port_name = port_name<EOL>self.data_type_id = data_type_id<EOL>if is_supported(self.data_type_id):<EOL><INDENT>self.to_dataframe = self._to_dataframe<EOL><DEDENT>

INTERNAL USE ONLY. Initialize an intermediate dataset.

Parameters
----------
workspace : Workspace
    Parent workspace of the dataset.
experiment : Experiment
    Parent experiment of the dataset.
node_id : str
    Module node id from the experiment graph.
port_name : str
    Output port of the module.
data_type_id : str
    Serialization format of the raw data.
    See the azureml.DataTypeIds class for constants.


return self.workspace._rest.open_intermediate_dataset_contents(<EOL>self.workspace.workspace_id,<EOL>self.experiment.experiment_id,<EOL>self.node_id,<EOL>self.port_name<EOL>)<EOL>


return self.workspace._rest.read_intermediate_dataset_contents_binary(<EOL>self.workspace.workspace_id,<EOL>self.experiment.experiment_id,<EOL>self.node_id,<EOL>self.port_name<EOL>)<EOL>


return self.workspace._rest.read_intermediate_dataset_contents_text(<EOL>self.workspace.workspace_id,<EOL>self.experiment.experiment_id,<EOL>self.node_id,<EOL>self.port_name<EOL>)<EOL>


<EOL>data = self.read_as_binary()<EOL>reader = BytesIO(data)<EOL>return deserialize_dataframe(reader, self.data_type_id)<EOL>


def __init__(self, workspace, metadata):

_not_none('<STR_LIT>', workspace)<EOL>_not_none('<STR_LIT>', metadata)<EOL>self.workspace = workspace<EOL>self._metadata = metadata<EOL>

INTERNAL USE ONLY. Initialize an experiment.

Parameters
----------
workspace : Workspace
    Parent workspace of the experiment.
metadata : dict
    Dictionary of experiment metadata as returned by the REST API.


return Experiment.Status(self._metadata['<STR_LIT>'])<EOL>


return self._metadata['<STR_LIT>']<EOL>


return self.experiment_id.startswith(_GLOBAL_WORKSPACE_ID)<EOL>

True for an example experiment, False for user created.


def get_intermediate_dataset(self, node_id, port_name, data_type_id):

return IntermediateDataset(self.workspace, self, node_id, port_name, data_type_id)<EOL>

Get an intermediate dataset.

Parameters
----------
node_id : str
    Module node id from the experiment graph.
port_name : str
    Output port of the module.
data_type_id : str
    Serialization format of the raw data.
    See the azureml.DataTypeIds class for constants.

Returns
-------
IntermediateDataset
    Dataset object.
    Use open(), read_as_binary(), read_as_text() or to_dataframe() on
    the dataset object to get its contents as a stream, bytes, str or
    pandas DataFrame.


INTERNAL USE ONLY. Initialize an experiment collection.

Parameters
----------
workspace : Workspace
    Parent workspace of the experiments.
example_filter : bool
    True to include only examples.
    False to include only user-created.
    None to include all.


_not_none('<STR_LIT:index>', index)<EOL>experiments = self._get_experiments()<EOL>if isinstance(index, numbers.Integral):<EOL><INDENT>return self._create_experiment(list(experiments)[index])<EOL><DEDENT>else:<EOL><INDENT>for experiment in experiments:<EOL><INDENT>if experiment['<STR_LIT>'] == index:<EOL><INDENT>return self._create_experiment(experiment)<EOL><DEDENT><DEDENT><DEDENT>raise IndexError('<STR_LIT>'.format(index))<EOL>

Retrieve an experiment by index or by id.


def __init__(self, workspace_id = None, authorization_token = None, endpoint=None):

workspace_id, authorization_token, endpoint, management_endpoint = _get_workspace_info(workspace_id, authorization_token, endpoint, None)<EOL>_not_none_or_empty('<STR_LIT>', workspace_id)<EOL>_not_none_or_empty('<STR_LIT>', authorization_token)<EOL>_not_none_or_empty('<STR_LIT>', endpoint)<EOL>self.workspace_id = workspace_id<EOL>self.authorization_token = authorization_token<EOL>self.api_endpoint = endpoint<EOL>self.management_endpoint = management_endpoint<EOL>self._rest = _RestClient(endpoint, authorization_token)<EOL>self.datasets = Datasets(workspace=self)<EOL>self.user_datasets = Datasets(workspace=self, example_filter=False)<EOL>self.example_datasets = Datasets(workspace=self, example_filter=True)<EOL>self.experiments = Experiments(workspace=self)<EOL>self.user_experiments = Experiments(workspace=self, example_filter=False)<EOL>self.example_experiments = Experiments(workspace=self, example_filter=True)<EOL>

Initialize a workspace.

Parameters
----------
workspace_id : str
    Unique identifier for the existing workspace. Can be obtained from
    the URL in ML Studio when editing a workspace.
authorization_token: str
    Access token for the workspace. Can be the primary or secondary
    token managed in ML Studio.
endpoint: str
    URL of the endpoint to connect to. Specify this only if you host
    ML Studio on your own server(s).

Parameters that are omitted will be read from ~/.azureml/settings.ini:
[workspace]
id = abcd1234
authorization_token = abcd1234
endpoint = https://studio.azureml.net


api_path = self.EXPERIMENTS_URI_FMT.format(workspace_id)<EOL>return self._send_get_req(api_path)<EOL>

Runs HTTP GET request to retrieve the list of experiments.


api_path = self.DATASOURCES_URI_FMT.format(workspace_id)<EOL>return self._send_get_req(api_path)<EOL>

Runs HTTP GET request to retrieve the list of datasets.


def get_dataset(self, workspace_id, dataset_id):

api_path = self.DATASOURCE_URI_FMT.format(workspace_id, dataset_id)<EOL>return self._send_get_req(api_path)<EOL>

Runs HTTP GET request to retrieve a single dataset.


def publish(func_or_workspace_id, workspace_id_or_token = None, workspace_token_or_none = None, files=(), endpoint=None):

if not callable(func_or_workspace_id):<EOL><INDENT>def do_publish(func):<EOL><INDENT>func.service = _publish_worker(func, files, func_or_workspace_id, workspace_id_or_token, endpoint)<EOL>return func<EOL><DEDENT>return do_publish<EOL><DEDENT>return _publish_worker(func_or_workspace_id, files, workspace_id_or_token, workspace_token_or_none, endpoint)<EOL>

publishes a callable function or decorates a function to be published.

Returns a callable, iterable object.  Calling the object will invoke the published service.
Iterating the object will give the API URL, API key, and API help url.

To define a function which will be published to Azure you can simply decorate it with
the @publish decorator.  This will publish the service, and then future calls to the
function will run against the operationalized version of the service in the cloud.

>>> @publish(workspace_id, workspace_token)
>>> def func(a, b):
>>>    return a + b

After publishing you can then invoke the function using:
func.service(1, 2)

Or continue to invoke the function locally:
func(1, 2)

You can also just call publish directly to publish a function:

>>> def func(a, b): return a + b
>>>
>>> res = publish(func, workspace_id, workspace_token)
>>>
>>> url, api_key, help_url = res
>>> res(2, 3)
5
>>> url, api_key, help_url = res.url, res.api_key, res.help_url

The returned result will be the published service.

You can specify a list of files which should be published along with the function.
The resulting files will be stored in a subdirectory called 'Script Bundle'.  The
list of files can be one of:
    (('file1.txt', None), )                      # file is read from disk
    (('file1.txt', b'contents'), )              # file contents are provided
    ('file1.txt', 'file2.txt')                  # files are read from disk, written with same filename
    ((('file1.txt', 'destname.txt'), None), )   # file is read from disk, written with different destination name

The various formats for each filename can be freely mixed and matched.


def service(url, api_key, help_url = None):

def do_publish(func):<EOL><INDENT>return published(url, api_key, help_url, func, None)<EOL><DEDENT>return do_publish<EOL>

Marks a function as having been published and causes all invocations to go to the remote
operationalized service.

>>> @service(url, api_key)
>>> def f(a, b):
>>>     pass


def l(func):<EOL><INDENT>if hasattr(func, '<STR_LIT>'):<EOL><INDENT>func.__annotations__.update(args)<EOL><DEDENT>else:<EOL><INDENT>func.__annotations__ = args<EOL><DEDENT>return func<EOL><DEDENT>return l<EOL>

Specifies the types used for the arguments of a published service.

@types(a=int, b = str)
def f(a, b):
    pass


def l(func):<EOL><INDENT>if hasattr(func, '<STR_LIT>'):<EOL><INDENT>func.__annotations__['<STR_LIT>'] = type<EOL><DEDENT>else:<EOL><INDENT>func.__annotations__ = {'<STR_LIT>': type}<EOL><DEDENT>return func<EOL><DEDENT>return l<EOL>

Specifies the return type for a published service.

@returns(int)
def f(...):
    pass


def do_attach(func):<EOL><INDENT>if hasattr(func, '<STR_LIT>'):<EOL><INDENT>func.__attachments__.append((name, contents))<EOL><DEDENT>else:<EOL><INDENT>func.__attachments__ = [(name, contents)]<EOL><DEDENT>return func<EOL><DEDENT>return do_attach<EOL>

attaches a file to the payload to be uploaded.

If contents is omitted the file is read from disk.
If name is a tuple it specifies the on-disk filename and the destination filename.


def l(func):<EOL><INDENT>func.__service_id__ = id<EOL>return func<EOL><DEDENT>return l<EOL>

Specifies the service ID to enable re-publishing to the same end point.
Can be applied to the function which is being published:

@publish(...)
@service_id('e5dd3903-796f-4544-b7aa-f4e08b2cc639')
def myfunc():
    return 42

When the function is published it will replace any existing instances of the
function.


def l(func):<EOL><INDENT>func.__service_name__ = name<EOL>return func<EOL><DEDENT>return l<EOL>

Provides a friendly name for the published web service which can include spaces and other characters illegal for Python functions.


def l(func):<EOL><INDENT>func.__dataframe_schema__ = args<EOL>return func<EOL><DEDENT>return l<EOL>

Indicates that the function operations on a data frame.  The function
    will receive a single input in the form of a data frame, and should return
    a data frame object.  The schema of the data frame is specified with this
    decorator.

@publish(...)
@dataframe_service(a = int, b = int)
def myfunc(df):
    return pandas.DataFrame([df['a'][i] + df['b'][i] for i in range(df.shape[0])])


def l(func):<EOL><INDENT>func.__input_name__ = name<EOL>return func<EOL><DEDENT>return l<EOL>

specifies the name of the input the web service expects to receive.  Defaults to 'input1


def l(func):<EOL><INDENT>func.__output_name__ = name<EOL>return func<EOL><DEDENT>return l<EOL>


cur_byte = <NUM_LIT:0><EOL>byte_code = code.co_code<EOL>names = set()<EOL>while cur_byte < len(byte_code):<EOL><INDENT>op = ord(byte_code[cur_byte])<EOL>if op >= dis.HAVE_ARGUMENT:<EOL><INDENT>if op == _LOAD_GLOBAL:<EOL><INDENT>oparg = ord(byte_code[cur_byte + <NUM_LIT:1>]) + (ord(byte_code[cur_byte + <NUM_LIT:2>]) << <NUM_LIT:8>)<EOL>name = code.co_names[oparg]<EOL>names.add(name)<EOL><DEDENT>cur_byte += <NUM_LIT:2><EOL><DEDENT>cur_byte += <NUM_LIT:1><EOL><DEDENT>return names<EOL>

walks the byte code to find the variables which are actually globals


call_args = [self._map_args(*cur_args)  for cur_args in zip(*args)]<EOL>r = self._invoke(call_args)<EOL>ret_type = _get_annotation('<STR_LIT>', self.func)<EOL>output_name = getattr(self.func, '<STR_LIT>', '<STR_LIT>')<EOL>return [_decode_response(<EOL>r['<STR_LIT>'][output_name]['<STR_LIT:value>'].get("<STR_LIT>"), <EOL>r['<STR_LIT>'][output_name]['<STR_LIT:value>'].get("<STR_LIT>"), <EOL>x, <EOL>ret_type) <EOL>for x in r['<STR_LIT>']['<STR_LIT>']['<STR_LIT:value>']['<STR_LIT>']]<EOL>

maps the function onto multiple inputs.  The input should be multiple sequences.  The
sequences will be zipped together forming the positional arguments for the call.  This is
equivalent to map(func, ...) but is executed with a single network call.


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


mem_info = dict()<EOL>for k, v in psutil.virtual_memory().__dict__.iteritems():<EOL><INDENT>mem_info[k] = int(v)<EOL><DEDENT>return mem_info<EOL>

Determine memory specifications of the machine.

    Returns
    -------
    mem_info : dictonary
        Holds the current values for the total, free and used memory of the system.


mem_free = memory()['<STR_LIT>']<EOL>if mem_free > <NUM_LIT>:<EOL><INDENT>chunk_size = int(((mem_free - <NUM_LIT>) * <NUM_LIT:1000>) / (<NUM_LIT:4> * n * N))<EOL>return chunk_size<EOL><DEDENT>elif mem_free > <NUM_LIT>:<EOL><INDENT>chunk_size = int(((mem_free - <NUM_LIT>) * <NUM_LIT:1000>) / (<NUM_LIT:4> * n * N))<EOL>return chunk_size<EOL><DEDENT>elif mem_free > <NUM_LIT>:<EOL><INDENT>chunk_size = int(((mem_free - <NUM_LIT>) * <NUM_LIT:1000>) / (<NUM_LIT:4> * n * N))<EOL>return chunk_size<EOL><DEDENT>elif mem_free > <NUM_LIT>:<EOL><INDENT>chunk_size = int(((mem_free - <NUM_LIT>) * <NUM_LIT:1000>) / (<NUM_LIT:4> * n * N))<EOL>return chunk_size<EOL><DEDENT>elif mem_free > <NUM_LIT>:<EOL><INDENT>chunk_size = int(((mem_free - <NUM_LIT>) * <NUM_LIT:1000>) / (<NUM_LIT:4> * n * N))<EOL>return chunk_size<EOL><DEDENT>elif mem_free > <NUM_LIT>:<EOL><INDENT>chunk_size = int(((mem_free - <NUM_LIT>) * <NUM_LIT:1000>) / (<NUM_LIT:4> * n * N))<EOL>return chunk_size<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>exit(<NUM_LIT:1>)<EOL><DEDENT>

Given a two-dimensional array with a dimension of size 'N',
        determine the number of rows or columns that can fit into memory.

    Parameters
    ----------
    N : int
        The size of one of the dimensions of a two-dimensional array.

    n : int
        The number of arrays of size 'N' times 'chunk_size' that can fit in memory.

    Returns
    -------
    chunk_size : int
        The size of the dimension orthogonal to the one of size 'N'.


data = np.atleast_2d(data)<EOL>nearest_distances = kneighbors_graph(data, <NUM_LIT:1>, mode = '<STR_LIT>', metric = metric, include_self = False).data<EOL>median_min_dist = np.median(nearest_distances, overwrite_input = True)<EOL>return round(median_min_dist, <NUM_LIT:4>)<EOL>

This function computes a graph of nearest-neighbors for each sample point in
        'data' and returns the median of the distribution of distances between those
        nearest-neighbors, the distance metric being specified by 'metric'.

    Parameters
    ----------
    data : array of shape (n_samples, n_features)
        The data-set, a fraction of whose sample points will be extracted
        by density sampling.

    metric : string
        The distance metric used to determine the nearest-neighbor to each data-point.
        The DistanceMetric class defined in scikit-learn's library lists all available
        metrics.

    Returns
    -------
    median_min_dist : float
        The median of the distribution of distances between nearest-neighbors.


def get_local_densities(data, kernel_mult = <NUM_LIT>, metric = '<STR_LIT>'):

data = np.atleast_2d(data)<EOL>assert isinstance(kernel_mult, numbers.Real) and kernel_mult > <NUM_LIT:0><EOL>kernel_width = kernel_mult * median_min_distance(data, metric)<EOL>N_samples = data.shape[<NUM_LIT:0>]<EOL>if <NUM_LIT> * get_chunk_size(N_samples, <NUM_LIT:1>) > N_samples:<EOL><INDENT>A = radius_neighbors_graph(data, kernel_width, mode = '<STR_LIT>', metric = metric, include_self = True)<EOL>rows, _ = A.nonzero()<EOL>with NamedTemporaryFile('<STR_LIT:w>', delete = True, dir = '<STR_LIT>') as file_name:<EOL><INDENT>fp = np.memmap(file_name, dtype = int, mode = '<STR_LIT>', shape = rows.shape)<EOL>fp[:] = rows[:]<EOL>_, counts = np.unique(fp, return_counts = True)<EOL><DEDENT>local_densities = np.zeros(N_samples, dtype = int)<EOL>for i in xrange(N_samples):<EOL><INDENT>local_densities[i] = counts[i]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>local_densities = np.zeros(N_samples, dtype = int)<EOL>chunks_size = get_chunk_size(N_samples, <NUM_LIT:2>)<EOL>for i in xrange(<NUM_LIT:0>, N_samples, chunks_size):<EOL><INDENT>chunk = data[i:min(i + chunks_size, N_samples)]<EOL>D = pairwise_distances(chunk, data, metric, n_jobs = <NUM_LIT:1>)<EOL>D = (D <= kernel_width)<EOL>local_densities[i + np.arange(min(chunks_size, N_samples - i))] = D.sum(axis = <NUM_LIT:1>)<EOL><DEDENT><DEDENT>return local_densities<EOL>

For each sample point of the data-set 'data', estimate a local density in feature
        space by counting the number of neighboring data-points within a particular
        region centered around that sample point.

    Parameters
    ----------
    data : array of shape (n_samples, n_features)
        The data-set, a fraction of whose sample points will be extracted
        by density sampling.

    kernel_mult : float, optional (default = 2.0)
        The kernel multiplier, which determine (in terms of the median of the distribution
        of distances among nearest neighbors) the extent of the regions centered
        around each sample point to consider for the computation of the local density
        associated to that particular sample point.

    metric : string, optional (default = 'manhattan')
        The distance metric used to determine the nearest-neighbor to each data-point.
        The DistanceMetric class defined in scikit-learn's library lists all available
        metrics.

    Returns
    -------
    local_densities : array of shape (n_samples,)
        The i-th entry of this vector corresponds to the local density of the i-th sample
        point in the order of the rows of 'data'.


def density_sampling(data, local_densities = None, metric = '<STR_LIT>', <EOL>kernel_mult = <NUM_LIT>, outlier_percentile = <NUM_LIT>, <EOL>target_percentile = <NUM_LIT>, desired_samples = None):

random_state = np.random.RandomState()<EOL>data = np.atleast_2d(data)<EOL>for x in (kernel_mult, outlier_percentile, target_percentile):<EOL><INDENT>assert isinstance(x, numbers.Real) and x > <NUM_LIT:0><EOL><DEDENT>for x in (outlier_percentile, target_percentile):<EOL><INDENT>assert x <= <NUM_LIT:1.0><EOL><DEDENT>if local_densities is None:<EOL><INDENT>local_densities = get_local_densities(data, kernel_mult, metric)<EOL><DEDENT>if reduce(operator.mul, local_densities.shape, <NUM_LIT:1>) != max(local_densities.shape):<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>local_densities = np.reshape(local_densities, local_densities.size)<EOL><DEDENT>outlier_density = np.percentile(local_densities, outlier_percentile)<EOL>target_density = np.percentile(local_densities, target_percentile)<EOL>samples_kept = np.where(local_densities > outlier_density)[<NUM_LIT:0>]<EOL>N_kept = samples_kept.size    <EOL>local_densities = local_densities[samples_kept]<EOL>if desired_samples is None:<EOL><INDENT>probs = np.divide(target_density + <NUM_LIT:0.0>, local_densities)<EOL>ind = np.where(probs > random_state.uniform(size = N_kept))[<NUM_LIT:0>]<EOL>samples_kept = samples_kept[ind]<EOL><DEDENT>elif desired_samples <= N_kept:<EOL><INDENT>sorted_densities = np.sort(local_densities)<EOL>temp = np.reciprocal(sorted_densities[::-<NUM_LIT:1>].astype(float))<EOL>cdf = np.cumsum(temp)[::-<NUM_LIT:1>]<EOL>target_density = (desired_samples + <NUM_LIT:0.0>) / cdf[<NUM_LIT:0>]<EOL>if target_density > sorted_densities[<NUM_LIT:0>]:<EOL><INDENT>temp = desired_samples - np.arange(<NUM_LIT:1.0>, N_kept + <NUM_LIT:1.0>)<EOL>possible_targets = np.divide(temp, cdf)<EOL>ind = np.argmax(possible_targets < sorted_densities)<EOL>target_density = possible_targets[ind]<EOL><DEDENT>probs = np.divide(target_density + <NUM_LIT:0.0>, local_densities)<EOL>ind = np.where(probs > random_state.uniform(size = N_kept))[<NUM_LIT:0>]<EOL>samples_kept = samples_kept[ind]<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(**locals()))<EOL>exit(<NUM_LIT:1>)<EOL><DEDENT>return samples_kept<EOL>

The i-th sample point of the data-set 'data' is selected by density sampling
        with a probability given by:

                                      | 0 if outlier_density > LD[i];
        P(keep the i-th data-point) = | 1 if outlier_density <= LD[i] <= target_density;
                                      | target_density / LD[i] if LD[i] > target_density.

        Here 'LD[i]' denotes the local density of the i-th sample point of the data-set,
        whereas 'outlier_density' and 'target_density' are computed as particular percentiles
        of that distribution of local densities.

    Parameters
    ----------
    data : array of shape (n_samples, n_features)
        The data-set, a fraction of whose sample points will be extracted
        by density sampling.

    local_densities : array of shape (n_samples,), optional (default = None)
        The i-th entry of this vector corresponds to the local density of the i-th sample
        point in the order of the rows of 'data'.

    metric : string, optional (default = 'manhattan')
        The distance metric used to determine the nearest-neighbor to each data-point.
        The DistanceMetric class defined in scikit-learn's library lists all available
        metrics.

    kernel_mult : float, optional (default = 2.0)
        The kernel multiplier, which determine (in terms of the median of the distribution
        of distances among nearest neighbors) the extent of the regions centered
        around each sample point to consider for the computation of the local density
        associated to that particular sample point.

    outlier_percentile : float, optional (default = 0.01)
        Specify the outlier density as a percentile of the distribution of local densities.

    target_percentile : float, optional (default = 0.05)
        Specifiy the target density as a percentile of the distribution of local densities.
        Relevant only if 'desired_samples' is left unspecified.

    desired_samples : int, optional (default = None)
        The number of samples to be selected from the whole data-set such that members
        of rare populations and members of more common populations are roughly
        equally represented. To that purpose, a target density is computed that to selects about
        'desired_samples' data-points.

    Returns
    -------
    samples_kept : array of shape (n_selected_samples,)
        If the 'i'-th sample point of 'data' has been selected by a given instance of
        density sampling, number 'i' is featured in the array returned by
        the present function.


@click.command()<EOL>@click.option('<STR_LIT>', '<STR_LIT>', help='<STR_LIT>')<EOL>@click.option('<STR_LIT>', '<STR_LIT>',  help='<STR_LIT>')<EOL>@click.option('<STR_LIT>', '<STR_LIT:-c>', nargs=<NUM_LIT:1>, type=click.File('<STR_LIT:w>'), help='<STR_LIT>')<EOL>def discover(scope, loglevel, capture):

if loglevel:<EOL><INDENT>level = getattr(logging, loglevel, None)<EOL>if not level:<EOL><INDENT>print("<STR_LIT>" % loglevel)<EOL>return<EOL><DEDENT>logger.setLevel(level)<EOL><DEDENT>run(scope=scope, capture=capture)<EOL>


if env.getAction() == ACTION_PROBE:<EOL><INDENT>return createProbeMessage(env)<EOL><DEDENT>if env.getAction() == ACTION_PROBE_MATCH:<EOL><INDENT>return createProbeMatchMessage(env)<EOL><DEDENT>if env.getAction() == ACTION_RESOLVE:<EOL><INDENT>return createResolveMessage(env)<EOL><DEDENT>if env.getAction() == ACTION_RESOLVE_MATCH:<EOL><INDENT>return createResolveMatchMessage(env)<EOL><DEDENT>if env.getAction() == ACTION_HELLO:<EOL><INDENT>return createHelloMessage(env)<EOL><DEDENT>if env.getAction() == ACTION_BYE:<EOL><INDENT>return createByeMessage(env)<EOL><DEDENT>

construct a a raw SOAP XML string, given a prepared SoapEnvelope object


try:<EOL><INDENT>dom = minidom.parseString(data)<EOL><DEDENT>except Exception:<EOL><INDENT>return None<EOL><DEDENT>if dom.getElementsByTagNameNS(NS_S, "<STR_LIT>"):<EOL><INDENT>return None<EOL><DEDENT>soapAction = dom.getElementsByTagNameNS(NS_A, "<STR_LIT>")[<NUM_LIT:0>].firstChild.data.strip()<EOL>if soapAction == ACTION_PROBE:<EOL><INDENT>return parseProbeMessage(dom)<EOL><DEDENT>elif soapAction == ACTION_PROBE_MATCH:<EOL><INDENT>return parseProbeMatchMessage(dom)<EOL><DEDENT>elif soapAction == ACTION_RESOLVE:<EOL><INDENT>return parseResolveMessage(dom)<EOL><DEDENT>elif soapAction == ACTION_RESOLVE_MATCH:<EOL><INDENT>return parseResolveMatchMessage(dom)<EOL><DEDENT>elif soapAction == ACTION_BYE:<EOL><INDENT>return parseByeMessage(dom)<EOL><DEDENT>elif soapAction == ACTION_HELLO:<EOL><INDENT>return parseHelloMessage(dom)<EOL><DEDENT>

parse raw XML data string, return a (minidom) xml document


Schedule stopping the thread.
        Use join() to wait, until thread really has been stopped


try:<EOL><INDENT>self._multiInSocket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, self._makeMreq(addr))<EOL><DEDENT>except socket.error:  <EOL><INDENT>pass<EOL><DEDENT>sock = self._createMulticastOutSocket(addr, self._observer.ttl)<EOL>self._multiOutUniInSockets[addr] = sock<EOL>self._poll.register(sock, select.POLLIN)<EOL>


if len(self._queue) == <NUM_LIT:0>:<EOL><INDENT>time.sleep(<NUM_LIT:0.1>)<EOL>return<EOL><DEDENT>msg = self._queue.pop(<NUM_LIT:0>)<EOL>if msg.canSend():<EOL><INDENT>self._sendMsg(msg)<EOL>msg.refresh()<EOL>if not (msg.isFinished()):<EOL><INDENT>self._queue.append(msg)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>self._queue.append(msg)<EOL>time.sleep(<NUM_LIT>)<EOL><DEDENT>


def setRemoteServiceHelloCallback(self, cb, types=None, scopes=None):

self._remoteServiceHelloCallback = cb<EOL>self._remoteServiceHelloCallbackTypesFilter = types<EOL>self._remoteServiceHelloCallbackScopesFilter = scopes<EOL>

Set callback, which will be called when new service appeared online
        and sent Hi message

        typesFilter and scopesFilter might be list of types and scopes.
        If filter is set, callback is called only for Hello messages,
        which match filter

        Set None to disable callback


self._remoteServiceByeCallback = cb<EOL>

Set callback, which will be called when new service appeared online
        and sent Hi message
        Service is passed as a parameter to the callback
        Set None to disable callback


self._remoteServiceDisppearedCallback = cb<EOL>

Set callback, which will be called when new service disappears
        Service uuid is passed as a parameter to the callback
        Set None to disable callback


self._startThreads()<EOL>self._serverStarted = True<EOL>

start the discovery server - should be called before using other functions


self.clearRemoteServices()<EOL>self.clearLocalServices()<EOL>self._stopThreads()<EOL>self._serverStarted = False<EOL>

cleans up and stops the discovery server


for service in list(self._localServices.values()):<EOL><INDENT>self._sendBye(service)<EOL><DEDENT>self._localServices.clear()<EOL>

send Bye messages for the services and remove them


def searchServices(self, types=None, scopes=None, timeout=<NUM_LIT:3>):

if not self._serverStarted:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>self._sendProbe(types, scopes)<EOL>time.sleep(timeout)<EOL>return self._filterServices(list(self._remoteServices.values()), types, scopes)<EOL>

search for services given the TYPES and SCOPES in a given TIMEOUT


def publishService(self, types, scopes, xAddrs):

if not self._serverStarted:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>instanceId = _generateInstanceId()<EOL>service = Service(types, scopes, xAddrs, self.uuid, instanceId)<EOL>self._localServices[self.uuid] = service<EOL>self._sendHello(service)<EOL>time.sleep(<NUM_LIT>)<EOL>

Publish a service with the given TYPES, SCOPES and XAddrs (service addresses)

        if xAddrs contains item, which includes {ip} pattern, one item per IP addres will be sent


def __init__(self, env, addr, port, msgType, initialDelay=<NUM_LIT:0>):

self._env = env<EOL>self._addr = addr<EOL>self._port = port<EOL>self._msgType = msgType<EOL>if msgType == self.UNICAST:<EOL><INDENT>udpRepeat, udpMinDelay, udpMaxDelay, udpUpperDelay =UNICAST_UDP_REPEAT,UNICAST_UDP_MIN_DELAY,UNICAST_UDP_MAX_DELAY,UNICAST_UDP_UPPER_DELAY<EOL><DEDENT>else:<EOL><INDENT>udpRepeat, udpMinDelay, udpMaxDelay, udpUpperDelay =MULTICAST_UDP_REPEAT,MULTICAST_UDP_MIN_DELAY,MULTICAST_UDP_MAX_DELAY,MULTICAST_UDP_UPPER_DELAY<EOL><DEDENT>self._udpRepeat = udpRepeat<EOL>self._udpUpperDelay = udpUpperDelay<EOL>self._t = (udpMinDelay + ((udpMaxDelay - udpMinDelay) * random.random())) / <NUM_LIT:2><EOL>self._nextTime = int(time.time() * <NUM_LIT:1000>) + initialDelay<EOL>

msgType shall be Message.MULTICAST or Message.UNICAST


return self._epr<EOL>


self._epr = epr<EOL>


@wraps(func)<EOL>def wrapped(*args, **kwargs):<EOL><INDENT>"""<STR_LIT>"""<EOL>return func(*args, **kwargs)<EOL><DEDENT>TESTS.append(wrapped)<EOL>return wrapped<EOL>

A decorator for providing a unittesting function/method with every card in
a librarian card library database when it is called.


@wraps(func)<EOL>def wrapped(*args, **kwargs):<EOL><INDENT>"""<STR_LIT>"""<EOL>return func(*args, **kwargs)<EOL><DEDENT>SINGLES.append(wrapped)<EOL>return wrapped<EOL>

A decorator for providing a unittest with a library and have it called only
once.


from argparse import ArgumentParser<EOL>from librarian.library import Library<EOL>import sys<EOL>parser = ArgumentParser(<EOL>description="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", help="<STR_LIT>")<EOL>parser.add_argument("<STR_LIT>", "<STR_LIT>", default="<STR_LIT>",<EOL>help="<STR_LIT>")<EOL>args = parser.parse_args(clargs)<EOL>descovery(args.tests)<EOL>library = Library(args.library)<EOL>cardcount, passes, failures = execute_tests(library)<EOL>print(RESULTS.format(len(SINGLES), len(TESTS), cardcount, passes,<EOL>failures))<EOL>sys.exit(failures)<EOL>


self._access_key = access_key<EOL>self._expiration = None<EOL>self._view_identifiers = []<EOL>self._parameters = []<EOL>self._attributes = {}<EOL>

Constructs a new token builder with the given access key.

        :param access_key: The access key to use.


self._expiration = expiration<EOL>return self<EOL>

Sets the expiration for the constructed token to the given time.

        After this time, the token will no longer be valid. All requests made
        using an expired token will fail.

        :param expiration(datetime.datetime): The time object to use for expiration. Should be aware or in UTC.


self._view_identifiers.append(identifier)<EOL>return self<EOL>

Adds the given view identifier to the list of view identifiers
        permitted by this token.

        If no view identifiers are added to this builder, all views in the
        given access key's project will be able to be loaded. Otherwise, only
        those added will be able to be loaded.

        :param identifier(string): The view identifier to restrict to.


self._parameters.append(parameter)<EOL>return self<EOL>

Adds a data-filtering parameter to this token.

        :param parameter(Parameter): The parameter to add.


self._attributes[name] = value<EOL>return self<EOL>

Sets the given attribute in this token.

        :param name(string): The attribute slug.
        :param value: The attribute's value, which must be serializable to JSON.


key = jwk.JWK(<EOL>kty='<STR_LIT>',<EOL>k=base64url_encode(uuid.UUID(secret_key).bytes),<EOL>)<EOL>header = {<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': self._access_key,<EOL>}<EOL>now = int(time.time())<EOL>payload = {<EOL>'<STR_LIT>': now,<EOL>'<STR_LIT>': now,<EOL>}<EOL>if self._expiration is not None:<EOL><INDENT>payload['<STR_LIT>'] = int(calendar.timegm(self._expiration.utctimetuple()))<EOL><DEDENT>if len(self._view_identifiers) > <NUM_LIT:0>:<EOL><INDENT>payload[VIEW_IDENTIFIERS_CLAIM_NAME] = self._view_identifiers<EOL><DEDENT>if len(self._parameters) > <NUM_LIT:0>:<EOL><INDENT>parameters = []<EOL>for parameter in self._parameters:<EOL><INDENT>serialized = {<EOL>'<STR_LIT>': parameter.field,<EOL>'<STR_LIT>': parameter.op,<EOL>}<EOL>if hasattr(parameter, '<STR_LIT>'):<EOL><INDENT>serialized['<STR_LIT>'] = list(parameter.value)<EOL><DEDENT>else:<EOL><INDENT>serialized['<STR_LIT:value>'] = parameter.value<EOL><DEDENT>parameters.append(serialized)<EOL><DEDENT>payload[PARAMETERS_CLAIM_NAME] = parameters<EOL><DEDENT>if len(self._attributes) > <NUM_LIT:0>:<EOL><INDENT>payload[ATTRIBUTES_CLAIM_NAME] = self._attributes<EOL><DEDENT>tok = jwe.JWE(json_encode(payload), protected=header)<EOL>tok.add_recipient(key)<EOL>return tok.serialize(compact=True)<EOL>

Builds a final copy of the token using the given secret key.

        :param secret_key(string): The secret key that corresponds to this builder's access key.


self.field = field<EOL>self.op = op<EOL>self.value = value<EOL>

Creates a new data-filtering parameter.

        :param field(string): The field name.
        :param op(string): The operation to apply, one of the constants in this class.
        :param value: The value(s) to test against.


def __attrs_post_init__(self, scriptfile=None, env=None):

logger.debug('<STR_LIT>')<EOL>self.scriptname = self.scriptname or scriptfile or SCRIPT_PATH<EOL>if env is None:<EOL><INDENT>self.env = dict.fromkeys(SCRIPT_ENV_KEYS, str('<STR_LIT>'))<EOL><DEDENT>else:<EOL><INDENT>self.env = env<EOL><DEDENT>self.env['<STR_LIT>'] = str()<EOL>self.env['<STR_LIT>'] = str(os.getpid())<EOL>


def script_init(self, lease, state, prefix='<STR_LIT>', medium='<STR_LIT>'):

logger.debug('<STR_LIT>', self.scriptname)<EOL>if self.scriptname is not None:<EOL><INDENT>logger.debug('<STR_LIT>')<EOL>if isinstance(state, int):<EOL><INDENT>reason = STATES2REASONS[state]<EOL><DEDENT>else:<EOL><INDENT>reason = state<EOL><DEDENT>self.env['<STR_LIT>'] = str(reason)<EOL>self.env['<STR_LIT>'] = self.env.get('<STR_LIT>') or str(medium)<EOL>self.env['<STR_LIT>'] = str('<STR_LIT>')<EOL>self.env['<STR_LIT>'] = str(os.getpid())<EOL>for k in LEASEATTRS_SAMEAS_ENVKEYS:<EOL><INDENT>self.env[k] = str(lease.__getattribute__(k))<EOL><DEDENT>for k, v in LEASEATTRS2ENVKEYS.items():<EOL><INDENT>self.env[v] = str(lease.__getattribute__(k))<EOL><DEDENT>self.env.update(ENV_OPTIONS_REQ)<EOL><DEDENT>else:<EOL><INDENT>logger.debug('<STR_LIT>')<EOL><DEDENT>

Initialize environment to pass to the external script.


def script_go(self, scriptname=None, env=None):

scriptname = self.scriptname or scriptname<EOL>if scriptname is not None:<EOL><INDENT>env = self.env or env<EOL>logger.info('<STR_LIT>', scriptname)<EOL>logger.info('<STR_LIT>', env)<EOL>proc = subprocess.Popen([scriptname], env=env,<EOL>stderr=subprocess.STDOUT)<EOL>try:<EOL><INDENT>(stdout, stderr) = proc.communicate()<EOL>return True<EOL><DEDENT>except TypeError as e:<EOL><INDENT>logger.error(e)<EOL><DEDENT><DEDENT>return False<EOL>


if DHCP in packet and (DHCPTypes.get(packet[DHCP].options[<NUM_LIT:0>][<NUM_LIT:1>]) ==<EOL>'<STR_LIT>' or packet[DHCP].options[<NUM_LIT:0>][<NUM_LIT:1>] == "<STR_LIT>"):<EOL><INDENT>logger.debug('<STR_LIT>')<EOL>return True<EOL><DEDENT>return False<EOL>


if DHCP in packet and (DHCPTypes.get(packet[DHCP].options[<NUM_LIT:0>][<NUM_LIT:1>]) ==<EOL>'<STR_LIT>' or packet[DHCP].options[<NUM_LIT:0>][<NUM_LIT:1>] == '<STR_LIT>'):<EOL><INDENT>logger.debug('<STR_LIT>')<EOL>return True<EOL><DEDENT>return False<EOL>


return {k: v for k, v in self.__dict__.items() if k in FSM_ATTRS}<EOL>

Return the self object attributes not inherited as dict.


def reset(self, iface=None, client_mac=None, xid=None, scriptfile=None):

logger.debug('<STR_LIT>')<EOL>if iface is None:<EOL><INDENT>iface = conf.iface<EOL><DEDENT>if client_mac is None:<EOL><INDENT>tempmac = get_if_raw_hwaddr(iface)<EOL>if isinstance(tempmac, tuple) and len(tempmac) == <NUM_LIT:2>:<EOL><INDENT>mac = tempmac[<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>mac = tempmac<EOL><DEDENT>client_mac = str2mac(mac)<EOL><DEDENT>self.client = DHCPCAP(iface=iface, client_mac=client_mac, xid=xid)<EOL>if scriptfile is not None:<EOL><INDENT>self.script = ClientScript(scriptfile)<EOL><DEDENT>else:<EOL><INDENT>self.script = None<EOL><DEDENT>self.time_sent_request = None<EOL>self.discover_attempts = <NUM_LIT:0><EOL>self.request_attempts = <NUM_LIT:0><EOL>self.current_state = STATE_PREINIT<EOL>self.offers = list()<EOL>

Reset object attributes when state is INIT.


def __init__(self, iface=None, server_port=None,<EOL>client_port=None, client_mac=None, xid=None,<EOL>scriptfile=None, delay_selecting=False,<EOL>delay_before_selecting=None,<EOL>timeout_select=None, debug_level=<NUM_LIT:5>, *args, **kargs):

logger.debug('<STR_LIT>')<EOL>super(DHCPCAPFSM, self).__init__(*args, **kargs)<EOL>self.debug_level = debug_level<EOL>self.delay_selecting = delay_selecting<EOL>self.delay_before_selecting = delay_before_selecting<EOL>self.timeout_select = timeout_select<EOL>self.reset(iface, client_mac, xid, scriptfile)<EOL>self.client.server_port = server_port or SERVER_PORT<EOL>self.client.client_port = client_port or CLIENT_PORT<EOL>self.socket_kargs = {<EOL>'<STR_LIT>': '<STR_LIT>'<EOL>'<STR_LIT>'.<EOL>format(self.client.server_port,<EOL>self.client.client_port,<EOL>self.client.client_mac)<EOL>}<EOL>if self.script is not None:<EOL><INDENT>self.script.script_init(self.client.lease, self.current_state)<EOL>self.script.script_go()<EOL><DEDENT>logger.debug('<STR_LIT>', self.threadid)<EOL>

Overwrites Automaton __init__ method.

        [ :rfc:`7844#section-3.4` ] ::
            If the hardware address is reset to a new
            randomized value, the DHCP client SHOULD use the new randomized
            value in the DHCP messages


state = STATES2NAMES[state]<EOL>for timeout_fn_t in self.timeout[state]:<EOL><INDENT>if timeout_fn_t[<NUM_LIT:1>] is not None andtimeout_fn_t[<NUM_LIT:1>].atmt_condname == function.atmt_condname:<EOL><INDENT>logger.debug('<STR_LIT>',<EOL>state, function.atmt_condname, timeout_fn_t[<NUM_LIT:0>])<EOL>return timeout_fn_t[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>return None<EOL>

Workaround to get timeout in the ATMT.timeout class method.


def set_timeout(self, state, function, newtimeout):

state = STATES2NAMES[state]<EOL>for timeout_fn_t in self.timeout[state]:<EOL><INDENT>if timeout_fn_t[<NUM_LIT:1>] is not None andtimeout_fn_t[<NUM_LIT:1>].atmt_condname == function.atmt_condname:<EOL><INDENT>timeout_l = list(timeout_fn_t)<EOL>timeout_l[<NUM_LIT:0>] = newtimeout<EOL>i = self.timeout[state].index(timeout_fn_t)<EOL>self.timeout[state][i] = tuple(timeout_l)<EOL>logger.debug('<STR_LIT>',<EOL>state, function.atmt_condname, newtimeout)<EOL><DEDENT><DEDENT>

Workaround to change timeout values in the ATMT.timeout class method.

self.timeout format is::

    {'STATE': [
        (TIMEOUT0, <function foo>),
        (TIMEOUT1, <function bar>)),
        (None, None)
        ],
    }


assert self.client<EOL>assert self.current_state == STATE_INIT orself.current_state == STATE_SELECTING<EOL>pkt = self.client.gen_discover()<EOL>sendp(pkt)<EOL>if self.discover_attempts < MAX_ATTEMPTS_DISCOVER:<EOL><INDENT>self.discover_attempts += <NUM_LIT:1><EOL><DEDENT>timeout = gen_timeout_resend(self.discover_attempts)<EOL>self.set_timeout(self.current_state,<EOL>self.timeout_selecting,<EOL>timeout)<EOL>


logger.debug('<STR_LIT>')<EOL>pkt = self.offers[<NUM_LIT:0>]<EOL>self.client.handle_offer(pkt)<EOL>

Select an offer from the offers received.

        [:rfc:`2131#section-4.2`]::

            DHCP clients are free to use any strategy in selecting a DHCP
            server among those from which the client receives a DHCPOFFER.

        [:rfc:`2131#section-4.4.1`]::

            The time
            over which the client collects messages and the mechanism used to
            select one DHCPOFFER are implementation dependent.

        Nor [:rfc:`7844`] nor [:rfc:`2131`] specify the algorithm.
        Here, currently the first offer is selected.

        .. todo::

           - Check other implementations algorithm to select offer.


assert self.client<EOL>if self.current_state == STATE_BOUND:<EOL><INDENT>pkt = self.client.gen_request_unicast()<EOL><DEDENT>else:<EOL><INDENT>pkt = self.client.gen_request()<EOL><DEDENT>sendp(pkt)<EOL>logger.debug('<STR_LIT>')<EOL>self.time_sent_request = nowutc()<EOL>logger.info('<STR_LIT>',<EOL>self.client.iface, self.client.client_ip,<EOL>self.client.server_ip, self.client.server_port)<EOL>if self.request_attempts < MAX_ATTEMPTS_REQUEST:<EOL><INDENT>self.request_attempts *= <NUM_LIT:2><EOL>logger.debug('<STR_LIT>',<EOL>self.request_attempts)<EOL><DEDENT>if self.current_state == STATE_RENEWING:<EOL><INDENT>timeout_renewing = gen_timeout_request_renew(self.client.lease)<EOL>self.set_timeout(self.current_state,<EOL>self.timeout_request_renewing,<EOL>timeout_renewing)<EOL><DEDENT>elif self.current_state == STATE_REBINDING:<EOL><INDENT>timeout_rebinding = gen_timeout_request_rebind(self.client.lease)<EOL>self.set_timeout(self.current_state,<EOL>self.timeout_request_rebinding,<EOL>timeout_rebinding)<EOL><DEDENT>else:<EOL><INDENT>timeout_requesting =gen_timeout_resend(self.request_attempts)<EOL>self.set_timeout(self.current_state,<EOL>self.timeout_requesting,<EOL>timeout_requesting)<EOL><DEDENT>

Send request.

        [:rfc:`2131#section-3.1`]::

        a client retransmitting as described in section 4.1 might retransmit
        the DHCPREQUEST message four times, for a total delay of 60 seconds

        .. todo::

           - The maximum number of retransmitted REQUESTs is per state or in
             total?
           - Are the retransmitted REQUESTs independent to the retransmitted
             DISCOVERs?


if isack(pkt):<EOL><INDENT>try:<EOL><INDENT>self.event = self.client.handle_ack(pkt,<EOL>self.time_sent_request)<EOL><DEDENT>except AddrFormatError as err:<EOL><INDENT>logger.error(err)<EOL>raise self.SELECTING()<EOL><DEDENT>logger.info('<STR_LIT>' %<EOL>(self.client.client_ip, self.client.server_ip))<EOL>return True<EOL><DEDENT>return False<EOL>

Process a received ACK packet.

        Not specifiyed in [:rfc:`7844`].
        Probe the offered IP in [:rfc:`2131#section-2.2.`]::

            the allocating
            server SHOULD probe the reused address before allocating the
            address, e.g., with an ICMP echo request, and the client SHOULD
            probe the newly received address, e.g., with ARP.

            The client SHOULD broadcast an ARP
            reply to announce the client's new IP address and clear any
            outdated ARP cache entries in hosts on the client's subnet.

        It is also not specifiyed in [:rfc:`7844`] nor [:rfc:`2131`] how to
        check that the offered IP is valid.

        .. todo::
           - Check that nor ``dhclient`` nor ``systemd-networkd`` send an ARP.
           - Check how other implementations check that the ACK paremeters
             are valid, ie, if the ACK fields match the fields in the OFFER.
           - Check to which state the client should go back to when the
             offered parameters are not valid.


if isnak(pkt):<EOL><INDENT>logger.info('<STR_LIT>',<EOL>self.client.client_ip, self.client.server_ip)<EOL>return True<EOL><DEDENT>return False<EOL>


<EOL>logger.debug('<STR_LIT>')<EOL>if self.current_state is not STATE_PREINIT:<EOL><INDENT>self.reset()<EOL><DEDENT>self.current_state = STATE_INIT<EOL>if self.delay_selecting:<EOL><INDENT>if self.delay_before_selecting is None:<EOL><INDENT>delay_before_selecting = gen_delay_selecting()<EOL><DEDENT>else:<EOL><INDENT>delay_before_selecting = self.delay_before_selecting<EOL><DEDENT><DEDENT>else:<EOL><INDENT>delay_before_selecting = <NUM_LIT:0><EOL><DEDENT>self.set_timeout(self.current_state,<EOL>self.timeout_delay_before_selecting,<EOL>delay_before_selecting)<EOL>if self.timeout_select is not None:<EOL><INDENT>self.set_timeout(STATE_SELECTING,<EOL>self.timeout_selecting,<EOL>self.timeout_select)<EOL><DEDENT>

INIT state.

        [:rfc:`2131#section-4.4.1`]::

            The client SHOULD wait a random time between one and ten
            seconds to desynchronize the use of DHCP at startup

        .. todo::
           - The initial delay is implemented, but probably is not in other
             implementations. Check what other implementations do.


logger.debug('<STR_LIT>')<EOL>self.current_state = STATE_SELECTING<EOL>


logger.debug('<STR_LIT>')<EOL>self.current_state = STATE_REQUESTING<EOL>


logger.debug('<STR_LIT>')<EOL>logger.info('<STR_LIT>', self.client.iface,<EOL>STATES2NAMES[self.current_state])<EOL>self.current_state = STATE_BOUND<EOL>self.client.lease.info_lease()<EOL>if self.script is not None:<EOL><INDENT>self.script.script_init(self.client.lease, self.current_state)<EOL>self.script.script_go()<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>set_net(self.client.lease)<EOL><DEDENT>except Exception as e:<EOL><INDENT>logger.error('<STR_LIT>', exc_info=True)<EOL><DEDENT><DEDENT>


logger.debug('<STR_LIT>')<EOL>self.current_state = STATE_RENEWING<EOL>if self.script is not None:<EOL><INDENT>self.script.script_init(self.client.lease, self.current_state)<EOL>self.script.script_go()<EOL><DEDENT>else:<EOL><INDENT>set_net(self.client.lease)<EOL><DEDENT>


logger.debug('<STR_LIT>')<EOL>self.current_state = STATE_REBINDING<EOL>if self.script is not None:<EOL><INDENT>self.script.script_init(self.client.lease, self.current_state)<EOL>self.script.script_go()<EOL><DEDENT>else:<EOL><INDENT>set_net(self.client.lease)<EOL><DEDENT>


logger.debug('<STR_LIT>')<EOL>self.current_state = STATE_END<EOL>if self.script is not None:<EOL><INDENT>self.script.script_init(self.client.lease, self.current_state)<EOL>self.script.script_go()<EOL><DEDENT>else:<EOL><INDENT>set_net(self.client.lease)<EOL><DEDENT>return<EOL>


logger.debug('<STR_LIT>')<EOL>self.current_state = STATE_ERROR<EOL>if self.script is not None:<EOL><INDENT>self.script.script_init(self.client.lease, self.current_state)<EOL>self.script.script_go()<EOL><DEDENT>set_net(self.client.lease)<EOL>raise self.INIT()<EOL>


logger.debug('<STR_LIT>',<EOL>self.current_state)<EOL>raise self.SELECTING()<EOL>


logger.debug('<STR_LIT>',<EOL>self.current_state)<EOL>if len(self.offers) >= MAX_OFFERS_COLLECTED:<EOL><INDENT>logger.debug('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>raise self.REQUESTING()<EOL><DEDENT>if self.discover_attempts >= MAX_ATTEMPTS_DISCOVER:<EOL><INDENT>logger.debug('<STR_LIT>'<EOL>'<STR_LIT>',<EOL>MAX_ATTEMPTS_DISCOVER, self.discover_attempts)<EOL>if len(self.offers) <= <NUM_LIT:0>:<EOL><INDENT>logger.debug('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>raise self.ERROR()<EOL><DEDENT>logger.debug('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>raise self.REQUESTING()<EOL><DEDENT>logger.debug('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>raise self.SELECTING()<EOL>

Timeout of selecting on SELECTING state.

        Not specifiyed in [:rfc:`7844`].
        See comments in :func:`dhcpcapfsm.DHCPCAPFSM.timeout_request`.


logger.debug("<STR_LIT>",<EOL>self.current_state)<EOL>if self.discover_requests >= MAX_ATTEMPTS_REQUEST:<EOL><INDENT>logger.debug('<STR_LIT>'<EOL>'<STR_LIT>',<EOL>MAX_ATTEMPTS_REQUEST, self.disover_requests)<EOL>raise self.ERROR()<EOL><DEDENT>logger.debug("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>raise self.REQUESTING()<EOL>

Timeout requesting in REQUESTING state.

        Not specifiyed in [:rfc:`7844`]

        [:rfc:`2131#section-3.1`]::

            might retransmit the
            DHCPREQUEST message four times, for a total delay of 60 seconds


logger.debug("<STR_LIT>",<EOL>self.current_state)<EOL>if self.request_attempts >= MAX_ATTEMPTS_REQUEST:<EOL><INDENT>logger.debug('<STR_LIT>'<EOL>'<STR_LIT>',<EOL>MAX_ATTEMPTS_REQUEST, self.disover_requests)<EOL><DEDENT>logger.debug("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>raise self.RENEWING()<EOL>

Timeout of renewing on RENEWING state.

        Same comments as in
        :func:`dhcpcapfsm.DHCPCAPFSM.timeout_requesting`.


logger.debug("<STR_LIT>",<EOL>self.current_state)<EOL>if self.request_attempts >= MAX_ATTEMPTS_REQUEST:<EOL><INDENT>logger.debug('<STR_LIT>'<EOL>'<STR_LIT>',<EOL>MAX_ATTEMPTS_REQUEST, self.disover_requests)<EOL><DEDENT>logger.debug("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>raise self.REBINDING()<EOL>

Timeout of request rebinding on REBINDING state.

        Same comments as in
        :func:`dhcpcapfsm.DHCPCAPFSM.timeout_requesting`.


logger.debug("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>raise self.RENEWING()<EOL>

Timeout renewing time (T1), transition to RENEWING.


logger.debug("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>raise self.REBINDING()<EOL>

Timeout rebinding time (T2), transition to REBINDING.


logger.debug("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>raise self.STATE_INIT()<EOL>

Timeout lease time, transition to INIT.

        Not sending DHCPRELEASE to minimize deanonymization

        [:rfc:`2131#section-4.4.6`]::

            Note that the correct operation
            of DHCP does not depend on the transmission of DHCPRELEASE.


logger.debug("<STR_LIT>")<EOL>if isoffer(pkt):<EOL><INDENT>logger.debug("<STR_LIT>")<EOL>self.offers.append(pkt)<EOL>if len(self.offers) >= MAX_OFFERS_COLLECTED:<EOL><INDENT>logger.debug("<STR_LIT>")<EOL>self.select_offer()<EOL>raise self.REQUESTING()<EOL><DEDENT>logger.debug("<STR_LIT>")<EOL>raise self.SELECTING()<EOL><DEDENT>


logger.debug("<STR_LIT>")<EOL>if self.process_received_ack(pkt):<EOL><INDENT>logger.debug("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>raise self.BOUND()<EOL><DEDENT>


logger.debug("<STR_LIT>")<EOL>if self.process_received_nak(pkt):<EOL><INDENT>logger.debug("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>raise self.INIT()<EOL><DEDENT>


logger.debug('<STR_LIT>',<EOL>self.current_state)<EOL>self.send_discover()<EOL>


logger.debug('<STR_LIT>'<EOL>'<STR_LIT>', self.current_state)<EOL>self.send_request()<EOL>


<EOL>logger.debug('<STR_LIT>')<EOL>self.set_timers()<EOL>

Action on receive ACK requesting in REQUESTING state: set timers.


self.client.lease.sanitize_net_values()<EOL>self.client.lease.set_times(self.time_sent_request)<EOL>self.set_timers()<EOL>

Action on renewing on RENEWING state.

        Not recording lease, but restarting timers.


if isinstance(td, str):<EOL><INDENT>td = float(td)<EOL><DEDENT>td = timedelta(seconds=td)<EOL>future_dt = dt + td<EOL>return future_dt.strftime(DT_PRINT_FORMAT)<EOL>


<EOL>now = datetime.now()<EOL>return now<EOL>


delay = float(random.randint(<NUM_LIT:0>, MAX_DELAY_SELECTING))<EOL>logger.debug('<STR_LIT>', delay)<EOL>logger.debug('<STR_LIT>',<EOL>future_dt_str(nowutc(), delay))<EOL>return delay<EOL>

Generate the delay in seconds in which the DISCOVER will be sent.

    [:rfc:`2131#section-4.4.1`]::

        The client SHOULD wait a random time between one and ten seconds to
        desynchronize the use of DHCP at startup.


timeout = <NUM_LIT:2> ** (attempts + <NUM_LIT:1>) + random.uniform(-<NUM_LIT:1>, +<NUM_LIT:1>)<EOL>logger.debug('<STR_LIT>',<EOL>future_dt_str(nowutc(), timeout))<EOL>return timeout<EOL>

Generate the time in seconds in which DHCPDISCOVER wil be retransmited.

    [:rfc:`2131#section-3.1`]::

        might retransmit the
        DHCPREQUEST message four times, for a total delay of 60 seconds

    [:rfc:`2131#section-4.1`]::

        For example, in a 10Mb/sec Ethernet
        internetwork, the delay before the first retransmission SHOULD be 4
        seconds randomized by the value of a uniform random number chosen
        from the range -1 to +1.  Clients with clocks that provide resolution
        granularity of less than one second may choose a non-integer
        randomization value.  The delay before the next retransmission SHOULD
        be 8 seconds randomized by the value of a uniform number chosen from
        the range -1 to +1.  The retransmission delay SHOULD be doubled with
        subsequent retransmissions up to a maximum of 64 seconds.


time_left = (lease.rebinding_time - lease.renewing_time) * RENEW_PERC<EOL>if time_left < <NUM_LIT>:<EOL><INDENT>time_left = <NUM_LIT><EOL><DEDENT>logger.debug('<STR_LIT>',<EOL>future_dt_str(nowutc(), time_left))<EOL>return time_left<EOL>

Generate time in seconds to retransmit DHCPREQUEST.

    [:rfc:`2131#section-4..4.5`]::

        In both RENEWING and REBINDING states,
        if the client receives no response to its DHCPREQUEST
        message, the client SHOULD wait one-half of the remaining
        time until T2 (in RENEWING state) and one-half of the
        remaining lease time (in REBINDING state), down to a
        minimum of 60 seconds, before retransmitting the
        DHCPREQUEST message.


time_left = (lease.lease_time - lease.rebinding_time) * RENEW_PERC<EOL>if time_left < <NUM_LIT>:<EOL><INDENT>time_left = <NUM_LIT><EOL><DEDENT>logger.debug('<STR_LIT>',<EOL>future_dt_str(nowutc(), time_left))<EOL>return time_left<EOL>


renewing_time = int(lease_time) * RENEW_PERC - elapsed<EOL>range_fuzz = int(lease_time) * REBIND_PERC - renewing_time<EOL>logger.debug('<STR_LIT>', range_fuzz)<EOL>fuzz = random.uniform(-(range_fuzz),<EOL>+(range_fuzz))<EOL>renewing_time += fuzz<EOL>logger.debug('<STR_LIT>', renewing_time)<EOL>return renewing_time<EOL>

Generate RENEWING time.

    [:rfc:`2131#section-4.4.5`]::

        T1
        defaults to (0.5 * duration_of_lease).  T2 defaults to (0.875 *
        duration_of_lease).  Times T1 and T2 SHOULD be chosen with some
        random "fuzz" around a fixed value, to avoid synchronization of
        client reacquisition.


rebinding_time = int(lease_time) * REBIND_PERC - elapsed<EOL>range_fuzz = int(lease_time) - rebinding_time<EOL>logger.debug('<STR_LIT>', range_fuzz)<EOL>fuzz = random.uniform(-(range_fuzz),<EOL>+(range_fuzz))<EOL>rebinding_time += fuzz<EOL>logger.debug('<STR_LIT>', rebinding_time)<EOL>return rebinding_time<EOL>


logger.debug('<STR_LIT>')<EOL>elapsed = (nowutc() - sent_dt).seconds<EOL>if self.renewal_time == '<STR_LIT>':<EOL><INDENT>self.renewal_time = gen_renewing_time(self.lease_time, elapsed)<EOL><DEDENT>if self.rebinding_time == '<STR_LIT>':<EOL><INDENT>self.rebinding_time = gen_rebinding_time(self.lease_time, elapsed)<EOL><DEDENT>self.expiry = future_dt_str(sent_dt, self.lease_time)<EOL>self.renew = future_dt_str(sent_dt, self.renewal_time)<EOL>self.rebind = future_dt_str(sent_dt, self.rebinding_time)<EOL>logger.debug('<STR_LIT>', self.lease_time,<EOL>self.expiry)<EOL>logger.debug('<STR_LIT>',<EOL>self.renewal_time, self.renew)<EOL>logger.debug('<STR_LIT>',<EOL>self.rebinding_time, self.rebind)<EOL>

Set timers for the lease given the time in which the request was sent.

[:rfc:`2131#section-4.4.1`]::

    The client records the lease expiration time
    as the sum of the time at which the original request was
    sent and the duration of the lease from the DHCPACK message.


for k, v in LEASE_ATTRS2LEASE_LOG. items():<EOL><INDENT>logger.debug("<STR_LIT>", v, getattr(self, k))<EOL><DEDENT>for k, v in ENV_OPTIONS_REQ.items():<EOL><INDENT>logger.debug("<STR_LIT>", k)<EOL><DEDENT>logger.info('<STR_LIT>', self.address)<EOL>logger.info('<STR_LIT>', self.subnet_mask_cidr, self.subnet_mask)<EOL>logger.info('<STR_LIT>', self.router)<EOL>logger.info('<STR_LIT>', self.server_id)<EOL>logger.info('<STR_LIT>', self.name_server)<EOL>logger.info('<STR_LIT>', self.domain)<EOL>logger.info('<STR_LIT>', self.lease_time)<EOL>


logger.debug('<STR_LIT>')<EOL>if self.iface is None:<EOL><INDENT>self.iface = conf.iface<EOL><DEDENT>if self.client_mac is None:<EOL><INDENT>_, client_mac = get_if_raw_hwaddr(self.iface)<EOL>self.client_mac = str2mac(client_mac)<EOL><DEDENT>if self.prl is None:<EOL><INDENT>self.prl = PRL<EOL><DEDENT>if self.xid is None:<EOL><INDENT>self.xid = gen_xid()<EOL><DEDENT>logger.debug('<STR_LIT>')<EOL>self.lease.interface = self.iface<EOL>

Initializes attributes after attrs __init__.

        These attributes do not change during the life of the object.


ether_ip = (Ether(src=self.client_mac, dst=BROADCAST_MAC) /<EOL>IP(src=META_ADDR, dst=BROADCAST_ADDR))<EOL>return ether_ip<EOL>

Generates link layer and IP layer part of DHCP packet.

        For broadcast packets is:
            Ether(src=client_mac, dst="ff:ff:ff:ff:ff:ff") /
            IP(src="0.0.0.0", dst="255.255.255.255") /


ether_ip = (Ether(src=self.client_mac, dst=self.server_mac) /<EOL>IP(src=self.client_ip, dst=self.server_ip))<EOL>return ether_ip<EOL>

Generates link layer and IP layer part of DHCP packet.

        For unicast packets is:
            Ether(src=client_mac, dst=server_mac) /
            IP(src=client_ip?, dst=server_ip) /


udp = (UDP(sport=self.client_port, dport=self.server_port))<EOL>return udp<EOL>

Generates UDP layer part of DHCP packet.

        UDP layer is always:
            UDP(sport=68, dport=67) /


bootp = (<EOL>BOOTP(chaddr=[mac2str(self.client_mac)], xid=self.xid)<EOL>)<EOL>return bootp<EOL>

Generates BOOTP layer part of DHCP packet.

        [ :rfc:`7844#section-3.4` ] ::

            The presence of this address is necessary for the proper operation
            of the DHCP service.

        [:rfc:`7844#section-3.`] ::

            MAY contain the Client Identifier option,


bootp = (<EOL>BOOTP(chaddr=[mac2str(self.client_mac)], xid=self.xid,<EOL>ciaddr=self.client_ip)<EOL>)<EOL>return bootp<EOL>

Generates BOOTP layer part of unicast DHCP packet.

        Same comments as in gen_bootp


dhcp_discover = (<EOL>self.gen_ether_ip() /<EOL>self.gen_udp() /<EOL>self.gen_bootp() /<EOL>DHCP(options=[<EOL>("<STR_LIT>", "<STR_LIT>"),<EOL>("<STR_LIT>", mac2str(self.client_mac)),<EOL>("<STR_LIT>", self.prl),<EOL>"<STR_LIT:end>"<EOL>])<EOL>)<EOL>logger.debug('<STR_LIT>', dhcp_discover.summary())<EOL>return dhcp_discover<EOL>

Generate DHCP DISCOVER packet.

[:rfc:`7844#section-3.1`] ::

    SHOULD randomize the ordering of options

    If this can not be implemented
    MAY order the options by option code number (lowest to highest).

[:rfc:`7844#section-3.`] ::

    MAY contain the Parameter Request List option.


dhcp_req = (<EOL>self.gen_ether_ip() /<EOL>self.gen_udp() /<EOL>self.gen_bootp() /<EOL>DHCP(options=[<EOL>("<STR_LIT>", "<STR_LIT>"),<EOL>("<STR_LIT>", mac2str(self.client_mac)),<EOL>("<STR_LIT>", self.prl),<EOL>("<STR_LIT>", self.lease.address),<EOL>("<STR_LIT>", self.lease.server_id),<EOL>"<STR_LIT:end>"])<EOL>)<EOL>logger.debug('<STR_LIT>', dhcp_req.summary())<EOL>return dhcp_req<EOL>

Generate DHCP REQUEST packet.

[:rfc:`7844#section-3.1`] ::

    SHOULD randomize the ordering of options

    If this can not be implemented
    MAY order the options by option code number (lowest to highest).

[:rfc:`7844#section-3.`] ::

    MAY contain the Parameter Request List option.

If in response to a DHCPOFFER,::

    MUST contain the corresponding Server Identifier option
    MUST contain the Requested IP address option.

    If the message is not in response to a DHCPOFFER (BOUND, RENEW),::
    MAY contain a Requested IP address option