473,769 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New Module: CommandLoop

This is something I've been working on for a bit, and I think it is
more or less ready to bring up on this list. I'd like to add a module
(though probably not for 2.5).

Before you ask, this module is _not_ compatible with cmd.py, as it is
command oriented, whereas cmd.py is line oriented.

Anyway, I'm looking for feedback, feature requests before starting the
submission process.

Code available here:
http://littlelanguages.com/web/softw...les/cmdloop.py

Base class for writing simple interactive command loop environments.

CommandLoop provides a base class for writing simple interactive user
environments. It is designed around sub-classing, has a simple command
parser, and is trivial to initialize.

Here is a trivial little environment written using CommandLoop:

import cmdloop

class Hello(cmdloop.C ommandLoop):
PS1='hello>'

@cmdloop.aliase s('hello', 'hi', 'hola')
@cmdloop.shorth elp('say hello')
@cmdloop.usage( 'hello TARGET')
def helloCmd(self, flags, args):
'''
Say hello to TARGET, which defaults to 'world'
'''
if flags or len(args) != 1:
raise cmdloop.Invalid Arguments
print 'Hello %s!' % args[0]

@cmdloop.aliase s('quit')
def quitCmd(self, flags, args):
'''
Quit the environment.
'''
raise cmdloop.HaltLoo p

Hello().runLoop ()

Here's a more complex example:

import cmdloop

class HelloGoodbye(cm dloop.CommandLo op):
PS1='hello>'

def __init__(self, default_target = 'world'):
self.default_ta rget = default_target
self.target_lis t = []

@cmdloop.aliase s('hello', 'hi', 'hola')
@cmdloop.shorth elp('say hello')
@cmdloop.usage( 'hello [TARGET]')
def helloCmd(self, flags, args):
'''
Say hello to TARGET, which defaults to 'world'
'''
if flags or len(args) > 1:
raise cmdloop.Invalid Arguments
if args:
target = args[0]
else:
target = self.default_ta rget
if target not in self.target_lis t:
self.target_lis t.append(target )
print 'Hello %s!' % target

@cmdloop.aliase s('goodbye')
@cmdloop.shorth elp('say goodbye')
@cmdloop.usage( 'goodbye TARGET')
def goodbyeCmd(self , flags, args):
'''
Say goodbye to TARGET.
'''
if flags or len(args) != 1:
raise cmdloop.Invalid Arguments
target = args[0]
if target in self.target_lis t:
print 'Goodbye %s!' % target
self.target_lis t.remove(target )
else:
print "I haven't said hello to %s." % target

@cmdloop.aliase s('quit')
def quitCmd(self, flags, args):
'''
Quit the environment.
'''
raise cmdloop.HaltLoo p

def _onLoopExit(sel f):
if len(self.target _list):
self.pushComman ds(('quit',))
for target in self.target_lis t:
self.pushComman ds(('goodbye', target))
else:
raise cmdloop.HaltLoo p

HelloGoodbye(). runLoop()

Feb 20 '06 #1
0 903

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3866
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res = PyArray_FromDimsAndData(1, int*dim, PyArray_DOUBLE, char*buf); Users will get a Numeric Array object and can change its values (and actually change the underlying C array).
5
2018
by: dody suria wijaya | last post by:
I found this problem when trying to split a module into two. Here's an example: ============== #Module a (a.py): from b import * class Main: pass ============== ==============
3
16519
by: David T. Ashley | last post by:
Hi, Red Hat Enterprise Linux 4.X. I'm writing command-line PHP scripts for the first time. I get the messages below. What do they mean? Are these operating system library modules, or something in PHP that I don't have? Do I need to install more Linux packages? Or adjust PHP in some way?
10
5496
by: Bonzol | last post by:
vb.net Hey there, could someone just tell me what the differnce is between classes and modules and when each one would be used compared to the other? Any help would be great Thanx in advance
21
34437
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready equipped for uploading files via the CGI.pm module, which has long been a core module and allows users...
40
3489
by: rjcarr | last post by:
Sorry if this is a completely newbie question ... I was trying to get information about the logging.handlers module, so I imported logging, and tried dir(logging.handlers), but got: AttributeError: 'module' object has no attribute 'handlers' The only experience I have in modules is os and os.path ... if I do the same thing, simply import os and then type dir(os.path), it displays the contents as expected.
4
2182
by: rkmr.em | last post by:
Hi I have a function data, that I need to import from a file data, in the directory data If I do this from python interactive shell (linux fedora core 8) from dir /home/mark it works fine: cwd = data os.chdir(cwd) print os.getcwd()
0
2165
by: Fredrik Lundh | last post by:
Jeff Dyke wrote: so how did that processing use the "mymodulename" name? the calling method has nothing to do with what's considered to be a local variable in the method being called, so that only means that the name is indeed available in the global scope.
6
2271
by: dudeja.rajat | last post by:
Hi, I found on the net that there is something called module initialization. Unfortunately, there is not much information for this. However, small the information I found module initialization can be of use to me in my project. I'm currently messing with a problem where I'm keeping my global variables ( or symbols) in a module and the other mdoules in the project acess these global variables.
0
9416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9850
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8862
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3948
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.