Thread: [pygccxml-development] Patch to add a little syntactic sugar
Brought to you by:
mbaas,
roman_yakovenko
From: Allen B. <al...@vr...> - 2006-05-17 16:54:37
Attachments:
scopedef_sugar.patch
|
I have included a patch that I find useful for simplifying decl lookup in pyplusplus. It allows code like the following to be written using the __getitem__ interface. ns = mb.global_ns.namespace( 'my_ns',) ns["MyClass"].exclude() class_a = ns["ClassA"] class_a["doSomething"].include() It also allows an attribute interface (similar to Pyste). ns.MyClass.exclude() class_a = ns.ClassA class_a.doSomething.include() These both can be used to dramatically reduce the amount of clutter (redundant code calls) using in a script to generate bindings. Although the second one could be seen as a little dangerous because of potential name conflicts, I think the interface of Pyste has proven that people like an interface like this because it allows something that feels like a very tight domain specific langauge for creating bindings. I know that neither of these methods is anything more then syntactic sugar, but IMHO it is pretty sweet sugar. :) -Allen |
From: Roman Y. <rom...@gm...> - 2006-05-17 17:23:17
|
On 5/17/06, Allen Bierbaum <al...@vr...> wrote: > I have included a patch that I find useful for simplifying decl lookup > in pyplusplus. > > It allows code like the following to be written using the __getitem__ > interface. > > ns =3D mb.global_ns.namespace( 'my_ns',) > ns["MyClass"].exclude() > class_a =3D ns["ClassA"] > class_a["doSomething"].include() > > It also allows an attribute interface (similar to Pyste). > > ns.MyClass.exclude() > class_a =3D ns.ClassA > class_a.doSomething.include() > > These both can be used to dramatically reduce the amount of clutter > (redundant code calls) using in a script to generate bindings. Although > the second one could be seen as a little dangerous because of potential > name conflicts, I think the interface of Pyste has proven that people > like an interface like this because it allows something that feels like > a very tight domain specific langauge for creating bindings. > > I know that neither of these methods is anything more then syntactic > sugar, but IMHO it is pretty sweet sugar. :) Thanks. I will apply the patch. There is only one question: Why not to implement the __getattr__ next way: found_decls =3D self.decls(name=3Dname, allow_empty=3DTrue) if found_decls: return found_decls else: raise AttributeError Why do you need treatment for '__' ? > -Allen Thanks. --=20 Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |
From: Roman Y. <rom...@gm...> - 2006-05-17 18:04:35
|
On 5/17/06, Roman Yakovenko <rom...@gm...> wrote: > On 5/17/06, Allen Bierbaum <al...@vr...> wrote: > > I have included a patch that I find useful for simplifying decl lookup > > in pyplusplus. > > > > It allows code like the following to be written using the __getitem__ > > interface. > > > > ns =3D mb.global_ns.namespace( 'my_ns',) > > ns["MyClass"].exclude() > > class_a =3D ns["ClassA"] > > class_a["doSomething"].include() I like this part of the patch > > It also allows an attribute interface (similar to Pyste). > > > > ns.MyClass.exclude() > > class_a =3D ns.ClassA > > class_a.doSomething.include() This one I like less. If user made some mistake and spelled wrong property = name of the class_t or namespace_t and there is a declaration with that name, then .... I prefer to apply the patch without __getattr__ part. Thoughts? --=20 Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |
From: Allen B. <al...@vr...> - 2006-05-17 18:18:20
|
Roman Yakovenko wrote: > On 5/17/06, Roman Yakovenko <rom...@gm...> wrote: > >> On 5/17/06, Allen Bierbaum <al...@vr...> wrote: >> > I have included a patch that I find useful for simplifying decl lookup >> > in pyplusplus. >> > >> > It allows code like the following to be written using the __getitem__ >> > interface. >> > >> > ns = mb.global_ns.namespace( 'my_ns',) >> > ns["MyClass"].exclude() >> > class_a = ns["ClassA"] >> > class_a["doSomething"].include() > > > I like this part of the patch > >> > It also allows an attribute interface (similar to Pyste). >> > >> > ns.MyClass.exclude() >> > class_a = ns.ClassA >> > class_a.doSomething.include() > > > This one I like less. If user made some mistake and spelled wrong > property name > of the class_t or namespace_t and there is a declaration with that name, > then .... I prefer to apply the patch without __getattr__ part. > > Thoughts? I assume you mean something like: MyClass.someMethod.call_policies = .... so what if they spell it 'call_pooicies', it that case it will raise the exception that python would normal raise and just throw and AttributeError. In the case of trying to look up a decl and spelling that decl wrong, for example: MyClass.someMethod_bad it still throws an AttributeError because that is the standard way to deal with a python attribute lookup problem. It would be nice if the exception included the name that failed, but I don't see a way to do this. I thought about just allowing a failed find to throw the pyplusplus RuntimeError exception for a failed lookup, but this would make all the decl classes behave dramatically different then they way python would expect them to work normally. For example in a previous e-mail I talk about how there are some failed attemtps to look up '__newstate__'. The code that is failing with this lookups is able to work correctly (I would guess) because it catches the AttributeError exception to detect that the lookup failed. If we changed it so the classes didn't throw this exception anymore, this code would fail because it would not be expecting a RuntimeError. So, it is not a perfect solution, but it seems like a reasonable one since it makes the attribute lookup keep behaving as standard python lookups would. -Allen |
From: Roman Y. <rom...@gm...> - 2006-05-17 18:31:58
|
On 5/17/06, Allen Bierbaum <al...@vr...> wrote: > Roman Yakovenko wrote: > >> > ns.MyClass.exclude() > >> > class_a =3D ns.ClassA > >> > class_a.doSomething.include() > > > > > > This one I like less. If user made some mistake and spelled wrong > > property name > > of the class_t or namespace_t and there is a declaration with that name= , > > then .... I prefer to apply the patch without __getattr__ part. > > > > Thoughts? > > I assume you mean something like: > > MyClass.someMethod.call_policies =3D .... > > so what if they spell it 'call_pooicies', it that case it will raise the > exception that python would normal raise and just throw and AttributeErro= r. I don't have a problem with exceptions you raise. I have a problem when exception is not raised. User wrote MyClass.named, when he meant MyClass.name and MyClass has named variable. Almost same example, different view: class X{ string name; }; User can not use X.name syntax, because name is the property of every declaration. So he is forced to switch to alternative syntax: X["name"]. I don't like this. > > -Allen > --=20 Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |
From: Allen B. <al...@vr...> - 2006-05-17 18:40:04
|
Roman Yakovenko wrote: > On 5/17/06, Allen Bierbaum <al...@vr...> wrote: > >> Roman Yakovenko wrote: >> >> > ns.MyClass.exclude() >> >> > class_a = ns.ClassA >> >> > class_a.doSomething.include() >> > >> > >> > This one I like less. If user made some mistake and spelled wrong >> > property name >> > of the class_t or namespace_t and there is a declaration with that >> name, >> > then .... I prefer to apply the patch without __getattr__ part. >> > >> > Thoughts? >> >> I assume you mean something like: >> >> MyClass.someMethod.call_policies = .... >> >> so what if they spell it 'call_pooicies', it that case it will raise the >> exception that python would normal raise and just throw and >> AttributeError. > > > I don't have a problem with exceptions you raise. I have a problem > when exception > is not raised. User wrote MyClass.named, when he meant MyClass.name and > MyClass has named variable. > > Almost same example, different view: > class X{ > string name; > }; > > User can not use X.name syntax, because name is the property of every > declaration. > > So he is forced to switch to alternative syntax: X["name"]. > > I don't like this. Yes, I don't see anyway around this. That is why this syntax is a little dangerous. It is nice when it works but there are certain reserved words that it is not going to work with. Personally I don't think this is enough reason to not use the API, but it does mean that users need to be careful when they do (or atleast they need to be warned about the potential problems in the documentation). It would have to be considered an advanced feature unless someone can think of a way around this problem. -Allen |
From: Roman Y. <rom...@gm...> - 2006-05-17 18:51:59
|
On 5/17/06, Allen Bierbaum <al...@vr...> wrote: > Yes, I don't see anyway around this. That is why this syntax is a > little dangerous. It is nice when it works but there are certain > reserved words that it is not going to work with. My main problem with this part, is that user can run into the problem. I try to explain my self. When he misspelled some reserved word, he don't h= ave to know that there is __getattr__ function that does some work. > Personally I don't think this is enough reason to not use the API, but > it does mean that users need to be careful when they do (or atleast they > need to be warned about the potential problems in the documentation). In most cases, this part of documentation will be skiped. > It would have to be considered an advanced feature unless someone can > think of a way around this problem. The bottom line: if you don't mind I would like to exclude __getattr__ part of the patch. If you feel, that this part is so good, then I will insert it, but user will have to turn this feature on. I will try to find how to do this. > -Allen > > --=20 Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |
From: Allen B. <al...@vr...> - 2006-05-17 18:10:53
|
Roman Yakovenko wrote: > On 5/17/06, Allen Bierbaum <al...@vr...> wrote: > >> I have included a patch that I find useful for simplifying decl lookup >> in pyplusplus. >> >> It allows code like the following to be written using the __getitem__ >> interface. >> >> ns = mb.global_ns.namespace( 'my_ns',) >> ns["MyClass"].exclude() >> class_a = ns["ClassA"] >> class_a["doSomething"].include() >> >> It also allows an attribute interface (similar to Pyste). >> >> ns.MyClass.exclude() >> class_a = ns.ClassA >> class_a.doSomething.include() >> >> These both can be used to dramatically reduce the amount of clutter >> (redundant code calls) using in a script to generate bindings. Although >> the second one could be seen as a little dangerous because of potential >> name conflicts, I think the interface of Pyste has proven that people >> like an interface like this because it allows something that feels like >> a very tight domain specific langauge for creating bindings. >> >> I know that neither of these methods is anything more then syntactic >> sugar, but IMHO it is pretty sweet sugar. :) > > > Thanks. I will apply the patch. There is only one question: > Why not to implement the __getattr__ next way: > found_decls = self.decls(name=name, allow_empty=True) > if found_decls: > return found_decls > else: > raise AttributeError > Why do you need treatment for '__' ? I didn't originally have it but without it I was seeing a lot of searches for '__getnewargs__' and '__getstate__'. I don't know why they were happening but I didn't think it was a good idea to spend time searching for decls that we know are not there. It was more of an optimization then anything. -Allen > > >> -Allen > > > Thanks. > > |
From: Allen B. <al...@vr...> - 2006-05-17 19:18:22
|
> > The bottom line: if you don't mind I would like to exclude __getattr__ > part of the > patch. If you feel, that this part is so good, then I will insert it, > but user will > have to turn this feature on. I will try to find how to do this. > I am fine without it. I just liked it because it allowed pyplusplus to feel a lot more like the Pyste interface. This could help with user adoption and porting. -Allen |
From: Roman Y. <rom...@gm...> - 2006-05-17 19:22:46
|
On 5/17/06, Allen Bierbaum <al...@vr...> wrote: > > > > > The bottom line: if you don't mind I would like to exclude __getattr__ > > part of the > > patch. If you feel, that this part is so good, then I will insert it, > > but user will > > have to turn this feature on. I will try to find how to do this. > > > I am fine without it. I just liked it because it allowed pyplusplus to > feel a lot more like the Pyste interface. This could help with user > adoption and porting. I understand all those advantages, but I think that the price it too high. Anyway thanks. > -Allen > > --=20 Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |