Discussion:
[Tutor] Diamond Equivalent
k***@tesco.net
2005-09-22 22:46:16 UTC
Permalink
I am coming to Python from Perl. Does Python have anything like the diamond
operator found in Perl?
Danny Yoo
2005-09-22 23:31:30 UTC
Permalink
Post by k***@tesco.net
I am coming to Python from Perl. Does Python have anything like the
diamond operator found in Perl?
Hi Kim,


According to 'perldoc perlop':

"""
The null filehandle <> is special: it can be used to emulate the
behavior of sed and awk. Input from <> comes either from standard
input, or from each file listed on the command line. Here's how it
works: the first time <> is evaluated, the @ARGV array is checked,
and if it is empty, $ARGV[0] is set to "-", which when opened gives
you standard input. The @ARGV array is then processed as a list
of filenames.
"""

Python includes a module called 'fileinput' that does sorta this:

http://www.python.org/doc/lib/module-fileinput.html

If you have more questions, please feel free to ask. Good luck to you!
bob
2005-09-23 04:05:05 UTC
Permalink
Post by k***@tesco.net
I am coming to Python from Perl. Does Python have anything like the diamond
operator found in Perl?
Some of us (who don't know Perl) might help you if you tell us what the
diamond operator does. How could we get to first base with it? What are its
facets? Does is have a heart counterpart? Ca it sing?
k***@tesco.net
2005-09-23 09:25:52 UTC
Permalink
Post by Danny Yoo
Post by k***@tesco.net
I am coming to Python from Perl. Does Python have
anything like the diamond operator found in Perl?
http://www.python.org/doc/lib/module-fileinput.html
If you have more questions, please feel free to ask.
Good luck to you!
Thanks, Danny. Is this module included with Python or will I have to download
it?
Kent Johnson
2005-09-23 09:53:11 UTC
Permalink
Post by k***@tesco.net
Post by Danny Yoo
Post by k***@tesco.net
I am coming to Python from Perl. Does Python have
anything like the diamond operator found in Perl?
http://www.python.org/doc/lib/module-fileinput.html
If you have more questions, please feel free to ask.
Good luck to you!
Thanks, Danny. Is this module included with Python or will I have to download
it?
It is included as part of the standard library (as are all the modules in the doc Danny referenced).

Kent
k***@tesco.net
2005-09-23 11:36:36 UTC
Permalink
Post by Kent Johnson
It is included as part of the standard
library (as are all the modules in the doc
Danny referenced).
Thanks, Kent.
k***@tesco.net
2005-09-23 11:37:16 UTC
Permalink
bob wrote <<<Some of us (who don't know Perl) might help you if you tell us
what the diamond operator does. How could we get to first base with it? What
are its facets? Does is have a heart counterpart? Ca it sing?>>>

Sorry bob, that's what comes of posting my question last thing at night when
finding the way upstairs is difficult enough.

In perl I can write this:

@array = <>;
print @array;

If I save that and call it from the command line, I can include the name of a
file after the script name. It will read in the file, putting each line into an
element of the array.

I know how to open a specific file using Python. I wanted to know how to give
a Python script a file name in the command line, and have it open that file.

-kim
bob
2005-09-23 17:10:13 UTC
Permalink
[snip]
@array = <>;
If I save that and call it from the command line, I can include the name of a
file after the script name. It will read in the file, putting each line into an
element of the array.
I know how to open a specific file using Python. I wanted to know how to give
a Python script a file name in the command line, and have it open that file.
So there are 2 parts to the question:

(1) how to retrieve command line arguments:

import sys
arg1 = sys.argv[1]

(2) how to read the lines of a file (whose path is in arg1) into an array

array = file(arg1).readlines()

That's all. Of course you don't need an intermediate variable; you can just:

import sys
array = file(sys.argv[1]).readlines()

and if you want to print the results:

print array
Michael Sparks
2005-09-23 19:15:03 UTC
Permalink
Post by k***@tesco.net
I am coming to Python from Perl. Does Python have anything like the diamond
operator found in Perl?
The correct answer is not really no, but you can manually perform the
same tasks. For those who don't know perl, <> is an incredibly useful
little operator that does some really wierd and wonderful things :-)

(That said, I don't think I'd like a magic operator like this in python :-)

If you want to do this:

@a = <>;

The equivalent is probably best expressed thus:

def diamond_array():
# Note, untested...
import sys
if len(sys.argv ==1):
return sys.stdin.readlines()
else:
result = []
for file in sys.argv[1:]:
try:
file = open(file)
result.extend(file.readlines())
except IOError:
pass
return result

a = diamond_array()


If however you want to do the equivalent of:

while ($line = <>) {
...
}

That mode of <> probably best equates to this:

for line in diamond_lines():
....

def diamond_lines():
# Note, untested...
import sys
if len(sys.argv ==1):
for line in sys.stdin.xreadlines():
yield line
else:
for file in sys.argv[1:]:
try:
file = open(file)
for line in file.xreadlines():
yield line
except IOError:
pass

As for this idiom:

while(1) {
$line = <>;
...
}

That probably matches best to this:

diamond_iterator = diamond_lines() # same function as above
while 1:
line = diamond_iterator.next()

Whereas this kind of trick:

$firstline = <>;
@remaining_lines = <>;

Would map to this: (using the functions above)

diamond_iterator = diamond_lines()
firstline = diamond_iterator.next()
remaining_lines = list(diamond_iterator.next())

Or to show a relatively common perl idiom:

$firstline, @remaining_lines= <>,<>;

Maps to:

diamond_iterator = diamond_lines()
firstline,remaining_lines = diamond_iterator.next(), list(diamond_iterator.next())

Best Regards,


Michael.
k***@tesco.net
2005-09-23 22:51:19 UTC
Permalink
Bob, Michael, my thanks to you both for answering my diamond operator
question.

Bob, I tried the lines you suggested and they worked just the way I wanted
them to. Thanks.

Michael, thanks for the user functions or subroutines: not sure what Python
calls them. A lot of food for thought there. Much appreciated.

Loading...