Re: python 3, subclassing TextIOWrapper.

2009-03-23 Thread lambertdw
For D. Murray's suggestion---I think that we programmers have to learn
the idiom.  We don't always control open, such as subprocess.Popen().

Thank you.  I hope these thoughts help with issue 5513 and the related
questions to follow about complete removal of file in python3.
Opening the file in binary mode for text behavior was not obvious to
me, but makes good sense now that you've explained the further
nesting.

--
http://mail.python.org/mailman/listinfo/python-list


Re: python 3, subclassing TextIOWrapper.

2009-03-22 Thread lambertdw
Return value of open undocumented?

The return value of open() is a stream, according to
http://docs.python.org/dev/py3k/library/io.html#module-io

Seems like time for a bug report.
--
http://mail.python.org/mailman/listinfo/python-list


python 3, subclassing TextIOWrapper.

2009-03-21 Thread lambertdw
'''
A python 3 question.
Presume this code is in file p.py.
The program fails.

$ python3 p.py
...
ValueError: I/O operation on closed file.

Removing the comment character to increase the stream
reference count fixes the program, at the expense of
an extra TextIOWrapper object.

Please, what is a better way to write the class with
regard to this issue?
'''

import re
import io

class file(io.TextIOWrapper):

'''
Enhance TextIO.  Streams have many sources,
a file name is insufficient.
'''

def __init__(self,stream):
#self.stream = stream
super().__init__(stream.buffer)

def seek_pattern(self,pattern):
'''
A motivational method, otherwise inconsequential to the
problem.
'''
search = re.compile(pattern).search
while True:
line = next(self)
if (not line) or search(line):
return line


print(file(open('p.py')).read())

--
http://mail.python.org/mailman/listinfo/python-list


Re: if expression source format

2008-12-11 Thread lambertdw
Consider following snippets:

# must examine code carefully to see that result has a value
if condition:
result = expression1
else:
result = another_expression
return result



# result has a value but difficult to understand how it comes
about
result = expression1 if condition else another_expression
return result



# must examine code carefully to ensure that it always
# returns a computed value
if condition:
return true_expression
else:
return false_expression



# Ahh!  I do use this idiom quite often.
if condition:
return true_expression
return default_expression



Actually, I was simply wondering if there is yet a  preferred way to
write the python ternary expression.  Samples:

# break expression near convenient max line length A)
true_expression if condition else (fa
lse_expression)

# break expression near convenient max line length B)
# (Humans be damned! form.)
true_expr \
ession if cond \
ition else \
false_e \
xpressi \
on


# ternary nature of expression extremely visible
(true_expression
  if condition else
 false_expression)


# I use this form but not happily,
# providing functions as necessary to make it fit onto two lines
# loosely guided by the KR admonition (or maybe it was KP) that
# a conditional expression is too complicated if you can't read
# it to your mother over the phone or something like that.
(true_expression if condition
 else false_expression)

--
http://mail.python.org/mailman/listinfo/python-list