From 99b6bb722bf06559210a4558fc0a432e2d38aa10 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Fri, 29 Aug 2014 16:01:44 +0000 Subject: [PATCH] #665 use enumerate() construct to simplify code git-svn-id: https://xpra.org/svn/Xpra/trunk@7484 3bb7dfac-3a0b-4e04-842a-767bc560f471 --- src/xpra/client/gtk_base/client_launcher.py | 4 +--- src/xpra/client/gtk_base/gtk_tray_menu_base.py | 4 +--- src/xpra/gtk_common/graph.py | 8 ++------ src/xpra/net/protocol.py | 4 +--- src/xpra/scripts/server.py | 4 +--- src/xpra/server/server_base.py | 4 +--- src/xpra/server/server_core.py | 11 +++-------- src/xpra/server/video_subregion.py | 4 +--- src/xpra/server/window_video_source.py | 4 +--- src/xpra/util.py | 12 +++--------- src/xpra/x11/server.py | 4 +--- src/xpra/x11/x11_server_base.py | 4 +--- 12 files changed, 17 insertions(+), 50 deletions(-) diff --git a/src/xpra/client/gtk_base/client_launcher.py b/src/xpra/client/gtk_base/client_launcher.py index 65b758b351..b669f28ac6 100755 --- a/src/xpra/client/gtk_base/client_launcher.py +++ b/src/xpra/client/gtk_base/client_launcher.py @@ -82,12 +82,10 @@ def validate_in_list(x, options): def get_active_item_index(optionmenu): - i = 0 menu = optionmenu.get_menu() - for x in menu.get_children(): + for i, x in enumerate(menu.get_children()): if hasattr(x, "get_active") and x.get_active(): return i - i += 1 return -1 def set_history_from_active(optionmenu): diff --git a/src/xpra/client/gtk_base/gtk_tray_menu_base.py b/src/xpra/client/gtk_base/gtk_tray_menu_base.py index 9d269ce0c6..b57eebed39 100644 --- a/src/xpra/client/gtk_base/gtk_tray_menu_base.py +++ b/src/xpra/client/gtk_base/gtk_tray_menu_base.py @@ -148,8 +148,7 @@ def populate_encodingsmenu(encodings_submenu, get_current_encoding, set_encoding encodings_submenu.index_to_encoding = {} encodings_submenu.encoding_to_index = {} NAME_TO_ENCODING = {} - i = 0 - for encoding in encodings: + for i, encoding in enumerate(encodings): name = ENCODINGS_TO_NAME.get(encoding, encoding) descr = ENCODINGS_HELP.get(encoding) NAME_TO_ENCODING[name] = encoding @@ -173,7 +172,6 @@ def encoding_changed(oitem): encodings_submenu.append(encoding_item) encodings_submenu.index_to_encoding[i] = encoding encodings_submenu.encoding_to_index[encoding] = i - i += 1 encodings_submenu.show_all() diff --git a/src/xpra/gtk_common/graph.py b/src/xpra/gtk_common/graph.py index 1b4c959434..550526991e 100644 --- a/src/xpra/gtk_common/graph.py +++ b/src/xpra/gtk_common/graph.py @@ -127,9 +127,8 @@ def make_graph_pixmap(data, labels=None, width=320, height=200, title=None, context.set_line_width(0.0) context.rectangle(x_offset, y_offset, x_offset+w, y_offset+h) context.clip() - i = 0 context.set_line_width(1.5) - for line_data in data: + for i, line_data in enumerate(data): colour = colours[i % len(colours)] context.set_source_rgb(*colour) j = 0 @@ -163,10 +162,8 @@ def make_graph_pixmap(data, labels=None, width=320, height=200, title=None, last_v = v, x, y j += 1 context.stroke() - i += 1 context.restore() - i = 0 - for line_data in data: + for i, line_data in enumerate(data): #show label: if labels and len(labels)>i: label = labels[i] @@ -177,5 +174,4 @@ def make_graph_pixmap(data, labels=None, width=320, height=200, title=None, context.move_to(x_offset/2+(width-x_offset)*i/len(labels), height-4) context.show_text(label) context.stroke() - i += 1 return pixmap diff --git a/src/xpra/net/protocol.py b/src/xpra/net/protocol.py index 3336c0d446..95f59e1ff9 100644 --- a/src/xpra/net/protocol.py +++ b/src/xpra/net/protocol.py @@ -331,10 +331,8 @@ def new_tree(append): if packet is None: return err("None value") if type(packet)==list: - i = 0 - for x in packet: + for i, x in enumerate(packet): self.do_verify_packet(new_tree("[%s]" % i), x) - i += 1 elif type(packet)==dict: for k,v in packet.items(): self.do_verify_packet(new_tree("key for value='%s'" % str(v)), k) diff --git a/src/xpra/scripts/server.py b/src/xpra/scripts/server.py index d4e2485177..a4187731b9 100644 --- a/src/xpra/scripts/server.py +++ b/src/xpra/scripts/server.py @@ -148,14 +148,12 @@ def get_info(self): info = {"children" : len(d), "children.dead" : len(self._dead_pids), "children.ignored" : len(self._ignored_pids)} - i = 0 - for pid in sorted(d.keys()): + for i, pid in enumerate(sorted(d.keys())): proc = d[pid] info["child[%i].live" % i] = pid not in self._dead_pids info["child[%i].pid" % i] = pid info["child[%i].command" % i] = proc.command info["child[%i].ignored" % i] = pid in self._ignored_pids - i += 1 return info diff --git a/src/xpra/server/server_base.py b/src/xpra/server/server_base.py index a54755d928..b1fa48c29b 100644 --- a/src/xpra/server/server_base.py +++ b/src/xpra/server/server_base.py @@ -1121,15 +1121,13 @@ def up(prefix, d, suffix=""): up("client", ss.get_info()) info.update(ss.get_window_info(window_ids)) elif n>1: - i = 0 - for ss in server_sources: + for i, ss in enumerate(server_sources): up("client[%i]" % i, ss.get_info()) wi = ss.get_window_info(window_ids) up("client[%i]" % i, wi) #this means that the last source overrides previous ones #(bad decision was made on the namespace for this..) info.update(wi) - i += 1 return info def add_windows_info(self, info, window_ids): diff --git a/src/xpra/server/server_core.py b/src/xpra/server/server_core.py index 4275d5dc1a..51b6898980 100644 --- a/src/xpra/server/server_core.py +++ b/src/xpra/server/server_core.py @@ -58,17 +58,12 @@ def get_thread_info(proto=None): "count" : threading.active_count() - len(info_threads), "info.count" : len(info_threads) } - i = 0 #threads used by the "info" client: - for t in info_threads: + for i, t in enumerate(info_threads): info["info[%s]" % i] = t.getName() - i += 1 - i = 0 #all non-info threads: - for t in threading.enumerate(): - if t not in info_threads: - info[str(i)] = t.getName() - i += 1 + for i, t in enumerate((x for x in threading.enumerate() if x not in info_threads)): + info[str(i)] = t.getName() #platform specific bits: try: from xpra.platform.info import get_sys_info diff --git a/src/xpra/server/video_subregion.py b/src/xpra/server/video_subregion.py index ef5993c5dc..c7b876007b 100644 --- a/src/xpra/server/video_subregion.py +++ b/src/xpra/server/video_subregion.py @@ -68,10 +68,8 @@ def get_info(self): "non_max_wait" : self.non_max_wait} rr = list(self.refresh_regions) if rr: - i = 0 - for r in rr: + for i, r in enumerate(rr): info["refresh_region[%s]" % i] = (r.x, r.y, r.width, r.height) - i += 1 return info diff --git a/src/xpra/server/window_video_source.py b/src/xpra/server/window_video_source.py index 3c3bc2eee4..fbc28f3c47 100644 --- a/src/xpra/server/window_video_source.py +++ b/src/xpra/server/window_video_source.py @@ -160,10 +160,8 @@ def up(prefix, d): up("encoding.pipeline_param", self.get_pipeline_info()) lps = self.last_pipeline_scores if lps: - i = 0 - for lp in lps: + for i, lp in enumerate(lps): up("encoding.pipeline_option[%s]" % i, self.get_pipeline_score_info(*lp)) - i += 1 return info def get_pipeline_info(self): diff --git a/src/xpra/util.py b/src/xpra/util.py index 0a4a74a648..7b5145a2f7 100644 --- a/src/xpra/util.py +++ b/src/xpra/util.py @@ -261,9 +261,7 @@ def do_log_screen_sizes(root_w, root_h, sizes): #log position if not (0, 0) info.append("at %sx%s" % (work_x, work_y)) log.info(" "+" ".join(info)) - i = 0 - for m in monitors: - i += 1 + for i, m in enumerate(monitors, start=1): if len(m)<7: log.info(" %s", m) continue @@ -284,8 +282,7 @@ def get_screen_info(screen_sizes): info = { "screens" : len(screen_sizes) } - i = 0 - for x in screen_sizes: + for i, x in enumerate(screen_sizes): if type(x) not in (tuple, list): #legacy clients: info["screen[%s]" % i] = str(x) @@ -297,8 +294,7 @@ def get_screen_info(screen_sizes): info["screen[%s].size_mm" % i] = x[3], x[4] if len(x)>=6: monitors = x[5] - j = 0 - for monitor in monitors: + for j, monitor in enumerate(monitors): if len(monitor)>=7: for k,v in { "name" : monitor[0], @@ -306,10 +302,8 @@ def get_screen_info(screen_sizes): "size_mm" : monitor[5:7], }.items(): info["screen[%s].monitor[%s].%s" % (i, j, k)] = v - j += 1 if len(x)>=10: info["screen[%s].workarea" % i] = x[6:10] - i += 1 return info diff --git a/src/xpra/x11/server.py b/src/xpra/x11/server.py index e11530a4fa..10ff020e41 100644 --- a/src/xpra/x11/server.py +++ b/src/xpra/x11/server.py @@ -239,12 +239,10 @@ def do_get_info(self, proto, server_sources, window_ids): else: info["cursor.is_default"] = bool(self.default_cursor_data and len(self.default_cursor_data)>=8 and len(cd)>=8 and cd[7]==cd[7]) #all but pixels: - i = 0 - for x in ("x", "y", "width", "height", "xhot", "yhot", "serial", None, "name"): + for i, x in enumerate(("x", "y", "width", "height", "xhot", "yhot", "serial", None, "name")): if x: v = cd[i] or "" info["cursor." + x] = v - i += 1 return info def get_ui_info(self, proto, wids, *args): diff --git a/src/xpra/x11/x11_server_base.py b/src/xpra/x11/x11_server_base.py index c573b74069..698d58438a 100644 --- a/src/xpra/x11/x11_server_base.py +++ b/src/xpra/x11/x11_server_base.py @@ -407,15 +407,13 @@ def delfile(msg): "%s" % len(monitors)] #the new config (numeric values only) config = [len(monitors)] - i = 0 - for m in monitors: + for i, m in enumerate(monitors): if len(m)<7: return delfile("cannot save fake xinerama settings: incomplete monitor data for monitor: %s" % m) plug_name, x, y, width, height, wmm, hmm = m[:8] data.append("# %s (%smm x %smm)" % (prettify_plug_name(plug_name, "monitor %s" % i), wmm, hmm)) data.append("%s %s %s %s" % (x, y, width, height)) config.append((x, y, width, height)) - i += 1 data.append("") contents = "\n".join(data) for filename in xinerama_files: