Re: [Tutor] datetime syntax error for May 8th and 9th 2008??

2008-05-20 Thread Che M



> Date: Fri, 16 May 2008 23:38:42 -0700
> From: [EMAIL PROTECTED]
> To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
> Subject: Re: [Tutor] datetime syntax error for May 8th and 9th 2008??
> CC: tutor@python.org
> 
> On Fri, May 16, 2008 at 9:29 PM, John Fouhy <[EMAIL PROTECTED]> wrote:
> > On 17/05/2008, Che M <[EMAIL PROTECTED]> wrote:
> >> >>> datetime.datetime(2008, 05, 08)
> >> SyntaxError: invalid token
> >
> > It's simpler than that... Try this:
> >
>  x = 08
> >  File "", line 1
> >x = 08
> > ^
> > SyntaxError: invalid token
>  x = 010
>  x
> > 8
> >
> > Basically, python interprets integer literals starting with 0 as octal
> > numbers.  It's an old convention from C (or earlier?).  It doesn't
> > affect strings, so int('010') == 10 (unless you use eval).
> 
> 
> che,
> 
> john is correct.  any leading zero (0) of an integer number will be
> translated as base 8 or octal, which counts by 8's:
> 
> 00, 01, 02, 03, 04, 05, 06, 07, 10 (decimal/base 10: 8*1+0=8),
> 11 (decimal: 8*1+1=9), 12 (decimal: 8*1+2=10), 13 (decimal 8+3=11),
> 14, 15, 16, 17 (decimal: 15), 20 (decimal: 8*2+0=16), 21 (decimal:
> 8*2+1=17), etc...
> 
> the problem with "08" is that because you're counting by 8s, a value
> of "08" is invalid because the digits "8" and "9" are not part of the
> octal "alphabet" or character set. that is why "07" works but not
> "08".
> 
> as an FYI, because of this confusion, starting with Python 3.0, octal
> numbers will require a lowercase "o" (in the same manner as
> hexadecimal/base 16 numbers require an "x") after leading 0, i.e.,
> 0o7, 0o16, etc.  (Oo8 is still an invalid octal number.)
> 
> hope this helps!
> -- wesley
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Thank you, Wesley, that is helpful and interesting.  
Best regards,
Che

_
E-mail for the greater good. Join the i’m Initiative from Microsoft.
http://im.live.com/Messenger/IM/Join/Default.aspx?source=EML_WL_ GreaterGood___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pythoncom Tutorial

2008-05-20 Thread Alan Gauld


"FT" <[EMAIL PROTECTED]> wrote

   A book is nice if you can see to read it. Still searching for the 
best
way to go on this. It was suggested to go to the API of windowes or 
ctypes.
I confress that I have not used ctypes when just hoping to find a 
python
format to get such things as the focus information when jumping from 
window


ctypes is a Python solution to accessing the windows API.
It avoids COM which is an extra layer of complexity.

You can also use the pythonwin package without COM and access
the API that way but there is little to choose between pythonwin and
ctypes inthat case and ctypes seems to be a bit more powerful with
the low level APIs. At least that's my impression.

I doubt if you will find a high level Python solution to very platform
specific things like grabbing focus, screen scraping etc You will need
to get your hands dirty and use the Win32 API functions I think.

Alan G 



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


Re: [Tutor] timed functions? Timeouts?

2008-05-20 Thread Kent Johnson
On Mon, May 19, 2008 at 11:23 PM, W W <[EMAIL PROTECTED]> wrote:
> Those examples are really helpful and I'm pretty sure they'll do what
> I need, I'll just have to play with them a little more.
>
> I do have a question about one of the examples though!
>
> Specifically this part:
>
>  4 class Operation(threading._Timer):
>  5 def __init__(self, *args, **kwargs):
>  6 threading._Timer.__init__(self, *args, **kwargs)
>  7 self.setDaemon(True)
>
> So in trying to understand the class definition, I found this:
>
> "In Python, the ancestor of a class is simply listed in parentheses
> immediately after the class name."
>
> So does that mean that Operation is similar or the same as say:
>
> Operation = threading._Timer?

No.

class Operation(threading._Timer)
creates a new class called Operation that inherits from
threading._Timer. I.e. threading._Timer is the base class, or
superclass, of Operation.

Operation = threading._Timer
this just makes the name Operation be an alias for threading._Timer;
it doesn't create a new class.

> Then I'm a little confused by the * and ** - they look just like the
> pointer and pointer to a pointer in C++, but do they perform the same
> function in python?

No, these are not pointers, they allow passing arbitrary lists and
dicts of arguments. I don't know of a good writeup of this syntax;
here are some pointers:
http://bytes.com/forum/thread25464.html

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


[Tutor] listing classes

2008-05-20 Thread Laureano Arcanio
Hi All,

I need to have a listing of all classes defined inside a class body,
something like this:

class A(object):
class B(object):
pass
class C(object):
pass(object):

and i need to get the classes to instantiate them.. something like this.

classes =[A,B]

Any ideas ? do i need meta classes or something ?

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


Re: [Tutor] listing classes

2008-05-20 Thread Thomas Pani

Hi,

dir(A) will essentially give you what you want (and a little more)

If you're only interested in classes, you can do something like:

import types
[ name for name in dir(A) if type(eval('A.'+name)) == types.ClassType ]

Thomas

Laureano Arcanio wrote:

Hi All,

I need to have a listing of all classes defined inside a class body,
something like this:

class A(object):
class B(object):
pass
class C(object):
pass(object):

and i need to get the classes to instantiate them.. something like this.

classes =[A,B]

Any ideas ? do i need meta classes or something ?

Thanks in advice.





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


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


Re: [Tutor] listing classes

2008-05-20 Thread python
Thomas,

> import types
> [ name for name in dir(A) if type(eval('A.'+name)) == types.ClassType ]

The == types.ClassType doesn't seem to pick out the classes.

Also, I think you should be returning eval( name ) vs. name so that the
OP gets a list of objects vs. names? (My take on what the poster
wanted).

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


Re: [Tutor] listing classes

2008-05-20 Thread Kent Johnson
On Tue, May 20, 2008 at 12:47 PM, Thomas Pani <[EMAIL PROTECTED]> wrote:
> Hi,
>
> dir(A) will essentially give you what you want (and a little more)
>
> If you're only interested in classes, you can do something like:
>
> import types
> [ name for name in dir(A) if type(eval('A.'+name)) == types.ClassType ]

There is no need to use eval() here. Python has powerful introspection
capabilities - use getattr() to get a named attribute of an object.
For example:

In [1]: class A:
   ...: class B:
   ...: pass
   ...: class C:
   ...: pass

In [2]: dir(A)
Out[2]: ['B', 'C', '__doc__', '__module__']

In [3]: type(A.B)
Out[3]: 

In [4]: type(A)
Out[4]: 

In [5]: [ name for name in dir(A) if type(getattr(A, name))==type(A) ]
Out[5]: ['B', 'C']

Note: types.ClassObj is the type of old-style classes. The OP used
new-style classes which are of type type. Using type(A) for the
comparison means it will work with either kind of classes as long as
they are the same. You could also use inspect.isclass() to decide if
it is a class.

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


[Tutor] Fwd: timed functions? Timeouts?

2008-05-20 Thread Kent Johnson
Forwarding to the list.


-- Forwarded message --
From: W W <[EMAIL PROTECTED]>
Date: Tue, May 20, 2008 at 12:31 PM
Subject: Re: [Tutor] timed functions? Timeouts?
To: Kent Johnson <[EMAIL PROTECTED]>


On Tue, May 20, 2008 at 7:18 AM, Kent Johnson <[EMAIL PROTECTED]> wrote:
>> Then I'm a little confused by the * and ** - they look just like the
>> pointer and pointer to a pointer in C++, but do they perform the same
>> function in python?
>
> No, these are not pointers, they allow passing arbitrary lists and
> dicts of arguments. I don't know of a good writeup of this syntax;
> here are some pointers:
> http://bytes.com/forum/thread25464.html

Ahhh, now I see (I think). Here's what I've discovered.

The * unpacks the list into a tuple, creating a "new" variable that as
far as I can tell, dies when the function ends.

** creates a copy of the dictionary, rather than passing the actual
dictionary, so it's possible to operate on it, leaving the original
dict intact.

Neither value is actually considered an argument:

>>> def SomeFunction(**foo):
... print foo
...
>>> mydict = {'abc':123}
>>> SomeFunction(mydict)
Traceback (most recent call last):
 File "", line 1, in 
TypeError: SomeFunction() takes exactly 0 arguments (1 given)
>>> SomeFunction(**mydict)
{'abc': 123}


Using a list without * doesn't create an error at all, instead it
yields results I didn't expect:

>>> def SomeFunction(*foo):
... print foo
...
>>> mylist = ['a', 'b', 'c']
>>> SomeFunction(mylist)
(['a', 'b', 'c'],)


Specifically, it doesn't unpack the list, it just packs the list into a tuple.

With a little more experimentation I've discovered that you can
manually enter data, instead of using a list:

>>> def another_function(*foo):
... print foo
...
>>> another_function('hello', 'spork', 1,2,3)
('hello', 'spork', 1, 2, 3)

Now I just have to figure out exactly how thread/threading uses these tuples!

Thanks again!
-Wayne

--
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] listing classes

2008-05-20 Thread Alan Gauld


"Laureano Arcanio" <[EMAIL PROTECTED]> wrote


I need to have a listing of all classes defined inside a class body,
something like this:

class A(object):
   class B(object):
   pass
   class C(object):
   pass(object):


Others have answered but I'm curious why you would want
to have such a structure. Defining nested classes is a fairly
unusual construct only used under fairly specific conditions.
It also limits reuse opportunities and flexibility quite a bit.

Is there any reason why you can't just define the classes
individually and put them in a list or dictionary? That would
be much simpler.

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



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


Re: [Tutor] timed functions? Timeouts?

2008-05-20 Thread Alan Gauld


"Kent Johnson" <[EMAIL PROTECTED]> wrote

Then I'm a little confused by the * and ** - they look just like 
the
pointer and pointer to a pointer in C++, but do they perform the 
same

function in python?


No, these are not pointers, they allow passing arbitrary lists and
dicts of arguments. I don't know of a good writeup of this syntax;


Soince you (the OP) seem to know some C the */** parameters
are somewhat similar to varargs in C. (as used in functions like
printf)

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



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


Re: [Tutor] timed functions? Timeouts?

2008-05-20 Thread Moishy Gluck
#A single " * " accepts arbitrary amount of unnamed parameters. Two " ** "
accepts an arbitrary amount of unnamed parameters.


# Using asterisk.
def FuncOne(*list, **dict):

for color in list:
print color

print ""

for [color, value] in dict.items():
print color.ljust(5), ":", value


# Not using asterisk.
def FuncTwo(list, dict):

for color in list:
print color

print ""

for [color, value] in dict.items():
print color.ljust(5), ":", value

#A single " * " turns a list into unnamed parameters. Two " ** " dictionary
in named parameters.

colors = ["red", "green", "blue"]

rgbValues = {"red": "ff", "green": "00ff00", "blue": "ff", }

print "FuncOne:\n"

# Using asterisk.
FuncOne(*colors, **rgbValues)



print "\n\nFuncTwo:\n"

# Not using asterisk.
FuncTwo(colors, rgbValues)

---
outputs to:

FuncOne:

red
green
blue

blue  : ff
green : 00ff00
red   : ff


FuncTwo:

red
green
blue

blue  : ff
green : 00ff00
red   : ff
On Tue, May 20, 2008 at 2:12 PM, Alan Gauld <[EMAIL PROTECTED]>
wrote:

>
> "Kent Johnson" <[EMAIL PROTECTED]> wrote
>
>  Then I'm a little confused by the * and ** - they look just like the
>>> pointer and pointer to a pointer in C++, but do they perform the same
>>> function in python?
>>>
>>
>> No, these are not pointers, they allow passing arbitrary lists and
>> dicts of arguments. I don't know of a good writeup of this syntax;
>>
>
> Soince you (the OP) seem to know some C the */** parameters
> are somewhat similar to varargs in C. (as used in functions like
> printf)
>
> HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Re Open a directory in the default file manager

2008-05-20 Thread Tony Cappellini
Message: 2
Date: Thu, 15 May 2008 20:10:05 +0200
From: Tim Michelsen <[EMAIL PROTECTED]>
Subject: [Tutor] Open a directory in the default file manager
To: tutor@python.org
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-15; format=flowed

Hello,
>>is there any function/module that allows me to open a directory in the
>>default file manager of a operating system?

The os module gives you file & directory management functions, but not a
graphical file manager.
The os module also has many functions for spawning other processes



This doesn't open a File Manager- per se, it opens a platform independent
File Browser, and from it you can do some limited functions that a file
manager would.

Try EasyDialogs
(On Windows you need to install it first, it's part of the Mac python
distribution, but is not for Windows.

EasyDialogs.AskFolder

also

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


Re: [Tutor] Tutor Open a directory in the default file manager

2008-05-20 Thread Tony Cappellini
*Tim Michelsen* timmichelsen at gmx-topmail.de

*Mon May 19 21:36:30 CEST 2008*

   - Previous message: [Tutor] Open a directory in the default file manager
   
   - Next message: [Tutor] Getting started with Python
   
   - *Messages sorted by:* [ date
] [
   thread ]
[
   subject ]
[
   author ]


**

>>I wasn't able to start explorer with os.startfile()

You need to use it like this
os.startfile('file.txt')

but it will launch the application associated with .TXT files- which
could be anything on Windows.
It wont launch Explorer unless you have Explorer associated with that extension.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] listing classes

2008-05-20 Thread Dave Kuhlman
On Tue, May 20, 2008 at 01:48:03PM -0400, Kent Johnson wrote:
> 
> Note: types.ClassObj is the type of old-style classes. The OP used
> new-style classes which are of type type. Using type(A) for the
> comparison means it will work with either kind of classes as long as
> they are the same. You could also use inspect.isclass() to decide if
> it is a class.

Using the inspect module sounds like a good suggestion to me.

I'm wondering why we don't use the inspect module all the way. 
Here is a bit of code:

import inspect

# Import the module containing the classes we want to list.
import test_inspect_data

def test():
outer_classes = inspect.getmembers(test_inspect_data, inspect.isclass)
print outer_classes
for name, class_ in outer_classes:
inner_classes = inspect.getmembers(class_, inspect.isclass)
print inner_classes

if __name__ == '__main__':
test()

Which prints out:

[('A', )]
[('B', ),
('C', ),
('__class__', )]


- Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor