print <<EOF;
...reams of text goes here...
EOF
Obviously this has great use when chugging out volumes of static HTML
with intermittent bursts of dynamic content. However, with Python I find
my self having to wrap all that text in quotes (often alternating between
double and single to get around double quotes in tags and single quotes
in posessive words), doing strange combinations of line continuations,
and splitting lines in unasthetic ways until the static HTML is no
longer readable.
Is there a Python equivalent of the above Perl code so I can preserve my
content _and_ use Python for the dynamic stuff?
-Walt Stoneburner, <w...@wwco.com>
print """...reams of text goes here...
...and here... with this'es and that"es
...and here"""
Triple quotes are handy. They can also be written '''.
See: http://www.python.org/doc/tut/node70.html
Cheers,
--david
PS: See also: http://maigret.cog.brown.edu/jak/cookbook.html for a
Perl/Python phrasebook.
David_...@Brown.EDU (401) 863-3926
# Python is my favorite language: http://www.python.org #
Of course this tends to degenerate to the point where your
Python-embedded HTML documents no longer contain HTML... :^)
--
Kent Polk: ke...@eaanu.nde.swri.edu
> I've switched almost totally from Perl to Python now, but there's one
> thing holding me back. Perl has the ability to do the following:
>
> print <<EOF;
> ...reams of text goes here...
> EOF
>
> Obviously this has great use when chugging out volumes of static HTML
> with intermittent bursts of dynamic content. However, with Python I find
> my self having to wrap all that text in quotes (often alternating between
> double and single to get around double quotes in tags and single quotes
> in posessive words), doing strange combinations of line continuations,
> and splitting lines in unasthetic ways until the static HTML is no
> longer readable.
>
> Is there a Python equivalent of the above Perl code so I can preserve my
> content _and_ use Python for the dynamic stuff?
Triple-quoted strings are what ypu'e looking for. Between two sets of """
you can put any chunk of bytes (including line-trunaround) you like. For
example.
simple_web_page = """\
Content-type: text/html
<html>
<head>
<title>A simple web page demonstrating tripl-quoted strings</title>
</head>
<body>
<h1>Slurping gobs of text</h1>
This should show you how you can do
something similar to (perl|sh)-style <<'s in python.
<p>
<address>
<a href="mailto:ni...@osg.saic.com">nick</a>
</address>
</body>
</html>
"""
print simple_web_page
> I am living a perl-free life and am a much, much
> happier person for it.
Amen, brother.
> You can, too. Just admit that perl is an
> inciduous, intoxicating and highly destructive
> addiction, break that addiction, and move on.
I feel the power of the Lord! Hallelujah!
Actually, even though I have used Perl for a significant number of
projects over the years, I was never addicted to it. The minute I had
to fix a script I wrote three months ago, I knew Perl was not going to
be an addiction. Besides, I could have quit any time, yeah yeah.
- Chris
ps - Go Pack!!!
In your case, you can do:
print """
reams of text
"""
Now-- the two things:
1) the argument processing module getopt sucks. Even the one in 1.4. I
wrote a much better one based on Perl's GetOpt::Long.... full support
for all of the GNU style CLI option processing.
http://www.friday.com/~bbum/py-stuff/
Ignore the formatting of the pages. They are ugly on purpose.
2) Python does not have formatted output support like perl. Ie; draw a
line-oriented picture of what your output should look like and 'write'
to it... now, while perl DOES have this functionality-- as usual-- the
implementation is horribly ugly, confusing, and just plain grotesque.
I haven't had time to re-implement this feature in python...
That's it.
I am living a perl-free life and am a much, much happier person for it.
You can, too. Just admit that perl is an inciduous, intoxicating and
highly destructive addiction, break that addiction, and move on. Most
of us have been guilty of that sin at some points in our life, so your
admission to said sin will not lead to any kind of persecution...
Perl is Pure Evil, Raving Lunacy -- strike it from your life before you,
too, succcumb to producing unreadable code that will make future
generations nauseous when they attempt to maintain it.
:-)
b.bum
That's not true, Robin Friedrich has posted such a module a few months
ago:
Goto http://search.dejanews.com/ and search for `ColumnReportTemplate':
(http://search.dejanews.com/query_profile.xp?query=ColumnReportTemplate&nofilt=1)
Michael
--
''''\ Michael Scharf
` c-@@ TakeFive Software
` > http://www.TakeFive.com
\_ V mailto:Michael...@TakeFive.co.at
:) On my own NetStijl pages, which does insert Python generated code
into an otherwise-HTML document, I noticed that I was making many that
only had the HTML <HEAD> section for the title and the
importing/embedding of code, and the <BODY> section consisted of only
one PYML specific markup.
But, I am working on a new tool that lets me use the Python/HTML
components I made outside of the document (to free myself of the extra
parser overhead for those single-call pages). What makes it doubly nice
though is that I can still embed the code inside of a page too. Thus:
<PYML OBJ="CoolClass" METHOD="evenCooler" WIDTH=600></PYML>
can now me gotten through CGI
.../cgi-bin/apog?aobj=path1.spam_module.CoolClass.evenCooler
As soon as I get in and work on a lot of the error-handling plumbing,
web development will be a cinch!
--
.jPS || je...@cynapses.com || http://www.cynapses.com/
netStijl Internet Objects || http://www.cynapses.com/netstijl/
> print <<EOF;
> ...reams of text goes here...
> EOF
(hmm, I thought that was a shell feature ;-)
Try triple quotes:
print """\
..reams of text goes here
"""
The backslash on the first line are there to get rid of the leading
newline; everything inside the triple quotes are added to the string.
/F
Is there enough interest here in starting a PYTHON-12STEP sig for
perl programmers?
Bill B. wrote:
BILL>I am living a perl-free life and am a much happier person for it.
BILL>You can, too. Just admit that perl is an inciduous, intoxicating
BILL>and highly destructive addiction, break that addiction...
I haven't been able to kick :-(
paul
Paul Taney, pta...@usgs.gov @ All have we got it seem we have lost
smail: ms431, reston,va 22092 @ We must have 'ready paid the cost.
voice: 703-648-5598 @ -- Marley
Interesting. I took a different approach in that the embedded parser
also contains many typical objects and functions I need in the same
module to cut down on overhead. The embedder can either contain sections
of Python code (including importing modules, etc) as well as being able
to insert variables and function calls directly in the HTML outside of
Python code. Most of my pages are built by calling one function with a
dictionary for the title, main headers, negotiation buttons, included
documents, etc. thus most of my 'pages' are a few lines of a dictionary
which is used to build the documents from various parts and pieces which
are easier to maintain separately (such as remote database queries for
periodic document updates). I then set up the server to route requests
for *.html to the embeddeder module, which extracts the document name from
the server and parses/ evaluates it. It probably isn't as general purpose
as yours, but it does what I need pretty efficiently. :^)
I looked at your stuff a while back when I was trying to figure out how to
serve the pages live without using the typical CGI mechanism (since I wanted
it to appear to be a static page), but you only had static page generation
at the time and it wasn't quite where I needed to go. I'm still experimenting
with the technique I use, but it works and it works darned fast too. Makes
maintaining hundreds of pages of 'html' documents almost trivial. Also allows
neat things like database queries without the use of a form and contest-
sensitive help (via the search engine) in the URI.
What's really neat about doing things on the fly (they ways we are) is that
it opens up many new possibilities - for just a tiny bit of extra overhead
(if you are careful). I wanted to use the PyApache PythonPersistent and
PythonTrustedScripts in order to keep the embedder and required modules
loaded, but PyApache often can't locate the required modules when I turn on
those options. :^( Haven't figured out what the problem is yet. If I could
get that working properly I'd reorganise my modules to be more general-purpose
but it's too big of a performance hit right now (IMHO).
BTW, 'live' documents can easily be made HTTP1.1-compliant on the fly
in a trivial fashion if the result is built as a string - len(result).
--
Kent Polk: Southwest Research Institute (will disavow any knowledge of me)
Internet : ke...@eaenki.nde.swri.edu
BUT... doesn't the perl one do dynamical stuff to the content between the
EOF's (ala sh)??? The """ option is only a long, unprocessed string...
Walt, this behaviour (dynamic processing) is what you were after, right?
(Me too).
David Ascher wrote:
> Walt Stoneburner wrote:
> > I've switched almost totally from Perl to Python now, but there's one
> > thing holding me back. Perl has the ability to do the following:
> >
> > print <<EOF;
> > ...reams of text goes here...
> > EOF
> > [...]
> > Is there a Python equivalent of the above Perl code so I can preserve my
> > content _and_ use Python for the dynamic stuff?
>
> print """...reams of text goes here...
> ...and here... with this'es and that"es
> ...and here"""
>
> Triple quotes are handy. They can also be written '''.
>
> See: http://www.python.org/doc/tut/node70.html
>
> Cheers,
>
> --david
>
> PS: See also: http://maigret.cog.brown.edu/jak/cookbook.html for a
> Perl/Python phrasebook.
>
> David_...@Brown.EDU (401) 863-3926
> # Python is my favorite language: http://www.python.org #
--
Cheers,
--ldl
-----------------------------------------------------------------------------
LD Landis l...@HealthPartners.Com N0YRQ Voice 612/883-5511 Fax 612/883-6363
HealthPartners, 8100 34th Avenue So, PO Box 1309, Minneapolis, MN 55440-1309
Shape your life not from your memories, but from your hopes. (Borrowed)
-----------------------------------------------------------------------------
Python does not do within string manipulation of variables, a la $varname.
However, you can use the % operator if you want:
print "var1 = %s, var2 = %s" % (var1, var2)
[you can also use """ if you want...]
You can also force $var type behavior using a little function which
would make use of regsub...
--david
Experimentally i wrote a string-interpolation module called "Itpl.py"
(available at http://www.lfw.org/python/) which does that. It lets
you stick ordinary variables in strings with "$", but also supports
array and dictionary subscripting, module and class attributes, and
evaluation of expressions typed directly into the string. The syntax
is quite a bit cleaner-looking than Perl's, IMHO.
Ping
print "var1 = %s, var2 = %s" % (var1, var2)
I find it very handy (and very close and maybe superior to Perl) to
use the dictionary substitution
print "var1 = %(var1)s, var2 = %(var2)s" % vars()
hope this helps - chris
David Ascher wrote:
>
> > BUT... doesn't the perl one do dynamical stuff to the content between the
> > EOF's (ala sh)??? The """ option is only a long, unprocessed string...
> > Walt, this behaviour (dynamic processing) is what you were after, right?
> > (Me too).
>
> Python does not do within string manipulation of variables, a la $varname.
> However, you can use the % operator if you want:
>
> print "var1 = %s, var2 = %s" % (var1, var2)
>
> [you can also use """ if you want...]
>
> You can also force $var type behavior using a little function which
> would make use of regsub...
>
> --david
Which as Don Beaudry just reminded me is not the Python way. The python
way is:
print "var1 = %(var1)s, var2 = %(var2)s" % vars()
-- da
Actually I just wanted a long blob of text to avoid having to switch
between single-quote and double-quoted strings. I'm curious if I can
change the """ to something else (like PERL lets you select the
terminating string), and it would be nice to do some dynamic
processing... however the answer given has met my needs.
Now here's the next logical question: Can I build a long string of text
(inside the """ which I can live with) and feed that through a regular
expression parser, effectively doing what lex does... so I can write
something like:
x = """ ... <H1>You Are The $counter Visitor</H1> ... """
And then use code that does "Find $counter and replace that string with
the return value of a function call or variable." It sounds simple
enough....
> Now here's the next logical question: Can I build a long string of text
> (inside the """ which I can live with) and feed that through a regular
> expression parser, effectively doing what lex does... so I can write
Sure you can. Strings inside """ delimeters are strings anyway, just
like strings in "" or in ', there is no difference.
Oleg.
---
Oleg Broytmann Institute for commercial engineering p...@ice.ru
Programmers don't die, they just GOSUB without RETURN.
> x = """ ... <H1>You Are The $counter Visitor</H1> ... """
> And then use code that does "Find $counter and replace that string with
> the return value of a function call or variable." It sounds simple
> enough....
Isn't %(counter)s simple enough?
# a very long string
x = """ ... <H1>You Are The %(counter)s Visitor</H1> ... """
# find %(counter)s and replace with return value of function call
print x % {"counter": getcounter())
Cheers /F
x = """ ... <H1>You Are The %s Visitor</H1> ... """ % (counter())
should work for this example
Terry writes:
> x = """ ... <H1>You Are The %s Visitor</H1> ... """ % (counter())
> should work for this example
x = """... <H1>You Are The %(counter)s Visitor</H1> ..."""%vars()
is closer to the original intent, not position sensitive, and doesn't
get cluttered if more than a couple of substitutions are used.
BTW: Digicool's DocumentTemplate stuff is a very nice solution to this
It allows simple drop in formats as either python extended format as
above
or as HTML comment tags.
See http://www.digicool.com/Papers/DocumentTemplate.html
Pat
--
Patrick Miller
Mail || at mailto:patm...@llnl.gov mailto:patm...@caesoft.com
WWW || http://www.caesoft.com/miller
Phone || (510) 423-0309 [LLNL], (209) 833-3459, (209) 833-6099 [FAX]