Re: [Tutor] Conflict with encoding in console view and file dump

2010-08-04 Thread Alex Baraibar
Hello, Peter. I checked my sys.stdout.encoding and it prints: >>> import sys >>> sys.stdout.encoding 'cp1252' Sometimes IDLE tells me I'm using characters that might not work if I don't place this at the beginning of my script: # -*- coding: cp1252 -*- Other than that, I have not made any change

Re: [Tutor] Conflict with encoding in console view and file dump

2010-08-04 Thread Peter Otten
Alex Baraibar wrote: > I just wish I understood better what happened and why. I've looked into > the Unicode section of the official docs, but I probably need to study it > more in order to fully understand it, so... Just in case you didn't come across it, there's a howto that covers the basics:

[Tutor] how to get str() to use my function?

2010-08-04 Thread Alex Hall
Hi all, I have a card class. A card object simply consists of a pair of numbers; 0,0 might be the ace of clubs, for example. I have a toString method in my card class. Is there a way to just say str(card) instead of card.toString()? Maybe some sort of basic, built-in function to override? TIA. Oh,

Re: [Tutor] how to get str() to use my function?

2010-08-04 Thread James Mills
On Thu, Aug 5, 2010 at 12:37 AM, Alex Hall wrote: > Hi all, > I have a card class. A card object simply consists of a pair of > numbers; 0,0 might be the ace of clubs, for example. I have a toString > method in my card class. Is there a way to just say str(card) instead > of card.toString()? Maybe

Re: [Tutor] how to get str() to use my function?

2010-08-04 Thread Huy Ton That
You could write __str__ function >>> class card(object): ... def __init__(self, card1, card2): ... self.card1, self.card2 = card1, card2 ... def __str__(self): ... return str(str(self.card1)+','+str(self.card2)) ... >>> a = card(0,0) >>> str(a) '0,0' On Wed, Aug 4,

Re: [Tutor] how to get str() to use my function?

2010-08-04 Thread Alex Hall
It worked, thanks. Is there a list of these functions somewhere? That is, the functions that map implicitly to operators or implied uses? For example, printing will call __str__, as will a cal to str(). What about math or comparison operators? I have heard of __eq__, __gt__, and so on, but I tried

Re: [Tutor] how to get str() to use my function?

2010-08-04 Thread Knacktus
Am 04.08.2010 17:37, schrieb Alex Hall: It worked, thanks. Is there a list of these functions somewhere? That is, the functions that map implicitly to operators or implied uses? For example, printing will call __str__, as will a cal to str(). What about math or comparison operators? I have heard

Re: [Tutor] how to get str() to use my function?

2010-08-04 Thread Huy Ton That
These are special method names. View section 3.4 and below here http://docs.python.org/reference/datamodel.html On Wed, Aug 4, 2010 at 11:37 AM, Alex Hall wrote: > It worked, thanks. Is there a list of these functions somewhere? That > is, the functions that map implicitly to operators or impl

Re: [Tutor] how to get str() to use my function?

2010-08-04 Thread Alex Hall
Thanks, and I also see what I did wrong with my __eq__ function. On 8/4/10, Huy Ton That wrote: > These are special method names. > > View section 3.4 and below here > > http://docs.python.org/reference/datamodel.html > > On Wed, Aug 4, 2010 at 11:37 AM, Alex Hall wrote: > >> It worked, thanks.

[Tutor] getattr()

2010-08-04 Thread Pete
Hi, I'm trying to understand the syntax for reflection in python. I was wondering about this. From the example on diveintopython: http://diveintopython.org/power_of_introspection/index.html import statsout def output(data, format="text"): output_function = getattr(statsout, "output_%s"

[Tutor] Formatting date/time

2010-08-04 Thread Eduardo Vieira
I'm trying this example from python docs: from time import gmtime, strftime strftime("%a, %d %b %Y %H:%M:%S +", gmtime()) Output = 'Wed, 04 Aug 2010 17:58:42 +' Is not there a string formatting option for the day without a leading zero? Like: 'Wed, 4 Aug 2010 17:58:42 +' It looks like

Re: [Tutor] Formatting date/time

2010-08-04 Thread Eric Hamiter
There are a few solutions here: http://stackoverflow.com/questions/904928/python-strftime-date-decimal-remove-0 Eric On Wed, Aug 4, 2010 at 1:30 PM, Eduardo Vieira wrote: > I'm trying this example from python docs: > from time import gmtime, strftime > strftime("%a, %d %b %Y %H:%M:%S +", g

Re: [Tutor] getattr()

2010-08-04 Thread Pete
Hey Huy, thanks. But what is the first parameter in this case? (Note there is nothing here I'm trying to accomplish except understand). In the original example, 'statsout' is the name of the module. In this case there is no module so why can you substitute 'output_text'? thanks, Pete On 2010

Re: [Tutor] Formatting date/time

2010-08-04 Thread Joel Goldstick
On Wed, Aug 4, 2010 at 2:45 PM, Eric Hamiter wrote: > There are a few solutions here: > > > http://stackoverflow.com/questions/904928/python-strftime-date-decimal-remove-0 > > Eric > > > > On Wed, Aug 4, 2010 at 1:30 PM, Eduardo Vieira wrote: > >> I'm trying this example from python docs: >> from

Re: [Tutor] getattr()

2010-08-04 Thread Huy Ton That
*You can do like below Pete. Where globals has a reference to all functions in this space.* def output(data, format="text"): output_function = getattr(globals()['FUNCTION'], "output_%s" % format, statsout.output_text) return output_function(data) On Wed, Aug 4, 2010 at 3:18 PM, Pete wr

Re: [Tutor] getattr()

2010-08-04 Thread bob gailer
On 8/4/2010 1:23 PM, Pete wrote: Hi, I'm trying to understand the syntax for reflection in python. I was wondering about this. From the example on diveintopython: http://diveintopython.org/power_of_introspection/index.html import statsout def output(data, format="text"): output_func

Re: [Tutor] getattr()

2010-08-04 Thread Huy Ton That
Oh, that's right, I should have tried to example in the interpreter instead of in my head:P Say Bob, Is that the preferred method over something like: >>> import __main__ as main On Wed, Aug 4, 2010 at 3:32 PM, bob gailer wrote: > On 8/4/2010 1:23 PM, Pete wrote: > > Hi, > > I'm trying to u

Re: [Tutor] getattr()

2010-08-04 Thread bob gailer
On 8/4/2010 3:44 PM, Huy Ton That wrote: Oh, that's right, I should have tried to example in the interpreter instead of in my head:P Say Bob, Is that the preferred method over something like: I would prefer to create a class and make these functions class methods. class A: def output_text

Re: [Tutor] Formatting date/time

2010-08-04 Thread Eduardo Vieira
On Wed, Aug 4, 2010 at 12:45 PM, Eric Hamiter wrote: > There are a few solutions here: > > http://stackoverflow.com/questions/904928/python-strftime-date-decimal-remove-0 > > Eric > > > On Wed, Aug 4, 2010 at 1:30 PM, Eduardo Vieira > wrote: >> >> I'm trying this example from python docs: >> from

Re: [Tutor] getattr()

2010-08-04 Thread bob gailer
On 8/4/2010 4:04 PM, bob gailer wrote: On 8/4/2010 3:44 PM, Huy Ton That wrote: Oh, that's right, I should have tried to example in the interpreter instead of in my head:P Say Bob, Is that the preferred method over something like: I would prefer to create a class and make these functions cl

[Tutor] access class through indexing?

2010-08-04 Thread Alex Hall
Hi all, Further to my questions about overriding builtin methods earlier, how would I make a class able to be accessed and changed using index notation? For example, take the following: deck=CardPile(52) #creates a new deck of cards print(len(deck)) #prints 52, thanks to my __len__ function for c i

Re: [Tutor] access class through indexing?

2010-08-04 Thread Evert Rol
> Hi all, > Further to my questions about overriding builtin methods earlier, how > would I make a class able to be accessed and changed using index > notation? For example, take the following: > deck=CardPile(52) #creates a new deck of cards > print(len(deck)) #prints 52, thanks to my __len__ func

Re: [Tutor] getattr()

2010-08-04 Thread Huy Ton That
I have a side question, I am using python 2.7. Why do you use class A: instead of class A(object): ? -Huy On Wed, Aug 4, 2010 at 4:08 PM, bob gailer wrote: > On 8/4/2010 4:04 PM, bob gailer wrote: > >> On 8/4/2010 3:44 PM, Huy Ton That wrote: >> >>> Oh, that's right, I should have tried to ex

Re: [Tutor] getattr()

2010-08-04 Thread bob gailer
class A: def call_by_name(self, func, data): f = A.__dict__.get(func, self.output_text) return f(data) def output_text(self, data):return data def output_hex(self, data):return '\\x' + data a = A() data = 'bar' print a.call_by_name('output_text', data) print a.call_by_name('output_he

Re: [Tutor] access class through indexing?

2010-08-04 Thread Jerry Hill
On Wed, Aug 4, 2010 at 4:17 PM, Alex Hall wrote: > Hi all, > Further to my questions about overriding builtin methods earlier, how > would I make a class able to be accessed and changed using index > notation? For example, take the following: > deck=CardPile(52) #creates a new deck of cards > pri

Re: [Tutor] access class through indexing?

2010-08-04 Thread Alex Hall
On 8/4/10, Jerry Hill wrote: > On Wed, Aug 4, 2010 at 4:17 PM, Alex Hall wrote: > >> Hi all, >> Further to my questions about overriding builtin methods earlier, how >> would I make a class able to be accessed and changed using index >> notation? For example, take the following: >> deck=CardPile(

Re: [Tutor] access class through indexing?

2010-08-04 Thread Evert Rol
>>> Further to my questions about overriding builtin methods earlier, how >>> would I make a class able to be accessed and changed using index >>> notation? For example, take the following: >>> deck=CardPile(52) #creates a new deck of cards >>> print(len(deck)) #prints 52, thanks to my __len__ func

Re: [Tutor] access class through indexing?

2010-08-04 Thread Alex Hall
On 8/4/10, Evert Rol wrote: Further to my questions about overriding builtin methods earlier, how would I make a class able to be accessed and changed using index notation? For example, take the following: deck=CardPile(52) #creates a new deck of cards print(len(deck)) #pr

Re: [Tutor] access class through indexing?

2010-08-04 Thread Evert Rol
On 4 Aug 2010, at 23:28 , Alex Hall wrote: > On 8/4/10, Evert Rol wrote: > Further to my questions about overriding builtin methods earlier, how > would I make a class able to be accessed and changed using index > notation? For example, take the following: > deck=CardPile(52) #cr

Re: [Tutor] access class through indexing?

2010-08-04 Thread Jerry Hill
On Wed, Aug 4, 2010 at 5:28 PM, Alex Hall wrote: > Here is my init, not half as pretty as yours, but it should work. > Maybe this will explain the problem. > > def __init__(self, size, cards=None, fill=False): > #creates a pile of cards. If "fill"==true, it will auto-fill the > pile starting fro

Re: [Tutor] access class through indexing?

2010-08-04 Thread Dave Angel
Alex Hall wrote: On 8/4/10, Evert Rol wrote: That depends how you create the Pile of 52 cards: list(52) also doesn't generate 52 (random) items. If you override __init__ to accept an integer that generates the cards for you, this should work. Here is my init, not half as pretty as you

Re: [Tutor] global exception handling?

2010-08-04 Thread Che M
> > But, the issue is, I have many places where I write to the database and > > would have to embed each of these in a try/except block. I can do that, > > but wondered if there is a "global" way to catch this 'database is locked' > > error? I found someone asking this sort of question onlin

Re: [Tutor] access class through indexing?

2010-08-04 Thread Alex Hall
On 8/4/10, Dave Angel wrote: > Alex Hall wrote: >> On 8/4/10, Evert Rol wrote: >> >>> >>> That depends how you create the Pile of 52 cards: list(52) also doesn't >>> generate 52 (random) items. >>> If you override __init__ to accept an integer that generates the cards >>> for >>> you, this shoul

[Tutor] string to list

2010-08-04 Thread Vikram K
Suppose i have this string: z = 'AT/CG' How do i get this list: zlist = ['A','T/C','G'] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor