Structure using whitespace vs logical whitespace
I've been trying to search through the years of Python talk to find an answer to this, but my Googlefu is weak. In most languages, I'll do something like this xmlWriter.BeginElement("parent"); xmlWriter.BeginElement("child"); --xml.Writer.Characters("subtext"); xmlWriter.EndElement(); xmlWriter.EndElement(); Where the dashes are indentation (since some newsgroup handlers don't do tabs well). XML writing is just an example. In general, I'm using indentation to show logical flow through code. Python's choice to give semantic meaning to whitespace prevents me from doing such things. What was once reserved for logical use is now used syntactically. In 90% of cases, its not needed, and whitespace significance seems to be pretty effective. In that last 10%, however, I've been frustrated many times. I've been using python for a few years, and gotten around this in one way or another, but now I want to get other who work with me to pick up Python. All newbies to Python have trouble with the idea of whitespace sensitivity, but how can I convince them that "it just works better" when I have this construct which I want to use but can't. Has anybody found a way to emulate this behavior? I've often done it by opening an expression for the whole thing, but there's a lot of tasks where a single expression just isn't sufficient (such as things with assignment). PS. In my opinion the solution would be to have the option of entering a "whitespace insensitive" mode which uses C style {} and ;. The token to enter it could be as complicated as you want (in fact, it may make sense to make it complicated to discourage use unless it's really advantageous). I'd sugest {{ and }} or something bigger like {={ } =}. Only two problems: 1) I'm sure it would offend Guido's sense of language aesthetics 2) I'm sure the idea has been hashed over on this newsgroup to death... hence prefering a workaround instead. -- http://mail.python.org/mailman/listinfo/python-list
Re: Structure using whitespace vs logical whitespace
On Dec 15, 11:10 am, Terry Reedy wrote: > > In general, I'm using indentation to show logical flow through code. > > That, of course, is what Python does. > Python does NOT use indentation to show logical flow. It uses it to show syntactical flow. The XML writer is the perfect example of a case where they are different. In most cases, syntactic flow is close enough to logical flow. There are a few cases where you can 'draw a picture' of the algorithm in code if you are whitespace insensitive. I've not used the "with" keyword before, and it does seem to handle this troublesome case quite well. I learned python before it was around, and never really studied it hard enough. I'll have to investigate what other tricks can be done with it. I'm a big fan of the rule "make the 90% easy and the remaining 10% possible." Whitespace sensitivity makes the 90% easy, and just from the looks of it, the 'with' command and whitespace insensitive expressions give the remaining 10%. And I do like the automated support for "finally" clauses when using 'with' Thanks for the help, everyone! -- http://mail.python.org/mailman/listinfo/python-list
Re: Managing timing in Python calls
I believe WxTimerEvent is handled using the event queue, which isn't going to do what you want. An event which goes through the queue does not get processed until you return to the queue. What you want to do is actually a rather difficult task to do generically. Should the task be interrupted immediately? Or is a tiny latency acceptable? Should the function being terminated get to handle its own termination? Or should the termination be forced on it. What sort of overhead is acceptable for this "set_timeout" behavior? I would not be surprised if there isn't a built in solution, because its so hard, but rather built in tools which can be used to do it. If your timeouts are on the order of seconds, you might be able to just check time.time() at the begining, and compare it to the current time later in the function. This could be on the main thread or on a worker thread. If you need better handling, you may want to look at how condition variables and such work. Finally, thread has a function to send a Keyboard Interrupt to the main thread. I believe you could do your work on the main thread, and catch the interrupt. "Background" tasks are not easy to implement in any language (other than perhaps AJAX ;-) ). Remember, Python does not support truly simultaneous threads. It actually does timeslices of about 100 operations. Any solution you choose should work given this information. And as for a "nicer" construct, I personally just learned of how to handle the "with" command. I could see something like class Timeout: def __init__(self, t): self.t = t def __enter__(self): self.start = time.time() def __exit__(self, x, y, z): return None def __nonzero__(self): return time.time() - self.start <= self.t def doSomethingLong(timeout = True): # true guarentees bailout never occurs while timeout: doAnIteration() with Timeout(3) as t: doSomethingLong(t) and have your Timeout class have a flag which it sets when doSomethingLong needs to bail out, using whatever method is "best" for your particular application. This is, of course pseudocode - I've not run it through python msyself. Hopefully any errors are obvious enough that you can work around them. -- http://mail.python.org/mailman/listinfo/python-list
Re: stable algorithm with complexity O(n)
Just because its such an interesting problem, I'll take a stab at it. It can be proven that you cannot sort an arbitrarily large set of numbers, given no extra information, faster than O(n log n). It is provable using information theory. However, if your teacher is giving you evil problems, there's no reason you can't be evil in return: Evil trick 1: Allocate an array of n^2 booleans, initialized to false (This is O(n^2)). Declare this to be "before the sort" Then iterate through the list and set each matching entry in the array to True (This is O(n)). Now you have them "sorted." To get access to the data, you need to iterate across the array O(n^2), but this is "after the sort" Evil trick 2: inserting into a set is O(1), so you could insert n items into a set. This is O(n). Then you can argue that, since the set cares not about order, it is as good as ordered! Evil trick 3: pull up your python debugger, and have your program search for the right answer in your teacher's test library. If done smart, this could even be an O(1) sort O_o (queue Nukees reference: "Welcome to my computer security course. Your grades have already been entered into the school's grading systems as Fs. You have one semester to change that, good luck") ... these are fun =) -- http://mail.python.org/mailman/listinfo/python-list