unicode bit me

2009-05-08 Thread anuraguni...@yahoo.com
#how can I print a list of object which may return unicode
representation?
# -*- coding: utf-8 -*-

class A(object):

def __unicode__(self):
return u"©au"

__str__ = __repr__ = __unicode__

a = A()

try:
print a # doesn't work?
except UnicodeEncodeError,e:
print e
try:
print unicode(a) # works, ok fine, great
except UnicodeEncodeError,e:
print e
try:
print unicode([a]) # what doesn't work?
except UnicodeEncodeError,e:
print e
"""
Now how can I print a list of object which may return unicode
representation?
loop/map is not an option as it goes much deepr in my real code
any can anyoen explain what is happening here under the hood?
"""
--
http://mail.python.org/mailman/listinfo/python-list


Re: unicode bit me

2009-05-09 Thread anuraguni...@yahoo.com
also not sure why (python 2.5)
print a # works
print unicode(a) # works
print [a] # works
print unicode([a]) # doesn't works
--
http://mail.python.org/mailman/listinfo/python-list


Re: unicode bit me

2009-05-09 Thread anuraguni...@yahoo.com
Sorry being unclear again, hmm I am becoming an expert in it.

I pasted that code as continuation of my old code at start
i.e
 class A(object):
 def __unicode__(self):
 return u"©au"

 def __repr__(self):
 return unicode(self).encode("utf-8")
 __str__ = __repr__

doesn't work means throws unicode error
my question boils down to
what is diff between, why one doesn't throws error and another does
print unicode(a)
vs
print unicode([a])

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


Re: unicode bit me

2009-05-09 Thread anuraguni...@yahoo.com
sorry for not being specfic and not given all info

"""
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
'Linux-2.6.24-19-generic-i686-with-debian-lenny-sid'
"""

My question has not much to do with stdout because I am able to print
unicode
so
print unicode(a) works
print unicode([a]) doesn't

without print too
s1 = u"%s"%a works
s2 = u"%s"%[a] doesn't
niether does s3 = u"%s"%unicode([a])
error is UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in
position 1: ordinal not in range(128)

so question is how can I use a list of object whose representation
contains unicode in another unicode string

I am now using __repr__ = unicode(self).encode("utf-8")
but it give error anyway
--
http://mail.python.org/mailman/listinfo/python-list


Re: unicode bit me

2009-05-09 Thread anuraguni...@yahoo.com
First of all thanks everybody for putting time with my confusing post
and I apologize for not being clear after so many efforts.

here is my last try (you are free to ignore my request for free
advice)

# -*- coding: utf-8 -*-

class A(object):

def __unicode__(self):
return u"©au"

def __repr__(self):
return unicode(self).encode("utf-8")

__str__ = __repr__

a = A()
u1 = unicode(a)
u2 = unicode([a])

now I am not using print so that doesn't matter stdout can print
unicode or not
my naive question is line u2 = unicode([a]) throws
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position
1: ordinal not in range(128)

shouldn't list class call unicode on its elements? I was expecting
that
so instead do i had to do this
u3 = "["+u",".join(map(unicode,[a]))+"]"
--
http://mail.python.org/mailman/listinfo/python-list


Re: unicode bit me

2009-05-09 Thread anuraguni...@yahoo.com
and yes replace string by u'\N{COPYRIGHT SIGN}au'
as mentioned earlier non-ascii char may not come correct posted here.

On May 10, 9:19 am, "[email protected]" 
wrote:
> First of all thanks everybody for putting time with my confusing post
> and I apologize for not being clear after so many efforts.
>
> here is my last try (you are free to ignore my request for free
> advice)
>
> # -*- coding: utf-8 -*-
>
> class A(object):
>
>     def __unicode__(self):
>         return u"©au"
>
>     def __repr__(self):
>         return unicode(self).encode("utf-8")
>
>     __str__ = __repr__
>
> a = A()
> u1 = unicode(a)
> u2 = unicode([a])
>
> now I am not using print so that doesn't matter stdout can print
> unicode or not
> my naive question is line u2 = unicode([a]) throws
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position
> 1: ordinal not in range(128)
>
> shouldn't list class call unicode on its elements? I was expecting
> that
> so instead do i had to do this
> u3 = "["+u",".join(map(unicode,[a]))+"]"

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


Re: unicode bit me

2009-05-10 Thread anuraguni...@yahoo.com
ok that explains it,
so
unicode(obj) calls __unicode__ on that object and if it isn't there
__repr__ is used
__repr__ of list by default return a str even if __repr__ of element
is unicode


so my only solution looks like to use my own list class everywhere i
use list
class mylist(list):
def __unicode__(self):
return u"["+u''.join(map(unicode,self))+u"]"
--
http://mail.python.org/mailman/listinfo/python-list


Re: unicode bit me

2009-05-10 Thread anuraguni...@yahoo.com
yes but my list sometimes have list of lists

On May 10, 2:59 pm, "Diez B. Roggisch"  wrote:
> [email protected] schrieb:
>
> > ok that explains it,
> > so
> > unicode(obj) calls __unicode__ on that object and if it isn't there
> > __repr__ is used
> > __repr__ of list by default return a str even if __repr__ of element
> > is unicode
>
> > so my only solution looks like to use my own list class everywhere i
> > use list
> > class mylist(list):
> >     def __unicode__(self):
> >         return u"["+u''.join(map(unicode,self))+u"]"
>
> Or you use a custom unicode_list-function whenever you care to print out
>   a list.
>
> Diez

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


Re: unicode bit me

2009-05-11 Thread anuraguni...@yahoo.com
On May 11, 10:47 am, Terry Reedy  wrote:
> [email protected] wrote:
> > so unicode(obj) calls __unicode__ on that object
>
> It will look for the existence of type(ob).__unicode__ ...
>
>  > and if it isn't there __repr__ is used
>
> According to the below, type(ob).__str__ is tried first.
>
> > __repr__ of list by default return a str even if __repr__ of element
> > is unicode
>
>  From the fine library manual, built-in functions section:
> (I reccommend using it, along with interactive experiments.)
>
> "repr( object)
> Return a string ..."
>
> "str( [object])
> Return a string ..."
>
> "unicode( [object[, encoding [, errors]]])
>
> Return the Unicode string version of object using one of the following
> modes:
>
> If encoding and/or errors are given, ...
>
> If no optional parameters are given, unicode() will mimic the behaviour
> of str() except that it returns Unicode strings instead of 8-bit
> strings. More precisely, if object is a Unicode string or subclass it
> will return that Unicode string without any additional decoding applied.
>
> For objects which provide a __unicode__() method, it will call this
> method without arguments to create a Unicode string. For all other
> objects, the 8-bit string version or representation is requested and
> then converted to a Unicode string using the codec for the default
> encoding in 'strict' mode.
> "
>
> 'unicode(somelist)' has no optional parameters, so skip to third
> paragraph.  Somelist is not a unicode instance, so skip to the last
> paragraph.  If you do dir(list) I presume you will *not* see
> '__unicode__' listed.  So skip to the last sentence.
> unicode(somelist) == str(somelist).decode(default,'strict').
>
> I do not believe str() and repr() are specifically documented for
> builtin classes other than the general description, but you can figure
> that str(collection) or repr(collection) will call str or repr on the
> members of the collection in order to return a str, as the doc says.
Thanks for the explanation.

> (Details are available by experiment.)  Str(uni_string) encodes with the
> default encoding, which seems to be 'ascii' in 2.x.  I am sure it uses
> 'strict' errors.
>
> I would agree that str(some_unicode) could be better documented, like
> unicode(some_str) is.
>
> > so my only solution looks like to use my own list class everywhere i
> > use list
> > class mylist(list):
> >     def __unicode__(self):
> >         return u"["+u''.join(map(unicode,self))+u"]"
>
> Or write a function and use that instead, or, if and when you can,
> switch to 3.x where str and repr accept and produce unicode.
>
> tjr

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


How to catch str exception?

2009-05-15 Thread anuraguni...@yahoo.com

import sys
try:
raise "xxx"
except str,e:
print "1",e # is not caught here
except:# is caught here
print "2",sys.exc_type,sys.exc_value

In the above code a string exception is raised which though deprecated
but still a 3rd party library I use uses it. So how can I catch such
exception without relying on catch all, which could be bad.

system: Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3
(Ubuntu 4.2.3-2ubuntu7)] on linux2
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to catch str exception?

2009-05-15 Thread anuraguni...@yahoo.com
but the whole point of catching such exception is that i can print its
value
there are many such exceptions and hence it is not feasible to catch
them all or know them all unless i go thru src code.

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


Re: How to catch str exception?

2009-05-15 Thread anuraguni...@yahoo.com

> try:
>     raise "abc"
> except:
>     e, t, tb = sys.exc_info()
>     if not isinstance(e, str):
>         raise
>     print "caught", e

This seems to be the solution, thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to catch str exception?

2009-05-15 Thread anuraguni...@yahoo.com

> except TypeError,e:
>     print "1",e # is caught here
>
> 1 exceptions must be classes or instances, not str

doesn't happen so in 2.5.2
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to catch str exception?

2009-05-15 Thread anuraguni...@yahoo.com

> It would be better to write your own exception class:
>
> class MyException(Exception):
>      pass


and how would i automatically inject this into 3rd part library
-- 
http://mail.python.org/mailman/listinfo/python-list