473,569 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overriding file.readline: "an integer is required"

kj


I'm trying to subclass file, overriding the readline method. The
new method definition begins with

def readline(self, size=None):
line = self.file.readl ine(size)
# etc., etc.

....where the self.file attribute is a regular file object.

This works fine if I invoke the new method with an integer argument,
but if I invoke it without arguments, I get the error

TypeError: an integer is required

....which I suppose comes from the call to self.file.readl ine(None).

I know that I could rewrite the method like this:

def readline(self, size=None):
if size == None:
line = self.file.readl ine()
else:
line = self.file.readl ine(size)
# etc., etc.

....but this seems to me exceptionally awkward. (Actually, it's worse
than awkward: it fails to complain when the overriding method is
called with the argument None. It would be better to test for the
number of arguments passed to the function, but I can't figure out
how to do this either.)

Is there a better idiom?

TIA!

Kynn
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jul 30 '08 #1
5 2717
kj wrote:
I'm trying to subclass file, overriding the readline method. The
new method definition begins with

def readline(self, size=None):
line = self.file.readl ine(size)
# etc., etc.

...where the self.file attribute is a regular file object.

This works fine if I invoke the new method with an integer argument,
but if I invoke it without arguments, I get the error

TypeError: an integer is required

...which I suppose comes from the call to self.file.readl ine(None).

I know that I could rewrite the method like this:

def readline(self, size=None):
if size == None:
line = self.file.readl ine()
else:
line = self.file.readl ine(size)
# etc., etc.

...but this seems to me exceptionally awkward. (Actually, it's worse
than awkward: it fails to complain when the overriding method is
called with the argument None. It would be better to test for the
number of arguments passed to the function, but I can't figure out
how to do this either.)

Is there a better idiom?
Since the manual implies that negative values have no effect, try this:

def readline(self, size=-1):
line = self.file.readl ine(size)
# etc., etc.

I tested this (just barely), and it seems to work as you wish.

Gary Herron

TIA!

Kynn
Jul 30 '08 #2
On Wed, Jul 30, 2008 at 5:52 PM, kj <so***@987jk.co m.invalidwrote:
I know that I could rewrite the method like this:

def readline(self, size=None):
if size == None:
line = self.file.readl ine()
else:
line = self.file.readl ine(size)
# etc., etc.

...but this seems to me exceptionally awkward. (Actually, it's worse
than awkward: it fails to complain when the overriding method is
called with the argument None.
IMO, the method should have been possible to call with None in the
first place (like str.split and list.sort), and it's not incorrect for
your method to allow it.
It would be better to test for the
number of arguments passed to the function, but I can't figure out
how to do this either.)
You could of course do:

def readline(self, *args):
line = self.file.readl ine(*args)
# etc., etc.

But this has its drawbacks.

-Miles
Jul 31 '08 #3
kj
In <ma************ *************** *********@pytho n.orgMiles <se*********@gm ail.comwrites:
>On Wed, Jul 30, 2008 at 5:52 PM, kj <so***@987jk.co m.invalidwrote:
>I know that I could rewrite the method like this:

def readline(self, size=None):
if size == None:
line = self.file.readl ine()
else:
line = self.file.readl ine(size)
# etc., etc.

...but this seems to me exceptionally awkward. (Actually, it's worse
than awkward: it fails to complain when the overriding method is
called with the argument None.
>You could of course do:
def readline(self, *args):
line = self.file.readl ine(*args)
# etc., etc.
>But this has its drawbacks.
Like what? (I'm pretty new to Python and these drawbacks are not
obvious to me.)

TIA!

kynn
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Aug 1 '08 #4
En Fri, 01 Aug 2008 16:21:26 -0300, kj <so***@987jk.co m.invalidescrib i�:
In <ma************ *************** *********@pytho n.orgMiles
<se*********@gm ail.comwrites:
>On Wed, Jul 30, 2008 at 5:52 PM, kj <so***@987jk.co m.invalidwrote:
>>I know that I could rewrite the method like this:

def readline(self, size=None):
if size == None:
line = self.file.readl ine()
else:
line = self.file.readl ine(size)
# etc., etc.

...but this seems to me exceptionally awkward. (Actually, it's worse
than awkward: it fails to complain when the overriding method is
called with the argument None.
>You could of course do:
> def readline(self, *args):
line = self.file.readl ine(*args)
# etc., etc.
>But this has its drawbacks.

Like what? (I'm pretty new to Python and these drawbacks are not
obvious to me.)
One thing I can think of: The help system (and the code autocompleter
found in some editors, and other introspection tools) can't tell you the
name/number/default value of the arguments anymore: they're all masked
into a generic *args

--
Gabriel Genellina

Aug 5 '08 #5


Gabriel Genellina wrote:
>>> def readline(self, size=None):
if size == None:
line = self.file.readl ine()
else:
line = self.file.readl ine(size)
# etc., etc.
Not obvious from the docs, but readline(-1) should be the same as
readline().
(In 3.0b2, None also works, but this is not yet documented.) So try:

def readline(self, size=-1):
line = self.file.readl ine(size)

Aug 5 '08 #6

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

Similar topics

9
6493
by: Russ Perry Jr | last post by:
I'm using "ID" and "Value" in the generic sense here... Let's say one page I had a <html:select> with a collection like this: <html:options collection="items" property="key" labelProperty="value"/> In this case "key" is what I mean by "ID", and "value" is what I mean by "Value" -- the "Value" is shown to the user, but the "ID" is what I...
7
1963
by: Ian | last post by:
Using Access 97 I have a form with several text boxes, I am trying to get each text box to display the word "Required" for the appropriate boxes on a new record. A friend gave me the following line of code that I put into the text box Format property: @;"Required" This works perfectly, problem is that this text box's Format also has to be...
7
6918
by: Robin Tucker | last post by:
Hiya, I'm using mshtml and the web browser control to embed an OLE object in my ..NET application. What I want to do is get a reference to the "object" embedded therein: The HTML looks something like this: <html>
4
2739
by: Supra | last post by:
value of type "Integer" cannot be convert to system.color Public Sub APIHighlight2(ByVal BgColour As Integer, ByVal FgColour As Integer) SelectionHighlightBackColour(BgColour) Dim rtb As New RichTextBox rtb.ForeColor = FgColour ===> error occured in fgcolour End Sub regards,
3
1220
by: Progalex | last post by:
Hi everybody! I'm creating a very simple VB .NET application that needs to launch the command line ml.exe compiler. At the moment I'm redirecting ML's output to a text file and then my app reads its content and puts it to a textbox for the results. Is it possible to redirect ML's output as VS does, I mean the output given line by line...
8
11427
by: kevin | last post by:
I have a form and in the form I have a sub that uses a class I instantiate using visual basic code: Public oCP As New Rs232 'instantiate the comm port I need to share this sub with another form so to declare the sub I use visual basic code: Public Shared Function IsPortAvailable(ByVal ComPort As Integer) As Boolean
35
75144
by: pinkfloydhomer | last post by:
How do I check if a string contains (can be converted to) an int? I want to do one thing if I am parsing and integer, and another if not. /David
4
14513
by: Vincent | last post by:
Regarding the dropdetect function, defined as follows: Sub dropdetect(DropFrm As Form, DropCtrl As Control, Button As Integer, Shift As Integer, X As Single, Y As Single) Is it possible to cast the Control parameter, DropCtrl, to a TextBox object? The underlying control is in fact a textbox which I wish to redraw given the X and Y...
13
1911
by: Jim | last post by:
Could somebody tell me why I need the "elif char == '\n'" in the following code? This is required in order the pick up lines with just spaces in them. Why doesn't the "else:" statement pick this up? OLD_INDENT = 5 # spaces NEW_INDENT = 4 # spaces print 'Reindent.py:'
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7982
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6286
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.