Re: Basic misunderstanding on object creation

2015-05-13 Thread andrew cooke
On Wednesday, 13 May 2015 13:37:23 UTC-3, Terry Reedy wrote: > On 5/13/2015 9:25 AM, andrew cooke wrote: > > > The following code worked on Python 3.2, but no longer works in 3.4. > > Bugfixes break code that depends on buggy behavior. See > https://bugs.python.org/issue168

Re: Basic misunderstanding on object creation

2015-05-13 Thread andrew cooke
On Wednesday, 13 May 2015 11:56:21 UTC-3, Ian wrote: > On Wed, May 13, 2015 at 8:45 AM, andrew cooke wrote: > >>>> class Foo: > > ... def __new__(cls, *args, **kargs): > > ... print('new', args, kargs) > > ... sup

Re: Basic misunderstanding on object creation

2015-05-13 Thread andrew cooke
> But then nothing will be passed to __init__ on the subclass. > > Andrew >>> class Foo: ... def __new__(cls, *args, **kargs): ... print('new', args, kargs) ... super().__new__(cls) ... >>> class Bar(Foo): ... def __init__(self, a): ... print('init', a) ... >>> B

Re: Basic misunderstanding on object creation

2015-05-13 Thread andrew cooke
On Wednesday, 13 May 2015 11:36:12 UTC-3, Thomas Rachel wrote: > Am 13.05.2015 um 15:25 schrieb andrew cooke: > > >>>> class Foo: > > ... def __new__(cls, *args, **kargs): > > ... print('new', args, kargs) > > ... s

Basic misunderstanding on object creation

2015-05-13 Thread andrew cooke
Hi, The following code worked on Python 3.2, but no longer works in 3.4. Did something change, or have I always been doing something dumb? (I realise the code is pointless as is - it's the simplest example I can give of a problem I am seeing with more complex code). >>> class Foo: ... de

Re: Confused about logger config from within Python (3)

2012-12-28 Thread andrew cooke
On Friday, 28 December 2012 21:56:46 UTC-3, Peter Otten wrote: > Other revolutionary ideas: read the docs > > ;) how do you think i knew about the root handler without reading the damn docs you condescending asshole? anywa

Re: Confused about logger config from within Python (3)

2012-12-28 Thread andrew cooke
similarly, if i run the following, i see only "done": from logging import DEBUG, root, getLogger if __name__ == '__main__': root.setLevel(DEBUG) getLogger(__name__).debug("hello world") print('done') -- http://mail.python.org/mailman/listinfo/python-list

Confused about logger config from within Python (3)

2012-12-28 Thread andrew cooke
When I use a config file things seem to work (in other projects), but for my current code I hoped to configure logging from Python. I distilled my problem down to the following test, which does not print anything. Please can someone explain why? I was expecting the module's logger to delegate

ordering with duck typing in 3.1

2012-04-07 Thread andrew cooke
hi, please, what am i doing wrong here? the docs say http://docs.python.org/release/3.1.3/library/stdtypes.html#comparisons "in general, __lt__() and __eq__() are sufficient, if you want the conventional meanings of the comparison operators" but i am seeing > assert 2 < three E T

Re: ABC-registered Exceptions are not caught as subclasses

2011-05-08 Thread andrew cooke
http://bugs.python.org/issue12029 -- http://mail.python.org/mailman/listinfo/python-list

ABC-registered Exceptions are not caught as subclasses

2011-05-07 Thread andrew cooke
This isn't hugely surprising, but doesn't seem to be documented. Is it a bug, or worth raising as one, or have I misunderstood? Python 3.2 (r32:88445, Feb 27 2011, 13:00:05) [GCC 4.5.0 20100604 [gcc-4_5-branch revision 160292]] on linux2 Type "help", "copyright", "credits" or "license" for mo

Re: Language & lib reference in man format ?

2011-04-20 Thread andrew cooke
(1) Python's docs use Sphinx, which uses restructured text as a markup. You can generate man pages from restructured text using rst2man (which is installed on my computer, probably as part of python/docutils). HOWEVER I imagine it's not going to work very well, if at all, because Sphinx uses l

Re: meteclasses 2.x/3.x compatibility

2011-04-20 Thread andrew cooke
What I do in Lepl is use two stages. The first calls the type/metaclass directly and the second subclasses that. This avoids using the "sugar" that changes between 2 and 3. So, for example, in http://code.google.com/p/lepl/source/browse/src/lepl/matchers/matcher.py#40 I have _Matcher = AB

Re: My stupidity / strange inconsistency overriding class methods

2011-04-20 Thread andrew cooke
I didn't phrase that very well. I do see the point about this being "an instance lookup on a class"... -- http://mail.python.org/mailman/listinfo/python-list

Re: My stupidity / strange inconsistency overriding class methods

2011-04-20 Thread andrew cooke
Thanks for finding that reference in the data model docs! I was about to post a bug report because in PEP 3119 it says otherwise: > The primary mechanism proposed here is to allow overloading the built-in > functions isinstance() and issubclass(). The overloading works as follows: > The call i

Re: My stupidity / strange inconsistency overriding class methods

2011-04-19 Thread andrew cooke
OK, sorry, I see the mistake. I'm confusing __class__ on the instance and on te class (the latter being the metaclass). Sorry again, Andrew -- http://mail.python.org/mailman/listinfo/python-list

Re: My stupidity / strange inconsistency overriding class methods

2011-04-19 Thread andrew cooke
Also, there's something strange about the number of arguments (they're not consistent between the two examples - the "A" to __instancecheck__ should not be needed). Yet it compiles and runs like that. Very confused :o( -- http://mail.python.org/mailman/listinfo/python-list

My stupidity / strange inconsistency overriding class methods

2011-04-19 Thread andrew cooke
Hi, I've been staring at this problem, in various forms, all day. Am I missing something obvious, or is there some strange hardwiring of isinstance? This is with Python 3.2. class A(metaclass=ABCMeta): @classmethod def __instancecheck__(cls, instance): return F

Replacing *instance* dict

2011-04-07 Thread andrew cooke
x27;three' assert d.a == 'three' print('woop') On Thursday, April 7, 2011 7:31:16 PM UTC-3, andrew cooke wrote: > > class TupleDict(dict): > '''Stores additional info, but removes it on __getitem__().''' > >

Re: Confused about __prepare__

2011-04-07 Thread andrew cooke
Yes, I think you're right, thanks. Makes sense from an efficiency POV. Luckily, it turns out I don't need to do that anyway :o) Cheers, Andrew -- http://mail.python.org/mailman/listinfo/python-list

Re: Confused about __prepare__

2011-04-07 Thread andrew cooke
Sorry I should probably have made clear that this is Python 3.2 -- http://mail.python.org/mailman/listinfo/python-list

Confused about __prepare__

2011-04-07 Thread andrew cooke
In the code below I use __prepare__ to change the class dictionary so that a tuple is stored in __setitem__(). Since __getitem__() removes the tuple I wasn't expecting any problems, but it seems that __init__ is being retrieved via some other mechanism. Why? Is a copy of the dict being made

Re: Why is __root checked for in OrderedDict?

2011-04-07 Thread andrew cooke
Is that normal? I mean, OK, it's possible (and yes I forgot it could be called directly), but is there any usual reason to do so? I guess what I'm asking is: if I'm writing library code should I be this careful? (I've written quite a lot of Python code without this ever biting me, but maybe

Why is __root checked for in OrderedDict?

2011-04-07 Thread andrew cooke
If you look at the code in http://hg.python.org/cpython/file/6adbf5f3dafb/Lib/collections/__init__.py#l49 the attribute __root is checked for, and only created if missing. Why? I ask because, from what I understand, the __init__ method will only be called when the object is first being created

Re: Why is return type in getfullspec().annotations named as "return"?

2011-04-02 Thread andrew cooke
Sorry, ignore that. I just realised that "return" will be a reserved word, so that can't happen. Andrew -- http://mail.python.org/mailman/listinfo/python-list

Why is return type in getfullspec().annotations named as "return"?

2011-04-02 Thread andrew cooke
This conflicts with any parameter named "return". Wouldn't it have been better to use "->" as the key? Is there any way this can be changed? Andrew -- http://mail.python.org/mailman/listinfo/python-list

Re: python3.2m installed as (additional) binary

2011-02-27 Thread andrew cooke
[Sorry I clicked the wrong button so I think my prev reply went only to Tom] Thanks. Yes, they're hard linked. And the bug report mentions PEP 3149 which says that "m" means --with-pymalloc was used http://www.python.org/dev/peps/pep-3149/ Cheers, Andrew -- http://mail.python.org/mailman/lis

python3.2m installed as (additional) binary

2011-02-27 Thread andrew cooke
Hi, I just downloaded, built and altinstalled Python3.2 on Linux x64. I noticed that in /usr/local/bin I have two identical (says diff) binaries called Python3.2 and Python3.2m. Is this expected? I can find very little reference to them apart from a short discussion in python-dev where some

Re: Another Regexp Question

2010-07-06 Thread andrew cooke
http://bugs.python.org/issue9179 On Jul 5, 9:38 pm, MRAB wrote: > andrew cooke wrote: > > On Jul 5, 8:56 pm, MRAB wrote: > >> andrew cooke wrote: > >>> What am I missing this time? :o( > >> Nothing. It's a bug. :-( > > > Sweet :o) > &

Re: Another Regexp Question

2010-07-05 Thread andrew cooke
On Jul 5, 8:56 pm, MRAB wrote: > andrew cooke wrote: > > What am I missing this time? :o( > Nothing. It's a bug. :-( Sweet :o) Thanks - do you want me to raise an issue or will you? Cheers, Andrew -- http://mail.python.org/mailman/listinfo/python-list

Another Regexp Question

2010-07-05 Thread andrew cooke
As ever, I guess it's most likely I've misunderstood something, but in Python 2.6 lookback seems to actually be lookahead. All the following tests pass: from re import compile assert compile('(a)b(?<=(?(2)x|c))(c)').match('abc') assert not compile('(a)b(?<=(?(2)b|x))(c)'

Re: Help with Regexp, \b

2010-05-29 Thread andrew cooke
On May 29, 11:24 am, Duncan Booth wrote: > andrew cooke wrote: > > Please can someone explain why the following fails: > > >         from re import compile > > >         p = compile(r'\bword\b') > >         m = p.match(' word ') > >

Help with Regexp, \b

2010-05-29 Thread andrew cooke
This is a bit embarassing, but I seem to be misunderstanding how \b works in regexps. Please can someone explain why the following fails: from re import compile p = compile(r'\bword\b') m = p.match(' word ') assert m My understanding is that \b matches a space a

Re: Ann: Validating Emails and HTTP URLs in Python

2010-05-03 Thread andrew cooke
> FYI, Fourthought's PyXML has a module called uri.py that contains   > regexes for URL validation. I've over a million URLs (harvested from   > the Internet) through their code. I can't say I checked each and every   > result, but I never saw anything that would lead me to believe it was   > misbe

Ann: Validating Emails and HTTP URLs in Python

2010-05-03 Thread andrew cooke
Hi, The latest Lepl release includes an implementation of RFC 3696 - the RFC that describes how best to validate email addresses and HTTP URLs. For more information please see http://www.acooke.org/lepl/rfc3696.html Lepl's main page is http://www.acooke.org/lepl Because Lepl compiles to regula

Re: Parser

2010-05-03 Thread andrew cooke
On May 2, 3:54 pm, Andreas Löscher wrote: > Hi, > I am looking for an easy to use parser. I am want to get an overview > over parsing and want to try to get some information out of a C-Header > file. Which parser would you recommend? > > Best, > Andreas I develop Lepl - http://www.acooke.org/lepl

Re: Confused by slash/escape in regexp

2010-04-11 Thread andrew cooke
On Apr 11, 7:18 pm, Paul McGuire wrote: [...] > > So I would say the surprise isn't that case 3 didn't match, but that > case 2 matched. > > Unless I just don't get what you were testing, not being an RE wiz. Case 2 is the regexp engine interpreting escapes that appear as literal strings. It's w

Re: Confused by slash/escape in regexp

2010-04-11 Thread andrew cooke
On Apr 11, 8:12 pm, Lie Ryan wrote: > In the first case, *python* will unescape the string literal '\x62' into > letters 'b'. In the second case, python will unescape the double > backslash '\\' into a single slash '\' and *regex* will unescape the > single-slash-62 into 'b'. In the third case, *p

Confused by slash/escape in regexp

2010-04-11 Thread andrew cooke
Is the third case here surprising to anyone else? It doesn't make sense to me... Python 2.6.2 (r262:71600, Oct 24 2009, 03:15:21) [GCC 4.4.1 [gcc-4_4-branch revision 150839]] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from re import compile >>> p1 = comp

Re: Function name unchanged in error message

2010-01-30 Thread andrew cooke
On Jan 29, 1:09 pm, Michele Simionato wrote: > On Jan 29, 2:30 pm, andrew cooke wrote: > > > Is there any way to change the name of the function in an error > > message?  In the example below I'd like the error to refer to bar(), > > for example (the motivation i

Re: Function name unchanged in error message

2010-01-30 Thread andrew cooke
On Jan 30, 7:17 pm, andrew cooke wrote: > On Jan 29, 5:37 pm, "Gabriel Genellina" > wrote: > > > The decorator module is a very fine addition to anyone's tool set -- but   > > in this case it is enough to use the wraps() function from the functools   >

Re: Function name unchanged in error message

2010-01-30 Thread andrew cooke
On Jan 29, 11:50 am, exar...@twistedmatrix.com wrote: > new.function and new.code will let you construct new objects with > different values (and copying over whichever existing attributes you > want to preserve). unfortunately new is deprecated and dropped from 3. i can't see how the same functi

Re: Function name unchanged in error message

2010-01-30 Thread andrew cooke
On Jan 29, 5:37 pm, "Gabriel Genellina" wrote: > The decorator module is a very fine addition to anyone's tool set -- but   > in this case it is enough to use the wraps() function from the functools   > standard module. ah, thanks! i thought something like this existed in the standard lib, but c

Re: Function name unchanged in error message

2010-01-30 Thread andrew cooke
On Jan 29, 11:22 am, Peter Otten <__pete...@web.de> wrote: > The name is looked up in the code object. As that is immutable you have to > make a new one: [details snipped] thanks very much! sorry i didn't reply earlier - been travelling. (also, thanks to any other replies - i'm just reading thro

Function name unchanged in error message

2010-01-29 Thread andrew cooke
Is there any way to change the name of the function in an error message? In the example below I'd like the error to refer to bar(), for example (the motivation is related function decorators - I'd like the wrapper function to give the same name) >>> def foo(): ... return 7 ... >>> foo.__name

Re: Comparison of parsers in python?

2009-09-26 Thread andrew cooke
On Sep 21, 10:59 am, Nobody wrote: > I have a similar question. > > What I want: a tokeniser generator which can take a lex-style grammar (not > necessarily lex syntax, but a set of token specifications defined by > REs, BNF, or whatever), generate a DFA, then run the DFA on sequences of > bytes.

Re: Intercepting binding?

2009-09-24 Thread andrew cooke
On Sep 24, 5:20 am, Steven D'Aprano wrote: > Speaking as a user (although not of Andrew's domain specific language), > I'd like to say to developers PLEASE PLEASE PLEASE don't try to "help me" > with half-baked unreliable solutions that only work sometimes. > > There's few things worse than unreli

Re: Intercepting binding?

2009-09-24 Thread andrew cooke
On Sep 24, 7:12 am, Carl Banks wrote: >     with capture_changed_bindings() as changed: >         b = 5 >         c = 4 >         d = 6 >     print changed > > test() > > Quick and dirty, not robust at all.  But you get the idea. > > Carl Banks brilliant. using the with context is an excellent i

Re: Intercepting binding?

2009-09-23 Thread andrew cooke
for the record, googling for "f_back.f_locals" reveals a wide variety of similar hacks and also a cleaner way to access the current frame: inspect.currentframe() -- http://mail.python.org/mailman/listinfo/python-list

Re: Intercepting binding?

2009-09-23 Thread andrew cooke
On Sep 23, 10:11 pm, Dave Angel wrote: > This comes up periodically in this list, and the answer is always > something like:  you can't get there from here. Well, I'm both flexible and desperate, so this is a possible route (perhaps near enough): import sys class Foo(object): def __rlshif

Re: Intercepting binding?

2009-09-23 Thread andrew cooke
On Sep 23, 8:40 pm, "Rhodri James" wrote: > eggs[42] = Foo() > beans['spam'] = Foo() > chips.spam = Foo() > spam[eggs.beans['chips']] = Foo() > spam.append(Foo()) these are valid points, but in practice the main use (for the restricted application i care about) is si,ple variables, and this is an

Re: Intercepting binding?

2009-09-23 Thread andrew cooke
For example, I assume it's possible to somehow access the dictionary for the current block, but I can't see how to do this after assignment. If I do it in the Foo constructor, for example, "a" will not yet be bound. On Sep 23, 8:15 pm, andrew cooke wrote: > This is a bit

Intercepting binding?

2009-09-23 Thread andrew cooke
This is a bit vague, I'm afraid, but is there any way for me to take code like: a = Foo() beta = Bar() and somehow attach the string "a" to the Foo instance and "beta" to the Bar instance. At some later point in the program I want to be able to look at the Bar instance and say to the user

Re: Comparison of parsers in python?

2009-09-20 Thread andrew cooke
On Sep 20, 3:16 pm, Peng Yu wrote: > On Sun, Sep 20, 2009 at 1:35 PM, andrew cooke wrote: > > On Sep 20, 9:12 am, andrew cooke wrote: > >> ps is there somewhere can download example files?  this would be > >> useful for my own testing.  thanks. > > > i rep

Re: Comparison of parsers in python?

2009-09-20 Thread andrew cooke
On Sep 20, 9:12 am, andrew cooke wrote: > ps is there somewhere can download example files?  this would be > useful for my own testing.  thanks. i replied to a lot of your questions here; any chance you could reply to this one of mine? the wig format looks like it could be a good test fo

Re: Comparison of parsers in python?

2009-09-20 Thread andrew cooke
> So for the track definition, using a lexer package would be better > than using regex in python, right? they are similar. a lexer is really just a library that packages regular expressions in a certain way. so you could write your own code and you would really be writing a simple lexer. the a

Re: Comparison of parsers in python?

2009-09-20 Thread andrew cooke
> I don't quite understand this point.  If I don't use a parser, since > python can read numbers line by line, why I need a lexer package? for the lines of numbers it would make no difference; for the track definition lines it would save you some work. as you said, this is a simple format, so the

Re: Comparison of parsers in python?

2009-09-20 Thread andrew cooke
also, parsing large files may be slow. in which case you may be better with a non-python solution (even if you call it from python). your file format is so simple that you may find a lexer is enough for what you want, and they should be stream oriented. have a look at the "shlex" package that i

Re: Comparison of parsers in python?

2009-09-20 Thread andrew cooke
> The file size of a wig file can be very large (GB). Most tasks on this > file format does not need the parser to save all the lines read from > the file in the memory to produce the parsing result. I'm wondering if > pyparsing is capable of parsing large wig files by keeping only > minimum requir

Re: Comparison of parsers in python?

2009-09-20 Thread andrew cooke
One word of warning - the documentation for that format says at the beginning that it is compressed in some way. I am not sure if that means within some program, or on disk. But most parsers will not be much use with a compressed file - you will need to uncompress it first. -- http://mail.pytho

Re: Comparison of parsers in python?

2009-09-20 Thread andrew cooke
On Sep 20, 8:11 am, Peng Yu wrote: > On Sun, Sep 20, 2009 at 6:50 AM, andrew cooke wrote: > > On Sep 19, 9:34 pm, Peng Yu wrote: > >> On Sep 19, 6:05 pm, Robert Kern wrote: > >> >http://nedbatchelder.com/text/python-parsers.html > > >> This is more a

Re: Comparison of parsers in python?

2009-09-20 Thread andrew cooke
On Sep 19, 11:39 pm, TerryP wrote: [...] > For flat data, simple unix style rc or dos style ini file will often > suffice, and writing a parser is fairly trivial; in fact writing a [...] python already includes parsers for ".ini" configuration files. [...] > The best way to choose a parser, is e

Re: Comparison of parsers in python?

2009-09-20 Thread andrew cooke
On Sep 19, 9:34 pm, Peng Yu wrote: > On Sep 19, 6:05 pm, Robert Kern wrote: > >http://nedbatchelder.com/text/python-parsers.html > > This is more a less just a list of parsers. I would like some detailed > guidelines on which one to choose for various parsing problems. it would be simpler if you

Re: Frustrated with scopes

2009-08-12 Thread andrew cooke
On Aug 12, 8:52 am, Dave Angel wrote: > Supply us with just enough source code to actually try it, give the full > error message including traceback, and tell us what you expected to > see.  Also tell us Python version   (sys.version) > > So far you've done none of these.  When I try the following

Re: Frustrated with scopes

2009-08-12 Thread andrew cooke
On Aug 12, 7:49 am, andrew cooke wrote: > On Aug 12, 1:51 am, James Stroud > wrote: > > > > > andrew cooke wrote: > > > Is there a way to make this work (currently scope and join are > > > undefined at runtime when the inner class attributes are define

Re: Frustrated with scopes

2009-08-12 Thread andrew cooke
On Aug 12, 1:51 am, James Stroud wrote: > andrew cooke wrote: > > Is there a way to make this work (currently scope and join are > > undefined at runtime when the inner class attributes are defined): > > > class _StreamFactory(object): > > >     @staticmethod &

Re: Frustrated with scopes

2009-08-11 Thread andrew cooke
correction: "source" and "join" are undefined. Sorry, Andrew -- http://mail.python.org/mailman/listinfo/python-list

Frustrated with scopes

2009-08-11 Thread andrew cooke
Is there a way to make this work (currently scope and join are undefined at runtime when the inner class attributes are defined): class _StreamFactory(object): @staticmethod def __call__(lines, source, join=''.join): class Line(object): __source = source

Re: Parsing Binary Structures; Is there a better way / What is your way?

2009-08-07 Thread andrew cooke
On Aug 5, 10:46 am, "Martin P. Hellwig" wrote: > Hi List, > > On several occasions I have needed (and build) a parser that reads a > binary piece of data with custom structure. For example (bogus one): > > BE > +-+-+-+-+--++ > | Version | Command

Re: Suppressing Implicit Chained Exceptions (Python 3.0)

2009-07-02 Thread andrew cooke
David Bolen wrote: > "andrew cooke" writes: > >> However, when printed via format_exc(), this new exception still has the old exception attached via the mechanism described at >> http://www.python.org/dev/peps/pep-3134/ (this is Python 3.0). > > If you're

Suppressing Implicit Chained Exceptions (Python 3.0)

2009-07-01 Thread andrew cooke
I have some (library) code where an exception is caught and, since the underlying cause is rather obscure, a different exception is raised that more clearly explains the issue to the caller. However, when printed via format_exc(), this new exception still has the old exception attached via the me

Re: Generic web parser

2009-05-18 Thread andrew cooke
http://groups.google.com/group/beautifulsoup/browse_thread/thread/d416dd19fdaa43a6 http://jjinux.blogspot.com/2008/10/python-some-notes-on-lxml.html andrew -- http://mail.python.org/mailman/listinfo/python-list

Re: SQL and CSV

2009-05-08 Thread andrew cooke
o be some kind of cryptic argument against parameters. andrew Nick wrote: > On May 8, 1:49 pm, "andrew cooke" wrote: >> Lawrence D'Oliveiro wrote: >> > In message , Peter Otten wrote: >> >> >> While it may not matter here using placeholders instead

Re: SQL and CSV

2009-05-08 Thread andrew cooke
Lawrence D'Oliveiro wrote: > In message , Peter Otten wrote: > >> While it may not matter here using placeholders instead of manually >> escaping user-provided values is a good habit to get into. > > Until you hit things it can't deal with. The post you are replying to was talking about using the

Re: The whole story

2009-04-28 Thread andrew cooke
Paul Hemans wrote: > Hi Andrew, > The reason I am using mapped objects is that I need to abstract from the > database implementation allowing the replication to target a number of > different platforms. This will definitely slow things down. have you looked at sqlalchemy's generic sql support? yo

Re: Is there a maximum size to a Python program?

2009-04-27 Thread andrew cooke
not sure i've read all the posts on this, and i don't fully understand the problem, but someone's mentioned sqlalchemy, so here's my experience with that and large updates using mapped objects. 1 - don't commit each object as you modify it. instead, process a whole pile in memory and then (perha

Re: [OT] large db question about no joins

2009-04-17 Thread andrew cooke
on the more general point about exactly how to handle large data sets, i found this article interesting - http://highscalability.com/unorthodox-approach-database-design-coming-shard andrew -- http://mail.python.org/mailman/listinfo/python-list

Re: Automatically generating arithmetic operations for a subclass

2009-04-14 Thread andrew cooke
Arnaud Delobelle wrote: > "andrew cooke" writes: >> Arnaud Delobelle wrote: >>> class MyInt(int): >>> for op in binops: >>> exec binop_meth % (op, op) >>> for op in unops: >>> exec unop_meth % (op,

Re: Automatically generating arithmetic operations for a subclass

2009-04-14 Thread andrew cooke
Arnaud Delobelle wrote: > I do this: > > binops = ['add', 'sub', 'mul', 'div', 'radd', 'rsub'] # etc > unops = ['neg', 'abs', invert'] # etc > > binop_meth = """ > def __%s__(self, other): > return type(self)(int.__%s__(self, other)) > """ > > unop_meth = """ > def __%s__(self): > return ty

Re: Why is it that *dbm modules don't provide an iterator? (Language design question)

2009-04-09 Thread andrew cooke
"Martin v. Löwis" wrote: >> I assumed there were some decisions behind this, rather than it's just >> not implemented yet. > > I believe this assumption is wrong - it's really that no code has been > contributed to do that. But doesn't the issue at http://bugs.python.org/issue662923 imply that the

Re: Recommendations on Pythonic tree data structure design techniques

2009-04-09 Thread andrew cooke
pyt...@bdurham.com wrote: > Any recommendations on Python based tree data structures that I > can study? I'm working on an application that will model a basic > outline structure (simple tree) and am looking for ideas on > Pythonic implementation techniques. By outline I mean a > traditional hierar

Re: Scrap Posts

2009-04-09 Thread andrew cooke
are you on the mailing list (python-list@python.org) or reading via google groups? groups is full of junk, but the list is filtered. the (filtered) list is also available via gmane and similar. (disclaimer - i also use spamassasin so it's possible that is cleaning the mail up, but this discussi

Re: Re: Why does Python show the whole array?

2009-04-09 Thread andrew cooke
Peter Otten wrote: > John Posner wrote: > >> Given how common string maniuplations are, I guess I'm surprised that >> Python hasn't yet made "contains()" into both a "string"-module function >> *and* a string-object method. > > Could you explain why you prefer 'contains(belly, beer)' > or 'belly.co

Re: Retrieving a specific object from a list?

2009-04-09 Thread andrew cooke
andrew cooke wrote: [...] > but when you need to access instances by more than one value (.bar and > .baz) then typically that's a hard problem, and there's a trade-off > somewhere. you might find writing a special container that contains two > dicts is useful. if so, you

Re: Retrieving a specific object from a list?

2009-04-09 Thread andrew cooke
Jeremiah Dodds wrote: > I've been looking over some of my code, and I've found something I do that > has a bit of a smell to it. I've searched the group and docs, and haven't > found much of anything that solves this particular problem, although I may > just not be searching correctly. > > Anyhow,

Re: nested looping

2009-04-08 Thread andrew cooke
bit late here, but if it's as simple as you say, i think it would be much more efficient (because you only scan checklist and alist once each) to do: known = set() for check in checklist: known.add(check[0:-1]) missing = filter(lambda alpha: alpha not in known, alist) andrew PK wrote: > Exce

Re: Why does Python show the whole array?

2009-04-08 Thread andrew cooke
Gilles Ganault wrote: > On Wed, 08 Apr 2009 12:11:55 +0200, Ulrich Eckhardt > wrote: >>find() returns the index where it is found or -1 if it is not found. Both >> an >>index>0 or a -1 evaluate to True when used as conditional expression. > > Thanks everyone. I shouldn't have assumed that "if test

Re: Creating Linked Lists in Python

2009-04-07 Thread andrew cooke
J-Burns wrote: > How can I do this? And if I could do this by some other way than using > linked lists than do tell me about that as well. replying to this dead thread mainly for anyone using google. there's now a python regular expression engine in lepl and the source code can be seen via http:/

Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread andrew cooke
MRAB wrote: > andrew cooke wrote: >> R. David Murray wrote: >>>> [...] >>>> try: >>>> dimensions.append(float(s)) >>>> except: >>>> dimensions.append(float(quantization[s])) >>> No, no, no; never use a b

Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread andrew cooke
R. David Murray wrote: >> [...] >> try: >> dimensions.append(float(s)) >> except: >> dimensions.append(float(quantization[s])) > > No, no, no; never use a bare except! :) can you explain why? i can't think of any reason why the code would be better catching a specific exception. as

Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread andrew cooke
convert('1,2,red') [1.0, 2.0, 0] >>> convert('1,2,blue') [1.0, 2.0, 1] >>> convert('1,2,blue,blue') [1.0, 2.0, 1, 0] andrew cooke wrote: > Carl Banks wrote: >> import collections >> import itertools >> >> def createIniti

Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread andrew cooke
Carl Banks wrote: > import collections > import itertools > > def createInitialCluster(fileName): > fixedPoints = [] > # quantization is a dict that assigns sequentially-increasing > numbers > # to values when reading keys that don't yet exit > quantization = defaultdict.collections

Re: Returning different types based on input parameters

2009-04-06 Thread andrew cooke
andrew cooke wrote: > George Sakkis wrote: >> That's more of a general API design question but I'd like to get an >> idea if and how things are different in Python context. AFAIK it's >> generally considered bad form (or worse) for functions/methods to

Re: Returning different types based on input parameters

2009-04-06 Thread andrew cooke
George Sakkis wrote: > That's more of a general API design question but I'd like to get an > idea if and how things are different in Python context. AFAIK it's > generally considered bad form (or worse) for functions/methods to > return values of different "type" depending on the number, type and/o

Re: Best way to start

2009-04-06 Thread andrew cooke
Avi wrote: > What is a good way to learn Python? > > Do you recommend going by a book (suggestions welcome) or learning > with tutorials? Both? how do you like to learn and how much experience do you have programming in other languages? andrew -- http://mail.python.org/mailman/listinfo/python-l

Re: speed of string chunks file parsing

2009-04-06 Thread andrew cooke
[disclaimer - this is just guessing from general knowledge of regular expressions; i don't know any details of python's regexp engine] if your regular expression is the bottleneck rewrite it to avoid lazy matching, references, groups, lookbacks, and perhaps even counted repeats. with a little th

Re: Spring-like IoC in python?

2009-04-05 Thread andrew cooke
David Stanek wrote: [...] > The documentation is a little lacking, but that will be changing in > the next few days. Examples of using snake-guice with CherryPy, Django > and TurboGears are just a few days off as well. The API tests[3] show > simple clear examples. [...] > 3. > http://code.google.c

Re: Why doesn't StopIteration get caught in the following code?

2009-04-04 Thread andrew cooke
grocery_stocker wrote: while True: > ...i = gen.next() > ...print i > ... > 0 > 1 > 4 python's magic isn't as magic as you hope. roughly speaking, it only does the necessary rewriting (writing the equivalent code with next etc etc) when you define a function or a method that contains

Re: Testing dynamic languages

2009-04-04 Thread andrew cooke
andrew cooke wrote: > if you are going to do that, stay with java. seriously - i too, am a java > developer about half the time, and you can make java pretty dynamic if you > try hard enough. look at exploiting aspects and functional programming > libraries, for example. also, of c

Re: Testing dynamic languages

2009-04-04 Thread andrew cooke
grkunt...@gmail.com wrote: > If I am writing in Python, since it is dynamically, but strongly > typed, I really should check that each parameter is of the expected > type, or at least can respond to the method I plan on calling ("duck" > typing). Every call should be wrapped in a try/except stateme

  1   2   3   4   >