From: Christian Heller Date: Thu, 26 Nov 2020 20:47:31 +0000 (+0100) Subject: Some more code style improvements after flake8. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/static/%7B%7Bprefix%7D%7D/edit?a=commitdiff_plain;h=df9a8d0a788b29913dae3eec4ef8113e2d8e9a41;p=plomrogue2 Some more code style improvements after flake8. --- diff --git a/plomrogue/commands.py b/plomrogue/commands.py index 0c3da0a..65b0540 100644 --- a/plomrogue/commands.py +++ b/plomrogue/commands.py @@ -146,20 +146,20 @@ def cmd_GET_GAMESTATE(game, connection_id): game.send_gamestate(connection_id) cmd_GET_GAMESTATE.argtypes = '' -#def cmd_QUERY(game, target_nick, msg, connection_id): -# if not connection_id in game.sessions: -# raise GameError('can only query when logged in') -# t = game.get_thing(game.sessions[connection_id], False) -# source_nick = t.name -# for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]: -# for c_id in game.sessions: -# if game.sessions[c_id] == t.id_: -# game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id) -# game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id) -# return -# raise GameError('target user offline') -# raise GameError('can only query with registered nicknames') -#cmd_QUERY.argtypes = 'string string' +# def cmd_QUERY(game, target_nick, msg, connection_id): +# if not connection_id in game.sessions: +# raise GameError('can only query when logged in') +# t = game.get_thing(game.sessions[connection_id], False) +# source_nick = t.name +# for t in [t for t in game.things if t.type_ == 'Player' and t.name == target_nick]: +# for c_id in game.sessions: +# if game.sessions[c_id] == t.id_: +# game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), c_id) +# game.io.send('CHAT ' + quote(source_nick+ '->' + target_nick + ': ' + msg), connection_id) +# return +# raise GameError('target user offline') +# raise GameError('can only query with registered nicknames') +# cmd_QUERY.argtypes = 'string string' def cmd_PING(game, connection_id): game.io.send('PONG', connection_id) @@ -181,7 +181,7 @@ def cmd_ANNOTATE(game, yx, msg, pw, connection_id): if little_yx in game.annotations[big_yx]: del game.annotations[big_yx][little_yx] else: - if not big_yx in game.annotations: + if big_yx not in game.annotations: game.annotations[big_yx] = {} game.annotations[big_yx][little_yx] = msg game.changed = True @@ -199,21 +199,21 @@ def cmd_PORTAL(game, yx, msg, pw, connection_id): if little_yx in game.portals[big_yx]: del game.portals[big_yx][little_yx] else: - if not big_yx in game.portals: + if big_yx not in game.portals: game.portals[big_yx] = {} game.portals[big_yx][little_yx] = msg game.changed = True cmd_PORTAL.argtypes = 'yx_tuple:nonneg string string' def cmd_GOD_ANNOTATE(game, big_yx, little_yx, msg): - if not big_yx in game.annotations: + if big_yx not in game.annotations: game.annotations[big_yx] = {} game.annotations[big_yx][little_yx] = msg game.changed = True cmd_GOD_ANNOTATE.argtypes = 'yx_tuple yx_tuple:nonneg string' def cmd_GOD_PORTAL(game, big_yx, little_yx, msg): - if not big_yx in game.portals: + if big_yx not in game.portals: game.portals[big_yx] = {} game.portals[big_yx][little_yx] = msg game.changed = True @@ -222,9 +222,9 @@ cmd_GOD_PORTAL.argtypes = 'yx_tuple yx_tuple:nonneg string' def cmd_GET_ANNOTATION(game, yx, connection_id): player = game.get_player(connection_id) big_yx, little_yx = player.fov_stencil.source_yxyx(yx) - annotation = '(unknown)'; + annotation = '(unknown)' if player.fov_test(big_yx, little_yx): - annotation = '(none)'; + annotation = '(none)' if big_yx in game.annotations: if little_yx in game.annotations[big_yx]: annotation = game.annotations[big_yx][little_yx] @@ -251,7 +251,7 @@ def cmd_MAP_CONTROL_PW(game, tile_class, password): cmd_MAP_CONTROL_PW.argtypes = 'char string' def cmd_THING(game, big_yx, little_yx, thing_type, thing_id): - if not thing_type in game.thing_types: + if thing_type not in game.thing_types: raise GameError('illegal thing type %s' % thing_type) _ = game.get_map(big_yx) t_old = None diff --git a/plomrogue/game.py b/plomrogue/game.py index 7936842..45e23e1 100755 --- a/plomrogue/game.py +++ b/plomrogue/game.py @@ -58,15 +58,15 @@ class SaveableMap(Map): self.set_line(3, 'X' * self.geometry.size.x) self.set_line(4, 'X' * self.geometry.size.x) for y in range(self.geometry.size.y): - self[YX(y,0)] = 'X' - self[YX(y,1)] = 'X' - self[YX(y,2)] = 'X' - self[YX(y,3)] = 'X' - self[YX(y,4)] = 'X' + self[YX(y, 0)] = 'X' + self[YX(y, 1)] = 'X' + self[YX(y, 2)] = 'X' + self[YX(y, 3)] = 'X' + self[YX(y, 4)] = 'X' elif type(self.geometry) == MapGeometryHex: # TODO: for this to work we need a map side length divisible by 6. - def draw_grid(offset=YX(0,0)): + def draw_grid(offset=YX(0, 0)): dirs = ('DOWNRIGHT', 'RIGHT', 'UPRIGHT', 'RIGHT') def draw_snake(start): @@ -89,20 +89,23 @@ class SaveableMap(Map): if alternate_hex: draw_snake(offset + YX(0, 0)) - draw_snake(offset + YX((0 + alternate_hex) * distance, -int(1.5*distance))) - draw_snake(offset + YX((1 + alternate_hex) * distance, 0)) - draw_snake(offset + YX((2 + alternate_hex) * distance, -int(1.5*distance))) + draw_snake(offset + YX((0 + alternate_hex) * distance, + -int(1.5 * distance))) + draw_snake(offset + YX((1 + alternate_hex) * distance, + 0)) + draw_snake(offset + YX((2 + alternate_hex) * distance, + -int(1.5 * distance))) distance = self.geometry.size.y // 3 draw_grid() - draw_grid(YX(2,0)) - draw_grid(YX(0,2)) - draw_grid(YX(1,0)) - draw_grid(YX(0,1)) - draw_grid(YX(-1,0)) - draw_grid(YX(0,-1)) - draw_grid(YX(-2,0)) - draw_grid(YX(0,-2)) + draw_grid(YX(2, 0)) + draw_grid(YX(0, 2)) + draw_grid(YX(1, 0)) + draw_grid(YX(0, 1)) + draw_grid(YX(-1, 0)) + draw_grid(YX(0, -1)) + draw_grid(YX(-2, 0)) + draw_grid(YX(0, -2)) self.modified = old_modified @@ -121,7 +124,7 @@ class Game(GameBase): self.map_controls = {} self.map_control_passwords = {} self.annotations = {} - self.spawn_point = YX(0,0), YX(0,0) + self.spawn_point = YX(0, 0), YX(0, 0) self.portals = {} self.player_chars = string.digits + string.ascii_letters self.player_char_i = -1 @@ -181,7 +184,7 @@ class Game(GameBase): return self.map_geometry.__class__.__name__[len('MapGeometry'):] def get_player(self, connection_id): - if not connection_id in self.sessions: + if connection_id not in self.sessions: return None player = self.get_thing(self.sessions[connection_id]['thing_id']) return player @@ -300,47 +303,47 @@ class Game(GameBase): def save(self): - def write(f, msg): - f.write(msg + '\n') - - with open(self.io.save_file, 'w') as f: - write(f, 'TURN %s' % self.turn) - map_geometry_shape = self.get_map_geometry_shape() - write(f, 'MAP %s %s' % (map_geometry_shape, self.map_geometry.size,)) - for big_yx in [yx for yx in self.maps if self.maps[yx].modified]: - for y, line in self.maps[big_yx].lines(): - write(f, 'MAP_LINE %s %5s %s' % (big_yx, y, quote(line))) - for big_yx in self.annotations: - for little_yx in self.annotations[big_yx]: - write(f, 'GOD_ANNOTATE %s %s %s' % - (big_yx, little_yx, quote(self.annotations[big_yx][little_yx]))) - for big_yx in self.portals: - for little_yx in self.portals[big_yx]: - write(f, 'GOD_PORTAL %s %s %s' % (big_yx, little_yx, - quote(self.portals[big_yx][little_yx]))) - for big_yx in [yx for yx in self.map_controls - if self.map_controls[yx].modified]: - for y, line in self.map_controls[big_yx].lines(): - write(f, 'MAP_CONTROL_LINE %s %5s %s' % (big_yx, y, quote(line))) - for tile_class in self.map_control_passwords: - write(f, 'MAP_CONTROL_PW %s %s' % (tile_class, - self.map_control_passwords[tile_class])) - for pw in self.admin_passwords: - write(f, 'ADMIN_PASSWORD %s' % pw) - for t in [t for t in self.things if not t.type_ == 'Player']: - write(f, 'THING %s %s %s %s' % (t.position[0], - t.position[1], t.type_, t.id_)) - if hasattr(t, 'name'): - write(f, 'THING_NAME %s %s' % (t.id_, quote(t.name))) - write(f, 'SPAWN_POINT %s %s' % (self.spawn_point[0], - self.spawn_point[1])) + def write(f, msg): + f.write(msg + '\n') + + with open(self.io.save_file, 'w') as f: + write(f, 'TURN %s' % self.turn) + map_geometry_shape = self.get_map_geometry_shape() + write(f, 'MAP %s %s' % (map_geometry_shape, self.map_geometry.size,)) + for big_yx in [yx for yx in self.maps if self.maps[yx].modified]: + for y, line in self.maps[big_yx].lines(): + write(f, 'MAP_LINE %s %5s %s' % (big_yx, y, quote(line))) + for big_yx in self.annotations: + for little_yx in self.annotations[big_yx]: + write(f, 'GOD_ANNOTATE %s %s %s' % + (big_yx, little_yx, quote(self.annotations[big_yx][little_yx]))) + for big_yx in self.portals: + for little_yx in self.portals[big_yx]: + write(f, 'GOD_PORTAL %s %s %s' % (big_yx, little_yx, + quote(self.portals[big_yx][little_yx]))) + for big_yx in [yx for yx in self.map_controls + if self.map_controls[yx].modified]: + for y, line in self.map_controls[big_yx].lines(): + write(f, 'MAP_CONTROL_LINE %s %5s %s' % (big_yx, y, quote(line))) + for tile_class in self.map_control_passwords: + write(f, 'MAP_CONTROL_PW %s %s' % (tile_class, + self.map_control_passwords[tile_class])) + for pw in self.admin_passwords: + write(f, 'ADMIN_PASSWORD %s' % pw) + for t in [t for t in self.things if not t.type_ == 'Player']: + write(f, 'THING %s %s %s %s' % (t.position[0], + t.position[1], t.type_, t.id_)) + if hasattr(t, 'name'): + write(f, 'THING_NAME %s %s' % (t.id_, quote(t.name))) + write(f, 'SPAWN_POINT %s %s' % (self.spawn_point[0], + self.spawn_point[1])) def get_map(self, big_yx, type_='normal'): if type_ == 'normal': maps = self.maps elif type_ == 'control': maps = self.map_controls - if not big_yx in maps: + if big_yx not in maps: maps[big_yx] = SaveableMap(self.map_geometry) if type_ == 'control': maps[big_yx].draw_presets(big_yx.y % 2) @@ -352,9 +355,9 @@ class Game(GameBase): self.annotations = {} self.portals = {} self.admin_passwords = [] - self.spawn_point = YX(0,0), YX(0,0) + self.spawn_point = YX(0, 0), YX(0, 0) self.map_geometry = map_geometry self.map_control_passwords = {'X': 'secret'} - self.get_map(YX(0,0)) - self.get_map(YX(0,0), 'control') + self.get_map(YX(0, 0)) + self.get_map(YX(0, 0), 'control') self.annotations = {} diff --git a/plomrogue/io.py b/plomrogue/io.py index 68f580b..252fac1 100644 --- a/plomrogue/io.py +++ b/plomrogue/io.py @@ -80,9 +80,9 @@ class GameIO(): command(*args, connection_id=connection_id) elif god_mode: command(*args) - #if store and not hasattr(command, 'dont_save'): - # with open(self.game_file_name, 'a') as f: - # f.write(input_ + '\n') + # if store and not hasattr(command, 'dont_save'): + # with open(self.game_file_name, 'a') as f: + # f.write(input_ + '\n') except ArgError as e: answer(connection_id, 'ARGUMENT_ERROR ' + quote(str(e))) except PlayError as e: @@ -99,7 +99,7 @@ class GameIO(): """ if connection_id: for server in self.servers: - if connection_id in server.clients: + if connection_id in server.clients: client = server.clients[connection_id] client.put(msg) else: diff --git a/plomrogue/io_tcp.py b/plomrogue/io_tcp.py index 09b9db1..93fa68e 100644 --- a/plomrogue/io_tcp.py +++ b/plomrogue/io_tcp.py @@ -15,7 +15,7 @@ class PlomSocket: def send(self, message, silent_connection_break=False): """Send via self.socket, encoded/delimited as way recv() expects. - In detail, all \ and $ in message are escaped with prefixed \, + In detail, all \\ and $ in message are escaped with prefixed \\, and an unescaped $ is appended as a message delimiter. Then, socket.send() is called as often as necessary to ensure message is sent fully, as socket.send() due to buffering may @@ -60,7 +60,7 @@ class PlomSocket: In detail, socket.recv() is looped over for sequences of bytes that can be decoded as a Unicode string delimited by an - unescaped $, with \ and $ escapable by \. If a sequence of + unescaped $, with \\ and $ escapable by \\. If a sequence of characters that ends in an unescaped $ cannot be decoded as Unicode, None is returned as its representation. Stop once socket.recv() returns nothing. diff --git a/plomrogue/io_websocket.py b/plomrogue/io_websocket.py index 0a74f4e..bf3766e 100644 --- a/plomrogue/io_websocket.py +++ b/plomrogue/io_websocket.py @@ -43,4 +43,3 @@ class PlomWebSocketServer(SimpleWebSocketServer): def server_close(self): self.close() - diff --git a/plomrogue/mapping.py b/plomrogue/mapping.py index 97f788b..5822dad 100644 --- a/plomrogue/mapping.py +++ b/plomrogue/mapping.py @@ -195,7 +195,7 @@ class Map(): if width_line != width_map: raise ArgError('map line width %s unequal map width %s' % (width_line, width_map)) self.terrain = self.terrain[:y * width_map] + line +\ - self.terrain[(y + 1) * width_map:] + self.terrain[(y + 1) * width_map:] def get_position_index(self, yx): return yx.y * self.geometry.size.x + yx.x @@ -212,7 +212,7 @@ class SourcedMap(Map): def __init__(self, source_maps, source_center, radius, get_map): self.source_maps = source_maps self.radius = radius - example_map = get_map(YX(0,0)) + example_map = get_map(YX(0, 0)) self.source_geometry = example_map.geometry size, self.offset, self.center = \ self.source_geometry.define_segment(source_center, radius) @@ -262,16 +262,16 @@ class DijkstraMap(SourcedMap): if self.terrain[j] < self.terrain[i] - 1: self.terrain[i] = self.terrain[j] + 1 shrunk = True - #print('DEBUG Dijkstra') - #line_to_print = [] - #x = 0 - #for n in self.terrain: - # line_to_print += ['%3s' % n] - # x += 1 - # if x >= self.size.x: - # x = 0 - # print(' '.join(line_to_print)) - # line_to_print = [] + # print('DEBUG Dijkstra') + # line_to_print = [] + # x = 0 + # for n in self.terrain: + # line_to_print += ['%3s' % n] + # x += 1 + # if x >= self.size.x: + # x = 0 + # print(' '.join(line_to_print)) + # line_to_print = [] @@ -281,7 +281,7 @@ class FovMap(SourcedMap): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.terrain = '?' * self.size_i #self.size.y * self.size.x + self.terrain = '?' * self.size_i self[self.center] = '.' self.shadow_cones = [] self.circle_out(self.center, self.shadow_process) @@ -302,7 +302,7 @@ class FovMap(SourcedMap): def in_shadow_cone(new_cone): for old_cone in self.shadow_cones: if old_cone[0] <= new_cone[0] and \ - new_cone[1] <= old_cone[1]: + new_cone[1] <= old_cone[1]: return True # We might want to also shade tiles whose middle arm is inside a # shadow cone for a darker FOV. Note that we then could not for @@ -336,9 +336,9 @@ class FovMap(SourcedMap): if unmerged: self.shadow_cones += [cone] - step_size = (CIRCLE/len(self.circle_out_directions)) / distance_to_center + step_size = (CIRCLE / len(self.circle_out_directions)) / distance_to_center number_steps = dir_i * distance_to_center + dir_progress - left_arm = correct_arm(step_size/2 + step_size*number_steps) + left_arm = correct_arm(step_size / 2 + step_size * number_steps) right_arm = correct_arm(left_arm + step_size) # Optimization potential: left cone could be derived from previous diff --git a/plomrogue/parser.py b/plomrogue/parser.py index 2a87e7d..a043bde 100644 --- a/plomrogue/parser.py +++ b/plomrogue/parser.py @@ -101,8 +101,8 @@ class Parser: tmpl_tokens = signature.split() if len(tmpl_tokens) != len(args_tokens): raise ArgError('Number of arguments (' + str(len(args_tokens)) + - ') not expected number (' + str(len(tmpl_tokens)) - + ').') + ') not expected number (' + str(len(tmpl_tokens)) + + ').') args = [] string_string = 'string' for i in range(len(tmpl_tokens)): diff --git a/plomrogue/tasks.py b/plomrogue/tasks.py index 2e12e5a..558b178 100644 --- a/plomrogue/tasks.py +++ b/plomrogue/tasks.py @@ -70,8 +70,9 @@ class Task_FLATTEN_SURROUNDINGS(Task): pass def do(self): - for yxyx in[self.thing.position] + \ - list(self.thing.game.map_geometry.get_neighbors_yxyx(self.thing.position).values()): + for yxyx in [self.thing.position] + \ + list(self.thing.game.map_geometry.get_neighbors_yxyx( + self.thing.position).values()): if not self.thing.game.can_do_tile_with_pw(*yxyx, self.args[0]): continue self.thing.game.maps[yxyx[0]][yxyx[1]] = '.' @@ -86,8 +87,8 @@ class Task_PICK_UP(Task): raise PlayError('already carrying something') nothing_to_pick_up = True for t in [t for t in self.thing.game.things - if t != self.thing and t.position == self.thing.position - and t.type_ != 'Player']: + if t != self.thing and t.position == self.thing.position and + t.type_ != 'Player']: nothing_to_pick_up = False break if nothing_to_pick_up: diff --git a/plomrogue/things.py b/plomrogue/things.py index a994609..0f058f2 100644 --- a/plomrogue/things.py +++ b/plomrogue/things.py @@ -6,7 +6,7 @@ from plomrogue.mapping import YX class ThingBase: type_ = '?' - def __init__(self, game, id_=0, position=(YX(0,0),YX(0,0))): + def __init__(self, game, id_=0, position=(YX(0, 0), YX(0, 0))): self.game = game if id_ == 0: self.id_ = self.game.new_thing_id() diff --git a/rogue_chat_curses.py b/rogue_chat_curses.py index 80a167e..466c9b7 100755 --- a/rogue_chat_curses.py +++ b/rogue_chat_curses.py @@ -467,8 +467,8 @@ class TUI: def reconnect(self): self.log_msg('@ attempting reconnect') self.send('QUIT') - time.sleep(0.1) # FIXME necessitated by some some strange SSL race - # conditions with ws4py, find out what exactly + # necessitated by some strange SSL race conditions with ws4py + time.sleep(0.1) # FIXME find out why exactly necessary self.switch_mode('waiting_for_server') self.connect() @@ -525,7 +525,7 @@ class TUI: if self.mode.name in {'control_tile_draw', 'control_tile_type', 'control_pw_type'}: self.map_mode = 'protections' - elif self.mode.name!= 'edit': + elif self.mode.name != 'edit': self.map_mode = 'terrain + things' if self.mode.shows_info or self.mode.name == 'control_tile_draw': player = self.game.get_thing(self.game.player_id) @@ -724,7 +724,7 @@ class TUI: if type(self.game.map_geometry) == MapGeometryHex: indent = 0 for line in map_lines_split: - map_lines += [indent*' ' + ''.join(line)] + map_lines += [indent * ' ' + ''.join(line)] indent = 0 if indent else 1 else: for line in map_lines_split: @@ -788,7 +788,7 @@ class TUI: for i in range(self.size.y): safe_addstr(i, self.window_width * (not self.mode.has_input_prompt), - ' '*self.window_width) + ' ' * self.window_width) lines = [] for line in content.split('\n'): lines += msg_into_lines_of_width(line, self.window_width) @@ -816,7 +816,7 @@ class TUI: draw_help() curses.curs_set(False) # hide cursor - curses.use_default_colors(); + curses.use_default_colors() stdscr.timeout(10) reset_screen_size() self.explorer = YX(0, 0) @@ -977,7 +977,7 @@ class TUI: if self.mode.mode_switch_on_key(self, key): continue elif key == self.keys['flatten'] and\ - 'FLATTEN_SURROUNDINGS' in self.game.tasks: + 'FLATTEN_SURROUNDINGS' in self.game.tasks: self.send('TASK:FLATTEN_SURROUNDINGS ' + quote(self.password)) elif key == self.keys['toggle_map_mode']: self.toggle_map_mode()