[Tutor] how do i get datetime object or time object from updated in rss

2006-08-03 Thread anil maran
How do i convert this string todatetime objectFri, 21 Apr 2006 03:02:17 +Previously I'd been using mxDateTime (which does everything but requires a C extension) to parse e-mail style dates (i.e. the ones used in RSS and changes.xml) but it looks like they're handled by the standard library. http://blogs.law.harvard.edu/tech/rssmx.DateTime.DateTimeFrom(d['entries'][1].updated)Traceback (most recent call last):  File "", line 1, in ?  File "/usr/lib/python2.4/site-packages/mx/DateTime/DateTime.py", line 226, in DateTimeFrom    value =
 float(arg)ValueError: invalid literal for float(): Fri, 21 Apr 2006 03:02:17 + __Do You Yahoo!?Tired of spam?  Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] smtp error from cgi script

2006-08-03 Thread Ewald Ertl
Hi Rob,

Rob wrote:
> Hi, can someone help me interpret the error output below?
>  
> I can't tell whether this is a coding error, or a configuration error,
> in which case, it would be up to my host provider.
>  
> For privacy reasons, I have changed the actual email addresses to
> [EMAIL PROTECTED]  and [EMAIL PROTECTED]
> .  The first was the recipient and the second
> was the sender.
>  
> Thanks in advance.
> -- Rob
>  
>  
> SMTPRecipientsRefused
> Python 2.3.5: /usr/bin/python
> Wed Aug 2 20:17:33 2006
>  
> A problem occurred in a Python script. Here is the sequence of function
> calls leading up to the error, in the order they occurred.
>  
>  
>62 
>63 server = smtplib.SMTP(mailserver)
>64 failed = server.sendmail(From, To, text)
>65 server.quit() 
>66 if failed:
> failed undefined, server = , server.sendmail =
> >, From =
> '[EMAIL PROTECTED]' ,
> To = '[EMAIL PROTECTED]' , text = 'From:
> [EMAIL PROTECTED] : [EMAIL PROTECTED]
> ... 1 1
> \n\n'
>  
>  
> /usr/lib/python2.3/smtplib.py
>  in sendmail(self=, from_addr='[EMAIL PROTECTED]'
> , to_addrs=['[EMAIL PROTECTED]'],
> msg='From: [EMAIL PROTECTED] :
> [EMAIL PROTECTED] ...
> 1 1 \n\n', mail_options=[],
> rcpt_options=[])
>   685 # the server refused all our recipients
>   686 self.rset()
>   687 raise SMTPRecipientsRefused(senderrs)
>   688 (code,resp) = self.data(msg)
>   689 if code != 250:
> global SMTPRecipientsRefused = ,
> senderrs = {'[EMAIL PROTECTED]' : (550,
> 'authentication required')}
>  
> SMTPRecipientsRefused: {'[EMAIL PROTECTED]' :
> (550, 'authentication required')}
>   args = ({'[EMAIL PROTECTED]' : (550,
> 'authentication required')},)
>   recipients = {'[EMAIL PROTECTED]' :
> (550, 'authentication required')}
> 

I've not worked with smtplib, but the error (550, 'authentication required'), 
suggests
to me, that you have to login into the mail-Server, otherwise the server would 
not
accept emails.
Looking into the doc for smtplib, the Class SMTP has method for that:

login(self, user, password)
Log in on an SMTP server that requires authentication.

The arguments are:
- user: The user name to authenticate with.
- password: The password for the authentication.

If there has been no previous EHLO or HELO command this session, this
method tries ESMTP EHLO first.

This method will return normally if the authentication was successful.

This method may raise the following exceptions:

 SMTPHeloErrorThe server didn't reply properly to
  the helo greeting.
 SMTPAuthenticationError  The server didn't accept the username/
  password combination.
 SMTPExceptionNo suitable authentication method was
  found.


HTH Ewald

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


Re: [Tutor] os.walk()

2006-08-03 Thread Alan Gauld

"Christopher Spears" <[EMAIL PROTECTED]> wrote> for 
root,dirs,files in os.walk(base):
> for name in files:
> path = os.path.join(root, name)
> print path
>
> Sample output:
>
> ./gui_screenshot.jpeg
> ./find_items.pyc
> ./find_items.py
> ./os
> ./LearningToProgram/loggablebankaccount.py

> I'm glad that the function works smoothly.  I'm just
> wondering how os.walk works.  Shouldn't I have to
> write a statement like this:
>
> path = os.path.join(root, dirs, name)
>

No, the dirs value holds a list of folders, the files
value holds a list of files. Thus if you wanted to print
out all the sub folders in the root you would
print root+dirs
But you want files not folders so you correctly use
print root+files

root holds the full path to the current point in the tree.
(And os.walk is a good example of a program using a
tree data structure! ;-)

Alan G. 



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


Re: [Tutor] critique my script!

2006-08-03 Thread Alan Gauld
>I created a function that takes a pattern and a base
> path and then uses os.walk and glob to traverse
> directories starting from the base path and place
> files that match the glob pattern in a dictionary.

I'm not sure why you are traversing the paths a second time.
Why not just apply glob within the os.walk traversal?
After all you are putting the path into the path list, then 
iterating over that list later, why not just apply glob the 
first time around?

> #!/usr/bin/python
> 
> import os, os.path, glob
> 
> def glob_files(pattern, base = '.'):
> path_list = []
> abs_base = os.path.abspath(base)
> path_list.append(abs_base)
> for root,dirs,files in os.walk(abs_base):
> for name in dirs:
> path = os.path.join(root, name)
> #print path
> path_list.append(path)
> globbed = {}
> cwd = os.getcwd()
> for p in path_list:
> os.chdir(p)
> matched_files = glob.glob(pattern)
> if matched_files != []:
> globbed[p] = matched_files
> os.chdir(abs_base)
> os.chdir(cwd)
> return globbed

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] Notes on namespaces, scopes, etc

2006-08-03 Thread Kent Johnson
Dave Kuhlman wrote:
> I believe that I've incorporated most, if not all, of the
> suggestions from those on the list, except for Danny's suggestion
> (above) about closures.
>
> The document itself is still here:
>
> http://www.rexx.com/~dkuhlman/python_comments.html#namespaces

Very nice. Just a few notes:

- When you talk about the hierarchy of namespace lookup, you might just 
mention nested scopes and refer to the PEP for more info, instead of 
omitting it completely.

- In the section "Accessing namespace dictionaries" you write, "Note 
that for lexically/statically nested scopes (for example, a function 
defined inside a function), it seems that globals() and locals() still 
give access to all items in the accessible namespaces, /but/ do not give 
dictionary style access to all visible scopes." globals() and locals() 
give access to items in the global and local namespaces, not all 
accessible namespaces. As was noted in the emails, variables in nested 
scopes are not included in locals() and globals(). I think that's what 
you're trying to say but it isn't clear.

- PEP 227 is definitely the present, not the future. Nested scopes have 
been available since Python 2.1.

Kent

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


Re: [Tutor] critique my script!

2006-08-03 Thread Kent Johnson
Alan Gauld wrote:
>> I created a function that takes a pattern and a base
>> path and then uses os.walk and glob to traverse
>> directories starting from the base path and place
>> files that match the glob pattern in a dictionary.
>> 
>
> I'm not sure why you are traversing the paths a second time.
> Why not just apply glob within the os.walk traversal?
> After all you are putting the path into the path list, then 
> iterating over that list later, why not just apply glob the 
> first time around?

Alternately you could use fnmatch.fnmatch() to filter the list of files 
received from os.walk(). Then you wouldn't be using glob() but fnmatch() 
is the file-name-matching part of glob(). A list comprehension would be 
good for this:
matchedFiles = [ f for f in files if fnmatch(f, pattern) ]

I think your whole program can be written in about six lines, but I'll 
let you puzzle it a bit rather than showing you.

Kent

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


[Tutor] Binary fractions, recommendations?

2006-08-03 Thread Liam Clarke
Hi all,

I was recently playing with the problem of implementing the floor()
functionality, modulo being specifically mentioned in the briefing...

so, after grokking that x = a - (a % b) would do it (i.e. for a = 5.5
and b = 1, you'd get x =5) I felt very pleased...

...until I saw the definiton of modulo.

Which would be...

def my_mod(x, y):
return x - (y*math.floor(x/y))

Ack. My floor() relies on modulo which relies on... floor.

So after some Googling, I find a page which indicates that the trick
seems to be bit operations  -
http://www.diycalculator.com/popup-m-round.shtml

Problem is; I can't bitshift on floats, so can't test. Can't bitshift
on floats in C either.  Does anyone know how I could work on a binary
representation of a float? Any language at all, I'm desperate...

Regards,

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


Re: [Tutor] Binary fractions, recommendations?

2006-08-03 Thread Luke Paireepinart
Liam Clarke wrote:
> Hi all,
>
> I was recently playing with the problem of implementing the floor()
> functionality, modulo being specifically mentioned in the briefing.
>
>   
you could convert the float to a string, split on the decimal point, and 
convert the left-hand side
back to an integer :)
or you could just convert the float to an integer.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Listbox selection

2006-08-03 Thread Joe Cox
I am still having a problem getting my listbox's binded to the radiobuttons.
I am getting closer.

###Assign each Radiobutton with its own listbox values, show one selected
button and one listbox###



from Tkinter import *

root = Tk()

var = StringVar()
var.set('a')

{ 'Aluminum' : ['Wrought', 'Die cast'],
   'Steel'   : ['Low Carbon', 'Medium-high carbon','Alloy'] }



def radio_command():
if var.get() == 'a':
# button with value 'a' selected
listbox.insert('a') #here I am trying to put the string starting
with 'Steel' in the listbox
elif var.get() == 'b':
# button with value 'b' selected
listbox.insert('b')   #here I am trying to put the string starting
with 'Aluminum' in the listbox



radio_a = Radiobutton(root,text="Steel", variable=var, value='a',
command=radio_command).pack()
radio_b = Radiobutton(root,text="Aluminum", variable=var, value='b',
command=radio_command).pack()
radio_c = Radiobutton(root,text="Cast Iron", variable=var, value='c',
command=radio_command).pack()
radio_d = Radiobutton(root,text="Nickel", variable=var, value='d',
command=radio_command).pack()
radio_e = Radiobutton(root,text="Titaniuim", variable=var, value='e',
command=radio_command).pack()


listbox = Listbox(root)
for item in ():
listbox.insert(END, item)
listbox.pack(side=LEFT, fill=BOTH)

root.mainloop()

























Joe Cox
513-293-4830

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


Re: [Tutor] critique my script!

2006-08-03 Thread Christopher Spears
I didn't know I could place the glob in the os.walk
traversal.  Could you give me an example of how to do
this?

--- Alan Gauld <[EMAIL PROTECTED]> wrote:

> >I created a function that takes a pattern and a
> base
> > path and then uses os.walk and glob to traverse
> > directories starting from the base path and place
> > files that match the glob pattern in a dictionary.
> 
> I'm not sure why you are traversing the paths a
> second time.
> Why not just apply glob within the os.walk
> traversal?
> After all you are putting the path into the path
> list, then 
> iterating over that list later, why not just apply
> glob the 
> first time around?
> 
> > #!/usr/bin/python
> > 
> > import os, os.path, glob
> > 
> > def glob_files(pattern, base = '.'):
> > path_list = []
> > abs_base = os.path.abspath(base)
> > path_list.append(abs_base)
> > for root,dirs,files in os.walk(abs_base):
> > for name in dirs:
> > path = os.path.join(root, name)
> > #print path
> > path_list.append(path)
> > globbed = {}
> > cwd = os.getcwd()
> > for p in path_list:
> > os.chdir(p)
> > matched_files = glob.glob(pattern)
> > if matched_files != []:
> > globbed[p] = matched_files
> > os.chdir(abs_base)
> > os.chdir(cwd)
> > return globbed
> 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
> 


"I'm the last person to pretend that I'm a radio.  I'd rather go out and be a 
color television set."
-David Bowie

"Who dares wins"
-British military motto

"I generally know what I'm doing."
-Buster Keaton
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Binary fractions, recommendations?

2006-08-03 Thread Kent Johnson
Liam Clarke wrote:
> Problem is; I can't bitshift on floats, so can't test. Can't bitshift
> on floats in C either.  Does anyone know how I could work on a binary
> representation of a float? Any language at all, I'm desperate...

In C you can certainly get access to the binary representation of a 
float as bytes. My C is really rusty but something like
float x = 1.3;
char* p = &x;

will give you a pointer to the binary representation of x. You can do 
something similar with the struct module in Python; struct.pack() will 
give you a byte string containing the representation of a float.

Kent

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


[Tutor] regular expression

2006-08-03 Thread arbaro arbaro
Hello,I'm trying to mount an usb device from python under linux.To do so, I read the kernel log /proc/kmsg and watch for something like:  "<6> /dev/scsi/host3/bus0/target0/lun0/:<7>usb-storage: device scan complete"
When I compile a regular _expression_ like:  "r = re.compile('<\d+>\s/dev/scsi/host\d+/bus\d+/target\d+/lun\d+')"It is found. But I don't want the <\d+>\s or '<6> ' in front of the path, so I tried:
   "r = re.compile('/dev/scsi/host\d+/bus\d+/target\d+/lun\d+')"But this way the usb device path it is not found.So what i'm trying to do is:- find the usb device path from the kernel log with the regular _expression_.
- Determine the start and end positions of the match (and add /disc or /part1 to the match).- And use that to mount the usb stick on /mnt/usb -> mount -t auto match /mnt/usbIf anyone can see what i'm doing wrong, please tell me, because I don't understand it anymore.
Thanks.Below is the code:# \d+ = 1 or more digits
# \s  = an empty spaceimport redef findusbdevice():    ''' Returns path of usb device '''    # I did a 'cat /proc/kmsg /log/kmsg' to be able to read the kernel message.    # Somehow I can't read /proc/kmsg directly.
    kmsg = open('/log/kmsg', 'r')     r = re.compile('/dev/scsi/host\d+/bus\d+/target\d+/lun\d+')    #r = re.compile('<\d+>\s/dev/scsi/host\d+/bus\d+/target\d+/lun\d+')    for line in kmsg:    if 'usb-storage' in line and 
r.match(line):    print 'Success', line
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] python processing of web html forms

2006-08-03 Thread Jeff Peery
Hello, I want to use python to process information input into a HTML form on my website. how is this typically performed? I saw cherrypy and also quixote for web python stuff... I have no experience with python on the web. Thanks.jeff 
		Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls.  Great rates starting at 1¢/min.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] critique my script!

2006-08-03 Thread Alan Gauld

>I didn't know I could place the glob in the os.walk
> traversal.  Could you give me an example of how to do
> this?

I'm not sure why you see a problem. A simplified form of your
code is like this:

for root,dirs,files in os.walk(abs_base):
   for name in dirs:
   path = os.path.join(root, name)
   path_list.append(path)

So you create the path list here

for p in path_list:
   os.chdir(p)
   matched_files = glob.glob(pattern)

then you iterate over it applying glob. Why not combine the lops like:

for root,dirs,files in os.walk(abs_base):
   for name in dirs:
   path = os.path.join(root, name)
   os.chdir(path)
   matched_files = glob.glob(pattern)

Doesn't that do the same thing - or am I missing something?

Alan G.


> --- Alan Gauld <[EMAIL PROTECTED]> wrote:
>
>> >I created a function that takes a pattern and a
>> base
>> > path and then uses os.walk and glob to traverse
>> > directories starting from the base path and place
>> > files that match the glob pattern in a dictionary.
>>
>> I'm not sure why you are traversing the paths a
>> second time.
>> Why not just apply glob within the os.walk
>> traversal?
>> After all you are putting the path into the path
>> list, then
>> iterating over that list later, why not just apply
>> glob the
>> first time around?
>>
>> > #!/usr/bin/python
>> >
>> > import os, os.path, glob
>> >
>> > def glob_files(pattern, base = '.'):
>> > path_list = []
>> > abs_base = os.path.abspath(base)
>> > path_list.append(abs_base)
>> > for root,dirs,files in os.walk(abs_base):
>> > for name in dirs:
>> > path = os.path.join(root, name)
>> > #print path
>> > path_list.append(path)
>> > globbed = {}
>> > cwd = os.getcwd()
>> > for p in path_list:
>> > os.chdir(p)
>> > matched_files = glob.glob(pattern)
>> > if matched_files != []:
>> > globbed[p] = matched_files
>> > os.chdir(abs_base)
>> > os.chdir(cwd)
>> > return globbed
>>
>> Alan Gauld
>> Author of the Learn to Program web site
>> http://www.freenetpages.co.uk/hp/alan.gauld
>>
>
>
> "I'm the last person to pretend that I'm a radio.  I'd rather go out 
> and be a color television set."
> -David Bowie
>
> "Who dares wins"
> -British military motto
>
> "I generally know what I'm doing."
> -Buster Keaton 

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


Re: [Tutor] regular expression

2006-08-03 Thread Kent Johnson
arbaro arbaro wrote:
> Hello,
>
> I'm trying to mount an usb device from python under linux.
> To do so, I read the kernel log /proc/kmsg and watch for something like:
>   "<6> /dev/scsi/host3/bus0/target0/lun0/:<7>usb-storage: device scan 
> complete"
>
> When I compile a regular expression like:
>   "r = re.compile('<\d+>\s/dev/scsi/host\d+/bus\d+/target\d+/lun\d+')"
You should use raw strings for regular expressions that contain \ 
characters.
> It is found. But I don't want the <\d+>\s or '<6> ' in front of the 
> path, so I tried:
>"r = re.compile('/dev/scsi/host\d+/bus\d+/target\d+/lun\d+')"
> But this way the usb device path it is not found.
You are using re.match(), which just looks for a match at the start of 
the string. Try using re.search() instead.
http://docs.python.org/lib/matching-searching.html

Kent
>
> So what i'm trying to do is:
> - find the usb device path from the kernel log with the regular 
> expression.
> - Determine the start and end positions of the match (and add /disc or 
> /part1 to the match).
> - And use that to mount the usb stick on /mnt/usb -> mount -t auto 
> match /mnt/usb
>
> If anyone can see what i'm doing wrong, please tell me, because I 
> don't understand it anymore.
> Thanks.
>
> Below is the code:
>
> # \d+ = 1 or more digits
> # \s  = an empty space
>
> import re
>
> def findusbdevice():
> ''' Returns path of usb device '''
> # I did a 'cat /proc/kmsg /log/kmsg' to be able to read the kernel 
> message.
> # Somehow I can't read /proc/kmsg directly.
> kmsg = open('/log/kmsg', 'r')
> r = re.compile('/dev/scsi/host\d+/bus\d+/target\d+/lun\d+')
> #r = re.compile('<\d+>\s/dev/scsi/host\d+/bus\d+/target\d+/lun\d+')
> for line in kmsg:
> if 'usb-storage' in line and r.match(line):
> print 'Success', line
> 
>
> ___
> 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] python processing of web html forms

2006-08-03 Thread Mike Hansen
 __

From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
On Behalf Of Jeff Peery
Sent: Thursday, August 03, 2006 11:38 AM
To: tutor@python.org
Subject: [Tutor] python processing of web html forms


Hello, I want to use python to process information input into a
HTML form on my website. how is this typically performed? I saw cherrypy
and also quixote for web python stuff... I have no experience with
python on the web. Thanks.

jeff




You might want to take a look at the cgi module. 
Devshed has an article on getting started with it.
http://www.devshed.com/c/a/Python/Writing-CGI-Programs-in-Python/

IMO, I think if you want to do web stuff, you should learn the basics
first(cgi), then move on to cherrypy, quixote, django, turbo gears...

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


[Tutor] File like object for Windows registry

2006-08-03 Thread K . Weinert
Hello!

My app should run on debian and windows platforms. For storing the 
configuration data, I use the ConfigParser module.

What I find difficult is to determine a place for my configuration file. On 
debian, it is simply

os.path.join(os.path.expanduser("~")),"myconfig")

but what am I supposed to do on Windows? I think a clean solution would be to 
create a file-like object that reads and writes to the registry, is it?

Kind regards,
Karsten.
-- 


"Feel free" – 10 GB Mailbox, 100 FreeSMS/Monat ...
Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python processing of web html forms

2006-08-03 Thread Lloyd Kvam
On Thu, 2006-08-03 at 10:38 -0700, Jeff Peery wrote:
> Hello, I want to use python to process information input into a HTML
> form on my website. how is this typically performed? I saw cherrypy
> and also quixote for web python stuff... I have no experience with
> python on the web. Thanks.

TurboGears provides a very nice tutorial.
http://www.turbogears.org/docs/wiki20/

That should help get you started.

> 
> jeff
> 
> 
> 
> __
> Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. Great
> rates starting at 1¢/min.
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:  603-653-8139
fax:320-210-3409

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


Re: [Tutor] regular expression

2006-08-03 Thread arbaro arbaro
Hello,

Im just answering my own email, since I just found out what my error was.

>From a regular expression howto: 
>http://www.amk.ca/python/howto/regex/regex.html

The match() function only checks if the RE matches at the beginning of
the string while search() will scan forward through the string for a
match. It's important to keep this distinction in mind.  Remember,
match() will only report a successful match which will start at 0; if
the match wouldn't start at zero,  match() will not report it.

That was exactly my problem. Replacing r.match(line) for
r.search(line) solved it.

Sorry for having bothered you prematurely.




On 8/3/06, arbaro arbaro <[EMAIL PROTECTED]> wrote:
>
> Hello,
>
> I'm trying to mount an usb device from python under linux.
> To do so, I read the kernel log /proc/kmsg and watch for something like:
>   "<6> /dev/scsi/host3/bus0/target0/lun0/:<7>usb-storage: device scan 
> complete"
>
> When I compile a regular expression like:
>   "r = re.compile('<\d+>\s/dev/scsi/host\d+/bus\d+/target\d+/lun\d+')"
> It is found. But I don't want the <\d+>\s or '<6> ' in front of the path, so 
> I tried:
>"r = re.compile('/dev/scsi/host\d+/bus\d+/target\d+/lun\d+')"
> But this way the usb device path it is not found.
>
> So what i'm trying to do is:
> - find the usb device path from the kernel log with the regular expression.
> - Determine the start and end positions of the match (and add /disc or /part1 
> to the match).
> - And use that to mount the usb stick on /mnt/usb -> mount -t auto match 
> /mnt/usb
>
> If anyone can see what i'm doing wrong, please tell me, because I don't 
> understand it anymore.
> Thanks.
>
> Below is the code:
>
> # \d+ = 1 or more digits
>  # \s  = an empty space
>
> import re
>
> def findusbdevice():
> ''' Returns path of usb device '''
> # I did a 'cat /proc/kmsg /log/kmsg' to be able to read the kernel 
> message.
> # Somehow I can't read /proc/kmsg directly.
> kmsg = open('/log/kmsg', 'r')
> r = re.compile('/dev/scsi/host\d+/bus\d+/target\d+/lun\d+')
> #r = re.compile('<\d+>\s/dev/scsi/host\d+/bus\d+/target\d+/lun\d+')
> for line in kmsg:
> if 'usb-storage' in line and  r.match(line):
> print 'Success', line
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File like object for Windows registry

2006-08-03 Thread Henry Finucane
On 8/3/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Hello!
>
> My app should run on debian and windows platforms. For storing the 
> configuration data, I use the ConfigParser module.
>
> What I find difficult is to determine a place for my configuration file. On 
> debian, it is simply
>
> os.path.join(os.path.expanduser("~")),"myconfig")
>
> but what am I supposed to do on Windows? I think a clean solution would be to 
> create a file-like object that reads and writes to the registry, is it?

You might be able to do that, I don't know much about win32
programming, but I believe a better solution is to use the built-in
windows variables. %APPDATA% is where you should store user-specific
application data (and even Microsoft is starting to store XML
configuration files there), and it's an easy variable to get.

>>> import os
>>> os.environ["APPDATA"]
'C:\\Documents and Settings\\UserName\\Application Data'

That should function just fine as a home directory replacement.

> Kind regards,
> Karsten.
> --
>
>
> "Feel free" – 10 GB Mailbox, 100 FreeSMS/Monat ...
> Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
--H.F.
My penguin is bigger than yours, mister...
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File like object for Windows registry

2006-08-03 Thread Andre Roberge
On 8/3/06, Henry Finucane <[EMAIL PROTECTED]> wrote:
> On 8/3/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> > Hello!
> >
> > My app should run on debian and windows platforms. For storing the 
> > configuration data, I use the ConfigParser module.
> >
> > What I find difficult is to determine a place for my configuration file. On 
> > debian, it is simply
> >
> > os.path.join(os.path.expanduser("~")),"myconfig")

This works on Windows as well.  I just tried it :-)

> >
> > but what am I supposed to do on Windows? I think a clean solution would be 
> > to create a file-like object that reads and writes to the registry, is it?

Messing with the registry is (imo)  a bad idea.

>
> You might be able to do that, I don't know much about win32
> programming, but I believe a better solution is to use the built-in
> windows variables. %APPDATA% is where you should store user-specific
> application data (and even Microsoft is starting to store XML
> configuration files there), and it's an easy variable to get.
>
> >>> import os
> >>> os.environ["APPDATA"]
> 'C:\\Documents and Settings\\UserName\\Application Data'
>
> That should function just fine as a home directory replacement.
>
...

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


Re: [Tutor] File like object for Windows registry

2006-08-03 Thread Henry Finucane
On 8/3/06, Andre Roberge <[EMAIL PROTECTED]> wrote:
> On 8/3/06, Henry Finucane <[EMAIL PROTECTED]> wrote:
> > On 8/3/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> > > Hello!
> > >
> > > My app should run on debian and windows platforms. For storing the 
> > > configuration data, I use the ConfigParser module.
> > >
> > > What I find difficult is to determine a place for my configuration file. 
> > > On debian, it is simply
> > >
> > > os.path.join(os.path.expanduser("~")),"myconfig")
>
> This works on Windows as well.  I just tried it :-)

Doh. Always try the simple stuff first :P.

> > >
> > > but what am I supposed to do on Windows? I think a clean solution would 
> > > be to create a file-like object that reads and writes to the registry, is 
> > > it?
>
> Messing with the registry is (imo)  a bad idea.
>
> >
> > You might be able to do that, I don't know much about win32
> > programming, but I believe a better solution is to use the built-in
> > windows variables. %APPDATA% is where you should store user-specific
> > application data (and even Microsoft is starting to store XML
> > configuration files there), and it's an easy variable to get.
> >
> > >>> import os
> > >>> os.environ["APPDATA"]
> > 'C:\\Documents and Settings\\UserName\\Application Data'
> >
> > That should function just fine as a home directory replacement.
> >
> ...
>
> André
>


-- 
--H.F.
My penguin is bigger than yours, mister...
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File like object for Windows registry

2006-08-03 Thread Alan Gauld
> My app should run on debian and windows platforms.

Hard lines :-(

> For storing the configuration data, I use the ConfigParser module.
> What I find difficult is to determine a place for my configuration 
> file.

Config parser basically produces an .in file.
The rules that Windows uses to locate .ini files vary
according to Windows version. If you can assume you
only have to deal with Win2K and XP then things are
a bit easier since there is a recommended location
and indeed users have the concept of a home directory
- even the $HOME environment variable works.

However traditionally ini files were stored in one of
1) the Windows directory (%WINDIR%) or
2) the application home directory.
3) The C:\ root directory - but this is now strongly
discouraged

If you want to have application level ini files as well as
per user configurations those are still the preferred
locations for the global files.

Between Windows 95 and Windows 2000 the Registry
was being pushed as the best place for config data
but as Registry performance and corruption problems
increase .ini files are coming back into favour.

To summarise. If you want to just have a per user
config file just store it in the users data folder. If you
also have a global ini file then I recommend putting
it in the app install folder. If the app is cross platform
I'd advise keeping well clear of the registry, but if
you must use it I'd favour using the WSH objects
rather than the Win32 API calls to access the
Registry - although it does add another dependency
to the app.

HTH,

Alan G. 

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


Re: [Tutor] critique my script!

2006-08-03 Thread Christopher Spears
I rewrote my code with Alan's suggestions in mind.

#!/usr/bin/python

import os, os.path, glob

def glob_files(pattern, base_path = '.'):
abs_base = os.path.abspath(base_path)
#path_list = []
#path_list.append(abs_base)
globbed_dict = {}
cwd = os.getcwd()
for root,dirs,files in os.walk(abs_base):
for name in dirs:
path = os.path.join(root, name)
print path
os.chdir(path)
matched_files = glob.glob(pattern)
#print matched_files
if matched_files != []:
globbed_dict[path] = matched_files
os.chdir(abs_base)
os.chdir(cwd)
return globbed_dict

if __name__ == "__main__":
base_path = raw_input("Enter a base path: ")
pattern = raw_input("Enter a glob pattern: ")

str(base_path)
str(pattern)

globbed_dict = glob_files(pattern, base_path)

print globbed_dict

However, the code still doesn't do exactly what I
want.

$ ./~/chris_python 126> ./find_items_01.py
Enter a base path: ./LearningToProgram
Enter a glob pattern: *.pyc
{}

Under the LearningToProgram directory is a test
directory that doesn't contain any .pyc files, so the
script's returned value is correct.  However, .pyc
files exist in the LearningToProgram directory, and I
would like to put those files in the dictionary too. 
Is there an elegant way to accomplish this?


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


Re: [Tutor] critique my script!

2006-08-03 Thread Alan Gauld
> Under the LearningToProgram directory is a test
> directory that doesn't contain any .pyc files, so the
> script's returned value is correct.  However, .pyc
> files exist in the LearningToProgram directory, and I
> would like to put those files in the dictionary too. 
> Is there an elegant way to accomplish this?

You just need to apply glob at the root level before starting
to walk the tree. Because your inner loop looks at the dirs
list you only glob the subdirectories, you need to glob the 
top level one too.

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


Re: [Tutor] critique my script!

2006-08-03 Thread Python
(Sorry about accidental posting before I had finished editing.)

On Thu, 2006-08-03 at 15:37 -0700, Christopher Spears wrote:
> I rewrote my code with Alan's suggestions in mind.
> 
> #!/usr/bin/python
> 
> import os, os.path, glob
> 
> def glob_files(pattern, base_path = '.'):
>   abs_base = os.path.abspath(base_path)
>   #path_list = []
>   #path_list.append(abs_base)
>   globbed_dict = {}
>   cwd = os.getcwd()
>   for root,dirs,files in os.walk(abs_base):
>   for name in dirs:
>   path = os.path.join(root, name)
>   print path
>   os.chdir(path)
>   matched_files = glob.glob(pattern)
>   #print matched_files
>   if matched_files != []:
>   globbed_dict[path] = matched_files
>   os.chdir(abs_base)
>   os.chdir(cwd)
>   return globbed_dict
>   
> if __name__ == "__main__":
>   base_path = raw_input("Enter a base path: ")
>   pattern = raw_input("Enter a glob pattern: ")
>   
>   str(base_path)
>   str(pattern)
>   
>   globbed_dict = glob_files(pattern, base_path)
>   
>   print globbed_dict
> 
> However, the code still doesn't do exactly what I
> want.
> 
> $ ./~/chris_python 126> ./find_items_01.py
> Enter a base path: ./LearningToProgram
> Enter a glob pattern: *.pyc
> {}
> 
> Under the LearningToProgram directory is a test
> directory that doesn't contain any .pyc files, so the
> script's returned value is correct.  However, .pyc
> files exist in the LearningToProgram directory, and I
> would like to put those files in the dictionary too. 
> Is there an elegant way to accomplish this?

I won't answer for elegance, but you glob the dirs, but do not glob
root.  Now all of your code is knotted together pretty tightly, so it's
hard to make the change.  Suppose we have:

def dirs(abs_base):
# (corrected from earlier accidental posting)
yield abs_base
for root,dirs,files in os.walk(abs_base):
for name in dirs:
yield os.path.join(root, name)

def globfiles(path,pattern):
os.chdir(pattern)
return glob.glob(pattern)

for path in dirs(abs_base):
matched_files = globfiles(path, pattern)
if matched_files:
globbed_dict[path] = matched_files

Hopefully that's a step in the right direction. 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp

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


Re: [Tutor] critique my script!

2006-08-03 Thread Python
On Thu, 2006-08-03 at 15:37 -0700, Christopher Spears wrote:
(in the os.walk processing)
> os.chdir(path)
> matched_files = glob.glob(pattern)
>From the os.walk documentation

Note: If you pass a relative pathname, don't change the current working
directory between resumptions of walk(). walk() never changes the
current directory, and assumes that its caller doesn't either.

Just making sure no one blindly copies this code without understanding
that an absolute path is essential.

-- 
Lloyd Kvam
Venix Corp

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


Re: [Tutor] critique my script!

2006-08-03 Thread Python
On Thu, 2006-08-03 at 15:37 -0700, Christopher Spears wrote:
> I rewrote my code with Alan's suggestions in mind.
> 
> #!/usr/bin/python
> 
> import os, os.path, glob
> 
> def glob_files(pattern, base_path = '.'):
>   abs_base = os.path.abspath(base_path)
>   #path_list = []
>   #path_list.append(abs_base)
>   globbed_dict = {}
>   cwd = os.getcwd()
>   for root,dirs,files in os.walk(abs_base):
>   for name in dirs:
>   path = os.path.join(root, name)
>   print path
>   os.chdir(path)
>   matched_files = glob.glob(pattern)
>   #print matched_files
>   if matched_files != []:
>   globbed_dict[path] = matched_files
>   os.chdir(abs_base)
>   os.chdir(cwd)
>   return globbed_dict
>   
> if __name__ == "__main__":
>   base_path = raw_input("Enter a base path: ")
>   pattern = raw_input("Enter a glob pattern: ")
>   
>   str(base_path)
>   str(pattern)
>   
>   globbed_dict = glob_files(pattern, base_path)
>   
>   print globbed_dict
> 
> However, the code still doesn't do exactly what I
> want.
> 
> $ ./~/chris_python 126> ./find_items_01.py
> Enter a base path: ./LearningToProgram
> Enter a glob pattern: *.pyc
> {}
> 
> Under the LearningToProgram directory is a test
> directory that doesn't contain any .pyc files, so the
> script's returned value is correct.  However, .pyc
> files exist in the LearningToProgram directory, and I
> would like to put those files in the dictionary too. 
> Is there an elegant way to accomplish this?

I won't answer for elegance, but you glob the dirs, but do not glob
root.  Now all of your code is knotted together pretty tightly, so it's
hard to make the change.  Suppose we have:

def dirs(abs_base):
for root,dirs,files in os.walk(abs_base):
yield root
for name in dirs:
yield os.path.join(root, name)

def globdir(path,pattern):
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp

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


Re: [Tutor] critique my script!

2006-08-03 Thread Python
On Fri, 2006-08-04 at 00:26 +0100, Alan Gauld wrote:
> > Under the LearningToProgram directory is a test
> > directory that doesn't contain any .pyc files, so the
> > script's returned value is correct.  However, .pyc
> > files exist in the LearningToProgram directory, and I
> > would like to put those files in the dictionary too. 
> > Is there an elegant way to accomplish this?
> 
> You just need to apply glob at the root level before starting
> to walk the tree. Because your inner loop looks at the dirs
> list you only glob the subdirectories, you need to glob the 
> top level one too.

Which of course is all I suggested except with more shuffling than
necessary.

> 
> Alan G.
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp

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


Re: [Tutor] Binary fractions, recommendations?

2006-08-03 Thread Liam Clarke
Hmmm, and then I could use struct to swing it back... so, I'll be
using string slices to model bitshifting then.

Always found it quite strange that working mathematically with binary
is so hard in programming languages. Even in Scheme, which will
happily play with imaginary numbers.

Regards,

Liam

On 8/4/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Liam Clarke wrote:
> > Problem is; I can't bitshift on floats, so can't test. Can't bitshift
> > on floats in C either.  Does anyone know how I could work on a binary
> > representation of a float? Any language at all, I'm desperate...
>
> In C you can certainly get access to the binary representation of a
> float as bytes. My C is really rusty but something like
> float x = 1.3;
> char* p = &x;
>
> will give you a pointer to the binary representation of x. You can do
> something similar with the struct module in Python; struct.pack() will
> give you a byte string containing the representation of a float.
>
> Kent
>
> ___
> 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] critique my script!

2006-08-03 Thread Kent Johnson
Christopher Spears wrote:
> I rewrote my code with Alan's suggestions in mind.
>
> #!/usr/bin/python
>
> import os, os.path, glob
>
> def glob_files(pattern, base_path = '.'):
>   abs_base = os.path.abspath(base_path)
>   #path_list = []
>   #path_list.append(abs_base)
>   globbed_dict = {}
>   cwd = os.getcwd()
>   for root,dirs,files in os.walk(abs_base):
>   for name in dirs:
>   path = os.path.join(root, name)
>   print path
>   os.chdir(path)
>   matched_files = glob.glob(pattern)
>   #print matched_files
>   if matched_files != []:
>   globbed_dict[path] = matched_files
>   os.chdir(abs_base)
>   os.chdir(cwd)
>   return globbed_dict
>   
> if __name__ == "__main__":
>   base_path = raw_input("Enter a base path: ")
>   pattern = raw_input("Enter a glob pattern: ")
>   
>   str(base_path)
>   str(pattern)
>   
>   globbed_dict = glob_files(pattern, base_path)
>   
>   print globbed_dict
>
> However, the code still doesn't do exactly what I
> want.
>
> $ ./~/chris_python 126> ./find_items_01.py
> Enter a base path: ./LearningToProgram
> Enter a glob pattern: *.pyc
> {}
>
> Under the LearningToProgram directory is a test
> directory that doesn't contain any .pyc files, so the
> script's returned value is correct.  However, .pyc
> files exist in the LearningToProgram directory, and I
> would like to put those files in the dictionary too. 
> Is there an elegant way to accomplish this?

The base_path directory will never appear in the dirs list from 
os.walk(). It will be returned as the root directory however. You could 
modify your program to glob the root directory instead of the list of 
dirs. Or use fnmatch.fnmatch() or fnmatch.filter(), which I still think 
is a better solution if you aren't married to glob():

import os, fnmatch

def glob_files(pattern, base_path = '.'):
abs_base = os.path.abspath(base_path)
globbed_dict = {}
for root,dirs,files in os.walk(abs_base):
matched_files = [ os.path.join(root, name) for name in 
fnmatch.filter(files, pattern) ]

if matched_files: # Note: no need to compare to []
globbed_dict[root] = matched_files
return globbed_dict

Kent

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


[Tutor] critique my script again!

2006-08-03 Thread Christopher Spears
Here is the complete script with documentation:

#!/usr/bin/python

#This script prompts the user for a path and a glob
pattern.
#The script firsts looks in the directory denoted by
the path
#for a matching file.  If a match is found, the path
and file are added
#to a dictionary as a key and value.  This process is
repeated for all
#directories underneath the base directory.  Finally,
the directories and
#their corresponding files are formatted and printed
to the screen.
#Next step: rework script as GUI using PyGTK.
#Created by Chris Spears 7/3/06.
#Thanks to [EMAIL PROTECTED]

import os, os.path, glob

def create_dict(path, globbed_dict):
os.chdir(path)
matched_files = glob.glob(pattern)
if matched_files != []:
globbed_dict[path] = matched_files
return globbed_dict

def glob_files(pattern, base_path = '.'):
abs_base = os.path.abspath(base_path)
globbed_dict = {}
cwd = os.getcwd()
#Check the root directory first
globbed_dict = create_dict(abs_base, globbed_dict)
#Check other directories
for root,dirs,files in os.walk(abs_base):
for name in dirs:
path = os.path.join(root, name)
globbed_dict = create_dict(path, globbed_dict)
os.chdir(abs_base)
#Make sure the script returns to the user's original
directory.
os.chdir(cwd)
return globbed_dict

def print_dict(globbed_dict):
paths = globbed_dict.keys()
paths.sort()
for p in paths:
print p,": "
file_list = globbed_dict[p]
for f in file_list:
print "\t",f


if __name__ == "__main__":
base_path = raw_input("Enter a base path: ")
pattern = raw_input("Enter a glob pattern: ")

str(base_path)
str(pattern)

globbed_dict = glob_files(pattern, base_path)

print_dict(globbed_dict)






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