[Tutor] Running a python script from another python script

2005-03-26 Thread M.Sinan ORUN
Hello,
I am a newbee in python and trying to make a small script for my school 
project . I try to run  a python cgi script from another phyton script 
as a result of an action . What is the neccesary command for this action 
and is there a special class have to be imported for this command.

below i am giving so for what i have writen and what am i trying to do. 
Thant you for your help

this is my first script for a basic log in action. i want to run the 
second script from this script as a result of login action

# index.cgi# login script
#!/usr/bin/python
import cgi
print "Content-Type: text/html\n\n"
# Define function to generate HTML form.
def generate_form():
   print "\n"
   print "\n"
   print "\tInfo Form\n"
   print "\n"
   print "\n"
   print "\tPlease, enter your UserName and Password.\n"
   print "\t\n"
   print "\t\t\n"
   print "\t\tUserName:\n"
   print "\t\tPassword:\n"
   print "\t\n"
   print "\t\n"
   print "\t\n"
   print "\t\n"
   print "\n"
   print "\n"

# Define main function.
def main():
form = cgi.FieldStorage()
if (form.has_key("action") and form.has_key("UserName") and 
form.has_key("Password")):
 if (form["UserName"].value == "sinan" and form["Password"].value== "666"):
  print "Welcome Boss !!"
   here i need a command to move on my second script 
(index1.cgi)#
 
 else:
  print " You are not recognized !!"
  generate_form()
else:
 print " You are not recognized !!"
 generate_form()
main()


## index1.cgi # this is my second script whish has 
to be runned from the first script
 as a result of login action#
#!/usr/bin/python
import cgi
print "Content-Type: text/html\n\n"

# Define function to generate HTML form.
#menulist
def menulist():
   print "\n"
   print "\n"
   print "\tMenu List\n"
   print "\n"
   print "\n"
   print "\tPlease choice a operation.\n"
   print "\n"
   print ""
   print "People"
   print "Lessons"
   print "Projects"
   print ""
   print ""
   print ""
   print ""
   print ""
   print "\n"
   print "\n"
def Menu():
form = cgi.FieldStorage()
if (form.has_key("operations")):
   if (form["operations"].value == "People"):
   print "people menu"
   elif (form["operations"].value == "Lessons"):
   print "lesson menu"
   elif (form["operations"].value == "Projects"):
   print "project menu"
   else:
   print "error"
else:
   menulist()
Menu()

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


Re: [Tutor] Defining functions

2005-03-26 Thread Jacob S.
Try this.
import sys
def my_raw_input(prompt):
   sys.stdout.write(prompt)## Since they're thinking of bonking off 
print as well.
   a = sys.stdin.readline()
   if a.startswith(prompt):
   return a[:len(prompt)]
   return a

It leaves the '\n' on the end... so it sucks.
I know there is a better way... Someone else-help?
Jacob
So, as a newbie, I see this thread and I check out the PEP and I see
that for future compatibility we should use sys.stdin.readline().  So
I import sys to see how it works.  Of course, sys.stdin.readline('type
anything: ') doesn't work in quite the same way as raw_input('type
anything: ') does.  The closest I can get after a few newbie stabs is:
print 'type anything: ', sys.stdin.readline()
type anything: hello
hello

What is the easiest way to get the exact functionality of raw_input()
(i.e. a prompt, no whitespace at the front, and no trailing \n) using
sys.stdin.readline()?
gabe
On Fri, Mar 25, 2005 at 11:02:43AM -0500, Jacob S. wrote:
Yeah. And they're thinking of removing raw_input() too.  I think it's 
good
to have a __builtin__ user input function.  Why should we have to import
sys everytime we want user input? Almost every program that newbies write
uses it, and advanced programmers also if they're using console programs.
IMHO, I see no reason to remove it.
## end rant

Jacob
>Michael Dunn wrote:
>>Something I've always wondered: if input() is so dangerous, why is it
>>there? What valid uses does it have in the wild?
>
>It's a mistake planned to be removed in Python 3.0, the "hypothetical
>future release of Python that can break backwards compatibility with the
>existing body of Python code."
>
>Python tries very hard to maintain backward compatibility so things like
>input() are not removed.
>
>http://www.python.org/peps/pep-3000.html#built-ins
>
>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
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Re: Dates and databases, and blobs and Python.

2005-03-26 Thread Lee Harr
I have heard a lot of really good things about SQLObject:
http://sqlobject.org/
However, that requires a more full-featured database, like postgresql.
According to the docs SQLObject supports SQLite:
http://sqlobject.org/docs/SQLObject.html#requirements

Wups. I even looked at that page before posting
SQLObject is even better than I thought!
_
FREE pop-up blocking with the new MSN Toolbar - get it now! 
http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/

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


Re: [Tutor] Dates and databases, and blobs and Python.

2005-03-26 Thread Liam Clarke
Hi Kent, 



> Most databases have a DATE type and you can directly store and retrieve date 
> objects. Looking at the
> pysqlite code I see it uses mxDateTime for date support - it doesn't use the 
> Python datetime module.
> 
> So the first step is to install mxDateTime from
> http://www.egenix.com/files/python/mxDateTime.html
> 
> OK, now let's create a table with a DATE column:
>   >>> import sqlite
>   >>> con = sqlite.connect('mydatabase.db')
>   >>> cur = con.cursor()
>   >>> cur.execute('CREATE TABLE foo (aDate DATE)')
> 
> The sqlite Date class is called sqlite.main.Date. (According to the DB-API 
> this should be exposed as
> sqlite.Date but whatever...). Let's create a Date:
>   >>> d=sqlite.main.Date(2005, 3, 26)
>   >>> d
> 
> 
> insert it into the table:
>   >>> cur.execute('insert into foo (aDate) values (%s)', d)
> 
> and read it back:
>   >>> cur.execute('select * from foo')
>   >>> cur.fetchall()
> [(,)]
> 
> Note how the data returned from fetchall() contains a Date object.
> 
> BTW there doesn't seem to be much available in the way of pysqlite docs. You 
> should probably become
> familiar with the DB-API spec (http://www.python.org/peps/pep-0249.html) and 
> the pysqlite source...


Thanks for that, I've installed mxDateTime, and just playing around at
the >>> with it, I can see that it's very useful, and being able to
pass it as a parameter is just what I needed.

And yeah, I'm familiar with the (lack of) documentation for pysqlite.
Apparently a whole new version is in the works, based around the
latest SQLite release, and it's a one man show as far as I can tell,
so perhaps I should get my skills up to speed and write the docs. : )

Regards, 

Liam Clarke


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why cant I return more than once here?

2005-03-26 Thread Kent Johnson
gerardo arnaez wrote:
Hi all, I am working a simple function 
and want it return a
a float
a list
and a dictionary

But it seems that I can only use
return once in the function
cant see to figure out why I can return more than once?
Thanks for the help!
'return' does two things - it specifies what values to return, and it exits the function. You can 
return as many values as you want but it has to be all from one return statement.

return InitalCoumadinWeekRegimen
return reduce ((lambda x,y: x+y), CoumadinValuesList), CoumadinValuesList
Just put all the values in one return:
return InitalCoumadinWeekRegimen, reduce ((lambda x,y: x+y), 
CoumadinValuesList), CoumadinValuesList
BTW reduce ((lambda x,y: x+y), CoumadinValuesList) is the same as 
sum(CoumadinValuesList)
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Why cant I return more than once here?

2005-03-26 Thread gerardo arnaez
Hi all, I am working a simple function 
and want it return a
a float
a list
and a dictionary

But it seems that I can only use
return once in the function
cant see to figure out why I can return more than once?
Thanks for the help!
--

#!/usr/bin/env python
def AddItUp():
# #Note I had to force the raw_input to become integers or else reduce
doesnt work
# Monday = raw_input("Monday: ")
# Tuesday = raw_input("Tuesday: ")
# Wednesday = raw_input("Wednesday: ")
# Thursday = raw_input("Thursday: ")
# Friday = raw_input("Friday: ")
# Saturday = raw_input("Saturday: ")
# Sunday = raw_input("Sunday: ")

InitalCoumadinWeekRegimen = {"Monday":float(Monday),
 "Tuesday":float(Tuesday),
 "Wednesday":float(Wednesday),
 "Thursday":float(Thursday),
 "Friday":float(Friday),
 "Saturday":float(Saturday),
 "Sunday":float(Sunday)
 }
CoumadinValuesList  = InitalCoumadinWeekRegimen.values()
return InitalCoumadinWeekRegimen
return reduce ((lambda x,y: x+y), CoumadinValuesList), CoumadinValuesList
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re: Dates and databases, and blobs and Python.

2005-03-26 Thread Kent Johnson
Lee Harr wrote:
I have heard a lot of really good things about SQLObject:
http://sqlobject.org/
However, that requires a more full-featured database, like postgresql.
According to the docs SQLObject supports SQLite:
http://sqlobject.org/docs/SQLObject.html#requirements
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Re: Dates and databases, and blobs and Python.

2005-03-26 Thread Lee Harr
Just a quick query. I want to store dates and work with them in my
SQLite database.
There's no specific need for any calculations to done on one side or
another (i.e. it's a single user database).
I googled how to work with dates in SQL, and I got one like this -
SELECT * FROM totp
WHERE wk BETWEEN '1980/05/20'
  AND '1980/05/26'
as an example of finding a range for a date. I'm not too sure about
how SQL works with dates (and times for that matter), so I'm tempted
to stick to what I know and just store dates/times as strings in the
db, and then use them to create datetime objects when needed.

I have not worked much with sqlite, but from this page:
http://www.sqlite.org/datatype3.html
it appears that sqlite does not have a date datatype.

theDateTime = datetime.datetime(intD[0], intD[1], intD[2], intT[0], 
intT[1])
Although working with dates like that doesn't seem that flash either.
Well... create a wrapper function to clean it up, right?

Alternatively, I was thinking of storing the actual datetime object in
the database (this is a blob I believe?), and that's a whole new
kettle of fish.
I have heard a lot of really good things about SQLObject:
http://sqlobject.org/
However, that requires a more full-featured database, like postgresql.
_
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

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


Re: [Tutor] a shorter way to write this

2005-03-26 Thread John Fouhy
Kent Johnson wrote:
jrlen balane wrote:
basically, i'm going to create a list with 96 members but with only 
one value:
is there a shorter way to write this one???
[1] * 96
Just a note on this ---
This will work fine for immutable types (such as integers or strings). 
But you can get into trouble if you do this with mutable types.

eg:
>>> l = [[]] * 10  # A list of ten empty lists
>>> l
[[], [], [], [], [], [], [], [], [], []]
>>> l[3].append(2)
>>> l
[[2], [2], [2], [2], [2], [2], [2], [2], [2], [2]]
--
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using python to write games

2005-03-26 Thread John Fouhy
David Holland wrote:
Is there any material anyone knows about how to use
pure python without pygame to write games ?  The
reason for asking, is that although pygame is good it
has the disadvantage of that your users must have
pygame.  It is also harder to create a stand alone
.exe with python ?
Why not distribute pygame with your game?
(I assume that would be allowed)
I mean, you could write your own game library, but then you'd just have 
to distribute that instead...

Also, in terms of creating standalone executables, have a look at py2exe.
--
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using python to write games

2005-03-26 Thread Kent Johnson
David Holland wrote:
Is there any material anyone knows about how to use
pure python without pygame to write games ?  The
reason for asking, is that although pygame is good it
has the disadvantage of that your users must have
pygame.  It is also harder to create a stand alone
.exe with python ?
What kind of games, for what platform? Obviously you could write a text-based game without PyGame. 
You can do simple animations in Tkinter - see http://effbot.org/zone/tkinter-animation.htm and 
http://www.astro.washington.edu/owen/TkinterSummary.html#After. You can play sounds with WinSound.

My guess is that for a complex game you will want things that pygame supplies like double-buffered 
drawing and an event system.

Just a guess though, I haven't done this myself.
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Using python to write games

2005-03-26 Thread David Holland
Is there any material anyone knows about how to use
pure python without pygame to write games ?  The
reason for asking, is that although pygame is good it
has the disadvantage of that your users must have
pygame.  It is also harder to create a stand alone
.exe with python ?

Send instant messages to your online friends http://uk.messenger.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] primes - sieve of odds

2005-03-26 Thread Sean Perry
John Miller wrote:
How does one actually use this module? For example:
 >>> import eratosthenes
 >>> eratosthenes.primes()

 >>> eratosthenes.primes().next()
2
 >>> eratosthenes.primes().next()
2
 >>> eratosthenes.primes().next()
2
How does one get beyond the first prime?
it = eratosthenes.primes()
it.next()
it.next()
or
# 'it' is shorthand for iterator
def primesToN(n):
it = eratosthenes.primes()
return [ x for x in it if x <= n ]
You were running into problems because the primes() function returns a 
new generator. The values are in the generator not the function primes().
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dates and databases, and blobs and Python.

2005-03-26 Thread Sean Perry
Liam Clarke wrote:
And then there's the overall question -
What would be the least fiddly & least error prone way of working with
dates and times? Python or SQL?
Liam, SQL is another language. You need to learn it like you are 
learning Python. It has pretty decent support for dates and times as 
part of the ANSI SQL spec.

There are defined types, SQL functions, etc.
week(date) = week(mydate) for instance.
I tend to prefer to write programming language agnostic databases 
because that way I can interact with them from any random language I 
need to. Sometimes things go bad and a quick little script which does:

rows = exec("select * from table")
exec("begin transaction")
for row in rows:
row[3] = row[2] + row[4] - 5 # we decided on a new value
exec("update fields in table")
can be handy.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] primes - sieve of odds

2005-03-26 Thread John Miller
On Mar 24, 2005, at 6:01 AM, C Smith <[EMAIL PROTECTED]> wrote:
What follows is an attempt based on the previous tutor-evolved sieve
that extends the range in which to find the next prime by a factor of
2 over the last known prime. A similar algorithm is on ASPN (I
bellieve), under
Space-efficient version of sieve of Eratosthenes.
D. Eppstein, May 2004
Oh, please...ignore what I suggested and look at Eppstein's code. It's
a thing of beauty and just keeps chugging out primes well past what the
inefficient version that I suggested could do with the same memory.
It's a "tortoise and hare" race as the memory gets chewed up by the
esieve approach.
The ASPN version of Eppstein's program is an older one than the one at
the following site:
http://www.ics.uci.edu/~eppstein/PADS/Eratosthenes.py
How does one actually use this module? For example:
>>> import eratosthenes
>>> eratosthenes.primes()

>>> eratosthenes.primes().next()
2
>>> eratosthenes.primes().next()
2
>>> eratosthenes.primes().next()
2
How does one get beyond the first prime?
Thanks,
John
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] re question

2005-03-26 Thread Kent Johnson
I don't know why this isn't working for you but this worked for me at a DOS 
console:
 >>> s='850hPa±'
 >>> s
'850hPa\xf1'
 >>> import re
 >>> re.sub('\xf1', '*', s)
'850hPa*'
 >>> import sys
 >>> sys.stdout.encoding
'cp437'
and also in IDLE with a different encoding:
>>> s='850hPa±'
>>> s
'850hPa\xb1'
>>> import re
>>> re.sub('\xb1', '*', s)
'850hPa*'
>>> import sys
>>> sys.stdout.encoding
'cp1252'
So one guess is that the data is in a different encoding than what you expect? When you print the 
string and get '\xb1', is that in the same program that is doing the regex?

Another approach would be to just pull out the numbers and ignore everything 
else:
 >>> s='Std Lvl:  850hPa, 1503m,  16.8C,  15.7C, 205 @ 11kts'
 >>> l=s.split(',')
 >>> l
['Std Lvl:  850hPa', ' 1503m', '  16.8C', '  15.7C', ' 205 @ 11kts']
 >>> [ re.search(r'[\d\.]+', i).group() for i in l]
['850', '1503', '16.8', '15.7', '205']
Kent
Ertl, John wrote:
All
I have a string that has a bunch of numbers with the units attached to them.
I want to strip off the units.  I am using a regular expression and sub to
do this.  This works great for almost all of the cases.  

These are the type of lines:
SigWind:  857hPa,  ,  21.0C,  20.1C, 210 @  9kts
SigWind:  850hPa±, ,   ,   , 205 @ 11kts
Std Lvl:  850hPa, 1503m,  16.8C,  15.7C, 205 @ 11kts
I am using the following cleanstring = re.compile( '(hPa|hPa\xb1|m|C|kts)'
).  And then the cleanstring.sub("",line).  I have tried using numerous \ to
escape the \xb1.
I also tried replacing all non numeric characters that are part of a
number-character string but I could not make that work. The idea was replace
all non-number characters in a "word" that is made up of numbers followed by
numbers.
I then split the line at the commas so in the current thinking I need the
commas for the split.  How do I deal with the hPa±?  When I print it out it
looks like it is a hexadecimal escape character (\xb1) but I am note sure
how to deal with this.
Any ideas?
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


[Tutor] Dates and databases, and blobs and Python.

2005-03-26 Thread Liam Clarke
Hi, 

Just a quick query. I want to store dates and work with them in my
SQLite database.
There's no specific need for any calculations to done on one side or
another (i.e. it's a single user database).

I googled how to work with dates in SQL, and I got one like this - 

SELECT * FROM totp
WHERE wk BETWEEN '1980/05/20'
  AND '1980/05/26'

as an example of finding a range for a date. I'm not too sure about
how SQL works with dates (and times for that matter), so I'm tempted
to stick to what I know and just store dates/times as strings in the
db, and then use them to create datetime objects when needed.

i.e. 

>>>cx.execute('select date, time from foo where c_id = 10')
>>>dat = cx.next()
>>>tim = cx.next()
>>>print dat, time
2005-12-31, 7:00
>>>splitD = dat.split('-')
>>>splitT = time.split(':')
>>>intD = [int(i) for item in splitD]
>>>intT = [int(i) for item in splitT]
>>> theDateTime = datetime.datetime(intD[0], intD[1], intD[2], intT[0], intT[1])

Although working with dates like that doesn't seem that flash either.

Alternatively, I was thinking of storing the actual datetime object in
the database (this is a blob I believe?), and that's a whole new
kettle of fish.

So far I've tried this -

>>> import datetime
>>> theDT = datetime.datetime(2004, 12, 31, 7, 30)
>>> print theDT
2004-12-31 07:30:00
>>> import sqlite
>>> c = sqlite.connect('foo.db')
>>> cx = c.cursor()
>>> import pickle
>>> j = pickle.dumps(theDT)
>>> cx.execute('insert into bob values(%s)', j)
>>> cx.execute('select A from bob')
>>> q = cx.next()
>>> print q
("cdatetime\ndatetime\np0\n(S'\\x07\\xd4\\x0c\\x1f\\x07\\x1e\\x00\\x00\\x00\\x00'\np1\ntp2\nRp3\n.",)
>>> w = pickle.loads(q[0])
>>> print w
2004-12-31 07:30:00

So, it works, but I'm not too sure. I tend to have a random approach
to using the standard library, as I don't fully understand what all of
the modules do.

This provokes the following questions - 

1. Should I be using StringIO for this instead?
2. Would my retrieved unpickled datetime object still work if datetime
hadn't been imported?
3. Is there a better way to work with blobs and Python?

And then there's the overall question -

What would be the least fiddly & least error prone way of working with
dates and times? Python or SQL?


Thank you for your time.


Regards, 

Liam Clarke





-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor