from rest_framework.views import APIView
from rest_framework.generics import ListAPIView
from rest_framework import status
from rest_framework.response import Response
from rest_framework.decorators import permission_classes
from django.http import JsonResponse
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from django.contrib.auth.models import User
from blockchain_project.models import DeployRequest
from blockchain_project.serializers.deploy_serializer import DeploymentRequestSerializer

from rest_framework import serializers
from blockchain_project.views.blockchain import deployOneFundraiseContract,CharityConfig
from web3 import Web3, HTTPProvider,eth
from blockchain_project.models import DeployedCharityContract
from web3.contract import ConciseContract

class DeployRequestView(APIView):
    '''
    用户请求代部署合约
    POST /api/fundraises
    ["name","owner_address","target_money","ipfs_hashes"]

    用户撤回请求代部署：
    DELETE /api/fundraises/delete/{id}

    用户查询代部署合约的申请记录
    GET /api/fundraises
    '''

    def post(self, request, format=None):
        # request.data 无法修改，重新复制一份data
        InfoDict=request.data.copy()
        InfoDict['state']=0
        InfoDict['owner']=User.objects.get(username=str(request.user)).id
        serializer = DeploymentRequestSerializer(data=InfoDict)

        if serializer.is_valid():
            serializer.save()
            return Response(status=status.HTTP_201_CREATED)
        else:
            print(serializer.errors)
            return Response(status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, pk, format=None):
        deployments = DeployRequest.objects.filter(id=pk,owner=request.user.id)
        if len(deployments) == 0:
            return Response(status=status.HTTP_404_NOT_FOUND)
        deployment = deployments.get(id=pk)
        deployment.delete()
        return Response(status=status.HTTP_200_OK)

    def get(self, request, format=None):
        print(request.user)
        user = User.objects.get(username=str(request.user))
        deployments = DeployRequest.objects.filter(owner=user.id)
        if len(deployments) == 0:
            return Response(status=status.HTTP_204_NO_CONTENT)
        else:
            InfoList = []
            for deployment in deployments:
                InfoList.append(deployment.id)
            return Response(InfoList, status=status.HTTP_200_OK)

class DeployRequestDetailView(APIView):
    '''
        用户查询单个代部署申请详情
        /api/fundraises/{id}
    '''
    def get(self,request,pk,format=None):
        deployments = DeployRequest.objects.filter(id=pk,owner=request.user.id)
        if len(deployments)==0:
            return Response(status=status.HTTP_404_NOT_FOUND)
        else:
            deployment=deployments[0]
            deploymentInfo={}
            deploymentInfo['id'] = deployment.id
            deploymentInfo['name'] = deployment.name
            deploymentInfo['owner_address'] = deployment.owner_address
            deploymentInfo['target_money'] = str(deployment.target_money)
            deploymentInfo['ipfs_hashes'] = deployment.ipfs_hashes.split(',')
            deploymentInfo['state'] = deployment.state

            return Response(data=deploymentInfo,status=status.HTTP_200_OK)

class DeploymentRejectView(APIView):
    '''
    管理员拒绝代部署合约
    POST /api/fundraises/request/{id}/reject
    '''
    permission_classes = (IsAdminUser,)

    def post(self, request, pk, format=None):
        deployments = DeployRequest.objects.filter(id=pk)
        if len(deployments) == 0:
            return Response(status=status.HTTP_404_NOT_FOUND)
        deployment = deployments.get(id=pk)
        deployment.state = 2
        deployment.save()
        return Response(status=status.HTTP_200_OK)

class AllDeployRequestView(APIView):
    '''
    管理员查询所有代部署申请
    GET /api/fundraises/request
    '''
    permission_classes = (IsAdminUser,)

    def get(self,request,format=None):
        # 查询所有代部署的申请
        deployments = DeployRequest.objects.all()
        if len(deployments) == 0:
            return Response(status=status.HTTP_204_NO_CONTENT)
        else:
            InfoList = []
            for deployment in deployments:
                InfoList.append(deployment.id)
            return Response(InfoList, status=status.HTTP_200_OK)

'''
 管理员查询单个代部署申请的信息
 GET api/fundraises/request/<int:pk>

'''
class OneDeployRequestView(APIView):
    permission_classes = (IsAdminUser,)
    def get(self,request,pk,format=None):
        deployment = DeployRequest.objects.get(id=pk)
        deploymentInfo={}
        deploymentInfo['id'] = deployment.id
        deploymentInfo['name'] = deployment.name
        deploymentInfo['owner_address'] = deployment.owner_address
        deploymentInfo['target_money'] = str(deployment.target_money)
        deploymentInfo['ipfs_hashes'] = deployment.ipfs_hashes.split(',')
        deploymentInfo['state'] = deployment.state

        return Response(data=deploymentInfo,status=status.HTTP_200_OK)

