Interesting topic.
Jacob,
Writing library code is a difficult and unrewarding task - I've been there so I sympathise, however...
I wouldn't say that...
So, how would one go about this in a non broken code way? Don't theyhavesomething like what I'm requesting.
Its not broken, its just different to what you want. What you want is much simpler than what lstrip does so you can easily code your requirements. Try writing the code to do what lstrip actually does - its much harder. So the library includes the more difficult function and lets you code the easy ones.
def lstrip(string,chars=' ') string = list(string) t = 0 for x in string: if x in chars: string.remove(t) else: break t = t+1
Okay, so it's not that difficult, but I get your point. Since all of these are not too hard to write, my library structure is probably what's flawed. I have so many miscellaneous functions that I am copying and pasting (oh no!) that I finally decided to move them to a different module. Unfortunately, they are quite random and I will have to find someway of seperating them.
It seems to me that a few things are flawed in the standarddistribution.
No library is perfect - you should try using C!
But the Python library isually has good reasons for its apparent limitations.
Little things like the overlooking of adding a method or function todecimalfor returning an instance with x places right of the decimal.
But there is very little need to do that. Usually you only want to do that in presentation - ie printing. Thus the mechanism for doing that is in the string formatting code - where it makes sense.
Uck. True usually in printing. String formatting doesn't cut it for a type that's not a string. Especially something not builtin.
Why would limiting the precision of the internal number representation be something you'd want to do? Very occassionally maybe, but in those cases its easy to fake (and it is a fake because the number is in binary and not true decimal so its always an approximation - in any language!)
quantize, but that's junk and not simple.
You mean it does a complex job and not the one youd like it to do? :-)
It does precisely what I would like it to do. I guess I just looked at it and
said, they are making it too difficult.
quantize -- a method of class Decimal in module decimal
arguments -- exp - a Decimal instance that has the required amount of places to the right of the decimal pt.
So, it seems to me that one could possibly add to the decimal module.
userprec = 10 ## A default
def q(decinst): ## simple name -- unfortunately might conflict -- decinst stands for decimal instance
return decinst.quantize(Decimal("0.%s" % ('0'*userprec))) ## Creates a Decimal instance w/userprec places right of #### decimal pt. then uses the given argument decinst method quantize to return decinst w/ userprec places right of the decimal.
Which, of course, I did.
You can even subclass string and make it a method if you like.
Ooooh! How do I do that?
I suspect your function should probably be:
def mylstrip(str,stripstring): if str.startswith(stripstring) and len(stripstring) < len(str): return str[len(stripstring):]
But thats just my guess at what *you* really want...
Well yeah. I didn't think it through that far. But that also contributes to my probably poor library structure. Where would I put such a function?
And finally remember that Python is built by volunteers mostly. You should be pleased the *bothered* to write any of it!
Excuse me... I didn't mean to offend those volunteers in any way. I'm beautifully happy with python! Just little quirks here and there... Maybe someday I'll become one of those volunteers.
Jacob Schmidt
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor