[Tutor] Print record x in a file

2005-01-24 Thread David Holland
Kent,
 
Yes you are right.  I looked at your code and that makes sense now.
Thanks for explaining that.
 
		 ALL-NEW 
Yahoo! Messenger 
- all new features - even more fun! 
 ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] ascii encoding

2005-01-24 Thread Luis N
How would I best turn this string:

'2005-01-24 00:00:00.0'

into this string:

'2005%2D01%2D24%2000%3A00%3A00%2E0'

In order to call a URL.

I've hunted through the standard library, but nothing seemed to jump out.

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


Re: [Tutor] ascii encoding

2005-01-24 Thread Max Noel
On Jan 24, 2005, at 23:29, Luis N wrote:
How would I best turn this string:
'2005-01-24 00:00:00.0'
into this string:
'2005%2D01%2D24%2000%3A00%3A00%2E0'
In order to call a URL.
I've hunted through the standard library, but nothing seemed to jump 
out.
The pathname2url in urllib seems to do what you want:
>>> import urllib
>>> urllib.pathname2url('2005-01-24 00:00:00.0')
'2005-01-24%2000%3A00%3A00.0'
And urllib.url2pathname does the opposite conversion.
-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?"

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


Re: [Tutor] ascii encoding

2005-01-24 Thread Chad Crabtree
I got this from spyce
http://spyce.sourceforge.net

_url_ch = re.compile(r'[^A-Za-z0-9_.!~*()-]') # RFC 2396 section 2.3
def url_encode(o, **kwargs):
  '''Return URL-encoded string.'''
  return _url_ch.sub(lambda match: "%%%02X" % ord(match.group(0)),
str(o))

It was just the first thing I found in google "python url encode"

Good Luck

Luis N wrote:

>How would I best turn this string:
>
>
>
>'2005-01-24 00:00:00.0'
>
>
>
>into this string:
>
>
>
>'2005%2D01%2D24%2000%3A00%3A00%2E0'
>
>
>
>In order to call a URL.
>
>
>
>I've hunted through the standard library, but nothing seemed to jump
out.
>
>
>
>Thank You.
>
>___
>
>Tutor maillist  -  Tutor@python.org
>
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
>  
>




__ 
Do you Yahoo!? 
Meet the all-new My Yahoo! - Try it today! 
http://my.yahoo.com 
 

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


Re: [Tutor] ascii encoding

2005-01-24 Thread Kent Johnson
Luis N wrote:
How would I best turn this string:
'2005-01-24 00:00:00.0'
into this string:
'2005%2D01%2D24%2000%3A00%3A00%2E0'
In order to call a URL.
urllib.quote_plus() is intended for this purpose though it doesn't have the 
result you ask for:
 >>> import urllib
 >>> s='2005-01-24 00:00:00.0'
 >>> urllib.quote_plus(s)
'2005-01-24+00%3A00%3A00.0'
Are you sure you need to escape the - and . ?
You can get exactly what you want with a regular expression and the sub() method. One of the cool 
features of re.sub() is that the replace parameter can be a *function*. The function receives the 
match object and returns the replacement string. This makes it easy to define replacements that are 
programmatically derived from the original string:

 >>> import re
 >>> def hexify(match):
 ...   return '%%%X' % ord(match.group(0))
 ...
hexify is the callback function, it will get a single character match and turn 
it into a hex string
 >>> lett_num = re.compile('[^a-zA-Z0-9]')
lett_num is a regular expression that matches everything except letters and 
digits.
 >>> lett_num.sub(hexify, s)
'2005%2D01%2D24%2000%3A00%3A00%2E0'
Kent
I've hunted through the standard library, but nothing seemed to jump out.
Thank You.
___
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] ascii encoding

2005-01-24 Thread Kent Johnson
Kent Johnson wrote:
 >>> import re
 >>> def hexify(match):
 ...   return '%%%X' % ord(match.group(0))
Ah, should be '%%%02X' ...
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re: glob or filter help

2005-01-24 Thread Jeff Shannon
Barnaby Scott wrote:
For anyone who doesn't like lambda, how about
import os
def get_fles(exts, upd_dir):
return [i for i in os.listdir(upd_dir) if i.split('.')[-1] in exts]
Better would be:
def get_fles(exts, upd_dir):
return [fname for fname in os.listdir(upd_dir) if \
   os.path.splitext(fname)[-1] in exts \
   and not fname.islink() ]
(This is split into three lines for readability, but can be entered as 
a single line.  Also, the '\' line-continuation is optional, as Python 
will see that the list comp is still open and automatically continue 
the line...)

Using os.path.splitext() to get the extension is safer and more 
portable than simply splitting on '.', and it's self-documenting too! 
 (A month from now, you might need to think a moment to figure out 
why you're splitting on '.', but the very name os.path.splitext() 
tells you about the intent.)

Note that there's a slight difference here, in that exts will need to 
list extensions *with* the leading '.' because splitext() will leave 
it on --

>>> os.path.splitext('filename.txt')
('filename', '.txt')
>>>
I've also added a second clause to ensure that returned files are not 
links, as per the rest of the O.P.'s spec.

Jeff Shannon
Technician/Programmer
Credit International
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Seek advise to code Re: Fw: Please submit to tutor list: dictionary update prob

2005-01-24 Thread Eri Mendz
Kent Johnson  tds.net> writes:

[snip]
 
> You still aren't doing anything with newdic. The absence of 'newdic' in the 
> code
> after 'read.close()' should be a clue 
> 
> I think you want to overwrite the saved dict, but with the new dict instead 
> of 
> with a filename string...

Hi Kent,

First off, thanks again to you and Jacob and all. I have updated the simple
address book program and showing it here. 

To me this program seems working to my expectation. but i need good advise from
the knowledgeable people here how to improve the program, comment on the coding
style, syntax and other things i obviously miss being a newbie.

I know this is not much of a program sans orinality as others must have been
done this a thousand times much better than mine. but anyway this is good
exercise for me to improve my understanding of python. TIA.

And sorry for this long boring code :-)). I hope the indentation stays intact.


#!/usr/bin/env python
'''
CHANGELOG   

Tue Jan 25 08:11:07 AST 2005
1. moved all globals under if __name__ == '__main__'

Mon Jan 24 13:12:39 AST 2005
1. wrap few more common codes with function call

Sun Jan 23 15:06:54 AST 2005
1. address book is complete(at least to me) :-))

'''

## Functions ##

def about_program():
try:
print
print '\t'*2, '%s - %s' % (_name_, _version_)
print '\t'*2, 'A simple address book written in Python'
print '\t'*2, 'Cebwrpg Pbyynobengvba bs: '
print '\t' *2, _authors_[0], ',', _authors_[1]
print '\t'*2, 'Created:', _created_
print '\t'*2, 'Revised:', _revised_
raw_input('\nPress  to continue...')
clear_screen()
except EOFError:
print

## add_contact ##
def add_contact(d, f):
while True:
name = add_name()
email = add_email()
d[name] = email
print 'Add another contact? '
ans = ask_yes_no()
if ans == 0:# no
print 'Save to address book? '
get_ans = ask_yes_no()
if get_ans == 1:# yes save
check = if_file_exist(f)
if check == True:
read = open(f, 'rb')
stored = cPickle.load(read)
stored.update(d)
print "'%s' added to address book!\n" % d[name]
read.close()

write_book(stored, filename)
clear_screen()

else:   # file not exist
write_book(data_holder, filename)
break
else:   # no save
d.clear()   #clear dict cache
clear_screen()
break

def add_name():
msg = _msg_ + ' (add): '
while True:
try:
name = raw_input(msg)
if len(name) != 0:
if len(name) <= 20:
return name
else:
print 'Name too long: please limit to 20 characters'
else:
print 'Try again: blank not allowed!'
except EOFError:# catch ^C-D keypress
print


def add_email():
msg = 'Enter email address: '
while True:
try:
email = raw_input(msg)
if len(email) == 0:
print 'Blank not allowed!'
else:
valid_format = r'[EMAIL PROTECTED](\.[-a-z0-9]+)*\.(com$|\
   
edu$|net$|gov$|mil$|org$|int$|aero$|biz$|coop$|museum$|pro$|info$)'
valid_email = re.compile(valid_format)
if valid_email.match(email):
return email
else:
print '%s is not a valid address: try again!' % email
except EOFError:
print

def ask_yes_no():
ask = raw_input('Yes or No? (y|[N]) ')
if ask.lower() in ['y', 'ye', 'yes', 'yep', 'ok']:
return 1# yes
else:
return 0# no

def if_file_exist(f):
''' test if file exists; returns boolean '''

return os.path.exists(os.path.join(os.path.expanduser('~'), f))

def write_book(d, f):
write = open(f, 'wb')
cPickle.dump(d, write)
write.close()

def update_dict(d, f):
''' update the saved dictionary file '''

read = open(f, 'rb')
newdic = cPickle.load(read)
newdic.update(d)
read.close()


def view_abook(d, f):
check = if_file_exist(f)
if check is True:
display_contacts(data_holder, filename)
raw_input('\nPress  to continue...')
clear_screen()
else:
print 'no contacts listed!'


def display_contacts(d, f):
read = open(f, 'rb')
d = cPickle.load(read)
print 'Total contact[s]: %d\n' % len(d)
while 1:
index = 0
for key in d:
name = key
while len(name) < 25:
name += '.' # append to name
index += 1