return md5(image_key)[:<NUM_LIT:16>]<EOL>

Return the first 16 characters of the md5 hash of image_key.


def __init__(self, verbose_name=None, name=None, width_field=None,<EOL>height_field=None, ppoi_field=None, placeholder_image=None,<EOL>**kwargs):

self.ppoi_field = ppoi_field<EOL>super(VersatileImageField, self).__init__(<EOL>verbose_name, name, width_field, height_field, **kwargs<EOL>)<EOL>self.placeholder_image = placeholder_image<EOL>self.placeholder_image_name = None<EOL>


if self.placeholder_image_name:<EOL><INDENT>return<EOL><DEDENT>placeholder_image_name = None<EOL>placeholder_image = self.placeholder_image<EOL>if placeholder_image:<EOL><INDENT>if isinstance(placeholder_image, OnStoragePlaceholderImage):<EOL><INDENT>name = placeholder_image.path<EOL><DEDENT>else:<EOL><INDENT>name = placeholder_image.image_data.name<EOL><DEDENT>placeholder_image_name = os.path.join(<EOL>VERSATILEIMAGEFIELD_PLACEHOLDER_DIRNAME, name<EOL>)<EOL>if not self.storage.exists(placeholder_image_name):<EOL><INDENT>self.storage.save(<EOL>placeholder_image_name,<EOL>placeholder_image.image_data<EOL>)<EOL><DEDENT><DEDENT>self.placeholder_image_name = placeholder_image_name<EOL>

Process the field's placeholder image.

Ensures the placeholder image has been saved to the same storage class
as the field in a top level folder with a name specified by
settings.VERSATILEIMAGEFIELD_SETTINGS['placeholder_directory_name']

This should be called by the VersatileImageFileDescriptor __get__.
If self.placeholder_image_name is already set it just returns right away.


def pre_save(self, model_instance, add):

file = super(VersatileImageField, self).pre_save(model_instance, add)<EOL>self.update_ppoi_field(model_instance)<EOL>return file<EOL>

Return field's value just before saving.


def update_ppoi_field(self, instance, *args, **kwargs):

<EOL>if not self.ppoi_field:<EOL><INDENT>return<EOL><DEDENT>file = getattr(instance, self.attname)<EOL>ppoi = None<EOL>if file and not isinstance(file, tuple):<EOL><INDENT>if hasattr(file, '<STR_LIT>'):<EOL><INDENT>ppoi = file.ppoi<EOL><DEDENT><DEDENT>if self.ppoi_field:<EOL><INDENT>setattr(instance, self.ppoi_field, ppoi)<EOL><DEDENT>

Update field's ppoi field, if defined.

This method is hooked up this field's pre_save method to update
the ppoi immediately before the model instance (`instance`)
it is associated with is saved.

This field's ppoi can be forced to update with force=True,
which is how VersatileImageField.pre_save calls this method.


def save_form_data(self, instance, data):

to_assign = data<EOL>if data and isinstance(data, tuple):<EOL><INDENT>if data[<NUM_LIT:0>] is None:<EOL><INDENT>current_field = getattr(instance, self.name)<EOL>if data[<NUM_LIT:1>]:<EOL><INDENT>current_field.ppoi = data[<NUM_LIT:1>]<EOL><DEDENT>to_assign = current_field<EOL><DEDENT>elif data[<NUM_LIT:0>] is False:<EOL><INDENT>to_assign = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>to_assign = data[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>super(VersatileImageField, self).save_form_data(instance, to_assign)<EOL>

Handle data sent from MultiValueField forms that set ppoi values.

`instance`: The model instance that is being altered via a form
`data`: The data sent from the form to this field which can be either:
* `None`: This is unset data from an optional field
* A two-position tuple: (image_form_data, ppoi_data)
    * `image_form-data` options:
        * `None` the file for this field is unchanged
        * `False` unassign the file form the field
    * `ppoi_data` data structure:
        * `%(x_coordinate)sx%(y_coordinate)s': The ppoi data to
          assign to the unchanged file


<EOL>defaults = {}<EOL>if self.ppoi_field:<EOL><INDENT>defaults['<STR_LIT>'] = SizedImageCenterpointClickDjangoAdminField<EOL><DEDENT>if kwargs.get('<STR_LIT>') is AdminFileWidget:<EOL><INDENT>del kwargs['<STR_LIT>']<EOL><DEDENT>defaults.update(kwargs)<EOL>return super(VersatileImageField, self).formfield(**defaults)<EOL>


if DJANGO_VERSION > (<NUM_LIT:1>, <NUM_LIT:9>):<EOL><INDENT>value = self.value_from_object(obj)<EOL><DEDENT>else:<EOL><INDENT>value = self._get_val_from_obj(obj)<EOL><DEDENT>return self.get_prep_value(value)<EOL>


self.path = path<EOL>

`path` - An absolute path to an on-disc image.


self.path = path<EOL>self.storage = storage<EOL>

`path` - A path on `storage` to an Image.
`storage` - A django storage class.


return name + '<STR_LIT>'<EOL>

Given the name of the image preview tag, return the HTML id for it.


return name + '<STR_LIT>'<EOL>

Given the name of the primary point of interest tag, return the HTML id for it.


def render(self, name, value, attrs=None, renderer=None):

if self.has_template_widget_rendering:<EOL><INDENT>return super(ClearableFileInputWithImagePreview, self).render(<EOL>name, value, attrs=attrs, renderer=renderer<EOL>)<EOL><DEDENT>else:<EOL><INDENT>context = self.get_context(name, value, attrs)<EOL>return render_to_string(self.template_name, context)<EOL><DEDENT>

Render the widget as an HTML string.

Overridden here to support Django < 1.11.


try:<EOL><INDENT>value.create_on_demand = True<EOL>return value.thumbnail['<STR_LIT>']<EOL><DEDENT>except Exception:<EOL><INDENT>return None<EOL><DEDENT>

Do not fail completely on invalid images


def get_context(self, name, value, attrs):

if self.has_template_widget_rendering:<EOL><INDENT>context = super(ClearableFileInputWithImagePreview, self).get_context(name, value, attrs)<EOL><DEDENT>else:<EOL><INDENT>context = {}<EOL>context['<STR_LIT>'] = {<EOL>'<STR_LIT:name>': name,<EOL>'<STR_LIT>': self.is_hidden,<EOL>'<STR_LIT>': self.is_required,<EOL>'<STR_LIT:value>': self._format_value(value),<EOL>'<STR_LIT>': self.build_attrs(self.attrs, attrs),<EOL>'<STR_LIT>': self.template_name,<EOL>'<STR_LIT:type>': self.input_type,<EOL>}<EOL><DEDENT>checkbox_name = self.clear_checkbox_name(name)<EOL>checkbox_id = self.clear_checkbox_id(checkbox_name)<EOL>context['<STR_LIT>'].update({<EOL>'<STR_LIT>': checkbox_name,<EOL>'<STR_LIT>': checkbox_id,<EOL>'<STR_LIT>': self.is_initial(value),<EOL>'<STR_LIT>': self.input_text,<EOL>'<STR_LIT>': self.initial_text,<EOL>'<STR_LIT>': self.clear_checkbox_label,<EOL>})<EOL>if value and hasattr(value, "<STR_LIT:url>"):<EOL><INDENT>context['<STR_LIT>'].update({<EOL>'<STR_LIT>': self.get_hidden_field_id(name),<EOL>'<STR_LIT>': self.get_point_stage_id(name),<EOL>'<STR_LIT>': self.get_ppoi_id(name),<EOL>'<STR_LIT>': self.get_sized_url(value),<EOL>'<STR_LIT>': self.image_preview_id(name),<EOL>})<EOL><DEDENT>return context<EOL>

Get the context to render this widget with.


def build_attrs(self, base_attrs, extra_attrs=None):

attrs = base_attrs.copy()<EOL>if extra_attrs is not None:<EOL><INDENT>attrs.update(extra_attrs)<EOL><DEDENT>return attrs<EOL>


from importlib import import_module<EOL>from django.apps import apps<EOL>from django.utils.module_loading import module_has_submodule<EOL>for app_config in apps.get_app_configs():<EOL><INDENT>try:<EOL><INDENT>before_import_sizedimage_registry = copy.copy(<EOL>versatileimagefield_registry._sizedimage_registry<EOL>)<EOL>before_import_filter_registry = copy.copy(<EOL>versatileimagefield_registry._filter_registry<EOL>)<EOL>import_module('<STR_LIT>' % app_config.name)<EOL><DEDENT>except Exception:<EOL><INDENT>versatileimagefield_registry._sizedimage_registry =before_import_sizedimage_registry<EOL>versatileimagefield_registry._filter_registry =before_import_filter_registry<EOL>if module_has_submodule(app_config.module, '<STR_LIT>'):<EOL><INDENT>raise<EOL><DEDENT><DEDENT><DEDENT>

Discover versatileimagefield.py modules.

Iterate over django.apps.get_app_configs() and discover
versatileimagefield.py modules.


self._sizedimage_registry = {}  <EOL>self._filter_registry = {}  <EOL>self.name = name<EOL>


def register_sizer(self, attr_name, sizedimage_cls):

if attr_name.startswith(<EOL>'<STR_LIT:_>'<EOL>) or attr_name in self.unallowed_sizer_names:<EOL><INDENT>raise UnallowedSizerName(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % (<EOL>attr_name,<EOL>'<STR_LIT:U+002CU+0020>'.join([<EOL>name<EOL>for name in self.unallowed_sizer_names<EOL>])<EOL>)<EOL>)<EOL><DEDENT>if not issubclass(sizedimage_cls, SizedImage):<EOL><INDENT>raise InvalidSizedImageSubclass(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>if attr_name in self._sizedimage_registry:<EOL><INDENT>raise AlreadyRegistered(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % attr_name<EOL>)<EOL><DEDENT>else:<EOL><INDENT>self._sizedimage_registry[attr_name] = sizedimage_cls<EOL><DEDENT>

Register a new SizedImage subclass (`sizedimage_cls`).

To be used via the attribute (`attr_name`).


if attr_name not in self._sizedimage_registry:<EOL><INDENT>raise NotRegistered(<EOL>'<STR_LIT>' % attr_name<EOL>)<EOL><DEDENT>else:<EOL><INDENT>del self._sizedimage_registry[attr_name]<EOL><DEDENT>

Unregister the SizedImage subclass currently assigned to `attr_name`.

If a SizedImage subclass isn't already registered to `attr_name`
NotRegistered will raise.


def register_filter(self, attr_name, filterimage_cls):

if attr_name.startswith('<STR_LIT:_>'):<EOL><INDENT>raise UnallowedFilterName(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % attr_name<EOL>)<EOL><DEDENT>if not issubclass(filterimage_cls, FilteredImage):<EOL><INDENT>raise InvalidFilteredImageSubclass(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>if attr_name in self._filter_registry:<EOL><INDENT>raise AlreadyRegistered(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % attr_name<EOL>)<EOL><DEDENT>else:<EOL><INDENT>self._filter_registry[attr_name] = filterimage_cls<EOL><DEDENT>

Register a new FilteredImage subclass (`filterimage_cls`).

To be used via the attribute (filters.`attr_name`)


if attr_name not in self._filter_registry:<EOL><INDENT>raise NotRegistered(<EOL>'<STR_LIT>' % attr_name<EOL>)<EOL><DEDENT>else:<EOL><INDENT>del self._filter_registry[attr_name]<EOL><DEDENT>

Unregister the FilteredImage subclass currently assigned to attr_name.

If a FilteredImage subclass isn't already registered to filters.
`attr_name` NotRegistered will raise.


if VERSATILEIMAGEFIELD_POST_PROCESSOR is None:<EOL><INDENT>return image_key<EOL><DEDENT>else:<EOL><INDENT>return VERSATILEIMAGEFIELD_POST_PROCESSOR(image_key)<EOL><DEDENT>

Apply the processor function associated with settings.VER


def get_resized_filename(filename, width, height, filename_key):

try:<EOL><INDENT>image_name, ext = filename.rsplit('<STR_LIT:.>', <NUM_LIT:1>)<EOL><DEDENT>except ValueError:<EOL><INDENT>image_name = filename<EOL>ext = '<STR_LIT>'<EOL><DEDENT>resized_template = "<STR_LIT>"<EOL>if ext.lower() in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>resized_template = resized_template + "<STR_LIT>"<EOL><DEDENT>resized_key = resized_template % ({<EOL>'<STR_LIT>': filename_key,<EOL>'<STR_LIT:width>': width,<EOL>'<STR_LIT>': height,<EOL>'<STR_LIT>': QUAL<EOL>})<EOL>return "<STR_LIT>" % ({<EOL>'<STR_LIT>': image_name,<EOL>'<STR_LIT>': post_process_image_key(resized_key),<EOL>'<STR_LIT>': ext<EOL>})<EOL>

Return the 'resized filename' (according to `width`, `height` and
`filename_key`) in the following format:
`filename`-`filename_key`-`width`x`height`.ext


def get_resized_path(path_to_image, width, height,<EOL>filename_key, storage):

containing_folder, filename = os.path.split(path_to_image)<EOL>resized_filename = get_resized_filename(<EOL>filename,<EOL>width,<EOL>height,<EOL>filename_key<EOL>)<EOL>joined_path = os.path.join(*[<EOL>VERSATILEIMAGEFIELD_SIZED_DIRNAME,<EOL>containing_folder,<EOL>resized_filename<EOL>]).replace('<STR_LIT:U+0020>', '<STR_LIT>')  <EOL>return joined_path<EOL>

Return a `path_to_image` location on `storage` as dictated by `width`, `height`
and `filename_key`


try:<EOL><INDENT>image_name, ext = filename.rsplit('<STR_LIT:.>', <NUM_LIT:1>)<EOL><DEDENT>except ValueError:<EOL><INDENT>image_name = filename<EOL>ext = '<STR_LIT>'<EOL><DEDENT>return "<STR_LIT>" % ({<EOL>'<STR_LIT>': image_name,<EOL>'<STR_LIT>': filename_key,<EOL>'<STR_LIT>': ext<EOL>})<EOL>

Return the 'filtered filename' (according to `filename_key`)
in the following format:
`filename`__`filename_key`__.ext


def get_filtered_path(path_to_image, filename_key, storage):

containing_folder, filename = os.path.split(path_to_image)<EOL>filtered_filename = get_filtered_filename(filename, filename_key)<EOL>path_to_return = os.path.join(*[<EOL>containing_folder,<EOL>VERSATILEIMAGEFIELD_FILTERED_DIRNAME,<EOL>filtered_filename<EOL>])<EOL>path_to_return = path_to_return.replace('<STR_LIT:U+0020>', '<STR_LIT>')<EOL>return path_to_return<EOL>


return FILE_EXTENSION_MAP.get(file_ext, JPEG)<EOL>

Receive a valid image file format and returns a 2-tuple of two strings:
    [0]: Image format (i.e. 'jpg', 'gif' or 'png')
    [1]: InMemoryUploadedFile-friendly save format (i.e. 'image/jpeg')
image_format, in_memory_file_type


try:<EOL><INDENT>for key, size_key in sizes:<EOL><INDENT>size_key_split = size_key.split('<STR_LIT>')<EOL>if size_key_split[-<NUM_LIT:1>] != '<STR_LIT:url>' and (<EOL>'<STR_LIT:x>' not in size_key_split[-<NUM_LIT:1>]<EOL>):<EOL><INDENT>raise InvalidSizeKey(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(size_key)<EOL>)<EOL><DEDENT><DEDENT><DEDENT>except ValueError:<EOL><INDENT>raise InvalidSizeKeySet(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(str(sizes))<EOL>)<EOL><DEDENT>return list(set(sizes))<EOL>

Validate a list of size keys.

`sizes`: An iterable of 2-tuples, both strings. Example:
[
    ('large', 'url'),
    ('medium', 'crop__400x400'),
    ('small', 'thumbnail__100x100')
]


img_key_split = image_key.split('<STR_LIT>')<EOL>if '<STR_LIT:x>' in img_key_split[-<NUM_LIT:1>]:<EOL><INDENT>size_key = img_key_split.pop(-<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>size_key = None<EOL><DEDENT>img_url = reduce(getattr, img_key_split, image_instance)<EOL>if size_key:<EOL><INDENT>img_url = img_url[size_key].url<EOL><DEDENT>return img_url<EOL>


def build_versatileimagefield_url_set(image_instance, size_set, request=None):

size_set = validate_versatileimagefield_sizekey_list(size_set)<EOL>to_return = {}<EOL>if image_instance or image_instance.field.placeholder_image:<EOL><INDENT>for key, image_key in size_set:<EOL><INDENT>img_url = get_url_from_image_key(image_instance, image_key)<EOL>if request is not None:<EOL><INDENT>img_url = request.build_absolute_uri(img_url)<EOL><DEDENT>to_return[key] = img_url<EOL><DEDENT><DEDENT>return to_return<EOL>

Return a dictionary of urls corresponding to size_set
- `image_instance`: A VersatileImageFieldFile
- `size_set`: An iterable of 2-tuples, both strings. Example:
    [
        ('large', 'url'),
        ('medium', 'crop__400x400'),
        ('small', 'thumbnail__100x100')
    ]

    The above would lead to the following response:
    {
        'large': 'http://some.url/image.jpg',
        'medium': 'http://some.url/__sized__/image-crop-400x400.jpg',
        'small': 'http://some.url/__sized__/image-thumbnail-100x100.jpg',
    }
- `request`:


try:<EOL><INDENT>rendition_key_set = IMAGE_SETS[key]<EOL><DEDENT>except KeyError:<EOL><INDENT>raise ImproperlyConfigured(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(key)<EOL>)<EOL><DEDENT>else:<EOL><INDENT>return validate_versatileimagefield_sizekey_list(rendition_key_set)<EOL><DEDENT>

Retrieve a validated and prepped Rendition Key Set from
settings.VERSATILEIMAGEFIELD_RENDITION_KEY_SETS


def __init__(self, path_to_image, storage, create_on_demand,<EOL>placeholder_image=None):

self.path_to_image = path_to_image<EOL>self.storage = storage<EOL>self.create_on_demand = create_on_demand<EOL>self.placeholder_image = placeholder_image<EOL>


def process_image(self, image, image_format, **kwargs):

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

Ensure NotImplemented is raised if not overloaded by subclasses.

Arguments:
    * `image`: a PIL Image instance
    * `image_format`: str, a valid PIL format (i.e. 'JPEG' or 'GIF')

Returns a BytesIO representation of the resized image.

Subclasses MUST implement this method.


def preprocess(self, image, image_format):

save_kwargs = {'<STR_LIT>': image_format}<EOL>if hasattr(image, '<STR_LIT>'):<EOL><INDENT>exif_datadict = image._getexif()  <EOL>if exif_datadict is not None:<EOL><INDENT>exif = dict(exif_datadict.items())<EOL>orientation = exif.get(EXIF_ORIENTATION_KEY, None)<EOL>if orientation == <NUM_LIT:3>:<EOL><INDENT>image = image.transpose(Image.ROTATE_180)<EOL><DEDENT>elif orientation == <NUM_LIT:6>:<EOL><INDENT>image = image.transpose(Image.ROTATE_270)<EOL><DEDENT>elif orientation == <NUM_LIT:8>:<EOL><INDENT>image = image.transpose(Image.ROTATE_90)<EOL><DEDENT><DEDENT><DEDENT>save_kwargs['<STR_LIT>'] = image.info.get('<STR_LIT>')<EOL>if hasattr(self, '<STR_LIT>' % image_format):<EOL><INDENT>image, addl_save_kwargs = getattr(<EOL>self,<EOL>'<STR_LIT>' % image_format<EOL>)(image=image)<EOL>save_kwargs.update(addl_save_kwargs)<EOL><DEDENT>return image, save_kwargs<EOL>

Preprocess an image.

An API hook for image pre-processing. Calls any image format specific
pre-processors (if defined). I.E. If `image_format` is 'JPEG', this
method will look for a method named `preprocess_JPEG`, if found
`image` will be passed to it.

Arguments:
    * `image`: a PIL Image instance
    * `image_format`: str, a valid PIL format (i.e. 'JPEG' or 'GIF')

Subclasses should return a 2-tuple:
    * [0]: A PIL Image instance.
    * [1]: A dictionary of additional keyword arguments to be used
           when the instance is saved. If no additional keyword
           arguments, return an empty dict ({}).


def preprocess_GIF(self, image, **kwargs):

if '<STR_LIT>' in image.info:<EOL><INDENT>save_kwargs = {'<STR_LIT>': image.info['<STR_LIT>']}<EOL><DEDENT>else:<EOL><INDENT>save_kwargs = {}<EOL><DEDENT>return (image, save_kwargs)<EOL>

Receive a PIL Image instance of a GIF and return 2-tuple.

Args:
    * [0]: Original Image instance (passed to `image`)
    * [1]: Dict with a transparency key (to GIF transparency layer)


def preprocess_JPEG(self, image, **kwargs):

save_kwargs = {<EOL>'<STR_LIT>': VERSATILEIMAGEFIELD_PROGRESSIVE_JPEG,<EOL>'<STR_LIT>': QUAL<EOL>}<EOL>if image.mode != '<STR_LIT>':<EOL><INDENT>image = image.convert('<STR_LIT>')<EOL><DEDENT>return (image, save_kwargs)<EOL>

Receive a PIL Image instance of a JPEG and returns 2-tuple.

Args:
    * [0]: Image instance, converted to RGB
    * [1]: Dict with a quality key (mapped to the value of `QUAL` as
           defined by the `VERSATILEIMAGEFIELD_JPEG_RESIZE_QUALITY`
           setting)


image = self.storage.open(path_to_image, '<STR_LIT:rb>')<EOL>file_ext = path_to_image.rsplit('<STR_LIT:.>')[-<NUM_LIT:1>]<EOL>image_format, mime_type = get_image_metadata_from_file_ext(file_ext)<EOL>return (<EOL>Image.open(image),<EOL>file_ext,<EOL>image_format,<EOL>mime_type<EOL>)<EOL>

Return a PIL Image instance stored at `path_to_image`.


def save_image(self, imagefile, save_path, file_ext, mime_type):

file_to_save = InMemoryUploadedFile(<EOL>imagefile,<EOL>None,<EOL>'<STR_LIT>' % file_ext,<EOL>mime_type,<EOL>imagefile.tell(),<EOL>None<EOL>)<EOL>file_to_save.seek(<NUM_LIT:0>)<EOL>self.storage.save(save_path, file_to_save)<EOL>

Save an image to self.storage at `save_path`.

Arguments:
    `imagefile`: Raw image data, typically a BytesIO instance.
    `save_path`: The path within self.storage where the image should
                 be saved.
    `file_ext`: The file extension of the image-to-be-saved.
    `mime_type`: A valid image mime type (as found in
                 versatileimagefield.utils)


def create_filtered_image(self, path_to_image, save_path_on_storage):

image, file_ext, image_format, mime_type = self.retrieve_image(<EOL>path_to_image<EOL>)<EOL>image, save_kwargs = self.preprocess(image, image_format)<EOL>imagefile = self.process_image(image, image_format, save_kwargs)<EOL>self.save_image(imagefile, save_path_on_storage, file_ext, mime_type)<EOL>

Creates a filtered image.
`path_to_image`: The path to the image with the media directory
                 to resize.
`save_path_on_storage`: Where on self.storage to save the filtered
                        image


try:<EOL><INDENT>prepped_filter = dict.__getitem__(self, key)<EOL><DEDENT>except KeyError:<EOL><INDENT>if key not in self.registry._filter_registry:<EOL><INDENT>raise InvalidFilter('<STR_LIT>' % key)<EOL><DEDENT>else:<EOL><INDENT>if not self.original_file_location and getattr(<EOL>settings, '<STR_LIT>', False<EOL>):<EOL><INDENT>filtered_path = None<EOL>prepped_filter = DummyFilter()<EOL><DEDENT>else:<EOL><INDENT>filtered_path = get_filtered_path(<EOL>path_to_image=self.original_file_location,<EOL>filename_key=key,<EOL>storage=self.storage<EOL>)<EOL>filtered_url = self.storage.url(filtered_path)<EOL>filter_cls = self.registry._filter_registry[key]<EOL>prepped_filter = filter_cls(<EOL>path_to_image=self.original_file_location,<EOL>storage=self.storage,<EOL>create_on_demand=self.create_on_demand,<EOL>filename_key=key<EOL>)<EOL>if self.create_on_demand is True:<EOL><INDENT>if cache.get(filtered_url):<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>if not self.storage.exists(filtered_path):<EOL><INDENT>prepped_filter.create_filtered_image(<EOL>path_to_image=self.original_file_location,<EOL>save_path_on_storage=filtered_path<EOL>)<EOL><DEDENT>cache.set(<EOL>filtered_url,<EOL><NUM_LIT:1>,<EOL>VERSATILEIMAGEFIELD_CACHE_LENGTH<EOL>)<EOL><DEDENT><DEDENT><DEDENT>for (<EOL>attr_name, sizedimage_cls<EOL>) in iteritems(self.registry._sizedimage_registry):<EOL><INDENT>setattr(<EOL>prepped_filter,<EOL>attr_name,<EOL>sizedimage_cls(<EOL>path_to_image=filtered_path,<EOL>storage=self.storage,<EOL>create_on_demand=self.create_on_demand,<EOL>ppoi=self.ppoi<EOL>)<EOL>)<EOL><DEDENT>self[key] = prepped_filter<EOL><DEDENT><DEDENT>return dict.__getitem__(self, key)<EOL>

Returns a FilteredImage instance built from the FilteredImage subclass
associated with self.registry[key]

If no FilteredImage subclass is associated with self.registry[key],
InvalidFilter will raise.


self.name = name<EOL>self.url = url<EOL>self.storage = storage<EOL>


return self.url<EOL>


def __init__(self, path_to_image, storage, create_on_demand, ppoi=None):

super(SizedImage, self).__init__(<EOL>path_to_image, storage, create_on_demand<EOL>)<EOL>self.ppoi = ppoi<EOL>try:<EOL><INDENT>key = self.get_filename_key()<EOL><DEDENT>except AttributeError:<EOL><INDENT>raise NotImplementedError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>else:<EOL><INDENT>del key<EOL><DEDENT>


return "<STR_LIT>" % (<EOL>str(self.ppoi[<NUM_LIT:0>]).replace('<STR_LIT:.>', '<STR_LIT:->'),<EOL>str(self.ppoi[<NUM_LIT:1>]).replace('<STR_LIT:.>', '<STR_LIT:->')<EOL>)<EOL>


return self.filename_key<EOL>

Return a string used to identify the resized image.


try:<EOL><INDENT>return cls.filename_key_regex<EOL><DEDENT>except AttributeError:<EOL><INDENT>try:<EOL><INDENT>return cls.filename_key<EOL><DEDENT>except AttributeError:   <EOL><INDENT>raise NotImplementedError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT><DEDENT>


raise NotImplementedError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % self.__class__.__name__<EOL>)<EOL>

Ensure attribute assignment is disabled.


try:<EOL><INDENT>width, height = [int(i) for i in key.split('<STR_LIT:x>')]<EOL><DEDENT>except (KeyError, ValueError):<EOL><INDENT>raise MalformedSizedImageKey(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % self.__class__.__name__<EOL>)<EOL><DEDENT>if not self.path_to_image and getattr(<EOL>settings, '<STR_LIT>', False<EOL>):<EOL><INDENT>resized_url = "<STR_LIT>" % (width, height)<EOL>resized_storage_path = resized_url<EOL><DEDENT>else:<EOL><INDENT>resized_storage_path = get_resized_path(<EOL>path_to_image=self.path_to_image,<EOL>width=width,<EOL>height=height,<EOL>filename_key=self.get_filename_key(),<EOL>storage=self.storage<EOL>)<EOL>try:<EOL><INDENT>resized_url = self.storage.url(resized_storage_path)<EOL><DEDENT>except Exception:<EOL><INDENT>resized_url = None<EOL><DEDENT>if self.create_on_demand is True:<EOL><INDENT>if cache.get(resized_url) and resized_url is not None:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>if resized_storage_path and not self.storage.exists(<EOL>resized_storage_path<EOL>):<EOL><INDENT>self.create_resized_image(<EOL>path_to_image=self.path_to_image,<EOL>save_path_on_storage=resized_storage_path,<EOL>width=width,<EOL>height=height<EOL>)<EOL>resized_url = self.storage.url(resized_storage_path)<EOL><DEDENT>cache.set(resized_url, <NUM_LIT:1>, VERSATILEIMAGEFIELD_CACHE_LENGTH)<EOL><DEDENT><DEDENT><DEDENT>return SizedImageInstance(<EOL>name=resized_storage_path,<EOL>url=resized_url,<EOL>storage=self.storage<EOL>)<EOL>

Return a URL to an image sized according to key.

Arguments:
    * `key`: A string in the following format
             '[width-in-pixels]x[height-in-pixels]'
             Example: '400x400'


def process_image(self, image, image_format, save_kwargs,<EOL>width, height):

Process a SizedImage.

Arguments:
    * `image`: a PIL Image instance
    * `image_format`: A valid image mime type (e.g. 'image/jpeg')
    * `save_kwargs`: A dict of any keyword arguments needed during
                     save that are provided by the preprocessing API.
    * `width`: value in pixels (as int) representing the intended width
    * `height`: value in pixels (as int) representing the intended
                height

Returns a BytesIO representation of the resized image.

Subclasses MUST implement this method.


def create_resized_image(self, path_to_image, save_path_on_storage,<EOL>width, height):

image, file_ext, image_format, mime_type = self.retrieve_image(<EOL>path_to_image<EOL>)<EOL>image, save_kwargs = self.preprocess(image, image_format)<EOL>imagefile = self.process_image(<EOL>image=image,<EOL>image_format=image_format,<EOL>save_kwargs=save_kwargs,<EOL>width=width,<EOL>height=height<EOL>)<EOL>self.save_image(imagefile, save_path_on_storage, file_ext, mime_type)<EOL>

Create a resized image.

`path_to_image`: The path to the image with the media directory to
                 resize. If `None`, the
                 VERSATILEIMAGEFIELD_PLACEHOLDER_IMAGE will be used.
`save_path_on_storage`: Where on self.storage to save the resized image
`width`: Width of resized image (int)
`height`: Desired height of resized image (int)
`filename_key`: A string that will be used in the sized image filename
                to signify what operation was done to it.
                Examples: 'crop' or 'scale'


return "<STR_LIT>".format(<EOL>key=self.filename_key,<EOL>ppoi=self.ppoi_as_str()<EOL>)<EOL>

Return the filename key for cropped images.


def crop_on_centerpoint(self, image, width, height, ppoi=(<NUM_LIT:0.5>, <NUM_LIT:0.5>)):

ppoi_x_axis = int(image.size[<NUM_LIT:0>] * ppoi[<NUM_LIT:0>])<EOL>ppoi_y_axis = int(image.size[<NUM_LIT:1>] * ppoi[<NUM_LIT:1>])<EOL>center_pixel_coord = (ppoi_x_axis, ppoi_y_axis)<EOL>orig_aspect_ratio = float(<EOL>image.size[<NUM_LIT:0>]<EOL>) / float(<EOL>image.size[<NUM_LIT:1>]<EOL>)<EOL>crop_aspect_ratio = float(width) / float(height)<EOL>if orig_aspect_ratio >= crop_aspect_ratio:<EOL><INDENT>orig_crop_width = int(<EOL>(crop_aspect_ratio * float(image.size[<NUM_LIT:1>])) + <NUM_LIT:0.5><EOL>)<EOL>orig_crop_height = image.size[<NUM_LIT:1>]<EOL>crop_boundary_top = <NUM_LIT:0><EOL>crop_boundary_bottom = orig_crop_height<EOL>crop_boundary_left = center_pixel_coord[<NUM_LIT:0>] - (orig_crop_width // <NUM_LIT:2>)<EOL>crop_boundary_right = crop_boundary_left + orig_crop_width<EOL>if crop_boundary_left < <NUM_LIT:0>:<EOL><INDENT>crop_boundary_left = <NUM_LIT:0><EOL>crop_boundary_right = crop_boundary_left + orig_crop_width<EOL><DEDENT>elif crop_boundary_right > image.size[<NUM_LIT:0>]:<EOL><INDENT>crop_boundary_right = image.size[<NUM_LIT:0>]<EOL>crop_boundary_left = image.size[<NUM_LIT:0>] - orig_crop_width<EOL><DEDENT><DEDENT>else:<EOL><INDENT>orig_crop_width = image.size[<NUM_LIT:0>]<EOL>orig_crop_height = int(<EOL>(float(image.size[<NUM_LIT:0>]) / crop_aspect_ratio) + <NUM_LIT:0.5><EOL>)<EOL>crop_boundary_left = <NUM_LIT:0><EOL>crop_boundary_right = orig_crop_width<EOL>crop_boundary_top = center_pixel_coord[<NUM_LIT:1>] - (orig_crop_height // <NUM_LIT:2>)<EOL>crop_boundary_bottom = crop_boundary_top + orig_crop_height<EOL>if crop_boundary_top < <NUM_LIT:0>:<EOL><INDENT>crop_boundary_top = <NUM_LIT:0><EOL>crop_boundary_bottom = crop_boundary_top + orig_crop_height<EOL><DEDENT>elif crop_boundary_bottom > image.size[<NUM_LIT:1>]:<EOL><INDENT>crop_boundary_bottom = image.size[<NUM_LIT:1>]<EOL>crop_boundary_top = image.size[<NUM_LIT:1>] - orig_crop_height<EOL><DEDENT><DEDENT>cropped_image = image.crop(<EOL>(<EOL>crop_boundary_left,<EOL>crop_boundary_top,<EOL>crop_boundary_right,<EOL>crop_boundary_bottom<EOL>)<EOL>)<EOL>return cropped_image.resize(<EOL>(width, height),<EOL>Image.ANTIALIAS<EOL>)<EOL>

Return a PIL Image instance cropped from `image`.

Image has an aspect ratio provided by dividing `width` / `height`),
sized down to `width`x`height`. Any 'excess pixels' are trimmed away
in respect to the pixel of `image` that corresponds to `ppoi` (Primary
Point of Interest).

`image`: A PIL Image instance
`width`: Integer, width of the image to return (in pixels)
`height`: Integer, height of the image to return (in pixels)
`ppoi`: A 2-tuple of floats with values greater than 0 and less than 1
        These values are converted into a cartesian coordinate that
        signifies the 'center pixel' which the crop will center on
        (to trim the excess from the 'long side').

Determines whether to trim away pixels from either the left/right or
top/bottom sides by comparing the aspect ratio of `image` vs the
aspect ratio of `width`x`height`.

Will trim from the left/right sides if the aspect ratio of `image`
is greater-than-or-equal-to the aspect ratio of `width`x`height`.

Will trim from the top/bottom sides if the aspect ration of `image`
is less-than the aspect ratio or `width`x`height`.

Similar to Kevin Cazabon's ImageOps.fit method but uses the
ppoi value as an absolute centerpoint (as opposed as a
percentage to trim off the 'long sides').


imagefile = BytesIO()<EOL>palette = image.getpalette()<EOL>cropped_image = self.crop_on_centerpoint(<EOL>image,<EOL>width,<EOL>height,<EOL>self.ppoi<EOL>)<EOL>if image_format == '<STR_LIT>':<EOL><INDENT>cropped_image.putpalette(palette)<EOL><DEDENT>cropped_image.save(<EOL>imagefile,<EOL>**save_kwargs<EOL>)<EOL>return imagefile<EOL>

Return a BytesIO instance of `image` cropped to `width` and `height`.

Cropping will first reduce an image down to its longest side
and then crop inwards centered on the Primary Point of Interest
(as specified by `self.ppoi`)


imagefile = BytesIO()<EOL>image.thumbnail(<EOL>(width, height),<EOL>Image.ANTIALIAS<EOL>)<EOL>image.save(<EOL>imagefile,<EOL>**save_kwargs<EOL>)<EOL>return imagefile<EOL>

Return a BytesIO instance of `image` that fits in a bounding box.

Bounding box dimensions are `width`x`height`.


def process_image(self, image, image_format, save_kwargs={}):

imagefile = BytesIO()<EOL>inv_image = ImageOps.invert(image)<EOL>inv_image.save(<EOL>imagefile,<EOL>**save_kwargs<EOL>)<EOL>return imagefile<EOL>

Return a BytesIO instance of `image` with inverted colors.


data = bytearray(data) <EOL>result = bytearray()<EOL>pos = <NUM_LIT:0><EOL>while pos < len(data):<EOL><INDENT>header_byte = data[pos]<EOL>if header_byte > <NUM_LIT>:<EOL><INDENT>header_byte -= <NUM_LIT><EOL><DEDENT>pos += <NUM_LIT:1><EOL>if <NUM_LIT:0> <= header_byte <= <NUM_LIT>:<EOL><INDENT>result.extend(data[pos:pos+header_byte+<NUM_LIT:1>])<EOL>pos += header_byte+<NUM_LIT:1><EOL><DEDENT>elif header_byte == -<NUM_LIT>:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>result.extend([data[pos]] * (<NUM_LIT:1> - header_byte))<EOL>pos += <NUM_LIT:1><EOL><DEDENT><DEDENT>return bytes(result)<EOL>


if len(data) == <NUM_LIT:0>:<EOL><INDENT>return data<EOL><DEDENT>if len(data) == <NUM_LIT:1>:<EOL><INDENT>return b'<STR_LIT:\x00>' + data<EOL><DEDENT>data = bytearray(data)<EOL>result = bytearray()<EOL>buf = bytearray()<EOL>pos = <NUM_LIT:0><EOL>repeat_count = <NUM_LIT:0><EOL>MAX_LENGTH = <NUM_LIT><EOL>state = '<STR_LIT>'<EOL>def finish_raw():<EOL><INDENT>if len(buf) == <NUM_LIT:0>:<EOL><INDENT>return<EOL><DEDENT>result.append(len(buf)-<NUM_LIT:1>)<EOL>result.extend(buf)<EOL>buf[:] = bytearray()<EOL><DEDENT>def finish_rle():<EOL><INDENT>result.append(<NUM_LIT>-(repeat_count - <NUM_LIT:1>))<EOL>result.append(data[pos])<EOL><DEDENT>while pos < len(data)-<NUM_LIT:1>:<EOL><INDENT>current_byte = data[pos]<EOL>if data[pos] == data[pos+<NUM_LIT:1>]:<EOL><INDENT>if state == '<STR_LIT>':<EOL><INDENT>finish_raw()<EOL>state = '<STR_LIT>'<EOL>repeat_count = <NUM_LIT:1><EOL><DEDENT>elif state == '<STR_LIT>':<EOL><INDENT>if repeat_count == MAX_LENGTH:<EOL><INDENT>finish_rle()<EOL>repeat_count = <NUM_LIT:0><EOL><DEDENT>repeat_count += <NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>if state == '<STR_LIT>':<EOL><INDENT>repeat_count += <NUM_LIT:1><EOL>finish_rle()<EOL>state = '<STR_LIT>'<EOL>repeat_count = <NUM_LIT:0><EOL><DEDENT>elif state == '<STR_LIT>':<EOL><INDENT>if len(buf) == MAX_LENGTH:<EOL><INDENT>finish_raw()<EOL><DEDENT>buf.append(current_byte)<EOL><DEDENT><DEDENT>pos += <NUM_LIT:1><EOL><DEDENT>if state == '<STR_LIT>':<EOL><INDENT>buf.append(data[pos])<EOL>finish_raw()<EOL><DEDENT>else:<EOL><INDENT>repeat_count += <NUM_LIT:1><EOL>finish_rle()<EOL><DEDENT>return bytes(result)<EOL>


parser = self.create_parser(argv[<NUM_LIT:0>], argv[<NUM_LIT:1>])<EOL>self.arguments = parser.parse_args(argv[<NUM_LIT:2>:])<EOL>handle_default_options(self.arguments)<EOL>options = vars(self.arguments)<EOL>self.execute(**options)<EOL>

Set up any environment changes requested (e.g., Python path
and Django settings), then run this command.


return self.usage or '<STR_LIT>'.format(subcommand=subcommand)<EOL>

Return a brief description of how to use this command, by
default from the attribute ``self.help``.


def create_parser(self, prog_name, subcommand):

Create and return the ``ArgumentParser`` which will be used to
parse the arguments to this command.


print(integers, options)<EOL>return super(Command, self).handle(integers, **options)<EOL>

[1, 2, 3, 4] {'accumulate': <built-in function max>}


def __init__(self, account_token, project_token, environment, tags=None,<EOL>endpoint="<STR_LIT>"):

self.account_token = account_token<EOL>self.project_token = project_token<EOL>self.endpoint = endpoint<EOL>self.environment = environment<EOL>if tags:<EOL><INDENT>self.tags = tags<EOL><DEDENT>else:<EOL><INDENT>self.tags = []<EOL><DEDENT>

:param account_token: Your account token, as supplied by StackSentinel
:param project_token: Your project token, as supplied by StackSentinel
:param environment: The environment of the project (eg, "production", "devel", etc)
:param tags: Any tags you want associated with *all* errors sent using this client.
:param endpoint: API endpoint. Defaults to StackSentinel backend.


try:<EOL><INDENT>return repr(obj)<EOL><DEDENT>except:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>

When the state of an exception includes something that we can't pickle, show something useful instead.


def handle_exception(self, exc_info=None, state=None, tags=None, return_feedback_urls=False,<EOL>dry_run=False):

if not exc_info:<EOL><INDENT>exc_info = sys.exc_info()<EOL><DEDENT>if exc_info is None:<EOL><INDENT>raise StackSentinelError("<STR_LIT>")<EOL><DEDENT>(etype, value, tb) = exc_info<EOL>try:<EOL><INDENT>msg = value.args[<NUM_LIT:0>]<EOL><DEDENT>except:<EOL><INDENT>msg = repr(value)<EOL><DEDENT>if not isinstance(tags, list):<EOL><INDENT>tags = [tags]<EOL><DEDENT>limit = None<EOL>new_tb = []<EOL>n = <NUM_LIT:0><EOL>while tb is not None and (limit is None or n < limit):<EOL><INDENT>f = tb.tb_frame<EOL>lineno = tb.tb_lineno<EOL>co = f.f_code<EOL>filename = co.co_filename<EOL>name = co.co_name<EOL>tb = tb.tb_next<EOL>n = n + <NUM_LIT:1><EOL>new_tb.append({'<STR_LIT>': lineno, '<STR_LIT>': filename, '<STR_LIT>': name})<EOL><DEDENT>if state is None:<EOL><INDENT>state = {}<EOL><DEDENT>if '<STR_LIT>' not in state:<EOL><INDENT>try:<EOL><INDENT>state['<STR_LIT>'] = self._get_sys_info()<EOL><DEDENT>except Exception as e:<EOL><INDENT>state['<STR_LIT>'] = '<STR_LIT>' % e<EOL><DEDENT><DEDENT>if '<STR_LIT>' not in state:<EOL><INDENT>try:<EOL><INDENT>state['<STR_LIT>'] = self._get_machine_info()<EOL><DEDENT>except Exception as e:<EOL><INDENT>state['<STR_LIT>'] = '<STR_LIT>' % e<EOL><DEDENT><DEDENT>if tags is None:<EOL><INDENT>tags = []<EOL><DEDENT>if sys.version_info.major > <NUM_LIT:2>:<EOL><INDENT>error_type = str(etype.__name__)<EOL>error_message = str(value)<EOL><DEDENT>else:<EOL><INDENT>error_type = unicode(etype.__name__)<EOL>error_message = unicode(value)<EOL><DEDENT>send_error_args = dict(error_type=error_type,<EOL>error_message=error_message,<EOL>traceback=new_tb,<EOL>environment=self.environment,<EOL>state=state,<EOL>tags=self.tags + tags,<EOL>return_feedback_urls=return_feedback_urls)<EOL>if dry_run:<EOL><INDENT>return send_error_args<EOL><DEDENT>else:<EOL><INDENT>return self.send_error(**send_error_args)<EOL><DEDENT>

Call this method from within a try/except clause to generate a call to Stack Sentinel.

:param exc_info: Return value of sys.exc_info(). If you pass None, handle_exception will call sys.exc_info() itself
:param state: Dictionary of state information associated with the error. This could be form data, cookie data, whatnot. NOTE: sys and machine are added to this dictionary if they are not already included.
:param tags: Any string tags you want associated with the exception report.
:param return_feedback_urls: If True, Stack Sentinel will return feedback URLs you can present to the user for extra debugging information.
:param dry_run: If True, method will not actively send in error information to API. Instead, it will return a request object and payload. Used in unittests.


def send_error(self, error_type, error_message, traceback, environment, state, tags=None,<EOL>return_feedback_urls=False):

(request, payload) = self._generate_request(environment, error_message, error_type, return_feedback_urls,<EOL>state, tags, traceback)<EOL>try:<EOL><INDENT>response = urlopen(request)<EOL><DEDENT>except HTTPError as e:<EOL><INDENT>if e.code == <NUM_LIT>:<EOL><INDENT>raise StackSentinelError(e.read())<EOL><DEDENT>else:<EOL><INDENT>raise<EOL><DEDENT><DEDENT>if sys.version_info.major > <NUM_LIT:2>:<EOL><INDENT>text_response = response.read().decode(response.headers.get_content_charset() or '<STR_LIT:utf8>')<EOL><DEDENT>else:<EOL><INDENT>encoding = response.headers.get('<STR_LIT>', '<STR_LIT>').split('<STR_LIT>')[-<NUM_LIT:1>].strip()<EOL>if encoding:<EOL><INDENT>text_response = response.read().decode('<STR_LIT:utf8>', '<STR_LIT:replace>')<EOL><DEDENT>else:<EOL><INDENT>text_response = response.read().decode(encoding)<EOL><DEDENT><DEDENT>return json.loads(text_response)<EOL>

Sends error payload to Stack Sentinel API, returning a parsed JSON response. (Parsed as in,
converted into Python dict/list objects)

:param error_type: Type of error generated. (Eg, "TypeError")
:param error_message: Message of error generated (Eg, "cannot concatenate 'str' and 'int' objects")
:param traceback: List of dictionaries. Each dictionary should contain, "line", "method", and "module" keys.
:param environment: Environment the error occurred in (eg, "devel")_
:param state: State of the application when the error happened. Could contain form data, cookies, etc.
:param tags: Arbitrary tags you want associated with the error. list.
:param return_feedback_urls: If True, return payload will offer URLs to send users to collect additional feedback for debugging.
:return: Parsed return value from Stack Sentinel API


self.app = app<EOL>self.client = client<EOL>

:param app: WSGI application object
:param client: Instance of StackSentinel


def force_text(s, encoding='<STR_LIT:utf-8>', errors='<STR_LIT:strict>'):

if isinstance(s, text_type):<EOL><INDENT>return s<EOL><DEDENT>try:<EOL><INDENT>if not isinstance(s, string_types):<EOL><INDENT>if hasattr(s, '<STR_LIT>'):<EOL><INDENT>s = s.__unicode__()<EOL><DEDENT>else:<EOL><INDENT>if PY3:<EOL><INDENT>if isinstance(s, bytes):<EOL><INDENT>s = text_type(s, encoding, errors)<EOL><DEDENT>else:<EOL><INDENT>s = text_type(s)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>s = text_type(bytes(s), encoding, errors)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>s = s.decode(encoding, errors)<EOL><DEDENT><DEDENT>except UnicodeDecodeError as e:<EOL><INDENT>if not isinstance(s, Exception):<EOL><INDENT>raise MailUnicodeDecodeError(s, *e.args)<EOL><DEDENT>else:<EOL><INDENT>s = '<STR_LIT:U+0020>'.join([force_text(arg, encoding, strings_only,<EOL>errors) for arg in s])<EOL><DEDENT><DEDENT>return s<EOL>

Similar to smart_text, except that lazy instances are resolved to
strings, rather than kept as lazy objects.

If strings_only is True, don't convert (some) non-string-like objects.


assert message.send_to, "<STR_LIT>"<EOL>if message.has_bad_headers(self.mail.default_sender):<EOL><INDENT>raise BadHeaderError<EOL><DEDENT>if message.date is None:<EOL><INDENT>message.date = time.time()<EOL><DEDENT>sender = message.sender or self.mail.default_sender<EOL>if self.host:<EOL><INDENT>self.host.sendmail(sanitize_address(sender) if sender is not None else None,<EOL>message.send_to,<EOL>message.as_string(self.mail.default_sender),<EOL>message.mail_options,<EOL>message.rcpt_options)<EOL><DEDENT>email_dispatched.send(message, mail=self.mail)<EOL>self.num_emails += <NUM_LIT:1><EOL>if self.num_emails == self.mail.max_emails:<EOL><INDENT>self.num_emails = <NUM_LIT:0><EOL>if self.host:<EOL><INDENT>self.host.quit()<EOL>self.host = self.configure_host()<EOL><DEDENT><DEDENT>

Verifies and sends message.

        :param message: Message instance.
        :param envelope_from: Email address to be used in MAIL FROM command.


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

self.send(Message(*args, **kwargs))<EOL>

Shortcut for send(msg).

        Takes same arguments as Message constructor.

        :versionadded: 0.3.5


def _mimetext(self, text, subtype='<STR_LIT>'):

charset = self.charset or '<STR_LIT:utf-8>'<EOL>return MIMEText(text, _subtype=subtype, _charset=charset)<EOL>

Creates a MIMEText object with the given subtype (default: 'plain')
        If the text is unicode, the utf-8 charset is used.


encoding = self.charset or '<STR_LIT:utf-8>'<EOL>attachments = self.attachments or []<EOL>if len(attachments) == <NUM_LIT:0> and not self.html:<EOL><INDENT>msg = self._mimetext(self.body)<EOL><DEDENT>elif len(attachments) > <NUM_LIT:0> and not self.html:<EOL><INDENT>msg = MIMEMultipart()<EOL>msg.attach(self._mimetext(self.body))<EOL><DEDENT>else:<EOL><INDENT>msg = MIMEMultipart()<EOL>alternative = MIMEMultipart('<STR_LIT>')<EOL>alternative.attach(self._mimetext(self.body, '<STR_LIT>'))<EOL>alternative.attach(self._mimetext(self.html, '<STR_LIT:html>'))<EOL>msg.attach(alternative)<EOL><DEDENT>if self.charset:<EOL><INDENT>msg['<STR_LIT>'] = Header(self.subject, encoding)<EOL><DEDENT>else:<EOL><INDENT>msg['<STR_LIT>'] = self.subject<EOL><DEDENT>sender = self.sender or default_from<EOL>if sender is not None:<EOL><INDENT>msg['<STR_LIT>'] = sanitize_address(sender, encoding)<EOL><DEDENT>msg['<STR_LIT>'] = '<STR_LIT:U+002CU+0020>'.join(list(set(sanitize_addresses(self.recipients, encoding))))<EOL>msg['<STR_LIT>'] = formatdate(self.date, localtime=True)<EOL>msg['<STR_LIT>'] = self.msgId<EOL>if self.cc:<EOL><INDENT>msg['<STR_LIT>'] = '<STR_LIT:U+002CU+0020>'.join(list(set(sanitize_addresses(self.cc, encoding))))<EOL><DEDENT>if self.reply_to:<EOL><INDENT>msg['<STR_LIT>'] = sanitize_address(self.reply_to, encoding)<EOL><DEDENT>if self.extra_headers:<EOL><INDENT>for k, v in self.extra_headers.items():<EOL><INDENT>msg[k] = v<EOL><DEDENT><DEDENT>for attachment in attachments:<EOL><INDENT>f = MIMEBase(*attachment.content_type.split('<STR_LIT:/>'))<EOL>f.set_payload(attachment.data)<EOL>encode_base64(f)<EOL>try:<EOL><INDENT>attachment.filename and attachment.filename.encode('<STR_LIT:ascii>')<EOL><DEDENT>except UnicodeEncodeError:<EOL><INDENT>filename = attachment.filename<EOL>if not PY3:<EOL><INDENT>filename = filename.encode('<STR_LIT:utf8>')<EOL><DEDENT>f.add_header('<STR_LIT>', attachment.disposition,<EOL>filename=('<STR_LIT>', '<STR_LIT>', filename))<EOL><DEDENT>else:<EOL><INDENT>f.add_header('<STR_LIT>', '<STR_LIT>' %<EOL>(attachment.disposition, attachment.filename))<EOL><DEDENT>for key, value in attachment.headers:<EOL><INDENT>f.add_header(key, value)<EOL><DEDENT>msg.attach(f)<EOL><DEDENT>return msg.as_string()<EOL>


sender = self.sender or default_from<EOL>reply_to = self.reply_to or '<STR_LIT>'<EOL>for val in [self.subject, sender, reply_to] + self.recipients:<EOL><INDENT>for c in '<STR_LIT:\r\n>':<EOL><INDENT>if c in val:<EOL><INDENT>return True<EOL><DEDENT><DEDENT><DEDENT>return False<EOL>

Checks for bad headers i.e. newlines in subject, sender or recipients.


connection.send(self)<EOL>


self.recipients.append(recipient)<EOL>

Adds another recipient to the message.

        :param recipient: email address of recipient.


self.attachments.append(<EOL>Attachment(filename, content_type, data, disposition, headers))<EOL>

Adds an attachment to the message.

        :param filename: filename of attachment
        :param content_type: file mimetype
        :param data: the raw file data
        :param disposition: content-disposition (if any)


if not email_dispatched:<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>outbox = []<EOL>def _record(message, mail):<EOL><INDENT>outbox.append(message)<EOL><DEDENT>email_dispatched.connect(_record)<EOL>try:<EOL><INDENT>yield outbox<EOL><DEDENT>finally:<EOL><INDENT>email_dispatched.disconnect(_record)<EOL><DEDENT>

Records all messages. Use in unit tests for example::

            with mail.record_messages() as outbox:
                response = app.test_client.get("/email-sending-view/")
                assert len(outbox) == 1
                assert outbox[0].subject == "testing"

        You must have blinker installed in order to use this feature.
        :versionadded: 0.4


with self.connect() as connection:<EOL><INDENT>message.send(connection)<EOL><DEDENT>

Sends a single message instance. If TESTING is True the message will
        not actually be sent.

        :param message: a Message instance.


doc = self.id()[self.id().rfind('<STR_LIT:.>')+<NUM_LIT:1>:]<EOL>return "<STR_LIT>" % (self.__class__.__name__, doc)<EOL>

Get's the one liner description to be displayed.
Source:
http://erikzaadi.com/2012/09/13/inheritance-within-python-unit-tests/


for verb in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>yield getattr(c, verb)<EOL><DEDENT>

A simple helper method to iterate through a range of
            HTTP Verbs and return the test_client bound instance,
            keeping writing our tests as DRY as possible.


@app.route("<STR_LIT:/>", methods=['<STR_LIT:GET>', '<STR_LIT>'])<EOL>@cross_origin(app, automatic_options=True)<EOL>def hello_world(request):

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

This view has CORS enabled for all domains, representing the simplest
configuration of view-based decoration. The expected result is as
follows:

$ curl --include -X GET http://127.0.0.1:5000/ \
    --header Origin:www.examplesite.com

>> HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 184
Access-Control-Allow-Origin: *
Server: Werkzeug/0.9.6 Python/2.7.9
Date: Sat, 31 Jan 2015 22:29:56 GMT

Hello CORS!
 Read about my spec at the
W3 Or, checkout my documentation
on Github


@app.route("<STR_LIT>", methods=['<STR_LIT:GET>', '<STR_LIT:POST>', '<STR_LIT>'])<EOL>@cross_origin(app, automatic_options=True, allow_headers=['<STR_LIT:Content-Type>'])<EOL>def cross_origin_json_post(request):

return json({"<STR_LIT:success>":True})<EOL>

This view has CORS enabled for all domains, and allows browsers
       to send the Content-Type header, allowing cross domain AJAX POST
       requests.

Browsers will first make a preflight request to verify that the resource
       allows cross-origin POSTs with a JSON Content-Type, which can be simulated
       as:
       $ curl --include -X OPTIONS http://127.0.0.1:5000/api/v1/users/create \
           --header Access-Control-Request-Method:POST \
           --header Access-Control-Request-Headers:Content-Type \
           --header Origin:www.examplesite.com
       >> HTTP/1.0 200 OK
       Content-Type: text/html; charset=utf-8
       Allow: POST, OPTIONS
       Access-Control-Allow-Origin: *
       Access-Control-Allow-Headers: Content-Type
       Access-Control-Allow-Methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT
       Content-Length: 0
       Server: Werkzeug/0.9.6 Python/2.7.9
       Date: Sat, 31 Jan 2015 22:25:22 GMT

       $ curl --include -X POST http://127.0.0.1:5000/api/v1/users/create \
           --header Content-Type:application/json \
           --header Origin:www.examplesite.com

       >> HTTP/1.0 200 OK
       Content-Type: application/json
       Content-Length: 21
       Access-Control-Allow-Origin: *
       Server: Werkzeug/0.9.6 Python/2.7.9
       Date: Sat, 31 Jan 2015 22:25:04 GMT

       {
         "success": true
       }


return text(

Since the path '/' does not match the regular expression r'/api/*',
this route does not have CORS headers set.


@app.route("<STR_LIT>", methods=['<STR_LIT:GET>', '<STR_LIT>'])<EOL>def list_users(request):

return json({"<STR_LIT:user>": "<STR_LIT>"})<EOL>

Since the path matches the regular expression r'/api/*', this resource
automatically has CORS headers set. The expected result is as follows:

$ curl --include -X GET http://127.0.0.1:5000/api/v1/users/ \
    --header Origin:www.examplesite.com
HTTP/1.0 200 OK
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Origin: *
Content-Length: 21
Content-Type: application/json
Date: Sat, 09 Aug 2014 00:26:41 GMT
Server: Werkzeug/0.9.4 Python/2.7.8

{
    "success": true
}


@app.route("<STR_LIT>", methods=['<STR_LIT:POST>', '<STR_LIT>'])<EOL>def create_user(request):

return json({"<STR_LIT:success>": True})<EOL>

Since the path matches the regular expression r'/api/*', this resource
automatically has CORS headers set.

Browsers will first make a preflight request to verify that the resource
allows cross-origin POSTs with a JSON Content-Type, which can be simulated
as:
$ curl --include -X OPTIONS http://127.0.0.1:5000/api/v1/users/create \
    --header Access-Control-Request-Method:POST \
    --header Access-Control-Request-Headers:Content-Type \
    --header Origin:www.examplesite.com
>> HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Allow: POST, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT
Content-Length: 0
Server: Werkzeug/0.9.6 Python/2.7.9
Date: Sat, 31 Jan 2015 22:25:22 GMT

$ curl --include -X POST http://127.0.0.1:5000/api/v1/users/create \
    --header Content-Type:application/json \
    --header Origin:www.examplesite.com

>> HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 21
Access-Control-Allow-Origin: *
Server: Werkzeug/0.9.6 Python/2.7.9
Date: Sat, 31 Jan 2015 22:25:04 GMT

{
  "success": true
}


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

Since the path matches the regular expression r'/api/*', this resource
automatically has CORS headers set.

Browsers will first make a preflight request to verify that the resource
allows cross-origin POSTs with a JSON Content-Type, which can be simulated
as:
$ curl --include -X OPTIONS http://127.0.0.1:5000/exception \
    --header Access-Control-Request-Method:POST \
    --header Access-Control-Request-Headers:Content-Type \
    --header Origin:www.examplesite.com
>> HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Allow: POST, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT
Content-Length: 0
Server: Werkzeug/0.9.6 Python/2.7.9
Date: Sat, 31 Jan 2015 22:25:22 GMT


@api_v1.route("<STR_LIT>", methods=['<STR_LIT:GET>', '<STR_LIT>'])<EOL>def list_users(request):


@api_v1.route("<STR_LIT>", methods=['<STR_LIT:POST>', '<STR_LIT>'])<EOL>def create_user(request):


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


_options = kwargs<EOL>_real_decorator = cors.decorate(app, *args, run_middleware=False, with_context=False, **kwargs)<EOL>def wrapped_decorator(f):<EOL><INDENT>spf = SanicPluginsFramework(app)  <EOL>try:<EOL><INDENT>plugin = spf.register_plugin(cors, skip_reg=True)<EOL><DEDENT>except ValueError as e:<EOL><INDENT>assert e.args and len(e.args) > <NUM_LIT:1><EOL>plugin = e.args[<NUM_LIT:1>]<EOL><DEDENT>context = cors.get_context_from_spf(spf)<EOL>log = context.log<EOL>log(logging.DEBUG, "<STR_LIT>".format(str(f), str(_options)))<EOL>return _real_decorator(f)<EOL><DEDENT>return wrapped_decorator<EOL>

This function is the decorator which is used to wrap a Sanic route with.
In the simplest case, simply use the default parameters to allow all
origins in what is the most permissive configuration. If this method
modifies state or performs authentication which may be brute-forced, you
should add some degree of protection, such as Cross Site Forgery
Request protection.

:param origins:
    The origin, or list of origins to allow requests from.
    The origin(s) may be regular expressions, case-sensitive strings,
    or else an asterisk

    Default : '*'
:type origins: list, string or regex

:param methods:
    The method or list of methods which the allowed origins are allowed to
    access for non-simple requests.

    Default : [GET, HEAD, POST, OPTIONS, PUT, PATCH, DELETE]
:type methods: list or string

:param expose_headers:
    The header or list which are safe to expose to the API of a CORS API
    specification.

    Default : None
:type expose_headers: list or string

:param allow_headers:
    The header or list of header field names which can be used when this
    resource is accessed by allowed origins. The header(s) may be regular
    expressions, case-sensitive strings, or else an asterisk.

    Default : '*', allow all headers
:type allow_headers: list, string or regex

:param supports_credentials:
    Allows users to make authenticated requests. If true, injects the
    `Access-Control-Allow-Credentials` header in responses. This allows
    cookies and credentials to be submitted across domains.

    :note: This option cannot be used in conjuction with a '*' origin

    Default : False
:type supports_credentials: bool

:param max_age:
    The maximum time for which this CORS request maybe cached. This value
    is set as the `Access-Control-Max-Age` header.

    Default : None
:type max_age: timedelta, integer, string or None

:param send_wildcard: If True, and the origins parameter is `*`, a wildcard
    `Access-Control-Allow-Origin` header is sent, rather than the
    request's `Origin` header.

    Default : False
:type send_wildcard: bool

:param vary_header:
    If True, the header Vary: Origin will be returned as per the W3
    implementation guidelines.

    Setting this header when the `Access-Control-Allow-Origin` is
    dynamically generated (e.g. when there is more than one allowed
    origin, and an Origin than '*' is returned) informs CDNs and other
    caches that the CORS headers are dynamic, and cannot be cached.

    If False, the Vary header will never be injected or altered.

    Default : True
:type vary_header: bool

:param automatic_options:
    Only applies to the `cross_origin` decorator. If True, Sanic-CORS will
    override Sanic's default OPTIONS handling to return CORS headers for
    OPTIONS requests.

    Default : True
:type automatic_options: bool


try:<EOL><INDENT>return regexp.pattern<EOL><DEDENT>except AttributeError:<EOL><INDENT>return str(regexp)<EOL><DEDENT>

Helper that returns regexp pattern from given value.

:param regexp: regular expression to stringify
:type regexp: _sre.SRE_Pattern or str
:returns: string representation of given regexp pattern
:rtype: str


def set_cors_headers(req, resp, context, options):

try:<EOL><INDENT>request_context = context.request[id(req)]<EOL><DEDENT>except AttributeError:<EOL><INDENT>LOG.debug("<STR_LIT>")<EOL>return resp<EOL><DEDENT>evaluated = request_context.get(SANIC_CORS_EVALUATED, False)<EOL>if evaluated:<EOL><INDENT>LOG.debug('<STR_LIT>')<EOL>return resp<EOL><DEDENT>if resp is None:<EOL><INDENT>return None<EOL><DEDENT>if resp.headers is None:<EOL><INDENT>resp.headers = CIMultiDict()<EOL><DEDENT>headers_to_set = get_cors_headers(options, req.headers, req.method)<EOL>LOG.debug('<STR_LIT>', str(headers_to_set))<EOL>try:<EOL><INDENT>resp.headers.extend(headers_to_set)<EOL><DEDENT>except Exception as e1:<EOL><INDENT>for k, v in headers_to_set.items():<EOL><INDENT>try:<EOL><INDENT>resp.headers.add(k, v)<EOL><DEDENT>except Exception as e2:<EOL><INDENT>resp.headers[k] = v<EOL><DEDENT><DEDENT>return resp<EOL><DEDENT>

Performs the actual evaluation of Sanic-CORS options and actually
modifies the response object.

This function is used both in the decorator and the after_request
callback
:param sanic.request.Request req:


return r'<STR_LIT>' if reg == r'<STR_LIT:*>' else reg<EOL>

Replace the invalid regex r'*' with the valid, wildcard regex r'/.*' to
enable the CORS app extension to have a more user friendly api.


if isinstance(maybe_regex, RegexObject):<EOL><INDENT>return re.match(maybe_regex, request_origin)<EOL><DEDENT>elif probably_regex(maybe_regex):<EOL><INDENT>return re.match(maybe_regex, request_origin, flags=re.IGNORECASE)<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>return request_origin.lower() == maybe_regex.lower()<EOL><DEDENT>except AttributeError:<EOL><INDENT>return request_origin == maybe_regex<EOL><DEDENT><DEDENT>

Safely attempts to match a pattern or string to a request origin.


options = DEFAULT_OPTIONS.copy()<EOL>options.update(get_app_kwarg_dict(appInstance))<EOL>if dicts:<EOL><INDENT>for d in dicts:<EOL><INDENT>options.update(d)<EOL><DEDENT><DEDENT>return serialize_options(options)<EOL>

Compute CORS options for an application by combining the DEFAULT_OPTIONS,
the app's configuration-specified options and any dictionaries passed. The
last specified option wins.


<EOL>app_config = getattr(appInstance, '<STR_LIT>', {})<EOL>return dict(<EOL>(k.lower().replace('<STR_LIT>', '<STR_LIT>'), app_config.get(k))<EOL>for k in CONFIG_OPTIONS<EOL>if app_config.get(k) is not None<EOL>)<EOL>

Returns the dictionary of CORS specific app configurations.


if obj is None:<EOL><INDENT>return None<EOL><DEDENT>elif(not isinstance(obj, str)<EOL>and isinstance(obj, collections.abc.Iterable)):<EOL><INDENT>return '<STR_LIT:U+002CU+0020>'.join(str(item) for item in sorted(obj))<EOL><DEDENT>else:<EOL><INDENT>return str(obj)<EOL><DEDENT>

A more flexible str function which intelligently handles stringifying
strings, lists and other iterables. The results are lexographically sorted
to ensure generated responses are consistent when iterables such as Set
are used.


if isinstance(inst, str):<EOL><INDENT>return [inst]<EOL><DEDENT>elif not isinstance(inst, collections.abc.Iterable):<EOL><INDENT>return [inst]<EOL><DEDENT>else:<EOL><INDENT>return inst<EOL><DEDENT>

Wraps scalars or string types as a list, or returns the iterable instance.


options = (opts or {}).copy()<EOL>for key in opts.keys():<EOL><INDENT>if key not in DEFAULT_OPTIONS:<EOL><INDENT>LOG.warning("<STR_LIT>", key)<EOL><DEDENT><DEDENT>options['<STR_LIT>'] = sanitize_regex_param(options.get('<STR_LIT>'))<EOL>options['<STR_LIT>'] = sanitize_regex_param(options.get('<STR_LIT>'))<EOL>if r'<STR_LIT>' in options['<STR_LIT>'] and options['<STR_LIT>'] and options['<STR_LIT>']:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>serialize_option(options, '<STR_LIT>')<EOL>serialize_option(options, '<STR_LIT>', upper=True)<EOL>if isinstance(options.get('<STR_LIT>'), timedelta):<EOL><INDENT>options['<STR_LIT>'] = str(int(options['<STR_LIT>'].total_seconds()))<EOL><DEDENT>return options<EOL>

A helper method to serialize and processes the options dictionary.


args = (sys.executable,) + args<EOL>return subprocess.call(args) == <NUM_LIT:0><EOL>


try:<EOL><INDENT>subprocess.check_call(cmd)<EOL><DEDENT>except subprocess.CalledProcessError:<EOL><INDENT>if os.access(target, os.F_OK):<EOL><INDENT>os.unlink(target)<EOL><DEDENT>raise<EOL><DEDENT>

Run the command to download target. If the command fails, clean up before
re-raising the error.


target = os.path.abspath(target)<EOL>ps_cmd = (<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>% vars()<EOL>)<EOL>cmd = [<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>ps_cmd,<EOL>]<EOL>_clean_check(cmd, target)<EOL>

Download the file at url to target using Powershell (which will validate
trust). Raise an exception if the command cannot complete.


src = urlopen(url)<EOL>try:<EOL><INDENT>data = src.read()<EOL><DEDENT>finally:<EOL><INDENT>src.close()<EOL><DEDENT>with open(target, "<STR_LIT:wb>") as dst:<EOL><INDENT>dst.write(data)<EOL><DEDENT>

Use Python to download the file, even though it cannot authenticate the
connection.


<EOL>to_dir = os.path.abspath(to_dir)<EOL>zip_name = "<STR_LIT>" % version<EOL>url = download_base + zip_name<EOL>saveto = os.path.join(to_dir, zip_name)<EOL>if not os.path.exists(saveto):  <EOL><INDENT>log.warn("<STR_LIT>", url)<EOL>downloader = downloader_factory()<EOL>downloader(url, saveto)<EOL><DEDENT>return os.path.realpath(saveto)<EOL>

Download setuptools from a specified location and return its filename

`version` should be a valid setuptools version number that is available
as an sdist for download under the `download_base` URL (which should end
with a '/'). `to_dir` is the directory where the egg will be downloaded.
`delay` is the number of seconds to pause before an actual download
attempt.

``downloader_factory`` should be a function taking no arguments and
returning a function for downloading a URL to a target.


return ['<STR_LIT>'] if options.user_install else []<EOL>

Build the arguments to 'python setup.py install' on the setuptools package


parser = optparse.OptionParser()<EOL>parser.add_option(<EOL>'<STR_LIT>', dest='<STR_LIT>', action='<STR_LIT:store_true>', default=False,<EOL>help='<STR_LIT>')<EOL>parser.add_option(<EOL>'<STR_LIT>', dest='<STR_LIT>', metavar="<STR_LIT>",<EOL>default=DEFAULT_URL,<EOL>help='<STR_LIT>')<EOL>parser.add_option(<EOL>'<STR_LIT>', dest='<STR_LIT>', action='<STR_LIT>',<EOL>const=lambda: download_file_insecure, default=get_best_downloader,<EOL>help='<STR_LIT>'<EOL>)<EOL>parser.add_option(<EOL>'<STR_LIT>', help="<STR_LIT>",<EOL>default=DEFAULT_VERSION,<EOL>)<EOL>options, args = parser.parse_args()<EOL>return options<EOL>


options = _parse_args()<EOL>archive = download_setuptools(<EOL>version=options.version,<EOL>download_base=options.download_base,<EOL>downloader_factory=options.downloader_factory,<EOL>)<EOL>return _install(archive, _build_install_args(options))<EOL>

Install or upgrade setuptools and EasyInstall


if hasattr(zipfile.ZipFile, '<STR_LIT>'):<EOL><INDENT>return zipfile.ZipFile(*args, **kwargs)<EOL><DEDENT>return super(ContextualZipFile, cls).__new__(cls)<EOL>

Construct a ZipFile or ContextualZipFile as appropriate


rm_nodes = set(rm_nodes)<EOL>ndf = network.nodes_df<EOL>edf = network.edges_df<EOL>nodes_to_keep = ~ndf.index.isin(rm_nodes)<EOL>edges_to_keep = ~(edf['<STR_LIT>'].isin(rm_nodes) | edf['<STR_LIT:to>'].isin(rm_nodes))<EOL>return ndf.loc[nodes_to_keep], edf.loc[edges_to_keep]<EOL>

Create DataFrames of nodes and edges that do not include specified nodes.

Parameters
----------
network : pandana.Network
rm_nodes : array_like
    A list, array, Index, or Series of node IDs that should *not*
    be saved as part of the Network.

Returns
-------
nodes, edges : pandas.DataFrame


def network_to_pandas_hdf5(network, filename, rm_nodes=None):

if rm_nodes is not None:<EOL><INDENT>nodes, edges = remove_nodes(network, rm_nodes)<EOL><DEDENT>else:<EOL><INDENT>nodes, edges = network.nodes_df, network.edges_df<EOL><DEDENT>with pd.HDFStore(filename, mode='<STR_LIT:w>') as store:<EOL><INDENT>store['<STR_LIT>'] = nodes<EOL>store['<STR_LIT>'] = edges<EOL>store['<STR_LIT>'] = pd.Series([network._twoway])<EOL>store['<STR_LIT>'] = pd.Series(network.impedance_names)<EOL><DEDENT>

Save a Network's data to a Pandas HDFStore.

Parameters
----------
network : pandana.Network
filename : str
rm_nodes : array_like
    A list, array, Index, or Series of node IDs that should *not*
    be saved as part of the Network.


with pd.HDFStore(filename) as store:<EOL><INDENT>nodes = store['<STR_LIT>']<EOL>edges = store['<STR_LIT>']<EOL>two_way = store['<STR_LIT>'][<NUM_LIT:0>]<EOL>imp_names = store['<STR_LIT>'].tolist()<EOL><DEDENT>return cls(<EOL>nodes['<STR_LIT:x>'], nodes['<STR_LIT:y>'], edges['<STR_LIT>'], edges['<STR_LIT:to>'], edges[imp_names],<EOL>twoway=two_way)<EOL>

Build a Network from data in a Pandas HDFStore.

Parameters
----------
cls : class
    Class to instantiate, usually pandana.Network.
filename : str

Returns
-------
network : pandana.Network


def pdna_network_from_bbox(<EOL>lat_min=None, lng_min=None, lat_max=None, lng_max=None, bbox=None,<EOL>network_type='<STR_LIT>', two_way=True,<EOL>timeout=<NUM_LIT>, memory=None, max_query_area_size=<NUM_LIT:50> * <NUM_LIT:1000> * <NUM_LIT:50> * <NUM_LIT:1000>):

nodes, edges = network_from_bbox(lat_min=lat_min, lng_min=lng_min,<EOL>lat_max=lat_max, lng_max=lng_max,<EOL>bbox=bbox, network_type=network_type,<EOL>two_way=two_way, timeout=timeout,<EOL>memory=memory,<EOL>max_query_area_size=max_query_area_size)<EOL>return Network(<EOL>nodes['<STR_LIT:x>'], nodes['<STR_LIT:y>'],<EOL>edges['<STR_LIT>'], edges['<STR_LIT:to>'], edges[['<STR_LIT>']])<EOL>

Make a Pandana network from a bounding lat/lon box
request to the Overpass API. Distance will be in the default units meters.

Parameters
----------
lat_min, lng_min, lat_max, lng_max : float
bbox : tuple
    Bounding box formatted as a 4 element tuple:
    (lng_max, lat_min, lng_min, lat_max)
network_type : {'walk', 'drive'}, optional
    Specify whether the network will be used for walking or driving.
    A value of 'walk' attempts to exclude things like freeways,
    while a value of 'drive' attempts to exclude things like
    bike and walking paths.
two_way : bool, optional
    Whether the routes are two-way. If True, node pairs will only
    occur once.
timeout : int, optional
    the timeout interval for requests and to pass to Overpass API
memory : int, optional
    server memory allocation size for the query, in bytes.
    If none, server will use its default allocation size
max_query_area_size : float, optional
    max area for any part of the geometry, in the units the geometry is in

Returns
-------
network : pandana.Network


uninteresting_tags = {<EOL>'<STR_LIT:source>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>}<EOL>node = {<EOL>'<STR_LIT:id>': e['<STR_LIT:id>'],<EOL>'<STR_LIT>': e['<STR_LIT>'],<EOL>'<STR_LIT>': e['<STR_LIT>']<EOL>}<EOL>if '<STR_LIT>' in e:<EOL><INDENT>for t, v in list(e['<STR_LIT>'].items()):<EOL><INDENT>if t not in uninteresting_tags:<EOL><INDENT>node[t] = v<EOL><DEDENT><DEDENT><DEDENT>return node<EOL>

Process a node element entry into a dict suitable for going into
a Pandas DataFrame.

Parameters
----------
e : dict
Returns
-------
node : dict


osm_url = '<STR_LIT>'<EOL>req = requests.get(osm_url, params={'<STR_LIT:data>': query})<EOL>req.raise_for_status()<EOL>return req.json()<EOL>

Make a request to OSM and return the parsed JSON.

Parameters
----------
query : str
    A string in the Overpass QL format.

Returns
-------
data : dict


def build_node_query(lat_min, lng_min, lat_max, lng_max, tags=None):

if tags is not None:<EOL><INDENT>if isinstance(tags, str):<EOL><INDENT>tags = [tags]<EOL><DEDENT>tags = '<STR_LIT>'.join('<STR_LIT>'.format(t) for t in tags)<EOL><DEDENT>else:<EOL><INDENT>tags = '<STR_LIT>'<EOL><DEDENT>query_fmt = (<EOL>'<STR_LIT>'<EOL>'<STR_LIT:(>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL>return query_fmt.format(<EOL>lat_min=lat_min, lng_min=lng_min, lat_max=lat_max, lng_max=lng_max,<EOL>tags=tags)<EOL>

Build the string for a node-based OSM query.

Parameters
----------
lat_min, lng_min, lat_max, lng_max : float
tags : str or list of str, optional
    Node tags that will be used to filter the search.
    See http://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide
    for information about OSM Overpass queries
    and http://wiki.openstreetmap.org/wiki/Map_Features
    for a list of tags.

Returns
-------
query : str


def node_query(lat_min, lng_min, lat_max, lng_max, tags=None):

node_data = make_osm_query(build_node_query(<EOL>lat_min, lng_min, lat_max, lng_max, tags=tags))<EOL>if len(node_data['<STR_LIT>']) == <NUM_LIT:0>:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>nodes = [process_node(n) for n in node_data['<STR_LIT>']]<EOL>return pd.DataFrame.from_records(nodes, index='<STR_LIT:id>')<EOL>

Search for OSM nodes within a bounding box that match given tags.

Parameters
----------
lat_min, lng_min, lat_max, lng_max : float
tags : str or list of str, optional
    Node tags that will be used to filter the search.
    See http://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide
    for information about OSM Overpass queries
    and http://wiki.openstreetmap.org/wiki/Map_Features
    for a list of tags.

Returns
-------
nodes : pandas.DataFrame
    Will have 'lat' and 'lon' columns, plus other columns for the
    tags associated with the node (these will vary based on the query).
    Index will be the OSM node IDs.


<EOL>df = pd.merge(pd.DataFrame({"<STR_LIT:left>": series2}),<EOL>pd.DataFrame({"<STR_LIT:right>": series1}),<EOL>left_on="<STR_LIT:left>",<EOL>right_index=True,<EOL>how="<STR_LIT:left>")<EOL>return df.right<EOL>

Reindex the first series by the second series.

Parameters
----------
series1 : pandas.Series
    Pandas series to reindex
series2 : pandas.Series
    Pandas series to set the index of series1 by

Returns
-------
df.right : pandas.DataFrame


return ph5.network_from_pandas_hdf5(cls, filename)<EOL>

Load a previously saved Network from a Pandas HDF5 file.

Parameters
----------
filename : str

Returns
-------
network : pandana.Network


def save_hdf5(self, filename, rm_nodes=None):

ph5.network_to_pandas_hdf5(self, filename, rm_nodes)<EOL>

Save network data to a Pandas HDF5 file.

Only the nodes and edges of the actual network are saved,
points-of-interest and data attached to nodes are not saved.

Parameters
----------
filename : str
rm_nodes : array_like
    A list, array, Index, or Series of node IDs that should *not*
    be saved as part of the Network.


return self.node_idx.index<EOL>

The node ids which will be used as the index of many return series


return [self.nodes_df.x.min(), self.nodes_df.y.min(),<EOL>self.nodes_df.x.max(), self.nodes_df.y.max()]<EOL>

The bounding box for nodes in this network [xmin, ymin, xmax, ymax]


def set(self, node_ids, variable=None, name="<STR_LIT>"):

if variable is None:<EOL><INDENT>variable = pd.Series(np.ones(len(node_ids)), index=node_ids.index)<EOL><DEDENT>df = pd.DataFrame({name: variable,<EOL>"<STR_LIT>": self._node_indexes(node_ids)})<EOL>length = len(df)<EOL>df = df.dropna(how="<STR_LIT>")<EOL>newl = len(df)<EOL>if length-newl > <NUM_LIT:0>:<EOL><INDENT>print(<EOL>"<STR_LIT>" %<EOL>(length-newl))<EOL><DEDENT>self.variable_names.add(name)<EOL>self.net.initialize_access_var(name.encode('<STR_LIT:utf-8>'),<EOL>df.node_idx.values.astype('<STR_LIT:int>'),<EOL>df[name].values.astype('<STR_LIT>'))<EOL>

Characterize urban space with a variable that is related to nodes in
the network.

Parameters
----------
node_ids : Pandas Series, int
    A series of node_ids which are usually computed using
    get_node_ids on this object.
variable : Pandas Series, numeric, optional
    A series which represents some variable defined in urban space.
    It could be the location of buildings, or the income of all
    households - just about anything can be aggregated using the
    network queries provided here and this provides the api to set
    the variable at its disaggregate locations.  Note that node_id
    and variable should have the same index (although the index is
    not actually used).  If variable is not set, then it is assumed
    that the variable is all "ones" at the location specified by
    node_ids.  This could be, for instance, the location of all
    coffee shops which don't really have a variable to aggregate. The
    variable is connected to the closest node in the Pandana network
    which assumes no impedance between the location of the variable
    and the location of the closest network node.
name : string, optional
    Name the variable.  This is optional in the sense that if you don't
    specify it, the default name will be used.  Since the same
    default name is used by aggregate on this object, you can
    alternate between characterize and aggregate calls without
    setting names.

Returns
-------
Nothing


self.net.precompute_range(distance)<EOL>

Precomputes the range queries (the reachable nodes within this
maximum distance.  So as long as you use a smaller distance, cached
results will be used.)

Parameters
----------
distance : float
    The maximum distance to use. This will usually be a distance unit
    in meters however if you have customized the impedance this could
    be in other units such as utility or time etc.

Returns
-------
Nothing


def aggregate(self, distance, type="<STR_LIT>", decay="<STR_LIT>", imp_name=None,<EOL>name="<STR_LIT>"):

imp_num = self._imp_name_to_num(imp_name)<EOL>type = type.lower()<EOL>if type == "<STR_LIT>":<EOL><INDENT>type = "<STR_LIT>"  <EOL><DEDENT>assert name in self.variable_names, "<STR_LIT>""<STR_LIT>"<EOL>res = self.net.get_all_aggregate_accessibility_variables(distance,<EOL>name.encode('<STR_LIT:utf-8>'),<EOL>type.encode('<STR_LIT:utf-8>'),<EOL>decay.encode('<STR_LIT:utf-8>'),<EOL>imp_num)<EOL>return pd.Series(res, index=self.node_ids)<EOL>

Aggregate information for every source node in the network - this is
really the main purpose of this library.  This allows you to touch
the data specified by calling set and perform some aggregation on it
within the specified distance.  For instance, summing the population
within 1000 meters.

Parameters
----------
distance : float
    The maximum distance to aggregate data within. 'distance' can
    represent any impedance unit that you have set as your edge
    weight. This will usually be a distance unit in meters however
    if you have customized the impedance this could be in other
    units such as utility or time etc.
type : string
    The type of aggregation, can be one of "ave", "sum", "std",
    "count", and now "min", "25pct", "median", "75pct", and "max" will
    compute the associated quantiles.  (Quantiles are computed by
    sorting so might be slower than the others.)
decay : string
    The type of decay to apply, which makes things that are further
    away count less in the aggregation - must be one of "linear",
    "exponential" or "flat" (which means no decay).  Linear is the
    fastest computation to perform.  When performing an "ave",
    the decay is typically "flat"
imp_name : string, optional
    The impedance name to use for the aggregation on this network.
    Must be one of the impedance names passed in the constructor of
    this object.  If not specified, there must be only one impedance
    passed in the constructor, which will be used.
name : string, optional
    The variable to aggregate.  This variable will have been created
    and named by a call to set.  If not specified, the default
    variable name will be used so that the most recent call to set
    without giving a name will be the variable used.

Returns
-------
agg : Pandas Series
    Returns a Pandas Series for every origin node in the network,
    with the index which is the same as the node_ids passed to the
    init method and the values are the aggregations for each source
    node in the network.


def get_node_ids(self, x_col, y_col, mapping_distance=None):

xys = pd.DataFrame({'<STR_LIT:x>': x_col, '<STR_LIT:y>': y_col})<EOL>distances, indexes = self.kdtree.query(xys.as_matrix())<EOL>indexes = np.transpose(indexes)[<NUM_LIT:0>]<EOL>distances = np.transpose(distances)[<NUM_LIT:0>]<EOL>node_ids = self.nodes_df.iloc[indexes].index<EOL>df = pd.DataFrame({"<STR_LIT>": node_ids, "<STR_LIT>": distances},<EOL>index=xys.index)<EOL>if mapping_distance is not None:<EOL><INDENT>df = df[df.distance <= mapping_distance]<EOL><DEDENT>return df.node_id<EOL>

Assign node_ids to data specified by x_col and y_col

Parameters
----------
x_col : Pandas series (float)
    A Pandas Series where values specify the x (e.g. longitude)
    location of dataset.
y_col : Pandas series (float)
    A Pandas Series where values specify the y (e.g. latitude)
    location of dataset.  x_col and y_col should use the same index.
mapping_distance : float, optional
    The maximum distance that will be considered a match between the
    x, y data and the nearest node in the network.  This will usually
    be a distance unit in meters however if you have customized the
    impedance this could be in other units such as utility or time
    etc. If not specified, every x, y coordinate will be mapped to
    the nearest node.

Returns
-------
node_ids : Pandas series (int)
    Returns a Pandas Series of node_ids for each x, y in the
    input data. The index is the same as the indexes of the
    x, y input data, and the values are the mapped node_ids.
    If mapping distance is not passed and if there are no nans in the
    x, y data, this will be the same length as the x, y data.
    If the mapping is imperfect, this function returns all the
    input x, y's that were successfully mapped to node_ids.


def plot(<EOL>self, data, bbox=None, plot_type='<STR_LIT>',<EOL>fig_kwargs=None, bmap_kwargs=None, plot_kwargs=None,<EOL>cbar_kwargs=None):

from mpl_toolkits.basemap import Basemap<EOL>fig_kwargs = fig_kwargs or {}<EOL>bmap_kwargs = bmap_kwargs or {}<EOL>plot_kwargs = plot_kwargs or {}<EOL>cbar_kwargs = cbar_kwargs or {}<EOL>if not bbox:<EOL><INDENT>bbox = (<EOL>self.nodes_df.y.min(),<EOL>self.nodes_df.x.min(),<EOL>self.nodes_df.y.max(),<EOL>self.nodes_df.x.max())<EOL><DEDENT>fig, ax = plt.subplots(**fig_kwargs)<EOL>bmap = Basemap(<EOL>bbox[<NUM_LIT:1>], bbox[<NUM_LIT:0>], bbox[<NUM_LIT:3>], bbox[<NUM_LIT:2>], ax=ax, **bmap_kwargs)<EOL>bmap.drawcoastlines()<EOL>bmap.drawmapboundary()<EOL>x, y = bmap(self.nodes_df.x.values, self.nodes_df.y.values)<EOL>if plot_type == '<STR_LIT>':<EOL><INDENT>plot = bmap.scatter(<EOL>x, y, c=data.values, **plot_kwargs)<EOL><DEDENT>elif plot_type == '<STR_LIT>':<EOL><INDENT>plot = bmap.hexbin(<EOL>x, y, C=data.values, **plot_kwargs)<EOL><DEDENT>bmap.colorbar(plot, **cbar_kwargs)<EOL>return bmap, fig, ax<EOL>

Plot an array of data on a map using matplotlib and Basemap,
automatically matching the data to the Pandana network node positions.

Keyword arguments are passed to the plotting routine.

Parameters
----------
data : pandas.Series
    Numeric data with the same length and index as the nodes
    in the network.
bbox : tuple, optional
    (lat_min, lng_min, lat_max, lng_max)
plot_type : {'hexbin', 'scatter'}, optional
fig_kwargs : dict, optional
    Keyword arguments that will be passed to
    matplotlib.pyplot.subplots. Use this to specify things like
    figure size or background color.
bmap_kwargs : dict, optional
    Keyword arguments that will be passed to the Basemap constructor.
    This can be used to specify a projection or coastline resolution.
plot_kwargs : dict, optional
    Keyword arguments that will be passed to the matplotlib plotting
    command used. Use this to control plot styles and color maps used.
cbar_kwargs : dict, optional
    Keyword arguments passed to the Basemap.colorbar method.
    Use this to control color bar location and label.

Returns
-------
bmap : Basemap
fig : matplotlib.Figure
ax : matplotlib.Axes


def set_pois(self, category, maxdist, maxitems, x_col, y_col):

if category not in self.poi_category_names:<EOL><INDENT>self.poi_category_names.append(category)<EOL><DEDENT>self.max_pois = maxitems<EOL>node_ids = self.get_node_ids(x_col, y_col)<EOL>self.poi_category_indexes[category] = node_ids.index<EOL>node_idx = self._node_indexes(node_ids)<EOL>self.net.initialize_category(maxdist, maxitems, category.encode('<STR_LIT:utf-8>'), node_idx.values)<EOL>

Set the location of all the pois of this category. The pois are
connected to the closest node in the Pandana network which assumes
no impedance between the location of the variable and the location
of the closest network node.

Parameters
----------
category : string
    The name of the category for this set of pois
maxdist - the maximum distance that will later be used in
    find_all_nearest_pois
maxitems - the maximum number of items that will later be requested
    in find_all_nearest_pois
x_col : Pandas Series (float)
    The x location (longitude) of pois in this category
y_col : Pandas Series (Float)
    The y location (latitude) of pois in this category

Returns
-------
Nothing


def nearest_pois(self, distance, category, num_pois=<NUM_LIT:1>, max_distance=None,<EOL>imp_name=None, include_poi_ids=False):

if max_distance is None:<EOL><INDENT>max_distance = distance<EOL><DEDENT>if category not in self.poi_category_names:<EOL><INDENT>assert <NUM_LIT:0>, "<STR_LIT>"<EOL><DEDENT>if num_pois > self.max_pois:<EOL><INDENT>assert <NUM_LIT:0>, "<STR_LIT>"<EOL><DEDENT>imp_num = self._imp_name_to_num(imp_name)<EOL>dists, poi_ids = self.net.find_all_nearest_pois(<EOL>distance,<EOL>num_pois,<EOL>category.encode('<STR_LIT:utf-8>'),<EOL>imp_num)<EOL>dists[dists == -<NUM_LIT:1>] = max_distance<EOL>df = pd.DataFrame(dists, index=self.node_ids)<EOL>df.columns = list(range(<NUM_LIT:1>, num_pois+<NUM_LIT:1>))<EOL>if include_poi_ids:<EOL><INDENT>df2 = pd.DataFrame(poi_ids, index=self.node_ids)<EOL>df2.columns = ["<STR_LIT>" % i for i in range(<NUM_LIT:1>, num_pois+<NUM_LIT:1>)]<EOL>for col in df2.columns:<EOL><INDENT>s = df2[col].astype('<STR_LIT:int>')<EOL>df2[col] = self.poi_category_indexes[category].values[s]<EOL>df2.loc[s == -<NUM_LIT:1>, col] = np.nan<EOL><DEDENT>df = pd.concat([df, df2], axis=<NUM_LIT:1>)<EOL><DEDENT>return df<EOL>

Find the distance to the nearest pois from each source node.  The
bigger values in this case mean less accessibility.

Parameters
----------
distance : float
    The maximum distance to look for pois. This will usually be a
    distance unit in meters however if you have customized the
    impedance this could be in other units such as utility or time
    etc.
category : string
    The name of the category of poi to look for
num_pois : int
    The number of pois to look for, this also sets the number of
    columns in the DataFrame that gets returned
max_distance : float, optional
    The value to set the distance to if there is NO poi within the
    specified distance - if not specified, gets set to distance. This
    will usually be a distance unit in meters however if you have
    customized the impedance this could be in other units such as
    utility or time etc.
imp_name : string, optional
    The impedance name to use for the aggregation on this network.
    Must be one of the impedance names passed in the constructor of
    this object.  If not specified, there must be only one impedance
    passed in the constructor, which will be used.
include_poi_ids : bool, optional
    If this flag is set to true, the call will add columns to the
    return DataFrame - instead of just returning the distance for
    the nth POI, it will also return the id of that POI.  The names
    of the columns with the poi ids will be poi1, poi2, etc - it
    will take roughly twice as long to include these ids as to not
    include them

Returns
-------
d : Pandas DataFrame
    Like aggregate, this series has an index of all the node ids for
    the network.  Unlike aggregate, this method returns a dataframe
    with the number of columns equal to the distances to the Nth
    closest poi.  For instance, if you ask for the 10 closest poi to
    each node, column d[1] wil be the distance to the 1st closest poi
    of that category while column d[2] will be the distance to the 2nd
    closest poi, and so on.


def low_connectivity_nodes(self, impedance, count, imp_name=None):

<EOL>self.set(self.node_ids.to_series(), name='<STR_LIT>')<EOL>agg = self.aggregate(<EOL>impedance, type='<STR_LIT:count>', imp_name=imp_name, name='<STR_LIT>')<EOL>return np.array(agg[agg < count].index)<EOL>

Identify nodes that are connected to fewer than some threshold
of other nodes within a given distance.

Parameters
----------
impedance : float
    Distance within which to search for other connected nodes. This
    will usually be a distance unit in meters however if you have
    customized the impedance this could be in other units such as
    utility or time etc.
count : int
    Threshold for connectivity. If a node is connected to fewer
    than this many nodes within `impedance` it will be identified
    as "low connectivity".
imp_name : string, optional
    The impedance name to use for the aggregation on this network.
    Must be one of the impedance names passed in the constructor of
    this object.  If not specified, there must be only one impedance
    passed in the constructor, which will be used.

Returns
-------
node_ids : array
    List of "low connectivity" node IDs.


total, correct = <NUM_LIT:0.0>, <NUM_LIT:0.0><EOL>for true_response, guess_dict in list(conf_matrix.items()):<EOL><INDENT>for guess, count in list(guess_dict.items()):<EOL><INDENT>if true_response == guess:<EOL><INDENT>correct += count<EOL><DEDENT>total += count<EOL><DEDENT><DEDENT>return correct/total<EOL>

Given a confusion matrix, returns the accuracy.
Accuracy Definition: http://research.ics.aalto.fi/events/eyechallenge2005/evaluation.shtml


parts = []<EOL>for true_response, guess_dict in list(conf_matrix.items()):<EOL><INDENT>error = <NUM_LIT:0.0><EOL>total = <NUM_LIT:0.0><EOL>for guess, count in list(guess_dict.items()):<EOL><INDENT>if true_response != guess:<EOL><INDENT>error += count<EOL><DEDENT>total += count<EOL><DEDENT>parts.append(error/total)<EOL><DEDENT>BER = sum(parts)/len(parts)<EOL>return <NUM_LIT:1> - BER<EOL>

Given a confusion matrix, returns Balanced Classification Rate.
BCR is (1 - Balanced Error Rate).
BER Definition: http://research.ics.aalto.fi/events/eyechallenge2005/evaluation.shtml


with open(filename) as audit_file:<EOL><INDENT>next(audit_file) <EOL>confusion_matrices = []<EOL>for line in audit_file:<EOL><INDENT>separator = "<STR_LIT::>"<EOL>separator_index = line.index(separator)<EOL>comma_index = line.index('<STR_LIT:U+002C>')<EOL>repair_level = float(line[separator_index+<NUM_LIT:2>:comma_index])<EOL>raw_confusion_matrix = line[comma_index+<NUM_LIT:2>:-<NUM_LIT:2>]<EOL>confusion_matrix = json.loads( raw_confusion_matrix.replace("<STR_LIT:'>","<STR_LIT>") )<EOL>confusion_matrices.append( (repair_level, confusion_matrix) )<EOL><DEDENT><DEDENT>confusion_matrices.sort(key = lambda pair: pair[<NUM_LIT:0>])<EOL>return confusion_matrices<EOL>

Loads a confusion matrix in a two-level dictionary format.

For example, the confusion matrix of a 75%-accurate model
that predicted 15 values (and mis-classified 5) may look like:
{"A": {"A":10, "B": 5}, "B": {"B":5}}

Note that raw boolean values are translated into strings, such that
a value that was the boolean True will be returned as the string "True".


def group_audit_ranks(filenames, measurer, similarity_bound=<NUM_LIT>):

def _partition_groups(feature_scores):<EOL><INDENT>groups = []<EOL>for feature, score in feature_scores:<EOL><INDENT>added_to_group = False<EOL>for i, group in enumerate(groups):<EOL><INDENT>mean_score, group_feature_scores = group<EOL>if abs(mean_score - score) < similarity_bound:<EOL><INDENT>groups[i][<NUM_LIT:1>].append( (feature, score) )<EOL>groups[i][<NUM_LIT:0>] = sum([s for _, s in group_feature_scores])/len(group_feature_scores)<EOL>added_to_group = True<EOL>break<EOL><DEDENT><DEDENT>if not added_to_group:<EOL><INDENT>groups.append( [score, [(feature,score)]] )<EOL><DEDENT><DEDENT>return [[feature for feature, score in group] for _, group in groups]<EOL><DEDENT>score_dict = {}<EOL>features = []<EOL>for filename in filenames:<EOL><INDENT>with open(filename) as audit_file:<EOL><INDENT>header_line = audit_file.readline()[:-<NUM_LIT:1>] <EOL>feature = header_line[header_line.index("<STR_LIT::>")+<NUM_LIT:1>:]<EOL>features.append(feature)<EOL><DEDENT>confusion_matrices = load_audit_confusion_matrices(filename)<EOL>for rep_level, matrix in confusion_matrices:<EOL><INDENT>score = measurer(matrix)<EOL>if rep_level not in score_dict:<EOL><INDENT>score_dict[rep_level] = {}<EOL><DEDENT>score_dict[rep_level][feature] = score<EOL><DEDENT><DEDENT>score_keys = sorted(score_dict.keys())<EOL>groups = [features]<EOL>while score_keys:<EOL><INDENT>key = score_keys.pop()<EOL>new_groups = []<EOL>for group in groups:<EOL><INDENT>group_features = [(f, score_dict[key][f]) for f in group]<EOL>sub_groups = _partition_groups(group_features)<EOL>new_groups.extend(sub_groups)<EOL><DEDENT>groups = new_groups<EOL><DEDENT>return groups<EOL>

Given a list of audit files, rank them using the `measurer` and
return the features that never deviate more than `similarity_bound`
across repairs.


def __init__(self, all_data, feature_to_repair, repair_level, kdd, features_to_ignore=[]):

self.all_data = all_data<EOL>self.feature_to_repair = feature_to_repair<EOL>self.repair_level = repair_level<EOL>self.kdd = kdd<EOL>self.features_to_ignore = features_to_ignore<EOL>

all_data should be a list of rows (ie, a list of lists) composing the entire
test and training dataset. Headers should not be included in data sets.

feature_to_repair should be the index of the feature to repair. (ie, 0 to k)
where k is the number of features in the dataset.

repair_level should be a float between [0,1] representing the level of repair.

features_to_ignore should be a list of feature indexes that should be ignored.


pass<EOL>

data_to_repair is the list of rows that actually should be repaired.


if not values:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>sorted_values = deepcopy(values)<EOL>sorted_values.sort() <EOL>if kdd:<EOL><INDENT>return sorted_values[len(values)//<NUM_LIT:2>]<EOL><DEDENT>else:<EOL><INDENT>if len(values) % <NUM_LIT:2> == <NUM_LIT:0>:<EOL><INDENT>return sorted_values[len(values)//<NUM_LIT:2>-<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>return sorted_values[len(values)//<NUM_LIT:2>]<EOL><DEDENT><DEDENT>

Given an unsorted list of numeric values, return median value (as a float).
Note that in the case of even-length lists of values, we apply the value to
the left of the center to be the median (such that the median can only be
a value from the list of values).
Eg: get_median([1,2,3,4]) == 2, not 2.5.


q75, q25 = numpy.percentile(feature_values, [<NUM_LIT>, <NUM_LIT>])<EOL>IQR = q75 - q25<EOL>return <NUM_LIT> * IQR * len(feature_values) ** (-<NUM_LIT:1.0>/<NUM_LIT>)<EOL>

The bin size in FD-binning is given by size = 2 * IQR(x) * n^(-1/3)
More Info: https://en.wikipedia.org/wiki/Freedman%E2%80%93Diaconis_rule

If the BinSize ends up being 0 (in the case that all values are the same),
return a BinSize of 1.


unmatched = list(b)<EOL>for element in a:<EOL><INDENT>try:<EOL><INDENT>unmatched.remove(element)<EOL><DEDENT>except ValueError:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>return not unmatched<EOL>

Used to check whether the two edge lists have the same edges
when elements are neither hashable nor sortable.


def list_to_tf_input(data, response_index, num_outcomes):

matrix = np.matrix([row[:response_index] + row[response_index+<NUM_LIT:1>:] for row in data])<EOL>outcomes = np.asarray([row[response_index] for row in data], dtype=np.uint8)<EOL>return matrix, outcomes<EOL>

Separates the outcome feature from the data.


def expand_and_standardize_dataset(response_index, response_header, data_set, col_vals, headers, standardizers, feats_to_ignore, columns_to_expand, outcome_trans_dict):

<EOL>modified_set = []<EOL>for row_index, row in enumerate(data_set):<EOL><INDENT>new_row = []<EOL>for col_index, val in enumerate(row):<EOL><INDENT>header = headers[col_index]<EOL>if col_index == response_index:<EOL><INDENT>new_outcome = outcome_trans_dict[val]<EOL>new_row.append(new_outcome)<EOL><DEDENT>elif header in feats_to_ignore:<EOL><INDENT>pass<EOL><DEDENT>elif header in columns_to_expand:<EOL><INDENT>for poss_val in col_vals[header]:<EOL><INDENT>if val == poss_val:<EOL><INDENT>new_cat_val = <NUM_LIT:1.0><EOL><DEDENT>else:<EOL><INDENT>new_cat_val = -<NUM_LIT:1.0><EOL><DEDENT>new_row.append(new_cat_val)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>new_cont_val = float((val - standardizers[header]['<STR_LIT>']) / standardizers[header]['<STR_LIT>'])<EOL>new_row.append(new_cont_val)<EOL><DEDENT><DEDENT>modified_set.append(new_row)<EOL><DEDENT>expanded_headers = []<EOL>for header in headers:<EOL><INDENT>if header in feats_to_ignore:<EOL><INDENT>pass<EOL><DEDENT>elif (header in columns_to_expand) and (header is not response_header):<EOL><INDENT>for poss_val in col_vals[header]:<EOL><INDENT>new_header = '<STR_LIT>'.format(header,poss_val)<EOL>expanded_headers.append(new_header)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>expanded_headers.append(header)<EOL><DEDENT><DEDENT>return modified_set, expanded_headers<EOL>

Standardizes continuous features and expands categorical features.


matrix = np.matrix([row[:response_index] + row[response_index+<NUM_LIT:1>:] for row in data])<EOL>outcomes = np.asarray([row[response_index] for row in data], dtype=np.uint8)<EOL>outcomes_onehot = (np.arange(num_outcomes) == outcomes[:, None]).astype(np.float32)<EOL>return matrix, outcomes_onehot<EOL>

Separates the outcome feature from the data and creates the onehot vector for each row.


if X[<NUM_LIT:0>] == [<NUM_LIT:1.0>] and X[<NUM_LIT:1>] == [<NUM_LIT>] and X[<NUM_LIT:2>] == [<NUM_LIT>] and X[<NUM_LIT:3>] == [<NUM_LIT>]:<EOL><INDENT>return [<NUM_LIT:0>,<NUM_LIT:0>,<NUM_LIT:0>,<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>return [<NUM_LIT:1>,<NUM_LIT:1>,<NUM_LIT:1>,<NUM_LIT:1>]<EOL><DEDENT>return prediction<EOL>

Only predicts [0,0,0,0] if given [1,2,3,4].


pass<EOL>


if not values:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>sorted_values = deepcopy(values)<EOL>sorted_values.sort() <EOL>if kdd:<EOL><INDENT>return sorted_values[len(values)/<NUM_LIT:2>]<EOL><DEDENT>else:<EOL><INDENT>if len(values) % <NUM_LIT:2> == <NUM_LIT:0>:<EOL><INDENT>return sorted_values[len(values)/<NUM_LIT:2>-<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>return sorted_values[len(values)/<NUM_LIT:2>]<EOL><DEDENT><DEDENT>


def record_error(hostname, exc_info, preceding_stack=None, error_threshold=None, additional_info=None):

stack = []<EOL>exc_type, exc_value, sys_traceback = exc_info<EOL>while sys_traceback is not None:<EOL><INDENT>stack.append(sys_traceback)<EOL>sys_traceback = sys_traceback.tb_next<EOL><DEDENT>stack_lines = []<EOL>for row in preceding_stack or []:<EOL><INDENT>stack_lines.append(<EOL>api_ttypes.StackLine(filename=os.path.abspath(row[<NUM_LIT:0>]), line_number=row[<NUM_LIT:1>],<EOL>function_name=row[<NUM_LIT:2>], text=row[<NUM_LIT:3>])<EOL>)<EOL><DEDENT>for index, tb in enumerate(stack):<EOL><INDENT>filename = tb.tb_frame.f_code.co_filename<EOL>func_name = tb.tb_frame.f_code.co_name<EOL>lineno = tb.tb_lineno<EOL>line = linecache.getline(filename, lineno, tb.tb_frame.f_globals)<EOL>frame_locals = None<EOL>if index >= (len(stack) - NUM_FRAMES_TO_SAVE):<EOL><INDENT>frame_locals = dict((k, _myrepr(k, v)) for k, v in<EOL>list(tb.tb_frame.f_locals.items())[:MAX_LOCALS] if k != "<STR_LIT>")<EOL>if "<STR_LIT>" in tb.tb_frame.f_locals and hasattr(tb.tb_frame.f_locals["<STR_LIT>"], "<STR_LIT>"):<EOL><INDENT>frame_locals.update(dict(("<STR_LIT>" + k, _myrepr(k, v)) for k, v in<EOL>list(tb.tb_frame.f_locals["<STR_LIT>"].__dict__.items())[:MAX_LOCALS]<EOL>if k != "<STR_LIT>"))<EOL><DEDENT><DEDENT>stack_lines.append(<EOL>api_ttypes.StackLine(filename=os.path.abspath(filename), line_number=lineno,<EOL>function_name=func_name, text=line, frame_locals=frame_locals)<EOL>)<EOL><DEDENT>key = CachedErrorInfo.get_hash_key(stack_lines)<EOL>info = ERROR_CACHE.get(key) or CachedErrorInfo()<EOL>info.increment()<EOL>ERROR_CACHE[key] = info<EOL>if info.should_report():<EOL><INDENT>error_count = info.mark_reported()<EOL>_send_request(<EOL>api_ttypes.RecordErrorRequest(<EOL>traceback=stack_lines,<EOL>exception_message=repr(exc_value),<EOL>exception_type=exc_type.__module__ + "<STR_LIT:.>" + exc_type.__name__,<EOL>hostname=hostname,<EOL>error_threshold=error_threshold,<EOL>additional_info=additional_info,<EOL>error_count=error_count,<EOL>)<EOL>)<EOL><DEDENT>

Helper function to record errors to the flawless backend


def wrap_function(func=None, error_threshold=None, reraise_exception=True, save_current_stack_trace=True):

Wraps a function with reporting to errors backend


methods = inspect.getmembers(cls, inspect.ismethod) + inspect.getmembers(cls, inspect.isfunction)<EOL>for method_name, method in methods:<EOL><INDENT>wrapped_method = flawless.client.client._wrap_function_with_error_decorator(<EOL>method if not im_self(method) else im_func(method),<EOL>save_current_stack_trace=False,<EOL>error_threshold=error_threshold,<EOL>)<EOL>if im_self(method):<EOL><INDENT>wrapped_method = classmethod(wrapped_method)<EOL><DEDENT>setattr(cls, method_name, wrapped_method)<EOL><DEDENT>return cls<EOL>

Wraps a class with reporting to errors backend by decorating each function of the class.
            Decorators are injected under the classmethod decorator if they exist.