Re: [Tutor] __init__.py

2005-05-23 Thread Alan G
HI Joseph,


> I've seen many (python) "plugins" (some located in site-packages
[windows
> here]) with the name __init__.py.

init.py is the file that controls the behaviour of python packages
- that is collections of modules used as a single entity.

Read the python docs for the fine detail, but essentially the init
file often imports functions or classes that the package designer
wants to appear as top level(in the package as opposed to being in
a sub module). Also initialisation of package variables such as
constants can be done there.

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld

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


Re: [Tutor] Checking and showing all errors before of exit

2005-05-23 Thread Jonas Melian
Kent Johnson wrote:
> 
>>Is there any way of checking all possible errors, show you all error
>>messages, and then exit.
> 
> I don't understand your question. Can you give an example of what you would 
> like to do?
> 
The idea was check all exceptions before of exit. I made it :)

flag_error = False
for x in [dir_lo, dir_k, dir_x]:
try:
os.listdir(x)
except OSError, err_o:
flag_error = True
print "Error! %s: %r" % (err_o.strerror, err_o.filename)

if flag_error:
sys.exit()

Now, I have a dudes:

1) Is it necessary use .exit() with some value (as 1) for indicate that
has been an error?

2) With .listdir() the exception can checks if the directory exist, but
how check if there are files in that directory too?

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


Re: [Tutor] Checking and showing all errors before of exit

2005-05-23 Thread Kent Johnson
Jonas Melian wrote:
> Kent Johnson wrote:
> 
>>>Is there any way of checking all possible errors, show you all error
>>>messages, and then exit.
>>
>>I don't understand your question. Can you give an example of what you would 
>>like to do?
>>
> 
> The idea was check all exceptions before of exit. I made it :)
> 
> flag_error = False
> for x in [dir_lo, dir_k, dir_x]:
> try:
> os.listdir(x)
> except OSError, err_o:
> flag_error = True
> print "Error! %s: %r" % (err_o.strerror, err_o.filename)
> 
> if flag_error:
> sys.exit()
> 
> Now, I have a dudes:
> 
> 1) Is it necessary use .exit() with some value (as 1) for indicate that
> has been an error?

The argument to sys.exit() is the exit status of your program. exit() is the 
same as exit(0). If you 
want to signal to other programs that you exited with an error then use a 
different value.
> 
> 2) With .listdir() the exception can checks if the directory exist, but
> how check if there are files in that directory too?

Look at the result of the call to listdir(), e.g.
   if len(os.listdir(x)) == 0:
 flag_error = True
 print "Error: Empty directory"

Kent

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


[Tutor] Davlib and HTTP Authentication

2005-05-23 Thread Rik Wade


Would anyone on the list with some knowledge of working with  
davlib.py (http://www.lyra.org/greg/python/) be able to advise  
regarding use of the library with a Dav server (apache2 with mod_dav)  
that is configured to require HTTP Basic Authentication?


Specifically, I have calls for davlib.put, which look like:

mydav.put(myurl,mydata,None,None,{"Authorization":"Basic %s"%auth})

where davlib.put is defined as:

def put(self, url, contents, content_type=None,  
content_enc=None, extra_hdrs={ }):


The put functions work fine, as do some other basic Dav functions  
which are defined in a similar manner

.

However, I would like to manipulate the properties of files on the  
server. The "user" function defined in davlib is:


def setprops(self, url, *xmlprops, **props):

which doesn't appear to permit the addition of extra_hdrs as before  
in order to carry out HTTP Basic Authentication. There is, however, a  
proppatch function defined as:


def proppatch(self, url, body, extra_hdrs={ }):

which does not have some of the XML parsing and formatting that  
occurs in setprops.


I am wondering whether I should be implementing my own setprops  
function, perhaps inheriting and extending from the davlib class,  
whether I should be writing my own setprops function from scratch, or  
whether I should just use the proppatch function. Davlib.py isn't  
very well documented.


So in generic terms, the question is "I am using a library but the  
function doesn't appear to do what I need it to, what's the best way  
to proceed?".


Many thanks,
--
rik



PGP.sig
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] clock.py

2005-05-23 Thread Feziwe Mpondo
clock.py,problem is to get the last two digits to be random.her's what i 
tried
from time import time,ctime
prev_time = ""
while(1):
the_time = ctime()
if (prev_time != the_time):
 print "The time is :",ctime(time())
prev_time = the_time
guess = 0
number = 1-60
while guess != number:
guess = input ("Guess the last two digits:")
if guess > number:
print "Too high"
elif guess < number:
print "Too low"
print "Just right"
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] clock.py

2005-05-23 Thread Nick Lunt
Feziwe Mpondo wrote:

>clock.py,problem is to get the last two digits to be random.her's what i 
>tried
>from time import time,ctime
>prev_time = ""
>while(1):
>the_time = ctime()
>if (prev_time != the_time):
> print "The time is :",ctime(time())
>prev_time = the_time
>guess = 0
>number = 1-60
>while guess != number:
>guess = input ("Guess the last two digits:")
>if guess > number:
>print "Too high"
>elif guess < number:
>print "Too low"
>print "Just right"
>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>  
>

Hi Feziwe,

you might want to look at the random module to generate 'guess'
ramdon.randrange() more specifically.

Nick .

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


[Tutor] Filtering Spreadsheet Data

2005-05-23 Thread Luke Jordan
Hi All,

I have several frighteningly cumbersome reports to review at my new
job. I would like to write a python program to help me with my
analysis. The goal of the program is to filter out information that
doesn't meet certain requirements and print  relevant results back to
a legible report that I can do detailed research and analysis on. The
reports come to me in Excel format.

I have a solid understanding of basic programming.

Any guidance/advice/starting points would be greatly appreciated.

Thanks!

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


[Tutor] passing variables between frames?

2005-05-23 Thread Jeff Peery
Hello, I am having trouble with passing variables between frames (Frame1 and Dialog1).  I'm using wxpython to write an application. when I run my application the parent frame appears and within the parent frame I can assign values to variables.  I then have a button that launches a child dialog.  I want to access the values from the parent frame in the dialog.  for example say I had a listbox in the parent frame and I choose the first entry 'a' from the list.  I then launch the child dialog and I want to put the value of the selection from the list box into a text field - when I lauch the child dialog a text field appears with the value '0'. From within Frame1 I can use the getselection() function to get the listbox selection. although in the child dialog I tried to use Frame1.listBox.getselection() to get the selection from Frame1. this doesn't work. not sure what to do here? any ideas? thanks.
 
Jeff___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Calendar Combo Box

2005-05-23 Thread Alberto Troiano
Hey everyone

Is there a built-in calendar combo box as in Visual Basic?
Can anyone point me in the right direction?

Thanks in advanced

Alberto


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


Re: [Tutor] passing variables between frames?

2005-05-23 Thread Kent Johnson
Jeff Peery wrote:
> Hello, I am having trouble with passing variables between frames (Frame1 
> and Dialog1).  I'm using wxpython to write an application. when I run my 
> application the parent frame appears and within the parent frame I can 
> assign values to variables.  I then have a button that launches a child 
> dialog.  I want to access the values from the parent frame in the 
> dialog.  for example say I had a listbox in the parent frame and I 
> choose the first entry 'a' from the list.  I then launch the child 
> dialog and I want to put the value of the selection from the list box 
> into a text field - when I lauch the child dialog a text field appears 
> with the value '0'. From within Frame1 I can use the getselection() 
> function to get the listbox selection. although in the child dialog I 
> tried to use Frame1.listBox.getselection() to get the selection from 
> Frame1. this doesn't work. not sure what to do here? any ideas? thanks.

It's hard to know specifically what is broken without seeing code or error 
messages. My guess is 
that the variable Frame1 is not in scope in the dialog box code. But I would 
suggest a different 
approach...

- Make the dialog box code independent of the values of variables in Frame1. 
Generally for me this 
means packaging up the dialog box in a function or class that is passed the 
values it needs to use 
and returns some result to the user. A very simple example is a dialog box that 
presents an error 
message; you could make a function showError(msg) that doesn't know anything 
about its caller.

- In the event handler for the button in Frame1, gather the values needed by 
the dialog, launch it, 
get the result and handle it.

This style keeps your dialog code independent of the rest of the program. An 
immediate benefit is 
that you can write a simple test driver that exercises the dialog without 
having to create any 
infrastructure. (This is the way I create most dialog boxes. I run it 
standalone until I am happy 
with its appearance and function, then I integrate it into the rest of the 
app.) Another long-term 
benefit is the dialogs are reusable; you may not see a need for this but in the 
long run some of 
them will probably have more than one use.

HTH,
Kent

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


[Tutor] better resolution on time.sleep()?

2005-05-23 Thread Roger Merchberger
I'm running an application that has a polling loop to check a serial port 
for certain signals, and on my laptop I can get about 6700 samples per 
second, which (of course) consumes 100% CPU; which may impact battery life. 
BTW, I'm running Python 2.2.2 on my laptop... not had a need to upgrade yet.

I really only need between 500 and 1000 samples per second, but as the 
smallest sleep available normally is time.sleep(.01) -- which brings my 
samples/sec to a nice 100; but I need it a little faster.

I found this:

http://freshmeat.net/projects/rtsched/

which is supposed to bring real-time scheduling to Python (which the docs 
say is dangerous if used incorrectly) and also implementations of 
msleep/usleep/nanosleep.

I downloaded it, compiled it, installed it, and popped in an import rtsched 
at the top and rtsched.msleep(1) where the time.sleep(.01) used to go... 
and now I get around 50 samples per second. I tried the usleep(1) and 
nanosleep(1) and the same thing happens. The rtsched implementation *is* 
working, as I can put in rtsched.msleep(100) and my samples/sec drops to 10.

I'm guessing there's so much overhead to calling rtsched that it's b0rking 
up the works. :-/

Anyone know of:

1) a working millisleep/microsleep implementation for python 2.2+?

-or-

2) will upgrading to a newer version of Python get me what I need?
[[ I looked thru the "New in 2.4" dox and nothing I needed was listed...]]

I've googled (off and on for more than a few days) and searched the 
archives, and came up empty so far...

This isn't life or death by any means... My laptop known for long battery 
life, so everything should be OK as is, but why grind the CPU if it's not 
necessary, eh? ;-) Just a niggling little thing that's been on the back of 
my mind...

[[ Oh, I found a reference to wxwindows that might have something that 
could work, but I was hoping for a little less overkill... my app barely 
uses ncurses. ]]

Thanks!
Roger "Merch" Merchberger

--
Roger "Merch" Merchberger  --  SysAdmin, Iceberg Computers
[EMAIL PROTECTED]

Hi! I am a .signature virus.  Copy me into your .signature to join in!

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


Re: [Tutor] better resolution on time.sleep()?

2005-05-23 Thread jfouhy
Quoting Roger Merchberger <[EMAIL PROTECTED]>:

> I really only need between 500 and 1000 samples per second, but as the 
> smallest sleep available normally is time.sleep(.01) -- which brings my
> samples/sec to a nice 100; but I need it a little faster.

Where did you find that out?
 
> 2) will upgrading to a newer version of Python get me what I need?
> [[ I looked thru the "New in 2.4" dox and nothing I needed was
> listed...]]

I just did some experimenting ... I am running ActiveState Python 2.4.1 under
Windows XP / cygwin.

>>> def check():
...  now = time.clock()
...  for i in xrange(1):
...   time.sleep(0.001)
...  print time.clock() - now
...
>>> check()
11.2695306247
>>> def check():
...  now = time.clock()
...  for i in xrange(10):
...   time.sleep(0.0001)
...  print time.clock() - now
...
>>> check()
2.77601985727
>>> check()
8.70900742972

The first test is fairly consistent with time.sleep(0.001) doing what you
expect.  The second is a bit puzzling to me, though...

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


[Tutor] Simple password generation

2005-05-23 Thread Trey Beck
Hi. First post.

I'm trying to (more or less) copy the functionality of quepasa  
(http://quepasa.sourceforge.net), a simple password generation script  
that builds passwords from a combination of a passphrase and another  
string (like a domain name).

If you enter the same passphrase and string, you should get the same  
password (in case you forget...).

I've added a bit so that if neither a passphrase nor a string is  
entered, the function returns a random (or somewhat random) eight- 
character string.

Am i on the right track here? (I'm brand new to sha and so forth.)  
Criticisms?

Thx!
Trey

---


import sha, re, base64, string
from random import choice

def QuePasa (salt='', passphrase='', length=8):
 salted = passphrase + salt
 newpasswd = ''

 if (salted):
 hash = sha.new(salted).digest()
 # for now, strip non-alphanumeric characters
 newpasswd = re.sub(r'\W', '', base64.encodestring(hash)) 
[:length]
 else:
 chars = string.letters + string.digits
 for i in range(length):
 newpasswd = newpasswd + choice(chars)

 return newpasswd

if __name__ == "__main__":
 sites = ['thissite.com','thatsite.com', 'theothersite.com','']
 passphrase = 'all your base are belong to us'

 for i in range(10):
 for site in sites:
 print "%s : %s" % (site, QuePasa(passphrase, site))

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


Re: [Tutor] better resolution on time.sleep()?

2005-05-23 Thread Roger Merchberger
Rumor has it that [EMAIL PROTECTED] may have mentioned these words:
>Quoting Roger Merchberger <[EMAIL PROTECTED]>:
>
> > I really only need between 500 and 1000 samples per second, but as the
> > smallest sleep available normally is time.sleep(.01) -- which brings my
> > samples/sec to a nice 100; but I need it a little faster.
>
>Where did you find that out?

Imperical testing -- I put in values smaller than .01, yet never got more 
than 100 samples per second. ;-) I then googled around and I saw a few 
other people found the same thing; but with no resolutions.

I *can* post some ugly code if necessary...

>I just did some experimenting ... I am running ActiveState Python 2.4.1 under
>Windows XP / cygwin.

I'm running under Linux From Scratch, book 4.0, kernel 2.4.29.

[code snippage]

>The first test is fairly consistent with time.sleep(0.001) doing what you
>expect.  The second is a bit puzzling to me, though...

Yea, seems to be a bit of weirdness there... Hrm

Anyway, maybe I'll go snag 2.4.x and see what that does for me...

Laterz,
Roger "Merch" Merchberger

--
Roger "Merch" Merchberger   | Anarchy doesn't scale well. -- Me
[EMAIL PROTECTED] |
SysAdmin, Iceberg Computers

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


Re: [Tutor] Filtering Spreadsheet Data

2005-05-23 Thread John Fouhy
Luke Jordan wrote:
> I have several frighteningly cumbersome reports to review at my new
> job. I would like to write a python program to help me with my
> analysis. The goal of the program is to filter out information that
> doesn't meet certain requirements and print  relevant results back to
> a legible report that I can do detailed research and analysis on. The
> reports come to me in Excel format.

pyExcelerator seems to work well for parsing Excel spreadsheets. (it 
basically gives you a dictionary for each worksheet)  Google should find 
it for you.

(you could also use COM ... but that's a bit more effort)

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


Re: [Tutor] better resolution on time.sleep()?

2005-05-23 Thread Roger Merchberger
Rumor has it that Roger Merchberger may have mentioned these words:

Yea, yea, I'm replying my own message you'll see why later! ;-)

>Rumor has it that [EMAIL PROTECTED] may have mentioned these words:

> >I just did some experimenting ... I am running ActiveState Python 2.4.1 
> under
> >Windows XP / cygwin.
>
>I'm running under Linux From Scratch, book 4.0, kernel 2.4.29.

I downloaded and compiled Python 2.4.1 this evening, and the granularity of 
sleep hasn't changed on my system... maybe it's a kernel limitation? Hrm...

Despite my pitiful C knowledge, I found a code snippet I modified to make a 
command-line "msleep" command which sleeps for 1 millisecond & exits, and 
called that with an os.system('msleep') call. The best I could get then is 
around 32 samples for second, so that seemed "marginally less efficient" 
calling my msleep command thru the OS compared to the "realtime usleep" 
function I downloaded earlier to run on Python 2.2.2. The realtime package 
wouldn't compile with Python 2.4.1 (not that it did me much good before... ;-).

=-=-=

I'm downloading the source to wxPython now, there is a wx.usleep function 
in there. As I mentioned, it seems a bit'o'overkill to me, but what the 
heck, in for a penny, in for a pound, eh? ;^>

I'll let y'all know how it turns out...

Laterz,
Roger "Merch" Merchberger

--
Roger "Merch" Merchberger   | Anarchy doesn't scale well. -- Me
[EMAIL PROTECTED] |
SysAdmin, Iceberg Computers

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


Re: [Tutor] passing variables between frames?

2005-05-23 Thread Alan G
> want to access the values from the parent frame in the dialog.  
> for example say I had a listbox in the parent frame and I choose 
> the first entry 'a' from the list.  I then launch the child 
> dialog and I want to put the value of the selection from the 
> list box into a text field - when I lauch the child dialog 
> a text field appears with the value '0'. From within Frame1 
> I can use the getselection() function to get the listbox 
> selection. although in the child dialog I tried to use 
> Frame1.listBox.getselection() to get the selection from Frame1. 
> this doesn't work. not sure what to do here? any ideas? thanks.

The dialog should be subclassed and you can add a parameter to 
the constructor. When you create the dialog pass in Frame1 as 
the value of your new parameter. Within the constructor assign 
the parameter to an attribute of the object, self.parent say. 
Within the event handlers in the dialog you can now reference 
Frame1 as self.parent.

HTH

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor