Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #24446 > unrolled thread
| Started by | Mladen Gogala <gogala.mladen@gmail.com> |
|---|---|
| First post | 2012-06-25 21:20 +0000 |
| Last post | 2012-06-25 14:50 -0700 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
Perl __DATA__ construct. Mladen Gogala <gogala.mladen@gmail.com> - 2012-06-25 21:20 +0000
Re: Perl __DATA__ construct. Benjamin Kaplan <benjamin.kaplan@case.edu> - 2012-06-25 14:41 -0700
Re: Perl __DATA__ construct. Miki Tebeka <miki.tebeka@gmail.com> - 2012-06-25 14:50 -0700
| From | Mladen Gogala <gogala.mladen@gmail.com> |
|---|---|
| Date | 2012-06-25 21:20 +0000 |
| Subject | Perl __DATA__ construct. |
| Message-ID | <jsakmp$bsp$1@solani.org> |
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
[toc] | [next] | [standalone]
| From | Benjamin Kaplan <benjamin.kaplan@case.edu> |
|---|---|
| Date | 2012-06-25 14:41 -0700 |
| Message-ID | <mailman.1500.1340660517.4697.python-list@python.org> |
| In reply to | #24446 |
On Mon, Jun 25, 2012 at 2:20 PM, Mladen Gogala <gogala.mladen@gmail.com> 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/introduction.html#strings
[toc] | [prev] | [next] | [standalone]
| From | Miki Tebeka <miki.tebeka@gmail.com> |
|---|---|
| Date | 2012-06-25 14:50 -0700 |
| Message-ID | <194157d2-d2bf-430a-ad6c-19a05a81a39f@googlegroups.com> |
| In reply to | #24446 |
> 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web