[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Short confusing example with unicode, print, and __str__

Gerard Brunick

3/5/2008 5:59:00 PM

I really don't understand the following behavior:

>>> class C(object):
.... def __init__(self, s): self.s = s
.... def __str__(self): return self.s
....
>>> cafe = unicode("Caf\xe9", "Latin-1")
>>> c = C(cafe)
>>> print "Print using c.s:", c.s
Print using c.s: Café
>>> print "Print using just c:", c
Print using just c: Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in
position 3: ordinal not in range(128)
>>> str(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in
position 3: ordinal not in range(128)

Why would "print c.s" work but the other two cases throw an exception?
Any help understanding this would be greatly appreciated.

Thanks in advance,
Gerard
3 Answers

OssieMac

3/24/2010 8:01:00 PM

0

Hi Bradley,

There is Analysis ToolPak and Analysis ToolPak - VBA. I think you need both.
However, if still won't work then replace the following code

lngRandom = WorksheetFunction _
.RandBetween(1, lngCells)

with these 2 lines of code

Randomize
lngRandom = Int((lngCells * Rnd) + 1)

--
Regards,

OssieMac


Chip Pearson

3/25/2010 2:32:00 PM

0

>There is Analysis ToolPak and Analysis ToolPak - VBA. I think you need both.
>However, if still won't work then replace the following code

Because RandBetween is not a native Excel function (in 2003 and
earlier), it is not going to be found under WorksheetFunction. If you
have the ATP VBA reference (you don't need both ATP references, just
the VBA one), you can call RandBetween as if it were a native VBA
function. Note that you must have the add-in loaded and your project
must reference the atpvbaen.xla library.

L = RandBetween(1, 100)

When I use functions from another library, I like to qualify the name
of the function with the library name, just to keep things clear and
well documented.

L = [atpvbaen.xls].RandBetween(1, 100)

The [ ] chars are required because the library name contains a period
(and yes, the referenced library is "xls" not "xla").

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com




On Wed, 24 Mar 2010 13:01:02 -0700, OssieMac
<OssieMac@discussions.microsoft.com> wrote:

>Hi Bradley,
>
>There is Analysis ToolPak and Analysis ToolPak - VBA. I think you need both.
>However, if still won't work then replace the following code
>
>lngRandom = WorksheetFunction _
> .RandBetween(1, lngCells)
>
>with these 2 lines of code
>
>Randomize
>lngRandom = Int((lngCells * Rnd) + 1)

OssieMac

3/25/2010 8:19:00 PM

0

Thanks Chip. I've now tested in xl2002. I was not aware that it did not work
in earlier versions of xl.

To Bradly,

Just to make it clear what you need to do.
In the Worksheet you need the Add-In Analysis ToolPak - VBA.
In the VBA Editor select menu item Tools -> References and check the box
against atpvbean.xls. (Ensure you check the box; not just select the line)
and then OK.

Then your code is as follows.

lngRandom = [atpvbaen.xls] _
.RandBetween(1, lngCells)

or you can leave out [atpvbaen.xls]. as follows. (However, I do like Chip's
suggestion to include it because it provides documentation.)

lngRandom = RandBetween(1, lngCells)


or you can use the alternative code I gave you as follows and you then do
not need Analysis ToolPak or the Reference in VBA. Probably the better
solution.

Randomize
lngRandom = Int((lngCells * Rnd) + 1)


--
Regards,

OssieMac


"Chip Pearson" wrote:

> >There is Analysis ToolPak and Analysis ToolPak - VBA. I think you need both.
> >However, if still won't work then replace the following code
>
> Because RandBetween is not a native Excel function (in 2003 and
> earlier), it is not going to be found under WorksheetFunction. If you
> have the ATP VBA reference (you don't need both ATP references, just
> the VBA one), you can call RandBetween as if it were a native VBA
> function. Note that you must have the add-in loaded and your project
> must reference the atpvbaen.xla library.
>
> L = RandBetween(1, 100)
>
> When I use functions from another library, I like to qualify the name
> of the function with the library name, just to keep things clear and
> well documented.
>
> L = [atpvbaen.xls].RandBetween(1, 100)
>
> The [ ] chars are required because the library name contains a period
> (and yes, the referenced library is "xls" not "xla").
>
> Cordially,
> Chip Pearson
> Microsoft Most Valuable Professional,
> Excel, 1998 - 2010
> Pearson Software Consulting, LLC
> www.cpearson.com
>
>
>
>
> On Wed, 24 Mar 2010 13:01:02 -0700, OssieMac
> <OssieMac@discussions.microsoft.com> wrote:
>
> >Hi Bradley,
> >
> >There is Analysis ToolPak and Analysis ToolPak - VBA. I think you need both.
> >However, if still won't work then replace the following code
> >
> >lngRandom = WorksheetFunction _
> > .RandBetween(1, lngCells)
> >
> >with these 2 lines of code
> >
> >Randomize
> >lngRandom = Int((lngCells * Rnd) + 1)
> .
>