Re: [Tutor] DOWHILE counter
cubanylinks4...@aol.com wrote Hi i am trying to write a pseudocode to read an input number and its 15% output value. The counter is supposed to process 4 input numbers. Help please!! Can you articulate what the program is supposed to do in more detail? Your description is very confusing to me. What is the program reading - one number or two? And what does it do with these to process them? Is there any output? Can you write down what a typical execution of this program will look like to the user? Maybe doing that will clarify in your mind what it is you must do. -- 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] Sounding Off, IDLE (Win7)
Wayne Watson wrote on 16 February 2010 at 17:58:- In Win7 IDLE, when I type in something with a syntax problem, a bell rings. How do I stop that? I've looked at Control Panel Sounds, but don't see anything of apparent use. I don't get this on my Win7 machine. But anyway, the sound is probably the same sound you get if you type CTRL-G at a command prompt in a DOS box, in which case it isn't one of the sounds you set in Control Panel. You can disable it using Device Manager. It's called 'System Speaker' and it's under 'System devices'. Right-click and choose 'Disable'. -- Michael ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Wrestling with the Py2exe Install, Win7[XP!], Py2.5
(This is the same msg as above, but I meant XP. I'm transitioning from XP to Win7, and am operating with two monitors and keyboards side by side. I thought I had used W7, but nope. Corrected wrestling it Subject.) I've finally decided to see if I could make an executable out of a py file. XP. Py2.5. I brought down the install file and proceeded with the install. I got two warning messages. Forgot the first. The second said,Could not set the key value. I again used OK. I think that was the only choice. It then issued a message in a larger dialog. I've attached it here, but have had zero luck recently and in the past. It was about setting a key, and pointed me to a log. It mentions a Removepy2exe -u Although it finished, I have no idea where the program is. It does not show up on the Start menu All Programs List nor my desktop. What's up. I've had these messages (key) occur on other Python installs as I transition to Win7. So far no problem. -- Crime is way down. War is declining. And that's far from the good news. -- Steven Pinker (and other sources) Why is this true, but yet the media says otherwise? The media knows very well how to manipulate us (see limbic, emotion, $$). -- WTW ___ 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
[Tutor] Find duplicates (using dictionaries)
I'm relatively new at Python and I'm trying to write a function that fills a dictionary acording the following rules and (example) data: Rules: * No duplicate values in field1 * No duplicates values in field2 and field3 simultaneous (highest value in field4 has to be preserved) Rec.no field1, field2, field3, field4 1. abc, def123, ghi123, 120 -- new, insert in dictionary 2. abc, def123, ghi123, 120 -- duplicate with 1. field4 same value. Do not insert in dictionary 3. bcd, def123, jkl125, 154 -- new, insert in dictionary 4. efg, def123, jkl125, 175 -- duplicate with 3 in field 2 and 3, but higher value in field4. Remove 3. from dict and replace with 4. 5. hij, ghi345, jkl125, 175 -- duplicate field3, but not in field4. New, insert in dict. The resulting dictionary should be: hij {'F2': ' ghi345', 'F3': ' jkl125', 'F4': 175} abc {'F2': ' def123', 'F3': ' ghi123', 'F4': 120} efg {'F2': ' def123', 'F3': ' jkl125', 'F4': 175} This is wat I came up with up to now, but there is something wrong with it. The 'bcd' should have been removed. When I run it it says: bcd {'F2': ' def123', 'F3': ' jkl125', 'F4': 154} hij {'F2': ' ghi345', 'F3': ' jkl125', 'F4': 175} abc {'F2': ' def123', 'F3': ' ghi123', 'F4': 120} efg {'F2': ' def123', 'F3': ' jkl125', 'F4': 175} Below is wat I brew (simplified). It took me some time to figure out that I was looking at the wrong values the wrong dictionary. I started again, but am ending up with a lot of dictionaries and for x in y-loops. I think there is a simpler way to do this. Can somebody point me in the right direction and explain to me how to do this? (and maybe have an alternative for the nesting. Because I may need to compare more fields. This is only a simplified dataset). # not working def createResults(field1, field2, field3, field4): #check if field1 exists. if not results.has_key(field1): if results.has_key(field2): #check if field2 already exists if results.has_key(field3): #check if field3 already exists #retrieve value field4 existing_field4 = results[field2][F4] #retrieve value existing field1 in dict existing_field1 = results[field1] #perform highest value check if int(existing_field4) int(field4): #remove existing record from dict. del results[existing_field1] values = {} values['F2'] = field2 values['F3'] = field3 values['F4'] = field4 results[field1] = values else: pass else: pass else: values = {} values['F2'] = field2 values['F3'] = field3 values['F4'] = field4 results[field1] = values else: pass for line in open(file.csv): field1, field2, field3, field4 = line.split(',') createResults(field1, field2, field3, int(field4)) #because this is quick and dirty I had to get rid of the \n in the csv for i in results.keys(): print i, '\t', results[i] contents file.csv abc, def123, ghi123, 120 abc, def123, ghi123, 120 bcd, def123, jkl125, 154 efg, def123, jkl125, 175 hij, ghi345, jkl125, 175 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The Order of Imports and install order ofmodulesandother matters (XP vs W7, ...)
-Original Message- [mailto:tutor-bounces+mike.hansen=atmel@python.org] On Behalf Of Alan Gauld Sent: Tuesday, February 16, 2010 5:58 PM Hansen, Mike mike.han...@atmel.com wrote I'm aware of Pep8. It's a good starting point. Anything more in-depth than Pep8 and the Zen of Python? There is the generic book Code Complete which is excellent, but definitely not short! And it's not Python specific - in fact doesn't even mention Python so far as I recall. But its advice is applicable to all languages. A bug fan, Alan g. Are you a bug fan of Code Complete? =) I need to revisit Code Complete. I read a huge amount of the first edition and about a 1/3 of the second edition. Although you can gather best practices from Pep8, the Zen of Python, The Python Cookbook, and other sources, it appears that a central resource for Python best practices doesn't exist. Mike ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Find duplicates (using dictionaries)
On 17 February 2010 16:31, Karjer Jdfjdf karper12...@yahoo.com wrote: I'm relatively new at Python and I'm trying to write a function that fills a dictionary acording the following rules and (example) data: Rules: * No duplicate values in field1 * No duplicates values in field2 and field3 simultaneous (highest value in field4 has to be preserved) Rec.no field1, field2, field3, field4 1. abc, def123, ghi123, 120 -- new, insert in dictionary 2. abc, def123, ghi123, 120 -- duplicate with 1. field4 same value. Do not insert in dictionary 3. bcd, def123, jkl125, 154 -- new, insert in dictionary 4. efg, def123, jkl125, 175 -- duplicate with 3 in field 2 and 3, but higher value in field4. Remove 3. from dict and replace with 4. 5. hij, ghi345, jkl125, 175 -- duplicate field3, but not in field4. New, insert in dict. The resulting dictionary should be: hij {'F2': ' ghi345', 'F3': ' jkl125', 'F4': 175} abc {'F2': ' def123', 'F3': ' ghi123', 'F4': 120} efg {'F2': ' def123', 'F3': ' jkl125', 'F4': 175} This is wat I came up with up to now, but there is something wrong with it. The 'bcd' should have been removed. When I run it it says: bcd {'F2': ' def123', 'F3': ' jkl125', 'F4': 154} hij {'F2': ' ghi345', 'F3': ' jkl125', 'F4': 175} abc {'F2': ' def123', 'F3': ' ghi123', 'F4': 120} efg {'F2': ' def123', 'F3': ' jkl125', 'F4': 175} Below is wat I brew (simplified). It took me some time to figure out that I was looking at the wrong values the wrong dictionary. I started again, but am ending up with a lot of dictionaries and for x in y-loops. I think there is a simpler way to do this. Can somebody point me in the right direction and explain to me how to do this? (and maybe have an alternative for the nesting. Because I may need to compare more fields. This is only a simplified dataset). # not working def createResults(field1, field2, field3, field4): #check if field1 exists. if not results.has_key(field1): if results.has_key(field2): #check if field2 already exists if results.has_key(field3): #check if field3 already exists #retrieve value field4 existing_field4 = results[field2][F4] #retrieve value existing field1 in dict existing_field1 = results[field1] #perform highest value check if int(existing_field4) int(field4): #remove existing record from dict. del results[existing_field1] values = {} values['F2'] = field2 values['F3'] = field3 values['F4'] = field4 results[field1] = values else: pass else: pass else: values = {} values['F2'] = field2 values['F3'] = field3 values['F4'] = field4 results[field1] = values else: pass for line in open(file.csv): field1, field2, field3, field4 = line.split(',') createResults(field1, field2, field3, int(field4)) #because this is quick and dirty I had to get rid of the \n in the csv for i in results.keys(): print i, '\t', results[i] contents file.csv abc, def123, ghi123, 120 abc, def123, ghi123, 120 bcd, def123, jkl125, 154 efg, def123, jkl125, 175 hij, ghi345, jkl125, 175 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor First observation: This strikes me as a poor use for dictionaries. You might be better using a list of tuples. If you will always have four fields in each line of data, then there is no need to check for the existence of the other elements, so you can cut out a lot of the checks. Whilst your requirements are not exactly clear to me, here is how I would do what it sounds like you need (using the same dict layout as you used previously): def add_record(field1, field2, field3, field4): if field1 in results: #duplicate in field1, do nothing return for key, item in results.iteritems(): if field2 == item['F2'] and field3 == item['F3'] #duplicate in
Re: [Tutor] Wrestling with the Py2exe Install, Win7[XP!], Py2.5
-Original Message- From: tutor-bounces+bermanrl=cfl.rr@python.org [mailto:tutor- bounces+bermanrl=cfl.rr@python.org] On Behalf Of Wayne Watson Sent: Wednesday, February 17, 2010 10:48 AM To: *tutor python Subject: [Tutor] Wrestling with the Py2exe Install, Win7[XP!], Py2.5 (This is the same msg as above, but I meant XP. I'm transitioning from XP to Win7, and am operating with two monitors and keyboards side by side. I thought I had used W7, but nope. Corrected wrestling it Subject.) I've finally decided to see if I could make an executable out of a py file. XP. Py2.5. I brought down the install file and proceeded with the install. I got two warning messages. Forgot the first. The second said,Could not set the key value. I again used OK. I think that was the only choice. It then issued a message in a larger dialog. I've attached it here, but have had zero luck recently and in the past. It was about setting a key, and pointed me to a log. It mentions a Removepy2exe -u Although it finished, I have no idea where the program is. It does not show up on the Start menu All Programs List nor my desktop. What's up. I've had these messages (key) occur on other Python installs as I transition to Win7. So far no problem. Hi Wayne, A few observations as I do this on Win 7 and it does work. The first thing you need to do is follow the tutorial that you should get at the web page found in the README file. If not, here it is: http://www.py2exe.org/index.cgi/Tutorial That will get you started. Now, if everything went reasonably well, you will find two folders in the folder where you ran py2exe called build and dist. Ignore build. In dist you will find the exe file and all the files necessary to run your program.gasp.sort of. There is a caveat. Sometimes you don't get all the necessary DLL's. If you are in that situation py2exe should have printed the files you still need to track down and include with your install. Now, I gave you all this information as background because we all know that really good programmers like mental and emotional pain. Well, perhaps some do. I like the easy but good way when available. Let me tell you what I use because it takes a lot of the drudgery out of the process. Go to http://code.google.com/p/gui2exe/ and download gui2exe. It will automate much of the process and it seems to do a great job of finding all the necessary files including the DLL's hidden from py2exe. I hope this helps. It is a good concept and good software. Robert Berman ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] datetime, time zones, and ISO time
I have been really scratching my head over this, it seems like there *should* be a nice easy way to do what I want but I can't find it for the life of me. What I would like to do would be something like this: datetime.datetime.now().isoformat() '2010-02-17T12:13:17.913260-06:00' But what actually happens is this: '2010-02-17T12:13:17.913260' I need to keep track of the time zone because I'm working with Google Calendar's query APIs, and it interprets all times as GMT unless you specify the time zone, which means my search results are wrong. Anyway, I was thinking I could get around it with something like: now=datetime.datetime.now() offset=datetime.datetime.utcoffset() [somehow add the offset info into the now datetime object] now.isoformat() But a) I don't know how to stick the offset info into a datetime object, and the documentation doesn't seem to say anything about this; and b) the offset line doesn't work anyway: datetime.datetime.utcoffset() Traceback (most recent call last): File stdin, line 1, in module TypeError: descriptor 'utcoffset' of 'datetime.datetime' object needs an argument I think there's a combination of problems here, involving me not understanding something fundamental about datetime objects, and also involving problems with the documentation (there are a whole lot of places where the optional arguments to the methods are left off of the syntax, for example.) Can anyone help sort me out here? In particular, is there a really straightforward way to do what I'm looking for? One more general kind of confusion that this has raised for me is that it seems that in the datetime documentation (and possibly other places as well) there is an unexplained distinction between methods which can be used to give you new information, and methods which only can tell you something about the instance they are invoked on. For example, this works the way I feel like it ought to: datetime.datetime.now() datetime.datetime(2010, 2, 17, 12, 42, 30, 520792) But this doesn't work at all: datetime.datetime.utcoffset() Traceback (most recent call last): File stdin, line 1, in module TypeError: descriptor 'utcoffset' of 'datetime.datetime' object needs an argument And I haven't been able to find another method instead of utcoffset() that will give me what I want. Thanks very much to anyone who can illuminate my darkness on this matter. :) -- -dave Pseudo-colored pictures of a person's brain lighting up are undoubtedly more persuasive than a pattern of squiggles produced by a polygraph. That could be a big problem if the goal is to get to the truth. -Dr. Steven Hyman, Harvard ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Find duplicates (using dictionaries)
On Wed, Feb 17, 2010 at 11:31 AM, Karjer Jdfjdf karper12...@yahoo.comwrote: I'm relatively new at Python and I'm trying to write a function that fills a dictionary acording the following rules and (example) data: Rules: * No duplicate values in field1 * No duplicates values in field2 and field3 simultaneous (highest value in field4 has to be preserved) Rec.no field1, field2, field3, field4 1. abc, def123, ghi123, 120 -- new, insert in dictionary 2. abc, def123, ghi123, 120 -- duplicate with 1. field4 same value. Do not insert in dictionary 3. bcd, def123, jkl125, 154 -- new, insert in dictionary 4. efg, def123, jkl125, 175 -- duplicate with 3 in field 2 and 3, but higher value in field4. Remove 3. from dict and replace with 4. 5. hij, ghi345, jkl125, 175 -- duplicate field3, but not in field4. New, insert in dict. The resulting dictionary should be: hij {'F2': ' ghi345', 'F3': ' jkl125', 'F4': 175} abc {'F2': ' def123', 'F3': ' ghi123', 'F4': 120} efg {'F2': ' def123', 'F3': ' jkl125', 'F4': 175} I'm not sure I understand the rules. Something like if (f2, f3) in list: if associated f4 new f4: delete old (f2, f3) entry add new entry (f1, f2, f3, f4) else if f1 not in list: add new entry (f1, f2, f3, f4) Is that right? If so, ISTM you need a table with two indexes, one by f1 and one by (f2, f3). This might be a good application for an in-memory sqlite database, which can maintain indexes for you. If the number of items is not too large, you could just implement this as a list of (f1, f2, f3, f4) tuples and repeated searches through the list, but that will get slow quickly as the number of items being added grows. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] datetime, time zones, and ISO time
On Wed, Feb 17, 2010 at 12:44:02PM -0600, David Perlman wrote: I have been really scratching my head over this, it seems like there *should* be a nice easy way to do what I want but I can't find it for the life of me. ... But a) I don't know how to stick the offset info into a datetime object, and the documentation doesn't seem to say anything about this; and b) the offset line doesn't work anyway: I think that you need to push in a tzinfo object, rather than a value: http://docs.python.org/library/datetime.html#datetime.tzinfo I get that from here: For applications requiring more, datetime and time objects have an optional time zone information member, tzinfo, that can contain an instance of a subclass of the abstract tzinfo class. These tzinfo objects capture information about the offset from UTC time, the time zone name, and whether Daylight Saving Time is in effect. Note that no concrete tzinfo classes are supplied by the datetime module. Supporting timezones at whatever level of detail is required is up to the application. The rules for time adjustment across the world are more political than rational, and there is no standard suitable for every application.[1] I suspect that it'll take some fooling around to see how it works though - use the interpreter or ipython to test things out. [1] http://docs.python.org/library/datetime.html -- yours, William ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] command error
Dnia 17-02-2010 o 00:41:21 Shurui Liu (Aaron Liu) shuru...@gmail.com napisał(a): But I have tried, it doesn't work on my workstation, i don't know why. I run it on my python software, python 3.0. The script is written in Python 2. Try rewriting it, mostly by changing the number formatting, function syntax and function names. Or simply install a Python 2.x interpreter. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] datetime, time zones, and ISO time
Yeah, I got this part. The thing that's hanging me up is that there doesn't seem to be any way to get a tzinfo instance that contains the current local time zone information. You can do time.timezone to get the seconds from UTC, but there doesn't seem to be any way to convert that into a tzinfo! I would be perfectly satisfied with this: tz=offset_to_tzinfo(time.timezone) # or whatever aware_now=datetime.datetime.now(tz) print aware_now.isoformat() I'm pretty sure that would give me what I want, but there doesn't seem to be any way to do step one without subclassing tzinfo. This makes me feel like I MUST be missing something obvious, because it shouldn't require so much coding just to find out what the current local time and timezone is! On Feb 17, 2010, at 1:42 PM, William Witteman wrote: On Wed, Feb 17, 2010 at 12:44:02PM -0600, David Perlman wrote: I have been really scratching my head over this, it seems like there *should* be a nice easy way to do what I want but I can't find it for the life of me. ... But a) I don't know how to stick the offset info into a datetime object, and the documentation doesn't seem to say anything about this; and b) the offset line doesn't work anyway: I think that you need to push in a tzinfo object, rather than a value: http://docs.python.org/library/datetime.html#datetime.tzinfo I get that from here: For applications requiring more, datetime and time objects have an optional time zone information member, tzinfo, that can contain an instance of a subclass of the abstract tzinfo class. These tzinfo objects capture information about the offset from UTC time, the time zone name, and whether Daylight Saving Time is in effect. Note that no concrete tzinfo classes are supplied by the datetime module. Supporting timezones at whatever level of detail is required is up to the application. The rules for time adjustment across the world are more political than rational, and there is no standard suitable for every application.[1] I suspect that it'll take some fooling around to see how it works though - use the interpreter or ipython to test things out. [1] http://docs.python.org/library/datetime.html -- yours, William ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- -dave Pseudo-colored pictures of a person's brain lighting up are undoubtedly more persuasive than a pattern of squiggles produced by a polygraph. That could be a big problem if the goal is to get to the truth. -Dr. Steven Hyman, Harvard ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] datetime, time zones, and ISO time
On Wed, Feb 17, 2010 at 3:12 PM, David Perlman dperl...@wisc.edu wrote: Yeah, I got this part. The thing that's hanging me up is that there doesn't seem to be any way to get a tzinfo instance that contains the current local time zone information. You can do time.timezone to get the seconds from UTC, but there doesn't seem to be any way to convert that into a tzinfo! I would be perfectly satisfied with this: tz=offset_to_tzinfo(time.timezone) # or whatever aware_now=datetime.datetime.now(tz) print aware_now.isoformat() I'm pretty sure that would give me what I want, but there doesn't seem to be any way to do step one without subclassing tzinfo. This makes me feel like I MUST be missing something obvious, because it shouldn't require so much coding just to find out what the current local time and timezone is! The docs make it clear that you do have to subclass tzinfo, that no implementations are provided. It sounds like you want the LocalTimezone class in the examples at the bottom of this section - A class capturing the platform's idea of local time: http://docs.python.org/library/datetime.html#tzinfo-objects Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] datetime, time zones, and ISO time
On Wed, Feb 17, 2010 at 3:48 PM, David Perlman dperl...@wisc.edu wrote: Surely there is a way to simply print out the local time, date and time zone without needing to write your own class... I can't believe this is the only way... Here's why I don't believe it. Both the datetime and time modules provide both functions for returning the current local time, and the current utc time. So, assuming that those functions are trustworthy, it *must* be true that the OS always knows the current offset from utc time. So why is there no straightforward way to get that offset in python? I mean, I can do this: time.timezone gives the offset for standard time. Kent datetime.datetime.now()-datetime.datetime.utcnow() datetime.timedelta(-1, 64799, 87) But then, of course, it's off by a few fractions of a microsecond. This works: time.localtime()[3]-time.gmtime()[3] -6 But doesn't that seem like a ridiculous hack, well outside of the spirit of the zen of python? Shouldn't there be one obvious right way to do this? Yes, I know that as William sent, the documentation points out that the rules the world over are complicated and irrational. Nonetheless, it is implicit in the existing definitions that the system *knows* the current offset, even if it doesn't know the whole set of rules... On Feb 17, 2010, at 2:26 PM, Kent Johnson wrote: On Wed, Feb 17, 2010 at 3:12 PM, David Perlman dperl...@wisc.edu wrote: Yeah, I got this part. The thing that's hanging me up is that there doesn't seem to be any way to get a tzinfo instance that contains the current local time zone information. You can do time.timezone to get the seconds from UTC, but there doesn't seem to be any way to convert that into a tzinfo! I would be perfectly satisfied with this: tz=offset_to_tzinfo(time.timezone) # or whatever aware_now=datetime.datetime.now(tz) print aware_now.isoformat() I'm pretty sure that would give me what I want, but there doesn't seem to be any way to do step one without subclassing tzinfo. This makes me feel like I MUST be missing something obvious, because it shouldn't require so much coding just to find out what the current local time and timezone is! The docs make it clear that you do have to subclass tzinfo, that no implementations are provided. It sounds like you want the LocalTimezone class in the examples at the bottom of this section - A class capturing the platform's idea of local time: http://docs.python.org/library/datetime.html#tzinfo-objects Kent -- -dave Pseudo-colored pictures of a person's brain lighting up are undoubtedly more persuasive than a pattern of squiggles produced by a polygraph. That could be a big problem if the goal is to get to the truth. -Dr. Steven Hyman, Harvard ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] datetime, time zones, and ISO time
But this doesn't help, because then you still don't know whether it's dst or not. You then would have to jump through whatever convolutions to do that calculation. All I want to know is the *current* offset between local time and utc. I know the system has this information already; it doesn't require any kind of fancy calculations about global politics or anything. On Feb 17, 2010, at 3:12 PM, Kent Johnson wrote: On Wed, Feb 17, 2010 at 3:48 PM, David Perlman dperl...@wisc.edu wrote: Surely there is a way to simply print out the local time, date and time zone without needing to write your own class... I can't believe this is the only way... Here's why I don't believe it. Both the datetime and time modules provide both functions for returning the current local time, and the current utc time. So, assuming that those functions are trustworthy, it *must* be true that the OS always knows the current offset from utc time. So why is there no straightforward way to get that offset in python? I mean, I can do this: time.timezone gives the offset for standard time. -- -dave Pseudo-colored pictures of a person's brain lighting up are undoubtedly more persuasive than a pattern of squiggles produced by a polygraph. That could be a big problem if the goal is to get to the truth. -Dr. Steven Hyman, Harvard ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] datetime, time zones, and ISO time
OK, here's a function that does precisely what I want: def tzDelta(): by whatever means necessary, return the current offset of the local time from utc. s=time.time() t,u=time.localtime(s),time.gmtime(s) osec=3600*(t[3]-u[3]) + 60*(t[4]-u[4]) + (t[5]-u[5]) return datetime.timedelta(seconds=osec) As far as I can tell, this should always work. So wouldn't it be nice if there were a less convoluted way to get this?? On Feb 17, 2010, at 3:12 PM, Kent Johnson wrote: On Wed, Feb 17, 2010 at 3:48 PM, David Perlman dperl...@wisc.edu wrote: Surely there is a way to simply print out the local time, date and time zone without needing to write your own class... I can't believe this is the only way... Here's why I don't believe it. Both the datetime and time modules provide both functions for returning the current local time, and the current utc time. So, assuming that those functions are trustworthy, it *must* be true that the OS always knows the current offset from utc time. So why is there no straightforward way to get that offset in python? I mean, I can do this: time.timezone gives the offset for standard time. Kent datetime.datetime.now()-datetime.datetime.utcnow() datetime.timedelta(-1, 64799, 87) But then, of course, it's off by a few fractions of a microsecond. This works: time.localtime()[3]-time.gmtime()[3] -6 But doesn't that seem like a ridiculous hack, well outside of the spirit of the zen of python? Shouldn't there be one obvious right way to do this? Yes, I know that as William sent, the documentation points out that the rules the world over are complicated and irrational. Nonetheless, it is implicit in the existing definitions that the system *knows* the current offset, even if it doesn't know the whole set of rules... On Feb 17, 2010, at 2:26 PM, Kent Johnson wrote: On Wed, Feb 17, 2010 at 3:12 PM, David Perlman dperl...@wisc.edu wrote: Yeah, I got this part. The thing that's hanging me up is that there doesn't seem to be any way to get a tzinfo instance that contains the current local time zone information. You can do time.timezone to get the seconds from UTC, but there doesn't seem to be any way to convert that into a tzinfo! I would be perfectly satisfied with this: tz=offset_to_tzinfo(time.timezone) # or whatever aware_now=datetime.datetime.now(tz) print aware_now.isoformat() I'm pretty sure that would give me what I want, but there doesn't seem to be any way to do step one without subclassing tzinfo. This makes me feel like I MUST be missing something obvious, because it shouldn't require so much coding just to find out what the current local time and timezone is! The docs make it clear that you do have to subclass tzinfo, that no implementations are provided. It sounds like you want the LocalTimezone class in the examples at the bottom of this section - A class capturing the platform's idea of local time: http://docs.python.org/library/datetime.html#tzinfo-objects Kent -- -dave Pseudo-colored pictures of a person's brain lighting up are undoubtedly more persuasive than a pattern of squiggles produced by a polygraph. That could be a big problem if the goal is to get to the truth. -Dr. Steven Hyman, Harvard -- -dave Pseudo-colored pictures of a person's brain lighting up are undoubtedly more persuasive than a pattern of squiggles produced by a polygraph. That could be a big problem if the goal is to get to the truth. -Dr. Steven Hyman, Harvard ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] datetime, time zones, and ISO time
On Wed, Feb 17, 2010 at 03:24:26PM -0600, David Perlman wrote: But this doesn't help, because then you still don't know whether it's dst or not. You then would have to jump through whatever convolutions to do that calculation. All I want to know is the *current* offset between local time and utc. I know the system has this information already; it doesn't require any kind of fancy calculations about global politics or anything. Well, does time.timezone help? It returns time offset from UTC in seconds. -- yours, William ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] datetime, time zones, and ISO time
On Wed, Feb 17, 2010 at 4:37 PM, David Perlman dperl...@wisc.edu wrote: OK, here's a function that does precisely what I want: def tzDelta(): by whatever means necessary, return the current offset of the local time from utc. s=time.time() t,u=time.localtime(s),time.gmtime(s) osec=3600*(t[3]-u[3]) + 60*(t[4]-u[4]) + (t[5]-u[5]) return datetime.timedelta(seconds=osec) As far as I can tell, this should always work. So wouldn't it be nice if there were a less convoluted way to get this?? Here is a shorter version based on the LocalTimezone example, and it only gets the time once so there is no possible race condition: In [5]: from datetime import timedelta In [6]: import time In [15]: def utcoffset(): : if time.localtime().tm_isdst 0: : return timedelta(seconds = -time.altzone) : return timedelta(seconds = -time.timezone) : In [16]: utcoffset() Out[16]: datetime.timedelta(-1, 68400) Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] datetime, time zones, and ISO time
On 17 February 2010 22:37, David Perlman dperl...@wisc.edu wrote: As far as I can tell, this should always work. So wouldn't it be nice if there were a less convoluted way to get this?? There is pytz [1] which should provide a simpler way to manage timezone info in python. Greets Sander [1] http://pytz.sourceforge.net/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] datetime, time zones, and ISO time
On Feb 17, 2010, at 4:17 PM, Sander Sweers wrote: On 17 February 2010 22:37, David Perlman dperl...@wisc.edu wrote: As far as I can tell, this should always work. So wouldn't it be nice if there were a less convoluted way to get this?? There is pytz [1] which should provide a simpler way to manage timezone info in python. Well, this is actually more complicated, not simpler. The description says: pytz brings the Olson tz database into Python. This library allows accurate and cross platform timezone calculations using Python 2.3 or higher. It also solves the issue of ambiguous times at the end of daylight savings, which you can read more about in the Python Library Reference (datetime.tzinfo). I don't want to deal with any of that stuff, I just want to know the actual current offset between local time and UTC. I don't care what the offset is in Nepal; I don't care what the offset will be come summertime; I don't care about anything in the Olson tz database. I just want something to tell me the current offset, right here, right now, on my own computer. Anyway, I already wrote a function to calculate it, so it's a done deal. At this point I'm just really surprised that I had to... For completeness, here's what I came up with. The format function was necessary because Google calendar queries require the time zone to be represented in the string like: 2010-03-01T21:00:00.000-06:00 However the isoformat() methods in python instead give something like: 2010-03-01T21:00:00.000-0600 which gives a server error if you try to send it to Google. Unfortunately, and also bizarrely, even the strftime() doesn't provide any way to generate the format Google demands, so I had to also write a function to do the formatting. def tzDeltaForm(self, tzd=None): return the tzdelta in the format Google wants it, such as -06:00 if not tzd: tzd=self.tzDelta() if tzd 0: sign = -1 tzd = -tzd else: sign = 1 h = sign * tzd // 3600 m = (tzd % 3600) // 60 form = '%+03d:%02d' % (h,m) return form def tzDelta(self): by whatever means necessary, return the current offset of the local time from utc. s=time.time() t,u=time.localtime(s),time.gmtime(s) osec=3600*(t[3]-u[3]) + 60*(t[4]-u[4]) + (t[5]-u[5]) #return datetime.timedelta(seconds=osec) return osec OK, I hope that is helpful to someone else someday, because it has been an astonishing amount of pain to accomplish something seemingly so simple... Thanks to everyone for the input, every bit of it helped guide me along. :) -- -dave Pseudo-colored pictures of a person's brain lighting up are undoubtedly more persuasive than a pattern of squiggles produced by a polygraph. That could be a big problem if the goal is to get to the truth. -Dr. Steven Hyman, Harvard ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Wrestling with the Py2exe Install, Win7[XP!], Py2.5
I'm following the tutorial and ran into a snag. Here is the console output.( Can I do this from IDLE?) C:\Sandia_Meteors\Sentinel_Development\Learn_Pythonc:\python25\python setup.py Traceback (most recent call last): File setup.py, line 2, in module import py2exe ImportError: No module named py2exe Note the need to back pedal to c:\python25\ Perhaps I need some path variable set? -- There is nothing so annoying as to have two people talking when you're busy interrupting. -- Mark Twain ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor