Re: reading a list from a file

2005-06-20 Thread Jordan Rastrick
Be careful, though - make sure you can absolutely trust your source of data before calling eval on it. If an unauthorised person could forseeably modify your file, then they could insert a string containing arbitrary Python code into it in place of your list, and then running your program would ca

Re: reading a list from a file

2005-06-20 Thread Jordan Rastrick
If you decide to steer clear of eval, the following comes close to what you want, and is somewhat Pythonic (I feel): def back_to_list(str): return str.strip("[]").split(", ") >>> s = "[1, 2, 3, 4, 5, 6]" >>> back_to_list(s) ['1', '2', '3', '4', '5', '6'] So parsing the list structure is prett

Re: references/addrresses in imperative languages

2005-06-20 Thread Jordan Rastrick
You can add Australia to the list :) Any volunteers for a fourth continent? Antarctica, perhaps? ;) - Jordan -- http://mail.python.org/mailman/listinfo/python-list

Re: Boss wants me to program

2005-06-28 Thread Jordan Rastrick
The problem with all posts that say "Use Visual Basic, its easy for small programs" is that small programs, seemingly inevitably, become bigger programs (unless they become dead, unmaintained programs). If your users - you, your boss, coworkers, whoever - find your software useful, and you start to

Re: Will Guido's "Python Regrets" ever get implemented/fixed?

2005-07-02 Thread Jordan Rastrick
Python 3000 is the proveribal and so far hypothetical version of the language in which backward incompatible changes will be allowed (and encouraged). See http://www.python.org/peps/pep-3000.html for details. [EMAIL PROTECTED] wrote: > Guido gave a nice "Python Regrets" Power Point talk at OSCO

Re: how to retrive highlighted text in a browser?

2005-07-03 Thread Jordan Rastrick
I don't know how hard this would be to do in Python - appending text to a file is straightforward, but it sounds like you'd need to extend the browser itself to get the functionality you want. The most obvious choice if you want an extensible browser is Mozilla Firefox (http://www.mozilla.org/produ

Re: Path inherits from basestring again

2005-07-27 Thread Jordan Rastrick
used for URLs too" debate, though). Ultimately, its just not different enough from backslash to be very confusing (hear I'll put in a disclaimer, that I don't have enough computing experience to know of any OS where neither type of slash is used in paths.) And thats my 14.5 cents :)

Re: Euclid's Algorithm in Python?

2005-08-04 Thread Jordan Rastrick
Raising an assertion error for a < b is a bit of overkill, since its not really a case of bad input. So normally you see Euclid done like this: def gcd(a,b): # All lowercase for a function is a bit more conventional. if a < b: a, b = b, a # Ensures a >= b by swapping a and b if nessec

Re: Metaclasses and new-style classes

2005-08-07 Thread Jordan Rastrick
Correct me if I'm wrong, but I think the OP was asking if metaclasses work with old-style classes, not new-style. [EMAIL PROTECTED] wrote: > This may be a limitation Zope imposes. > > I wrote this program: > #--- > class M(type):

Re: Euclid's Algorithm in Python?

2005-08-07 Thread Jordan Rastrick
Good point. I suppose I'd only ever seen it implemented with the if test, but you're right, the plain while loop should work fine. Silly me. def gcd(a,b): while b != 0: a, b = b, a%b return a Even nicer. -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionary inheritance

2005-08-12 Thread Jordan Rastrick
Talin asked: > Also, on a completely different subject: Has there been much discussion > about extending the use of the 'is' keyword to do type comparisons a la > C# (e.g. "if x is list:") ? > > -- Talin No, is already has a specific, well defined meaning - object identity. IDLE 1.1 >>> a = [1,2

Re: How can I exclude a word by using re?

2005-08-15 Thread Jordan Rastrick
could ildg said: > I want to use re because I want to extract something from a html. It > will be very complicated without using re. But while using re, I > found that I must exlude a hole word "", certainly, there are > many many "" in this html. Actually, for properly processing html, you shou

Re: question about binary and serial info

2005-08-17 Thread Jordan Rastrick
Sounds like you want the bitwise and operator, & >>> 2 & 3 2 >>> 32 & 16 0 >>> 31 & 12 12 etc. [EMAIL PROTECTED] inquired: > i have an interesting project at work going on. here is the challenge. > i am using the serial module to read data from a serial input. > it comes in as a hex. i need to m

Re: list insertion

2005-08-23 Thread Jordan Rastrick
What you've posted looks right, more or less. To get any sort of meaningful feedback, you really need to post a full version of your code, including the print statements, what's being printed, and why you think this shows the code is not working. What you've shown is far too little for anybody els

Re: How to write this iterator?

2005-09-20 Thread Jordan Rastrick
I've written this kind of iterator before, using collections.deque, which personally I find a little more elegant than the list based approach: from collections import deque def interleave(*iterables): iters = deque(iter(iterable) for iterable in iterables) while iters: it = iters

Suggesting a new feature - "Inverse Generators"

2005-03-25 Thread Jordan Rastrick
First, a disclaimer. I am a second year Maths and Computer Science undergraduate, and this is my first time ever on Usenet (I guess I'm part of the http generation). On top of that, I have been using Python for a grand total of about a fortnight now. Hence, I apologise if what follows is a stupid s

Re: Suggesting a new feature - "Inverse Generators"

2005-03-25 Thread Jordan Rastrick
Sorry about the mangled formatting... like i said, first time on Usenet Suggestions, comments, replies, etc most welcome. This definitely includes replies of the form: "This is stupid, because..." provided it isnt followed with "youre a jerk who knows nothing. Period." Heres a follow up rant in

Re: Suggesting a new feature - "Inverse Generators"

2005-03-25 Thread Jordan Rastrick
Thanks for the very fast feedback :) I specifically set optionalline = None to deal with that bug you mentioned, with the implicit assumption createRecord knows how to deal with a None argument. If that guard got destroyed in the copy paste process, my bad. As for you solution, yes, you could do

Re: Suggesting a new feature - "Inverse Generators"

2005-03-25 Thread Jordan Rastrick
Yes, granted. This is basically the same as Andrew's reply, except with Iterators in place of generators, so I'll let my answer to that stand. In fact, its my solution, but with iter.next() in place of accept :) This is probably something like how I wanted to solve the problem when I first was lo

Re: Suggesting a new feature - "Inverse Generators"

2005-03-25 Thread Jordan Rastrick
Wow, if I'm going to get replies (with implemented solutions!) this quickly, I'll post here more often :-) This is the most different to my solution, and also the shortest, and therefore the most interesting, reply so far. Its also the last one I'll reply to before I go to bed. Its taken

Re: Suggesting a new feature - "Inverse Generators"

2005-03-25 Thread Jordan Rastrick
> No, it's nothing special about groupby. record simply stores its state in a > mutable default parameter. This isn't general good practice: at least you have > to be careful with it. You can see the behavior in the following example: > >>> def accumulate(value, accum = []): > ... accum.

Re: Suggesting a new feature - "Inverse Generators"

2005-03-26 Thread Jordan Rastrick
Hmmm, I like the terminology consumers better than acceptors. The ability to have 'full coroutines', or at least more 'coroutiney behaviour' than is provided by generators alone, was I think what I was trying to get at with my original idea, although things got a bit sidetracked by the way I focus

Re: the bugs that try men's souls

2005-04-03 Thread Jordan Rastrick
I think I found your bug, although it took a bit of time, a fair bit of thought, and a fair bit of extra test-framework code - your program is very concise, reasonably complex, and very unreadable. Its perfect for testing maths theorems of your own interest, but you probably should have polished it

Re: the bugs that try men's souls

2005-04-05 Thread Jordan Rastrick
Sean McIlroy wrote: > > Wow again. I had a real "V8 moment" when I looked at your solution > (smacking my forhead, groaning ruefully, etc). You were right: my > intention was simply to hide the trivial cases from view; I completely > missed the fact that I was now testing for membership in a diffe

Re: sorting a list and counting interchanges

2005-04-06 Thread Jordan Rastrick
Unless I'm mistaken, this doesnt quite work, because it switches the parity of phase every time a comparison is made, rather than every time a swap is made. So: # phase = 1 def mycmp(x,y): global phase c = cmp(x,y) if c > 0: # i.e. a swap will be performed in the sort phase = -pha

Re: dictionary comparison

2005-05-05 Thread Jordan Rastrick
rickle wrote: > I'm trying to compare sun patch levels on a server to those of what sun > is recommending. For those that aren't familiar with sun patch > numbering here is a quick run down. > > A patch number shows up like this: > 113680-03 > ^^ ^^ > patch# revision > > What I want to do is

Re: Merging overlapping spans/ranges

2005-05-10 Thread Jordan Rastrick
Max M wrote: > I am writing a "find-free-time" function for a calendar. There are a lot > of time spans with start end times, some overlapping, some not. > > To find the free time spans, I first need to convert the events into a > list of non overlapping time spans "meta-spans". > > This nice asci

Re: Merging overlapping spans/ranges

2005-05-11 Thread Jordan Rastrick
Should work fine as far as I can see. Of course, thats not very 'pythonic' - I should probably give at least 10 different unit tests that it passes ;) Its gratifying to know I'm not the only one who needed that final yield. Jim Sizelove wrote: > Bengt Richter wrote: > > On Tue, 10 May 2005 15:14:

Is isinstance always "considered harmful"?

2005-05-15 Thread Jordan Rastrick
y first time, how exciting!), and in the meantime, I'll let replies start accumulating froma whole lot of people who are a lot smarter and more experience in Python than myself :) Several-weeks-in-and-still-liking-Python-better-than-any-other-previously-learned-language-ly yours, Jordan Rastrick -- http://mail.python.org/mailman/listinfo/python-list

Re: Is isinstance always "considered harmful"?

2005-05-16 Thread Jordan Rastrick
Leif K-Brooks wrote: > Regardless of the various issues surrounding isinstance(), you have a > difference in functionality. Since you're just storing a reference in > the case of another LinkedList instead of copying it, mutating the > LinkedList will be different from mutating another iterable t

Re: Is isinstance always "considered harmful"?

2005-05-16 Thread Jordan Rastrick
Thanks for the replies, everyone. As is usual when reading comp.lang.python, I got some invaluable exposure to new ideas (multiple dispatching in particular seems to fill a gap I've felt in OO programming in the past.) I'm starting to think this newsgroup is in its own right an excellent reason to

Re: "End Of Line" Confusion

2005-05-17 Thread Jordan Rastrick
Well, copying and pasting this text, and changing <<>> to Foo so that its a legal Python identifier (why did you not want to name your class, out of curiosity), I get no problems with this. class Foo: def digest(): ''' char[28] digest ( ) Return the digest of the strings passe

Re: "End Of Line" Confusion

2005-05-18 Thread Jordan Rastrick
If you email the script to me, I'd be happy to take a look at it and see if I come up with the same error (I'm running IDLE on a Windows XP box here, cant remember if the filesystem is FAT or NTFS ;-)) Although as a relative newbie I've never come across it myself, one possible source of such myst

Re: any macro-like construct/technique/trick?

2005-06-01 Thread Jordan Rastrick
Is having to read two lines really that much worse than one? And the only thing you find objectionable about the most obvious solution? If so, then what's wrong with: def foo(): # do some stuff if debug: emit_dbg_obj(DbgObjFoo(a,b,c)) # do more stuff To my mind, this is no l

Re: Lost in the inheritance tree...

2005-06-06 Thread Jordan Rastrick
Although you get infinite recursion with this code, you still get enough information on the error from the interpreter to help you debug. Running IDLE, I get a traceback of: File "C:/Documents and Settings/Jordan/Desktop/more_blah.py", line 11, in __init__ self.createFrames() File "C:/Docum

Re: Lost in the inheritance tree... THANKS!

2005-06-06 Thread Jordan Rastrick
comp.lang.python is a great newsgroup in that respect - so long as you ask a semi-intelligent question, you nearly always end up with a quick and helpful response. Good luck with learning programming, and Python (IMO its one of the best possible languages to do it in) -- http://mail.python.org/m

Annoying behaviour of the != operator

2005-06-08 Thread Jordan Rastrick
Can anybody please give me a decent justification for this: class A(object): def __init__(self, a): self.a = a def __eq__(self, other): return self.a == other.a s = A(3) t = A(3) >>> print s == t True >>> print s != t True I just spent a long, long time tracking down a

Re: Annoying behaviour of the != operator

2005-06-08 Thread Jordan Rastrick
Well, I don't really want the objects to be comparable. In fact, to quote that PEP you linked: An additional motivation is that frequently, types don't have a natural ordering, but still need to be compared for equality. Currently such a type *must* implement comparison and thus

Re: Annoying behaviour of the != operator

2005-06-08 Thread Jordan Rastrick
Just because a behaviour is documented, doesn't mean its not counter intutitive, potentially confusing, and unnessecary. I have spent a fair amount of time reading the Python docs. I have not yet memorised them. I may have read this particular section of the reference manual, or I may have not, I

Re: Annoying behaviour of the != operator

2005-06-08 Thread Jordan Rastrick
I'd suggest the only nessecary change is, if objects a,b both define __eq__ and not __ne__, then a != b should return not (a == b) If a class defines __ne__ but not __eq__, well that seems pretty perverse to me. I don't especially care one way or another how thats resolved to be honest. The order

Re: Annoying behaviour of the != operator

2005-06-08 Thread Jordan Rastrick
Well, I'll admit I haven't ever used the Numeric module, but since PEP207 was submitted and accepted, with Numeric as apparently one of its main motivations, I'm going to assume that the pros and cons for having == and ilk return things other than True or False have already been discussed at length

Re: Annoying behaviour of the != operator

2005-06-08 Thread Jordan Rastrick
I'm a Maths and Philosophy undergraduate first and foremost, with Computer Science as a tacked on third; I've studied a fair bit of logic both informally and formally, and am familiar with things such as the non-nessecity of the law of the excluded middle in an arbitrary propositional calculus farm

Re: Please help me understand this code....

2005-06-17 Thread Jordan Rastrick
The : is used in Python for slice notation, which is explained pretty thoroughly in the Python tutorial, specifically at: http://www.python.org/doc/2.4/tut/node5.html -- http://mail.python.org/mailman/listinfo/python-list

Re: Overcoming herpetophobia (or what's up w/ Python scopes)?

2005-06-17 Thread Jordan Rastrick
Python's current scoping differs from that in versions 2.1 and earlier - statically nested (lexical) scoping was introduced under PEP 227. I don't know what differences, if any, remain between Python's and Perl's scoping rules, or if there is any tutorial concerning Python scoping thats aimed spec

Re: OO approach to decision sequence?

2005-06-18 Thread Jordan Rastrick
I've coded some simple recursive tree data structures using OO before (unfortunately not in Python though). It's not nessecarily an ill-suited approach to the task, although it depends on the specific details of what you're doing. What's the the piece of code from which your if...elif fragment is t

Re: extreme newbie

2005-06-18 Thread Jordan Rastrick
Another thing to keep in mind is that while technically both Python and Java are converted into intermediate byte-codes, which are then interpreted, the Java Virtual Machine runs java bytecode significantly faster. Partly this is because Sun have put a lot of effort into making Java as fast as poss