class AgreeDeployView(APIView):
    '''
    管理员同意代部署合约***：
    POST /api/fundraises/request/agreement/{id}
    (部署deploy一份新的fundraise合约）
    '''
    def post(self,request,pk,format=None):
        # 获取代部署申请的对象和信息
        try:
            deployment = DeployRequest.objects.get(id=pk)
        except:
            print("找不到对应的代部署申请!!!")
            return Response(status=status.HTTP_404_NOT_FOUND)
        web3=Web3(HTTPProvider('http://localhost:7545'))
        # 读取机构钱包私钥
        with open('BlockchainManagement/config/keyfile','r') as f:
            encrypted_key=f.read()
            private_key=web3.eth.account.decrypt(encrypted_key,'blockchain')

        name=deployment.name
        try:
            # 获取管理员对应的机构地址
            charityAddr = DeployedCharityContract.objects.all()[0].contractAddr
        except:
            return Response(data={"找不到机构合约！！"},status=status.HTTP_404_NOT_FOUND)
        # 获取对应的机构合约
        charity_contract = web3.eth.contract(address=charityAddr, abi=CharityConfig['abi'],
                                             ContractFactoryClass=ConciseContract)
        targetMoney=deployment.target_money
        # 部署合约花费的gas由机构钱包出
        result=deployOneFundraiseContract(web3,web3.eth.accounts[0],private_key, name, charityAddr,deployment.owner_address , targetMoney)

        if True in result:
            # 机构合约添加fundraise合约地址
            res = charity_contract.addFundraise(result[1], transact={'from': web3.eth.accounts[0]})
            deployment.state=1
            deployment.save()
            return Response(status=status.HTTP_200_OK)
        else:
            return Response({'detail': '部署募捐项目出错'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)


from sklearn.datasets import load_svmlight_file, dump_svmlight_file
import argparse
import numpy as np

parser = argparse.ArgumentParser()
parser.add_argument("--file", help="path to file",
    type = str,required =True)

args = parser.parse_args()
f = args.file

data = load_svmlight_file(f)
dump_svmlight_file(data[0],data[1], f, zero_based=False)


sandwich_orders = ['tuna', 'chicken', 'beef', 'vegan']
finished_sandwiches = []

while sandwich_orders:
    current_sandwich = sandwich_orders.pop()

    print(f"I made the {current_sandwich} sandwich !")
    finished_sandwiches.append(current_sandwich)

print(f"\nThese are the sandwiches that has been made:")
for sandwich in finished_sandwiches:
    print(f"\t {sandwich} sandwich")


from flask import Flask, render_template, request
from joblib import load
from tensorflow.keras.models import load_model
from sklearn.preprocessing import StandardScaler
import pickle
import pandas as pd

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

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

    def represents_int(s):
        try:
            int(s)
            return True
        except ValueError:
            return False

    if request.method == 'POST':
        postcode = request.form['postcode']
        state = request.form['state']
        policies = request.form['policies']
        age = request.form['age']
        coast = request.form['coast']
        form = request.form['form']
        fire = request.form['fire']
        fire_prox = request.form['fire_prox']
        payment = request.form['payment']
        burglar = request.form['burglar']
        portable = request.form['portable']

        with open('structure.pkl', 'rb') as f:
            structure = pickle.load(f)
        sc = load('scaler.joblib')
        model = load_model('oversample_model.h5')
        df_ = pd.DataFrame(columns=structure)
        final = []

        if state == '' or postcode == '' or policies == '' or age == '' or coast == '' or form == '' or fire == ''\
                or fire_prox == '' or payment == '' or burglar == '' or portable == '' or \
                represents_int(postcode) is False or represents_int(age) is False:
            return render_template('index.html', message='Please enter required fields')
        else:
            final.append(int(postcode))
            if fire == 'True':
                final.append(1)
            else:
                final.append(0)
            if burglar == 'True':
                final.append(1)
            else:
                final.append(0)
            if portable == 'True':
                final.append(1)
            else:
                final.append(0)
            final.append(int(age))
            final.append(int(policies))

            states = [i[i.index("_") + len("_"):] for i in structure if i.startswith('state_')]

            for j, item in enumerate(states):
                if item == state or state not in states and j == len(states) - 1:
                    final.append(1)
                elif j < len(states):
                    final.append(0)

            forms = [i[i.index("_") + len("_"):] for i in structure if i.startswith('form_')]

            for j, item in enumerate(forms):
                if item == form:
                    final.append(1)
                else:
                    final.append(0)

            coasts = [i[i.index("_") + len("_"):] for i in structure if i.startswith('coast_')]

            for j, item in enumerate(coasts):
                if item == coast or coast not in coasts and j == len(coasts) - 1:
                    final.append(1)
                elif j < len(coasts):
                    final.append(0)

            fires = [i[i.rfind("_") + len("_"):] for i in structure if i.startswith('fire_housing_proximity_')]

            for j, item in enumerate(fires):
                if item == fire_prox or fire_prox not in fires and j == len(fires) - 1:
                    final.append(1)
                elif j < len(fires):
                    final.append(0)

            policies_count = [i[i.rfind("_") + len("_"):] for i in structure if i.startswith('previous_policies_')]

            for j, item in enumerate(policies_count):
                if item == policies or policies not in policies_count and j == len(policies_count) - 1:
                    final.append(1)
                elif j < len(policies_count):
                    final.append(0)

            payments = [i[i.rfind("_") + len("_"):] for i in structure if i.startswith('card_type_')]

            for j, item in enumerate(payments):
                if item == payment:
                    final.append(1)
                else:
                    final.append(0)

            a_series = pd.Series(final, index=structure)
            df_ = df_.append(a_series, ignore_index=True)

            x_target = sc.transform(df_)

            prediction = model.predict_classes(x_target)[0]

            if prediction == 0:
                return render_template('index.html', message='Your submission has been processed!')
            else:
                return render_template('index.html', message='Oh! :( There has been an error with your submission. '
                                                             'Please, contact a Lemonade assistant')

if __name__ == '__main__':
    app.run()


def make_dropdown(init_value, choices, callback):
	a = html.A(init_value, **{'class':'dropdown-toggle', 'data-toggle':'dropdown',
								'aria-haspopup':'true', 'aria-expanded':'false',
								'id':'page_config_editor_select_id_1'})
	ll = html.DIV(**{'class':'dropdown-menu', 'aria-labelledby':'page_config_editor_select_id_1'})
	dropdown = html.TD(a + ll, **{'class':'dropdown'})
	for c in choices:
		li = html.A(c, **{'class':'dropdown-item small'})
		ll <= li
		def on_click(ev):
			a.text = ev.target.text
			callback(ev.target.text)
		li.bind('click', on_click)
	return dropdown

class page_config_editor_obj_py:
	def __init__(self):
		self.type_name = 'page_config_editor'
		self.elt = html.DIV()
		self.event_listener = None
		self.component = None
		self.del_btn = None
	def use_example_data(self):
		self.elt <= html.DIV(self.type_name, **{'class':'alert alert-info', 'role':'alert'})
	def get_data(self, data_name):
		if self.component is None: return None
		return self.component
	def set_data(self, data_name, data):
		self.component = None
		self.elt.clear()
		if data is None: return
		self.component = data
		component = self.component['rows'][0]
		tb = html.TABLE(**{'class':'small table table-sm table-hover'})
		self.elt <= tb
		thead = html.THEAD(**{'class':'thead-dark'})
		tb <= thead
		tbody = html.TBODY()
		tb <= tbody
		tr = html.TR()
		thead <= tr
		tr <= html.TH()
		tr <= html.TH('页面参数')
		tr <= html.TH('设置值')
		# page name
		a1 = html.A(component[0], **{'contenteditable':'true'})
		tr = html.TR(**{'class':'table-info'})
		tbody <= tr
		tr <= html.TD(html.SPAN('page', **{'class':'badge badge-info'}))
		tr <= html.TD('页面名称')
		tr <= html.TD(a1)
		def onblur1(ev):
			nonlocal a1
			text = a1.text.strip()
			if text != component[0]:
				component[0] = text
				self.del_btn.text = '删除页面 "'+text+'"'
				if self.event_listener is not None:
					self.event_listener('change', 'component')
		a1.bind('blur', onblur1)
		# login option
		tr = html.TR(**{'class':'table-info'})
		tbody <= tr
		tr <= html.TD(html.SPAN('page', **{'class':'badge badge-info'}))
		tr <= html.TD('何时显示')
		text2 = component[1].split(',')
		a2 = html.A(text2[1] if len(text2) > 1 else '', **{'contenteditable':'true'})
		def on_login_option():
			nonlocal text2, a2
			text2 = ','.join(text2)
			if component[1] != text2:
				component[1] = text2
				if self.event_listener is not None:
					self.event_listener('change', 'component')
		def callback(option):
			nonlocal text2
			if option == '用户组':
				text2 = [option,a2.text.strip()]
			else:
				text2 = [option]
			on_login_option()
		tr <= make_dropdown(text2[0], ['总是', '登录后', '登录前', '网站作者', '用户组'], callback)
		# user group
		tr = html.TR(**{'class':'table-info'})
		tbody <= tr
		tr <= html.TD(html.SPAN('page', **{'class':'badge badge-info'}))
		tr <= html.TD('用户组名')
		tr <= html.TD(a2)
		def onblur2(ev):
			nonlocal text2
			if len(text2) > 1:
				text2[1] = a2.text.strip()
				on_login_option()
		a2.bind('blur', onblur2)
		# delete page
		tr = html.TR(**{'class':'table-info'})
		tbody <= tr
		tr <= html.TD()
		tr <= html.TD()
		td = html.TD()
		tr <= td
		self.del_btn = html.BUTTON('删除页面 "'+component[0]+'"', style={'font-size':'smaller'}, **{'type':'button', 'class':'btn-sm btn-dark'})
		td <= self.del_btn
		def delete_page(ev):
			self.set_data(None, None)
			self.event_listener('change', 'component')
		def confirm_delete_page(ev):
			from browser import self as window
			window.confirm_modal('请确认删除页面: ' + component[0], delete_page)
		self.del_btn.bind('click', confirm_delete_page)


from ..cw_model import CWModel

class PurchaseOrderStatus(CWModel):

    def __init__(self, json_dict=None):
        # With: id(Integer)
        # Search: ^(\s*)(\*?)([_a-z0-9]*)\s?(\(.*$)
        # Replace: $1self.$3 = None  # $2$4
        # To get: self.id = None  # (Integer)
        self.id = None  # (Integer)
        self._info = None  # (Metadata)
        self.defaultFlag = None  # (Boolean)
        self.closedFlag = None  # (Boolean)
        self.inactiveFlag = None  # (Boolean)
        self.defaultClosedFlag = None  # (Boolean)
        self.sortOrder = None  # (Integer)
        self.emailTemplate = None  # (PurchaseOrderStatusEmailTemplateReference)

        # initialize object with json dict
        super().__init__(json_dict)


from django.forms import ModelForm
from django_summernote.widgets import SummernoteWidget, SummernoteInplaceWidget
from django import forms
from django.conf import settings
from clientes.models import Empresa
from .models import Arquivo, Telefone

class ArquivoModelForm(ModelForm):
    class Meta:
        model = Arquivo
        fields = ['nome','file','nunca_expira','data_expire']
        widgets = {
        'nome':forms.TextInput(attrs={'class':'form-control','required':True,'arial-describedby':'nome_arquivo','placeholder':'Ex: Darfs'}),
        'file':forms.ClearableFileInput(attrs={'class':'form-control-file','required':True,'arial-describedby':'arquivo','multiple':True}),
        'data_expire':forms.DateField(input_formats=settings.DATE_INPUT_FORMATS),
        'data_expire':forms.DateInput(attrs={'class':'form-control readonly datepicker','readonly':True,'arial-describedby':'data_que_expira'})
        }

status_list = (
    ('aberto','aberto'),
    ('reaberto','reaberto'),
    ('resolvido','resolvido'),
    ('todos','todos')
)

class FiltroForm(forms.Form):

    def __init__(self,*args,**kwargs):
        super(FiltroForm,self).__init__(*args,**kwargs)
        self.fields['status'] = forms.ChoiceField(choices=status_list,widget=forms.Select(attrs={'class':'custom-select'}))
        self.initial['status'] = 'aberto'

class SummerNoteForm(forms.Form):
    editor = forms.CharField(widget=SummernoteInplaceWidget())

class TelefoneModelForm(ModelForm):
    class Meta:
        operadoras = (('FIXO','Fixo'),('CLARO','Claro'),('OI','OI'),('TIM','Tim'),('VIVO','Vivo'))

        model = Telefone
        exclude = ['empresa']
        widgets = {
            'ddd':forms.TextInput(attrs={'class':'form-control ddd-mask','required':True,'placeholder':'DDD'}),
            'numero':forms.TextInput(attrs={'class':'form-control numero-mask','minlength':9,'required':True,'placeholder':'Numero'}),
            'operadora':forms.Select(attrs={'class':'form-control'},choices=operadoras),
        }


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

# Coded for Python 3 (tested under Python 3.5 for Windows 64-bit)
#
# Code by genBTC. Created from scratch 10/6/2015.  Relies on other pre-requisite steps 1,2 that I scripted also.
#
# Version 0.1 - functional since 10/9/2015.
# Version 0.1a - now uses directory paths obtained from settings, ss.Preferences() 10/14/2015
# Version 0.1b - Changed code to target Python 3 @ 10/16/2015
#
# Script #3: Renames all your what.cd downloads to have a proper and uniform name.
#       All naming decisions happen here. You should/must edit this file to suit yourself, especially "Sorted_Record_Labels_List"
#       This program takes the results of Script #1,#2  and re-builds from scratch the desired filename.
#       This was built specific to my needs, the what and why of which needs explaining:
#
# FUNCTIONALITY notes:
#
#   1. The Sorted_Record_Labels_List is a list of record labels that I wish to store SORTED by the catalog number such as [DMZ001] (at the start of the filename)
#       this causes the program to omit output of a {Digital Mystikz} tag near the end, as its obvious to me because I have these stored in named folders.
#   1b. Any Record labels not part of this list will have the {Record Label} tag at the end BUT the catalog number omitted entirely.
#   1c. In the catalog number I removed all spaces, and converted it all to uppercase.
#   2. Much confusion arose from "Torrentobj.torrent.remastered" Edition which is a flag where different editions can have 4 modified fields: (year,name) and 2 of which (catalog,tor.group.recordLabel)
#       needed special handling.  Upon careful inspection, about 1% of these had flawed data in some way, and the code is very branched and unclear, although i did my best.
#       I handled as many edge cases as possible while trying to ascertain whether to use the original fields or the tor.torrent.remastered edition fields.
#       You would want to debug how these "Guesses" apply to your library and I am working on a way to delay changes until the end for confirmation or something.
#   3. The ReleaseType field (EP, Album) - fully listed below - I wanted to be handled so it would be displayed in [] on all files, but [Album] seemed redundant to me.
#       EP also got redundant as some albums are named as "The Something EP" [EP] so if the name ended in EP, i just omitted the bracketed [EP] as well.
#   3b. The (Blu-ray - FLAC - 24bit Lossless) seemed too long so i settled on just [FLAC 100%] for log FLACs or just [FLAC] and I dont have many 24-bits, so I skipped it.
#   3c. For MP3, I omitted the word MP3, and for VBR i omitted the words VBR. example: [WEB 320] [CD V0]
#   3d. For Torrentobj.torrent.scene releases, i just replaced the word [WEB] with [Torrentobj.torrent.scene] since its redundant to me, and there was no easy way to grab the Torrentobj.torrent.scene Group's name.
#   3e. For AAC, i searched the tor.torrent.description and tor.torrent.filepath (what the original torrent owner names it) for 'itunes' and if found, tagged it as [iTunes AAC].
#       tor.torrent.filePath is the field that goes into uTorrent, but is too unpredictable overall - even though it can contain valid info found nowhere else sometimes.
#       (actually, that field is what this program is attempting to correct, and there will be a followup program to re-import the new name back into uTorrent to seed)
#   4. The last thing the program does is re-copy all the tor.torrent.infoHash JSON files into the new-proper-name filenames, JUST FOR REFERENCE to have later. (easily deletable)
#       My advice to read these humanly, is to use Notepad++ with the plugin called JSON Viewer (http://nppjsonviewer.sourceforge.net/) or some other JSON viewer.
#       To use: CTRL+A (Select All) and CTRL+ALT+SHIFT+M (Torrentobj.torrent.format as JSON). Don't resave it, or there will be a ton of \t tabs and \n newlines in the saved file and will screw it up.
#   4b. Please note that simply renaming a .torrent file to the new-names will not affect the Bencoded tor.torrent.filePath inside and thus will not seed to that location, and there is no way
#       to modify the tor.torrent.filePath inside the torrent even with a BEncode Editor, because this will change the value of the tor.torrent.infoHash and will come up as "Torrent not recognized"
#
# PROGRAMMATICALLY, the following is explained:
#
#   1. Useful comments were left along the way as i went
#   2. My python is mediocre at best, so if you're wondering why I coded it Z way instead of X or Y its because I however it came to mind is how it got coded.
#       And step-by-step readability was preferred over abstraction.
#   3. I went through hell with the ascii vs unicode vs mbcs filesystem encodings and HTML-unescaped JSON, and this is just how I cobbled it all together to work.
#   4. While I NEEDED to grab the artist from the torrent filename, for some reason I got the album, year from it. This is for no reason.
#   5. The filenames needed to be sanitized from Windows's banned character list of \ / : * ? " < > | and have been replaced by Unicode looking clones
#       except the ? which I could only find a backwards looking replacement for.

# CODING TODO LIST:
# instead of  grabbing album,year from filename, grab from named hash JSON-dump file
# think of better naming concepts
# an idea: $1 $2 $3 beginning middle end, select which pieces of the program to write as the new name
# might be a good idea to create an undo.dat file
# *DONE*maybe sort the master list output file alphabetically
# save list of  files the program modified unusually
# wait till the end to display list of errors
# *DONE*refactor->create a class or wrapper function to hold the response[] (the shell of which can be borrowed from torrent-torrent.py,torrent-group.py)
# *DONE*(see bottom)profile performance and figure out what is the most cpu intensive part, and improve performance
# use multiple threads to process many things at once. (Caps out at 12% per Core) so 8 threads would be way faster, and SSD usage is at a minimum (1-5%)
# Figure out a better way to reduce the exponential nature of this file (first loop has X entries, nested loop has X entries, so thats X^2).
# Unify the encoding schemes and remove as many conversions as possible and just stick to unicode for everything.
# *DONE*create better way to interface with people other than myself / editing source code magic strings (added settings.ini Config file)

import bencode
import base64
import sys
import os
import hashlib
try:
    from urlparse import urlparse
except:
    import urllib.parse as urlparse
import time
import json
from enum import Enum
import ntpath
import re
import codecs
try:
    import HTMLParser
except:
    import html.parser as HTMLParser
import shutil
import difflib
from torrentclass import Torrent
from settings import Preferences

class ReleaseType(Enum):
    Unspecified = 0
    Album = 1
    Soundtrack = 3
    EP = 5
    Anthology = 6
    Compilation = 7
    DJ_Mix = 8
    Single = 9
    Live = 11
    Remix = 13
    Bootleg = 14
    Interview = 15
    Mixtape = 16
    Unknown = 21
    Concert = 22
    Demo = 23

Sorted_Record_Labels_List = ["abducted",
"abducted dub",
"abducted limited",
"abducted records",
"armada captivating",
"armada deep",
"big apple",
"butterz",
"buygore",
"chestplate",
"circus",
"deep medi",
"disciple",
"dmz",
"digital mystikz",
"dub police",
"eatbrain",
"firepower",
"hyperdub",
"kannibalen",
"kapsize",
"luckyme",
"mainstage music",
"mau5trap",
"monstercat",
"musical freedom",
"nest",
"never say die",
"nsd:",
"osiris music",
"owsla",
"play me",
"prime audio",
"revealed",
"rottun",
"smog",
"storming",
"sub soldiers",
"subhuman",
"swamp81",
"swamp 81",
"symbols",
"tempa",
"ultragore",
"zouk" ]

class TorrentEntry:
    def __init__(self):
        self.hash = None
        self.pathname = None
        self.filename = None
        self.artistalbum = None
        self.torrentid = None
        self.createdpropername = None
        self.fmttdMediaEncodeFormat = ""

def getUniqueWords(iterable):
    seen = set()
    result = []
    for item in iterable:
        if item.lower() not in seen:  # compare lowercases
            seen.add(item.lower())
            result.append(item)  # but preserve actual case
    return result  # return a list (in order)

def main():

    global fmttdcatalogueNumber     #to fix an issue with scope (line 330,333).

    ss = Preferences()
    hashtorrlistfile = ss.getwpath("outpath1")
    directory_path = ss.getwpath("script2destdir")   #as source dir (hash-grabs)
    allfiles = [os.path.join(directory_path, filename) for filename in next(os.walk(directory_path))[2]]  # gives absolute paths + names

    hashtofilenamefolder = ss.getwpath("script3destdir") #as dest dir (hash-grabs-as-filenames)
    writelistfile = codecs.open(ss.getwpath("outpath3"), 'wb', "utf-8")  # write-out a text file with one entry per line. main output file (3propernames.txt)

    writelistcontainer = []
    currentfilenumber = 1

    for hashidfilename in allfiles:  # iterate through filenames of what.cd JSON data

        with open(hashidfilename, 'r') as stringfile:  # open them
            needFixLabeltoNewEdition = False

            jsonresponse = json.load(stringfile)
            tor = Torrent(jsonresponse)

            if tor.group.categoryName != "Music":
                continue  # do not continue altering any non-music torrents.

            releaseTypeName = ReleaseType(tor.group.releaseType).name  # turn int. value into a string using the enum class above
            fmttdreleaseTypeName = "[" + releaseTypeName + "]"

            if tor.torrent.remastered:
                if tor.torrent.remasterTitle:
                    tor.group.name += " (" + tor.torrent.remasterTitle + ")"
                if tor.torrent.remasterYear > tor.group.year:
                    tor.group.year = tor.torrent.remasterYear
                if tor.torrent.remasterRecordLabel:
                    if tor.group.recordLabel.lower() != tor.torrent.remasterRecordLabel.lower():  # so not case sensitive
                        if not tor.group.recordLabel:
                            tor.group.recordLabel = tor.torrent.remasterRecordLabel
                        else:
                            # then things get complicated and we need to figure out which Label/catalog field is the best one to use, or combine them or both
                            needFixLabeltoNewEdition = True

                # if its been determined that its a remaster (new edition), process new label and catalog
                #  checking whether to combine with old, or which one to use, etc, etc.....
                if needFixLabeltoNewEdition == True:

                    score = difflib.SequenceMatcher(None, tor.group.recordLabel.lower(), tor.torrent.remasterRecordLabel.lower()).ratio()
                    if (score < 0.5):
                        # considered similar at 0.6 but this way is not that accurate. if they are lower than 0.5 similar, just use the new one
                        tor.group.recordLabel = tor.torrent.remasterRecordLabel
                        if tor.torrent.remasterCatalogueNumber:
                            tor.group.catalogueNumber = tor.torrent.remasterCatalogueNumber  # if tor.torrent.remasterCatalogueNumber is not blank, use it as the new tor.group.catalogueNumber

                    elif all(word in tor.torrent.remasterRecordLabel.lower() for word in tor.group.recordLabel.lower()) \
                            or \
                            all(word in tor.group.recordLabel.lower() for word in tor.torrent.remasterRecordLabel.lower()):
                        # If all the words in the old label is encompassed in the new one, use the new one.
                        # This would mean the new edition record label is most likely longer and is similar enough to use that,
                        # and preferred, since its more applicable to this specific release anyway.
                        # Even if the reverse is true, this code-block should only catch labels that differ in slight ways.(?)

                        # example 1: originallabel={Big Beat Records}  remasterlabel={Big Beat}          #elif new label in old label
                        #   result:     {Big Beat}                                                      #or
                        # example 2: originallabel={Big Beat}  remasterlabel={Big Beat Records}          #if old label in new label
                        #   result:     {Big Beat Records}                                              #then
                        tor.group.recordLabel = tor.torrent.remasterRecordLabel  # always choose new label.
                        if tor.torrent.remasterCatalogueNumber:
                            tor.group.catalogueNumber = tor.torrent.remasterCatalogueNumber  # if tor.torrent.remasterCatalogueNumber is not blank, use it as the new tor.group.catalogueNumber
                    else:
                        # This else-block is used when the above are not true, and we can't decide on which one to use, so we use both. Combined.

                        # example 1: originallabel={Island Records}  remasterlabel={Island Records / Lokal Legend}
                        #   result:     {Island Records / Lokal Legend}
                        # example 2: originallabel {Wall Recordings}  remasterlabel={Tiger Records}
                        #   result:     {Wall Recordings / Tiger Records}
                        new = ""
                        splitorig = re.sub("[(,),-]", " ",
                                           tor.group.recordLabel).split()  # remove delimiter chars that mess up stuff
                        sepchar = ["/"]
                        splitnew = re.sub("[(,),-]", " ", tor.torrent.remasterRecordLabel).split()  # turn everything into a list

                        new = " ".join(["%s" % (v) for v in getUniqueWords(
                            splitorig + sepchar + splitnew)])  # append unique words to the orig.

                        if tor.torrent.remasterCatalogueNumber:
                            if tor.group.catalogueNumber != tor.torrent.remasterCatalogueNumber:
                                if tor.group.catalogueNumber:
                                    tor.group.catalogueNumber += " / " + tor.torrent.remasterCatalogueNumber
                                else:
                                    tor.group.catalogueNumber = tor.torrent.remasterCatalogueNumber
                        tor.group.recordLabel = new

            # ntpath.basename was really slow so doing it manually.... (32 times faster)= 0.128 seconds vs 0.004 seconds
            # whats happening here is due to an exponential nested for loop, ie: 4129 results^2 = 17 million function calls of either basename or .rfind('\\')
            # there should be a better way to do this.
            hashfilesepidloc = hashidfilename.rfind("\\") + 1
            cmphashfn = hashidfilename[hashfilesepidloc:]
            iterhashfile = open(hashtorrlistfile, 'r',encoding='utf-8').readlines()  # read everything into memory
            for i in iterhashfile:  # read line
                splitline = i.strip().split(' / ')      # 0 torrentID / 1 Hash / 2 torrentfilename
                if splitline[1] == cmphashfn:  # if it matches, start processing
                    newEntry = TorrentEntry()  # instanciate class
                    newEntry.hash = splitline[1]  # store Hash for reference
                    newEntry.pathname = splitline[2]  # filename + extension
                    locextension = newEntry.pathname.find(".torrent")  # location of extension
                    locid = newEntry.pathname.rfind("-") + 1  # location of tor.torrent.id
                    newEntry.filename = newEntry.pathname[:locextension]  # chop the extension off (manually)
                    newEntry.artistalbum = newEntry.filename[:locid - 1]  # JUST the name (no ID#)
                    newEntry.torrentid = newEntry.filename[locid:locextension]  # grab ID for future reference (tor.torrent.id on what.cd)
                    # example : S-Type - Billboard (Lido Remix) - 2014 (WEB - MP3 - 320)
                    newEntry.artist = newEntry.artistalbum[:newEntry.artistalbum.find(" - ")]  # grab artist
                    tempalbum = newEntry.artistalbum[newEntry.artistalbum.find(" - ") + 3:]  # temp value helps with string processing
                    newEntry.album = tempalbum[:tempalbum.find(" - ")]  # not needed since it can be pulled from [group]
                    newEntry.year = tempalbum[tempalbum.find(" - ") + 3:tempalbum.find(" - ") + 7]  # not needed since it can be pulled from [group]

                    # ------------Recreate name------------#
                    # -------Special RULES SECTION---------#
                    newEntry.createdpropername = newEntry.artist + " - " + tor.group.name + " "
                    if tor.group.releaseType > 1:  # dont put it for Album or Unspecified
                        if tor.group.releaseType != 5:  # do something different for EP
                            newEntry.createdpropername += fmttdreleaseTypeName + " "
                        else:  # make a rule so [EP] doesnt come up if there is " EP " already
                            if tor.group.name[-2:] != "EP":
                                newEntry.createdpropername += fmttdreleaseTypeName + " "

                    newEntry.createdpropername += "(" + str(tor.group.year) + ")"

                    # written like this for easy humanreading
                    #           format = MP3, FLAC, AAC,
                    #          media = cd, web, vinyl, soundboard, dat
                    #        encoding = lossless,320,v0,256,v2,192
                    if tor.torrent.format == "FLAC":
                        newEntry.fmttdMediaEncodeFormat = "FLAC"
                        # log and logscore only applicable to flac.
                        if tor.torrent.hasLog:
                            newEntry.fmttdMediaEncodeFormat += " " + str(
                                tor.torrent.logScore) + "%"  # the % implies "log" so leave out the word log
                    if tor.torrent.format == "AAC":
                        if (any("itunes" in word.lower() for word in tor.torrent.description.split())) or (
                        any("itunes" in word.lower() for word in tor.torrent.filePath.split())):
                            newEntry.fmttdMediaEncodeFormat = "iTunes "
                        newEntry.fmttdMediaEncodeFormat += "AAC"
                    if tor.torrent.format == "MP3":
                        # dont actually write mp3
                        # only write Scene or WEB if it is an mp3
                        if tor.torrent.scene:
                            newEntry.fmttdMediaEncodeFormat += "Scene"
                        elif tor.torrent.media == "WEB":
                            newEntry.fmttdMediaEncodeFormat += "WEB"
                        else:
                            newEntry.fmttdMediaEncodeFormat += tor.torrent.media
                        if "VBR" in tor.torrent.encoding:
                            newEntry.fmttdMediaEncodeFormat += " " + tor.torrent.encoding[:2]
                        else:
                            newEntry.fmttdMediaEncodeFormat += " " + tor.torrent.encoding

                    newEntry.fmttdMediaEncodeFormat = "[" + newEntry.fmttdMediaEncodeFormat + "]"

                    newEntry.createdpropername += " " + newEntry.fmttdMediaEncodeFormat

                    # put catalog number in brackets
                    if tor.group.catalogueNumber:
                        fmttdcatalogueNumber = ("[" + tor.group.catalogueNumber + "]").replace(" ", "").upper()
                    elif tor.group.recordLabel:
                        fmttdcatalogueNumber = "[" + tor.group.recordLabel + "]"  # combines with next part to put recordLabel in the front if Cat# missing

                    if any(word in tor.group.recordLabel.lower() for word in Sorted_Record_Labels_List):  # so not case sensitive
                        newEntry.createdpropername = fmttdcatalogueNumber + " " + newEntry.createdpropername
                    elif tor.group.recordLabel:
                        newEntry.createdpropername += " " + "{" + tor.group.recordLabel + "}"
                        # if tor.group.catalogueNumber:                                                       #This will put [CATA###] after all releases, even labels not in your list
                        #     newEntry.createdpropername += " " + fmttdcatalogueNumber              #Gets kind of cumbersome for me. (uncomment to use it anyway)

                    # these 2 lines are a quick fix, for an oversight in my naming process
                    # if these are single file .mp3's (or a single .flac) they will need a .mp3 at the end of the filename
                    if not tor.torrent.filePath:
                        newEntry.createdpropername += "." + tor.torrent.format.lower()

                    try:
                        print(currentfilenumber, newEntry.createdpropername.encode('ascii', errors='ignore').decode())
                    except:
                        print("COULD NOT PRINT UNICODE FILENAME TO CONSOLE. HASH=", tor.torrent.infoHash)

                    ########-------------replace characters section----------------#########
                    newEntry.createdpropername = newEntry.createdpropername.replace("\\","＼")  # U+FF3C               FULLWIDTH REVERSE SOLIDUS
                    # these forward slashes are strange. "FullWidth" is very wide and would be too wide if theres already spaces around it.
                    newEntry.createdpropername = newEntry.createdpropername.replace(" / ","／")  # U+FFOF  (wide)       FULLWIDTH SOLIDUS
                    # "Division" slash is too narrow and needs spaces inserted surrounding it (and is still less width than the fullwidth)
                    newEntry.createdpropername = newEntry.createdpropername.replace("/"," ∕ ")  # U+2215  (narrow)     DIVISION SLASH
                    newEntry.createdpropername = newEntry.createdpropername.replace(":","꞉")  # U+A789               MODIFIER LETTER COLON
                    newEntry.createdpropername = newEntry.createdpropername.replace("*","※")  # U+203B               REFERENCE MARK
                    newEntry.createdpropername = newEntry.createdpropername.replace("?","؟")  # U+061F               ARABIC QUESTION MARK
                    newEntry.createdpropername = newEntry.createdpropername.replace('"',"ʺ")  # U+02BA               MODIFIER LETTER DOUBLE PRIME
                    newEntry.createdpropername = newEntry.createdpropername.replace("<","˂")  # U+02C2               MODIFIER LETTER LEFT ARROWHEAD
                    newEntry.createdpropername = newEntry.createdpropername.replace(">","˃")  # U+02C3               MODIFIER LETTER RIGHT ARROWHEAD
                    newEntry.createdpropername = newEntry.createdpropername.replace("|","ǀ")  # U+01C0               LATIN LETTER DENTAL CLICK
                    #####--windows filename banned chars replacement with unicode--#########

                    ######----------HashGrabs-as-Filenames--------########
                    # File output. Move all files named as hashes to a new dir as the proper name
                    if not os.path.exists(hashtofilenamefolder + newEntry.createdpropername):
                        shutil.copy(hashidfilename, hashtofilenamefolder + newEntry.createdpropername)

                    currentfilenumber += 1
                    #####------------make propernames.txt (has the hash in it also) ---------########
                    # Add it to the container (since this is in a loop)
                    writelistcontainer.append(newEntry.createdpropername + " / " + tor.torrent.infoHash + "\n")
    ##File Output. The Master List file of everything.##
    # when the loop exits, Sort it, and write it to the file.
    writelistcontainer.sort()
    for eachline in writelistcontainer:
        writelistfile.write(eachline)
    writelistfile.close()

if __name__ == "__main__":
    main()

    # PROFILING:
    # if __name__ == "__main__":
    #     import cProfile, pstats
    #     cProfile.run("main()", "{}.profile".format(__file__))
    #     s = pstats.Stats("{}.profile".format(__file__))
    #     s.strip_dirs()
    #     s.sort_stats("time").print_stats(25)

    # Sun Oct 11 00:19:57 2015    create-proper-names.py.profile
    #          35543736 function calls (35543709 primitive calls) in 28.724 seconds
    #    Ordered by: internal time
    #    List reduced from 171 to 10 due to restriction <10>
    #
    #    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    #         1   14.085   14.085   28.723   28.723 create-proper-names.py:175(main)
    #  17309720    4.716    0.000    4.716    0.000 {method 'rfind' of 'unicode' objects}
    #      4161    4.478    0.001    4.478    0.001 {method 'readlines' of 'file' objects}
    #  17313841    1.725    0.000    1.725    0.000 {method 'strip' of 'str' objects}
    #     16565    1.007    0.000    1.007    0.000 {open}
    #      4120    0.501    0.000    1.700    0.000 shutil.py:66(copyfile)
    #     16481    0.492    0.000    0.492    0.000 {nt.stat}
    #      4163    0.349    0.000    0.349    0.000 decoder.py:372(raw_decode)
    #     12418    0.251    0.000    0.251    0.000 {method 'read' of 'file' objects}
    #      4120    0.152    0.000    0.152    0.000 {nt.chmod}


#!/usr/bin/env python-2.4.3
#
# January 18 2010, Christian E. Hopps
#
# Copyright (c) 2009-2010 by cisco Systems, Inc.
# All rights reserved.
#

# 1. Standard preamble
import pdb, sys, os, re
os.environ['PY_XRUT_ROOT'] = os.path.dirname(os.path.dirname(os.path.abspath(sys.path[0])))
sys.path[1:1] = [os.environ.get('PY_XRUT_ROOT') + '/modules']

# 2. Import the modules we use.
import xrut, pre, results, pprint, utng
from xrut import routers
from utng import exec_logger

#
# Create a common topology for all the demo scripts.
#
topology = xrut.ng_topology_t( {'net1': ( 'one', 'two' ) },
                               toponame="demotopo" )
one = routers['one']
two = routers['two']

#
# 4. Define a simple test suite
#
class example_suite_t (utng.test_suite_t):

    # Wait up to 30 seconds for routers interfaces up
    required_prerequisites = pre.prerequisite_t([
        pre.interface_up_pred_t(routers['one'].netifs['net1']),
        pre.interface_up_pred_t(routers['two'].netifs['net1']), ],
                                                30)

    # verify arp table has correct entry
    def test_log_arp_results (self):
        # useful locals variable references
        oneintf = one.netifs['net1'].name
        twointf = one.netifs['net1'].name
        twoaddr = two.netifs['net1'].ipv4_prefix.addressstr()

        # Fetch the arp table in a results object
        res = results.column_table_result_t(one,
                                            "show arp location 0/0/CPU0",
                                            [ "Address", "Age", "Hardware Addr", "State", "Type", "Interface" ])
        # print all the results
        exec_logger.info("Results:\n" + pprint.pformat(res.entries))

        # print some info from the results
        macaddr = res.entries[twoaddr]['Hardware Addr']
        intf = res.entries[twoaddr]['Interface']
        exec_logger.info("One has " + twoaddr + " on " + intf + " with addr " + macaddr)

        # grab the actual mac value from two
        output = two.send_command("show interface " + twointf + " | inc Hardware is")
        match = re.search(r'address is ([0-9A-Za-z\.]+)', output)
        assert match

        # Assert that two's idea of it's mac is what we have in our arp table.
        self.assert_test_case(match.group(1) == macaddr)

    def test_log_arp_result_real (self):
        twointf = two.netifs['net1'].name
        twoaddr = two.netifs['net1'].ipv4_prefix.addressstr()

        # Fetch the arp table on router one in a results object
        res = results.column_table_result_t(one,
                                            "show arp location 0/0/CPU0",
                                            [ "Address", "Age", "Hardware Addr", "State", "Type", "Interface" ])

        for intf in topology.networks['net1'].interfaces:
            macaddr = res.entries[intf.ipv4_prefix.addressstr()]['Hardware Addr']

            # grab the actual mac value from two and verify it's correct
            output = intf.router.send_command("show interface " + intf.name + " | inc Hardware is")
            match = re.search(r'address is ([0-9A-Za-z\.]+)', output)
            exec_logger.info("Checking macaddr for " + intf.fullname())
            self.assert_test_case(match.group(1) == macaddr)

# * Run the test suite.
topology.execute_test(example_suite_t())


# -*- coding: utf-8 -*-
# @Time : 2019/4/15 21:40
# @Author : Ymy
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_word():
	app.logger.error('sdddsds')
	return 'hello_word'

if __name__ == '__main__':
	app.run('0.0.0.0')


import numpy as np
sample_list = np.array([[0, 1, 3], [5, 7, 9]])
sample_list1 = np.array([[0, 2, 4], [6, 8, 10]])
x = np.concatenate((sample_list, sample_list1), 1)
print(x)


#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import sys
import time
import argparse
import logging
import requests
from BeautifulSoup import BeautifulSoup

log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
loghandler = logging.StreamHandler(sys.stderr)
loghandler.setFormatter(logging.Formatter("[%(asctime)s] %(message)s"))
log.addHandler(loghandler)

base_url = "http://www.tripadvisor.com/"
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36"

parser = argparse.ArgumentParser(description='Scrape tripadvisor')
parser.add_argument('-datadir', type=str,
                    help='Directory to store raw html files',
                    default="data/")
parser.add_argument('-state', type=str,
                    help='State for which the hotel data is required.',
                    required=True)
parser.add_argument('-city', type=str,
                    help='City for which the hotel data is required.',
                    required=True)
args = parser.parse_args()

def get_city_page(city, state):
    """ Returns the URL of the list of the hotels in a city. Corresponds to
    STEP 1 & 2 of the slides.

    Parameters
    ----------
    city : str

    state : str

    Returns
    -------
    url : str
        The relative link to the website with the hotels list.

    """
    # Build the request URL
    url = base_url + "city=" + city + "&state=" + state
    # Request the HTML page
    headers = {'User-Agent': user_agent}
    response = requests.get(url, headers=headers)
    html = response.text.encode('utf-8')
    with open(os.path.join(args.datadir, city + '-tourism-page.html'), "w") as h:
        h.write(html)

    # Use BeautifulSoup to extract the url for the list of hotels in
    # the city and state we are interested in.

    # For example in this case we need to get the following href
    #

    # ...
    soup = BeautifulSoup(html)
    li = soup.find("li", {"class": "hotels twoLines"})
    city_url = li.find('a', href=True)
    return city_url['href']

def get_hotellist_page(city_url, page_count):
    """ Returns the hotel list HTML. The URL of the list is the result of
    get_city_page(). Also, saves a copy of the HTML to the disk. Corresponds to
    STEP 3 of the slides.

    Parameters
    ----------
    city_url : str
        The relative URL of the hotels in the city we are interested in.
    page_count : int
        The page that we want to fetch. Used for keeping track of our progress.

    Returns
    -------
    html : str
        The HTML of the page with the list of the hotels.
    """
    url = base_url + city_url
    # Sleep 2 sec before starting a new http request
    time.sleep(2)
    # Request page
    headers = { 'User-Agent' : user_agent }
    response = requests.get(url, headers=headers)
    html = response.text.encode('utf-8')
    # Save the webpage
    with open(os.path.join(args.datadir, args.city + '-hotelist-' + str(page_count) + '.html'), "w") as h:
        h.write(html)
    return html

def parse_hotellist_page(html):
    """Parses the website with the hotel list and prints the hotel name, the
    number of stars and the number of reviews it has. If there is a next page
    in the hotel list, it returns a list to that page. Otherwise, it exits the
    script. Corresponds to STEP 4 of the slides.

    Parameters
    ----------
    html : str
        The HTML of the website with the hotel list.

    Returns
    -------
    URL : str
        If there is a next page, return a relative link to this page.
        Otherwise, exit the script.
    """
    soup = BeautifulSoup(html)
    # Extract hotel name, star rating and number of reviews
    hotel_boxes = soup.findAll('div', {'class' :'listing wrap reasoning_v5_wrap jfy_listing p13n_imperfect'})
    if not hotel_boxes:
        log.info("#################################### Option 2 ######################################")
        hotel_boxes = soup.findAll('div', {'class' :'listing_info jfy'})
    if not hotel_boxes:
        log.info("#################################### Option 3 ######################################")
        hotel_boxes = soup.findAll('div', {'class' :'listing easyClear  p13n_imperfect'})

    for hotel_box in hotel_boxes:
        #print info for each hotel
        h  = hotel_box.find("a", {"target" : "_blank"})
        hotel_url = base_url + h['href']
        time.sleep(2)
        heads = { 'User-Agent' : user_agent }
        resp = requests.get(hotel_url, headers=heads)
        hml = resp.text.encode('utf-8')
        soup2 = BeautifulSoup(hml) #get the soup for the hotel's page so we can scrape the info
        rating = soup2.findAll('span', {'class' : 'compositeCount'})
        hotel_name = hotel_box.find("a", {"target" : "_blank"}).find(text=True)
        log.info("Hotel name: %s" % hotel_name.strip())

        #traveler rating
        ratings = []
        for r in rating:
            ratings.append(r.find(text = True))
        log.info("Traveler rating for excellent: %s" % ratings[0])
        log.info("Traveler rating for very good: %s" % ratings[1])
        log.info("Traveler rating for average: %s" % ratings[2])
        log.info("Traveler rating for poor: %s" % ratings[3])
        log.info("Traveler rating for terrible: %s" % ratings[4])

        #reviews
        review = soup2.findAll('div', {'class' : 'value'})
        reviews = []
        for r in review:
            reviews.append(r.find(text = True))

        log.info("See reviews for families: %s" % reviews[0])
        log.info("See reviews for couples: %s" % reviews[1])
        log.info("See reviews for solo: %s" % reviews[2])
        log.info("See reviews for business: %s" % reviews[3])

        #review summary
        sp = soup2.findAll('span', {'class' : 'rate sprite-rating_s rating_s'})
        summaries = []
        count = 1
        for item in sp:
            s = item.find('img', alt = True)
            rate = s['alt'].strip()[0:3]
            if rate[-1] == 'o':
                summaries.append(rate[0:1])
            else:
                summaries.append(rate)

        log.info("Rating summary for sleep quality: %s" % summaries[0])
        log.info("Rating summary for location: %s" % summaries[1])
        log.info("Rating summary for rooms: %s" % summaries[2])
        log.info("Rating summary for service: %s" % summaries[3])
        log.info("Rating summary for value: %s" % summaries[4])
        log.info("Rating summary for cleanliness: %s" % summaries[5])

    # Get next URL page if exists, otherwise exit
    div = soup.find("div", {"class" : "pgLinks"})
    # check if this is the last page
    if div.find('span', {'class' : 'guiArw pageEndNext'}):
        log.info("We reached last page")
        sys.exit()
    # If not, return the url to the next page
    hrefs = div.findAll('a', href= True)
    for href in hrefs:
        c = href['class'].encode('ascii', 'ignore')
        if c == 'guiArw sprite-pageNext ':
            log.info("Next url is %s" % href['href'])
            return href['href']

if __name__ == "__main__":
    # Get current directory
    current_dir = os.getcwd()
    # Create datadir if does not exist
    if not os.path.exists(os.path.join(current_dir, args.datadir)):
        os.makedirs(os.path.join(current_dir, args.datadir))

    # Get URL to obtaint the list of hotels in a specific city
    city_url = get_city_page(args.city, args.state)
    c=0
    while(True):
        c +=1
        html = get_hotellist_page(city_url,c)
        city_url = parse_hotellist_page(html)


import numpy as np
from math import radians, cos, sin
from projection import *
g = 9.8
def move(roll, pitch, yaw, thrust, mass):
    A = radians(yaw)
    B = radians(pitch)
    C = radians(roll)
    f = np.matrix([0,0,float(thrust)/float(mass), 1])
    R = np.matrix([[cos(A)*cos(B), cos(A)*sin(B)*sin(C)-sin(A)*cos(C), cos(A)*sin(B)*cos(C) + sin(A)*sin(C), 0],
                   [sin(A)*cos(B), sin(A)*sin(B)*sin(C)+cos(A)*cos(C), sin(A)*sin(B)*cos(C) - cos(A)*sin(C), 0],
                   [-sin(B),       cos(B)*sin(C),                      cos(B)*cos(C),                        0],
                   [0,             0,                                  0,                                    1]])
    a = R*np.transpose(f)
    a = np.transpose(a[:3])
    return a


# Kandu! Entertaiment est. 6/30/2018
# This is the store module for Mysteries of Galoo.
# This module will set up the store to buy sell inventory using gold
from armory import get_armor, get_amulet, get_weapon
from character import *
from game_items import *
import setup

class Store:
    def __init__(self, classtype):
        self._classtype = classtype
        self._equipment = setup.starting_character._equipment
        self._inventory = None
        self._in_store = True
        self._item = None

    def generate_armory_item(self):
        new_armor_1 = get_armor(setup.starting_character._type)
        new_armor_2 = get_armor(setup.starting_character._type)
        new_armor_3 = get_armor(setup.starting_character._type)
        new_weapon_1 = get_weapon(setup.starting_character._type)
        new_weapon_2 = get_weapon(setup.starting_character._type)
        new_weapon_3 = get_weapon(setup.starting_character._type)
        new_amulet_1 = get_amulet(setup.starting_character._type)
        new_amulet_2 = get_amulet(setup.starting_character._type)
        new_amulet_3 = get_amulet(setup.starting_character._type)

        self._inventory = [[new_armor_1, new_armor_2, new_armor_3],
                           [new_weapon_1, new_weapon_2, new_weapon_3],
                           [new_amulet_1, new_amulet_2, new_amulet_3]]
        return self._inventory[random.randint(0, 2)][random.randint(0, 2)]

    def display_item(self, item):
        print(item)

    def bought_equipped(self):
        print()
        print("Your item has been bought and equipped")
        print()

    def bought_inventory(self):
        print()
        print("Your item has been bought and added to your inventory")
        print()

    def sell_inventory_item(self, item, quantity):
        self._item = item
        self._item.zero()
        self._item.increment_quantity(quantity)
        price = self._item.get_price()
        setup.starting_character._inventory['Gold'] += price

    def buy_inventory_item(self, item, quantity):
        self._item = item
        self._item.zero()
        self._item.increment_quantity(quantity)
        price = self._item.get_price()
        if setup.starting_character._inventory['Gold'] >= price:
            setup.starting_character._inventory['Gold'] -= price
            setup.starting_character.add_to_inventory(self._item.get_item())
        else:
            print("You do not have enough gold to purchase that item")

    def buy_armor_item(self, item):
        self._item = item
        player_item = None
        type_collector = []
        for key_item, value in self._item.items():
            for key, value in value.items():
                if key == "Worth":
                    gold_value = value

        if setup.starting_character._inventory['Gold'] >= gold_value:
            for item in setup.starting_character._equipment:
                for item_name, attributes in item.items():
                    for attribute_name, attribute_value in attributes.items():
                        if attribute_name == 'Type':
                            type_collector.append(attribute_value)

            for item_name, value in self._item.items():
                for key, value in value.items():
                    if key == 'Type':
                        bought_value = value
                    if key == 'Worth':
                        worth_value = value

            def index_indicator():
                index = 1
                for type in type_collector:
                    if bought_value == type:
                        return index
                    else:
                        index += 1

            if index_indicator() == 1:
                player_item = setup.new_equipment_1
            elif index_indicator() == 2:
                player_item = setup.new_equipment_2
            elif index_indicator() == 3:
                player_item = setup.new_equipment_3
            elif index_indicator() == 4:
                player_item = setup.new_equipment_4
            elif index_indicator() == 5:
                player_item = setup.new_equipment_5
            elif index_indicator() == 6:
                player_item = setup.new_equipment_6
            elif index_indicator() == 7:
                player_item = setup.new_equipment_7

            for item_name, attributes in player_item.items():
                for attribute_name, attribute_value in attributes.items():
                    if attribute_name == 'Worth':
                        player_item_worth = attribute_value

            setup.starting_character.remove_item(player_item, index_indicator() - 1)
            setup.starting_character.equip(self._item, index_indicator() - 1)
            setup.starting_character._inventory['Gold'] -= gold_value
            setup.starting_character._inventory['Gold'] += player_item_worth

        else:
            print("You dont have enought gold yet")

    def display(self):
        choice = None
        while choice != 'x':
            print()
            print("<Welcome to the Store>")
            print("Press 1 to Buy Armor, Weapons, and Amulets")
            print("Press 2 to Buy or Sell Inventory")
            print("Press 'x' to exit the Store")
            choice = str(input("Choice: "))

            if choice == "1":
                while choice != 'x':
                    item_1 = self.generate_armory_item()
                    item_2 = self.generate_armory_item()
                    item_3 = self.generate_armory_item()
                    item_4 = self.generate_armory_item()
                    item_5 = self.generate_armory_item()
                    print()
                    print("""Welcome to Zizzle's Armory

Buy any item you wish there are only five items to choose from...
Once you buy a item from the store that same type of armor, weapon or amulet in your equipment
will then be sold to the store and replaced with the armor, weapon, or amulet you purchase

Happy Hunting! (press x to exit)""")
                    print()
                    self.display_item(item_1)
                    print()
                    self.display_item(item_2)
                    print()
                    self.display_item(item_3)
                    print()
                    self.display_item(item_4)
                    print()
                    self.display_item(item_5)
                    print()
                    try:
                        print("To Buy an Item Enter the Number [Top to Bottom]")
                        choice = str(input("Choice: "))
                        choices = ['1', '2', '3', '4', '5', 'x']
                        if choice in choices:
                            if choice == '1':

                                self.buy_armor_item(item_1)

                            elif choice == '2':

                                self.buy_armor_item(item_2)

                            elif choice == '3':

                                self.buy_armor_item(item_3)

                            elif choice == '4':

                                self.buy_armor_item(item_4)

                            elif choice == '5':

                                self.buy_armor_item(item_5)
                            elif choice == 'x':
                                continue
                            self.bought_equipped()
                            buy_again = input("Do you wish to buy another item?: ")
                            if buy_again in ['n', 'no', 'N', 'No']:
                                choice = 'x'
                            else:
                                continue
                        else:
                            print()
                            print("Please select a piece of equipment from above or press x to exit the store")
                    except TypeError:
                        print("Please select and enter a number from the items above")
                        sleep(2)
                    except ValueError:
                        print("Please select and enter a number from the items above")
                        sleep(2)
            if choice == "2":
                print("Press 1 to Buy Inventory")
                print("Press 2 to Sell Inventory")
                choice = input("Choice: ")

                if choice == '1':

                    while choice != 'x':
                        item_1 = get_inventory_item(1)
                        item_2 = get_inventory_item(2)
                        item_3 = get_inventory_item(3)
                        item_4 = get_inventory_item(4)
                        print()
                        print("Welcome to the Zizzle's Inventory Bazzar")
                        print("Buy Any Item You Wish")
                        print("1 Health Potion")
                        print("2 Defense Potion")
                        print("3 Energy Potion")
                        print("4 Strength Potion")
                        try:
                            print("To Buy an Item Enter the Number [Top to Bottom]")
                            choice = str(input("Choice: "))
                            choices = ['1', '2', '3', '4']
                            if choice in choices:
                                if choice == '1':
                                    quantity = int(input("How many do you want?: "))
                                    self.buy_inventory_item(item_1, quantity)

                                elif choice == '2':
                                    quantity = int(input("How many do you want?: "))
                                    self.buy_inventory_item(item_2, quantity)

                                elif choice == '3':
                                    quantity = int(input("How many do you want?: "))
                                    self.buy_inventory_item(item_3, quantity)

                                elif choice == '4':
                                    quantity = int(input("How many do you want?: "))
                                    self.buy_inventory_item(item_4, quantity)

                                self.bought_inventory()
                                buy_again = input("Do you wish to buy another item?: ")
                                if buy_again in ['n', 'no', 'N', 'No']:
                                    choice = 'x'
                                else:
                                    continue
                                sleep(2)
                            else:
                                print()
                                print("Please select an item from above or press x to exit the store")
                                sleep(2)
                        except ValueError:
                            print("Please select and enter a number from the items above")
                            sleep(2)

                if choice == '2':
                    while choice != 'x':
                        print()
                        print("Which item do you wish to sell from inventory")
                        print("Type name of item and quantity")
                        setup.starting_character.display_inventory()
                        try:
                            item = input("Choice of Item ex...(Health Potion): ")
                            quantity = int(input("Quantity to remove ex..(2, 3, 4): "))
                            setup.starting_character.remove_inventory_item(item, quantity)
                            if item == 'Health Potion':
                                item = get_inventory_item(1)
                            elif item == 'Defense Potion':
                                item = get_inventory_item(2)
                            elif item == 'Energy Potion':
                                item = get_inventory_item(3)
                            elif item == 'Strength Potion':
                                item = get_inventory_item(4)
                            elif item == 'Sapphire':
                                item = get_inventory_item(5)
                            elif item == 'Dirty Clothes':
                                item = get_inventory_item(6)
                            elif item == 'Ruby Necklace':
                                item = get_inventory_item(7)
                            elif item == 'Autographed Picture of Pee-wee Herman':
                                item = get_inventory_item(8)
                            elif item == 'Diamond':
                                item = get_inventory_item(9)
                            elif item == 'Dust':
                                item = get_inventory_item(10)
                            self.sell_inventory_item(item, quantity)
                            print("Transaction Completed")
                            print()
                            sell_again = input("Would you like to sell another item?: ")
                            if sell_again in ['n', 'N', 'no', 'No']:
                                choice = 'x'
                            else:
                                continue
                        except ValueError:
                            print("Try again")
            else:
                return


# -*- coding: utf8 -*-
from __future__ import unicode_literals

from django.db import models
import submit.models
from django.contrib.auth.models import User
from markdown import markdown
from django.utils.html import mark_safe

# sada uloh

class TaskSet(models.Model):
    id = models.SlugField(  # kratky nazov
        primary_key=True
    )
    title = models.CharField(  # pekny nazov
        max_length=256
    )
    public = models.BooleanField(  # nie public vidi iba admin
        default=True
    )

    # ak nie je public, moze ju vidiet iba admin
    def can_see(self, user):
        if not self.public:
            if (user.is_active and user.is_staff):
                return True
            return False
        return True

    def __unicode__(self):
        return '%s (%s)' % (self.title, self.id)

# uloha; citaci aj submitovatelny typ
class Task(models.Model):
    TASK_TEMPLATE = """Rozpravka
####Úloha

####Formát vstupu

####Formát výstupu

####Príklad

    Vstup
    Výstup

"""

    EX_SOL_TEMPLATE = """

Escape-nuty zdrojak

"""

    ID_HELP = """Nesmie byť zhodné s inou úlohou na Experimente."""

    TEXT_HELP = 'Obsah bude prehnaný Markdownom.'

    EX_SOL_HELP = 'Obsah bude prehnaný Markdownom.
' \
                  'Zdroják musí byť ' \
                  'escape-nutý od special chars.
 Používajte naň tag <pre></pre> ' \
                  '(blokový) alebo <code></code> (riadkový). Na syntax highlight pridajte ' \
                  '<pre class = "prettyprint"></pre>.'

    SUBMIT = 'S'
    READ = 'R'
    TYPES = (
        (SUBMIT, 'Submit'),
        (READ, 'Read'),
    )

    id = models.SlugField(  # kratky nazov
        primary_key=True,
        help_text=ID_HELP
    )
    title = models.CharField(  # pekny nazov
        max_length=256
    )
    text = models.TextField(  # zadanie ulohy alebo sprievodny text
        default=TASK_TEMPLATE,
        help_text=TEXT_HELP
    )
    example_solution = models.TextField(  # vzorak submitovatelnych uloch
        blank=True,
        default=EX_SOL_TEMPLATE,
        help_text=EX_SOL_HELP
    )

    public = models.BooleanField(  # nie public vidi iba admin
        default=True
    )

    type = models.CharField(  # ci sa submituje alebo cita
        max_length=1,
        choices=TYPES,
        default=SUBMIT
    )

    task_set = models.ForeignKey(TaskSet)  # do ktorej sady patri

    timestamps = models.ManyToManyField(  # zaznamenava zobrazenie zadania
        User,
        through='Stalker'
    )

    prereqs = models.ManyToManyField(  # preprekvizity (hrany, kt. do mna v grafe vedu)
        "self",
        symmetrical=False,
        related_name='prevs',
        blank=True
    )

    def __unicode__(self):
        return '%s: %s' % (self.task_set.id, self.id)

    # ak ja som public, ale moja sada nie je
    def _get_is_public(self):
        if self.public and self.task_set.public:
            return True
        return False
    is_public = property(_get_is_public)

    # prezenie cez markdown
    def _get_rendered_text(self):
        return mark_safe(markdown(self.text, safe_mode=False))
    rendered_text = property(_get_rendered_text)

    # prezenie cez markdown
    def _get_rendered_ex_sol(self):
        return mark_safe(markdown(self.example_solution, safe_mode=False))
    rendered_ex_sol = property(_get_rendered_ex_sol)

    # ci je vyriesena userom x
    def is_solved(self, user):
        if self.type == self.SUBMIT:
            n = submit.models.Submit.objects.filter(message=submit.models.Submit.OK, task=self, user=user).count()
            if n > 0:
                return True
            else:
                return False

        elif self.type == self.READ:
            s = Stalker.objects.filter(user=user, task=self)
            if s.count() >= 1:
                return True
            else:
                return False

    # ci user x moze vidiet zadanie = uz ju objavil
    def is_enabled(self, user):
        required = self.prereqs.all()
        for task in required:
            if not Task.is_solved(task, user):
                return False
        return True

    # ci user moze vediet o jej existencii
    def can_see(self, user, field):
        if (user.is_active and user.is_staff):
            return True
        if not self.public:
            return False
        if field == 't':  # text zadania
            if not self.is_enabled(user):
                return False
            return True
        if field == 's':  # solution    #ci je vyriesena userom x

            if not self.is_solved(user):
                return False
            else:
                return True
        if field == 'g':  # invisible ulohy v grafe
            return True
        return False

    # zisti pocet riesitelov (!= pocet OK)
    def _get_num_solvers(self):
        if self.type == self.SUBMIT:
            num = submit.models.Submit.objects.filter(
                message=submit.models.Submit.OK,
                task=self).distinct('user').count()
        elif self.type == self.READ:
            num = -1
        return num
    num_solvers = property(_get_num_solvers)

# loguje zobrazenia zadania; casom sa bude hodit na statistiky a achievementy (napr. ako dlho trva vyriesenie ulohy)
class Stalker(models.Model):
    task = models.ForeignKey(Task)  # ktora uloha
    user = models.ForeignKey(User)  # ktory user
    seen = models.DateTimeField(  # kedy
        null=True
    )

    def __unicode__(self):
        return '%s | %s | %s' % (self.user, self.task, self.seen)

# ktore ulohy a sady su pre ktoreho uzivatela aktivne (= zobrazuju sa defaultne)
class Active(models.Model):
    user = models.OneToOneField(User)  # ktory user
    task_set = models.ForeignKey(TaskSet, default='intro')  # ktoru sadu
    task = models.ForeignKey(Task, default='submit')  # ktoru ulohu

    # (niekedy v buducnosti) ci ma na pozeranie sady styl graf alebo zoznam
    # GRAPH = 'G'
    # LIST = 'L'
    # task_set_style = models.CharField(max_length = 1, default = GRAPH, choices = ((GRAPH, 'Graph'),(LIST, 'List')))

    def __unicode__(self):
        return '%s | %s | %s' % (self.user, self.task_set.id, self.task.id)


'''
原始 PCM 的录音参数必须符合 8k/16k 采样率、
16bit 位深、单声道，支持的格式有：pcm（不压缩）、wav（不压缩，pcm编码）、amr（压缩格式）
http://yuyin.baidu.com/docs/asr/190
'''

from aip import AipSpeech
from pydub import AudioSegment
import os
import datetime
starttime = datetime.datetime.now()
currentdir = os.getcwd()

def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()
def save_to_file(file_name, contents):
    fh = open(file_name, 'w')
    fh.write(contents)
    fh.close()
def get_relative_time():
    return datetime.datetime.now()-starttime
def loop_and_asr_all(filenum,client):
    # filenum = return value of load_and_divide_wav function
    resultstr = str()
    for time in range(1,filenum+1):
        print('*****Trail',time,'Transforming*****')
        print('TIME:',get_relative_time())
        tempname = currentdir + '/TEMPWAV/seg' + str(time) + '.wav'
        tempresult = client.asr(get_file_content(tempname), 'wav', 16000, {'dev_pid': '1536',})
        print('Trail',time,'GETING:','\n',tempresult)
        if tempresult['err_msg'] != 'success.':
            print('ERROR:',tempresult['err_msg'])
            break
        resultstr = resultstr + tempresult['result'][0]
    return resultstr
def load_and_divide_wav(wavfilename,SecPerFile):#SecPerFile: MAX 59.9
    # WAV LOAD and Get LENGTH in seconds
    wavload = AudioSegment.from_wav(wavfilename) # converted to AudioSegment Object
    wavLenSEC = wavload.duration_seconds
    dividetimes = int(wavLenSEC/SecPerFile)+1
    print(' Wave Length[s]: ',wavLenSEC,'\n','Divide Times: ', dividetimes)
    # WAV FILE CUT: EACH 50s
    print('***DIVIDING WAV FILE***')
    print('Segments:',dividetimes)
    for time in range(1,dividetimes+1):
        tempname = currentdir + '/TEMPWAV/seg' +str(time) + '.wav'
        wavload[SecPerFile*(time-1)*1000:SecPerFile*time*1000].export(tempname,format='wav')
    print('***DIVIDING COMPLETE***')
    return dividetimes

def get_client():
# Input APP_ID & API_KEY & SECRET_KEY Here
    APP_ID = ''
    API_KEY = ''
    SECRET_KEY = ''
    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
    return client

# WAV FILENAME INPUT
wavname = input('[TYPE:WAV] FILENAME: ')
wavinput = wavname + '.wav'
if len(wavname)<1 : wavinput = 'test.wav'
print('Analysing WAV File:',wavinput)

# Divide File into small pieces
dividetimes = load_and_divide_wav(wavinput,50)
# Get ASR result as a string: resultstr
resultstr = loop_and_asr_all(dividetimes,get_client())

# Print result
print('TIME:',get_relative_time())
print('ASR Result: ',resultstr)
savename = 'RESULT'+ wavname +'.txt'
save_to_file(savename,resultstr)
print('File has been saved to',currentdir)


import os, sys
import json
from os.path import abspath, dirname, join

sys.path.append(dirname(abspath(__file__)))
sys.path.append(dirname(dirname(abspath(__file__))))
#FAB_BASE_DIR = os.path.dirname(os.path.abspath(__file__))

os.environ.setdefault('TA2_STATIC_TEST_MODE',
                      'False')
os.environ.setdefault('DJANGO_SETTINGS_MODULE',
                      'tworavensproject.settings.local_settings')

import django
try:
    django.setup()
except Exception as e:
    print("WARNING: Can't configure Django. %s" % e)

from tworaven_apps.datamart_endpoints.static_vals import \
    (DATAMART_ISI_NAME,)
from tworaven_apps.datamart_endpoints.materialize_util import \
    (MaterializeUtil,)
from tworaven_apps.datamart_endpoints.tasks import make_materialize_call

def test_materialize():
    """run materialize"""
    user_workspace_id = 89

    sr_fname = join('.', 'input', 'isi_materialize_req_01.json')

    datamart_params = json.loads(open(sr_fname, 'r').read())

    mu = MaterializeUtil(DATAMART_ISI_NAME,
                         user_workspace_id,
                         datamart_params)

    if mu.has_error():
        print('error: ', mu.get_error_message())
    else:
        for key, val in mu.materialize_result.items():
            if not val:
                print(f'\n{key}: (none)')
            else:
                print('\n%s: %s' % (key, str(val)[:150]))

        #print('materialize_result', mu.materialize_result)
    #    print('success: ', mu.materialize_result)
    #mu.show_info()

def test_celery_materialize():
    """run materialize"""
    user_workspace_id = 21

    sr_fname = join('.', 'input', 'isi_materialize_req_01.json')

    datamart_params = json.loads(open(sr_fname, 'r').read())

    mu_info = make_materialize_call(\
                     DATAMART_ISI_NAME,
                     user_workspace_id,
                     datamart_params,
                     **dict(websocket_id='dev_admin'))

    if not mu_info.success:
        print('error: ', mu_info.err_msg)
    else:
        print('mu_info msg', mu_info.result_obj)

if __name__ == '__main__':
    test_celery_materialize()


#""" This is my fizzbuzz program """
count = int(input('Gimme the top number: '))

def fizzbuzz(max_number):
    # """ this is the fizzbuzz algorithm applied to the value x """
    for x in range(1,max_number):
        if x % 3 == 0:
            print('fizz')
        elif x % 5 == 0:
            print('buzz')
        elif x % 3 == 0 and x % 5 == 0:
            print('fizzbuzz')
        else:
            print(x)

fizzbuzz(count)


#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "版权所有@源码商城：https://shop530346312.taobao.com/?spm=a1z10.1-c.0.0.1c1f6daeeNP5VM"
__date__ = "2019/05/10 11:45"

# form表单验证

from django import forms
from .models import GetMessage

class GetMessageForm(forms.ModelForm):
    """读者留言验证,注意这里继承的是ModelForm"""

    class Meta:
        """继承GetMessage类"""
        model = GetMessage
        fields = ['name', 'email', 'subject', 'message']


import os

# The custom shell for termi to use.
# Default: os.environ["SHELL"]
shell = os.environ["SHELL"]

# The fallback shell for termi to use incase the normal shell breaks.
fallback_shell = "/bin/sh"

# The working directory for the terminal to use when creating new VTEs.
# Default: os.environ["HOME"]
working_directory = os.environ["HOME"]

# Whether or not termi should use an audible bell. Default is False
audible_bell = False

# The default editor for termi to open the configuration file in
# when the configure button is pressed.
editor = "code"


from decimal import Decimal
from django.conf import settings
from category.models import Item

class Order(object):
	def __init__(self, request):
		self.session = request.session
		order = self.session.get(settings.ORDER_SESSION_ID)
		if not order:
			order = self.session[settings.ORDER_SESSION_ID] = {}
		self.order = order

	def add(self, item, quantity=1, update_quantity=False):
		item_id = str(item.id)
		if item_id not in self.order:
			self.order[item_id] = {'quantity': 0, 'weight': str(item.weight)}
		if update_quantity:
			self.order[item_id]['quantity'] = quantity
		else:
			self.order[item_id]['quantity'] += quantity
		self.save()

	def save(self):
		self.session[settings.ORDER_SESSION_ID] = self.order
		self.session.modified = True

	def remove(self, item):
		item_id = str(item.id)
		if item_id in self.order:
			del self.order[item_id]
			self.save()

	def __iter__(self):
		item_ids = self.order.keys()
		items = Item.objects.filter(id__in=item_ids)
		for item in items:
			self.order[str(item.id)]['item'] = item

		for item in self.order.values():
			item['weight'] = Decimal(item['weight'])
			item['total_weight'] = item['quantity'] * item['weight']
			yield item

	def __len__(self):
		return sum(item['quantity'] for item in self.order.values())

	def get_total_weight(self):
		return sum(Decimal(item['weight'])*item['quantity'] for item in self.order.values())

	def clear(self):
		del self.session[settings.ORDER_SESSION_ID]
		self.session.modified = True


# -*- coding:utf-8 -*-
# 通过PDF，生成训练数据
# !!!! 生成Latex公式  https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_9166116218594072791%22%7D&n_type=0&p_from=1

import os
import argparse
import numpy as np
import random
from tools import pdf_latex_utils as pdf
from tools import latex_txt_utils as txt
import cv2
import lmdb
import time

def writeCache(env, cache):
    with env.begin(write=True) as txn:
        for k, v in cache.items():
            txn.put(k.encode(), v)

def write_error(data_root, err_lines):
    with open(os.path.sep.join([data_root, 'formula_error.txt']), 'a', encoding='utf-8') as f:
        err_lines = '\n'.join(err_lines)
        f.write(err_lines)

# 生成训练数据, 通过latex组成pdf
''' https://latex.91maths.com/eg/czjxjh.html 常用的学校数学公式
    data root :     数据根目录
    formula file:   数学公式文本
'''
def gen_train_data_by_pdf(data_root, formula_file, begin_pos=8750, key_idx=6586, size=100):

    outputPath = os.path.sep.join([data_root, 'lmdb'])
    env = lmdb.open(outputPath, map_size=500511627)

    with open(os.path.sep.join([data_root,'data',formula_file]), 'r', encoding='utf-8') as f:
        formula_lines = f.readlines()

    # print('formula lines:', len(formula_lines))
    # 不支持数组方式，后期看怎么解决
    formula_lines = [txt.latex_remove_space(x).strip() for x in formula_lines if len(x.strip()) > 0]

    key_idx = 0
    try:
        with env.begin(write=False) as txn:
            nSamples = int(txn.get('total'.encode()))

        key_idx = nSamples
    except:
        pass

    # key_idx = 3500

    print('key idx:', key_idx)

    batch_size = 5
    m_lines = []
    start_time = time.time()

    for idx, m in enumerate(formula_lines[begin_pos:]):
        if idx % 50 ==  0:
            print('已经处理{}条数据, 索引序列: {} 耗时：{:.3f}'.format(idx,key_idx,(time.time() - start_time)))
            start_time = time.time()

        if (idx+1) % batch_size == 0:

            try:
                # 通过PDF生成数学公式图片
                print('m lines:', m_lines)
                pdf.gen_latex_pdf(data_root, 'temp', m_lines)
                # 得到PDF中公式图片位置信息
                math_images = pdf.gen_latex_img_pos(data_root=data_root, file_name='temp',imgH=8192)
                print('idx :', idx, ' math images len:', len(math_images))

                cache = {}

                for _idx in range(len(m_lines)):
                    # t_key =
                    cache[f't_{key_idx}'] = m_lines[_idx].encode()
                    cache[f'i_{key_idx}'] = cv2.imencode('.jpg', math_images[_idx])[1].tobytes()
                    cache['total'] = str(key_idx).encode()
                    key_idx = key_idx + 1

                writeCache(env,cache)
            except Exception as e:
                print(e)
                write_error(data_root, m_lines)

            m_lines = []
        else:
            m_lines.append(m)

        # writeCache(env, {'total':str(key_idx).encode()})

    print('生成训练数据完成！')

# 生成训练数据，采用下载的训练数据, 暂时不用。
def gen_train_data_by_file(data_root):
    formula_file_name = os.path.sep.join([data_root, 'data', 'im2latex_formulas.norm.txt'])
    train_file_name = os.path.sep.join([data_root, 'data','im2latex_train_filter.txt'])

    with open(formula_file_name, 'r', encoding='utf-8') as f:
        formula_lines = f.readlines()

    with open(train_file_name, 'r', encoding='utf-8') as f:
        train_lines = f.readlines()

    outputPath = os.path.sep.join([data_root, 'lmdb'])
    env = lmdb.open(outputPath, map_size=5024511627)

    for idx, item in enumerate(train_lines):
        img_name, formula_idx = item.split()
        # print('image name:', img_name, ' formula index:', formula_idx)
        image_data = get_image_data(os.path.sep.join([data_root,'data', 'images',img_name]))
        formula_txt = formula_lines[int(formula_idx)]
        cache = {}
        cache[f't_{idx}'] = formula_txt.encode()
        cache[f'i_{idx}'] = image_data
        if idx % 100 == 0:
            cache['total'] = str(idx).encode()
            print('load data number : ', idx)
        writeCache(env, cache)
    cache = {}
    cache['total'] = str(idx).encode()
    writeCache(env, cache)

def get_image_data(image_file):
    with open(image_file, 'rb') as f:
        data = f.read()
    return data

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description='gen latex pos train data')
    parser.add_argument('--data_root', default='D:\\PROJECT_TW\\git\\data\\im2latex',help='data set root')
    parser.add_argument('--formula_file', default='im2latex_formulas_custom.txt', type=str, help='数学公式文本')
    parser.add_argument('--gen_size', default='5',type=int,help='gen size')
    args = parser.parse_args()
    # gen_train_data_by_file(args.data_root)
    gen_train_data_by_pdf(args.data_root,formula_file=args.formula_file, begin_pos=0,key_idx=0)


# -*- coding: utf-8 -*-
"""
Created on Fri Sep 18 19:00:18 2015

@author: AliSajid
"""

from rosalind.common import Problem, TestData

def make_data(data):
    return [int(x) for x in data.split(" ")]

def calculate_rabbits(data):
    n, k = data
    if n <= 2:
        return 1
    else:
        return calculate_rabbits((n-2,k)) * k + calculate_rabbits((n-1,k))

test = TestData("5 3", 19)
fib = Problem(name="fib", run_function=calculate_rabbits, clean_function=make_data, test_data=test)

print(fib.solve_problem(test.input))
print(fib.test_solution())
print(fib.solve_problem())
fib.output_file()


from  path import Path
import imageio
import cv2

root = Path('/home/roit/datasets/2019c/car/cut')
dump_root = Path('./car_cut_256512')
dump_root_dir = dump_root.makedirs_p()

folderName = Path(root)
if folderName.exists()==False:
	print('folder no exist')
files = folderName.files('*.{}'.format('png'))
files.sort()
size = (512,256)
frames =[]
for img_p in files:
    img = cv2.imread(img_p)
    img = cv2.resize(src=img,dsize=size,interpolation=cv2.INTER_AREA)
    cv2.imwrite(dump_root_dir/img_p.stem+'.jpg',img)
    frames.append(img)
    print('ok')


''' Web Routes '''
from masonite.helpers.routes import get, post, group

ROUTES = [
    get('/', 'HomeController@index').name('welcome'),
    get('/login', 'LoginController@show').name('login'),
    post('/login', 'LoginController@store'),
    get('/register', 'RegisterController@show'),
    post('/register', 'RegisterController@store'),
    get('/questions/@id', 'QuestionController@show'),

    # auth middleware
    get('/logout', 'LoginController@logout').middleware('auth'),
    get('/ask', 'QuestionController@create').middleware('auth'),
    group('/questions', [
        post('', 'QuestionController@store').middleware('auth'),
        post('/@id/answers', 'AnswerController@store').middleware('auth'),
        get('/@id/upvote', 'QuestionController@upvote').middleware('auth'),
        get('/@id/downvote', 'QuestionController@downvote').middleware('auth'),
    ]),
    get('/me/questions', 'QuestionController@questions').middleware('auth'),
    get('/me/answers', 'AnswerController@answers').middleware('auth'),
]


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

from gi.repository import Gio, GLib, Gtk, EvinceView, EvinceDocument
import sys, subprocess, os

class PdfPreviewer(Gtk.Window):

	def __init__(self):
		Gtk.Window.__init__(self)

		# Path to the PDF file that is shown in the preview.
		# This corresponds to the master .tex filed with ".tex"
		# replaced by ".pdf"
		self._filename = None

		# List of tex files loaded by this preview.
		self._tex_files = []

		self.set_title("Tex companion")
		self.connect("destroy", self.destroy)

		self.build_ui()
		self.set_size_request(480, 480)

	def build_ui(self):
		"""Init the UI of the TexCompanion"""

		# Construct a Box that will hold the status bar
		# and that will display the errors when needed
		box = Gtk.Box(orientation = Gtk.Orientation.VERTICAL,
			      spacing = 6)

		EvinceDocument.init()
		self._view = EvinceView.View()

		scrolled_window = Gtk.ScrolledWindow()
		scrolled_window.add(self._view)

		box.pack_start(scrolled_window, True, True, 0)

		# Status bar creation
		self._status_bar = Gtk.Label("")
		self._status_bar.set_justify(Gtk.Justification.LEFT)
		self._status_bar.set_alignment(0.0, 0.5)
		self._status_bar.set_padding(6, 0)
		box.pack_start(self._status_bar, False, True, 0)

		self.add(box)

	def show_message(self, message):
		self._status_bar.set_markup(message)

		while Gtk.events_pending():
			Gtk.main_iteration()

	def destroy(self, win = None):
		Gtk.main_quit()

	def add_tex_file(self, tex_file):
		self._tex_files.append(tex_file)

		# Add some hooks to update this window when
		# the file is compiled
		tex_file._on_compilation_start.append (lambda : self.show_message("Reloading file..."))
		tex_file._on_compilation_finished.append(lambda error_message :
							 self.reload_file(error_message))

		self.load_file(tex_file.get_master().get_filename().replace(".tex", ".pdf"))

	def load_file(self, path):
		self._filename = path
		doc = EvinceDocument.Document.factory_get_document ("file://" +
								    os.path.abspath(path))
		self._model = EvinceView.DocumentModel.new_with_document(doc)
		self._model.set_sizing_mode(EvinceView.SizingMode.BEST_FIT)
		self._view.set_model(self._model)

		self.show_message("Document loaded")

	def reload_file(self, error_message):
		if error_message is not None:
			self.show_message("<b>An error occurred during the compilation</b>:\n" +
					  error_message)
		elif self._filename is not None:
			current_page = self._model.get_page()
			self.load_file(self._filename)
			self._model.set_page(current_page)

class TexFile():

	def __init__(self, filename, main_file = None):
		self._filename = filename
		self._file = Gio.File.new_for_path(self._filename)

		self.reload_hash()
		self._main_file = main_file

		self._monitor = self._file.monitor_file(Gio.FileMonitorFlags.NONE, None)
		self._monitor.connect("changed", self.on_file_changed)

		self._compiling = False

		# List of hooks functions that should be called before
		# and after a compilation. The latter functions will be
		# passed a string describing the error in the compilation,
		# of True if it didn't fail.
		self._on_compilation_start = []
		self._on_compilation_finished = []

	def reload_hash(self):
		with open(self._filename) as handle:
			self._hash = hash(handle.read())

	def on_file_changed(self, file, other_file, event_type, user_param):
		old_hash = self._hash
		self.reload_hash()

		if old_hash != self._hash:
			self._compiling = True
			self.compile()
			self._compiling = False

	def is_master(self):
		return self._main_file is None

	def get_filename(self):
		return self._filename

	def get_master(self):
		return self if self._main_file is None else self._main_file

	def compile(self):

		for hook in self._on_compilation_start:
			hook()

		error_message = ""
		if self._main_file is None:
			# We are the main file, compile!
			p = subprocess.Popen([ "pdflatex", "-interaction", "nonstopmode",
					       self._filename ],
					     stdout = subprocess.PIPE)
			output = p.communicate()[0]

			if p.wait() != 0:
				lines = output.split("\n")
				for index in [ lines.index(l) for l in lines
					       if len(l) > 0 and l[0] == '!' ]:
					error_message += "\n".join(
						lines[index:min(index+3, len(lines))])
		else:
			self._main_file.compile()

		for hook in self._on_compilation_finished:
			hook(error_message if error_message != "" else None)

