def __init__(self, value): Token.__init__(self, value) self.supported_neighbors = '([+]|[-]|[*]|[=]|[!]|[>]|[<]|[\/]|[;]|[,]|[.]|[&]|[|]|[(]|[)]|[{]|[}]|[\[]|[\]])' self.reserved_words = ("var", "const", "typedef", "struct", "extends", "procedure", "function", "start", "return", "if", "else", "then", "while", "read", "print", "int", "real", "boolean", "string", "true", "false", "global", "local") self.value = value
def _resolve_duel(self, attacking_token: Token, defending_token: Token) -> int: if attacking_token.unit.physical: attack = attacking_token.unit.attack if randint(0, 100) < attacking_token.unit.criticalChance: attack *= int(uniform(1.5, 2)) else: attack = randint(0, attack) defense = randint(0, defending_token.unit.defense) else: attack = attacking_token.unit.attack defense = randint(0, defending_token.unit.magicResist) distance = defending_token.distance(attacking_token) attack_range = attacking_token.unit.range if distance <= 0: raise ValueError("Distance between tokens is 0") damage_reduction_divider = self._calculate_damage_divider( distance, attack_range) damage = max(1, attack - defense) damage /= damage_reduction_divider defending_token.unit.hp = max(0, defending_token.unit.hp - damage) return damage
def generateUserToken(user_id: str): """ Creates a token for a specific user. Removes any token previously created for the user. """ response = ApiResponse() user = User.query.filter_by(id=user_id).first() timestamp = time.time() timestamp_millis = int(round(timestamp * 1000)) token_ids = sha256(hash_id(timestamp_millis + randint(0, 9999))) token_value = sha256(hash_id(timestamp_millis) + str(uuid4())) expires_at = int(timestamp + TOKEN_EXPIRATION_TIME) if user: token = Token(ids=token_ids, ip=request.remote_addr, token=token_value, User_id=user.id, ut_created_at=timestamp, ut_expires_at=expires_at) TokenService.clearUserTokens(user.id) if database.save_changes(token) is False: response.setMessage( "An error occured while persisting data to the database") else: response.setSuccess() response.setMessage("Token successfuly generated") response.setDetails({ "token": token_value, "expires_at": expires_at }) else: response.setMessage("User not found in the database") return response
def from_dict(cls, d: dict): units = map(Unit.from_dict, d['units']) units = {unit.id: unit for unit in units} tokens = [ Token(units[placement['unitId']], (placement['x'], placement['y'])) for placement in d['unitsPlacement'] ] return cls(tokens)
def clean_token(conf, token): t = Token() t.set_spacy_token(token) # spelled = spell.check(token.lemma_, conf.spa_dict) spelled = spell.check_exact(token.lemma_, conf.spa_dict) # print(spelled) # if '+93' in token.text: # print(token.text, str(token.pos_), str(token.ent_type_)) if str(token.pos_) == 'NOUN': # \ # or str(token.pos_) == 'PROPN': t.stop = False if str(token.ent_type_) == 'PER' \ or token.like_num \ or token.like_url \ or token.like_email \ or token.is_quote \ or token.is_bracket \ or token.is_space \ or token.is_right_punct \ or token.is_left_punct \ or token.is_punct \ or token.is_digit \ or token.is_currency \ or len(token.text) <= 3 \ or len(spelled) == 0: t.stop = True if str(token.ent_type_) == 'MISC' \ or str(token.ent_type_) == 'ORG' \ or len(token.text) > 3 and len(spelled) > 0: t.stop = False if str(token.pos_) == 'SPACE' \ or str(token.pos_) == 'NUM' \ or str(token.pos_) == 'DET' \ or str(token.pos_) == 'CONJ' \ or str(token.pos_) == 'SCONJ' \ or str(token.pos_) == 'PUNCT' \ or '/' in token.text \ or '' in token.text \ or len(token.text) <= 2: t.stop = True # or '/' in token.text \??? return t
def parse_next_line(self): line = self.read_next_line() self.current_line += 1 new_col, string, col, line_size, tabs = 1, "", 0, len(line), 0 while col < line_size and (line[col] == ' ' or line[col] == '\t'): new_col += 1 if line[col] == ' ' else self.TAB_SIZE tabs += 1 if line[col] == '\t' else 0 col += 1 if col == line_size: # empty line return False while col < line_size: c = line[col] if c == '\'' or c == '"': # string and character literals string = c col += 1 while col < line_size: c = line[col] if c == '\\': col += 1 if col < line_size and line[col] in self.escape_char: string += self.escape_char[line[col]] else: string += '\\' else: string += c if c == string[0]: break col += 1 category = self.get_category(string) if category != TokenCategory.unknown: string = string[1:len(string) - 1] self.token_buffer.append( Token(TokenPosition(self.current_line, new_col), category, string)) string, new_col = "", col + 1 + (self.TAB_SIZE * tabs) elif self.is_separator(c): if string: self.token_buffer.append( Token(TokenPosition(self.current_line, new_col), self.get_category(string), string)) if c != ' ' and c != '\t': new_col = col + 1 + (self.TAB_SIZE - 1) * tabs if c == '*': col += 1 if col < line_size and (line[col] == '/' or line[col] == '*'): c += line[col] else: col -= 1 if c == '<' or c == '>': col += 1 if col < line_size and (line[col] == '<' or line[col] == '>' or line[col] == '='): c += line[col] else: col -= 1 if c == '/': col += 1 if col < line_size and line[col] == '/': return True col -= 1 if c == '&' or c == '|': col += 1 if col < line_size and (line[col] == '&' or line[col] == '|'): c += line[col] else: col -= 1 self.token_buffer.append( Token(TokenPosition(self.current_line, new_col), self.separators[c], c)) string, new_col = "", col + 2 + (self.TAB_SIZE - 1) * tabs else: string += c col += 1 if string: self.token_buffer.append( Token(TokenPosition(self.current_line, new_col), self.get_category(string), string)) return True
def __init__(self, value): Token.__init__(self, value) self.error = True self.unknown_symbol = False
def __init__(self, value): Token.__init__(self, value)
def __init__(self, value): Token.__init__(self, value) self.supported_neighbors = '([a-z]|[A-Z]|[0-9]|[{]|[}]|["]|[;]|[_]|[=]|[)]|[(])|[+]|[-]|[!]|[/]' self.value = value
def __init__(self, value): Token.__init__(self, value) self.supported_neighbors = '([a-z]|[A-Z]|[0-9]|[;]|[+]|[-]|["]|[(])' self.supported_value = ['==', '!=', '>=', '<=']
def __init__(self, value): Token.__init__(self, value) self.supported_neighbors = '([a-z]|[A-Z]|[0-9]|[(])' self.supported_value = ['&&', '||']
assert token43.unit.id == "43" assert token43.position == (2, 3) assert token44.unit.id == "44" assert token44.position == (5, 7) @pytest.mark.parametrize('board, expected', [ ( Board([]), False ), ( Board([ Token(Unit(id=2, hp=0), (3, 5)) ]), False ), ( Board([ Token(Unit(id=2, hp=100), (3, 5)) ]), True ), ( Board([ Token(Unit(id=2, hp=100), (3, 5)), Token(Unit(id=7, hp=0), (11, 13)), Token(Unit(id=17, hp=100), (19, 21)) ]),
from math import sqrt import pytest from pytest import approx from model.Token import Token from model.Unit import Unit @pytest.mark.parametrize('token1, token2, expected', [(Token(Unit(), (0, 0)), (Token(Unit(), (0, 0))), 0.0), (Token(Unit(), (3, 4)), (Token(Unit(), (0, 0))), 5.0), (Token(Unit(), (21, 9)), (Token(Unit(), (37, 11))), sqrt(260))]) def test_eq(token1, token2, expected): assert token1.distance(token2) - token2.distance(token1) == approx( 0, abs=1e-10) assert token1.distance(token2) == approx(expected, abs=1e-10)
def __init__(self, value): Token.__init__(self, value) self.point_delimiter = False self.supported_neighbors = '([+]|[-]|[*]|[=]|[!]|[>]|[<]|[\/]|[;]|[,]|[(]|[)]|[\[]|[\]])'
def nextToken(self): if(self.array_pointer <= len(self.content)): while(True): currentChar = self.getNextChar() if(currentChar is not None): if self.current_state==0: self.current_token = None if(Token.isChar(currentChar)): self.current_state = 1 self.current_token = Identifier(currentChar) elif(Token.isNumber(currentChar)): self.current_state = 2 self.current_token = Digit(currentChar) elif(Token.isArithmeticOperator(currentChar)): nextChar = self.content[self.array_pointer] if(Token.isCommentDelimiter(currentChar, nextChar)): self.current_state = 7 self.current_token = Comment(currentChar+nextChar) else: self.current_state = 3 self.current_token = Arithmetic(currentChar) elif(Token.isRelationalOperator(currentChar)): self.current_state = 4 self.current_token = Relational(currentChar) elif(Token.isLogicOperator(currentChar)): self.current_state = 5 self.current_token = Logic(currentChar) elif(Token.isDelimiter(currentChar)): self.current_state = 6 self.current_token = Delimiter(currentChar) elif(Token.isStringDelimeter(currentChar)): self.current_state = 8 self.current_token = String(currentChar) elif(currentChar == '\n'): self.current_line+=1 else: if(not Token.isSpace(currentChar)): self.current_state = 1 self.current_token = Identifier(currentChar) self.current_token.error = True elif(self.current_state ==1): if(self.current_token.isValid(currentChar) and not Token.isSpace(currentChar)): self.current_token.setValue(currentChar) self.current_state = 1 else: if(not self.current_token.validNeighbors(currentChar) and not Token.isSpace(currentChar)): self.current_token.setValue(currentChar) self.current_state = 1 else: self.current_state = 0 self.back() self.token_list.append(self.current_token.returnValue(self.current_line)) return self.current_token.returnValue(self.current_line) elif(self.current_state==2): if(self.current_token.isValid(currentChar) and not Token.isSpace(currentChar)): self.current_token.setValue(currentChar) self.current_state = 2 else: if(not self.current_token.validNeighbors(currentChar) and not Token.isSpace(currentChar)): self.current_token.setValue(currentChar) self.current_state = 2 else: self.current_state = 0 self.back() self.token_list.append(self.current_token.returnValue(self.current_line)) return self.current_token.returnValue(self.current_line) elif self.current_state ==3: if(self.current_token.isValid(currentChar) and not Token.isSpace(currentChar)): self.current_token.setValue(currentChar) self.current_state = 3 else: if(not self.current_token.validNeighbors(currentChar) and not Token.isSpace(currentChar)): self.current_token.setValue(currentChar) self.current_state = 3 elif Token.isSpace(currentChar): self.current_state = 0 self.back() self.token_list.append(self.current_token.returnValue(self.current_line)) return self.current_token.returnValue(self.current_line) else: self.current_state = 0 self.back() self.token_list.append(self.current_token.returnValue(self.current_line)) return self.current_token.returnValue(self.current_line) elif self.current_state ==4: if(self.current_token.isValid(currentChar) and not Token.isSpace(currentChar)): self.current_token.setValue(currentChar) self.current_state = 3 else: if(not self.current_token.validNeighbors(currentChar) and not Token.isSpace(currentChar)): self.current_token.setValue(currentChar) self.current_state = 3 elif Token.isSpace(currentChar): self.current_state = 0 self.back() self.token_list.append(self.current_token.returnValue(self.current_line)) return self.current_token.returnValue(self.current_line) else: self.current_state = 0 self.back() self.token_list.append(self.current_token.returnValue(self.current_line)) return self.current_token.returnValue(self.current_line) elif self.current_state==5: if(self.current_token.isValid(currentChar) and not Token.isSpace(currentChar)): self.current_token.setValue(currentChar) self.current_state = 5 else: if(not self.current_token.validNeighbors(currentChar) and not Token.isSpace(currentChar)): self.current_token.setValue(currentChar) self.current_state = 5 elif Token.isSpace(currentChar): self.current_state = 0 self.back() self.token_list.append(self.current_token.returnValue(self.current_line)) return self.current_token.returnValue(self.current_line) else: self.current_state = 0 self.back() self.token_list.append(self.current_token.returnValue(self.current_line)) return self.current_token.returnValue(self.current_line) elif self.current_state==6: if(self.current_token.isValid(currentChar) and not Token.isSpace(currentChar)): self.current_token.setValue(currentChar) self.current_state = 6 else: if(not self.current_token.validNeighbors(currentChar) and not Token.isSpace(currentChar)): self.current_token.setValue(currentChar) self.current_state = 6 elif Token.isSpace(currentChar): self.current_state = 0 self.back() self.token_list.append(self.current_token.returnValue(self.current_line)) return self.current_token.returnValue(self.current_line) else: self.current_state = 0 self.back() self.token_list.append(self.current_token.returnValue(self.current_line)) return self.current_token.returnValue(self.current_line) elif self.current_state==7: if(self.current_token.isInlineComment()): if(self.current_token.isEndInlineComment(currentChar)): self.current_state = 0 self.back() elif(self.current_token.isBlockComment()): if(self.current_token.isBreakLine(currentChar)): self.current_line+=1 elif(self.array_pointer < len(self.content)): nextChar = self.content[self.array_pointer] if(self.current_token.isEndBlockComment(currentChar+nextChar)): self.array_pointer+=1 self.current_state = 0 elif self.current_state==8: self.current_token.setValue(currentChar) if(self.current_token.isBreakLine(currentChar)): self.back() self.current_state = 0 self.token_list.append(self.current_token.returnValue(self.current_line)) return self.current_token.returnValue(self.current_line) elif(self.current_token.isValid(currentChar)): pass elif(Token.isStringDelimeter(currentChar)): prevChar = self.content[self.array_pointer-2] if(not self.current_token.isEscapedDelimeter(prevChar+currentChar)): self.current_token.setError() self.current_state = 0 self.token_list.append(self.current_token.returnValue(self.current_line)) return self.current_token.returnValue(self.current_line) elif(not Token.isSymbol(currentChar)): self.current_token.unknown_symbol = True else: if(self.current_token and self.current_token.value!="//"): token = self.current_token.returnValue(self.current_line) self.current_token = None self.token_list.append(token) return token return else: return None
def __init__(self, value): Token.__init__(self, value) self.supported_neighbors = '([a-z]|[A-Z]|[0-9]|[(]|[)]|[;]|[\[]|[\]])' self.supported_value = ['++', '--']