def domain_acquire(self, request): user = self.authenticate(request) validator = Validator(request) user_domain = validator.new_user_domain() device_mac_address = validator.device_mac_address() device_name = validator.string('device_name', required=True) device_title = validator.string('device_title', required=True) check_validator(validator) with self.create_storage() as storage: domain = storage.get_domain_by_name(user_domain) if domain and domain.user_id != user.id: raise servicesexceptions.parameter_error('user_domain', 'User domain name is already in use') update_token = util.create_token() if not domain: domain = Domain(user_domain, device_mac_address, device_name, device_title, update_token=update_token) domain.user = user storage.add(domain) else: domain.update_token = update_token domain.device_mac_address = device_mac_address domain.device_name = device_name domain.device_title = device_title return domain
def port_probe(self, request, request_ip): validator = Validator(request) token = validator.token() port = validator.port('port', True) protocol = validator.string('protocol', False) ip = validator.string('ip', False) check_validator(validator) domain = None with self.create_storage() as storage: domain = storage.get_domain_by_update_token(token) if not domain or not domain.user.active: raise servicesexceptions.bad_request('Unknown domain update token') try: if ip: request_ip = ip response = requests.get('{0}://{1}:{2}/ping'.format(protocol, request_ip, port), timeout=1, verify=False, allow_redirects=False) if response.status_code == 200: return {'message': response.text, 'device_ip': request_ip}, 200 # TODO: Remove after release 18.07 reaches wider adoption elif response.status_code == 301 and protocol == 'http': return {'message': "OK", 'device_ip': request_ip}, 200 except Exception, e: pass
def get_domain(self, request): validator = Validator(request) token = validator.token() with self.create_storage() as storage: domain = storage.get_domain_by_update_token(token) if not domain or not domain.user.active: raise servicesexceptions.bad_request('Unknown domain update token') return domain
def user_set_subscribed(self, request, user_email): validator = Validator(request) subscribed = validator.boolean('subscribed', required=True) check_validator(validator) with self.create_storage() as storage: user = storage.get_user_by_email(user_email) if not user: raise servicesexceptions.bad_request('Unknown user') user.unsubscribed = not subscribed
def user_log(self, request): validator = Validator(request) token = validator.token() data = validator.string('data') include_support = validator.boolean('include_support', False, True) with self.create_storage() as storage: user = storage.get_user_by_update_token(token) if not user: raise servicesexceptions.bad_request('Invalid update token') self.mail.send_logs(user.email, data, include_support)
def domain_update(self, request, request_ip=None): validator = Validator(request) token = validator.token() ip = validator.ip(request_ip) local_ip = validator.local_ip() map_local_address = validator.boolean('map_local_address', required=False) platform_version = validator.string('platform_version', required=False) web_protocol = validator.web_protocol(required=True) web_local_port = validator.port('web_local_port', required=True) web_port = validator.port('web_port', required=False) check_validator(validator) if map_local_address is None: map_local_address = False with self.create_storage() as storage: domain = storage.get_domain_by_update_token(token) if not domain or not domain.user.active: raise servicesexceptions.bad_request('Unknown domain update token') update_ip = (domain.map_local_address != map_local_address) or (domain.ip != ip) or (domain.local_ip != local_ip) domain.ip = ip domain.local_ip = local_ip domain.map_local_address = map_local_address domain.platform_version = platform_version domain.web_protocol = web_protocol domain.web_local_port = web_local_port domain.web_port = web_port if update_ip: self.dns.update_domain(self.main_domain, domain) domain.last_update = datetime.now() return domain
def authenticate(self, request): validator = Validator(request) email = validator.email() password = validator.password() check_validator(validator) user = self.get_user(email) if not user or not user.active or not util.hash(password) == user.password_hash: raise servicesexceptions.bad_request('Authentication failed') return user
def user_reset_password(self, request): validator = Validator(request) email = validator.email() check_validator(validator) with self.create_storage() as storage: user = storage.get_user_by_email(email) if user and user.active: action = user.enable_action(ActionType.PASSWORD) self.mail.send_reset_password(user.email, action.token)
def user_domain_delete(self, request, user): validator = Validator(request) user_domain = validator.user_domain() check_validator(validator) with self.create_storage() as storage: domain = storage.get_domain_by_name(user_domain) if not domain or domain.user.email != user.email: raise servicesexceptions.bad_request('Unknown domain') self.dns.delete_domain(self.main_domain, domain) storage.delete_domain(domain)
def authenticate(self, request): validator = Validator(request) email = validator.email() password = validator.password() errors = validator.errors if errors: message = ", ".join(errors) raise servicesexceptions.bad_request(message) user = self.get_user(email) if not user or not user.active or not util.hash(password) == user.password_hash: raise servicesexceptions.forbidden('Authentication failed') return user
def activate(self, request): validator = Validator(request) token = validator.token() check_validator(validator) with self.create_storage() as storage: user = storage.get_user_by_activate_token(token) if not user: raise servicesexceptions.bad_request('Invalid activation token') if user.active: raise servicesexceptions.bad_request('User is active already') user.active = True return True
def delete_user(self, request): validator = Validator(request) email = validator.email() password = validator.password() check_validator(validator) with self.create_storage() as storage: user = storage.get_user_by_email(email) if not user or not user.active or not util.hash(password) == user.password_hash: raise servicesexceptions.bad_request('Authentication failed') for domain in user.domains: self.dns.delete_domain(self.main_domain, domain) storage.delete_user(user)
def user_reset_password(self, request): validator = Validator(request) email = validator.email() errors = validator.errors if errors: message = ", ".join(errors) raise servicesexceptions.bad_request(message) with self.create_storage() as storage: user = storage.get_user_by_email(email) if user and user.active: action = user.enable_action(ActionType.PASSWORD) self.mail.send_reset_password(user.email, action.token)
def __init__(self, game_id=None, manager=None): ''' Initializes the instance variables.''' self.__super.__init__() self.transport = None # A function that accepts messages self.rep = None # The Representation to use self.game_id = game_id # A specific game to connect to self.game_opts= GameOptions() # Variant options for the current game self.closed = False # Whether the connection has ended, or should end self.map = None # The game board self.saved = {} # Positions saved from a SVE message self.use_map = False # Whether to initialize a Map; saves time for simple observers self.quit = True # Whether to close immediately when a game ends self.power = None self.manager = manager or ThreadManager() # Default name and version, for the NME message self.name = self.__class__.__name__ self.version = version_string() if self.options.validate: self.validator = Validator() else: self.validator = None # A list of variables to remember across SVE/LOD self.remember = ['map']
def addShip(self, coordinates, player: int): """ Adds a ship to the game for the specified player at the given coordinates. The rest of the board is unmodified If all the ships are places, subsequent calls to this function replace the ships that already exist :param coordinates: The coordinates of the ship squares in the form C1L1C2L2C3L3 :param player: The player that adds the ship :raises CoordinateInvalid: If the given string do not represent coordinates or the coordinates are out of the board or the ship intersects with another ship In case an exception is thrown, the board remains unmodified """ if len(coordinates) != 6: raise CoordinateInvalid() coordinate1 = coordinates[0:2] coordinate2 = coordinates[2:4] coordinate3 = coordinates[4:6] shipCoordinates = [coordinate1, coordinate2, coordinate3] Validator.validateCoordinate(coordinate1) Validator.validateCoordinate(coordinate2) Validator.validateCoordinate(coordinate3) Validator.validateShip(coordinate1, coordinate2, coordinate3) board = self.boardRepository.getBoard(player, 1) bonusShip = None if self.shipRepository.length(player) == 2: # remove ship to be replaced bonusShip = self.shipRepository.getShip( self.shipRepository.shipIndex[player], player) for coordinate in bonusShip.coordinates: board.markAtCoordinate(coordinate, ".") for coordinate in shipCoordinates: if board.getMark(coordinate) != ".": if bonusShip is not None: # put the bonus ship back for bonusShipCoordinates in bonusShip.coordinates: board.markAtCoordinate(bonusShipCoordinates, "+") raise CoordinateInvalid() for coordinate in shipCoordinates: board.markAtCoordinate(coordinate, "+") ship = Ship(coordinate1, coordinate2, coordinate3) if self.shipRepository.length(player) != 2: self.shipRepository.addShip(ship, player) else: self.shipRepository.putShip(ship, self.shipRepository.shipIndex[player], player)
class ALS_helper: def __init__(self,factors=10,train_df_path='train_df.csv',test_df_path='test_df.csv'): self.model = als(factors=factors) self.train_df = pd.read_csv(train_df_path) self.test_df = pd.read_csv(test_df_path) self.ratings_matrix = self.__get_matrix() self.user_items = self.ratings_matrix.T.tocsr() self.validator = Validator(test_df_path) os.environ["OPENBLAS_NUM_THREADS"] = '1' logger.info('The heplper is initialized succesfully') def train(self): logger.info('Model is training now') self.model.fit(self.ratings_matrix) logger.info('Model is trained') def __recommend(self): recom_list = self.model.recommend_all(self.user_items,filter_already_liked_items=True) return recom_list def validate(self): users = np.unique(self.test_df.userId.values) recom_list = self.__recommend() ndc1_als = [] ndc10_als = [] for user in tqdm(users): n1,n10 = self.validator.valid(user,recom_list[user]) ndc1_als.append(n1) ndc10_als.append(n10) return np.mean(ndc1_als),np.mean(ndc10_als) def __get_matrix(self): self.train_df['userId'] = self.train_df['userId'].astype('category') self.train_df['movieId'] = self.train_df['movieId'].astype('category') ratings_matrix = sp.coo_matrix( (self.train_df['rating'].astype(np.float32) , ( self.train_df['movieId'].cat.codes.copy(), self.train_df['userId'].cat.codes.copy() ) ) ) ratings_matrix = ratings_matrix.tocsr() return ratings_matrix
def user_set_password(self, request): validator = Validator(request) token = validator.token() password = validator.new_password() check_validator(validator) with self.create_storage() as storage: user = storage.get_user_by_token(ActionType.PASSWORD, token) if not user: raise servicesexceptions.bad_request('Invalid password token') user.password_hash = util.hash(password) self.mail.send_set_password(user.email) action = storage.get_action(token) storage.delete(action)
def save_message_db(): message_data = request.get_json(force=True) validation_message = Validator() message_text = message_data['text'] messengers = message_data['messangers'] datetime_object = validation_message.date(message_data['datetime']) if datetime_object is None: return 'incorrect time format' data = [] for messenger, phone in messengers.items(): phone = validation_message.phone(phone) if phone is None: return 'incorrect phone number' data.append((messenger, message_text, datetime_object, phone, False, 0)) sql = "insert into Messanger(messanger, text, datetime, phone, status,n) values (%s,%s,%s,%s,%s,%s)" cursor.executemany(sql, data) connection.commit() return 'ok'
def update_item(cid, iid): # from validation instance validate = Validator() selected_category, item, category_has_item = \ model.category_item(iid=iid, cid=cid) # boolean checks if user is authorized to access this page authorized = category_has_item and is_user()\ and item.user_id == login_session['id'] if not authorized: return redirect(url_for('login')) categories = model.all_categories() if request.method == 'POST': # get fields into variables iid, name, description, category_id = \ request.form.get('id'), \ request.form.get('name').strip(), \ request.form.get('description').strip(), \ request.form.get('category_id') # validate form validate.validate(field=name, field_name='Name') validate.validate(description, 'Description') validate.validate(category_id, "Category Name") # if form valid update if validate.valid_form(): item = model.update_item(item, name=name, description=description, cid=category_id) # set message shows updated done successfully msg = "%s Item Updated Successfully" % name flash(msg) return redirect( url_for("update_item", iid=item.id, cid=item.category_id)) return render_template("update_item.html", categories=categories, selected_category=selected_category, item=item, form_errors=validate.get_form_errors(), category_has_item=category_has_item, is_user=is_user(), login_session=login_session)
def create_new_user(self, request): validator = Validator(request) email = validator.email() password = validator.password() user_domain = validator.new_user_domain(error_if_missing=False) errors = validator.errors if errors: message = ", ".join(errors) raise servicesexceptions.bad_request(message) user = None action = None with self.create_storage() as storage: by_email = storage.get_user_by_email(email) if by_email and by_email.email == email: raise servicesexceptions.conflict('Email is already registered') if user_domain: by_domain = storage.get_domain_by_name(user_domain) if by_domain and by_domain.user_domain == user_domain: raise servicesexceptions.conflict('User domain name is already in use') update_token = util.create_token() user = User(email, util.hash(password), not self.activate_by_email) if user_domain: domain = Domain(user_domain, None, update_token) domain.user = user user.domains.append(domain) storage.add(domain) if self.activate_by_email: action = user.enable_action(ActionType.ACTIVATE) storage.add(user) if self.activate_by_email: self.mail.send_activate(self.main_domain, user.email, action.token) return user
def activate(self, request): validator = Validator(request) token = validator.token() errors = validator.errors if errors: message = ", ".join(errors) raise servicesexceptions.bad_request(message) with self.create_storage() as storage: user = storage.get_user_by_activate_token(token) if not user: raise servicesexceptions.bad_request('Invalid activation token') if user.active: raise servicesexceptions.conflict('User is active already') user.active = True return True
def domain_acquire(self, request): user = self.authenticate(request) validator = Validator(request) user_domain = validator.new_user_domain() with self.create_storage() as storage: domain = storage.get_domain_by_name(user_domain) if domain and domain.user_id != user.id: raise servicesexceptions.conflict('User domain name is already in use') update_token = util.create_token() if not domain: domain = Domain(user_domain, None, update_token) domain.user = user storage.add(domain) else: domain.update_token = update_token return domain
def test_service(): ''' This function tests the service ''' repository = Repository() validation = Validator() test_service = Service(repository,validation) len1 = len(test_service.repository.list) test_question = Question(1,"Largest number",1,2,3,3,"Easy") test_service.repository.add_question(test_question) assert len(test_service.repository.list) == len1 + 1
def domain_update(self, request, request_ip=None): validator = Validator(request) token = validator.token() ip = validator.ip(request_ip) errors = validator.errors if errors: message = ", ".join(errors) raise servicesexceptions.bad_request(message) with self.create_storage() as storage: domain = storage.get_domain_by_update_token(token) if not domain or not domain.user.active: raise servicesexceptions.bad_request('Unknown domain update token') map(self.validate_service, request['services']) request_services = [new_service_from_dict(s) for s in request['services']] added_services = self.get_missing(request_services, domain.services) removed_services = self.get_missing(domain.services, request_services) storage.delete(removed_services) for s in added_services: s.domain = domain domain.services.append(s) storage.add(added_services) is_new_dmain = domain.ip is None update_ip = domain.ip != ip domain.ip = ip if is_new_dmain: self.dns.new_domain(self.main_domain, domain) else: self.dns.update_domain(self.main_domain, domain, update_ip=update_ip, added=added_services, removed=removed_services) domain.last_update = datetime.now() return domain
def __init__(self): self.validator = Validator() self.db = Database() self.collection_name = 'todo' self.fields = { "title": "string", "body": "string", } self.create_required_fields = ["title", "body"] # Fields optional for CREATE self.create_optional_fields = [] # Fields required for UPDATE self.update_required_fields = [] # Fields optional for UPDATE self.update_optional_fields = []
def port_probe(self, request, request_ip): validator = Validator(request) token = validator.token() port = validator.port('port', True) protocol = validator.string('protocol', False) ip = validator.string('ip', False) check_validator(validator) domain = None with self.create_storage() as storage: domain = storage.get_domain_by_update_token(token) if not domain or not domain.user.active: raise servicesexceptions.bad_request('Unknown domain update token') try: if ip: request_ip = ip response = requests.get('{0}://{1}:{2}/ping'.format( protocol, request_ip, port), timeout=1, verify=False, allow_redirects=False) if response.status_code == 200: return {'message': response.text, 'device_ip': request_ip}, 200 # TODO: Remove after release 18.07 reaches wider adoption elif response.status_code == 301 and protocol == 'http': return {'message': "OK", 'device_ip': request_ip}, 200 except Exception, e: pass
def domain_acquire(self, request): user = self.authenticate(request) validator = Validator(request) user_domain = validator.new_user_domain() device_mac_address = validator.device_mac_address() device_name = validator.string('device_name', required=True) device_title = validator.string('device_title', required=True) check_validator(validator) with self.create_storage() as storage: domain = storage.get_domain_by_name(user_domain) if domain and domain.user_id != user.id: raise servicesexceptions.parameter_error( 'user_domain', 'User domain name is already in use') update_token = util.create_token() if not domain: domain = Domain(user_domain, device_mac_address, device_name, device_title, update_token=update_token) domain.user = user storage.add(domain) else: domain.update_token = update_token domain.device_mac_address = device_mac_address domain.device_name = device_name domain.device_title = device_title return domain
def user_set_password(self, request): validator = Validator(request) token = validator.token() password = validator.new_password() errors = validator.errors if errors: message = ", ".join(errors) raise servicesexceptions.bad_request(message) with self.create_storage() as storage: user = storage.get_user_by_token(ActionType.PASSWORD, token) if not user: raise servicesexceptions.forbidden('Invalid password token') user.password_hash = util.hash(password) self.mail.send_set_password(user.email) action = storage.get_action(token) storage.delete(action)
def drop_device(self, request): user = self.authenticate(request) validator = Validator(request) user_domain = validator.new_user_domain() check_validator(validator) with self.create_storage() as storage: domain = storage.get_domain_by_name(user_domain) if not domain or not domain.user.active: raise servicesexceptions.bad_request('Unknown domain') domain.update_token = None domain.device_mac_address = None domain.device_name = None domain.device_title = None domain.ip = None domain.local_ip = None self.dns.delete_domain(self.main_domain, domain) return domain
def domain_update(self, request, request_ip=None): validator = Validator(request) token = validator.token() ip = validator.ip(request_ip) local_ip = validator.local_ip() map_local_address = validator.boolean('map_local_address', required=False) platform_version = validator.string('platform_version', required=False) web_protocol = validator.web_protocol(required=True) web_local_port = validator.port('web_local_port', required=True) web_port = validator.port('web_port', required=False) check_validator(validator) if map_local_address is None: map_local_address = False with self.create_storage() as storage: domain = storage.get_domain_by_update_token(token) if not domain or not domain.user.active: raise servicesexceptions.bad_request( 'Unknown domain update token') update_ip = (domain.map_local_address != map_local_address) or ( domain.ip != ip) or (domain.local_ip != local_ip) domain.ip = ip domain.local_ip = local_ip domain.map_local_address = map_local_address domain.platform_version = platform_version domain.web_protocol = web_protocol domain.web_local_port = web_local_port domain.web_port = web_port if update_ip: self.dns.update_domain(self.main_domain, domain) domain.last_update = datetime.now() return domain
def main(): inputs = {'tes': 'evance', 'age': 18, 'experience': 2} rules = { 'name': 'req|bail', 'age': 'num|min:20', 'experience': 'num|min:3' } validator = Validator.make(inputs, rules) if not validator.fails(): print('Data validation passed!') else: for a, e in validator.errors(): print(a, e, end='\n')
def create_new_user(self, request): validator = Validator(request) email = validator.email() password = validator.new_password() check_validator(validator) user = None action = None with self.create_storage() as storage: by_email = storage.get_user_by_email(email) if by_email and by_email.email == email: raise servicesexceptions.parameter_error('email', 'Email is already registered') user = User(email, util.hash(password), not self.activate_by_email) if self.activate_by_email: action = user.enable_action(ActionType.ACTIVATE) storage.add(user) if self.activate_by_email: self.mail.send_activate(self.main_domain, user.email, action.token) return user
def email(): data = json_data(flask.request) valid, msg = Validator().validate(data) if not valid: return abort(msg) email = Email(data) for s in get_services(email, app): email.service = s resp = send_email(email) if resp.status_code is 200: save_email(db, email) return success(email) else: log_error(app.logger, email, resp) return abort('An error has occurred.', resp.status_code)
def __init__(self, reflector, *rotors): self._rotor_chain = RotorChain(reflector, *rotors) self.plugboard = Plugboard() self.__alpha_string_validator = Validator( TypeValidator(str), LengthValidator(1, lambda x, y: x >= y), AlphaValidator())
class Observer(ClientProgram): ''' Generic Diplomacy client. This class contains methods useful for all clients, but is designed to be subclassed. Handle messages from the server by defining a handle_XXX method, where XXX is the name of the first token in the message; for HUH, YES, REJ, and NOT messages, instead define handle_XXX_YYY, where YYY is the first token in the submessage. This class defines handlers for HLO, MAP, MDF, OFF, DRW, SLO, SMR, SVE, LOD, PNG, HUH_OBS, HUH_SEL, YES_SEL, and REJ_SEL; most of them do everything you will need, in combination with instance variables. '''#''' __section__ = 'clients' __options__ = ( ('response', str, 'HUH', 'invalid message response', 'How to react when a message fails a syntax check or results in an error.', '"close" or "die": Send a final message to the server, and stop', '"print" or "warn": Display a warning to the user', '"HUH" or "complain": Send an error message to the server', '"carp" or "croak": All three of the above', '"ignore" or anything else: None of the above', 'The second option will also re-raise the error, if any.'), ('validate', bool, False, 'validate incoming messages', 'Whether to validate messages received from the server.', 'Turn this on when testing a server or to protect against garbage;', 'however, doing so will slow down the game, particularly startup.'), ) def __init__(self, game_id=None, manager=None): ''' Initializes the instance variables.''' self.__super.__init__() self.transport = None # A function that accepts messages self.rep = None # The Representation to use self.game_id = game_id # A specific game to connect to self.game_opts= GameOptions() # Variant options for the current game self.closed = False # Whether the connection has ended, or should end self.map = None # The game board self.saved = {} # Positions saved from a SVE message self.use_map = False # Whether to initialize a Map; saves time for simple observers self.quit = True # Whether to close immediately when a game ends self.power = None self.manager = manager or ThreadManager() # Default name and version, for the NME message self.name = self.__class__.__name__ self.version = version_string() if self.options.validate: self.validator = Validator() else: self.validator = None # A list of variables to remember across SVE/LOD self.remember = ['map'] def register(self, transport, representation): ''' Registers the client with the server. Should be called as soon as possible after connection. If self.game_id is not None, sends a SEL (game_id) command; otherwise, skips straight to the OBS, NME, or IAM message. '''#''' self.transport = transport self.rep = representation if self.game_id is None: self.send_identity() else: self.send(SEL(self.game_id)) def close(self): self.log.debug("Closing.") self.closed = True if self.transport and not self.transport.closed: self.transport.close() # Sending messages to the Server def send(self, message): if self.transport and not self.closed: self.transport.write(Message(message)) def send_list(self, message_list): 'Sends a list of Messages to the server.' for msg in message_list: self.send(msg) def send_admin(self, text, *args): self.send(ADM(self.name or 'Observer')(str(text) % args)) def accept(self, message): self.send(YES(message)) def reject(self, message): self.send(REJ(message)) # Handling messages from the server def handle_message(self, message): ''' Process a new message from the server. Hands it off to a handle_XXX() method, where XXX is the first token of the message, if such a method is defined. Generally, message handling is sequential; that is, the event loop will wait for one message handler to finish before starting a new one. Message handlers may start new threads to change this behavior. '''#''' self.log_debug(5, '<< %s', message) if self.validator: # Check message syntax if message[0] is HLO: try: level = message[message.index(LVL) + 1].value() except (ValueError, IndexError): level = 0 self.validator.syntax_level = level reply = self.validator.validate_server_message(message) else: reply = None if reply: self.handle_invalid(message, reply=reply) else: # Note that try: / except AttributeError: doesn't work below, # because the method calls might produce AttributeErrors. method_name = 'handle_' + message[0].text # Special handling for common prefixes if message[0] in (YES, REJ, NOT, HUH): method_name += '_' + message[2].text self.log_debug(15, 'Searching for %s() methods', method_name) # Call map handlers first if self.map: method = getattr(self.map, method_name, None) if method: self.apply_handler(method, message) # Then call client handlers method = getattr(self, method_name, None) if method: self.apply_handler(method, message) self.log_debug(12, 'Finished %s message', message[0]) def apply_handler(self, method, message): self.log_debug(12, 'Calling %s', method) try: method(message) except Exception, e: self.handle_invalid(message, error=e) def handle_invalid(self, message, error=None, reply=None): response = self.options.response if response in ('print', 'warn', 'carp', 'croak'): if error: self.log_debug(1, 'Error processing command: ' + str(message)) else: self.log_debug(1, 'Invalid server command: ' + str(message)) if (response in ('huh', 'complain', 'carp', 'croak') and ERR not in message): self.send(reply or HUH(ERR + message)) if response in ('die', 'close', 'carp', 'croak'): self.close() if error and response not in ('print', 'close', 'huh', 'carp', 'ignore'): raise # Starting the game def send_identity(self): ''' Registers the observer with the server.''' self.send(OBS (self.name) (self.version)) def handle_HUH_OBS(self, message): ''' The server didn't like our OBS ('name') ('version') message.''' self.send(OBS) def handle_HUH_SEL(self, message): self.send_identity() def handle_YES_SEL(self, message): self.send_identity() def handle_REJ_SEL(self, message): if self.quit: self.close() def handle_HLO(self, message): self.game_opts.parse_message(message) def handle_SEL(self, message): self.game_id = message.fold()[1][0] # Automatic map handling def handle_MAP(self, message): ''' Handles the MAP command, creating a new map if possible. Sends MDF for unknown maps, YES for valid. '''#''' if self.use_map: mapname = message.fold()[1][0] try: variant = variants[mapname] except KeyError: variant = Variant(mapname, rep=self.rep) self.map = Map(variant) if self.map.valid: if self.process_map(): self.accept(message) if self.power: self.send_list([HLO, SCO, NOW]) else: self.reject(message) else: self.send(MDF) else: self.accept(message) def handle_MDF(self, message): ''' Replies to the MDF command. The map should have loaded itself, but might not be valid. '''#''' if self.map: if self.map.valid and self.process_map(): self.accept(MAP(self.map.name)) if self.power: self.send_list([HLO, SCO, NOW]) else: self.reject(MAP(self.map.name)) else: raise ValueError('MDF before MAP') def process_map(self): ''' Performs any supplemental processing on the map. The map will be loaded and valid by this point. Returns whether to accept the MAP message. '''#''' return True def phase(self): return self.map.current_turn.phase() # End of the game def handle_OFF(self, message): self.close() def game_over(self, message): if self.quit: self.close() else: self.in_game = False handle_DRW = game_over handle_SLO = game_over handle_SMR = game_over # Other generic handlers def handle_SVE(self, message): ''' Attempts to save everything named by self.remember.''' try: game = {} for name in self.remember: try: value = deepcopy(getattr(self, name)) except AttributeError: value = None game[name] = value self.saved[message.fold()[1][0]] = game self.accept(message) except: self.reject(message) def handle_LOD(self, message): ''' Restores attributes saved from a SVE message.''' game = message.fold()[1][0] if self.saved.has_key(game): self.__dict__.update(self.saved[game]) #for (name, value) in self.saved[game]: # setattr(self, name, value) if self.power: self.send(IAM(self.power)(self.pcode)) else: self.accept(message) else: self.reject(message) def handle_PNG(self, message): self.accept(message)
def wiring(self, wiring): Validator(TypeValidator(str), AlphaValidator(), LengthValidator(26)).validate(wiring) self.__wiring = wiring.upper()
def validate_service(self, data): validator = Validator(data) validator.port('port', required=False) validator.port('local_port') check_validator(validator)
from handler import DriverValidator from handler import TripValidator from handler import VanValidator from validation import Validator if __name__ == "__main__": request = { "van": { "id": 1, "name": "U01" }, "driver": { "id": 1, "name": "John Doe" }, "trip": { "id": 1, "seats": 1 }, } van = VanValidator() driver = DriverValidator() trip = TripValidator() validations = van.set_next(driver).set_next(trip) result = Validator(validations).validate(request) print(f"validating request: {request}, result: {result}")
def data_validation(): logger.info('run caa validation...') Validator.validate_caa_data() logger.info('completed...')
if (options["general"]["usecudnnbenchmark"] and options["general"]["usecudnn"]): print("Running cudnn benchmark...") torch.backends.cudnn.benchmark = True #Create the model. model = LipRead(options) if (options["general"]["loadpretrainedmodel"]): model.load_state_dict( torch.load(options["general"]["pretrainedmodelpath"], map_location=lambda storage, loc: storage)) #model = torch.load('/home/admin2/grp7/Lipreading-PyTorch/trainedmodel.pt',map_location=lambda storage, loc: storage) #Move the model to the GPU. if (options["general"]["usecudnn"]): model = model.cuda(options["general"]["gpuid"]) #if(options["general"]["usecudnn"]): # model = model.cuda(options["general"]["gpuid"]) #self.model = model.CPU().double() trainer = Trainer(options) validator = Validator(options) for epoch in range(options["training"]["startepoch"], options["training"]["epochs"]): if (options["training"]["train"]): trainer.epoch(model, epoch) if (options["validation"]["validate"]): validator.epoch(model)
def init(self): self.vdt = Validator.make(self.inputs, self.rules)
def testCoordinateValidation(self): Validator.validateCoordinate("A1") Validator.validateCoordinate("B5") with self.assertRaises(CoordinateInvalid): Validator.validateCoordinate("a0") with self.assertRaises(CoordinateInvalid): Validator.validateCoordinate("f6") with self.assertRaises(CoordinateInvalid): Validator.validateCoordinate("123") with self.assertRaises(CoordinateInvalid): Validator.validateCoordinate("r4")
class Observer(VerboseObject): ''' Generic Diplomacy client. This class contains methods useful for all clients, but is designed to be subclassed. Handle messages from the server by defining a handle_XXX method, where XXX is the name of the first token in the message; for HUH, YES, REJ, and NOT messages, instead define handle_XXX_YYY, where YYY is the first token in the submessage. This class defines handlers for HLO, MAP, MDF, OFF, DRW, SLO, SMR, SVE, LOD, PNG, HUH_OBS, HUH_SEL, YES_SEL, and REJ_SEL; most of them do everything you will need, in combination with instance variables. '''#''' __section__ = 'clients' __options__ = ( ('response', str, 'HUH', 'invalid message response', 'How to react when a message fails a syntax check or results in an error.', '"close" or "die": Send a final message to the server, and stop', '"print" or "warn": Display a warning to the user', '"HUH" or "complain": Send an error message to the server', '"carp" or "croak": All three of the above', '"ignore" or anything else: None of the above', 'The second option will also re-raise the error, if any.'), ('validate', bool, False, 'validate incoming messages', 'Whether to validate messages received from the server.', 'Turn this on when testing a server or to protect against garbage;', 'however, doing so will slow down the game, particularly startup.'), ) def __init__(self, send_method, representation, game_id=None, manager=None): ''' Initializes the instance variables.''' self.__super.__init__() self.send_out = send_method # A function that accepts messages self.rep = representation # The representation message self.game_id = game_id # A specific game to connect to self.game_opts= GameOptions() # Variant options for the current game self.closed = False # Whether the connection has ended, or should end self.map = None # The game board self.saved = {} # Positions saved from a SVE message self.use_map = False # Whether to initialize a Map; saves time for simple observers self.quit = True # Whether to close immediately when a game ends self.power = None self.manager = manager or ThreadManager() # Default name and version, for the NME message self.name = self.__class__.__name__ self.version = version_string() if self.options.validate: self.validator = Validator() else: self.validator = None # A list of variables to remember across SVE/LOD self.remember = ['map'] # A list of message handlers that should be called in parallel. self.threaded = [] def register(self): ''' Registers the client with the server. Should be called as soon as possible after connection. If self.game_id is not None, sends a SEL (game_id) command; otherwise, skips straight to the OBS, NME, or IAM message. '''#''' if self.game_id is None: self.send_identity() else: self.send(SEL(self.game_id)) def close(self): self.closed = True # Sending messages to the Server def send(self, message): if not self.closed: self.send_out(Message(message)) def send_list(self, message_list): 'Sends a list of Messages to the server.' for msg in message_list: self.send(msg) def send_admin(self, text, *args): self.send(ADM(self.name or 'Observer')(str(text) % args)) def accept(self, message): self.send(YES(message)) def reject(self, message): self.send(REJ(message)) # Handling messages from the server def handle_message(self, message): ''' Process a new message from the server. Hands it off to a handle_XXX() method, where XXX is the first token of the message, if such a method is defined. Generally, message handling is sequential; that is, the event loop will wait for one message handler to finish before starting a new one. Message handlers may start new threads to change this behavior. '''#''' self.log_debug(5, '<< %s', message) if self.validator: # Check message syntax if message[0] is HLO: try: level = message[message.index(LVL) + 1].value() except (ValueError, IndexError): level = 0 self.validator.syntax_level = level reply = self.validator.validate_server_message(message) else: reply = None if reply: self.handle_invalid(message, reply=reply) else: # Note that try: / except AttributeError: doesn't work below, # because the method calls might produce AttributeErrors. method_name = 'handle_' + message[0].text # Special handling for common prefixes if message[0] in (YES, REJ, NOT, HUH): method_name += '_' + message[2].text self.log_debug(15, 'Searching for %s() methods', method_name) # Call map handlers first if self.map: method = getattr(self.map, method_name, None) if method: self.apply_handler(method, message) # Then call client handlers method = getattr(self, method_name, None) if method: if method_name in self.threaded: self.manager.new_thread(self.apply_handler, method, message) else: self.apply_handler(method, message) self.log_debug(12, 'Finished %s message', message[0]) def apply_handler(self, method, message): self.log_debug(12, 'Calling %s', method) try: method(message) except Exception, e: self.handle_invalid(message, error=e) def handle_invalid(self, message, error=None, reply=None): response = self.options.response if response in ('print', 'warn', 'carp', 'croak'): if error: self.log_debug(1, 'Error processing command: ' + str(message)) else: self.log_debug(1, 'Invalid server command: ' + str(message)) if (response in ('huh', 'complain', 'carp', 'croak') and ERR not in message): self.send(reply or HUH(ERR + message)) if response in ('die', 'close', 'carp', 'croak'): self.close() if error and response not in ('print', 'close', 'huh', 'carp', 'ignore'): raise # Starting the game def send_identity(self): ''' Registers the observer with the server.''' self.send(OBS (self.name) (self.version)) def handle_HUH_OBS(self, message): ''' The server didn't like our OBS ('name') ('version') message.''' self.send(OBS) def handle_HUH_SEL(self, message): self.send_identity() def handle_YES_SEL(self, message): self.send_identity() def handle_REJ_SEL(self, message): if self.quit: self.close() def handle_HLO(self, message): self.game_opts.parse_message(message) # Automatic map handling def handle_MAP(self, message): ''' Handles the MAP command, creating a new map if possible. Sends MDF for unknown maps, YES for valid. '''#''' if self.use_map: mapname = message.fold()[1][0] try: variant = variants[mapname] except KeyError: variant = Variant(mapname, rep=self.rep) self.map = Map(variant) if self.map.valid: if self.process_map(): self.accept(message) if self.power: self.send_list([HLO, SCO, NOW]) else: self.reject(message) else: self.send(MDF) else: self.accept(message) def handle_MDF(self, message): ''' Replies to the MDF command. The map should have loaded itself, but might not be valid. '''#''' if self.map: if self.map.valid and self.process_map(): self.accept(MAP(self.map.name)) if self.power: self.send_list([HLO, SCO, NOW]) else: self.reject(MAP(self.map.name)) else: raise ValueError, 'MDF before MAP' def process_map(self): ''' Performs any supplemental processing on the map. The map will be loaded and valid by this point. Returns whether to accept the MAP message. '''#''' return True def phase(self): return self.map.current_turn.phase() # End of the game def handle_OFF(self, message): self.close() def game_over(self, message): if self.quit: self.close() else: self.in_game = False handle_DRW = game_over handle_SLO = game_over handle_SMR = game_over # Other generic handlers def handle_SVE(self, message): ''' Attempts to save everything named by self.remember.''' try: game = {} for name in self.remember: try: value = deepcopy(getattr(self, name)) except AttributeError: value = None game[name] = value self.saved[message.fold()[1][0]] = game self.accept(message) except: self.reject(message) def handle_LOD(self, message): ''' Restores attributes saved from a SVE message.''' game = message.fold()[1][0] if self.saved.has_key(game): self.__dict__.update(self.saved[game]) #for (name, value) in self.saved[game]: # setattr(self, name, value) if self.power: self.send(IAM(self.power)(self.pcode)) else: self.accept(message) else: self.reject(message) def handle_PNG(self, message): self.accept(message)
''' Created on Feb 6, 2019 @author: Marius ''' from ui import Console from bussines import Service from persistance import Repository from validation import Validator repository = Repository() validator = Validator() service = Service(repository, validator) console = Console(service) console.run()
} print('matched keys:', len(pretrained_dict)) model_dict.update(pretrained_dict) model.load_state_dict(model_dict) #Move the model to the GPU. #criterion = model.loss() if (options["general"]["usecudnn"]): torch.cuda.manual_seed(options["general"]['random_seed']) torch.cuda.manual_seed_all(options["general"]['random_seed']) if (options["training"]["train"]): trainer = Trainer(options) if (options["validation"]["validate"]): validator = Validator(options, 'validation') if (options['test']['test']): tester = Validator(options, 'test') for epoch in range(options["training"]["startepoch"], options["training"]["epochs"]): if (options["training"]["train"]): trainer(model, epoch) if (options["validation"]["validate"]): result, re_all = validator(model) print('-' * 21) print('All acc:' + str(re_all)) print('{:<10}|{:>10}'.format('Cls #', 'Accuracy')) for i in range(len(result)): print('{:<10}|{:>10}'.format(i, result[i])) print('-' * 21)
print_log(model, log=args.logfile) model = reload_model(model, args.logfile, args.path) # reload model model = model.cuda() # move the model to the GPU. if args.modelname == 'C3D_CONV_BLSTM_frontfix': print_log('\n\nwith freezing frontend', log=args.logfile) for param in model.frontend.parameters(): param.requires_grad = False for param in model.resnet.parameters(): param.requires_grad = False else: print_log('\n\nno freezing', log=args.logfile) print_log('loading data', log=args.logfile) if args.train: trainer = Trainer(args) if args.val: validator = Validator(args) if args.train: loss_history_train, loss_history_val = [], [] accu_history_train, accu_history_val = [], [] for epoch in range(args.start_epoch, args.end_epoch): loss_train, accu_train = trainer.epoch(model, epoch) if args.val: loss_val, accu_val = validator.epoch(model, epoch) # plot figure loss_history_train.append([loss_train]) loss_history_val.append([loss_val]) accu_history_train.append([accu_train]) accu_history_val.append([accu_val]) plot_loss(loss_history_train, loss_history_val, save_dir=args.save_dir) plot_accu(accu_history_train, accu_history_val, save_dir=args.save_dir)
print_log('creating the model', log=options["general"]["logfile"]) model = LipRead(options) print_log('loading model', log=options["general"]["logfile"]) if options["general"]["loadpretrainedmodel"]: print_log('loading the pretrained model at %s' % options["general"]["pretrainedmodelpath"], log=options["general"]["logfile"]) model.load_state_dict(torch.load( options["general"]["pretrainedmodelpath"])) #Create the model. if options["general"]["usecudnn"]: model = model.cuda( options["general"]["gpuid"]) #Move the model to the GPU. print_log('loading data', log=options["general"]["logfile"]) if options["training"]["train"]: trainer = Trainer(options) if options["validation"]["validate"]: validator = Validator(options) validator.epoch(model, epoch=0) if options["training"]["train"]: for epoch in range(options["training"]["startepoch"], options["training"]["endepoch"]): trainer.epoch(model, epoch) if options["validation"]["validate"]: validator.epoch(model, epoch) # if options["testing"]["test"]: # tester = Tester(options) # tester.epoch(model) options["general"]["logfile"].close()
class Enigma: """ This class simulates the Enigma machine As models M3 and M4 of the Enigma machine, it supports 3 or 4 rotors configuration. The order of reflector and rotors definition respect the physical order (left-to-right). :param reflector: The reflector associated to the Enigma machine :type reflector: Reflector :param *rotors: The rotors associated to the Enigma machine :type *rotors: Variable length rotor list :raises: :class:`TypeError, ValueError`: If one of the provided parameter is invalid Example:: enigma = Enigma(ReflectorB(), RotorI(), RotorII(), RotorIII("V")) enigma.encode("HELLOWORLDHELLOWORLDHELLOWORLD") enigma = Enigma(ReflectorA(), RotorIV("E", 18), RotorV("Z", 24), RotorBeta("G", 3), RotorI("P", 5)) enigma.plugboard.set("PC XZ FM QA ST NB HY OR EV IU") enigma.encode("BUPXWJCDPFASXBDHLBBIBSRNWCSZXQOLBNXYAXVHOGCUUIBCVMPUZYUUKHI") """ def __init__(self, reflector, *rotors): self._rotor_chain = RotorChain(reflector, *rotors) self.plugboard = Plugboard() self.__alpha_string_validator = Validator( TypeValidator(str), LengthValidator(1, lambda x, y: x >= y), AlphaValidator()) def encode(self, string): """ Perform the whole encoding of a string on the Enigma machine Each character of the string is first encoded by the plug board then the character is encoded through the rotor chain and finally the character is encoded by the plug board again. :param char: The string to encode :type char: str :return: The encoded string :rtype: str """ self.__alpha_string_validator.validate(string) encoded_string = "" for letter in string: encoded_string += self.plugboard.encode( self._rotor_chain.encode(self.plugboard.encode(letter))) return encoded_string def reset(self): """ Reset all rotors of the machine to position to "A" and the ring setting to 1 """ self._rotor_chain.reset() @property def plugboard(self): """ The plug board associated to the Enigma machine :getter: Returns the plug board :setter: Sets the plug board :type: Plugboard """ return self.__plugboard @plugboard.setter def plugboard(self, plugboard): self.__plugboard = plugboard @property def reflector(self): """ The reflector associated to the Enigma machine :getter: Returns the reflector :setter: Sets the reflector :type: Reflector """ return self._rotor_chain.reflector @reflector.setter def reflector(self, reflector): self._rotor_chain.reflector = reflector @property def rotors(self): """ The rotors associated to the Enigma machine :getter: Returns the list of rotors :setter: Sets the list of rotors :type: Rotors list """ return self._rotor_chain.rotors @rotors.setter def rotors(self, rotors): self._rotor_chain.rotors = rotors
def ring_setting(self, ring_setting): Validator(TypeValidator(int), RangeValidator(1, 26)).validate(ring_setting) self.__ring_setting = ring_setting
model = LipRead(options) load_model(model, state_dict, grad_states) # load weights and freeze states last_epoch = states["epoch"] # save current options with open(os.path.join(result_dir, "options_used.toml"), 'w') as f: toml.dump(options, f) if(options["general"]["usecudnnbenchmark"] and options["general"]["usecudnn"]): print("Running cudnn benchmark...") torch.backends.cudnn.benchmark = True if options["training"]["train"]: trainer = Trainer(options) if(options["validation"]["validate"]): validator = Validator(options, result_dir) final_epoch = args.final_epoch if args.final_epoch is not None else options["training"]["epochs"] for epoch in range(last_epoch + 1, final_epoch): loss = trainer.epoch(model, epoch) if options["training"]["train"] else '' accuracy = validator.epoch(model, epoch) if options["validation"]["validate"] else '' csv.add(epoch, accuracy=accuracy, loss=loss) save_checkpoint(result_dir, epoch, model, options=options) # save the final model torch.save(model.state_dict(), os.path.join(result_dir, "epoch{}.pt".format(epoch)))
def testShipValidatoin(self): Validator.validateShip("A1", "A2", "A3") Validator.validateShip("B0", "B1", "B2") Validator.validateShip("A1", "B1", "C1") Validator.validateShip("D2", "E2", "F2") with self.assertRaises(CoordinateInvalid): Validator.validateShip("D1", "E2", "F2") with self.assertRaises(CoordinateInvalid): Validator.validateShip("A1", "A5", "A3") with self.assertRaises(CoordinateInvalid): Validator.validateShip("D1", "D2", "F2") with self.assertRaises(CoordinateInvalid): Validator.validateShip("A1", "B2", "C1")
def validate_service(self, data): validator = Validator(data) validator.port() if validator.errors: message = ", ".join(validator.errors) raise servicesexceptions.bad_request(message)