Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Perl __DATA__ construct.

Reply
Thread Tools

Perl __DATA__ construct.

 
 
Mladen Gogala
Guest
Posts: n/a
 
      06-25-2012
I have a script in Perl that I need to rewrite to Python. The script
contains __DATA__ at the end of the script, which enables Perl to access
all the data after that through a file descriptor, like this:

usage() if ( !$stat or !defined($home) or !defined($base) or !defined
($sid) );
while (<DATA>) {
s/%OB/$base/;
if ( length($home) > 0 ) {
s/%OH/$home/;
}
else {
s/\/%OH$//;
}
if ( length($sid) > 0 && /%OS/ ) {
s/%OS/$sid/;
}
elsif (/%OS/) {
next;
}
s/%VR/$ver/;
print;
}
__DATA__
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
set -a

# User specific aliases and functions
export PATH=/sbin:/bin:/usr/sbin:$PATH
export EDITOR=vi
export ORACLE_BASE=%OB
export ORACLE_HOME=$ORACLE_BASE/product/%VR/%OH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/opt/odbc/lib:$ORACLE_HOME/lib32
export CLASSPATH=/opt/java/lib/tools.jar:$ORACLE_HOME/jdbc/lib/
ojdbc14.jar:.

.......



How do I do the same thing in Python? Alternatively, in Perl I can put an
entire file into a string by using something like:

$str=<<EOF
This is all a single string,
no matter how many lines do
I put in it, but I do have to
escape the special character
EOF
;

Is there a way to do the same thing in Python? The idea of the script is
to generate $HOME/.bashrc for any automagically provisioned Oracle
installation.

--
http://mgogala.byethost5.com
 
Reply With Quote
 
 
 
 
Benjamin Kaplan
Guest
Posts: n/a
 
      06-25-2012
On Mon, Jun 25, 2012 at 2:20 PM, Mladen Gogala <(E-Mail Removed)> wrote:
> I have a script in Perl that I need to rewrite to Python. The script
> contains __DATA__ at the end of the script, which enables Perl to access
> all the data after that through a file descriptor, like this:
>
> usage() if ( !$stat or !defined($home) or !defined($base) or !defined
> ($sid) );
> while (<DATA>) {
> * *s/%OB/$base/;
> * *if ( length($home) > 0 ) {
> * * * *s/%OH/$home/;
> * *}
> * *else {
> * * * *s/\/%OH$//;
> * *}
> * *if ( length($sid) > 0 && /%OS/ ) {
> * * * *s/%OS/$sid/;
> * *}
> * *elsif (/%OS/) {
> * * * *next;
> * *}
> * *s/%VR/$ver/;
> * *print;
> }
> __DATA__
> # .bashrc
> # Source global definitions
> if [ -f /etc/bashrc ]; then
> * * * *. /etc/bashrc
> fi
> set -a
>
> # User specific aliases and functions
> export PATH=/sbin:/bin:/usr/sbin:$PATH
> export EDITOR=vi
> export ORACLE_BASE=%OB
> export ORACLE_HOME=$ORACLE_BASE/product/%VR/%OH
> export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/opt/odbc/lib:$ORACLE_HOME/lib32
> export CLASSPATH=/opt/java/lib/tools.jar:$ORACLE_HOME/jdbc/lib/
> ojdbc14.jar:.
>
> ......
>
>
>
> How do I do the same thing in Python? Alternatively, in Perl I can put an
> entire file into a string by using something like:
>
> $str=<<EOF
> This is all a single string,
> no matter how many lines do
> I put in it, but I do have to
> escape the special character
> EOF
> ;
>
> Is there a way to do the same thing in Python? The idea of the script is
> to generate $HOME/.bashrc for any automagically provisioned Oracle
> installation.
>

either escape the new-line
'hello \
world'

or use triple-quoted strings
"""hello
world"""

http://docs.python.org/tutorial/intr...n.html#strings
 
Reply With Quote
 
 
 
 
Miki Tebeka
Guest
Posts: n/a
 
      06-25-2012
> Is there a way to do the same thing in Python?
Not without some clever tricks. Either you store data at the beginning of the file or you have another file with the data.

If you really need one file and data at the end, you can roll your own. Something like:
def data():
with open(__file__) as fo:
for line in fo:
if line.startswith('# __data__'):
return ''.join(line[2:] for line in fo)

def main():
print(data())


if __name__ == '__main__':
main()

# __data__
# blah
# blah
# blah
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
rcr: request for __DATA__ besides __END__ .. botp Ruby 0 09-25-2010 03:32 AM
HTML::TokeParser; __DATA__ as a filehandle Jonathan@D4HRQQB1.i-did-not-set--mail-host-address--so-tickle-me Perl Misc 2 10-24-2006 06:27 PM
seek and __DATA__ not behaving how I would hope Chris Marshall Perl Misc 8 06-04-2005 10:18 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments