from decimal import Decimal
from django.http import JsonResponse
from django.shortcuts import render_to_response, render, get_object_or_404
from django.middleware import csrf
from django.views.generic import DetailView, ListView, FormView

from deal.models import Deal
from utils.decorators.permission import perm_required
from mlm.forms import ManagerCreateAgentForm
from mlm.models import AgentPayment
from mlm.views.mlm_agent import MLMAgentView
from utils.remote_forms.forms import RemoteForm

class MLMCabinetManagerView(MLMAgentView):

    @perm_required('mlm.mlm_manager')
    def dispatch(self, request, *args, **kwargs):
        self.manager = request.user.person.mlm_agent
        return super().dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context.update(dict(
            title='Личный кабинет менеджера',
            month_set=self.month_set,
            permissions=self.permissions
        ))
        return context

    def get(self, request, *args, **kwargs):
        context = self.get_context_data()
        context.update(
            child_agents=self.get_child_agents()
        )
        if self.request.GET.get('debug'):
            return render_to_response('debug.jinja2', locals())
        return JsonResponse(context, safe=False)

class MLMCabinetManagerInviteView(ListView):
    permissions = []
    manager = None
    count = 0
    total = Decimal('0.00')

    @perm_required('mlm.mlm_manager')
    def dispatch(self, request, *args, **kwargs):
        self.model = Deal
        self.permissions = ['view']
        self.manager = request.user.person.mlm_agent
        return super().dispatch(request, *args, **kwargs)

    def get_queryset(self):
        queryset = self.model.objects.filter(mlm_agent=self.manager, step__name='done', cost__gt=0)
        return queryset.distinct()

    def get_items(self):
        items = []
        _queryset = self.get_queryset()
        self.count = _queryset.count()
        for item in _queryset:
            bonus = round(item.cost / 100 * self.manager.level_1, 2)
            self.total += bonus
            items.append(dict(
                id=item.id,
                cost=item.cost,
                start_datetime=item.start_datetime.strftime('%Y.%m.%d'),
                bonus=bonus
            ))
        return items

    def get(self, request, *args, **kwargs):
        if 'view' not in self.permissions:
            return JsonResponse(dict(answer='No permissions'), safe=False)

        context = dict(
            permissions=self.permissions,
            items=self.get_items(),
            count=self.count,
            total=self.total,
            headers=[
                dict(order=None, text='#', value='id'),
                dict(order=None, text='День сделки', value='start_datetime'),
                dict(order=None, text='Стоимость', value='cost'),
                dict(order=None, text='Начислено', value='bonus'),
            ]
        )
        if request.GET.get('debug'):
            from django.shortcuts import render_to_response
            return render_to_response('debug.jinja2', locals())

        return JsonResponse(context)

class MLMCabinetManagerPaymentsView(ListView):
    permissions = []
    manager = None
    count = 0
    total = Decimal('0.00')

    @perm_required('mlm.mlm_manager')
    def dispatch(self, request, *args, **kwargs):
        self.model = AgentPayment
        self.permissions = ['view']
        self.manager = request.user.person.mlm_agent
        return super().dispatch(request, *args, **kwargs)

    def get_queryset(self):
        queryset = self.model.objects.filter(agent=self.manager)
        return queryset.distinct()

    def get_items(self):
        items = []
        _queryset = self.get_queryset()
        self.count = _queryset.count()
        for item in _queryset:
            self.total += item.cost
            items.append(dict(
                id=item.id,
                cost=item.cost,
                created_at=item.created_at.strftime('%Y.%m.%d'),
            ))
        return items

    def get(self, request, *args, **kwargs):
        if 'view' not in self.permissions:
            return JsonResponse(dict(answer='No permissions'), safe=False)

        context = dict(
            permissions=self.permissions,
            items=self.get_items(),
            count=self.count,
            total=self.total,
            headers=[
                dict(order=None, text='#', value='id'),
                dict(order=None, text='День выплаты', value='created_at'),
                dict(order=None, text='Выплата', value='cost'),
            ]
        )
        if request.GET.get('debug'):
            from django.shortcuts import render_to_response
            return render_to_response('debug.jinja2', locals())

        return JsonResponse(context)

class MLMCabinetManagerCreateAgentView(FormView):
    form_class = ManagerCreateAgentForm
    action = None
    manager = None
    permissions = []

    @perm_required('mlm.mlm_manager')
    def dispatch(self, request, *args, **kwargs):
        self.action = kwargs.get('action')
        self.manager = request.user.person.mlm_agent
        self.permissions = ['view', 'add', 'change']
        return super().dispatch(request, *args, **kwargs)

    def get_form(self, form_class=None):
        if self.request.POST:
            self.request.POST._mutable = True
            self.request.POST['manager'] = self.manager
            print('self.request.POST', self.request.POST)
            return self.form_class(self.request.POST)
        return self.form_class()

    def post(self, request, *args, **kwargs):
        form = self.get_form()
        if form.is_valid():
            form.save()
            return JsonResponse(dict(
                # form=form,
                message=dict(type='success', text='Сохранено'),
            ))
        return JsonResponse(dict(
            form=dict(errors=form.errors)
        ))

    def get_context_data(self, **kwargs):
        context = dict(
            title='Регистрация партнера',
            csrf_token=csrf.get_token(self.request),
            # form=self.get_form(),
            form=RemoteForm(self.get_form(), csrf_token=csrf.get_token(self.request)).as_dict(),
            permissions=self.permissions
        )
        return context

    def get(self, request, *args, **kwargs):
        context = self.get_context_data()
        return JsonResponse(context)


import sqlite3
import pandas as pd
from .constants import CREATE_SUBMISSIONS_TABLE, CREATE_SQLITE_INDEX

class SQLiteConnection:
    def __init__(self, database):
        self.conn = sqlite3.connect(database)

    def __del__(self):
        self.conn.close()

    def read_db_into_pandas(self, limit=None):
        # Warning this is expensive on the memory
        query = "SELECT * from submissions"
        if limit is not None:
            query += f" LIMIT {limit}"
        df = pd.read_sql_query(query, self.conn)
        return df

    def create_tables(self):
        c = self.conn.cursor()
        c.execute(CREATE_SUBMISSIONS_TABLE)
        c.execute(CREATE_SQLITE_INDEX)
        c.close()
        self.conn.commit()

    def get_latest_submission(self):
        c = self.conn.cursor()
        c.execute("SELECT created_utc FROM submissions ORDER BY created_utc DESC LIMIT 1;")
        item = c.fetchone()
        c.close()
        return item

    def insert_batch(self, batch, key_tpl):
        """
        Stores batch to database
        :param batch: Batch of submissions
        :param key_tpl: ordered tuple of keys that are in the batch and should be stored
        :return:
        """
        c = self.conn.cursor()

        c.executemany(f"INSERT INTO submissions {str(key_tpl)} "
                      f"VALUES ({','.join(['?'] * len(key_tpl))});", batch)
        c.close()
        self.conn.commit()


from pyspark.sql import SparkSession
import datetime
import time

start=time.time()
spark = SparkSession.builder.appName("Query1_RDD").getOrCreate()

sc = spark.sparkContext

data= sc.textFile("hdfs://master:9000/project/yellow_tripdata_1m.csv"). \
				map(lambda line: (float(datetime.datetime.strptime(line.split(',')[1],"%Y-%m-%d %H:%M:%S").strftime("%H")),(float(line.split(',')[3]), float(line.split(',')[4]),1))). \
				filter(lambda line: (-75.0<=line[1][0] and line[1][0]<=-73.0) and (40.0<=line[1][1] and line[1][1]<=41.0)). \
				reduceByKey(lambda accum, data: (accum[0] + data[0], accum[1] + data[1], accum[2] + data[2])). \
				map(lambda x: (x[0],x[1][0]/x[1][2],x[1][1]/x[1][2]))

#data.cache()
for i in data.collect():
	print(i)

print("Execution Time: ", time.time()-start)


from collections import defaultdict
import numpy as np

def datatable_to_dict(table, job_id, db):
    """
    Reads dicts of numpy arrays from database
    args:
        table e.g dbLinkBudget.SAT
        job_id eg 1
        db eg. dbLinkBudget
    output is a dictionary of np arrays
    """
    keys = list(table.fields)
    keys.remove("Job_ID")
    keys.remove("id")
    mydict = defaultdict(list)
    row_list=db(table.Job_ID == job_id).iterselect(*keys).as_list()
    for row in range(0,len(row_list)):
        for key, value in row_list[row]['_extra'].items():
            mydict[key] = np.append(mydict[key],value)
    return mydict

def write_dict_to_table(table, dic, job_id, db):
    # type: (object, object, object) -> object
    """
    Used to read in dictionaries which contain
    np arrays created when reading from database

    Args:
        table: db table eg db.SAT
        dic: dictionary of arrays
        job_id: is the scenario ID

    """
    row = dic.fromkeys(dic)
    for v in range(dic.values()[0].size):
        for k in range(len(dic.keys())):
            row[dic.keys()[k]] = dic.values()[k][v]
        job_id_check = table.Job_ID == job_id
        if table is db.Gateway:
            table.update_or_insert((table.GW_ID == row['GW_ID']) & (job_id_check), Job_ID=job_id,
                                **row) #all these query if these fields are equal. If so then updates. Otherwise inserts
        elif table is db.SAT:
            table.update_or_insert((table.SAT_ID == row['SAT_ID']) & (job_id_check), Job_ID=job_id, **row)
        elif table is db.TRSP:
            table.update_or_insert((table.TRSP_ID == row['TRSP_ID']) & (job_id_check), Job_ID=job_id, **row)
        elif table is db.VSAT:
            table.update_or_insert((table.VSAT_ID == row['VSAT_ID']) & (job_id_check), Job_ID=job_id, **row)
        elif table is db.Earth_coord_GW:
            table.update_or_insert(
                (job_id_check) & (table.LON == row['LON']) & (table.LAT == row['LAT']) & (table.GW_ID == row['GW_ID']) & (
                    table.TRSP_ID == row['TRSP_ID']), Job_ID=job_id, **row)
        elif table is db.EARTH_coord_VSAT:
            table.update_or_insert((job_id_check) & (table.LON == row['LON']) & (table.LAT == row['LAT']) & (table.VSAT_ID == row['VSAT_ID']), Job_ID=job_id, **row)

        else:
            table.update_or_insert(Job_ID=job_id, **row)


from statistics import mode
from menu import products

def get_product_by_id(id: int):
    if not type(id) == int:
        raise TypeError("product id must be an int")

    for element in products:
        if element["_id"] == id:
            return element

    return {}

def get_products_by_type(food_type: str):

    if not type(food_type) == str:
        raise TypeError("product type must be a str")

    new_list = []

    for element in products:
        if element["type"] == food_type:
            new_list.append(element)
    print(new_list)
    return new_list

def add_product(products, **menu: dict):
    if len(products) > 0:
        menu["_id"] = len(products)+1
        products.append(menu)

        return menu

    menu["_id"] = 1
    products.append(menu)
    return menu

def menu_report():
    count_product = 0
    average_price = 0
    most_common_type = []

    for item in products:
            count_product +=1
            average_price += item['price']
            most_common_type.append(item['type'])

    most_common_type_aux = mode(most_common_type)
    average_price_aux = round(average_price/count_product, 2)
    return f"Products Count: {count_product} - Average Price: ${average_price_aux} - Most Common Type: {most_common_type_aux}"


from selenium import webdriver
import allure
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

# def test_open_page():
#     driver.get('https://openweathermap.org/')
#     driver.maximize_window()
#     assert 'openweathermap' in driver.current_url
#     print('browser.current_url')

@allure.suite('Forms')
def test_search_city_field():
    driver.get('https://openweathermap.org/')
    driver.maximize_window()
    search_city_field = driver.find_element(By.CSS_SELECTOR, "input[placeholder='Search city']")
    search_city_field.send_keys('New York')
    search_button = driver.find_element(By.CSS_SELECTOR,'button[class="button-round dark"]')
    time.sleep(5)
    search_button.click()
    time.sleep(5)
    search_option = driver.find_element(By.CSS_SELECTOR,'.search-dropdown-menu>li:nth-child(1)')
    time.sleep(5)
    search_option.click()
    expected_city = 'New York City, US'
    time.sleep(5)
    displayed_city = WebDriverWait(driver,15).until(EC.presence_of_element_located((By.XPATH, '//*[@id="weather-widget"]/div[2]/div[1]/div[1]/div[1]/h2')))
    displayed_city_text = displayed_city.text
    print(displayed_city_text)
    assert expected_city == displayed_city_text


from django.shortcuts import render
from django.http import HttpResponse
from django.conf import settings
from ws4redis.redis_store import RedisMessage
from ws4redis.publisher import RedisPublisher
import pdb
import json

from .models import Video
from .tasks import sync_channel_videos,test
import sync

# Create your views here.
def index(request):
    #get access_token
    if not "access_token" in request.session:
        access_token,refresh_token = sync.get_access_token(sync.youku_user_dict)
        request.session["access_token"] = access_token
        request.session["refresh_token"] = refresh_token
    #get user playlists
    playlist_ids = list()
    playlists = dict()
    for video in Video.objects.all():
        if not video.playlist_id in playlist_ids:
            playlist_ids.append(video.playlist_id)
            playlists[video.playlist_id] = list()
    for playlist_id in playlist_ids:
        for video in Video.objects.all():
            if video.playlist_id == playlist_id:
                playlists[playlist_id].append(video.video_title)
    protocol = request.is_secure() and 'wss://' or 'ws://'
    WEBSOCKET_URI =protocol + request.get_host() + settings.WEBSOCKET_URL
    context = {"playlists": playlists,"WEBSOCKET_URI":WEBSOCKET_URI,"WS4REDIS_HEARTBEAT":settings.WS4REDIS_HEARTBEAT}
    return render(request,"sync_subscription/index.html",context)

#get youku existing videos and check for that are from youtube
#and show videos that are published failed
def check_youku_existing_youtube_video(request):
    if not "access_token" in request.session:
        access_token,refresh_token = sync.get_access_token(sync.youku_user_dict)
    playlists= sync.get_playlist(sync.youku_user_dict,request.session["access_token"],request.session["refresh_token"])
    response = {"playlists": playlists}
    request.session["playlists"] = playlists
    data = json.dumps(response)
    return HttpResponse(data,content_type='application/json')

#get youku video for each playlist
def get_youku_videos(request):
    playlist_id = request.GET["playlist_id"]
    if not "access_token" in request.session:
        access_token,refresh_token = sync.get_access_token(sync.youku_user_dict)
    videos = sync.get_playlist_videos(sync.youku_user_dict,playlist_id,request.session["access_token"],request.session["refresh_token"],request.session["playlists"])
    response = {"videos":videos}
    return HttpResponse(json.dumps(response),content_type='application/json')
#delete videos
def delete_videos(request):
    video_ids = request.POST.getlist("video_ids[]")
    playlist_ids = request.POST.getlist("playlist_ids[]")
    if not "access_token" in request.session:
        access_token,refresh_token = sync.get_access_token(sync.youku_user_dict)
    deleted_video_ids = sync.delete_videos(video_ids,playlist_ids,sync.youku_user_dict,request.session["access_token"],request.session["refresh_token"])

    if deleted_video_ids != "1002" and len(deleted_video_ids) == len(video_ids):
        return HttpResponse(json.dumps({"result":"success"}),content_type='application/json')
    else:
        return HttpResponse(json.dumps({"result":"failed"}),content_type='application/json')

def search_youtube_channel(request):
    query = request.GET["query"]
    search_type = request.GET["search_type"]
    if "result_more" in request.GET:
        channels,next_page_token = sync.youtube_search(query,search_type,request.session["next_page_token"],sync.google_user_dict)
        if next_page_token != "none":
            request.session["next_page_token"] = next_page_token
            return HttpResponse(json.dumps({"channels":channels}),content_type='application/json')
        else:
            return HttpResponse(json.dumps({"result":"no_more_results"}),content_type='application/json')
    else:
        channels,next_page_token = sync.youtube_search(query,search_type,"none",sync.google_user_dict)
        request.session["next_page_token"] = next_page_token
        return HttpResponse(json.dumps({"channels":channels}),content_type='application/json')

def get_channel_video_number(request):
    channel_id = request.GET["channel_id"]
    if "published_after" in request.GET:
        video_number = sync.get_channel_video_number(channel_id,request.GET["published_after"],sync.google_user_dict)
        return HttpResponse(json.dumps({"video_number":video_number}),content_type='application/json')

def sync_channel(request):
    if not "access_token" in request.session:
        access_token,refresh_token = sync.get_access_token(sync.youku_user_dict)
    channel_id = request.POST["channel_id"]
    published_after = request.POST["published_after"]
    sync_playlist_bool = request.POST["sync_playlist_bool"]
    videos = sync.get_channel_videos(channel_id,published_after,sync.google_user_dict)
    #redis_publisher = RedisPublisher(facility='uploading', broadcast=True)
    #uploading_status = RedisMessage("hello")
    #redis_publisher.publish_message(uploading_status)
    videos_trunked = trunks(videos,10) #split video_ids into 10 length trunk
    for videos_trunk in videos_trunked:
       sync_channel_videos.delay(videos_trunk,sync.youku_user_dict,sync.google_user_dict,request.session["access_token"],request.session["refresh_token"])

    return HttpResponse(json.dumps({"videos":videos}),content_type='application/json')

def trunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in xrange(0, len(l), n):
        yield l[i:i+n]


from django.contrib import admin
from .models import LoginAttempt,BlackList

# Register your models here.
class LoginAttemptAdmin(admin.ModelAdmin):
    list_display = [
        "id",
        "username",
        "ip_address",
        "path",
        "created_date",
    ]

admin.site.register(LoginAttempt,LoginAttemptAdmin)
admin.site.register(BlackList)


from typing import List, Tuple
from Levenshtein import distance
from itertools import combinations
from unidecode import unidecode

class Utils:
    @staticmethod
    def distance(word1, word2):
        word1 = unidecode(word1)
        word2 = unidecode(word2)
        max_score = 0
        if not word2: return max_score
        word2_split = word2.split(' ')
        len1 = len(word1.split(' '))
        len2 = len(word2_split)
        if len1 > len2:
            return 0
        for word in combinations(word2_split, len1):
            word = ' '.join(word)
            dist = distance(word1, word)
            score = 1 - dist/max(len(word), len(word1))
            max_score = max(max_score, score)
        return max_score

    @staticmethod
    def find_closest_n(search: str, list_to_match: List[Tuple[str, str]], n: int=1, min_sim: float=0.7) -> List[str]:

        item_distances = [(_id, name, Utils.distance(search, name))
                            for (_id, name) in list_to_match]
        item_distances.sort(key=lambda x: x[2], reverse=True)
        return [_id for _id, _, dist in item_distances[:n] if dist >= min_sim]


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse

from .const import VERSION, OS, KICOMAV, VT_API_KEY, KIBANA, CLAMAV
from tools.vT import ipReport, domainReport, urlScan, urlReport, fileScan, fileReport
import os

# Create your views here.
#Index
def index(request):
    return render(request, 'index.html', {'index' : 'active', \
    										'version' : VERSION, \
    										'os' : OS, 'kibana' : KIBANA})

#KicomAV and ClamAV
def scan(request):
	if(request.method == 'POST'):
		path = request.POST['path']
		opt = request.POST['options']
		kicom = KICOMAV + " " + path + " " + opt
		clam = CLAMAV + path
		resultKicom = os.popen(kicom).read()
		resultClam = os.popen(clam).read()
		return render(request, 'scan.html', {'scan' : 'active', 'cDir': os.getcwd(), \
												'resultKicom': resultKicom, 'resultClam': resultClam })
	else:
		return render(request, 'scan.html', {'scan' : 'active', 'cDir': os.getcwd()})

#VTScan
def vtscan(request):
	if(request.method == 'POST'):
		#result = request.POST['mode'] + " " + request.POST['data']
		if(request.POST['mode'] == 'ip'):
			ip = request.POST['data']
			result = ipReport(ip, VT_API_KEY)
			return JsonResponse(result, safe=False)

		elif(request.POST['mode'] == 'domain'):
			domain = request.POST['data']
			result = domainReport(domain, VT_API_KEY)
			return JsonResponse(result, safe=False)

		elif(request.POST['mode'] == 'url'):
			url = request.POST['data']
			scan = urlScan(url, VT_API_KEY)
			result = urlReport(scan['scan_id'], VT_API_KEY)
			return JsonResponse(result, safe=False)

		elif(request.POST['mode'] == 'file'):
			file = request.FILES['file']
			scan = fileScan(file, VT_API_KEY)
			result = fileReport(scan['sha1'], VT_API_KEY)
			return JsonResponse(result, safe=False)
	else:
		return render(request, 'virustotal.html', {'vtscan' : 'active'})

#Command
def cmd(request, path=None):
	cmd = KICOMAV + " " + path
	result = os.popen(cmd).read()
	return HttpResponse(result)


from django.shortcuts import render
from .models import Banks,Branches
from .forms import IfscForm, BranchForm

def index(request):
    return render(request, 'Bank/index.html')

def get_name(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form1 = IfscForm(request.POST)
        # check whether it's valid:
        if form1.is_valid():
            obj = Branches.objects.all().filter(ifsc=form1.cleaned_data['Bank_ifsc'])
            my_dic = {'banks': obj}
            return render(request, 'Bank/results.html', my_dic)
    else:
        form1 = IfscForm()

    return render(request, 'Bank/form_page.html', {'form1': form1})

def branch_name(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:

        form2 = BranchForm(request.POST)
        # check whether it's valid:
        if  form2.is_valid():
            # a = Bank.objects.all().filter(city=form2.cleaned_data['Bank_city'])
            # my_dic = {'e': a }
            obj = Branches.objects.all().filter(city=form2.cleaned_data['Bank_city'])
            my_dic = {'bank': obj }
            return render(request, 'Bank/results1.html', my_dic)
    else:

        form2 = BranchForm()
    return render(request, 'Bank/form_page.html', {'form2': form2})


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
from PySide2.QtWidgets import QWidget, QApplication
from PySide2.QtCore import QPoint, QPropertyAnimation

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(500, 500)
        self.child = QWidget(self)
        self.child.setStyleSheet("background-color: green; border-radius: 30%;")
        self.child.resize(60, 60)
        self.anim = QPropertyAnimation(self.child, b"pos")
        self.anim.setDuration(1500)

    def mousePressEvent(self, event):
        self.anim.setEndValue(QPoint(event.x() - 25, event.y() - 25))
        self.anim.start()

def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

Задание 5.py

Задание 5.py


import tensorflow as tf
import cv2
import numpy as np
from tensorflow import keras
import time

SSD_MOBILENET_V2_SAVED_MODEL_PATH="/home/pi/Desktop/My_Server/backend/models/"
category_map = {
    1: 'person',
    2: 'bicycle',
    3: 'car',
    4: 'motorcycle',
    5: 'airplane',
    6: 'bus',
    7: 'train',
    8: 'truck',
    9: 'boat',
    10: 'traffic light',
    11: 'fire hydrant',
    13: 'stop sign',
    14: 'parking meter',
    15: 'bench',
    16: 'bird',
    17: 'cat',
    18: 'dog',
    19: 'horse',
    20: 'sheep',
    21: 'cow',
    22: 'elephant',
    23: 'bear',
    24: 'zebra',
    25: 'giraffe',
    27: 'backpack',
    28: 'umbrella',
    31: 'handbag',
    32: 'tie',
    33: 'suitcase',
    34: 'frisbee',
    35: 'skis',
    36: 'snowboard',
    37: 'sports ball',
    38: 'kite',
    39: 'baseball bat',
    40: 'baseball glove',
    41: 'skateboard',
    42: 'surfboard',
    43: 'tennis racket',
    44: 'bottle',
    46: 'wine glass',
    47: 'cup',
    48: 'fork',
    49: 'knife',
    50: 'spoon',
    51: 'bowl',
    52: 'banana',
    53: 'apple',
    54: 'sandwich',
    55: 'orange',
    56: 'broccoli',
    57: 'carrot',
    58: 'hot dog',
    59: 'pizza',
    60: 'donut',
    61: 'cake',
    62: 'chair',
    63: 'couch',
    64: 'potted plant',
    65: 'bed',
    67: 'dining table',
    70: 'toilet',
    72: 'tv',
    73: 'laptop',
    74: 'mouse',
    75: 'remote',
    76: 'keyboard',
    77: 'cell phone',
    78: 'microwave',
    79: 'oven',
    80: 'toaster',
    81: 'sink',
    82: 'refrigerator',
    84: 'book',
    85: 'clock',
    86: 'vase',
    87: 'scissors',
    88: 'teddy bear',
    89: 'hair drier',
    90: 'toothbrush'
}
img_width=300
img_height=300
image = cv2.imread("/home/pi/Desktop/happyPeople.jpg")

class Detector:
    def __init__(self, model_path, category_map, model):
        start_time = time.time()
        #self.model = tf.saved_model.load(model_path)
        #self.model = tf.lite.Interpreter(model_path="/home/pi/Desktop/My_Server/backend/models/tf_lite/model.tflite")
        current_time = time.time()
        elapsed_time = current_time - start_time
        print("Time loading the model: " + str(elapsed_time))
        #self.model = tf.lite.Interpreter(model_path="/home/pi/Desktop/My_Server/backend/models/tf_lite/model.tflite")
        self.category_map = category_map

    def detect(self, image, img_width, img_height):
        #model_SSD = tf.keras.models.load_model(SSD_MOBILENET_V2_SAVED_MODEL_PATH)
        #model_SSD = keras.models.load_model(SSD_MOBILENET_V2_SAVED_MODEL_PATH)
        #frame = image.copy()
        #frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        #img = cv2.resize(frame_rgb, (img_width, img_height))
        '''
        print("frame_resized: ")
        print(frame_resized.shape)
        frame_suited=np.expand_dims(frame_resized, axis=0)
        print("frame_suited: ")
        print(frame_suited.shape)
        #summary=model_SSD.summary()
        #print(summary)
        output = model_SSD.predict(frame_suited)
        print(outputff)
        # load model
        '''
        #print(list(loaded.signatures.keys()))  # ["serving_default"]
        '''
        infer = self.model.signatures["serving_default"]#{'output_0': TensorSpec(shape=(1, 2), dtype=tf.float32, name='output_0')}

        # convert img to tf
        x = tf.keras.preprocessing.image.img_to_array(image, dtype='uint8')
        x = tf.keras.applications.mobilenet.preprocess_input(
            x[tf.newaxis,...]).astype(dtype='uint8')
        # (1,3,network_size, network_size)

        labeling = infer(tf.constant(x))
        return labeling
        '''
        '''
        print(labeling)

        img_width=1000
        img_height=1000
        final_image = cv2.resize(img, (1000, 1000))

        for box, score, detection_class in zip(labeling['detection_boxes'][0], labeling['detection_scores'][0], labeling['detection_classes'][0]):
            if score > 0.1:
                font = cv2.FONT_HERSHEY_SIMPLEX
                print('detection_class: ' + str(detection_class.numpy()))
                #print('detection_class type: ' + type(detection_class))
                cv2.putText(final_image, category_map[int(detection_class)], (int(box[1]*img_width), int(box[0]*img_height)), font, 3, (0, 255, 0), 2, cv2.LINE_AA)
                final_image = cv2.rectangle(final_image, (int(box[1]*img_width), int(box[0]*img_height)), (int(box[3]*img_width), int(box[2]*img_height)), (0, 255, 0), 2)

        cv2.imshow("Window", final_image)

        for sr, box in zip(score_rescaled, boxes):
            print('PREDICTED LABEL: ' + LABELS[box.label])
            xmin = self.adjust_minmax(int(box.xmin * IMAGE_W), IMAGE_W)
            ymin = self.adjust_minmax(int(box.ymin * IMAGE_H), IMAGE_H)
            xmax = self.adjust_minmax(int(box.xmax * IMAGE_W), IMAGE_W)
            ymax = self.adjust_minmax(int(box.ymax * IMAGE_H), IMAGE_H)
            if LABELS[box.label] == 'person':
                predicted_boxes.append(
                    {'name': LABELS[box.label], 'score': box.get_score(),
                                                    'xmin': xmin, 'ymin': ymin, 'xmax': xmax, 'ymax': ymax})
                                                    '''
        # Use keypoints if available in detections

        #predict_class = np.argmax(labeling['output_0'].numpy())
        #print(predict_class ) # int, depends on your task -- mine was img classfication
        #self.model_SSD.setInput(cv2.dnn.blobFromImage(frame_resized, size=(img_w, img_h), swapRB=True))
        #output = self.model_SSD.forward()
        '''
        x = tf.keras.preprocessing.image.img_to_array(image, dtype='uint8')
        x = tf.keras.applications.mobilenet.preprocess_input(
        x[tf.newaxis,...]).astype(dtype='uint8')
        output = self.model.predict(cv2.dnn.blobFromImage(frame_resized, size=(img_w, img_h), swapRB=True))
        print(output)
        return output
        '''
        '''
        detector = hub.load("https://tfhub.dev/tensorflow/ssd_mobilenet_v2/2")
        detector_output = detector(frame_suited)
        class_ids = detector_output["detection_classes"]
        print(class_ids)
        '''
        '''
        # Load TFLite model and allocate tensors.
        interpreter = tf.lite.Interpreter(model_path="/home/pi/Desktop/My_Server/backend/models/tf_lite/model.tflite")
        '''
        '''
        self.model.allocate_tensors()

        # Get input and output tensors.
        input_details = self.model.get_input_details()
        output_details = self.model.get_output_details()

        # Test model on random input data.
        input_shape = input_details[0]['shape']
        print("input_details: ")
        print(input_details)
        print("input_blob: ")
        #input_blob = cv2.dnn.blobFromImage(image, size=(300, 300), swapRB=True)
        x = tf.keras.preprocessing.image.img_to_array(image, dtype='uint8')
        x = tf.keras.applications.mobilenet.preprocess_input(
        x[tf.newaxis,...]).astype(dtype='uint8')
        #print(input_blob.shape)
        #input_data = np.array(np.random.random_sample(input_shape), dtype=np.uint8)
        #input_data = np.array(cv2.dnn.blobFromImage(frame_resized, size=(300, 300), swapRB=True), dtype=np.uint8)
        self.model.set_tensor(input_details[0]['index'], x)

        self.model.invoke()

        # The function `get_tensor()` returns a copy of the tensor data.
        # Use `tensor()` in order to get a pointer to the tensor.
        output_data = self.model.get_tensor(output_details[0]['index'])
        print(output_data)
        '''

        xml_file = '/home/pi/Desktop/My_Server/backend/endpoints/haarcascades/haarcascade_upperbody.xml'
        classifier = cv2.CascadeClassifier(xml_file)

        gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        faces_rect = classifier.detectMultiScale(
            gray_img, scaleFactor=1.1, minNeighbors=9)
        return faces_rect


"""
检测车辆横向边缘
"""
import cv2
import numpy as np
import matplotlib.pyplot as plt
from detection_model.diamondSpace import DiamondSpace

def neighborhood(grad, winSize=9):
    l = []
    if winSize % 2 != 1:
        print("winSize should be an odd number.")
        raise TypeError
    low = int((winSize - 1) / 2)
    top = int((winSize + 1) / 2)
    for i in range(-low, top):
        l.append(np.stack([np.arange(-low, top).reshape(1, -1), np.full((1, winSize), i)], axis=2))
    weight = np.vstack(l)
    grad_pad = np.pad(grad, ((low, low), (low, low)), 'constant')
    height, width = grad_pad.shape[:2]
    orientations = np.zeros((height - 2 * low, width - 2 * low, 2))
    quality = np.zeros_like(grad)

    # y_index, x_index = np.where(grad_pad != 0)
    # start = time()
    # for y in y_index:
    #     for x in x_index:
    for y in range(height):
        for x in range(width):
            if grad_pad[y][x] == 0:
                continue
            window = grad_pad[y - low:y + top, x - low:x + top]
            x_weight = np.multiply(window, weight[:, :, 0]).ravel()
            y_weight = np.multiply(window, weight[:, :, 1]).ravel()

            o, q = calculate(np.stack([x_weight, y_weight], axis=1))
            orientations[y - low][x - low] = o
            quality[y - low][x - low] = q
    # print(f'time is {time()-start}')
    return orientations, quality

def accumulate_orientation(orientations, quality, winSize=9, threshold=0.25):
    if (quality == 0).all():
        raise
    mask = np.zeros_like(quality)
    thres = np.percentile(quality[quality != 0], 100 * (1 - threshold))
    y_index, x_index = np.where(quality >= thres)
    height, width = mask.shape[:2]
    for i, j in zip(y_index, x_index):
        dx, dy = orientations[i, j]
        if dx < 1e-6:
            continue
        slope = dy / dx
        x = np.arange(j - int((winSize - 1) / 2), j + int((winSize - 1) / 2))
        y = np.around(i - slope * j + slope * x).astype(np.int)
        if (x < width).all() and (y < height).all():
            try:
                mask[y, x] += quality[i, j]
            except Exception as e:
                print(e)
    return mask, thres

def calculate(X):
    u, s, v = np.linalg.svd(X.T @ X)
    W = v
    sigma = s.transpose() * s
    orientation = W[:, 0]
    quality = sigma[0] / sigma[1] if sigma[1] != 0 else 255
    return orientation, quality

def get_lines(edges, orientation, box=None):
    y, x = np.where(edges > 0)
    lines = []
    if box is not None:
        real_x = x + box[0]
        real_y = y + box[1]
        for i, j in zip(x, y):
            dx, dy = orientation[j, i]
            a = dy
            b = -dx
            c = (j + box[1]) * dx - dy * (i + box[0])
            # k = IPM[j, i][1] / IPM[j, i][0]
            # b = read_y - k * real_x
            lines.append([a, b, c])
    else:
        for i, j in zip(x, y):
            k = orientation[j, i][1] / orientation[j, i][0]
            b = j - k * i
            lines.append([k, -1, b])
            # start = np.round(index + 5 * IPM[j, i]).astype(np.int32)
            # end = np.round(index - 5 * IPM[j, i]).astype(np.int32)
            # cv2.line(img, start, end, (0, 0, 255), 1)
    return np.vstack(lines)

if __name__ == '__main__':
    DS = DiamondSpace(720)

    # path = './detection_model/car.jpg'
    path = 'pics/cars.jpg'
    img = cv2.imread(path)
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(img_gray, 700, 700, L2gradient=True)
    orientation, quality = neighborhood(edges)
    thres, edges = cv2.threshold(quality, 0, 255, cv2.THRESH_OTSU)
    lines = get_lines(edges, orientation)
    peak = (147.45110199, -238.7251731)
    DS.filter_lines_from_vp(peak, lines)
    p, v, p_ds = DS.find_peaks(t=0.9)

    A = DS.attach_spaces()
    _, ax = plt.subplots(1, 2, figsize=(20, 10))
    ax[0].imshow(A, cmap="Greys", extent=(
        (-DS.size + 0.5) / DS.scale, (DS.size - 0.5) / DS.scale, (DS.size - 0.5) / DS.scale,
        (-DS.size + 0.5) / DS.scale))
    ax[0].set(title="Accumulator", xticks=np.linspace(-DS.size + 1, DS.size - 1, 5) / DS.scale,
              yticks=np.linspace(-DS.size + 1, DS.size - 1, 5) / DS.scale)
    ax[0].plot(p_ds[:, 0] / DS.scale, p_ds[:, 1] / DS.scale, "r+")
    plt.figure()
    print(img.shape)
    print(p)
    ax[1].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    # ax.plot(peak[0], peak[1], 'r+')
    ax[1].plot(p[0, 0], p[0, 1], 'r+')
    ax[0].invert_yaxis()
    plt.show()


from graphviz import Digraph

state_set = [['initial'],
    ['show_tutorial', 'to_en_ja', 'to_zh_ja', 'to_zh_en'],
    ['zh_to_en_result', 'zh_to_ja_result', 'en_to_zh_result', 'en_to_ja_result', 'ja_to_zh_result', 'ja_to_en_result'],
    ['zh_en_ex', 'zh_ja_ex', 'en_zh_ex', 'en_ja_ex', 'ja_zh_ex', 'ja_en_ex'],
    ['check_zh_ex', 'check_en_ex', 'check_ja_ex'],
    ['no_ex']
]
colors = ['black', 'blue', 'green', 'orange', 'yellow', 'red']

def e(a, b, l, c):
    dot.edge(a, b, label=l, color=c)

dot = Digraph(format="png")
dot.attr(rankdir='LR', size='8,5')

for states, color in zip(state_set, colors):
    for state in states:
        dot.node(name=state, label=state, color=color)
#
e('initial', 'show_manual', '嗨', 'black')
e('show_tutorial', 'to_en_ja', ':中文', 'black')
e('show_tutorial', 'to_zh_ja', ':英文', 'black')
e('show_tutorial', 'to_zh_en', ':日文', 'black')
e('initial', 'to_en_ja', ':中文', 'black')
e('initial', 'to_zh_ja', ':英文', 'black')
e('initial', 'to_zh_en', ':日文', 'black')
#
e('to_en_ja', 'zh_to_en_result', '帮我翻英文', 'black')
e('to_en_ja', 'zh_to_ja_result', '帮我翻日文', 'black')

e('to_zh_ja', 'en_to_zh_result', '帮我翻中文', 'black')
e('to_zh_ja', 'en_to_ja_result', '帮我翻日文', 'black')

e('to_zh_en', 'ja_to_zh_result', '帮我翻中文', 'black')
e('to_zh_en', 'ja_to_en_result', '帮我翻英文', 'black')
#
e('zh_to_en_result', 'check_zh_ex', '看例句', 'black')
e('check_zh_ex', 'no_ex', '如果没例句', 'black')
e('zh_en_ex', 'check_zh_ex', '下一句', 'black')
e('check_zh_ex', 'zh_en_ex', '如果有中文例句', 'black')
e('zh_en_ex', 'to_en_ja', '尝试其他翻译方式', 'black')

e('zh_to_ja_result', 'check_zh_ex', '看例句', 'black')
e('zh_ja_ex', 'check_zh_ex', '下一句', 'black')
e('check_zh_ex', 'zh_ja_ex', '如果有中文例句', 'black')
e('zh_ja_ex', 'to_en_ja', '尝试其他翻译方式', 'black')

e('en_to_zh_result', 'check_en_ex', '看例句', 'black')
e('check_en_ex', 'no_ex', '如果没例句', 'black')
e('en_zh_ex', 'check_en_ex', '下一句', 'black')
e('check_en_ex', 'en_zh_ex', '如果有英文例句', 'black')
e('en_zh_ex', 'to_zh_ja', '尝试其他翻译方式', 'black')

e('en_to_ja_result', 'check_en_ex', '看例句', 'black')
e('en_ja_ex', 'check_en_ex', '下一句', 'black')
e('check_en_ex', 'en_ja_ex', '如果有英文例句', 'black')
e('en_ja_ex', 'to_zh_ja', '尝试其他翻译方式', 'black')

e('ja_to_zh_result', 'check_ja_ex', '看例句', 'black')
e('check_ja_ex', 'no_ex', '如果没例句', 'black')
e('ja_zh_ex', 'check_ja_ex', '下一句', 'black')
e('check_ja_ex', 'ja_zh_ex', '如果有日文例句', 'black')
e('ja_zh_ex', 'to_zh_en', '尝试其他翻译方式', 'black')

e('ja_to_en_result', 'check_ja_ex', '看例句', 'black')
e('check_ja_ex', 'ja_en_ex', '下一句', 'black')
e('check_ja_ex', 'ja_zh_ex', '如果有日文例句', 'black')
e('ja_en_ex', 'to_zh_en', '尝试其他翻译方式', 'black')
#
e('no_ex', 'initial', '换一个', 'black')
#

print(dot.source)
dot.render(filename='fsm', directory="./imgs",view=False)
dot.render(filename='fsm', directory="./static",view=False)


"""
This is a Twitter snowflake like scheme generator that fits in 53 bits (for JavaScript, Redis sorted sets, ...)
Its scheme is:
    timestamp | worker_id | sequence
     41 bits  |  8 bits   |  4 bits
Timestamp is in milliseconds since the epoch allowing for 69 years of ids.
Each id generated per millisecond receives a unique auto incrementing sequence
number. Each worker can produce 16 ids per millisecond.
The scheme allows for 256 unique workers, so at most 4 million ids
(256*16*1000) can be produced per second with this scheme.
"""

import time

from logging import getLogger

log = getLogger(__name__)

__version__ = '1.0'

# Tuesday, February 18, 2020 3:00:00 AM GMT
fitepoch = 1581994800000

worker_id_bits = 8
max_worker_id = -1 ^ (-1 << worker_id_bits)
sequence_bits = 4
worker_id_shift = sequence_bits
timestamp_left_shift = sequence_bits + worker_id_bits
sequence_mask = -1 ^ (-1 << sequence_bits)
timestamp_mask = -1 >> timestamp_left_shift << timestamp_left_shift

def to_timestamp(_id):
    _id = _id >> timestamp_left_shift  # strip the lower bits
    _id += fitepoch                    # adjust for epoch
    _id = _id / 1000                   # convert from milliseconds to seconds
    return _id

def generator(
        worker_id=1,
        sleep=lambda x: time.sleep(x/1000.0),
        now=lambda: int(time.time()*1000)):
    """
    worker_id: a unique for your *entire* environment number between 0 and 255
               to identify this generator.
    sleep(n):  function to pause this worker for n milliseconds. you usually
               want to supply a custom method for this in asynchronous
               processes.
    now():     function to return a current unix timestamp in milliseconds.
               useful for testing.
    returns an iterator which yields a series of increasing integers,
    guaranteed to be unique for this worker_id.
    """
    #log.info("snowflake generator.")
    assert worker_id >= 0 and worker_id <= max_worker_id

    last_timestamp = -1
    sequence = 0

    while True:
        timestamp = now()

        if last_timestamp > timestamp:
            log.warning(
                "clock is moving backwards. waiting until %i" % last_timestamp)
            sleep(last_timestamp-timestamp)
            continue

        if last_timestamp == timestamp:
            sequence = (sequence + 1) & sequence_mask
            if sequence == 0:
                log.warning("sequence overrun")
                sequence = -1 & sequence_mask
                sleep(1)
                continue
        else:
            sequence = 0

        last_timestamp = timestamp

        yield (
            ((timestamp-fitepoch) << timestamp_left_shift) |
            (worker_id << worker_id_shift) |
            sequence)

if __name__ == "__main__":
    import datetime
    print(int(datetime.datetime.utcnow().timestamp()))
    print(int(datetime.datetime.now().timestamp() ))
    print(int(time.time() ))
    sf = generator(now=lambda: int(datetime.datetime.utcnow().timestamp() * 1000))
    sfid = next(sf)
    print(f'id {sfid}')


import sys
import os
import warnings
import random
import cv2
import numpy as np
import torchvision
import torchvision.transforms as T
import torch
import matplotlib.pyplot as plt
from PIL import Image

# ensure we are running on the correct gpu
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "6"  # (xxxx is your specific GPU ID)
if not torch.cuda.is_available() or torch.cuda.device_count() != 1:
    print('exiting')
    sys.exit()
else:
    print('GPU is being properly used')

# set to evaluation mode
model = torch.load('../../DoE-Project/mask-rcnn-pebble.pt')
model.eval()
CLASS_NAMES = ['__background__', 'pebble']
device = torch.device(
    'cuda') if torch.cuda.is_available() else torch.device('cpu')
model.to(device)

def get_coloured_mask(mask):
    """
    random_colour_masks
      parameters:
        - image - predicted masks
      method:
        - the masks of each predicted object is given random colour for visualization
    """
    colours = [[0, 255, 0], [0, 0, 255], [255, 0, 0], [0, 255, 255], [255, 255, 0], [
        255, 0, 255], [80, 70, 180], [250, 80, 190], [245, 145, 50], [70, 150, 250], [50, 190, 190]]
    r = np.zeros_like(mask).astype(np.uint8)
    g = np.zeros_like(mask).astype(np.uint8)
    b = np.zeros_like(mask).astype(np.uint8)
    r[mask == 1], g[mask == 1], b[mask == 1] = colours[random.randrange(0, 10)]
    coloured_mask = np.stack([r, g, b], axis=2)
    return coloured_mask

def get_prediction(img, confidence):
    """
    get_prediction
      parameters:
        - img_path - path of the input image
        - confidence - threshold to keep the prediction or not
      method:
        - Image is obtained from the image path
        - the image is converted to image tensor using PyTorch's Transforms
        - image is passed through the model to get the predictions
        - masks, classes and bounding boxes are obtained from the model and soft masks are made binary(0 or 1) on masks
          ie: eg. segment of cat is made 1 and rest of the image is made 0

    """
    transform = T.Compose([T.ToTensor()])
    img = transform(img)

    img = img.to(device)
    pred = model([img])
    pred_score = list(pred[0]['scores'].detach().cpu().numpy())
    print('pred scores:', pred_score)
    pred_t = [pred_score.index(x) for x in pred_score if x > confidence]
    if len(pred_t) == 0:
        return None, None, None
    pred_t = pred_t[-1]
    masks = (pred[0]['masks'] > 0.5).detach().cpu().numpy()
    masks = masks.reshape(-1, *masks.shape[-2:])
    print(masks.shape)
    # print(pred[0]['labels'].numpy().max())
    pred_class = [CLASS_NAMES[i]
                  for i in list(pred[0]['labels'].cpu().numpy())]
    pred_boxes = [[(int(i[0]), int(i[1])), (int(i[2]), int(i[3]))]
                  for i in list(pred[0]['boxes'].detach().cpu().numpy())]
    masks = masks[:pred_t+1]
    pred_boxes = pred_boxes[:pred_t+1]
    pred_class = pred_class[:pred_t+1]
    return masks, pred_boxes, pred_class

def make_mask_image(img, masks, boxes, pred_cls, ind, rect_th=2, text_size=2, text_th=2):
    for i in range(len(masks)):
        rgb_mask = get_coloured_mask(masks[i])
        img = cv2.addWeighted(img, 1, rgb_mask, 0.5, 0)
        cv2.rectangle(img, boxes[i][0], boxes[i][1],
                      color=(0, 255, 0), thickness=rect_th)
        cv2.putText(img, pred_cls[i], boxes[i][0], cv2.FONT_HERSHEY_SIMPLEX,
                    text_size, (0, 255, 0), thickness=text_th)
    # save frame as JPG file
    # cv2.imwrite("./ceramicimages/image"+str(ind) + "_mask.jpg", img)

def crop_pebble(img, masks, boxes, ind):
    mask = np.asarray(masks[0], dtype="uint8")
    # obtain only the mask pixels from the image
    only_mask = cv2.bitwise_and(img, img, mask=mask)
    bbox = boxes[0]
    # crop the image to only contain the pebble
    crop = only_mask[bbox[0][1]:bbox[1][1], bbox[0][0]:bbox[1][0]]

    # put pebble on standard 1000x1000 image
    imgSize = 1100
    background = np.zeros((imgSize, imgSize, 3), np.uint8)
    ch, cw = crop.shape[:2]

    # compute xoff and yoff for placement of upper left corner of resized image
    yoff = round((imgSize-ch)/2)
    xoff = round((imgSize-cw)/2)

    background[yoff:yoff+ch, xoff:xoff+cw] = crop
    # save crop as JPG file
    # cv2.imwrite("./ceramicimages/image" + str(ind) + "/crop.jpg", background)

    return background

vidcap = cv2.VideoCapture(
    '../../DoE-Project/Moving Pebbles - Ceramic Paint.MOV')
frame_count = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
print('video has', str(frame_count), 'frames.')

rotations = [0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150,
             165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345]
sharpen_kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])

count = 0
while (vidcap.isOpened()):
    hasFrames, image = vidcap.read()
    if hasFrames:
        if count >= 300 and count <= 305:
            # check if image has a pebble
            masks, boxes, pred_cls = get_prediction(image, .9)
            if masks is not None:
                if len(masks) == 1:
                    # make image directory
                    path = "./ceramicimages/image" + str(count) + "/"
                    if not os.path.isdir(path):
                        os.mkdir(path)
                        # save unmodified image
                    # save frame as JPG file
                    cv2.imwrite(path + "unmodified.jpg", image)
                    # make_mask_image(np.copy(image), masks,
                    #                 boxes, pred_cls, count)
                    image = crop_pebble(np.copy(image), masks, boxes, count)

                    # resize image
                    # image = cv2.resize(image, (100, 100))

                    for rotation in rotations:
                        image_center = tuple(np.array(image.shape[1::-1]) / 2)
                        rot_mat = cv2.getRotationMatrix2D(
                            image_center, rotation, 1.0)
                        result = cv2.warpAffine(
                            image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)
                        # sharpened = cv2.filter2D(result, -1, sharpen_kernel)

                        # deblurred = cv2.fastNlMeansDenoisingColored(
                        #     sharpened, None, 10, 10, 7, 21)
                        # save frame as JPG file
                        cv2.imwrite(path + str(rotation)+".jpg", result)
    else:
        break
    count += 1

# When everything done, release the capture
vidcap.release()
cv2.destroyAllWindows()


from selenium import webdriver
import time

option = webdriver.ChromeOptions()
option.add_argument("-incognito")
option.add_experimental_option("excludeSwitches", ['enable-automation']);
#option.add_argument("--headless")
#option.add_argument("disable-gpu")
browser = webdriver.Chrome(executable_path=r'C:\Users\sheik\Downloads\chromedriver_win32/chromedriver.exe', options=option)

browser.get("https://docs.google.com/forms/d/e/1FAIpQLSeRP3-IvxiGvLNoHL5efeXoukWIzehU7g-beVtVKQf_eJBqYQ/viewform")

##final = browser.find_element_by_class_name("quantumWizTextinputPapertextareaContentArea.exportContentArea")

for i in range(1500):
    textboxes = browser.find_elements_by_class_name("quantumWizTextinputPaperinputInput")
    radiobuttons = browser.find_elements_by_class_name("docssharedWizToggleLabeledLabelWrapper")
    ##time.sleep(1)
    textboxes[0].send_keys("Muhammad Mujtaba")
    textboxes[1].send_keys("muhammad.mujtaba@centre.edu")
    radiobuttons[1].click()
    textboxes[2].send_keys("sheikhmujtaba")
    ##final.send_keys("Physical workouts, extreme sports")
    submit = browser.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[3]/div[1]/div/div/span/span')
    submit.click()
    another_response = browser.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/div/div[4]/a')
    another_response.click()

browser.close()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from random import choice, randrange as rand
import matplotlib.pyplot as plt
import copy

##
# Dimensions du labyrinthe
width = 20
height = 20

##
# Constantes servant à décrire les directions des passages
N, S, E, W  = 1, 2, 4, 8
IN          = 0x10
FRONTIER    = 0x20
OPPOSITE    = {E: W, W: E, S: N, N: S}

##
# Structures de données
grid = [([0] * width) for _ in range(height)]
frontier = set()

##
# Méthodes utilitaires
def add_frontier(x, y):
    if (x >= 0 and y >= 0 and y < len(grid)
            and x < len(grid[y]) and grid[y][x] == 0):
        grid[y][x] |= FRONTIER
        frontier.add((x, y))

def mark(x, y):
    grid[y][x] |= IN
    add_frontier(x-1, y)
    add_frontier(x+1, y)
    add_frontier(x, y-1)
    add_frontier(x, y+1)

def neighbors(x, y):
    if x > 0 and (grid[y][x-1] & IN):
        yield (x-1, y)
    if x + 1 < len(grid[y]) and (grid[y][x+1] & IN):
        yield (x+1, y)
    if y > 0 and (grid[y-1][x] & IN):
        yield (x, y-1)
    if y + 1 < len(grid) and (grid[y+1][x] & IN):
        yield (x, y+1)

def direction(fx, fy, tx, ty):
    return {(fx < tx): E,
            (fx > tx): W,
            (fy < ty): S,
            (fy > ty): N}[True]

def is_empty(cell):
    return cell == 0 or cell == FRONTIER

def display_plt():
    plt.text(2,5*height-2,'D')
    plt.text(5*width-3,2,'A')
    for y, row in enumerate(grid):
        for x, cell in enumerate(row):
            if cell&N==0:
                plt.plot([5*x,5*x+5],[5*height-(5*y),5*height-(5*y)],'b')
            if cell&S==0:
                plt.plot([5*x,5*x+5],[5*height-(5*y+5),5*height-(5*y+5)],'b')
            if cell&E==0:
                plt.plot([5*x+5,5*x+5],[5*height-(5*y+5),5*height-(5*y)],'b')
            if cell&W==0:
                plt.plot([5*x,5*x],[5*height-(5*y+5),5*height-(5*y)],'b')
    plt.axis('equal')

def choice_dir(x,y):
    list_dir=[N,S,E,W]
    for test in parcours:
        if test[0]==(x,y):
            if test[1] in list_dir:
                list_dir.remove(test[1])
    return(list_dir)

def generation_grille():
    mark(rand(width), rand(height))
    while frontier:
        ##
        # Choix d'un voisin à la frontière
        x, y = choice(list(frontier))
        frontier.remove((x, y))
        nx, ny = choice(list(neighbors(x, y)))

        ##
        # Création d'un passage
        dir = direction(x, y, nx, ny)
        grid[y][x] |= dir
        grid[ny][nx] |= OPPOSITE[dir]

        mark(x, y)

        ## DEBUG :
        # print("frontier =", frontier)
        # print("(%d, %d) -> (%d, %d)" % (x, y, nx, ny))
        # display_maze()
        # input("Appuyez sur Entree pour continuer")

def test_grille():
    i=0
    x,y=0,0
    parcours=[]
    grid_parcours = [([0] * width) for _ in range(height)]
    grid_parcours[0][0]=1
    while (x,y)!=(width-1,height-1):
        test_case=(grid[y][x]&N)+(grid[y][x]&S)+(grid[y][x]&E)+(grid[y][x]&W)
        if test_case in [N,S,E,W]:
            banni.append([x,y,test_case])
        i+=1
        parcours.append([x,y])
        idee=choice([N,S,E,W])
        if idee==E and grid[y][x]&E!=0 and (x+1,y) not in banni:
            x+=1
        elif idee==S and grid[y][x]&S!=0 and (x,y+1) not in banni:
            y+=1
        elif idee==W and grid[y][x]&W!=0 and (x-1,y) not in banni:
            x+=-1
        elif idee==N and grid[y][x]&N!=0 and (x,y-1) not in banni:
            y+=-1
        grid_parcours[y][x]=1
    parcours.append([x,y])
    #plt.scatter([5*ban[0]+2.5 for ban in banni],[5*height-(5*ban[1]+2.5) for ban in banni])
    return(parcours)

def mise_a_jour_grille():
    for ban in banni[1:]:
        if [ban[0],ban[1]] not in traite and (ban[0],ban[1])!=(0,0):
            if ban[2]==N and ban[1]>0:
                traite.append([ban[0],ban[1]])
                grid[ban[1]][ban[0]] +=- ban[2]
                grid[ban[1]-1][ban[0]] +=- OPPOSITE[ban[2]]
            elif ban[2]==S and ban[1]<height-1:
                traite.append([ban[0],ban[1]])
                grid[ban[1]][ban[0]] +=- ban[2]
                grid[ban[1]+1][ban[0]] +=- OPPOSITE[ban[2]]
            if ban[2]==E and ban[0]<width-1:
                traite.append([ban[0],ban[1]])
                grid[ban[1]][ban[0]] +=- ban[2]
                grid[ban[1]][ban[0]+1] +=- OPPOSITE[ban[2]]
            if ban[2]==W and ban[0]>0:
                traite.append([ban[0],ban[1]])
                grid[ban[1]][ban[0]] +=- ban[2]
                grid[ban[1]][ban[0]-1] +=- OPPOSITE[ban[2]]

plt.figure('Initial')

generation_grille()
grid_init=copy.deepcopy(grid)
display_plt()

for i in range(50):
    print('Essai '+str(i))
    banni=[]
    parcours=test_grille()
    if i in [0]:
        plt.figure('Essai '+str(i))
        plt.plot([5*par[0]+2.5 for par in parcours],[5*height-(5*par[1]+2.5) for par in parcours],'r')
        display_plt()
    traite=[]
    mise_a_jour_grille()

plt.figure('Final')
grid=copy.deepcopy(grid_init)
display_plt()
plt.plot([5*par[0]+2.5 for par in parcours],[5*height-(5*par[1]+2.5) for par in parcours],'r')

plt.show()


from datetime import datetime
from pathlib import Path
from django.shortcuts import render, redirect, reverse
from django.http import HttpResponseBadRequest
from fragview.projects import Project
from fragview.projects import current_project
from fragview.forms import KillJobForm
from jobs import client

def _elapsed_time(start: datetime, end: datetime) -> dict[str, int]:
    """
    calculate time elapsed from start to end time,
    returns elapsed time divided into hours, minutes and seconds
    """
    delta = end - start

    # let's use seconds precision
    seconds_delta = delta.seconds

    seconds = seconds_delta % 60
    minutes = (seconds_delta // 60) % 60
    hours = seconds_delta // 3600

    return dict(hours=hours, minutes=minutes, seconds=seconds)

def _get_jobs(project: Project) -> list[dict]:
    def _run_time(start_time):
        if start_time is None:
            return None

        return _elapsed_time(start_time, now)

    now = datetime.now()

    #
    # convert Jobs table into a format that is more
    # convenient for presenting to the user
    #
    jobs = []
    for job in project.get_running_jobs():
        jobs.append(
            dict(
                id=job.id,
                name=job.description,
                stdout=Path(job.stdout).name,
                stderr=Path(job.stderr).name,
                run_time=_run_time(job.started),
            )
        )

    return jobs

def status(request):
    return render(
        request, "hpcstatus.html", {"jobs": _get_jobs(current_project(request))}
    )

def kill(request):
    form = KillJobForm(request.POST)

    if not form.is_valid():
        return HttpResponseBadRequest(f"{form.errors}")

    project = current_project(request)
    client.cancel_jobs(project.id, form.get_job_ids())

    return redirect(reverse("hpcstatus"))

def jobhistory(request):
    def _finished_jobs():
        for job in project.get_finished_jobs():
            stdout = Path(job.stdout).name
            stderr = Path(job.stderr).name
            yield stdout, job.finished, stdout, stderr

    project = current_project(request)
    return render(request, "jobhistory.html", {"logHistory": _finished_jobs()})


from coalib.bearlib.abstractions.Linter import linter

@linter(executable='ruby',
        use_stdout=False,
        use_stderr=True,
        output_format='regex',
        output_regex=r'.+?:(?P<line>\d+): (?P<message>.*?'
                     r'(?P<severity>error|warning)[,:] \S+)\s?'
                     r'(?:\S+\s(?P<column>.*?)\^)?')
class RubySyntaxBear:
    """
    Checks the code with ``ruby -wc`` on each file separately.
    """
    LANGUAGES = "Ruby"

    @staticmethod
    def create_arguments(filename, file, config_file):
        return '-wc', filename


#!/usr/bin/env python3

# the Cartesian product
from itertools import product

import sys
import copy

##############################################################################
class Automaton:
    '''Represents an automaton'''

    ###########################################
    def __init__(self):
        '''__init__(self)

Constructor.
'''
        self.startStates = []
        self.acceptStates = []
        self.transitions = []

    ###########################################
    def clearAcceptStates(self):
        '''clearAcceptStates() -> Automaton

Clear accepting states of an automaton.
'''
        result = Automaton()
        result.startStates = self.startStates[:]
        result.transitions = self.transitions[:]
        result.acceptStates = []

        return result

    ###########################################
    def clearStartStates(self):
        '''clearStartStates() -> Automaton

Clear starting states of an automaton.
'''
        result = Automaton()
        result.startStates = []
        result.transitions = self.transitions[:]
        result.acceptStates = self.acceptStates[:]

        return result

    ###########################################
    def getTgtState(trans):
        '''getTgtState(trans) -> State

Retrieve the target state of a transition.
'''
        if len(trans) == 2:
            return trans[1]
        elif len(trans) == 3:
            return trans[2]
        elif len(trans) == 4:
            return trans[3]
        else:
            raise Exception("Internal error")

    ###########################################
    def getSrcState(trans):
        '''getSrcState(trans) -> State

Retrieve the source state of a transition.
'''
        return trans[0]

    ###########################################
    def getSymbol(trans):
        '''getSymbol(trans) -> Symbol

Get the symbol in an automaton transition.
'''
        assert len(trans) == 3
        return trans[1]

    ###########################################
    def getSymbol1(trans):
        '''getSymbol1(trans) -> Symbol

Get the first symbol in a transducer transition.
'''
        assert len(trans) == 4
        return trans[1]

    ###########################################
    def getSymbol2(trans):
        '''getSymbol2(trans) -> Symbol

Get the second symbol in a transducer transition.
'''
        assert len(trans) == 4
        return trans[2]

    ###########################################
    def isEpsilonTrans(trans):
        '''isEpsilonTrans(trans) -> Bool

Determine an epsilon transition.
    '''
        return len(trans) == 2

    ###########################################
    def makeEpsTrans(src, tgt):
        '''makeTrans(src, tgt) -> transition

Makes an epsilon transition.
'''
        return (src, tgt)

    ###########################################
    def makeTrans(src, symbol, tgt):
        '''makeTrans(src, symbol, tgt) -> transition

Makes a transition.
'''
        return (src, symbol, tgt)

    ###########################################
    def makeTransTransd(src, symbol1, symbol2, tgt):
        '''makeTrans(src, symbol1, symbol2, tgt) -> transition

Makes a transducer transition.
'''
        return (src, symbol1, symbol2, tgt)

    ###########################################
    def singleStartState(self, name):
        '''singleStartState(self, name) -> Automaton

Make a single start state called name, using epsilon transitions to go to the
original start states.
'''
        result = self
        startingStates = result.startStates[:]

        result.clearStartStates()
        result.startStates = [name]
        for state in startingStates:
            result.addTrans(transition = Automaton.makeEpsTrans(name, state))

        return result

    ###########################################
    def postTrans(self, state):
        '''postTrans(self, state) -> [transitions]

A list of transitions leaving state.
'''
        return list(filter(lambda trans: Automaton.getSrcState(trans) == state,
            self.transitions))

    ###########################################
    def preTrans(self, state):
        '''preTrans(self, state) -> [transitions]

A list of transitions entering state.
'''
        return list(filter(lambda trans: Automaton.getTgtState(trans) == state,
            self.transitions))

    ###########################################
    def addTrans(self, src = None, symb = None, tgt = None, transition = None):
        '''addTrans(self, src, symb, tgt)

Add a new transition src --symb--> tgt.
'''
        if transition is None:
            assert (src is not None) and (symb is not None) and (tgt is not None)
            self.transitions.append(Automaton.makeTrans(src, symb, tgt))
        else:
            assert (src is None) and (symb is None) and (tgt is None)
            self.transitions.append(transition)

    ###########################################
    def addTransTransd(self, src = None, symb1 = None, symb2 = None, tgt = None,
        transition = None):
        '''addTrans(self, src, symb1, symb2, tgt)

Add a new transition src --symb1/symb2--> tgt.
'''
        if transition is None:
            assert ((src is not None) and (symb1 is not None) and (symb2 is not None) and
                (tgt is not None))
            self.transitions.append(Automaton.makeTransTransd(src, symb1, symb2, tgt))
        else:
            assert (src is None) and (symb1 is None) and (symb2 is None) and (tgt is None)
            self.transitions.append(transition)

    ###########################################
    def addTransitions(self, transitions):
        '''addTransitions(transitions)

Adds a set of transitions into the automaton.
'''
        self.transitions.extend(transitions)

    ###########################################
    def appendAut(self, aut):
        '''appendAut(self, aut) -> Automaton

Append aut to the automaton. That is, keep the original starting state and add all transitions and accepting states from aut.
'''
        result = self
        result.transitions += aut.transitions
        result.acceptStates += aut.acceptStates

        return result

    ###########################################
    def flattenProductStateNames(self):
        '''flattenProductStateNames(self) -> Automaton

Flatten names of states of a product automaton.
'''
        def flattenProdState(prodState):
            return prodState[0] + "X" + prodState[1]

        # result = Automaton()
        # result.startStates = list(map(flattenProdState, self.startStates))
        # result.acceptStates = list(map(flattenProdState, self.acceptStates))
        #
        # # hey, without list(..), I get only an ierator
        # result.transitions = list(map(lambda trans: (
        #         flattenProdState(trans[0]), # src state
        #         trans[1],                   # symbol
        #         flattenProdState(trans[2])  # tgt state
        #     ), self.transitions))

        result = self.renameStates(flattenProdState)
        return result

    ###########################################
    def generalIntersection(autLhs, autRhs, funSymMatch, funDetSymb):
        '''generalIntersection(autLhs, autRhs, funSymMatch, funDetSymb) -> Automaton

Performs a general intersection of autLhs and autRhs, such that symbols are
matched using the funSymMatch predicate.  The symbol of a new transition is then
determined using funDetSymb.  Generates only reachable transitions and states.
'''
        print('generalIntersection: warning: ignoring epsilon transitions', file=sys.stderr)

        result = Automaton()
        newStartStates = list(product(autLhs.startStates, autRhs.startStates))
        result.startStates = newStartStates[:]
        result.acceptStates = []
        allAcceptStates = list(product(autLhs.acceptStates, autRhs.acceptStates))

        worklist = newStartStates[:]
        processed = set(newStartStates)

        while not (len(worklist) == 0):
            (lhs, rhs) = worklist.pop(0)

            for (_, symbLhs, tgtStateLhs) in autLhs.postTrans(lhs):
                for (_, symbRhs, tgtStateRhs) in autRhs.postTrans(rhs):
                    # go over all transitions from lhs and rhs
                    if (funSymMatch(symbLhs, symbRhs)):
                        # if the symbols match
                        newState = (tgtStateLhs, tgtStateRhs)
                        if newState not in processed:
                            worklist.append(newState)
                            processed.add(newState)
                            if newState in allAcceptStates:
                                result.acceptStates.append(newState)

                        result.addTrans(transition = Automaton.makeTrans(
                                (lhs, rhs),
                                funDetSymb(symbLhs, symbRhs),
                                newState
                            ))

        return result.flattenProductStateNames()

    ###########################################
    def intersection(autLhs, autRhs):
        '''intersection(self, autLhs, autRhs) -> Automaton

Performs a standard intersection of two automata using a product construction.
'''
        return Automaton.generalIntersection(autLhs, autRhs,
            lambda x, y: x == y,
            lambda x, y: x)

    ###########################################
    def renameStates(self, funcRem):
        '''renameStates(self, funcRem) -> Automaton

Rename states of the automaton using funcRem.
'''
        result = Automaton()

        result.startStates = list(map(funcRem, self.startStates))
        result.acceptStates = list(map(funcRem, self.acceptStates))

        ##############################################
        def sodomizeTrans(trans):                    #
            if len(trans) == 2:                      #
                return (                             #
                    funcRem(trans[0]),               #
                    funcRem(trans[1])                #
                )                                    #
            if len(trans) == 3:                      #
                return (                             #
                    funcRem(trans[0]),               #
                    trans[1],                        #
                    funcRem(trans[2])                #
                )                                    #
            elif len(trans) == 4:                    #
                return (                             #
                    funcRem(trans[0]),               #
                    trans[1],                        #
                    trans[2],                        #
                    funcRem(trans[3])                #
                )                                    #
            else:                                    #
                raise Exception("Internal error")    #
        ##############################################

        result.transitions = list(map(sodomizeTrans, self.transitions))

        return result

    ###########################################
    def removeUseless(self):
        '''removeUseless(self) -> Automaton

Removes useless (forward and backward) states from the automaton.
'''
        result = Automaton()
        result.startStates = []
        result.acceptStates = []

        # keep only forward reachable states
        worklist = self.startStates[:]
        processed = set(self.startStates)

        while not (len(worklist) == 0):
            state = worklist.pop(0)

            if state in self.acceptStates:
                result.acceptStates.append(state)

            for trans in self.postTrans(state):
                # go over all transitions _from_ state
                tgtState = Automaton.getTgtState(trans)

                if tgtState not in processed:
                    worklist.append(tgtState)
                    processed.add(tgtState)

        # keep only backward reachable states
        worklist = result.acceptStates[:]
        bwdProcessed = set(result.acceptStates)

        while not (len(worklist) == 0):
            state = worklist.pop(0)

            if state in self.startStates:
                result.startStates.append(state)

            for trans in self.preTrans(state):
                # go over all transitions _to_ state
                srcState = Automaton.getSrcState(trans)

                if srcState in processed and srcState not in bwdProcessed:
                    # if srcState is forward-useful and not backward-processed yet
                    worklist.append(srcState)
                    bwdProcessed.add(srcState)

                result.addTrans(transition = trans)

        return result

    ###########################################
    def removeEpsilon(self):
        '''removeEpsilon(self) -> Automaton

Removes epsilon transitions from the automaton.
'''
        result = copy.deepcopy(self)

        eClosure = {}
        eTransitions = list(filter(lambda trans: Automaton.isEpsilonTrans(trans),
            result.transitions))

        while len(eTransitions) > 0:
            trans = eTransitions.pop()
            result.transitions.remove(trans)
            (src, tgt) = (Automaton.getSrcState(trans), Automaton.getTgtState(trans))

            if tgt in result.acceptStates:
                result.acceptStates.append(src)

            for nextTrans in result.postTrans(tgt):
                if Automaton.isEpsilonTrans(nextTrans):
                    newTrans = (src, Automaton.getTgtState(nextTrans))
                    result.addTrans(transition = newTrans)
                    eTransitions.append(newTrans)
                else:
                    newTrans = (src, Automaton.getSymbol(nextTrans),
                        Automaton.getTgtState(nextTrans))
                    result.addTrans(transition = newTrans)

        return result

    ###########################################
    def toTransducer(self):
        '''toTransducer(self) -> Automaton

Transforms an automaton into a transducer encoding the identity relation.
'''
        result = self.mapTrans(lambda trans:
            (
                trans[0],
                trans[1],
                trans[1],
                trans[2]
            ))

        return result

    ###########################################
    def packTransdToAutomaton(self):
        '''toAutomaton(self) -> Automaton

Transforms a transducer into an automaton by packing the two symbols inside a
transition to a pair.
'''
        ##############################################
        def packer(trans):                           #
            if len(trans) == 2:                      #
                return trans                         #
            elif len(trans) == 4:                    #
                return (                             #
                    trans[0],                        #
                    (trans[1], trans[2]),            #
                    trans[3]                         #
                )                                    #
            else:                                    #
                raise Exception("Internal error")    #
        ##############################################

        result = self.mapTrans(packer)
        return result

    ###########################################
    def unpackAutomatonToTransd(self):
        '''toAutomaton(self) -> Automaton

Transforms an automaton with pairs on edges into a transducer by unpacking the pair.
'''
        ##############################################
        def unpacker(trans):                         #
            if len(trans) == 2:                      #
                return trans                         #
            elif len(trans) == 3:                    #
                return (                             #
                    trans[0],                        #
                    trans[1][0],                     #
                    trans[1][1],                     #
                    trans[2]                         #
                )                                    #
            else:                                    #
                raise Exception("Internal error")    #
        ##############################################

        result = self.mapTrans(unpacker)
        return result

    ###########################################
    def filterTrans(self, pred):
        '''filterTrans(self, pred) -> Automaton

Filters transitions of automaton with respect to the predicate pred.
'''
        result = Automaton()
        result.startStates = self.startStates[:]
        result.acceptStates = self.acceptStates[:]
        result.transitions = list(filter(pred, self.transitions))
        return result

    ###########################################
    def mapTrans(self, mapper):
        '''mapTrans(self, mapper) -> Automaton

Maps transitions w.r.t. mapper.
'''
        result = Automaton()
        result.startStates = self.startStates[:]
        result.acceptStates = self.acceptStates[:]
        result.transitions = list(map(mapper, self.transitions))
        return result

    ###########################################
    def transdAutAutProd(transd, aut1, funMatch1, funDet1, aut2, funMatch2, funDet2):
        '''transdAutAutProd(transd, aut1, funMatch1, funDet1, aut2, funMatch2, funDet2) -> Automaton

Peform a ternarny product of a transducer transd with automata aut1 and aut2,
such that aut1 matches to the first tape of trands and aut2 matches on the
second tape of transd.  funMatch1 and funMatch2 are used to determine whether
the symbols of transd and aut1 and aut2 respectively match.  funDet1 and
funDet2 are used to determine the symbol in the result.
'''
        result = Automaton()

        result.startStates = list(product(
            transd.startStates,
            aut1.startStates,
            aut2.startStates))

        result.acceptStates = []
        allAcceptStates = list(product(
            transd.acceptStates,
            aut1.acceptStates,
            aut2.acceptStates))

        worklist = result.startStates[:]
        processed = set(result.startStates)

        while not (len(worklist) == 0):
            (lhs, st1, st2) = worklist.pop(0)

            for lhsTrans in transd.postTrans(lhs):
                if Automaton.isEpsilonTrans(lhsTrans):
                    # for epsilon transitions
                    newState = (Automaton.getTgtState(lhsTrans), st1, st2)
                    if newState not in processed:
                        worklist.append(newState)
                        processed.add(newState)
                        if newState in allAcceptStates:
                            result.acceptStates.append(newState)

                    # insert an epsilon transition
                    newTrans = (
                            (lhs, st1, st2),
                            newState
                        )
                    result.addTrans(transition = newTrans)
                else:
                    # for ordinary transitions
                    (_, symbLhs1, symbLhs2, tgtStateLhs) = lhsTrans

                    for (_, symbAut1, tgtState1) in aut1.postTrans(st1):
                        if (funMatch1(symbLhs1, symbAut1)):
                            for (_, symbAut2, tgtState2) in aut2.postTrans(st2):
                                if (funMatch2(symbLhs2, symbAut2)):
                                    # go over all matching transitions from
                                    # lhs, st1, and st2

                                    # target product state
                                    newState = (tgtStateLhs, tgtState1, tgtState2)

                                    if newState not in processed:
                                        worklist.append(newState)
                                        processed.add(newState)
                                        if newState in allAcceptStates:
                                            result.acceptStates.append(newState)

                                    # insert an ordinary transition
                                    newTrans = (
                                            (lhs, st1, st2),
                                            funDet1(symbLhs1, symbAut1),
                                            funDet2(symbLhs2, symbAut2),
                                            newState
                                        )
                                    result.addTrans(transition = newTrans)

        ################################################################
        def stateSodomizer(stTuple):                                   #
            assert len(stTuple) == 3                                   #
            return stTuple[0] + "X" + stTuple[1] + "X" + stTuple[2]    #
        ################################################################

        result = result.renameStates(stateSodomizer)

        return result

    ###########################################
    def union(self, rhs):
        '''union(self, rhs) -> Automaton

Unites the automaton with automaton rhs.
'''
        result = Automaton()

        print('union: check whether the automata are disjoint first!', file=sys.stderr)
        result.startStates = self.startStates + rhs.startStates
        result.acceptStates = self.acceptStates + rhs.acceptStates
        result.transitions = self.transitions + rhs.transitions

        return result

    ###########################################
    def autUnion(lhs, rhs):
        '''autUnion(lhs, rhs) -> Automaton

Unites the automaton lhs with rhs.
'''
        return lhs.union(rhs)

    ###########################################
    def exportToDot(self):
        '''exportToDot(self) -> String

Export the automaton to the dot format.
'''
        result = ""
        result += "digraph {\n"
        result += "\"__init\" [shape=point];\n"

        for stState in self.startStates:
            result += "\"__init\" -> \"" + stState + "\";\n"

        for accState in self.acceptStates:
            result += "\"" + accState + "\" [shape=doublecircle];\n"

        for trans in self.transitions:
            result += "  \"" + Automaton.getSrcState(trans)
            result += "\" -> \"" + Automaton.getTgtState(trans)
            result += "\" [label=\""

            if len(trans) == 2:
                result += 'epsilon'
            elif len(trans) == 3:
                result += Automaton.getSymbol(trans)
            elif len(trans) == 4:
                result += Automaton.getSymbol1(trans)
                result += "/"
                result += Automaton.getSymbol2(trans)
            else:
                raise Exception("Internal error")

            result += "\"];\n"

        result += "}\n"
        return result

    ###########################################
    def __str__(self):
        '''__str__(self) -> String

Transforms automaton into a string.
'''
        output = ""
        output += "init: "
        first = True
        for state in self.startStates:
            if not first:
                output += ", "

            output += state
            first = False

        output += ";\n"

        for trans in self.transitions:
            if len(trans) == 2:
                output += trans[0] + " -> " + trans[1] + ";\n"
            elif len(trans) == 3:
                output += trans[0] + " -> " + trans[2] + " " + trans[1] + ";\n"
            elif len(trans) == 4:
                output += trans[0] + " -> " + trans[3] + " " + trans[1] + "/" + trans[2] + ";\n"
            else:
                raise Exception("Internal error")

        output += "accepting: "
        first = True
        for state in self.acceptStates:
            if not first:
                output += ", "

            output += state
            first = False

        output += ";\n"

        return output

###############################################################################
if __name__ == '__main__':
    print("Library module")


# encoding=utf-8
import time
import xmlrpc.client
import logging,sys
import datetime
import random

import common
from common import *
import FBUserHelper,FBUserCrawler
from utility import OurFBAccount
from utility import FBHelper,FBBaseUser
from loghelper.loghelper import logHelper

#远程服务代理
serverconfig = common.getDispatchServerConfig()  # 3
serProxy = xmlrpc.client.ServerProxy('http://{0}:{1}'.format(serverconfig[0], serverconfig[1]))

# def getatask(taskType):
#    return serProxy.getATask(taskType)

def getFBUserTask(tasktype):
   return serProxy.getATaskUser(tasktype)
def reportFBTaskUserComplete(taskid,runningState,completeDescription):
   return serProxy.reportFBTaskUserComplete(taskid,runningState,completeDescription)

def main():
    fbuserhelper = FBUserHelper.FBUserHelper()
    while True:
        try:
            if common.testDispatchServer() == 'Disconnected!':  #1.测试调度服务器连接
                logHelper.getLogger().info('Dispatch server is disconnected!')
                time.sleep(5)
                continue
            logHelper.getLogger().info('Dispatch server is connected!')

            time.sleep(random.randint(1,2))

            logHelper.getLogger().debug('connecting database server ...')
            if common.testDatabaseServer() == 'Disconnected!':  #2.测试服务器连接
                logHelper.getLogger().info('Database server is disconnected!,trying again later.')
                time.sleep(5)
                continue
            print('Database server is connected!')

            spiderType = 'Facebook.userInfo'    #对应任务类型
            task = getFBUserTask(spiderType)

            taskid = task['id']
            if int(taskid) == -1:
                time.sleep(5)
                continue

            fbid = task['fbid']
            originalfbid = task['originalfbid']
            deep = int(task['deep'])     #当前任务深度，在保存tb_user_friends时+1保存入库
            name = task['name']
            priority = task['priority']

            fbUser = FBBaseUser(fbid,name,deep,priority)

            # logHelper.getLogger().debug(task)
            logHelper.getLogger().info('Spider [{0}] have got a task:{1}/{2}/{3}. spider is working...'.format(spiderType,taskid,fbid,originalfbid))
            time.sleep(3)
            #开始单线程爬取工作#####################################################################################################
            try:
                # 填写分配任务时间和承担的spider=''
                fbuserhelper.UpdateTaskDispatch(int(taskid))

                #完成工作,入库
                print('Spider have done the job. Saving the results...')

                #告知调度端完成情况
                print('Spider have saved the results,and reporting the job to dispatch server...')
                k = reportFBTaskUserComplete(taskid, 2, 'completed normally.')
                print('task id:{0},fbid:{1} has reported the job status.'.format(taskid,fbid))

            except Exception as e1:
                logHelper.getLogger().error('Scrawling error!',e1)
                reportFBTaskUserComplete(taskid, 3, 'completed abnormally.Error:{0}'.format(e1))

            time.sleep(5)
            # 结束单线程爬取工作#####################################################################################################
        except Exception as e:
            logHelper.getLogger().error(e)
            logHelper.getLogger().error('the main loop error,restart it!')
            time.sleep(50)

if __name__ == '__main__':
    main()


from rest_framework import serializers, validators
from djoser.serializers import AbstractUserRegistrationSerializer

from .models import JOIE, Employer, User, Industry, Company, SocialLink, Financial
from joieAPI.adhoc import ModelChoiceField, ImageField

class UserRegistrationSerializer(AbstractUserRegistrationSerializer):
    """
    override default registration serializer, separate the the need of REQUIRED_FIELDS
    """
    class Meta(AbstractUserRegistrationSerializer.Meta):
        model = User
        fields = (
            User.USERNAME_FIELD,
            User._meta.pk.name,
            'password',
            'app_user_type',
        )
        write_only_fields = (
            'password',
        )

    def create(self, validated_data):
            return User.objects.create_user(**validated_data)

class SocialLinkSerializer(serializers.ModelSerializer):
    """
    used by serializer of user
    """

    class Meta:
        model = SocialLink
        fields = ('network_name', 'link')

class AccountSerializer(serializers.ModelSerializer):
    """
    first_name and last_name can be updated by user
    """
    socialLinks = SocialLinkSerializer(many=True, required=False)

    # required fields
    first_name = serializers.CharField(max_length=30, required=True)
    last_name = serializers.CharField(max_length=30, required=True)

    class Meta:
        model = User
        first_sign_in = serializers.SerializerMethodField(method_name='first_sign_in')

        fields = (
            'email', 'first_name', 'last_name', 'app_user_type', 'socialLinks', 'create_at', 'create_by', 'update_at',
            'update_by', 'last_login', 'first_sign_in', 'status', )
        read_only_fields = (
            'email', 'create_at', 'create_by', 'update_at', 'update_by', 'app_user_type', 'last_login')

    def first_sign_in(self, obj):
        return obj.first_sign_in()

    def update(self, instance, validated_data):
        user = instance
        socialLinks_data = validated_data.pop('socialLinks', 'empty')
        if socialLinks_data and socialLinks_data != 'empty':
            # user.socialLinks.clear()
            SocialLink.objects.filter(user=user).delete()
            for socialLink_data in socialLinks_data:
                try:
                    socialLink = SocialLink.objects.get(user=user, network_name=socialLink_data['network_name'])
                    socialLink.link = socialLink_data['link']
                    socialLink.save()
                except SocialLink.DoesNotExist:
                    SocialLink.objects.create(user=user, **socialLink_data)
        elif socialLinks_data != 'empty':
            # got the key and the value is null, means the use want to remove the values
            SocialLink.objects.filter(user=user).delete()
        for attr, value in validated_data.items():
            setattr(instance, attr, value)
        instance.save()
        return instance

class AccountMeSerializer(AccountSerializer):
    """
    user cannot update the status by his own
    """
    status = serializers.CharField(read_only=True)

class AccountAdminSerializer(AccountSerializer):
    """
    use by admin users themselves
    """

    class Meta(AccountSerializer.Meta):
        model = User
        fields = (
            'email', 'first_name', 'last_name', 'create_at', 'create_by', 'update_at',
            'update_by', 'last_login')
        read_only_fields = (
            'email', 'create_at', 'create_by', 'update_at', 'update_by', 'last_login')

class IndustrySerializer(serializers.HyperlinkedModelSerializer):
    """
    used by serializer of company
    """

    class Meta:
        model = Industry
        fields = ('url', 'name', 'description')

class CompanySerializer(serializers.ModelSerializer):
    """
    used by serializer of employer
    """

    industry = ModelChoiceField(choices=Industry.objects.all().values_list('name', flat=True))
    logo = ImageField(allow_null=True, required=False)

    # required fields
    name = serializers.CharField(max_length=100, required=True)
    roc = serializers.CharField(max_length=40, required=True)
    business_type = serializers.ChoiceField(choices=[('Direct', 'Direct'), ('Agency', 'Agency')], required=True)
    ea = serializers.CharField(max_length=40, required=False, allow_null=True, allow_blank=True)
    credit_amount = serializers.FloatField(required=True)

    class Meta:
        model = Company

    def get_validation_exclusions(self):
        exclusions = super(CompanySerializer, self).get_validation_exclusions()
        return exclusions + ['postal_code']

    def create(self, validated_data):
        industry_name = validated_data.pop('industry')
        industry = Industry.objects.get(name=industry_name)
        if validated_data['business_type'] == 'Agency':
            if 'ea' not in validated_data or not validated_data['ea']:
                raise serializers.ValidationError('EA is Mandatory for Agency Type.')
        company = Company.objects.create(industry=industry, **validated_data)
        return company

    def update(self, instance, validated_data):
        name = validated_data.get('name')
        filter_set = Company.objects.filter(name=name).exclude(pk=instance.pk)
        if filter_set.exists():
            raise validators.ValidationError('company name must be unique.')
        industry_name = validated_data.pop('industry', instance.industry)
        industry = Industry.objects.get(name=industry_name)
        instance.industry = industry
        # deleting the image because has a None value
        if 'image' in validated_data and not validated_data['image']:
            validated_data.pop('image')
        if 'ea' in validated_data and not validated_data['ea']:
            if instance.business_type == 'Agency' or validated_data['business_type'] == 'Agency':
                raise serializers.ValidationError('EA is Mandatory for Agency Type.')
        if 'business_type' in validated_data:
            if instance.business_type == 'Agency' or validated_data['business_type'] == 'Agency':
                if not instance.ea and not validated_data['ea']:
                    raise serializers.ValidationError('EA is Mandatory for Agency Type.')
        for attr, value in validated_data.items():
            setattr(instance, attr, value)
        instance.save()
        return instance

class CompanyMeSerializer(CompanySerializer):
    # user cannot update the credit_amount by his own.
    credit_amount = serializers.FloatField(read_only=True)

class BaseEmployerSerializer(serializers.HyperlinkedModelSerializer):
    """
    BaseEmployerSerializer for super admin user.
    super admin user can update all of the necessary fields
    """

    user = AccountSerializer(required=True)
    company = CompanySerializer(required=True)

    class Meta:
        model = Employer

    def update(self, instance, validated_data):
        user_data = validated_data.get('user', None)
        company_data = validated_data.get('company', None)
        if user_data:
            instance.user = AccountSerializer().update(instance.user, user_data)
        if company_data:
            instance.company = CompanySerializer().update(instance.company, company_data)
        instance.save()
        return instance

class EmployerAdminSerializer(serializers.HyperlinkedModelSerializer):
    """
    for admin user.
    super admin user can update all of the necessary fields
    """

    user = AccountSerializer(required=True)
    company = CompanyMeSerializer(required=True)

    class Meta:
        model = Employer

    def update(self, instance, validated_data):
        user_data = validated_data.get('user', None)
        company_data = validated_data.get('company', None)
        if user_data:
            instance.user = AccountSerializer().update(instance.user, user_data)
        if company_data:
            instance.company = CompanyMeSerializer().update(instance.company, company_data)
        instance.save()
        return instance

class EmployerMeSerializer(BaseEmployerSerializer):
    """
    For /me viewset

    """
    user = AccountMeSerializer(required=True)
    company = CompanyMeSerializer(required=True)

    def update(self, instance, validated_data):
        user_data = validated_data.get('user', None)
        company_data = validated_data.get('company', None)
        if user_data:
            instance.user = AccountMeSerializer().update(instance.user, user_data)
        if company_data:
            instance.company = CompanyMeSerializer().update(instance.company, company_data)
        instance.save()
        return instance

class FinancialSerializer(serializers.ModelSerializer):
    """
    used by serializer of joie
    """

    class Meta:
        model = Financial
        fields = ('bank_code', 'branch_code', 'account_number')

class BaseJOIESerializer(serializers.HyperlinkedModelSerializer):
    """
    for super admin user. the admin user cannot update the financial detail
    admin user can update all of the necessary fields
    """
    user = AccountSerializer(required=True)
    financial_detail = FinancialSerializer(required=False)

    # required fields
    nric = serializers.CharField(max_length=20, required=True)
    nric_name = serializers.CharField(max_length=40, required=True)
    nric_type = serializers.ChoiceField(choices=[('Singaporean', 'Singaporean'), ('PR', 'PR')], required=True)
    date_of_birth = serializers.DateField(required=True)
    gender = serializers.ChoiceField(choices=[(0, 'Male'), (1, 'Female')], required=True)
    contact_number = serializers.CharField(max_length=20, required=True)

    class Meta:
        model = JOIE

    def update(self, instance, validated_data):
        user_data = validated_data.pop('user', None)  # in case of partial update, it could be None
        financial_data = validated_data.pop('financial_detail', None)
        if user_data:
            instance.user = AccountSerializer().update(instance.user, user_data)
        # financial object created during JOIE object creation
        if financial_data:
            instance.financial_detail = FinancialSerializer().update(instance.financial_detail, financial_data)
        for attr, value in validated_data.items():
            setattr(instance, attr, value)
        instance.save()
        return instance

class JOIEAdminSerializer(BaseJOIESerializer):
    """
    admin user cannot update the financial detail
    """
    financial_detail = FinancialSerializer(read_only=True)

class JOIEMESerializer(BaseJOIESerializer):
    """
    For /me viewset
    """
    user = AccountMeSerializer(required=True)
    financial_detail = FinancialSerializer(required=False)

    punctuality = serializers.FloatField(read_only=True)
    performance = serializers.FloatField(read_only=True)
    attitude = serializers.FloatField(read_only=True)
    rating = serializers.FloatField(read_only=True)

class StaffRegistrationSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = (
            User.USERNAME_FIELD,
            User._meta.pk.name,
            'password',
        )
        write_only_fields = (
            'password',
        )

    def create(self, validated_data):
        return User.objects.create_admin(**validated_data)