'''
6. Determine the bandwidth when forwarding MPI messages depending on the size of the data.
    1 Create a list object of one int type element
    2 Measure the size L of the object using the sys library
    3 host should send a message to the worker, then the worker should send it back
    4 repeat point (3) N=10 times
    5 measure the time T from sending the first message to receiving the last one at the host
    6 bandwidth is measured by the formula: R=(2*N*L) / T
    7 output the result to the console in the format "object_size (bytes): R (MB/s)
    8 increase the number of sheet elements by 1000
    9 repeat from point (2) until the number of elements is equal to 50 000 elements
'''

'''
6. Определите пропускную способность при пересылке MPI-сообщений в зависимости от размера данных.
    1 Создайте объект списка из одного элемента типа int
    2 Измерьте размер L объекта с помощью библиотеки sys
    3 хост должен отправить сообщение работнику, затем работник должен отправить его обратно
    4 повторите пункт (3) N=10 раз
    5 измерьте время T от отправки первого сообщения до получения последнего на хосте
    6 пропускная способность измеряется по формуле: R=(2*N*L) / T
    7 выведите результат на консоль в формате "object_size (байты): R (МБ/с)
    8 увеличьте количество элементов листа на 1000
    9 повторяйте с точки (2) до тех пор, пока количество элементов не станет равным 50 000 элементов
'''

import time
import sys
from mpi4py import MPI as mpi

def sendRecvMessage(N: int, listSize: int):
    comm = mpi.COMM_WORLD
    rank = comm.Get_rank()
    size = comm.Get_size()

    # 1 Create a list object of one int type element
    # 8 increase the number of sheet elements by 1000
    intList = [1] * listSize

    # 2 Measure the size L of the object using the sys library
    L = sys.getsizeof(intList) # sizeof list in bytes

    message = None

    # 3 host should send a message to the worker, then the worker should send it back
    # 4 repeat point (3) N=10 times
    for i in range(N):

        # start timer before first message sending
        if (i == 0 and rank == 0):
            t_start = time.perf_counter()

        if (rank == 1):
            # first of all to recieve message from root
            message = comm.recv(tag=i, source=0)

            # and then send this message to root
            comm.send(message, dest=0, tag=i+1)
        elif (rank == 0):
            # send message to all workers from host and each worker get this message
            comm.send(intList, dest=1, tag=i)

            # root shoult get message
            message = comm.recv(tag=i+1, source=1)

            # stop timer after last message receiving
            if (i == N - 1):
                t_stop = time.perf_counter()
                T = float(t_stop - t_start)

                # 5 measure the time T from sending the first message to receiving the last one at the host
                print("Time from first message sending to last message receiving: {:0.5f} sec ({:0.5f} mls)".format(T, T*1000))

                # 6 bandwidth is measured by the formula: R=(2*N*L) / T
                # 7 output the result to the console in the format "object_size (bytes): R (MB/s)
                R = (2 * N * L) / T
                print("object_size {} (bytes): R {:0.2f} (MB/s)".format(L, R))

def main():
    if (mpi.COMM_WORLD.Get_size() != 2):
        if (mpi.COMM_WORLD.rank == 0):
            print("Wrong number of threads!")
        return

    # 4 repeat point (3) N=10 times
    N = 10
    # 8 increase the number of sheet elements by 1000
    elementsCount = 1000
    # 9 repeat from point (2) until the number of elements is equal to 50 000 elements
    totalElementsCount = 50000

    iterCount = totalElementsCount // elementsCount + 1

    # for elements list with one element

    if (mpi.COMM_WORLD.rank == 0):
            print("With one element")
    sendRecvMessage(N, 1)
    if (mpi.COMM_WORLD.rank == 0):
            print()

    for i in range(1, iterCount):
        if (mpi.COMM_WORLD.rank == 0):
            print("Iter #{}".format(i))

        sendRecvMessage(N, elementsCount*i)

        if (mpi.COMM_WORLD.rank == 0):
            print()

if __name__ == "__main__":
    main()


from hashlib import md5

import requests
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.http import Http404, HttpResponseNotAllowed, HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response, get_object_or_404, render
from django.template import RequestContext
from django.utils.log import getLogger
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import DeleteView, CreateView, UpdateView
from django.views.generic import View
from django.views.generic.list import ListView

from apps.core.extend.paginator import ExtentPaginator as Jpaginator
from apps.core.forms.entity import EditEntityForm, EntityImageForm, \
    CreateEntityForm, load_entity_info, BuyLinkForm, EditBuyLinkForm, AddEntityForm
from apps.core.mixins.views import FilterMixin
from apps.core.models import Entity, Buy_Link, GKUser
from apps.core.tasks.entity import fetch_image
from apps.core.utils.http import SuccessJsonResponse, ErrorJsonResponse
from apps.management.decorators import staff_and_editor
from apps.management.forms.sku import SKUForm
from apps.management.mixins.auth import EditorRequiredMixin
from apps.order.models import SKU

log = getLogger('django')
image_host = getattr(settings, 'IMAGE_HOST', None)

class EntityListView(FilterMixin, ListView):
    template_name = 'management/entities/list.html'
    model = Entity
    paginate_by = 30
    context_object_name = 'entities'
    paginator_class = Jpaginator

    def get_amazon_entities(self, qs):
        amazon_entity_ids = Buy_Link.objects \
            .filter(origin_source__icontains='amazon') \
            .values_list('entity_id', flat=True)
        return qs.filter(pk__in=amazon_entity_ids)

    def get_queryset(self):
        qs = super(EntityListView, self).get_queryset()
        status = self.request.GET.get('status', None)
        if status is None:
            return qs
        elif status == '999':
            entity_list = self.get_amazon_entities(qs)
        elif status == '888':
            entity_list = self.get_editor_frozen_entities(qs)
        elif status == '666':
            entity_list = self.get_active_user_entities(qs)
        else:
            entity_list = qs.filter(status=int(status)).order_by('-updated_time')
        return entity_list

    def get_active_user_entities(self, qs):
        active_users = GKUser.objects.active_user()
        entity_list = Entity.objects \
            .filter(user__in=active_users) \
            .order_by('-updated_time')
        return entity_list

    def get_editor_frozen_entities(self, qs):
        editors = GKUser.objects.editor()
        entity_frozen = Entity.objects \
            .filter(status=Entity.freeze, user__in=editors) \
            .order_by('-updated_time')
        return entity_frozen

    # TODO: need clear input  in filter Mixin
    def filter_queryset(self, qs, filter_param):
        filter_field, filter_value = filter_param
        if filter_field == 'brand':
            qs = qs.filter(brand__icontains=filter_value.strip())
        elif filter_field == 'title':
            qs = qs.filter(title__icontains=filter_value.strip())
        else:
            pass
        return qs

    def get_context_data(self, *args, **kwargs):
        context = super(EntityListView, self).get_context_data(*args, **kwargs)
        context['status'] = self.request.GET.get('status', None)
        return context

    @staff_and_editor
    def dispatch(self, request, *args, **kwargs):
        return super(EntityListView, self).dispatch(request, *args, **kwargs)

@login_required
@staff_and_editor
def edit(request, entity_id, template='management/entities/edit.html'):
    _update = None
    try:
        entity = Entity.objects.get(pk=entity_id)
    except Entity.DoesNotExist:
        raise Http404

    data = {
        # 'id':entity.pk,
        'creator': entity.user.profile.nickname,
        'brand': entity.brand,
        'title': entity.title,
        'price': entity.price,
        'status': entity.status,
        'category': entity.category.group_id,
        'sub_category': entity.category_id,
    }

    if request.method == "POST":
        _forms = EditEntityForm(
            entity,
            request.POST,
            initial=data
        )
        _update = 1

        if _forms.is_valid():
            _forms.save()
            _update = 0

    else:
        # log.info(entity.category)
        _forms = EditEntityForm(
            entity=entity,
            initial=data
        )

    return render_to_response(
        template,
        {
            'entity': entity,
            'forms': _forms,
            'update': _update,
        },
        context_instance=RequestContext(request)
    )

@login_required
@staff_and_editor
def create(request, template='management/entities/new.html'):
    _url = request.GET.get('url')

    if _url is None:
        raise Http404
        # print(_url)
    res = load_entity_info(_url)
    # log.info("OKOKO %s", len(res))

    if res.has_key('entity_id'):
        return HttpResponseRedirect(
            reverse('management_entity_edit', args=[res['entity_id']]))

    if len(res) == 0:
        return HttpResponse('not support')

    if request.method == "POST":
        # log.info(request.POST)
        _forms = CreateEntityForm(request=request, data=request.POST,
                                  initial=res)
        # _forms = EntityURLFrom(request=request, data=request.POST)
        if _forms.is_valid():
            entity = _forms.save()
            return HttpResponseRedirect(
                reverse('management_entity_edit', args=[entity.pk]))
    else:

        # log.info("category %s %s" % (res['cid'], res['origin_source']))
        key_string = "%s%s" % (res['cid'], res['origin_source'])
        # log.info(ke)
        key = md5(key_string.encode('utf-8')).hexdigest()
        category_id = cache.get(key)
        # res.update('category_id', category_id)
        if category_id:
            res['category_id'] = category_id
        else:
            res['category_id'] = 300
        # log.info("%s %s" % (key, category_id))
        _forms = CreateEntityForm(request=request, initial=res)

    return render_to_response(
        template,
        {
            'res': res,
            'forms': _forms,
        },
        context_instance=RequestContext(request)
    )

class ImportEntity(View):
    def __init__(self, template='management/entities/new.html', entity_edit_url='management_entity_edit',
                 form=CreateEntityForm, **kwargs):
        self.template = template
        self.entity_edit_url = entity_edit_url
        self.form = form
        super(ImportEntity, self).__init__(**kwargs)

    @staticmethod
    def get_res(request):
        _url = request.GET.get('url')
        if _url is None:
            raise Http404
        return load_entity_info(_url)

    def get(self, request, *args, **kwargs):
        res = ImportEntity.get_res(request)
        if not res:
            return HttpResponse('not support')
        if 'entity_id' in res:
            # entity already exists
            return HttpResponseRedirect(reverse(self.entity_edit_url, args=[res['entity_id']]))
        else:
            key_string = "%s%s" % (res['cid'], res['origin_source'])
            key = md5(key_string.encode('utf-8')).hexdigest()
            category_id = cache.get(key)
            if category_id:
                res['category_id'] = category_id
            else:
                res['category_id'] = 300
            _forms = self.form(request=request, initial=res)
            return render(request, self.template, {'res': res, 'forms': _forms})

    def post(self, request, *args, **kwargs):
        res = ImportEntity.get_res(request)
        _forms = self.form(request=request, data=request.POST, initial=res)
        if _forms.is_valid():
            entity = _forms.save()
            return HttpResponseRedirect(reverse(self.entity_edit_url, args=[entity.pk]))

class Add_local(View):
    template_name = 'management/entities/add.html'
    form_class = AddEntityForm

    def get(self, request, *args, **kwargs):
        form = self.form_class(request)
        return render(request, self.template_name, {'forms': form})

    def post(self, request, *args, **kwargs):
        form = self.form_class(request, data=request.POST, files=request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('management_entity_list'))
        return render(request, self.template_name, {'forms': form})

@login_required
@staff_and_editor
def buy_link(request, entity_id, template='management/entities/buy_link.html'):
    try:
        _entity = Entity.objects.get(pk=entity_id)
    except Entity.DoesNotExist:
        raise Http404

    if request.method == 'POST':
        _forms = BuyLinkForm(entity=_entity, data=request.POST)
        if _forms.is_valid():
            _forms.save()
            return HttpResponseRedirect(
                reverse('management_entity_edit', args=[entity_id]))
    else:
        _forms = BuyLinkForm(entity=_entity)

    return render_to_response(
        template,
        {
            'entity': _entity,
            'forms': _forms,
        },
        context_instance=RequestContext(request)
    )

@csrf_exempt
@login_required
@staff_and_editor
def edit_buy_link(request, bid,
                  template='management/entities/edit_buy_link.html'):
    try:
        buy = Buy_Link.objects.get(pk=bid)
    except Buy_Link.DoesNotExist:
        raise Http404

    if request.method == 'POST':
        _forms = EditBuyLinkForm(buy_link=buy, data=request.POST)
        if _forms.is_valid():
            buy_link = _forms.save()
            return HttpResponseRedirect(
                reverse('management_entity_edit', args=[buy_link.entity_id]))
    else:
        # log.info(int(buy.default))
        _forms = EditBuyLinkForm(buy_link=buy, initial={
            'link': buy.link,
            'default': buy.default,
        })

    return render_to_response(
        template,
        {
            'entity': buy.entity,
            'forms': _forms,
        },
        context_instance=RequestContext(request))

@csrf_exempt
@login_required
@staff_and_editor
def remove_buy_link(request, bid):
    try:
        b = Buy_Link.objects.get(pk=bid)
    except Buy_Link.DoesNotExist:
        return ErrorJsonResponse(status=404)

    if b.default:
        return ErrorJsonResponse(status=403)
    b.delete()
    return SuccessJsonResponse()

class CheckBuyLinkView(View):
    # http_method_names = ['GET']

    def get_context_data(self, **kwargs):
        try:
            b = Buy_Link.objects.get(pk=self.bid)
        except Buy_Link.DoesNotExist:
            raise Http404

        data = {
            'project': 'default',
            'spider': 'taobao',
            'setting': 'DOWNLOAD_DELAY=2',
            'item_id': b.origin_id,
        }
        res = requests.post('http://10.0.2.49:6800/schedule.json', data=data)
        if res.status_code == 200:
            return SuccessJsonResponse(data=res.json())
        else:
            raise Http404

    def get(self, request, *args, **kwargs):
        self.bid = kwargs.pop('bid', None)
        assert self.bid is not None
        return self.get_context_data(**kwargs)

    @staff_and_editor
    def dispatch(self, request, *args, **kwargs):
        return super(CheckBuyLinkView, self).dispatch(request, *args, **kwargs)

# TODO:
'''
    Handle Image
'''

@login_required
@staff_and_editor
def refetch_image(request, entity_id):
    try:
        _entity = Entity.objects.get(pk=entity_id)
    except Entity.DoesNotExist:
        raise Http404

    log.info(_entity.images)

    fetch_image(_entity.images, _entity.pk)
    return SuccessJsonResponse(data={'status': 'ok'})

@login_required
@staff_and_editor
def image(request, entity_id,
          template='management/entities/upload_image.html'):
    try:
        _entity = Entity.objects.get(pk=entity_id)
    except Entity.DoesNotExist:
        raise Http404

    if request.method == "POST":
        _forms = EntityImageForm(entity=_entity, data=request.POST,
                                 files=request.FILES)
        if _forms.is_valid():
            _forms.save()
            return HttpResponseRedirect(reverse('management_entity_edit', args=[_entity.id]))

    else:
        _forms = EntityImageForm(entity=_entity)

    return render_to_response(
        template,
        {
            'entity': _entity,
            'forms': _forms,
        },
        context_instance=RequestContext(request)
    )

@csrf_exempt
@login_required
@staff_and_editor
def delete_image(request, entity_id):
    if request.method == 'POST':
        _index = request.POST.get('index', None)
        log.info(_index)
        try:
            _entity = Entity.objects.get(pk=entity_id)
            images = _entity.images
            log.info(images)
            images.remove(_index)
            _entity.images = images
            _entity.save()
        except Entity.DoesNotExist:
            raise Http404

        status = True
        return SuccessJsonResponse(data={'status': status})

    return HttpResponseNotAllowed

class EntitySKUListView(EditorRequiredMixin, ListView):
    template_name = 'management/entities/entity_sku_list.html'

    def get_queryset(self):
        entity = self.get_entity()
        return entity.skus.all()

    def get_entity(self):
        return get_object_or_404(Entity, id=self.kwargs.get('entity_id', None))

    def get_context_data(self, **kwargs):
        context = super(EntitySKUListView, self).get_context_data(**kwargs)
        context['entity'] = self.get_entity()
        return context

class EntitySKUCreateView(EditorRequiredMixin, CreateView):
    model = SKU
    form_class = SKUForm
    template_name = 'management/entities/create_sku.html'

    def get_success_url(self):
        return reverse('management_entity_skus', args=[self.get_entity().id])

    def get_initial(self):
        entity = self.get_entity()
        return {
            'entity': entity.id
        }

    def get_entity(self):
        entity_id = self.kwargs.get('entity_id')
        entity = get_object_or_404(Entity, id=entity_id)
        return entity

    def get_context_data(self, **kwargs):
        context = super(EntitySKUCreateView, self).get_context_data(**kwargs)
        context['entity'] = self.get_entity()
        context['entity_id'] = self.get_entity().id
        return context

class EntitySKUUpdateView(EditorRequiredMixin, UpdateView):
    model = SKU
    form_class = SKUForm
    template_name = 'management/entities/update_sku.html'

    def get_entity(self):
        entity_id = self.kwargs.get('entity_id')
        entity = get_object_or_404(Entity, id=entity_id)
        return entity

    def get_context_data(self, **kwargs):
        context = super(EntitySKUUpdateView, self).get_context_data(**kwargs)
        context['entity_id'] = self.get_entity().id
        return context

    def get_success_url(self):
        return reverse('management_entity_skus', args=[self.get_entity().id])

class EntitySKUDeleteView(EditorRequiredMixin, DeleteView):
    model = SKU
    template_name = 'management/entities/delete_sku.html'

    def get_entity(self):
        entity_id = self.kwargs.get('entity_id')
        entity = get_object_or_404(Entity, id=entity_id)
        return entity

    def get_context_data(self, **kwargs):
        context = super(EntitySKUDeleteView, self).get_context_data()
        context['entity'] = self.get_entity()
        return context

    def get_success_url(self):
        return reverse('management_entity_skus', args=[self.get_entity().id])

__author__ = 'edison7500'


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

from ..urls import app_name

USERNAME = "username"
SLUG = "slug"
ID = 1

ROUTES = (
    ("index", "/", []),
    ("post_edit", f"/posts/{ID}/edit/", [ID]),
    ("post_create", "/create/", []),
    ("post_detail", f"/posts/{ID}/", [ID]),
    ("profile", f"/profile/{USERNAME}/", [USERNAME]),
    ("add_comment", f"/posts/{ID}/comment/", [ID]),
    ("group_list", f"/group/{SLUG}/", [SLUG]),
    ("follow_index", "/follow/", []),
    ("profile_follow", f"/profile/{USERNAME}/follow/", [USERNAME]),
    ("profile_unfollow", f"/profile/{USERNAME}/unfollow/", [USERNAME]),
)

class RoutesTests(TestCase):
    def test_urls_correctly_resolved(self):
        for key, url, args in ROUTES:
            with self.subTest(key=key):
                self.assertEqual(reverse(f"{app_name}:{key}", args=args), url)


#!/usr/bin/env python
'''
Created on 26 feb 2019

@author: Alessandro Ogier <alessandro.ogier@gmail.com>
'''
import cProfile
import multiprocessing
import os
import sys

import click
import cloudant
import ijson

STOP = 'KTHXBYE'

def _worker(in_queue, database, when_existing):

    batch = []
    for item in iter(in_queue.get, STOP):

        try:
            doc = database[item['_id']]
        except KeyError:
            batch.append(item)
            continue

        if item.items() <= doc.items():
            continue

        if when_existing == 'overwrite':
            print(f'overwriting {item}')
            doc.delete()
            doc.update(item)
        elif when_existing == 'update':
            print(f'updating {item}')
            doc.update(item)
        elif when_existing == 'ignore':
            #             print(f'ignoring {item}')
            continue

        batch.append(doc)

        if len(batch) > 100:
            database.bulk_docs(batch)
            batch = []

    if batch:
        database.bulk_docs(batch)

def _profi_worker(in_queue, database, when_existing):  # pylint: disable=unused-argument
    cProfile.runctx('_worker(in_queue, database, when_existing)',
                    globals(), locals(), 'prof%d.prof' % os.getpid())

@click.command(help='Ingest data into a db')
@click.option('--couchdb-user', help='couchdb username', required=True)
@click.option('--couchdb-pass', help='couchdb password', required=True)
@click.option('--couchdb-url', 'couchdb_url',
              help='couchdb url',
              default='http://localhost:5984', show_default=True)
@click.option('--database', 'dbname',
              help='database name', required=True)
@click.option('--create-database',
              help='create database if it does not exist',
              default=True, show_default=True)
@click.option('--id-field',
              help='which json field is document _id')
@click.option('--when-existing',
              help='action when record already exists.',
              type=click.Choice(['overwrite', 'update', 'ignore']),
              default='ignore', show_default=True)
@click.option('--pool-size',
              help='pool size',
              default=2)
@click.option('--single',
              help='input is a single object, ie. not an array',
              is_flag=True, default=False)
@click.option('--profile',
              help='profile process (debug only',
              is_flag=True, default=False)
def main(couchdb_user, couchdb_pass, couchdb_url,  # pylint: disable=too-many-arguments, too-many-locals
         dbname, create_database,
         id_field, when_existing,
         pool_size, single, profile):
    '''
    Ingest data into a db

    :param couchdb_user:
    :param couchdb_pass:
    :param couchdb_url:
    :param dbname:
    :param create_database:
    :param id_field:
    :param when_existing:
    :param pool_size:
    :param single:
    :param profile:
    '''

    process_queue = multiprocessing.Queue(50)

    process_pool = []
    for _ in range(pool_size):
        client = cloudant.CouchDB(couchdb_user, couchdb_pass,
                                  url=couchdb_url,
                                  connect=True)

        if create_database:
            database = client.create_database(dbname)
        else:
            try:
                database = client[dbname]
            except KeyError:
                print(f'database {dbname} not found')
                sys.exit(1)

        process = multiprocessing.Process(
            target=_profi_worker if profile else _worker,
            args=(process_queue, database, when_existing))

        process.start()
        process_pool.append(process)

    for item in ijson.items(sys.stdin, '' if single else 'item'):

        if id_field:
            if not item[id_field]:
                print(f'skipping null id: {item}')
                continue
            else:
                item['_id'] = item[id_field]

        process_queue.put(item)

    for _ in range(pool_size):
        process_queue.put(STOP)

    for process in process_pool:
        process.join()

if __name__ == '__main__':
    main()  # pylint: disable=no-value-for-parameter


import json
import os
import sysconfig
from argparse import ArgumentParser, ArgumentTypeError
from pathlib import Path
from pprint import pprint

from django.core.management.base import BaseCommand

from app.models import AirBnBResponse, AirBnBResponseTypes, AirBnBListing, AirBnBUser

os.environ.setdefault(
    "PROJ_LIB", os.getenv('PROJ_LIB') or Path(
        Path(sysconfig.get_paths()["purelib"]) / r"osgeo/data/proj"
    ).as_posix())

def check_positive(value) -> int:
    value = int(value)
    if value < 0:
        raise ArgumentTypeError(f"{value} must be positive integer.")

    return value

class Command(BaseCommand):
    help = 'Import AOI to the database from a shapefile/geojson.'

    def add_arguments(self, parser: ArgumentParser):
        subparsers = parser.add_subparsers(title='subcommands', description='valid subcommands',
                                           help='sub-command help', dest='command')
        parser_get_calendar: ArgumentParser = subparsers.add_parser('calendar', help='get the calendar for a listing')
        parser_get_details: ArgumentParser = subparsers.add_parser('listing-detail',
                                                                   help='get the listing Details for a listing')
        parser_get_booking_quote: ArgumentParser = subparsers.add_parser('booking-quote',
                                                                         help='get a Booking Quote for a listing')
        parser_get_user: ArgumentParser = subparsers.add_parser('user-detail', help='get the User',
                                                                description='Get user details form the Database for user with USER_ID.\n'
                                                                            'If the user does not exist it will be fetched from airbnb.\n'
                                                                            ' TODO: --refresh')
        parser_get_calendar.add_argument('listing_id', type=check_positive, help="listing_id")
        parser_get_details.add_argument('listing_id', type=check_positive, help="listing_id")
        parser_get_booking_quote.add_argument('listing_id', type=check_positive, help="listing_id")
        parser_get_user.add_argument('user_id', type=check_positive, help="user_id")

    def handle(self, *args, **options):
        error = {"error": None, "msg": None}
        try:
            if options.get('command') == 'calendar':
                from app.tasks import task_update_calendar
                listing_id = task_update_calendar(options.get('listing_id'))
                response = AirBnBResponse.objects.filter(listing_id=listing_id,
                                                         _type=AirBnBResponseTypes.calendar).order_by(
                    "timestamp").first()
                self.stdout.write(json.dumps(response.payload, indent=4, sort_keys=True))

            if options.get('command') == 'listing-detail':
                from app.tasks import task_add_listing_detail
                listing_id = task_add_listing_detail(options.get('listing_id'))
                response = AirBnBListing.responses.objects.filter(listing_id=listing_id,
                                                                  _type=AirBnBResponseTypes.listingDetail).order_by(
                    "timestamp").first()

                self.stdout.write(json.dumps(response.payload, indent=4, sort_keys=True))

            if options.get('command') == 'user-detail':
                from app.tasks import task_get_or_create_user, task_update_user_details

                user_id = task_get_or_create_user(user_id=options.get('user_id'), defer=False)
                user_obj = AirBnBUser.objects.filter(user_id=user_id).first()
                response = user_obj.responses.order_by('timestamp').first()

                self.stdout.write(json.dumps(response.payload, indent=4, sort_keys=True))

            if options.get('command') == 'booking-quote':
                from app.tasks import task_update_calendar, task_get_booking_detail
                listing_id = options.get('listing_id')
                # listing_id = task_update_calendar(listing_id)
                listing_id = task_get_booking_detail(listing_id=listing_id)
                listing_obj = AirBnBListing.objects.get(listing_id=listing_id)

                booking_response = listing_obj.responses.filter(_type=AirBnBResponseTypes.bookingQuote).order_by(
                    "timestamp").first()
                self.stdout.write(json.dumps(booking_response.payload, indent=4, sort_keys=True))

        except AirBnBResponse.DoesNotExist as exc:
            error['error'] = "ResponseError"
            error['msg'] = f"{exc}"
            self.stdout.write(pprint(error))
        except AirBnBListing.DoesNotExist as exc:
            error['error'] = "ResponseError"
            error['msg'] = f"{exc}"
            self.stdout.write(pprint(error))


from ortools.sat.python import cp_model
from SolutionPrinter import SolutionPrinter

def main(num_candy, num_people, min_candy, max_candy):
	model = cp_model.CpModel()

	cVar = [model.NewIntVar(0, num_candy, 'cVar_%i' % i) for i in range(1, num_people + 1)]
	model.Add(sum(cVar) == num_candy)
	for i in range(num_people):
		model.Add(cVar[i] >= min_candy)
		model.Add(cVar[i] <= max_candy)

	solver = cp_model.CpSolver()
	solution_printer = SolutionPrinter(cVar)
	status = solver.SearchForAllSolutions(model, solution_printer)
	print()
	print('Solutions found : %i' % solution_printer.SolutionCount())

if __name__ == "__main__":
	num_candy = 10
	num_people = 3
	min_candy = 2
	max_candy = 5
	main(num_candy, num_people, min_candy, max_candy)


from time import sleep
from lacp_lib import create_lag
from lacp_lib import turn_on_interface
from lacp_lib import validate_turn_on_interfaces
from lacp_lib import associate_interface_to_lag
from lacp_lib import verify_lag_config
from lacp_lib import create_vlan
from lacp_lib import verify_vlan_full_state
from lacp_lib import check_connectivity_between_hosts
from lacp_lib import associate_vlan_to_l2_interface
from lacp_lib import associate_vlan_to_lag
from lacp_lib import verify_lag_interface_key
from lacp_lib import verify_lag_interface_priority
from lacp_lib import verify_lag_interface_id
from lacp_lib import verify_lag_interface_system_id
from lacp_lib import verify_lag_interface_system_priority
from lacp_lib import verify_lag_interface_lag_id
from lacp_lib import validate_lag_state_static
from lacp_lib import LOCAL_STATE
from lacp_lib import REMOTE_STATE
from lacp_lib import retry_wrapper
from lacp_lib import compare_lag_interface_basic_settings
import pytest

TOPOLOGY = """
#            +-----------------+
#            |                 |
#            |      Host 1     |
#            |                 |
#            +-----------------+
#                     |
#                     |
#     +-------------------------------+
#     |                               |
#     |                               |
#     |            Switch 1           |
#     |                               |
#     +-------------------------------+
#          |         |        |
#          |         |        |
#          |         |        |
#     +-------------------------------+
#     |                               |
#     |                               |
#     |            Switch 2           |
#     |                               |
#     +-------------------------------+
#                     |
#                     |
#            +-----------------+
#            |                 |
#            |     Host 2      |
#            |                 |
#            +-----------------+

# Nodes
[type=openswitch name="Switch 1"] sw1
[type=openswitch name="Switch 2"] sw2
[type=host name="Host 1" image="openswitch/ubuntutest:latest"] hs1
[type=host name="Host 2" image="openswitch/ubuntutest:latest"] hs2

# Links

sw1:1 -- hs1:1
sw2:1 -- hs2:1
sw1:2 -- sw2:2
sw1:3 -- sw2:3
sw1:4 -- sw2:4
"""

# Global variables
SW_LBL_PORTS = ['1', '2', '3', '4']
LAG_ID = '1'
LAG_VLAN = 900
NETWORK = '10.90.0.'
NETMASK = '24'
NUMBER_PINGS = 5
BASE_IPERF_PORT = 5000
IPERF_TIME = 30
IPERF_BW = '10m'
SW_COUNTERS_DELAY = 20

def compare_values(value1, value2, error=0.02):
    value2_max = value2 * (1 + error)
    value2_min = value2 * (1 - error)
    assert value1 <= value2_max, ' '.join(
        ['Value of {} is more than {}'.format(value1, error * 100),
         'percent higher than {}'.format(value2)]
    )
    assert value1 >= value2_min, ' '.join(
        ['Value of {} is more than {}'.format(value1, error * 100),
         'percent lower than {}'.format(value2)]
    )

def compare_switches_counters(sw_list, stats):
    # Verify switches counters match
    for port in SW_LBL_PORTS:
        print('Compare port {} between switches'.format(port))
        print('rx from sw1 vs tx from sw2')
        compare_values(
            int(stats[sw_list[0]][port]['{}_packets'.format('rx')]),
            int(stats[sw_list[1]][port]['{}_packets'.format('tx')])
        )
        print('tx from sw1 vs rx from sw2')
        compare_values(
            int(stats[sw_list[0]][port]['{}_packets'.format('tx')]),
            int(stats[sw_list[1]][port]['{}_packets'.format('rx')])
        )

def compare_lag_to_switches_counters(sw_stats, lag_stats, ports):
    for param in [
        'rx_bytes',
        'rx_packets',
        'rx_crc_fcs',
        'rx_dropped',
        'rx_error',
        'tx_packets',
        'tx_bytes',
        'tx_collisions',
        'tx_dropped',
        'tx_errors',
        'speed'
    ]:
        total = 0
        for port in ports:
            total += int(sw_stats[port][param])
        print('Verifying LAG interface value for {}'.format(param))
        compare_values(int(lag_stats[param]), total)
    for port in ports:
        assert lag_stats['speed_unit'] == sw_stats[port]['speed_unit'],\
            'Unexpected change in speed unit {}, Expected {}'.format(
            lag_stats['speed_unit'],
            sw_stats[port]['speed_unit']
        )

def verify_lacp_state(
    sw1,
    sw2
):
    lacp_def_value = ''
    lacp_def_priority = ''
    sys_id = ''
    print('Verify LACP state on LAG members')
    for port in SW_LBL_PORTS[1:]:
        sw1_lacp_state = sw1.libs.vtysh.show_lacp_interface(port)
        sw2_lacp_state = sw2.libs.vtysh.show_lacp_interface(port)
        sw_lacp_states = [sw1_lacp_state, sw2_lacp_state]
        for (
            sw_lacp_state,
            rev_sw_lacp_state
        ) in zip(
            sw_lacp_states,
            reversed(sw_lacp_states)
        ):
            verify_lag_interface_lag_id(sw_lacp_state)
            verify_lag_interface_key(
                sw_lacp_state,
                rev_sw_lacp_state,
                key=lacp_def_value,
                value_check=True,
                cross_check=True
            )
            verify_lag_interface_priority(
                sw_lacp_state,
                rev_sw_lacp_state,
                priority=lacp_def_value,
                value_check=True,
                cross_check=True
            )
            verify_lag_interface_system_priority(
                sw_lacp_state,
                sw2_int_map_lacp=rev_sw_lacp_state,
                system_priority=lacp_def_priority,
                value_check=True,
                cross_check=True
            )
            verify_lag_interface_system_id(
                sw_lacp_state,
                sw2_int_map_lacp=rev_sw_lacp_state,
                system_id=sys_id,
                value_check=True,
                cross_check=True
            )
            verify_lag_interface_id(
                sw_lacp_state,
                rev_sw_lacp_state,
                id='',
                value_check=True,
                cross_check=True
            )
            validate_lag_state_static(sw_lacp_state, LOCAL_STATE)
            validate_lag_state_static(sw_lacp_state, REMOTE_STATE)

def enable_switches_interfaces(sw_list, step):
    step('Enable switches interfaces')
    for sw in sw_list:
        for port in SW_LBL_PORTS:
            turn_on_interface(sw, port)
    # Defining internal method to use decorator

    @retry_wrapper(
        'Ensure interfaces are turned on',
        'Interfaces not yet ready',
        5,
        60)
    def internal_check_interfaces(sw_list):
        for sw in sw_list:
            validate_turn_on_interfaces(sw, SW_LBL_PORTS)
    internal_check_interfaces(sw_list)

def configure_lags(sw_list, sw_real_ports, step):
    step('Create LAGs')
    for sw in sw_list:
        create_lag(sw, LAG_ID, 'off')
        for port in sw_real_ports[sw][1:]:
            associate_interface_to_lag(sw, port, LAG_ID)
        verify_lag_config(
            sw,
            LAG_ID,
            sw_real_ports[sw][1:]
        )
    check_func = retry_wrapper(
        'Verify LACP status on both devices',
        'Configuration not yet applied',
        2,
        4
    )(verify_lacp_state)
    check_func(
        sw_list[0],
        sw_list[1]
    )

def configure_vlans(sw_list, sw_real_ports, step):
    step('Configure VLANs on devices')
    for sw in sw_list:
        # Create VLAN
        create_vlan(sw, LAG_VLAN)
        # Associate VLAN to LAG

        associate_vlan_to_lag(sw, str(LAG_VLAN), LAG_ID)
        # Associate VLAN to host interface
        associate_vlan_to_l2_interface(
            sw,
            str(LAG_VLAN),
            sw_real_ports[sw][0]
        )
        # Verify VLAN configuration was successfully applied
        verify_vlan_full_state(
            sw,
            LAG_VLAN, interfaces=[
                sw_real_ports[sw][0],
                'lag{}'.format(LAG_ID)
            ]
        )

def configure_workstations(hs_list, step):
    step('Configure workstations')
    for hs_num, hs in enumerate(hs_list):
        hs.libs.ip.interface(
            SW_LBL_PORTS[0],
            addr='{}{}/{}'.format(NETWORK, hs_num + 1, NETMASK),
            up=True
        )

def validate_connectivity(hs_list, wait, step):
    step('Check workstations connectivity')
    if wait is False:
        check_connectivity_between_hosts(
            hs_list[0],
            '{}{}'.format(NETWORK, 1),
            hs_list[1],
            '{}{}'.format(NETWORK, 2),
            NUMBER_PINGS,
            True
        )
    else:
        check_func = retry_wrapper(
            'Verifying workstations connectivity',
            'Configuration not yet applied',
            5,
            15
        )(check_connectivity_between_hosts)
        check_func(
            hs_list[0],
            '{}{}'.format(NETWORK, 1),
            hs_list[1],
            '{}{}'.format(NETWORK, 2),
            NUMBER_PINGS,
            True
        )

def verify_lag_statistics(sw_list, hs_list, sw_real_ports, step):
    sw_stats_before = {}
    sw_stats_after = {}
    step('Verify LAG statistics')
    print('Collect all switch ports statistics')
    for sw in sw_list:
        sw_stats_before[sw] = {}
        for port in SW_LBL_PORTS:
            sw_stats_before[sw][port] = sw.libs.vtysh.show_interface(port)
        sw_stats_before[sw]['lag{}'.format(LAG_ID)] =\
            sw.libs.vtysh.show_interface('lag{}'.format(LAG_ID))
        print('Verify LAG statistic interface basic settings')
        compare_lag_interface_basic_settings(
            sw_stats_before[sw]['lag{}'.format(LAG_ID)],
            LAG_ID,
            sw_real_ports[sw][1:]
        )
        print('Compare LAG counters vs switches interfaces')
        compare_lag_to_switches_counters(
            sw_stats_before[sw],
            sw_stats_before[sw]['lag{}'.format(LAG_ID)],
            SW_LBL_PORTS[1:]
        )
    print('Start iperf servers')
    for i, hs in enumerate(hs_list):
        hs.libs.iperf.server_start(BASE_IPERF_PORT + i, udp=True)
    print('Start traffic transmission')
    for hs, other_base in zip(hs_list, [2, 1]):
        hs.libs.iperf.client_start(
            '{}{}'.format(NETWORK, other_base),
            BASE_IPERF_PORT + other_base - 1,
            time=IPERF_TIME,
            udp=True,
            bandwidth=IPERF_BW
        )
    print('Wait for traffic to finish in {} seconds'.format(IPERF_TIME))
    sleep(IPERF_TIME)
    print('Stop iperf')
    for hs in hs_list:
        hs.libs.iperf.server_stop()
        hs.libs.iperf.client_stop()

    @retry_wrapper(
        'Obtain interfaces information and verify their consistency',
        'Information provided by counters is not yet reliable',
        5,
        SW_COUNTERS_DELAY)
    def internal_check():
        print('Get statistics from switches')
        for sw in sw_list:
            sw_stats_after[sw] = {}
            for port in SW_LBL_PORTS:
                sw_stats_after[sw][port] = sw.libs.vtysh.show_interface(port)
            sw_stats_after[sw]['lag{}'.format(LAG_ID)] =\
                sw.libs.vtysh.show_interface('lag{}'.format(LAG_ID))
        print('Verify obtained information is consistent')
        print('Compare counters between siwtches')
        compare_switches_counters(sw_list, sw_stats_after)
    internal_check()
    print('Compare switch counters individually')
    for sw in sw_list:
        print('Verify LAG statistic interface basic settings')
        compare_lag_interface_basic_settings(
            sw_stats_after[sw]['lag{}'.format(LAG_ID)],
            LAG_ID,
            sw_real_ports[sw][1:]
        )
        print('Compare LAG counters vs switches interfaces')
        compare_lag_to_switches_counters(
            sw_stats_after[sw],
            sw_stats_after[sw]['lag{}'.format(LAG_ID)],
            SW_LBL_PORTS[1:]
        )

@pytest.mark.skipif(True, reason="Skipping due to instability")
def test_ft_lag_statistics(topology, step):
    hs1 = topology.get('hs1')
    hs2 = topology.get('hs2')
    sw1 = topology.get('sw1')
    sw2 = topology.get('sw2')

    assert hs1 is not None, 'hs1 was not initialized'
    assert hs2 is not None, 'hs2 was not initialized'
    assert sw1 is not None, 'sw1 was not initialized'
    assert sw2 is not None, 'sw2 was not initialized'

    sw_real_ports = {
        sw1: [sw1.ports[port] for port in SW_LBL_PORTS],
        sw2: [sw2.ports[port] for port in SW_LBL_PORTS]
    }

    # Enable switches interfaces
    enable_switches_interfaces([sw1, sw2], step)

    # Configure static LAGs with members
    configure_lags([sw1, sw2], sw_real_ports, step)

    # Add VLAN configuration to LAGs and workstation interfaces
    configure_vlans([sw1, sw2], sw_real_ports, step)

    # Configure workstations
    configure_workstations([hs1, hs2], step)

    # Validate workstations can communicate
    validate_connectivity([hs1, hs2], True, step)

    # Obtain and validate LAG statistics
    verify_lag_statistics([sw1, sw2], [hs1, hs2], sw_real_ports, step)


"""
Record a realsense video.

When done, press ctrl-c once and give it a second to close up.
"""
import os
import pyrealsense2 as rs

if __name__ == '__main__':
    FILENAME = 'realsense_video'

    number = -1

    for filename in os.listdir('.'):
        if FILENAME in filename:
            file_number = int(filename.replace(FILENAME, '').replace('.bag', ''))

            number = max(number, file_number)

    FILENAME += f'{number + 1}.bag'

    ##
    pipeline = rs.pipeline()
    config = rs.config()

    config.enable_stream(rs.stream.depth, 0, 0, rs.format.z16, 0)
    config.enable_stream(rs.stream.color, 0, 0, rs.format.rgb8, 0)

    config.enable_record_to_file(FILENAME)

    ##
    pipeline.start(config)

    try:
        i = 0
        while True:
            i += 1
            frames = pipeline.wait_for_frames()

            depth_frame = frames.get_depth_frame()
            color_frame = frames.get_color_frame()

            if not depth_frame or not color_frame:
                print("Failed to capture image.")
                continue

    except Exception as e:
        print(e)

    pipeline.stop()


#!/usr/bin/env python
# -*- coding:utf-8 -*-
#@Time  : 2021/5/24 14:48
#@Author: Pengli Mo
#@File  : function_list.py

import xlrd
from class_define import *

def get_data(num_S,num_T):
    book = xlrd.open_workbook('E:\paper9-passenger\data\data.xls')
    TE = book.sheet_by_index(0) # travel time and energy consumption
    PA = book.sheet_by_index(1) # passenger
    TU = book.sheet_by_index(2)  # timetable_up
    TD = book.sheet_by_index(3)  # timetable_dn
    data = Data()
    data.travel=[0 for i in range(num_S*2)]
    data.dwell = [0 for i in range(num_S * 2)]
    data.E_tr=[[] for i in range(num_S*2)]
    data.E_rg = [[] for i in range(num_S * 2)]
    data.D_tr = [[] for i in range(num_S * 2)]
    data.D_br = [[] for i in range(num_S * 2)]
    for l in range(num_S):
        data.dwell[l]=int(TU.cell_value(l+1,1))
        data.travel[l]=int(TU.cell_value(l+1,0))
        data.E_tr[l]=[max(0, int(TE.cell_value(l,i))) for i in range(1,data.travel[l]+1)]
        data.E_rg[l]=[max(0, -int(TE.cell_value(l, i))) for i in range(1, data.travel[l] + 1)]
        data.D_tr[l]=sum(i>0 for i in data.E_tr[l])
        data.D_br[l]=sum(i>0 for i in data.E_rg[l])
        data.travel[l+num_S] = int(TD.cell_value(l+1, 0))
        data.dwell[l+num_S] = int(TD.cell_value(l+1, 1))
        data.E_tr[l+num_S] = [max(0, int(TE.cell_value(l+20, i))) for i in range(1, data.travel[l+num_S] + 1)]
        data.E_rg[l+num_S] = [max(0, -int(TE.cell_value(l+20, i))) for i in range(1, data.travel[l+num_S] + 1)]
        data.D_tr[l+num_S] = sum(i > 0 for i in data.E_tr[l+num_S])
        data.D_br[l+num_S] = sum(i > 0 for i in data.E_rg[l+num_S])
    for l in range(1,PA.nrows):
        if int(PA.cell_value(l,0))<=num_S and int(PA.cell_value(l,1))<=num_S:
            data.passenger[str(int(PA.cell_value(l,0))-1)+'-'+str(int(PA.cell_value(l,1))-1)]=[int(PA.cell_value(l,i)) for i in range(2, 2+num_T)]
    num_k_1=0
    while int(TU.cell_value(num_S-1,num_k_1+2))<num_T and num_k_1<(TU.ncols-2):
        num_k_1+=1
    num_k_2=0
    while int(TD.cell_value(num_S-1,num_k_2+2))<num_T and num_k_2<(TD.ncols-2):
        num_k_2+=1
    num_K=min(num_k_1,num_k_2)
    data.num_K=num_K
    data.timetable=[[] for i in range(num_K)]
    for k in range(num_K):
        tem_line=[0 for i in range(2*num_S)]
        tem_line[0:num_S]=[int(TU.cell_value(i, k+2)-1) for i in range(num_S)]
        tem_line[num_S:2*num_S] = [int(TD.cell_value(i, k + 2)-1) for i in range(num_S)]
        data.timetable[k]=tem_line

    return data

def get_eff_rolling(num_rolling_S,num_K,num_T, T_1, T_2, T_3, M):
    eff_rolling={}
    for s in range(num_rolling_S):
        for k in range(num_K+1):
            for kk in range(num_K+1):
                for t in range(num_T):
                    for tt in range(num_T):
                        dt=tt-t
                        if k<num_K+1 and kk<num_K+1:
                            if dt<T_1:
                                eff_rolling[str(s)+'_'+str(k)+'_'+str(kk)+'_'+str(t)+'_'+str(tt)]=M
                            elif dt<=T_2:
                                eff_rolling[str(s)+'_'+str(k)+'_'+str(kk)+'_'+str(t)+'_'+str(tt)]=0
                            elif dt>=T_3:
                                eff_rolling[str(s)+'_'+str(k)+'_'+str(kk)+'_'+str(t)+'_'+str(tt)]=6
                            else:
                                eff_rolling[str(s)+'_'+str(k)+'_'+str(kk)+'_'+str(t)+'_'+str(tt)]=M
                        else:
                            eff_rolling[str(s) + '_' + str(k) + '_' + str(kk) + '_' + str(t) + '_' + str(tt)] = 100
    return eff_rolling

def get_eff_E(num_K,set_S, num_T,data,candidate_T):
    dic_tr={}
    dic_rg={}
    for k in range(num_K):
        for s in set_S:
            for t in range(num_T):
                tem_energy_tr=[0 for tau in range(num_T)]
                for tt in range(t,min(num_T,t+len(data.E_tr[s]))):
                    tem_energy_tr[tt] = data.E_tr[s][tt - t]
                dic_tr[str(k) + '_' + str(s) + '_' + str(t-candidate_T[str(k)+'_'+str(s)][0])] = tem_energy_tr

    for k in range(num_K):
        for s in set_S:
            for t in range(num_T):
                tem_energy_rg = [0 for tau in range(num_T)]
                for tt in range(t,min(num_T,t+len(data.E_rg[s]))):
                    tem_energy_rg[tt] = data.E_rg[s][tt - t]
                dic_rg[str(k) + '_' + str(s) + '_' + str(t-candidate_T[str(k)+'_'+str(s)][0])] = tem_energy_rg
    return dic_tr,dic_rg

def get_candidta_T(data, num_S, headway_lower, headway_upper, dwell_lower):
    num_K=data.num_K
    candidate_T={}
    #首末车固定
    for s in range(num_S):
        candidate_T[str(0) + '_' + str(s)]=[data.timetable[0][s]]
        candidate_T[str(num_K-1) + '_' + str(s)] = [data.timetable[num_K-1][s]]
        candidate_T[str(num_K) + '_' + str(s)] = [0]
    #中间的车推导
    for k in range(1, num_K-1):
        for s in range(num_S):
            t_left=max(data.timetable[0][s]+k*(headway_lower+dwell_lower), data.timetable[num_K-1][s]-(num_K-1-k)*(headway_upper+dwell_lower))
            t_right=min(data.timetable[0][s]+k*(headway_upper+dwell_lower), data.timetable[num_K-1][s]-(num_K-1-k)*(headway_lower+dwell_lower))
            candidate_T[str(k)+'_'+str(s)]=[i for i in range(max(t_left,data.timetable[k][s]-5), min(t_right+1, data.timetable[k][s]+5))]

    return candidate_T

def get_candidate_service(data, candidate_T, num_K, set_S_over,set_S_fan):
    candidate_service={}
    for k in range(num_K):
        for s in set_S_over:
            tem=[]
            for kk in range(num_K):
                # for ss in set_S_op:
                for ss in [s,set_S_fan[s]]:
                    #如果牵引最大<=制动最小，牵引最小>=制动最大，则无反馈，反之则归入candidate
                    if max(candidate_T[str(kk)+'_'+str(ss)])+data.D_tr[ss]>=min(candidate_T[str(k)+'_'+str(s)])+data.travel[s]-data.D_br[s] and min(candidate_T[str(kk) + '_' + str(ss)]) + data.D_tr[ss] <= max(
                            candidate_T[str(k) + '_' + str(s)]) + data.travel[s] - data.D_br[s]:
                        tem.append([kk,ss])
            candidate_service[str(k)+'_'+str(s)]=tem
    return candidate_service

def timetable2x(data,num_S,num_T):
    x=[[[0 for i in range(num_T)] for s in range(num_S)] for k in range(data.num_K)]
    for k in range(data.num_K):
        for s in range(num_S):
            x[k][s][data.timetable[k][s]]=1
    return x
def x2timetable(x,num_S, num_K, num_T, candidate_T):
    timetable=[[0 for s in range(num_S)] for k in range(num_K)]
    for k in range(num_K):
        for s in range(num_S):
            for t in range(num_T):
                if x[k][s][t]==1:
                    timetable[k][s]+=t
    return timetable
def xvalue2timetable(x,num_S, num_K, num_T, candidate_T):
    timetable=[[0 for s in range(num_S)] for k in range(num_K)]
    for k in range(num_K):
        for s in range(num_S):
            for t in range(len(candidate_T[str(k) + '_' + str(s)])):
                if x[k][s][t]==1:
                    timetable[k][s]+=t+candidate_T[str(k) + '_' + str(s)][0]
    return timetable

def get_lamda_inital(data,set_S_over,num_K, candidate_service):
    lamda_inital={}
    for k in range(num_K):
        for s in set_S_over:
            for service in candidate_service[str(k) + '_' + str(s)]:
                kk = service[0]
                ss = service[1]
                tem=[0 for i in range(4)]
                #condition 3
                if data.timetable[k][s] + data.travel[s] - data.D_br[s] <= data.timetable[kk][ss] and \
                        data.timetable[k][s] + data.travel[s] - data.D_tr[ss] >= data.timetable[kk][ss]:
                    tem[2]=1
                    if tem[2]==0:
                        if data.timetable[k][s] + data.travel[s] - data.D_br[s] >= data.timetable[kk][ss] and \
                                data.timetable[k][s] + data.travel[s] - data.D_tr[ss] <= data.timetable[kk][ss]:
                            tem[3] = 1
                            if tem[3]==0:
                                if data.timetable[k][s] + data.travel[s] - data.D_br[s] + max(data.D_br[s]- data.D_tr[ss],0) < \
                                        data.timetable[kk][ss] and data.timetable[k][s] + data.travel[s] > \
                                        data.timetable[kk][ss]:
                                    tem[0] = 1
                                    if tem[0]==0:
                                        if data.timetable[k][s] + data.travel[s] - data.D_br[s] + min(data.D_br[s]- data.D_tr[ss],0) > \
                                                data.timetable[kk][ss] and data.timetable[k][s] + data.travel[s] - \
                                                data.D_br[s] - data.D_tr[ss] < data.timetable[kk][ss]:
                                            tem[1] = 1
                lamda_inital[str(k) + '_' + str(s) + '_' + str(kk) + '_' + str(ss)]=tem

    return lamda_inital


import os, shutil, fire
from typing import List, Optional
from pyfiglet import Figlet

def main(path: Optional[str] = None) -> None:
    __print_logo('url - desktop')
    fire.Fire(__convert)

def __convert(path: Optional[str] = None) -> None:
    if path is None:
        path = os.getcwd()

    try:
        url_list_path = __get_url_files_path(path)
        exit_if_no_files(url_list_path)
        url_filenames = __get_url_filenames_without_extension(url_list_path)
        old_url_folder_name = __create_old_url_folder(path, url_list_path)
        __copy_url_files_to_old_url_folder(
            path, url_list_path, old_url_folder_name)
        __change_url_to_desktop(url_list_path, url_filenames)
        print('\nSuccessful conversion!\n')
    except Exception as e:
        print(f'Error: {str(e)}')

def __print_logo(text_logo: str) -> None:
    figlet = Figlet(font='slant')
    print(figlet.renderText(text_logo))

def __get_url_files_path(path: str) -> List[str]:
    url_list_path = list()
    for file in os.listdir(path):
        if file.endswith(".url") or file.endswith(".URL"):
            url_list_path.append(os.path.join(path, file))
    return url_list_path

def exit_if_no_files(url_list_path: List[str]) -> None:
    if len(url_list_path) <= 0:
        print('No URL to convert!')
        exit()

def __get_url_filenames_without_extension(
    url_list_path: List[str]
) -> List[str]:
    url_filenames = list()
    for path in url_list_path:
        url_filenames.append(os.path.splitext(os.path.basename(path))[0])
    return url_filenames

def __create_old_url_folder(
    path: str,
    url_list_path: List[str]
) -> str:
    if path is None:
        path = os.getcwd()

    old_url_folder_name = 'old urls'
    if not os.path.isdir(os.path.join(path, old_url_folder_name)):
        os.mkdir(os.path.join(path, old_url_folder_name))
    return old_url_folder_name

def __copy_url_files_to_old_url_folder(
    path: str,
    url_list_path: List[str],
    old_url_folder_name: str
) -> None:
    for file_path in url_list_path:
        shutil.copy(file_path, os.path.join(path, old_url_folder_name))

def __change_url_to_desktop(
    url_list_path: List[str],
    url_filenames: List[str]
) -> None:
    for file_path, filename in zip(url_list_path, url_filenames):
        # Take the URL from file
        url_file = open(file_path, 'r')
        for i, line in enumerate(url_file):
            if i == 1:
                url = line
        url_file.close()

        # Verify if we got the URL
        if not url.startswith('URL'):
            break

        # Convert to DESKTOP
        url_file = open(file_path, 'w')
        url_file.write('[Desktop Entry]\n')
        url_file.write('Encoding=UTF-8\n')
        url_file.write(f'Name={filename}\n')
        url_file.write('Type=Link\n')
        url_file.write(url)
        url_file.write('Icon=text-html\n')
        url_file.close()

        # Rename the file from .url to .desktop
        os.rename(
            file_path, f'{os.path.join(os.path.dirname(file_path),filename)}.desktop')


import enum
import functools
import re
import time
import warnings

from catkit.hardware.pyvisa_instrument import CommandEchoError, DEFAULT_POLL_TIMEOUT, PyVisaInstrument
import catkit.util
import numpy as np
import pyvisa

DEFAULT_POLL = 0.8  # As documented in manual (seconds).

class ASCIIControlCodes(enum.Enum):
    ENTER_CMD_MODE = " "  # AKA INIT.
    # INIT = ' '
    EXIT_CMD_MODE = chr(3)  # AKA RESET.
    # RESET = chr(3)  # Stops motion, sets counter to 0 (so man says, but doesn't), assumes idle state.
    SOFT_STOP = '@'  # Causes deacceleration to stop.
    DISABLE_HOME_SWITCH = "A0"
    ENABLE_HOME_SWITCH = "A8"
    ENABLE_HIGH_ACCURACY_HOMING = "A24"
    CLEAR = "C1"
    FIND_EDGE = 'F'
    # EXEC_INTERNAL_FUNC = 'G'
    START_VELOCITY = 'I'
    RAMP_SLOPE = 'K'
    PROGRAM_MODE = 'P'
    CONSTANT_VELOCITY_MOVE = 'M'
    SAVE = 'S'
    SCAN_SPEED = 'V'
    PAUSE = 'W'  # Wait/sleep.
    READ_PARAMS = 'X'
    GET_LIMIT_SWITCH_STATUS = ']'  # 0=no lim encountered, 32=home lim enc., 64=high lim enc., 128=low lim enc.
    SCAN_UP = '+'
    SCAN_DOWN = '-'
    GET_MOTION_STATUS = '^'  # 0=not moving, 1=moving, 2=high in const vel, 16=slewing ramping complete.

class LimitSwitchStatus(enum.Enum):
    # NOTE: It's possible to be both HOME & LOW as the HOME region is wide enough to include the LOW limit.
    INBETWEEN = 0  # Not at any of the below. NOTE: Could be HOME but requires home switch to be enabled.
    HOME = 32  # NOTE: Requires home switch to be enabled.
    HIGH = 64
    LOW = 128

class MotionStatus(enum.Enum):
    STOPPED = 0
    MOVING = 1
    CONSTANT_VELOCITY = 2
    SLEWING_RAMPING_COMPLETE = 16

class Parameters(enum.Enum):
    RAMP_SLOPE = ASCIIControlCodes.RAMP_SLOPE.value
    START_VELOCITY = ASCIIControlCodes.START_VELOCITY.value
    SCAN_SPEED = ASCIIControlCodes.SCAN_SPEED.value

class McPherson789A4(PyVisaInstrument):
    """ Base class for Mcpherson 789A-4 scan controller.
        NOTE: This class has no homing functionality, for that use McPherson789A4WithLimitSwitches.
    """

    _HAS_LIMIT_SWITCHES = False  # Use McPherson789A4WithLimitSwitches if True.

    BAUD_RATE = 9600
    DATA_BITS = 8
    STOP_BITS = pyvisa.constants.StopBits.one
    PARITY = pyvisa.constants.Parity.none
    FLOW_CONTROL = pyvisa.constants.ControlFlow.xon_xoff
    ENCODING = "ascii"
    QUERY_DELAY = 0.8  # s
    QUERY_TIMEOUT = 2000  # ms
    WRITE_TERMINATION = '\r'
    READ_TERMINATION = "\r\n"

    MIN_SCAN_SPEED = 32
    MAX_SCAN_SPEED = 61440  # NOTE: Manual states 60000, but device's default is 61440.

    def initialize(self, visa_id, steps_per_rev=36000, timeout=None):
        self.visa_id = visa_id
        self.timeout = timeout
        self.instrument_lib = self.instrument_lib
        self.timeout = self.QUERY_TIMEOUT if timeout is None else timeout
        self._name = self.__class__.__name__

        self.steps_per_rev = steps_per_rev
        self.firmware_version = None

    def reset(self):
        """ Reset device as if it were power cycled. """

        # NOTE: We don't use self.command() here due to the response being dependent on existing state.

        cmd = ASCIIControlCodes.EXIT_CMD_MODE
        self.instrument.write(cmd.value)
        time.sleep(2)

        try:
            # There's no response if calling this whilst in "command mode".
            mirrored_cmd = self.instrument.read('\r')  # For whatever reason, the read term is different from other commands.

            if ASCIIControlCodes(mirrored_cmd) is not cmd:
                raise RuntimeError(f"The device responded with a command different from that sent. Expected: '{cmd}' got '{mirrored_cmd}'")
        except pyvisa.VisaIOError:
            pass
        finally:
            # NOTE: There's a bug in pyvisa where the above actually changes the read_termination for all other reads.
            self.instrument.read_termination = self.READ_TERMINATION

    def init(self):
        """ Initialize device ready for excepting commands. """

        # NOTE: Upon power up INIT will return the firmware version & a superfluous "#" (as a separate read).
        # However, any call after that returns only "#".

        resp = self.instrument.query(ASCIIControlCodes.ENTER_CMD_MODE.value).strip()

        if resp.startswith("v"):
            self.firmware_version = resp

            # Read the superfluous read associated with INIT.
            assert self.instrument.read().strip() == "#"
        elif resp == "#":
            pass
        else:
            raise RuntimeError(f"Unexpected response '{resp}'")

        time.sleep(1)

    def _open(self):
        """ Open connection to the device. This is called by Instrument.__enter__() such that the device is context
            managed.
        """

        # rm = self.instrument_lib.ResourceManager('@py')

        # Open connection.
        self.instrument = self.instrument_lib.open_resource(self.visa_id,
                                           baud_rate=self.BAUD_RATE,
                                           data_bits=self.DATA_BITS,
                                           flow_control=self.FLOW_CONTROL,
                                           parity=self.PARITY,
                                           stop_bits=self.STOP_BITS,
                                           encoding=self.ENCODING,
                                           timeout=self.timeout,
                                           query_delay=self.QUERY_DELAY,
                                           write_termination=self.WRITE_TERMINATION,
                                           read_termination=self.READ_TERMINATION)
        time.sleep(1)

        # For a more consistent and repeatable operation we attempt
        # to RESET prior to sending INIT. Doing so is needed when considering that the device could be moving upon
        # connection and resetting will stop any movement.
        self.reset()

        self.init()

        self.command(ASCIIControlCodes.DISABLE_HOME_SWITCH)
        return self.instrument

    def _close(self):
        if self.instrument:
            try:
                self.stop_motion()
                self.command(ASCIIControlCodes.DISABLE_HOME_SWITCH)
                self.reset()
            finally:
                self.instrument.close()

    @staticmethod
    def parse_command(message):

        data = None

        if isinstance(message, ASCIIControlCodes):
            return message, data

        if not isinstance(message, str):
            raise TypeError(f"Message must either be of type str or '{ASCIIControlCodes.__name__}'")

        resp_list = message.split()
        resp_length = len(resp_list)

        cmd = resp_list[0]

        if resp_length == 1:
            data = None
        elif resp_length == 2:
            data = resp_list[1].strip()
        elif resp_length > 2:
            data = resp_list[1:]
        else:
            raise RuntimeError(f"Incorrect response length: {resp_length}")

        return cmd, data

    def read_params(self):
        """ Read parameters ramp slope (K), initial velocity (I), & scan speed (V).
            Returns a dict of Parameter enums members.
        """

        cmd = ASCIIControlCodes.READ_PARAMS.value
        resp = self.instrument.query(cmd)

        if resp[0] != cmd:
            raise CommandEchoError(cmd, resp[0])

        resp = resp[1:]  # The first char is the mirrored cmd 'X' so remove it from data before parsing.
        resp = re.findall("[A-Z]+=[ ]*[0-9]+", resp)  # Example: resp = "K= 50 I= 1000 V= 61440"
        resp = [x.split("= ") for x in resp]
        resp = {Parameters(x[0]): int(x[1]) for x in resp}

        return resp

    def command(self, cmd, data=''):
        """ Send command to device. Sent in the format: f"{ASCIIControlCodes(cmd).value}{data}".

        :param cmd: ASCIIControlCodes, str - The command to send to the device.
        :param data: Any type with a __str__ method. (optional)
        """

        # Whilst it's possible to send a message like "30000" we'll restrict commanding to being explicit, i.e.,
        # "+30000". That way we can command-check.

        # Type check.
        cmd = ASCIIControlCodes(cmd)

        if not self._HAS_LIMIT_SWITCHES and cmd in (ASCIIControlCodes.ENABLE_HIGH_ACCURACY_HOMING,
                                                    ASCIIControlCodes.ENABLE_HOME_SWITCH,
                                                    ASCIIControlCodes.FIND_EDGE,
                                                    ASCIIControlCodes.GET_LIMIT_SWITCH_STATUS):
            raise NotImplementedError(f"This model does not support limit switches and the command: '{cmd}'")

        message_sent = f"{cmd.value}{data}"
        try:
            resp = self.instrument.query(message_sent)
        except Exception as error:
            raise RuntimeError(f"The command: '{cmd}' failed. (data: '{data}')") from error

        if cmd is ASCIIControlCodes.READ_PARAMS:
            cmd_resp, resp_data = resp[0], resp[1:]
        else:
            # Parse returned mirrored cmd from data (if any).
            cmd_resp, resp_data = (resp, '') if data else self.parse_command(resp)

        if message_sent != cmd_resp:
            # The controller replies back with the written cmd, failing to read this will cause communication to become out
            # of sync and thus cause future commands to fail to work (without raising an error).
            raise CommandEchoError(message_sent, cmd_resp)

        return resp_data

    def set_slew_speed(self, steps_per_second, rtol=0.02, throw=False):
        """ Set the speed used by self.slew().

        :param steps_per_second: int - velocity value to set.
        :param rtol: float - The motor velocity doesn't have integer fidelity and thus the motor velocity used may not
                             be exactly that requested. rtol is the relative tolerance used to compare the actual
                             velocity to that requested. If not within tolerance it will raise a warning, or raise an
                             exception if throw is True.
        :param throw: bool (optional) - If set velocity is not within tolerance raise an exception instead or a warning.

        :return: int - The actual velocity with which the motor will slew.
        """

        steps_per_second = int(np.abs(steps_per_second))

        if self.MIN_SCAN_SPEED < steps_per_second > self.MAX_SCAN_SPEED:
            raise ValueError(f"{self.MIN_SCAN_SPEED} > SCAN_SPEED < {self.MAX_SCAN_SPEED} NOT {steps_per_second} (steps/s).")

        self.command(ASCIIControlCodes.SCAN_SPEED, steps_per_second)

        # NOTE: The manual states that the actual scan speed may be different from that set.
        actual_scan_speed = self.read_params()[Parameters.SCAN_SPEED]

        if not np.isclose(actual_scan_speed, steps_per_second, rtol=rtol):
            msg = f"Set scan speed to {steps_per_second}, however, actual scan speed is {actual_scan_speed} (rtol={rtol})."
            if throw:
                raise ValueError(msg)
            else:
                warnings.warn(msg)

        return actual_scan_speed

    def get_motion_status(self):
        """ This never returns MotionStatus.Moving, if it is moving is returns in which
            mode it is moving.
        """

        resp = np.int32(self.command(ASCIIControlCodes.GET_MOTION_STATUS))

        if resp & np.int32(MotionStatus.CONSTANT_VELOCITY.value):
            status = MotionStatus.CONSTANT_VELOCITY
        elif resp & np.int32(MotionStatus.SLEWING_RAMPING_COMPLETE.value):
            status = MotionStatus.SLEWING_RAMPING_COMPLETE
        elif resp & np.int32(MotionStatus.MOVING.value):
            status = MotionStatus.MOVING
        else:
            status = MotionStatus.STOPPED

        return status

    def is_moving(self):
        """ Returns bool. """
        return self.get_motion_status() is not MotionStatus.STOPPED

    def poll_motion(self, break_conditions, *args, **kwargs):
        """ Poll device for motion status. See catkit.util.poll_status() for more info. """
        return catkit.util.poll_status(break_conditions, self.get_motion_status, *args, **kwargs)

    def await_stop(self, timeout=DEFAULT_POLL_TIMEOUT):
        """ Wait for device to indicate it has stopped moving. Poll the device at intervals of self.QUERY_DELAY until
        it has stopped.

        :param timeout: int, float (optional) - Raise TimeoutError if the devices hasn't stopped within timeout seconds.
                                                0, None, & negative values => infinite timeout.
        """
        self.poll_motion((MotionStatus.STOPPED,), timeout=timeout)

    def stop_motion(self, timeout=DEFAULT_POLL_TIMEOUT, wait=True):
        """ Send command to stop motor then (optionally) wait on confirmation that it has actually stopped.
        See self.await_stop() for timeout semantics.

        NOTE: If using this interactively after interrupting running control, you should call self.read_all_bytes()
              before and after calling this func to prevent errors caused by the interrupt, e.g., orphaned responses on
              the device.
        """

        self.command(ASCIIControlCodes.SOFT_STOP)
        if wait:
            self.await_stop(timeout=timeout)

    def start_motion(self, steps_per_second):
        """ Move motor at constant velocity until either hitting a high|low limit or motion is explicitly stopped.

        :param steps_per_second: int - Move motor with velocity := steps_per_second.
        """
        self.command(ASCIIControlCodes.CONSTANT_VELOCITY_MOVE, steps_per_second)

    def slew(self, steps, steps_per_second=None, reset_speed=True, wait=False, timeout=DEFAULT_POLL_TIMEOUT):
        """ Move motor by a number of steps at a given speed.

        :param: steps: int - The number of steps to move by. + values move "up", - values move "down". NOTE: May not
                             move by full amount if motor encounters a (high|low) limit stop.
        :param steps_per_second: int (optional) - Requested motor velocity. If `None` it uses the velocity set on the
                                                  device. NOTE: The speed that the motor moves at might not be exactly
                                                  that requested so check return value for actual motor velocity.
        :param reset_speed: bool (optional) - To slew at the requested velocity (if given) the velocity must be set on
                                              the device. This value will be persistent. If False allow value to persist
                                              otherwise reset velocity to that prior to calling this func.
                                              NOTE: Only available if wait is True.
        :param wait: bool (optional) - Whether to wait for motion to have stopped before returning from this func.
        :param timeout: int, float (optional) - Raise TimeoutError if the devices hasn't stopped within timeout
                                                seconds (only applies when `wait` is True.
                                                0, None, & negative values => infinite timeout.

        :return: int - steps_per_second, actual motor velocity.
        """

        if not isinstance(steps, int):
            raise TypeError(f"steps must be of type int not {type(steps)}.")

        if steps_per_second and not isinstance(steps_per_second, int):
            raise TypeError(f"steps_per_second must be of type int not {type(steps_per_second)}.")

        cmd = ASCIIControlCodes.SCAN_UP if steps >= 0 else ASCIIControlCodes.SCAN_DOWN

        initial_scan_speed = actual_scan_speed = self.read_params()[Parameters.SCAN_SPEED]
        velocity_changed = False

        # Set speed.
        if steps_per_second is not None and steps_per_second != actual_scan_speed:
                actual_scan_speed = self.set_slew_speed(steps_per_second)
                velocity_changed = True

        self.command(cmd, steps)

        if wait:
            self.await_stop(timeout=timeout)

            if reset_speed and velocity_changed:
                self.set_slew_speed(initial_scan_speed)

        return actual_scan_speed

    def home(self):
        """ This is implemented by the derived class below. """
        raise NotImplementedError("This model does not support limit switches and, therefore, has no homing feature.")

class McPherson789A4WithLimitSwitches(McPherson789A4):
    _HAS_LIMIT_SWITCHES = True
    DEFAULT_HOMING_TIMEOUT = 60 * 5  # Depending on the initial motor position and velocity, this may take a while.
    DEFAULT_HOMING_SPEED = 23000  # As documented in the manual.
    FAST_HOMING_SPEED = 3000000  # As documented in the manual.
    EDGE_DETECTION_SPEED = 1000  # As documented in the manual.

    def initialize(self, visa_id, steps_per_rev=36000, timeout=1, home_on_startup=True):
        super().initialize(visa_id, steps_per_rev=steps_per_rev, timeout=timeout)

        self.home_on_startup = home_on_startup
        if not self.home_on_startup:
            warnings.warn("It is strongly recommended to home on startup before proceeding with anything else!")

    def _open(self, *args, **kwargs):
        self.instrument = super()._open(*args, **kwargs)

        if self.home_on_startup:
            self.home()

        return self.instrument

    def get_limit_status(self):
        """ Query the device for limit status.

        :return: LimitSwitchStatus.LOW, LimitSwitchStatus.HIGH, LimitSwitchStatus.INBETWEEN. NOTE: This does not return
                 LimitSwitchStatus.HOME for that equivalent functionality call self.is_home().
        """
        resp = np.int32(self.command(ASCIIControlCodes.GET_LIMIT_SWITCH_STATUS))

        is_home = resp & np.int32(LimitSwitchStatus.HOME.value)

        if is_home:
            resp = resp - np.int32(LimitSwitchStatus.HOME.value)

        status = LimitSwitchStatus(resp)

        return status

    def poll_limit_status(self, break_conditions, *args, **kwargs):
        """ Poll device for limit status. See catkit.util.poll_status() for more info. """
        return catkit.util.poll_status(break_conditions, self.get_limit_status, *args, **kwargs)

    def is_home(self, toggle_home_switch=True):
        """ Query whether the device is in the HOME position/region.

        :param toggle_home_switch: bool (optional) - If the home switch is disabled it is not possible to query it. This
                                                     func will enable it if toggle_home_switch is True. It will also
                                                     disable it once queried. This functionality may not be desired when
                                                     e.g., homing, where the home switch must not be disabled.

        :return: bool. NOTE: If the home switch is disabled and toggle_home_switch=False this will always return False.
        """

        if toggle_home_switch:
            self.command(ASCIIControlCodes.ENABLE_HOME_SWITCH)

        try:
            resp = np.int32(self.command(ASCIIControlCodes.GET_LIMIT_SWITCH_STATUS))
            return bool(resp & np.int32(LimitSwitchStatus.HOME.value))
        finally:
            if toggle_home_switch:
                self.command(ASCIIControlCodes.DISABLE_HOME_SWITCH)

    def await_home(self, timeout=DEFAULT_POLL_TIMEOUT):
        """ Wait for device to indicate it is in the home position/region. Poll the device at intervals of
        self.QUERY_DELAY until it is in the home position/region.

        NOTE: The home switch must be explicitly enabled before calling this.

        :param timeout: int, float (optional) - Raise TimeoutError if the devices isn't at home within timeout seconds.
                                                0, None, & negative values => infinite timeout.
        """

        poll_func = functools.partial(self.is_home, toggle_home_switch=False)
        catkit.util.poll_status((True,), poll_func, timeout=timeout)

    def await_not_home(self, timeout=DEFAULT_POLL_TIMEOUT):
        """ Wait for device to indicate it is NO LONGER in the home position/region. Poll the device at intervals of
        self.QUERY_DELAY until it is NO LONGER in the home position/region.

        NOTE: The home switch must be explicitly enabled before calling this.

        :param timeout: int, float (optional) - Raise TimeoutError if the devices is still at home within timeout seconds.
                                                0, None, & negative values => infinite timeout.
        """

        poll_func = functools.partial(self.is_home, toggle_home_switch=False)
        catkit.util.poll_status((False,), poll_func, timeout=timeout)

    def find_edge(self, steps_per_second, timeout=DEFAULT_POLL_TIMEOUT):
        """ Find the upper edge of the home region. This is the actual "home" position.

        NOTE: This only moves in the up direction and, therefore, the motor must be below the edge, in the home region.
              Raises a RuntimeError ff not in the home region when called.

        :param steps_per_second: int - Velocity at which to move. This should be slow.
        :param timeout: int (optional) - The timeout passed to self.await_stop().
        """
        # NOTE: When edge is found the position is just + of the home region such that self.is_home := False.

        if not self.is_home(toggle_home_switch=False):
            raise RuntimeError("Home switch must be enabled and motor within the home region to find edge.")

        # Enable "high accuracy" circuit.
        try:
            self.command(ASCIIControlCodes.ENABLE_HIGH_ACCURACY_HOMING)
            self.command(ASCIIControlCodes.FIND_EDGE, steps_per_second)
            self.await_stop(timeout=timeout)
        finally:
            self.command(ASCIIControlCodes.DISABLE_HOME_SWITCH)

    def home(self, steps_per_second=DEFAULT_HOMING_SPEED, edge_detection_velocity=EDGE_DETECTION_SPEED,
             timeout=DEFAULT_HOMING_TIMEOUT, fast=False):
        """ Complete homing procedure to find the home position which is defined as the upper edge of the home region.

        NOTE: This procedure is as outlined in the manual.
        NOTE: When edge is found the position is just + of the home region such that self.is_home := False.
        NOTE: The Home region is wide and includes the low limit.
        NOTE: For steps_per_second = 3e6 it takes ~210s to move from the low limit to the high limit and ~117s from
              the high to home.
              For steps_per_second = 23000 it takes ~470s to move from the low limit to the high limit and ~260s
              from the high to home.
                  Suitable timeouts must therefore be used.

        :param steps_per_second: int (optional) - The velocity used when finding the home region.
        :param edge_detection_velocity: int (optional) - The velocity passed to self.find_edge().
        :param timeout: int, float (optional) - The timeout used to wait upon individual moves within this procedure.
                                                NOTE: It is NOT the timeout for the overall complete homing procedure.
        :param fast: bool (optional) - If True, overrides steps_per_second=self.FAST_HOMING_SPEED.
                                       TODO: The backlash section of this procedure needs calibrating and may fail.
        """
        if fast:
            steps_per_second = self.FAST_HOMING_SPEED

        if steps_per_second > self.DEFAULT_HOMING_SPEED:
            warnings.warn(f"Uncalibrated backlash for speed: {steps_per_second} (>{self.DEFAULT_HOMING_SPEED}). Homing may fail.")

        print("Homing in progress... (this may take a while)")  # TODO: Should be self.log().
        t0 = time.perf_counter()
        try:
            # Enable home circuit.
            self.command(ASCIIControlCodes.ENABLE_HOME_SWITCH)

            # Check whether already in home region and move accordingly.
            if self.is_home(toggle_home_switch=False):
                self.start_motion(steps_per_second=steps_per_second)
                self.await_not_home(timeout=timeout)
            else:
                self.start_motion(steps_per_second=-steps_per_second)
                self.await_home(timeout=timeout)

            # Stop.
            self.stop_motion()

            # The high accuracy edge detection circuit only moves in the + direction and, therefore, we need to be far
            # enough below the edge of the home position to give it room to work.

            # TODO: It is the following two steps that aren't calibrated for velocities other than that outlined in the
            #  manual. It may even fail when using the faster velocity stated in the manual - it did numerous times
            #  whilst testing.

            # Move further down into home region (3 motor revs).
            self.slew(-self.steps_per_rev*3, wait=True)

            # Remove some backlash from the last move (2 motor revs).
            self.slew(self.steps_per_rev*2, wait=True)

            # Find edge of homing switch.
            # NOTE: Depending on the speed used to find the home region, the last two steps may have actually moved the
            # motor beyond the home region which will cause self.find_edge() to raise.
            self.find_edge(steps_per_second=edge_detection_velocity, timeout=timeout)

            print(f"Homing complete. ({time.perf_counter() - t0}s)")  # TODO: Should be self.log().
        finally:
            self.command(ASCIIControlCodes.DISABLE_HOME_SWITCH)


import xlrd
import sys
import xlwt
from xlwt import easyxf
from pprint import pprint
import logging
logging.basicConfig(filename='firewall_2.log', level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

def getSubnetIPs(book = "", name = ""):
    xl_sheet = book.sheet_by_name(name)
    logging.info('Processing BDA sheet - {0}'.format(name))
    req_col_name_01 = "INTERFACE CATEGORY"
    req_col_name_02 = "Subnet Details"
    req_columns_found = False
    req_column_index_01 = 0
    req_column_index_02 = 0
    sheet_ips_list = []

    num_cols = xl_sheet.ncols   # Number of columns
    num_rows = xl_sheet.nrows   # Number of rows
    for row_idx in range(0, num_rows):    # Iterate through rows
        row_data = []
        for col_idx in range(0, num_cols):  # Iterate through columns
            cell_obj = xl_sheet.cell(row_idx, col_idx)  # Get cell object by row, col
            if cell_obj:
                row_data.append(str(cell_obj.value))
#                 pprint(row_data)
            else:
                row_data.append(str("NA"))
        if (req_col_name_01 in row_data) and (req_col_name_02 in row_data):     # Checking for the required value in the
            #print("Found.. in row - {0}".format(row_idx))
            req_column_index_01 = row_data.index(req_col_name_01)
            req_column_index_02 = row_data.index(req_col_name_02)
            req_columns_found = True
            continue
            #process further
        else:
            pass
            #print("Not Found.. in row - {0}".format(row_idx))

        if req_columns_found and req_column_index_01 and req_column_index_02:
            if row_data[req_column_index_02] and row_data[req_column_index_01]:
                if row_data[req_column_index_01].lower().startswith('customer'):
                    sheet_ips_list.append(row_data[req_column_index_02])

    return sheet_ips_list

def get_fe_be_mgmt_IPs(book = "", name = ""):
    xl_sheet = book.sheet_by_name(name)
    logging.info('Processing exdata sheet - {0}'.format(name))
    req_col_name_01 = "INTERFACE CATEGORY"
    req_col_name_02 = "Subnet Details"
    req_columns_found = False
    req_column_index_01 = 0
    req_column_index_02 = 0
    fe_ips_list = []
    be_ips_list = []
    mgmt_ips_list = []
    bdcs_mgmt_ip = []

    num_cols = xl_sheet.ncols   # Number of columns
    for row_idx in range(0, xl_sheet.nrows):    # Iterate through rows
        #print ('Row: %s' % row_idx)
        row_data = []
        for col_idx in range(0, num_cols):  # Iterate through columns
            cell_obj = xl_sheet.cell(row_idx, col_idx)  # Get cell object by row, col
#             pprint(cell_obj)
            if cell_obj:
                row_data.append(str(cell_obj.value))
#                 pprint(row_data)
            else:
                row_data.append(str("NA"))
        #print ('Row: [%s] data: [%s]' % (row_idx, ", ".join(row_data)))
        if (req_col_name_01 in row_data) and (req_col_name_02 in row_data):
            #print("Found.. in row - {0}".format(row_idx))
            req_column_index_01 = row_data.index(req_col_name_01)
            req_column_index_02 = row_data.index(req_col_name_02)
            req_columns_found = True
            continue
            #process further
        else:
            pass
            #print("Not Found.. in row - {0}".format(row_idx))

        if req_columns_found and req_column_index_01 and req_column_index_02:
            if row_data[req_column_index_02] and row_data[req_column_index_01]:
                if row_data[req_column_index_01].lower().startswith('customer') and row_data[req_column_index_01].lower().endswith('fe'):
                    fe_ips_list.append(row_data[req_column_index_02])
                elif row_data[req_column_index_01].lower().startswith('customer') and row_data[req_column_index_01].lower().endswith('be'):
                    be_ips_list.append(row_data[req_column_index_02])
                elif row_data[req_column_index_01].lower().startswith('customer') and row_data[req_column_index_01].lower().endswith('mgmt'):
                    mgmt_ips_list.append(row_data[req_column_index_02])
                elif row_data[req_column_index_01] == "BDCS Management":
                    bdcs_mgmt_ip.append(row_data[req_column_index_02])

    return fe_ips_list, be_ips_list, mgmt_ips_list, bdcs_mgmt_ip

def process_all_sheets():
    all_info = []
    for sheet in sheet_name:
        if sheet.endswith("BDA"):
            try:
                sheet_data = getSubnetIPs(book = book, name = sheet)
                all_info += sheet_data
            except Exception as e:
                logging.critical("Exception > process_all_sheets() for BDA -> {0}".format(e))
    fe_info = []
    be_info = []
    mgmt_info = []
    bdcs_mgmt_info = []
    for sheet in sheet_name:
        if sheet.lower().endswith("exdata"):
            #sheet_data = getSubnetIPs(book = book, name = sheet)
            #print(sheet)
            try:
                fe_data, be_data, mgmt_data, bdcs_mgmt = get_fe_be_mgmt_IPs(book = book, name = sheet)
                fe_info += fe_data
                be_info += be_data
                mgmt_info += mgmt_data
                bdcs_mgmt_info += bdcs_mgmt
            except Exception as e:
                logging.critical("Exception > process_all_sheets() for exdata -> {0}".format(e))
    #     else:
    #         logging.info("There is no sheets with the suffix exdata")

    EM_SM = ["nlcl423ru05.nldc1.oraclecloud.com","nlcl423ru06.nldc1.oraclecloud.com","nlcl423ru07.nldc1.oraclecloud.com","nlcl423ru11.nldc1.oraclecloud.com"]
    US_SM = ["chr302ru27.usdc2.oraclecloud.com","chr302ru26.usdc2.oraclecloud.com","chr302ru25.usdc2.oraclecloud.com","chr302ru22.usdc2.oraclecloud.com"]

    if all_info and bdcs_mgmt_info and mgmt_info and be_info and fe_info:
        logging.info("Generating output xlsx ...")
        style1=xlwt.easyxf('font: name Calibri, color-index black, bold on, height 280; pattern: pattern solid, fore_colour yellow')
        style2=xlwt.easyxf('align: wrap yes; font: name Calibri, height 220')
        workbook=xlwt.Workbook(encoding="utf-8")
        sheet1=workbook.add_sheet("firewall Sheet")
        sheet1.col(0).width = 8000
        sheet1.col(1).width = 7000
        sheet1.col(2).width = 3000
        sheet1.col(3).width = 7000
        sheet1.col(4).width = 7000
        sheet1.col(5).width = 9000
        sheet1.write(0,0, "Source IP", style1)
        sheet1.write(0,1, "IP Type", style1)
        sheet1.write(0,2, "Protocol", style1)
        sheet1.write(0,3, "Destination IP", style1)
        sheet1.write(0,4, "Port", style1)
        sheet1.write(0,5, "Description", style1)
        sheet1.write(1,0, "Internet/Customer", style2)
        sheet1.write(1,1, "Internernet source IP", style2)
        sheet1.write(1,2, "SSH", style2)
        sheet1.write(1,3, "\n".join(all_info), style2)
        sheet1.write(1,4, "22", style2)
        sheet1.write(1,5, "Customer ssh to bda vm", style2)
        sheet1.write(2,0, "Internet/Customer", style2)
        sheet1.write(2,1, "Internernet source IP", style2)
        sheet1.write(2,2, "SSH", style2)
        sheet1.write(2,3, "\n".join(fe_info), style2)
        sheet1.write(2,4, "22", style2)
        sheet1.write(2,5, "customer ssh access to Exadata VMs (client Interface)", style2)
        sheet1.write(3,0, "Internet/Customer", style2)
        sheet1.write(3,1, "Internernet source IP", style2)
        sheet1.write(3,2, "SSH", style2)
        sheet1.write(3,3, "\n".join(be_info), style2)
        sheet1.write(3,4, "22", style2)
        sheet1.write(3,5, "customer ssh access to Exadata VM (backup Interface)", style2)
        sheet1.write(4,0, "Internet/Customer", style2)
        sheet1.write(4,1, "Internernet source IP", style2)
        sheet1.write(4,2, "SSH", style2)
        sheet1.write(4,3, "\n".join(mgmt_info), style2)
        sheet1.write(4,4, "22", style2)
        sheet1.write(4,5, "customer ssh access to Exadata VM (mgmt Interface)", style2)
        sheet1.write(5,0, "Internet/Customer", style2)
        sheet1.write(5,1, "Internernet source IP", style2)
        sheet1.write(5,2, "HTTPS", style2)
        sheet1.write(5,3, "\n".join(all_info), style2)
        sheet1.write(5,4, "7183", style2)
        sheet1.write(5,5, "Customer access to Cloudera Manager UI Inside BDA Vms", style2)
        sheet1.write(6,0, "Internet/Customer", style2)
        sheet1.write(6,1, "Internernet source IP", style2)
        sheet1.write(6,2, "HTTPS", style2)
        sheet1.write(6,3, "\n".join(all_info), style2)
        sheet1.write(6,4, "8888", style2)
        sheet1.write(6,5, "customer access to Hue UI inside BDA VMs", style2)
        sheet1.write(7,0, "\n".join(all_info), style2)
        sheet1.write(7,1, "---", style2)
        sheet1.write(7,2, "---", style2)
        sheet1.write(7,3, "ALL", style2)
        sheet1.write(7,4, "ALL", style2)
        sheet1.write(7,5, "Unlimted access from Customer VM to Internet", style2)
        sheet1.write(8,0, "\n".join(EM_SM), style2)
        sheet1.write(8,1, "---", style2)
        sheet1.write(8,2, "SSH", style2)
        sheet1.write(8,3, "\n".join(all_info), style2)
        sheet1.write(8,4, "22", style2)
        sheet1.write(8,5, "SM ssh access to bdcs mgmt network", style2)
        sheet1.write(9,0, "\n".join(EM_SM), style2)
        sheet1.write(9,1, "---", style2)
        sheet1.write(9,2, "SSH", style2)
        sheet1.write(9,3, "\n".join(fe_info), style2)
        sheet1.write(9,4, "22", style2)
        sheet1.write(9,5, "SM MT1/MT2 ssh access to Exadata Customer VM", style2)

        workbook.save("Firewall.xls")
        logging.info("Fire wall sheet Generated Successfully...")

if len(sys.argv) == 2 and ( str(sys.argv[1]).endswith("xlsx") or str(sys.argv[1]).endswith("xls") ):
    try:
        book = xlrd.open_workbook(sys.argv[1])
        sheet_name = book.sheet_names()
        logging.info("Processing  started with the given input file" + "---->" + "'" + str(sys.argv[1]) + "'")
        logging.info("List of input sheets - {0}".format(sheet_name))
        logging.info("Number of sheets found in the file" + ":" + str(book.nsheets))

    except Exception as k:
        logging.critical("exception occurred - {0}".format(k))
        sys.exit()
else:
    logging.info("Execution format -> # python parse_xlsx.py <xlsx / xls file>")
    logging.info("Exiting the script input file format is not xlsx")
    sys.exit()

if __name__ == '__main__':
    try:
        process_all_sheets()
    except Exception as emsg:
        logging.critical("exception occurred - {0}".format(emsg))
# else:
#     logging.info("Execution format -> # python parse_xlsx.py <xlsx / xls file>")


import cv2
import numpy as np

# original = cv2.imread("14.jpg")
# duplicate = cv2.imread("862.jpg")

original = cv2.imread("1929.jpg")
duplicate = cv2.imread("1437.jpg")

original = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)
duplicate = cv2.cvtColor(duplicate, cv2.COLOR_BGR2GRAY)

height1, width1= original.shape
height2, width2 = duplicate.shape

# print(width1)
# print(width2)

# original = cv2.normalize(original, 0, 255, norm_type=cv2.NORM_MINMAX)
# duplicate = cv2.normalize(duplicate, 0, 255, norm_type=cv2.NORM_MINMAX)

# original = cv2.Sobel(original, cv2.CV_8U, 1, 0, ksize=3)
# duplicate = cv2.Sobel(duplicate, cv2.CV_8U, 1, 0, ksize=3)

retval, original = cv2.threshold(original, 150, 255, cv2.THRESH_BINARY)
retval, duplicate = cv2.threshold(duplicate, 150, 255, cv2.THRESH_BINARY)

# print(cv2.countNonZero(original))
# print(cv2.countNonZero(duplicate))

cv2.imshow("img1", original)
cv2.imshow("img2", duplicate)
# cv2.imshow("img4", original_1)

# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(original, None)
kp2, des2 = sift.detectAndCompute(duplicate, None)

# FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict()

flann = cv2.FlannBasedMatcher(index_params, search_params)

matches = flann.knnMatch(des1, des2, k=2)

print(len(kp1))
print(len(kp2))
print(len(matches))

good_points = []

# ratio test
for match1, match2 in matches:
    if match1.distance < 0.2 * match2.distance:
        good_points.append(match1)

print(len(good_points))
flann_matches = cv2.drawMatches(original, kp1, duplicate, kp2, good_points, None)

cv2.imshow("img3", flann_matches)

cv2.waitKey(0)
cv2.destroyAllWindows()

Find similarities between two images/2/project.py


import pytest
from com_hrbot_logger.log import *
from com_hrbot_constant.hrbot_config_mapper import *
from com_htbot_utility.utility import *
from com_htbot_utility.excel_reader import *
from analytics_module.conftest import *
import allure
from allure_commons.types import AttachmentType

logger = Log.get_logger()

@pytest.mark.usefixtures('setup')
class Test26:

    def test26_Verify_users_data_after_clicking_on_user_setting(self):
        driver  = self.driver
        try:
            logger.info('enter user\'s credentials to login page')
            element = self.driver.find_element_by_xpath(HrBot_Config_mapper.hrbot_locators.get("login_user_box"))
            element.send_keys(HrBot_Config_mapper.hrbot_config.get("username"))

            time.sleep(2)
            element = self.driver.find_element_by_xpath(HrBot_Config_mapper.hrbot_locators.get("login_pass_box"))
            element.send_keys(HrBot_Config_mapper.hrbot_config.get("password"))
            logger.info('User has successfully entered credentials')

            self.driver.find_element_by_xpath(HrBot_Config_mapper.hrbot_locators.get("login_button")).click()
            logger.info('User has successfully clicked on login button')

            logger.info('Waiting for the element')
            Utility.explicit_Wait1(HrBot_Config_mapper.hrbot_locators.get('conversation_reports'), 'visibility',  driver)

            url = self.driver.current_url
            if url:
                assert url == ExcelReader.read_excel().get("hrbot_homepage_url")
                logger.info('User has been navigated successfully to home page')
            else:
                logger.info('User is not navigated to home page')

            self.driver.find_element_by_xpath(HrBot_Config_mapper.hrbot_locators.get('users_management')).click()
            logger.info('User has successfully clicked on user\'s  module')

            url = self.driver.current_url
            url1 = ExcelReader.read_excel().get('hrbot_users_url')
            if url:
                assert url == url1.replace('\n', '')
                logger.info('User has been navigated successfully to user\'s page')
            else:
                logger.info('User is not navigated to user\'s page')

            Utility.explicit_Wait1(HrBot_Config_mapper.hrbot_locators.get('user_management'), 'visibility', driver)

            self.driver.find_element_by_xpath(HrBot_Config_mapper.hrbot_locators.get('total_users')).click()
            logger.info('User has clicked on total users tab successfully')

            time.sleep(2)
            try:
                users_number = self.driver.find_element_by_xpath(HrBot_Config_mapper.hrbot_locators.get('total_users_num')).text
                logger.info('Total users number has been fetched successfully how many users exist currently')
                print(users_number)
            except Exception as e:
                print('-----amjad-----', e)

            ls = ['user_name', 'email', 'role', 'region', 'active', 'created']
            dct = {}
            for i in range(1, 11):
                for j in range(1, 7):
                    print(str([j]))
                    time.sleep(1)
                    xpath = "//*[@class='row grid-odd no-gutters']"+ str([i]) +"/div[@class='pr-3 undefined']" +str([j])+ "/span"
                    print(xpath)
                    try:
                        data = self.driver.find_element_by_xpath("//*[@class='row grid-odd no-gutters']"+ str([i]) +"/div[@class='pr-3 undefined']" +str([j])+ "/span").text
                        dct[ls[j-1]] = data
                    except  Exception as e:
                        print('--------3-------', e)
                    print(dct)
                    logger.info('data has been displayed correctly as expected')

        except Exception as e:
            allure.attach(driver.get_screenshot_as_png(), 'screenshots', AttachmentType.PNG)
            logger.error('An Exception occurred in test case verify users data respectively 26')
            print(e)
            pytest.fail(e, pytrace=True)