if __name__ == "__main__":

	preview = PdfPreviewer()

	files = sys.argv[1:]
	if len(sys.argv) > 1:
		main_file = TexFile(files[0])
		preview.add_tex_file(main_file)

	for tex_file in files[1:]:
		preview.add_tex_file(TexFile(tex_file, main_file))

	preview.show_all()
	Gtk.main()


from pymongo import MongoClient

class DB:

    def __init__(self):
        client = MongoClient()
        self.db = client.get_database("hackathon")
        self.reports = self.db.get_collection("reports")

    def insert_report(self, li_num, _date, _time, img,area):
        report = {
            'license_number': li_num,
            'date': _date,
            'time': _time,
            'image': img,
            'area': area,
            'status': 'not sent'
        }

        self.reports.insert_one( report)


from flask import Flask, request, jsonify, g, redirect, url_for

import datetime
#from isoweek import Week

from db.database_handler import *
from db.models import *

from functions import *

## STATIC

# houses
frankes = House("Fischer")
gaytopia = House("LMDG og Juren")
julleb = House("Mio og Benja")
frejasg = House("VT og HrS")

## people
fischer = Person("Fischer", frankes)
lmdg = Person("LMDG", gaytopia)
juren = Person("Juren", gaytopia)
mio = Person("Mio", julleb)
benja = Person("Benja", julleb)
valentin = Person("Valentin", frejasg)
hrs = Person("HrS", frejasg)

