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

Home Posts Topics Members FAQ

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.Inconsis tentUnits: 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.Inconsis tentUnits: ('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 InconsistentUni ts(Exception):
def __init__(self, args=""): self.args = args

and here is my instantiation of it:

raise scalar.Inconsis tentUnits, 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 1666
Lie
On Dec 22, 2:57*am, "Russ P." <Russ.Paie...@g mail.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.Inconsis tentUnits: 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.Inconsis tentUnits: ('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 InconsistentUni ts(Exception):
* * * * def __init__(self, args=""): self.args = args

and here is my instantiation of it:

* * * * raise scalar.Inconsis tentUnits, 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 InconsistentUni ts 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 InconsistentUni ts(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 InconsistentUni ts(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.quelque ch...@free.quel quepart.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.quelque ch...@free.quel quepart.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

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

Similar topics

1
4910
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 = fopen(...) == NULL) { perror("..."); exit(1); } by the C++ style? The only thing I can do now is like
3
2940
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 application that is going to be used to extract data from a legacy system (VSAM based mainframe file structure), and output data in pipe-delimited...
19
3196
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 and Everyone, with Full Control to see if it's a permissions problem. The project is hosted in a Windows 2003 Server and developed from PCs in a...
4
2237
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 application that is going to be used to extract data from a legacy system (VSAM based mainframe file structure), and output data in pipe-delimited...
7
2667
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 server returned an error: (500) Internal Server Error." I found a post that suggested to catch the WebException to retrieve the actual HttpWebResponse...
5
5729
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 thread.Abort()) is reported twice: once in the debug window, and once through the message box. Is it because the thread was sleeping when the...
5
1368
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 access and if it throws exception (like cannot find stored procedure), how do I retrieve that exception message back in the web form? In the...
2
24505
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 this point a lot of it is working just fine, my app running fine, threads r being made and destroyed, memory is being dynamically allocated at God...
12
2165
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
3464
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) End Try
0
7605
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
7917
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
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5217
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
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
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
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
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.