[IronPython] IronPython Attribute Workaround

Michael Foord fuzzyman at voidspace.org.uk
Mon Jul 23 22:32:10 CEST 2007


Tim Riley wrote:
[snip..]
>  AutoCAD's .NET API allows
> you to register a command that is callable from the AutoCAD via a CommandMethod attribute[1]. Since IP doesn't support attributes I'd like to develop some sort of decorator that would clone the functionality of the CommandMethod attribute. So I could execute
> python code like:
>
> @commandmethod("test")
> def tester:
>     print "test worked"
>
> and it would register the test command so  I could call it from the
> command line.
>
>   

Right - so you want to dynamically create  methods (or functions) with 
attributes. The attribute takes the name that the command will be 
exposed with.

I wonder if AutoCAD even supports these being dynamically created?

If it does I wonder if we can do it by creating an inner class in C#. 
The method marked with the attribute would call down to another method 
(marked as virtual) that we can override from IronPython.

I'm not exactly sure of the C# syntax/semantics for inner classes - (a 
class factory) - so I'll have to do some digging. (i.e. will it allow us 
to set the argument to the attribute at runtime rather than compile time 
and can we return classes or only instances.)

AFAIK, attributes are normally bound at compile time rather than 
runtime, so using C# to create classes in this way may not work.

The proper way (*sigh*) is to use the Reflection.Emit API to add 
attributes. I've experimented Reflection.Emit - but never got as far as 
adding attributes dynamically. (You need to 'program in bytecode' using 
this API - which isn't too bad but has a bit of a learning curve 
associated.)

A *third* approach is to dynamically generate C# and compile to in 
memory assemblies (using the CodeDOM API if I remember correctly). That 
turns out to be surprisingly easy.

Michael Foord
http://www.voidspace.org.uk/ironpython/index.shtml

> [1] As an example:
> http://through-the-interface.typepad.com/through_the_interface/2007/07/updating-a-spec.html
>
>
> Tim
>
> On 7/23/07, Michael Foord <fuzzyman at voidspace.org.uk> wrote:
>   
>> Tim Riley wrote:
>>     
>>> Michael:
>>>
>>> Thanks for the reply. However when reading it my eyes glazed over. Is
>>> there any way you could provide a simple man like myself with some
>>> example code for me to peruse?
>>>
>>>       
>> What do you want to achieve? Write the IronPython code you would like -
>> and I will try and provide a C# stub that you can subclass. (You will
>> need a C# compiler - Visual Studio Express C# is probably the most
>> straightforward if you are using Windows.)
>>
>> Michael Foord
>> http://www.voidspace.org.uk/ironpython/index.shtml
>>
>>     
>>> Tim
>>>
>>> On 7/23/07, Michael Foord <fuzzyman at voidspace.org.uk> wrote:
>>>
>>>       
>>>> Tim Riley wrote:
>>>>
>>>>         
>>>>> I know that IronPython doesn't support attributes but does anyone know
>>>>> of a workaround that will allow IP code to use them?
>>>>>
>>>>>
>>>>>           
>>>> The IronPython team are strangely quiet every time someone asks this... ;-)
>>>>
>>>> A lot of people would like an answer to this question. Currently the
>>>> only way is to use stub C# classes.
>>>>
>>>> Michael Foord
>>>> http://www.voidspace.org.uk/ironpython/index.shtml
>>>>
>>>>
>>>>
>>>>         
>>>>> _______________________________________________
>>>>> users mailing list
>>>>> users at lists.ironpython.com
>>>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>>>>
>>>>>
>>>>>
>>>>>           
>>>> _______________________________________________
>>>> users mailing list
>>>> users at lists.ironpython.com
>>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>>>
>>>>
>>>>         
>>> _______________________________________________
>>> Users mailing list
>>> Users at lists.ironpython.com
>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>>
>>>
>>>       
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>     
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>   




More information about the Ironpython-users mailing list