text:
# coding=utf-8
"""
API for dataset indexing, access and search.
"""
from __future__ import absolute_import

import logging

from cachetools.func import lru_cache

from datacube import compat
from datacube.model import Dataset, DatasetType, MetadataType
from datacube.utils import InvalidDocException, check_doc_unchanged, jsonify_document, get_doc_changes, contains
from . import fields
from .exceptions import DuplicateRecordError, UnknownFieldError

_LOG = logging.getLogger(__name__)

class MetadataTypeResource(object):
    def __init__(self, db):
        """
        :type db: datacube.index.postgres._api.PostgresDb
        """
        self._db = db

    def add(self, definition, allow_table_lock=False):
        """
        :type definition: dict
        :param allow_table_lock:
            Allow an exclusive lock to be taken on the table while creating the indexes.
            This will halt other user's requests until completed.

            If false, creation will be slightly slower and cannot be done in a transaction.
        :rtype: datacube.model.MetadataType
        """
        # This column duplication is getting out of hand:
        MetadataType.validate(definition)

        name = definition['name']

        existing = self._db.get_metadata_type_by_name(name)
        if existing:
            # They've passed us the same one again. Make sure it matches what is stored.
            # TODO: Support for adding/updating search fields?
            check_doc_unchanged(
                existing.definition,
                definition,
                'Metadata Type {}'.format(name)
            )
        else:
            self._db.add_metadata_type(
                name=name,
                definition=definition,
                concurrently=not allow_table_lock
            )
        return self.get_by_name(name)

    @lru_cache()
    def get(self, id_):
        """
        :rtype: datacube.model.MetadataType
        """
        return self._make(self._db.get_metadata_type(id_))

    @lru_cache()
    def get_by_name(self, name):
        """
        :rtype: datacube.model.MetadataType
        """
        record = self._db.get_metadata_type_by_name(name)
        if not record:
            return None
        return self._make(record)

    def check_field_indexes(self, allow_table_lock=False, rebuild_all=False):
        """
        Create or replace per-field indexes and views.
        :param allow_table_lock:
            Allow an exclusive lock to be taken on the table while creating the indexes.
            This will halt other user's requests until completed.

            If false, creation will be slightly slower and cannot be done in a transaction.
        """
        self._db.check_dynamic_fields(concurrently=not allow_table_lock, rebuild_all=rebuild_all)

    def _make_many(self, query_rows):
        return (self._make(c) for c in query_rows)

    def _make(self, query_row):
        """
        :rtype list[datacube.model.MetadataType]
        """
        definition = query_row['definition']
        dataset_ = definition['dataset']
        return MetadataType(
            query_row['name'],
            dataset_,
            dataset_search_fields=self._db.get_dataset_fields(query_row),
            id_=query_row['id']
        )

class DatasetTypeResource(object):
    """
    :type _db: datacube.index.postgres._api.PostgresDb
    :type metadata_type_resource: MetadataTypeResource
    """

    def __init__(self, db, metadata_type_resource):
        """
        :type db: datacube.index.postgres._api.PostgresDb
        :type metadata_type_resource: MetadataTypeResource
        """
        self._db = db
        self.metadata_type_resource = metadata_type_resource

    def from_doc(self, definition):
        """
        Create a Product from its definitions

        :param dict definition: product definition document
        :rtype: datacube.model.DatasetType
        """
        # This column duplication is getting out of hand:
        DatasetType.validate(definition)

        metadata_type = definition['metadata_type']

        # They either specified the name of a metadata type, or specified a metadata type.
        # Is it a name?
        if isinstance(metadata_type, compat.string_types):
            metadata_type = self.metadata_type_resource.get_by_name(metadata_type)
        else:
            # Otherwise they embedded a document, add it if needed:
            metadata_type = self.metadata_type_resource.add(metadata_type, allow_table_lock=False)

        if not metadata_type:
            raise InvalidDocException('Unknown metadata type: %r' % definition['metadata_type'])

        return DatasetType(metadata_type, definition)

    def add(self, type_):
        """
        Add a Product

        :param datacube.model.DatasetType type_: Product to add
        :rtype: datacube.model.DatasetType
        """
        DatasetType.validate(type_.definition)

        existing = self._db.get_dataset_type_by_name(type_.name)
        if existing:
            # TODO: Support for adding/updating match rules?
            # They've passed us the same collection again. Make sure it matches what is stored.
            check_doc_unchanged(
                existing.definition,
                jsonify_document(type_.definition),
                'Dataset type {}'.format(type_.name)
            )
        else:
            self._db.add_dataset_type(
                name=type_.name,
                metadata=type_.metadata_doc,
                metadata_type_id=type_.metadata_type.id,
                definition=type_.definition
            )
        return self.get_by_name(type_.name)

    def update(self, type_, allow_unsafe_updates=False):
        """
        Update a product. Unsafe changes will throw a ValueError by default.

        (An unsafe change is anything that may potentially make the product
        incompatible with existing datasets of that type)

        :param datacube.model.DatasetType type_: Product to add
        :param allow_unsafe_updates bool: Allow unsafe changes. Use with caution.
        :rtype: datacube.model.DatasetType
        """
        DatasetType.validate(type_.definition)

        existing = self._db.get_dataset_type_by_name(type_.name)
        if not existing:
            raise ValueError('Unknown product %s, cannot update – did you intend to add it?' % type_.name)

        def handle_unsafe(msg):
            if not allow_unsafe_updates:
                raise ValueError(msg)
            else:
                _LOG.warning("Ignoring %s", msg)

        # We'll probably want to use offsets in the future (ie. nested dicts), not just keys, but for now this suffices.
        safe_keys_to_change = ('description', 'metadata')

        doc_changes = get_doc_changes(existing.definition, jsonify_document(type_.definition))
        for offset, old_value, new_value in doc_changes:
            _LOG.info('Changing %s %s: %r -> %r', type_.name, '.'.join(offset), old_value, new_value)

            key_name = offset[0]
            if key_name not in safe_keys_to_change:
                handle_unsafe('Potentially unsafe update: changing %r of product definition.' % key_name)

            # You can safely make the match rules looser but not tighter.
            if key_name == 'metadata':
                # Tightening them could exclude datasets already matched to the product.
                # (which would make search results wrong)
                if not contains(old_value, new_value, case_sensitive=True):
                    handle_unsafe('Unsafe update: new product match rules are not a superset of old ones.')

        if doc_changes:
            _LOG.info("Updating product %s", type_.name)
            self._db.update_dataset_type(
                name=type_.name,
                metadata=type_.metadata_doc,
                metadata_type_id=type_.metadata_type.id,
                definition=type_.definition
            )
            # Clear our local cache. Note that other users may still have
            # cached copies for the duration of their connections.
            self.get_by_name.cache_clear()
            self.get.cache_clear()
        else:
            _LOG.info("No changes detected for product %s", type_.name)

    def update_document(self, definition, allow_unsafe_update=False):
        """
        Update a Product using its difinition

        :param dict definition: product definition document
        :rtype: datacube.model.DatasetType
        """
        type_ = self.from_doc(definition)
        return self.update(type_, allow_unsafe_updates=allow_unsafe_update)

    def add_document(self, definition):
        """
        Add a Product using its difinition

        :param dict definition: product definition document
        :rtype: datacube.model.DatasetType
        """
        type_ = self.from_doc(definition)
        return self.add(type_)

    @lru_cache()
    def get(self, id_):
        """
        Retrieve Product by id

        :param int id_: id of the Product
        :rtype: datacube.model.DatasetType
        """
        return self._make(self._db.get_dataset_type(id_))

    @lru_cache()
    def get_by_name(self, name):
        """
        Retrieve Product by name

        :param str name: name of the Product
        :rtype: datacube.model.DatasetType
        """
        result = self._db.get_dataset_type_by_name(name)
        if not result:
            return None
        return self._make(result)

    def get_with_fields(self, field_names):
        """
        Return dataset types that have all the given fields.

        :param tuple[str] field_names:
        :rtype: __generator[DatasetType]
        """
        for type_ in self.get_all():
            for name in field_names:
                if name not in type_.metadata_type.dataset_fields:
                    break
            else:
                yield type_

    def search(self, **query):
        """
        Return dataset types that have all the given fields.

        :param dict query:
        :rtype: __generator[DatasetType]
        """
        for type_, q in self.search_robust(**query):
            if not q:
                yield type_

    def search_robust(self, **query):
        """
        Return dataset types that match match-able fields and dict of remaining un-matchable fields.

        :param dict query:
        :rtype: __generator[(DatasetType, dict)]
        """
        for type_ in self.get_all():
            q = query.copy()
            if q.pop('product', type_.name) != type_.name:
                continue
            if q.pop('metadata_type', type_.metadata_type.name) != type_.metadata_type.name:
                continue

            for key, value in list(q.items()):
                try:
                    exprs = fields.to_expressions(type_.metadata_type.dataset_fields.get, **{key: value})
                except UnknownFieldError as e:
                    break

                try:
                    if all(expr.evaluate(type_.metadata_doc) for expr in exprs):
                        q.pop(key)
                    else:
                        break
                except (AttributeError, KeyError, ValueError) as e:
                    continue
            else:
                yield type_, q

    def get_all(self):
        """
        Retrieve all Products

        :rtype: iter[datacube.model.DatasetType]
        """
        return (self._make(record) for record in self._db.get_all_dataset_types())

    def _make_many(self, query_rows):
        return (self._make(c) for c in query_rows)

    def _make(self, query_row):
        """
        :rtype datacube.model.DatasetType
        """
        return DatasetType(
            definition=query_row['definition'],
            metadata_type=self.metadata_type_resource.get(query_row['metadata_type_ref']),
            id_=query_row['id'],
        )

class DatasetResource(object):
    """
    :type _db: datacube.index.postgres._api.PostgresDb
    :type types: datacube.index._datasets.DatasetTypeResource
    """

    def __init__(self, db, dataset_type_resource):
        """
        :type db: datacube.index.postgres._api.PostgresDb
        :type dataset_type_resource: datacube.index._datasets.DatasetTypeResource
        """
        self._db = db
        self.types = dataset_type_resource

    def get(self, id_, include_sources=False):
        """
        Get dataset by id

        :param uuid id_: id of the dataset to retrieve
        :param bool include_sources: get the full provenance graph?
        :rtype: datacube.model.Dataset
        """
        if not include_sources:
            return self._make(self._db.get_dataset(id_), full_info=True)

        datasets = {result['id']: (self._make(result, full_info=True), result)
                    for result in self._db.get_dataset_sources(id_)}
        for dataset, result in datasets.values():
            dataset.metadata_doc['lineage']['source_datasets'] = {
                classifier: datasets[str(source)][0].metadata_doc
                for source, classifier in zip(result['sources'], result['classes']) if source
                }
            dataset.sources = {
                classifier: datasets[str(source)][0]
                for source, classifier in zip(result['sources'], result['classes']) if source
                }
        return datasets[id_][0]

    def get_derived(self, id_):
        """
        Get drived datasets

        :param uuid id_: dataset id
        :rtype: list[datacube.model.Dataset]
        """
        return [self._make(result) for result in self._db.get_derived_datasets(id_)]

    def has(self, dataset):
        """
        Have we already indexed this dataset?

        :param datacube.model.Dataset dataset: dataset to check
        :rtype: bool
        """
        return self._db.contains_dataset(dataset.id)

    def add(self, dataset, skip_sources=False):
        """
        Ensure a dataset is in the index. Add it if not present.

        :param datacube.model.Dataset dataset: dataset to add
        :param bool skip_sources: don't attempt to index source (use when sources are already indexed)
        :rtype: datacube.model.Dataset
        """
        if not skip_sources:
            for source in dataset.sources.values():
                self.add(source)

        was_inserted = False
        sources_tmp = dataset.type.dataset_reader(dataset.metadata_doc).sources
        dataset.type.dataset_reader(dataset.metadata_doc).sources = {}
        try:
            _LOG.info('Indexing %s', dataset.id)
            with self._db.begin() as transaction:
                try:
                    was_inserted = transaction.insert_dataset(dataset.metadata_doc, dataset.id, dataset.type.id)
                    for classifier, source_dataset in dataset.sources.items():
                        transaction.insert_dataset_source(classifier, dataset.id, source_dataset.id)

                    # try to update location in the same transaction as insertion.
                    # if insertion fails we'll try updating location later
                    # if insertion succeeds the location bit can't possibly fail
                    if dataset.local_uri:
                        transaction.ensure_dataset_location(dataset.id, dataset.local_uri)
                except DuplicateRecordError as e:
                    _LOG.warning(str(e))

            if not was_inserted:
                existing = self.get(dataset.id)
                if existing:
                    check_doc_unchanged(
                        existing.metadata_doc,
                        jsonify_document(dataset.metadata_doc),
                        'Dataset {}'.format(dataset.id)
                    )

                # reinsert attempt? try updating the location
                if dataset.local_uri:
                    try:
                        self._db.ensure_dataset_location(dataset.id, dataset.local_uri)
                    except DuplicateRecordError as e:
                        _LOG.warning(str(e))
        finally:
            dataset.type.dataset_reader(dataset.metadata_doc).sources = sources_tmp

        return dataset

    def archive(self, ids):
        """
        Mark datasets as archived

        :param list[uuid] ids: list of dataset ids to archive
        """
        with self._db.begin() as transaction:
            for id_ in ids:
                transaction.archive_dataset(id_)

    def restore(self, ids):
        """
        Mark datasets as not archived

        :param list[uuid] ids: list of dataset ids to restore
        """
        with self._db.begin() as transaction:
            for id_ in ids:
                transaction.restore_dataset(id_)

    def get_field_names(self, type_name=None):
        """
        :param str type_name:
        :rtype: __generator[str]
        """
        if type_name is None:
            types = self.types.get_all()
        else:
            types = [self.types.get_by_name(type_name)]

        for type_ in types:
            for name in type_.metadata_type.dataset_fields:
                yield name

    def get_locations(self, dataset):
        """
        :param datacube.model.Dataset dataset: dataset
        :rtype: list[str]
        """
        return self._db.get_locations(dataset.id)

    def _make(self, dataset_res, full_info=False):
        """
        :rtype datacube.model.Dataset

        :param bool full_info: Include all available fields
        """
        return Dataset(
            self.types.get(dataset_res.dataset_type_ref),
            dataset_res.metadata,
            dataset_res.local_uri,
            indexed_by=dataset_res.added_by if full_info else None,
            indexed_time=dataset_res.added if full_info else None
        )

    def _make_many(self, query_result):
        """
        :rtype list[datacube.model.Dataset]
        """
        return (self._make(dataset) for dataset in query_result)

    def search_by_metadata(self, metadata):
        """
        Perform a search using arbitrary metadata, returning results as Dataset objects.

        Caution – slow! This will usually not use indexes.

        :param dict metadata:
        :rtype: list[datacube.model.Dataset]
        """
        return self._make_many(self._db.search_datasets_by_metadata(metadata))

    def search(self, **query):
        """
        Perform a search, returning results as Dataset objects.

        :param dict[str,str|float|datacube.model.Range] query:
        :rtype: __generator[datacube.model.Dataset]
        """
        for dataset_type, datasets in self._do_search_by_product(query):
            for dataset in self._make_many(datasets):
                yield dataset

    def search_by_product(self, **query):
        """
        Perform a search, returning datasets grouped by product type.

        :param dict[str,str|float|datacube.model.Range] query:
        :rtype: __generator[(datacube.model.DatasetType,  __generator[datacube.model.Dataset])]]
        """
        for dataset_type, datasets in self._do_search_by_product(query):
            yield dataset_type, self._make_many(datasets)

    def count(self, **query):
        """
        Perform a search, returning count of results.

        :param dict[str,str|float|datacube.model.Range] query:
        :rtype: int
        """
        # This may be optimised into one query in the future.
        result = 0
        for product_type, count in self._do_count_by_product(query):
            result += count

        return result

    def count_by_product(self, **query):
        """
        Perform a search, returning a count of for each matching product type.

        :param dict[str,str|float|datacube.model.Range] query:
        :returns: Sequence of (product, count)
        :rtype: __generator[(datacube.model.DatasetType,  int)]]
        """
        return self._do_count_by_product(query)

    def count_by_product_through_time(self, period, **query):
        """


text:
# pyOCD debugger
# Copyright (c) 2019 Arm Limited
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re
from collections import namedtuple

from . import target_kinetis

FamilyInfo = namedtuple("FamilyInfo", "vendor matches klass")

## @brief Lookup table to convert from CMSIS-Pack family names to a family class.
#
# The vendor name must be an exact match to the 'Dvendor' attribute of the CMSIS-Pack family
# element.
#
# At least one of the regexes must match the entirety of either the CMSIS-Pack 'Dfamily' or
# 'DsubFamily' (if present) attributes.
FAMILIES = [
    FamilyInfo("NXP",   [re.compile(r'MK[LEVWS]?.*')],              target_kinetis.Kinetis),
    ]

domain: python


text:
from django.test import TestCase
from django.urls import reverse

from rest_framework import status
from rest_framework.test import APIClient

from dailymed.models import Set, Spl, InactiveIngredient

from dailymed.serializers import SplSerializer

import json
from pathlib import Path

SPL_URL = reverse('spl-list')
PRODUCT_URL = reverse('product-list')
PACKAGE_URL = reverse('package-list')

class PublicApiTest(TestCase):
    """Test public daily med API"""

    def setUp(self):
        self.client = APIClient()

        """Creates sample data for database"""
        cwd = Path(__file__).parent.absolute()
        with open(f'{cwd}/test.json', 'r') as f:
            default = json.load(f)

        for data in default['results']:
            set_id = data.pop('set_id')
            products_data = data.pop('products')

            set_obj = Set.objects.create(id=set_id)

            spl_obj = set_obj.spls.create(**data)

            for product_data in products_data:
                product_data.pop('name')
                packages_data = product_data.pop('packages')
                if 'inactive_ingredients' in product_data:
                    inactive_ingredients_data = product_data\
                        .pop('inactive_ingredients')

                    inactive_ingredients_list = []
                    for inactive_ingredient_data in inactive_ingredients_data:
                        try:
                            ingredient = InactiveIngredient.objects.get(
                                **inactive_ingredient_data
                            )
                            inactive_ingredients_list.append(ingredient)
                        except Exception:
                            ingredient = InactiveIngredient.objects.create(
                                **inactive_ingredient_data
                            )
                            inactive_ingredients_list.append(ingredient)

                product_obj = spl_obj.products.create(**product_data)
                product_obj.inactive_ingredients\
                    .add(*inactive_ingredients_list)

                for package_data in packages_data:
                    product_obj.packages.create(**package_data)

    def test_retrieve_spls(self):
        """Test retrieving spls"""
        res = self.client.get(
            SPL_URL,
            format='json'
            )

        serializer = SplSerializer(Spl.objects.filter(), many=True)

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(serializer.data, res.data['results'])

    def test_retrieve_spls_filter_by_set(self):
        """Test retrieving a spl by set filter"""
        set_id = Set.objects.first()
        res = self.client.get(
            SPL_URL,
            {'set_id': set_id.id},
            format='json')

        serializer = SplSerializer(
            Spl.objects.filter(set__id=set_id.id), many=True
            )

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(serializer.data, res.data['results'])

    def test_retrieve_spls_filter_by_inactive_ing(self):
        """Test retrieving a spl by inactive ingredient filter"""
        inactive_ing = 'alcohol'
        res = self.client.get(
            SPL_URL,
            {'inactive_ingredient_name': inactive_ing},
            format='json')

        serializer = SplSerializer(
            Spl.objects.filter(
                products__inactive_ingredients__name__icontains=inactive_ing)
            .distinct(),
            many=True
        )

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(serializer.data, res.data['results'])

    def test_retrieve_spls_filter_by_schedule(self):
        """Test retrieving spls by schedule filter"""
        schedule = 'CIV'
        res = self.client.get(
            SPL_URL,
            {'schedule': schedule},
            format='json')

        serializer = SplSerializer(Spl.objects.filter(
            products__schedule=schedule).distinct(),
            many=True
        )

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(serializer.data, res.data['results'])

    def test_retrieve_spls_filter_by_drug_name(self):
        """Test retrieving spls by drug name filter"""
        name = 'Ciprofloxacin'
        res = self.client.get(
            SPL_URL,
            {'product_name': name},
            format='json')

        serializer = SplSerializer(Spl.objects.filter(
            products__name=name).distinct(),
            many=True
        )

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(serializer.data, res.data['results'])

    def test_retrieve_spls_filter_by_complex(self):
        """Test retrieving spls filtered by set & inactive ingredient"""
        set_id = 'b88efb93-f1d1-4606-a669-6896f432a27f'
        inactive_ing = 'alcohol'
        res = self.client.get(
            SPL_URL,
            {'set_id': set_id,
             'inactive_ingredient_name': inactive_ing},
            format='json'
        )

        serializer = SplSerializer(
            Spl.objects.filter(
                products__inactive_ingredients__name__icontains=inactive_ing,
                set__id=set_id)
            .distinct(),
            many=True
        )

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(len(res.data['results']), 1)
        self.assertEqual(serializer.data, res.data['results'])


text:
import urllib2
from zope.interface import implements

from plone.portlets.interfaces import IPortletDataProvider
from plone.app.portlets.portlets import base
from Products.CMFCore.utils import getToolByName

from zope import schema
from zope.formlib import form

from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile

from wad.blog.utils import find_portlet_assignment_context
from wad.blog.blogentry import IBlogEntry
from wad.blog import MessageFactory as _

class IBlogCategoriesPortlet(IPortletDataProvider):
    """A portlet

    It inherits from IPortletDataProvider because for this portlet, the
    data that is being rendered and the portlet assignment itself are the
    same.
    """
    archive_view = schema.TextLine(
        title=_(u"Archive view"),
        description=_(u"The name of the archive view"),
        default=u'blog-view',
        required=True
    )

class Assignment(base.Assignment):
    """Portlet assignment.

    This is what is actually managed through the portlets UI and associated
    with columns.
    """

    implements(IBlogCategoriesPortlet)

    def __init__(self, archive_view=u'blog-view'):
        self.archive_view = archive_view

    @property
    def title(self):
        """This property is used to give the title of the portlet in the
        "manage portlets" screen.
        """
        return _("Categories")

class Renderer(base.Renderer):
    """Portlet renderer.

    This is registered in configure.zcml. The referenced page template is
    rendered, and the implicit variable 'view' will refer to an instance
    of this class. Other methods can be added and referenced in the template.
    """

    render = ViewPageTemplateFile('categories.pt')

    def keywords(self):
        catalog = getToolByName(self.context, 'portal_catalog')
        keywords = catalog.uniqueValuesFor('Subject')
        keywords = [unicode(k, 'utf-8') for k in keywords]
        return keywords

    def archive_url(self, subject):
        # Get the path of where the portlet is created. That's the blog.
        assignment_context = find_portlet_assignment_context(self.data,
                                                             self.context)
        if assignment_context is None:
            assignment_context = self.context
        self.folder_url = assignment_context.absolute_url()
        sub = urllib2.quote(subject.encode('utf-8'))
        url = '%s/%s?category=%s' % (self.folder_url,
                                     self.data.archive_view,
                                     sub)
        return url

    def blog_url(self):
        assignment_context = find_portlet_assignment_context(self.data,
                                                             self.context)
        if assignment_context is None:
            assignment_context = self.context
        return assignment_context.absolute_url()

    def count_entries(self, subject):
        catalog = getToolByName(self.context, 'portal_catalog')
        brains = catalog(object_provides=IBlogEntry.__identifier__,
                         Subject=subject.encode('utf-8'))
        return len(brains)

    def count_all_entries(self):
        catalog = getToolByName(self.context, 'portal_catalog')
        brains = catalog(object_provides=IBlogEntry.__identifier__)
        return len(brains)

class AddForm(base.AddForm):
    """Portlet add form.

    This is registered in configure.zcml. The form_fields variable tells
    zope.formlib which fields to display. The create() method actually
    constructs the assignment that is being added.
    """
    form_fields = form.Fields(IBlogCategoriesPortlet)

    def create(self, data):
        return Assignment(**data)

class EditForm(base.EditForm):
    """Portlet edit form.

    This is registered with configure.zcml. The form_fields variable tells
    zope.formlib which fields to display.
    """
    form_fields = form.Fields(IBlogCategoriesPortlet)


text:
from django.contrib.auth.decorators import permission_required
from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from catalog import models as cmod
from django_mako_plus import view_function, jscontext
import requests
import json
# @permission_required('manager')
@view_function
def process_request (request):
    category_name = request.GET.get('category')
    product_name = request.GET.get('name')
    max_price = request.GET.get('max_price')
    page = request.GET.get('page')
    if page is not None:
        pnum = int(page)
    else:
        pnum = 1
    products = []
    qry = cmod.Product.objects.all()
    if product_name:
        qry = qry.filter(name__icontains=product_name)
    if max_price:
        qry = qry.filter(price__lte=max_price)
    if category_name:
        qry = qry.filter(category__name__icontains=category_name)
    qry = qry.order_by('category','name')
    for p in qry:
        item = {
                'category': p.category.name,
                'name': p.name,
                'price': p.price,
                }
        products.append(item)
    products = products[(pnum - 1)*6:pnum*6]
    return JsonResponse(products, safe=False)


text:
# -*- coding: utf-8 -*-
#Chucky_Bot

import LINETCR
from LINETCR.lib.curve.ttypes import *
from datetime import datetime
from bs4 import BeautifulSoup
from threading import Thread
from googletrans import Translator
from gtts import gTTS
import time,random,sys,json,codecs,threading,glob,urllib,urllib2,urllib3,re,ast,os,subprocess,requests,tempfile

cl = LINETCR.LINE()
#cl.login(qr=True)
cl.login(token='EsOvPPzeFykCVG8OoGf0.hE4TS1Hheb46PcdMzZKaaa.rzBOrFqSAApZownyv2qBJWU3PWWbf9/oE6G+sSVzUTo=')
cl.loginResult()
print "Azmi 1-Login Success\n"

ki = LINETCR.LINE()
#ki.login(qr=True)
ki.login(token='EsTdk3fyUSbT7LJVwoEd.rLylacrPH39WJb0UIwB8Nq.GYYzsgzj7aHd7mzCSluc3162Uqrry6Jjwf/bFuq9Etw=')
ki.loginResult()
print "Ki-Login Success\n"

kk = LINETCR.LINE()
#kk.login(qr=True)
kk.login(token='EsNKJDaP0J7Pt7syTOW9.GgPTp3/FisKkVX1rJHeroq.hUG0VDbWHz8R7o80xI0Pvme8dBb3dSsmCnat0PRX+JM=')
kk.loginResult()
print "Kk-Login Success\n"

#kc = LINETCR.LINE()
#kc.login(qr=True)
#kc.login(token='TOKEN_KAMU_DISINI_BEIB')
#kc.loginResult()
#print "Kc-Login Success\n"

#kr = LINETCR.LINE()
#kr.login(qr=True)
#kr.login(token='TOKEN_KAMU_DISINI_BEIB')
#kr.loginResult()
#print "Kr-Login Success\n"

#km = LINETCR.LINE()
#km.login(qr=True)
#km.login(token='TOKEN_KAMU_DISINI_BEIB')
#km.loginResult()
print "Km-Login Success\n\n=====[Sukses All Login]====="

reload(sys)
sys.setdefaultencoding('utf-8')

selfMessage ="""
╔═════════════════════════
║            ☆☞ S E L F ☜☆
╠═════════════════════════
╠➩〘Hi〙
╠➩〘Me〙
╠➩〘Mymid〙
╠➩〘Mid @〙
╠➩〘SearchID: (ID LINE)〙
╠➩〘Checkdate (DD/MM/YY)〙
╠➩〘Kalender〙
╠➩〘Steal contact〙
╠➩〘Pp @〙
╠➩〘Cover @〙
╠➩〘Auto like〙
╠➩〘Scbc Text〙
╠➩〘Cbc Text〙
╠➩〘Gbc Text〙
╠➩〘Getbio @〙
╠➩〘Getinfo @〙
╠➩〘Getname @〙
╠➩〘Getprofile @〙
╠➩〘Getcontact @〙
╠➩〘Getvid @〙
╠➩〘Friendlist〙
╠═════════════════════════
║             ☆☞ S E L F ☜☆
╚═════════════════════════
"""

botMessage ="""
╔═════════════════════════
║             ☆☞ B O T ☜☆
╠═════════════════════════
╠➩〘Absen〙
╠➩〘Respon〙
╠➩〘Runtime〙
╠➩〘Kapten copy @〙
╠➩〘TC1 copy @〙
╠➩〘TC2 copy @〙
╠➩〘TC3 copy @〙
╠➩〘TC4 copy @〙
╠➩〘Backup all〙
╠➩〘/bio Text〙
╠➩〘@bye (Usir Kapten)〙
╠➩〘Bye all (Usir Semua)〙
╠═════════════════════════
║            ☆☞ B O T ☜☆
╚═════════════════════════
"""

mediaMessage ="""
╔═════════════════════════
║           ☆☞ M E D I A ☜☆
╠═════════════════════════
╠➩〘Gift〙
╠➩〘Gift1 @ s/d Gift10 @〙
╠➩〘Giftbycontact〙
╠➩〘All gift〙
╠➩〘Gif gore〙
╠➩〘Google: (Text)〙
╠➩〘Playstore NamaApp〙
╠➩〘Fancytext: Text〙
╠➩〘/musik Judul-Penyanyi〙
╠➩〘/lirik Judul-Penyanyi〙
╠➩〘/musrik Judul-Penyanyi〙
╠➩〘/ig UrsnameInstagram〙
╠➩〘Checkig UrsnameInstagram〙
╠➩〘/apakah Text (Kerang Ajaib)〙
╠➩〘/kapan Text (Kerang Ajaib)〙
╠➩〘/hari Text (Kerang Ajaib)〙
╠➩〘/berapa Text (Kerang Ajaib)〙
╠➩〘/berapakah Text〙
╠➩〘Youtubelink: Judul Video〙
╠➩〘Youtubevideo: Judul Video〙
╠➩〘Youtubesearch: Judul Video〙
╠➩〘Image NamaGambar〙
╠➩〘Say-id Text〙
╠➩〘Say-en Text〙
╠➩〘Say-jp Text〙
╠➩〘Image NamaGambar〙
╠➩〘Tr-id Text (Translate En Ke ID〙
╠➩〘Tr-en Text (Translate ID Ke En〙
╠➩〘Tr-th Text (Translate ID Ke Th〙
╠➩〘Id@en Text (Translate ID Ke En〙
╠➩〘Id@th Text (Translate ID Ke TH〙
╠➩〘En@id Text (Translate En Ke ID〙
╠═════════════════════════
║            ☆☞ M E D I A ☜☆
╚═════════════════════════
"""

groupMessage ="""
╔═════════════════════════
║           ☆☞ G R O U P ☜☆
╠═════════════════════════
╠➩〘Welcome〙
╠➩〘Say welcome〙
╠➩〘Invite creator〙
╠➩〘Setview〙
╠➩〘Viewseen〙
╠➩〘Gn: (NamaGroup)〙
╠➩〘Tag all〙
╠➩〘Recover〙
╠➩〘Cancel〙
╠➩〘Cancelall〙
╠➩〘Gcreator〙
╠➩〘Ginfo〙
╠➩〘Gurl〙
╠➩〘List group〙
╠➩〘Pict group: (NamaGroup)〙
╠➩〘Spam: (Text)〙
╠➩〘Spam〙
╠➩〘Add all〙
╠➩〘Kick: (Mid)〙
╠➩〘Invite: (Mid)〙
╠➩〘Invite〙
╠➩〘Memlist〙
╠➩〘Getgroup image〙
╠➩〘Urlgroup Image〙
╠═════════════════════════
║             ☆☞ G R O U P ☜☆
╚═════════════════════════
"""
tjia="u71b6799e1c37868a871d442e67633182"

setMessage ="""
╔═════════════════════════
║              ☆☞ S E T ☜☆
╠═════════════════════════
╠➩〘Sambutan on/off〙
╠➩〘Url on/off〙
╠➩〘Alwaysread on/off〙
╠➩〘Sider on/off〙
╠➩〘Contact on/off〙
╠➩〘Simisimi on/off〙
╠═════════════════════════
║             ☆☞ S E T ☜☆
╚═════════════════════════
"""

creatorMessage ="""
╔═════════════════════════
║         ☆☞ C R E A T O R ☜☆
╠═════════════════════════
╠➩〘Admin add @〙
╠➩〘Admin remove @〙
╠➩〘/cnkapten〙
╠➩〘/cntc1〙
╠➩〘/cntc2〙
╠➩〘/cntc3〙
╠➩〘/cntc4〙
╠➩〘Crash〙
╠➩〘Kickall〙
╠➩〘Bc: (Text)〙
╠➩〘Nk: @〙
╠➩〘Ulti @〙
╠➩〘Join group: (NamaGroup〙
╠➩〘Leave group: (NamaGroup〙
╠➩〘Leave all group〙
╠➩〘Bot restart〙
╠➩〘Turn off〙
╠═════════════════════════
║          ☆☞ C R E A T O R ☜☆
╚═════════════════════════
"""

adminMessage ="""
╔═════════════════════════
║            ☆☞ A D M I N ☜☆
╠═════════════════════════
╠➩〘Admin list〙
╠➩〘Ban〙
╠➩〘Unban〙
╠➩〘Ban @〙
╠➩〘Unban @〙
╠➩〘Ban list〙
╠➩〘Clear ban〙
╠➩〘Kill〙
╠➩〘Kick @〙
╠➩〘Set member: (Jumblah)〙
╠➩〘Ban group: (NamaGroup〙
╠➩〘Del ban: (NamaGroup〙
╠➩〘List ban〙
╠➩〘Kill ban〙
╠➩〘Glist〙
╠➩〘Glistmid〙
╠➩〘Details group: (Gid)〙
╠➩〘Cancel invite: (Gid)〙
╠➩〘Invitemeto: (Gid)〙
╠➩〘Kapten acc invite〙
╠➩〘TC1 acc invite〙
╠➩〘TC2 acc invite〙
╠➩〘TC3 acc invite〙
╠➩〘TC4 acc invite〙
╠➩〘Removechat〙
╠➩〘Join on/off〙
╠➩〘Joincancel on/off〙
╠➩〘Respon on/off〙
╠➩〘Responkick on/off〙
╠➩〘Leave on/off〙
╠➩〘All join / (TC1/2/3/4 Join)〙
╠═════════════════════════
║           ☆☞ A D M I N ☜☆
╚═════════════════════════
"""

helpMessage ="""
╔═════════════════════════
║              ☆☞ H E L P ☜☆
╠═════════════════════════
╠➩〘Help protect〙
╠➩〘Help self〙
╠➩〘Help bot〙
╠➩〘Help group〙
╠➩〘Help set〙
╠➩〘Help media〙
╠➩〘Help admin〙
╠➩〘Help creator〙
╠➩〘Owner〙
╠➩〘Pap owner〙
╠➩〘Admin〙
╠➩〘Speed〙
╠➩〘Speed test〙
╠➩〘Status〙
╠═════════════════════════
║             ☆☞ H E L P ☜☆
╚═════════════════════════
"""

protectMessage ="""
╔═════════════════════════
║           ☆☞ P R O T E C T ☜☆
╠═════════════════════════
╠➩〘Allprotect on/off〙
╠➩〘Autocancel on/off〙
╠➩〘Qr on/off〙
╠➩〘Autokick on/off〙
╠➩〘Ghost on/off〙
╠➩〘Invitepro on/off〙
╠═════════════════════════
║           ☆☞ P R O T E C T ☜☆
╚═════════════════════════
"""

KAC=[cl,ki,kk]
mid = cl.getProfile().mid
Amid = ki.getProfile().mid
Bmid = kk.getProfile().mid
Bots=[mid,Amid,Bmid]
Creator=["u71b6799e1c37868a871d442e67633182"]
admin=["u71b6799e1c37868a871d442e67633182"]

contact = cl.getProfile()
backup1 = cl.getProfile()
backup1.displayName = contact.displayName
backup1.statusMessage = contact.statusMessage
backup1.pictureStatus = contact.pictureStatus

contact = ki.getProfile()
backup2 = ki.getProfile()
backup2.displayName = contact.displayName
backup2.statusMessage = contact.statusMessage
backup2.pictureStatus = contact.pictureStatus

contact = kk.getProfile()
backup3 = kk.getProfile()
backup3.displayName = contact.displayName
backup3.statusMessage = contact.statusMessage
backup3.pictureStatus = contact.pictureStatus

responsename = cl.getProfile().displayName
responsename2 = ki.getProfile().displayName
responsename3 = kk.getProfile().displayName

wait = {
    "LeaveRoom":True,
    "AutoJoin":False,
    "AutoJoinCancel":True,
    "memberscancel":0,
    "Members":1,
    "AutoCancel":{},
    "AutoCancelon":False,
    "joinkick":False,
    "AutoKick":{},
    "AutoKickon":False,
    'pap':{},
    'invite':{},
    'steal':{},
    'gift':{},
    'likeOn':{},
    'Leave':{},
    'detectMention':True,
    'kickMention':False,
    'timeline':True,
    "Timeline":True,
    "comment1":"Kenapa Kak?",
    "comment2":"Wkwkwk ＼（○＾ω＾○）／",
    "comment3":"Lucu Banget!!! ヘ(^_^)ヘ",
    "comment4":"Nice Kak (^_^)",
    "comment5":"Bot Auto Like ©By : Azmi\nContact Me : 👉 line.me/ti/p/~a_ulul15",
    "commentOn":True,
    "commentBlack":{},
    "message":"Thx For Add Me (^_^)\nInvite Me To Your Group ヘ(^_^)ヘ",
    "blacklist":{},
    "wblacklist":False,
    "dblacklist":False,
    "Qr":{},
    "Qron":False,
    "Contact":False,
    "Sambutan":True,
    "Ghost":False,
    "inviteprotect":False,
    "alwaysRead":False,
    "Sider":{},
    "Simi":{},
    "lang":"JP",
    "BlGroup":{}
}

settings = {
    "simiSimi":{}
    }

cctv = {
    "cyduk":{},
    "point":{},
    "sidermem":{}
}

wait2 = {
    "readPoint":{},
    "readMember":{},
    "setTime":{},
    "ROM":{}
    }

setTime = {}
setTime = wait2['setTime']
mulai = time.time()

def download_page(url):
    version = (3,0)
    cur_version = sys.version_info
    if cur_version >= version:
        import urllib,request
        try:
            headers = {}
            headers['User-Agent'] = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
            req = urllib,request.Request(url, headers = headers)
            resp = urllib,request.urlopen(req)
            respData = str(resp.read())
            return respData
        except Exception as e:
            print(str(e))
    else:
        import urllib2
        try:
            headers = {}
            headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"
            req = urllib2.Request(url, headers = headers)
            response = urllib2.urlopen(req)
            page = response.read()
            return page
        except:
            return"Page Not found"

def _images_get_next_item(s):
    start_line = s.find('rg_di')
    if start_line == -1:
        end_quote = 0
        link = "no_links"
        return link, end_quote
    else:
        start_line = s.find('"class="rg_meta"')
        start_content = s.find('"ou"',start_line+90)
        end_content = s.find(',"ow"',start_content-90)
        content_raw = str(s[start_content+6:end_content-1])
        return content_raw, end_content

def _images_get_all_items(page):
    items = []
    while True:
        item, end_content = _images_get_next_item(page)
        if item == "no_links":
            break
        else:
            items.append(item)
            time.sleep(0.1)
            page = page[end_content:]
    return items

def waktu(secs):
    mins, secs = divmod(secs,60)
    hours, mins = divmod(mins,60)
    return '%02d Jam %02d Menit %02d Detik' % (hours, mins, secs)

def cms(string, commands):# /XXX, >XXX, ;XXX, ^XXX, %XXX, $XXX...
    tex = ["+","@","/",">",";","^","%","$","＾","サテラ:","サテラ:","サテラ：","サテラ："]
    for texX in tex:
        for command in commands:
            if string ==command:
                return True
    return False

def upload_tempimage(client):
     '''
         Upload a picture of a kitten. We don't ship one, so get creative!
     '''
     config = {
         'album': album,
         'name':  'bot auto upload',
         'title': 'bot auto upload',
         'description': 'bot auto upload'
     }

     print("Uploading image... ")
     image = client.upload_from_path(image_path, config=config, anon=False)
     print("Done")
     print()

     return image

def sendAudio(self, to_, path):
       M = Message()
       M.text = None
       M.to = to_
       M.contentMetadata = None
       M.contentPreview = None
       M.contentType = 3
       M_id = self._client.sendMessage(0,M).id
       files = {
             'file': open(path,  'rb'),
       }

def sendMessage(to, text, contentMetadata={}, contentType=0):
    mes = Message()
    mes.to, mes.from_ = to, profile.mid
    mes.text = text
    mes.contentType, mes.contentMetadata = contentType, contentMetadata
    if to not in messageReq:
        messageReq[to] = -1
    messageReq[to] += 1

def sendImage(self, to_, path):
      M = Message(to=to_, text=None, contentType = 1)
      M.contentMetadata = None
      M.contentPreview = None
      M2 = self._client.sendMessage(0,M)
      M_id = M2.id
      files = {
         'file': open(path, 'rb'),
      }
      params = {
         'name': 'media',
         'oid': M_id,
         'size': len(open(path, 'rb').read()),
         'type': 'image',
         'ver': '1.0',
      }
      data = {
         'params': json.dumps(params)
      }
      r = self.post_content('https://obs-sg.line-apps.com/talk/m/upload.nhn', data=data, files=files)
      if r.status_code != 201:
         raise Exception('Upload image failure.')
      return True

def sendImageWithURL(self, to_, url):
      path = '%s/pythonLine-%i.data' % (tempfile.gettempdir(), randint(0, 9))
      r = requests.get(url, stream=True)
      if r.status_code == 200:
         with open(path, 'w') as f:
            shutil.copyfileobj(r.raw, f)
      else:
         raise Exception('Download image failure.')
      try:
         self.sendImage(to_, path)
      except:
         try:
            self.sendImage(to_, path)
         except Exception as e:
            raise e

def sendAudio(self, to_, path):
        M = Message()
        M.text = None
        M.to = to_
        M.contentMetadata = None
        M.contentPreview = None
        M.contentType = 3
        M_id = self._client.sendMessage(0,M).id
        files = {
            'file': open(path, 'rb'),
        }
        params = {
            'name': 'media',
            'oid': M_id,
            'size': len(open(path, 'rb').read()),
            'type': 'audio',
            'ver': '1.0',
        }
        data = {
            'params': json.dumps(params)
        }
        r = self.post_content('https://os.line.naver.jp/talk/m/upload.nhn', data=data, files=files)
        if r.status_code != 201:
            raise Exception('Upload audio failure.')
        return True

def sendAudioWithURL(self, to_, url):
        path = self.downloadFileWithURL(url)
        try:
            self.sendAudio(to_, path)
        except Exception as e:
            raise Exception(e)

def sendAudioWithUrl(self, to_, url):
        path = '%s/pythonLine-%1.data' % (tempfile.gettempdir(), randint(0, 9))
        r = requests.get(url, stream=True, verify=False)
        if r.status_code == 200:
           with open(path, 'w') as f:
              shutil.copyfileobj(r.raw, f)
        else:
           raise Exception('Download audio failure.')
        try:
            self.sendAudio(to_, path)
        except Exception as e:
            raise e

def downloadFileWithURL(self, fileUrl):
        saveAs = '%s/pythonLine-%i.data' % (tempfile.gettempdir(), randint(0, 9))
        r = self.get_content(fileUrl)
        if r.status_code == 200:
            with open(saveAs, 'wb') as f:
                shutil.copyfileobj(r.raw, f)
            return saveAs
        else:
            raise Exception('Download file failure.')

def restart_program():
    python = sys.executable
    os.execl(python, python, * sys.argv)

def bot(op):
    try:

        if op.type == 0:
            return

        if op.type == 5:
           if wait["autoAdd"] == True:
              cl.findAndAddContactsByMid(op.param1)
              if(wait["message"]in[""," ","\n",None]):
                pass
              else:
                cl.sendText(op.param1,str(wait["message"]))

        if op.type == 55:
	    try:
	      group_id = op.param1
	      user_id=op.param2
	      subprocess.Popen('echo "'+ user_id+'|'+str(op.createdTime)+'" >> dataSeen/%s.txt' % group_id, shell=True, stdout=subprocess.PIPE, )
	    except Exception as e:
	      print e

        if op.type == 55:
                try:
                    if cctv['cyduk'][op.param1]==True:
                        if op.param1 in cctv['point']:
                            Name = cl.getContact(op.param2).displayName
                            Name = ki.getContact(op.param2).displayName
                            Name = kk.getContact(op.param2).displayName
                            Name = kc.getContact(op.param2).displayName
                            Name = kr.getContact(op.param2).displayName
                            if Name in cctv['sidermem'][op.param1]:
                                pass
                            else:
                                cctv['sidermem'][op.param1] += "\n• " + Name
                                if " " in Name:
                                    nick = Name.split(' ')
                                    if len(nick) == 2:
                                        random.choice(KAC).sendText(op.param1, "Haii " + "☞ " + nick[0] + " ☜" + "\nNgintip Aja Niih. . .\nChat Kek Idiih (-__-)   ")
                                    else:
                                        random.choice(KAC).sendText(op.param1, "Haii " + "☞ " + nick[1] + " ☜" + "\nBetah Banget Jadi Penonton. . .\nChat Napa (-__-)   ")
                                else:
                                    random.choice(KAC).sendText(op.param1, "Haii " + "☞ " + Name + " ☜" + "\nNgapain Kak Ngintip Aja???\nSini Gabung Chat...   ")
                        else:
                            pass
                    else:
                        pass
                except:
                    pass

        else:
            pass

        if op.type == 22:
            cl.leaveRoom(op.param1)

        if op.type == 21:
            cl.leaveRoom(op.param1)

        if op.type == 13:
	    print op.param3
            if op.param3 in mid:
		if op.param2 in Creator:
		    cl.acceptGroupInvitation(op.param1)
            if op.param3 in Amid:
		if op.param2 in Creator:
		    ki.acceptGroupInvitation(op.param1)
            if op.param3 in Bmid:
		if op.param2 in Creator:
		    kk.acceptGroupInvitation(op.param1)
            if op.param3 in Cmid:
		if op.param2 in Creator:
		    kc.acceptGroupInvitation(op.param1)
            if op.param3 in Dmid:
		if op.param2 in Creator:
		    kr.acceptGroupInvitation(op.param1)

            if op.param3 in mid:
		if op.param2 in Amid:
		    cl.acceptGroupInvitation(op.param1)
            if op.param3 in mid:
		if op.param2 in Bmid:
		    cl.acceptGroupInvitation(op.param1)
            if op.param3 in mid:
		if op.param2 in Cmid:
		    cl.acceptGroupInvitation(op.param1)

            if op.param3 in Amid:
		if op.param2 in mid:
		    ki.acceptGroupInvitation(op.param1)
            if op.param3 in Amid:
		if op.param2 in Bmid:
		    ki.acceptGroupInvitation(op.param1)
            if op.param3 in Amid:
		if op.param2 in Cmid:
		    ki.acceptGroupInvitation(op.param1)

            if op.param3 in Bmid:
		if op.param2 in mid:
		    kk.acceptGroupInvitation(op.param1)
            if op.param3 in Bmid:
		if op.param2 in Amid:
		    kk.acceptGroupInvitation(op.param1)
            if op.param3 in Bmid:
		if op.param2 in Cmid:
		    kk.acceptGroupInvitation(op.param1)

            if op.param3 in Cmid:
		if op.param2 in mid:
		    kc.acceptGroupInvitation(op.param1)
            if op.param3 in Cmid:
		if op.param2 in Amid:
		    kc.acceptGroupInvitation(op.param1)
            if op.param3 in Cmid:
		if op.param2 in Cmid:
		    kc.acceptGroupInvitation(op.param1)

            if op.param3 in Dmid:
		if op.param2 in mid:
		    kr.acceptGroupInvitation(op.param1)
            if op.param3 in Dmid:
		if op.param2 in Amid:
		    kr.acceptGroupInvitation(op.param1)
            if op.param3 in Dmid:
		if op.param2 in Bmid:
		    kr.acceptGroupInvitation(op.param1)

	    if mid in op.param3:
                if wait["AutoJoinCancel"] == True:
		    G = cl.getGroup(op.param1)
                    if len(G.members) <= wait["memberscancel"]:
                        cl.acceptGroupInvitation(op.param1)
                        cl.sendText(op.param1,"Maaf " + cl.getContact(op.param2).displayName + "\nMember Kurang Dari 30 Orang\nUntuk Info, Silahkan Chat Owner Kami!")
                        c = Message(to=op.param1, from_=None, text=None, contentType=13)
                        c.contentMetadata={'mid':tjia}
                        cl.sendMessage(c)
                        cl.leaveGroup(op.param1)
		    else:
                        cl.acceptGroupInvitation(op.param1)
			G = cl.getGroup(op.param1)
			G.preventJoinByTicket = False
			cl.updateGroup(G)
			Ti = cl.reissueGroupTicket(op.param1)


text:
from abcunits import ConversionUnit

KFACTOR = 273.15 #Difference Kelvin, C (how precise is this known?)

class TempUnit(ConversionUnit):
   """ Temperature units.  ALl conversions go through Kelvin. """

#http://www.metric-conversions.org/temperature/fahrenheit-to-kelvin.htm
class Kelvin(TempUnit):
   short = 'K'
   full = 'Kelvin' #Proper names, keep this way?
   symbol = 'r$^{\deg}K$' #Isn't degree Kelvin technially wrong?
   _canonical = True

   @staticmethod
   def to_canonical(x):
      return x

   @staticmethod
   def from_canonical(x):
      return x

class Celsius(TempUnit):
   short = 'C'
   full = 'Celsius'
   symbol = 'r$^{\deg}C$'

   @staticmethod
   def to_canonical(x):
      return x + KFACTOR

   @staticmethod
   def from_canonical(x):
      return x - KFACTOR

class Farenheiht(TempUnit):
   short = 'F'
   full = 'Farenheiht'
   symbol = 'r$^{\deg}F$'

   @staticmethod
   def to_canonical(x):
      return ((x - 32.00)/1.80) + 273.15

   @staticmethod
   def from_canonical(x):
      return ((x - KFACTOR) * 1.80) + 32.00

_tempunits = (Kelvin(),
              Celsius(),
              Farenheiht(),
              ConversionUnit() #For null case
              )
TEMPUNITS = dict((obj.short, obj) for obj in _tempunits)


text:
from __future__ import absolute_import

def cleanup():
    from .models import MetadataType, MetadataSet

    MetadataType.objects.all().delete()
    MetadataSet.objects.all().delete()


text:
#!/usr/bin/env python2.7

# Advanced Multi-Mission Operations System (AMMOS) Instrument Toolkit (AIT)
# Bespoke Link to Instruments and Small Satellites (BLISS)
#
# Copyright 2016, by the California Institute of Technology. ALL RIGHTS
# RESERVED. United States Government Sponsorship acknowledged. Any
# commercial use must be negotiated with the Office of Technology Transfer
# at the California Institute of Technology.
#
# This software may be subject to U.S. export control laws. By accepting
# this software, the user agrees to comply with all applicable U.S. export
# laws and regulations. User has the responsibility to obtain export licenses,
# or other export authority as may be required before exporting such
# information to foreign countries or providing access to foreign persons.

import time
import datetime
import mock
import os
import os.path
import nose
import nose.tools

import ait.core
from ait.core import dmc

LEAPSECOND_DATA_RESPONSE = '''#
#    Updated through IERS Bulletin C55
#    File expires on:  28 December 2018
#
#@	3754944000
#
2272060800	10	# 1 Jan 1972
2287785600	11	# 1 Jul 1972
2303683200	12	# 1 Jan 1973
2335219200	13	# 1 Jan 1974
2366755200	14	# 1 Jan 1975
2398291200	15	# 1 Jan 1976
2429913600	16	# 1 Jan 1977
2461449600	17	# 1 Jan 1978
2492985600	18	# 1 Jan 1979
2524521600	19	# 1 Jan 1980
2571782400	20	# 1 Jul 1981
2603318400	21	# 1 Jul 1982
2634854400	22	# 1 Jul 1983
2698012800	23	# 1 Jul 1985
2776982400	24	# 1 Jan 1988
2840140800	25	# 1 Jan 1990
2871676800	26	# 1 Jan 1991
2918937600	27	# 1 Jul 1992
2950473600	28	# 1 Jul 1993
2982009600	29	# 1 Jul 1994
3029443200	30	# 1 Jan 1996
3076704000	31	# 1 Jul 1997
'''

class MockResponse:
    def __init__(self, text, status_code):
        self.text = text
        self.status_code = status_code

def test_getTimestampUTC():
    expected = time.strftime('%Y-%j', time.gmtime())

    actual = time.strftime('%Y-%j', time.gmtime(dmc.getTimestampUTC()[0]))

    assert actual == expected

def test_getUTCDatetimeDOY_w_days():
    days      = 1
    t         = datetime.datetime.utcnow() + datetime.timedelta(days=days)
    timestamp = t.timetuple()
    exp_year  = timestamp.tm_year
    exp_day   = '%03d' % timestamp.tm_yday

    dtime     = dmc.getUTCDatetimeDOY(days=days).split('T')[0].split('-')
    assert str(exp_year) == dtime[0]
    assert str(exp_day)  == dtime[1]

def test_leap_second_attrs():
    ait.config.leapseconds._config['filename'] = os.path.join(
        os.path.dirname(__file__), "testdata", "dmc", "leapseconds.dat"
    )

    ls = dmc.LeapSeconds
    ls._load_leap_second_data()
    assert ls.leapseconds == ls._data['leapseconds']
    assert ls.valid_date == ls._data['valid']
    assert ls.get_current_GPS_offset() == ls.leapseconds[-1][-1]

@nose.tools.raises(ValueError)
def test_leap_second_by_date_invalid_gps_date():
    ait.config.leapseconds._config['filename'] = os.path.join(
        os.path.dirname(__file__), "testdata", "dmc", "leapseconds.dat"
    )

    dmc.LeapSeconds._load_leap_second_data()
    dmc.LeapSeconds.get_GPS_offset_for_date(datetime.datetime(1980, 1, 1))

def test_leap_second_by_date():
    ait.config.leapseconds._config['filename'] = os.path.join(
        os.path.dirname(__file__), "testdata", "dmc", "leapseconds.dat"
    )

    ls = dmc.LeapSeconds
    ls._load_leap_second_data()
    assert ls.get_GPS_offset_for_date(datetime.datetime(1981, 1, 1)) == 0
    assert ls.get_GPS_offset_for_date(datetime.datetime(1981, 7, 1)) == 1
    assert ls.get_GPS_offset_for_date(datetime.datetime(1982, 7, 1)) == 2
    assert ls.get_GPS_offset_for_date(datetime.datetime(1983, 7, 1)) == 3
    assert ls.get_GPS_offset_for_date(datetime.datetime(1985, 7, 1)) == 4
    assert ls.get_GPS_offset_for_date(datetime.datetime(1988, 1, 1)) == 5
    assert ls.get_GPS_offset_for_date(datetime.datetime(1990, 1, 1)) == 6
    assert ls.get_GPS_offset_for_date(datetime.datetime(1991, 1, 1)) == 7
    assert ls.get_GPS_offset_for_date(datetime.datetime(1992, 7, 1)) == 8
    assert ls.get_GPS_offset_for_date(datetime.datetime(1993, 7, 1)) == 9
    assert ls.get_GPS_offset_for_date(datetime.datetime(1994, 7, 1)) == 10
    assert ls.get_GPS_offset_for_date(datetime.datetime(1996, 1, 1)) == 11
    assert ls.get_GPS_offset_for_date(datetime.datetime(1997, 7, 1)) == 12
    assert ls.get_GPS_offset_for_date(datetime.datetime(1999, 1, 1)) == 13
    assert ls.get_GPS_offset_for_date(datetime.datetime(2006, 1, 1)) == 14
    assert ls.get_GPS_offset_for_date(datetime.datetime(2009, 1, 1)) == 15
    assert ls.get_GPS_offset_for_date(datetime.datetime(2012, 7, 1)) == 16
    assert ls.get_GPS_offset_for_date(datetime.datetime(2015, 7, 1)) == 17
    assert ls.get_GPS_offset_for_date(datetime.datetime(2017, 1, 1)) == 18

    # Make sure not supplying a date returns the offset for the current date
    assert (ls.get_GPS_offset_for_date(datetime.datetime.utcnow()) ==
            ls.get_GPS_offset_for_date())

def test_leap_second_data_load():
    ait.config.leapseconds._config['filename'] = os.path.join(
        os.path.dirname(__file__), "testdata", "dmc", "leapseconds.dat"
    )

    assert type(dmc.LeapSeconds.leapseconds) == type([])
    assert dmc.LeapSeconds.leapseconds[0] == (datetime.datetime(1981, 7, 1), 1)
    assert type(dmc.LeapSeconds.valid_date) == type(datetime.datetime.now())

@nose.tools.raises(ValueError)
@mock.patch('requests.get', mock.MagicMock(return_value=MockResponse(LEAPSECOND_DATA_RESPONSE, 400)))
def test_failed_leapsecond_load_and_update():
    ait.config.leapseconds._config['filename'] = os.path.join(
        os.path.dirname(__file__), "invalidpath", "leapseconds.dat"
    )

    dmc.LeapSeconds._data = None
    dmc.LeapSeconds._load_leap_second_data()

@mock.patch('requests.get', mock.MagicMock(return_value=MockResponse(LEAPSECOND_DATA_RESPONSE, 200)))
def test_update_leap_second_data():
    ait.config.leapseconds._config['filename'] = os.path.join(
        os.path.dirname(__file__), "testdata", "dmc", "tmp_leapseconds.out"
    )

    dmc.LeapSeconds._data = None
    dmc.LeapSeconds._update_leap_second_data()

    assert type(dmc.LeapSeconds.leapseconds) == type([])
    assert dmc.LeapSeconds.leapseconds[0] == (datetime.datetime(1981, 7, 1), 1)
    assert type(dmc.LeapSeconds.valid_date) == type(datetime.datetime.now())

    assert os.path.isfile(ait.config.leapseconds.filename)
    os.remove(ait.config.leapseconds.filename)

@nose.tools.raises(ValueError)
@mock.patch('requests.get', mock.MagicMock(return_value=MockResponse(LEAPSECOND_DATA_RESPONSE, 400)))
def test_unable_to_pull_leapsecond_data():
    ait.config.leapseconds._config['filename'] = os.path.join(
        os.path.dirname(__file__), "testdata", "dmc", "tmp_leapseconds.out"
    )

    dmc.LeapSeconds._data = None
    dmc.LeapSeconds._update_leap_second_data()

if __name__ == '__main__':
    nose.main()


text:
# Copyright 2019 TerraPower, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import random
import shutil
import string

import armi
from armi import runLog
from armi.utils import pathTools

def _changeDirectory(destination):
    if os.path.exists(destination):
        os.chdir(destination)
    else:
        raise IOError(
            "Cannot change directory to non-existent location: {}".format(destination)
        )

class DirectoryChanger(object):
    """
    Utility to change directory.

    Parameters
    ----------
    destination : str
        Path of directory to change into
    filesToMove : list of str, optional
        Filenames to bring from the CWD into the destination
    filesToRetrieve : list of str, optional
        Filenames to bring back from the destination to the cwd
    dumpOnException : bool, optional
        Flag to tell system to retrieve the entire directory if an exception
        is raised within a the context manager.

    Use with 'with' statements to execute code in a different dir, guaranteeing a clean
    return to the original directory

    >>> with DirectoryChanger('C:\\whatever')
    ...     pass

    """

    def __init__(
        self, destination, filesToMove=None, filesToRetrieve=None, dumpOnException=True
    ):
        """Establish the new and return directories"""
        self.initial = pathTools.armiAbsPath(os.getcwd())
        self.destination = None
        if destination is not None:
            self.destination = pathTools.armiAbsPath(destination)
        self._filesToMove = filesToMove or []
        self._filesToRetrieve = filesToRetrieve or []
        self._dumpOnException = dumpOnException

    def __enter__(self):
        """At the inception of a with command, navigate to a new directory if one is supplied."""
        runLog.debug("Changing directory to {}".format(self.destination))
        self.moveFiles()
        self.open()
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        """At the termination of a with command, navigate back to the original directory."""
        runLog.debug("Returning to directory {}".format(self.initial))
        if exc_type is not None and self._dumpOnException:
            runLog.info(
                "An exception was raised within a DirectoryChanger. "
                "Retrieving entire folder for debugging."
            )
            self._retrieveEntireFolder()
        else:
            self.retrieveFiles()
        self.close()

    def __repr__(self):
        """Print the initial and destination paths"""
        return "<{} {} to {}>".format(
            self.__class__.__name__, self.initial, self.destination
        )

    def open(self):
        """
        User requested open, used to stalling the close from a with statement.

        This method has been made for old uses of :code:`os.chdir()` and is not
        recommended.  Please use the with statements
        """
        if self.destination:
            _changeDirectory(self.destination)

    def close(self):
        """User requested close."""
        if self.initial != os.getcwd():
            _changeDirectory(self.initial)

    def moveFiles(self):
        initialPath = self.initial
        destinationPath = self.destination
        self._transferFiles(initialPath, destinationPath, self._filesToMove)

    def retrieveFiles(self):
        """Retrieve any desired files."""
        initialPath = self.destination
        destinationPath = self.initial
        fileList = self._filesToRetrieve
        self._transferFiles(initialPath, destinationPath, fileList)

    def _retrieveEntireFolder(self):
        """Retrieve all files."""
        initialPath = self.destination
        destinationPath = self.initial
        folderName = os.path.split(self.destination)[1]
        destinationPath = os.path.join(destinationPath, f"dump-{folderName}")
        fileList = os.listdir(self.destination)
        self._transferFiles(initialPath, destinationPath, fileList)

    @staticmethod
    def _transferFiles(initialPath, destinationPath, fileList):
        """
        Transfer files into or out of the directory.

        .. warning:: On Windows the max number of characters in a path is 260.
            If you exceed this you will see FileNotFound errors here.

        """
        if not fileList:
            return
        if not os.path.exists(destinationPath):
            os.mkdir(destinationPath)
        for ff in fileList:
            if isinstance(ff, tuple):
                # allow renames in transit
                fromName, destName = ff
            else:
                fromName, destName = ff, ff

            fromPath = os.path.join(initialPath, fromName)
            toPath = os.path.join(destinationPath, destName)
            runLog.extra("Copying {} to {}".format(fromPath, toPath))
            shutil.copy(fromPath, toPath)

class TemporaryDirectoryChanger(DirectoryChanger):
    """
    Create temporary directory, changes into it, and if there is no error/exception
    generated when using a :code:`with` statement, it deletes the directory.

    Notes
    -----
    If there is an error/exception generated while in a :code:`with` statement, the
    temporary directory contents will be copied to the original directory and then the
    temporary directory will be deleted.
    """

    _home = armi.context.FAST_PATH

    def __init__(
        self, root=None, filesToMove=None, filesToRetrieve=None, dumpOnException=True
    ):
        DirectoryChanger.__init__(
            self, root, filesToMove, filesToRetrieve, dumpOnException
        )
        root = root or TemporaryDirectoryChanger._home
        if not os.path.exists(root):
            os.makedirs(root)
        self.initial = os.path.abspath(os.getcwd())
        self.destination = TemporaryDirectoryChanger.GetRandomDirectory(root)
        while os.path.exists(self.destination):
            self.destination = TemporaryDirectoryChanger.GetRandomDirectory(root)

    @classmethod
    def GetRandomDirectory(cls, root):
        return os.path.join(
            root,
            "temp-"
            + "".join(
                random.choice(string.ascii_letters + string.digits) for _ in range(10)
            ),
        )

    def __enter__(self):
        os.mkdir(self.destination)
        return DirectoryChanger.__enter__(self)

    def __exit__(self, exc_type, exc_value, traceback):
        DirectoryChanger.__exit__(self, exc_type, exc_value, traceback)
        shutil.rmtree(self.destination)

class ForcedCreationDirectoryChanger(DirectoryChanger):
    """
    Creates the directory tree necessary to reach your desired destination

    Attributes
    ----------
    clean : bool
        if True and the directory exists, clear all contents on entry.
    """

    def __init__(
        self,
        destination,
        filesToMove=None,
        filesToRetrieve=None,
        dumpOnException=True,
        clean=False,
    ):
        DirectoryChanger.__init__(
            self, destination, filesToMove, filesToRetrieve, dumpOnException
        )
        self.clean = clean

    def __enter__(self):
        if not os.path.exists(self.destination):
            runLog.debug(f"Creating destination folder {self.destination}")
            try:
                os.makedirs(self.destination)
            except OSError:
                # even though we checked exists, this still fails
                # sometimes when multiple MPI nodes try
                # to make the dirs due to I/O delays
                runLog.debug(f"Failed to make destination folder")
        else:
            runLog.debug(f"Destination folder already exists: {self.destination}")
        DirectoryChanger.__enter__(self)
        if self.clean:
            shutil.rmtree(".", ignore_errors=True)
        return self

def directoryChangerFactory():
    if armi.MPI_SIZE > 1:
        from .directoryChangersMpi import MpiDirectoryChanger

        return MpiDirectoryChanger
    else:
        return DirectoryChanger


text:
import six
import pytest

from pymemcache.test.utils import MockMemcacheClient

@pytest.mark.unit()
def test_get_set():
    client = MockMemcacheClient()
    assert client.get(b"hello") is None

    client.set(b"hello", 12)
    assert client.get(b"hello") == 12

@pytest.mark.unit()
def test_get_set_unicide_key():
    client = MockMemcacheClient()
    assert client.get(u"hello") is None

    client.set(b"hello", 12)
    assert client.get(u"hello") == 12

@pytest.mark.unit()
def test_get_set_non_ascii_value():
    client = MockMemcacheClient()
    assert client.get(b"hello") is None

    # This is the value of msgpack.packb('non_ascii')
    non_ascii_str = b'\xa9non_ascii'
    client.set(b"hello", non_ascii_str)
    assert client.get(b"hello") == non_ascii_str

@pytest.mark.unit()
def test_get_many_set_many():
    client = MockMemcacheClient()
    client.set(b"h", 1)

    result = client.get_many([b"h", b"e", b"l", b"o"])
    assert result == {b"h": 1}

    # Convert keys into bytes
    d = dict((k.encode('ascii'), v)
             for k, v in six.iteritems(dict(h=1, e=2, z=3)))
    client.set_many(d)
    assert client.get_many([b"h", b"e", b"z", b"o"]) == d

@pytest.mark.unit()
def test_get_many_set_many_non_ascii_values():
    client = MockMemcacheClient()

    # These are the values of calling msgpack.packb() on '1', '2', and '3'
    non_ascii_1 = b'\xa11'
    non_ascii_2 = b'\xa12'
    non_ascii_3 = b'\xa13'
    client.set(b"h", non_ascii_1)

    result = client.get_many([b"h", b"e", b"l", b"o"])
    assert result == {b"h": non_ascii_1}

    # Convert keys into bytes
    d = dict((k.encode('ascii'), v)
             for k, v in six.iteritems(
                dict(h=non_ascii_1, e=non_ascii_2, z=non_ascii_3)
             ))
    client.set_many(d)
    assert client.get_many([b"h", b"e", b"z", b"o"]) == d

@pytest.mark.unit()
def test_add():
    client = MockMemcacheClient()

    client.add(b"k", 2)
    assert client.get(b"k") == 2

    client.add(b"k", 25)
    assert client.get(b"k") == 2

@pytest.mark.unit()
def test_delete():
    client = MockMemcacheClient()

    client.add(b"k", 2)
    assert client.get(b"k") == 2

    client.delete(b"k")
    assert client.get(b"k") is None

@pytest.mark.unit()
def test_incr_decr():
    client = MockMemcacheClient()

    client.add(b"k", 2)

    client.incr(b"k", 4)
    assert client.get(b"k") == 6

    client.decr(b"k", 2)
    assert client.get(b"k") == 4

@pytest.mark.unit()
def test_prepand_append():
    client = MockMemcacheClient()

    client.set(b"k", '1')
    client.append(b"k", 'a')
    client.prepend(b"k", 'p')
    assert client.get(b"k") == b'p1a'


text:
# -*- coding: utf-8 -*-

###############################################################################
#
# RetrieveCoupon
# Retrieves a coupon with specified coupon id.
#
# Python versions 2.6, 2.7, 3.x
#
# Copyright 2014, Temboo Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
#
#
###############################################################################

from temboo.core.choreography import Choreography
from temboo.core.choreography import InputSet
from temboo.core.choreography import ResultSet
from temboo.core.choreography import ChoreographyExecution

import json

class RetrieveCoupon(Choreography):

    def __init__(self, temboo_session):
        """
        Create a new instance of the RetrieveCoupon Choreo. A TembooSession object, containing a valid
        set of Temboo credentials, must be supplied.
        """
        super(RetrieveCoupon, self).__init__(temboo_session, '/Library/Stripe/Coupons/RetrieveCoupon')

    def new_input_set(self):
        return RetrieveCouponInputSet()

    def _make_result_set(self, result, path):
        return RetrieveCouponResultSet(result, path)

    def _make_execution(self, session, exec_id, path):
        return RetrieveCouponChoreographyExecution(session, exec_id, path)

class RetrieveCouponInputSet(InputSet):
    """
    An InputSet with methods appropriate for specifying the inputs to the RetrieveCoupon
    Choreo. The InputSet object is used to specify input parameters when executing this Choreo.
    """
    def set_APIKey(self, value):
        """
        Set the value of the APIKey input for this Choreo. ((required, string) The API Key provided by Stripe)
        """
        super(RetrieveCouponInputSet, self)._set_input('APIKey', value)
    def set_CouponID(self, value):
        """
        Set the value of the CouponID input for this Choreo. ((required, string) The unique identifier of the coupon you want to retrieve)
        """
        super(RetrieveCouponInputSet, self)._set_input('CouponID', value)

class RetrieveCouponResultSet(ResultSet):
    """
    A ResultSet with methods tailored to the values returned by the RetrieveCoupon Choreo.
    The ResultSet object is used to retrieve the results of a Choreo execution.
    """

    def getJSONFromString(self, str):
        return json.loads(str)

    def get_Response(self):
        """
        Retrieve the value for the "Response" output from this Choreo execution. ((json) The response from Stripe)
        """
        return self._output.get('Response', None)

class RetrieveCouponChoreographyExecution(ChoreographyExecution):

    def _make_result_set(self, response, path):
        return RetrieveCouponResultSet(response, path)


text:
# -----------------------------------------------------------------------------
# Task 002
print('''
    Задача 002:
    ===========
Каждый следующий элемент ряда Фибоначчи получается при сложении двух
предыдущих. Начиная с 1 и 2, первые 10 элементов будут:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
\x1b[33m
Найдите сумму всех четных элементов ряда Фибоначчи,
которые не превышают четыре миллиона.
\x1b[0m
''')

# Способ 1. Механический. Перебор. пока Единственный

def fib_sum(limit):
    """
    Считает сумму положительных чисел Фибаначи, которые меньше указанного значения
    """
    u1 = 1
    u2 = 2
    summ = 2
    _u = u1 + u2
    while _u < limit:
        if _u % 2 == 0:
            summ += _u
        u1 = u2
        u2 = _u
        _u = u1 + u2
    return summ

x = 4000000

result = fib_sum(x)
print("{:,}".format(result))

# сгенерированный ряд
# >>> [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987,
#     1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025,
#     121393, 196418, 317811, 514229, 832040, 1346269,
#     2178309, 3524578]


text:
"""
Expose top-level symbols that are safe for import *
"""
from __future__ import print_function, division, absolute_import
import re
from . import testing, decorators
from ._version import get_versions
from . import special, types, config

# Re-export typeof
from .special import *
from .pycc.decorators import export, exportmany

# Version
__version__ = get_versions()['version']

# Re-export all type names
from .types import *

# Re export decorators
jit = decorators.jit
autojit = decorators.autojit
njit = decorators.njit

# Re export vectorize decorators
from .npyufunc import vectorize, guvectorize

# Re export from_dtype
from .numpy_support import from_dtype

# Re-export test entrypoint
test = testing.test

# Try initialize cuda
from . import cuda

__all__ = """
jit
autojit
njit
vectorize
guvectorize
export
exportmany
cuda
from_dtype
""".split() + types.__all__ + special.__all__

def _sentry_llvm_version():
    """
    Make sure we meet min llvmpy version
    """
    import warnings
    import llvm
    min_version = (0, 12, 6)

    # Only look at the the major, minor and bugfix version numbers.
    # Ignore other stuffs
    regex = re.compile(r'(\d+)\.(\d+).(\d+)')
    m = regex.match(llvm.__version__)
    if m:
        ver = tuple(map(int, m.groups()))
        if ver < min_version:
            msg = ("Numba requires at least version %d.%d.%d of llvmpy.\n"
                   "Installed version is %s.\n"
                   "Please update llvmpy." %
                   (min_version + (llvm.__version__,)))
            raise ImportError(msg)
    else:
        # Not matching?
        warnings.warn("llvmpy version format not recognized!")

_sentry_llvm_version()


text:
import asyncio
import os
from pstats import Stats
from tempfile import NamedTemporaryFile

from aiomisc.service.profiler import Profiler

async def test_profiler_start_stop():
    profiler = Profiler(interval=0.1, top_results=10)
    try:
        await profiler.start()
        await asyncio.sleep(0.5)
    finally:
        await profiler.stop()

async def test_profiler_dump():
    profiler = None
    fl = NamedTemporaryFile(delete=False)
    path = NamedTemporaryFile(delete=False).name
    fl.close()
    try:
        profiler = Profiler(
            interval=0.1, top_results=10,
            path=path
        )
        await profiler.start()

        # Get first update
        await asyncio.sleep(0.01)
        stats1 = Stats(path)

        # Not enough sleep till next update
        await asyncio.sleep(0.01)
        stats2 = Stats(path)

        # Getting the same dump
        assert stats1.stats == stats2.stats

        # Enough sleep till next update
        await asyncio.sleep(0.2)
        stats3 = Stats(path)

        # Getting updated dump
        assert stats2.stats != stats3.stats

    finally:
        if profiler:
            await profiler.stop()
        os.remove(path)


text:
from . import AWSProperty, AWSAttribute, validate_pausetime
from .validators import positive_integer, integer, boolean

class AutoScalingRollingUpdate(AWSProperty):
    props = {
        'MaxBatchSize': (positive_integer, False),
        'MinInstancesInService': (integer, False),
        'MinSuccessfulInstancesPercent': (integer, False),
        'PauseTime': (validate_pausetime, False),
        'SuspendProcesses': ([str], False),
        'WaitOnResourceSignals': (boolean, False),
    }

class AutoScalingScheduledAction(AWSProperty):
    props = {
        'IgnoreUnmodifiedGroupSizeProperties': (boolean, False),
    }

class AutoScalingReplacingUpdate(AWSProperty):
    props = {
        'WillReplace': (boolean, False),
    }

class CodeDeployLambdaAliasUpdate(AWSProperty):
    props = {
        'AfterAllowTrafficHook': (str, False),
        'ApplicationName': (boolean, True),
        'BeforeAllowTrafficHook': (str, False),
        'DeploymentGroupName': (boolean, True),
    }

class UpdatePolicy(AWSAttribute):
    props = {
        'AutoScalingRollingUpdate': (AutoScalingRollingUpdate, False),
        'AutoScalingScheduledAction': (AutoScalingScheduledAction, False),
        'AutoScalingReplacingUpdate': (AutoScalingReplacingUpdate, False),
        'CodeDeployLambdaAliasUpdate': (CodeDeployLambdaAliasUpdate, False),
        'UseOnlineResharding': (boolean, False),
        'EnableVersionUpgrade': (boolean, False),
    }

class ResourceSignal(AWSProperty):
    props = {
        'Count': (positive_integer, False),
        'Timeout': (validate_pausetime, False),
    }

class AutoScalingCreationPolicy(AWSProperty):
    props = {
        'MinSuccessfulInstancesPercent': (integer, False),
    }

class CreationPolicy(AWSAttribute):
    props = {
        'AutoScalingCreationPolicy': (AutoScalingCreationPolicy, False),
        'ResourceSignal': (ResourceSignal, True),
    }


text:
def demo():
    """Output:
    ---------⌝
    ----------
    ----?????-
    ----------
    ----------
    --!!!-----
    --!!!-----
    ----------
    ----------
    ⌞---------
    """
    n = 10

    # Construction is easy:
    grid = {}

    # Assignment is easy:
    grid[(0, 0)] = "⌞"
    grid[(n - 1, n - 1)] = "⌝"

    # Helper functions that just work on the dictionary:
    fill(grid, "!", start=(2, 3), stop=(5, 5))
    fill(grid, "?", start=(4, 7), stop=(9, 8))

    print(stringify(grid, n))

def fill(grid: dict, value: str, start=(0, 0), stop=(0, 0)):
    """Using product allows for flatter loops."""
    from itertools import product

    for coord in product(range(start[0], stop[0]), range(start[1], stop[1])):
        grid[coord] = value

def stringify(grid: dict, n: int) -> str:
    """Stringify with (0, 0) in the lower-left corner."""
    rows = []
    for y in reversed(range(n)):
        row = []
        for x in range(n):
            value = grid.get((x, y), "-")
            row.append(value)

        rows.append(row)

    return "\n".join("".join(row) for row in rows)

if __name__ == "__main__":
    demo()


text:
# coding: utf-8

"""
    Kubernetes

    No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)

    OpenAPI spec version: v1.14.4

    Generated by: https://github.com/swagger-api/swagger-codegen.git
"""

from pprint import pformat
from six import iteritems
import re

class V1APIGroup(object):
    """
    NOTE: This class is auto generated by the swagger code generator program.
    Do not edit the class manually.
    """

    """
    Attributes:
      swagger_types (dict): The key is attribute name
                            and the value is attribute type.
      attribute_map (dict): The key is attribute name
                            and the value is json key in definition.
    """
    swagger_types = {
        'api_version': 'str',
        'kind': 'str',
        'name': 'str',
        'preferred_version': 'V1GroupVersionForDiscovery',
        'server_address_by_client_cid_rs': 'list[V1ServerAddressByClientCIDR]',
        'versions': 'list[V1GroupVersionForDiscovery]'
    }

    attribute_map = {
        'api_version': 'apiVersion',
        'kind': 'kind',
        'name': 'name',
        'preferred_version': 'preferredVersion',
        'server_address_by_client_cid_rs': 'serverAddressByClientCIDRs',
        'versions': 'versions'
    }

    def __init__(self, api_version=None, kind=None, name=None, preferred_version=None, server_address_by_client_cid_rs=None, versions=None):
        """
        V1APIGroup - a model defined in Swagger
        """

        self._api_version = None
        self._kind = None
        self._name = None
        self._preferred_version = None
        self._server_address_by_client_cid_rs = None
        self._versions = None
        self.discriminator = None

        if api_version is not None:
          self.api_version = api_version
        if kind is not None:
          self.kind = kind
        self.name = name
        if preferred_version is not None:
          self.preferred_version = preferred_version
        if server_address_by_client_cid_rs is not None:
          self.server_address_by_client_cid_rs = server_address_by_client_cid_rs
        self.versions = versions

    @property
    def api_version(self):
        """
        Gets the api_version of this V1APIGroup.
        APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources

        :return: The api_version of this V1APIGroup.
        :rtype: str
        """
        return self._api_version

    @api_version.setter
    def api_version(self, api_version):
        """
        Sets the api_version of this V1APIGroup.
        APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources

        :param api_version: The api_version of this V1APIGroup.
        :type: str
        """

        self._api_version = api_version

    @property
    def kind(self):
        """
        Gets the kind of this V1APIGroup.
        Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds

        :return: The kind of this V1APIGroup.
        :rtype: str
        """
        return self._kind

    @kind.setter
    def kind(self, kind):
        """
        Sets the kind of this V1APIGroup.
        Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds

        :param kind: The kind of this V1APIGroup.
        :type: str
        """

        self._kind = kind

    @property
    def name(self):
        """
        Gets the name of this V1APIGroup.
        name is the name of the group.

        :return: The name of this V1APIGroup.
        :rtype: str
        """
        return self._name

    @name.setter
    def name(self, name):
        """
        Sets the name of this V1APIGroup.
        name is the name of the group.

        :param name: The name of this V1APIGroup.
        :type: str
        """
        if name is None:
            raise ValueError("Invalid value for `name`, must not be `None`")

        self._name = name

    @property
    def preferred_version(self):
        """
        Gets the preferred_version of this V1APIGroup.
        preferredVersion is the version preferred by the API server, which probably is the storage version.

        :return: The preferred_version of this V1APIGroup.
        :rtype: V1GroupVersionForDiscovery
        """
        return self._preferred_version

    @preferred_version.setter
    def preferred_version(self, preferred_version):
        """
        Sets the preferred_version of this V1APIGroup.
        preferredVersion is the version preferred by the API server, which probably is the storage version.

        :param preferred_version: The preferred_version of this V1APIGroup.
        :type: V1GroupVersionForDiscovery
        """

        self._preferred_version = preferred_version

    @property
    def server_address_by_client_cid_rs(self):
        """
        Gets the server_address_by_client_cid_rs of this V1APIGroup.
        a map of client CIDR to server address that is serving this group. This is to help clients reach servers in the most network-efficient way possible. Clients can use the appropriate server address as per the CIDR that they match. In case of multiple matches, clients should use the longest matching CIDR. The server returns only those CIDRs that it thinks that the client can match. For example: the master will return an internal IP CIDR only, if the client reaches the server using an internal IP. Server looks at X-Forwarded-For header or X-Real-Ip header or request.RemoteAddr (in that order) to get the client IP.

        :return: The server_address_by_client_cid_rs of this V1APIGroup.
        :rtype: list[V1ServerAddressByClientCIDR]
        """
        return self._server_address_by_client_cid_rs

    @server_address_by_client_cid_rs.setter
    def server_address_by_client_cid_rs(self, server_address_by_client_cid_rs):
        """
        Sets the server_address_by_client_cid_rs of this V1APIGroup.
        a map of client CIDR to server address that is serving this group. This is to help clients reach servers in the most network-efficient way possible. Clients can use the appropriate server address as per the CIDR that they match. In case of multiple matches, clients should use the longest matching CIDR. The server returns only those CIDRs that it thinks that the client can match. For example: the master will return an internal IP CIDR only, if the client reaches the server using an internal IP. Server looks at X-Forwarded-For header or X-Real-Ip header or request.RemoteAddr (in that order) to get the client IP.

        :param server_address_by_client_cid_rs: The server_address_by_client_cid_rs of this V1APIGroup.
        :type: list[V1ServerAddressByClientCIDR]
        """

        self._server_address_by_client_cid_rs = server_address_by_client_cid_rs

    @property
    def versions(self):
        """
        Gets the versions of this V1APIGroup.
        versions are the versions supported in this group.

        :return: The versions of this V1APIGroup.
        :rtype: list[V1GroupVersionForDiscovery]
        """
        return self._versions

    @versions.setter
    def versions(self, versions):
        """
        Sets the versions of this V1APIGroup.
        versions are the versions supported in this group.

        :param versions: The versions of this V1APIGroup.
        :type: list[V1GroupVersionForDiscovery]
        """
        if versions is None:
            raise ValueError("Invalid value for `versions`, must not be `None`")

        self._versions = versions

    def to_dict(self):
        """
        Returns the model properties as a dict
        """
        result = {}

        for attr, _ in iteritems(self.swagger_types):
            value = getattr(self, attr)
            if isinstance(value, list):
                result[attr] = list(map(
                    lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
                    value
                ))
            elif hasattr(value, "to_dict"):
                result[attr] = value.to_dict()
            elif isinstance(value, dict):
                result[attr] = dict(map(
                    lambda item: (item[0], item[1].to_dict())
                    if hasattr(item[1], "to_dict") else item,
                    value.items()
                ))
            else:
                result[attr] = value

        return result

    def to_str(self):
        """
        Returns the string representation of the model
        """
        return pformat(self.to_dict())

    def __repr__(self):
        """
        For `print` and `pprint`
        """
        return self.to_str()

    def __eq__(self, other):
        """
        Returns true if both objects are equal
        """
        if not isinstance(other, V1APIGroup):
            return False

        return self.__dict__ == other.__dict__

    def __ne__(self, other):
        """
        Returns true if both objects are not equal
        """
        return not self == other


text:
"""
===================================
Demo of OPTICS clustering algorithm
===================================

.. currentmodule:: sklearn

Finds core samples of high density and expands clusters from them.
This example uses data that is generated so that the clusters have
different densities.
The :class:`~cluster.OPTICS` is first used with its Xi cluster detection
method, and then setting specific thresholds on the reachability, which
corresponds to :class:`~cluster.DBSCAN`. We can see that the different
clusters of OPTICS's Xi method can be recovered with different choices of
thresholds in DBSCAN.
"""

# Authors: Shane Grigsby <refuge@rocktalus.com>
#          Adrin Jalali <adrin.jalali@gmail.com>
# License: BSD 3 clause

from sklearn.cluster import OPTICS, cluster_optics_dbscan
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np

# Generate sample data

np.random.seed(0)
n_points_per_cluster = 250

C1 = [-5, -2] + .8 * np.random.randn(n_points_per_cluster, 2)
C2 = [4, -1] + .1 * np.random.randn(n_points_per_cluster, 2)
C3 = [1, -2] + .2 * np.random.randn(n_points_per_cluster, 2)
C4 = [-2, 3] + .3 * np.random.randn(n_points_per_cluster, 2)
C5 = [3, -2] + 1.6 * np.random.randn(n_points_per_cluster, 2)
C6 = [5, 6] + 2 * np.random.randn(n_points_per_cluster, 2)
X = np.vstack((C1, C2, C3, C4, C5, C6))

clust = OPTICS(min_samples=50, xi=.05, min_cluster_size=.05)

# Run the fit
clust.fit(X)

labels_050 = cluster_optics_dbscan(reachability=clust.reachability_,
                                   core_distances=clust.core_distances_,
                                   ordering=clust.ordering_, eps=0.5)
labels_200 = cluster_optics_dbscan(reachability=clust.reachability_,
                                   core_distances=clust.core_distances_,
                                   ordering=clust.ordering_, eps=2)

space = np.arange(len(X))
reachability = clust.reachability_[clust.ordering_]
labels = clust.labels_[clust.ordering_]

plt.figure(figsize=(10, 7))
G = gridspec.GridSpec(2, 3)
ax1 = plt.subplot(G[0, :])
ax2 = plt.subplot(G[1, 0])
ax3 = plt.subplot(G[1, 1])
ax4 = plt.subplot(G[1, 2])

# Reachability plot
colors = ['g.', 'r.', 'b.', 'y.', 'c.']
for klass, color in zip(range(5), colors):
    Xk = space[labels == klass]
    Rk = reachability[labels == klass]
    ax1.plot(Xk, Rk, color, alpha=0.3)
ax1.plot(space[labels == -1], reachability[labels == -1], 'k.', alpha=0.3)
ax1.plot(space, np.full_like(space, 2., dtype=float), 'k-', alpha=0.5)
ax1.plot(space, np.full_like(space, 0.5, dtype=float), 'k-.', alpha=0.5)
ax1.set_ylabel('Reachability (epsilon distance)')
ax1.set_title('Reachability Plot')

# OPTICS
colors = ['g.', 'r.', 'b.', 'y.', 'c.']
for klass, color in zip(range(5), colors):
    Xk = X[clust.labels_ == klass]
    ax2.plot(Xk[:, 0], Xk[:, 1], color, alpha=0.3)
ax2.plot(X[clust.labels_ == -1, 0], X[clust.labels_ == -1, 1], 'k+', alpha=0.1)
ax2.set_title('Automatic Clustering\nOPTICS')

# DBSCAN at 0.5
colors = ['g', 'greenyellow', 'olive', 'r', 'b', 'c']
for klass, color in zip(range(6), colors):
    Xk = X[labels_050 == klass]
    ax3.plot(Xk[:, 0], Xk[:, 1], color, alpha=0.3, marker='.')
ax3.plot(X[labels_050 == -1, 0], X[labels_050 == -1, 1], 'k+', alpha=0.1)
ax3.set_title('Clustering at 0.5 epsilon cut\nDBSCAN')

# DBSCAN at 2.
colors = ['g.', 'm.', 'y.', 'c.']
for klass, color in zip(range(4), colors):
    Xk = X[labels_200 == klass]
    ax4.plot(Xk[:, 0], Xk[:, 1], color, alpha=0.3)
ax4.plot(X[labels_200 == -1, 0], X[labels_200 == -1, 1], 'k+', alpha=0.1)
ax4.set_title('Clustering at 2.0 epsilon cut\nDBSCAN')

plt.tight_layout()
plt.show()


text:
"""
input: image
output: little squares with faces
"""

import face_recognition
image = face_recognition.load_image_file("people.png")
face_locations = face_recognition.face_locations(image)

print(face_locations)