ANN: mpmath 0.12 released
Hi all, Mpmath version 0.12 is now available from the website: http://code.google.com/p/mpmath/ It can also be downloaded from the Python Package Index: http://pypi.python.org/pypi/mpmath/0.12 Mpmath is a pure-Python library for arbitrary-precision floating-point arithmetic that implements an extensive set of mathematical functions. It can be used as a standalone library or via SymPy (http://code.google.com/p/sympy/). Version 0.12 mostly contains bug fixes and speed improvements. New features include various special functions from analytic number theory, Newton's method as an option for root-finding, and more versatile printing of intervals. It is now also possible to create multiple working contexts each with its own precision. Finally, mpmath now recognizes being installed in Sage and will automatically wrap Sage's fast integer arithmetic if available. For more details, see the changelog: http://mpmath.googlecode.com/svn/tags/0.12/CHANGES Bug reports and other comments are welcome at the issue tracker at http://code.google.com/p/mpmath/issues/list or the mpmath mailing list: http://groups.google.com/group/mpmath Fredrik -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html
python-colormath 1.0.2 Released
Greetings, python-colormath 1.0.2 has been released, which addresses an issue with chromatic adaptations. What is python-colormath? python-colormath is a developer-oriented module that abstracts a number of color math operations behind a small set of classes representing color spaces (IE: RGB, CIE Lab, XYZ, and LCH, etc.). Color conversions, delta E comparisons, and density calculations are all relatively involved, but are hid behind the simple API. For example, conversions from RGB to CMY and CMYK are trivial, while conversions from Spectral to LCHab are equally so (even though much more math happens behind the scenes). What's new in 1.0.2? === * Conversions between all known illuminants is now possible with several different chromatic adaptation transforms (Von Kries, Bradford, and XYZ Scaling). Previously this was only possible for some well-known CIE illuminants. * Chromatic adaptations may be _slightly_ more accurate (we're talking miniscule) on certain hardware. Where is python-colormath? = Getting Started/Website/Development: http://code.google.com/p/python-colormath/ Documentation: http://code.google.com/p/python-colormath/w/list Download: http://pypi.python.org/pypi/colormath/ Support: supp...@l11solutions.com http://code.google.com/p/python-colormath/issues/list -- Gregory Taylor -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html
Re: random number including 1 - i.e. [0,1]
Steven D'Aprano wrote: On Tue, 09 Jun 2009 18:28:23 -0700, John Yeung wrote: The docs are now... sort of correct. For some values of a and b, uniform() can never return b. Notably, I believe uniform(0, 1) is equivalent to random(), and will never return 1. However, uniform(1, 2) CAN return 2, if this is any indication: a=0.0 b=1.0 a+(b-a)*z b True a=1.0 b=2.0 a+(b-a)*z b False But you haven't shown what value z has, so there's no way of interpreting that example. I'd say that uniform(1, 2) should NOT return 2, at least for systems which round correctly. Given a random z such that: 0 = z 1 and two floats a, b such that: a b (b is strictly the larger of the two) then: 0 = z 1 Multiply all sides by (b-a): 0 = (b-a)*z (b-a) Add a to all sides: a = a + (b-a)*z b Hence uniform(a, b) should always return a result less than b, and greater or equal to a. However, there is one proviso: floats are not reals. The above holds true for real numbers, but floats are subject to weird rounding effects. That means that there may be weird edge cases where Bad Things happen and things cancel catastrophically or round incorrectly. A realistic edge case is that a + (b-a) doesn't always give b: a = -1e90 b = 1.0 a b True a + (b-a) == b False a + (b-a) 0.0 However, even in this case, it merely narrows the range of possible results, it doesn't widen it. Did you try to find the edge case for z ? For the following, I'm using Python 2.6.2, running on XP, on a Pentium Core Duo. I figure the highest theoretical value that random.random() should return is a number just under 1.0 The easiest way to generate that value is using the fromhex() method of float. z = float.fromhex(0x1.fp-1) z 0.99989 z1.0 True z2 = 1.0 + z z2 2.0 z2 2.0 False -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On Jun 9, 11:28�pm, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Tue, 09 Jun 2009 21:04:49 -0700, Mensanator wrote: On Jun 9, 8:28 pm, John Yeung gallium.arsen...@gmail.com wrote: On Jun 9, 8:45 pm, Mensanator mensana...@aol.com wrote: On Jun 9, 6:05 pm, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: py a+(b-a)*z b # the expression used for uniform(a,b) False py a+(b-a)*z 11.0 What you do with the number after it's created is not random's concern. Mensanator, you missed Gabriel's point. What he's saying is that, effectively, random.uniform(a, b) returns a + (b - a) * random.random (). So z may not be random()'s concern, but it very much is uniform ()'s concern. The docs are already updated to reflect this:http://svn.python.org/view/python/trunk/Doc/library/ random.rst?r1=687... The docs are now wrong. Why would they do that? The docs are now... sort of correct. I'm not actually disputing that. Funny, saying The docs are now wrong sure sounds like you're disputing that they're correct to me! Those statements were made over two hours apart. Things can change in two hours. I'm simply puzzled why this issue was swept under the rug by pretending it's supposed to work that way. I'm completely confused. What is this issue, [0,1) vs. [0,1] and why are we pretending that it's supposed to work that way? By changing the documentation without explaining why. If the intention was [0,1), as implied by the 2.6.1 docs, but wasn't actually doing that, it should be so stated. This isn't simply fixing a typo. Yes, I've read the thread. I still have no idea what you are complaining about. According to your reply to John Young, you know exactly what the problem is. We're not children here, you can explain that what is supposed to work in theory sometimes has problems in practice. We're not all going to abandon Python and run out and buy Mathematica. Look at how the change of random to the Mersenne Twister was handled. That's what we, the users, want to see. Speak for yourself. What about the change that you think we, the users, want to see? Things like this: Python uses the Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937-1. The underlying implementation in C is both fast and threadsafe. The Mersenne Twister is one of the most extensively tested random number generators in existence. Is that strictly necessary in the random documentation that defines what the random module does? _I_ appreciate it being there and am glad someone made the effort to put it there. If there is a change from a=Nb to a=N=b I would like to see that pointed out also, even if it isn't strictly part of the definition of what the function does. Otherwise, it creates endless confusion. Not as confused as this discussion. I thought you said you read the thread? -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: graph edge generators
On Jun 9, 11:58 pm, William Clifford mr.william.cliff...@gmail.com wrote: I've become interested in basic graphs and networks and I'm wondering about what algorithms are there for generating basic regular graphs like the simplex graph or dodecahedron graph, etc (I'm sure there are many). I'm particularly keen on understanding the very basic functions for determining edges in the graphs. If one didn't want the complete graph but just a function generates the edges connected to a given node. I've been surfing around for this sort of info, but I'm having trouble finding stuff at my level. If anyone knows of any resources or tutorials or that sort of thing, I'd like to hear about those too. Thanks! -- William Clifford Depending on how much of a basis you have in CS, you may want to take a look at http://www.amazon.com/Combinatorial-Algorithms-Enumeration-Mathematics-Applications/dp/084933988X which I found to be an excellent book that covers a lot of the ground you're talking about. Also, check out graphine (graphine.org)- I think its a pretty easy-to-use graph package for python, although as the primary author I'm pretty biased. Geremy Condra -- http://mail.python.org/mailman/listinfo/python-list
Re: How to insert string in each match using RegEx iterator
504cr...@gmail.com wrote: By what method would a string be inserted at each instance of a RegEx match? For example: string = '123 abc 456 def 789 ghi' newstring = ' INSERT 123 abc INSERT 456 def INSERT 789 ghi' Have a look at re.sub(): s = '123 abc 456 def 789 ghi' re.compile(r(\d+\s)).sub(rINSERT \1, s) 'INSERT 123 abc INSERT 456 def INSERT 789 ghi' Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On Jun 10, 1:52 am, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Tue, 09 Jun 2009 22:21:26 -0700, John Yeung wrote: Therefore, to me the most up-to-date docs (which say that uniform(a, b) returns a float in the closed interval [a, b]) is closer to correct than before, but still fails to point out the full subtlety of the behavior. Which is? That uniform(a, b) will return a random float in the semi-open interval [a, b) for certain values of a and b; and in the closed interval [a, b] for other values of a and b. (Swap a and b if a b.) To me, the fact that you sometimes get a semi-open interval and sometimes a closed interval is worth noting in the docs. John -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
John Yeung wrote: On Jun 10, 1:52am, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Tue, 09 Jun 2009 22:21:26 -0700, John Yeung wrote: Therefore, to me the most up-to-date docs (which say that uniform(a, b) returns a float in the closed interval [a, b]) is closer to correct than before, but still fails to point out the full subtlety of the behavior. Which is? That uniform(a, b) will return a random float in the semi-open interval [a, b) for certain values of a and b; and in the closed interval [a, b] for other values of a and b. (Swap a and b if a b.) To me, the fact that you sometimes get a semi-open interval and sometimes a closed interval is worth noting in the docs. John I took the following direct from "The Python Library Reference (Release 2.6.2)" , Guido van Rossum, Fred L. Drake, Jr. editor, June 10, 2009. On p. 216, Almost all module functions depend on the basic function random(), which generates a random float uniformly in the semi-open range [0.0, 1.0). Python uses the Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937-1. The underlying implementation in C is both fast and threadsafe. The Mersenne Twister is one of the most extensively tested random number generators in existence. However, being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes. The notation above means that 0 is included but 1 is not (as pointed out by Esmail). I agree with Esmail, that it is important to know if this is correct, since the "drawing" of pseudo RVs from other distributions can depend on this function. The following is taken from MatLab (R2007b), The rand function now supports a method of random number generation called the Mersenne Twister. The algorithm used by this method, developed by Nishimura and Matsumoto, generates double precision values in the closed interval [2^(-53), 1-2^(-53)], with a period of (2^19937-1)/2. Note, that it will not generate a 0 or 1; i.e., the interval for the pseudo RV can be written as (0,1) or [2^(-53), 1-2^(-53)], where the latter is more informative. For a full description of the Mersenne twister algorithm, see http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html. If indeed Python 2.6.2 is using the Mersenne twister algorithm as defined by the creators of this algorithm (go to the link given above), then IMHO the documentation should be corrected. I hope that this helps. --V. Stokes -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
Miles Kaufmann writes: [...] I'm curious what algorithm calls for random numbers on a closed interval. The Box-Muller transform, polar form. At least Wikipedia says so. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using logging module to log into flash drive
On Jun 9, 7:57 pm, Carl Banks pavlovevide...@gmail.com wrote: On Jun 9, 8:57 am, kretel krzysztof.re...@googlemail.com wrote: Hi All, I am trying to implement the following functionality: 1. log messages to the flash drive 2. if the flash drive is not available, switch handler to the BufferringHandler and log into buffer, 3. once the flash drive is plugged in and available store the logs from BufferHandler into that flash drive and switch the handler into RotateFileHandler. Which approach would you suggest to use while implementing this functionality? One that come into my mind is to have one process or thread to check periodically if the flashdrive is available, and have a flag that will indicate that we can store the logs into the flash drive. But I don't particularly like this approach. Would you do it different way? Any suggestions are appreciated. I'd refactor the steps this way: 1. log messages to a buffer 2. periodically flush the buffer to the flash drive, if it's available The periodically part could be accomplished with a thread or scheduled delays, however suits your application. It might not be a final if you need to log messages promptly, but that's how I'd begin. Carl Banks Hi Carl, Thanks for the advice. I haven't think about it this way, but it looks like that might be the starting point. Thanks, Krzysztof -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On 10 Giu, 06:23, Esmail ebo...@hotmail.com wrote: Here is part of the specification of an algorithm I'm implementing that shows the reason for my original query: vid = w * vid + c1 * rand( ) * ( pid – xid ) + c2 * Rand( ) * (pgd –xid ) (1a) xid = xid + vid (1b) where c1 and c2 are two positive constants, rand() and Rand() are two random functions in the range [0,1], ^ and w is the inertia weight. 1) I second John Yeung's suggestion: use random integers between 0 and N-1 or N inclusive and divide by N to obtain a maximum value of (N-1)/ N or 1 as you prefer. Note that N doesn't need to be very large. 2) I'm not sure a pseudo-closed range is different from a pseudo-open one. You are perturbing vid and xid by random amounts, scaled by arbitrary coefficients c1 and c2: if you multiply or divide these coefficients by (N-1)/N the minimum and maximum results for the two choices can be made identical up to floating point mangling. Regards, Lorenzo Gatti -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On Jun 10, 3:24 pm, John Yeung gallium.arsen...@gmail.com wrote: Alex, did you bother to read what I quoted? Paul McGuire suggested an alternative in case the OP was choosing integers in a roundabout way. I was merely pointing out that Paul's solution can be more simply achieved using a library function. My apologies, John. I *did* read the quote, my brain just didn't parse it correctly. Sorry about that :) -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On Wed, Jun 10, 2009 at 8:25 AM, John Yeunggallium.arsen...@gmail.com wrote: That uniform(a, b) will return a random float in the semi-open interval [a, b) for certain values of a and b; and in the closed interval [a, b] for other values of a and b. (Swap a and b if a b.) To me, the fact that you sometimes get a semi-open interval and sometimes a closed interval is worth noting in the docs. If it is important whether b is included or not, apparently the difference between b and the largest float smaller than b is important. If a difference as small as that makes a difference, you should not be using floats, or random generators giving floats, anyway, but something with a higher precision. In other words, if you do care about the difference between [a, b) and [a, b], the random module is not giving what you meet even if it does give the 'right' one of those. -- André Engels, andreeng...@gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
Esmail writes: random.random() will generate a random value in the range [0, 1). Is there an easy way to generate random values in the range [0, 1]? I.e., including 1? I am implementing an algorithm and want to stay as true to the original design specifications as possible though I suppose the difference between the two max values might be minimal. You could generate from a larger range and reject the values that you do not want: generate from [0, 2), say, until you get a value in [0, 1]. If you generate from [0, 1 + epsilon) with small epsilon, rejections will be rare. (I didn't notice this suggestion in the thread, so I'm voicing it just in case it's not there yet.) -- http://mail.python.org/mailman/listinfo/python-list
Re: setting program name, like $0= in perl?
m...@pixar.com m...@pixar.com wrote: I'm sure this is a FAQ, but I certainly haven't been able to find an answer. Is it possible to set the program name as seen by the operating system or lower-level libraries? I'm connecting to a database, and the runtime helpfully sends some information to the server, such as username, pid, and program name. Unfortunately, all my python programs get the name '/usr/bin/python', and I would like to force that to be the names of the individual scripts. You can use this module http://code.google.com/p/procname/ Just for fun I made a pure python version using ctypes #!/usr/bin/python Attempt to set the process name with ctypes import os from subprocess import Popen, PIPE from ctypes import pythonapi, c_int, c_char_p, POINTER, addressof, pointer, CDLL, memmove, memset from ctypes.util import find_library Py_GetArgcArgv = pythonapi.Py_GetArgcArgv c_lib = CDLL(find_library(c)) PR_SET_NAME = 15# linux specific argc_t = POINTER(c_char_p) Py_GetArgcArgv.restype = None Py_GetArgcArgv.argtypes = [POINTER(c_int), POINTER(argc_t)] def set_name(name): argv = c_int(0) argc = argc_t() Py_GetArgcArgv(argv, pointer(argc)) name0 = name+\0 memset(argc.contents, 0, 256) # could do this better! memmove(argc.contents, name0, len(name0)) # prctl doesn't seem to be needed on linux? c_lib.prctl(PR_SET_NAME, name+\0, 0, 0, 0) def ps(): print Popen([ps, v, str(os.getpid())], stdout=PIPE).communicate()[0] def main(): print Before ps() set_name(sausage) print After ps() if __name__ == __main__: main() This prints $ ./procname.py Before PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND 9159 pts/7S+ 0:00 0 1000 5551 3404 0.1 /usr/bin/python ./procname.py After PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND 9159 pts/7S+ 0:00 0 1000 5551 3420 0.1 sausage -- Nick Craig-Wood n...@craig-wood.com -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On Jun 10, 7:25 am, John Yeung gallium.arsen...@gmail.com wrote: On Jun 10, 1:52 am, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Tue, 09 Jun 2009 22:21:26 -0700, John Yeung wrote: Therefore, to me the most up-to-date docs (which say that uniform(a, b) returns a float in the closed interval [a, b]) is closer to correct than before, but still fails to point out the full subtlety of the behavior. Which is? That uniform(a, b) will return a random float in the semi-open interval [a, b) for certain values of a and b; and in the closed interval [a, b] for other values of a and b. (Swap a and b if a b.) To me, the fact that you sometimes get a semi-open interval and sometimes a closed interval is worth noting in the docs. Do you want to submit a doc patch? For practical purposes, I think you'd be hard-pressed to find a statistical test that could reliably distinguish between a large sample of values from random.uniform(a, b) and a sample from a 'perfect' uniform distribution on the closed interval [a, b]. It's true that there are values of a and b such that random.uniform(a, b) can never produce b. But for given a and b, not too close together, there may be many other values that can't be produced as well, so it hardly seems worth pointing out one particular value that can never be produced. Example: on a typical system there are almost 2**62 floats in the range [0, 1); the vast majority of these can never be produced by random.random(), which can only ever return one of 2**53 distinct values (it essentially just returns a value of the form n/2**53 with n an integer in the range [0, 2**53)). So random.uniform(0, 1) will miss lots of possible values. On the other hand, it's easy to find examples of a and b such that random.uniform(a, b) has a significant chance of producing b. For example, take a = 10**16, b = 10**16 + 4, then there's about a 1 in 4 chance of getting b. Or for a more extreme example, simply take a = b. Hence the doc change. Mark -- http://mail.python.org/mailman/listinfo/python-list
Re: What is the actual type of interrupted system call?
mrstevegross wrote: exceptions.EOFError exceptions.ReferenceError exceptions.ZeroDivisionError ... exceptions.NotImplementedError exceptions.UnicodeError exceptions.__str__ Is there a single parent exception to all those? Or should I just write it as: try: ... catch Exception: ... Thanks, --Steve You're right, Exception is the parent of (almost) all exceptions. I wouldn't advise writing such block catching all exceptions, it makes error tracking quite difficult. However if you don't know the type of exception involved in a particular case, you can write try: ... except Exception, excInstance: print excInstance.__class__.__name__ print excInstance.__dict__ If you know how to trigger the exception, it should print the class name of the exception, along with its attributes. It will help you then write a more focused except clause. Jean-Michel -- http://mail.python.org/mailman/listinfo/python-list
Re: Where should I store docs in my project?
Matthew Wilson wrote: I used paster to create a project named pitz. I'm writing a bunch of user documentation. Where should I put it? The project looks a little like this: /home/matt/projects/pitz setup.py pitz/ __init__.py # has my project code docs/ # has my reST files tests # has some tests Is there a convention for where to put the docs folder? I wouldn't put it into the pitz-package - because then these get (potentially) installed into site-packages - where no one is going to see them anyway. I'd put the docs-folder one the root-level, besides the setup.py Diez -- http://mail.python.org/mailman/listinfo/python-list
Compiling Python3.1
Hello group, I just wanted to switch from Py3.0 to Py3.1. No luck here: [...] ar rc libpython3.1.a Python/_warnings.o Python/Python-ast.o Python/asdl.o Python/ast.o Python/bltinmodule.o Python/ceval.o Python/compile.o Python/codecs.o Python/errors.o Python/frozen.o Python/frozenmain.o Python/future.o Python/getargs.o Python/getcompiler.o Python/getcopyright.o Python/getplatform.o Python/getversion.o Python/graminit.o Python/import.o Python/importdl.o Python/marshal.o Python/modsupport.o Python/mystrtoul.o Python/mysnprintf.o Python/peephole.o Python/pyarena.o Python/pyctype.o Python/pyfpe.o Python/pymath.o Python/pystate.o Python/pythonrun.o Python/structmember.o Python/symtable.o Python/sysmodule.o Python/traceback.o Python/getopt.o Python/pystrcmp.o Python/pystrtod.o Python/dtoa.o Python/formatter_unicode.o Python/dynload_shlib.o Python/thread.o ar rc libpython3.1.a Modules/config.o Modules/getpath.o Modules/main.o Modules/gcmodule.o ar rc libpython3.1.a Modules/_threadmodule.o Modules/signalmodule.o Modules/posixmodule.o Modules/errnomodule.o Modules/pwdmodule.o Modules/_sre.o Modules/_codecsmodule.o Modules/_weakref.o Modules/_functoolsmodule.o Modules/_localemodule.o Modules/_iomodule.o Modules/iobase.o Modules/fileio.o Modules/bytesio.o Modules/bufferedio.o Modules/textio.o Modules/stringio.o Modules/zipimport.o Modules/symtablemodule.o Modules/xxsubtype.o ranlib libpython3.1.a gcc -pthread -Xlinker -export-dynamic -o python \ Modules/python.o \ libpython3.1.a -lpthread -ldl -lutil -lm Traceback (most recent call last): File ./setup.py, line 1675, in module main() File ./setup.py, line 1670, in main Tools/scripts/2to3] File /home/joe/Python-3.1rc1/Lib/distutils/core.py, line 149, in setup dist.run_commands() File /home/joe/Python-3.1rc1/Lib/distutils/dist.py, line 921, in run_commands self.run_command(cmd) File /home/joe/Python-3.1rc1/Lib/distutils/dist.py, line 940, in run_command cmd_obj.run() File /home/joe/Python-3.1rc1/Lib/distutils/command/build.py, line 128, in run self.run_command(cmd_name) File /home/joe/Python-3.1rc1/Lib/distutils/cmd.py, line 315, in run_command self.distribution.run_command(command) File /home/joe/Python-3.1rc1/Lib/distutils/dist.py, line 940, in run_command cmd_obj.run() File /home/joe/Python-3.1rc1/Lib/distutils/command/build_ext.py, line 347, in run self.build_extensions() File ./setup.py, line 102, in build_extensions missing = self.detect_modules() File ./setup.py, line 728, in detect_modules f = open(f).read() File /home/joe/Python-3.1rc1/Lib/encodings/ascii.py, line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 917: ordinal not in range(128) make: *** [sharedmods] Fehler 1 What can I do about that? Kind regards, Johannes -- Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit, verlästerung von Gott, Bibel und mir und bewusster Blasphemie. -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik 48d8bf1d$0$7510$54022...@news.sunrise.ch -- http://mail.python.org/mailman/listinfo/python-list
Re: How to insert string in each match using RegEx iterator
On Jun 9, 11:13 pm, 504cr...@gmail.com 504cr...@gmail.com wrote: By what method would a string be inserted at each instance of a RegEx match? Some might say that using a parsing library for this problem is overkill, but let me just put this out there as another data point for you. Pyparsing (http://pyparsing.wikispaces.com) supports callbacks that allow you to embellish the matched tokens, and create a new string containing the modified text for each match of a pyparsing expression. Hmm, maybe the code example is easier to follow than the explanation... from pyparsing import Word, nums, Regex # an integer is a 'word' composed of numeric characters integer = Word(nums) # or use this if you prefer integer = Regex(r'\d+') # attach a parse action to prefix 'INSERT ' before the matched token integer.setParseAction(lambda tokens: INSERT + tokens[0]) # use transformString to search through the input, applying the # parse action to all matches of the given expression test = '123 abc 456 def 789 ghi' print integer.transformString(test) # prints # INSERT 123 abc INSERT 456 def INSERT 789 ghi I offer this because often the simple examples that get posted are just the barest tip of the iceberg of what the poster eventually plans to tackle. Good luck in your Pythonic adventure! -- Paul -- http://mail.python.org/mailman/listinfo/python-list
easiest way to check python version?
hi all, what is easiest way to check python version (to obtain values like 2.4, 2.5, 2.6, 3.0 etc) from Python env? I don't mean python -V from command prompt. Thank you in advance, D. -- http://mail.python.org/mailman/listinfo/python-list
Re: easiest way to check python version?
dmitrey wrote: hi all, what is easiest way to check python version (to obtain values like 2.4, 2.5, 2.6, 3.0 etc) from Python env? I don't mean python -V from command prompt. Thank you in advance, D. You don't mean: sys.version either? -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' -- http://mail.python.org/mailman/listinfo/python-list
Re: easiest way to check python version?
On Jun 10, 9:01 pm, dmitrey dmitrey.kros...@scipy.org wrote: hi all, what is easiest way to check python version (to obtain values like 2.4, 2.5, 2.6, 3.0 etc) from Python env? I don't mean python -V from command prompt. | import sys | sys.version | '2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]' | sys.version_info | (2, 6, 2, 'final', 0) | easiest depends on purpose; e.g. version for display or for logging exactly what the customer is running. version_info (or a prefix of it) is the best for things like conditional imports etc E.g. py_version = sys.version_info[:2] if py_version == (2, 3): from sets import Set as set Cheers, John -- http://mail.python.org/mailman/listinfo/python-list
Re: easiest way to check python version?
A common way to do it is (it is widely accepted): python -c 'import sys; print sys.version[:3]' Regards, Antonio On Wednesday 10 June 2009 12:25:22 John Machin wrote: On Jun 10, 9:01 pm, dmitrey dmitrey.kros...@scipy.org wrote: hi all, what is easiest way to check python version (to obtain values like 2.4, 2.5, 2.6, 3.0 etc) from Python env? I don't mean python -V from command prompt. | import sys | sys.version | | '2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]' | sys.version_info | | (2, 6, 2, 'final', 0) easiest depends on purpose; e.g. version for display or for logging exactly what the customer is running. version_info (or a prefix of it) is the best for things like conditional imports etc E.g. py_version = sys.version_info[:2] if py_version == (2, 3): from sets import Set as set Cheers, John -- http://mail.python.org/mailman/listinfo/python-list
xmlrpclib and generators
It would be really cool if an rpc call could return a generator. I know that there are a few reasons why one would not expect it to be part of the library (e.g. the client would need to receive asynchronous messages, and it's not part of the rpc standard), however below I show a possible implementation, given that the client is also able to be a server... I am aware of MultiCall, but that is actually something like the inverse of what I want. I need a consumer. Example: # hypothetical client code (running at http://rpc.myserver.com;) from fancyxmlrpc import FancyXMLRPCServer server = FancyXMLRPCServer(('localhost', 9000)) def primes(): n = 2 p = [] while True: if not any( n % f == 0 for f in p ): yield n p.append( n ) n += 1 server.register_generator(primes) server.serve_forever() # hypothetical client code (running at http://www.mywebsite.com): from fancyxmlrpc import FancyServerProxy server_url = http://rpc.myserver.com; local_url = http://www.mywebsite.com; server = FancyServerProxy(server_url, local_url) g = server.examples.getStateNames() for x in g: print x Okay, so to implement this, the trick would be to implement the generator wrapper as a hidden xmlrpc conversation. On the client side, FancyServerProxy would encapsulate a hidden RPC server with a function that receives each item that is yielded by the server and queues them while the client generator consumes the queue and yields the items. The practical constraints of my specific application are: 1. The rpc server is a highly specialized slave system that does heavy duty work. 2. The rpc client is itself a web server that dispatches work requests to the rpc server(s) and displays the current status of work done so far. 3. The generators will typically run for a long time (hours) and yield data periodically (perhaps once a minute). 4. Trusted users will write generators on the server and consumers on the client (web site) and use the web site to make requests. 5. It would be even better if my generator yields numpy arrays rather than lists. 6. It would be nice to be able to scale to allow the web site to dispatch to multiple work servers. So my questions are: 1. Does using xmlrpc make any sense for this? 2. I am missing an easier way to do this? 3. Any good examples of things like this? -- http://mail.python.org/mailman/listinfo/python-list
Re: multi-thread python interpreaters and c++ program
On Jun 9, 6:50 am, myopc my...@aaa.com wrote: I am ruuning a c++ program (boost python) , which create many python interpreaters and each run a python script with use multi-thread (threading). when the c++ main program exit, I want to shut down python interpreaters, but it crashed. Your threads are daemonic, you could be seeing http://bugs.python.org/issue1856 You'll have to check your stack in a debugger to know. But as said this can be avoided by making the threads finish themself and joining them. Regards Floris -- http://mail.python.org/mailman/listinfo/python-list
Re: Any idea of stopping the execution of PyRun_File()
On Jun 10, 7:41 am, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: En Mon, 08 Jun 2009 22:15:22 -0300, BigHand hewei...@gmail.com escribió: I have an embedded python application. which is a MFC app with Python interpreter embedded. In the App, I have a separate thread to execute a Python script (using the PyRun_File), but if the user want to stop the executing script, how should I do? A possible way is terminate the thread of executing the scripts by TerminateThread(). but TerminateThread() is unsafe and not recommended. guys, could you guide me on this? You could use PyThreadState_SetAsyncExc (to inject an exception), or perhaps PyErr_SetInterrupt (to emulate ^C, which in turn generates a KeyboardInterrupt). This should work fine for well-behaved scripts, but if it ignores all exceptions like this: try: ... except: pass you'll have to look at ceval.c how to break out of the running loop. (this is yet another argument against indiscriminately using a bare except clause...) -- Gabriel Genellina hello.Gabriel. PyThreadState_SetAsyncExc cause me a win32 exception of Stack overflow msvcr80d.dll!_getptd_noexit() Line 592 C msvcr80d.dll!_getptd() Line 658 + 0x5 bytesC msvcr80d.dll!_LocaleUpdate::_LocaleUpdate(localeinfo_struct * plocinfo=0x) Line 264 + 0x5 bytes C++ msvcr80d.dll!_output_l(_iobuf * stream=0x10311d40, const char * format=0x1e26e0ac, localeinfo_struct * plocinfo=0x, char * argptr=0x000333c4) Line 1028 C++ msvcr80d.dll!fprintf(_iobuf * str=0x10311d40, const char * format=0x1e26e0ac, ...) Line 70 + 0x13 bytes C python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2000 + 0x19 bytesC python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytesC python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001 + 0x5 bytes C python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytesC python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001 + 0x5 bytes C python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytesC python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001 + 0x5 bytes C python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytesC python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001 + 0x5 bytes C python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytesC python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001 + 0x5 bytes C python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytesC python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001 + 0x5 bytes C python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytesC My app is a MFC app, it have the main thread and the sub thread, the sub thread run the script(Py_RunFile), That I am looking for a way to stop the Py_RunFile from the main thread. that looks like I have to use the TerminateThread() . thanks.Gabriel, could you find some example for me? -- http://mail.python.org/mailman/listinfo/python-list
getop or optparse with option with spaces?
Hello, I have been trying to find an example of how to deal with options that have spaces in them. I am using jython, which is the same I think as python 2.2.3. I feebly tried to use optparse and argparse with no success (got gettext, locale, and optparse). The code is as follows: try: (opts, args) = getopt.getopt(sys.argv[1:], hs:p:n:j:d:l:u:c:t:w:q:v, [help, server=, port=, dsName=,jndiName=,driverName=,driverURL=,user=,passWD=,targetServer=,whereProp=,testTableName=,version]) except getopt.GetoptError: usage() for opt in opts: (key, value) = opt if (key in (-v, --version)): print Version: + version sys.exit(1) if (key in (-h, --help)): usage() if (key in (-s, --server)): server = value if (key in (-p, --port)): port = value if (key in (-n, --dsName)): dsName = value if (key in (-j, --jndiName)): jndiName = value if (key in (-d, --driverName)): driverName = value if (key in (-l, --driverURL)): driverURL = value if (key in (-u, --user)): user = value if (key in (-c, --passWD)): passWD = value if (key in (-t, --targetServer)): targetServer = value if (key in (-q, --testTableName)): testTableName = value if (key in (-w, --whereProp)): whereProp = value print server: + server print port: + port print dsName: + dsName print jndiName: + jndiName print driverName: + driverName print driverURL: + driverURL print user: + user print passWD: + passWD print testtable: + testTableName print targetServer: + targetServer print whereProp: + whereProp The one that gives me trouble is with the -q option, because it can look like: -q SQL 1 TABLE. It returns back just SQL. How do I get it to return the whole thing that is in double quotes? Another problem is that whereProp value is just not seen. Is there a limit to the size for argv? If I use optparse instead of getopt, I see that SQL 1 TABLE goes into args instead of values by the way. A temporary workaround is to use .join(args) and assign that to testTableName, but I worry about what will happen if testTableName is blank or has something with no spaces. Also, it just seem weird I have to do a work around like that. I could have swore using double quotes should have fixed this issue, but they do not seem to work. David -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
Esmail ebo...@hotmail.com wrote: Hi, random.random() will generate a random value in the range [0, 1). Is there an easy way to generate random values in the range [0, 1]? I.e., including 1? [...] Here are three recipes, each more pedantic than the last. They all assume that Python floats are IEEE 754 binary64 format (which they almost certainly are on your machine), and that randrange generates all values with equal likelihood (which it almost certainly doesn't, but the assumption should be good enough for government work). import random def random1(): Random float in [0, 1]. Generates all floats of the form n/2**53, 0 = n = 2**53, with equal probability. return random.randrange(2**53+1)/2.**53 def random2(): Random float in [0, 1]. Generates all floats of the forn n/2**53, 0 = n = 2**53, with values in (0.0, 1.0) equally likely, and the endpoints 0.0 and 1.0 occuring with half the probability of any other value. This is equivalent to generating a random real number uniformly on the closed interval [0, 1] and then rounding that real number to the nearest float of the form n/2**53. return (random.randrange(2**54)+1)//2/2.**53 def random3(): Random float in [0, 1]. Generates *all* floats in the interval [0.0, 1.0] (including 0.0 and 1.0, but excluding -0.0). Each float is generated with a probability corresponding to the size of the subinterval of [0, 1] that rounds to the given float under round-to-nearest. This is equivalent to generating a random real number uniformly on the closed interval [0, 1] and then rounding that real number to the nearest representable floating-point number. m = (random.randrange(2**53)+1)//2 e = 1022 while random.randrange(2) and e: e -= 1 return (m+2**52) * 2.**(e-1075) if e else m*2.**-1074 -- Mark Dickinson -- http://mail.python.org/mailman/listinfo/python-list
Re: getop or optparse with option with spaces?
Hello, It's strange behaviour. Have you tried argparse (http://code.google.com/p/argparse/)? I've been using it for long time without any problem like that? Best regards, Javier 2009/6/10 David Shapiro david.shap...@sas.com: Hello, I have been trying to find an example of how to deal with options that have spaces in them. I am using jython, which is the same I think as python 2.2.3. I feebly tried to use optparse and argparse with no success (got gettext, locale, and optparse). The code is as follows: try: (opts, args) = getopt.getopt(sys.argv[1:], hs:p:n:j:d:l:u:c:t:w:q:v, [help, server=, port=, dsName=,jndiName=,driverName=,driverURL=,user=,passWD=,targetServer=,whereProp=,testTableName=,version]) except getopt.GetoptError: usage() for opt in opts: (key, value) = opt if (key in (-v, --version)): print Version: + version sys.exit(1) if (key in (-h, --help)): usage() if (key in (-s, --server)): server = value if (key in (-p, --port)): port = value if (key in (-n, --dsName)): dsName = value if (key in (-j, --jndiName)): jndiName = value if (key in (-d, --driverName)): driverName = value if (key in (-l, --driverURL)): driverURL = value if (key in (-u, --user)): user = value if (key in (-c, --passWD)): passWD = value if (key in (-t, --targetServer)): targetServer = value if (key in (-q, --testTableName)): testTableName = value if (key in (-w, --whereProp)): whereProp = value print server: + server print port: + port print dsName: + dsName print jndiName: + jndiName print driverName: + driverName print driverURL: + driverURL print user: + user print passWD: + passWD print testtable: + testTableName print targetServer: + targetServer print whereProp: + whereProp The one that gives me trouble is with the -q option, because it can look like: -q SQL 1 TABLE. It returns back just SQL. How do I get it to return the whole thing that is in double quotes? Another problem is that whereProp value is just not seen. Is there a limit to the size for argv? If I use optparse instead of getopt, I see that SQL 1 TABLE goes into args instead of values by the way. A temporary workaround is to use .join(args) and assign that to testTableName, but I worry about what will happen if testTableName is blank or has something with no spaces. Also, it just seem weird I have to do a work around like that. I could have swore using double quotes should have fixed this issue, but they do not seem to work. David -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On Jun 9, 11:23 pm, Esmail ebo...@hotmail.com wrote: Here is part of the specification of an algorithm I'm implementing that shows the reason for my original query: vid = w * vid + c1 * rand( ) * ( pid – xid ) + c2 * Rand( ) * (pgd –xid ) (1a) xid = xid + vid (1b) where c1 and c2 are two positive constants, rand() and Rand() are two random functions in the range [0,1], ^ and w is the inertia weight. It is entirely possible that the documentation you have for the original rand() and Rand() functions have misstated their range. In my experience, rand() functions that I have worked with have always been [0,1). -- Paul -- http://mail.python.org/mailman/listinfo/python-list
retrieve bitwise float representation
Hi! I need to pack a floating point value into a vector of 32-bit unsigned values in IEEE format. Further, I maintain a CRC32 checksum for integrity checking. For the latter, I actually need the float as integral value. What I currently do is this: tmp = struct.pack(=f, f) (i,) = struct.unpack(=L, tmp) IOW, I pack and unpack the float using the struct module, which works. What I'm wondering is whether there are any better or alternative ways to achieve this, the overhead now seems enormous and unnecessary to me here. Thank you! Uli -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 -- http://mail.python.org/mailman/listinfo/python-list
RE: getop or optparse with option with spaces?
Unfortunately, I had no luck installing argparse, which is really confusing because I would need to use some old version pre-optik to use I think. The Jython I use is like python 2.2.3. I spent all of yesterday trying to get either getopt, argparse, or optparse to work. Even with optparse I had to modify a module. For example, in textwrap.py, they have @ line 124 in the module: if self.replace_whitespace: #if isinstance(text,str): # text = text.translate(self.whitespace_trans) #elif isinstances(text,Unicode): text = text.translate(self.unicode_whitespace_trans) return text I had to comment out the if isinstance(text,str) and elif. Is the double quotes supposed to work? -Original Message- From: Javier Collado [mailto:javier.coll...@gmail.com] Sent: Wednesday, June 10, 2009 9:38 AM To: David Shapiro Cc: python-list@python.org Subject: Re: getop or optparse with option with spaces? Hello, It's strange behaviour. Have you tried argparse (http://code.google.com/p/argparse/)? I've been using it for long time without any problem like that? Best regards, Javier 2009/6/10 David Shapiro david.shap...@sas.com: Hello, I have been trying to find an example of how to deal with options that have spaces in them. I am using jython, which is the same I think as python 2.2.3. I feebly tried to use optparse and argparse with no success (got gettext, locale, and optparse). The code is as follows: try: (opts, args) = getopt.getopt(sys.argv[1:], hs:p:n:j:d:l:u:c:t:w:q:v, [help, server=, port=, dsName=,jndiName=,driverName=,driverURL=,user=,passWD=,targetServer=,whereProp=,testTableName=,version]) except getopt.GetoptError: usage() for opt in opts: (key, value) = opt if (key in (-v, --version)): print Version: + version sys.exit(1) if (key in (-h, --help)): usage() if (key in (-s, --server)): server = value if (key in (-p, --port)): port = value if (key in (-n, --dsName)): dsName = value if (key in (-j, --jndiName)): jndiName = value if (key in (-d, --driverName)): driverName = value if (key in (-l, --driverURL)): driverURL = value if (key in (-u, --user)): user = value if (key in (-c, --passWD)): passWD = value if (key in (-t, --targetServer)): targetServer = value if (key in (-q, --testTableName)): testTableName = value if (key in (-w, --whereProp)): whereProp = value print server: + server print port: + port print dsName: + dsName print jndiName: + jndiName print driverName: + driverName print driverURL: + driverURL print user: + user print passWD: + passWD print testtable: + testTableName print targetServer: + targetServer print whereProp: + whereProp The one that gives me trouble is with the -q option, because it can look like: -q SQL 1 TABLE. It returns back just SQL. How do I get it to return the whole thing that is in double quotes? Another problem is that whereProp value is just not seen. Is there a limit to the size for argv? If I use optparse instead of getopt, I see that SQL 1 TABLE goes into args instead of values by the way. A temporary workaround is to use .join(args) and assign that to testTableName, but I worry about what will happen if testTableName is blank or has something with no spaces. Also, it just seem weird I have to do a work around like that. I could have swore using double quotes should have fixed this issue, but they do not seem to work. David -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
xml application advice
I am writing a application to calculate pressure drop for a piping network. Namely a building sprinkler system. This will be a command line program at first with the system described in xml (at least that is how I think I want to do it). An important part of this calculation is finding the 'hydraulically most remote' sprinkler. This is something that I could specify with an attribute for now and later think about how to automate it. I need to walk through the dom tree until I find a node of type sprinkler that has an attribute of hydraulically_most_remote with a value of True. After I find this I need to break the itterator/for loop and then start walking backwards keeping a running total of the pressure drop until I reach a node that has multiple pipesections and then walk to the end of each branch and calculate the pressure drop, and then add them to the branch that contained the hydraulically most remote sprinkler, and then move on, repeating this until I walk all the way back to the inflow node. I am having trouble finding a decent python/xml resource on the web. I have ordered Python XML by Jones and Drake, but I am anxious to get something started. The only decent online resource that I can seem to find is http://pyxml.sourceforge.net/topics/howto/xml-howto.html which doesn't seem to be a very comprehensive how-to. Do demonstrate just about everything I know about xml and python I attached t.py and ex.xml. Another thing that is confusing is dir(walker) does not show walker having an attribute currentNode and dir(walker.currentNode) does not show walker.currentNode having an attribute tagName. Bill ?xml version=1.0? project name=test inflow static=60 residual=20 pipesection diameter=1.05 length=10 node id=H1 k=5.6 type=sprinkler pipesection diameter=1.05 length=4 node id=1 type=T pipesection diameter=1.05 length=6 node id=H2 hydraulically_most_remote=True /node /pipesection pipesection diameter=1.05 length=5 node id=H3 /node /pipesection /node /pipesection /node /pipesection /inflow /project from xml.dom.ext.reader import Sax2 from xml.dom.NodeFilter import NodeFilter reader = Sax2.Reader() doc = reader.fromUri('ex.xml') walker = doc.createTreeWalker(doc.documentElement,NodeFilter.SHOW_ELEMENT,None,0) while 1: print walker.currentNode.tagName next = walker.nextNode() if next is None: break -- http://mail.python.org/mailman/listinfo/python-list
RE: xml application advice
How about using web server (tomcat jsp) and then java for the xml part, which would allow you to build a nice gui for you. You can use python for backend work. If you can combine some of the levels of your xml it will be easier to traverse. I am not sure this will work for you, but I put as an example: project name=test - inflow static=60 residual=20 - pipesection diameter=1.05 length=10 nodeid=H1 k=5.6 type=sprinkler - pipesection diameter=1.05 length=4 nodeid=1 type=T - pipesection diameter=1.05 length=6 nodeid=H2 hydraulically_most_remote=True / /pipesection - pipesection diameter=1.05 length=5 nodeid=H3 / /pipesection /node /pipesection /node /pipesection /inflow /project -Original Message- From: python-list-bounces+david.shapiro=sas@python.org [mailto:python-list-bounces+david.shapiro=sas@python.org] On Behalf Of William Purcell Sent: Wednesday, June 10, 2009 9:58 AM To: python-list@python.org Subject: xml application advice I am writing a application to calculate pressure drop for a piping network. Namely a building sprinkler system. This will be a command line program at first with the system described in xml (at least that is how I think I want to do it). An important part of this calculation is finding the 'hydraulically most remote' sprinkler. This is something that I could specify with an attribute for now and later think about how to automate it. I need to walk through the dom tree until I find a node of type sprinkler that has an attribute of hydraulically_most_remote with a value of True. After I find this I need to break the itterator/for loop and then start walking backwards keeping a running total of the pressure drop until I reach a node that has multiple pipesections and then walk to the end of each branch and calculate the pressure drop, and then add them to the branch that contained the hydraulically most remote sprinkler, and then move on, repeating this until I walk all the way back to the inflow node. I am having trouble finding a decent python/xml resource on the web. I have ordered Python XML by Jones and Drake, but I am anxious to get something started. The only decent online resource that I can seem to find is http://pyxml.sourceforge.net/topics/howto/xml-howto.html which doesn't seem to be a very comprehensive how-to. Do demonstrate just about everything I know about xml and python I attached t.py and ex.xml. Another thing that is confusing is dir(walker) does not show walker having an attribute currentNode and dir(walker.currentNode) does not show walker.currentNode having an attribute tagName. Bill -- http://mail.python.org/mailman/listinfo/python-list
Re: retrieve bitwise float representation
Ulrich Eckhardt eckha...@satorlaser.com wrote: Hi! I need to pack a floating point value into a vector of 32-bit unsigned values in IEEE format. Further, I maintain a CRC32 checksum for integrity checking. For the latter, I actually need the float as integral value. [...] You could try using frexp to extract the significand and exponent of the float, and then pack those values directly into an integer, following the IEEE 754 format. For example: Python 2.6.2 (r262:71600, Jun 8 2009, 14:57:27) [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin Type help, copyright, credits or license for more information. from struct import pack, unpack from math import frexp, copysign x = -34.125 unpack('L', pack('f', x))[0] 3255336960L m, e = frexp(abs(x)) (e+125 23) + int(m*2.**24) + (2**31 if x 0.0 else 0) 3255336960L The above formula works for most x that are exactly representable as an IEEE single-precision value, but extra effort would be required to make it work with nans, infinities, zeros or denormals. I'm not sure whether this is any faster than the pack/unpack route, though. Obviously, if you're going to convert many floats then the 2.**24 and 2**31 constants should be precalculated. If your values aren't already exactly representable as IEEE single-precision floats then you may have a problem with rounding: the call to int() in the above would truncate, while the implicit conversion from double to single precision involved in packing with 'f' is more likely to do a round-to-nearest. -- Mark Dickinson -- http://mail.python.org/mailman/listinfo/python-list
Connection tester
Hey! I am developing a small application that tests multiple websites and compares their response time. Some of these sites do not respond to a ping and, for the measurement to be standardized, all sites must have the same action preformed upon them. Another problem is that not all of the sites have the same page size and I am not interested in how long it takes to load a page but instead just how long it takes for the website to respond. Finally, I am looking to keep this script platform independent, if at all possible. Here is the code: try: # Get the starting time origTime = time.time() # Create the socket connection and then close s = socket.socket(AF_INET, SOCK_STREAM) s.connect((targetIP, port)) s.send(GET / HTTP/1.0\r\n\r\n) result = s.recv(1024) s.shutdown(SHUT_RDWR) except: result = # Check for problems and report back the time if result == : return Result((time.time() - origTime) * 1000, True) else: return Result((time.time() - origTime) * 1000, False) Result is just an object that holds the time it took for the method to finish and if there were any errors. What I am worried about is that the socket is potentially closed before the website can finish sending in all the data. Does anyone have any suggestions or is the script fine as it is? -- http://mail.python.org/mailman/listinfo/python-list
Re: retrieve bitwise float representation
Ulrich Eckhardt wrote: I need to pack a floating point value into a vector of 32-bit unsigned values in IEEE format. Further, I maintain a CRC32 checksum for integrity checking. For the latter, I actually need the float as integral value. What I currently do is this: tmp = struct.pack(=f, f) (i,) = struct.unpack(=L, tmp) IOW, I pack and unpack the float using the struct module, which works. What I'm wondering is whether there are any better or alternative ways to achieve this, the overhead now seems enormous and unnecessary to me here. binascii.crc32() operates on strings, so you don't need to unpack. If you have many float values array.array() should be more effective than struct.pack(). some_floats = map(float, range(5)) binascii.crc32(array.array(f, some_floats)) 1758053516 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to insert string in each match using RegEx iterator
On Jun 10, 5:17 am, Paul McGuire pt...@austin.rr.com wrote: On Jun 9, 11:13 pm, 504cr...@gmail.com 504cr...@gmail.com wrote: By what method would a string be inserted at each instance of a RegEx match? Some might say that using a parsing library for this problem is overkill, but let me just put this out there as another data point for you. Pyparsing (http://pyparsing.wikispaces.com) supports callbacks that allow you to embellish the matched tokens, and create a new string containing the modified text for each match of a pyparsing expression. Hmm, maybe the code example is easier to follow than the explanation... from pyparsing import Word, nums, Regex # an integer is a 'word' composed of numeric characters integer = Word(nums) # or use this if you prefer integer = Regex(r'\d+') # attach a parse action to prefix 'INSERT ' before the matched token integer.setParseAction(lambda tokens: INSERT + tokens[0]) # use transformString to search through the input, applying the # parse action to all matches of the given expression test = '123 abc 456 def 789 ghi' print integer.transformString(test) # prints # INSERT 123 abc INSERT 456 def INSERT 789 ghi I offer this because often the simple examples that get posted are just the barest tip of the iceberg of what the poster eventually plans to tackle. Good luck in your Pythonic adventure! -- Paul Thanks for all of the instant feedback. I have enumerated three responses below: First response: Peter, I wonder if you (or anyone else) might attempt a different explanation for the use of the special sequence '\1' in the RegEx syntax. The Python documentation explains: \number Matches the contents of the group of the same number. Groups are numbered starting from 1. For example, (.+) \1 matches 'the the' or '55 55', but not 'the end' (note the space after the group). This special sequence can only be used to match one of the first 99 groups. If the first digit of number is 0, or number is 3 octal digits long, it will not be interpreted as a group match, but as the character with octal value number. Inside the '[' and ']' of a character class, all numeric escapes are treated as characters. In practice, this appears to be the key to the key device to your clever solution: re.compile(r(\d+)).sub(rINSERT \1, string) 'abc INSERT 123 def INSERT 456 ghi INSERT 789' re.compile(r(\d+)).sub(rINSERT , string) 'abc INSERT def INSERT ghi INSERT ' I don't, however, precisely understand what is meant by the group of the same number -- or maybe I do, but it isn't explicit. Is this just a shorthand reference to match.group(1) -- if that were valid -- implying that the group match result is printed in the compile execution? Second response: I've encountered a problem with my RegEx learning curve which I'll be posting in a new thread -- how to escape hash characters # in strings being matched, e.g.: string = re.escape('123#456') match = re.match('\d+', string) print match _sre.SRE_Match object at 0x00A6A800 print match.group() 123 Third response: Paul, Thanks for the referring me to the Pyparsing module. I'm thoroughly enjoying Python, but I'm not prepared right now to say I've mastered the Pyparsing module. As I continue my work, however, I'll be tackling the problem of parsing addresses, exactly as the Pyparsing module example illustrates. I'm sure I'll want to use it then. -- http://mail.python.org/mailman/listinfo/python-list
Re: multi-core software
Arved Sandstrom wrote: Jon Harrop wrote: Arved Sandstrom wrote: Jon Harrop wrote: No. Concurrent programming is about interleaving computations in order to reduce latency. Nothing to do with parallelism. Jon, I do concurrent programming all the time, as do most of my peers. Way down on the list of why we do it is the reduction of latency. What is higher on the list? Correctness. I'm not being facetious. I write applications that run on application servers, and from time to time I have had to write various special purpose servers. This kind of programming is all about managing concurrent execution of computations. The overarching concern is reliability and correct function. For many corporate situations, even with hundreds of users, the actual load at any instant is low enough that the various servers involved are nowhere close to being stressed out - performance is a secondary issue. In other words, without concurrency the latency would be so high that you would consider the program to be wrong. However you cut it, the real reason is latency. -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/?u -- http://mail.python.org/mailman/listinfo/python-list
Re: How to insert string in each match using RegEx iterator
On Jun 10, 5:17 am, Paul McGuire pt...@austin.rr.com wrote: On Jun 9, 11:13 pm, 504cr...@gmail.com 504cr...@gmail.com wrote: By what method would a string be inserted at each instance of a RegEx match? Some might say that using a parsing library for this problem is overkill, but let me just put this out there as another data point for you. Pyparsing (http://pyparsing.wikispaces.com) supports callbacks that allow you to embellish the matched tokens, and create a new string containing the modified text for each match of a pyparsing expression. Hmm, maybe the code example is easier to follow than the explanation... from pyparsing import Word, nums, Regex # an integer is a 'word' composed of numeric characters integer = Word(nums) # or use this if you prefer integer = Regex(r'\d+') # attach a parse action to prefix 'INSERT ' before the matched token integer.setParseAction(lambda tokens: INSERT + tokens[0]) # use transformString to search through the input, applying the # parse action to all matches of the given expression test = '123 abc 456 def 789 ghi' print integer.transformString(test) # prints # INSERT 123 abc INSERT 456 def INSERT 789 ghi I offer this because often the simple examples that get posted are just the barest tip of the iceberg of what the poster eventually plans to tackle. Good luck in your Pythonic adventure! -- Paul Thanks for all of the instant feedback. I have enumerated three responses below: First response: Peter, I wonder if you (or anyone else) might attempt a different explanation for the use of the special sequence '\1' in the RegEx syntax. The Python documentation explains: \number Matches the contents of the group of the same number. Groups are numbered starting from 1. For example, (.+) \1 matches 'the the' or '55 55', but not 'the end' (note the space after the group). This special sequence can only be used to match one of the first 99 groups. If the first digit of number is 0, or number is 3 octal digits long, it will not be interpreted as a group match, but as the character with octal value number. Inside the '[' and ']' of a character class, all numeric escapes are treated as characters. In practice, this appears to be the key to the key device to your clever solution: re.compile(r(\d+)).sub(rINSERT \1, string) 'abc INSERT 123 def INSERT 456 ghi INSERT 789' re.compile(r(\d+)).sub(rINSERT , string) 'abc INSERT def INSERT ghi INSERT ' I don't, however, precisely understand what is meant by the group of the same number -- or maybe I do, but it isn't explicit. Is this just a shorthand reference to match.group(1) -- if that were valid -- implying that the group match result is printed in the compile execution? Second response: I've encountered a problem with my RegEx learning curve which I'll be posting in a new thread -- how to escape hash characters # in strings being matched, e.g.: string = re.escape('123#456') match = re.match('\d+', string) print match _sre.SRE_Match object at 0x00A6A800 print match.group() 123 Third response: Paul, Thanks for the referring me to the Pyparsing module. I'm thoroughly enjoying Python, but I'm not prepared right now to say I've mastered the Pyparsing module. As I continue my work, however, I'll be tackling the problem of parsing addresses, exactly as the Pyparsing module example illustrates. I'm sure I'll want to use it then. -- http://mail.python.org/mailman/listinfo/python-list
Can not dump class object created on runtime
Hello, Can not dump class object created on runtime. Is there anybody can help me? Thank. Following is testing code: import pickle from new import classobj class A: def __str__(self): return self.__class__.name if __name__ == __main__: c = classobj('B', (A, ), {}) # create class obj on runtime print c print pickle.dumps(c) # get dump string Bellows are outputs: __main__.B Traceback (most recent call last): File C:\USERS\train\_work\test\test.py, line 11, in module print pickle.dumps(c) File c:\USERS\train\Python25\lib\pickle.py, line 1366, in dumps Pickler(file, protocol).dump(obj) File c:\USERS\train\Python25\lib\pickle.py, line 224, in dump self.save(obj) File c:\USERS\train\Python25\lib\pickle.py, line 286, in save f(self, obj) # Call unbound method with explicit self File c:\USERS\train\Python25\lib\pickle.py, line 748, in save_global (obj, module, name)) pickle.PicklingError: Can't pickle class __main__.B at 0x00AF4CF0: it's not found as __main__.B - Tommy HZ 23026 MP 13958175281 -- http://mail.python.org/mailman/listinfo/python-list
Re: Connection tester
Not al pages suppost GET. If a page pings and returns does not mean it can be logged into and work (maybe database down). Have you seen soapui? - Original Message - From: python-list-bounces+david.shapiro=sas@python.org python-list-bounces+david.shapiro=sas@python.org To: python-list@python.org python-list@python.org Sent: Wed Jun 10 10:26:22 2009 Subject: Connection tester Hey! I am developing a small application that tests multiple websites and compares their response time. Some of these sites do not respond to a ping and, for the measurement to be standardized, all sites must have the same action preformed upon them. Another problem is that not all of the sites have the same page size and I am not interested in how long it takes to load a page but instead just how long it takes for the website to respond. Finally, I am looking to keep this script platform independent, if at all possible. Here is the code: try: # Get the starting time origTime = time.time() # Create the socket connection and then close s = socket.socket(AF_INET, SOCK_STREAM) s.connect((targetIP, port)) s.send(GET / HTTP/1.0\r\n\r\n) result = s.recv(1024) s.shutdown(SHUT_RDWR) except: result = # Check for problems and report back the time if result == : return Result((time.time() - origTime) * 1000, True) else: return Result((time.time() - origTime) * 1000, False) Result is just an object that holds the time it took for the method to finish and if there were any errors. What I am worried about is that the socket is potentially closed before the website can finish sending in all the data. Does anyone have any suggestions or is the script fine as it is? -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: xmlrpclib and generators
Ken Seehart wrote: 8 implementation -- The practical constraints of my specific application are: 1. The rpc server is a highly specialized slave system that does heavy duty work. 2. The rpc client is itself a web server that dispatches work requests to the rpc server(s) and displays the current status of work done so far. 3. The generators will typically run for a long time (hours) and yield data periodically (perhaps once a minute). If this yield can be made to be, or if it is, supply side driven, instead of yielding on demand like a generator, then I would set up a simple TCP/IP peer to peer socket link and just send the result back when it is ready. If you have to serve more than one such link, it is a simple matter to keep a list of queues linking the different socket sets to the generator, and to iterate over the list, putting a copy of the thing that was just produced into each queue. Of course, the thing you want to pass back must be serializable. Have you looked at Pyro? So my questions are: 1. Does using xmlrpc make any sense for this? I think you are going to have to do some heavy lifting to get it to work. 2. I am missing an easier way to do this? Maybe - see above - depends on how the stuff is generated 3. Any good examples of things like this? Don't know. - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: xml application advice
William Purcell wrote: I am writing a application to calculate pressure drop for a piping network. Namely a building sprinkler system. This will be a command line program at first with the system described in xml (at least that is how I think I want to do it). An important part of this calculation is finding the 'hydraulically most remote' sprinkler. This is something that I could specify with an attribute for now and later think about how to automate it. I need to walk through the dom tree until I find a node of type sprinkler that has an attribute of hydraulically_most_remote with a value of True. After I find this I need to break the itterator/for loop and then start walking backwards keeping a running total of the pressure drop until I reach a node that has multiple pipesections and then walk to the end of each branch and calculate the pressure drop, and then add them to the branch that contained the hydraulically most remote sprinkler, and then move on, repeating this until I walk all the way back to the inflow node. I am having trouble finding a decent python/xml resource on the web. I have ordered Python XML by Jones and Drake, but I am anxious to get something started. The only decent online resource that I can seem to find is http://pyxml.sourceforge.net/topics/howto/xml-howto.html which doesn't seem to be a very comprehensive how-to. Do demonstrate just about everything I know about xml and python I attached t.py and ex.xml. Another thing that is confusing is dir(walker) does not show walker having an attribute currentNode and dir(walker.currentNode) does not show walker.currentNode having an attribute tagName. Use lxml2 and xpath. http://codespeak.net/lxml/ http://codespeak.net/lxml/xpathxslt.html See the below piece of code to get you started: import lxml.etree as et xml = ?xml version=1.0? project name=test inflow static=60 residual=20 pipesection diameter=1.05 length=10 node id=H1 k=5.6 type=sprinkler pipesection diameter=1.05 length=4 node id=1 type=T pipesection diameter=1.05 length=6 node id=H2 hydraulically_most_remote=True /node /pipesection pipesection diameter=1.05 length=5 node id=H3 /node /pipesection /node /pipesection /node /pipesection /inflow /project project = et.fromstring(xml) hydraulically_most_remote = project.xpath(//no...@hydraulically_most_remote='True'])[0] print hydraulically_most_remote.attrib[id] # find node with multiple pipesections that's upwards def find_mp_node(node): pipesections = node.xpath(pipesection) if len(pipesections) 1: return node parent = node.getparent() if parent is not None: return find_mp_node(parent) print find_mp_node(hydraulically_most_remote).attrib[id] -- http://mail.python.org/mailman/listinfo/python-list
How to escape # hash character in regex match strings
I've encountered a problem with my RegEx learning curve -- how to escape hash characters # in strings being matched, e.g.: string = re.escape('123#abc456') match = re.match('\d+', string) print match _sre.SRE_Match object at 0x00A6A800 print match.group() 123 The correct result should be: 123456 I've tried to escape the hash symbol in the match string without result. Any ideas? Is the answer something I overlooked in my lurching Python schooling? -- http://mail.python.org/mailman/listinfo/python-list
Printing dictionary values rather than references
Hi all. I'd like to print-out a dictionary of objects. The printed values are references. How Do I print the actual objects. class MyClass: def __str__(self): return str(self.__dict__) if __name__ == '__main__': dict = dict() classA = MyClass() setattr(classA, attr-1, val-1) dict['a']= classA print classA ''' Desired output: {'attr-1': 'val-1'}''' print dict ''' Actual output: {'a': __main__.MyClass instance at 0x79cfc8}''' Thanks, Amit -- http://mail.python.org/mailman/listinfo/python-list
Re: multi-core software
On Jun 9, 9:08 pm, Arved Sandstrom dces...@hotmail.com wrote: Jon Harrop wrote: Arved Sandstrom wrote: Jon, I do concurrent programming all the time, as do most of my peers. Way down on the list of why we do it is the reduction of latency. What is higher on the list? Correctness. IMO, that response is a bit of a cop-out. Correctness is _always_ most important, no matter what application you are creating; without it, you don't have a job and the company you work for goes out of business. But, assuming that your program works and does what it's supposed to, I agree with Jon that performance needs to be right near the top of the list of concerns. Why? Performance isn't about looking good as a programmer, or having fun making a function run in 15 cycles instead of 24, or coming up with some neat bit packing scheme so that your app now only uses 20K instead of 200K. Performance is - pure and simple - about one thing only: money. Programs that use more memory require more money for the hardware of every user. Programs that run slower eat more time per day. If you have 100,000 users, all doing an operation once per day that takes 20 seconds, being able to shave 5 seconds off that saves 5.78 man-days of work. Hell, for some applications, that 20 seconds is just startup time spent at a splash screen. Just imagine if every Google search took even 5 seconds to resolve, how much time would be wasted every day around the world - ignoring the fact that Google wouldn't exist if that were the case ;-). Obviously Google engineers work incredibly hard every day to ensure correct results, but performance better be right up there at the top of the list as well. Jeff M. -- http://mail.python.org/mailman/listinfo/python-list
Re: easiest way to check python version?
John Machin wrote: On Jun 10, 9:01 pm, dmitrey dmitrey.kros...@scipy.org wrote: hi all, what is easiest way to check python version (to obtain values like 2.4, 2.5, 2.6, 3.0 etc) from Python env? ... easiest depends on purpose; e.g. version for display or for logging exactly what the customer is running. version_info (or a prefix of it) is the best for things like conditional imports etc E.g. py_version = sys.version_info[:2] if py_version == (2, 3): from sets import Set as set Note also that the definition of tuple comparison help you here: if (2, 1, 1) sys.version_info (2, 3): ... elif (2, 5) = sys.version_info = (2, 6, 2, 'final'): ... else: print('Untested') --Scott David Daniels scott.dani...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: multi-core software
Jeff M. mass...@gmail.com writes: On Jun 9, 9:08 pm, Arved Sandstrom dces...@hotmail.com wrote: Jon Harrop wrote: Arved Sandstrom wrote: Jon, I do concurrent programming all the time, as do most of my peers. Way down on the list of why we do it is the reduction of latency. What is higher on the list? Correctness. IMO, that response is a bit of a cop-out. Correctness is _always_ most important, no matter what application you are creating; without it, you don't have a job and the company you work for goes out of business. But, assuming that your program works and does what it's supposed to, I agree with Jon that performance needs to be right near the top of the list of concerns. Why? Performance isn't about looking good as a programmer, or having fun making a function run in 15 cycles instead of 24, or coming up with some neat bit packing scheme so that your app now only uses 20K instead of 200K. Performance is - pure and simple - about one thing only: money. Programmer time is vastly more expensive than CPU time, so the money argument often leads to slow (low performance) solutions as long as they are good enough because developing a faster solution would mean spending more valuable programmer time at a cost that cannot be recovered over the life cycle of the product in question. That being said, there are plenty of situations where performance obviously does matter a great deal -- as you correctly pointed out. (It all depends on the above mentioned product in question and the nature of its life cycle.) Matthias -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing dictionary values rather than references
Amit Dor-Shifer wrote: Hi all. I'd like to print-out a dictionary of objects. The printed values are references. How Do I print the actual objects. Thanks, Amit How about this: class MyClass: def __str__(self): return str(self.__dict__) def __repr__(self): return str(self.__dict__) if __name__ == '__main__': my_dict = dict() classA = MyClass() setattr(classA, attr-1, val-1) my_dict['a']= classA print my_dict ''' Actual output: {'attr-1': 'val-1'}''' Though I'm guessing someone is going to say that this is not how repr is supposed be used. See this for more information: http://docs.python.org/reference/datamodel.html#object.__repr__ Cheers, Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing dictionary values rather than references
On Jun 10, 10:19 am, Amit Dor-Shifer ami...@oversi.com wrote: Hi all. I'd like to print-out a dictionary of objects. The printed values are references. How Do I print the actual objects. class MyClass: def __str__(self): return str(self.__dict__) if __name__ == '__main__': dict = dict() classA = MyClass() setattr(classA, attr-1, val-1) dict['a']= classA print classA ''' Desired output: {'attr-1': 'val-1'}''' print dict ''' Actual output: {'a': __main__.MyClass instance at 0x79cfc8}''' Thanks, Amit class MyClass: def __repr__(self): # --- see http://docs.python.org/library/functions.html#repr return str(self.__dict__) HTH, Jeff mcjeff.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How to insert string in each match using RegEx iterator
504cr...@gmail.com wrote: I wonder if you (or anyone else) might attempt a different explanation for the use of the special sequence '\1' in the RegEx syntax. The Python documentation explains: \number Matches the contents of the group of the same number. Groups are numbered starting from 1. For example, (.+) \1 matches 'the the' or '55 55', but not 'the end' (note the space after the group). This special sequence can only be used to match one of the first 99 groups. If the first digit of number is 0, or number is 3 octal digits long, it will not be interpreted as a group match, but as the character with octal value number. Inside the '[' and ']' of a character class, all numeric escapes are treated as characters. In practice, this appears to be the key to the key device to your clever solution: re.compile(r(\d+)).sub(rINSERT \1, string) 'abc INSERT 123 def INSERT 456 ghi INSERT 789' re.compile(r(\d+)).sub(rINSERT , string) 'abc INSERT def INSERT ghi INSERT ' I don't, however, precisely understand what is meant by the group of the same number -- or maybe I do, but it isn't explicit. Is this just a shorthand reference to match.group(1) -- if that were valid -- implying that the group match result is printed in the compile execution? If I understand you correctly you are right. Another example: re.compile(r([a-z]+)(\d+)).sub(rnumber=\2 word=\1, a1 zzz42) 'number=1 word=a number=42 word=zzz' For every match of [a-z]+\d+ in the original string \1 in number=\2 word=\1 is replaced with the actual match for [a-z]+ and \2 is replaced with the actual match for \d+. The result, e. g. number=1 word=a, is then used to replace the actual match for group 0, i. e. a1 in the example. Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: retrieve bitwise float representation
Ulrich Eckhardt wrote: I need to pack a floating point value into a vector of 32-bit unsigned values in IEEE format. Further, I maintain a CRC32 checksum for integrity checking. For the latter, I actually need the float as integral value. What I currently do is ... pack and unpack the float using struct What I'm wondering is whether there are any better or alternative ways to achieve this, the overhead now seems enormous and unnecessary to me here. If you have just a few values, ignore the overhead. Do you really need more cycles for minesweeper? If you have a vector, look into writing from the array directly -- using cStringIO if you need to get to the result within Python, rather than as an I/O format. --Scott David Daniels scott.dani...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: multi-core software
Jon Harrop j...@ffconsultancy.com writes: I'm not being facetious. I write applications that run on application servers, and from time to time I have had to write various special purpose servers. This kind of programming is all about managing concurrent execution of computations. The overarching concern is reliability and correct function. For many corporate situations, even with hundreds of users, the actual load at any instant is low enough that the various servers involved are nowhere close to being stressed out - performance is a secondary issue. In other words, without concurrency the latency would be so high that you would consider the program to be wrong. However you cut it, the real reason is latency. I don't think that follows, if there is two-way communication and dependency between the servers, combined with lack of control over when any particular server decides to initiate an outgoing request. Stuff may have to happen concurrently to avoid complete deadlock. -- http://mail.python.org/mailman/listinfo/python-list
Re: Connection tester
Sparky wrote: Hey! I am developing a small application that tests multiple websites and compares their response time. Some of these sites do not respond to a ping and, for the measurement to be standardized, all sites must have the same action preformed upon them. Another problem is that not all of the sites have the same page size and I am not interested in how long it takes to load a page but instead just how long it takes for the website to respond. Finally, I am looking to keep this script platform independent, if at all possible. Yes, lots of people block ICMP so you can't use it to reliably tell whether a machine is there or not. At least three possible solutions. 1) Perform a HEAD request against the document root. This is likely to be a static page and making it a HEAD request will make most responses take similar times. 2) Perform an OPTIONS request as specified in the RFC below for the * resource. This doesn't always work. 3) Perform a request you believe will fail so that you are provided with a 4XX error code, the only time this should take any appreciable time is when someone has cute server-generated error pages. HTTP/1.1 RFC - http://www.ietf.org/rfc/rfc2616.txt n -- http://mail.python.org/mailman/listinfo/python-list
Re: How to escape # hash character in regex match strings
504cr...@gmail.com wrote: I've encountered a problem with my RegEx learning curve -- how to escape hash characters # in strings being matched, e.g.: string = re.escape('123#abc456') match = re.match('\d+', string) print match _sre.SRE_Match object at 0x00A6A800 print match.group() 123 The correct result should be: 123456 .join(re.findall(\d+, 123#abc456)) '123456' I've tried to escape the hash symbol in the match string without result. Any ideas? Is the answer something I overlooked in my lurching Python schooling? re.escape() is used to build the regex from a string that may contain characters that have a special meaning in regular expressions but that you want to treat as literals. You can for example search for rC:\dir with re.compile(re.escape(rC:\dir)).findall(rC:\dir C:7ir) ['C:\\dir'] Without escaping you'd get re.compile(rC:\dir).findall(rC:\dir C:7ir) ['C:7ir'] Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: xml application advice
William Purcell wrote: I am writing a application to calculate pressure drop for a piping network. Namely a building sprinkler system. This will be a command line program at first with the system described in xml (at least that is how I think I want to do it). An important part of this calculation is finding the 'hydraulically most remote' sprinkler. This is something that I could specify with an attribute for now and later think about how to automate it. I need to walk through the dom tree until I find a node of type sprinkler that has an attribute of hydraulically_most_remote with a value of True. After I find this I need to break the itterator/for loop and then start walking backwards keeping a running total of the pressure drop until I reach a node that has multiple pipesections and then walk to the end of each branch and calculate the pressure drop, and then add them to the branch that contained the hydraulically most remote sprinkler, and then move on, repeating this until I walk all the way back to the inflow node. I am having trouble finding a decent python/xml resource on the web. I have ordered Python XML by Jones and Drake, but I am anxious to get something started. The only decent online resource that I can seem to find is http://pyxml.sourceforge.net/topics/howto/xml-howto.html which doesn't seem to be a very comprehensive how-to. Do demonstrate just about everything I know about xml and python I attached t.py and ex.xml. Another thing that is confusing is dir(walker) does not show walker having an attribute currentNode and dir(walker.currentNode) does not show walker.currentNode having an attribute tagName. I'd probably start with a few python classes representing the sprinkler system. The exact layout may change a few times until you have found one that makes your questions clear and the calculations as easy as possible. You can then add a read_model_from_file() function converting the xml into your model using ElementTree or its close relative lxml. My guess is that it'll be a lot more fun this way... Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Connection tester
On Jun 10, 10:26 am, Sparky samnspa...@gmail.com wrote: Hey! I am developing a small application that tests multiple websites and compares their response time. Some of these sites do not respond to a ping and, for the measurement to be standardized, all sites must have the same action preformed upon them. Another problem is that not all of the sites have the same page size and I am not interested in how long it takes to load a page but instead just how long it takes for the website to respond. Finally, I am looking to keep this script platform independent, if at all possible. Here is the code: try: # Get the starting time origTime = time.time() # Create the socket connection and then close s = socket.socket(AF_INET, SOCK_STREAM) s.connect((targetIP, port)) s.send(GET / HTTP/1.0\r\n\r\n) result = s.recv(1024) s.shutdown(SHUT_RDWR) except: result = # Check for problems and report back the time if result == : return Result((time.time() - origTime) * 1000, True) else: return Result((time.time() - origTime) * 1000, False) Result is just an object that holds the time it took for the method to finish and if there were any errors. What I am worried about is that the socket is potentially closed before the website can finish sending in all the data. Does anyone have any suggestions or is the script fine as it is? ICMP and application-level response times are two different animals. Are you interested in simply whether or not a server is up and responding, or do you care about the actual response time and performance of the web site you're checking? I did something like this recently and there were a few different metrics we wound up using. Connect time, first-byte, page download, DNS resolution, and so on. Since you don't care about any of that, just use a HEAD request. It will return the response headers, but as per specification it will not return a message body. Take a look at http://www.w3.org/Protocols/ rfc2616/rfc2616-sec9.html for a full primer on the different verbs. A somewhat simplistic alternative would be to connect to port 80 on the destination server and drop the connection once it has been made. This will tell you how long it took to establish a TCP session and that something is indeed listening on the destination port. That's slightly more information than you would get from an ICMP reply. -- http://mail.python.org/mailman/listinfo/python-list
Python-URL! - weekly Python news and links (Jun 10)
QOTW: Most power systems math can be summed this way: take a really big number and multiply by the square root of two. - iceowl http://everything2.com/index.pl?node_id=1348321 The chuzer project provides a means for severely disabled people to express their most basic needs. The project desperately needs help, or it will die. http://comments.gmane.org/gmane.comp.python.general/625159 Never heard of this - guys competing to see whose is shorter! http://comments.gmane.org/gmane.comp.python.general/625868 Correctly implementing __copy__ and __deepcopy__ with multiple inheritance: http://comments.gmane.org/gmane.comp.python.general/625072 http://comments.gmane.org/gmane.comp.python.general/625291 An overly complicated proposed function leads to discuss good API design principles: http://comments.gmane.org/gmane.comp.python.general/625433 List, tuple, set: when to use each type: http://comments.gmane.org/gmane.comp.python.general/625942 Comparing programming languages: how to do the same thing on several languages: http://comments.gmane.org/gmane.comp.python.general/625637 Generating a dynamic plot of x-y data: http://comments.gmane.org/gmane.comp.python.general/625346 __hash__, __eq__, dictionaries, and the big-Oh notation: http://comments.gmane.org/gmane.comp.python.general/625034 The unladen-swallow project, the LLVM virtual machine, and how they relate to the future of CPython: http://comments.gmane.org/gmane.comp.python.general/625493 Accessing data located in the filesystem, inside a package directory: http://comments.gmane.org/gmane.comp.python.general/625209 Closures in Python don't work exactly the same as in other languages: http://comments.gmane.org/gmane.comp.python.general/625475 How to iterate over several lists, one after another? http://comments.gmane.org/gmane.comp.python.general/625532 Everything Python-related you want is probably one or two clicks away in these pages: Python.org's Python Language Website is the traditional center of Pythonia http://www.python.org Notice especially the master FAQ http://www.python.org/doc/FAQ.html PythonWare complements the digest you're reading with the marvelous daily python url http://www.pythonware.com/daily Just beginning with Python? This page is a great place to start: http://wiki.python.org/moin/BeginnersGuide/Programmers The Python Papers aims to publish the efforts of Python enthusiasts: http://pythonpapers.org/ The Python Magazine is a technical monthly devoted to Python: http://pythonmagazine.com Readers have recommended the Planet sites: http://planetpython.org http://planet.python.org comp.lang.python.announce announces new Python software. Be sure to scan this newsgroup weekly. http://groups.google.com/group/comp.lang.python.announce/topics Python411 indexes podcasts ... to help people learn Python ... Updates appear more-than-weekly: http://www.awaretek.com/python/index.html The Python Package Index catalogues packages. http://www.python.org/pypi/ Much of Python's real work takes place on Special-Interest Group mailing lists http://www.python.org/sigs/ Python Success Stories--from air-traffic control to on-line match-making--can inspire you or decision-makers to whom you're subject with a vision of what the language makes practical. http://www.pythonology.com/success The Python Software Foundation (PSF) has replaced the Python Consortium as an independent nexus of activity. It has official responsibility for Python's development and maintenance. http://www.python.org/psf/ Among the ways you can support PSF is with a donation. http://www.python.org/psf/donations/ The Summary of Python Tracker Issues is an automatically generated report summarizing new bugs, closed ones, and patch submissions. http://search.gmane.org/?author=status%40bugs.python.orggroup=gmane.comp.python.develsort=date Although unmaintained since 2002, the Cetus collection of Python hyperlinks retains a few gems. http://www.cetus-links.org/oo_python.html Python FAQTS http://python.faqts.com/ The Cookbook is a collaborative effort to capture useful and interesting recipes. http://code.activestate.com/recipes/langs/python/ Many Python conferences around the world are in preparation. Watch this space for links to them. Among several Python-oriented RSS/RDF feeds available, see: http://www.python.org/channews.rdf For more, see:
Re: xml application advice
Diez B. Roggisch wrote: William Purcell wrote: I am writing a application to calculate pressure drop for a piping network. Namely a building sprinkler system. This will be a command line program at first with the system described in xml (at least that is how I think I want to do it). Use lxml2 and xpath. http://codespeak.net/lxml/ http://codespeak.net/lxml/xpathxslt.html This looks promising. I will start playing around with it and see what I can come up with. Thanks for the example. Peter Otten wrote: I'd probably start with a few python classes representing the sprinkler system. The exact layout may change a few times until you have found one that makes your questions clear and the calculations as easy as possible. You can then add a read_model_from_file() function converting the xml into your model using ElementTree or its close relative lxml. My guess is that it'll be a lot more fun this way... This was my initial plan, but I have never messed with xml and didn't know if it was what I wanted. I have messed around with plistlib on a mac. If I remember correctly the reader in plistlib returns a dict so I thought I would be getting a dict from an xml reader (but maybe xml and plist aren't as close as I thought). Reading xml seems more complicated than I initially expected, but probably rightfully so. But I digress. I will take your advice and start with some classes and then work on getting the data to my classes. -- http://mail.python.org/mailman/listinfo/python-list
Re: Career Track: Computer Programmer
On Mon, 8 Jun 2009 07:49:42 -0700 (PDT), youssef_edward3...@yahoo.com youssef_edward3...@yahoo.com wrote: Roles and Responsibilities : The primary role of a Computer Programmer is to write programs according to the instructions determined primarily by computer software engineers and systems analysts. I hope this is a direct quote from a 1976 issue of some sleazy business magazine, not something anyone believes in today. Except for the systems analysts, maybe. In a nutshell, Computer Programmers are the ones that take the completed designs and convert them into the instructions that the computer can actually follow. That's not a programmer, that's a compiler. Or (to at least *pretend* to be on topic) that's the Python interpreter. /Jorgen -- // Jorgen Grahn grahn@Ph'nglui mglw'nafh Cthulhu \X/ snipabacken.se R'lyeh wgah'nagl fhtagn! -- http://mail.python.org/mailman/listinfo/python-list
Re: xml application advice
William Purcell wrote: I am writing a application to calculate pressure drop for a piping network. Namely a building sprinkler system. This will be a command line program at first with the system described in xml If you are going to be doing a lot of tree walking, try etree. Simple example: import xml.etree.ElementTree as ET # or wherever you get ElementTree def find_remote_and_path(node, path): for child in node: for result in walks(child, path + [node]): yield result if node.tag == 'node' and node.get('hydraulically_most_remote' ) == 'True': yield node, path tree = ET.parse('ex.xml') for node, path in find_remote_and_path(tree.getroot(), []): for t in path: print '', t.tag, t.get('id', '-') print node.tag, ', '.join(sorted('%s=%r' % pair for pair in node.items())) --Scott David Daniels scott.dani...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: xml application advice
On Wed, 10 Jun 2009 08:57:42 -0500, William Purcell flye...@gmail.com wrote: ... I am writing a application to calculate pressure drop for a piping network. Namely a building sprinkler system. This will be a command line program at first with the system described in xml (at least that is how I think I want to do it). How about (re)using the dot graph language from Graphviz? It's a file format for describing directed graphs, which I suppose a sprinkler system is. It might fit; it might not. An important part of this calculation is finding the 'hydraulically most remote' sprinkler. This is something that I could specify with an attribute for now and later think about how to automate it. I need to walk through the dom tree until I find a node of type sprinkler that has an attribute of hydraulically_most_remote with a value of True. After I find this I need to break the itterator/for loop and then start walking backwards keeping a running total of the pressure drop until I reach a node that has multiple pipesections and then walk to the end of each branch and calculate the pressure drop, and then add them to the branch that contained the hydraulically most remote sprinkler, and then move on, repeating this until I walk all the way back to the inflow node. I am having trouble finding a decent python/xml resource on the web. I have ordered Python XML by Jones and Drake, but I am anxious to get something started. If what you're interested in is to get real work done, why decide to make XML a showstopper? I see two tasks: (a) transforming a text file description of a sprinkler system into a Python data structure, and (b) implementing algorithms to find out important stuff about such a data structure. You do not need (a) before you can do (b). You can even have Python as your input format, and eval() the file. Crude and insecure, but it works, at almost zero cost. /Jorgen -- // Jorgen Grahn grahn@Ph'nglui mglw'nafh Cthulhu \X/ snipabacken.se R'lyeh wgah'nagl fhtagn! -- http://mail.python.org/mailman/listinfo/python-list
Re: xml application advice
Scott David Daniels wrote: William Purcell wrote: I am writing a application to calculate pressure drop for a piping network. Namely a building sprinkler system. This will be a command line program at first with the system described in xml If you are going to be doing a lot of tree walking, try etree. Simple example: import xml.etree.ElementTree as ET # or wherever you get ElementTree def find_remote_and_path(node, path): for child in node: for result in walks(child, path + [node]): yield result if node.tag == 'node' and node.get('hydraulically_most_remote' ) == 'True': yield node, path tree = ET.parse('ex.xml') for node, path in find_remote_and_path(tree.getroot(), []): for t in path: print '', t.tag, t.get('id', '-') print node.tag, ', '.join(sorted('%s=%r' % pair for pair in node.items())) --Scott David Daniels scott.dani...@acm.org Scott, Thanks for the reply. I am having a little trouble finding where to import `walks` from. Bill -- http://mail.python.org/mailman/listinfo/python-list
RE: How to escape # hash character in regex match strings
Maybe a using a Unicode equiv of # would do the trick. -Original Message- From: python-list-bounces+david.shapiro=sas@python.org [mailto:python-list-bounces+david.shapiro=sas@python.org] On Behalf Of Peter Otten Sent: Wednesday, June 10, 2009 11:32 AM To: python-list@python.org Subject: Re: How to escape # hash character in regex match strings 504cr...@gmail.com wrote: I've encountered a problem with my RegEx learning curve -- how to escape hash characters # in strings being matched, e.g.: string = re.escape('123#abc456') match = re.match('\d+', string) print match _sre.SRE_Match object at 0x00A6A800 print match.group() 123 The correct result should be: 123456 .join(re.findall(\d+, 123#abc456)) '123456' I've tried to escape the hash symbol in the match string without result. Any ideas? Is the answer something I overlooked in my lurching Python schooling? re.escape() is used to build the regex from a string that may contain characters that have a special meaning in regular expressions but that you want to treat as literals. You can for example search for rC:\dir with re.compile(re.escape(rC:\dir)).findall(rC:\dir C:7ir) ['C:\\dir'] Without escaping you'd get re.compile(rC:\dir).findall(rC:\dir C:7ir) ['C:7ir'] Peter -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: xml application advice
William Purcell wrote: Scott David Daniels wrote: William Purcell wrote: I am writing a application to calculate pressure drop for a piping network. Namely a building sprinkler system. This will be a command line program at first with the system described in xml If you are going to be doing a lot of tree walking, try etree. Simple example: import xml.etree.ElementTree as ET # or wherever you get ElementTree def find_remote_and_path(node, path): for child in node: for result in walks(child, path + [node]): yield result if node.tag == 'node' and node.get('hydraulically_most_remote' ) == 'True': yield node, path tree = ET.parse('ex.xml') for node, path in find_remote_and_path(tree.getroot(), []): for t in path: print '', t.tag, t.get('id', '-') print node.tag, ', '.join(sorted('%s=%r' % pair for pair in node.items())) --Scott David Daniels scott.dani...@acm.org Scott, Thanks for the reply. I am having a little trouble finding where to import `walks` from. Bill Sorry, renamed and forgot to repaste. walks is just find_remote_and_path -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On Jun 10, 4:01 am, Mark Dickinson dicki...@gmail.com wrote: On Jun 10, 7:25 am, John Yeung gallium.arsen...@gmail.com wrote: On Jun 10, 1:52 am, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Tue, 09 Jun 2009 22:21:26 -0700, John Yeung wrote: Therefore, to me the most up-to-date docs (which say that uniform(a, b) returns a float in the closed interval [a, b]) is closer to correct than before, but still fails to point out the full subtlety of the behavior. Which is? That uniform(a, b) will return a random float in the semi-open interval [a, b) for certain values of a and b; and in the closed interval [a, b] for other values of a and b. (Swap a and b if a b.) To me, the fact that you sometimes get a semi-open interval and sometimes a closed interval is worth noting in the docs. Do you want to submit a doc patch? For practical purposes, I think you'd be hard-pressed to find a statistical test that could reliably distinguish between a large sample of values from random.uniform(a, b) and a sample from a 'perfect' uniform distribution on the closed interval [a, b]. It's true that there are values of a and b such that random.uniform(a, b) can never produce b. So, the 2.6.2 documentation is STILL wrong. Before it implied it was ALWAYS a semi-open interval, and now it says it's ALWAYS a closed interval. But neither is correct. I think a doc patch is definitely warranted. But for given a and b, not too close together, there may be many other values that can't be produced as well, so it hardly seems worth pointing out one particular value that can never be produced. Example: on a typical system there are almost 2**62 floats in the range [0, 1); the vast majority of these can never be produced by random.random(), which can only ever return one of 2**53 distinct values (it essentially just returns a value of the form n/2**53 with n an integer in the range [0, 2**53)). So random.uniform(0, 1) will miss lots of possible values. On the other hand, it's easy to find examples of a and b such that random.uniform(a, b) has a significant chance of producing b. For example, take a = 10**16, b = 10**16 + 4, then there's about a 1 in 4 chance of getting b. Or for a more extreme example, simply take a = b. Hence the doc change. Mark -- http://mail.python.org/mailman/listinfo/python-list
Re: xml application advice
If what you're interested in is to get real work done, why decide to make XML a showstopper? I see two tasks: (a) transforming a text file description of a sprinkler system into a Python data structure, and (b) implementing algorithms to find out important stuff about such a data structure. You do not need (a) before you can do (b). You can even have Python as your input format, and eval() the file. Crude and insecure, but it works, at almost zero cost. While I certainly agree that XML often means more trouble than it's worth, I don't concur in this concrete case. If the OP has a tree-structure, XPath is a *very* powerful way to operate on that - and as he seems to have questions like get the one node with the attribute X, then the first one above that having more than one child of kind Y, it can get in handy. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On Jun 10, 6:21 pm, Mensanator mensana...@aol.com wrote: So, the 2.6.2 documentation is STILL wrong. Before it implied it was ALWAYS a semi-open interval, and now it says it's ALWAYS a closed interval. But neither is correct. Exactly which bit of the 2.6.2 documentation do you think is incorrect? The documentation for random.uniform says: Return a random floating point number N such that a = N = b for a = b and b = N = a for b a. And that's precisely what it does. Nowhere does the documentation say that *every* floating-point number N in the interval [a, b] can occur. Mark -- http://mail.python.org/mailman/listinfo/python-list
Re: Connection tester
On Jun 10, 10:01 am, Jeff McNeil j...@jmcneil.net wrote: On Jun 10, 10:26 am, Sparky samnspa...@gmail.com wrote: Hey! I am developing a small application that tests multiple websites and compares their response time. Some of these sites do not respond to a ping and, for the measurement to be standardized, all sites must have the same action preformed upon them. Another problem is that not all of the sites have the same page size and I am not interested in how long it takes to load a page but instead just how long it takes for the website to respond. Finally, I am looking to keep this script platform independent, if at all possible. Here is the code: try: # Get the starting time origTime = time.time() # Create the socket connection and then close s = socket.socket(AF_INET, SOCK_STREAM) s.connect((targetIP, port)) s.send(GET / HTTP/1.0\r\n\r\n) result = s.recv(1024) s.shutdown(SHUT_RDWR) except: result = # Check for problems and report back the time if result == : return Result((time.time() - origTime) * 1000, True) else: return Result((time.time() - origTime) * 1000, False) Result is just an object that holds the time it took for the method to finish and if there were any errors. What I am worried about is that the socket is potentially closed before the website can finish sending in all the data. Does anyone have any suggestions or is the script fine as it is? ICMP and application-level response times are two different animals. Are you interested in simply whether or not a server is up and responding, or do you care about the actual response time and performance of the web site you're checking? I did something like this recently and there were a few different metrics we wound up using. Connect time, first-byte, page download, DNS resolution, and so on. Since you don't care about any of that, just use a HEAD request. It will return the response headers, but as per specification it will not return a message body. Take a look at http://www.w3.org/Protocols/ rfc2616/rfc2616-sec9.html for a full primer on the different verbs. A somewhat simplistic alternative would be to connect to port 80 on the destination server and drop the connection once it has been made. This will tell you how long it took to establish a TCP session and that something is indeed listening on the destination port. That's slightly more information than you would get from an ICMP reply. Thank you all for your responses. I will play with everything but the HEAD request seems to be what I was looking for. Sam -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On Jun 10, 12:37 pm, Mark Dickinson dicki...@gmail.com wrote: On Jun 10, 6:21 pm, Mensanator mensana...@aol.com wrote: So, the 2.6.2 documentation is STILL wrong. Before it implied it was ALWAYS a semi-open interval, and now it says it's ALWAYS a closed interval. But neither is correct. Exactly which bit of the 2.6.2 documentation do you think is incorrect? The documentation for random.uniform says: Return a random floating point number N such that a = N = b for a = b and b = N = a for b a. And that's precisely what it does. I didn't say it didn't. Nowhere does the documentation say that *every* Unless qualified otherwise, that statement implies for all (a,b). floating-point number N in the interval [a, b] can occur. Mark -- http://mail.python.org/mailman/listinfo/python-list
Re: multi-core software
On Jun 10, 12:49 pm, Seamus MacRae smacrae...@live.ca.invalid wrote: Jeff M. wrote: On Jun 9, 9:08 pm, Arved Sandstrom dces...@hotmail.com wrote: Jon Harrop wrote: Arved Sandstrom wrote: Jon, I do concurrent programming all the time, as do most of my peers. Way down on the list of why we do it is the reduction of latency. What is higher on the list? Correctness. IMO, that response is a bit of a cop-out. Correctness is _always_ most important, no matter what application you are creating; without it, you don't have a job and the company you work for goes out of business. And when, exactly, did Microsoft go out of business? I hadn't heard it mentioned in the news. :) Touche. :) Jeff M. -- http://mail.python.org/mailman/listinfo/python-list
Re: multi-core software
Jeff M. wrote: On Jun 9, 9:08 pm, Arved Sandstrom dces...@hotmail.com wrote: Jon Harrop wrote: Arved Sandstrom wrote: Jon, I do concurrent programming all the time, as do most of my peers. Way down on the list of why we do it is the reduction of latency. What is higher on the list? Correctness. IMO, that response is a bit of a cop-out. Correctness is _always_ most important, no matter what application you are creating; without it, you don't have a job and the company you work for goes out of business. And when, exactly, did Microsoft go out of business? I hadn't heard it mentioned in the news. :) -- http://mail.python.org/mailman/listinfo/python-list
Re: multi-core software
Jeff M. wrote: On Jun 9, 9:08 pm, Arved Sandstrom dces...@hotmail.com wrote: Jon Harrop wrote: Arved Sandstrom wrote: Jon, I do concurrent programming all the time, as do most of my peers. Way down on the list of why we do it is the reduction of latency. What is higher on the list? Correctness. IMO, that response is a bit of a cop-out. Correctness is _always_ most important, no matter what application you are creating; without it, you don't have a job and the company you work for goes out of business. PC / Video Games definitely fall out of the correctness. As long as the game does not crash your XBOX/PS3/Whatever for certain amount of time, and behaves well then, it's fine. Bugs are already part of the genre. In reality you can't ship on time, there are always BUGS :) Most important thing in games is (at least for large percent of them) speed of graphics - fluid 60fps, or stable 30fps. But, assuming that your program works and does what it's supposed to, I agree with Jon that performance needs to be right near the top of the list of concerns. Why? Performance isn't about looking good as a programmer, or having fun making a function run in 15 cycles instead of 24, or coming up with some neat bit packing scheme so that your app now only uses 20K instead of 200K. Performance is - pure and simple - about one thing only: money. Programs that use more memory require more money for the hardware of every user. Programs that run slower eat more time per day. If you have 100,000 users, all doing an operation once per day that takes 20 seconds, being able to shave 5 seconds off that saves 5.78 man-days of work. Hell, for some applications, that 20 seconds is just startup time spent at a splash screen. Just imagine if every Google search took even 5 seconds to resolve, how much time would be wasted every day around the world - ignoring the fact that Google wouldn't exist if that were the case ;-). Obviously Google engineers work incredibly hard every day to ensure correct results, but performance better be right up there at the top of the list as well. Jeff M. -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On 2009-06-09 19:27, Mensanator wrote: On Jun 9, 6:12 pm, Robert Kernrobert.k...@gmail.com wrote: On 2009-06-09 18:05, Mensanator wrote: On Jun 9, 4:33 pm, Esmailebo...@hotmail.comwrote: Hi, random.random() will generate a random value in the range [0, 1). Is there an easy way to generate random values in the range [0, 1]? I.e., including 1? I am implementing an algorithm and want to stay as true to the original design specifications as possible though I suppose the difference between the two max values might be minimal. Thanks, Esmail ps: I'm confused by the docs for uniform(): random.uniform(a, b) Return a random floating point number N such that a= N= b for a= b That's wrong. Where did you get it? http://docs.python.org/library/random Ok, but the 2.6.1 docs say random.uniform(a, b) Return a random floating point number N such that a= N b for a= b and b= N a for b a. Is that a new feature of 2.6.2? As already pointed out, it's not really a new feature of the method, but rather a fix for the buggy documentation. Because of floating point arithmetic, one cannot guarantee that a+(b-a)*u is strictly in [a,b) even though u is strictly in [0,1). -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
Mensanator wrote: So, the 2.6.2 documentation is STILL wrong. Before it implied it was ALWAYS a semi-open interval, and now it says it's ALWAYS a closed interval. But neither is correct. If a x b is true, then a = x = b is true. But docs say that in general end point values might happen. They do not say that in every particular case, they will happen. A full technical discussion does not below in the docs, in my opinion. A wike article would be fine. tjr -- http://mail.python.org/mailman/listinfo/python-list
How do you insert an image into Powerpoint using python/win32?
I have python 3.0.1, and have downloaded pywin32 for python 3.x, aka build #213. I ran win32com.client.makepy.py on Microsoft Office 12.0 Object Library and Microsoft PowerPoint 12.0 Object Library. The output files were placed in win32com.gen_py. I renamed them as MSO.py and MSPPT.py, respectively. The following code is located in my c:\Python30 folder. Thinkter is used to prompt the user to choose the image that they want to put onto the slide. THe 10 pointed star was an experiment, to try to import something into the image. Here is the code as I currently have it: from tkinter import * import tkinter.filedialog as tkFileDialog import win32com.client # middleman/translator/messanger between windows and python import win32com.gen_py.MSO as MSO # contains constants refering to Microsoft Office Objects import win32com.gen_py.MSPPT as MSPPT # contains constants refering to Microsoft Office Power Point Objects g = globals() # a dictonary of global vlaues, that will be the constants of the two previous imports for c in dir(MSO.constants): g[c] = getattr(MSO.constants, c) # globally define these for c in dir(MSPPT.constants): g[c] = getattr(MSPPT.constants, c) Application = win32com.client.Dispatch(PowerPoint.Application) Application.Visible = True # shows what's happening, not required, but helpful for now Presentation = Application.Presentations.Add() # adds a new presentation Slide1 = Presentation.Slides.Add(1, ppLayoutBlank) # new slide, at beginning TenptStr = Slide1.Shapes.AddShape(msoShape10pointStar, 100, 100, 200, 200) pictName = tkFileDialog.askopenfilename(title=Please Select the Image you wish to load) print(pictName) Pict1 = Slide1.Shapes.AddPicture(FileName=pictName, LinkToFile=False, SaveWithDocument=True, Left=100, Top=100, Width=200, Height=200) this is the error: Traceback (most recent call last): File C:\Python30\PowerPointEditer.py, line 21, in module Pict1 = Slide1.Shapes.AddPicture(FileName=pictName, LinkToFile=False, SaveWithDocument=True, Left=100, Top=100, Width=200, Height=200)#pictName, pictName, 200, 200, 200, 200) File C:\Python30\lib\site-packages\win32com\gen_py\MSPPT.py, line 8544, in AddPicture , Height) pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, The specified file wasn't found., None, 0, -2147024809), None) It seems I'm passing AddPictures the wrong varibles Thanks. -- Jonathan Cherry jcher...@gatech.edu Student Computer Science Major (working on it) Georgia Institute of Technology -- http://mail.python.org/mailman/listinfo/python-list
Restart the interactive python shell like in IDLE
Is there a way to reproduce the behavior of IDLE's restart shell ability by using a function? I thought there would be since you can exit python by executing the simple quit() function I thought there would be an equally simple function name something like restart(). I'd prefer something like this as opposed to having to exit the shell and then start it up again to refresh it. -- Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On Jun 10, 6:57 pm, Mensanator mensana...@aol.com wrote: On Jun 10, 12:37 pm, Mark Dickinson dicki...@gmail.com wrote: On Jun 10, 6:21 pm, Mensanator mensana...@aol.com wrote: So, the 2.6.2 documentation is STILL wrong. Before it implied it was ALWAYS a semi-open interval, and now it says it's ALWAYS a closed interval. But neither is correct. Exactly which bit of the 2.6.2 documentation do you think is incorrect? The documentation for random.uniform says: Return a random floating point number N such that a = N = b for a = b and b = N = a for b a. And that's precisely what it does. I didn't say it didn't. Nowhere does the documentation say that *every* Unless qualified otherwise, that statement implies for all (a,b). Sure. For all a = b, it's true that a = uniform(a, b) = b. And similarly for b = a. I really don't see the problem: the documentation is both technically correct and useful in practice. The assertion implicit in the name and description of the random.uniform function is that, to a very good approximation, the values produced by random.uniform(a, b) will be uniformly distributed on the closed interval [a, b]. And that's true (at least within the limits of floating-point arithmetic). Can you think of a single practical situation where it would matter that, for some values of a and b, uniform(a, b) can never produce the value b? Mark -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On 2009-06-10 13:53, Terry Reedy wrote: Mensanator wrote: So, the 2.6.2 documentation is STILL wrong. Before it implied it was ALWAYS a semi-open interval, and now it says it's ALWAYS a closed interval. But neither is correct. If a x b is true, then a = x = b is true. But docs say that in general end point values might happen. They do not say that in every particular case, they will happen. I'm not so sure. Context is important. When discussing the bounds of random number generators, specifying = instead of strongly suggests that the bound is one of the possible results. I've had to read a lot of random number generator documentation in my time. To take the point to absurdity, it would be wrong for the function to return just values within (a+0.25*b, a+0.75*b) even though the docs just say that the result will be between a and b. A full technical discussion does not below in the docs, in my opinion. A wike article would be fine. True. However, a brief note that Due to floating point arithmetic, for some values of a and b, b may or may not be one of the possible generated results. might be worthwhile. The actual details of *why* this is the case can be discussed elsewhere. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: SPAM-LOW: Re: Function/method returning list of chars in string?
Carl Banks wrote: Sometimes alternate constructors are needed when there is more than one possible way to create an instance from a given input. In the case of str(iterable), one could want either a string representing the iterable itself, just as with non-iterables, or a string representing the concatenated contents of the iterable. Str.join implements the second choice, with an added string parameter to allow a constant string to be interpolated between the joined items. But then how do you rationalize str.split(), which is a method of str but a constructor of list? Str.join takes any iterable of strings and constructs a string. Only str knows how to do that, though it could have a built-in that called a hypothetical .__join__ method. However, Python started with just one string type and there does not seem much use for joining an indefinite number of tuples or lists. (In any case, there is no string equivalent of list.extend, which can be used for joining multiple lists.) Str.split only takes a string arg and splits it up. Only str should know how to split a string. That it returns the pieces as a list is a historical artifact. I could have returned a tuple. It could have been changed in 3.0 to return an iterater, like map and others were. If Python were being designed today with iterators as a basic protocol, and without the baggage of back compatibility, I am fairly sure .split would return an iterator. Perhaps instead of worrying about symmetry all the time we should just accept the inevitability that things will always be asymmetric and impure from someone's perspective. Terry's symmetry is Hendrik's asymmetry and vice versa. That was one of my implied points ;-). Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
object reincarnation
Hello everyone, Since the real world objects often needs to be deleted even if they have some reference from some other object, I am going to use this approach to better model this situation, by cleaning up the attributes and assigning self.__class__ to a different class. Any comment on this approach. class Deleted(object): pass class RealWorldObj(object): def __init__(self, *args): self.attrs = args def getAttrs(self,): return self.attrs def delete(self,): del self.attrs self.__class__ = Deleted a = RealWorldObj(1,2,3) print a.attrs (1, 2, 3) a.delete() a __main__.Deleted object at 0x893ae2c a.attrs Traceback (most recent call last): File stdin, line 1, in module AttributeError: 'Deleted' object has no attribute 'attrs' -- http://mail.python.org/mailman/listinfo/python-list
Re: How do you insert an image into Powerpoint using python/win32?
Never mind, its just that the choose file option produces a file path with '/ rather than '\', and python cannot use the former. Thanks anyway - Original Message - From: jcher...@gatech.edu To: python-list@python.org Sent: Wednesday, June 10, 2009 2:22:22 PM GMT -05:00 US/Canada Eastern Subject: How do you insert an image into Powerpoint using python/win32? I have python 3.0.1, and have downloaded pywin32 for python 3.x, aka build #213. I ran win32com.client.makepy.py on Microsoft Office 12.0 Object Library and Microsoft PowerPoint 12.0 Object Library. The output files were placed in win32com.gen_py. I renamed them as MSO.py and MSPPT.py, respectively. The following code is located in my c:\Python30 folder. Thinkter is used to prompt the user to choose the image that they want to put onto the slide. THe 10 pointed star was an experiment, to try to import something into the image. Here is the code as I currently have it: from tkinter import * import tkinter.filedialog as tkFileDialog import win32com.client # middleman/translator/messanger between windows and python import win32com.gen_py.MSO as MSO # contains constants refering to Microsoft Office Objects import win32com.gen_py.MSPPT as MSPPT # contains constants refering to Microsoft Office Power Point Objects g = globals() # a dictonary of global vlaues, that will be the constants of the two previous imports for c in dir(MSO.constants): g[c] = getattr(MSO.constants, c) # globally define these for c in dir(MSPPT.constants): g[c] = getattr(MSPPT.constants, c) Application = win32com.client.Dispatch(PowerPoint.Application) Application.Visible = True # shows what's happening, not required, but helpful for now Presentation = Application.Presentations.Add() # adds a new presentation Slide1 = Presentation.Slides.Add(1, ppLayoutBlank) # new slide, at beginning TenptStr = Slide1.Shapes.AddShape(msoShape10pointStar, 100, 100, 200, 200) pictName = tkFileDialog.askopenfilename(title=Please Select the Image you wish to load) print(pictName) Pict1 = Slide1.Shapes.AddPicture(FileName=pictName, LinkToFile=False, SaveWithDocument=True, Left=100, Top=100, Width=200, Height=200) this is the error: Traceback (most recent call last): File C:\Python30\PowerPointEditer.py, line 21, in module Pict1 = Slide1.Shapes.AddPicture(FileName=pictName, LinkToFile=False, SaveWithDocument=True, Left=100, Top=100, Width=200, Height=200)#pictName, pictName, 200, 200, 200, 200) File C:\Python30\lib\site-packages\win32com\gen_py\MSPPT.py, line 8544, in AddPicture , Height) pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, The specified file wasn't found., None, 0, -2147024809), None) It seems I'm passing AddPictures the wrong varibles Thanks. -- Jonathan Cherry jcher...@gatech.edu Student Computer Science Major (working on it) Georgia Institute of Technology -- Jonathan Cherry jcher...@gatech.edu Student Computer Science Major (working on it) Georgia Institute of Technology -- http://mail.python.org/mailman/listinfo/python-list
Re: Function/method returning list of chars in string?
Robert Kern wrote: Important correction noted. But how did you find those? When I search for 'Shaw' with the search box (I tried it again), I only get a couple of other, irrelevant hits. Is the search box buggy? I suspect so. I knew of most of them already, and Googling site:pypi.python.org picked up the rest. Will use that in the future. In the meanwhile, I submitted a bug report. -- http://mail.python.org/mailman/listinfo/python-list
Re: Compiling Python3.1
What can I do about that? Remove the non-ASCII characters from db.h. Regards, Martin -- http://mail.python.org/mailman/listinfo/python-list
cleaning up an ASCII file?
Hi all, So I'm parsing an XML file returned from a database. However, the database entries have occasional non-ASCII characters, and this is crashing my parsers. Is there some handy function out there that will schlep through a file like this, and do something like fix the characters that it can recognize, and delete those that it can't? Basically, like the BBEdit convert to ASCII menu option under Text. I googled some on this, but nothing obvious came up that wasn't specific to fixing one or a few characters. Thanks! Nick -- Nicholas J. Matzke Ph.D. Candidate, Graduate Student Researcher Huelsenbeck Lab Center for Theoretical Evolutionary Genomics 4151 VLSB (Valley Life Sciences Building) Department of Integrative Biology University of California, Berkeley Lab websites: http://ib.berkeley.edu/people/lab_detail.php?lab=54 http://fisher.berkeley.edu/cteg/hlab.html Dept. personal page: http://ib.berkeley.edu/people/students/person_detail.php?person=370 Lab personal page: http://fisher.berkeley.edu/cteg/members/matzke.html Lab phone: 510-643-6299 Dept. fax: 510-643-6264 Cell phone: 510-301-0179 Email: mat...@berkeley.edu Mailing address: Department of Integrative Biology 3060 VLSB #3140 Berkeley, CA 94720-3140 - [W]hen people thought the earth was flat, they were wrong. When people thought the earth was spherical, they were wrong. But if you think that thinking the earth is spherical is just as wrong as thinking the earth is flat, then your view is wronger than both of them put together. Isaac Asimov (1989). The Relativity of Wrong. The Skeptical Inquirer, 14(1), 35-44. Fall 1989. http://chem.tufts.edu/AnswersInScience/RelativityofWrong.htm -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On Jun 10, 8:15 pm, Robert Kern robert.k...@gmail.com wrote: On 2009-06-10 13:53, Terry Reedy wrote: A full technical discussion does not below in the docs, in my opinion. A wike article would be fine. True. However, a brief note that Due to floating point arithmetic, for some values of a and b, b may or may not be one of the possible generated results. might be worthwhile. The actual details of *why* this is the case can be discussed elsewhere. I find it difficult to see how such a disclaimer would have any practical value, without also knowing *which* values of a and b are affected. It can certainly be useful to know that endpoints *aren't* included where that's true. For example, knowing that random.random() can never produce the value 1.0 means that one can safely generate a mean 1 exponential variate with -log(1-random.random()), without worrying about the possibility of taking log of 0. But I don't know why it would be useful to know that endpoints *are* sometimes included, without knowing exactly when. Mark -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing dictionary values rather than references
Amit Dor-Shifer wrote: Hi all. I'd like to print-out a dictionary of objects. The printed values are references. How Do I print the actual objects. You can only print string representations, as defined by type(ob).__str__ and type(ob).__repr__. class MyClass: def __str__(self): return str(self.__dict__) if __name__ == '__main__': dict = dict() Rebinding built-in names is a bad idea unless you *really* mean to replace the original. classA = MyClass() setattr(classA, attr-1, val-1) dict['a']= classA print classA ''' Desired output: {'attr-1': 'val-1'}''' print dict ''' Actual output: {'a': __main__.MyClass instance at 0x79cfc8}''' Thanks, Amit -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On 2009-06-10 14:46, Mark Dickinson wrote: On Jun 10, 8:15 pm, Robert Kernrobert.k...@gmail.com wrote: On 2009-06-10 13:53, Terry Reedy wrote: A full technical discussion does not below in the docs, in my opinion. A wike article would be fine. True. However, a brief note that Due to floating point arithmetic, for some values of a and b, b may or may not be one of the possible generated results. might be worthwhile. The actual details of *why* this is the case can be discussed elsewhere. I find it difficult to see how such a disclaimer would have any practical value, without also knowing *which* values of a and b are affected. It can certainly be useful to know that endpoints *aren't* included where that's true. For example, knowing that random.random() can never produce the value 1.0 means that one can safely generate a mean 1 exponential variate with -log(1-random.random()), without worrying about the possibility of taking log of 0. But I don't know why it would be useful to know that endpoints *are* sometimes included, without knowing exactly when. That's a fair point. However, one issue that hasn't been brought up is that it might be confusing to a user why random.random() returns values in a half-open interval while random.uniform() claims a closed interval. Even for reasonably sophisticated floating point users, it's not necessarily obvious that that is the reasoning behind the different claims. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: object reincarnation
Manavan wrote: Hello everyone, Since the real world objects often needs to be deleted even if they have some reference from some other object, I am going to use this approach to better model this situation, by cleaning up the attributes and assigning self.__class__ to a different class. Any comment on this approach. It might be easier to give the class a status variable so instances can be de-activated without destroying info and perhaps re-activated. class Deleted(object): pass class RealWorldObj(object): def __init__(self, *args): self.attrs = args def getAttrs(self,): return self.attrs def delete(self,): del self.attrs self.__class__ = Deleted a = RealWorldObj(1,2,3) print a.attrs (1, 2, 3) a.delete() a __main__.Deleted object at 0x893ae2c a.attrs Traceback (most recent call last): File stdin, line 1, in module AttributeError: 'Deleted' object has no attribute 'attrs' -- http://mail.python.org/mailman/listinfo/python-list
Re: cleaning up an ASCII file?
2009/6/10 Nick Matzke mat...@berkeley.edu: Hi all, So I'm parsing an XML file returned from a database. However, the database entries have occasional non-ASCII characters, and this is crashing my parsers. Is there some handy function out there that will schlep through a file like this, and do something like fix the characters that it can recognize, and delete those that it can't? Basically, like the BBEdit convert to ASCII menu option under Text. I googled some on this, but nothing obvious came up that wasn't specific to fixing one or a few characters. Thanks! Nick -- Nicholas J. Matzke Ph.D. Candidate, Graduate Student Researcher Huelsenbeck Lab Center for Theoretical Evolutionary Genomics 4151 VLSB (Valley Life Sciences Building) Department of Integrative Biology University of California, Berkeley Lab websites: http://ib.berkeley.edu/people/lab_detail.php?lab=54 http://fisher.berkeley.edu/cteg/hlab.html Dept. personal page: http://ib.berkeley.edu/people/students/person_detail.php?person=370 Lab personal page: http://fisher.berkeley.edu/cteg/members/matzke.html Lab phone: 510-643-6299 Dept. fax: 510-643-6264 Cell phone: 510-301-0179 Email: mat...@berkeley.edu Mailing address: Department of Integrative Biology 3060 VLSB #3140 Berkeley, CA 94720-3140 - [W]hen people thought the earth was flat, they were wrong. When people thought the earth was spherical, they were wrong. But if you think that thinking the earth is spherical is just as wrong as thinking the earth is flat, then your view is wronger than both of them put together. Isaac Asimov (1989). The Relativity of Wrong. The Skeptical Inquirer, 14(1), 35-44. Fall 1989. http://chem.tufts.edu/AnswersInScience/RelativityofWrong.htm -- http://mail.python.org/mailman/listinfo/python-list Hi, depending on the circumstances, there are probably more sophisticated ways (what does fix the characters mean?), but do you maybe think something like: uaáčbüêcîßd.encode(ascii, ignore) 'abcd' ? It might be important to ensure, that you won't loose any useful information; where are the unexpected characters coming from, or couldn't it possibly be fixed in that source? hth, vbr -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On 2009-06-10 15:32, Robert Kern wrote: On 2009-06-10 14:46, Mark Dickinson wrote: But I don't know why it would be useful to know that endpoints *are* sometimes included, without knowing exactly when. That's a fair point. However, one issue that hasn't been brought up is that it might be confusing to a user why random.random() returns values in a half-open interval while random.uniform() claims a closed interval. Even for reasonably sophisticated floating point users, it's not necessarily obvious that that is the reasoning behind the different claims. Basically, if we can forestall another tedious thread about imagined asymmetry and hemispatial neglect with a single sentence, I'm all for it. :-) -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: cleaning up an ASCII file?
Nick Matzke wrote: Hi all, So I'm parsing an XML file returned from a database. However, the database entries have occasional non-ASCII characters, and this is crashing my parsers. Is there some handy function out there that will schlep through a file like this, and do something like fix the characters that it can recognize, and delete those that it can't? Basically, like the BBEdit convert to ASCII menu option under Text. Lookup str.maketrans and str.translate, which can leave alone, replace, or delete each char. -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
Robert Kern robert.k...@gmail.com wrote: On 2009-06-10 14:46, Mark Dickinson wrote: On Jun 10, 8:15 pm, Robert Kernrobert.k...@gmail.com wrote: On 2009-06-10 13:53, Terry Reedy wrote: A full technical discussion does not below in the docs, in my opinion. A wike article would be fine. True. However, a brief note that Due to floating point arithmetic, for some values of a and b, b may or may not be one of the possible generated results. might be worthwhile. The actual details of *why* this is the case can be discussed elsewhere. I find it difficult to see how such a disclaimer would have any practical value [lots more badly wrapped text snipped... ] That's a fair point. However, one issue that hasn't been brought up is that it might be confusing to a user why random.random() returns values in a half-open interval while random.uniform() claims a closed interval. Even for reasonably sophisticated floating point users, it's not necessarily obvious that that is the reasoning behind the different claims. True. I guess the original post in this thread is good evidence of that. Though I'm not sure I'm capable of coming up with extra wording for the docs that won't just cause more confusion, so I'll leave that to someone else. I seem to recall that when this originally came up in the tracker (issue 4979) the fun part of the analysis was proving that random.uniform(a, b) can never produce values *outside* the interval [a, b]. :-) -- Mark Dickinson -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On 2009-06-10 15:54, Mark Dickinson wrote: Robert Kernrobert.k...@gmail.com wrote: On 2009-06-10 14:46, Mark Dickinson wrote: On Jun 10, 8:15 pm, Robert Kernrobert.k...@gmail.com wrote: On 2009-06-10 13:53, Terry Reedy wrote: A full technical discussion does not below in the docs, in my opinion. A wike article would be fine. True. However, a brief note that Due to floating point arithmetic, for some values of a and b, b may or may not be one of the possible generated results. might be worthwhile. The actual details of *why* this is the case can be discussed elsewhere. I find it difficult to see how such a disclaimer would have any practical value [lots more badly wrapped text snipped... ] That's a fair point. However, one issue that hasn't been brought up is that it might be confusing to a user why random.random() returns values in a half-open interval while random.uniform() claims a closed interval. Even for reasonably sophisticated floating point users, it's not necessarily obvious that that is the reasoning behind the different claims. True. I guess the original post in this thread is good evidence of that. Though I'm not sure I'm capable of coming up with extra wording for the docs that won't just cause more confusion, so I'll leave that to someone else. I did make a concrete suggestion. I seem to recall that when this originally came up in the tracker (issue 4979) the fun part of the analysis was proving that random.uniform(a, b) can never produce values *outside* the interval [a, b]. :-) I was a bit worried about that part myself for a little bit. :-) -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: free chart lib for Python?
On May 7, 10:27 pm, oyster lepto.pyt...@gmail.com wrote: I mean chart, not plot. If you don't know the difference, you can checkwww.advsofteng.com, which is a commercial program is there such a thing with many kinds ofchart, i.e. pie-chart, line-chart, ..? A long time ago, I programmed a rmchart interface, however rmchart is windows only, andwww.rmchart.comdisappeared now See you I'm about to announce WHIFF/amChart after some more testing. WHIFF/amCharts generates highly sophisticated interactive charts using Adobe/Flash plug-in applets. Documentation here: http://aaron.oirt.rutgers.edu/myapp/amcharts/doc You can get it from the WHIFF mercurial repository for now. http://aaron.oirt.rutgers.edu/cgi-bin/whiffRepo.cgi I will upload a tarball to http://whiff.sourceforge.net soonish. Thanks in advance for any comments. -- Aaron Watters === I want to achieve immortality by not dieing. -- Woody Allen -- http://mail.python.org/mailman/listinfo/python-list
Re: Compiling Python3.1
Martin v. Löwis schrieb: What can I do about that? Remove the non-ASCII characters from db.h. Ehh... $ find -type f | grep -i db.h ./Modules/unicodename_db.h ./Modules/unicodedata_db.h ./Objects/unicodetype_db.h There's no db.h file in the Python-3.1rc1 distribution. The ones above contain thousands of lines (~15k, 5k and 2k) all of which consist of endless arrays of unsigned characters - I really would not know what to remove from those. Kind regards, Johannes -- Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit, verlästerung von Gott, Bibel und mir und bewusster Blasphemie. -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik 48d8bf1d$0$7510$54022...@news.sunrise.ch -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
On Jun 9, 2:33 pm, Esmail ebo...@hotmail.com wrote: Hi, random.random() will generate a random value in the range [0, 1). Is there an easy way to generate random values in the range [0, 1]? I.e., including 1? I am implementing an algorithm and want to stay as true to the original design specifications as possible though I suppose the difference between the two max values might be minimal. Well, I guess someone should actually describe a solution to this rather than debate the necessity, if only for academic interest. So, in order to do this for real: Generate a random integer the range [0,2**53+1), probably the easiest thing is to get a 64 bit integer and scale it using integer division (which will also help to minimize selection bias). random.randrange probably does something like this already. If the number is exactly 2**53, return 1.0. Else stuff the bits of the number into the mantissa of a double, along with -1 as the exponent, and return that. Implementation left as exercise, mostly because it really won't make a difference. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: random number including 1 - i.e. [0,1]
Robert Kern robert.k...@gmail.com wrote: On 2009-06-10 15:54, Mark Dickinson wrote: [...] I'm not sure I'm capable of coming up with extra wording for the docs that won't just cause more confusion, so I'll leave that to someone else. I did make a concrete suggestion. Yes, you did. Thank you. Submitted as http://bugs.python.org/issue6261 I seem to recall that when this originally came up in the tracker (issue 4979) the fun part of the analysis was proving that random.uniform(a, b) can never produce values *outside* the interval [a, b]. :-) I was a bit worried about that part myself for a little bit. :-) I think the key was to show that multiplication by (1-2**-53) (the largest possible output of random.random()) always makes any float smaller in magnitude[*], so assuming that a = b the value of the Python expression random.random()*(b-a) can't be larger than the exact real value of (b-a), which in turn means that a + random.random()*(b-a) can't exceed b. [*] Well, almost. This isn't true for subnormals. But if the result of the subtraction b-a is subnormal then that subtraction was also necessarily exact, so everything works in this case, too. And of course I'm wrong. I shouldn't have said *never*, above: from random import uniform uniform(-1e308, 1e308) inf :-( Somehow this doesn't seem worth either fixing or documenting, though. -- Mark Dickinson -- http://mail.python.org/mailman/listinfo/python-list