473,394 Members | 1,739 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,394 software developers and data experts.

Python end of file marker similar to perl's __END__

Hi All,

This is just a very simple question about a python trick.

In perl, I can write __END__ in a file and the perl interpreter will
ignore everything below that line. This is very handy when testing my
program. Does python have something similar?

Thanks,
Geoffrey

Aug 1 '07 #1
17 6569
In perl, I can write __END__ in a file and the perl interpreter will
ignore everything below that line. This is very handy when testing my
program. Does python have something similar?
Sorry, no, it doesn't.

Regards,
Martin
Aug 1 '07 #2
On Aug 1, 5:53 am, beginner <zyzhu2...@gmail.comwrote:
Hi All,

This is just a very simple question about a python trick.

In perl, I can write __END__ in a file and the perl interpreter will
ignore everything below that line. This is very handy when testing my
program. Does python have something similar?

Thanks,
Geoffrey
I wished from something like that. What you can do at the
moment, is to comment or triple quote the code you
don't want to run.

Michele Simionato

Aug 1 '07 #3
On Wed, 01 Aug 2007 05:44:21 +0000, Michele Simionato wrote:
On Aug 1, 5:53 am, beginner <zyzhu2...@gmail.comwrote:
>Hi All,

This is just a very simple question about a python trick.

In perl, I can write __END__ in a file and the perl interpreter will
ignore everything below that line. This is very handy when testing my
program. Does python have something similar?

I wished from something like that. What you can do at the moment, is to
comment or triple quote the code you don't want to run.
Or, if in a function body, you could insert a `return` statement. When in
top-level code, invoking `sys.exit` (or exit/quit) can do the trick. A
``raise Exception`` might help, too, but could be kinda distracting
sometimes.

So, there is no general purpose solution as perl has it (I guess that
__END__ works everywhere at least), rather different solutions for
different cases.
Aug 1 '07 #4
beginner <zy*******@gmail.comwrites:
In perl, I can write __END__ in a file and the perl interpreter will
ignore everything below that line.
IIRC, this Perl feature is specifically intended to work with its
feature of reading data from the same file, as all the lines following
that marker.
This is very handy when testing my program.
If my understanding above is correct, then your use of it is a happy
repurposing of the feature, and not an intended use.
Does python have something similar?
Since it doesn't have the behaviour that inspired that feature in
Perl, I doubt it.

--
\ "Everything is futile." -- Marvin of Borg |
`\ |
_o__) |
Ben Finney
Aug 1 '07 #5
Stargaming wrote:
On Wed, 01 Aug 2007 05:44:21 +0000, Michele Simionato wrote:
>On Aug 1, 5:53 am, beginner <zyzhu2...@gmail.comwrote:
>>Hi All,

This is just a very simple question about a python trick.

In perl, I can write __END__ in a file and the perl interpreter will
ignore everything below that line. This is very handy when testing my
program. Does python have something similar?
I wished from something like that. What you can do at the moment, is to
comment or triple quote the code you don't want to run.

Or, if in a function body, you could insert a `return` statement. When in
top-level code, invoking `sys.exit` (or exit/quit) can do the trick. A
``raise Exception`` might help, too, but could be kinda distracting
sometimes.

So, there is no general purpose solution as perl has it (I guess that
__END__ works everywhere at least), rather different solutions for
different cases.
I think you have missed the point. A return statement or call to
sys.exit() doesn't remove the requirement that the rest ofthe source
file be legal Python. In a Perl program you can put anything you like
after __END__.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 1 '07 #6
On Wed, 01 Aug 2007 06:56:36 -0400, Steve Holden wrote:
Stargaming wrote:
>On Wed, 01 Aug 2007 05:44:21 +0000, Michele Simionato wrote:
>>On Aug 1, 5:53 am, beginner <zyzhu2...@gmail.comwrote:
Hi All,

This is just a very simple question about a python trick.

In perl, I can write __END__ in a file and the perl interpreter will
ignore everything below that line. This is very handy when testing my
program. Does python have something similar?
I wished from something like that. What you can do at the moment, is
to comment or triple quote the code you don't want to run.

Or, if in a function body, you could insert a `return` statement. When
in top-level code, invoking `sys.exit` (or exit/quit) can do the trick.
A ``raise Exception`` might help, too, but could be kinda distracting
sometimes.

So, there is no general purpose solution as perl has it (I guess that
__END__ works everywhere at least), rather different solutions for
different cases.

I think you have missed the point. A return statement or call to
sys.exit() doesn't remove the requirement that the rest ofthe source
file be legal Python. In a Perl program you can put anything you like
after __END__.

regards
Steve
That was my point actually. No, there is no such general purpose solution
as in perl, but if he just wanted to quit execution (to, eg., not commit
changes to his database), this would be the way to go. Multiline strings
are the other way to include (nearly) arbitrary data.
Aug 1 '07 #7
beginner wrote:
Hi All,

This is just a very simple question about a python trick.

In perl, I can write __END__ in a file and the perl interpreter will
ignore everything below that line. This is very handy when testing my
program. Does python have something similar?
raise SystemExit() exits the program at that point (unless you
catch the exception...) "import sys;sys.exit(0)" is basically
another spelling of the same thing. It doesn't mean that the
interpreter ignores the rest of the file though, so it will
complain about syntax in the whole file.

Since I don't usually write linear top-to-bottom scripts in Python,
I don't really see the use of splitting a file in an interpreted
top and an ignored bottom though.

I'd suggest employing a test driven approach to development. Then
you don't usually have big chunks of code that you don't want to
run. All that's there works (almost)...

See e.g.
http://powertwenty.com/kpd/downloads...ntInPython.pdf
Aug 1 '07 #8
On Jul 31, 10:53 pm, beginner <zyzhu2...@gmail.comwrote:
Hi All,

This is just a very simple question about a python trick.

In perl, I can write __END__ in a file and the perl interpreter will
ignore everything below that line. This is very handy when testing my
program. Does python have something similar?

Thanks,
Geoffrey
Thanks everyone for responding. It doesn't look like python has it. I
would definitely miss it. As Steve said, the nice thing about __END__
is that things below __END__ do not have to have legit syntax. That
let me focus on the lines of code I am debugging and do not have to
worry about some bad syntax down the line. This feature is especially
handy if I am, saying, replacing modoules or changing data structures.
Aug 1 '07 #9
On 2007-08-01, beginner <zy*******@gmail.comwrote:
Thanks everyone for responding. It doesn't look like python has
it. I would definitely miss it. As Steve said, the nice thing
about __END__ is that things below __END__ do not have to have
legit syntax. That let me focus on the lines of code I am
debugging and do not have to worry about some bad syntax down
the line. This feature is especially handy if I am, saying,
replacing modoules or changing data structures.
A C-like trick might be helpful while refactoring:

<working code>
if False:
<non-working code>

You have to indent all the non-working code by one level, but
with a good editor that's a snap.

Python will still parse the following lines (it must be valid
Python syntax), but the resulting parse tree won't be executed.

--
Neil Cerutti
Aug 1 '07 #10
beginner wrote:
On Jul 31, 10:53 pm, beginner <zyzhu2...@gmail.comwrote:
>Hi All,

This is just a very simple question about a python trick.

In perl, I can write __END__ in a file and the perl interpreter will
ignore everything below that line. This is very handy when testing my
program. Does python have something similar?

Thanks,
Geoffrey

Thanks everyone for responding. It doesn't look like python has it. I
would definitely miss it. As Steve said, the nice thing about __END__
is that things below __END__ do not have to have legit syntax. That
let me focus on the lines of code I am debugging and do not have to
worry about some bad syntax down the line. This feature is especially
handy if I am, saying, replacing modoules or changing data structures.
In emacs, I simply mark that portion of code and do

M-x comment-region

That's it. And I don't think a language should support things __END__ -
commenting is enough. It's unfortunate that Python doesn't support
multi-line-comments though. But as I said, that my Editor can handle for
me.

Diez
Aug 1 '07 #11
On 8/1/07, beginner <zy*******@gmail.comwrote:
On Jul 31, 10:53 pm, beginner <zyzhu2...@gmail.comwrote:
Hi All,

This is just a very simple question about a python trick.

In perl, I can write __END__ in a file and the perl interpreter will
ignore everything below that line. This is very handy when testing my
program. Does python have something similar?

Thanks,
Geoffrey

Thanks everyone for responding. It doesn't look like python has it. I
would definitely miss it. As Steve said, the nice thing about __END__
is that things below __END__ do not have to have legit syntax. That
let me focus on the lines of code I am debugging and do not have to
worry about some bad syntax down the line. This feature is especially
handy if I am, saying, replacing modoules or changing data structures.
You'll probably find greater gains by embracing the limitation and
using it to help you refactor your code into smaller, more discrete
modules. Many of pythons limitations that beginners complain about are
of this nature.
Aug 1 '07 #12
In article <sl********************@FIAD06.norwich.edu>,
Neil Cerutti <ho*****@yahoo.comwrote:
>On 2007-08-01, beginner <zy*******@gmail.comwrote:
>Thanks everyone for responding. It doesn't look like python has
it. I would definitely miss it. As Steve said, the nice thing
about __END__ is that things below __END__ do not have to have
legit syntax. That let me focus on the lines of code I am
debugging and do not have to worry about some bad syntax down
the line. This feature is especially handy if I am, saying,
replacing modoules or changing data structures.

A C-like trick might be helpful while refactoring:

<working code>
if False:
<non-working code>

You have to indent all the non-working code by one level, but
with a good editor that's a snap.

Python will still parse the following lines (it must be valid
Python syntax), but the resulting parse tree won't be executed.
Aug 1 '07 #13
On 2007-08-01, Cameron Laird <cl****@lairds.uswrote:
In article <sl********************@FIAD06.norwich.edu>,
Neil Cerutti <ho*****@yahoo.comwrote:
>>On 2007-08-01, beginner <zy*******@gmail.comwrote:
>>Thanks everyone for responding. It doesn't look like python has
it. I would definitely miss it. As Steve said, the nice thing
about __END__ is that things below __END__ do not have to have
legit syntax. That let me focus on the lines of code I am
debugging and do not have to worry about some bad syntax down
the line. This feature is especially handy if I am, saying,
replacing modoules or changing data structures.

A C-like trick might be helpful while refactoring:

<working code>
if False:
<non-working code>

You have to indent all the non-working code by one level, but
with a good editor that's a snap.

Python will still parse the following lines (it must be valid
Python syntax), but the resulting parse tree won't be executed.
.
.
.
I want to re-emphasize the "triple-quote it" tip mentioned
earlier in this thread. I think the original questioner
will find this quite satisfying, if I understand his situ-
ation at all.

*I* certainly have source code with embedded "junk"
commented out as multi-line strings.
I used to do that, but now that I use doctests so much it's
infeasible to comment out arbitrary code that way, since they
can't necessarily nest.

But Diez suggestion is even easier than the if False suggestion I
made.

--
Neil Cerutti

Aug 1 '07 #14
beginner <zy*******@gmail.comwrites:
Thanks everyone for responding. It doesn't look like python has
it. I would definitely miss it. As Steve said, the nice thing about
__END__ is that things below __END__ do not have to have legit
syntax.
I think the unaddressed question is: Why is there so much code in your
module with invalid syntax that this trick would be useful?
That let me focus on the lines of code I am debugging and do not
have to worry about some bad syntax down the line. This feature is
especially handy if I am, saying, replacing modoules or changing
data structures.
I would strongly recommend you examine what part of your practice is
leading you to write so much code with invalid syntax, without
immediately testing and fixing it. Eliminate that part of the process
— possibly with test-driven development.

--
\ "Holy unrefillable prescriptions, Batman!" -- Robin |
`\ |
_o__) |
Ben Finney
Aug 2 '07 #15
Neil Cerutti wrote:
On 2007-08-01, Cameron Laird <cl****@lairds.uswrote: .
>I want to re-emphasize the "triple-quote it" tip mentioned
earlier in this thread. I think the original questioner
will find this quite satisfying, if I understand his situ-
ation at all.

*I* certainly have source code with embedded "junk"
commented out as multi-line strings.

I used to do that, but now that I use doctests so much it's
infeasible to comment out arbitrary code that way, since they
can't necessarily nest.
If you consistently use e.g. ''' for doc strings, you can use
""" to comment out code blocks.

I still think it's better to do test-driven programming though.
Then you will very rarely have large blocks on non-working code.
Aug 2 '07 #16
On Thu, 02 Aug 2007 10:00:04 +1000, Ben Finney wrote:
beginner <zy*******@gmail.comwrites:
>Thanks everyone for responding. It doesn't look like python has
it. I would definitely miss it. As Steve said, the nice thing about
__END__ is that things below __END__ do not have to have legit
syntax.

I think the unaddressed question is: Why is there so much code in your
module with invalid syntax that this trick would be useful?
It's not just "bad syntax" that makes the triple-quote or comment trick
useful. Sometimes you're experimenting, or perhaps tinkering is a better
description. Your aim isn't to end up with a working piece of code, but to
learn something (e.g. "how do decorators work?"). You may end up with
working code at the end, but the finished code isn't the aim of the
exercise and may not be kept.

Because you're experimenting, you might end up with ten different versions
of a function, and not all of them will compile, let alone execute
correctly. It's handy to be able to comment them out and reload() the
file, and while some editors will do bulk comment/uncomment of text, the
triple-quoted string trick is easy enough that you can use it in
Microsoft's Notepad.

>That let me focus on the lines of code I am debugging and do not
have to worry about some bad syntax down the line. This feature is
especially handy if I am, saying, replacing modoules or changing
data structures.

I would strongly recommend you examine what part of your practice is
leading you to write so much code with invalid syntax, without
immediately testing and fixing it. Eliminate that part of the process
-- possibly with test-driven development.
While the discipline of TDD is good and useful, there's a time and place
for unstructured and informal experimentation too.
--
Steven.

Aug 2 '07 #17
Steven D'Aprano <st***@REMOVE.THIS.cybersource.com.auwrites:
It's not just "bad syntax" that makes the triple-quote or comment
trick useful.
Okay. That was the original reason given for refusing suggestions like
"return early from the function" etc. I answered in that context.
Because you're experimenting, you might end up with ten different
versions of a function, and not all of them will compile, let alone
execute correctly.
If a function doesn't compile, I don't understand why you're not
either throwing it away or fixing it.

As for multiple versions? That's what version control is for. With
client-only, distributed VCSes like Bazaar and Mercurial (both written
in Python) you don't even have to do anything other than install the
software to begin using it.

There's no reason not to be using a version control system any time
you're coding — even (especially!) while "just experimenting".
It's handy to be able to comment them out and reload() the file
Why not simply fix them and reload() instead? Why keep something
inside the module that doesn't work, and indeed has just been proven
not to work? (If the only reason is "because I might need it later",
again, that's what a VCS is for.)

--
\ "I'd take the awe of understanding over the awe of ignorance |
`\ any day." -- Douglas Adams |
_o__) |
Ben Finney
Aug 2 '07 #18

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

Similar topics

4
by: FLEB | last post by:
I like PHP for its excellent inline integration into standard HTML files, but I like Perl for its quick-moving syntax and simpler data-processing. To resolve this deep-seated inner turmoil (oh, the...
14
by: Xah Lee | last post by:
Just bumped into another irresponsibility in perl. the crime in question this time is the module File::Basename. Reproduction: 1. create a directory containing a file of this name:...
4
by: Avi Kak | last post by:
Hello: Does Python provide a token like Perl's __END__ that would mark the logical end of a Python script which could occur before the actual end of the file. I have found Perl's __END__...
77
by: Hunn E. Balsiche | last post by:
in term of its OO features, syntax consistencies, ease of use, and their development progress. I have not use python but heard about it quite often; and ruby, is it mature enough to be use for...
1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
2
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # suppose you want to fetch a webpage. from urllib import urlopen print urlopen('http://xahlee.org/Periodic_dosage_dir/_p2/russell-lecture.html').read() #...
9
by: Martin Foster | last post by:
Hi. I would like to be able to mimic the unix tool 'uniq' within a Perl script. I have a file with entries that look like this 4 10 21 37 58 83 111 145 184 226...
12
by: Xah Lee | last post by:
would anyone like to translate the following perl script to Python or Scheme (scsh)? the file takes a inpath, and report all html files in it above certain size. (counting inline images) also...
3
by: David Bear | last post by:
I have a hash function written by another organization that I need to use. It is implemented in perl. I've been attempting to decode what they are doing in their hash function and it is taking way...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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...

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.