Re: multi split function taking delimiter list

2006-11-14 Thread Sam Pointon
On Nov 14, 7:56 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > Hi, I'm looking for something like: > > multi_split( 'a:=b+c' , [':=','+'] ) > > returning: > ['a', ':=', 'b', '+', 'c'] > > whats the python way to achieve this, preferably without regexp? pyparsing

Re: Specify string with uniform indentation ignored

2006-09-14 Thread Sam Pointon
tobiah wrote: > Like a docstring, I would like to specify a string such as: > > def thing: > > string = """ > function otherlang(){ > doit() > } > """ > > And have the string end up actually being defined as: > > """ > function otherlang

Re: Setting "value" of an int-derived class

2006-09-02 Thread Sam Pointon
Ken Schutte wrote: > So, that's good. But, how can I change the value of x from 5 to > something else, without creating a new instance? > > I suppose I could create a function that creates a new "foo" and copies > its attributes, but is there a more direct way? Is the value "5" stored > in some s

Re: Coding style and else statements

2006-08-28 Thread Sam Pointon
Bruno Desthuilliers wrote: > foo = lambda thing: thing and thing + 1 or -1 The and ... or trick is buggy (what if thing == -1?) and bad style. If you -do- want a conditional expression, 2.5 provides one: thing + 1 if thing else -1 No subtle logical bugs, and a good deal more obvious. On the top

Re: getting started, .py file

2006-02-20 Thread Sam Pointon
python -i source_file.py will do what you want. -Sam -- http://mail.python.org/mailman/listinfo/python-list

Re: Spelling mistakes!

2006-01-07 Thread Sam Pointon
[EMAIL PROTECTED] wrote: > >> In fact, googling for "referer" and "referrer" reports a similar > >> number of hits, unlike most misspellings. > > Terry> You know, I almost mentioned that myself. Drives me crazy. > > Me too. I'm one of those people who, for better or worse, is a good > spel

Re: Regex anomaly

2006-01-03 Thread Sam Pointon
Would this particular inconsistency be candidate for change in Py3k? Seems to me the pos and endpos arguments are redundant with slicing, and the re.match function would benefit from having the same arguments as pattern.match. Of course, this is a backwards-incompatible change; that's why I suggest

Re: loops breaks and returns

2006-01-01 Thread Sam Pointon
rbt wrote: > Is it more appropriate to do this: > > while 1: > if x: > return x > > Or this: > > while 1: > if x: > break > return x > > Or, does it matter? I would pick the first form if that's the only place where x would be returned from the function. However, if the

Re: Line replace

2006-01-01 Thread Sam Pointon
>Hello > > I need some help > > I have a text file which changes dynamically and has > 200-1800 lines. I need to replace a line , this line > can be located via a text marker like : > > somelines > # THIS IS MY MARKER > This is the line to be replaced > somemorelines > > My question is how

Re: problems binding a dictionary

2005-12-10 Thread Sam Pointon
You're not 'exploding' the dict to the param1='blah' etc form - you-re actually passing it in as a single dict object. To solve this, add a ** to the front of a dict you want to explode in a function, just as you'd add a * to explode a sequence. -- http://mail.python.org/mailman/listinfo/python-l

Re: regexp non-greedy matching bug?

2005-12-03 Thread Sam Pointon
My understanding of .*? and its ilk is that they will match as little as is possible for the rest of the pattern to match, like .* will match as much as possible. In the first instance, the first (.*?) did not have to match anything, because all of the rest of the pattern can be matched 0 or more t

Re: Checking length of each argument - seems like I'm fighting Python

2005-12-03 Thread Sam Pointon
It depends what you mean by 'scalar'. If you mean in the Perlish sense (ie, numbers, strings and references), then that's really the only way to do it in Python - there's no such thing as 'scalar context' or anything - a list is an object just as much as a number is. So, assuming you want a Things

Re: How to do "new_variable = (variable) ? True : False; " (php) on python?

2005-11-18 Thread Sam Pointon
Daniel Crespo wrote: > Hello to all, > > How can I do > > new_variable = (variable) ? True : False; > > in Python in one line? > > I want to do something like this: > > dic = {'item1': (variable) ? True-part : False-part} > > Any suggestions? > > Daniel There's a trick using the short-circuiting b

Re: Hot to split string literals that will across two or more lines ?

2005-11-17 Thread Sam Pointon
> print "a string which is very loo" \ > + "ong." Minor pedantry, but the plus sign is redundant. Python automatically concatenates string literals on the same logical line separated by only whitespace. -- http://mail.python.org/mailman/listinfo/python-list

Re: Multikey Dict?

2005-11-12 Thread Sam Pointon
> If I could just say to Python: john and graham (and ...) are all a part > of a "superdict" and either their id or their name can be used as keys. > Can I do that somehow? Sure you can. There are two obvious ways to do this - enlist the aid of a Superdict (or similar) class to take care of the d

Re: Importing Modules

2005-11-02 Thread Sam Pointon
On the second point, a combination of sys.path, os.listdir and __import__ should do what you're after, although sifting through the whole of sys.path and subfolders from Python, rather than the interpreter itself, could be slow. (And it'll be redundant as well - __import__ will have do the same th

Re: mixin helper class for unknown attribute access?

2005-10-31 Thread Sam Pointon
One alrady exists, __slots__. >>> class Foo(object): __slots__ = ['bar', 'baz', 'qig'] >>> f = Foo() >>> f.foo = 'bar' Traceback (most recent call last): File "", line 1, in -toplevel- f.foo = 'bar' AttributeError: 'Foo' object has no attribute 'foo' >>> f.bar = 'foo' However, __

Re: Pickling and unpickling inherited attributes

2005-10-30 Thread Sam Pointon
Of course, in __setstate__, there should be a tup = list(tup) as well - sheer forgetfulness and a confusing name in one. -- http://mail.python.org/mailman/listinfo/python-list

Re: Pickling and unpickling inherited attributes

2005-10-30 Thread Sam Pointon
The reason the state of obj.A and obj.B aren't preserved is because your __getstate__ and __setstate__ don't preserve them - they only save obj.C, and you don't make a call to the parent class's respective methods. Here's what I mean: >>> import pickle >>> class Child(Parent): __slots__=[

Re: How do I sort these?

2005-10-28 Thread Sam Pointon
> unzip doesn't seem to work for me... It's not a part of the standard Python distribution, but this is a naive implementation (it doesn't react well to the list's contents having different lengths). def unzip(seq): result = [[] for i in range(len(seq[0]))] for item in seq: for in

Re: index and find

2005-10-22 Thread Sam Pointon
>>> s = 'foobar' >>> s.find('z') -1 >>> s.index('z') Traceback (most recent call last): File "", line 1, in -toplevel- s.index('z') ValueError: substring not found Pretty self explanatory. -- http://mail.python.org/mailman/listinfo/python-list

Re: High Order Messages in Python

2005-10-22 Thread Sam Pointon
This can be suitably applied to Python with the use of Higher Order Functions, though. It's not quite the Ruby version because Python allows you to use functions as first-class objects, complicating the All-You-Can-Do-Is-Pass-A-Message philosophy. This is my 5-minute implementation: class HigherO

Re: Calling an exe from Python?

2005-10-16 Thread Sam Pointon
from subprocess import Popen proc = Popen('my_programme.exe') Use proc.communicate(input) to send input to stdin, and get a tuple of (stdout, stderr) back. If you need the returncode, use proc.poll() or proc.wait(), depending on if you want it to block or not. -- http://mail.python.org/mailman/l

Re: wierd threading behavior

2005-10-14 Thread Sam Pointon
thread is a low-level threading module, and start_new is deprecated. Aside from that, thread.start_new(test.()) is syntaxically wrong (a pair of brackets can't follow a dot). However, your example does work for me once I fix the syntax, and it prints hello but then hangs. I can't explain the other

Re: Works only in interactive mode

2005-10-10 Thread Sam Pointon
That looks like a different module named pcop getting in the way. If there's another pcop.py in the directory where you're running it as a script, then that gets priority and you'll end up with an error like the one you got. However, if you run it interactively, then that directory is not checked,

Re: Function decorator that caches function results

2005-10-08 Thread Sam Pointon
What about not storing args at all? Something like this: def cache_function(func, args_list): cache = {} def cached_result(*args, **kwargs): kwargs.update(dict(zip(args_list, args))) if kwargs in cache: return cache[kwargs] result = func(**kwargs)

Re: how do you pronounce wxpython

2005-10-08 Thread Sam Pointon
I'd always assumed it was doubleyew-ecks python, but it could be wicks python, or similar. -- http://mail.python.org/mailman/listinfo/python-list

Re: What is "self"?

2005-09-22 Thread Sam Pointon
self is the class instance that the bound function being called belongs to. This example should illustrate a bit. class Foo(object): def __init__(self, value): self.value = value # so the Foo instance now has an attribute, value def get_value(self): return self.value # Th

Re: Redundant code in multiple methods

2005-09-09 Thread Sam Pointon
How about using a class, with __call__, as a wrapper instead of the function itself? class FunctionWrapper(object): def __init__(self, cls, function): self._function = function self._cls = cls def __call__(self, *args, **kwargs): REQUEST = self.REQUEST SE

Re: Question about consistency in python language

2005-09-08 Thread Sam Pointon
update updates the dictionary in place - it actually returns None, not the updated dict. However, you can construct a dictionary from a list of (key, value) pairs using dict(list). Example: >>>l = [('foo', 'bar'), ('baz', 'qig')] >>>d = dict(l) >>>print d {'foo': 'bar', 'baz': 'qig'} -- http://m