[Tutor] installing Turbogears without using Internet

2007-05-14 Thread ShivKumar Anand

hello Tutors,
 
I want solution to my problem. I want to install Turbogears and Cherrypy 
without using internet. means that locally is i have eggs. how it is possible?
 
Shiv Kumar
_
Palate Teasers: Straight from Master Chef!
http://content.msn.co.in/Lifestyle/Moreonlifestyle/LifestylePT_101106_1530.htm___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generating simple HTML from within Python

2007-05-14 Thread Duncan Gibson
> I've been searching for a module to allow simple HTML generation, but
> most of them appear to be more concerned with *parsing* HTML and XML.
> The only one that looks promising from reviews dating back to 1998 or
> so is HTMLgen, but the link on starship.python.net appears long dead.
> 
> So my question is, what is the preferred way of generating simple HTML
> or XHTML files these days? Is there a 'son of HTMLgen' or similar module?

OK, my thanks to everyone who replied, either on the list or privately.
I followed the links, and the links from those links, and poked around
in the various documentation. What I want to generate depends on the
contents of directories, etc. and is therefore has more variation than
is handled easily in the templating systems.

I chose the HyperText module (http://dustman.net/andy/python/HyperText/)
because this fits more naturally with the code I'm updating, and also
allows subclassing for customisation, etc.

To give you a flavour, one code snippet changes from:

if len(theCase.inputData.relatedFiles)>0:
htmlFile.write('The related file(s) for this case are:\n')
htmlFile.write('\n')
for related_file in theCase.inputData.relatedFiles:
htmlFile.write('%s\n' % (
related_file, related_file))
htmlFile.write('\n')

to the slightly more verbose, but much clearer and less error-prone:

if len(testCaseData.inputData.relatedFiles) > 0:
text = "The related file(s) for this case are:"
heading = HTML.H2(text)
document.append(heading)
bulletList = HTML.UL()
for fileName in testCaseData.inputData.relatedFiles:
anchor = HTML.A(fileName, href=fileName)
listItem = HTML.LI(anchor)
bulletList.append(listItem)
document.append(bulletList)
 
and the module handles all of the start and end tags, indentation and other
pretty printing. My only concern is that it is almost as old as HTMLgen :-(

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


Re: [Tutor] web spider

2007-05-14 Thread Sönmez Kartal
Hello,
I'm working on a web spider project right now. And, I suggest you look 
at sgmllib and urllib modules of standard library, then you will realize 
some stuff in your mind.

Happy coding...
-- 
Sönmez Kartal

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


[Tutor] Adding a (Python) Command Button in Maya

2007-05-14 Thread Preecha Bundrikwong

Dear Tutor,

I'm pretty new to Python. I want to add a command button in Maya. Once
clicked, it runs Python's script/program. I'm not sure where I should write
to ask Python or Maya;

Not in details, please tell me briefly so that I can investigate more from
there. Your suggestion would be highly appreciated!


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


Re: [Tutor] Getting value from web page

2007-05-14 Thread Kent Johnson
Vladimir Strycek wrote:
> Hi all,
> 
> i have a page which when i download from web by python and put tu 
> variable have something like:
> 
>  
>  
>  119/1157/43/40  
>  
> 
> neer end ( actualy this is only thing on that page +  )
> 
> What i need actualy is to get this values 119/1157/43/40 to 
> variables, they are changing allthe time, but they stay numbers and 
> "/" is always between numbers, they are always 4
> 
> I tried using re to search for it but without luck :(
> 
> could somebody more experienced with re than me how to do it ?
> 
> something like match = re.match('+/',htmltxt)  (in htmltxt is the source 
> code downloaded from web)  <-- example not working ;)

You should use re.search(), not re.match() - match() only looks at the 
start of the text. Try
   match = re.search(r'(\d+)/(\d+)/(\d+)/(\d+)')

Then match.groups(1, 2, 3, 4) will be a tuple of the string 
representations of the numbers.

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


Re: [Tutor] installing Turbogears without using Internet

2007-05-14 Thread Kent Johnson
ShivKumar Anand wrote:
> hello Tutors,
>  
> I want solution to my problem. I want to install Turbogears and Cherrypy 
> without using internet. means that locally is i have eggs. how it is 
> possible?

http://peak.telecommunity.com/DevCenter/EasyInstall#installing-on-un-networked-machines

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


[Tutor] ActivePython and CPython

2007-05-14 Thread OkaMthembo

Hi guys,

What are the difference between ActiveState's Python distributions and the
standard Python distribution from python.org, and which is better?
Also, what IDE will give a user the ability to view HTML designs of a web
application as well as the code view (like Dreamweaver does with PHP pages)?

--
"The Stupidry Foundry"
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ActivePython and CPython

2007-05-14 Thread Senthil_OR

What are the difference between ActiveState's Python distributions and
the standard Python distribution from   python.org  ,
and   which is better?

 
Answer:  Python from python.org is Open Source Software developed under
OSI compatible license. Bunch of Python Developers are working on fixing
bugs and releasing next version of python on various platforms.
ActiveState provides a commertial distribution of same python with
support options. They have a custom built installer, which will install
extra modules like Python Windows Extensions and Documentation like
regex tutorials and Mark Pilgrim's excellent work "Dive Into Python".
All these are available for download from web however.
 
I dont think there is a question of which is better. Both are better,
check with your requirements. If you willing to contribute back to
python community and raise bugs against Python software, download and
use the latest from python.org 
 
 Also, what IDE will give a user the ability to view HTML designs of a
web application as well as the code view (like Dreamweaver does with PHP
pages)? 
 
Answer: PyDev for Eclipse turns Eclipse into a pretty good IDE for
Python. You may figure out similar extension for Eclipse for HTML
development as well. That would satisfy your need. As it said, Eclipse
is an IDE for any thing in general and nothing in particular.
 
HTH.
 
Senthil
 

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


Re: [Tutor] Adding a (Python) Command Button in Maya

2007-05-14 Thread Senthil_OR
 
From:  Preecha Bundrikwong
Sent: Monday, May 14, 2007 3:24 PM
To: tutor@python.org
Subject: [Tutor] Adding a (Python) Command Button in Maya

I'm pretty new to Python. I want to add a command button in Maya. Once
clicked, it runs Python's script/program. I'm not sure where I should
write to ask Python or Maya;
 
Answer: Design it in Maya and call it via Python. Thats what I
understand in this tutorial on Python Maya Package.
All the solutions for your questions should be here:
 
http://cgkit.sourceforge.net/mayadoc/module-maya.gui.html
 
-- 
Senthil
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ActivePython and CPython

2007-05-14 Thread Alan Gauld
"OkaMthembo" <[EMAIL PROTECTED]> wrote

> What are the difference between ActiveState's Python 
> distributions and the standard Python distribution from 
> python.org, and which is better?

I prefer the ActiveState version for windows, I haven't 
tried their Luinux/Mac versions. Basically the Windows 
version comes with a bunch of extras targetting windows 
users. You get most of it by adding the winall package 
linked from python.org but ActiveState package it all up 
nicely. The help system is much better too and includes 
some extra material.

> Also, what IDE will give a user the ability to view HTML 
> designs of a web application as well as the code view 
> (like Dreamweaver does with PHP pages)?

Dreamweaver, I believe, will work with Python too.
But you don't generally embed python into HTML 
as you do with PHP or ASP (although some solutions do)
Modern thinking on web design suggests you should 
separate the code and HTML as much as possible and 
most web frameworks use a templating system like 
Cheetah/Kid to do that.

Any XML/HTML editor should give you a web preview 
of any of the templating languages.

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] ActivePython and CPython

2007-05-14 Thread Alan Gauld

<[EMAIL PROTECTED]> wrote 
> Answer: PyDev for Eclipse turns Eclipse into a pretty good IDE for
> Python. 

Agreed, I downloaded pydev over the weekend and have been 
playing. The editor isn't exactly vim/emacs but its as good as 
IDLE/Pythonwin

However I think the OP wants an HTML preview and I don't see that 
facility in Eclipse anywhere.

Alan G.

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


Re: [Tutor] Web GUI for a network management tool

2007-05-14 Thread Thanos Panousis
I know jfreechart is java only. It looks quite capable but the
colleagues here say its not the easiest tool to use.

I was about to start with a JSP solution for my web interface. But a
short tutorial convinced me that writing this thing in java/jsp is
simply crazy My god, coming from everyday python development it
just shocked me to see how much code these guys are actually
writing Just to do the most obvious of tasks.

I am now seriously considering a matplotlib/python web framework
solution. I think that the single advantage of rapid development will
be so overwhelming compared to the J2EE monstrosities that it will
compensate for any other dissadvantages. I just hope that matplotlib
plays well with the web, and that I can integrate it to play with an
MVC architecture that is employed by turbogears for example.

On 5/11/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Thanos Panousis wrote:
> > I 'm going to need some hardcore graphing functionalities. And for
> > that reason alone, I think jfreechart is the only thing that provides
> > free and decent solution, also designed with the web in mind.
>
> jfreechart is a Java library so it will not integrate easily into a
> Python web framework.
>
> Did you look at matplotlib? It is a very capable graphing library. You
> can see examples of its output here:
> http://matplotlib.sourceforge.net/screenshots.html
>
> > So everybody seems to prefer some framework even for not complicated
> > (business) logic. I 'll try to go for something lightweight.
>
> web.py is pretty light, I think:
> http://webpy.org/
>
> Kent
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Web GUI for a network management tool

2007-05-14 Thread Kent Johnson
Thanos Panousis wrote:
> I was about to start with a JSP solution for my web interface. But a
> short tutorial convinced me that writing this thing in java/jsp is
> simply crazy My god, coming from everyday python development it
> just shocked me to see how much code these guys are actually
> writing Just to do the most obvious of tasks.

Yes. And then they look at you crazy if you suggest there might be a 
better way. I'm a refugee from Java myself.
> 
> I am now seriously considering a matplotlib/python web framework
> solution. I just hope that matplotlib
> plays well with the web, and that I can integrate it to play with an
> MVC architecture that is employed by turbogears for example.

I am using matplotlib and Django to create dynamic charts, it works 
fine. Maybe you missed my earlier pointer to a recipe for this:
http://www.scipy.org/Cookbook/Matplotlib/Django

The recipe creates a chart in memory and returns it as a string. 
Something very similar should work with other web frameworks.
> 
> On 5/11/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
>> Thanos Panousis wrote:
>> > I 'm going to need some hardcore graphing functionalities. And for
>> > that reason alone, I think jfreechart is the only thing that provides
>> > free and decent solution, also designed with the web in mind.
>>
>> jfreechart is a Java library so it will not integrate easily into a
>> Python web framework.
>>
>> Did you look at matplotlib? It is a very capable graphing library. You
>> can see examples of its output here:
>> http://matplotlib.sourceforge.net/screenshots.html
>>
>> > So everybody seems to prefer some framework even for not complicated
>> > (business) logic. I 'll try to go for something lightweight.
>>
>> web.py is pretty light, I think:
>> http://webpy.org/
>>
>> Kent
>>
> 

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


Re: [Tutor] Adding a (Python) Command Button in Maya

2007-05-14 Thread Michiel Duvekot
 

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED]
> Sent: Monday, May 14, 2007 9:31 AM
> To: [EMAIL PROTECTED]; tutor@python.org
> Subject: Re: [Tutor] Adding a (Python) Command Button in Maya
> 
>  
> From:  Preecha Bundrikwong
> Sent: Monday, May 14, 2007 3:24 PM
> To: tutor@python.org
> Subject: [Tutor] Adding a (Python) Command Button in Maya
> 
> I'm pretty new to Python. I want to add a command button in 
> Maya. Once clicked, it runs Python's script/program. I'm not 
> sure where I should write to ask Python or Maya;
>  
> Answer: Design it in Maya and call it via Python. Thats what 
> I understand in this tutorial on Python Maya Package.
> All the solutions for your questions should be here:
>  
> http://cgkit.sourceforge.net/mayadoc/module-maya.gui.html

Not quite. The site you're referring to is a different project. You
should refer to the Maya manuals for information on how to create menu
items in Python. If you have questions about Maya and Python, a good
place to ask is the Maya forum on the Area
http://discussion.autodesk.com/adskcsp/forum.jspa?forumID=201 (free
registration) or Ask Autodesk at
http://forums.alias.com/WebX?14@@.3bb21e03 (requires Subscription)

To create a button on the shelf, you would MMB-drag a command from the
ScriptEditor's Python tab to the shelf. Here is an example that will
make a button that will open the help docs for the button command.

import maya.cmds as cmds
cmds.help ('button',doc=True)

--
Michiel


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


Re: [Tutor] Parsing text file

2007-05-14 Thread Dave Kuhlman
On Sun, May 13, 2007 at 03:04:36PM -0700, Alan wrote:
> I'm looking for a more elegant way to parse sections of text files that 
> are bordered by BEGIN/END delimiting phrases, like this:
> 
> some text
> some more text
> BEGIN_INTERESTING_BIT
> someline1
> someline2
> someline3
> END_INTERESTING_BIT
> more text
> more text
> 
> What I have been doing is clumsy, involving converting to a string and 
> slicing out the required section using split('DELIMITER'): 
> 
> import sys
> infile = open(sys.argv[1], 'r')
> #join list elements with @ character into a string
> fileStr = '@'.join(infile.readlines())
> #Slice out the interesting section with split, then split again into 
> lines using @
> resultLine = 
> fileStr.split('BEGIN_INTERESTING_BIT')[1].split('END_INTERESTING_BIT')[0].split('@')
> for line in resultLine:
> do things
> 
> Can anyone point me at a better way to do this?
> 

Possibly over-kill, but ...

How much fun are you interested in having?  Others have given you
the "low fun" easy way.  Now ask yourself whether this task is
likely to become more complex (the interesting parts more hidden in
a more complex grammar) and perhaps you also can't wait to have
some fun.  Is so, consider this suggestion:

1. Write grammar rules that describe your input text.  In your
   case, those rules might look something like the following:

   Seq ::= {InterestingChunk | UninterestingChunk}*
   InterestingChunk ::= BeginToken InterestingSeq EndToken
   InterestingSeq ::= InterestingChunk*


2. For each rule, write a Python function that tries to recognize
   what the rule describes.  To do its job, each function might
   call other functions that implement other grammar rules and
   might call a tokenizer function (see below) when it needs
   another token from the input stream.  Example:

   def InterestingChunk_reco(self):
   if self.token_type == Tok_Begin:
   self.get_token()
   if self.InterestingSeq_reco():
   if self.token_type == Tok_End:
   self.get_token()
   return True
   else:
   self.Error('bad interesting sequence')

3. Write a tokenizer function.  Each time this function is called,
   it returns the next "token" (probably a word) from the input
   stream and a code that indicates the token type.  Recognizer
   functions call this tokenizer function each time another token
   is needed.  In your case there might be 3 token types: (1) plain
   word, (2) BeginTok, and (3) EndTok.

If you do the above, you have just written your first recursive
descent parser.

Then, the next time you are at a party, beer bar, or wedding, any
time the conversation comes even remotely close to the subject of
parsing text, you say, "Well, for that kind of problem I usually
write a recursive descent parser.  It's the most powerful way and
the only way to go.  ..." Now, that's how to impress your friends
and relations.

But, seriously, recursive descent parsers are quite easy and are a
useful technique to have in your tool bag.  And, like I said above:
It's fun.

Besides, if your problem becomes more complex, and, for example,
the input is not quite so line oriented, you will need a more
powerful approach.

Wikipedia has a better explanation than mine plus an example and
links: http://en.wikipedia.org/wiki/Recursive_descent_parser

I've attached a sample solution and sample input.

Also, be aware that there are parse generators for Python.

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
#!/usr/bin/env python
# -*- mode: pymode; coding: latin1; -*-
"""
Recognize and print out interesting parts of input.
A recursive descent parser is used to scan the input.

Usage:
python recursive_descent_parser.py [options] 
Options:
-h, --help  Display this help message.
Example:
python recursive_descent_parser.py infile

Grammar:
Seq ::= {InterestingChunk | UninterestingChunk}*
InterestingChunk ::= BeginToken InterestingSeq EndToken
InterestingSeq ::= InterestingChunk*
"""


#
# Imports

import sys
import getopt


#
# Globals and constants

# Token types:
Tok_EOF, Tok_Begin, Tok_End, Tok_Word = range(1, 5)


#
# Classes

class InterestingParser(object):
def __init__(self, infilename=None):
self.current_token = None
if infilename:
self.infilename = infilename
self.read_input()
#print self.input
self.get_token()
def read_input(self):
self.infile = open(self.infilename, 'r')
self.input = []
for line in self.infile:
self.input.extend(line.rstrip('\n').split(' '))
self.infile.close()
self.input_iterator = iter(self.input)
def parse(self):
return self.Seq_reco()
def get_token(self):
try:
token = self.input_iterator.next()
except StopIteration, e:
token = None
self.token = token

[Tutor] How to test for a remainder from division

2007-05-14 Thread Matt Smith
Hi there,

I'm trying to write a short function to test whether a year is a leap
year or not. To do this I need to check whether the year divides exactly
by 4, 100 and 400. I can't think of an easy way to test whether there is
a remainder or not. The best I can come up with so far is:

if (year / 4.0) - (year // 4.0) <> 0:

This doesn't seem to work, it is always True, is there a problem with
the comparison? The arithmetic seems to be the correct way to isolate
the remainder of the division.

Can anyone suggest a better way of performing this test or alternately,
how can I get the line above to work.

Thanks,

Matt


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


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

2007-05-14 Thread Luke Paireepinart
Matt Smith wrote:
> Hi there,
>
> I'm trying to write a short function to test whether a year is a leap
> year or not. To do this I need to check whether the year divides exactly
> by 4, 100 and 400. I can't think of an easy way to test whether there is
> a remainder or not. The best I can come up with so far is:
>
> if (year / 4.0) - (year // 4.0) <> 0:
>
> This doesn't seem to work, it is always True, is there a problem with
> the comparison? The arithmetic seems to be the correct way to isolate
> the remainder of the division.
>
> Can anyone suggest a better way of performing this test or alternately,
> how can I get the line above to work.
> Matt
>   
Matt:  I'm not sure about your pseudocode, but have you tried to 
accomplish this with the modulus operator?
It provides the remainder of integer division (i.e. a remainder of 0 
indicates a perfect divisor.)
so you could do:
if year % 4 or year % 100 or year % 400: #then it's divisible perfectly 
by any of [4,100,400]
for example.
HTH,
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2007-05-14 Thread Eric Walstad
Hey Matt,

Skirting your question that looks like a modulo issue[1]...

Maybe isleapyear[2] will work for you:

import calendar

calendar.isleap(2007)  -> False
calendar.isleap(2008)  -> True


I hope that helps,

Eric.
[1] http://docs.python.org/ref/binary.html
[2] http://docs.python.org/lib/module-calendar.html

Matt Smith wrote:
> Hi there,
> 
> I'm trying to write a short function to test whether a year is a leap
> year or not. To do this I need to check whether the year divides exactly
> by 4, 100 and 400. I can't think of an easy way to test whether there is
> a remainder or not. The best I can come up with so far is:
> 
> if (year / 4.0) - (year // 4.0) <> 0:
> 
> This doesn't seem to work, it is always True, is there a problem with
> the comparison? The arithmetic seems to be the correct way to isolate
> the remainder of the division.
> 
> Can anyone suggest a better way of performing this test or alternately,
> how can I get the line above to work.
> 
> Thanks,
> 
> Matt
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
--
_

Eric Walstad
2856 Diamond Street
San Francisco, CA 94131
[EMAIL PROTECTED]
415-864-4224
_
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] easiest way to get true/false of odd/even number

2007-05-14 Thread shawn bright

nevermind, i found it.

4 %2 = 0  (even)
5 %2 = 1 (odd)

pretty simple

shawn

On 5/14/07, shawn bright <[EMAIL PROTECTED]> wrote:


Hey there all,
i know this sounds kinda easy, but i was wanting the least verbose way to
get a True / False of a number being odd (not even)
thanks
shawn

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


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

2007-05-14 Thread Bill Campbell
On Mon, May 14, 2007, Luke Paireepinart wrote:
>Matt Smith wrote:
>> Hi there,
>>
>> I'm trying to write a short function to test whether a year is a leap
>> year or not. To do this I need to check whether the year divides exactly
>> by 4, 100 and 400. I can't think of an easy way to test whether there is
>> a remainder or not. The best I can come up with so far is:
>>
>> if (year / 4.0) - (year // 4.0) <> 0:
>>
>> This doesn't seem to work, it is always True, is there a problem with
>> the comparison? The arithmetic seems to be the correct way to isolate
>> the remainder of the division.
>>
>> Can anyone suggest a better way of performing this test or alternately,
>> how can I get the line above to work.
>> Matt
>>   
>Matt:  I'm not sure about your pseudocode, but have you tried to 
>accomplish this with the modulus operator?
>It provides the remainder of integer division (i.e. a remainder of 0 
>indicates a perfect divisor.)
>so you could do:
>if year % 4 or year % 100 or year % 400: #then it's divisible perfectly 
>by any of [4,100,400]

While the modulus operator is generally useful for things like
this, one might want to use the calendar module for this if it's
smarter about some of the subtle quirks of leapyear:

import calendar
if calendar.isleap(year): ...

Bill
--
INTERNET:   [EMAIL PROTECTED]  Bill Campbell; Celestial Software, LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
FAX:(206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676

We contend that for a nation to try to tax itself into prosperity is like a
man standing in a bucket and trying to lift himself up by the handle.
-- Winston Churchill
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2007-05-14 Thread Arthur Coleman
Hi,

I believe it should be if !(year % 4) or !(year % 100) or !(year % 400)

Since 4*n % 4 = 0 for n integer

Arthur

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Luke Paireepinart
Sent: Monday, May 14, 2007 2:32 PM
To: Matt Smith
Cc: Python Tutor
Subject: Re: [Tutor] How to test for a remainder from division

Matt Smith wrote:
> Hi there,
>
> I'm trying to write a short function to test whether a year is a leap
> year or not. To do this I need to check whether the year divides exactly
> by 4, 100 and 400. I can't think of an easy way to test whether there is
> a remainder or not. The best I can come up with so far is:
>
> if (year / 4.0) - (year // 4.0) <> 0:
>
> This doesn't seem to work, it is always True, is there a problem with
> the comparison? The arithmetic seems to be the correct way to isolate
> the remainder of the division.
>
> Can anyone suggest a better way of performing this test or alternately,
> how can I get the line above to work.
> Matt
>   
Matt:  I'm not sure about your pseudocode, but have you tried to 
accomplish this with the modulus operator?
It provides the remainder of integer division (i.e. a remainder of 0 
indicates a perfect divisor.)
so you could do:
if year % 4 or year % 100 or year % 400: #then it's divisible perfectly 
by any of [4,100,400]
for example.
HTH,
-Luke
___
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] How to test for a remainder from division

2007-05-14 Thread John Fouhy
On 15/05/07, Arthur Coleman <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I believe it should be if !(year % 4) or !(year % 100) or !(year % 400)

FWIW, the correct leapyear test is:

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
# then year is a leap year

Year 2100 will not be a leap year, despite being divisible by 4.  But
y2k was a leap year, because it was a multiple of 400.

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


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

2007-05-14 Thread Bob Gailer
Luke Paireepinart wrote:
> Matt Smith wrote:
>   
>> Hi there,
>>
>> I'm trying to write a short function to test whether a year is a leap
>> year or not. To do this I need to check whether the year divides exactly
>> by 4, 100 and 400. I can't think of an easy way to test whether there is
>> a remainder or not. The best I can come up with so far is:
>>
>> if (year / 4.0) - (year // 4.0) <> 0:
>>
>> This doesn't seem to work, it is always True, is there a problem with
>> the comparison? The arithmetic seems to be the correct way to isolate
>> the remainder of the division.
>>
>> Can anyone suggest a better way of performing this test or alternately,
>> how can I get the line above to work.
>> Matt
>>   
>> 
> Matt:  I'm not sure about your pseudocode, but have you tried to 
> accomplish this with the modulus operator?
> It provides the remainder of integer division 
No, it provides the modulus, and applies to float not just integer!. For 
positive left argument that happens to = the remainder, but not for 
negative!
See http://en.wikipedia.org/wiki/Modular_arithmetic which is how it 
works in python.


-- 
Bob Gailer
510-978-4454

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


Re: [Tutor] version and path

2007-05-14 Thread linda.s
how to check how many versions of Python i have in my mac machine?
also, how to put the path to the version I desire?
Thanks a lot!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2007-05-14 Thread Alan Gauld

"Bob Gailer" <[EMAIL PROTECTED]> wrote

>> Matt:  I'm not sure about your pseudocode, but have you tried to
>> accomplish this with the modulus operator?
>> It provides the remainder of integer division
> No, it provides the modulus, and applies to float not just integer!.

While modulus is technically correct I prefer to use the
term modulo since modulus can also be used to mean the
magnitude or absolute value of a number. i.e. its true to say
that abs(-7) provides the modulus of -7 (i.e. 7) which is different
to the modulo - which is a binary operator...

But one of the nice things about Python is that, whatever we
call it, Python does it correctly! :-)

Alan G. 


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


[Tutor] easiest way to get true/false of odd/even number

2007-05-14 Thread shawn bright

Hey there all,
i know this sounds kinda easy, but i was wanting the least verbose way to
get a True / False of a number being odd (not even)
thanks
shawn
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] ctypes typedef

2007-05-14 Thread Adam Pridgen
Hey everyone,

I am messing around with the ctypes package, and I wasn wondering how
I go about defining a new type, where I essentially call an int foo.

For example in C, I would use the following statement:
typedef int foo;

Is there an equivalent statement using ctypes, or is there another way
of performing this task?

Thanks in advance,

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


[Tutor] Web

2007-05-14 Thread Jeff Molinari
How exacly can you use python to create a web site? 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.
   
-
Be a better Globetrotter. Get better travel answers from someone who knows.
Yahoo! Answers - Check it out.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2007-05-14 Thread Kent Johnson
Eric Walstad wrote:
> Hey Matt,
> 
> Skirting your question that looks like a modulo issue[1]...
> 
> Maybe isleapyear[2] will work for you:
> 
> import calendar
> 
> calendar.isleap(2007)  -> False
> calendar.isleap(2008)  -> True

And one of the nice things about Python, as well as having lots of 
useful stuff built in, much of the useful stuff is itself written in 
Python making it very easy to look under the hood and see what is going 
on. 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)

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


Re: [Tutor] version and path

2007-05-14 Thread Tom Tucker
I would use the traditional Unix find command to find the various
versions installed.

find / -name "python*"



On 5/14/07, linda.s <[EMAIL PROTECTED]> wrote:
> how to check how many versions of Python i have in my mac machine?
> also, how to put the path to the version I desire?
> Thanks a lot!
> ___
> 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] Web

2007-05-14 Thread Tom Tucker
Jeff,
Can you clarify what you are looking to do.  You have a small program
that you want to distribute via Internet (allow users to view,
download, etc)? You want to use Python to create a website? Integrate
your program into a web page (modpython for example).



On 5/14/07, Jeff Molinari <[EMAIL PROTECTED]> wrote:
> How exacly can you use python to create a web site? 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.
>
>  
> Be a better Globetrotter. Get better travel answers from someone who knows.
> Yahoo! Answers - Check it out.
> ___
> 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] version and path

2007-05-14 Thread linda.s
On 5/14/07, Tom Tucker <[EMAIL PROTECTED]> wrote:
> I would use the traditional Unix find command to find the various
> versions installed.
>
> find / -name "python*"
>
>
>
> On 5/14/07, linda.s <[EMAIL PROTECTED]> wrote:
> > how to check how many versions of Python i have in my mac machine?
> > also, how to put the path to the version I desire?
> > Thanks a lot!

That is what I got:
find: /.Spotlight-V100: Permission denied
find: /.Trashes: Permission denied
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] version and path

2007-05-14 Thread Tom Tucker
If you let the command finish, you should have seen the following or
something equivalent below.  Running the find command I sent you
works, however you will see some noise (Permission Denied message).
These are common messages because your general user account is trying
to access restricted parts of the filesystem.  This is normal
behavior.

Here are some example files on my Mac system.

/usr/bin/python
/usr/bin/python2.3
/usr/bin/pythonw
/usr/bin/pythonw2.3
/usr/lib/python2.3

I'm assuming you are running a stock version of OS X and you have NOT
installed Python manually.  The typical Python path on my version OS X
(11.4) is /usr/bin/python. /usr/bin/python is a link to the actual
Python binary installed at /usr/bin/python2.3

For example...

>which python
/usr/bin/python

>ls -l /usr/bin/python
lrwxr-xr-x   1 root  wheel  9 Jan 17  2006 /usr/bin/python -> python2.3


Does this help?



On 5/14/07, linda. s <[EMAIL PROTECTED]> wrote:
> On 5/14/07, Tom Tucker <[EMAIL PROTECTED]> wrote:
> > I would use the traditional Unix find command to find the various
> > versions installed.
> >
> > find / -name "python*"
> >
> >
> >
> > On 5/14/07, linda.s <[EMAIL PROTECTED]> wrote:
> > > how to check how many versions of Python i have in my mac machine?
> > > also, how to put the path to the version I desire?
> > > Thanks a lot!
>
> That is what I got:
> find: /.Spotlight-V100: Permission denied
> find: /.Trashes: Permission denied
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] html links

2007-05-14 Thread max .

does anyone know of a tutorial for finding links in a web site with python.

or creating files and asking ware to create a file.

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


Re: [Tutor] html links

2007-05-14 Thread Luke Paireepinart
max . wrote:
> does anyone know of a tutorial for finding links in a web site with 
> python.
I believe BeautifulSoup could help with this, but am uncertain.
>
> or creating files and asking ware to create a file.
There are many ways to do this. The simplest would be:
filepath = raw_input("Where would you like to create the file? ")
And this can vary in difficulty; if you wanted, for example, a GUI that 
lets them
browse their directory tree graphically for the file they want, you 
could do that too.
>
> thanks
yw
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2007-05-14 Thread Matt Smith
Hi,

Thanks for all the help, 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. The modulus operator was just what I was
looking for, I had been trying to check for a difference between the
division and the floor division - a bit of a long winded way of doing
things. Here 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

# 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


Re: [Tutor] html links

2007-05-14 Thread Sönmez Kartal
Hi,

import urllib
from BeautifulSoup import BeautifulSoup

page = urllib.urlopen(url) # url is the page address, if you are reading 
a file in your hard drive you could use just open(filename) too, and no 
needing to urllib
soup = BeautifulSoup(page)

#links = [str(link) for link in soup.findAll('a')]
links = soup.findAll('a')

I have used list compherension, comment outed line, because findAll 
function returns a different type specific to BeautifulSoup. First line, 
list compherension used converts every link to string.

Happy coding...

-- 
Sönmez Kartal


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