from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from .views import db_locsapp
from .views import APIrequests
from .APIrequest import paginationAPI
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import api_view, permission_classes
from django.contrib.auth import get_user_model
from rest_framework.views import APIView
from django.core.exceptions import ObjectDoesNotExist
from django.core.mail import EmailMessage
from django.conf import settings
from .utility import Utility
from .permissions import is_profile_full
import math
from django.core.mail import EmailMessage

from dateutil.parser import parse

import json
from bson import ObjectId
from bson import json_util

import pytz
from datetime import datetime
import re
from django.http import HttpResponse

from django.contrib.auth import get_user_model

from pymongo import MongoClient
from pymongo import ASCENDING, DESCENDING

# Connects to the db and creates a MongoClient instance
mongodb_client = MongoClient('mongo', 27017)
db_locsapp = mongodb_client['locsapp']

""" Seller Article """

class FindUserByIdForArticle(APIView):

    def get(self, request, user_pk):
        try:
            user = get_user_model().object.get(pk=user_pk)
            return JsonResponse({"username": user.username, "notation_renter": "4",
                                 "nb_notation_renter": "50"}, status=200)
        except ObjectDoesNotExist:
            return JsonResponse(
                {"message": "user id does not exist"}, status=404)

""" Articles """

# Creates a new article

@csrf_exempt
@api_view(['POST'])
def searchArticles(request):
    document = {"metadatas": {}, "articles": []}
    body = json.loads(request.body.decode('utf8'))
    search = {}
    print(body)
    if (not "_pagination" in body or not isinstance(
            body["_pagination"], type({}))):
        return JsonResponse(
            {"Error": "You need a _pagination dict in your document."})
    if (not "page_number" in body["_pagination"]
            or not "items_per_page" in body["_pagination"]):
        return JsonResponse(
            {"Error": "The pagination is not in the correct format."})
    ordering = []
    if ("_order" in body):
        if (not isinstance(body["_order"], type([]))):
            return JsonResponse({"Error": "The order must be a list."})
        for fields_order in body["_order"]:
            if (not isinstance(fields_order, type({})) or len(fields_order.keys()) != 2 or not "order" in fields_order or not "field_name" in fields_order or not isinstance(
                    fields_order["order"], str) or not isinstance(fields_order["field_name"], str)):
                return JsonResponse(
                    {"Error": "The order is not correctly formated."})
            order_temp = None
            print ("INNIT")
            if (fields_order["order"] == "ASC"):
                order_temp = 1
            else:
                order_temp = -1
            ordering.append((fields_order["field_name"], order_temp))
    for key in body:
        if key != "_pagination" and key != "_order":
            if key == "title" or key == "description":
                if not "$or" in search:
                    search["$or"] = []
                search["$or"].append(
                    {key: {"$regex": re.compile(str(body[key]), re.IGNORECASE)}})
            else:
                search[key] = {"$in": body[key]}
    search["availibility_end"] = {"$gte": datetime.now(pytz.utc)}
    search["available"] = True
    number_items = body["_pagination"]["items_per_page"]
    page_number = body["_pagination"]["page_number"]
    print(search)
    if ("_order" in body and len(body["_order"]) > 0):
        results = db_locsapp["articles"].find(search).sort(ordering)[
            ((page_number -
              1) *
             number_items): (
                (page_number -
                 1) *
                number_items) +
            number_items]
    else:
        results = db_locsapp["articles"].find(search)[
            ((page_number -
              1) *
             number_items): (
                (page_number -
                 1) *
                number_items) +
            number_items]
    document["metadatas"]["page_number"] = body[
        "_pagination"]["page_number"]
    number_pages = int(results.count() / number_items)
    document["metadatas"][
        "total_pages"] = 1 if number_pages < 1 else number_pages
    document["metadatas"]["total_items"] = results.count()
    for instance in results:
        document["articles"].append(APIrequests.parseObjectIdToStr(instance))
    return JsonResponse(document)

def demandsOptions(request, key_check, visibility, status, status_message):
    if request.method == "POST":
        body = json.loads(request.body.decode('utf8'))
        print(body["id_demand"])
        if (body["id_demand"] is None):
            return JsonResponse(
                {"Error": "The key id_demand is needed."}, status=401)
        demand = db_locsapp["article_demands"].find_one(
            {"_id": ObjectId(body["id_demand"])})
        if (demand is None):
            return JsonResponse(
                {"Error": "The demand doesn't exist."}, status=401)
        if (demand[key_check] != request.user.pk):
            return JsonResponse(
                {"Error": "You are not allowed to do that."}, status=401)
        db_locsapp["article_demands"].update({"_id": ObjectId(body["id_demand"])}, {
                                             "$set": {"visible": visibility, "status": status}})
        return JsonResponse(
            {"message": status_message}, status=200)
    else:
        return JsonResponse({"Error": "Method not allowed!"}, status=405)

@csrf_exempt
@api_view(['POST'])
@permission_classes((IsAuthenticated,))
def refuseDemand(request):
    return demandsOptions(request, "id_target", False, "refused",
                          "The demand has been successfully denied!")

@csrf_exempt
@api_view(['POST'])
@permission_classes((IsAuthenticated,))
def retractDemand(request):
    return demandsOptions(request, "id_author", False, "retracted",
                          "You have successfully retracted!")

@csrf_exempt
@api_view(['POST', 'GET'])
@permission_classes((IsAuthenticated,))
def demandsMain(request):

    if is_profile_full(request) is False:
        return JsonResponse({"error": "Fill all your profile (including billing and living addresses )!"}, status=401)

    model = {
        "id_author": {
            "_type": int,
            "_default": request.user.pk,
            "_protected": True
        },
        "author_name": {
            "_type": str
        },
        "author_notation": {
            "_type": int,
            "_max": 5,
            "_min": -1
        },
        "id_target": {
            "_type": int
        },
        "name_target": {
            "_type": str
        },
        "id_article": {
            "_type": ObjectId()
        },
        "availibility_start": {
            "_type": str
        },
        "availibility_end": {
            "_type": str
        },
        "date_issued": {
            "_type": str,
            "_protected": True,
            "_default": datetime.now(pytz.utc)
        },
        "article_name": {
            "_type": str
        },
        "article_thumbnail_url": {
            "_type": str
        },
        "status": {
            "_type": str,
            "_default": "pending"
        },
        "visible": {
            "_type": bool,
            "_default": True
        }
    }

    if request.method == "POST":
        return APIrequests.POST(
            request, model, "article_demands", "The demand has been successfully issued!", verifyIfDemandAlreadyIssued)
    elif request.method == "GET":
        return APIrequests.GET(
            'article_demands', special_field={"id_target": request.user.pk, "visible": True, "status": "pending"})
    else:
        return JsonResponse({"Error": "Method not allowed!"}, status=405)

"""
HISTORY
"""
@csrf_exempt
@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def getArticleHistoryAsClient(request, id_page):
    if request.method == "GET":
        id_page = int(id_page)
        field = {"id_author": request.user.pk, "visible": True}
        nb_page, articles_as_client = paginationAPI(id_page, db_locsapp["article_demands"], field)
        return JsonResponse(
            {"nb_page": nb_page, "articles_as_client": articles_as_client})

    else:
        return JsonResponse({"Error": "Method not allowed!"}, status=405)

@csrf_exempt
@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def getArticleHistoryAsRenter(request, id_page):
    if request.method == "GET":
        id_page = int(id_page)
        field = {"id_target": request.user.pk, "visible": True}
        nb_page, articles_as_renter = paginationAPI(id_page, db_locsapp["article_demands"], field)
        return JsonResponse(
            {"nb_page": nb_page, "articles_as_renter": articles_as_renter})

    else:
        return JsonResponse({"Error": "Method not allowed!"}, status=405)

@csrf_exempt
@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def getNotationsAsClient(request, id_page):
    if request.method == "GET":
        id_page = int(id_page)
        try:
            user = get_user_model().objects.get(username=request.user.username)
            user_pk = user.pk
            nb_item = db_locsapp["notations"].count({"id_target": int(user_pk), "as_renter": False})
            item_on_a_page = 10
            nb_page = math.ceil(nb_item / item_on_a_page)
            notations_as_client = []

            if (id_page - 1) * 10 > nb_item:
                id_page = nb_page

            skip_page = id_page - 1
            if skip_page < 0:
                skip_page = 0
            for notation_as_client in db_locsapp["notations"].find({"id_target": int(user_pk), "as_renter": False}).sort("date_issued", DESCENDING).skip((skip_page) * item_on_a_page).limit(item_on_a_page):
                notation_as_client['_id'] = str(notation_as_client['_id'])
                notations_as_client.append(notation_as_client)

            return JsonResponse({"nb_page": nb_page, "notations_as_client": notations_as_client, "average_mark": user.tenant_score})

        except ObjectDoesNotExist:
            return JsonResponse(
                {"message": "username does not exist"}, status=404)
    else:
        return JsonResponse({"Error": "Method not allowed"}, status=405)

@csrf_exempt
@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def getNotationsAsRenter(request, id_page):
    if request.method == "GET":
        id_page = int(id_page)
        # On get la target
        try:
            user = get_user_model().objects.get(username=request.user.username)
            user_pk = user.pk
            nb_item = db_locsapp["notations"].count({"id_target": int(user_pk), "as_renter": True})
            item_on_a_page = 10
            nb_page = math.ceil(nb_item / item_on_a_page)
            notations_as_renter = []

            if (id_page - 1) * 10 > nb_item:
                id_page = nb_page

            skip_page = id_page - 1
            if skip_page < 0:
                skip_page = 0

            for notation_as_renter in db_locsapp["notations"].find({"id_target": int(user_pk), "as_renter": True}).sort("date_issued", DESCENDING).skip((skip_page) * item_on_a_page).limit(item_on_a_page):
                notation_as_renter['_id'] = str(notation_as_renter['_id'])
                notations_as_renter.append(notation_as_renter)

            return JsonResponse({"nb_page": nb_page, "notations_as_renter": notations_as_renter, "average_mark": user.renter_score})
        except ObjectDoesNotExist:
            return JsonResponse(
                {"message": "username does not exist"}, status=404)

    else:
        return JsonResponse({"Error": "Method not allowed"}, status=405)
    """"
    if request.method == "GET":
        return APIrequests.GET(
            "notations", special_field={"id_target": request.user.pk, "as_renter": True})
    else:
        return JsonResponse({"Error": "Method not allowed!"}, status=405)
    """

@csrf_exempt
@api_view(['POST'])
@permission_classes((IsAuthenticated,))
def acceptDemand(request):
    if request.method == "POST":
        if request.body:
            answer = json.loads(request.body.decode('utf8'))
            if not answer["id_demand"]:
                return JsonResponse(
                    {"Error": "You need to send the id of the demand."}, status=401)
            print(answer["id_demand"])
            document = db_locsapp["article_demands"].find_one(
                {"_id": ObjectId(answer['id_demand'])})
            if document is None:
                return JsonResponse(
                    {"Error": "The demand doesn't exist."}, status=401)
            if document["id_target"] != request.user.pk:
                return JsonResponse(
                    {"Error": "You're not allowed to accept this demand."}, status=401)
            if document["status"] != "pending":
                return JsonResponse(
                    {"Error": "This demand isn't a pending request."}, status=401)
            print(document)
            id_article = document["id_article"]
            db_locsapp["article_demands"].update(
                {
                    "id_target": request.user.pk, "id_article": id_article, "status": "pending"}, {
                    "$set": {
                        "status": "refused"}}, multi=True)
            db_locsapp["article_demands"].update({"_id": ObjectId(answer['id_demand'])}, {
                "$set": {"status": "accepted"}})
            db_locsapp["articles"].update({"_id": ObjectId(id_article)}, {
                                          "$set": {"available": False}})

            id_author = db_locsapp["article_demands"].find_one({"_id": ObjectId(answer['id_demand'])})['id_author']

            tenant = get_user_model().objects.get(pk=id_author)
            #renter =
            message = "Veuillez prendre contact avec le loueur par téléphone au numéro suivant " + request.user.phone
            email = EmailMessage('[Locsapp] Votre demande a été accepté',
                                 message, to=[tenant.email])
            email.send()
            # (Julian) Send contact of the renter to the tenant

            return JsonResponse(
                {"message": "Request has been successfully accepted!"})
        else:
            return JsonResponse({"Error": "Please send a json"}, status=404)
    else:
        return JsonResponse({"Error": "Method not allowed!"}, status=405)

@csrf_exempt
@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def demandsAsRenting(request):
    if request.method == "GET":
        return APIrequests.GET(
            'article_demands', special_field={"id_author": request.user.pk, "visible": True, "status": "pending"})
    else:
        return JsonResponse({"Error": "Method not allowed!"}, status=405)

@csrf_exempt
@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def currentTimelines(request):
    if request.method == "GET":
        docs = APIrequests.GET(
            'article_demands', special_field={"id_target": request.user.pk, "visible": True, "status": "accepted"}, raw=True)
        for idx, document in enumerate(docs["article_demands"]):
            if (datetime.now(pytz.utc) > parse(document["availibility_end"])):
                db_locsapp["article_demands"].update({"_id": ObjectId(document['_id'])}, {
                                                     "$set": {"status": "finished"}})
                docs["article_demands"].pop(idx)
        return (HttpResponse(json.dumps(
                docs, sort_keys=True, indent=4, default=json_util.default)))
    else:
        return JsonResponse({"Error": "Method not allowed!"}, status=405)

@csrf_exempt
@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def currentTimelinesAsRenting(request):
    if request.method == "GET":
        docs = APIrequests.GET(
            'article_demands', special_field={"id_author": request.user.pk, "visible": True, "status": "accepted"}, raw=True)
        for idx, document in enumerate(docs["article_demands"]):
            if (datetime.now(pytz.utc) > parse(document["availibility_end"])):
                db_locsapp["article_demands"].update({"_id": ObjectId(document['_id'])}, {
                                                     "$set": {"status": "finished"}})
                docs["article_demands"].pop(idx)
        return (HttpResponse(json.dumps(
                docs, sort_keys=True, indent=4, default=json_util.default)))
    else:
        return JsonResponse({"Error": "Method not allowed!"}, status=405)

def verifyIfDemandAlreadyIssued(document):
    if (db_locsapp["article_demands"].find_one({"$and": [{"id_article": document["id_article"]}, {
            "id_author": document["id_author"]}, {"status": {"$ne": "retracted"}}]}) is not None):
        return ({"error": "You already made the demand for this article."})
    elif (db_locsapp["article_demands"].find_one({"id_article": document["id_article"], "id_target": document["id_author"]}) is not None):
        return ({"error": "You can't demand for your own article."})
    else:
        return (True)

@csrf_exempt
@api_view(['POST'])
@permission_classes((IsAuthenticated,))
def postNewMark(request):
    model = {
        "id_author": {
            "_type": int,
            "_default": request.user.pk,
            "_protected": True
        },
        "author_name": str,
        "id_target": int,
        "id_demand": {
            "_type": ObjectId()
        },
        "id_article": {
            "_type": ObjectId()
        },
        "article_name": str,
        "value": {
            "_type": int,
            "_min": 1,
            "_max": 5
        },
        "as_renter": {
            "_type": bool
        },
        "comment": {
            "_type": str,
            "_length": 90
        },
        "date_issued": {
            "_type": str,
            "_protected": True,
            "_default": datetime.now(pytz.utc)
        }
    }
    if request.method == "POST":
        return APIrequests.POST(
            request, model, "notations", "The mark has been successfully added!", verifyIfNotAlreadyIssued)
    else:
        return JsonResponse({"Error": "Method not allowed!"}, status=405)

@csrf_exempt
@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def getMarkForClient(request, id_page):
    if request.method == "GET":
        docs = APIrequests.GET(
            'article_demands', special_field={"id_target": request.user.pk, "visible": True, "status": "finished"}, raw=True)
        for idx, document in enumerate(docs["article_demands"]):
            if (db_locsapp["notations"].find_one(
                    {"id_demand": document["_id"], "as_renter": False}) is not None):
                docs["article_demands"].pop(idx)
        return (HttpResponse(json.dumps(
                docs, sort_keys=True, indent=4, default=json_util.default)))
    else:
        return JsonResponse({"Error": "Method not allowed!"}, status=405)

@csrf_exempt
@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def getMarkForRenter(request, id_page):
    if request.method == "GET":
        docs = APIrequests.GET(
            'article_demands', special_field={"id_author": request.user.pk, "visible": True, "status": "finished"}, raw=True)
        for idx, document in enumerate(docs["article_demands"]):
            if (db_locsapp["notations"].find_one(
                    {"id_demand": document["_id"], "as_renter": True}) is not None):
                docs["article_demands"].pop(idx)
        return (HttpResponse(json.dumps(
                docs, sort_keys=True, indent=4, default=json_util.default)))
    else:
        return JsonResponse({"Error": "Method not allowed!"}, status=405)

def verifyIfNotAlreadyIssued(document):
    print(document)
    if (db_locsapp["articles"].find_one(
            {"_id": ObjectId(document['id_article'])}) is None):
        return ({"Error": "The article doesn't exist."})
    retrieve = db_locsapp["article_demands"].find_one(
        {"_id": ObjectId(document['id_demand'])})
    if (retrieve is None or retrieve["id_article"] != document["id_article"]):
        return ({"Error": "id_demand not conform."})
    if (retrieve["status"] == "accepted"):
        if (datetime.now(pytz.utc) > parse(document["availibility_end"])):
            db_locsapp["article_demands"].update({"_id": ObjectId(document['id_demand'])}, {
                                                 "$set": {"status": "finished"}})
        else:
            return ({"Error": "The article renting is not finished yet."})
    elif (retrieve["status"] == "pending"):
        return ({"Error": "The demand hasn't even been accepted yet."})
    elif (retrieve["status"] == "completed"):
        return ({"Error": "The transaction has already been completed"})
    if ((document["as_renter"] is True and retrieve["id_author"] != document["id_author"]) or (
            document["as_renter"] is False and retrieve["id_target"] != document["id_author"])):
        return ({"Error": "You are not allowed to make this notation."})
    if (db_locsapp["marks"].find_one({"id_demand": document["id_demand"], "id_article": document[
            'id_article'], "id_target": document["id_target"], "id_author": document["id_author"]}) is not None):
        return ({"Error": "You already gave a mark for this article."})
    if (db_locsapp["notations"].find_one({"id_demand": document[
            "id_demand"], "as_renter": not document["as_renter"]}) is not None):
        db_locsapp["article_demands"].update({"_id": ObjectId(document['id_demand'])}, {
            "$set": {"status": "completed"}})
    try:
        User = get_user_model()
        current_user = User.objects.get(pk=document["id_target"])
        number_of_marks = db_locsapp["notations"].find(
            {"id_target": document["id_author"], "as_renter": document["as_renter"]}).count()
        if (number_of_marks != 0):
            if (document["as_renter"] is True):
                new_average = current_user.renter_score + (document["value"] -
                                                           current_user.renter_score) / number_of_marks
                current_user.renter_score = int(new_average)
            else:
                new_average = current_user.tenant_score + (document["value"] -
                                                           current_user.tenant_score) / number_of_marks
                current_user.tenant_score = int(new_average)
        else:
            new_average = document["value"]
            if (document["as_renter"] is True):
                current_user.renter_score = int(new_average)
            else:
                current_user.tenant_score = int(new_average)
        print("IN HERE")
        current_user.save()
    except:
        return ({"Error": "Error while updating the user model."})
    return (True)

@csrf_exempt
@api_view(['POST'])
@permission_classes((IsAuthenticated,))
def postNewArticle(request):

    if is_profile_full(request) is False:
        return JsonResponse({"error": "Fill all your profile (including billing and living addresses )!"}, status=401)

    model = {
        "title": {
            "_type": str,
            "_length": 50
        },
        "id_author": {
            "_type": int,
            "_default": request.user.pk,
            "_protected": True
        },
        "username_author": {
            "_type": str,
            "_default": request.user.username,
            "_protected": True
        },
        "url_thumbnail": {
            "_type": str,
            "_default": "media/articles/default.jpg",
            "_length": 100
        },
        "url_pictures": {
            "_type": [str],
            "_required": False
        },
        "comments": {
            "_type": [ObjectId()],
            "_required": False
        },
        "gender": ObjectId(),
        "base_category": ObjectId(),
        "sub_category": ObjectId(),
        "tags": {
            "_type": [ObjectId()],
            "_required": False
        },
        "size": ObjectId(),
        "payment_methods": [ObjectId()],
        "brand": ObjectId(),
        "clothe_condition": ObjectId(),
        "description": {
            "_type": str,
            "_default": "This article has no description",
            "_length": 5000
        },
        "availibility_start": {
            "_type": str,
            "_length": 50
        },
        "availibility_end": {
            "_type": str,
            "_length": 50
        },
        "creation_date": {
            "_type": str,
            "_protected": True,
            "_default": datetime.now(pytz.utc)
        },
        "modified_date": {
            "_type": str,
            "_required": False
        },
        "location": {
            "_type": ObjectId(),
            "_required": False
        },
        "price": {
            "_type": int,
            "_max": 500,
            "_min": 0
        },
        "color": ObjectId(),
        "demands": {
            "_type": [ObjectId()],
            "_required": False
        },
        "id_renter": {
            "_type": int,
            "_required": False
        },
        "available": {
            "_type": bool,
            "_default": True
        },
        "article_state": {
            "_type": ObjectId(),
            "_required": False
        }
    }

    if request.method == "POST":
        return APIrequests.POST(
            request, model, "articles", "The article has been successfully created!", convertDates)
        '''
        return APIrequests.forgeAPIrequestCreate(
            "POST", request, fields_definition, db_locsapp["articles"])
        '''
    else:
        return JsonResponse({"Error": "Method not allowed!"}, status=405)

def convertDates(document):
    try:
        availibility_start = datetime.strptime(
            document["availibility_start"], "%d/%m/%Y")
        availibility_end = datetime.strptime(
            document["availibility_end"], "%d/%m/%Y")
        document["availibility_start"] = availibility_start
        document["availibility_end"] = availibility_end
    except:
        return({"error": "Misformatted availibility_start or availibility_end"})
    return (True)

# Updates an article

@csrf_exempt
def articleAlone(request, article_pk):
    model = {
        "title": {
            "_type": str,
            "_length": 50
        },
        "id_author": {
            "_type": int,
            "_protected": True
        },
        "url_thumbnail": {
            "_type": str,
            "_length": 100
        },
        "url_pictures": [str],
        "comments": [ObjectId()],
        "gender": ObjectId(),
        "base_category": ObjectId(),
        "sub_category": ObjectId(),
        "tags": [ObjectId()],
        "size": ObjectId(),
        "payment_methods": ObjectId(),
        "brand": ObjectId(),
        "clothe_condition": ObjectId(),
        "description": {
            "_type": str,
            "_default": "This article has no description",
            "_length": 5000
        },
        "availibility_start": {
            "_type": str,
            "_length": 50
        },
        "availibility_end": {
            "_type": str,
            "_length": 50
        },
        "creation_date": {
            "_type": str,
            "_protected": True
        },
        "modified_date": str,
        "location": ObjectId(),
        "price": {
            "_type": float,
            "_max": 500,
            "_min": 0
        },
        "color": ObjectId(),
        "demands": [ObjectId()],
        "id_renter": int,
        "article_state": ObjectId()
    }

    if request.method == "PUT":
        return APIrequests.PUT(
            request, model, "articles", "The article has been successfully modified!", article_pk)
    elif request.method == "GET":
        doc = db_locsapp["articles"].find_one({"_id": ObjectId(article_pk)})
        answer = APIrequests.parseObjectIdToStr(doc)
        return (JsonResponse(answer))
    else:
        return JsonResponse({"Error": "Method not allowed!"}, status=405)

""" Show article """

@csrf_exempt
def getArticle(request, article_pk):
    if request.method == "GET":
        # We also need to send the global mark of the user and the nb of notation as a renter
        article = db_locsapp["articles"].find_one({"_id": ObjectId(article_pk)})
        if article is None:
            return JsonResponse({"Error": "Article not found"}, status=404)
        article = APIrequests.parseObjectIdToStr(article)
        global_mark_as_renter = get_user_model().objects.get(pk=article["id_author"]).renter_score
        nb_mark_as_renter = db_locsapp["notations"].count({"id_target": int(article["id_author"]), "as_renter": True})

        is_in_favorite = False
        user = Utility.checkUserAuthenticated(request)
        if user is not False and db_locsapp["favorite_article"].find_one({"id_user": user.pk, "id_article": article_pk}):
            is_in_favorite = True

        is_reported = False
        if user is not False and db_locsapp['reports'].find_one({"id_author": user.pk, "id_article": article["_id"]}):
            is_reported = True

        """
        We look for article in the same:
         range of price + or - 10 euros
         same category
         and near same km range IMPORTANT
         same subcategory (maybe)
        """
        # articles_recommend = []
        # price_range = 1000
        #article_recommend_mongo = db_locsapp["articles"].find({"price": {"$gt": article["price"] - price_range, "$lt": article["price"] + price_range}}
        #                                                ).limit(10)
        # print("article recooment = ", article_recommend)
        """"
        for article_recommend in article_recommend_mongo:
            print("article = ", article_recommend)
            if article_recommend["_id"] != ObjectId(article_pk):
                article_recommend = APIrequests.parseObjectIdToStr(article_recommend)
                articles_recommend.append(article_recommend)
         """

        return JsonResponse({"article": article, "global_mark_as_renter": global_mark_as_renter,
                             "nb_mark_as_renter": nb_mark_as_renter, "is_in_favorite": is_in_favorite,
                             "is_reported": is_reported, "articles_recommend": []})
        # return APIrequests.GET('articles', id=article_pk)
    else:
        return JsonResponse({"Error": "Method not allowed!"}, status=405)

# Route for show articles in user profile
@csrf_exempt
def getFirstFourArticleOwnedByUser(request, user_pk):
    if request.method == "GET":
        articles = []
        total_article = db_locsapp["articles"].count({"id_author": int(user_pk), "available": True})

        for article in db_locsapp["articles"].find({"id_author": int(user_pk), "available": True}).sort("creation_date", DESCENDING).limit(4):
            article['_id'] = str(article['_id'])
            articles.append(article)

        return JsonResponse({"nb_total_articles": total_article, "articles": articles})
    else:
        return JsonResponse({"Error": "Method not allowed"}, status=405)

# Route for show all all articles owend by an user
# We print 10 item by page
@csrf_exempt
def getAllArticleOwnedByUser(request, user_pk, id_page):
    if request.method == "GET":
        id_page = int(id_page)
        nb_item = db_locsapp["articles"].count({"id_author": int(user_pk), "available": True})
        item_on_a_page = 10
        nb_page = nb_item / item_on_a_page
        articles = []

        # W
        if (id_page - 1) * 10 > nb_item:
            id_page = nb_page

        for article in db_locsapp["articles"].find({"id_author": int(user_pk), "available": True}).sort("creation_date", DESCENDING).skip((id_page - 1) * item_on_a_page).limit(item_on_a_page):
            article['_id'] = str(article['_id'])
            articles.append(article)

        return JsonResponse({"nb_page": nb_page, "articles": articles})
    else:
        return JsonResponse({"Error": "Method not allowed"}, status=405)

"""
We send an email to the adminstrator that tell us who user send a report for which article
"""

@csrf_exempt
@api_view(['POST'])
def sendReport(request):
    if request.method == "POST":
        if request.body:
            answer = json.loads(request.body.decode('utf8'))
            print("answer = ", answer)

            if 'id_article' not in answer or 'id_report_type' not in answer or 'email' not in answer:
                return JsonResponse({"Error": "Your forget to add fields"}, status=405)
            if not ObjectId.is_valid(answer['id_article']) or not ObjectId.is_valid(answer['id_report_type']):
                return JsonResponse({"Error": "Please add a correct Object id fields"}, status=405)

            article = db_locsapp["articles"].find_one({"_id": ObjectId(answer["id_article"])})
            report_type = db_locsapp["report_types"].find_one({"_id": ObjectId(answer["id_report_type"])})

            if article is None or report_type is None:
                return JsonResponse({"Error": "Add an existing article and report_type"}, status=405)

            current_user_pk = -1
            current_user_username = "anonymous"
            current_user = Utility.checkUserAuthenticated(request)
            if current_user is not False:
                current_user_pk = current_user.pk
                current_user_username = current_user.username

            if article["id_author"] == current_user_pk and current_user_pk != -1:
                return JsonResponse({"Error": "You are the owner of this article"}, status=403)

            model = {
                "id_article": {
                    "_type": ObjectId(),
                    "_required": True
                },

                "id_owner_article": {
                    "_type": int,
                    "_protected": True,
                    "_default": article['id_author']
                },
                "username_owner_article": {
                    "_type": str,
                    "_protected": True,
                    "_default": article['username_author']
                },

                "id_report_type": {
                  "_type": ObjectId(),
                },

                "report_type_str": {
                    "_type": str,
                    "_default": report_type['name']
                },

                "id_author": {
                    "_type": int,
                    "_protected": True,
                    "_default": current_user_pk
                },
                "username_author": {
                    "_type": str,
                    "_protected": True,
                    "_default": current_user_username
                },
                "email": {
                    "_type": str,
                    "_length": 50
                },
                "first_name": {
                    "_type": str,
                    "_length": 50,
                },
                "last_name": {
                    "_type": str,
                    "_length": 50
                },
                "message": {
                    "_type": str,
                    "_length": 1000,
                    "_required": False
                },
                "creation_date": {
                    "_type": str,
                    "_protected": True,
                    "_default": datetime.now(pytz.utc)
                },

            }

            if current_user_pk == -1:
                report = db_locsapp['reports'].find_one({"email": answer['email'], "id_article": answer['id_article']})
            else:
                    report = db_locsapp['reports'].find_one({"id_author": current_user_pk, "id_article": answer['id_article']})
            if report:
                return JsonResponse({"Error": "Your already reported this article"}, status=405)
            else:
                return APIrequests.POST(
                    request, model, "reports", "The report has been successfully sent!")

            """
            message = 'The user ' + request.user.username + ' sent a report about this article' + \
                ' <a href="' + settings.URL_FRONT + 'article/' + str(article['_id']) + '">' + \
                article['title'] + '</a>'
            email = EmailMessage('Report for article ' + article['title'], message,
                                 to=list_email)
            email.send()
            """
            return JsonResponse(
                {"Success": "Report sent to the administrators."}, status=200)
        else:
            return JsonResponse({"Error": "Please send a json"}, status=404)
    else:
        return JsonResponse({"Error": "Method not allowed!"}, status=405)


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl

# 防止中文乱码
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.linear_model import ElasticNet
from sklearn.linear_model import Lasso

from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import PolynomialFeatures

# 1. 加载数据(数据一般存在于磁盘或者数据库)
data = pd.read_csv(r"D:\人工智能\波士顿房价数据.csv")
# print(data.info())
# data.split('  ')
# print(data.info())
# 2. 数据清洗

# 3. 根据需求获取最原始的特征属性矩阵X和目标属性Y
X = data.iloc[:, 0:13]
Y = data.iloc[:, 13]

# print(X.head())
# print(Y.head())

# 4. 数据分割
x_train, x_test, y_train, y_test = train_test_split(X, Y)

# 5. 特征工程的操作
# print('转换前:',x_train.shape)
ss = StandardScaler()
x_train = ss.fit_transform(x_train)
# y_train = np.mat(y_train).reshape(-1, 1)
# print('转换后:',x_train.shape)

# 6. 模型对象的构建
# thea = (x_train.T * x_train).I * x_train.T * y_train
# print(thea)
# 7. 模型的训练

model = Pipeline([
    ('Poly', PolynomialFeatures()),  # 给定进行多项式扩展操作， 第一个操作：多项式扩展
    ('Linear', LinearRegression(fit_intercept=False))  # 第二个操作，线性回归
])
# print(len(y_pred))
# print('**'*20)
# print(y_test)
# 8. 模型效果评估
x_test = ss.transform(x_test)
y_pred = lr.predict(x_test)
print("各个特征属性的权重系数，也就是ppt上的theta值:{}".format(lr.coef_))
print("截距项值:{}".format(lr.intercept_))
# b. 直接通过评估相关的API查看效果
print("模型在训练数据上的效果(R2)：{}".format(lr.score(x_train, y_train)))
# 在测试的时候对特征属性数据必须做和训练数据完全一样的操作
# x_test = ss.transform(x_test)
print("模型在测试数据上的效果(R2)：{}".format(lr.score(x_test, y_test)))
# print(np.mean(y_pred - y_test.T))
# print('均方差为:', R)
t = np.arange(len(x_test))
plt.plot(t, y_test, 'r', linewidth=2, label=u'真实值')
plt.plot(t, y_pred, 'b', linewidth=2, label=u'真实值')
plt.xlabel('波士顿房价的各个样本')
plt.ylabel('房价')
plt.title('波士顿房价预测')
plt.legend(loc='upper right')
plt.show()
# 9. 模型保存\模型持久化

项目实战-波士顿房价拟合/基于sklearn的通用回归分析.py


#
# Copyright 2019-2023 - Swiss Data Science Center (SDSC)
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
# Eidgenössische Technische Hochschule Zürich (ETHZ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Renku service project view tests."""
import json
import time

import portalocker
import pytest

from renku.ui.service.errors import ProgramInvalidGenericFieldsError
from tests.service.views.test_dataset_views import assert_rpc_response
from tests.utils import retry_failed

@pytest.mark.service
@pytest.mark.integration
@retry_failed
def test_show_project_view(svc_client_with_repo):
    """Test show project metadata."""
    svc_client, headers, project_id, url_components = svc_client_with_repo

    show_payload = {
        "git_url": url_components.href,
    }
    response = svc_client.post("/project.show", data=json.dumps(show_payload), headers=headers)

    assert_rpc_response(response)
    assert {
        "id",
        "name",
        "description",
        "created",
        "creator",
        "agent",
        "custom_metadata",
        "template_info",
        "keywords",
    } == set(response.json["result"])

@pytest.mark.service
@pytest.mark.integration
@pytest.mark.parametrize(
    "custom_metadata",
    [
        [
            {
                "@id": "http://example.com/metadata12",
                "@type": "https://schema.org/myType",
                "https://schema.org/property1": 1,
                "https://schema.org/property2": "test",
            },
        ],
        [
            {
                "@id": "http://example.com/metadata12",
                "@type": "https://schema.org/myType",
                "https://schema.org/property1": 1,
                "https://schema.org/property2": "test",
            },
            {
                "@id": "http://example.com/metadata1",
                "@type": "https://schema.org/myType1",
                "https://schema.org/property4": 3,
                "https://schema.org/property5": "test1",
            },
        ],
    ],
)
@pytest.mark.parametrize("custom_metadata_source", [None, "testSource"])
@retry_failed
def test_edit_project_view(svc_client_with_repo, custom_metadata, custom_metadata_source):
    """Test editing project metadata."""
    svc_client, headers, project_id, url_components = svc_client_with_repo

    edit_payload = {
        "git_url": url_components.href,
        "description": "my new title",
        "creator": {"name": "name123", "email": "name123@ethz.ch", "affiliation": "ethz"},
        "custom_metadata": custom_metadata,
    }
    if custom_metadata_source is not None:
        edit_payload["custom_metadata_source"] = custom_metadata_source
    response = svc_client.post("/project.edit", data=json.dumps(edit_payload), headers=headers)

    assert_rpc_response(response)
    assert {"warning", "edited", "remote_branch"} == set(response.json["result"])
    assert {
        "description": "my new title",
        "creator": {"name": "name123", "email": "name123@ethz.ch", "affiliation": "ethz"},
        "custom_metadata": custom_metadata,
    } == response.json["result"]["edited"]

    edit_payload = {
        "git_url": url_components.href,
    }
    response = svc_client.post("/project.edit", data=json.dumps(edit_payload), headers=headers)

    assert_rpc_response(response)
    assert {"warning", "edited", "remote_branch"} == set(response.json["result"])
    assert 0 == len(response.json["result"]["edited"])

@pytest.mark.service
@pytest.mark.integration
@retry_failed
def test_edit_project_view_unset(svc_client_with_repo):
    """Test editing project metadata by unsetting values."""
    svc_client, headers, project_id, url_components = svc_client_with_repo

    edit_payload = {
        "git_url": url_components.href,
        "description": "my new title",
        "creator": {"name": "name123", "email": "name123@ethz.ch", "affiliation": "ethz"},
        "keywords": ["keyword1", "keyword2"],
        "custom_metadata": [
            {
                "@id": "http://example.com/metadata12",
                "@type": "https://schema.org/myType",
                "https://schema.org/property1": 1,
                "https://schema.org/property2": "test",
            }
        ],
    }
    response = svc_client.post("/project.edit", data=json.dumps(edit_payload), headers=headers)

    edit_payload = {"git_url": url_components.href, "custom_metadata": None, "keywords": None}
    response = svc_client.post("/project.edit", data=json.dumps(edit_payload), headers=headers)

    assert_rpc_response(response)
    assert {"warning", "edited", "remote_branch"} == set(response.json["result"])
    assert {
        "keywords": None,
        "custom_metadata": None,
    } == response.json[
        "result"
    ]["edited"]

@pytest.mark.service
@pytest.mark.integration
@retry_failed
def test_edit_project_view_failures(svc_client_with_repo):
    """Test failures when editing project metadata providing wrong data."""
    svc_client, headers, project_id, url_components = svc_client_with_repo

    payload = {
        "git_url": url_components.href,
        "description": "my new title",
        "creator": {"name": "name123", "email": "name123@ethz.ch", "affiliation": "ethz"},
        "custom_metadata": [
            {
                "@id": "http://example.com/metadata12",
                "@type": "https://schema.org/myType",
                "https://schema.org/property1": 1,
                "https://schema.org/property2": "test",
            }
        ],
    }
    payload["FAKE_FIELD"] = "FAKE_VALUE"
    response = svc_client.post("/project.edit", data=json.dumps(payload), headers=headers)

    assert_rpc_response(response, "error")
    assert ProgramInvalidGenericFieldsError.code == response.json["error"]["code"]

@pytest.mark.integration
@pytest.mark.service
@retry_failed
def test_remote_edit_view(svc_client, it_remote_repo_url, identity_headers):
    """Test creating a delayed edit."""
    response = svc_client.post(
        "/project.edit",
        data=json.dumps(dict(git_url=it_remote_repo_url, is_delayed=True)),
        headers=identity_headers,
    )

    assert_rpc_response(response)
    assert response.json["result"]["created_at"]
    assert response.json["result"]["job_id"]

@pytest.mark.integration
@pytest.mark.service
def test_get_lock_status_unlocked(svc_client_setup):
    """Test getting lock status for an unlocked project."""
    svc_client, headers, project_id, url_components, _ = svc_client_setup

    response = svc_client.get(
        "/1.1/project.lock_status",
        query_string={"git_url": url_components.href, "timeout": 1.0},
        headers=headers,
        content_type="text/xml",
    )

    assert_rpc_response(response)
    assert {"locked"} == set(response.json["result"].keys())
    assert response.json["result"]["locked"] is False

@pytest.mark.integration
@pytest.mark.service
def test_get_lock_status_locked(svc_client_setup):
    """Test getting lock status for a locked project."""
    svc_client, headers, project_id, url_components, repository = svc_client_setup

    def mock_lock():
        return portalocker.Lock(f"{repository.path}.lock", flags=portalocker.LOCK_EX, timeout=0)

    with mock_lock():
        start = time.monotonic()
        response = svc_client.get(
            "/1.1/project.lock_status", query_string={"git_url": url_components.href, "timeout": 1.0}, headers=headers
        )
        assert time.monotonic() - start >= 1.0

    assert_rpc_response(response)
    assert {"locked"} == set(response.json["result"].keys())
    assert response.json["result"]["locked"] is True

@pytest.mark.integration
@pytest.mark.service
def test_get_lock_status_for_project_not_in_cache(svc_client, identity_headers):
    """Test getting lock status for an unlocked project which is not cached."""
    response = svc_client.get(
        "/1.1/project.lock_status", query_string={"git_url": "https://example.com/repo.git"}, headers=identity_headers
    )

    assert_rpc_response(response)
    assert {"locked"} == set(response.json["result"].keys())
    assert response.json["result"]["locked"] is False


from time import strftime, gmtime
import App_Utils
import model.Model
__author__ = 'huanpc'

import asyncio
import json
from aiohttp import web
from docker import Client
# config
HOST = '127.0.0.1'
PORT = 9090
#

cli = Client(base_url='unix://var/run/docker.sock')

@asyncio.coroutine
def deploy_app(request):
    '''
    [POST] Method to deploy app
    Posted Data Format: {'app_name': 'xxx', 'git_source_code': '',
    'programming_base': 'php',
    'database': 'mysql', 'tools':[{'name': 'phpMyAdmin', 'version': '1.x'}, {'name': 'yy', 'version': 'zz'}]}
    :param request:
    :return:
    '''

    data = yield from request.text()
    json_data = json.loads(data)
    result = App_Utils.run_app(json_data)
    res = '{"message": "error"}'
    if(result[0]):
        currentTime = strftime("%Y-%m-%d %H:%M:%S", gmtime())
        app = model.Model.App(app_name=json_data['app_name'], docker_file=result[1],
                              git_source_code=json_data['git_source_code'], image_base=json_data['programming_base'],
              min_instances=0, max_instances=10,state='running',started_time=currentTime)
        if model.Model.insert(app):
            res = '{"id": "'+str(app.id)+'", "name": "'+str(app.app_name)+'"}'
        else:
            res = '{"error":"duplicate application name: '+json_data['app_name']+'"}'
        return web.Response(status=200,body=res.encode('utf-8'))
    return web.Response(status=200,body=res.encode('utf-8'))

@asyncio.coroutine
def stop_app(request):
    '''
    [DELETE]
    :param request:
    :return:
    '''
    app_name = request.match_info.get('app_name')
    is_ok = App_Utils.stop_app(app_name)
    if(is_ok):
        return web.Response(status=200,body='OK'.encode('utf-8'))
    return web.Response(status=200,body='Error!'.encode('utf-8'))

@asyncio.coroutine
def add_service(request):
    '''
    [PUT]
    PUT Data Format:
    {'database': 'mysql', 'tools':[{'name': 'phpMyAdmin', 'version': '1.x'}, {'name': 'yy', 'version': 'zz'}]}
    :param request:
    :return:
    '''
    app_name = request.match_info.get('app_name')
    # TODO: check existed service by info in database
    # TODO: add new service to docker-compose and restart app
    return

@asyncio.coroutine
def remove_service(request):
    app_name = request.match_info.get('app_name')
    service_name = request.match_info.get('service_name')
    # TODO: check existed service by info in database
    # TODO: delete service record in database and docker-compose up
    return

@asyncio.coroutine
def restart_app(request):
    app_name = request.match_info.get('app_name')
    is_ok = App_Utils.restart_app(app_name)
    if(is_ok):
        return web.Response(status=200,body='OK'.encode('utf-8'))
    return web.Response(status=200,body='Error!'.encode('utf-8'))
    return

@asyncio.coroutine
def init(loop):
    app = web.Application(loop=loop)
    # Deploy app
    app.router.add_route('POST', '/apps', deploy_app)
    # Restart app
    app.router.add_route('PUT', '/apps/{app_name}/restart', deploy_app)
    # Stop app
    app.router.add_route('DELETE', '/apps/{app_name}', stop_app)
    # Add new service
    app.router.add_route('PUT', '/apps/{app_name}', add_service)
    # Remove existed service
    app.router.add_route('DELETE', '/apps/{app_name}/{service_name}', add_service)

    srv = yield from loop.create_server(app.make_handler(),HOST, PORT)
    print("Server started at "+HOST+":"+str(PORT))
    return srv

loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
try:
    loop.run_forever()
except KeyboardInterrupt:
    pass


import os, stat
from flask import Flask, flash, request, redirect, url_for, send_file, Response
from werkzeug.utils import secure_filename
import shutil
import threading
import queue
import time
import atexit

UPLOAD_FOLDER = './tmp'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'zip', '7z'}
num_files = 0
managerThread = threading.Thread()
commandQueue = queue.Queue()
clientRequestStatus = dict()
clientRequestStatusLock = threading.Lock()
quitEvent = threading.Event()
def createApp():
    app = Flask(__name__)
    global managerThread
    global commandQueue
    global clientRequestStatus
    global clientRequestStatusLock

    def manageQueue():
        while not quitEvent.wait(5.0):
            #print("checking queue, is there a request?", not commandQueue.empty())
            if not commandQueue.empty():  # execute next item in queue
                args = commandQueue.get()
                clientDir = args['clientDir']
                clientRequestStatusLock.acquire()
                clientRequestStatus[args['ip']] = 'running'
                clientRequestStatusLock.release()
                print("========================================")
                print("Processing request from: ", args['ip'])
                print("========================================")
                print(args['command'])
                rval = os.system(args['command'])
                print("========================================")
                print("Finished processing request from: ", args['ip'])
                print("returned value of ", rval)
                print("========================================")
                clientRequestStatusLock.acquire()
                if rval == 0:
                    clientRequestStatus[args['ip']] = 'finished'
                else:
                    clientRequestStatus[args['ip']] = "Error: " + str(rval)
                #print("client request status is now\n\t",clientRequestStatus)
                clientRequestStatusLock.release()
        return 0

    managerThread = threading.Thread(target=manageQueue)
    managerThread.start()

    return app
try:
    app = createApp()
    app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
    app.config['USE_CPU'] = False
    app.config['NET_NAME'] = "whiskersBW3"
except KeyboardInterrupt:
    print("got a keyboard interrupt")

def allowed_file(filename):
    return '.' in filename and \
        filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route("/")
def index():
    return redirect("./index.html")

@app.route("/hello")
def helloWorld():
    return "Hello World"

@app.route("/GETtest")
def getTest():
    return "successfully pinged"

@app.route('/App',methods=['POST'])
def upload_file():

    files = request.files.getlist('images')
    ip = request.remote_addr
    clientDir = app.config['UPLOAD_FOLDER'] + '/' + ip
    if not os.path.isdir(app.config['UPLOAD_FOLDER']):
        os.mkdir(app.config['UPLOAD_FOLDER'])

    if os.path.isdir(clientDir):
        for f in os.listdir(clientDir):
            p = os.path.join(clientDir, f)
            if os.path.isdir(p):
                shutil.rmtree(p)
            else:
                os.remove(p)
    else:
        os.mkdir(clientDir)
    if not os.path.isdir(os.path.join(clientDir, "images")):
        os.mkdir(os.path.join(clientDir, "images"))
    imagesDir = os.path.join(clientDir, "images")

    for f in files:
        name = secure_filename(f.filename)
        name = os.path.basename(name)
        # if this filename already exists
        path = os.path.join(imagesDir, name)
        if os.path.isfile(path):
            i = 0
            tmp, ext = os.path.splitext(name)
            while os.path.isfile(path):  # add "_i" until it is unique filename
                i += 1
                path = os.path.join(clientDir, tmp + "_{0}{1}".format(i, ext))
                print(path)
            name = tmp + "_" + str(i) + ext
        f.save(os.path.join(imagesDir, name))
    dataroot = imagesDir
    netname = app.config['NET_NAME']
    command = ("python ./pix2pix/test.py --dataroot " + dataroot + " " +
               "--results_dir " + clientDir+"/results/ "
               "--name " + netname + " --model test --netG unet_1024 --direction AtoB --dataset_mode single " +
               "--loadSize 1024 --fineSize 1024 --norm batch --use_upsample_conv --input_nc 1 --output_nc 1")
    if app.config["USE_CPU"]:
        command = command + " --gpu_ids -1"
    args = {'command': command, 'ip': ip, 'clientDir': clientDir, 'imagesDir': imagesDir}
    print("placing command in queue")
    commandQueue.put(args)
    clientRequestStatusLock.acquire()
    clientRequestStatus[ip] = 'received'
    print(clientRequestStatus)
    clientRequestStatusLock.release()
    return "received files"

@app.route('/CheckStatus',methods=['GET'])
def check_status():
    ip = request.remote_addr

    # check the status of their request and return
    clientRequestStatusLock.acquire()
    status = 'Not Found'
    if ip in clientRequestStatus:
        status = clientRequestStatus[ip]
    clientRequestStatusLock.release()
    return status

@app.route('/download',methods=['GET'])
def download_file():
    ip = request.remote_addr
    clientDir = app.config['UPLOAD_FOLDER'] + '/' + ip
    imagesDir = os.path.join(clientDir, "images")
    outputFolder = os.path.join(clientDir, "results/whiskersBW3/test_latest/images")
    # if their request is finished, return zip data to them
    files = [f for f in os.listdir(outputFolder) if os.path.isfile(os.path.join(outputFolder, f)) and
             f.find("fake") != -1]
    os.mkdir(os.path.join(clientDir, "laserfiles"))
    for f in files:
        newname = f.replace("_fake_B", "")
        os.rename(os.path.join(outputFolder, f), os.path.join(clientDir, "laserfiles", newname))
    shutil.make_archive(os.path.join(clientDir, "laserfiles"), 'zip', os.path.join(clientDir, "laserfiles"))
    shutil.rmtree(os.path.join(clientDir, "results"))
    shutil.rmtree(imagesDir)
    shutil.rmtree(os.path.join(clientDir, "laserfiles"))
    clientRequestStatusLock.acquire()
    clientRequestStatus.pop(ip)
    clientRequestStatusLock.release()
    return send_file(os.path.join(clientDir, "laserfiles.zip"), mimetype="application/zip")

if __name__ == '__main__':
    app.run(host='192.168.1.13',port='5000')


from django.conf import settings
from web.models import Order, LineItems
import requests
import json

def create_order_from_shopify_data(order_data):
    ''' Create order and cline items from the shopify data'''
    order = Order.objects.create(
        shopify_order_number= order_data['order_number'],
        courier_type= get_courier_type(order_data['shipping_lines']),
        total_paid = order_data['total_price'],
        discount_amount = order_data['total_discounts']
    )

    for order_item in order_data['line_items']:
        create_order_line_item(order, order_item)
    return order

def create_order_line_item(order, order_item):
    '''Create line item from order data'''
    (print_file_name, product_image_url )= get_product_info(order_item['product_id'])
    if len(order_item["discount_allocations"])>0:
        discount= order_item["discount_allocations"][0]['amount']
    else:
        discount="0"
    item = LineItems.objects.create(
        order = order,
        title = order_item['title'],
        shopify_line_item_id = str(order_item['id']),
        product_id = str(order_item['product_id']),
        variant_title = order_item['sku'],
        quantity = order_item['quantity'],
        image_url = product_image_url,
        print_file_name = print_file_name,
        price = order_item['price'],
        discount_total = discount
    )
    return item

def get_product_info(product_id):
    headers ={'Authorization': settings.STORE_TOKEN ,
              'Content-Type': "application/json"}
    url = '{}{}{}.json'.format(settings.STORE_URL,
                       '/admin/products/',
                       product_id)
    response = requests.get(url=url, headers=headers)
    product = json.loads(response._content)['product']
    return (product['product_type'],product['image']['src'])

def get_courier_type(shipping_lines):
    shipping_info = shipping_lines[0]
    type = shipping_info['title']
    if type == "Standard Shipping":
        return "Courier"
    return "Collection"


import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import model_from_json

import shap

import numpy as np
import pandas as pd

from flask import request
from flask import jsonify
from flask import Flask

import warnings
warnings.filterwarnings('ignore')

app = Flask(__name__)

def get_model():
    global model
    model = model_from_json(open('./model/model_architecture.json').read())
    model.load_weights('./model/model_weights.h5')
    model.compile(loss='binary_crossentropy',
                  optimizer=keras.optimizers.Adam(),
                  metrics=['accuracy'])
    print(" * Keras model loaded.")

def get_data():
    global data
    data = pd.read_csv("./data/iris_train.csv")
    print(" * Training data loaded.")

def compute_shap():
    global explainer
    explainer = shap.KernelExplainer(model.predict_proba, data)
    print(" * SHAP values computed.")

def abs_adimensional_jacobian_1output(x, model):
    x_tensor = tf.convert_to_tensor(x.reshape(1, -1), dtype=tf.float32)
    with tf.GradientTape() as g:
        g.watch(x_tensor)
        y_tensor = model(x_tensor)
    jacobian = g.jacobian(y_tensor, x_tensor)
    jacobian = jacobian.numpy()[0][0][0]
    input_by_loss = x / y_tensor.numpy()[0][0]
    adim_jacobian = np.multiply(jacobian, input_by_loss)
    return np.absolute(adim_jacobian)

def weigh_instance_by_sensitivity(x, model):
    sens_wts = abs_adimensional_jacobian_1output(x, model)
    weighted_x = np.multiply(sens_wts, x)
    return weighted_x

def weigh_matrix_by_sensitivity(reference, model):
    return np.apply_along_axis(weigh_instance_by_sensitivity,
                               1,
                               reference, model=model)

def min_weighted_euclidean_dist(query, reference, model):
    weighted_query = weigh_instance_by_sensitivity(query, model)
    weighted_ref = weigh_matrix_by_sensitivity(reference, model)
    dists = np.apply_along_axis(np.linalg.norm,
                                1,
                                (weighted_ref - weighted_query))
    min_dist = min(dists)
    return min_dist

def in_forbidden_zone(query):
    return np.any(query < 0)

def compute_novelty_index(query, nov_deno, reference, model):
    if(in_forbidden_zone(query)):
        return 10000.0
    nov_nume = min_weighted_euclidean_dist(query, reference, model)
    return nov_nume / nov_deno

print(" * Loading Keras model...")
get_model()
print(" * Loading Training data...")
get_data()
print(" * Computing SHAP values...")
compute_shap()

@app.route("/predict", methods=['POST'])
def predict():
    message = request.get_json(force=True)
    sepal_length = float(message['sepal_length'])
    sepal_width = float(message['sepal_width'])
    petal_length = float(message['petal_length'])
    petal_width = float(message['petal_width'])

    query = np.array([sepal_length, sepal_width,
                      petal_length, petal_width]).reshape(1, -1)

    novelty = compute_novelty_index(query, 1.7835123384693827,
                                    data.to_numpy(), model)

    y_pred = model.predict(query).tolist()

    query_pd = pd.DataFrame(query, columns=data.columns)
    shap_values = explainer.shap_values(query_pd, nsamples=100)

    response = {
        'novelty': novelty,
        'prediction': y_pred[0][0],
        'baseValue': explainer.expected_value[0],
        'featureEffects': {
            '0': shap_values[0][0, :][0],
            '1': shap_values[0][0, :][1],
            '2': shap_values[0][0, :][2],
            '3': shap_values[0][0, :][3]
        }
    }

    return jsonify(response)


 #输入13:21打印13到21行，输入:21打印前21行，输入21:打印第21行到文件尾部，输入:打印全部
def read_file(path, target):
    if target.strip() == ':':
        prompt = 'All the paper:'
        file_all = open(path)
        file_all_list = list(file_all)
        for each_line in file_all_list:
            print(each_line)
    else:
        (start, end) = target.split(":")

        if start == '':
            prompt = 'From start to line %s:' % end
        elif end == '':
            prompt = 'From line %s to the end:' % start
        else:
            prompt = 'From line %s to line %s:' % (start, end)

        print('\n%s\n' % prompt)

        file = open(path)
        if end == '':
            for i in range(int(start)):
                file.readline()
            print(file.readline())  #此处有误

path = input("input the file path: ")
target = input("what do you want to print: ")
read_file(path, target)

未完成_输出指定行数.py

未完成_输出指定行数.py


"""empty message

Revision ID: 376773551960
Revises: 25449f89e610
Create Date: 2014-01-22 12:37:23.444807

"""

# revision identifiers, used by Alembic.
revision = '376773551960'
down_revision = '25449f89e610'

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('watchlists',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('user_id', sa.Integer(), nullable=False),
        sa.Column('movie_id', sa.Integer(), nullable=False),
        sa.Column('path', sa.String(length=255), nullable=True),
        sa.Column('status', sa.String(length=10), nullable=False),
        sa.Column('vote', sa.Integer(), nullable=True),
        sa.Column('created_on', sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(['movie_id'], [u'movies.id'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['user_id'], [u'users.id'], ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('user_id', 'movie_id', name='watchlist_movie_user_uc')
    )
    op.create_index('ix_watchlists_status', 'watchlists', ['status'], unique=False)
    ### end Alembic commands ###

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_index('ix_watchlists_status', 'watchlists')
    op.drop_table('watchlists')
    ### end Alembic commands ###


import argparse
import os
import subprocess

from mel2samp import files_to_list

if __name__ == "__main__":
    # Get defaults so it can work with no Sacred
    parser = argparse.ArgumentParser()
    parser.add_argument('-f', "--filelist_path", required=True)
    parser.add_argument('-o', '--output_dir', type=str,
                        help='Output directory')
    args = parser.parse_args()

    filepaths = files_to_list(args.filelist_path)

    for filepath in filepaths:
        source_wav = filepath
        filename = os.path.basename(filepath)
        dest_wav = f'{args.output_dir}/{filename}'
        command = f'sox {source_wav} {dest_wav} remix 1,2 rate 22050'
        subprocess.run(command, shell=True)


from lang.validators import TextValidator, DecimalValidator, BaseValidator

__author__ = 'Alen Suljkanovic'

def action_processor(action):
    """
    Action processor
    """
    print(str(action.expression.second_operand.first_operand.name))

def property_processor(property):
    """
    Property processor
    """
    def resolve_date(args):
        for arg in args:
            if arg.name == "format":
                print("format value %s" % arg.value)

    def _validate(prop):
        validator = None
        if prop.type == "string" or prop.type == "combo":
            validator = TextValidator(prop)
        elif prop.type == "decimal":
            validator = DecimalValidator(prop)
        else:
            validator = BaseValidator(prop)

        validator.validate()

    _validate(property)

    if not hasattr(property, "django_field"):
        setattr(property, "django_field", None)

    django_mappings = {
        "string": "CharField",
        "text": "TextField",
        "int": "IntegerField",
        "float": "FloatField",
        "decimal": "DecimalField",
        "date": "DateField",
        "datetime": "DateTimeField",
        "combo": "CharField"
    }

    if property.type == "combo":
        choices = property.args_dict["choices"]
        choices_data = choices.value.split(",")
        # value for 'choices' argumet is string, so create list of tupples
        new_value = []
        for data in choices_data:
            short_name, long_name = data.split(":")
            new_value.append((short_name, long_name))

        choices.value = tuple(new_value)
        # Dynamically add max length if it's not declared
        if "max_length" not in property.args_dict:
            from lang.meta import PropertyArgument
            arg = PropertyArgument(property, "max_length", len(new_value))
            property.arguments.insert(0, arg)

    if property.type in django_mappings:
        property.django_field = django_mappings[property.type]

def property_argument_processor(prop_argument):
    """
    Property argument processor.
    """
    if prop_argument.name == "unique":
        if not prop_argument.value:
            prop_argument.value = True

def class_processor(_class):
    """
    Class processor
    """
    # print(_class)
    pass

def model_processor(model):
    """
    Model processor
    """
    # Dictionary of all classes in model
    all_classes = {c.name: c for c in model.classes}

    for c in model.classes:
        for p in c.properties:
            if p.type in all_classes and not c.session:
                if p.list:
                    ref_class = all_classes[p.type]
                    ref_class.foreign_key = c


import cv2
import ctypes
import base64
from PIL import Image
from io import BytesIO
import json
from datetime import datetime

if __name__ == '__main__':
    so = ctypes.cdll.LoadLibrary("/home/adminpc/jiaopan/projects/detector-tensorrtx-linux/build/libdetector.so")
    model_path = bytes("/home/adminpc/jiaopan/projects/detector-tensorrtx-linux/build/yolov5s.engine", "utf-8")
    so.init(model_path)
    image = bytes("/home/adminpc/jiaopan/resource/bus.jpg", "utf-8")
    detectFile = so.detectByFile
    detectFile.restype = ctypes.c_char_p
    start = datetime.now()
    result = detectFile(image, ctypes.c_float(0.5))
    end = datetime.now()
    print("time cost:",(end-start).total_seconds())

    result = ctypes.string_at(result, -1).decode("utf-8")
    print(result)

    image = cv2.imread("/home/adminpc/jiaopan/resource/bus.jpg")
    result = json.loads(result)
    data = result["data"]
    for box in data:
        x = int(box["location"]["x"])
        y = int(box["location"]["y"])
        width = int(box["location"]["width"])
        height = int(box["location"]["height"])
        cv2.putText(image, box["label"], (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 255, 255), 1)
        cv2.rectangle(image,(x,y),(x+width,y+height),(255,0,0),2)
    cv2.imwrite("output.jpg",image)


# 给你两个有序整数数组 nums1 和 nums2，请你将 nums2 合并到 nums1 中，使 nums1 成为一个有序数组。
#
#
#
#  说明:
#
#
#  初始化 nums1 和 nums2 的元素数量分别为 m 和 n 。
#  你可以假设 nums1 有足够的空间（空间大小大于或等于 m + n）来保存 nums2 中的元素。
#
#
#
#
#  示例:
#
#  输入:
# nums1 = [1,2,3,0,0,0], m = 3
# nums2 = [2,5,6],       n = 3
#
# 输出: [1,2,2,3,5,6]
#  Related Topics 数组 双指针
#  👍 563 👎 0

# leetcode submit region begin(Prohibit modification and deletion)
class Solution(object):
    def merge(self, nums1, m, nums2, n):
        """
        :type nums1: List[int]
        :type m: int
        :type nums2: List[int]
        :type n: int
        :rtype: None Do not return anything, modify nums1 in-place instead.
        """
        """
        1.合并后再排序
        时间复杂度 : O((n + m)log(n + m))
        空间复杂度:O(1)
        """
        # for i in range(m, m+n):
        #     nums1[i] = nums2[i-m]
        # nums1.sort()
        """
        2.双指针法，从前往后
        时间复杂度：O(N+M)
        空间复杂度：O(m)
        """
        arr = [0 for _ in range(m+n)]
        i = 0
        p = 0
        q = 0
        while p < m or q < n:
            # 注意两个边界条件
            if p == m:
                arr[i] = nums2[q]
                q += 1
            elif q == n:
                arr[i] = nums1[p]
                p += 1
            elif nums1[p] <= nums2[q]:
                arr[i] = nums1[p]
                p += 1
            else:
                arr[i] = nums2[q]
                q += 1
            i += 1
        for j in range(len(arr)):
            nums1[j] = arr[j]

# leetcode submit region end(Prohibit modification and deletion)
if __name__ == '__main__':
    import copy
    # nums1 = [1,2,3,0,0,0]
    # nums2 = [2,5,6]
    # nums0 = copy.deepcopy(nums1)
    # nums1 = []
    # print(nums1,nums0)
    # p = 0
    # q = 0
    # while p < 3 and q < 3:
    #     if nums0[p] < nums2[q]:
    #         nums1.append(nums0[p])
    #         p += 1
    #     else:
    #         nums1.append(nums2[q])
    #         q += 1
    # if p < 3:
    #     nums1[p + q:] = nums0[p:]
    # if q < 3:
    #     nums1[p + q:] = nums2[q:]
    # print(nums1)

[88]合并两个有序数组.py


from modeller import *
from modeller.automodel import *    # Load the AutoModel class

log.verbose()
env = Environ()

# directories for input atom files
env.io.atom_files_directory = ['.', '../atom_files']

a = LoopModel(env, alnfile = 'alignment.ali',
              knowns = '7dun', sequence = '7dun_fill')
a.starting_model= 1
a.ending_model  = 1

a.loop.starting_model = 1
a.loop.ending_model   = 2
a.loop.md_level       = refine.fast
a.make()


import sys
import os
import hashlib
import json
import time
import pprint

from chunk import MAX_CHUNK_SIZE
from chunk import chunks_to_partition

# group 0
# pc1 300M
# pc2 1500M
# group0_quota = [1024*1024*300+12, 1024*1024*1600]
group0_quota = [1000*1000*300+12, 1000*1000*1600]
group0_device_no = len(group0_quota)
group0_current_device_index = 0

new_group0_quota = [1024*1024*150, 1024*1024*1600]
new_group0_device_no = len(new_group0_quota)
quota = group0_quota[group0_current_device_index]
# group 1
# pc3 200M
# pc4 300M

if __name__ == '__main__':
    print(sys.argv[2:])
    folder_meta_hash = sys.argv[1]
    folder_meta_json = open('pc1/meta/%s' % folder_meta_hash, 'rb').read()
    folder_meta_data = json.loads(folder_meta_json)

    print('folder_meta_hash', folder_meta_hash, len(folder_meta_json))
    pprint.pprint(folder_meta_data)

    chunks_to_move = {}
    for file_meta_data in folder_meta_data['items']:
        # print(file_meta_data)
        chunks = file_meta_data[4]
        # print(chunks)
        for chunk in chunks:
            device = chunk[2]
            chunks_to_move.setdefault(device, set())
            chunks_to_move[device].add(tuple(chunk))
    print('chunks_to_move fill')
    pprint.pprint(chunks_to_move)

    new_group0_quota_left = []
    for device, quota in enumerate(new_group0_quota):
        chunks_to_move.setdefault(device, set())
        if device < group0_device_no:
            chunks_to_stay = set()
            for chunk in chunks_to_move[device]:
                # chunk_hash = chunk[0]
                chunk_size = chunk[1]
                if quota >= chunk_size:
                    quota -= chunk_size
                    # print(quota)
                    chunks_to_stay.add(chunk)
            chunks_to_move[device] -= chunks_to_stay
            # pprint.pprint(chunks_to_stay)
        new_group0_quota_left.append(quota)
    print('chunks_to_move stay')
    pprint.pprint(chunks_to_move)

    chunks_to_move, new_group0_quota_left = chunks_to_partition(chunks_to_move[0], new_group0_quota_left)
    print('chunks_to_move')
    pprint.pprint(chunks_to_move)

    # folder_meta_data_items = []
    for file_meta_data in folder_meta_data['items']:
        # print(file_meta_data)
        chunks = file_meta_data[4]
        # print(chunks)
        new_chunks = []
        for chunk_hash, chunk_size, device in chunks:
            chunk = tuple([chunk_hash, chunk_size, device])
            if chunk in chunks_to_move:
                new_chunks.append([chunk_hash, chunk_size, chunks_to_move[chunk]])
            else:
                new_chunks.append([chunk_hash, chunk_size, device])
        file_meta_data[4] = new_chunks
        # folder_meta_data_items.append(file_meta_data)
    # folder_meta_data['items'] = folder_meta_data_items
    pprint.pprint(folder_meta_data)
    folder_meta_json = json.dumps(folder_meta_data).encode()
    folder_meta_hash = hashlib.sha256(folder_meta_json).hexdigest()
    with open('pc1/meta/%s' % folder_meta_hash, 'wb') as f:
        f.write(folder_meta_json)
    print('new folder_meta_hash', folder_meta_hash, len(folder_meta_json))

    print('group0_quota         ', group0_quota)
    print('new_group0_quota     ', new_group0_quota)
    print('new_group0_quota_left', new_group0_quota_left)


# ---------------------- REQUIREMENTS ---------------------- #

# Documentation: https://python-docx.readthedocs.io/en/latest/
import docx
from docx import Document
import xlsxwriter
import datetime
import os

# ---------------------- READ FILE ---------------------- #

# Get input file from user
# clippings_file = input("\nPlease enter path to clippings file: \n------------------------------------------\n")
clippings_file = "My Clippings.txt"

# Open file
text = open(clippings_file, mode="r", encoding ='utf-8-sig')

# Create list containing each individual line
lines = text.readlines()

# Close file
text.close()

# ---------------------- USEFUL FUNCTIONS ---------------------- #

def get_title(section):
    title = section[0].split("(", 1)[0].strip()
    return title

def get_authors(section):
    authors = section[0].split("(", 1)[1].replace(")","").strip().split(";")
    return authors

def get_location(section):
    location = section[1].split("Added on", 1)[0].replace("- Your Highlight ","").strip()
    if "at location" in location:
        index = location.find("at location") + 12
    elif "on page" in location:
        index = location.find("location") + 9

    location = location[index:index + 10]
    if "-" in location:
        location = location.split("-")[0]
    location = location.replace("|","").strip()

    return int(location)

def get_date(section):
    date = section[1].split("Added on", 1)[1].split(", ", 1)[1]
    return date

def get_quote(section):
    quote = section[3].strip()
    return quote

# ---------------------- 1. EXTRACT INFORMATION ---------------------- #

# List of dicts to store structured information
sections = []

# Temp value to store current section
section = []

# Iterate over each line
for line in lines:

    # If separator is found, extract information from section and reset section
    if "==========" in line:
        information = {
            "title": get_title(section),
            "authors" : get_authors(section),
            "location" : get_location(section),
            "date" : get_date(section),
            "quote" : get_quote(section),
        }

        # Add structured information to list of dicts
        sections.append(information)

        # Reset temp section
        section = []

    # Else continue adding lines to the existing section
    else:
        section.append(line)

# Order list of quotes based on title and location
sections = sorted(sections, key = lambda i: (i["title"] , i['location']))

### -------------------------------------- 2. WRITE ALL QUOTES TO EXCEL -------------------------------------- ###

today = datetime.date.today()

try:
    os.mkdir("clips_" + str(today))
except:
    print("<!> Folder could not be created.\n")
else:
    print("<!> Folder was successfully created.\n")
workbook = xlsxwriter.Workbook('clips_' + str(today) + '/#clippings_ ' + str(today) + '.xlsx')
worksheet = workbook.add_worksheet('Clippings')

# Set filter on headers
worksheet.autofilter('A1:E1')

# Set the columns widths
worksheet.set_column('A:A', 30)
worksheet.set_column('B:B', 20)
worksheet.set_column('C:C', 15)
worksheet.set_column('D:D', 120)
worksheet.set_column('E:E', 30)

# Set row height
worksheet.set_row(0, 20)

# Add a header format
header = workbook.add_format()
header.set_bold(1)
header.set_font_color('white')
header.set_bg_color('#4E4E4E')
header.set_align('center')
header.set_align('vcenter')

# Add a cell format
cell_format = workbook.add_format()
cell_format.set_text_wrap()
cell_format.set_align('center')
cell_format.set_align('vcenter')

# Add an Excel date format.
date_format = workbook.add_format({'num_format': 'mmmm d yyyy'})
date_format.set_text_wrap()
date_format.set_align('center')
date_format.set_align('vcenter')

# Add a wrap format.
wrap = workbook.add_format({'num_format': 'mmmm d yyyy'})
wrap.set_text_wrap()
wrap.set_align('center')
wrap.set_align('left')

# Write some data headers.
worksheet.write('A1', 'Book Title', header)
worksheet.write('B1', 'Location', header)
worksheet.write('C1', 'Date', header)
worksheet.write('D1', 'Quote', header)
worksheet.write('E1', 'Authors', header)

# Start from the first cell. Rows and columns are zero indexed.
row = 1
col = 0

# Iterate over the data and write it out row by row.
for section in sections:
    worksheet.write(row, col, section["title"], cell_format)
    worksheet.write(row, col + 1, section["location"], cell_format)

    # Convert the date string into a datetime object.
    worksheet.write(row, col + 2, section["date"], date_format)
    worksheet.write(row, col + 3, section["quote"], wrap)
    worksheet.write(row, col + 4, str(section["authors"])[1:-1].replace("'", ""), wrap)
    row += 1

print(f"___ EXCEL ___ \n We created #{row-1} rows in the Excel file. You can find 'clippings.xlsx' in the current folder.\n")
workbook.close()

all_books = []

# Get unique title values
for section in sections:
    if section["title"] in all_books:
        continue
    else:
        text = section["title"]
        all_books.append(text)

def list_books(all_books):

    # ---------------------- 3. SHOW USER LIST OF BOOKS ---------------------- #

    # Present list of books to user
    print("\nAvailable Books:\n---------------------------")

    for book in all_books:
        print("->", book)

def choose_books(all_books):

    # ---------------------- 4. USER SELECTS BOOK ---------------------- #

    # Get input from user
    title = input("\nEnter desired book title: \n-------------------------\n")

    # Find book titles matching user input
    selected_books = [book for book in all_books if title in book]

    # If no match found, signal error
    if len(selected_books) == 0:
        print("\n<!> We didn't find any books.\n")
        decision = "n"

    # If at least one match found, confirm list with user
    else:
        print("\nWe found the following books: \n------------------------------")
        for i in selected_books:
            print("->", i)

        decision = input("\nAre you sure you want to select these books?(y/n)\n--------------------------------------------\n")

        # If user says yes, go ahead and create the file
        if decision.lower() == "y" or decision.lower() == "yes":
            write_books(title, selected_books)
        else:
            return False

def write_books(title, selected_books):

# ---------------------- 5. WRITE QUOTES FROM BOOK IN WORD DOCUMENT ---------------------- #

    # Create file
    document = docx.Document()

    # Write book title
    document.add_heading('Selected books:', level = 1)

    # Write list of selected books
    document.add_paragraph(selected_books , style='List Bullet')

    # Filter list of quotes based on title
    sections_filter = [section for section in sections if section["title"] in selected_books]

    # Write authors (if one book selected only)

    # Write header
    document.add_heading('Authors:', level = 1)

    # Write list of authors
    for author in sections_filter[0]["authors"]:
        document.add_paragraph(author, style='List Bullet')

    # formatting...
    document.add_paragraph("")
    document.add_heading('Highlights', level = 1)
    document.add_paragraph("")

    # Write list of quotes associated with selected book(s)
    for section in sections_filter:
        document.add_paragraph(section["quote"] + " (" + str(section["location"]) + ")")

        # formatting...
        document.add_paragraph("______________________________")
        document.add_paragraph("")

    # Save file to disk
    document.save("clips_" + str(today) + "/"+title+".docx")

    # Confirm success to user
    print("\n<!> File was successfully created: '{0}.docx'".format(title))

    # Ask if he wishes to start over
    #choice = input("\nWould you like to try another book?(y/n)\n--------------------------------------------\n")
    #if choice.lower() == "y" or choice.lower() == "yes":
    #    choose_books(all_books)
    #else:
    #    exit

# Show all books to user
#list_books(all_books)

# Ask user to select book
#choose_books(all_books)

for book in all_books:
    title = book.replace(":", "").strip()[0:56]
    title = "".join(title)
    write_books(title, book)


import turtle as t
x = -100
y = 100
for line in range(10):
    t.penup()
    t.goto(x, y)
    t.pendown()
    x_t = x
    y_t = y
    for i in range(1, line + 2):
        t.write(str(i) + "  ", font = ("微软雅黑", 20, "normal"))
        x_t += 40
        t.penup()
        t.goto(x_t, y_t)
        t.pendown()
    y -= 30
t.hideturtle()
t.done()


#udp cleint from https://docs.python.org/3/library/asyncio-protocol.html#udp-echo-client-protocol

import asyncio as asyncio
import time
import queue
import json
import reedsolo

protocol_start_time = time.time()

fps = [30, 30, 30]
timeout_time = 5
tick_length = .01
latency_time_allowed = .020 #second, target for ml models
e = latency_time_allowed / tick_length
time_delay_client_timestamps = 0
fec = 0
print("fps ", fps)

def data_generator(original_time, fps):
  current_time = time.time()
  lst = []
  append = lst.append
  for index in range(len(fps)):
    f = fps[index]
    for i in range(f):
      #use i/f instead of wait(1/f) after list append for code to run faster
      append([((current_time + index - original_time + i/f + time_delay_client_timestamps))//tick_length, 100])
  return lst

def local_data(original_time=None):
    data = data_generator(original_time, fps)
    pq = queue.PriorityQueue(maxsize = 2000)
    pqs.insert(0,pq)#tuples empty without this. Why doesnt time_sync work when reference_queue isnt local queue?
    add_packets_to_queue(pq, data)
    return original_time
def time_sync():
    t = []
    if len(pqs) == 0:
        return t
    if len(pqs) == 1:
        return list(pqs[0].queue)
    reference_queue = pqs.pop()
    times = time.time
    append = t.append
    while reference_queue.qsize() > 0:
            reference_packet = reference_queue.get()
            timeout = times() + timeout_time
            for q in pqs:
                if timeout - times() >= 0:
                    q_packet = sync_packet(reference_packet, q)
                    if q_packet != None:
                        append((reference_packet, q_packet))
    return t

def queue_not_empty(lst_queues):
    for q in lst_queues:
        if q.qsize() > 0:
            return True
    return False
def sync_packet(reference_packet, queue):
    for p in queue.queue:
        if abs(p.time - reference_packet.time) <= e :
            queue.get(p)
            return p
    print("did not sync data")
    return None

def add_packets_to_queue(queue, data):
    put = queue.put
    p = lambda t: packet(t[0], t[1])
    packets = map(p, data)
    for pack in packets:
        put(pack)
#only one queue here in current test

class packet(object):
    def __init__(self, time, data):
        self.time = time
        self.data = data
    def __eq__(self, other):
        return other != None or  self.time == other.time
    def __ne__(self,other):
        return other == None or self.time != other.time
    def __lt__(self,other):
        return self.time < other.time
    def __le__(self,other):
        return self.time <= other.time
    def __gt__(self,other):
        return self.time > other.time
    def __ge__(self,other):
        return self.time >= other.time
    def __repr__(self):
        return "time" + ": "+str(self.time) + ", data: " + str(self.data)

class EchoClientProtocol:
    def __init__(self, message, loop, tick_length = 1):
        self.message = message
        self.loop = loop
        self.transport = None
        self.connection_time = time.time()
        self.tick_length = tick_length
        self.qs = {}

    def connection_made(self, transport):
        # print("udp connction made")
        global udp_time_start
        udp_time_start = time.time()

        self.transport = transport
        # print('Send:', self.message)
        self.transport.sendto("request data".encode())

    def datagram_received(self, data, addr):
        global time_data_recieved_start
        global time_data_recieved_and_loaded
        time_data_recieved_start = time.time()
        time_fec_starts = 0
        if (fec > 0):
            rs = reedsolo.RSCodec(fec)
            time_fec_starts = time.time()
            data =  rs.decode(data)
        time_json_start = time.time()
        original_message = json.loads(data.decode())
        time_for_fec_and_dejson_end = time.time()
        if addr not in self.qs.keys():
            self.qs[addr] = queue.PriorityQueue(maxsize = 2000)
            pqs.append(self.qs[addr])
        # print("recieved data")
        add_packets_to_queue(self.qs[addr], original_message)
        time_data_recieved_and_loaded = time.time()
        time_packet_sync_start = time.time()
        tuples = time_sync()
        end_time = time.time()
        analyze_tuples(tuples, end_time, time_data_recieved_start, \
            time_data_recieved_and_loaded, time_packet_sync_start, \
            time_for_fec_and_dejson_end, time_fec_starts, time_json_start)
        # print("Close the socket")
        self.transport.close()
    def get_time():
        return (time.time() - original_time) // tick_length
    def get_time_from_message(message):
        return int(message)
    def error_received(self, exc):
        print('Error received:', exc)
    def connection_lost(self, exc):
        print('The server closed the connection')
        print('Stop the event loop')
        # self.loop.stop()

class ControllerClientProtocol(asyncio.Protocol):
    def __init__(self, message, loop):
        self.message = message
        self.loop = loop

    def connection_made(self, transport):
        global time_timesync_started
        self.transport = transport
        self.transport.write("request to sync time".encode())
        self.sync_time = time.time()
        time_timesync_started = time.time()
        #transport.write(self.message.encode())
        #print('Data sent: {!r}'.format(self.message))

    def data_received(self, data):
        global time_timesync_ended
        message = data.decode()
        # print(message)
        psuedo_time = float(message)
        latency = (time.time() - self.sync_time) / 2
        original_time = ((time.time()- latency)//tick_length - psuedo_time) * tick_length
        time_timesync_ended = time.time()
        local_data(original_time)
        self.transport.close()

    def connection_lost(self, exc):
        print('The server closed the connection')
        print('Stop the event loop')
        self.loop.stop()

# def keep_track_of_tuples(t, end_time, time_data_recieved_start, \
#     time_data_recieved_and_loaded, time_packet_sync_start, \
#     time_for_fec_and_dejson_end, time_fec_starts, time_json_start):
#     lst_end_time.append(end_time)
#     lst_time_data_recieved_start.append(time_data_recieved_start):
def analyze_tuples(t, end_time, time_data_recieved_start, \
    time_data_recieved_and_loaded, time_packet_sync_start, \
    time_for_fec_and_dejson_end, time_fec_starts, time_json_start):
    # print("tuples: ", t)
    # print("client total tuples of frames: ", len(t))
    print("seconds took for time handshake: ", time_timesync_ended - time_timesync_started)
    print("seconds since recieved data to packets being synced: ", end_time - time_data_recieved_start)
    print("seconds from udp connection made to data transfered, loaded in original form, and in queue: ", time_data_recieved_and_loaded - udp_time_start)
    print("seconds from udp connection made to data recieved : ", time_data_recieved_start - udp_time_start)
    print("seconds from udp data transfered to data in queue : ", time_data_recieved_and_loaded - time_data_recieved_start)
    print("seconds to decode json: ", time_for_fec_and_dejson_end - time_json_start)
    if (fec > 0):
        print("time fec: ", time_json_start - time_fec_starts)
    print("seconds for packet sync code to run :", end_time - time_packet_sync_start)
    print("seconds for experiment to run", end_time - protocol_start_time)
    print("average tuples / frames per second combining local and remote: ",len(t)/ (len(fps)))

pqs=[]
original_time = None
time_data_recieved_start = None
time_timesync_ended = None
time_timesync_started = None

def run_test():
    loop = asyncio.get_event_loop()
    message = 'Hello World!'
    coro = loop.create_connection(lambda: ControllerClientProtocol(message, loop),
                              '127.0.0.1', 8889)
    client = loop.run_until_complete(coro)
    loop.run_forever()
# loop.close()

# loop = asyncio.new_event_loop()
    message = "udp message!"
    connect = loop.create_datagram_endpoint(
        lambda: EchoClientProtocol(message, loop),
        remote_addr=('127.0.0.1', 9999))
    transport, protocol = loop.run_until_complete(connect)
    loop.run_forever()
    transport.close()
    loop.close()
run_test()


from pykafka import KafkaClient
from pykafka.simpleconsumer import OffsetType
import logging

logging.getLogger("pykafka.broker").setLevel('ERROR')

client = KafkaClient(hosts="localhost:9092")
topic = client.topics["service-calls"]

consumer = topic.get_balanced_consumer(
    consumer_group = b'pytkafka-test-2',
    auto_commit_enable = False,
    auto_offset_reset=OffsetType.EARLIEST,
    zookeeper_connect = 'localhost:2181'
)

#Print out the messages
for message in consumer:
    if message is not None:
        print(message.value.decode('utf-8'))


import unittest

from vector.Vector import Vector

class TestVectorProjection(unittest.TestCase):
    def test_projection(self):
        x = Vector((0, 4))
        y = Vector((2, 3))

        # y projection on x
        expected = (0, 3)
        result = y.projection_on(x).coordinates()
        self.assertEqual(expected, result, 'Vector Y projection on X is incorrect')

        # x projection on y
        ratio = (3 * 4) / (2 ** 2 + 3 ** 2)
        expected = (ratio * 2, ratio * 3)
        result = x.projection_on(y).coordinates()
        self.assertEqual(expected, result, 'Vector X projection on Y is incorrect')

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


from pathlib import Path
path = Path(__file__).parent / "../../input.txt"

startTimestamp = 0
with path.open("rt") as f:
    data = f.readlines()

startTimestamp = int(data[0].strip("\n"))
busesRaw = data[1].strip("\n").split(",")
buses = []

for x in range(0,len(busesRaw)):
    if busesRaw[x].isnumeric():
        buses.append([int(busesRaw[x]),x])

#sort buses
buses.sort(reverse=True)

#find first possible ts
currentTimestamp = startTimestamp
found = False
while not found:
    if ((currentTimestamp + buses[0][1]) % buses[0][0]) == 0:
        found = True
    else:
        currentTimestamp += 1

largestCount = 0
while (True):
    count = 0
    for bus in buses:
        if ((currentTimestamp + bus[1]) % bus[0]) != 0:
            break
        else:
            count += 1
    if count == len(buses):
        break
    if count > largestCount:
        largestCount = count
    currentTimestamp += buses[0][0]
print(currentTimestamp)
pass


'''
Copyright (C) 2018-2021  Bryant Moscon - bmoscon@gmail.com

Please see the LICENSE file for the terms and conditions
associated with this software.
'''

from copy import deepcopy

from yapic import json

from cryptofeed import FeedHandler
from cryptofeed.backends.zmq import BookZMQ, TickerZMQ, BookDeltaZMQ
from cryptofeed.callback import BookCallback, BookUpdateCallback
from cryptofeed.defines import L2_BOOK, TICKER, BOOK_DELTA, BID, ASK
from cryptofeed.exchanges import Coinbase, Kraken, Bittrex

import pprint
pp = pprint.PrettyPrinter(indent=4)

import zmq
import zmq.asyncio

class DeltaBookZMQ(object):
    def __init__(self, name, host='127.0.0.1', port=6000, **kwargs):
        self.book = None
        self.name = name
        self.L2 = {L2_BOOK: BookCallback(self.handle_book),
                   BOOK_DELTA: BookUpdateCallback(self.handle_l2_delta)}
        url = "tcp://{}:{}".format(host, port)
        ctx = zmq.asyncio.Context.instance()
        self.con = ctx.socket(zmq.PUB)
        self.con.bind(url)

    def check_books(self, master):
        """Check that master is equal to self.book."""
        for side in (BID, ASK):
            if len(master[side]) != len(self.book[side]):
                return False

            for price in master[side]:
                if price not in self.book[side]:
                    return False

            for price in self.book[side]:
                if price not in master[side]:
                    return False
        return True

    async def handle_book(self, feed, symbol, book, timestamp, receipt_timestamp):
        """Handle full book updates."""
        if not self.book:
            self.book = deepcopy(book)
            print("%s: Book Set" % self.name)
        else:
            assert(self.check_books(book))
            print("%s: Books match!" % self.name)

    async def handle_l2_delta(self, feed, symbol, update, timestamp, receipt_timestamp):
        """Handle L2 delta updates."""
        # handle updates for L2 books
        dic = {}
        for side in (BID, ASK):
            for price, size in update[side]:
                if size == 0:
                    del self.book[side][price]
                else:
                    self.book[side][price] = size
        ask, ask_volume = self.book[ASK].peekitem(index=-1)
        bid, bid_volume = self.book[BID].peekitem(index=0)
        dic = {
            'type': 'ticker',
            'feed': self.name.lower(),
            'pair': str(symbol),
            'timetamp': timestamp,
            'time_received': receipt_timestamp,
            'ask': str(ask),
            'ask_volume': str(ask_volume),
            'bid': str(bid),
            'bid_volume': str(bid_volume),
        }
        await self.con.send_string(json.dumps(dic))
        pp.pprint(dic)

    def parse(self, book):
        dic = {}
        dic.append(book[ASK])
        dic['bid'] = book[BID]
        return dic

def main():
    try:

        f = FeedHandler()
        #f.add_feed(Kraken(max_depth=1, channels=[L2_BOOK], symbols=['ETH-USD'], callbacks={L2_BOOK: BookZMQ(port=5678)}))
        f.add_feed(Bittrex(max_depth=1, channels=[L2_BOOK], symbols=['BTC-USD'], callbacks=DeltaBookZMQ("Bittrex", port=6000).L2))

        f.run()

    finally:
        print('done!')

if __name__ == '__main__':
    main()