Re: [Tutor] Reading from files problem

2009-04-20 Thread Alan Gauld
Chris Castillo ctc...@gmail.com wrote 


I am trying to open a text file and read multiple lines containing the
following:

745777686,Alam,Gaus,CIS,15,30,25,15,5,31,15,48,70,97
888209279,Anderson,Judy,Math Ed,14,30,30,13,11,30,16,18,58,72


You could use the CSV module for this, although a basic 
for loop is fine too if the data is this simple.



I want to ask the user for the student number, check to see if that
number is even valid and display something like:


You might want to store the data in a dictionary keyed by ID number?


#Open and read text file

gradesfile = open(grades.dat, r
for lines in gradesfile:
   lines = lines.split(,)
   identifier = lines[0]
   lastname = lines[1]
   firstname = lines[2]
   major = lines[3]


OK so far


   hw1 = lines[4]
   hw2 = lines[5]
   hw3 = lines[6]
   hw4 = lines[7]
   hw5 = lines[8]
   hw6 = lines[9]
   hw7 = lines[10]


You could store these in a list using list slicing, and convert 
to numbers using a list comprehension


hw = [float(n) for n in lines[4:11]]



   test1 = lines[11]
   test2 = lines[12]
   test3 = lines[13]


And here:

test = [float(n) for n in lines[11:14]]



   totalpoints = 550
   hwgrades = float(hw1) + float(hw2) + float(hw3) + float(hw4) +
float(hw5) + float(hw6) + float(hw7)


You could then use sum() to get the totals

hwgrades = sum(hw)


   testgrades = float(test1) + float(test2) + float(test3)


testgrades = sum(test)


   studentpoints = float(hwgrades) + float(testgrades)


you don;t need the flooat conversions because you already 
did that above.



   avgrades = round(float(studentpoints / totalpoints) * 100.0, 2)


same here.


   print identifier


gradesfile.close()



That's what I have so far but I have a feeling I shouldn't use a for
loop. I would really like any help trying to figure out this program.
Thanks in advance.


Thats all fine for reading one stiudent, but you overwrite the 
data each time through the loop! This also looks like an obvious 
use for a class so I'd create a Student class to hold all the data
(You could create methods to do the totals/averages too, plus 
add a __str__  method to print the student data in the format 
required- I'll leave that as an excercise for the reader!))


So I'd change the structure to be like this(pseudo code)

students = dict()  # empty dict
for line in gradesfile:
   line = line.split(',')
   s = Student()
   s.id = line[0]
   s.lastname = line[1]
   etc
   s.hwtotal = sum(hw)
   etc
   students[s.id] = s

Now you can access/print any student record with

id = raw_input('type an id number')
s = students[id]
print s

Finally you could put the for loop inside the Student 
class __init__ method so it boils down to:


for line in gradesfile:
s = Student(line)
students[s.id] = s

In fact you could cram it all into one line with another 
list comprehension, but that seems like a step too far to me...


HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] Reading from files problem

2009-04-20 Thread spir
Le Mon, 20 Apr 2009 07:59:15 +0100,
Alan Gauld alan.ga...@btinternet.com s'exprima ainsi:

  #Open and read text file
  
  gradesfile = open(grades.dat, r
  for lines in gradesfile:
 lines = lines.split(,)
 identifier = lines[0]
 lastname = lines[1]
 firstname = lines[2]
 major = lines[3]  
 
 OK so far

Not OK for me. The structure is the one of a table. A variety of 
words/concepts/metaphors can properly describe a table structure. But the above 
choice is certainly wrong: It says that a table row/record is a 'lines' *and* 
is made of 'lines'!
Possible alternatives:

for record in gradesfile:
   fields = record.split(,)

for row in gradesfile:
   cells = row.split(,)

for line in gradesfile:
   data = line.split(,)   # 'data' is supposed to be plural of 'datum' (?)

Denis
--
la vita e estrany
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reading from files problem

2009-04-20 Thread spir
Le Mon, 20 Apr 2009 07:59:15 +0100,
Alan Gauld alan.ga...@btinternet.com s'exprima ainsi:

 So I'd change the structure to be like this(pseudo code)
 
 students = dict()  # empty dict
 for line in gradesfile:
 line = line.split(',')
 s = Student()
 s.id = line[0]
 s.lastname = line[1]
 etc
 s.hwtotal = sum(hw)
 etc
 students[s.id] = s

If a student type is created, then it's worth having a readDataLine() method 
(whatever its name) to feed it:

students = dict()  # empty dict
for line in gradestable:# not a file, but data read from it
line = line.split(',')
student = Student()
student.readDataLine(line)
id = line[0]
students[id] = student

[Actually, you wouldn't even need splitting the line before passing it, except 
here its also a practicle way to extract the id to use it as dict key.]

If ever this is the standard, or even only, way to feed a student object with 
data, then the data-line-reading method may be __init__ itself. So that the 
loop becomes:

for line in gradestable:
line = line.split(',')
student = Student(line)
id = line[0]
students[id] = student

Denis
--
la vita e estrany
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reading from files problem

2009-04-20 Thread Scott SA

On Apr 20, 2009, at 12:59 AM, Alan Gauld wrote:


You might want to store the data in a dictionary keyed by ID number?


I had thought of suggesting this, but it appeared that the OP was  
going to re-iterate the file each time he wished to query the CSV.


May have been a bad assumption on my part as I envisioned pickling a  
dict. and that just got too complicated.



test = [float(n) for n in lines[11:14]]
hwgrades = sum(hw)


The composite of this would be:
sum([float(n) for n in lines[11:14]])

... which, I agree, is easier on the eyes/brain than the  
reduce(lambda:...) example I gave.


sum is also on http://docs.python.org/library/functions.html along  
with with range and other built-ins.


Chris: wrapping the for-loop in square-brackets is part of list  
comprehension, found here (part 5.1.4)

http://docs.python.org/tutorial/datastructures.html

Thats all fine for reading one stiudent, but you overwrite the data  
each time through the loop! This also looks like an obvious use for  
a class so I'd create a Student class to hold all the data
(You could create methods to do the totals/averages too, plus add a  
__str__  method to print the student data in the format required-  
I'll leave that as an excercise for the reader!))


This part is actually the reason I've replied, everything before this  
was 'just along the way'.


Classes are a big subject for starting out, here are the main docs.
http://docs.python.org/tutorial/classes.html

Also, check out 'dive into python' and others for help in getting a  
handle on that.


I figured that the Student class proposed probably needed an example  
to get over the initial hurdle.


class Student(object):
def __init__(self)
pass

In its most basic form, this is pretty much the 'hello world' for  
classes.



So I'd change the structure to be like this(pseudo code)

students = dict()  # empty dict
for line in gradesfile:
  line = line.split(',')
  s = Student()


This step creates an instance of the class. Just for the moment, think  
of it as a fancy variable -- how python will store and reference the  
live data. In the end, you would need a class-instance for each and  
every student (line of the file).



  s.id = line[0]


And this adds an 'id' attribute to the class

Pre-defined in the class, this would look like:

class Student(object):
def __init__(self)
self.id = None

When the instance is created, the id has None as its value (or  
anything you wanted). The self reference means the instance of the  
class itself, more on that in a moment.


Still accessed the same as above:
s.id = n


  s.lastname = line[1]
  etc
  s.hwtotal = sum(hw)
  etc
  students[s.id] = s


As mentioned, id, lastname, hwtotal, etc. become attributes of the  
class. Nothing terribly magical, they are actually stored in a  
dictionary (i.e. s.__dict__) and long-hand access would be:  
s.__dict__['id']


So, the next step to this would be to actually use the class to do the  
heavy lifting. This is what Alan is talking about a bit further down.


class Student(object):
def __init__(self, csv_data):
csv_list = csv_data.split(',')

self.id = csv_list[0]
...
self. hwgrades = self._listFloats(csv_list[4:10])

def _list_floats(self, str_list):
return [float(n) for n in str_list]

def hw_grade_total(self):
sum(self.hwgrades)

The two methods are part of the business-logic of the class - notice  
internally they are accessed by 'self'. This is very important, so  
python knows what data to work with.


Assuming you're not using the CSV library or already have the row/line  
from the file as a list:


for student_data in grades_file:
s = Student(student_data)
student_dict[s.id] = s

So, when python creates the class instance, it calls the __init__  
method. Since you've passed it a list of student data, it processes it  
at the same time. In this example, it will cause an error if you don't  
pass any data, by the way. You might need to consider verifying that  
each line has the correct number of fields otherwise an error could be  
generated.


Accessing the grade total method is like this:

grade_total = s.hw_grade_total()

Or, if you want the raw list of floats:

grade_list = s.hwgrades

I still contend that all of this is ideal for a database, like SQLite,  
which would allow for searching by name as well as ID, etc. It is the  
persistence of data that motivates this perspective. So what I would  
do is make a class Students, with a file-import method using the CSV  
lib which then did the work of putting all the data into a database,  
bypassing a Student class 

[Tutor] vars()

2009-04-20 Thread spir
Hello,

Just discovered vars(). Looking for the reasons and use cases that led to 
introduce this builtin. But could not find any reference.
Any pointer/clue?

Denis

PS: same about all() and any() -- but here I found some literature... in french:
fr.wikipedia.org/wiki/Python_(langage)

--
la vita e estrany
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reading from files problem

2009-04-20 Thread Alan Gauld


Scott SA py...@rscorp.ab.ca wrote

May have been a bad assumption on my part as I envisioned pickling a  
dict. and that just got too complicated.


A pickled dict?
That would be a shelve mebbe?


PS. My email is acting up, did my prev. message actually make it to  
the list?


Yes, I saw it just after sending mine...

Alan G

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


Re: [Tutor] vars()

2009-04-20 Thread Padmanaban Ganesan
Hello Denis,


Please find more details , Use cases about the Built-in in the following
book under the

*Chapter 9* - Oops, Creating modules.

*Book Name :* Python Programming for the Absolute Beginner
*Author:* Michael Dawson

Ta,
Paddy
2009/4/20 spir denis.s...@free.fr

 Hello,

 Just discovered vars(). Looking for the reasons and use cases that led to
 introduce this builtin. But could not find any reference.
 Any pointer/clue?

 Denis

 PS: same about all() and any() -- but here I found some literature... in
 french:
 fr.wikipedia.org/wiki/Python_(langage)

 --
 la vita e estrany
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor




-- 
Ta,
Paddy
The Secret to creativity is knowing how to hide your sources 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Webpy vs Django

2009-04-20 Thread vishwajeet singh
Thanks for your elaborate reply.

On Mon, Apr 20, 2009 at 12:47 AM, Scott SA py...@rscorp.ab.ca wrote:

 On Apr 19, 2009, at 4:52 AM, vishwajeet singh wrote:

  This is not to flame any war; I just wanted to know the key features to
 consider among the two web frame works.

 What advantage and disadvantages you have when you decide using any one of
 them.


 I can't speak with any authority on Webpy (webpy.org), as I have not
 actually used it though I did read up on its features about a year ago and
 now just a quick glance again. I do have some experience with a different
 'light-weight' framework called Quixote (www.quixote.ca) and have a
 production project using it... that I'm migrating to Django. I also have a
 couple of projects using Zope... also being migrated to Django. I don't
 mention all of this as any indication of prowess, in fact much of the code
 on these projects has been written by other developers I've contracted to,
 but that I have some similar experience.

 The main point I wish to make of all this is that these different
 frameworks kind of coalesce into sub-groups. They sort-of form into three
 groups: light-weight framework, full-featured framework and heavy CMS (lots
 of inseparable overhead). Furthermore, like a series of balls or coins in a
 simple Pachinko machine (http://en.wikipedia.org/wiki/Pachinko) where each
 one takes a different route, the application you are developing will have a
 huge influence on requirements.

 Personally, I've been working with Django on and off for about 2-1/2 years
 and find it a joy to work with. It does not have the overhead and hidden
 'magic' of Zope and I don't have to re-invent almost everything like
 Quixote. For _my_ needs, it has been a great blend of capabilities without
 the oppressing overhead of the CMS. I can add or remove the modules needed
 on a project-by-project basis.

 Out of the box, the automated admin interface is a huge time-saver and
 its object-relational model is quite good at wrapping relational databases
 into a python-friendly framework. Also it's user-admin and authentication
 takes care of the core of most sites I work on. It has a good blend for my
 requirements of granular framework access and functional tools to save me
 time. And now, there is quite a nice selection of add-on applications that
 extend Django in a very modular fashion, though there are still growing
 pains. Like Goldie Locks and the three bears, this one is just right for
 my needs.

 Just as Zope is overkill for projects that don't need CMS, Django would be
 overkill for a project that didn't need much for database access and admin
 interface. The authors of Django, try pretty hard to not hide anything that
 is going on in the background but there is still a lot being done for the
 developer. For example in creating db schemas, wrapping queries and
 result-sets into object calls, rendering content through templates and much
 more. All have ways of being modified but not necessarily by novice python
 developers.

 So the short answer to your question is that Webpy and Django are quite
 different in their tool-sets and are therefore tough to compare. As with all
 lightweight frameworks, they are _shorter_ on pre-defined features. That's
 not a negative, that _is_ the point of them. Django has tried to bridge, and
 in my opinion done a good job of it, a lighter-weight core with a well
 documented and developer-friendly interface for extension. It is a framework
 that has allowed me to gradually dig deeper and deeper into its workings to
 extract more advanced features while not bogging me down with redundant and
 tedious things like forms and user authentication.

 I chose Django because it had the most to offer for what I need and it
 seems to have the legs to cover a lot of ground. That said, I don't that
 anyone could say you were wrong by choosing either of the projects you
 mentioned given appropriate circumstances (though personally, I can't find
 _any_ circumstances where I'd recommend Zope today -- but that's probably a
 personal bias and I won't digress).

 I know this doesn't answer your question directly but I hope it helps
 broaden the perspective a little,

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




-- 
Cheers,
Vishwajeet
http://www.singhvishwajeet.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Looping

2009-04-20 Thread Matt
Hey everyone,

First post to this list. I hope I'm doing it right.

Let's say I want to run func 10 times Is there a more pythonic way to do it
than this:
for i in xrange(10):
 func()

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


[Tutor] String Encoding problem

2009-04-20 Thread Matt
Hey everyone,

I'm hoping someone here can help me solve an odd problem (bug?). I'm having
trouble with string encoding, object deletion, and the xml.etree library. If
this isn't the right list to be posting this question, please let me know.
I'm new to Python and don't know of any other help me Python mailing
lists. I have tried debugging this ad-infinitem. Anyway, at the bottom of
this e-mail you will find the code of a python file. This is a gross
over-simplification of my code, with little exception handling so that the
errors are obvious.

Running this interactively, if you finish off with 'del db', it exits fine
and creates a skeleton xml file called 'db.xml' with text 'root /'.
However, if you instead CTRL-D, it throws at exception while quitting and
then leaves an empty 'db.xml' which won't work. Can anyone here help me
figure out why this is?

Stuff I've done:
I've traced this down to the self.commit() call in __del__. The stacktrace
and a few print statements injected into xml.etree leads me to the call
'root'.encode('us-ascii') throwing a LookupError on line 751 of
xml.etree.ElementTree. This makes no sense to me, since it works fine
normally.

Thank you very much. Any and all help or pointers are appreciated.

~Matt

 db.py ###
from xml.etree import ElementTree as ET
import os

class Database(object):
def __init__(self, path):
self.__dbpath = path## Path to the database
self.load()
def __del__(self):
## FIXME: Known bug:
##  del db at command line works properly
##  Ctrl-D, when there is no db file present, results in a
LookupError
##and empty xml file
from StringIO import StringIO
from traceback import print_exc
trace = StringIO()
try:
print 5
self.commit()
print 7
except Exception:
print_exc(100, trace)
print trace.getvalue()
def load(self):
if os.path.exists(self.__dbpath):
self.root = ET.parse(self.__dbpath).getroot()
else:
self.root = ET.Element(root)
def commit(self):
ET.ElementTree(self.root).write(self.__dbpath)
db = Database('db.xml')
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looping

2009-04-20 Thread W W
On Mon, Apr 20, 2009 at 8:48 AM, Matt
hellzfury+pyt...@gmail.comhellzfury%2bpyt...@gmail.com
 wrote:

 Hey everyone,

 First post to this list. I hope I'm doing it right.

 Let's say I want to run func 10 times Is there a more pythonic way to do it
 than this:
 for i in xrange(10):
  func()


AFAIK that's the most pythonic way to do it... and probably how I'd do it...
if I had a function I needed to run 10 times :P

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


Re: [Tutor] Looping

2009-04-20 Thread A.T.Hofkamp

Matt wrote:

Hey everyone,

First post to this list. I hope I'm doing it right.

Let's say I want to run func 10 times Is there a more pythonic way to do it 
than this:
for i in xrange(10):
 func()


no, that looks fine for your requirement.

What may be a bit weird here is the requirement to run the same function 10 
times without further input/output parameters. I have never needed such a 
construct.

What kind of function do you have that needs to be called 10 times?


If you are doing timing experiments, the timeit library has a way of repeating 
the same experiment a number of times.



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


[Tutor] parse text for paragraghs/sections

2009-04-20 Thread Dinesh B Vadhia
Hi!  I want to parse text and pickup sections.  For example, from the text:

t = abc pre DEF ghi jkl /pre MNO pqr

... pickup all text between the tags pre and /pre and replace with another 
piece of text.

I tried 

t = re.sub(r\pre[A-Za-z0-9]\/pre, DBV, t)

... but it doesn't work.

How do you do this with re?  

Thanks

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


[Tutor] py-editdist

2009-04-20 Thread عماد نوفل
Hi Tutors,
I tried to install this edit distance module but failed:
http://www.mindrot.org/projects/py-editdist/

I'm running Ubuntu Linux. The error message is below. Your help appreciated


e...@emad-desktop:~/Desktop/py-editdist-0.3$ python setup.py build
running build
running build_ext
building 'editdist' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall
-Wstrict-prototypes -fPIC -I/usr/include/python2.5 -c editdist.c -o
build/temp.linux-i686-2.5/editdist.o
editdist.c:22:20: error: Python.h: No such file or directory
editdist.c:91: error: expected ‘)’ before string constant
editdist.c:94: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’
before ‘*’ token
editdist.c:110: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’
before ‘editdist_methods’
editdist.c:116: error: expected ‘)’ before string constant
editdist.c:119: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’
before ‘initeditdist’
error: command 'gcc' failed with exit status 1

-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
No victim has ever been more repressed and alienated than the truth

Emad Soliman Nawfal
Indiana University, Bloomington

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


Re: [Tutor] Looping

2009-04-20 Thread spir
Le Mon, 20 Apr 2009 17:26:30 +0200,
A.T.Hofkamp a.t.hofk...@tue.nl s'exprima ainsi:

 Matt wrote:
  Hey everyone,
  
  First post to this list. I hope I'm doing it right.
  
  Let's say I want to run func 10 times Is there a more pythonic way to do
  it than this: for i in xrange(10):
   func()
 
 no, that looks fine for your requirement.
 
 What may be a bit weird here is the requirement to run the same function 10 
 times without further input/output parameters. I have never needed such a 
 construct.
 What kind of function do you have that needs to be called 10 times?

Actually, it's even rare to have to run a func n times, where n is known at 
runtime only. Builtin iteration (for item in container) removes 99% (about ;-) 
of such needs.

Denis
--
la vita e estrany
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sending a disconnect after openssl s_client command?

2009-04-20 Thread Martin Walsh
Kayvan Sarikhani wrote:
 Tutors,
 
   I'm working on a script to verify whether a particular website
 supports SSLv2 via the following:
 
 --- BEGIN ---
 #!/usr/bin/python
 import os, re
 
 checkssl_out = open('checkssl.txt','w')
 
 website = 'somewebsitename'
 sslv2 = 'Protocol  : SSLv2'
 
 print 'Checking:', website
 
 checksslv2 = os.popen('openssl s_client -ssl2 -connect
 somewebsitename:443').read().strip()
 
 if re.search(sslv2, checksslv2) == None:
 print  checkssl_out, website, 'does NOT support SSLv2'
 else:
 print  checkssl_out, website, 'supports: SSLv2'
 
 checkssl_out.close()
 --- END ---
 
   It works, but the problem is that OpenSSL does not automatically
 disconnect after end of input. I was curious if there's a way to send a
 CTRL-C at the end of the command, so that it *does* capture the output,
 and breaks after it. Any suggestions or help is appreciated!

You can do something like the following (untested) to simulate a CTRL-C,
but I'd recommend against it, as I doubt it will work as you expect ...

import os, signal
from subprocess import Popen, PIPE

openssl_cmd = 'openssl s_client -ssl2 -connect somewebsitename:443'
openssl = Popen(openssl_cmd, shell=True, stdout=PIPE, stderr=PIPE)

os.kill(openssl.pid, signal.SIGINT)

# dead, I bet, before any output is generated
stdout, stderr = openssl.communicate()


Instead, you may want to try to mimic this command-line behavior ...

echo GET / | openssl s_client -ssl2 -connect somewebsitename:443

... in which case, you can try something like this ...

from subprocess import Popen, PIPE

openssl_cmd = 'openssl s_client -ssl2 -connect somewebsitename:443'
openssl = Popen(
  openssl_cmd, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE
)
stdout, stderr = openssl.communicate('GET /')

Alternatively, if you're using python 2.6 and above, it looks like you
can do something similar with a few lines of code, and the ssl module
from the standard lib ...

# untested!
import ssl
try:
cert = ssl.get_server_certificate(
('somewebsitename', 443), ssl.PROTOCOL_SSLv2
)
except ssl.SSLError, ex:
# site may not support sslv2
...

HTH,
Marty

 
 K
 

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


Re: [Tutor] String Encoding problem

2009-04-20 Thread spir
Le Mon, 20 Apr 2009 10:46:47 -0400,
Matt hellzfury+pyt...@gmail.com s'exprima ainsi:

 Hey everyone,
 
 I'm hoping someone here can help me solve an odd problem (bug?). I'm having
 trouble with string encoding, object deletion, and the xml.etree library. If
 this isn't the right list to be posting this question, please let me know.
 I'm new to Python and don't know of any other help me Python mailing
 lists. I have tried debugging this ad-infinitem. Anyway, at the bottom of
 this e-mail you will find the code of a python file. This is a gross
 over-simplification of my code, with little exception handling so that the
 errors are obvious.
 
 Running this interactively, if you finish off with 'del db', it exits fine
 and creates a skeleton xml file called 'db.xml' with text 'root /'.
 However, if you instead CTRL-D, it throws at exception while quitting and
 then leaves an empty 'db.xml' which won't work. Can anyone here help me
 figure out why this is?
 
 Stuff I've done:
 I've traced this down to the self.commit() call in __del__. The stacktrace
 and a few print statements injected into xml.etree leads me to the call
 'root'.encode('us-ascii') throwing a LookupError on line 751 of
 xml.etree.ElementTree. This makes no sense to me, since it works fine
 normally.
 
 Thank you very much. Any and all help or pointers are appreciated.
 
 ~Matt
 
  db.py ###
 from xml.etree import ElementTree as ET
 import os
 
 class Database(object):
 def __init__(self, path):
 self.__dbpath = path## Path to the database
 self.load()
 def __del__(self):
 ## FIXME: Known bug:
 ##  del db at command line works properly
 ##  Ctrl-D, when there is no db file present, results in a
 LookupError
 ##and empty xml file
 from StringIO import StringIO
 from traceback import print_exc
 trace = StringIO()
 try:
 print 5
 self.commit()
 print 7
 except Exception:
 print_exc(100, trace)
 print trace.getvalue()
 def load(self):
 if os.path.exists(self.__dbpath):
 self.root = ET.parse(self.__dbpath).getroot()
 else:
 self.root = ET.Element(root)
 def commit(self):
 ET.ElementTree(self.root).write(self.__dbpath)
 db = Database('db.xml')

Actually, it all runs well for me -- after the following modification:

def __del__(self):
## FIXME: Known bug:
##  del db at command line works properly
##  Ctrl-D, when there is no db file present, results in a LookupError
##and empty xml file
try:
print 5
self.commit()
print 7
except Exception:
raise

Notes:
* I don't know for what reason you needed such a complicated traceback 
construct.
* Before I did this modif, I indeed had a weird exception about stringIO.
* __del__() seems to do the contrary: it writes back to file through commit()???
* del db works fine, anyway
* When I run without any bd.xml, it properly creates one with text root /.
* When I run with an ampty db.xml, I have the following exception message:

Traceback (most recent call last):
  File xmlTree.py, line 29, in module
db = Database('db.xml')
  File xmlTree.py, line 10, in __init__
self.load()
  File xmlTree.py, line 24, in load
self.root = ET.parse(self.__dbpath).getroot()
  File /usr/lib/python2.5/xml/etree/ElementTree.py, line 862, in parse
tree.parse(source, parser)
  File /usr/lib/python2.5/xml/etree/ElementTree.py, line 587, in parse
self._root = parser.close()
  File /usr/lib/python2.5/xml/etree/ElementTree.py, line 1254, in close
self._parser.Parse(, 1) # end of data
xml.parsers.expat.ExpatError: no element found: line 2, column 0
5
Exception exceptions.AttributeError: AttributeError('Database' object has no 
attribute 'root',) in bound method Database.__del__ of __main__.Database 
object at 0xb7e78fec ignored

--
la vita e estrany
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looping

2009-04-20 Thread Alan Gauld

Matt hellzfury+pyt...@gmail.com wrote

Let's say I want to run func 10 times Is there a more pythonic way to do 
it

than this:
for i in xrange(10):
 func()


Yes, use range() rather than xrange :-)

But more seriously, as others have pointed out, if func() is well
written then calling it ten times will have no effect over calling
it once. (Except taking up 10 times as much time!)

Its like saying

for n in range(10):
   x = 42

The assignment is identical each time through the loop

In fact it's worse than that because you don't even store the
result of func() so it has no effect whatsoever on your program
outside the loop. It would be more normal to do something like:

for value in mycollection:
 result = func(somevalue)

in other words apply func() to each value in a collection.

So unless your func() really takes arguments or prints time
related output or modifies global data (a bad practice) then
what you are asking doesn't make much sense.

Finally, if your function is really a program in disguise - ie it
does all its own input/output then it would make more sense
to put the loop inside the function and put the repeat count
as a parameter:

def func(repetitions):
   for n in range(repetitions):
# your old func code here

And call it:

func(repetitions=10)   # use keyword for clarity of purpose

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



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


Re: [Tutor] parse text for paragraghs/sections

2009-04-20 Thread spir
Le Mon, 20 Apr 2009 09:07:01 -0700,
Dinesh B Vadhia dineshbvad...@hotmail.com s'exprima ainsi:

 t = abc pre DEF ghi jkl /pre MNO pqr
 
 ... pickup all text between the tags pre and /pre and replace with
 another piece of text.
 
 I tried 
 
 t = re.sub(r\pre[A-Za-z0-9]\/pre, DBV, t)
 
 ... but it doesn't work.

You need:
-1- Add ' ' to the character class.
-2- Repete it with '*' or '+'.

from re import compile as Pattern
p = Pattern(rpre[A-Za-z0-9 ]*/pre)
t = abc pre DEF ghi jkl /pre MNO pqr
print p.sub('@', t)
==
abc @ MNO pqr

Denis

PS: I really wonder why sub() takes (1) the replacement string (2) the source 
text, as parameters. I always get caught by this (IMO weird) parameter order. 
And you get no error... only wrong result.
--
la vita e estrany
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looping

2009-04-20 Thread W W
On Mon, Apr 20, 2009 at 12:34 PM, Matt
hellzfury+pyt...@gmail.comhellzfury%2bpyt...@gmail.com
 wrote:

 Thank you. Func is in fact a call to an external non-python program. =]
 Therefore, the loop must actually be mindlessly done. My question, however,
 has been answered.
 As an aside, why use range rather than xrange? I was under the impression
 that xrange is a generator and therefore more memory efficient.


Probably because for small values (under 100), range just looks cleaner, and
as far as memory goes it's not a whole lot of difference.

AFAIK,
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] py-editdist

2009-04-20 Thread عماد نوفل
2009/4/20 Alan Gauld alan.ga...@btinternet.com


 Emad Nawfal (عماد نوفل) emadnaw...@gmail.com wrote

  e...@emad-desktop:~/Desktop/py-editdist-0.3$ python setup.py build
 running build
 gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall
 editdist.c:22:20: error: Python.h: No such file or directory


 Looks like you need the Python source code installed,
 do you have that?

 Alan G

Python comes with Ubuntu, or do you mean that I should re-install it from
source code?


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




-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
No victim has ever been more repressed and alienated than the truth

Emad Soliman Nawfal
Indiana University, Bloomington

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


Re: [Tutor] String Encoding problem

2009-04-20 Thread Strax-Haber, Matthew (LARC-D320)



 From: spir denis.s...@free.fr
 Date: Mon, 20 Apr 2009 12:22:59 -0500
 To: Python Tutor tutor@python.org
 Subject: Re: [Tutor] String Encoding problem

 Le Mon, 20 Apr 2009 10:46:47 -0400,
 Matt hellzfury+pyt...@gmail.com s'exprima ainsi:

 Hey everyone,

 I'm hoping someone here can help me solve an odd problem (bug?). I'm having
 trouble with string encoding, object deletion, and the xml.etree library. If
 this isn't the right list to be posting this question, please let me know.
 I'm new to Python and don't know of any other help me Python mailing
 lists. I have tried debugging this ad-infinitem. Anyway, at the bottom of
 this e-mail you will find the code of a python file. This is a gross
 over-simplification of my code, with little exception handling so that the
 errors are obvious.

 Running this interactively, if you finish off with 'del db', it exits fine
 and creates a skeleton xml file called 'db.xml' with text 'root /'.
 However, if you instead CTRL-D, it throws at exception while quitting and
 then leaves an empty 'db.xml' which won't work. Can anyone here help me
 figure out why this is?

 Stuff I've done:
 I've traced this down to the self.commit() call in __del__. The stacktrace
 and a few print statements injected into xml.etree leads me to the call
 'root'.encode('us-ascii') throwing a LookupError on line 751 of
 xml.etree.ElementTree. This makes no sense to me, since it works fine
 normally.

 Thank you very much. Any and all help or pointers are appreciated.

 ~Matt

  db.py ###
 from xml.etree import ElementTree as ET
 import os

 class Database(object):
 def __init__(self, path):
 self.__dbpath = path## Path to the database
 self.load()
 def __del__(self):
 ## FIXME: Known bug:
 ##  del db at command line works properly
 ##  Ctrl-D, when there is no db file present, results in a
 LookupError
 ##and empty xml file
 from StringIO import StringIO
 from traceback import print_exc
 trace = StringIO()
 try:
 print 5
 self.commit()
 print 7
 except Exception:
 print_exc(100, trace)
 print trace.getvalue()
 def load(self):
 if os.path.exists(self.__dbpath):
 self.root = ET.parse(self.__dbpath).getroot()
 else:
 self.root = ET.Element(root)
 def commit(self):
 ET.ElementTree(self.root).write(self.__dbpath)
 db = Database('db.xml')

 Actually, it all runs well for me -- after the following modification:

 def __del__(self):
 ## FIXME: Known bug:
 ##  del db at command line works properly
 ##  Ctrl-D, when there is no db file present, results in a LookupError
 ##and empty xml file
 try:
 print 5
 self.commit()
 print 7
 except Exception:
 raise

I must be missing something I run the following code (in DB.py) without any 
other files in the current directory:
from xml.etree import ElementTree as ET import os class Database(object):
def __init__(self, path):self.dbpath = path## Path to the database  
  self.load()def __del__(self):try:print 5  
  self.commit()print 7except Exception:raise
def load(self):if os.path.exists(self.dbpath):self.root = 
ET.parse(self.dbpath).getroot()else:self.root = 
ET.Element(root)def commit(self):
ET.ElementTree(self.root).write(self.dbpath) db = Database('db.xml')

Output:
5 Exception LookupError: LookupError('unknown encoding: us-ascii',) in bound 
method Database.__del__ of __main__.Database object at 0x87870 ignored

If you're not getting the same output, please let me know what your environment 
is. Perhaps this is an implementation difference across platforms.



 Notes:
 * I don't know for what reason you needed such a complicated traceback
 construct.

That was only to demonstrate the error. Without that, you see a LookupError 
without any trace.

 * Before I did this modif, I indeed had a weird exception about stringIO.
Top-level imports are not consistently available in __del__. That shouldn't be 
necessary with the code I have above.

 * __del__() seems to do the contrary: it writes back to file through 
 commit()???
Yes, I know. In my actual code, there is a flag that is set when certain 
run-time conditions are met or when the user wants the DB to be saved on quit. 
Most of the time, however, modifications to the database need to be done in 
memory because they are not intended to be saved.

 * del db works fine, anyway
 * When I run without any bd.xml, it properly creates one with text root /.
 * When I run with an ampty db.xml, I have the following exception message:

 Traceback (most recent call last):
   File xmlTree.py, line 29, in module
 db = 

Re: [Tutor] py-editdist

2009-04-20 Thread ALAN GAULD
I mean you need a source installation not a binary one, it is looking for 
python.h 
which is one of the C header files in the Python source code.

 Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/






From: Emad Nawfal (عماد نوفل) emadnaw...@gmail.com
To: Alan Gauld alan.ga...@btinternet.com
Cc: tutor@python.org
Sent: Monday, 20 April, 2009 6:55:31 PM
Subject: Re: [Tutor] py-editdist




2009/4/20 Alan Gauld alan.ga...@btinternet.com


Emad Nawfal (عماد نوفل) emadnaw...@gmail.com wrote


e...@emad-desktop:~/Desktop/py-editdist-0.3$ python setup.py build
running build

gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall

editdist.c:22:20: error: Python.h: No such file or directory

Looks like you need the Python source code installed,
do you have that?

Alan G 

Python comes with Ubuntu, or do you mean that I should re-install it from 
source code? 


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



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد 
الغزالي
No victim has ever been more repressed and alienated than the truth

Emad Soliman Nawfal
Indiana University, Bloomington

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


Re: [Tutor] py-editdist

2009-04-20 Thread عماد نوفل
On Mon, Apr 20, 2009 at 2:13 PM, ALAN GAULD alan.ga...@btinternet.comwrote:

 I mean you need a source installation not a binary one, it is looking for
 python.h
 which is one of the C header files in the Python source code.

 Alan Gauld
 Author of the Learn To Program website
 http://www.alan-g.me.uk/


 --
 *From:* Emad Nawfal (عماد نوفل) emadnaw...@gmail.com
 *To:* Alan Gauld alan.ga...@btinternet.com
 *Cc:* tutor@python.org
 *Sent:* Monday, 20 April, 2009 6:55:31 PM
 *Subject:* Re: [Tutor] py-editdist



 2009/4/20 Alan Gauld alan.ga...@btinternet.com


 Emad Nawfal (عماد نوفل) emadnaw...@gmail.com wrote

  e...@emad-desktop:~/Desktop/py-editdist-0.3$ python setup.py build
 running build
 gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall
 editdist.c:22:20: error: Python.h: No such file or directory


 Looks like you need the Python source code installed,
 do you have that?

 Alan G

 Python comes with Ubuntu, or do you mean that I should re-install it from
 source code?


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



 Thank you Alan,,

Problem solved. I installed the Python-dev packages and everything worked
fine.


 --
 لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
 الغزالي
 No victim has ever been more repressed and alienated than the truth

 Emad Soliman Nawfal
 Indiana University, Bloomington
 




-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
No victim has ever been more repressed and alienated than the truth

Emad Soliman Nawfal
Indiana University, Bloomington

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


Re: [Tutor] String Encoding problem

2009-04-20 Thread Martin Walsh
Matt wrote:
 Hey everyone,
 
 I'm hoping someone here can help me solve an odd problem (bug?). I'm
 having trouble with string encoding, object deletion, and the xml.etree
 library. If this isn't the right list to be posting this question,
 please let me know. I'm new to Python and don't know of any other help
 me Python mailing lists. I have tried debugging this ad-infinitem.
 Anyway, at the bottom of this e-mail you will find the code of a python
 file. This is a gross over-simplification of my code, with little
 exception handling so that the errors are obvious.
 
 Running this interactively, if you finish off with 'del db', it exits
 fine and creates a skeleton xml file called 'db.xml' with text 'root
 /'. However, if you instead CTRL-D, it throws at exception while
 quitting and then leaves an empty 'db.xml' which won't work. Can anyone
 here help me figure out why this is?
 
 Stuff I've done:
 I've traced this down to the self.commit() call in __del__. The
 stacktrace and a few print statements injected into xml.etree leads me
 to the call 'root'.encode('us-ascii') throwing a LookupError on line 751
 of xml.etree.ElementTree. This makes no sense to me, since it works fine
 normally.

The environment available to __del__ methods during program termination
is wonky, and apparently not very consistent either. I can't say that I
completely understand it myself, perhaps someone else can provide a
better explanation for both of us, but some of the causes are described
in the documentation:

http://docs.python.org/reference/datamodel.html#object.__del__

What is your rationale for using __del__? Are you trying to force a
'commit()' call on Database instances when your program terminates -- in
the case of an unhandled exception, for example?

HTH,
Marty

 
 Thank you very much. Any and all help or pointers are appreciated.
 
 ~Matt
 
  db.py ###
 from xml.etree import ElementTree as ET
 import os
 
 class Database(object):
 def __init__(self, path):
 self.__dbpath = path## Path to the database
 self.load()
 def __del__(self):
 ## FIXME: Known bug:
 ##  del db at command line works properly
 ##  Ctrl-D, when there is no db file present, results in a
 LookupError
 ##and empty xml file
 from StringIO import StringIO
 from traceback import print_exc
 trace = StringIO()
 try:
 print 5
 self.commit()
 print 7
 except Exception:
 print_exc(100, trace)
 print trace.getvalue()
 def load(self):
 if os.path.exists(self.__dbpath):
 self.root = ET.parse(self.__dbpath).getroot()
 else:
 self.root = ET.Element(root)
 def commit(self):
 ET.ElementTree(self.root).write(self.__dbpath)
 db = Database('db.xml')

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


Re: [Tutor] String Encoding problem

2009-04-20 Thread Kent Johnson
On Mon, Apr 20, 2009 at 10:46 AM, Matt hellzfury+pyt...@gmail.com wrote:
 Running this interactively, if you finish off with 'del db', it exits fine
 and creates a skeleton xml file called 'db.xml' with text 'root /'.
 However, if you instead CTRL-D, it throws at exception while quitting and
 then leaves an empty 'db.xml' which won't work. Can anyone here help me
 figure out why this is?

 Stuff I've done:
 I've traced this down to the self.commit() call in __del__. The stacktrace
 and a few print statements injected into xml.etree leads me to the call
 'root'.encode('us-ascii') throwing a LookupError on line 751 of
 xml.etree.ElementTree. This makes no sense to me, since it works fine
 normally.

Please show the exact error message and stack trace when you post
errors, it can be very helpful.

What you are doing with __del__ is unusual and not common practice. A
better way to ensure cleanup is to use a close() method which a client
must call, or to use a context manager and 'with' statement.

I think the reason your code is failing is because some module needed
by the encode() call has already been unloaded before your __del__()
method is called.

 Thank you very much. Any and all help or pointers are appreciated.

If you defined a close() method, you could write client code like this:

from contextlib import closing
with closing(Database('db.xml')) as db:
  # do something with db
  # when this block exits db will be closed

It's also not too hard to make an openDatabase() function so you could write
with (openDatabase('db.xml')) as db:
  # etc

though that is not really a beginner challenge. Some notes and further
pointers here:
http://personalpages.tds.net/~kent37/kk/00015.html

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


Re: [Tutor] Webpy vs Django

2009-04-20 Thread Tim Michelsen

I can recommend you web2py:
http://www.web2py.com/

It has been designed for didactical needs and has a low learning curve.

Look at the manual extarct for an idea:
Free manual chapters - 
http://mdp.cti.depaul.edu/examples/static/web2py_manual_cut.pdf


or check the docs:
http://www.web2py.com/examples/default/docs

All the best,
Timmie

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


Re: [Tutor] Sending a disconnect after openssl s_client command?

2009-04-20 Thread Kayvan Sarikhani
On Mon, Apr 20, 2009 at 1:17 PM, Martin Walsh mwa...@mwalsh.org wrote:

 from subprocess import Popen, PIPE

 openssl_cmd = 'openssl s_client -ssl2 -connect somewebsitename:443'
 openssl = Popen(
  openssl_cmd, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE
 )
 stdout, stderr = openssl.communicate('GET /')

 Alternatively, if you're using python 2.6 and above, it looks like you
 can do something similar with a few lines of code, and the ssl module
 from the standard lib ...

 # untested!
 import ssl
 try:
cert = ssl.get_server_certificate(
('somewebsitename', 443), ssl.PROTOCOL_SSLv2
)
 except ssl.SSLError, ex:
# site may not support sslv2
...

 HTH,
 Marty


Thanks Marty; this does indeed help...it just also means I need to really
learn how subprocess works. ;) I wish I could claim to be using 2.6, but
unfortunately the most current version at work is Python 2.5.2...most boxes
here are even below, and I can't convince them to upgrade. Ah, well.

Thanks again though!

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


Re: [Tutor] Looping

2009-04-20 Thread Alan Gauld


Matt hellzfury+pyt...@gmail.com wrote


As an aside, why use range rather than xrange? I was under the impression
that xrange is a generator and therefore more memory efficient.


xrange was a kludge to improve on range's memory efficiency
but it is a horrible name that obscures the code.

Also it does not exist in v3 so if you use it you will need to change
the code for v3. It is as well to be as consistent with v3 as possible
IMHO

Alan G





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


Re: [Tutor] String Encoding problem

2009-04-20 Thread Strax-Haber, Matthew (LARC-D320)
Sorry about that. Hopefully this is better:
In operator:
def __init__(self, path, saveDB=True, cleanUp=True):
'''Constructor'''
## Calculate filesystem paths
self.WORK_DIR= path + '.tmp'
DB_PATH= path + '.xml'
self.SAVE_DB= saveDB## finish(): Delete unnecessary files created 
by run?
self.CLEANUP= cleanUp## finish(): Delete database at end of run?

## Make sure we have a working directory (exception on failed write)
if not os.path.isdir(self.WORK_DIR):
os.mkdir(self.WORK_DIR)

self._db = DB.Database(DB_PATH)
## SOME OTHER ENVIRONMENT SETUP STUFF

def _cleanUpEnvironment(self):
'''Delete temp files created for this run'''
try:
for path,dirs,files in os.walk(self.WORK_DIR, topdown=False):
for f in files:os.unlink(os.path.join(path,f))
for d in dirs:os.rmdir(os.path.join(path,d))
os.rmdir(self.WORK_DIR)
except:
print sys.stderr, 'Could not delete temp files; left at:'
print sys.stderr, self.WORK_DIR

def finish(self):
'''Clean up and finish the run (write out to the database)'''
if self.SAVE_DB:self._db.commit()
if self.CLEANUP:self._cleanUpEnvironment()

def __del__(self):
## FIXME: Known bug:
##  del t at command line works properly
##  Ctrl-D, when there is no db file present, results in a LookupError
self.finish()

if __name__ == '__main__':
printHelp()
## Provide tab completion to the user
import readline, rlcompleter
readline.parse_and_bind('tab: complete')
t= OperatorClassName(os.path.splitext(__file__)[0])


In database:
def __init__(self, path):
'''Constructor'''
self.__dbpath = path## Path to the database
self.load()

def load(self):
'''Read the database out from the file'''
from xml.parsers.expat import ExpatError

if os.path.exists(self.__dbpath):
## Noticed exceptions: IOError, ExpatError
try:
self.root = ET.parse(self.__dbpath).getroot()
except ExpatError:
raise ExpatError('Invalid XML in ' + self.__dbpath)
else:
self.root = ET.Element(root)

def commit(self):
'''Write the database back to the file'''
## Noticed exceptions: IOError
ET.ElementTree(self.root).write(self.__dbpath)
--
~Matthew Strax-Haber
National Aeronautics and Space Administration
Langley Research Center (LaRC)
Co-op, Safety-Critical Avionics Systems Branch
W: 757-864-7378; C: 561-704-0029
Mail Stop 130
matthew.strax-ha...@nasa.gov



From: Martin Walsh mwa...@mwalsh.org
Date: Mon, 20 Apr 2009 16:05:01 -0500
To: Python Tutor tutor@python.org
Cc: Strax-Haber, Matthew (LARC-D320) matthew.strax-ha...@nasa.gov
Subject: Re: [Tutor] String Encoding problem

Forwarding to the list. Matt, perhaps you can repost in plain text, my
mail client seems to have mangled your source ...

Strax-Haber, Matthew (LARC-D320) wrote:
 *From: *Martin Walsh mwa...@mwalsh.org

 The environment available to __del__ methods during program termination
 is wonky, and apparently not very consistent either. I can't say that I
 completely understand it myself, perhaps someone else can provide a
 better explanation for both of us, but some of the causes are described
 in the documentation:

 http://docs.python.org/reference/datamodel.html#object.__del__

 What is your rationale for using __del__? Are you trying to force a
 'commit()' call on Database instances when your program terminates -- in
 the case of an unhandled exception, for example?

 Perhaps I oversimplified a bit. In my actual code, there is a database
 class and an operator class. The actual structure is this:

 In operator:
 def __init__(self, path, saveDB=True, cleanUp=True):
'''Constructor'''## Calculate filesystem paths
self.WORK_DIR= path + '.tmp'DB_PATH= path
 + '.xml'self.SAVE_DB= saveDB## finish(): Delete
 unnecessary files created by run?self.CLEANUP= cleanUp##
 finish(): Delete database at end of run?## Make sure we
 have a working directory (exception on failed write)if not
 os.path.isdir(self.WORK_DIR):os.mkdir(self.WORK_DIR)

self._db = DB.Database(DB_PATH)
 ## SOME OTHER ENVIRONMENT SETUP STUFF
 def _cleanUpEnvironment(self):  try:## Delete
 temp files created for this runfor path,dirs,files in
 os.walk(self.WORK_DIR, topdown=False):for f in files:
os.unlink(os.path.join(path,f))for d in dirs:
os.rmdir(os.path.join(path,d))os.rmdir(self.WORK_DIR)
except:print sys.stderr, 'Could not delete temp
 files; left at:'print sys.stderr, self.WORK_DIRdef
 finish(self):'''Clean up and finish the run (write out to
 the database)'''if self.SAVE_DB:

Re: [Tutor] String Encoding problem

2009-04-20 Thread Strax-Haber, Matthew (LARC-D320)
I've solved the problem by passing on the work of deciding when to commit to 
client code. This isn't ideal but it will do what is necessary and 
unfortunately I don't have any more time to dedicate to this. I hate not being 
able to find a reasonable workaround :/.
--
~Matthew Strax-Haber
National Aeronautics and Space Administration
Langley Research Center (LaRC)
Co-op, Safety-Critical Avionics Systems Branch
W: 757-864-7378; C: 561-704-0029
Mail Stop 130
matthew.strax-ha...@nasa.gov



From: Kent Johnson ken...@tds.net
Date: Mon, 20 Apr 2009 16:55:16 -0500
To: Strax-Haber, Matthew (LARC-D320) matthew.strax-ha...@nasa.gov
Cc: Python Tutor tutor@python.org
Subject: Re: [Tutor] String Encoding problem

Can you give us a simple description of what you are trying to do? And
if you can post in plain text instead of HTML that would be helpful.

Maybe this will give you some ideas - you can trap the control-D and
do your cleanup:
http://openbookproject.net/pybiblio/tips/wilson/simpleExceptions.php

Kent

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


Re: [Tutor] Looping

2009-04-20 Thread Paul McGuire
 
 xrange was a kludge to improve on range's memory efficiency
 but it is a horrible name that obscures the code.
 
 Also it does not exist in v3 so if you use it you will need to change
 the code for v3. It is as well to be as consistent with v3 as possible
 IMHO
 
 Alan G

I have felt this way also (i.e., xrange was a kludge and it is a horrible
name), and have resisted the general adoption of xrange in my code.
Fortunately, I rarely use range, but iterate over sequences directly; or if
I absolutely need the item's index, I iterate over enumerate(seq).

But even if you use range exclusively, you may need to change code when
migrating to Python 3.  In Py2, range returns a list; in Py3 range returns
an iterator (a la Py2-xrange's behavior).  If you have code that uses the
value returned from range as a list, then in Py3 you will need to explicitly
convert it to a list using list(range(n)).

I concur with Alan's suggestion that you code to Py3 semantics, but there
are certainly cases where xrange makes sense vs. range (such as doing some
sort of repetitive test 1e8 times, there is no sense in creating a
100-million entry list just to iterate over it).  So I'll propose some
usages for those who use range:

1. For Py2-Py3 range-xrange compatibility, add this code to the top of your
Python scripts:

try:
range = xrange
except NameError:
pass

In this way, your code under Py2 will use xrange whenever you call range,
and you will adopt the future-compatible range behavior.  (Hmm, maybe this
would have been a good option to add to the future module...)

2. In all cases where you really must use the result returned from range as
a list, ALWAYS write this as list(range(n)), as in:

nonnegative_numbers_up_to_but_not_including_10 = list(range(10))
even_numbers_up_to_but_not_including_20 = list(range(0,20,2))

Using the list(range(n)) form when you actually need a list will prepare
you for the change in idiom when you upgrade to Py3, and is fully Py2
compatible (even if you don't first bind xrange to range as in suggestion 1
- it simply copies the original list).  It also makes it very explicit that
you REALLY WANT THE RANGE AS A LIST, and not just as something for counting
up to n.

-- Paul


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


[Tutor] Functional Derivatives

2009-04-20 Thread Matt
I'm trying to calculate the derivative of a function in Python like so:

def D5(func,h=1e-5):
    ' Return derivative of function func'''
    def df(x):return (func(x+h)-func(x))/h
df.__name__ = func.__name__ + '_dx'
    return df

However, I run into the problem of limited float precision. This is
evident for this:
import math
print D5(D5(D5(D5(math.sin(0.3)
 = -5551.11512313
print math.sin(0.3)
 = 0.295520206661
Is there any way that any of you can think of to avoid this for
general-purpose functions? Thanks.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Functional Derivatives

2009-04-20 Thread Chris Fuller

You should look up numerical methods or similar.  There are ways of 
rearranging your calculations to minimize the loss of precision.  You could 
also try a numerical/scientific library like GMP (for integers or rational 
numbers).  I don't know of any (Python) library for extended precision 
floats, but there ought to be.

Cheers

On Monday 20 April 2009 18:53, Matt wrote:
 I'm trying to calculate the derivative of a function in Python like so:

 def D5(func,h=1e-5):
     ' Return derivative of function func'''
     def df(x):return (func(x+h)-func(x))/h
 df.__name__ = func.__name__ + '_dx'
     return df

 However, I run into the problem of limited float precision. This is
 evident for this:
 import math
 print D5(D5(D5(D5(math.sin(0.3)
  = -5551.11512313
 print math.sin(0.3)
  = 0.295520206661
 Is there any way that any of you can think of to avoid this for
 general-purpose functions? Thanks.
 ___
 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] Functional Derivatives

2009-04-20 Thread Kent Johnson
On Mon, Apr 20, 2009 at 7:53 PM, Matt hellzfury+pyt...@gmail.com wrote:
 I'm trying to calculate the derivative of a function in Python like so:

 def D5(func,h=1e-5):
     ' Return derivative of function func'''
     def df(x):    return (func(x+h)-func(x))/h
    df.__name__ = func.__name__ + '_dx'
     return df

 However, I run into the problem of limited float precision.

 Is there any way that any of you can think of to avoid this for
 general-purpose functions? Thanks.

mpmath?
http://code.google.com/p/mpmath/

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


Re: [Tutor] Reading from files problem

2009-04-20 Thread Chris Castillo
this worked for me fine.

#Author - Chris Castillo
#Created 4/19/09
#Program 6

import time

#Open and read text file with student information
gradesfile = open(grades.dat, r)
time_acc = time.asctime()

#Ask user for student ID to lookup student information
sought_id = raw_input(Please enter a student identifier to look up grades:
)



#Assign values for each students' info in list
for line in gradesfile:
lines = line.split(,)
student_id = lines[0]
last_name = lines[1]
first_name = lines[2]
major = lines[3]
hw1 = lines[4]
hw2 = lines[5]
hw3 = lines[6]
hw4 = lines[7]
hw5 = lines[8]
hw6 = lines[9]
hw7 = lines[10]
test1 = lines[11]
test2 = lines[12]
test3 = lines[13]

#Arithmetic operations for all students' grades
total_points = 550
hw_grades = float(hw1) + float(hw2) + float(hw3) + float(hw4) +
float(hw5) + float(hw6) + float(hw7)
test_grades = float(test1) + float(test2) + float(test3)
student_points = float(hw_grades) + float(test_grades)
avg_grade = round(float(student_points / total_points) * 100.0, 2)

#Assign grades to letter values
if avg_grade  60:
 letter_grade=F
elif avg_grade  70:
 letter_grade=D
elif avg_grade  80:
 letter_grade=C
elif avg_grade  90:
 letter_grade=B
elif avg_grade  101:
 letter_grade=A
else:
 print avg_grade, is not valid

#variable for output to request.dat text file
req_output = student_id + ,  + last_name + ,  + first_name + ,  +
time_acc

#Check to see if student id number is in the list of student id numbers
if lines[0] == sought_id:

new_file = open(requests.dat, w+)
new_file.write(req_output)


print \n, last_name + , + first_name + ,  + student_id + , 
+ major
print \n, time_acc
print \nExams - , test1 + ,  + test2 + ,  + test3
print Homework - , hw1, , ,hw2, , , hw3, , , hw4, ,, hw5,
, , hw6, , , hw7
print \nTotal points earned - , student_points
print \nGrade:  , avg_grade, that is a , letter_grade

gradesfile.close()

hope this helps someone. I know it's probably not the most efficient way to
handle this kind of problem but It works nonetheless.






On Mon, Apr 20, 2009 at 8:24 AM, Alan Gauld alan.ga...@btinternet.comwrote:


 Scott SA py...@rscorp.ab.ca wrote

  May have been a bad assumption on my part as I envisioned pickling a
  dict. and that just got too complicated.


 A pickled dict?
 That would be a shelve mebbe?


  PS. My email is acting up, did my prev. message actually make it to  the
 list?


 Yes, I saw it just after sending mine...

 Alan G


 ___
 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] Webpy vs Django

2009-04-20 Thread vishwajeet singh
Thanks for your answer but can you give me reasons for same.

On Tue, Apr 21, 2009 at 2:38 AM, Tim Michelsen
timmichel...@gmx-topmail.dewrote:

 I can recommend you web2py:
 http://www.web2py.com/

 It has been designed for didactical needs and has a low learning curve.

 Look at the manual extarct for an idea:
 Free manual chapters -
 http://mdp.cti.depaul.edu/examples/static/web2py_manual_cut.pdf

 or check the docs:
 http://www.web2py.com/examples/default/docs

 All the best,
 Timmie


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




-- 
Cheers,
Vishwajeet
http://www.singhvishwajeet.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Functional Derivatives

2009-04-20 Thread Chris Fuller
His problem was composing four functions, each with a small error.  The first 
two applications work well enough, but there is a about a percent error in 
the third composition.  The big source of error is f(x+h)-f(x).  Subtracting 
two floating point numbers that are nearly equal is a known source of 
innaccuracy.  Scaling all the terms by a very large number reduced the error, 
but not as well as fewer compositions.

Cheers

BIGNUM = 1

def D5(func, h=1e-5):
' Return derivative of function func'''
def df(x):
return (BIGNUM*func(x+h)-BIGNUM*func(x))/(BIGNUM*h)
return df

import math

print D5(math.sin)(0.3)
print math.cos(0.3)

print D5(D5(math.sin))(0.3)
print -math.sin(0.3)

print
print D5(D5(D5(math.sin)))(0.3)
print -math.cos(0.3)

# actually, other powers, higher or lower, work less well..
BIGNUM = 1e10
print D5(D5(D5(math.sin)))(0.3)
print -math.cos(0.3)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor