Re: [Tutor] system()? popen2()? How to execute a command & save its output?
You might also consider pexpect. http://pexpect.sourceforge.net/ It's designed for interactive console applications like ftp. For popen() style access, the recommended approach is the subprocess module. You should be able to find an example in the docs to fit your application. http://docs.python.org/library/subprocess.html Cheers On Wednesday 29 September 2010, James Hartley wrote: > I'm needing to transfer the following shell construct to Python, plus save > the output of execution: > > FTP_SITE='ftp.somesite.com' > ftp -a $FTP_SITE < binary > prompt off > cd /some_dir > dir > bye > EOF > > Here, the FTP client accepts commands from STDIN, so all commands are saved > in a temporary file which is redirected to the client application. > > I also need to save whatever output is generated. How can this be done in > Python? > > Thanks. > > Jim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] system()? popen2()? How to execute a command & save its output?
command_name = 'ps -ax|grep sometext >>/tmp/output.txt'f = os.popen('%s' %command_name) fp = open('/tmp/output.txt') print fp.readlines() With Regards Vijay --- On Thu, 30/9/10, James Hartley wrote: From: James Hartley Subject: [Tutor] system()? popen2()? How to execute a command & save its output? To: tutor@python.org Date: Thursday, 30 September, 2010, 9:59 AM I'm needing to transfer the following shell construct to Python, plus save the output of execution: FTP_SITE='ftp.somesite.com' ftp -a $FTP_SITE
Re: [Tutor] generating independent random numbers
On 2:59 PM, Dave Angel wrote: On 9/29/2010 9:17 PM, Carter Danforth wrote: On Wed, Sep 29, 2010 at 1:53 PM, Dave Angel wrote: On 9/28/2010 5:11 PM, Carter Danforth wrote: Thanks for the replies, Dave and Joel. The reason I'm not just using the time or datetime modules for a random date is because it's restricted to 1970-2038; I'm pulling dates from 1600-3099. Thanks a lot for the pointer about the leap years, Dave, as well the class instances; just updated it and it's all working now, and also included the rest of the code too w/ answer verification and time tracking. I want to start using this program to test myself for speed calculation using Zeller's formula, it's pretty cool for determining the days of dates - http://mathforum.org/dr/math/faq/faq.calendar.html Because of the way variables C and D are split up from the year in the formula, I split up the year for self.c and self.y. import random, time, datetime, calendar class Date: def __init__(self): self.c = random.randint(16,30) self.y = random.randint(0,99) self.month = random.randint(1,12) self.year = self.c*100 + self.y apr = [4,6,9,11] feb = [2] notleap = [1700, 1800, 1900, 3000] if self.month in feb: if self.year%4 == 0: if self.year in notleap: self.k = random.randint(1,28) else: self.k = random.randint(1,29) else: self.k = random.randint(1,28) elif self.month in apr: self.k = random.randint(1,30) else: self.k = random.randint(1,31) if self.month in [1,2]: d = self.y - 1 m = self.month + 10 else: d = self.y m = self.month - 2 z = self.k + (13*m-1)/5 + d + d/4 + self.c/4 - 2*self.c if z< 0: r = (abs(z)/7)*7 + z + 7 else: r = z%7 dict = { 0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday' } self.day = dict[r] t1m = time.localtime().tm_min t1s = time.localtime().tm_sec t1 = t1m + t1s/100.0 n = 0 x = 0 while n< 10: newdate = Date() print '\n',calendar.month_name[newdate.month], newdate.k,',', newdate.year,'=', answer = raw_input() if answer.capitalize() == newdate.day: pass else: x += 1 n += 1 t2m = time.localtime().tm_min t2s = time.localtime().tm_sec t2 = t2m + t2s/100.0 td = t2 - t1 print '\n',x,'out of 10 wrong\nAvg time/question:',td/10,'\nTotal time:',td You top-posted again. Put your comments after the part you're quoting, not before. You still have a problem in the code, and I still think it's in the 2*self.c, though I don't have time to debug it. Look up the day for 1/1/2099, and for 1/1/2100 and it comes out the same. That's not correct. No adjacent years start on the same day, it's always either one day or two. You have too much in one function (method), which makes it hard to debug it. Factor it into separate functions, and then test each independently. And using k for day and d for year make no sense to me, though perhaps it does in some other language. DaveA Hey Dave, you probably left c and y alone when comparing the years. If the date's 1/1/2099, then self.c = 20 and self.y=99. If you try doing it again while changing those values, for 1/1/2099, the day comes out to be Thursday, and for 1/1/2100 you'll get Wednesday. Glad you pointed out the 2100 date though, there actually was a problem in it, but it's not the 2*self.c; I had to account for d = y - 1 when y = 00 (zeller subtracts months by 2, so it needs to be the previous yr for jan/feb). Below is the updated code, I put in a few comments to make it read easier. -- import random, time, datetime, calendar class Date: def __init__(self): self.c = random.randint(16,30) # first two digits in a year self.y = random.randint(0,99) # last two digits in a year self.month = random.randint(1,12) self.year = self.c*100 + self.y apr = [4,6,9,11] feb = [2] notleap = [1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600, 2700, 2900, 3000] if self.month in feb: # assigns days, given the month if self.year%4 == 0: if self.year in notleap: self.k = random.randint(1,28) else: self.k = random.randint(1,29) else: self.k = random.randint(1,28) elif self.month in apr: self.k = random.randint(1,30) else: self.k = random.randint(1,31) if self.month in [1,2]:# months in zeller's rule are subtracted by 2 if self.y == 0:# ne
[Tutor] system()? popen2()? How to execute a command & save its output?
I'm needing to transfer the following shell construct to Python, plus save the output of execution: FTP_SITE='ftp.somesite.com' ftp -a $FTP_SITE <___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] generating independent random numbers
On 9/29/2010 9:17 PM, Carter Danforth wrote: On Wed, Sep 29, 2010 at 1:53 PM, Dave Angel wrote: On 9/28/2010 5:11 PM, Carter Danforth wrote: Thanks for the replies, Dave and Joel. The reason I'm not just using the time or datetime modules for a random date is because it's restricted to 1970-2038; I'm pulling dates from 1600-3099. Thanks a lot for the pointer about the leap years, Dave, as well the class instances; just updated it and it's all working now, and also included the rest of the code too w/ answer verification and time tracking. I want to start using this program to test myself for speed calculation using Zeller's formula, it's pretty cool for determining the days of dates - http://mathforum.org/dr/math/faq/faq.calendar.html Because of the way variables C and D are split up from the year in the formula, I split up the year for self.c and self.y. import random, time, datetime, calendar class Date: def __init__(self): self.c = random.randint(16,30) self.y = random.randint(0,99) self.month = random.randint(1,12) self.year = self.c*100 + self.y apr = [4,6,9,11] feb = [2] notleap = [1700, 1800, 1900, 3000] if self.month in feb: if self.year%4 == 0: if self.year in notleap: self.k = random.randint(1,28) else: self.k = random.randint(1,29) else: self.k = random.randint(1,28) elif self.month in apr: self.k = random.randint(1,30) else: self.k = random.randint(1,31) if self.month in [1,2]: d = self.y - 1 m = self.month + 10 else: d = self.y m = self.month - 2 z = self.k + (13*m-1)/5 + d + d/4 + self.c/4 - 2*self.c if z< 0: r = (abs(z)/7)*7 + z + 7 else: r = z%7 dict = { 0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday' } self.day = dict[r] t1m = time.localtime().tm_min t1s = time.localtime().tm_sec t1 = t1m + t1s/100.0 n = 0 x = 0 while n< 10: newdate = Date() print '\n',calendar.month_name[newdate.month], newdate.k,',', newdate.year,'=', answer = raw_input() if answer.capitalize() == newdate.day: pass else: x += 1 n += 1 t2m = time.localtime().tm_min t2s = time.localtime().tm_sec t2 = t2m + t2s/100.0 td = t2 - t1 print '\n',x,'out of 10 wrong\nAvg time/question:',td/10,'\nTotal time:',td You top-posted again. Put your comments after the part you're quoting, not before. You still have a problem in the code, and I still think it's in the 2*self.c, though I don't have time to debug it. Look up the day for 1/1/2099, and for 1/1/2100 and it comes out the same. That's not correct. No adjacent years start on the same day, it's always either one day or two. You have too much in one function (method), which makes it hard to debug it. Factor it into separate functions, and then test each independently. And using k for day and d for year make no sense to me, though perhaps it does in some other language. DaveA Hey Dave, you probably left c and y alone when comparing the years. If the date's 1/1/2099, then self.c = 20 and self.y=99. If you try doing it again while changing those values, for 1/1/2099, the day comes out to be Thursday, and for 1/1/2100 you'll get Wednesday. Glad you pointed out the 2100 date though, there actually was a problem in it, but it's not the 2*self.c; I had to account for d = y - 1 when y = 00 (zeller subtracts months by 2, so it needs to be the previous yr for jan/feb). Below is the updated code, I put in a few comments to make it read easier. -- import random, time, datetime, calendar class Date: def __init__(self): self.c = random.randint(16,30) # first two digits in a year self.y = random.randint(0,99) # last two digits in a year self.month = random.randint(1,12) self.year = self.c*100 + self.y apr = [4,6,9,11] feb = [2] notleap = [1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600, 2700, 2900, 3000] if self.month in feb: # assigns days, given the month if self.year%4 == 0: if self.year in notleap: self.k = random.randint(1,28) else: self.k = random.randint(1,29) else: self.k = random.randint(1,28) elif self.month in apr: self.k = random.randint(1,30) else: self.k = random.randint(1,31) if self.month in [1,2]:# months in zeller's rule are subtracted by 2 if self.y == 0:# need to acct for jan/feb year change d = 99
Re: [Tutor] generating independent random numbers
On Wed, Sep 29, 2010 at 1:53 PM, Dave Angel wrote: > On 9/28/2010 5:11 PM, Carter Danforth wrote: > >> Thanks for the replies, Dave and Joel. The reason I'm not just using the >> time or datetime modules for a random date is because it's restricted to >> 1970-2038; I'm pulling dates from 1600-3099. Thanks a lot for the pointer >> about the leap years, Dave, as well the class instances; just updated it >> and >> it's all working now, and also included the rest of the code too w/ answer >> verification and time tracking. >> >> I want to start using this program to test myself for speed calculation >> using Zeller's formula, it's pretty cool for determining the days of dates >> - >> http://mathforum.org/dr/math/faq/faq.calendar.html >> >> Because of the way variables C and D are split up from the year in the >> formula, I split up the year for self.c and self.y. >> >> >> >> import random, time, datetime, calendar >> >> class Date: >> def __init__(self): >> self.c = random.randint(16,30) >> self.y = random.randint(0,99) >> self.month = random.randint(1,12) >> self.year = self.c*100 + self.y >> >> apr = [4,6,9,11] >> feb = [2] >> notleap = [1700, 1800, 1900, 3000] >> >> if self.month in feb: >> if self.year%4 == 0: >> if self.year in notleap: >> self.k = random.randint(1,28) >> else: >> self.k = random.randint(1,29) >> else: >> self.k = random.randint(1,28) >> elif self.month in apr: >> self.k = random.randint(1,30) >> else: >> self.k = random.randint(1,31) >> >> if self.month in [1,2]: >> d = self.y - 1 >> m = self.month + 10 >> else: >> d = self.y >> m = self.month - 2 >> >> z = self.k + (13*m-1)/5 + d + d/4 + self.c/4 - 2*self.c >> >> if z< 0: >> r = (abs(z)/7)*7 + z + 7 >> else: >> r = z%7 >> >> dict = { 0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', >> 4: >> 'Thursday', 5: 'Friday', 6: 'Saturday' } >> self.day = dict[r] >> >> t1m = time.localtime().tm_min >> t1s = time.localtime().tm_sec >> t1 = t1m + t1s/100.0 >> n = 0 >> x = 0 >> >> while n< 10: >> newdate = Date() >> >> print '\n',calendar.month_name[newdate.month], newdate.k,',', >> newdate.year,'=', >> answer = raw_input() >> if answer.capitalize() == newdate.day: >> pass >> else: >> x += 1 >> n += 1 >> >> t2m = time.localtime().tm_min >> t2s = time.localtime().tm_sec >> t2 = t2m + t2s/100.0 >> td = t2 - t1 >> >> print '\n',x,'out of 10 wrong\nAvg time/question:',td/10,'\nTotal >> time:',td >> >> >> >> You top-posted again. Put your comments after the part you're quoting, > not before. > > You still have a problem in the code, and I still think it's in the > 2*self.c, though I don't have time to debug it. > > Look up the day for 1/1/2099, and for 1/1/2100 and it comes out the same. > That's not correct. No adjacent years start on the same day, it's always > either one day or two. > > You have too much in one function (method), which makes it hard to debug > it. Factor it into separate functions, and then test each independently. > And using k for day and d for year make no sense to me, though perhaps it > does in some other language. > > DaveA > > Hey Dave, you probably left c and y alone when comparing the years. If the date's 1/1/2099, then self.c = 20 and self.y=99. If you try doing it again while changing those values, for 1/1/2099, the day comes out to be Thursday, and for 1/1/2100 you'll get Wednesday. Glad you pointed out the 2100 date though, there actually was a problem in it, but it's not the 2*self.c; I had to account for d = y - 1 when y = 00 (zeller subtracts months by 2, so it needs to be the previous yr for jan/feb). Below is the updated code, I put in a few comments to make it read easier. -- import random, time, datetime, calendar class Date: def __init__(self): self.c = random.randint(16,30) # first two digits in a year self.y = random.randint(0,99) # last two digits in a year self.month = random.randint(1,12) self.year = self.c*100 + self.y apr = [4,6,9,11] feb = [2] notleap = [1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600, 2700, 2900, 3000] if self.month in feb: # assigns days, given the month if self.year%4 == 0: if self.year in notleap: self.k = random.randint(1,28) else: self.k = random.randint(1,29) else: self.k = random.randint(1,28) elif self.month in apr: self.k = random.randint(1,30) else: self.k = random.randint(1,31) if self.month
Re: [Tutor] Runnig a windows.exe from python
"Susana Iraiis Delgado Rodriguez" wrote I'm working in a simple python module to run a external command, this command is named "ogr2ogr.exe" . When I execute my python script : *import os def call(): os.system(' "C:\\Archivos de programa\\FWTools2.4.7\\bin\\ogr2ogr.exe" ')* * raw_input()* *call()* It runs, but if I want to enter arguments: *ogr2ogr* to test my .exe, it shows me an error *"ogr2ogr is not define", *any suggestion? You need to read the arfguments before calling the exe. Then create the full command string and pass that tro os.system. However os.system is a blunt instrument, you will find you have much more control using the subprocess module which supercedes os.system and various others ways of executing external programs. It take a wee bit more effort but the extra control is worth it. The documentation has numerous examples and the "Using the OS" topic of my tutorial has some info too. HTH -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] function error
"roberto" wrote Perhaps if you provide the full traceback from the error here it is: TypeError Traceback (most recent call last) ~/randomMove2.py in () > 1 2 3 4 5 ... ~/checkForward.py in out_of_bounds() 19 20 def outOfBounds(): ---> 21 if (abs(turtle.position()[0]) > turtle.window_height()/2) or (abs(turtle.position()[1]) > turtle.window_width()/2): 22 return "true" 23 else: TypeError: 'function' object is unsubscriptable OK, Thats a non standard error trace so presumably you are running it inside an IDE of some kind? It may be the IDE is masking the true error. What happens if you just execute the code from a shell prompt in a console window? Can you send the error trace from that? HTH -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Runnig a windows.exe from python
On Thu, 30 Sep 2010 05:49:53 am Susana Iraiis Delgado Rodriguez wrote: > Hello everyone: > > I'm working in a simple python module to run a external command, this > command is named "ogr2ogr.exe" . When I execute my python script : > > *import os > def call(): > os.system(' "C:\\Archivos de > programa\\FWTools2.4.7\\bin\\ogr2ogr.exe" ')* > * raw_input()* > *call()* > It runs, but if I want to enter arguments: *ogr2ogr* to test my > .exe, it shows me an error *"ogr2ogr is not define", *any suggestion? Please don't add extra "bits" around the code you use -- there's no need to add asterisks * around the code. Just copy and paste it it into the email. Can you please copy and paste the exact error message you get? Include the full traceback, not just the last line. Also, it is a little-known thing that Windows accepts forward slashes as well as back-slashes in pathnames. So it is easier to write: "C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr.exe" than: "C:\\Archivos de programa\\FWTools2.4.7\\bin\\ogr2ogr.exe" -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] function error
>> Perhaps if you provide the full traceback from the error (assuming you're >> still getting this >error); tracebacks generally show the offending code as >> well. It may be something that's >simply overlooked but shows in the >> traceback. >> >> > > here it is: > > TypeError Traceback (most recent call last) > > ~/randomMove2.py in () > > 1 > 2 > 3 > 4 > 5 > > ~/randomMove2.py in randomMove2(d1, d2, a1, a2) > 6 while 1: > 7 turtle.left(random.uniform(a1,a2)) > > 8 checkForward.checkForward(random.uniform(d1,d2)) > 9 if forward_failed == 'true': > 10 turtle.right(180) > > ~/checkForward.py in checkForward(distance) > 8 > 9 turtle.forward(distance) > ---> 10 forward_failed = outOfBounds() > 11 turtle.setx(old_position[0]); turtle.sety(old_position[1]) > 12 turtle._pen.down() > > ~/checkForward.py in out_of_bounds() > 19 > 20 def outOfBounds(): > ---> 21 if (abs(turtle.position()[0]) > > turtle.window_height()/2) or (abs(turtle.position()[1]) > > turtle.window_width()/2): > 22 return "true" > 23 else: > > TypeError: 'function' object is unsubscriptable The only thing that strikes me as a bit odd, is that the last bit if given as '~/checkForward.py in out_of_bounds()', while the actual function seems to be called outOfBounds(). I don't know this traceback (ie, I can't tell what Python environment/editor you're using), but that could suggest the byte-compiled code you are running doesn't match with the actual current code. So you may have changed the function name in the meantime, *as well as* fixed a parenthesis in the process. But perhaps you haven't then recompiled your current code, and for some reason your Python executable hasn't picked up the most recent source code and recompiled that for you. If this is the case, it may be dependent on your development environment that does this (somewhat incorrectly, assuming you have at least saved the source file), but as I mentioned, I have no idea what you're using. Then again, this is still a guess. But it's the only thing I can see that stands out. Cheers, Evert ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Runnig a windows.exe from python
Hello everyone: I'm working in a simple python module to run a external command, this command is named "ogr2ogr.exe" . When I execute my python script : *import os def call(): os.system(' "C:\\Archivos de programa\\FWTools2.4.7\\bin\\ogr2ogr.exe" ')* * raw_input()* *call()* It runs, but if I want to enter arguments: *ogr2ogr* to test my .exe, it shows me an error *"ogr2ogr is not define", *any suggestion? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] function error
On Tue, Sep 28, 2010 at 8:26 PM, Evert Rol wrote: > > Perhaps if you provide the full traceback from the error (assuming you're > still getting this >error); tracebacks generally show the offending code as > well. It may be something that's >simply overlooked but shows in the > traceback. > > here it is: TypeError Traceback (most recent call last) ~/randomMove2.py in () > 1 2 3 4 5 ~/randomMove2.py in randomMove2(d1, d2, a1, a2) 6 while 1: 7 turtle.left(random.uniform(a1,a2)) > 8 checkForward.checkForward(random.uniform(d1,d2)) 9 if forward_failed == 'true': 10 turtle.right(180) ~/checkForward.py in checkForward(distance) 8 9 turtle.forward(distance) ---> 10 forward_failed = outOfBounds() 11 turtle.setx(old_position[0]); turtle.sety(old_position[1]) 12 turtle._pen.down() ~/checkForward.py in out_of_bounds() 19 20 def outOfBounds(): ---> 21 if (abs(turtle.position()[0]) > turtle.window_height()/2) or (abs(turtle.position()[1]) > turtle.window_width()/2): 22 return "true" 23 else: TypeError: 'function' object is unsubscriptable thank you -- roberto ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] generating independent random numbers
On 9/28/2010 5:11 PM, Carter Danforth wrote: Thanks for the replies, Dave and Joel. The reason I'm not just using the time or datetime modules for a random date is because it's restricted to 1970-2038; I'm pulling dates from 1600-3099. Thanks a lot for the pointer about the leap years, Dave, as well the class instances; just updated it and it's all working now, and also included the rest of the code too w/ answer verification and time tracking. I want to start using this program to test myself for speed calculation using Zeller's formula, it's pretty cool for determining the days of dates - http://mathforum.org/dr/math/faq/faq.calendar.html Because of the way variables C and D are split up from the year in the formula, I split up the year for self.c and self.y. import random, time, datetime, calendar class Date: def __init__(self): self.c = random.randint(16,30) self.y = random.randint(0,99) self.month = random.randint(1,12) self.year = self.c*100 + self.y apr = [4,6,9,11] feb = [2] notleap = [1700, 1800, 1900, 3000] if self.month in feb: if self.year%4 == 0: if self.year in notleap: self.k = random.randint(1,28) else: self.k = random.randint(1,29) else: self.k = random.randint(1,28) elif self.month in apr: self.k = random.randint(1,30) else: self.k = random.randint(1,31) if self.month in [1,2]: d = self.y - 1 m = self.month + 10 else: d = self.y m = self.month - 2 z = self.k + (13*m-1)/5 + d + d/4 + self.c/4 - 2*self.c if z< 0: r = (abs(z)/7)*7 + z + 7 else: r = z%7 dict = { 0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday' } self.day = dict[r] t1m = time.localtime().tm_min t1s = time.localtime().tm_sec t1 = t1m + t1s/100.0 n = 0 x = 0 while n< 10: newdate = Date() print '\n',calendar.month_name[newdate.month], newdate.k,',', newdate.year,'=', answer = raw_input() if answer.capitalize() == newdate.day: pass else: x += 1 n += 1 t2m = time.localtime().tm_min t2s = time.localtime().tm_sec t2 = t2m + t2s/100.0 td = t2 - t1 print '\n',x,'out of 10 wrong\nAvg time/question:',td/10,'\nTotal time:',td You top-posted again. Put your comments after the part you're quoting, not before. You still have a problem in the code, and I still think it's in the 2*self.c, though I don't have time to debug it. Look up the day for 1/1/2099, and for 1/1/2100 and it comes out the same. That's not correct. No adjacent years start on the same day, it's always either one day or two. You have too much in one function (method), which makes it hard to debug it. Factor it into separate functions, and then test each independently. And using k for day and d for year make no sense to me, though perhaps it does in some other language. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Writing txt from dbf
Thanks Steven and Alan, your recommendation worked perfectly! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] generating independent random numbers
Wow... I'm really slipping here with the leaps years, good catch on the 2000s. And yeah, a list does make a whole lot more sense. Thanks Dave. I've checked multiple sources on Zeller's formula, initially came across it on this book on vedic math (highly recommend it): http://amzn.to/bNXBM6. But here's the Wikipedia on it: http://en.wikipedia.org/wiki/Zeller%27s_congruence It's not *2 in the Julian calendar, but it is in Gregorian, which is what we're also using for the leap yrs - http://en.wikipedia.org/wiki/Leap_year On Tue, Sep 28, 2010 at 9:34 PM, Dave Angel wrote: > On 9/28/2010 5:11 PM, Carter Danforth wrote: > >> Thanks for the replies, Dave and Joel. The reason I'm not just using the >> time or datetime modules for a random date is because it's restricted to >> 1970-2038; I'm pulling dates from 1600-3099. Thanks a lot for the pointer >> about the leap years, Dave, as well the class instances; just updated it >> and >> it's all working now, and also included the rest of the code too w/ answer >> verification and time tracking. >> >> I want to start using this program to test myself for speed calculation >> using Zeller's formula, it's pretty cool for determining the days of dates >> - >> http://mathforum.org/dr/math/faq/faq.calendar.html >> >> Because of the way variables C and D are split up from the year in the >> formula, I split up the year for self.c and self.y. >> >> >> >> import random, time, datetime, calendar >> >> class Date: >> def __init__(self): >> self.c = random.randint(16,30) >> self.y = random.randint(0,99) >> self.month = random.randint(1,12) >> self.year = self.c*100 + self.y >> >> apr = [4,6,9,11] >> feb = [2] >> notleap = [1700, 1800, 1900, 3000] >> >> if self.month in feb: >> if self.year%4 == 0: >> if self.year in notleap: >> self.k = random.randint(1,28) >> else: >> self.k = random.randint(1,29) >> else: >> self.k = random.randint(1,28) >> elif self.month in apr: >> self.k = random.randint(1,30) >> else: >> self.k = random.randint(1,31) >> >> if self.month in [1,2]: >> d = self.y - 1 >> m = self.month + 10 >> else: >> d = self.y >> m = self.month - 2 >> >> z = self.k + (13*m-1)/5 + d + d/4 + self.c/4 - 2*self.c >> >> if z< 0: >> r = (abs(z)/7)*7 + z + 7 >> else: >> r = z%7 >> >> dict = { 0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', >> 4: >> 'Thursday', 5: 'Friday', 6: 'Saturday' } >> self.day = dict[r] >> >> t1m = time.localtime().tm_min >> t1s = time.localtime().tm_sec >> t1 = t1m + t1s/100.0 >> n = 0 >> x = 0 >> >> while n< 10: >> newdate = Date() >> >> print '\n',calendar.month_name[newdate.month], newdate.k,',', >> newdate.year,'=', >> answer = raw_input() >> if answer.capitalize() == newdate.day: >> pass >> else: >> x += 1 >> n += 1 >> >> t2m = time.localtime().tm_min >> t2s = time.localtime().tm_sec >> t2 = t2m + t2s/100.0 >> td = t2 - t1 >> >> print '\n',x,'out of 10 wrong\nAvg time/question:',td/10,'\nTotal >> time:',td >> >> >> >> >> (You top-posted your response, so your message is out of order) > > I haven't tried to run your code, but there is at least one problem. > > Your notleap list is very incomplete. > > notleap = [1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600, 2700, 2900, > 3000] > > I'm a little suspicious of your version of Zeller. I wouldn't think that > last term should have a 2* in it. > > I'm not sure why you use a dictionary to calculate self.day. A list would > work just as well. > > DaveA > > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing a txt from dbf
"Susana Iraiis Delgado Rodriguez" wrote Your guess was rigth, that's what I'm looking for! I need an output as: beta gamma etc... but I'm showing betagammaetc.. I assume you mean in the file? print adds a newline automatically when printing to stdout. Steven already showed you how to add a \n when writing the data to the file. Have you tried that? Are you still having problems? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing a txt from dbf
Hello Steven! Your guess was rigth, that's what I'm looking for! I need an output as: beta gamma etc... but I'm showing betagammaetc.. What can I do to my code to get the first ouput? By the way thanks for answering! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am looking for a book on Beginners who never programmed before or have no experience in programming
On 29/09/2010 12:20, Calle's Pyt(h)onkonto wrote: Or if you're like me (= Thinks reading a guide online is annoying since you have to switch between your browser and IDLE all the time), and would like to have a book that is easy to read and easy to understand, Merely from that perspective alone, you might be interested in something like http://trypython.org which gives you the main Python tutorial side by side with a console in a browser window. It uses Silverlight (it's put together by Michael Foord, long associated with promoting IronPython) which may be a stumbling block for technical or philosophical reasons, but it's quite a neat tool. TJG ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am looking for a book on Beginners who never programmed before or have no experience in programming
Or if you're like me (= Thinks reading a guide online is annoying since you have to switch between your browser and IDLE all the time), and would like to have a book that is easy to read and easy to understand, I would recommend Python Programming For The Absolute Beginner 3rd editon by Michael Dawson. I'm on the second chapter of the book, and have already learned alot about Python. Plus it's very good written. Hope I helped a little. // Calle From: Rodney Lewis Sent: Wednesday, September 29, 2010 1:22 AM To: tutor@python.org Subject: Re: [Tutor] I am looking for a book on Beginners who never programmed before or have no experience in programming making games is the best way to learn programming, and the book is free http://programming.gather.com/viewArticle.action?articleId=281474978440241 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] generating independent random numbers
Ewald Ertl wrote: >> Just an attempt from my side: > The year 1500 didn't have a 29th of February, the 28th work for me but > 29th also fails here. datetime.date( 1500, 2, 28 ) > datetime.date(1500, 2, 28) datetime.date( 1500, 2, 29 ) > Traceback (most recent call last): > File "", line 1, in > ValueError: day is out of range for month Yes, it's an expected "failure". I should have said that explicitly. Peter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] generating independent random numbers
Hi, On Wed, Sep 29, 2010 at 11:42 AM, Peter Otten <__pete...@web.de> wrote: > Carter Danforth wrote: > > > Thanks for the replies, Dave and Joel. The reason I'm not just using the > > time or datetime modules for a random date is because it's restricted to > > 1970-2038; I'm pulling dates from 1600-3099. Thanks a lot for the pointer > > The datetime module is not restricted to 1970...2038. It allows years > 1... (it uses the Gregorian calendar even before its adoption). > > >>> import datetime > >>> datetime.MINYEAR, datetime.MAXYEAR > (1, ) > >>> datetime.date(1500, 2, 29) > Traceback (most recent call last): > File "", line 1, in > ValueError: day is out of range for month > >>> datetime.date(1600, 2, 29) > datetime.date(1600, 2, 29) > > Just an attempt from my side: The year 1500 didn't have a 29th of February, the 28th work for me but 29th also fails here. >>> datetime.date( 1500, 2, 28 ) datetime.date(1500, 2, 28) >>> datetime.date( 1500, 2, 29 ) Traceback (most recent call last): File "", line 1, in ValueError: day is out of range for month HTH Ewald ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] generating independent random numbers
Carter Danforth wrote: > Thanks for the replies, Dave and Joel. The reason I'm not just using the > time or datetime modules for a random date is because it's restricted to > 1970-2038; I'm pulling dates from 1600-3099. Thanks a lot for the pointer The datetime module is not restricted to 1970...2038. It allows years 1... (it uses the Gregorian calendar even before its adoption). >>> import datetime >>> datetime.MINYEAR, datetime.MAXYEAR (1, ) >>> datetime.date(1500, 2, 29) Traceback (most recent call last): File "", line 1, in ValueError: day is out of range for month >>> datetime.date(1600, 2, 29) datetime.date(1600, 2, 29) The range allowed by the time module is probably implementation dependent. I can do things like >>> time.gmtime(-11670998400) time.struct_time(tm_year=1600, tm_mon=2, tm_mday=29, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=60, tm_isdst=0) >>> time.gmtime(2**55) time.struct_time(tm_year=1141709097, tm_mon=6, tm_mday=13, tm_hour=6, tm_min=26, tm_sec=8, tm_wday=6, tm_yday=164, tm_isdst=0) >>> time.gmtime(2**56) Traceback (most recent call last): File "", line 1, in ValueError: (75, 'Value too large for defined data type') Peter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problems install Python
> I had just download PeGreSQL, unzip and installed it, but I have a problem > such as: > > phuong...@ubuntu:~/PyGreSQL-4.0$ python setup.py build > sh: pg_config: not found > Traceback (most recent call last): > File "setup.py", line 94, in > pg_include_dir = pg_config('includedir') > File "setup.py", line 56, in pg_config > raise Exception("pg_config tool is not available.") > Exception: pg_config tool is not available. > > I do not know this errors. Could you help me, please? I can not install it > right now. Have you installed Postgresql? You probaby need the development package for that. Pygresql depends on Postgresql, and if you install from source as here, you probably need the Postgresql development stuff. But since you're on Ubuntu, can't you just install Pygresql through a package manager? That would take care of everything. Evert ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Problems install Python
Dear all, I had just download PeGreSQL, unzip and installed it, but I have a problem such as: phuong...@ubuntu:~/PyGreSQL-4.0$ python setup.py build sh: pg_config: not found Traceback (most recent call last): File "setup.py", line 94, in pg_include_dir = pg_config('includedir') File "setup.py", line 56, in pg_config raise Exception("pg_config tool is not available.") Exception: pg_config tool is not available. I do not know this errors. Could you help me, please? I can not install it right now. Thanks you very much. Best regards, Phuong -- FIT - HUFI ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor