473,242 Members | 1,435 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

exception message output problem

I am baffled about why my exception messages are not displaying
properly.

I have a class that represents physical scalars with units. If I type
>>3 * s + 4 * m
I should get something like this:

scalar.InconsistentUnits: 3 s, 4 m

to show that seconds cannot be added to meters. Instead, the message
is broken up into individual characters, like this:

scalar.InconsistentUnits: ('3', ' ', 's', ',', ' ', '4', ' ', 'm')

This worked correctly for months, so I don't understand what went
wrong. I am using version 2.5.1 on a Dell workstation with Red Hat
Enterprise WS Linux.

As far as I can tell, I am doing everything "by the book." Here is my
exception class:

class InconsistentUnits(Exception):
def __init__(self, args=""): self.args = args

and here is my instantiation of it:

raise scalar.InconsistentUnits, str(self) + ", " + str(other)

I've tried various little changes, but nothing seems to make any
difference. Has anyone else noticed this sort of behavior? Thanks.

By the way, my scalar class is available for free from http://RussP.us/scalar.htm
.. A complete user guide is available. Check it out. I think you'll
like it.

Dec 21 '07 #1
11 1650
Lie
On Dec 22, 2:57*am, "Russ P." <Russ.Paie...@gmail.comwrote:
I am baffled about why my exception messages are not displaying
properly.

I have a class that represents physical scalars with units. If I type
>3 * s + 4 * m

I should get something like this:

scalar.InconsistentUnits: 3 s, 4 m

to show that seconds cannot be added to meters. *Instead, the message
is broken up into individual characters, like this:

scalar.InconsistentUnits: ('3', ' ', 's', ',', ' ', '4', ' ', 'm')

This worked correctly for months, so I don't understand what went
wrong. I am using version 2.5.1 on a Dell workstation with Red Hat
Enterprise WS Linux.

As far as I can tell, I am doing everything "by the book." Here is my
exception class:

* * class InconsistentUnits(Exception):
* * * * def __init__(self, args=""): self.args = args

and here is my instantiation of it:

* * * * raise scalar.InconsistentUnits, str(self) + ", " + str(other)

I've tried various little changes, but nothing seems to make any
difference. Has anyone else noticed this sort of behavior? Thanks.

By the way, my scalar class is available for free fromhttp://RussP.us/scalar.htm
. A complete user guide is available. Check it out. I think you'll
like it.
The problem is in the InconsistentUnits Exception. It seems that the
act of:
self.args = 'Some String'
would break the string into chars, possibly because self.args think
'Some String' is a tuple of some sort.

Change the exception into this:
class InconsistentUnits(Exception):
def __init__(self, args=""): self.args = (args,)
# Python have an odd (read: broken) singleton implementation
# single member tuple must have a comma behind it
Dec 21 '07 #2
Lie a écrit :
(snip)
# Python have an odd (read: broken) singleton implementation
# single member tuple must have a comma behind it
You may call it weird or even a wart if you want, but given that what
makes the tuple is the comma - not the parens[1] -, it is _not_ broken.

[1] with the exception of the empty tuple.
Dec 21 '07 #3
On Dec 21, 2:58 pm, Lie <Lie.1...@gmail.comwrote:
Change the exception into this:
class InconsistentUnits(Exception):
def __init__(self, args=""): self.args = (args,)
# Python have an odd (read: broken) singleton implementation
# single member tuple must have a comma behind it
Hey, that worked. Thanks.

Actually, the parens aren't needed, so this works too:

def __init__(self, args=""): self.args = args,

The trailing comma wasn't necessary a while back (pre 2.5?), so
something in Python must have changed. I'd say that it looks a bit
cleaner without the trailing comma, so maybe whatever changed should
get changed back.
Dec 21 '07 #4
Lie
On Dec 22, 6:18*am, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
Lie a écrit :
(snip)
# Python have an odd (read: broken) singleton implementation
# single member tuple must have a comma behind it

You may call it weird or even a wart if you want, but given that what
makes the tuple is the comma - not the parens[1] -, it is _not_ broken.

[1] with the exception of the empty tuple.

I also realized that you don't need to use parens to make tuple, it's
rather my habit to always use parens in making a tuple (because "print
'%s + %s + %s' % (a, b, c)" _must_ have a parens (otherwise Python
thinks b and c is arguments for the print not for the string
formatting) and because Python always return a tuple w/ parens)

PS: My wording on broken doesn't actually means broken so it won't
work, but rather broken syntactically, making the syntax inconsistent,
funny, illogical, etc. One could argue though that the trailing comma
is a formalized workaround.
Dec 22 '07 #5
Lie
PPS: Actually, what makes a tuple is both the parens and the comma,
with comma as the minimum signifier, inspect this: "str(a) +
str((a,b,c))", you have to use the double parens, one to make the
tuple and the other as part of the str. This harmless little case
gives error if done without the double parens, but what would happen
if we exchange the str into a function that accepts one argument and
several optional arguments (or better yet, one argument and an
optional * argument)?
Dec 22 '07 #6
Russ P. wrote:
Actually, the parens aren't needed, so this works too:

def __init__(self, args=""): self.args = args,

The trailing comma wasn't necessary a while back (pre 2.5?), so
something in Python must have changed. I'd say that it looks a bit
cleaner without the trailing comma, so maybe whatever changed should
get changed back.
as the name implies, "args" is supposed to be a sequence, and is stored
internally as a tuple. if something has changed, it's probably that 2.5
enforces this behaviour via a property.

to fix your code, just replace your own __init__ method with a "pass",
and leave the initialization to the Exception class itself.

</F>

Dec 22 '07 #7
Mel
Lie wrote:
PPS: Actually, what makes a tuple is both the parens and the comma,
with comma as the minimum signifier, inspect this: "str(a) +
str((a,b,c))", you have to use the double parens, one to make the
tuple and the other as part of the str. This harmless little case
gives error if done without the double parens, but what would happen
if we exchange the str into a function that accepts one argument and
several optional arguments (or better yet, one argument and an
optional * argument)?
I think the effect there is operator precedence. In

str(a,b,c)

the function-calling operator () takes over, and the commas are
considered as argument separators. In

str ((a,b,c))

the inner parens present a single tuple-expression to the
function-calling operator.

Just like a+b/c as against (a+b)/c but with esoteric overloading
of ( ) and , .
Mel.
Dec 22 '07 #8
Lie a écrit :
PPS: Actually, what makes a tuple is both the parens and the comma,
Nope, it's definively the comma. You can check the language's grammar,
it's part of the doc. Or just test FWIW:

Python 2.4.3 (#1, Mar 12 2007, 23:32:01)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on
linux2
Type "help", "copyright", "credits" or "license" for more information.
>>a = 1,
type(a)
<type 'tuple'>
>>>
with comma as the minimum signifier, inspect this: "str(a) +
str((a,b,c))", you have to use the double parens, one to make the
tuple and the other as part of the str.
This is a problem of operator precedence.
Dec 22 '07 #9
Lie
On Dec 23, 4:30*am, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
Lie a écrit :
PPS: Actually, what makes a tuple is both the parens and the comma,

Nope, it's definively the comma. You can check the language's grammar,
it's part of the doc. Or just test FWIW:

Python 2.4.3 (#1, Mar 12 2007, 23:32:01)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on
linux2
Type "help", "copyright", "credits" or "license" for more information.
*>>a = 1,
*>>type(a)
<type 'tuple'>
*>>>
with comma as the minimum signifier, inspect this: "str(a) +
str((a,b,c))", you have to use the double parens, one to make the
tuple and the other as part of the str.

This is a problem of operator precedence.
I think some people have misunderstood me, I know parens isn't a
formal definition for tuples, but it's more or less a de facto
grammar, as it is hard to create parens-less tuples without the
interpreter mistaking it for other things, except for a few cases.
Dec 23 '07 #10
On Dec 22, 5:34 am, Fredrik Lundh <fred...@pythonware.comwrote:
Russ P. wrote:
Actually, the parens aren't needed, so this works too:
def __init__(self, args=""): self.args = args,
The trailing comma wasn't necessary a while back (pre 2.5?), so
something in Python must have changed. I'd say that it looks a bit
cleaner without the trailing comma, so maybe whatever changed should
get changed back.

as the name implies, "args" is supposed to be a sequence, and is stored
internally as a tuple. if something has changed, it's probably that 2.5
enforces this behaviour via a property.

to fix your code, just replace your own __init__ method with a "pass",
and leave the initialization to the Exception class itself.
That works. And because it is the simplest solution here, I'd say it's
the "correct" solution. Thanks.

Dec 23 '07 #11
Lie <Li******@gmail.comwrote:

# Python have an odd (read: broken) singleton implementation
# single member tuple must have a comma behind it
Otherwise (1+2)+(3+4) would evaluate to (3, 7) instead of 10.

Florian
--
<http://www.florian-diesch.de/>
-----------------------------------------------------------------------
** Hi! I'm a signature virus! Copy me into your signature, please! **
-----------------------------------------------------------------------
Dec 23 '07 #12

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

Similar topics

1
by: Newsgroup - Ann | last post by:
The fstream is supposed to be a replacement of the stdio.h and the exception is supposed to be a replacement of the return error code. So how do I replace my old codes of the style: if (output =...
3
by: Professor Frink | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
19
by: Diego F. | last post by:
I think I'll never come across that error. It happens when running code from a DLL that tries to write to disk. I added permissions in the project folder, the wwwroot and in IIS to NETWORK_SERVICE...
4
by: Craig831 | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
7
by: Chuck Hartman | last post by:
I have a Windows service that requests web pages from a site using an HttpWebRequest object. When I try to request a page from an ASP.NET 2 site, I get a WebException with message "The remote...
5
by: Lucvdv | last post by:
Can someone explain why this code pops up a messagebox saying the ThreadAbortException wasn't handled? The first exception is reported only in the debug pane, as expected. The second (caused by...
5
by: Sridhar | last post by:
Hi, I have created a web application. I have a data access dll to interact with database. I added this dll as a reference to the web application. Now let's say if I am calling a function in data...
2
by: Abubakar | last post by:
Hi all, I'm writing an app in vc++ 2k5 (all native/unmanaged). This application does a lot of multithreading and socket programming. Its been months since I'm developing this application, at...
12
by: josh | last post by:
Hi I have this code: class Exception : public exception { public: Exception(string m="exception!") : msg(m) {} ~Exception() throw() {} const char* what() { return msg.c_str(); } private:
3
by: Miro | last post by:
I cant seem to find an example on how to do something, ( vb2005.express ) i have a Try ListeningSerialPort.Open() TestText.Enabled = True Catch ex As Exception 'Debug.WriteLine(ex.Message)...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.