Re: [Tutor] dynamic attribute assignment
"John" <[EMAIL PROTECTED]> wrote vars.var3 > ['a','b','c'] > > This is great, I had no idea! However, is there then a way to > reassign or > update the values? For instance, can you say: > vars.var1='some new text' Why not try it at the >>> prompt? Thats the great thing about python, its very easy to try things like that out and get a definitive answer! (Even faster than posting to a mailing list! :-) In this case its a yes... -- 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] Problem with while loop
Perfect... Thanks very much for your help... On 9/7/07, Michael Connors <[EMAIL PROTECTED]> wrote: > > I think it will work if you cast your input to an int: > howmany = int(raw_input( "How many: " )) Now I just need to figure out how to only get 4 digit pin numbers :) -m ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Problem with while loop
Hi guys Please excuse me but I've been out of Python for a while since my laptop was stolen... I'm battling with a very basic while loop -- 8< -- #!/usr/bin/python from random import randint counter = 0 howmany = raw_input( "How many: " ) while counter < howmany: pin = randint(,) print pin counter += 1 -- 8< -- For some reason if I use an integer in place of "howmany" it works, but I'd like it to work as I can logically see it above. What am I missing ? -m ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with while loop
You could use string formatting to output all pin numbers as 4 character strings. http://docs.python.org/lib/typesseq-strings.html On 07/09/2007, matte <[EMAIL PROTECTED]> wrote: > > Perfect... > > Thanks very much for your help... > > On 9/7/07, Michael Connors <[EMAIL PROTECTED]> wrote: > > > > I think it will work if you cast your input to an int: > > howmany = int(raw_input( "How many: " )) > > > Now I just need to figure out how to only get 4 digit pin numbers :) > > -m > -- Michael Connors ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Multi-threading help
Hi there, I'm working on a multi-threaded utility to monitor network connections between three machines The configuration of the three machines are: a web machine that feeds back to two machines for processing. Sometimes when the web connection is closed, the corresponding processes on the two backend machines don't close. How, I have the current set up is: class web: def __init__(self): def run(self,args): class mach1: def __init__(self): def run(self,args): class mach2: def __init__(self): def run(self,args): class target: def run(self,args): ... collect data from web, mach1, mach2 classes, monitor connections, yada yada yada My question is how to get the information to the target class. The target class must have the web, mach1, mach2 data sets before doing any sort of matching up of information. Now the ways that I have thought of are: -Directing sending the information to the target object (there will only be one) -Using the Observer pattern to send the information from the web, mach1, mach2 classes to the target object And other question that I do have are: -Is there a way to "block" run until I get information from the three other objects. Presently, I can only think of looping and polling to see if the information has come in yet. I'm sure there is a way because I have seen it used in the SocketHandler class. -Is there any way that I can be sure that two different objects are not writing to target at the same time? The present way that I'm thinking about is using locking. Thanks Tino ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python / CGI
Wow, thanks for all the info guys, more than i was expecting :). After looking around at various options, I did download the python gdata module for google's calendar. What I'm thinking is, since my fiance has two schools, she can just log into her google account and create a calendar for each school. I'm going to try to make a custom cgi front-end that will pull the data, and offer 'slots' to sign up for. When you sign up for a slot, you have to use a username for the front-end, I'm thinking I can store them in a db. That way I don't have to worry about everyone needing google calendars to be able to access hers. So when someone picks a slot and submits it, the program behind the scenes will actually just use her google account to submit, and append the username to the entry, so you can tell who did what, but it still will only require one real google account. Gdata comes with some really nice samples, one that basically does any of the calendar functions i'm going to need, so I think I should bea ble to rip the code out of that module to use, so I still get to learn the cgi doing my front-end, but that way I know I have a well functioning back-end to easy some of my troubles. http://code.google.com/apis/calendar/developers_guide_python.html Here is a bit of the output from their 'sample' python script that comes with the package. C:\Python25\gdata.py-1.0.8\samples\calendar>python calendarExample.py --user [EMAIL PROTECTED] --pw xxx Printing allcalendars: Lee McClintock's Calendar List 0. Lee McClintock Printing owncalendars: Lee McClintock's Calendar List 0. Lee McClintock Events on Primary Calendar: Lee McClintock 0. Rent 0. [EMAIL PROTECTED] Lee McClintock None Full text query for events on Primary Calendar: 'Tennis' Date range query for events on Primary Calendar: 2007-01-01 to 2007-07-01 0. Rent Start time: 2007-03-01 End time: 2007-03-02 Start time: 2007-06-01 End time: 2007-06-02 Start time: 2007-01-01 End time: 2007-01-02 Start time: 2007-05-01 End time: 2007-05-02 Start time: 2007-04-01 End time: 2007-04-02 Start time: 2007-02-01 End time: 2007-02-02 New single event inserted: http://www.google.com/calendar/feeds/default/private/full/cmvade9oqrd95oeaaf9b7ha5cc Event edit URL: http://www.google.com/calendar/feeds/default/private/full/cmvade9oqrd95oeaaf9b7ha5cc/63324787637 Event HTML URL: http://www.google.com/calendar/event?eid=Y212YWRlOW9xcmQ5NW9lYWFmOWI3aGE1Y2MgZml5YXdlcnhAbQ Updating title of event from:'One-time Tennis with Beth' to:'New title for single event' Adding 30 minute reminder to event Adding extended property to event: 'propname'='propvalue' New recurring event inserted: http://www.google.com/calendar/feeds/default/private/full/dpjp06smht75o466g8s0gfdnro Event edit URL: http://www.google.com/calendar/feeds/default/private/full/dpjp06smht75o466g8s0gfdnro/63324787638 Event HTML URL: http://www.google.com/calendar/event?eid=ZHBqcDA2c21odDc1bzQ2Nmc4czBnZmRucm9fMjAwNzA1MDEgZml5YXdlcnhAbQ Inserting Simple Web Content Event Inserting Web Content Gadget Event Lee McClintock's access control list 0. owner Role: http://schemas.google.com/gCal/2005#owner Scope user - [EMAIL PROTECTED] 1. editor Role: http://schemas.google.com/gCal/2005#editor Scope user - [EMAIL PROTECTED] freebusy Role: http://schemas.google.com/gCal/2005#freebusy Scope user - [EMAIL PROTECTED] Creating new calendar with title "Little League Schedule" Updating the calendar titled "Little League Schedule" with the title "New Title" Subscribing to the calendar with ID: c4o4i7m2lbamc4k26sc2vokh5g%40group.calendar.google.com Updating the calendar subscription with ID: c4o4i7m2lbamc4k26sc2vokh5g%40group.calendar.google.com -- So it seems it already has samples of anything I'm going to need to do. (Got a message that the mail bounced due to being too learge, cut out the quoted section) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Jason Timms is out of the office.
I will be out of the office starting 09/07/2007 and will not return until 09/09/2007. I will respond to your message when I return. ** Privileged and/or confidential information may be contained in this message. If you are not the addressee indicated in this message (or are not responsible for delivery of this message to that person) , you may not copy or deliver this message to anyone. In such case, you should destroy this message and notify the sender by reply e-mail. If you or your employer do not consent to Internet e-mail for messages of this kind, please advise the sender. Shaw Industries does not provide or endorse any opinions, conclusions or other information in this message that do not relate to the official business of the company or its subsidiaries. ** ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python / CGI
John wrote: > Steve, > > If you're interested in just banging out a Python app, though, my > experience > was writing a calendaring tool for a group of my friends who get > together > [...] > This sounds very cool, is it something you could post? Okay. It's not the greatest implementation (as I said it was kind of a quick hack) but here it is... A few notes to help understand the app: It's a single stand-alone CGI script in Python, and serves the three calendar views (scheduled games, the form for players to vote on good dates, and a form for the admin to decide on the game dates based on the votes) within this one script. It stores the data for the calendar in a MySQL database. The users need to authenticate to the web server in order to get to the page, so we can look in the script's environment to see who they are (so we know who voted for what days, and who gets the admin access page). The admin (and only the admin) can add a "?for=username" to the URL to access the application as if they were one of the players (in case they can't get in to the calendar but wish to change their vote data). #!/usr/bin/python # # Display a calendar of our game days, allow "voting" for what days any person # is available or not. # import cgitb; cgitb.enable() import os import sys import calendar import datetime import cgi import MySQLdb monthnames = ('January', 'February', 'March', 'April', 'May', 'June', 'July','August', 'September', 'October', 'November', 'December') daynames = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat') calendar.setfirstweekday(6) # start weeks on Sunday currentdaybg = "#33" votecolors = ( "#ff", "#00ff00", "#00" ) form = cgi.FieldStorage() today = datetime.date.today() print "Content-Type: text/html\n" print '' if 'year' in form: year = int(form.getfirst('year')) if not 2000 < year < 2020: print "Sorry, we're not supporting that year right now." sys.exit(0) else: year = today.year print """ .popup { COLOR: #ff; CURSOR: help; TEXT-DECORATION: none; } """ if 'REMOTE_USER' not in os.environ: print "Sorry, this page cannot be used without authentication." sys.exit(0) me = os.environ['REMOTE_USER'] admin = (me == 'login-name-of-admin-1' or me == 'login-name-of-admin-2') #admin = False mode = 'normal' forWhom = None if admin and 'for' in form: me = form.getfirst('for') forWhom=me print "***",me,"***" if 'mode' in form: mode = form.getfirst('mode') if mode == 'admin' and not admin: print "Sorry, restricted access to this application is denied." sys.exit(0) if mode == 'admin' or mode == 'vote': print '' if mode == 'vote': print ''' Voting For Game Dates Generally, we play on Saturdays. If there's a special date we are considering playing on, such as a holiday, indicate your preference for that, but otherwise we're looking just for Saturdays. Indicate your preferences by selecting the appropriate response under each date: ---: I am available, most likely, and willing to play that date. Best: This date is particularly good for me. Please play on that date if possible. NO: This date is not good, I most likely will not be able to play. The default is "available", so if you're ok with playing any weekend in a month, just leave them all as "---". Don't mark them all as "best". The "best" setting is provided so that you can indicate unusually favorable dates, like when your SO is away or something. When finished, click the "Save" button at the bottom. You'll be brought back to this form again in case you want to make more changes. Click the "Return to Calendar View" link (or navigate away from this page) when you're done and have saved all the changes you want to keep. ''' # # Connect to database # db = MySQLdb.connect(host='HHH', user='XXX', passwd='YYY', db='ZZZ') # # Accept updates from user # votelist = None if mode == 'vote' or mode == 'admin': for key in form: if len(key) == 4: try: vmonth = int(key[0:2]) vdate = int(key[2:4]) vv= form.getfirst(key) except: # must not be the four-digit key we're looking for continue if 1 <= vmonth <= 12 and 1 <= vdate <= 31: if votelist is None: votelist = [] if mode == 'vote': if vv == '-': pass# just let the old vote (if any) die elif vv == 'Y': votelist.append((datetime.date(year,vmonth
[Tutor] (no subject)
using the built in function "open" how do you specify non local files such as the C: drive without copying the program there thanks___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] manually sorting variables
"Christopher Spears" <[EMAIL PROTECTED]> wrote > Have the user enter three numeric values and store > them in three different variables. Without using > lists or sorting algorithms, manually sort these three > numbers from smallest to largest. Hmm, the restriction of not using a sorting algorithm is interesting. But if you only have three values its doable. With more than three I'm not sure how it would be done. > def smallest_var(x,y): So you can get the smallest var. Now if you could get the biggest then the one that's neither must be in the middle. You are then left with selecting one of 6 possible outcomes: 1,2,3, 1,3,2 2,1,3 2.3.1, 3,1,2 3,2,1 Does that help? Alternatively you could write a comparison function rather than a get smallest/get biggest. That way you can compare the original variables rather than creating new ones. > var1 = raw_input("Enter a number for var1: ") > var2 = raw_input("Enter a number for var2: ") > var3 = raw_input("Enter a number for var3: ") You should convert these to integers! > small = smallest_var(var1, var2) > #print small_var_1 > smallest = smallest_var(small, var3) > print smallest 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] Problem with while loop
I think it will work if you cast your input to an int: howmany = int(raw_input( "How many: " )) On 07/09/2007, matte <[EMAIL PROTECTED]> wrote: > > Hi guys > > Please excuse me but I've been out of Python for a while since my laptop > was stolen... > > I'm battling with a very basic while loop > > -- 8< -- > #!/usr/bin/python > > from random import randint > > counter = 0 > > howmany = raw_input( "How many: " ) > > while counter < howmany: > > pin = randint(,) > print pin > counter += 1 > > -- 8< -- > > For some reason if I use an integer in place of "howmany" it works, but > I'd > like it to work as I can logically see it above. > > What am I missing ? > > -m > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > -- Michael Connors ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
"Lawrence Barrott" <[EMAIL PROTECTED]> wrote > using the built in function "open" how do you specify non local > files such as the C: drive without copying the program there Just provide the full path to the file. f = open(r"C:\TEMP\somefile.txt") Note the use of raw to avoid problems with the DOS backslash character. Another option is to use forward slashes which python accepts too: f = open("C:/TEMP/somefile.txt") -- 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] Fun with Cookies
Hi all, I am trying to create a cookie and send it a long with a request. I searched the archives and found this code from Kent Johnson: import cookielib, urllib2 cj = cookielib.CookieJar() cookie = cookielib.Cookie(...your cookie data here...) cj.set_cookie(cookie) opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) urllib2.install_opener(opener) data = urllib2.urlopen(...).read() It seemed to solve a problem I was having with SimpleCookie(), but I cannot figure out what I should put where Kent has written "...your cookie data here...". I have tried strings, SimpleCookie instances, etc. and I always get this error on that line: __init__() takes at least 17 arguments (2 given) Thanks for your time. Alex Ezell ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fun with Cookies
Alex Ezell wrote: > Hi all, > I am trying to create a cookie and send it a long with a request. > > I searched the archives and found this code from Kent Johnson: > > import cookielib, urllib2 > > cj = cookielib.CookieJar() > cookie = cookielib.Cookie(...your cookie data here...) > cj.set_cookie(cookie) > > opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) > urllib2.install_opener(opener) > > data = urllib2.urlopen(...).read() > > It seemed to solve a problem I was having with SimpleCookie(), but I > cannot figure out what I should put where Kent has written "...your > cookie data here...". I have tried strings, SimpleCookie instances, > etc. and I always get this error on that line: > > __init__() takes at least 17 arguments (2 given) The Cookie constructor is def __init__(self, version, name, value, port, port_specified, domain, domain_specified, domain_initial_dot, path, path_specified, secure, expires, discard, comment, comment_url, rest, rfc2109=False, ) You should specify at least name, value, domain and path (all strings). The rest can be None. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fun with Cookies
Kent, Thanks so much. I will give that a try. Your name is all over these kinds of questions on the web. I guess you fought through it a while back? Here's where I show off my Python newb status. What's the best way to specify those attributes? If I only include the 4 you mention (name, value, domain and path), it seems messy to have a bunch of "None"s in there. Thanks again, Alex On 9/7/07, Kent Johnson <[EMAIL PROTECTED]> wrote: > Alex Ezell wrote: > > Hi all, > > I am trying to create a cookie and send it a long with a request. > > > > I searched the archives and found this code from Kent Johnson: > > > > import cookielib, urllib2 > > > > cj = cookielib.CookieJar() > > cookie = cookielib.Cookie(...your cookie data here...) > > cj.set_cookie(cookie) > > > > opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) > > urllib2.install_opener(opener) > > > > data = urllib2.urlopen(...).read() > > > > It seemed to solve a problem I was having with SimpleCookie(), but I > > cannot figure out what I should put where Kent has written "...your > > cookie data here...". I have tried strings, SimpleCookie instances, > > etc. and I always get this error on that line: > > > > __init__() takes at least 17 arguments (2 given) > > The Cookie constructor is > > def __init__(self, version, name, value, > port, port_specified, > domain, domain_specified, domain_initial_dot, > path, path_specified, > secure, > expires, > discard, > comment, > comment_url, > rest, > rfc2109=False, > ) > > You should specify at least name, value, domain and path (all strings). > The rest can be None. > > Kent > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fun with Cookies
Alex Ezell wrote: > Kent, > Thanks so much. I will give that a try. Your name is all over these > kinds of questions on the web. I guess you fought through it a while > back? I figured it out for part of the Python Challenge :-) http://www.pythonchallenge.com/ Don't know how it got all over the web though...it's not the first time it has come up here... > Here's where I show off my Python newb status. What's the best way to > specify those attributes? If I only include the 4 you mention (name, > value, domain and path), it seems messy to have a bunch of "None"s in > there. You have to include the None's. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with while loop
matte wrote: > Hi guys > > Please excuse me but I've been out of Python for a while since my laptop > was stolen... > > I'm battling with a very basic while loop > > -- 8< -- > #!/usr/bin/python > > from random import randint > > counter = 0 > > howmany = raw_input( "How many: " ) > > while counter < howmany: > > pin = randint(,) > print pin > counter += 1 > > -- 8< -- > > For some reason if I use an integer in place of "howmany" it works, but I'd > like it to work as I can logically see it above. > > What am I missing ? > raw_input returns a string. Have to convert it. BTW, instead of the while loop you might try this : for i in xrange(int(howmany)) : print randint(, ) saves you some code and a few variables. Or if what you want is a list for further processing : randomInts = [randint(, ) for i in xrange(int(howmany))] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] WxPython Splashscreen?
Hey all, Does anyone know how to make a wxPython splashscreen? It would be great if you could show a (working) example, as I have googled this topic, yet have not found any working examples. Thanks. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] rewriting script
I have written a script that reads and displays text files: #!/usr/bin/env python 'readTextFile.py -- read and display text file' import os # get filename while True: fname = raw_input('Enter file name: ') print if os.path.exists(fname): fobj = open(fname, 'r') for eachLine in fobj: print eachLine, fobj.close() else: print"*** File doesn't exist" break However, whenever I run the script, I get this result: [EMAIL PROTECTED] ./chap3 314> python readTextFile1.py Enter file name: datefile Fri Sep 7 22:13:03 PDT 2007 Traceback (most recent call last): File "readTextFile1.py", line 13, in ? for eachLine in fobj: ValueError: I/O operation on closed file The original script appeared in 'Core Python Programming' in this form: #!/usr/bin/env python 'readTextFile.py -- read and display text file' # get filename fname = raw_input('Enter file name: ') print # attempt to open file for reading try: fobj = open(fname, 'r') except IOError, e: print"*** file open error:", e else: # display contents to the screen for eachLine in fobj: print eachLine, fobj.close() I modified the script as an answer to one of the end of chapter questions. Basically, I am supposed to rewrite the script, so that runs with 'try' and 'except'. What is causing the error message? I thought the file was opened earlier in the script. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python scripting
I am on a linux machine and I was wondering about python scripting. Mainly to get it up and running but also what can I do with it etc. Any help would be appreciated, Thnx ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] replacing while loop
I've been reading 'Core Python Programming (2nd Edition)'. I have been given the following script: #!/usr/bin/env python 'makeTextFile.py -- create text file' import os # get filename while True: fname = raw_input('Enter file name: ') if os.path.exists(fname): print"*** ERROR: '%s' already exists" % fname else: break # get file content (text) lines all = [] print "\nEnter lines ('.' by itself to quit).\n" # loop until user terminates input while True: entry = raw_input('> ') if entry == '.': break else: all.append(entry) # write lines to file with NEWLINE line terminator fobj = open(fname, 'w') fobj.write('\n'.join(all)) fobj.close() print 'DONE!' I have been asked to replace this while loop with a try and except clause: while True: fname = raw_input('Enter file name: ') if os.path.exists(fname): print"*** ERROR: '%s' already exists" % fname else: break I'm not sure how to do this. I looked at the back of the book, and I don't see an exception that is raised when a previously existing file is found. Any hints? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rewriting script
Christopher Spears wrote: > I have written a script that reads and displays text > files: > > #!/usr/bin/env python > > 'readTextFile.py -- read and display text file' > > import os > > # get filename > while True: > fname = raw_input('Enter file name: ') > print > if os.path.exists(fname): > fobj = open(fname, 'r') > for eachLine in fobj: > print eachLine, > fobj.close() You're closing the file after reading the first line. You want that fobj.close() line to be outside the for loop (outdent one level) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Undo/Redo in wxpython?
What is the proper way to undo/redo changes in a text box? I read somewhere that the default undo depth is 1. How could I change this to, say, about 35? Here's a snippet of my code- #!/usr/bin/python import wx import os ... ... ... class MainWin(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(850, 450)) ... ... ... #Program body section start self.code = wx.TextCtrl(self, ID_TEXTBOX, size=(200, 130), style=wx.TE_MULTILINE) ... ... ... app = MyApp(redirect=True, filename = "_error.log") app.MainLoop() Now, how would it be possible for me to do undo/redo for the "self.code" TextCtrl widget? Help is greatly appreciated. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor