[Tutor] File access by os.system

2007-05-15 Thread lohith madireddy
Hello,
   I am kind of stuck with this problem for many days. I would
really appreciate the person who helps me in solving this problem.
Here is how I am stuck..
I have files named 'x.pdb', 'y.pdb',. in one directory. I am
using python to read the files in that directory and do a system
operation on each of the file using os.system. When I use this, it
always gives an error saying 'could not read the file'.
   I use a for loop to pass each file to the executable. The
problem comes in the identification of this file by system. To make
you clear here is the rough code I was trying to use.

import sys,os,tempfile,shutil
Pdbs = os.listdir(os.getcwd())
temp1=tempfile.TemporaryFile()
for Pdb in Pdbs:
print(type(Pdb))
os.system("dsspcmbi -v Pdb temp1")


I will have to process that temp1 file after I get the output from the
executable 'dsspcmbi'. The system is not recognising file name Pdb.
Please tell me how can I obviate this problem.
Thanks in advance.

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


Re: [Tutor] Web

2007-05-15 Thread Alan Gauld
"Jeff Molinari" <[EMAIL PROTECTED]> wrote

> How exacly can you use python to create a web site? 

Short question, lots of answers!
Have a look at the WebProgramming Topic Guide on python.org.

http://wiki.python.org/moin/WebProgramming

And check out the CgiScripts topic then the Web Frameworks topic.

> I have a small program I'm working on that I would like 
> to create available via internet but I dont know how to 
> make python work through internet.

At a very basic level you just need to get the python
program to spit out html on stdout and set it up as a cgi program.
But in practice there is more to consider and the topic guides 
should give you a feel for things.

I'm actually writing a couple of new topics for my tutorial that 
covers web programming but at the current rate of progress 
they won't be finished till the end of the year!

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] html links

2007-05-15 Thread Alan Gauld

"max ." <[EMAIL PROTECTED]> wrote

> does anyone know of a tutorial for finding links in a web site with 
> python.
>
Beautifulsuop has been mentioned but its not part of standard python.

Her is an example of the standard library parser:

html = '''
Test page


Here is the first heading

A short paragraph
A second heading
A paragraph containing a
hyperlink to python

'''

from HTMLParser import HTMLParser

class H1Parser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.h1_count = 0
self.isHeading = False

def handle_starttag(self,tag,attributes=None):
if tag == 'h1':
self.h1_count += 1
self.isHeading = True

def handle_endtag(self,tag):
if tag == 'h1':
self.isHeading = False

def handle_data(self,data):
if self.isHeading and self.h1_count == 2:
print "Second Header contained: ", data

parser = H1Parser()
parser.feed(html)
parser.close()

> or creating files and asking ware to create a file.

I'm not sure what you mean here? Do you mean fetching a file
from a remote server? There is an ftp module if its from an ftp 
site...


-- 
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] Text matching

2007-05-15 Thread Gardner, Dean
 So I took Kents advice and refactored but I have uncovered another
problem which I am hoping people may be able to help with. I realized
that the string I was using to identify the end of a record can in some
cases not be found (i.e. if a commit wasn't reviewed). This can lead to
additional records being returned. 

Can anyone suggest how I can get round this?

Text file example ( in this case looking for commit 1 would give details
of commit two also):

 SomeSpec -0001 

> some commit 1

Reviewed By: someone
 SomeSpec -0002 
> some commit 2

 SomeSpec -0003 
>some commit 1
Reviewed By: Someone


Code:

 def searchChangeLog(self,filename):
uid = self.item.Uid()
record=[]
logList=[]
displayList=[]
f = open(filename)
logTextFile="temp.txt"
""" searched through the changelog 'breaking' it up into
individual entries"""

for line in f:
if ("Reviewed: 000" in line):
logList.append(record)
record = []
else:
record.append(line)

""" searches to determine if we can find entries for
a particualr item"""
for record in logList:
for item in record:
if uid in item:
displayList.append(record)
""" creates a temporary file to write our find results to """
removeFile = os.path.normpath( os.path.join(os.getcwd(),
logTextFile))

# if the file exists, get rid of it before writing our new
findings
if Shared.config.Exists(removeFile):
os.remove(removeFile)
recordLog = open(logTextFile,"a")

for record in range(len(displayList)):
for item in displayList[record]:
recordLog.write(item)
recordLog.close()
#display our results
commandline = "start cmd /C " + logTextFile
os.system(commandline)

Dean Gardner
Test Engineer 
Barco
Bonnington Bond, 2 Anderson Place, Edinburgh EH6 5NP, UK
Tel + 44 (0) 131 472 5731 Fax + 44 (0) 131 472 4799
www.barco.com 
[EMAIL PROTECTED] 


-Original Message-
From: Kent Johnson [mailto:[EMAIL PROTECTED] 
Sent: 04 May 2007 11:26
To: Gardner, Dean
Cc: tutor@python.org
Subject: Re: [Tutor] Text matching

Gardner, Dean wrote:
>  
> So here it isit might not be pretty (it seems a bit un-python like

> to me) but it works exactly as required. If anyone can give any tips 
> for possible optimisation or refactor I would be delighted to hear 
> from them.
> 
> Thanks
> 
> uid = self.item.Uid()
> record=[]
> logList=[]
> displayList=[]
> f = open(filename)
> logTextFile="temp.txt"
> """ searched through the changelog 'breaking' it up into
> individual entries"""
> try:
> while 1:
> endofRecord=0
> l = f.next()
> if l.startswith(""):
> record.append(l)
> l=f.next()
> while endofRecord==0:
> if "Reviewed: 000" not in l:
> record.append(l)
> l=f.next()
> else:
> logList.append(record)
> record=[]
> endofRecord=1
> except StopIteration:
> pass

I don't think you need endofRecord and the nested loops here. In fact I
think you could use a plain for loop here. AFAICT all you are doing is
accumulating records with no special handling for anything except the
end records. What about this:
record = []
for line in f:
   if "Reviewed: 000" in line:
 logList.append(record)
 record = []
   else:
 record.append(line)

> """ searches to determine if we can find entries for
> a particualr item"""
> for record in logList:
> currRec = record
> for item in currRec:
> if uid in item:
> displayList.append(currRec)

The currRec variable is not needed, just use record directly.
If uid can only be in a specific line of the record you can test that
directly, e.g.
for record in logList:
   if uid in record[1]:

> """ creates a temporary file to write our find results to """
> removeFile = os.path.normpath( os.path.join(os.getcwd(),
> logTextFile))
> 
> # if the file exists, get rid of it before writing our new 
> findings
> if Shared.config.Exists(removeFile):
> os.remove(removeFile)
> recordLog = open(logTextFile,"a")
> 
> for record in range(len(displayList)):
> for item in displayList[record]:
> recordLog.write(item)

for record in displayList:
   recordLog.writelines(record)

> recordLog.close()
> #display our results
> commandline = "start cmd /C " + logTextFile
> os.syste

Re: [Tutor] File access by os.system

2007-05-15 Thread Alan Gauld

"lohith madireddy" <[EMAIL PROTECTED]> 

>I have files named 'x.pdb', 'y.pdb',. in one directory. I am
> using python to read the files in that directory and do a system
> operation on each of the file using os.system. When I use this, it
> always gives an error saying 'could not read the file'.

There are several likely problems.
The first is that the files might not have security pernissions set 
for you to manipulate - can you do the system() command from 
the OS prompt oK?

Second, you are only passing the name of the file not the full 
path so the system command can't find the file. 

Third you could be passing something other than a 
file - eg another directory - to the command.

> import sys,os,tempfile,shutil
> Pdbs = os.listdir(os.getcwd())
> temp1=tempfile.TemporaryFile()
> for Pdb in Pdbs:

You probably need to use stat here to check that the item 
really is a file and that it has permissions.

Alternatively use a try except clause and if an exception 
is raised simply use continue to start the next loop iteration.
(possibly after printing an error message with the pdb name 
in it so you can check why it failed later)

>print(type(Pdb))
>os.system("dsspcmbi -v Pdb temp1")

You can get more about doing stat checks in the OS topic 
of my tutorial.

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


[Tutor] ImportError: No module named _apache

2007-05-15 Thread ShivKumar Anand

dear all,
 
I am trying to implement a text from mod_python manual. the code is:
 
from mod_python import apache
def handler(req):req.content_type ="text/plain\n\n"
req.send_http_header()req.write("Hello World!!!")return apache.ok
 
and the conf file of apache is having
  AddHandler mod_python .py  
PythonHandler SoapTest  PythonDebug On  I am getting this error 
message:
 
Traceback (most recent call last):  File "C:\Python24\exp\Web\SoapTest.py", 
line 1, in ?from mod_python import apache  File 
"C:\Python24\lib\site-packages\mod_python\apache.py", line 28, in ?import 
_apacheImportError: No module named _apache
 
where as i have mod_python installed and there is apache.py file also in the mod
-python dir.
 
Pls give me solution.
 
Thanks
Shiv
_
Sign in and get updated with all the action!
http://content.msn.co.in/Sports/FormulaOne/Default___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to test for a remainder from division

2007-05-15 Thread Kent Johnson
Matt Smith wrote:
> ere is the final code I have come up with, any comments?
> 
> # A program to determine whether a year is a leap year or not
> 
> def is_leap_year(year):
> # Function that accepts a year as input and returns true if it is a leap
> year, false if it is not
> if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
> return True
> else:
> return False

There is no need for the if statement, you can return the result directly:
   return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

Kent

> 
> # Main program logic
> year = raw_input("What year? ")
> year = int(year)
> if is_leap_year(year):
> print year, "is a leap year."
> else:
> print year, "is not a leap year"
> 
> Thanks,
> 
> Matt
> 
> 
> ___
> 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] Text matching

2007-05-15 Thread Kent Johnson
Gardner, Dean wrote:
>  So I took Kents advice and refactored but I have uncovered another
> problem which I am hoping people may be able to help with. I realized
> that the string I was using to identify the end of a record can in some
> cases not be found (i.e. if a commit wasn't reviewed). This can lead to
> additional records being returned. 
> 
> Can anyone suggest how I can get round this?

Your sample data doesn't seem to match either the problem you describe 
or the code. I suggest looking for the start of a record rather than the 
end. At the end of the record-finding loop you will have one last record 
still to process in the record list.

Kent
> 
> Text file example ( in this case looking for commit 1 would give details
> of commit two also):
> 
>  SomeSpec -0001 
> 
>> some commit 1
> 
> Reviewed By: someone
>  SomeSpec -0002 
>> some commit 2
> 
>  SomeSpec -0003 
>> some commit 1
> Reviewed By: Someone
> 
> 
> Code:
> 
>  def searchChangeLog(self,filename):
> uid = self.item.Uid()
> record=[]
> logList=[]
> displayList=[]
> f = open(filename)
> logTextFile="temp.txt"
> """ searched through the changelog 'breaking' it up into
> individual entries"""
> 
> for line in f:
> if ("Reviewed: 000" in line):
> logList.append(record)
> record = []
> else:
> record.append(line)
> 
> """ searches to determine if we can find entries for
> a particualr item"""
> for record in logList:
> for item in record:
> if uid in item:
> displayList.append(record)
> """ creates a temporary file to write our find results to """
> removeFile = os.path.normpath( os.path.join(os.getcwd(),
> logTextFile))
> 
> # if the file exists, get rid of it before writing our new
> findings
> if Shared.config.Exists(removeFile):
> os.remove(removeFile)
> recordLog = open(logTextFile,"a")
> 
> for record in range(len(displayList)):
> for item in displayList[record]:
> recordLog.write(item)
> recordLog.close()
> #display our results
> commandline = "start cmd /C " + logTextFile
> os.system(commandline)
> 
> Dean Gardner
> Test Engineer 
> Barco
> Bonnington Bond, 2 Anderson Place, Edinburgh EH6 5NP, UK
> Tel + 44 (0) 131 472 5731 Fax + 44 (0) 131 472 4799
> www.barco.com 
> [EMAIL PROTECTED] 
> 
> 
> -Original Message-
> From: Kent Johnson [mailto:[EMAIL PROTECTED] 
> Sent: 04 May 2007 11:26
> To: Gardner, Dean
> Cc: tutor@python.org
> Subject: Re: [Tutor] Text matching
> 
> Gardner, Dean wrote:
>>  
>> So here it isit might not be pretty (it seems a bit un-python like
> 
>> to me) but it works exactly as required. If anyone can give any tips 
>> for possible optimisation or refactor I would be delighted to hear 
>> from them.
>>
>> Thanks
>>
>>uid = self.item.Uid()
>> record=[]
>> logList=[]
>> displayList=[]
>> f = open(filename)
>> logTextFile="temp.txt"
>> """ searched through the changelog 'breaking' it up into
>> individual entries"""
>> try:
>> while 1:
>> endofRecord=0
>> l = f.next()
>> if l.startswith(""):
>> record.append(l)
>> l=f.next()
>> while endofRecord==0:
>> if "Reviewed: 000" not in l:
>> record.append(l)
>> l=f.next()
>> else:
>> logList.append(record)
>> record=[]
>> endofRecord=1
>> except StopIteration:
>> pass
> 
> I don't think you need endofRecord and the nested loops here. In fact I
> think you could use a plain for loop here. AFAICT all you are doing is
> accumulating records with no special handling for anything except the
> end records. What about this:
> record = []
> for line in f:
>if "Reviewed: 000" in line:
>  logList.append(record)
>  record = []
>else:
>  record.append(line)
> 
>> """ searches to determine if we can find entries for
>> a particualr item"""
>> for record in logList:
>> currRec = record
>> for item in currRec:
>> if uid in item:
>> displayList.append(currRec)
> 
> The currRec variable is not needed, just use record directly.
> If uid can only be in a specific line of the record you can test that
> directly, e.g.
> for record in logList:
>if uid in record[1]:
> 
>> """ creates a temporary file to write our find results to """
>> removeFile = os.path.normpath( os.path.join(os.getcwd(),
>> logTextFile))
>>
>>

[Tutor] Text matching

2007-05-15 Thread Jason Massey

heh... forwarding to the list, too.

-- Forwarded message --
From: Jason Massey <[EMAIL PROTECTED]>
Date: May 15, 2007 6:51 AM
Subject: Re: [Tutor] Text matching
To: "Gardner, Dean" <[EMAIL PROTECTED]>

Look at it a different way.  If the one thing that is sure to be there is
the SomeSpec portion, then how about making a listing of where they occur
and slicing everything up.

Concretely, as Mr. Yoo would say:

f = open(r"c:\python24\test.txt",'r').readlines()
logList = []

indices = [x for x,line in enumerate(f) if 'SomeSpec' in line]
for x in range(len(indices)-1):
   logList.append(f[indices[x]:indices[x+1]])

#tack on the last record
logList.append(f[indices[-1]:])

for count,x in enumerate(logList):
   print count,':',x

C:\Python24>test.py
0 : [' SomeSpec -0001 \n', '\n', '> some commit 1\n', '\n',
'Reviewed By: someone\n']
1 : [' SomeSpec -0002 \n', '> some commit 2\n', '\n']
2 : [' SomeSpec -0003 \n', '>some commit 1\n', 'Reviewed By:
Someone']

On 5/15/07, Gardner, Dean <[EMAIL PROTECTED]> wrote:


So I took Kents advice and refactored but I have uncovered another
problem which I am hoping people may be able to help with. I realized
that the string I was using to identify the end of a record can in some
cases not be found ( i.e. if a commit wasn't reviewed). This can lead to
additional records being returned.

Can anyone suggest how I can get round this?

Text file example ( in this case looking for commit 1 would give details
of commit two also):

 SomeSpec -0001 

> some commit 1

Reviewed By: someone
 SomeSpec -0002 
> some commit 2

 SomeSpec -0003 
>some commit 1
Reviewed By: Someone


Code:

def searchChangeLog(self,filename):
uid = self.item.Uid()
record=[]
logList=[]
displayList=[]
f = open(filename)
logTextFile="temp.txt"
""" searched through the changelog 'breaking' it up into
individual entries"""

for line in f:
if ("Reviewed: 000" in line):
logList.append(record)
record = []
else:
record.append(line)

""" searches to determine if we can find entries for
a particualr item"""
for record in logList:
for item in record:
if uid in item:
displayList.append(record)
""" creates a temporary file to write our find results to """
removeFile = os.path.normpath( os.path.join(os.getcwd(),
logTextFile))

# if the file exists, get rid of it before writing our new
findings
if Shared.config.Exists(removeFile):
os.remove(removeFile)
recordLog = open(logTextFile,"a")

for record in range(len(displayList)):
for item in displayList[record]:
recordLog.write (item)
recordLog.close()
#display our results
commandline = "start cmd /C " + logTextFile
os.system(commandline)

Dean Gardner
Test Engineer
Barco
Bonnington Bond, 2 Anderson Place, Edinburgh EH6 5NP, UK
Tel + 44 (0) 131 472 5731 Fax + 44 (0) 131 472 4799
www.barco.com
[EMAIL PROTECTED]


-Original Message-
From: Kent Johnson [mailto: [EMAIL PROTECTED]
Sent: 04 May 2007 11:26
To: Gardner, Dean
Cc: tutor@python.org
Subject: Re: [Tutor] Text matching

Gardner, Dean wrote:
>
> So here it isit might not be pretty (it seems a bit un-python like

> to me) but it works exactly as required. If anyone can give any tips
> for possible optimisation or refactor I would be delighted to hear
> from them.
>
> Thanks
>
> uid = self.item.Uid()
> record=[]
> logList=[]
> displayList=[]
> f = open(filename)
> logTextFile=" temp.txt"
> """ searched through the changelog 'breaking' it up into
> individual entries"""
> try:
> while 1:
> endofRecord=0
> l = f.next()
> if l.startswith(""):
> record.append(l)
> l=f.next()
> while endofRecord==0:
> if "Reviewed: 000" not in l:
> record.append(l)
> l=f.next()
> else:
> logList.append(record)
> record=[]
> endofRecord=1
> except StopIteration:
> pass

I don't think you need endofRecord and the nested loops here. In fact I
think you could use a plain for loop here. AFAICT all you are doing is
accumulating records with no special handling for anything except the
end records. What about this:
record = []
for line in f:
   if "Reviewed: 000" in line:
 logList.append(record)
 record = []
   else:
 record.append(line)

> """ searches to determine if we can find entries for
> a particualr item"""
> 

[Tutor] object design question

2007-05-15 Thread Kent Tenney
Howdy,

I would be interested in some discussion of
which of the following approaches is preferred and why. 

class RstManager:

def __init__(self, text):
self.text = text
self.parseRst()

def parseRst(self):
parsed = 
self.parsed = parsed


class RstManager:

def __init__(self, text):
self.parsed = parseRst(text)

def parseRst(self, text):
parsed = 
return parsed


Thanks,
Kent

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


Re: [Tutor] File access by os.system

2007-05-15 Thread Mike Hansen
> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Alan Gauld
> Sent: Tuesday, May 15, 2007 1:44 AM
> To: tutor@python.org
> Subject: Re: [Tutor] File access by os.system
> 
> 
> "lohith madireddy" <[EMAIL PROTECTED]> 
> 
> >I have files named 'x.pdb', 'y.pdb',. in one directory. I am
> > using python to read the files in that directory and do a system
> > operation on each of the file using os.system. When I use this, it
> > always gives an error saying 'could not read the file'.
> 
> There are several likely problems.
> The first is that the files might not have security pernissions set 
> for you to manipulate - can you do the system() command from 
> the OS prompt oK?
> 
> Second, you are only passing the name of the file not the full 
> path so the system command can't find the file. 
> 
> Third you could be passing something other than a 
> file - eg another directory - to the command.
> 
> > import sys,os,tempfile,shutil
> > Pdbs = os.listdir(os.getcwd())
> > temp1=tempfile.TemporaryFile()
> > for Pdb in Pdbs:
> 
> You probably need to use stat here to check that the item 
> really is a file and that it has permissions.
> 
> Alternatively use a try except clause and if an exception 
> is raised simply use continue to start the next loop iteration.
> (possibly after printing an error message with the pdb name 
> in it so you can check why it failed later)
> 
> >print(type(Pdb))
> >os.system("dsspcmbi -v Pdb temp1")

Should the os.system command be something like
command = "dsspcmbi -v %s %s" %(Pdb, temp1)
os.system(command)

?

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


Re: [Tutor] How to test for a remainder from division

2007-05-15 Thread Kent Johnson
Matt Smith wrote:
>> Here is the implementation of calendar.isleap():
>>
>> def isleap(year):
>>  """Return 1 for leap years, 0 for non-leap years."""
>>  return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
> 
> Hi Kent,
> 
> Thanks for the help. For future reference how do I go look at the
> implementation of a particular function (the ones coded in Python, I
> don't know C)?

Look in the lib directory that was installed with Python. The location 
varies. On Windows look for C:\Python2.x\Lib. My Mac has it at
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5

In the python interpreter, try
 >>> import sys
 >>> sys.path

The lib directory will be listed as one element of sys.path.

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


Re: [Tutor] object design question

2007-05-15 Thread Kent Johnson
Kent Tenney wrote:
> Howdy,
> 
> I would be interested in some discussion of
> which of the following approaches is preferred and why. 
> 
> class RstManager:
> 
> def __init__(self, text):
> self.text = text
> self.parseRst()
> 
> def parseRst(self):
> parsed = 
> self.parsed = parsed
> 
> 
> class RstManager:
> 
> def __init__(self, text):
> self.parsed = parseRst(text)
> 
> def parseRst(self, text):
> parsed = 
> return parsed

If you have no further need of text, I prefer the second. If you need to 
keep text around then use the first one or possibly

class RstManager:

 def __init__(self, text):
 self.text = text
 self.parsed = parseRst(text)

 def parseRst(self, text):
 parsed = 
 return parsed

which makes the assignment to self.parsed more explicit.

Kent

> 
> 
> Thanks,
> 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] File access by os.system

2007-05-15 Thread Rikard Bosnjakovic
On 5/15/07, Mike Hansen <[EMAIL PROTECTED]> wrote:

> Should the os.system command be something like
> command = "dsspcmbi -v %s %s" %(Pdb, temp1)
> os.system(command)
>
> ?

Yes.


-- 
- Rikard - http://bos.hack.org/cv/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to test for a remainder from division

2007-05-15 Thread Eric Walstad
Hey Matt,

Matt Smith wrote:
> I guessed that there would be a module out
> there providing a function to do this but wanted to go through the
> process as a learning exercise.
Good form, old boy.


> Here is the final code I have come up with, any comments?
I think your code looks fine.  I like to do the following, just to be a
little more concise:

def is_leap_year(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

That is, the 'if' in your code is evaluating the statement into a
boolean.  Why not just return the results of that evaluation?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ImportError: No module named _apache

2007-05-15 Thread Eric Walstad
Hey ShivKumar,

ShivKumar Anand wrote:
> *I am getting this error message:*
>  
> Traceback (most recent call last):
>   File "C:\Python24\exp\Web\SoapTest.py", line 1, in ?
> from mod_python import apache
>   File "C:\Python24\lib\site-packages\mod_python\apache.py", line 28, in ?
> import _apache
> ImportError: No module named _apache

Are you trying to call your mod_python code from the command line?  If so:
"you can't import mod_python into scripts that are run from the command
line or any other context outside a running apache server."



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


Re: [Tutor] object design question

2007-05-15 Thread Alan Gauld
"Kent Johnson" <[EMAIL PROTECTED]> wrote
>> Howdy,
>>
>> I would be interested in some discussion of
>> which of the following approaches is preferred and why.
>>
>> class RstManager:
>> def __init__(self, text):
>> self.parsed = parseRst(text)
>> def parseRst(self, text):
>> parsed = 
>> return parsed
>
> If you have no further need of text, I prefer the second. If you 
> need to
> keep text around then use the first one or possibly

I agree with Kent J. It depends a lot on what else your class is 
doing.
If it needs text or if it will parse reguilarly.

However I'm also always suspicious of a class called xxxManager.

Classes are by definition there to manage some data by
providing some behaviour. Usually the Manager bit can
be left off the name. Then the class is just Rst.

I don't know what an Rst is, but my guess is that you will
want one of them as an object rather than some kind of
manager object. To use a more mundane example:

class FileManager:
def open(self, name): 
def close(self):

fm = FileManager('foo.txt')

Do I really want a file manager object or do I want a file?
Which is the object? Often when we thinkmof objects as
managers we are really thinking in terms of some data
that we want to apply functions to. Really we should be
thinking of the class as representing the objects themselves.

If you do have a real manager object it is usual for it to contain
one or more of the things it manages, thius you'd expect
the definition to look something like:

class FooManager:
   def __init__(self, aFoo):
  self.Foos.append(aFoo)

   def sort(self):
   def find(self,aFooRef):

ie The init takes a Foo as an argument and manages a
collection of Foos.

But I don't know your domain and you may well really mean
an RstManager. But its worth considering, it may well make
your code more intuitive to read.

If you want to read much more on this theme try and find
a copy of the book OOP by Coad and Nicola. They discuss
the problems that can arise in an OOP program with Manager
objects.

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


[Tutor] python project books/webs from scratch? and python OOP

2007-05-15 Thread Jesus Rodriguez

Hi!

I have good python knowledge but I don't know how to face a project.Yes, i
can do some small projects, but i'm scared of big projectsI don't know
how to start!!

Then, I'm looking for a book or web with projects, like console text games
or console programs explained from scratch to finish.


On the other hand, I'm looking for book or tutorial of OO{P, D, A}
(programming, analisys and design) with python.


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


Re: [Tutor] How to test for a remainder from division

2007-05-15 Thread Terry Carroll
On Tue, 15 May 2007, Kent Johnson wrote:

> Matt Smith wrote:
> > 
> > Thanks for the help. For future reference how do I go look at the
> > implementation of a particular function (the ones coded in Python, I
> > don't know C)?
> 
> Look in the lib directory that was installed with Python. The location 
> varies. On Windows look for C:\Python2.x\Lib. My Mac has it at
> /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5
> 
> In the python interpreter, try
>  >>> import sys
>  >>> sys.path
> 
> The lib directory will be listed as one element of sys.path.

Will the following approach always work?  It's what I start with.

>>> import calendar
>>> print calendar.__file__
C:\Python25\lib\calendar.py


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


Re: [Tutor] File access by os.system

2007-05-15 Thread Luke Paireepinart
Rikard Bosnjakovic wrote:
> On 5/15/07, Mike Hansen <[EMAIL PROTECTED]> wrote:
>
>   
>> Should the os.system command be something like
>> command = "dsspcmbi -v %s %s" %(Pdb, temp1)
>> os.system(command)
>>
>> ?
>> 
>
> Yes.
>   
Not Quite, I think.
 >>> import tempfile
 >>> help(tempfile.TemporaryFile)
Help on function NamedTemporaryFile in module tempfile:

NamedTemporaryFile(mode='w+b', bufsize=-1, suffix='', prefix='tmp', 
dir=None)
Create and return a temporary file.
Arguments:
'prefix', 'suffix', 'dir' -- as for mkstemp.
'mode' -- the mode argument to os.fdopen (default "w+b").
'bufsize' -- the buffer size argument to os.fdopen (default -1).
The file is created as mkstemp() would do it.
   
Returns an object with a file-like interface; the name of the file
is accessible as file.name.  The file will be automatically deleted
when it is closed.


So I'd expect to use

command = "dsspcmbi -v %s %s" %(Pdb, temp1.name)

That is, unless __str__ and/or __repr__ return the file name also.
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor