Re: [Tutor] Two Newbile Questions: porting drawmap.c, & Python as a lifetime language
Really depends on what you do, if you program for a living most probably you'll use whatever your employer tells you to use, if you work by yourself you can choose, personally i like python because of the enormous amount of modules available to do all sort of stuff, from GUI programing, to unittest, database, compression, graphics (OpenGL), etc. I like it because is crossplatform, it may not be fast but most modules are written in C already and are fast. I like the ability to use py2exe and generate a windows executable in a folder with ALL that i need to deploy on windows, i just copy the folder and i'm done. The Python comunity is very nice too, this is a big plus if you are learning a new languaje, the tutor list is always very helpful and active. You can always make use of your C skills even on Python, by writting modules to extend python functionability or to speed it up. Good Luck! Regards Carlos Daniel Ruvalcaba Valenzuela On 7/4/06, Kent Johnson <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > 2. Seeing Python hailed as a good language for learning programming, > > how do you > > rate it as a lifetime language? (I can imagine that many people have > > settled into one language for doing the remainder of their life's work. > > If > > I am pressed, I will choose Perl at this point.) > Python is great as a general-purpose programming language and it is my > language of choice today. But I'll be pretty surprised if it keeps the > spot on the top of the heap for the rest of my working life... > > Kent > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Two Newbile Questions: porting drawmap.c, & Python as a lifetime language
[EMAIL PROTECTED] wrote: > 2. Seeing Python hailed as a good language for learning programming, > how do you > rate it as a lifetime language? (I can imagine that many people have > settled into one language for doing the remainder of their life's work. If > I am pressed, I will choose Perl at this point.) Python is great as a general-purpose programming language and it is my language of choice today. But I'll be pretty surprised if it keeps the spot on the top of the heap for the rest of my working life... Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I Give Up. - Follow up post
OK .. so far so good.. :) > ultimately want is not meshing well together. > > Let's clarify the requirement: you want to have a mapping from services > to their configurations. Ignoring the format of the configuration file > for the moment, it sounds like you ultimately want to parse the > configruation and get: > > ### machine_config.py > machines = { 'apache': ('http', 80), > ## fill me in with the config of other machines > } > > > could be used as your configuration file format. The value portion of a > dictionary can be an arbitrary value. Here, we map a machine's name to > a tuple containing the protocol and port. No string hackery is involved > here. Umm well it would work nicely . *if* the configuration never changed- on the other hand, if it is to do as intended, the user of the application needs to be able to add and edit the configuration. Unless they are well versed with python and/or programming and syntax, (in which case they probably arent going to use this script anyway) it is easier for a non programmer to edit a configuration that makes some sort of logical sense and is easy to edit, without screwing up (need to make it more "forgiving" of things like line endings, spacing between name , = and value elements, etc) So the ideal format is a configuration file where they can set up (within documented settings parameters) additional machines or protocols or ports to suit their own network or layout.. Easiest = a simple file where I set name = value and import it. HOWEVER, if it is to be imported, it must follow proper python syntax, correct? so if it doesnt, they break it an INI file on the other hand is a bit more forgiving, thus the choice to use it. > > > >> believe me I have tried dictionaries, Ive tried parsing the file by >> other means, but the only way I could get results I needed was through >> configparser > > > I think you're making this problem too hard for yourself. > perhaps, when I get back to the project I plan to do a code review of code to date.. I have no problem with refactoring if I can find a better, momre effective way, but it *must* also be user friendly (ease of use and configuration for a non-programmer) Another option I had was to do a web based interface for setting up additional machines, etc, but web based is non-ideal for this application. > > >> Yeah, basically you carry values in a dictionary named by keyname , >> but.. there have been situations where I need the key name as the >> variable name , I.E. config_options[name] = value could become >> >> name = value as if it was explicitly defined that way > > > > Can you show us the situation you're talking about that requires this? if cfg.has_section("Hosts"): hostnames = cfg.items("Hosts") for hostname in hostnames: watch_hosts[hostname[0]] = hostname[1] would result in like watch_hosts = {"http" : "80","https" : "443"} Sure, I could also do this by reading a config file and parsing it , but .. Say you split at the = , and the config file is like http = 80 #standard http port https=443#https port Are you going to be 100% certain that when you call watch_hosts[http], that you are gonna GET string "80"? what about the space between http and = ? if you wanted to check if watch_hosts[keyname] == "http" , it would never work, right?(because if you split at =, you have string "http " <- see the space char?) if I, however use configparser, I will always get "http" and "80" where I expect them to be , I dont have to worry (or send values through a cleanup or trim() function) if it has a comment for the line or not, I dont need to worry if there is whitespace. right? This is intended for an end user to be able to edit a config file with a minimum of trouble, and be able to leave comments so they remember what each is for.. :) (and if they do mess up somehow, I can catch that kind of thing in the code) I just didnt see any more effective way to get this than to use ConfigParser as opposed to attempting to parse a file using regular expressions , trim, split, etc > > > >> It's difficult to grasp or even effectively explain the concept or idea, > > > Try to do so. I think this is a real point that needs to be cleared up. I understand the concept/idea behind it, I think above is about as good an explanation as it gets - I tried to express how Im thinking/approaching this issue. Keeping in mind this is going to be edited by someone who may or may not know how to write a single line of program code.. :) > > Take a look at: > > http://www.python.org/doc/faq/general.html#why-isn-t-there-a-switch-or-case-statement-in-python > > Thanks. reading up on it now. > > If you need more examples, ask, and someone here on the list will be > happy to help. > > !DSPAM:44aae7ac185203757830825! > > _
Re: [Tutor] Two Newbile Questions: porting drawmap.c, & Python as a lifetime language
[EMAIL PROTECTED] wrote: > 1. What's involved in a port of a C program into Python? (drawmap is > offered in a number of linux distributions btw.) It really (really) depends on the C program -- C's so flexible that you can write in a "Pythonish" style (specifically, avoiding pointer tricks, keeping OS-specific stuff to a minimum, and using good functional / data abstractions) or in a style that's so obfuscated as to make any sort of translation impossible. The flip side of that is that Python is flexible enough to accommodate many programming styles. It sounds like this program basically parses an input file and produces an output file, so I would bet that you can find some existing code that will read the input file, and some other existing code that will write the output file. Then you just have to write the middle part. > 2. Seeing Python hailed as a good language for learning programming, > how do you > rate it as a lifetime language? (I can imagine that many people have > settled into one language for doing the remainder of their life's work. If > I am pressed, I will choose Perl at this point.) Eep, Perl! Once a polyglot, always a polyglot. My choice of language depends on the context. For quick web stuff, PHP (O! How I hate thee!). For quick manipulation of files and/or using lots of external programs, shell. For just about everything else, Python. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I Give Up. - Follow up post
> Now, if I want to iterate over a list of machines , and check each > machine for whatever it was set for (in the config file) > So the config file requires http to be the first part of the string , > followed by something to make it unique (I.E. http1, http2, http3) Hi Brian, Have you considered using something other than the INI-style file format? ConfigParser works best when there is a fixed, known set of keys: it doesn't work so well when you want to use an arbitrary collection where you do not know the keys in advance. Bluntly put: it does sound like the INI file format and what you ultimately want is not meshing well together. Let's clarify the requirement: you want to have a mapping from services to their configurations. Ignoring the format of the configuration file for the moment, it sounds like you ultimately want to parse the configruation and get: { 'apache' : ('http', 80), ... } where a 'machine' is the key into the corresponding (protocol, port) value. If you are just prototyping this, a 'module' file like: ### machine_config.py machines = { 'apache': ('http', 80), ## fill me in with the config of other machines } could be used as your configuration file format. The value portion of a dictionary can be an arbitrary value. Here, we map a machine's name to a tuple containing the protocol and port. No string hackery is involved here. > believe me I have tried dictionaries, Ive tried parsing the file by > other means, but the only way I could get results I needed was through > configparser I think you're making this problem too hard for yourself. > Yeah, basically you carry values in a dictionary named by keyname , > but.. there have been situations where I need the key name as the > variable name , I.E. config_options[name] = value could become > > name = value as if it was explicitly defined that way Can you show us the situation you're talking about that requires this? > It's difficult to grasp or even effectively explain the concept or idea, Try to do so. I think this is a real point that needs to be cleared up. > Agreed. however I just set this as a preliminary - in no way is this > code finished. :) I actually intended to build a function to do this. > (In perl, I use Switch/Case statements, does Python have anything > similar? - with switch/case, you can set a final default "catch-all" at > which point I log a "unknown event" message ) Take a look at: http://www.python.org/doc/faq/general.html#why-isn-t-there-a-switch-or-case-statement-in-python If you need more examples, ask, and someone here on the list will be happy to help. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Two Newbile Questions: porting drawmap.c, & Python as a lifetime language
Relying heavily on the "newbies treated well" advertisment... :^) I'm an old C programmer, which is to say (a) I am old, and (b) even when young, I somehow managed to program in "old C". I have been working--for years--on creating a personal variant of drawmap.c, Fred M. Erickson's wonderful USGS-maps-to-shaded-relief-or-contour-map-renderer, and I have finally hit the wall. I have managed to make things sooo complex that the effort to learn a new language no longer seems unaffordable. I am seeking opinions from seasoned veterans on the following two questions: 1. What's involved in a port of a C program into Python? (drawmap is offered in a number of linux distributions btw.) 2. Seeing Python hailed as a good language for learning programming, how do you rate it as a lifetime language? (I can imagine that many people have settled into one language for doing the remainder of their life's work. If I am pressed, I will choose Perl at this point.) Humbly, -- David ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Correct way to code data or parse?
Hi Everyone *Please forgive me the repost ... I sent the first as Html not Text Dumb, Doh! I'm new to python and I want to write a rpg program that is able to use the Correct way to code data or parse? I have pages of .PDF file Data tables copied to text files. I check out many python boards & Books with little illumination to my problem. I'm still not sure about what to use for this data listing: Lists?, Tuple? there is some expressions with a variable(WP) in the data! Dictionary? same as Tuple? Text file read? Function?, Module?, Class? How would a Pro approch this ? What I'v been thinking is like this: ... Very basic Brakedown of program: (#=Number Variables, $=String Data) = Which Damage Table? : ... Type .. $(Cutting, Puncture, Bludgeon. Three(3) Types of weapon inflicted wounds) Zone .. #(2d6 dice roll or Number generated by players Attack choice. { A General Body Location.}) Which Row? : Location ... #(1d6 die roll. { A more Specific body location.}) Which Column? : . Wound Level #(Number generated by Wounds Taken.) == WOUND (#$Blood-Loss, #$Shock, #$Pain, $##Wound Description) == Sample of One Zones Data ... ZONE: 14, ARM - # My data is like this: # Roll, Location, # Level One: BL, Shock, Pain, Wound Description # Level Two: BL, Shock, Pain, Wound Description # Level Three:... BL, Shock, Pain, Wound Description # Level Four: BL, Shock, Pain, Wound Description # Level Five: . BL, Shock, Pain, Wound Description 1, Hand 0, 6-WP, 5-WP, Surface graze. May drop anything held in hand. 0, 3, 4-WP, Some flesh (like the palm) and bruised bone. May drop at -3 2, 9-WP, 6-WP, Pierced hand totally. May drop handheld items (at -4) 5, 7, 9-WP, Hit wrist bones (instantly drop whatever may be held in that hand) 9, 8, 9-WP, As previous, a slashed artery or vein 2-3, Forearm 0 5-WP 4-WP, 1 5 6-WP, 2 5 6-WP, 6 7 8-WP, 7 8 9-WP, Grazed Bone chipped (may drop handheld items) As a level two, plus you automatically drop anything held Totally passes through, causing greater blood loss and forcing any item to be dropped As level four, with more blood and some bone damage 4, Elbow 0 6-WP 5-WP, 0 4 6-WP, 3 6 7-WP, 5 8 9-WP, 7 9 11-WP, Glancing blow Solid blow; funnybone effect. May drop items in that hand Torn ligament or similar wound; instantly drop items in that hand Dislocated or otherwise jacked up elbow. Use of arm temporarily lost Shattered elbow. Arm now useless 5-6, Upper arm 0 4-WP 4-WP, 1 3 5-WP, 3 5 6-WP, 5 6 7-WP, 7 7 8-WP, Light laceration Deeper puncture, including torn muscle Serious flesh wound, including torn tendons More serious damage and bleeding, including some bone damage As level four, but with more serious bleeding (a blood vessel was hit) .end sample ... Thanks for any advice or help you can give me! -- Steve Goodman __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I Give Up. - Follow up post
Danny Yoo wrote: >> I tried it by opening a file , but could find no way to do variable >> variables > > > > Hi Brian, > > Look for the concept of dictionaries in Python. "Variable variables" in > languages like PHP and Perl are doable in Python, but a dictionary > usually handles such situations in a safer way. Yeah I am familiar with the concept , and it would work nicely for some applications, but I couldnt make it work the way I need it to for this application :) Reason: It's more to do with the further functions in the application that I did not paste in the follow up :) The issue, again is that I want to call a variable by *name* , but I do not *KNOW* what that variable's name is, exactly For example, say I have apache running on port 80, apache-ssl on 443, apache2 on port 81, and Caudium on port 8080 (yes I do have a machine running not only those servers, but AOLServer, simultaneously) If I want to configure a protocol to check, *for multiple machines* I might do (pseudo function) Check_Host(machine,protocol,port,last_results) Now, if I want to iterate over a list of machines , and check each machine for whatever it was set for (in the config file) I cannot obviously have a config that says (Protocol = Port) http = 80 http = 81 http = 8080 smtp = 25 So the config file requires http to be the first part of the string , followed by something to make it unique (I.E. http1, http2, http3) Now, if I pass each value in turn to check_host, how do I tell what type of check to make ? (Obviously if I want to get a string from a web page, I need to SEND a GET request, but if I am checking smtp, I cannot use that same function, SO ) I check for the base of the configuration (if http in protocol: then run http request, else something else) so that I can run the correct request type on the correct port , and maintain a log, but I cannot do that as effectively if it is a dictionary or list or even a foreach .. As I said, I have looked at this from every angle I could see, but using dictionary (which was my *first* choice) did not work the way I needed it to, nor did simply reading a file to get name value pairs I needed the name to be an actual variable name.. believe me I have tried dictionaries, Ive tried parsing the file by other means, but the only way I could get results I needed was through configparser > >> and then read it in as a file , which builds an array (AKA dictionary) >> and then just assign thus: >> >> foreach ($filearray as $key => $value) >> { >> $$key = $value >> } > > This is not safe in Python, because the $key given could easily be the > name of something that shouldn't be treated as configuration, such as > the built-in functions. It also makes debugging a bit harder if we have > variables in a program that aren't explicitely named. Yes, I am well aware of the dangers of taking values unchecked into an array such as this method shows (simplified).. I rarely ever use it, or if I do, it is on *known* clean data (example, pulling config data out of a database , reading a .ini file, etc.. not untrusted user input) I have seen people use this method to extract POST and GET data for example (untrusted user input) , which I find horrifying :) > > The safe way to do this is to sandbox these variables in a dictionary. > Conceptually, the pseudocode looks like: > > > config_options = {} > for (name, value) in the file: > config_options[name] = value > > > and the real code to do this doesn't look too much different. > Yeah, basically you carry values in a dictionary named by keyname , but.. there have been situations where I need the key name as the variable name , I.E. config_options[name] = value could become name = value as if it was explicitly defined that way It's difficult to grasp or even effectively explain the concept or idea, but sometimes you need to KNOW the key name, *without* knowing the key name :) (confused yet? hahaha) > > > Let's look at some of the config-reading code: > >> if cfg.has_section("Parameters"): >> myparams = cfg.items("Parameters") >> for item in myparams: >> parameter[item[0]] = item[1] >> else: >> log_error("Parameters","not found") >> if cfg.has_section("Ports"): >> ports = cfg.items("Ports") >> for port in ports: >> watch_port[port[0]] = port[1] >> else: >> log_error("Ports","Not Found") >> if cfg.has_section("Hosts"): >> hostnames = cfg.items("Hosts") >> for hostname in hostnames: >> watch_hosts[hostname[0]] = hostname[1] >> else: >> log_error("Hosts","Not Found") >> if cfg.has_section("IP Addresses"): >> ips = cfg.items("IP Addresses") >> for ip in ips: >> watch_ips[ip[0]] = ip[1] >> else: >> log_error("IP Addresses","Not Found") >>
Re: [Tutor] I Give Up. - Follow up post
> I tried it by opening a file , but could find no way to do variable > variables Hi Brian, Look for the concept of dictionaries in Python. "Variable variables" in languages like PHP and Perl are doable in Python, but a dictionary usually handles such situations in a safer way. > and then read it in as a file , which builds an array (AKA dictionary) > and then just assign thus: > > foreach ($filearray as $key => $value) > { > $$key = $value > } This is not safe in Python, because the $key given could easily be the name of something that shouldn't be treated as configuration, such as the built-in functions. It also makes debugging a bit harder if we have variables in a program that aren't explicitely named. The safe way to do this is to sandbox these variables in a dictionary. Conceptually, the pseudocode looks like: config_options = {} for (name, value) in the file: config_options[name] = value and the real code to do this doesn't look too much different. Let's look at some of the config-reading code: > if cfg.has_section("Parameters"): > myparams = cfg.items("Parameters") > for item in myparams: > parameter[item[0]] = item[1] > else: > log_error("Parameters","not found") > if cfg.has_section("Ports"): > ports = cfg.items("Ports") > for port in ports: > watch_port[port[0]] = port[1] > else: > log_error("Ports","Not Found") > if cfg.has_section("Hosts"): > hostnames = cfg.items("Hosts") > for hostname in hostnames: > watch_hosts[hostname[0]] = hostname[1] > else: > log_error("Hosts","Not Found") > if cfg.has_section("IP Addresses"): > ips = cfg.items("IP Addresses") > for ip in ips: > watch_ips[ip[0]] = ip[1] > else: > log_error("IP Addresses","Not Found") > if cfg.has_section("Alerts"): > alerts_to = cfg.items("Alerts") > else: > log_error("Hosts","Not Found") There's a lot of repetition here. When we have the temptation to copy and paste, try to see if a helper function can do some lifting. In fact, there's a bug there because of the copy-and-paste. If there are no Alerts, the system will spuriously blame Hosts. I resent being blamed for things I don't do, and I'm sure my programs feel the same. *grin* Here's a possible refactoring: # def read_config_section(section_name, output_dictionary): if cfg.has_section(section_name) section = cfg.items(section_name) else: log_error(section_name, "Not Found") for key_value in section: output_dictionary[key_value[0]] = key_value[1] # Once we have this, we can then do: read_config_section("Parameters", parameter) read_config_section("Ports", watch_port) ... and eliminate a lot of that repetitive code. Does this make sense? Feel free to ask more questions. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I Give Up. - Follow up post
OK see, what I was doing originally (and I found time to finally get it partially working) I have a configuration file that may be edited by webmaster thus [Ports] http = 80 https = 443 http1 = 81 smtp = 25 smtp2 = 587 (the above is a small example, it could be *anything* ) Now I have a function that takes machine, protocol, and a dictionary of last alerts (would be empty if none) The whole is a small multiple machine monitoring script, By the way... The configuration file needed to be read by config parser and assign the config name as if it were a variable, and the value along with it I tried it by opening a file , but could find no way to do variable variables (In PHP, you can do something to the effect of: name = value name1 = value1 and then read it in as a file , which builds an array (AKA dictionary) and then just assign thus: foreach ($filearray as $key => $value) { $$key = $value } in python I could see no way to do so, so I ended up with configparser, which I *finally* got working - working code below I am still working on this , as I wanna do it in python (currently operational in Perl , and running as a daemon, which I want this code to do eventually) So, the below is a working example of reading a config file in python using configparser Also note: I found that ConfigParser != configparser (it is case sensitive) Just wanted to post this back in case anyone else needs to figure out how to use configparser in python. import ConfigParser cfg = ConfigParser.SafeConfigParser() cfg.read("/home/brian/pymon.cfg") if cfg.has_section("Parameters"): myparams = cfg.items("Parameters") for item in myparams: parameter[item[0]] = item[1] else: log_error("Parameters","not found") if cfg.has_section("Ports"): ports = cfg.items("Ports") for port in ports: watch_port[port[0]] = port[1] else: log_error("Ports","Not Found") if cfg.has_section("Hosts"): hostnames = cfg.items("Hosts") for hostname in hostnames: watch_hosts[hostname[0]] = hostname[1] else: log_error("Hosts","Not Found") if cfg.has_section("IP Addresses"): ips = cfg.items("IP Addresses") for ip in ips: watch_ips[ip[0]] = ip[1] else: log_error("IP Addresses","Not Found") if cfg.has_section("Alerts"): alerts_to = cfg.items("Alerts") else: log_error("Hosts","Not Found") print parameter print watch_port print watch_hosts print watch_ips print alerts_to ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Correct way to code data or parse?
Hi Everyone I'm new to python and I want to write a rpg program that is able to use theCorrect way to code data or parse? I have pages of .PDF file Data tables copied to text files. I check out many python boards & Books with little illumination to my problem. I'm still not sure about what to use for this data listing: Lists?, Tuple? there is some expressions with a variable(WP) in the data! Dictionary? same as Tuple? Text file read? Function?, Module?, Class? How would a Pro approch this ? What I'v been thinking is like this: ... Very basic Brakedown of program: (#=Number Variables, $=String Data) = Which Damage Table? : ... Type .. $(Cutting, Puncture, Bludgeon. Three(3) Types of weapon inflicted wounds) Zone .. #(2d6 dice roll or Number generated by players Attack choice. { A General Body Location.})Which Row? : Location ... #(1d6 die roll. { A more Specific body location.})Which Column? : . Wound Level #(Number generated by Wounds Taken.) == WOUND (#$Blood-Loss, #$Shock, #$Pain, $##Wound Description) == Sample of One Zones Data ... ZONE: 14, ARM - # My data is like this: # Roll, Location, # Level One: BL, Shock, Pain, Wound Description # Level Two: BL, Shock, Pain, Wound Description # Level Three:... BL, Shock, Pain, Wound Description # Level Four: BL, Shock, Pain, Wound Description # Level Five: . BL, Shock, Pain, Wound Description 1, Hand0, 6-WP, 5-WP, Surface graze. May drop anything held in hand.0, 3, 4-WP, Some flesh (like the palm) and bruised bone. May drop at -32, 9-WP, 6-WP, Pierced hand totally. May drop handheld items (at -4)5, 7, 9-WP, Hit wrist bones (instantly drop whatever may be held in that hand)9, 8, 9-WP, As previous, a slashed artery or vein 2-3, Forearm0 5-WP 4-WP,1 5 6-WP,2 5 6-WP,6 7 8-WP,7 8 9-WP,Grazed Bone chipped (may drop handheld items)As a level two, plus you automatically drop anything heldTotally passes through, causing greater blood loss and forcing any item to be droppedAs level four, with more blood and some bone damage 4, Elbow0 6-WP 5-WP,0 4 6-WP,3 6 7-WP,5 8 9-WP,7 9 11-WP,Glancing blowSolid blow; funnybone effect. May drop items in that handTorn ligament or similar wound; instantly drop items in that handDislocated or otherwise jacked up elbow. Use of arm temporarily lostShattered elbow. Arm now useless 5-6, Upper arm0 4-WP 4-WP,1 3 5-WP,3 5 6-WP,5 6 7-WP,7 7 8-WP,Light lacerationDeeper puncture, including torn muscleSerious flesh wound, including torn tendonsMore serious damage and bleeding, including some bone damageAs level four, but with more serious bleeding (a blood vessel was hit) .end sample ... Thanks for any advice or help you can give me!--Steve Goodman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] stfftime question
Tom Tucker wrote: > I found a temporary solution. The goal in the end was to compare two > dates/times and retrieve the millisecond delta between the two. > > Work around > # > import datetime > import time > t1 = datetime.datetime(1973,9,4,04,3,25,453) > t2 = datetime.datetime(1973,9,4,04,3,25,553) > t1tuple = time.mktime(t1.timetuple())+(t1.microsecond/1000.) > t2tuple = time.mktime(t2.timetuple())+(t2.microsecond/1000.) > delta = (t2tuple - t1tuple) * 1000 > print delta You could also subtract the datetimes directly to get a timedelta: In [13]: t1 = datetime.datetime(1973,9,4,04,3,25,453) In [14]: t2 = datetime.datetime(1973,9,4,04,3,25,553) In [15]: diff = t2-t1 In [16]: diff Out[16]: datetime.timedelta(0, 0, 100) In [17]: diff.microseconds Out[17]: 100 or if the diff can be bigger use ((diff.days * 24*60*60) * diff.seconds) * 1000 + diff.microseconds > On 7/4/06, Tom Tucker <[EMAIL PROTECTED]> wrote: >> Below is an example of me converting a datetime to milliseconds on a >> Mac running Pythong 2.3.5. The same working code on a Solaris system >> with Python 2.3.2 fails. Any thoughts? What arguments am I missing? >> >> >> >> From my Mac >> # >> Python 2.3.5 (#1, Oct 5 2005, 11:07:27) >> [GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin >> Type "help", "copyright", "credits" or "license" for more information. > import datetime > dtstr = datetime.datetime(1973,9,4,04,3,25,453) > output = dtstr.strftime('%s.%%03d') % (dtstr.microsecond) > print output >> 115977805.453 I think you want '%S.%%03d' as the format string (uppercase S). %s is not a standard format and it is probably handled differently on Mac OS and Solaris. What is the result of dtstr.strftime('%s.%%03d') on each machine? On Windows I get In [11]: dtstr.strftime('%s.%%03d') Out[11]: '.%03d' Perhaps Solaris just passes the unknown format to output, that would give the error you see. Kent >> >> >> From Work (Solaris) >> >> Python 2.3.2 (#1, Nov 17 2003, 22:32:28) >> [GCC 2.95.3 20010315 (release)] on sunos5 >> Type "help", "copyright", "credits" or "license" for more information. > import datetime > dtstr = datetime.datetime(1973,9,4,04,3,25,453) > output = dtstr.strftime('%s.%%03d') % (dtstr.microsecond) >> Traceback (most recent call last): >> File "", line 1, in ? >> TypeError: not enough arguments for format string > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Query regarding Unittest module behaviour
[snip] > Script abc.py imports xyz.py. > Now when I execute abc.py from commandlline all unittestcases of > xyz.py are also executed. > Why is this happening and what can be the solution to this. anything that's in the global scope of an imported module gets executed. For example... -- a.py print "hello" >>> import a This will cause the "hello" to be printed from 'a.py' because it's in the global scope. so what you want to do is something like this: -a.py def main(): print "hello" if __name__ == "__main__": main() --- Now if you do >>> import a nothing will be printed. But, if you do "python a.py" on command line, you'll see "hello" be printed. I think this is the problem you're having. HTH, -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Moved Python installation. Need advice rebuilding all *.pyc
On 03/07/06, Andreas <[EMAIL PROTECTED]> wrote: On 03.07.2006 20:45 Adam wrote> Erm trying to remember exactly how to do this in windows but if you do the> equivalent of an rm -r E:\Python24\site-packages\*.pyc ie remove all the> *.pyc files they will be rebuilt as and when you next import those modules. Thanks for the info. However, if this is being done automatically onimport, why does "python setup.py install" often do some compilation ?It reduces the loading time for the modules if there is already a *.pyc so the setup script often does it in advance. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] stfftime question
I found a temporary solution. The goal in the end was to compare two dates/times and retrieve the millisecond delta between the two. Work around # import datetime import time t1 = datetime.datetime(1973,9,4,04,3,25,453) t2 = datetime.datetime(1973,9,4,04,3,25,553) t1tuple = time.mktime(t1.timetuple())+(t1.microsecond/1000.) t2tuple = time.mktime(t2.timetuple())+(t2.microsecond/1000.) delta = (t2tuple - t1tuple) * 1000 print delta On 7/4/06, Tom Tucker <[EMAIL PROTECTED]> wrote: > Below is an example of me converting a datetime to milliseconds on a > Mac running Pythong 2.3.5. The same working code on a Solaris system > with Python 2.3.2 fails. Any thoughts? What arguments am I missing? > > > > From my Mac > # > Python 2.3.5 (#1, Oct 5 2005, 11:07:27) > [GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin > Type "help", "copyright", "credits" or "license" for more information. > >>> import datetime > >>> dtstr = datetime.datetime(1973,9,4,04,3,25,453) > >>> output = dtstr.strftime('%s.%%03d') % (dtstr.microsecond) > >>> print output > 115977805.453 > > > From Work (Solaris) > > Python 2.3.2 (#1, Nov 17 2003, 22:32:28) > [GCC 2.95.3 20010315 (release)] on sunos5 > Type "help", "copyright", "credits" or "license" for more information. > >>> import datetime > >>> dtstr = datetime.datetime(1973,9,4,04,3,25,453) > >>> output = dtstr.strftime('%s.%%03d') % (dtstr.microsecond) > Traceback (most recent call last): > File "", line 1, in ? > TypeError: not enough arguments for format string > >>> > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Query regarding Unittest module behaviour
Hi,I have 2 scripts , abc.py and xyz.py, In both I have made unittest cases using the unittest module.Script abc.py imports xyz.py.Now when I execute abc.py from commandlline all unittestcases of xyz.py are also executed. Why is this happening and what can be the solution to this.Also I have tried commenting the unittest.main() in xyz.py, but all in vain.Please helpAkanksha __Do You Yahoo!?Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] stfftime question
Below is an example of me converting a datetime to milliseconds on a Mac running Pythong 2.3.5. The same working code on a Solaris system with Python 2.3.2 fails. Any thoughts? What arguments am I missing? >From my Mac # Python 2.3.5 (#1, Oct 5 2005, 11:07:27) [GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import datetime >>> dtstr = datetime.datetime(1973,9,4,04,3,25,453) >>> output = dtstr.strftime('%s.%%03d') % (dtstr.microsecond) >>> print output 115977805.453 >From Work (Solaris) Python 2.3.2 (#1, Nov 17 2003, 22:32:28) [GCC 2.95.3 20010315 (release)] on sunos5 Type "help", "copyright", "credits" or "license" for more information. >>> import datetime >>> dtstr = datetime.datetime(1973,9,4,04,3,25,453) >>> output = dtstr.strftime('%s.%%03d') % (dtstr.microsecond) Traceback (most recent call last): File "", line 1, in ? TypeError: not enough arguments for format string >>> ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor