Posts

Showing posts with the label interop

A Good Mix 33: Embedding Python and Ruby, Profiling IronPython, News on JScript, ctypes and DeviantArt

More IronPython and DLR related projects, articles and news from around the web. Embedded IronRuby and IronPython in Silverlight with Multiple Source Files A  nice example of embedding both IronPython and IronRuby in a single C# project. As an added bonus the project is a Silverlight project so you can add both Python and Ruby scripting to applications that run in the browser. slimtune: A free profiling and performance tuning tool for .NET applications IronPython 2.6 has useful new hooks for profiling and debugging IronPython code. Unfortunately most 'standard' .NET tools don't know how to use these, and if you attempt to profile IronPython code (particularly in an embedded environment) you have to work hard to get useful information about performance of your Python code. It's nice to see a new (and open source) tool that is designed to work with IronPython: SlimTune is a free profiler and performance analysis/tuning tool for .NET based applications, including C#...

Executing IronPython Code from IronRuby

The Dynamic Language Runtime is a framework for writing dynamic languages that run on the .NET framework, with the two "Microsoft sponsored" languages being IronPython and IronRuby. There is also IronScheme , a community project hosted on Codeplex. The promise of the DLR is not just that it makes implementing dynamic languages possible , but also that through the DLR .NET languages can interoperate. This includes IronPython and IronRuby (etc) interacting with C#, F# and VB.NET (the supported and statically typed Microsoft .NET languages) but also the reverse (statically typed languages interoperating with dynamically typed languages) and dynamically typed languages interoperating amongst themselves. All very incestuous. As far as I know this is still unique amongst the modern polyglot runtimes (.NET and Mono, the JVM, LLVM, Parrot and so on). Whilst IronRuby in particular has been changing very rapidly (IronRuby has only recently reached 1.0 RC 1) it has been hard to g...

IronRuby 0.9: Performance and interop with IronPython

I'm a few weeks behind the curve on this one, but I'm finally catching up with my backlog; IronRuby 0.9 has been released. It looks like they didn't make the version 1.0 by OSCON but they have made a lot of progress. IronRuby 0.9 Released Jimmy Schementi lets us know the highlights of the new release: Library performance was a big focus for this release; basically going though the Ruby Benchmark suite and making sure any obvious slowness was fixed. As I said in my previous post about OSCON, IronRuby is approximately 2x faster than MRI when running the benchmark suite, but in the near future a complete evaluation of IronRuby’s performance will be done and published on the website. Antonio Cangiano has already published benchmark results between IronRuby 0.9 and Ruby 1.8.6, and things look really good for IronRuby. On the compatibility front, the Win32OLE Ruby library is now available in IronRuby. This builds on top of IronRuby’s existing COM interop from version 0.5, lettin...

__clrtype__ Metaclasses: Positional Attribute Parameters

Harry Pierson (the IronPython Program Manager for Microsoft) has been exploring and explaining the new __clrtype__ feature coming in IronPython 2.6 (or already available if you are using the beta). When we last left him Harry had been creating Python classes with simple .NET attributes applied to members. In this post he delves further and applies attributes that take parameters: __clrtype__ Metaclasses: Positional Attribute Parameters The basic infrastructure for custom attributes in IronPython is in place, but it’s woefully limited. Specifically, it only works for custom attributes that don’t have parameters. Of course, most of the custom attributes that you’d really want to use require additional parameters, both the positional or named variety. Since positional parameters are easier, let’s start with them. Positional parameters get passed to the custom attribute’s constructor. As we saw in the previous post, you need a CustomAttributeBuilder to attach a custom attribute to an att...

__clrtype__ Metaclasses: Simple Custom Attributes

One of the exciting new features in IronPython 2.6 is the addition of the __clrtype__ metaclass . This allows you to specify and customize the .NET type that backs your Python classes. This is important because by default new Python classes aren't also .NET classes. This is for various reasons: Python classes are mutable, .NET classes can't be garbage collected and so on. This causes one of the biggest gaps in the IronPython integration; using .NET attributes requires them to be applied to a real .NET class. There are other features, like some kinds of data binding, also a real .NET class. There are techniques to overcome this, but they're clunky at best. In IronPython in Action I demostrate runtime generation, compilation and subclassing of C# classes as one technique. At last the __clrtype__ metaclass provides a better way built into IronPython, but it is a pretty low-level feature requiring a lot of work to build on top of it. Harry Pierson, the IronPython program mana...

Addition of __clrtype__ to IronPython

One of the important additions to IronPython 2.6 is something that enables several important new ways of integrating with the .NET framework. One the use cases is for .NET attribute support, which has long been missing from IronPython. The new feature is __clrtype__ . This allows you to customize the CLR type used for Python classes. By default a Python class in IronPython is not a true .NET class. This is because Python classes are much more flexible than .NET classes (and can be garbage collected) and are better represented by a .NET object than a .NET type. This unfortunately means that as well as not supporting attributes, Python classes don't play well with any .NET feature that uses reflection over the CLR type to introspect the features of an object. This particularly includes data-binding. __clrtype__ works by extending the standard Python metaclass mechanism that allows you to customize class creation. To customize the CLR type that forms the underlying type for your cla...

Consuming Extension Methods in IronPython

Extension methods are a feature of .NET 3 that allows you to add methods to existing types. These are effectively static methods, but are called as if they were instance methods on the extended type. Saveen Reddy has a blog entry on using these methods from IronPython. Consuming Extension Methods in IronPython One of my projects implemented in C# makes frequent use of extension methods. Recently I started using IronPython to script that project and what I learned is that consuming those extension methods in C# is straightforward but with IronPython some extra work is involved. It turns out to be harder than it should be...

Adding References to Assemblies in IronPython

The 'first point' of interoperation between Python code and the .NET framework is the clr module. This allows you to dynamically add references to .NET assemblies in order to be able to import classes from the namespaces they contain. The clr module has a bewildering array of different functions for adding references to assemblies, and each of them have slightly different semantics and use cases. Dino Veihland (IronPython Developer) recently posted a very useful explanation to the mailing list: clr.AddReference(str) will first try Assembly.Load and then try Assembly.LoadWithPartialName - this should mean that trying with the strong name won't do anything because LoadWithPartialName will do that for you. This is kind of the one-stop shopping for loading assemblies in the GAC or the app base. If those don't work it will also ultimately search sys.path when the load fails. clr.AddReference(asm) will just add the assembly that you've already loaded - this is u...

OLE Automation support ON in IronPython 2.0 Beta 4

IronPython 2.0 beta 4 isn't out of the door yet, but already the team are blogging about some of the new features. Shri Borde has a new blog entry about OLE Automation with IronPython, without having to rely on interop assemblies. This makes for easier automation of Excel, Word and any other COM enabled Windows applications. In previous versions of IronPython trying to doing this without the Primary Interop Assemblies available would try to generate them on the fly (which could take several minutes). But with IronPython 2.0 BEta 4 it should work fine through IDispatch . Shri provides some details of how it works in practise, how it works under the hood, and some of the known issues (including how to turn it off if it causes any problems!). OLE Automation support ON in IronPython 2.0 Beta 4

IronPython and Active Directory

Two new recipes have been posted to the IronPython Cookbook . They are both about interacting with Active Directory from IronPython. ActiveDirectory and RefreshCache DirectoryServices to Dictionary The first article shows how to use ' System.DirectoryServices ' and ' RefreshCache ' to load attributes not exposed by ADSI. The second is a helper object that turns ' DirectoryService' objects into Python dictionaries, which are easier to work with.

.NET 4, C# 4 and the DLR

The recent C# 4 Meet the Design Team Video discussed the future of C# 4, and in particular how it might have more features inspired by dynamic languages and offer better interoperation with the DLR languages like IronPython and IronRuby. (Currently dynamic objects created using these languages can be used from statically typed .NET languages - the DLR provides an API to do this - but it could certainly be made more convenient ). In this blog entry, Jb looks at potential DLR integration into C# 4 and particularly examines the similarities between LINQ Expression Trees and DLR ASTs (in fact these will be merged completely at some point to use the same representation for LINQ expressions and parsed dynamic languages - this will not only be simpler for developers working with both but make it a lot easier to bring LINQ into DLR languages): .NET 4, C# 4 and the DLR UPDATE: He's followed up with a new post, confirming that in IronPython 2 Beta 4 (not yet released but the sourcecode is r...

CLS Compilation of IronPython

In IronPython (and other Dynamic Language Runtime languages) Python classes are not static .NET types. There are various reasons for this. .NET classes can't be garbage collected (assemblies can't be unloaded - unless you load them in another AppDomain and unload the whole AppDomain). Additionally dynamic languages typically allow you to add and remove members at runtime and in Python you can even change the base classes at runtime. For instances of Python classes we can also change the class at runtime, not usually recommended but possible... This means that normally a Python class can't be a static .NET type, instead Python classes are .NET objects (instances of their metaclass - for new style classes without a custom metaclass that means ' PythonType '). This isn't without problems - in order to apply .NET attributes you basically need a sttic type. This leaves a hole in the interoperation between Common Language Runtime features and dynamic languages, and m...

Embedded IronPython 1 - The Simple (and Chinese) Way

This blog entry by Terence Chao is very short, and barring about two words all in Chinese. However it links to a PDF document (by the author of the blog) with examples of embedding IronPython 1 in C# and using IronPython to automate Excel. More Chinese text, but straighforward code examples: Embedded IronPython 1 - The Simple Way His Excel automation example takes command line arguments to convert Excel files into CSV files. If you are (or can read) Chinese, then you might also be interested in these entries: Dynamic Language Console: Create Python Host A simple example of the IronPython 2 hosting API, executing code from a string. This entry is mostly code, with screenshots and diagram. IronPython Tutorial This one is mainly text, but appears to be a pretty impressive IronPython tutorial. It looks like it is the start of a translation of the tutorial (by Microsoft) that comes with IronPython. You can find it on the web here . IronPython and Silverlight Another blog entry with screensh...

exchange2ical Available on Codeplex

Jon Udell has written an IronPython script that takes calendars from an Exchange server and publishes them in iCalendar format. The project is available from codeplex: exchange2ical Available on Codeplex

Quick Scripting for .NET

Regdeveloper have started a series of articles on scripting .NET with IronPython. The first one introduces Python and illustrates its power by automating Office with a few lines of IronPython code: Quick Scripting for .NET with IronPython

IronPython 2, 'with' and COM

IronPython 2 targets Python 2.5, which means that the with statement is available. This is convenient because the currently released version of IronPython 2 there is a memory leak that keeps COM objects alive after they are used. This bug is fixed in the IronPython source code control, but in the meantime a Japanese blogger has a workaround that uses 'with' and calls Marshal.ReleaseComObject - his fantastically nested example is for Excel Interop. with statement and COM His blog has some other interesting IronPython stuff, including this example of Excel 2007 interop that creates a spreadsheet from a bitmap! Excel 2007 Interop

SharePoint, IronPython, and another lesson in the virtue of laziness

A blog entry from Jon Udell on using IronPython to work with documents accessed via a Sharepoint server: SharePoint, IronPython, and another lesson in the virtue of laziness I’m doing an internal project that involves reading several different data sources from a SharePoint 2007 server, merging them, and posting the merged data back to the server. Being lazy, I wanted to use IronPython, write as little code as possible, and do everything dynamically .

Visio and IronPython

Two blog entries from Saveen Reddy, illustrating using IronPython with Microsoft Visio : A preview of embedding IronPython into Visio 2007 Automating Visio with IronPython for creating Continuous Gradients

Windows Media Encoder

It looks like there is a lot of IronPython going on in Japan! This is an example of using Windows Media Encoder Interop : Windows Media Encoder の COM Once again the text is in Japanese but the code speaks for itself...

Reading CSV Files, Folder Backup, Interop and the SaveFileDialog on the IronPython Cookbook

New additions to the IronPython Cookbook include: Reading CSV Files with a Fast Csv Reader Deploying ironPython Scripts by Dodiggitydag (!) Introduction to Interop by Seo Sanghyeon Folder Backup Script also by Dodiggitydag; it uses the open source SharpZipLib The Windows Forms SaveFileDialog