Re: [Freevo-devel] Python question
On Mon, 2008-03-17 at 23:32 -0700, Michel Lespinasse wrote: > Not a big deal, but it did trip me up the first time (and, I still don't know > of a good way to create the two-dimensional array with actual separate > copies for each row) A bit kludgy, but you could use a list comprehension: [ [0]*4 for i in range(4) ] The cleanest way is probably to use numeric python's array. - This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse012070mrt/direct/01/ ___ Freevo-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/freevo-devel
Re: [Freevo-devel] Python question
On Mon, Mar 17, 2008 at 12:01:04PM -0400, Jason Tackaberry wrote: > On Mon, 2008-03-17 at 16:47 +0100, Duncan Webb wrote: > > Thanks both Jason and James, it makes sense that the list is created > > when the method is first parsed, this is something that I need to watch > > out for. I guess that this is only a problem for mutable objects, so > > strings and numbers are not a problem. > > They are also evaluated prior to instantiation, but yes, because they > are immutable it's not a problem in practice. A different issue of the same 'shooting your foot in python' kind: array = [[0] * 4] * 3 This creates and initializes a bidimensional array: [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] Now after setting array[1][2] = 1 the array contents are: [[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0]] This is because all rows point to the same one-dimensional array object - though, for some reason, all cells in that object do not point to the same integer. Not a big deal, but it did trip me up the first time (and, I still don't know of a good way to create the two-dimensional array with actual separate copies for each row) -- Michel Lespinasse - This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse012070mrt/direct/01/ ___ Freevo-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/freevo-devel
Re: [Freevo-devel] Python question
On Mon, 2008-03-17 at 16:47 +0100, Duncan Webb wrote: > Thanks both Jason and James, it makes sense that the list is created > when the method is first parsed, this is something that I need to watch > out for. I guess that this is only a problem for mutable objects, so > strings and numbers are not a problem. They are also evaluated prior to instantiation, but yes, because they are immutable it's not a problem in practice. - This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse012070mrt/direct/01/ ___ Freevo-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/freevo-devel
Re: [Freevo-devel] Python question
Jason Tackaberry wrote: > On Mon, 2008-03-17 at 15:42 +0100, Duncan Webb wrote: > >> class A: >> def __init__(self, l=[]): >> self.l = l >> > > Note that this creates a default list for l only once at > class-declaration time. All instances of A will share this list. This > is a common python gotcha. You might want instead: > > class A: >def __init__(self, l=None): > self.l = l or [] > > > >> This behaviour seems a bit strange, clearly list is class A is the same >> objects for both instances and I'm wondering if this is correct? >> > > Yes, it's expected behaviour. > Thanks both Jason and James, it makes sense that the list is created when the method is first parsed, this is something that I need to watch out for. I guess that this is only a problem for mutable objects, so strings and numbers are not a problem. Duncan - This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse012070mrt/direct/01/ ___ Freevo-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/freevo-devel
Re: [Freevo-devel] Python question
On Mon, 2008-03-17 at 15:42 +0100, Duncan Webb wrote: > class A: > def __init__(self, l=[]): > self.l = l Note that this creates a default list for l only once at class-declaration time. All instances of A will share this list. This is a common python gotcha. You might want instead: class A: def __init__(self, l=None): self.l = l or [] > This behaviour seems a bit strange, clearly list is class A is the same > objects for both instances and I'm wondering if this is correct? Yes, it's expected behaviour. Cheers, Jason. - This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse012070mrt/direct/01/ ___ Freevo-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/freevo-devel
Re: [Freevo-devel] Python question
On March 17, 2008, Duncan Webb wrote:
> Here is a bit of code:
>
> class A:
> def __init__(self, l=[]):
> self.l = l
>
> class B:
> def __init__(self):
> self.l = []
>
> a1 = A()
> a2 = A()
>
> b1 = B()
> b2 = B()
>
> a1.l += ['a1']
> a2.l += ['a2']
> b1.l += ['b1']
> b2.l += ['b2']
>
> print 'a1:', a1.__dict__, '0x%08x' % abs(id(a1.l))
> print 'a2:', a2.__dict__, '0x%08x' % abs(id(a2.l))
> print 'b1:', b1.__dict__, '0x%08x' % abs(id(b1.l))
> print 'b2:', b2.__dict__, '0x%08x' % abs(id(b2.l))
>
> That gives:
> a1: {'l': ['a1', 'a2']} 0x484db114
> a2: {'l': ['a1', 'a2']} 0x484db114
> b1: {'l': ['b1']} 0x484db174
> b2: {'l': ['b2']} 0x484db2d4
>
> This behaviour seems a bit strange, clearly list is class A is the same
> objects for both instances and I'm wondering if this is correct?
Default arguments are evaluated when a function or method is defined, not when
the associated code is called.
A somewhat common FAQ on the Python lists is why using live variables in
default arguments doesn't do what you might expect.
I've never had this particular problem, strangely enough. If I have a list
member in an object, it is usually set entirely at some point, instead of
modified. If the list is integral, I usually subclass list to make the
interface more natural.
--
James Oakley
[EMAIL PROTECTED]
-
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse012070mrt/direct/01/
___
Freevo-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-devel
[Freevo-devel] Python question
Here is a bit of code:
class A:
def __init__(self, l=[]):
self.l = l
class B:
def __init__(self):
self.l = []
a1 = A()
a2 = A()
b1 = B()
b2 = B()
a1.l += ['a1']
a2.l += ['a2']
b1.l += ['b1']
b2.l += ['b2']
print 'a1:', a1.__dict__, '0x%08x' % abs(id(a1.l))
print 'a2:', a2.__dict__, '0x%08x' % abs(id(a2.l))
print 'b1:', b1.__dict__, '0x%08x' % abs(id(b1.l))
print 'b2:', b2.__dict__, '0x%08x' % abs(id(b2.l))
That gives:
a1: {'l': ['a1', 'a2']} 0x484db114
a2: {'l': ['a1', 'a2']} 0x484db114
b1: {'l': ['b1']} 0x484db174
b2: {'l': ['b2']} 0x484db2d4
This behaviour seems a bit strange, clearly list is class A is the same
objects for both instances and I'm wondering if this is correct?
Duncan
-
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse012070mrt/direct/01/
___
Freevo-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-devel
Re: AW: [Freevo-devel] Python Question
In the Mpy3 package there is a small module just for the crystal fontz lcds(pyCFontz.py). This may give you some clues on how to do their raw LCD protocol but I am not sure if you want to program all the different types or just a few of the popular ones. I know there is at least a different protocol for a serial convertor common sold on mpja.com and also a parallel protocol and matrix orbital's high end lcd's have their own protocol. This is one of the advatages of using LCDproc. Mike Ruelle [EMAIL PROTECTED] On Fri, 2003-03-14 at 11:12, Starkeeper wrote: > I know LCProc and I think it is to big for such a small job. There are many > modules, with small design for every LCD wich provide access via a > device-file. This seems to be very simple and resource saving. > > -Ursprungliche Nachricht- > Von: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] Auftrag von Thomas > Schueppel > Gesendet: Freitag, 14. Marz 2003 14:38 > An: [EMAIL PROTECTED] > Betreff: Re: [Freevo-devel] Python Question > > > On Fri, 14 Mar 2003, Starkeeper wrote: > > > I try to write a small LCD-script for freevo, but I have no experience in > > programming Python. I did not find anything about device-files on > python.org > > so I ask here. > > How can I print out some text to a device-file with Python? > > For reading or writing just open and handle it like a normal > file. For ioctls there is a special module. > I would suggest to use LCDProc for accessing the LCD. It supports > a large no. of LCDs and has a quite easy TCP interface. > > (see lcdproc.sf.net) > > l8r... > Thomas > > > > --- > This SF.net email is sponsored by:Crypto Challenge is now open! > Get cracking and register here for some mind boggling fun and > the chance of winning an Apple iPod: > http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en > ___ > Freevo-devel mailing list > [EMAIL PROTECTED] > https://lists.sourceforge.net/lists/listinfo/freevo-devel > > > > > > --- > This SF.net email is sponsored by:Crypto Challenge is now open! > Get cracking and register here for some mind boggling fun and > the chance of winning an Apple iPod: > http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en > ___ > Freevo-devel mailing list > [EMAIL PROTECTED] > https://lists.sourceforge.net/lists/listinfo/freevo-devel --- This SF.net email is sponsored by:Crypto Challenge is now open! Get cracking and register here for some mind boggling fun and the chance of winning an Apple iPod: http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en ___ Freevo-devel mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/freevo-devel
AW: [Freevo-devel] Python Question
I know LCProc and I think it is to big for such a small job. There are many modules, with small design for every LCD wich provide access via a device-file. This seems to be very simple and resource saving. -Ursprungliche Nachricht- Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Auftrag von Thomas Schueppel Gesendet: Freitag, 14. Marz 2003 14:38 An: [EMAIL PROTECTED] Betreff: Re: [Freevo-devel] Python Question On Fri, 14 Mar 2003, Starkeeper wrote: > I try to write a small LCD-script for freevo, but I have no experience in > programming Python. I did not find anything about device-files on python.org > so I ask here. > How can I print out some text to a device-file with Python? For reading or writing just open and handle it like a normal file. For ioctls there is a special module. I would suggest to use LCDProc for accessing the LCD. It supports a large no. of LCDs and has a quite easy TCP interface. (see lcdproc.sf.net) l8r... Thomas --- This SF.net email is sponsored by:Crypto Challenge is now open! Get cracking and register here for some mind boggling fun and the chance of winning an Apple iPod: http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en ___ Freevo-devel mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/freevo-devel --- This SF.net email is sponsored by:Crypto Challenge is now open! Get cracking and register here for some mind boggling fun and the chance of winning an Apple iPod: http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en ___ Freevo-devel mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/freevo-devel
Re: [Freevo-devel] Python Question
On Fri, 14 Mar 2003, Starkeeper wrote: > I try to write a small LCD-script for freevo, but I have no experience in > programming Python. I did not find anything about device-files on python.org > so I ask here. > How can I print out some text to a device-file with Python? For reading or writing just open and handle it like a normal file. For ioctls there is a special module. I would suggest to use LCDProc for accessing the LCD. It supports a large no. of LCDs and has a quite easy TCP interface. (see lcdproc.sf.net) l8r... Thomas --- This SF.net email is sponsored by:Crypto Challenge is now open! Get cracking and register here for some mind boggling fun and the chance of winning an Apple iPod: http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en ___ Freevo-devel mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/freevo-devel
[Freevo-devel] Python Question
I try to write a small LCD-script for freevo, but I have no experience in programming Python. I did not find anything about device-files on python.org so I ask here. How can I print out some text to a device-file with Python? --- This SF.net email is sponsored by:Crypto Challenge is now open! Get cracking and register here for some mind boggling fun and the chance of winning an Apple iPod: http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en ___ Freevo-devel mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/freevo-devel
