Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6844 > unrolled thread

help me reviewing and organizing my code =)

Started byTracubik <affdfsdfdsfsd@b.com>
First post2011-06-02 07:31 +0000
Last post2011-06-02 12:41 +0200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  help me reviewing and organizing my code =) Tracubik <affdfsdfdsfsd@b.com> - 2011-06-02 07:31 +0000
    Re: help me reviewing and organizing my code =) Chris Angelico <rosuav@gmail.com> - 2011-06-02 18:04 +1000
    Re: help me reviewing and organizing my code =) Peter Otten <__peter__@web.de> - 2011-06-02 12:41 +0200

#6844 — help me reviewing and organizing my code =)

FromTracubik <affdfsdfdsfsd@b.com>
Date2011-06-02 07:31 +0000
Subjecthelp me reviewing and organizing my code =)
Message-ID<4de73c5d$0$38638$4fafbaef@reader1.news.tin.it>
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?

help for your thanks
thanks for your help

Nico



[toc] | [next] | [standalone]


#6846

FromChris Angelico <rosuav@gmail.com>
Date2011-06-02 18:04 +1000
Message-ID<mailman.2384.1307001873.9059.python-list@python.org>
In reply to#6844
On Thu, Jun 2, 2011 at 5:31 PM, Tracubik <affdfsdfdsfsd@b.com> wrote:
>    UNAME_CODE = ['uname']
>    LS_CODE = ['cd /home/myUserId/Images/SashaGray',
>               'ls *.jpg']
>
>    command_list = {
>    "uname" : UNAME_CODE,
>    "ls"    : LS_CODE
>    }
>
> 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?

I'd dispense with the indirection and simply build the dictionary as a
single literal. Beyond that, I won't advise, as you're using a
framework I'm not overly familiar with - others will be better placed
to give recommendations.

Chris Angelico

[toc] | [prev] | [next] | [standalone]


#6852

FromPeter Otten <__peter__@web.de>
Date2011-06-02 12:41 +0200
Message-ID<mailman.2386.1307011325.9059.python-list@python.org>
In reply to#6844
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web