# ?
cle = Person("Simon", None)
mix = Person("Mikkel", None)
emil = Person("Elim R", None)
eide = Person("Amalie", None)

teams = [fischer, lmdg, mio, valentin, juren, benja, hrs]

db = create_session()

## CONTROLLERS

def next_dinner():
    week, year = this_week_year()

    return dinner(week,year), week

def get_household(week,year):
    # append with number of all cancellations prior to the queried date
    cancellations = db.query(Cancellation).filter(Cancellation.year <= year, Cancellation.week <= week).all()
    skip = len(cancellations)

    return teams[(week - skip) % len(teams)]

def dinner(week,year):
    # check if we have a cancellation at this specific week
    try:
        cancel = db.query(Cancellation).filter(Cancellation.year == year, Cancellation.week == week).one()
        # ok, so we do have a cancellation; lets post that message instead
        return placeholder(cancel.msg)
    except:
        # we did not, so just proceed unnoticed
        pass

    #Check if date is a from swap and return swap info if so
    try:
        swapfrom = db.query(Swap).filter(Swap.from_year == year, Swap.from_week == week).all()
        swapfrom_sorted = sorted(swapfrom, key=lambda x: x.id, reverse=True)
        #print swapfrom_sorted
        most_recent_fromswap = swapfrom_sorted[0]
        #print most_recent_fromswap.to_week
        # ok, so we do have a cancellation; return the to swap info
        return get_household(most_recent_fromswap.to_week, year)
    except:
        # not a from swap week, so np
          pass

    # Check if date is a to swap and return swap info if so
    try:
        swapto = db.query(Swap).filter(Swap.to_year == year, Swap.to_week == week).all()
        swapto_sorted = sorted(swapto, key=lambda x: x.id, reverse=True)
        most_recent_toswap = swapto_sorted[0]
        #print most_recent_toswap.from_week
        # ok, so we do have a cancellation; return the from swap info
        return get_household(most_recent_toswap.from_week, year)
    except:
        # not a to swap week, so np
        pass

    return get_household(week,year)

# message: String, e.g. DurumBar, or "cancelled"
# NB: not a household, use swap for this
def skip_week(week, year, msg):
    db.query(Cancellation).filter(Cancellation.week == week, Cancellation.year == year).delete()

    #When a cancellation is cancelled and original is reinstated
    if msg == "":
        # undo potential swap increments
        swaps_after_cancels = db.query(Swap).filter(Swap.to_year == year, Swap.to_week >= week).all()
        for c in swaps_after_cancels:
            if c.from_week >= week:
                c.from_week -= 1
            c.to_week -= 1
        return

    # Check if and update swaps according to cancellation
    swaps_after_cancels = db.query(Swap).filter(Swap.to_year == year, Swap.to_week >= week).all()
    for c in swaps_after_cancels:
        if c.from_week >= week:
            c.from_week += 1
        c.to_week += 1

    c = Cancellation()
    c.week = week
    c.year = year
    c.msg = msg

    db.add(c)
    db.commit()

def swap_houses_to_db(from_week,from_year, to_week, to_year):
    s = Swap()
    s.from_week = from_week
    s.from_year = from_year
    s.to_week = to_week
    s.to_year = to_year

    db.add(s)
    db.commit()