Exemplo n.º 1
0
    def pickRequest(self, dbUpdateObject, sessionObject, botResponse, item):
        print botResponse + "Response 8"
        validateObject = Validate()
        info = self.messageSplit(botResponse)
        print botResponse + "Response 9"

        req_fields = dictonary.required_fields[info[1]].split('&')
        bot_qry = dictonary.bot_query
        func_map = dictonary.function_map
        print botResponse + "Response 10"
        for i in range(0, (len(req_fields) - 2)):
            info[i + 2] = validateObject.validate(i, info[i + 2])
            print botResponse + "Response 11"
            if str(info[i + 2]) == '0' or info[i + 2] == 0.0 or str(
                    info[i + 2]) == '':
                print botResponse + "Response 12"
                print bot_qry[req_fields[i + 1]]
                print item[1]
                x = sessionObject.respond(bot_qry[req_fields[i + 1]], item[1])
                print x
                return x

        temp = func_map[req_fields[len(req_fields) - 1]](
            info, item[1], dbUpdateObject, sessionObject)  ##########
        print "Naman Agarwal\n\n" + temp + "\n\n"
        dbUpdateObject.deletePendingRequest(item)
        return temp
Exemplo n.º 2
0
    def download_sub(self):
        print 'Validation: ' + str(self.validate)
        if self.validate:
            validate = Validate(self.movie_path)

        chain_iterators = chain(DBSub().download_sub(self.movie_path),
                                OpenSubs().download_sub(self.movie_path))

        for file_path in chain_iterators:
            if self.validate:
                subs = pysrt.open(file_path)
                text_slices = subs.slice(starts_after={'minutes': validate.start_min - 1, 'seconds': 59},
                                         ends_before={'minutes': validate.start_min,
                                                      'seconds': 11})
                text = ''
                for t_slice in text_slices.data:
                    text = text + t_slice.text + ' '
                text = ' '.join(text.split())
                print("For file : {} Movie Text is : {}".format(file_path, text))
                if validate.validate(text):
                    print("Found validated subtitle")
                    self._final(True)
                    return
                os.remove(file_path)
            else:
                continue
        self._final(False)
Exemplo n.º 3
0
    def test_dir_exists(self):
        # when dir exists
        try:
            Validate.dir_exists(".")
            self.assertTrue(False)
        except:
            self.assertTrue(True)

        # when dir doesn't exist
        exists = Validate.dir_exists("~/IdontExist")
        self.assertFalse(exists)
Exemplo n.º 4
0
    def test_isValidPort(self):

        validPorts = [2, 3, 87, 35589]
        for port in validPorts:
            errorMsg = str(port) + " is not valid."
            self.assertTrue(Validate.isValidPort(port), msg=errorMsg)

        inValidPorts = [0, 'af', 76666]

        for port in inValidPorts:
            errorMsg = str(port) + " is not valid."
            self.assertFalse(Validate.isValidPort(port), msg=errorMsg)
Exemplo n.º 5
0
    def testValidate(self):
        val = Validate(MultiDict([(u'size', u'0')]))
        size_cond = [
            ["%(name)s must be an integer", lambda i: not isinstance(int(i), int)],
            ["%(name)s cannot be negative", lambda i: int(i) < 0 ],
            ["%(name)s cannot exceed max value", lambda i: int(i) > 1024 ]
        ]

        # size is valid
        val.validate('size', size_cond)

        val = Validate(MultiDict([(u'size', u'foo')]))
        self.assertRaises(HTTPPreconditionFailed, val.validate, 'size', size_cond)
Exemplo n.º 6
0
    def test_isValidIP(self):

        correctIp = ['172.31.2.7', '1.2', '0.0']

        for ip in correctIp:
            errorMsg = ip + " is not valid."
            self.assertTrue(Validate.isValidIP(ip), msg=errorMsg)

        incorrectIp = ['1.a.2.3', '0.0.']

        for ip in incorrectIp:
            errorMsg = ip + " is not valid."
            self.assertFalse(Validate.isValidIP(ip), msg=errorMsg)
Exemplo n.º 7
0
    def validate_params(self):

        if not Validate.isValidIP(self.vip_ip):
            self.errorHelper("VIP is not valid. Exiting.")

        for ip in self.target_ip:
            if not Validate.isValidIP(ip):
                self.errorHelper("Target ip are not valid. Exiting.")

        if not Validate.isValidPort(self.vip_port):
            self.errorHelper("Port not valid. Exiting.")


        if not Validate.isInterfaceUp(self.vip_interface):
            self.errorHelper(self.vip_interface+" is not up. Check using ifconfig. Exiting.")
Exemplo n.º 8
0
def change_reset_password(reset, data):
    '''helper method for change password through reset token'''
    if not data:
        return dict(message="Fields can't be empty"), 400
    reset_password = data.get("reset_password")
    new_password = data.get("new_password")
    confirm_password = data.get("confirm_password")

    if reset_password is None or new_password is None:
        return dict(
            message="reset password or new password fields missing"), 400

    password = Validate().validate_password(new_password, confirm_password)
    if "message" in password:
        return password, 400

    user_identity = get_jwt_identity()
    user = UserModel.get_user_by_username(user_identity)
    if check_password_hash(reset, reset_password) is True:
        user.password = generate_password_hash(new_password)
        user.reset_password = False
        user.last_reset_password = datetime.now()
        user.save()

        #revoke reset token after successfully changing password
        json_token_identifier = get_raw_jwt()['jti']
        revoked_token = RevokedTokenModel(
            json_token_identifier=json_token_identifier)
        revoked_token.save()
        return dict(message="password changed successfully"), 200

    return dict(message="incorrect reset password"), 401
Exemplo n.º 9
0
 def start(self):
     for callback_label in range(self.crawler.__ParseFuncCount__):
         callback = self.crawler.__ParseFunc__[callback_label]
         # 获取代理
         proxy_list = self.crawler.get_proxies(callback)
         # 验证可用性
         Validate().valid_many(proxy_list, 'insert')
Exemplo n.º 10
0
    def post(self):
        '''method ['POST']'''
        data = request.get_json()
        if not data:
            return {"message": "Fields cannot be empty"}, 400
        username = data.get('username')
        name = data.get('name')
        email = data.get('email')
        password = data.get('password')
        confirm_password = data.get('confirm_password')
        if not username or not name or not email or not password or not confirm_password:
            return dict(message=
                        "name, username, email, password or confirm_password fields missing"), 400
        v = Validate().validate_register(username, name, email, password, confirm_password)
        if "message" in v:
            return v, 400

        admin = request.args.get('admin')

        if UserModel.get_user_by_username(v["username"]):
            return {"message": "Add user failed. Username already exists"}, 409
        if UserModel.get_user_by_email(v["email"]):
            return {"message": "Add user failed. Email entered already exists"}, 409
        if admin is None:
            my_user = UserModel(username=v["username"], name=v["name"],
                                email=v["email"], password=v["password"])
            my_user.save()
            return {"message": "User successfully added"}, 201

        
        my_user = UserModel(username=v["username"], name=v["name"],
                                email=v["email"], password=v["password"], admin=True)
        my_user.save()
        return {"message": "Admin user successfully added"}, 201
	def insert(self, data = {}):
		'''add record'''
		#check and validate data
		validate = Validate()
		validate._validate(data, self._dataValidate)

		fields = ''
		values = ''
		for key,value in data.items():
			fields = fields + "`" + key + "`, "
			values = values + "'" + value + "', "
		fields = fields[:-2]
		values = values[:-2]
		sql = 'insert into '+ self._name +' (' + fields + ') values (' + values + ')'

		return self.db.execute_lastrowid(sql)
	def save(self, data = {}, id = 0):
		'''update record by primary'''
		#check and validate data
		validate = Validate()
		validate._validate(data, self._dataValidate)
		
		fields = ''
		for key,value in data.items():
			fields = fields + "`" + key + "` = '" + value + "', "
		fields = fields[:-2]
		if id == 0:
			where = ' limit 1'
		else:
			where = ' where ' + self._primary + " ='" + str(id) + "'"
		sql = 'update '+ self._name +' set ' + fields + where

		return self.db.execute_rowcount(sql)
Exemplo n.º 13
0
    def post(self, race_id):
        data = Validate(self.request.body).new_race_user()

        race_id = ObjectId(race_id)

        if data.has_key('friend_id'):
            friend_id = ObjectId(data['friend_id'])
        else:
            friend_id = None

        if (not self.race_user_check(self.user_id, race_id) and 
           (self.friend_check(self.user_id, friend_id) or 
           self.race_public_check(race_id))):
            self.db.insert('race_user', dict(race_id=race_id,
                                             user_id=self.user_id))
        else:
            raise tornado.web.HTTPError(403)
Exemplo n.º 14
0
    def read(self):
        rule = self.rule

        # 验证报文数据
        Validate().analysis(sign=rule['sign'])

        Common.im218_is_out_duty = False
        Common.pwm_is_run = False
	def update(self, data = {}, where = "", flag = 0):
		'''update record'''
		#check and validate data
		validate = Validate()
		validate._validate(data, self._dataValidate)

		fields = ''
		for key,value in data.items():
			fields = fields + "`" + key + "` = '" + value + "', "
		fields = fields[:-2]
		if not where:
			where = ' where 1 = 1'
		else:
			where = ' where ' + where
		if flag == 0:
			limit = ' limit 200'
		else:
			limit = ''
		sql = 'update '+ self._name +' set ' + fields + where + limit

		return self.db.execute_rowcount(sql)
Exemplo n.º 16
0
def check():
    '''
    定时检测数据库中代理的可用性
    :return:
    '''
    while True:
        m = MongoDB()
        count = m.get_count()
        if not count == 0:
            logging.info('开始检测数据库中代理可用性>>>>>>>>')
            proxies = m.get(count)
            Validate().valid_many(proxies, 'check')
        time.sleep(10 * 60)
Exemplo n.º 17
0
def save_arrangement():
    arrangement = request.json

    if Validate.validate_arrangment(arrangement):
        arrangement_obj.pass_json(arrangement)
        data = arrangement_obj.build()
        arrangement_exists = arrangementsMDB.check_arrangement_exists(data)
        if arrangement_exists:
            arrangementsMDB.replace_arrangement(data)
        else:
            arrangementsMDB.add_arrangement(data)
        return dumps(data)
    else:
        return jsonify({'message':'json is not validate'})
Exemplo n.º 18
0
 def cadastrar(self, usuarios):
     print("Informe seus dados abaixo para fazer o cadastro!\n")
     self.typeuser = int(input("Tipo de usuário:\n<<1>> Funcionário\n<<2>> Cliente\n1 ou 2: "))
     self.username = input("\nNome (somente o primeiro nome): ")
     self.cpf = input("\nCPF (somente números): ")
     self.telefone = input("\nTelefone (DDD + número de telefone): ")
     self.email = input("\nE-mail (ex: [email protected]): ")
     self.senha = input("\nSenha: ")
     self.accept = int(input("\nVocê concorda com todos os Termos de Uso da aplicação?\n<<1>> Sim\n<<2>> Não\n1 ou 2: "))
     
     while(Validate.valide_typeuser(self, self.typeuser) != True):       
         self.typeuser = int(input("Tipo de usuário:\n<<1>> Funcionário\n<<2>> Cliente\n1 ou 2: "))
     while(Validate.valide_name(self, self.username) != True):       
         self.username = input("\nNome (somente o primeiro nome): ")
     while(Validate.valide_cpf(self, self.cpf) != True):
         self.cpf = input("\nCPF (somente números): ")
     while(Validate.valide_phone(self, self.telefone) != True):
         self.telefone = input("\nTelefone (DDD + número de telefone): ")
     while(Validate.valide_email(self, self.email) != True):
         self.email = input("\nE-mail (ex: [email protected]): ")
     while(Validate.valide_password(self, self.senha) != True):
         self.senha = input("\nSenha: ")
     while(Validate.valide_accept(self, self.accept) != True):
         self.accept = int(input("\nVocê concorda com todos os Termos de Uso da aplicação?\n<<1>> Sim\n<<2>> Não\n1 ou 2: "))
     
     if(Validate.valide_typeuser(self, self.typeuser) == True and Validate.valide_name(self, self.username) and Validate.valide_cpf(self, self.cpf) == True and Validate.valide_phone(self, self.telefone) == True and Validate.valide_email(self, self.email) == True and Validate.valide_password(self, self.senha) == True and Validate.valide_accept(self, self.accept) == True):
         print("Seu cadastro foi concluído com sucesso!")
         usuarios['typeuser'] = self.typeuser
         usuarios['name'] = self.username 
         usuarios['cpf'] = self.cpf 
         usuarios['phone'] = self.telefone
         usuarios['email'] = self.email
         usuarios['password'] = self.senha
         usuarios['agreement'] = self.accept
         with open("user.json", "a+", encoding="utf8") as json_file:
           json.dump(usuarios, json_file)
         time.sleep(2)
Exemplo n.º 19
0
    def put(self):
        '''Change a password'''
        data = request.get_json()

        claims = get_jwt_claims()
        reset = claims["reset_password"].encode('ascii')
        if reset != "false":
            '''if reset password is not false call change_reset_password() helper method'''
            return change_reset_password(reset, data)

        if not data:
            return dict(message="Fields cannot be empty"), 400

        old_password = data.get("old_password")
        new_password = data.get("new_password")
        confirm_password = data.get("confirm_password")

        if old_password is None or new_password is None:
            return dict(
                message="old_password or new_password fields missing"), 400
        my_list = [old_password, new_password, confirm_password]
        for i in my_list:
            i = i.strip()
            if not i:
                return dict(message="enter valid data"), 400
        password = Validate().validate_password(new_password, confirm_password)
        if "message" in password:
            return password

        user_identity = get_jwt_identity()
        user = UserModel.get_user_by_username(user_identity)
        if check_password_hash(user.password, old_password) is True:
            user.password = generate_password_hash(new_password)
            user.last_change_password = datetime.now()
            user.save()

            return dict(message="password changed successfully"), 200
        return dict(message="Incorrect password"), 401
Exemplo n.º 20
0
def signup():
    if request.method == 'POST':
        username = request.form["username"]
        password = request.form["passwd"]
        confirm_password = request.form["confirmPasswd"]

        validate = Validate()
        validate.set_username_validators(min_length=4, max_length=12)

        username_check = validate.validate_username(username.strip())
        password_check = validate.validate_password(password, confirm_password)

        if username_check == 'PASS' and password_check == 'PASS':
            # Insert into database 
            global conn
            params = (username, password)
            conn.execute("INSERT INTO user (username, password) VALUES (?, ?)", params)
            conn.commit()

            session.permanent = True
            session['user'] = username
            return redirect(url_for('login', user=username))
        elif username_check == 'USERNAME_NULL':
            message = "username field is left blank"
        elif username_check == 'USERNAME_LENGTH_VIOLATED':
            message = "username length should be of min 4 and max 12 characters"
        elif username_check == 'USERNAME_VIOLATED':
            message = "username can consist of alphanumeric characters and cannot start with a digit"
        elif password_check == 'PASSWD_UNMATCH':
            message = "passwords do not match"
        elif password_check == 'PASSWD_WEAK':
            message = "weak password"
        else: 
            message = "some error occurred"

        return render_template('register.html', message=message)

    else:
        if user_authenticated(): 
            return render_template('force_logout.html')

    return render_template('register.html', message='')
Exemplo n.º 21
0
 def testAllowed(self):
     val = Validate(self.post)
     # Values 'a' or 'b' are allowed
     val.allowed('check', ['a', 'b'])
     # Value 'd' is not allowed
     self.assertRaises(HTTPPreconditionFailed, val.allowed, 'check', ['d'])
Exemplo n.º 22
0
 def test_is_valid_dir_name(self):
     with captured_output() as (out, err):
         Validate.is_valid_dir_name('Test-Dir')
         output = out.getvalue().strip()
         self.assertEqual(output, '')
Exemplo n.º 23
0
def validate():
    result = Validate.validate()
    return jsonify(result)
Exemplo n.º 24
0
def validate():
    validate = Validate()
    print "-----------------start to reflesh--------------------------"
    validate.reflesh()
    print "------------------start to update-------------------------"
    validate.ping()
Exemplo n.º 25
0
    def alterarDados(self):

      with open("user.json") as json_file:
        user = json.load(json_file)
        email = user.get("email")
        senha = user.get("password")
        nome = user.get("name")
        tipo = user.get("typeuser")
        cpf = user.get("cpf")
        telefone = user.get("phone")
        accept = user.get("agreement")

      dado = int(input("Informe o dado que deseja alterar:\n<<1>> Nome\n<<2>> CPF\n<<3>> Telefone\n<<4>> E-mail\n<<5>> Senha\nRESPOSTA: "))

      if(dado == 1):
        nome_anterior = input("Digite o nome salvo anteriormente: ")
        if(nome_anterior == nome):
          self.username = input("\nNome (somente o primeiro nome): ")
          while(Validate.valide_name(self, self.username) != True):       
            self.username = input("\nNome (somente o primeiro nome): ")
          nome = self.username
      if(dado == 2):
        cpf_anterior = input("Digite o CPF salvo anteriormente: ")
        if(cpf_anterior == cpf):
          self.cpf = input("\nCPF (somente números): ")
          while(Validate.valide_cpf(self, self.cpf) != True):
              self.cpf = input("\nCPF (somente números): ")
          cpf = self.cpf
      if(dado == 3):
        telefone_anterior = input("Digite o telefone salvo anteriormente: ")
        if(telefone_anterior == telefone):
          self.telefone = input("\nTelefone (DDD + número de telefone): ")
          while(Validate.valide_phone(self, self.telefone) != True):
            self.telefone = input("\nTelefone (DDD + número de telefone): ")
          telefone = self.telefone
      if(dado == 4):
        email_anterior = input("Digite o email salvo anteriormente: ")
        if(email_anterior == email):
          self.email = input("\nE-mail (ex: [email protected]): ")
          while(Validate.valide_email(self, self.email) != True):
            self.email = input("\nE-mail (ex: [email protected]): ")
          email = self.email
      if(dado == 5):
        senha_anterior = input("Digite a senha salva anteriormente: ")
        if(senha_anterior == senha):
          self.username = input("\nNome (somente o primeiro nome): ")
          self.senha = input("\nSenha: ")
          while(Validate.valide_password(self, self.senha) != True):
            self.senha = input("\nSenha: ")
          senha = self.senha
      print("Alteração feita com sucesso!")
      usuarios = {
        'typeuser':tipo,
        'name':nome,
        'cpf':cpf,
        'phone':telefone,
        'email':email,
        'password':senha,
        'agreement':accept
      }
      with open("user.json", "w", encoding="utf8") as json_file:
        json.dump(usuarios, json_file)
      time.sleep(2)
Exemplo n.º 26
0
    def validate(self,request):
        request = self.resolveAliases(request)
        s = self._schema
        skeys = set([ x for x in s.keys() if x[0] != '_' ])
        rkeys = set(request.keys())
        cloning = {}
        # if request has keywords which are not in the schema, we
        # shutdown straight away
        if not skeys.issuperset(rkeys):
            raise ValueError('Unknown keyword(s) in %s: %s' % (self._name,', '.join(rkeys.difference(skeys))))

        # if the request does not have keywords which are required, we also
        # shutdown straight away (they could have a default value though
        # in that case we assign them).
        required = []
        for key in skeys.difference(rkeys):
            if s[key]['required']:
                required.append(key)

        excludes = []
        for key in s:
            if 'excludes' in s[key]:
                excluded = set()
                excluded.add(key)
                for ex in s[key]['excludes']:
                    excluded.add(ex)
                excludes.append(excluded)
            if 'cloneinto' in s[key]:
                targets = []
                for target in s[key]['cloneinto']:
                    if not target in request:
                        targets.append(target)
                cloning[key] = targets
        allexcluded = set()
        for group in excludes:
            allexcluded = allexcluded.union(group)

        if len(required) > 0:
            remaining = []
            for key in required:
                if 'default' in s[key]:
                    request[key] = s[key]['default']
                elif not key in allexcluded:
                    remaining.append(key)
            if len(remaining) > 0:
                raise ValueError('Required keyword(s) are missing in %s: %s' % (self._name,', '.join(remaining)))

        for group in excludes:
            v,r = self.find_excluded(request,group,s)
            if len(v) == 0:
                if len(r) > 0:
                    raise ValueError('In "%s", at least one of "%s" is required' % (self._name,', '.join(group)))
            elif len(v) > 1:
                    raise ValueError('In "%s", only one of "%s" is required, your specifed "%s"' % (self._name,', '.join(group), ', '.join(v)))

        # deal with lists and non-lists
        # also expand sub-schemas
        for k,i in request.items():
            if not s[k]['uncountable']:
                if s[k]['unique']:
                    if is_sequence_and_not_string(i) and len(i) > 1:
                        raise ValueError('In %s, the keyword "%s" allows one value, not a list' % (self._name,k))
                    request[k] = no_list(i)
                else:
                    request[k] = make_list(i)
                    #if len(request[k]) == 0 and s[k]['required']:
                    #    raise ValueError('In %s, the keyword "%s" cannot be an empty list' % (self._name,k))
            sub_schema = self.hasSchema(k,s[k],request)
            if sub_schema is not None:
                if s[k]['unique']:
                    lang = Language(sub_schema,schemapath=self._schemapath)
                    request[k] = lang.validate(i)
                else:
                    i = make_list(i)
                    for index,entry in enumerate(i):
                        lang = Language(sub_schema,schemapath=self._schemapath)
                        i[index] = lang.validate(entry)

        # now validate the values in the request if we can
        for k,i in request.items():
            if "type" in s[k]:
                request[k] = self.createType(s[k]["type"],request[k])
            if 'validate' in s[k]:
                assert(Validate.isRegistered(s[k]['validate'][0]))
                f = Validate.create(s[k]['validate'][0],*s[k]['validate'][1:])
                request[k] = f(self._schemaNames,k,request[k],request)

        # as a last thing we clone whatever needs to be cloned
        for clone, targets in cloning.items():
            if clone in request:
                for target in targets:
                    if not s[clone]['unique'] and s[target]['unique']:
                        request[target] = request[clone][0]
                    else:
                        request[target] = request[clone]

        return request
Exemplo n.º 27
0
        validate.errorMessages.strip(),
        time.time() - start))
    validate.Close()


# In[ ]:
if __name__ == '__main__':
    Header()
    args = ParseCmdLine()
    if args.selectedvalidations == None:
        selectedvalidations = '1,2,3,4,5,6,7'
    else:
        selectedvalidations = args.selectedvalidations
    activevalidations = map(int, selectedvalidations.split(','))
    if (args.csvresults != None):
        Validate.CreateCsv(5, args.csvresults, activevalidations)
    path, filemask = os.path.split(args.inputfname)
    files = sorted(Validate.FindFiles(path, filemask))
    if len(files) == 0:
        print('There' 's no file to process.')
        sys.exit(1)
    verystart = time.time()
    print('Processing {0} files.'.format(len(files)))
    threads = args.processorcores
    jobs = []
    i = 0

    while i < (len(files) / threads * threads):
        if threads == 1:
            ProcessFile(files[i], args.lasversion, args.minimumpointsdensity,
                        args.displayheader, args.cellsize,
#
#   bikeshare.py - program to interactively explore bike share data across
#   three U.S. cities.
#

from csv_data import CsvData
from data_stats import DataStats
from validate import Validate
from pretty_print import PrettyPrint
import bikeshare_functions as bike_funs

# Init all objects
print("Initializing program. Please wait.\n")
bikeshare_data = CsvData()
bikeshare_stats = DataStats(bikeshare_data.get_data())
validator = Validate()
pprint = PrettyPrint()

# Main loop
city_names = bikeshare_data.get_city_names()

# Intro
print("Hello. Let's explore some bike share data.")

while True:
    # Get filter options
    filter_options = bike_funs.get_filter_options(validator, city_names)

    # Calculate stats
    all_stats = bike_funs.calculate_stats(bikeshare_stats, filter_options)
Exemplo n.º 29
0
 def testOnly(self):
     val = Validate(self.post)
     # Only these parameters are acceptable
     val.only(['check', 'foo', 'name'])
     # 'name' is not acceptable
     self.assertRaises(HTTPPreconditionFailed, val.only, ['check', 'foo', 'last'])
Exemplo n.º 30
0
    def battle(self):
        self.max_health(self.trainer.pokemon_party)
        if len(self.trainer.pokemon_party) == 0:
            print("You have no pokemon")
        else:
            current_pokemon = 0
            enemy_pokemon = 0
            cp = 0
            ep = 0

            print("\033c")
            print(f'\n{self.enemy.name} wants to battle')
            # Check trainer and enemy still have pokemon to send out
            while self.party_status(
                    self.trainer.pokemon_party) > 0 and self.party_status(
                        self.enemy.pokemon_party) > 0:
                if current_pokemon > cp:
                    print(
                        f'{self.trainer.name} sent out {self.trainer.pokemon_party[current_pokemon].name}!\n'
                    )
                    cp += 1
                elif enemy_pokemon > ep:
                    print(
                        f'{self.enemy.name} sent out {self.enemy.pokemon_party[enemy_pokemon].name}!\n'
                    )
                    ep += 1
                else:
                    print(
                        f'{self.enemy.name} sent out {self.enemy.pokemon_party[enemy_pokemon].name}!\n'
                    )
                    print(
                        f'{self.trainer.name} sent out {self.trainer.pokemon_party[current_pokemon].name}!\n'
                    )
                # Check current pokemon is not fainted
                while self.trainer.pokemon_party[
                        current_pokemon].health > 0 and self.enemy.pokemon_party[
                            enemy_pokemon].health > 0:
                    # Pokemon attack options for user
                    t_party = self.trainer.pokemon_party
                    e_party = self.enemy.pokemon_party
                    count = 0
                    while len(t_party[current_pokemon].attacks) > count:
                        print(
                            f'{count + 1}. {t_party[current_pokemon].attacks[count].name}'
                        )
                        count += 1
                    user_input = Validate().range(
                        1, len(t_party[current_pokemon].attacks),
                        "Please choose a valid option",
                        "What would you like to do? ")
                    e_party[enemy_pokemon].health -= t_party[
                        current_pokemon].attacks[user_input - 1].damage
                    print("\033c")
                    print(
                        f'\n{t_party[current_pokemon].name} used {t_party[current_pokemon].attacks[user_input - 1].name}, the enemy {e_party[enemy_pokemon].name} now has {e_party[enemy_pokemon].health} HP'
                    )
                    # User attacks first, and if enemy pokemon is still alive, they will return with an attack
                    if self.alive(self.enemy.pokemon_party[enemy_pokemon]):
                        t_party[current_pokemon].health -= e_party[
                            current_pokemon].attacks[0].damage
                        print(
                            f'The enemy {e_party[enemy_pokemon].name} used {e_party[enemy_pokemon].attacks[0].name}, {t_party[current_pokemon].name} has {t_party[current_pokemon].health} HP\n'
                        )
                # Check to see if pokemon fainted after attack sequence, change pokemon at the start of the loop
                if self.alive(self.trainer.pokemon_party[current_pokemon]):
                    print(
                        f'Enemy {self.enemy.pokemon_party[enemy_pokemon].name} fainted!\n'
                    )
                    enemy_pokemon += 1
                else:
                    print(
                        f'{self.trainer.pokemon_party[current_pokemon].name} fainted\n'
                    )
                    current_pokemon += 1
        # After healthy pokemon check finishes, check to see whose pokemon party is still alive
        if self.party_status(self.trainer.pokemon_party):
            print(f'You defeated {self.enemy.name}!')
            input("Press enter to continue")
        else:
            print(f'{self.enemy.name} defeated you!')
            input("Press enter to continue")
Exemplo n.º 31
0
 def testRequired(self):
     val = Validate(self.post)
     # These parameters are required
     val.required(['check', 'foo', 'name'])
     # 'last' is missing, so we fail the required check
     self.assertRaises(HTTPPreconditionFailed, val.required, ['check', 'foo', 'last'])
Exemplo n.º 32
0
    def validate(self,request):
        request = self.resolveAliases(request)
        s = self._schema
        skeys = set([ x for x in s.keys() if x[0] != '_' ])
        rkeys = set(request.keys())
        cloning = {}
        # if request has keywords which are not in the schema, we
        # shutdown straight away
        if not skeys.issuperset(rkeys):
            raise ValueError('Unknown keyword(s) in %s: %s' % (self._name,', '.join(rkeys.difference(skeys))))

        # if the request does not have keywords which are required, we also
        # shutdown straight away (they could have a default value though
        # in that case we assign them).
        required = []
        for key in skeys.difference(rkeys):
            if s[key]['required']:
                required.append(key)

        excludes = []
        for key in s:
            if 'excludes' in s[key]:
                excluded = set()
                excluded.add(key)
                for ex in s[key]['excludes']:
                    excluded.add(ex)
                excludes.append(excluded)
            if 'cloneinto' in s[key]:
                targets = []
                for target in s[key]['cloneinto']:
                    if not target in request:
                        targets.append(target)
                cloning[key] = targets
        allexcluded = set()
        for group in excludes:
            allexcluded = allexcluded.union(group)

        if len(required) > 0:
            remaining = []
            for key in required:
                if 'default' in s[key]:
                    request[key] = s[key]['default']
                elif not key in allexcluded:
                    remaining.append(key)
            if len(remaining) > 0:
                raise ValueError('Required keyword(s) are missing in %s: %s' % (self._name,', '.join(remaining)))

        for group in excludes:
            v,r = self.find_excluded(request,group,s)
            if len(v) == 0:
                if len(r) > 0:
                    raise ValueError('In "%s", at least one of "%s" is required' % (self._name,', '.join(group)))
            elif len(v) > 1:
                    raise ValueError('In "%s", only one of "%s" is required, your specifed "%s"' % (self._name,', '.join(group), ', '.join(v)))

        # deal with lists and non-lists
        # also expand sub-schemas
        for k,i in request.items():
            if not s[k]['uncountable']:
                if s[k]['unique']:
                    if is_sequence_and_not_string(i) and len(i) > 1:
                        raise ValueError('In %s, the keyword "%s" allows one value, not a list' % (self._name,k))
                    request[k] = no_list(i)
                else:
                    request[k] = make_list(i)
                    #if len(request[k]) == 0 and s[k]['required']:
                    #    raise ValueError('In %s, the keyword "%s" cannot be an empty list' % (self._name,k))
            sub_schema = self.hasSchema(k,s[k],request)
            if sub_schema is not None:
                if s[k]['unique']:
                    lang = Language(sub_schema,schemapath=self._schemapath)
                    request[k] = lang.validate(i)
                else:
                    i = make_list(i)
                    for index,entry in enumerate(i):
                        lang = Language(sub_schema,schemapath=self._schemapath)
                        i[index] = lang.validate(entry)

        # now validate the values in the request if we can
        for k,i in request.items():
            if "type" in s[k]:
                request[k] = self.createType(s[k]["type"],request[k])
            if 'validate' in s[k]:
                assert(Validate.isRegistered(s[k]['validate'][0]))
                f = Validate.create(s[k]['validate'][0],*s[k]['validate'][1:])
                request[k] = f(self._schemaNames,k,request[k],request)

        # as a last thing we clone whatever needs to be cloned
        for clone, targets in cloning.items():
            if clone in request:
                for target in targets:
                    if not s[clone]['unique'] and s[target]['unique']:
                        request[target] = request[clone][0]
                    else:
                        request[target] = request[clone]

        return request
Exemplo n.º 33
0
def test_not_valid_arrangement():
    with app.app_context():
        assert Validate.validate_payload(mock_bad_arrangement()) == False
Exemplo n.º 34
0
def test_validate_arrangement_owner():
    with app.app_context():
        assert Validate.validate_payload(mock_arrangement_owner())
Exemplo n.º 35
0
def main():
    results = {}
    results['mode'] = config['mode']
    results['estimator'] = config['estimator_type']
    start = time.time()
    now = datetime.datetime.now()
    ts = '{}-{}-{}-{}-{}'.format(now.year, now.month, now.day, now.hour,
                                 now.minute)
    print(ts)

    criterion = losses.photometric_reconstruction_loss()
    exp_loss = losses.explainability_loss()
    Reconstructor = stn.Reconstructor().to(device)
    loss = losses.Compute_Loss(Reconstructor,
                               criterion,
                               exp_loss,
                               exp_weight=config['exp_weight'])
    model = mono_model_joint.joint_model(
        num_img_channels=(6 + 2 * config['use_flow']),
        output_exp=args.exploss,
        dropout_prob=config['dropout_prob'],
        mode=args.mode).to(device)

    params = list(model.parameters())
    optimizer = torch.optim.Adam(params,
                                 lr=config['lr'],
                                 weight_decay=config['wd'])  #, amsgrad=True)

    cudnn.benchmark = True

    est_traj_stacked = {}
    corr_pose_change_vecs_stacked = {}
    corr_stacked = {}
    losses_stacked = {}
    best_val_loss, best_rot_seg_err, best_trans_err, most_loop_closure = {}, {}, {}, {}
    best_rot_acc_epoch, best_trans_acc_epoch, best_loss_epoch, most_loop_closure_epoch = {}, {}, {}, {}
    for key, dset in eval_dsets.items():
        est_traj_stacked[key] = np.empty(
            (0, eval_dsets[key].dataset.raw_gt_trials[0].shape[0], 4, 4))
        losses_stacked[key] = np.empty(
            (0, eval_dsets[key].dataset.raw_gt_trials[0].shape[0] - 1))
        corr_pose_change_vecs_stacked[key] = np.empty(
            (0, eval_dsets[key].dataset.raw_gt_trials[0].shape[0] - 1, 6))
        corr_stacked[key] = np.copy(corr_pose_change_vecs_stacked[key])
        best_val_loss[key], best_rot_seg_err[key], best_trans_err[
            key], most_loop_closure[key] = 1e5, 1e5, 1e5, 0

    for epoch in range(0, config['num_epochs']):
        optimizer = exp_lr_scheduler(
            model, optimizer, epoch, lr_decay_epoch=config['lr_decay_epoch']
        )  ## reduce learning rate as training progresses

        train_loss = Train_Mono(device, model, Reconstructor,
                                dset_loaders['train'], loss, optimizer, epoch)
        val_loss = Validate(device, model, Reconstructor, dset_loaders['val'],
                            loss)
        #
        if epoch == 0:
            writer = SummaryWriter(comment="-val_seq-{}-test_seq-{}".format(
                args.val_seq[0], args.test_seq[0]))
        writer.add_scalars('', {
            'train': train_loss,
            'val': val_loss
        }, epoch + 1)
        for key, dset in eval_dsets.items():
            print("{} Set, Epoch {}".format(key, epoch))

            ###plot images, depth map, explainability mask
            img_array, disparity, exp_mask = test_depth_and_reconstruction(
                device, model, Reconstructor, dset)
            img_array = plot_img_array(img_array)
            depth = plot_disp(disparity[-1])
            writer.add_image(key + '/imgs', img_array, epoch + 1)
            writer.add_image(key + '/depth', depth, epoch + 1)

            if args.exploss:
                exp_mask = plot_img_array(exp_mask)
                writer.add_image(key + '/exp_mask', exp_mask, epoch + 1)

            ###plot trajectories
            corr, gt_corr, corr_pose_change_vec, odom_pose_change_vec, gt_pose_change_vec, corr_traj, corr_traj_rot, est_traj, gt_traj, \
                rot_seg_err, trans_err, cum_dist = test_trajectory(device, model, Reconstructor, dset, epoch)

            corrections = plot_6_by_1(corr, title='Corrections')
            correction_errors = plot_6_by_1(np.abs(corr - gt_corr),
                                            title='6x1 Errors')
            est_traj_img = plot_multi_traj(est_traj, 'Odom.', gt_traj, 'GT',
                                           key + ' Set')
            corr_traj_img = plot_multi_traj(corr_traj, 'corr.', gt_traj, 'GT',
                                            key + ' Set')
            corr_traj_rot_img = plot_multi_traj(corr_traj_rot, 'corr.',
                                                gt_traj, 'GT', key + ' Set')

            writer.add_image(key + '/corr_traj', corr_traj_img, epoch + 1)
            writer.add_image(key + '/corr_traj (Rot. only)', corr_traj_rot_img,
                             epoch + 1)
            writer.add_image(key + '/est_traj', est_traj_img, epoch + 1)
            writer.add_image(key + '/correction_errors', correction_errors,
                             epoch + 1)

            corr_stacked[key] = np.vstack(
                (corr_stacked[key], corr.reshape((1, -1, 6))))
            est_traj_stacked[key] = np.vstack(
                (est_traj_stacked[key], corr_traj_rot.reshape((1, -1, 4, 4))))
            #                losses_stacked = np.vstack((losses_stacked, loss_per_img_pair.reshape((1,-1))))
            corr_pose_change_vecs_stacked[key] = np.vstack(
                (corr_pose_change_vecs_stacked[key],
                 corr_pose_change_vec.reshape((1, -1, 6))))

            results[key] = {
                'val_seq': args.val_seq,
                'test_seq': args.test_seq,
                'epochs': epoch + 1,
                'est_traj': est_traj_stacked[key],
                'corrections': corr_stacked[key],
                'est_traj_reconstruction_loss': losses_stacked[key],
                'corr_pose_vecs': corr_pose_change_vecs_stacked[key],
                'odom_pose_vecs': odom_pose_change_vec,
                'gt_traj': gt_traj,
            }

            if args.save_results and config['mode'] == 'online':
                os.makedirs('results/online-mode/{}'.format(config['date']),
                            exist_ok=True)
                if config['estimator_type'] == 'mono':
                    _, num_loop_closure, _ = find_loop_closures(
                        corr_traj, cum_dist)
                if config['estimator_type'] == 'stereo':
                    _, num_loop_closure, _ = find_loop_closures(
                        corr_traj_rot, cum_dist)
                print("{} Loop Closures detected".format(num_loop_closure))
                ##Save the best models

                if val_loss < best_val_loss[key]:
                    best_val_loss[key] = val_loss
                    best_loss_epoch[key] = epoch
                    state_dict_loss = model.state_dict()
                    print("Lowest validation loss (saving model)")
                    if key == 'val':
                        torch.save(
                            state_dict_loss,
                            'results/online-mode/{}/{}-best-loss-val_seq-{}-test_seq-{}.pth'
                            .format(config['date'], ts, args.val_seq[0],
                                    args.test_seq[0]))
                if rot_seg_err < best_rot_seg_err[key]:
                    best_rot_seg_err[key] = rot_seg_err
                    best_rot_acc_epoch[key] = epoch
                    state_dict_acc = model.state_dict()
                    print("Lowest error (saving model)")
                    if key == 'val':
                        torch.save(
                            state_dict_acc,
                            'results/online-mode/{}/{}-best_rot_acc-val_seq-{}-test_seq-{}.pth'
                            .format(config['date'], ts, args.val_seq[0],
                                    args.test_seq[0]))
                if trans_err < best_trans_err[key]:
                    best_trans_err[key] = trans_err
                    best_trans_acc_epoch[key] = epoch
                    state_dict_trans = model.state_dict()
                    print("Lowest position error (saving model)")
                    if key == 'val':
                        torch.save(
                            state_dict_trans,
                            'results/online-mode/{}/{}-best_trans_acc-val_seq-{}-test_seq-{}.pth'
                            .format(config['date'], ts, args.val_seq[0],
                                    args.test_seq[0]))

                if num_loop_closure >= most_loop_closure[key]:
                    most_loop_closure[key] = num_loop_closure
                    most_loop_closure_epoch[key] = epoch
                    state_dict_loop_closure = model.state_dict()
                    print("Most Loop Closures detected ({})".format(
                        most_loop_closure[key]))
                    if key == 'val':
                        torch.save(
                            state_dict_loop_closure,
                            'results/online-mode/{}/{}-most_loop_closures-val_seq-{}-test_seq-{}.pth'
                            .format(config['date'], ts, args.val_seq[0],
                                    args.test_seq[0]))
                results[key]['best_rot_acc_epoch'] = best_rot_acc_epoch[key]
                results[key]['best_trans_acc_epoch'] = best_trans_acc_epoch[
                    key]
                results[key]['best_loss_epoch'] = best_loss_epoch[key]
                results[key][
                    'best_loop_closure_epoch'] = most_loop_closure_epoch[key]
                sio.savemat(
                    'results/online-mode/{}/{}-results-val_seq-{}-test_seq-{}'.
                    format(config['date'], ts, args.val_seq[0],
                           args.test_seq[0]), results)
                f = open(
                    "results/online-mode/{}/{}-config.txt".format(
                        config['date'], ts), "w")
                f.write(str(config))
                f.close()

    if config['mode'] == 'offline':
        os.makedirs('results/offline-mode/{}'.format(config['date']),
                    exist_ok=True)
        sio.savemat(
            'results/offline-mode/{}/{}-results-val_seq-{}-test_seq-{}'.format(
                config['date'], ts, args.val_seq[0], args.test_seq[0]),
            results)
        torch.save(
            model.state_dict(),
            'results/offline-mode/{}/{}-val_seq-{}-test_seq-{}.pth'.format(
                config['date'], ts, args.val_seq[0], args.test_seq[0]))

    duration = timeSince(start)
    print("Training complete (duration: {})".format(duration))
Exemplo n.º 36
0
from validate import Validate

if __name__ == '__main__':

    Validate().detect_all()
Exemplo n.º 37
0
def validate():
    validate = Validate()
    print "-----------------start to reflesh--------------------------"
    validate.reflesh()
    print "------------------start to update-------------------------"
    validate.ping()
Exemplo n.º 38
0
                methods=['GET'])
app.add_url_rule('/gallery/',
                view_func=Gallery.as_view('gallery'),
                methods=['GET'])
app.add_url_rule('/login/',
                view_func=Login.as_view('login'),
                methods=['GET', 'POST'])
app.add_url_rule('/videos/',
                view_func=Videos.as_view('videos'),
                methods=['GET'])
app.add_url_rule('/tutorial/',
                view_func=Tutorial.as_view('tutorial'),
                methods=['GET'])
app.add_url_rule('/upload',
                view_func=Upload.as_view('upload'),
                methods=['GET'])
app.add_url_rule('/upload',
                view_func=Validate.as_view('validate'),
                methods=['GET'])
app.add_url_rule('/upload',
                view_func=Posted.as_view('posted'),
                methods=['GET'])


@app.errorhandler(404)
def page_not_found(error):
     return flask.render_template('404.html'), 404

app.debug = True
app.run()
Exemplo n.º 39
0
def ProcessFile(inputfname, lasversion, minimumpointsdensity, displayheader,
                cellsize, maxpercentcellsbelowdensity, activevalidations,
                deletefiles, validatefilespath, csvresults, verbose):
    Validate.version = lasversion
    Validate.minimumpointsdensity = minimumpointsdensity
    Validate.displayheader = displayheader
    Validate.cellsize = cellsize
    Validate.maxpercentcellsbelowdensity = maxpercentcellsbelowdensity
    Validate.validatefilespath = validatefilespath
    Validate.deletefiles = deletefiles
    Validate.csvresults = csvresults
    Validate.activevalidations = activevalidations
    Validate.verbose = verbose

    start = time.time()
    failCount = 0
    if (Validate.displayheader):
        lidarutils.displayInfo(inputfname)
    parameters = valParameters()
    try:
        validate = Validate(inputfname, parameters)
        if 1 in validate.activevalidations:
            failCount += validate.CheckLiDARFileSignature()

        if 2 in validate.activevalidations:
            failCount += validate.CheckFileVersion()

        if 3 in validate.activevalidations:
            failCount += validate.CheckNumberofReturns()

        if 4 in validate.activevalidations:
            failCount += validate.CheckReturnNumbers()

        if 5 in validate.activevalidations:
            failCount += validate.CheckMinMaxValues()

        if (6 in validate.activevalidations) or (
                7 in validate.activevalidations):
            validate.CreateShpFileLAS()
            catalogOk = validate.RunCatalog()
            if catalogOk:
                validate.CalcShapeFileArea()

        if 6 in validate.activevalidations:
            failCount += validate.CheckGlobalPointsDensity()

        if 7 in validate.activevalidations:
            if catalogOk:
                failCount += validate.CheckMaxCellsBelowDensity()

        if 8 in validate.activevalidations:
            failCount += validate.CheckXtYtZt()
    except:
        if 'validate' in locals():
            validate.TestFail('Exception: {0}'.format(traceback.format_exc()))
        else:
            print("Error {0} opening file {1}".format(traceback.format_exc(),
                                                      inputfname))
            os._exit(0)

        failcount += 1

    if Validate.verbose > 0:
        if failCount == 0:
            print('All validations passed successfully.')
        else:
            print('{0} validation(s) failed.'.format(failCount))
            print(validate.errorMessages)
    print('File: {0},'.format(inputfname)),
    print('{0} (elapsed time: {1:.2f}s)'.format(
        'ok' if failCount == 0 else 'failed: ' +
        validate.errorMessages.strip(),
        time.time() - start))
    validate.Close()
Exemplo n.º 40
0
    url = get_URL()
    profile = {}
    if (url != ''):
        profile = fs.scraping(url)
    else:
        profile["name"] = get_name()
    get_information(profile)
    view.clear()
    view.show_info(profile)
    finish = False
    t = Combinations(profile)
    info = t.info
    op = menu('')

    while (not finish):
        if (op == '1'):
            wg(info, profile)
            message = 'Your wordlist is ready, the file is wordlist.txt'
            op = menu(message)

        elif (op == '2'):
            Validate(info)
            op = menu('')

        else:
            try:
                os.remove("wordlist.txt")

            except Exception:
                finish = True