Re: [Tutor] When am I ever going to use this?

2006-08-02 Thread Michael P. Reilly
On 8/1/06, Christopher Spears <[EMAIL PROTECTED]> wrote:
I've been working through a tutorial:http://www.ibiblio.org/obp/thinkCSpy/index.htm.Lately, I have been learning about abstract data types(linked lists, stacks, queues, trees, etc.).  While I
do enjoy the challenge of creating these objects, I amnot sure what they are used for.Queues and linked lists are also used extensively inside operating systems.  Process lists are a large collections of queues organized in multiple queues implemented as linked lists and stored in one array.  File systems store files by accessing data blocks through linked "blocks" (not quite the same as your linked node structure) and access to the disks and the network are through queues.  Trees are used less often, but still very useful.  The directory structure (folders and files) on your disk are trees, the program to search your email may use a tree.  Stacks are often used for network protocols and program execution (every time you call a function, you are pushing something onto a stack).
How about real life uses for these structures.  Queues are one of the most natural structures from social life brought into computer science.  Insects, banks, highways, birds, even the lunch line in grade school used queues.  Stacks are the next, with stacks of books, papers, etc.  Trees are used for people's genealogies.  And you might used a linked list in a treasure hunt or for footnotes and bibliographies.  Even post-it notes are a form of mental linked list.
I hope this helps your understanding.  -Arcege-- There's so many different worlds,So many different suns.And we have just one world,But we live in different ones.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Update BLOB with odbc

2006-08-02 Thread Ron Phillips
It must be easier than I am making it. 


sql = "UPDATE sa2.outfalls SET photo_content = %s WHERE
pppoint_id ='"+record[0].strip()+"';"
newsql = sql%pstuff
print newsql[:150]
cursor.execute(newsql)
__
the print newsql[:150] => "UPDATE sa2.outfalls SET photo_content =
ÿØÿá*EExif . . . "

and I get this error:

TypeError: argument 1 must be string without null bytes, not str

which sounds reasonable, but I don't know how to fix it. Is there some
escape thing that I'm missing?

Ron


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


[Tutor] latin-1 to unicode in python

2006-08-02 Thread anil maran
Unicode?im getting this error:invalid byte sequence for encoding "UTF8": 0x92 since the db is not handling latin-1 and is set to use UTF8 how do i handle thisthanks a lot 
		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] latin-1 to unicode in python

2006-08-02 Thread Kent Johnson
anil maran wrote:
> Unicode?
> im getting this error:
> invalid byte sequence for encoding "UTF8": 0x92
>
> since the db is not handling latin-1 and is set to use UTF8 how do i 
> handle this

If you have a latin-1 string and you want utf-8, convert it to Unicode 
and then to utf-8 using decode() and encode():

In [1]: s='\x92'

In [3]: s.decode('latin-1').encode('utf-8')
Out[3]: '\xc2\x92'

Kent

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


Re: [Tutor] When am I ever going to use this?

2006-08-02 Thread Alan Gauld
> Lately, I have been learning about abstract data types
> (linked lists, stacks, queues, trees, etc.).  While I
> do enjoy the challenge of creating these objects, I am
> not sure what they are used for.

To be honest, with the possible exception of trees,  not very often!

Most of these data types are taught for historic reasons. Older 
programminng
languages like assembler, C, BASIC, Fortran, Pascal typically only had
arrays(and often only single dimensioned arrays)  for collecting data,
and arrays were either fixed size or had to be explicitly resized in 
blocks
of storage. Thus a whole science grew up in Computing about how to
represent data more abstractly using arrays as the underpinnings.
For example a linked list could be built using 2 identical arrays.
One held the data and the other held the index of the next item,
like this:

indexintListnext
0421
1272
266-1<-- where -1 indicates end of list...

which was equivalent to a list: 42,27,66

Now we can insert a member between 42 and 27 by adding an item at 
index 3
then modifying the next value for 42 to be 3 and setting the next 
value at 3 to
be 1 - like this:

indexintListnext
0423
1272
266-1<-- where -1 indicates end of list...
3991

All very convoluted but produced the illusion of a python like list in 
a
language without lists.

But if the language has lists built in, which almost all modern 
languages do,
there is no real need for most of this. You can add features like 
priorities
and auto sorting etc etc, but this is almost trivial in modern 
languages
whereas in old languages it was a lot of work.

And if the language suuppors a dictionary type ijn addition to lists, 
and if
it can store data of moxed types, then you can implement almost any
abstract data collection easily. But the concepts of abstract data 
types
are still taught to an increasingly bewildered audience...

In practice, the average Python programmer can easily simulate or
replace all of the classic data structures with built in types (with 
the
possible exception of trees) or by using a relational database - the 
use
of which wasn't possible in the 60's, 70's and early 80's because
they hadn't been invented yet! Now implementing an RDBMS as part
of a solution is so trivial that there's rarely need to worry about 
bulding
trees etc for fast searches.

That may be a slightly controversial view but it reflects my personal
experience of programming with Python (and Ruby, Tcl, Smalltalk etc)!
Of course if you ever have to use one of the older languages then the
data structure stuff instantly applies again.

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] latin-1 to unicode in python

2006-08-02 Thread Kent Johnson
anil maran wrote:
> In [1]: s='\x92'
>
> In [3]: s.decode('latin-1').encode('utf-8')
>
>
> Out[3]: '\xc2\x92'
>
> is this some kind of python shell, pls clarify
Yes, it's IPython.
http://ipython.scipy.org/

Kent
>
> */Kent Johnson <[EMAIL PROTECTED]>/* wrote:
>
> anil maran wrote:
> > Unicode?
> > im getting this error:
> > invalid byte sequence for encoding "UTF8": 0x92
> >
> > since the db is not handling latin-1 and is set to use UTF8 how
> do i
> > handle this
>
> If you have a latin-1 string and you want utf-8, convert it to
> Unicode
> and then to utf-8 using decode() and encode():
>
> In [1]: s='\x92'
>
> In [3]: s.decode('latin-1').encode('utf-8')
> Out[3]: '\xc2\x92'
>
> Kent
>
> ___
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> __
> 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] latin-1 to unicode in python

2006-08-02 Thread Kent Johnson
anil maran wrote:
> how to determine
> wat encoding it is in
> for eg i m might not know it is in latin-1
This is hard. It is better by far to know what encoding your data is in. 
There is no way to determine for sure what encoding it is by looking at 
the data. The best you can do is rule out some encodings, and make a 
best guess. Here is one way:
http://chardet.feedparser.org/

You can also try to decode the text using different encodings and use 
the first one that works. This is risky because latin-1 will always work.

Finally, a non-Python method - MS Word is pretty good about guessing the 
encodings of text files.

Kent

PS Please reply on list
>
> */Kent Johnson <[EMAIL PROTECTED]>/* wrote:
>
> anil maran wrote:
> > Unicode?
> > im getting this error:
> > invalid byte sequence for encoding "UTF8": 0x92
> >
> > since the db is not handling latin-1 and is set to use UTF8 how
> do i
> > handle this
>
> If you have a latin-1 string and you want utf-8, convert it to
> Unicode
> and then to utf-8 using decode() and encode():
>
> In [1]: s='\x92'
>
> In [3]: s.decode('latin-1').encode('utf-8')
> Out[3]: '\xc2\x92'
>
> Kent
>
> ___
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> 
> How low will we go? Check out Yahoo! Messenger’s low PC-to-Phone call 
> rates. 
> 


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


Re: [Tutor] latin-1 to unicode in python

2006-08-02 Thread Kent Johnson
anil maran wrote:
> 'ascii' codec can't encode character u'\x96' in position 213: ordinal 
> not in range(128)
>
> now i m getting this error
Please show your code and the traceback and reply on list, not to me 
personally.

Kent

>
> */Kent Johnson <[EMAIL PROTECTED]>/* wrote:
>
> anil maran wrote:
> > Unicode?
> > im getting this error:
> > invalid byte sequence for encoding "UTF8": 0x92
> >
> > since the db is not handling latin-1 and is set to use UTF8 how
> do i
> > handle this
>
> If you have a latin-1 string and you want utf-8, convert it to
> Unicode
> and then to utf-8 using decode() and encode():
>
> In [1]: s='\x92'
>
> In [3]: s.decode('latin-1').encode('utf-8')
> Out[3]: '\xc2\x92'
>
> Kent
>
> ___
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> 
> Do you Yahoo!?
> Get on board. You're invited 
> 
>  
> to try the new Yahoo! Mail Beta. 


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


Re: [Tutor] Notes on namespaces, scopes, etc

2006-08-02 Thread Dave Kuhlman
On Fri, Jul 28, 2006 at 02:36:08PM -0700, Danny Yoo wrote:
> > If there were really such a thing as nested scopes/namespaces, we would 
> > have a function that would give us access to them, similar to the way 
> > that locals() and globals() give us access to the local and global 
> > namespace.
> 
> It would be _convenient_ to have such a function for inspection, but it's 
> not a requirement.
> 
> Here's a function that shows lexical scope in action:
> 
> ##
> >>> def pair(x, y):
> ... def f(b):
> ... if b: return x
> ... return y
> ... return f
> ...
> >>> p = pair(3, 4)
> >>> p(True)
> 3
> >>> p(False)
> 4
> ##

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.  To paraphrase "The Treasure of the Sierra
Madre":

"Closures?  Closures?  We don't got to show you no stinking
closures."

After all, these notes on notes and bindings were intended to
beginners, not computer science students.  But, I did add a link to
an explanation of closures.

I've also added a collection of links to related material at the
end of these notes.  Hopefully, if my explanation is confusing,
Alan's explanation or the standard docs will pull them through.

The document itself is still here:

http://www.rexx.com/~dkuhlman/python_comments.html#namespaces

And, thanks again for all the help.

Off topic -- For those of you who need the quote:

"Badges? We ain't got no badges. We don't need no badges.  I
don't have to show you any stinking badges!"
  --Gold Hat, as played by Alfonso Bedoya
  "The Treasure of the Sierra Madre" (1948)

Dave

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


[Tutor] os.walk()

2006-08-02 Thread Christopher Spears
I'm creating a function that traverses a directory
tree and prints out paths to files:

#!/usr/bin/python

import os, os.path, glob

def traverse(base = '.'):
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
./LearningToProgram/BankAccounts.pyc
./LearningToProgram/KeysApp.py
./LearningToProgram/Message.py
./LearningToProgram/Message.pyc
./LearningToProgram/a.txt
./LearningToProgram/b.txt

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)


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


Re: [Tutor] Notes on namespaces, scopes, etc

2006-08-02 Thread Alan Gauld
Looks good Dave.

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


[Tutor] smtp error from cgi script

2006-08-02 Thread Rob



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
 
 SMTPRecipientsRefusedPython 2.3.5: 
/usr/bin/pythonWed 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.
 
6263 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]: name@domain1.net\nDa... 1 1 
\n\n'
 
 /usr/lib/python2.3/smtplib.py in 
sendmail(self=, from_addr='[EMAIL PROTECTED]', 
to_addrs=['[EMAIL PROTECTED]'], msg='From: name@domain2.net\nTo: name@domain1.net\nDa...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')} 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] critique my script!

2006-08-02 Thread Christopher Spears
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.

#!/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

Tell me what you think.  This script would probably
have been easier to write with reqular expressions,
but I was determined to use glob.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor