Bad Code (that works) help me re-write!

MatthewWarren matthew_j_warren at hotmail.com
Wed Oct 11 08:39:07 EDT 2006


Matthew Warren wrote:
> > -----Original Message-----
> > From:
> > python-list-bounces+matthew.warren=digica.com at python.org
> > [mailto:python-list-bounces+matthew.warren=digica.com at python.o
> > rg] On Behalf Of Giles Brown
> > Sent: 11 October 2006 12:38
> > To: python-list at python.org
> > Subject: Re: Bad Code (that works) help me re-write!
> >
> > Matthew Warren wrote:
> > > I have the following piece of code,
> > <snip>
> > No doubt you will get some kind soul to suggest some things,
> > but if you
> > want really good
> > answers I think you need explain why you want this command file (as
> > opposed to using
> > say a python script itself).  Are you attempting to create a simpler
> > syntax than Python?
> > A restricted set of operations?
> >
>
> The code is from a larger project called the FatController.
>
> FatController is currently a cli based utility for administering &
> monitoring devices & applications in an enterprise environment.
>
> Users enter commands at the prompt do things such as, define a managed
> entity, execute instructions against that entity, etc
>
> The processcommand() function and the command file are my attempt at
> implementing the 'commands' that are available to the user. Rather than
> hard-code all of the syntax and commands inside the module, I wanted an
> external file to maintain the definitions of the commands available to
> the user. The idea is that users will be able to implement their own
> 'entity' modules, which FatController will import and then use to manage
> devices & applications the given entty applies to. So, at the time of
> writing the code  thought It would be a good idea to have the command
> definitions file external to the code, so and-users who write their own
> modules can also extend the cammands available to Fatcontroller for
> using those modules, if required.
>
>
>
> Matt.
>

More info.  An entity 'module' is something that creates connections to
an app/device/machine using whatever protocol, sends native commands to
that device/app/machine, and returns the result.  For example, I have
written an entity of type 'TELNET' that manages anything that you can
use telnet to connect to (hence prev. post on using twisted rather than
telnetlib) . The user could enter something like the following at the
Fatcontroller prompt;

FC:> define entity TELNET unixbox1 192.168.4.5 23 mylogin mypass

the user could then write something like;

FC:> execute unixbox1 ps -ef | grep -c crit_process

the processcommand() function and the command definition file are used
to parse/analyse the user input and pass the relevant parms to the
entity module that then defines the entity for later use/managment, or
pass the given entity command to the entity for execution, and return
of the output. Fatcontroller provides other functionality for working
with entities / groups of entities, scheduling commands to be run
against entites / groups of entities and saving / extracting / alerting
against the output.


...but really, all I'm after is a learning experience, (this whole
project is what I decided to do to learn python) and I know
processcommand() can be written in a more elegant way and that eval()
shouldnt be there, but I have no formal acquaintance with things like
parsers and the subtleties of pythons approach etc.. So I'm hoping
through deconstructing and rebulding the code I can increase my
knowledge of python and the pythonic way to do things.

Aaanyhoo, 

Ta :)

Matt.



Matt.




More information about the Python-list mailing list