Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'executing': 0.05; 'dictionary': 0.07; 'json': 0.07; 'python': 0.08; 'bash': 0.09; 'data:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'def': 0.12; 'gui': 0.14; 'wrote:': 0.14; '40+': 0.16; 'buttons': 0.16; 'command)': 0.16; 'module?': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:organizing': 0.16; 'subject:reviewing': 0.16; 'suggest': 0.19; 'command': 0.19; 'subject:help': 0.20; 'subject:code': 0.23; 'skip:b 20': 0.23; 'load': 0.24; 'code': 0.24; 'script': 0.27; "i'm": 0.27; 'skip:p 30': 0.28; 'import': 0.29; 'like,': 0.29; 'unicode': 0.29; 'class': 0.29; 'instead': 0.29; 'module': 0.30; 'from:addr:web.de': 0.30; 'objects.': 0.30; 'print': 0.31; 'header:X-Complaints-To:1': 0.32; 'to:addr:python-list': 0.33; 'it?': 0.33; "i've": 0.33; '...': 0.34; "i'll": 0.34; 'file': 0.34; 'skip:o 20': 0.37; 'another': 0.37; 'received:org': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'skip:s 20': 0.39; 'header:Mime- Version:1': 0.39; 'to:addr:python.org': 0.39; 'simply': 0.60; 'making': 0.67; 'skip:/ 30': 0.67; 'become': 0.72 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: help me reviewing and organizing my code =) Date: Thu, 02 Jun 2011 12:41:54 +0200 Organization: None References: <4de73c5d$0$38638$4fafbaef@reader1.news.tin.it> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084a02d.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 89 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307011325 news.xs4all.nl 49042 [::ffff:82.94.164.166]:57229 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6852 Tracubik wrote: > if you like, off course :) > > I'm making a port in python of a program made of bash commands + zenity > for the GUI. > so, i've to re-create a GUI in pyGTK and associate the right bash commands > to the buttons. > Instead of executing the bash script i simply print they in the console. > > so, here's my code > > import gtk > > class myProgram: > > def __init__(self): > ... > self.btnUname = gtk.Button("uname") > self.btnLs = gtk.Button("ls") > > self.btnUname.connect("clicked", self.print_associated_command, > "uname") self.btnLs.connect("clicked", self.print_associated_command, > "ls") ... > > def print_associated_command(self, widget, data=None): > UNAME_CODE = ['uname'] > LS_CODE = ['cd /home/myUserId/Images/SashaGray', > 'ls *.jpg'] > > command_list = { > "uname" : UNAME_CODE, > "ls" : LS_CODE > } > for item in command_list[data]: > print 'COMMAND: ' + item > print '-----------------------------------------------------' > > > do you like it? > considering i'll have about 40+ buttons, do you suggest me to move some > part of code outside in a different module? If all buttons work the same you can treat them uniformly: import gtk command_defs = { "uname" : ['uname'], "ls" : ['cd /home/myUserId/Images/EarlGray', 'ls *.jpg'] } class MyProgram: def __init__(self): ... for name, command in command_defs.iteritems(): button = gtk.Button(name) button.connect("clicked", self.print_associated_command, command) button.show() ... ... def print_associated_command(self, widget, data=None): for item in data: print 'COMMAND:', item print '-----------------------------------------------------' You can then move the command_defs dictionary into another module or alternatively turn it into a JSON file and load it with the json module from the standard library: import json with open("command_defs.json") as f: command_defs = json.load(f) The corresponding JSON file would then look like { "uname" : ["uname"], "ls" : ["cd /home/myUserId/Images/EarlGray", "ls *.jpg"] } Note that with this approach all strings become unicode objects.