Re: [Tutor] Puzzled

2008-08-29 Thread Kent Johnson
On Fri, Aug 29, 2008 at 6:15 PM, ammar azif <[EMAIL PROTECTED]> wrote:
> Thanks for the explanation. Btw, How can I get the size of python primitive
> data types in bytes? Is it defined somewhere in a file that I can look at?

Not really. sys.maxint gives the largest int, from which you can infer the size.

Here is a (complex) recipe that reports sizes:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/546530

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Puzzled

2008-08-29 Thread ammar azif
Thanks for the explanation. Btw, How can I get the size of python primitive 
data types in bytes? Is it defined somewhere in a file that I can look at? 

--- On Fri, 8/29/08, Kent Johnson <[EMAIL PROTECTED]> wrote:
From: Kent Johnson <[EMAIL PROTECTED]>
Subject: Re: [Tutor] Puzzled
To: [EMAIL PROTECTED]
Cc: tutor@python.org
Date: Friday, August 29, 2008, 4:41 PM

On Fri, Aug 29, 2008 at 5:13 PM, ammar azif <[EMAIL PROTECTED]> wrote:
> I wrote a python program that used time() function from the time module to
> retrieve time in seconds since Epoch. After the value was retrieved which
I
> checked is a float by using type(),  the value was then written into a
file
> in binary format. Then another C program that I wrote opened the file and
> converted the value into a time_t variable but it was totally different
from
> the correct value. Then I found that the time_t size is actually 4 byte
> integer which is not the same with 8-byte float value returned by
> time.time(). Why is this so? Being written with C library, isn't
python
> suppose to work well with it?

The C time_t type is very loosely specified; in ANSI C it is only
required to be an arithmetic type. According to Wikipedia
(http://en.wikipedia.org/wiki/Time_t), Posix-compliant systems still
have latitude to implement it as 32 or 64 bits.

Python tries to be bit higher level, giving you fractional seconds if
the implementation supports it and a common data type across
implementations. So there is not an exact match in functionality.

If you want to write data to file in a format that can be read by
another program, you should look at the struct module.

Kent



  ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Puzzled

2008-08-29 Thread Kent Johnson
On Fri, Aug 29, 2008 at 5:13 PM, ammar azif <[EMAIL PROTECTED]> wrote:
> I wrote a python program that used time() function from the time module to
> retrieve time in seconds since Epoch. After the value was retrieved which I
> checked is a float by using type(),  the value was then written into a file
> in binary format. Then another C program that I wrote opened the file and
> converted the value into a time_t variable but it was totally different from
> the correct value. Then I found that the time_t size is actually 4 byte
> integer which is not the same with 8-byte float value returned by
> time.time(). Why is this so? Being written with C library, isn't python
> suppose to work well with it?

The C time_t type is very loosely specified; in ANSI C it is only
required to be an arithmetic type. According to Wikipedia
(http://en.wikipedia.org/wiki/Time_t), Posix-compliant systems still
have latitude to implement it as 32 or 64 bits.

Python tries to be bit higher level, giving you fractional seconds if
the implementation supports it and a common data type across
implementations. So there is not an exact match in functionality.

If you want to write data to file in a format that can be read by
another program, you should look at the struct module.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is this a "Class" problem?

2008-08-29 Thread Kent Johnson
On Fri, Aug 29, 2008 at 12:19 PM, Adrian Greyling
<[EMAIL PROTECTED]> wrote:

> Here's where I get fuzzy...  Let's say I've got a "frame_1" object
> that opens a new "frame_2" object.  As you've suggested above, I'll use "m"
> to create an instance of a frame object.  Now frame_2 opens a "dialog_1'"
> which asks for information that is sent back to 'frame_2'. How do I
> reference 'frame_2' in this case?  Especially when frame_2 hasn't been
> closed and has just been waiting behind dialog_1 until dialog_1 closes.
> When I try to reference it again as "m = frame_2(self)" from a new function
> definition, aren't I creating a brand new frame_2 object that has "blank"
> attributes, so to speak?

Generally the way this works is something like:
- frame 2 creates dialog box
- frame 2 shows dialog box and waits for the dialog box to be dismissed
- frame 2 gets result from dialog box

There are several examples of this in the wx demo, see
MultiChoiceDialog, SingleChoiceDialog, TextEntryDialog. If you are
writing your own custom dialog, make a method that allows the client
code to retrieve the user data from it.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Puzzled

2008-08-29 Thread ammar azif
I wrote a python program that used time() function from the time module to 
retrieve time in seconds since Epoch. After the value was retrieved which I 
checked is a float by using type(),  the value was then written into a file in 
binary format. Then another C program that I wrote opened the file and 
converted the value into a time_t variable but it was totally different from 
the correct value. Then I found that the time_t size is actually 4 byte integer 
which is not the same with 8-byte float value returned by time.time(). Why is 
this so? Being written with C library, isn't python suppose to work well with 
it?



  ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is this a "Class" problem?

2008-08-29 Thread Timothy Grant
On Fri, Aug 29, 2008 at 9:19 AM, Adrian Greyling
<[EMAIL PROTECTED]> wrote:
> Thanks for the response Jeff, although your answer has spawned another
> question or two!  In your answer, you showed that the attribute "
> MySecondFrame.text_ctrl_2" doesn't exist and to correct that, you suggested
> the code below.  (What I understand from your response is that I can't
> reference the original object, but I must create an instance of it.  Is that
> right??)
>
> def MainToSecond(self, event): # wxGlade: MyMainFrame.
>
> m = MySecondFrame(self)
> m.Show()
>
> m.text_ctrl_2.SetValue("This text was generated from the 'MainFrame'
> window")
>
> Here's where I get fuzzy...  Let's say I've got a "frame_1" object
> that opens a new "frame_2" object.  As you've suggested above, I'll use "m"
> to create an instance of a frame object.  Now frame_2 opens a "dialog_1'"
> which asks for information that is sent back to 'frame_2'. How do I
> reference 'frame_2' in this case?  Especially when frame_2 hasn't been
> closed and has just been waiting behind dialog_1 until dialog_1 closes.
> When I try to reference it again as "m = frame_2(self)" from a new function
> definition, aren't I creating a brand new frame_2 object that has "blank"
> attributes, so to speak?
>
> I'm sure I've made things clear as mud, but hopefully with my blathering,
> someone will undertand my utter confusion!
>
> Thanks everyone!
> Adrian
>
>
>
>
> On Mon, Aug 18, 2008 at 3:29 PM, Jeff Younker <[EMAIL PROTECTED]> wrote:
>>
>> On Aug 18, 2008, at 9:13 AM, Adrian Greyling wrote:
>>
>> def MainToSecond(self, event): # wxGlade: MyMainFrame.
>> MySecondFrame(self).Show()
>> MySecondFrame.text_ctrl_2.SetValue("This text was generated from
>> the 'MainFrame' window")
>>
>> The expression MySecondFrame(self) creates a new object.  It
>> initializes the new object by calling the MySecondFrame's __init__
>> method.
>>
>> class MySecondFrame(wx.Frame):
>> def __init__(self, *args, **kwds):
>> # begin wxGlade: MySecondFrame.__init__
>> ...
>>
>>self.text_ctrl_2 = wx.TextCtrl(self, -1, "",
>> style=wx.TE_MULTILINE)
>> ...
>>
>> The __init__ method calls sets the variable text_ctrl_2 in the object
>> m.
>> Your function MainToSecond is trying to get the attribute
>> MySecondFrame.text_ctrl_2.
>> This attribute does not exist.  You want to get the attribute
>> m.text_ctrl_2.  So, the method
>> should be:
>> def MainToSecond(self, event): # wxGlade: MyMainFrame.
>> m = MySecondFrame(self)
>> m.Show()
>> m.text_ctrl_2.SetValue("This text was generated from the
>> 'MainFrame' window")
>>
>> Also, method and function names should always start with a lower case
>> letter: always
>> mainToSecond and never MainToSecond
>> -jeff
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
Adrian,

Two things...

You should check out the wxpython mailing list. It's a much better
place to ask wxPython related questions.

You should check out the wx.lib.pubsub module. It will allow you to
publish data in one object and subscribe to it in another.



-- 
Stand Fast,
tjg. [Timothy Grant]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dynamic argument lists

2008-08-29 Thread Bob Gailer
On Fri, Aug 29, 2008 at 12:20 PM, Kent Johnson <[EMAIL PROTECTED]> wrote:

> On Fri, Aug 29, 2008 at 9:49 AM, eShopping
> <[EMAIL PROTECTED]> wrote:
> > Hi
> >
> > I have a GUI program that extracts some information from the user as
> > strings, and I then want to use the strings to form an argument list to
> > another function.  Hopefully the following code gives some flavour:
> >
> > def myfunc(**kwargs):
> >while kwargs:
> >name, value = kwargs.popitem()
> >print name, value
> >
> > myfunc(a=1, b=2, c=3, d=4)
> > arg_str = "a=1, b=2, c=3, d=4"
> > myfunc(arg_str)
> >
> > ARG_STR will be built up from the data extracted from the GUI.  I get
> this
> > error
> >
> > TypeError: myfunc() takes exactly 0 arguments (1 given)
> >
> > I understand that ARG_STR is a string and that MYFUNC is expecting
> something
> > else ,,, but not sure what it is.  I have tried various dictionary
> > configurations such as
> >
> > arg1 = ["a","b","c","d"]
> > arg2 = [1,2,3,4]
> > arg3 = dict(zip(arg1,arg2))
> > myfunc(arg3)
>

 myfunc(**arg3)

Let's back up to arg_str = "a=1, b=2, c=3, d=4"

To create a dictionary from that:

argDict = dict(pair.split('=') for pair in arg_str.split(','))

If there is no compelling requirement that myfunc's argument be in the form
**kwargs then

def myfunc(kwargs):
while kwargs:
name, value = kwargs.popitem()
print name, value

myfunc(argDict)

-- 
Bob Gailer
919-636-4239
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dynamic argument lists

2008-08-29 Thread Kent Johnson
On Fri, Aug 29, 2008 at 9:49 AM, eShopping
<[EMAIL PROTECTED]> wrote:
> Hi
>
> I have a GUI program that extracts some information from the user as
> strings, and I then want to use the strings to form an argument list to
> another function.  Hopefully the following code gives some flavour:
>
> def myfunc(**kwargs):
>while kwargs:
>name, value = kwargs.popitem()
>print name, value
>
> myfunc(a=1, b=2, c=3, d=4)
> arg_str = "a=1, b=2, c=3, d=4"
> myfunc(arg_str)
>
> ARG_STR will be built up from the data extracted from the GUI.  I get this
> error
>
> TypeError: myfunc() takes exactly 0 arguments (1 given)
>
> I understand that ARG_STR is a string and that MYFUNC is expecting something
> else ,,, but not sure what it is.  I have tried various dictionary
> configurations such as
>
> arg1 = ["a","b","c","d"]
> arg2 = [1,2,3,4]
> arg3 = dict(zip(arg1,arg2))
> myfunc(arg3)

If you want to pass a dict containing the keywords, rather than actual
keywords, the syntax is
myfunc(**arg3)

It parallels the syntax used to define myfunc().

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is this a "Class" problem?

2008-08-29 Thread Adrian Greyling
Thanks for the response Jeff, although your answer has spawned another
question or two!  In your answer, you showed that the attribute "
MySecondFrame.text_ctrl_2" doesn't exist and to correct that, you suggested
the code below.  (What I understand from your response is that I can't
reference the original object, but I must create an instance of it.  Is that
right??)

def MainToSecond(self, event): # wxGlade: MyMainFrame.

m = MySecondFrame(self)
m.Show()

m.text_ctrl_2.SetValue("This text was generated from the 'MainFrame'
window")

Here's where I get fuzzy...  Let's say I've got a "frame_1" object
that opens a new "frame_2" object.  As you've suggested above, I'll use "m"
to create an instance of a frame object.  Now frame_2 opens a "dialog_1'"
which asks for information that is sent back to 'frame_2'. How do I
reference 'frame_2' in this case?  Especially when frame_2 hasn't been
closed and has just been waiting behind dialog_1 until dialog_1 closes.
When I try to reference it again as "m = frame_2(self)" from a new function
definition, aren't I creating a brand new frame_2 object that has "blank"
attributes, so to speak?

I'm sure I've made things clear as mud, but hopefully with my blathering,
someone will undertand my utter confusion!

Thanks everyone!
Adrian





On Mon, Aug 18, 2008 at 3:29 PM, Jeff Younker <[EMAIL PROTECTED]> wrote:

>   On Aug 18, 2008, at 9:13 AM, Adrian Greyling wrote:
>
>  def MainToSecond(self, event): # wxGlade: MyMainFrame.
> MySecondFrame(self).Show()
> MySecondFrame.text_ctrl_2.SetValue("This text was generated from
> the 'MainFrame' window")
>
>
> The expression MySecondFrame(self) creates a new object.  It
> initializes the new object by calling the MySecondFrame's __init__
> method.
>
>  class MySecondFrame(wx.Frame):
>  def __init__(self, *args, **kwds):
> # begin wxGlade: MySecondFrame.__init__
> ...
>
> self.text_ctrl_2 = wx.TextCtrl(self, -1, "",
> style=wx.TE_MULTILINE)
> ...
>
>
>
>  The __init__ method calls sets the variable text_ctrl_2 in the object
> m.
>
> Your function MainToSecond is trying to get the attribute
> MySecondFrame.text_ctrl_2.
> This attribute does not exist.  You want to get the attribute
> m.text_ctrl_2.  So, the method
> should be:
>
>  def MainToSecond(self, event): # wxGlade: MyMainFrame.
> m = MySecondFrame(self)
> m.Show()
> m.text_ctrl_2.SetValue("This text was generated from the
> 'MainFrame' window")
>
>
> Also, method and function names should always start with a lower case
> letter: always
> mainToSecond and never MainToSecond
>
> -jeff
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] dynamic argument lists

2008-08-29 Thread eShopping

Hi

I have a GUI program that extracts some information from the user as 
strings, and I then want to use the strings to form an argument list 
to another function.  Hopefully the following code gives some flavour:


def myfunc(**kwargs):
while kwargs:
name, value = kwargs.popitem()
print name, value

myfunc(a=1, b=2, c=3, d=4)
arg_str = "a=1, b=2, c=3, d=4"
myfunc(arg_str)

ARG_STR will be built up from the data extracted from the GUI.  I get 
this error


TypeError: myfunc() takes exactly 0 arguments (1 given)

I understand that ARG_STR is a string and that MYFUNC is expecting 
something else ,,, but not sure what it is.  I have tried various 
dictionary configurations such as


arg1 = ["a","b","c","d"]
arg2 = [1,2,3,4]
arg3 = dict(zip(arg1,arg2))
myfunc(arg3)

but still get the same error message.  All suggestions welcome!

Thanks in advance

Alun Griffiths

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Excel com strange behavior

2008-08-29 Thread Kent Johnson
On Fri, Aug 29, 2008 at 8:18 AM, johan nilsson <[EMAIL PROTECTED]> wrote:

> "c:\pythonxy\python\lib\site-packages\pywin32-2.11-py2.5-win32.egg\win32com\client\build.py",
> line 542, in 
> return filter( lambda char: char in valid_identifier_chars, className)
> UnicodeDecodeError: 'ascii' codec can't decode byte 0x83 in position 52:
> ordinal not in range(128)

I don't understand how your code is causing this error, but what it
means is that some string that is expected to be ascii contains a
non-ascii character with code 0x83. In Windows code page 1252 that is
an ƒ character.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Excel com strange behavior

2008-08-29 Thread johan nilsson
Dear all,

I have a problem with talking to Excel. Somehow the command Workbooks.Add()
gives me problems?
If I open a workbook in Excel, then I get the trace back below. I can not
say that I quite understand why this does not work. Any insights would be
highly appreciated.

If I "manually" open the workbook, I can fill the data in the sheet

Johan


>>> from win32com.client import Dispatch
>>> xlApp = Dispatch("Excel.Application")
>>> xlApp.Visible = 1
>>> xlApp.Workbooks.Add()

Traceback (most recent call last):
  File "", line 1, in 
xlApp.Workbooks.Add()
  File
"c:\pythonxy\python\lib\site-packages\pywin32-2.11-py2.5-win32.egg\win32com\client\dynamic.py",
line 467, in __getattr__
if self._olerepr_.mapFuncs.has_key(attr): return
self._make_method_(attr)
  File
"c:\pythonxy\python\lib\site-packages\pywin32-2.11-py2.5-win32.egg\win32com\client\dynamic.py",
line 295, in _make_method_
methodCodeList =
self._olerepr_.MakeFuncMethod(self._olerepr_.mapFuncs[name], methodName,0)
  File
"c:\pythonxy\python\lib\site-packages\pywin32-2.11-py2.5-win32.egg\win32com\client\build.py",
line 297, in MakeFuncMethod
return self.MakeDispatchFuncMethod(entry, name, bMakeClass)
  File
"c:\pythonxy\python\lib\site-packages\pywin32-2.11-py2.5-win32.egg\win32com\client\build.py",
line 318, in MakeDispatchFuncMethod
s = linePrefix + 'def ' + name + '(self' + BuildCallList(fdesc, names,
defNamedOptArg, defNamedNotOptArg, defUnnamedArg, defOutArg) + '):'
  File
"c:\pythonxy\python\lib\site-packages\pywin32-2.11-py2.5-win32.egg\win32com\client\build.py",
line 604, in BuildCallList
argName = MakePublicAttributeName(argName)
  File
"c:\pythonxy\python\lib\site-packages\pywin32-2.11-py2.5-win32.egg\win32com\client\build.py",
line 542, in MakePublicAttributeName
return filter( lambda char: char in valid_identifier_chars, className)
  File
"c:\pythonxy\python\lib\site-packages\pywin32-2.11-py2.5-win32.egg\win32com\client\build.py",
line 542, in 
return filter( lambda char: char in valid_identifier_chars, className)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x83 in position 52:
ordinal not in range(128)
>>>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor