473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

cmd all commands method?

Hi all,

if i want to treat every cmdloop prompt entry as a potential command
then i need to overwrite the default() method ?

What i want to achieve is to be able to support global variable
creation for example;

res = sum 1 2

this would create a variable res with the result of the method
do_sum() ?

then would i be able to run;

sum a 5

this would return 8 or an error saying that res is not defined
Cheers

Feb 17 '07 #1
8 1633

placid wrote:
Hi all,

if i want to treat every cmdloop prompt entry as a potential command
then i need to overwrite the default() method ?

What i want to achieve is to be able to support global variable
creation for example;

res = sum 1 2

this would create a variable res with the result of the method
do_sum() ?

then would i be able to run;

sum a 5
this should have been,

sum res 5
>
this would return 8 or an error saying that res is not defined
Cheers
Feb 17 '07 #2
placid wrote:
if i want to treat every cmdloop prompt entry as a potential
command then i need to overwrite the default() method ?
Excuse me, what's a cmdloop prompt? What's the "default() method"?
What i want to achieve is to be able to support global variable
creation for example;

res = sum 1 2

this would create a variable res with the result of the method
do_sum() ?

then would i be able to run;

sum a 5

this would return 8 or an error saying that res is not defined
Are you sure you're talking about Python here?

Regards,
Björn

--
BOFH excuse #7:

poor power conditioning

Feb 17 '07 #3
On Feb 17, 11:44 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
placid wrote:
if i want to treat every cmdloop prompt entry as a potential
command then i need to overwrite the default() method ?

Excuse me, what's a cmdloop prompt? What's the "default() method"?
What i want to achieve is to be able to support global variable
creation for example;
res = sum 1 2
this would create a variable res with the result of the method
do_sum() ?
then would i be able to run;
sum a 5
this would return 8 or an error saying that res is not defined

Are you sure you're talking about Python here?

Yes, he is talking about the cmd module: http://docs.python.org/dev/lib/Cmd-objects.html.
However that module was never intended as a real interpreter, so
defining variables
as the OP wants would require some work.

Michele Simionato

Feb 18 '07 #4
On Feb 18, 7:17 pm, "Michele Simionato" <michele.simion...@gmail.com>
wrote:
On Feb 17, 11:44 pm, Bjoern Schliessmann <usenet-

mail-0306.20.chr0n...@spamgourmet.comwrote:
placid wrote:
if i want to treat every cmdloop prompt entry as a potential
command then i need to overwrite the default() method ?
Excuse me, what's a cmdloop prompt? What's the "default() method"?
What i want to achieve is to be able to support global variable
creation for example;
res = sum 1 2
this would create a variable res with the result of the method
do_sum() ?
then would i be able to run;
sum a 5
this would return 8 or an error saying that res is not defined
Are you sure you're talking about Python here?

Yes, he is talking about the cmd module:http://docs.python.org/dev/lib/Cmd-objects.html.
However that module was never intended as a real interpreter, so
defining variables
as the OP wants would require some work.

Michele Simionato
How much work does it require ?

Feb 18 '07 #5
placid wrote:
On Feb 18, 7:17 pm, "Michele Simionato" <michele.simion...@gmail.com>
wrote:
>On Feb 17, 11:44 pm, Bjoern Schliessmann <usenet-

mail-0306.20.chr0n...@spamgourmet.comwrote:
placid wrote:
if i want to treat every cmdloop prompt entry as a potential
command then i need to overwrite the default() method ?
Excuse me, what's a cmdloop prompt? What's the "default() method"?
What i want to achieve is to be able to support global variable
creation for example;
res = sum 1 2
this would create a variable res with the result of the method
do_sum() ?
then would i be able to run;
sum a 5
this would return 8 or an error saying that res is not defined
Are you sure you're talking about Python here?

Yes, he is talking about the cmd
module:http://docs.python.org/dev/lib/Cmd-objects.html. However that
module was never intended as a real interpreter, so defining variables
as the OP wants would require some work.

Michele Simionato

How much work does it require ?
Too much. However, here's how far I got:

import cmd
import shlex

DEFAULT_TARGET = "_"

def number(arg):
for convert in int, float:
try:
return convert(arg)
except ValueError:
pass
return arg

class MyCmd(cmd.Cmd):
def __init__(self, *args, **kw):
cmd.Cmd.__init__(self, *args, **kw)
self.namespace = {}
self.target = DEFAULT_TARGET
def precmd(self, line):
parts = line.split(None, 2)
if len(parts) == 3 and parts[1] == "=":
self.target = parts[0]
return parts[2]
self.target = DEFAULT_TARGET
return line
def resolve(self, arg):
args = shlex.split(arg)
result = []
for arg in args:
try:
value = self.namespace[arg]
except KeyError:
value = number(arg)
result.append(value)
return result
def calc(self, func, arg):
try:
result = self.namespace[self.target] = func(self.resolve(arg))
except Exception, e:
print e
else:
print result

def do_sum(self, arg):
self.calc(sum, arg)
def do_max(self, arg):
self.calc(max, arg)
def do_print(self, arg):
print " ".join(str(arg) for arg in self.resolve(arg))
def do_values(self, arg):
pairs = sorted(self.namespace.iteritems())
print "\n".join("%s = %s" % nv for nv in pairs)
def do_EOF(self, arg):
return True

if __name__ == "__main__":
c = MyCmd()
c.cmdloop()

Peter

Feb 18 '07 #6
On Feb 18, 10:49 am, "placid" <Bul...@gmail.comwrote:
On Feb 18, 7:17 pm, "Michele Simionato" <michele.simion...@gmail.com>
Yes, he is talking about the cmd module:http://docs.python.org/dev/lib/Cmd-objects.html.
However that module was never intended as a real interpreter, so
defining variables
as the OP wants would require some work.
Michele Simionato

How much work does it require ?
Have you ever written an interpreter? It is a nontrivial job.

Michele Simionato

Feb 18 '07 #7
On Feb 18, 8:59 pm, "Michele Simionato" <michele.simion...@gmail.com>
wrote:
On Feb 18, 10:49 am, "placid" <Bul...@gmail.comwrote:
On Feb 18, 7:17 pm, "Michele Simionato" <michele.simion...@gmail.com>
Yes, he is talking about the cmd module:http://docs.python.org/dev/lib/Cmd-objects.html.
However that module was never intended as a real interpreter, so
defining variables
as the OP wants would require some work.
Michele Simionato
How much work does it require ?

Have you ever written an interpreter? It is a nontrivial job.

Michele Simionato
No i have never written an interpreter and i can just imagine how much
work/effort is needed to write something like that.

If anyone can provide a suggestion to replicate the following Tcl
command in Python, i would greatly appreciate it.

namespace eval foo {
variable bar 12345
}

what this does is create a namespace foo with the variable bar set to
12345.

http://aspn.activestate.com/ASPN/doc...d/variable.htm

The code provided by Peter Otten is a good start for me. Cheers for
that!
Cheers

Feb 19 '07 #8
En Mon, 19 Feb 2007 00:08:45 -0300, placid <Bu****@gmail.comescribió:
If anyone can provide a suggestion to replicate the following Tcl
command in Python, i would greatly appreciate it.

namespace eval foo {
variable bar 12345
}

what this does is create a namespace foo with the variable bar set to
12345.
Python namespaces are simple dictionaries. See the eval function.

pys = "3+x**2"
pyfreevars = {"x": 2}
pyeval(s, {}, freevars)
7

--
Gabriel Genellina

Feb 20 '07 #9

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

Similar topics

4
by: ÂÑTØÑ | last post by:
Hi, I was looking for a list of commands, but I can't find it. It's about commands you can type in the Internet Explorer adress bar, to get some information about a website. For instance...
3
by: baileystuff | last post by:
I have an issue where I have three Cases that SHOULD act the same but don't. If you look at the code below you will see that the general premise is to open a MSWord template and then use SendKey...
15
by: Madhanmohan S | last post by:
Hi All, I want to run a command line appplication from C#. When i start the application, it will go into specific mode. After that, i have to give commands to use the application. How Can This Be...
0
by: Ghost | last post by:
Hello. I have such question: I want to create SqlCeDataAdapter object at runtime... I'm writing: SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter(); then I specify Select Command:...
7
by: Rex Winn | last post by:
I've Googled until my eyes hurt looking for a way to issue Telnet commands from C# and cannot find anything but $300 libraries that encapsulate it for you. I don't want to be able to create a...
28
by: Tim_Mac | last post by:
hi, i'm new to .net 2.0, and am just starting to get to grips with the gridview. my page has autoEventWireUp set to true, which i gather is supposed to figure out which handlers to invoke when...
8
by: Joanna Carter [TeamB] | last post by:
Hi Folks I am just trying to get my head around whether I can use a single SQLConnection for the life of the application or whether I should create it only when needed. I want to create...
6
by: Peted | last post by:
Hi wondering what is the best way to do this Need a user to click a button, that sends 3 or 4 string based commands via a TCP/ip socket link I can connect to the ip device no problems, am...
15
by: tmp123 | last post by:
Hello, Thanks for your time. We have very big files with python commands (more or less, 500000 commands each file). It is possible to execute them command by command, like if the commands...
0
by: rhyspatto | last post by:
Hi, I have a problem but cannot figure out a good solution to it. I am trying to create a form that creates a bunch of panels during runtime, and I want to be able to send commands (such as false...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.