Re: [Tutor] Need a better name for this function
Richard D. Moores wrote: On Wed, Dec 16, 2009 at 20:23, Dave Angel wrote: Richard D. Moores wrote: There are conceivably better ways to get at the mantissa of the fp number, but you can simply parse the hex digits as I did manually, and add one and subtract one from the given mantissa (the part between the decimal point and the 'p'). Then it just remains to figure out which two of those three values actually span the desired value. Using the numbers and strings you supply, the three values would be: 0x1.bd70a3d70a3d6p-2 0x1.bd70a3d70a3d7p-2 0x1.bd70a3d70a3d8p-2 and the second one is somewhat less than .435, while the 3rd is more. Now, while this is good enough to do by hand, you have to realize there are some places it may not work, and another where it won't. Dave, I was hoping to find a way to NOT do it by hand, for the simple cases such as 0x1.bd70a3d70a3d7p-2 . I'm weak on hex arithmetic. For these simple cases, is there a general way to "add" something to the last digit of a hex value to bump it up and down by 1? After I can do that, I'll try to deal with the cases you mention below. Dick I'm not sure whether trailing zeroes are always provided in the hex() method. So you may have to pad it out before adjusting the last digit. I'm also not positive how it normalizes the hex value printed out. As the example in the help indicates, there are more than one hex string that can be converted to the same float - if you denormalize, it'll still convert it. I just don't know if hex() ever produces a non-normalized value. More drastically, if you're trying to be complete, is the infinities, the NAN values, and the numbers, *very* close to zero, where gradual underflow takes effect. The notion here is that when the exponent gets as negative as it can safely store, the standard requires that the number be stored denormalized, rather than going immediately to zero. I don't know how those values are represented by the hex() method, but they have fewer significant digits, so the adjustment you would need would be in a different column. If I had to write a function to do what you ask, and if I couldn't get better specs as to what Python is actually doing with the hex() method, I'd work out an algorithm where tweak what seems to be the bottom digit, then see if the float has a new value. If it does not, I'm working on the wrong column. DaveA Try the following in Python 3.1: def increment(floatval, incr=1): #Given a float of "reasonable" size, increment it by smallest amount # and if incr is -1, then decrement stringval = floatval.hex() mantissa, exponent = stringval.split("p") mantissa = mantissa.replace(".", "") #ignore the period mantissa = hex(int(mantissa, 16) + incr) newstringval = mantissa[:3] + "." + mantissa[3:] + "p" + exponent newfloatval = float.fromhex(newstringval) #print(floatval, newstringval, newfloatval) return newfloatval You can specify an increment of +1 or -1, but larger values also work just as well. From limited testing, this works for any positive values that aren't in the gradual underflow range. The parsing and reassembly of the mantissa and exponent are pretty sloppy, but maybe they are even correct, for the output of the hex() method. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to run two os.system commands simultaneously
Hi, I have two computationally intensive commands that I would like to render simultaneously on two fifferent computers. When I run these two commands, os.system(theTerminalCommandMacPro4) os.system(theTerminalCommandMacPro1) the second command doesn't begin until the first command has finished rendering. Is there a way to send them both off at the same time? Pete ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to see a terminal window showing progress of os.system
Hi I am sending commands to the command line using python's os.system. Is there a way to see a terminal window showing the progress of os.system as if you had just run the command from a normal terminal window? As it is now it runs completely in the background ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run two os.system commands simultaneously
On Thu, Dec 17, 2009 at 9:29 AM, pedro wrote: > Hi, I have two computationally intensive commands that I would like to > render simultaneously on two fifferent computers. When I run these two > commands, > > os.system(theTerminalCommandMacPro4) > os.system(theTerminalCommandMacPro1) > > > the second command doesn't begin until the first command has finished > rendering. Is there a way to send them both off at the same time? Use subprocess.Popen() to start the external process asynchronously: http://docs.python.org/library/subprocess.html#replacing-os-system Or call os.system() in a thread. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run two os.system commands simultaneously
On 2009-12-17 09:52:34 -0500, Kent Johnson said: call os.system() in a thread Hi Kent, pardon my ignorance but what do you mean by call os.system() in a thread? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need a better name for this function
On Thu, Dec 17, 2009 at 02:13, Dave Angel wrote: > Try the following in Python 3.1: > > def increment(floatval, incr=1): > #Given a float of "reasonable" size, increment it by smallest amount > # and if incr is -1, then decrement > stringval = floatval.hex() > mantissa, exponent = stringval.split("p") > mantissa = mantissa.replace(".", "") #ignore the period > mantissa = hex(int(mantissa, 16) + incr) > newstringval = mantissa[:3] + "." + mantissa[3:] + "p" + exponent > newfloatval = float.fromhex(newstringval) > #print(floatval, newstringval, newfloatval) > return newfloatval > > > You can specify an increment of +1 or -1, but larger values also work just > as well. From limited testing, this works for any positive values that > aren't in the gradual underflow range. > > The parsing and reassembly of the mantissa and exponent are pretty sloppy, > but maybe they are even correct, for the output of the hex() method. > > DaveA Thanks very much Dave. Enlightening. Dick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run two os.system commands simultaneously
On Thu, Dec 17, 2009 at 10:14 AM, pedro wrote: > On 2009-12-17 09:52:34 -0500, Kent Johnson said: > >> call os.system() in a thread > > Hi Kent, pardon my ignorance but what do you mean by call os.system() in a > thread? Your basic problem is that os.system() blocks - it waits until the new process completes before continuing execution of your code. You don't want to block - you want to be able to start the next process before the first one completes. One way to convert a blocking operation to a non-blocking one is to start a new thread and run the blocking operation in the second thread. Then the main program can continue, only the new thread is blocked. I think my first suggestion is simpler, though, especially if you don't know what threads are. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Please take a look at this function
def prestrings2list(a_str): word = "" list_of_strings = [] length_of_a_string = len(a_str) for i, char in enumerate(a_str): if i == length_of_a_string - 1: word += char word = word.rstrip() list_of_strings.append(word) elif char == ",": word = word.strip() list_of_strings.append(word) word = "" elif char != " ": word += char elif char == " " and word != "" and a_str[i - 1] != " ": word += char return list_of_strings The idea for this came from looking at the interests of bloggers on a blogging site. Some have a great many interests listed -- so many that I thought it would handy to have a function that could put them in a Python list so they could be counted, sorted, dupes weeded out, etc. For example, here's a shorter one that I copied and pasted, modified, and then pasted again into a pair of quotes, thereby creating one long Python string: a_str = "blender , synthetic DNA, myrmecology, fungi, quorum sensing, theoretical physic's, reason, love, hope, virtual reality, google operating system, space, life, mystery, truth's, universe, immortality, strangeness, fun ,living, hope, eternity, knowledge, Egyptian secrets of the dead, n-space, hyper-time , theory of everything, light, nuclear theory, particle theory, myrmec, self replicating RNA, MMOG, MMOR%PG, symbiosis,Black's Plague, selddir, Da Vinci, Newton, Archimedes, Cantor7, Leibnitz,myrmecology" prestrings2list(a_str) returns ['blender', 'synthetic DNA', 'myrmecology', 'fungi', 'quorum sensing', "theoretical physic's", 'reason', 'love', 'hope', 'virtual reality', 'google operating system', 'space', 'life', 'mystery', "truth's", 'universe', 'immortality', 'strangeness', 'fun', 'living', 'hope', 'eternity', 'knowledge', 'Egyptian secrets of the dead', 'n-space', 'hyper-time', 'theory of everything', 'light', 'nuclear theory', 'particle theory', 'myrmec', 'self replicating RNA', 'MMOG', 'MMOR%PG', 'symbiosis', "Black's Plague", 'selddir', 'Da Vinci', 'Newton', 'Archimedes', 'Cantor7', 'Leibnitz', 'myrmecology'] This is exactly what I wanted, but please tell me if the function name makes any sense, and if the function could be made to conform better to standard Python practice. And what a good docstring for it might be. I've assumed that the items in these strings will be separated by commas. But there could be some using semicolons instead. That revision will be easy to make. Thanks, Tutors, Dick Moores ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to see a terminal window showing progress of os.system
"pedro" wrote Hi I am sending commands to the command line using python's os.system. Is there a way to see a terminal window showing the progress of os.system as if you had just run the command from a normal terminal window? As it is now it runs completely in the background You can sometimes launch a terminal with your program running in it but thats usually not the best way to do it. Normally you would use the subprocess module and the Popen class to capture the output of the command and either monitor it for some event or display it within your own program. That looks more professional and gives you much more control You will find examples of using subprocess in the Using the OS topic of my tutorial. 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] Please take a look at this function
On Thu, Dec 17, 2009 at 7:57 PM, Richard D. Moores wrote: > def prestrings2list(a_str): > word = "" > list_of_strings = [] > length_of_a_string = len(a_str) > for i, char in enumerate(a_str): > if i == length_of_a_string - 1: > word += char > word = word.rstrip() > list_of_strings.append(word) > elif char == ",": > word = word.strip() > list_of_strings.append(word) > word = "" > elif char != " ": > word += char > elif char == " " and word != "" and a_str[i - 1] != " ": > word += char > return list_of_strings I think you want def prestrings2list(a_str): return [i.strip() for i in a_str.split(',')] Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Please take a look at this function
On Thu, Dec 17, 2009 at 18:26, Kent Johnson wrote: > > On Thu, Dec 17, 2009 at 7:57 PM, Richard D. Moores wrote: > > def prestrings2list(a_str): > > word = "" > > list_of_strings = [] > > length_of_a_string = len(a_str) > > for i, char in enumerate(a_str): > > if i == length_of_a_string - 1: > > word += char > > word = word.rstrip() > > list_of_strings.append(word) > > elif char == ",": > > word = word.strip() > > list_of_strings.append(word) > > word = "" > > elif char != " ": > > word += char > > elif char == " " and word != "" and a_str[i - 1] != " ": > > word += char > > return list_of_strings > > I think you want > def prestrings2list(a_str): > return [i.strip() for i in a_str.split(',')] Wow, Kent! Perfect except for the extra interior spaces in items. E.g., 'synthetic DNA'. Could you fix that? Thanks, Dick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Please take a look at this function
Richard D. Moores wrote: def prestrings2list(a_str): word = "" list_of_strings = [] length_of_a_string = len(a_str) for i, char in enumerate(a_str): if i == length_of_a_string - 1: word += char word = word.rstrip() list_of_strings.append(word) elif char == ",": word = word.strip() list_of_strings.append(word) word = "" elif char != " ": word += char elif char == " " and word != "" and a_str[i - 1] != " ": word += char return list_of_strings The idea for this came from looking at the interests of bloggers on a blogging site. Some have a great many interests listed -- so many that I thought it would handy to have a function that could put them in a Python list so they could be counted, sorted, dupes weeded out, etc. For example, here's a shorter one that I copied and pasted, modified, and then pasted again into a pair of quotes, thereby creating one long Python string: a_str = "blender , synthetic DNA, myrmecology, fungi, quorum sensing, theoretical physic's, reason, love, hope, virtual reality, google operating system, space, life, mystery, truth's, universe, immortality, strangeness, fun ,living, hope, eternity, knowledge, Egyptian secrets of the dead, n-space, hyper-time , theory of everything, light, nuclear theory, particle theory, myrmec, self replicating RNA, MMOG, MMOR%PG, symbiosis,Black's Plague, selddir, Da Vinci, Newton, Archimedes, Cantor7, Leibnitz,myrmecology" prestrings2list(a_str) returns ['blender', 'synthetic DNA', 'myrmecology', 'fungi', 'quorum sensing', "theoretical physic's", 'reason', 'love', 'hope', 'virtual reality', 'google operating system', 'space', 'life', 'mystery', "truth's", 'universe', 'immortality', 'strangeness', 'fun', 'living', 'hope', 'eternity', 'knowledge', 'Egyptian secrets of the dead', 'n-space', 'hyper-time', 'theory of everything', 'light', 'nuclear theory', 'particle theory', 'myrmec', 'self replicating RNA', 'MMOG', 'MMOR%PG', 'symbiosis', "Black's Plague", 'selddir', 'Da Vinci', 'Newton', 'Archimedes', 'Cantor7', 'Leibnitz', 'myrmecology'] This is exactly what I wanted, but please tell me if the function name makes any sense, and if the function could be made to conform better to standard Python practice. And what a good docstring for it might be. I've assumed that the items in these strings will be separated by commas. But there could be some using semicolons instead. That revision will be easy to make. Thanks, Tutors, Dick Moores Seems to me the function would be much simpler using the string method .split() Something like: def myfunc(a_str): list_of_strings = a_str.split(",") return list_of_strings Now, if you want to also allow semicolons, you could pre-translate any semicolons to commas (using replace()). And if you need to strip leading and trailing whitespace, you could do a separate pass over the list. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Please take a look at this function
On Fri, Dec 18, 2009 at 3:44 AM, Richard D. Moores wrote: > On Thu, Dec 17, 2009 at 18:26, Kent Johnson wrote: >> >> On Thu, Dec 17, 2009 at 7:57 PM, Richard D. Moores >> wrote: >> > def prestrings2list(a_str): >> > word = "" >> > list_of_strings = [] >> > length_of_a_string = len(a_str) >> > for i, char in enumerate(a_str): >> > if i == length_of_a_string - 1: >> > word += char >> > word = word.rstrip() >> > list_of_strings.append(word) >> > elif char == ",": >> > word = word.strip() >> > list_of_strings.append(word) >> > word = "" >> > elif char != " ": >> > word += char >> > elif char == " " and word != "" and a_str[i - 1] != " ": >> > word += char >> > return list_of_strings >> >> I think you want >> def prestrings2list(a_str): >> return [i.strip() for i in a_str.split(',')] > > Wow, Kent! Perfect except for the extra interior spaces in items. > E.g., 'synthetic DNA'. Could you fix that? Probably easiest to use a regular expression to fix that particular thing, as in: import re mult_space = re.compile(r'\s+') def prestrings2list(a_str): return [re.sub(mult_space, ' ', x).strip() for x in a_str.split(',')] Hugo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Please take a look at this function
On Thu, Dec 17, 2009 at 19:49, Hugo Arts wrote: > Probably easiest to use a regular expression to fix that particular > thing, as in: > > import re > mult_space = re.compile(r'\s+') > def prestrings2list(a_str): >return [re.sub(mult_space, ' ', x).strip() for x in a_str.split(',')] > > Hugo That's perfect, Hugo! a_str = "blender , syntheticDNA, myrmecology, fungi, quorum sensing, theoretical physic's, reason, love, hope, virtual reality, google operating system, space, life, mystery, truth's, universe, immortality, strangeness, fun ,living, hope, eternity, knowledge, Egyptian secrets of the dead, n-space, hyper-time , theory of everything, light, nuclear theory, particle theory, myrmec, self replicating RNA, MMOG, MMOR%PG, symbiosis,Black's Plague, selddir, Da Vinci, Newton, Archimedes, Cantor7, Leibnitz, myrmecology" def prestrings2list(a_str): import re mult_space = re.compile(r'\s+') return [re.sub(mult_space, ' ', x).strip() for x in a_str.split(',')] lst = prestrings2list(a_str) print(lst) = OUTPUT ['blender', 'synthetic DNA', 'myrmecology', 'fungi', 'quorum sensing', "theoretical physic's", 'reason', 'love', 'hope', 'virtual reality', 'google operating system', 'space', 'life', 'mystery', "truth's", 'universe', 'immortality', 'strangeness', 'fun', 'living', 'hope', 'eternity', 'knowledge', 'Egyptian secrets of the dead', 'n-space', 'hyper-time', 'theory of everything', 'light', 'nuclear theory', 'particle theory', 'myrmec', 'self replicating RNA', 'MMOG', 'MMOR%PG', 'symbiosis', "Black's Plague", 'selddir', 'Da Vinci', 'Newton', 'Archimedes', 'Cantor7', 'Leibnitz', 'myrmecology'] Now for a better function name, and a proper docstring. Anyone? Dick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] What is URL to online Python interpreter?
What is URL to online Python interpreter? Ben ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Generating Unique Permutations
I'm just a philosophy teacher, and I don't know much about mathematics or computers. I'm writing a python program (not the best language for this topic, but it is what I know), and I need to solve a problem which requires more knowledge than I have. I'm hoping you can help me. =) I'm looking for an efficient way to create all the unique, non-duplicated permutations of a list (I believe these are called "necklaces" in combinatorics). I need to do this without actually generating every possible permutation (which includes all the duplicates). For example: List = [a,a,b,b] My output would be something like: a,a,b,b a,b,a,b a,b,b,a b,a,a,b b,a,b,a b,b,a,a Importantly, you'll see that these are only generated once. There are four permutations which can be generated from the list which all look like (a,a,b,b), but I only want to generate this output a single time. My problem is large enough that I can't feasibly generate all the permutations (60! I think) and eliminate the duplicates from there, but I could feasibly generate the unique ones (a much small search space), especially if I use an efficient algorithm (I'm sorry to focus so much on efficiency, but it matters). What is the best way to do this? If you don't know how it would work in Python, can you explain in psuedocode? As I said, I'm not very smart about these things, so treat like a child to the topic (because I am a child to the topic, an interested child!). Oh, and thanks for this mailing/reading list! I spend countless hours browsing and reading other people's code. It is a lot of fun =). Sincerely, Michael ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generating Unique Permutations
create your desired list >>> l = ['a', 'a', 'b', 'b'] do a permutation and take unique values >> import itertools >> set(itertools.permutations(l)) HTH On Fri, Dec 18, 2009 at 11:24 AM, Michael Morrissey wrote: > I'm just a philosophy teacher, and I don't know much about mathematics or > computers. I'm writing a python program (not the best language for this > topic, but it is what I know), and I need to solve a problem which requires > more knowledge than I have. I'm hoping you can help me. =) > > I'm looking for an efficient way to create all the unique, non-duplicated > permutations of a list (I believe these are called "necklaces" in > combinatorics). I need to do this without actually generating every possible > permutation (which includes all the duplicates). > > For example: > > List = [a,a,b,b] > > My output would be something like: > > a,a,b,b > a,b,a,b > a,b,b,a > b,a,a,b > b,a,b,a > b,b,a,a > > Importantly, you'll see that these are only generated once. There are four > permutations which can be generated from the list which all look like > (a,a,b,b), but I only want to generate this output a single time. > > My problem is large enough that I can't feasibly generate all the > permutations (60! I think) and eliminate the duplicates from there, but I > could feasibly generate the unique ones (a much small search space), > especially if I use an efficient algorithm (I'm sorry to focus so much on > efficiency, but it matters). > > What is the best way to do this? If you don't know how it would work in > Python, can you explain in psuedocode? As I said, I'm not very smart about > these things, so treat like a child to the topic (because I am a child to > the topic, an interested child!). > > Oh, and thanks for this mailing/reading list! I spend countless hours > browsing and reading other people's code. It is a lot of fun =). > > > > > Sincerely, > Michael > > > > > ___ > 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] What is URL to online Python interpreter?
On Thu, Dec 17, 2009 at 09:32:44PM -0800, Benjamin Castillo wrote: > What is URL to online Python interpreter? Google could have helped you too. Anyways, http://shell.appspot.com/ -- Senthil ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor