473,404 Members | 2,170 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,404 software developers and data experts.

Print String

Hello Eveybody,

I have written a method which prints the prefix notation of any expression.

here is the method...

def PrintPrefix(expr):
if expr.__class__==E:
print expr.operator,
PrintPrefix(expr.left),
PrintPrefix(expr.right),
else:
print expr,

Now if i pass an expression to it like e1=x+y+z:

I call it by passing PrintPrefix(e1):
The output is ++xyz

now i want to store this in a string...

Can anyone help....

Balaji
Jul 18 '05 #1
2 1993
I'd try something along these lines:
def GeneratePrefix(expr):
if expr.__class__ == E:
yield str(expr.operator)
for i in GeneratePrefix(expr.left): yield i
for i in GeneratePrefix(expr.right): yield i
else:
yield str(expr)
The successive elements produced by this (untested) code should be the
same as the elements printed by your code.

To join these into a string without any whitespace, you could write
def StringPrefix(expr):
return "".join(GeneratePrefix(expr))
and you could re-write the original PrintPrefix as
def PrintPrefix(expr):
for item in expr:
print item,

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFAvMn/Jd01MZaTXX0RAgZnAKCUjKB5SigE/kXtjCVs4jNfRlsx2wCfcJkS
3LeJ0Tbl0hYuAa5aMUiRIa0=
=KSG8
-----END PGP SIGNATURE-----

Jul 18 '05 #2
Balaji wrote:
I have written a method which prints the prefix notation of any
expression.

here is the method...
The convention is to call it "method" only if it is part of a class, so what
you show is a "function".
def PrintPrefix(expr):
if expr.__class__==E:
Explicit class checks are a bad idea. I'd rather let the class decide how it
wants to print or convert an instance to string - i. e. I would prefer
methods over a function to implement this feature.
print expr.operator,
PrintPrefix(expr.left),
PrintPrefix(expr.right),
else:
print expr,

Now if i pass an expression to it like e1=x+y+z:

I call it by passing PrintPrefix(e1):
The output is ++xyz

now i want to store this in a string...


Below is a minimal example that uses __str__() (the method that is
implicitely called by the str() function) to produce infix notation and
__repr__() (corresponding to repr()) for prefix notation. Just say

print repr(e)

to print in prefix notation and

s = repr(e)

to store it as a string.

class Base(object):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
__repr__ = __str__
def __add__(self, other):
return Expression(Operator("+"), self, other)

class Operator(Base):
pass

class Variable(Base):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name

class Expression(Base):
def __init__(self, operator, left, right):
self.operator = operator
self.left = left
self.right = right
def __str__(self):
""" infix """
return "%s%s%s" % (self.left, self.operator, self.right)
def __repr__(self):
""" prefix """
return "%r%r%r" % (self.operator, self.left, self.right)

x = Variable("x")
y = Variable("y")
z = Variable("z")
e = x + y + z
print repr(e)
print str(e)
Peter

Jul 18 '05 #3

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

Similar topics

6
by: Ruchika | last post by:
Hi, I am new to Python, so this may be a very simple question for most of you. What does the % operator stand for in Python? I came across a script that uses the % operator as follows - def...
0
by: David Bolen | last post by:
I ran into this strange behavior when noticing some missing spaces in some debugging output. It seems that somewhere in the print processing, there is special handling for string contents that...
14
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version:...
16
by: John Baker | last post by:
HI; I feel like a fool..I put CUTE FTP in my last request for help --it should have been CUTE PDF! I have FTP on my mind because I have been working on a web based application, and somehow my...
2
by: Dean Slindee | last post by:
Anybody written code in VB.NET to: 1) show a print preview window of reports already written and stored in an Access 2002 database; or 2) execute the print of a report stored in an Access 2002...
1
by: Michael Beck | last post by:
I need to select one of about 15 printers, which I have been able to do. Then I need to set that printer as the printer to use, run a Crystal Reports reports, and track if/when the printing job...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
3
by: James J. Besemer | last post by:
I would like to champion a proposed enhancement to Python. I describe the basic idea below, in order to gage community interest. Right now, it's only an idea, and I'm sure there's room for...
2
by: Phoe6 | last post by:
print and softspace in python In python, whenever you use >>>print statement it will append a newline by default. If you don't want newline to be appended, you got use a comma at the end (>>>print...
3
by: Tim | last post by:
Folks, I'm trying to format a print string so that it reports progress whilst processing a looping structure with a date time stamp appended to the end of the string. This started out life as a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...

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.