Dynamic Languages and .NET – Developer Day Scotland slides and code samples

This weekend I was in Glasgow presenting my ‘dynamic languages and .NET’ presentation at DeveloperDay Scotland. I demonstrated how you can take advantage of IronRuby as a language, but also how you could host the DLR within your own application to enhance your feature set. I had a great time and the conference was a great success. A massive thank you to Colin Mackay and the others for organising the event, as well as everyone who attended my session.

Slides can be found below, hosted via SlideShare. Code samples can be downloaded here. Photos can be found here.

I was also involved in the Database Testing panel. Very interesting discussion on how to test databases, thank you to everyone who attend the discussion.

One slight disappointment, Gary Short left before giving his IronSmalltalk grok talk – http://www.flickr.com/photos/colinangusmackay/3496808871/

Next up, DDD South West.

Embedding IronRuby and the DLR into Red Gate’s SQL Data Generator

Over the past few months there has been a lot of discussions online about how embedding the DLR (Dynamic Language Runtime) into your application opens up some amazing use cases. For example, Phil Haack recently wrote about how ASP.net MVC views can be stored in the database thanks for the DLR.  Resolver Systems have been doing with with great success.

Tonight, my latest article for Simple Talk was published. The article explains how I went about creating a new generator for SQL Data Generator which allows the user to define the data to be generated based on IronRuby code.  I think this is an amazing use case, both for SQL Data Generator and the DLRIronRuby. By taking advantage of the DLR, we have opened up some extremely useful use-cases within the application. To find out more take a look at the article on Simple Talk.

An example of the generator in action is shown in the screenshot.

IronRuby_SDG 

The article includes the source code and binaries for the SQL Data Generator example generator which you can start using within your project.

NOTE: This Plug-in generator is provided ‘as-is’ and is completely unsupported.

Downloading IronRuby from GitHub

This week the IronRuby team moved their source repository over to GitHub, with the layout now reflecting the structure they maintain internally. I just wanted to cover how I downloaded and built IronRuby on a Windows Vista machine – LinuxMac users will need to build via Mono. The first task is to download the code, as such you will need a git client, at the moment the best client I have found is msysgit.

msysgit installs both a GUI and command line, to download the source code I simply used the command line. Executing the following command within the directory where you want the code to be stored, for me this was E:IronRubyGit.

git clone git://github.com/ironruby/ironruby.git

The next task is to build the project, if you have MRI (Matz’s ruby) you can use the rake compile command, this will compile everything for you. If you don’t have MRI installed, you can build the Ruby.sln file using Visual Studio 2008 or the C# compiler (csc).

E:IronRubyGitironrubymerlinmainLanguagesRuby>rake compile
(in E:/IronRubyGit/ironruby/merlin/main/Languages/Ruby)
——————————————————————————-
dlr_core
——————————————————————————-
rake aborted!
No such file or directory – e:ironrubygitironrubymerlinmainlanguagesrubysrcmicrosoft.scripting.core

(See full trace by running task with –trace)

However, I was greeted with a error message saying it was unable to find a file or directory. Luckily, the IronRuby mailing list is a great resource, and it turns out I needed to set the MERLIN_ROOT environment variable.

E:IronRubyGitironrubymerlinmainLanguagesRuby>set MERLIN_ROOT=e:ironrubygitironrubymerlinmain

As a side note, merlin was the original codename for the IronPython team, which now includes IronRuby and the DLR (see John Lam’s post)

After solving this, I hit another error regarding tf.exe, the Team Foundation Source Control command line, being missing.

Cannot find tf.exe on system path.

The build script (rake) does a check to see if everything is configured correctly for IronRuby development, however tf isn’t required for building and actually only required internally within the team. The check for tf.exe is made within E:IronRubyGitironrubymerlinmainLanguagesRubyrakemisc.rake, search for the string and simply remove it from the array.

commands += [‘tf.exe’, ‘svn.exe’] if IronRuby.is_merlin?

Run rake compile again and IronRuby should happily build with the assemblies being built to ‘E:IronRubyGitironrubymerlinmainbindebug’

However, after launching ir.exe (the IronRuby REPL), I was unable to reference rubygems. After a bit of investigating, turns out the $LOAD_PATHS where incorrect. These paths are set within ir.exe.config, all you need to do is replace the following part of the path ExternalLanguagesRubyRuby-1.8.6libruby with => externallanguagesrubyredist-libsruby . It’s used three times, after replacing the values I could happily use IronRuby.

Hope this helps!

Technorati Tags: , ,

Announcing IronEditor – An Editor for IronRuby, IronPython and other DLR languages

IronEditor 1.0.0.44For a while now I have been working on an application called IronEditor, this is a simple application designed to make it easier to pick up and start coding against the DLR based languages. By taking advantage of the DLR’s Hosting API, the application can execute code for any language built on top of the DLR platform.

The project is hosted at CodePlex, along with all of the source code.

Download: http://www.codeplex.com/IronEditor

Build: 1.0.0.46

Out of the box, the application works with IronRuby and IronPython, however one of the main aims of the application is to allow other languages to be easily embedded into the application.

The reason why I decided to build this is because Visual Studio Integration for the languages is a long way off and while playing around and creating code to use the languages is painful via the provided console applications. As such, the aim of the application is to provide a very lightweight way to edit and execute code, great while learning the languages and giving demos (I used this application for my NxtGenUG Oxford DLR session).

One of the items I’m really pleased about is the fact that the application works on Mono (Tested only on Ubuntu 8.04 and Mono 1.9.1), something which will definitely not be possible with the Visual Studio integration.

To run the application, you will need to ensure you have Mono installed on your machine. Download the application and extract the zip into a directory. Then enter the command:

mono IronEditor.exe

You will then have the same application, same binaries everything working on Mono.  The only difference is that syntax highlighting for IronPython doesn’t work at the moment.

IronEditor running on Mono 

I admit, at the moment the application is very basic. However, over the next few weeks and months I will build new features into the application to make it easier to start playing around with the DLR languages.

Executing IronRuby and IronPython Code

1) Start the application

2) File > New Project

3) From the drop down, select your language

NewProject

4) Type some code (print ‘Hello World’ is always good)

5) Press the green arrow, or hit F5.  Code is executed and the output is wrote to the Output Window.

Very quick and easy I think!

There are some very big limitations and bugs within the application, but I’m going for the ‘Release Early, Release Often’ approach. Various items could be improved, for example Ruby doesn’t have any syntax highlighting but this will come in time. There are some other much larger features I want to implement, keep an eye on the blog for more information as and when. Over the next few weeks I will also be blogging about the implementation of IronEditor and how it uses the DLR Hosting API. 

Any feedback is most welcome!

NOTE: As I mentioned this is a very early, it hasn’t had a great deal of testing.  Please don’t use it on your production code base just yet! I wanted to get a release out for some initial feedback, if it causes everything to go wrong – I’m very sorry!

Download: http://www.codeplex.com/IronEditor

Build: 1.0.0.46

IronPython – Classes within separate files

Tonight I was asked an interesting question regarding IronPython (well, Python) and how it handles the loose files when executing.  With languages like C#, everything is compiled into an assembly and you have namespaces in order to locate the classes.  However, in Python it is a little bit different.

With python, files can be either compiled into an assembly, or left loose within a directory depending on what you want to do.  If you have your files loose, then you will need to reference the different files (modulesclasses).  In the example below, I have a Customer class in a file called CustomerFile.py.  I then have a main method which creates the Customer and calls the method within a file called main.py.  The code for the files are here:

customerfile.py
class Customer(object):
  def PrintHello(self):
    print “Hello World!”

main.py
from customerfile import Customer
def main():
    print ‘Hello World’
    c = Customer()
    print ‘Created Customer’
    c.PrintHello()

if __name__ == “__main__”:
   print ‘Hello’
   main()

In order for the main file to know about the Customer File, we use the syntax – from import .  Here we say from the customerfile, import the Customer class at the top of the class. If we execute the file, everything is wrote out as expected.

IronPython-2.0B2>ipy.exe main.py
Hello
Hello World
Created Customer
Hello World!

It’s important that both files are located in a directory defined in the path search variable, by default it is the main directory and the Lib directory.

>>> import sys
>>> print sys.path
[‘.’, ‘…IronPython-2.0B2’, ‘…IronPython-2.0B2Lib’]

If for example, CustomerFile.py was moved into a directory called Customer within the main folder, we would need to add that to the path variable in order for IronPython to look in the directory for the file. The code below does this, note that before we can import os, we need to add a reference to the location of the standard library.

import sys
sys.path.append(“C:python25Lib”)
import os
sys.path.append(os.path.abspath(“Customer”))

Now, when we call the same code, IronPython will also search the Customer directory.  The path would now look something like this:

[‘.’, ‘IronPython-2.0B2’, ‘IronPython-2.0B2Lib’, ‘C:python25Lib’, ‘IronPython-2.0B2Customer’]

Not overly difficult, but I can see new users becoming confused by this.

Technorati Tags: ,

IronPython – The Language – Variables, Functions and Classes

In my previous post I talked about how to get started with IronPython, in this post I want to follow up with some very basic information about the actual language and some tips to enable you to start reading sample code. This post won’t make you an expert, but it should be enough to get you going.

One of the great things about IronPython and IronRuby is that there are already a huge amount of resources online about the language. A good starting point (apart form this post of course) is http://docs.python.org/ which contains the documentation for the Python language.  One problem with that approach is that there are minor differences different Python and IronPython.

Defining a variable

Defining a variable in IronPython seems a logical place to start.  The easiest way to play around with the language is by using the interactive console (ipy.exe).

To define a variable, you simply say = , for example:

>> x = 1

This creates a variable called x, of type integer (it has inferred the type) with the value of 1. As Python is a dynamic language, the way it handles variables is sightly different than you would expect compared to a static language like C#.  While Python is still a strongly typed language, it is perfectly legal to do this following:

>>> x = 1
>>> print x
1
>>> x = “Hello World”
>>> print x
Hello World
>>>

What happens is that within the first statement we create an integer object and assign it to x, in the second statement we create a string object and assign it to x, removing the reference to x. However, if we try and mix the types then it throws an exception, here we are trying to add an int to a string.

>>> x = “Hello World” + 1
Traceback (most recent call last):
  File , line unknown, in Initialize##24
TypeError: unsupported operand type(s) for +: ‘str’ and ‘int’

Python does know the type of the variable, it only uses it when it is actually required at runtime (late-binding) as far as I can tell.  For example, using the type function will return the correct type of the variable.

>>> print type(x)

So what does this mean while you are developing, well, it makes unit testing a lot easier as you don’t need to worry so much about getting an object of the extract type – it just needs to look like the type. Also, code is more generic as it doesn’t depend on the extract type.   However, you do run the risk of the type being wrong which is only found at runtime – but your automated tests will catch that right?

Creating a function

Now we can define variables, the next step is to define functions (methods). While Python is a dynamic language, it still has very strict language rules which must be followed, including it’s important on whitespace and tabbing unlike C#.

To define a method, we start with def, we then provide a name for our method, we add a number of parameters separated by commas, no need to include the type as Python will work it out for itself. Finally, we need to end the line with a colon :.  Next, we need to fill in the body of the method, the most important thing is to tab in to indicate the body, we can then write a number of lines of code, if the method should return a value then we can include that as the last line. To mark the end of the method, you have another carriage return.

Below we create a function called x, with the parameters y and z. In the method body, we just add the two and print them to the output (the console).

>>> def x(y, z):
…     print y + z

We can then simply call this method, notice that we can use both ints and strings and the correct action is taken.
>>> x(1,2)
3
>>> x(“Hello”, “World”)
HelloWorld

As we create the method, if we get something wrong then an error will be returned to alert us. Below I attempted to create a method but I forgot to include the colon at the end of the method.

>>> def x(y, z)
  File “”, line 1
    def x(y, z)
            ^
SyntaxError: unexpected token ‘

Similarly, if we do not indent the function body an error is returned.

>>> def x(y, x):
… print y + x
  File “”, line 2
    print y + x
    ^
IndentationError: expected an indented block

Below is just an example of how to return a value from a method.

>>> def x(y,x):
…     return y + x

>>> y = x(1,2)
>>> print y

Finally, python allows you to create functions within functions, which will have local scope. Great for code re-use and readability!

>>> def y():
…     def x(y, z):
…             print y + z
…     x(1,2)
…     x(“Hello”, “World”)

>>> y()
3
HelloWorld

Creating a Class

Creating a class is very similar to creating a function. The syntax is class ():  In the brackets, we can simply define all of the base classes.

>>> class MyClass(object):

We can then define function on the class in the same way, however one change is that we need to include self (similar to this in C#) as the first parameter of the method.
…     def PrintHello(self):
…             print “Hello!”
…     def Method2(self, x):
…             print “TWO TWO TWO”

To initialise the class, we simply say this:
>>> x = MyClass()

We can then call methods on it:

>>> x.PrintHello()
Hello!
>>> x.Method2(“Parameter”)
TWO TWO TWO

If we want to know all the methods on a class, we can do a dir.  Everything with a double underscore is an internal method. In the class we can define the actions to take when they get called.

>>> dir (x)
[‘Method2’, ‘PrintHello’, ‘__class__’, ‘__delattr__’, ‘__dict__’, ‘__doc__’, ‘__getattribute__’, ‘__hash__’, ‘__init__’, ‘__module__’, ‘__name__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__str__’, ‘__weakref__’]

For example:

>>> class InitClass:
…     def __init__(self):
…             print “Created”

>>> i = InitClass()
Created

Just to sum up, Python is a full featured language, I haven’t even touch onto some of the features of the language but hopefully it will provide you some insight into the code.

Technorati Tags: ,

IronPython Studio

In my previous post, I discussed Visual Studio Integration, IronPython and how difficult it was to get started. However, I came across an application called IronPython Studio (Colin also pointed it out on the comments) which uses the Visual Studio 2008 Shell to provide a IronPython IDE. You can download the application from http://www.codeplex.com/IronPythonStudio.

On my first attempt to install the application, I was greeted with this error message:

—————————
Microsoft Visual Studio Shell Isolated
—————————
Cannot find one or more components. Please reinstall the application.
—————————
OK  
—————————

Turns out you need to have Microsoft Visual Studio 2008 Shell (isolated mode) Redistributable Package (http://www.microsoft.com/downloads/details.aspx?FamilyId=ACA38719-F449-4937-9BAC-45A9F8A73822&displaylang=en) installed which the installer doesn’t check for, this is a 384mb download.

Installing this, I found that it doesn’t actually install it – if I had actually read it correctly I would have read that it was just extracting.  I then had to go into the directory where I had extracted it to (C:VS 2008 Shell RedistIsolated Mode) and run the real installer (vs_shell_isolated.enu.exe).  I then uninstalled and reinstalled IP Studio without any problems.

Upon launching Iron Python Studio, I was greeted with a very familiar feel to Visual Studio. Going File > New > Project gave me the same options as was available in the VS 2008 SDK integration.

image

After creating a new Windows Application, it looks and feels the same as VS so you won’t have any trouble navigating around.  There is some intellisense (it didn’t like System.Windows…) and the syntax highlighting seems to work.

image

You can build and debug just as you would for Visual Studio 2008 and C#.  It is a good application to have around, however for now I think I will stick with ipy and notepad2. The fact you need the VS2008 Shell installed at 385mb is another fact which turns me against this.  It’s a shame they didn’t just release it as a VS Package which could plug straight into VS2008.

Technorati Tags: ,

Getting started with IronPython

For the past week, I have been thinking about aims and target technology which I really want to learn more about, or dig even deeper into in some cases. One of those technologies is the DLR and the related dynamic languages. This post will just cover the very basics of getting started with one of the DLR languages – IronPython.  IronPython is about bring the Python language onto the DLR for use with the .Net framework.

1) Download

The first step to getting started is to actual download IronPython.  The project is hosted on CodePlex with all of the source code available, at the moment there are two main releases for IronPython, the first is 1.1.1 which is the latest 1x version and is built on top of the CLI.  The second is the latest 2.0 Beta 1 and is built on top of the DLR and is targeting Python v2.5

Download links:

1.1.1 – http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?ReleaseId=5141

2.0 Beta 1 – http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?ReleaseId=10266

For this series of posts, I will be focusing on 2.0 Beta 1.

2) Command Line

After you have downloaded the binaries as a zip file, extract it into its own directory.  You now have IronPython installed and setup – easy.  Within your extracted folder, you have everything you need to start using IronPython.

The main application is ipy.exe, this is the interactive command line for IronPython.  This allows you to execute IronPython code, as Python is a dynamic language there is no need to build the code as it can be executed on the fly.  This is great for experimenting with the language and trying out samples.

image

The other exe is ipyw.exe.  This is the same as ipy.exe, however runs windowless which allows you to execute a IronPython script without having to display the console.  For example, if we save the following code in a file called Hello.py in the same directory.

import clr
clr.AddReference(“System.Windows.Forms”)
import System.Windows.Forms as WinForms
WinForms.MessageBox.Show(“Hello”, “Hello World”)

And execute the file using ipyw with the following command.

>ipyw.exe Hello.py

The only thing displayed to the end user is the message box.

image

Other important files are the assembles, IronPython.dll, IronPython.Modules.dll and Microsoft.Scripting.dll.  From what I can tell, Microsoft.Scripting.dll is the core DLR code and it will be referred by the languages (IronRuby etc) and also hosters (Silverlight). IronPython.dll is the main assembly containing the language with Modules containing additional functionality and classes.  In Python, modules are useful utilities and ‘classes’ – for example PythonRegex.

3) Visual Studio Integration

At the moment, Visual Studio integration can be done via the SDK (http://www.microsoft.com/downloads/details.aspx?familyid=30402623-93ca-479a-867c-04dc45164f5b&displaylang=en), it looks like this is targeting IronPython 1.1.1. After installing the SDK, the IronPython integration is just a series of samples. After building the solution (IronPython.sln), you will have multiple assemblies but nothing actually which can be executed.  Well, in order to use these assemblies, you need to load them using the ‘Experimental Hive’, I always think this sounds like something from Resident Evil but this in fact is Visual Studio with a separate registry so you can destroy it without taking down your machine instance. The easiest way to do this is press F5, yet, while using the integration, I found it throw far too many exceptions and just be really difficult to use. Using Ctrl-F5 (Run without debugging).  This stopped Visual Studio kicking in saying something has gone wrong on every mouse click.

What the integration gives you is a Visual Studio project types for WinForms, ASP.net, Console Applications, WPF and Class library.  It also provides colorisation and intellisense (especial for the modem day developer) and a tool window which is similar to ipy.

I really hope this is involved in the near future, I’ve heard that the IronRuby team are working on integration so I guess its only time before this is officially released.

4) Executing stand-alone applications

When it comes to actually using IronPython as the basis for your own application you have a couple of routes and your requirements.  IronPython currently supports Silverlight and ASP.net (see point 5).  When it comes to WinFormsstandalone applications I can find three solutions which might work.  One is executing ipyw as shown in point 2.  Another solution is to host IronPython in a C# application, which I will discuss in a later post. The final solution is to use the Visual Studio Integration (point 3),  this includes the Visual Studio template which will compile the code into a single exe as static assemblies.

At present, not sure which is the ‘bestrecommend’ solution, had a quick chat with Michael Foord (who mentioned Static Assemblies) and it appears as if compiling your code into an assembly instead of loose py files is possible in 1.0 and 1.1 but not currently available in 2.0.

5) Silverlight and ASP.net

There has been a lot of talk about Silverlight and the DLR recently so I won’t go into details in this post.  It appears as if you just have your codebehind file as .py and Silverlight will do the rest for you (more or less).  Silverlight Quickstarts outlines how to do this – http://www.silverlight.net/Quickstarts/ProgramDlr.aspx

An area which hasn’t got as much attention is ASP.net futures (http://www.microsoft.com/downloads/details.aspx?FamilyId=A5189BCB-EF81-4C12-9733-E294D13A58E6&displaylang=en).  First, you need to create a new website (File > New > Website), you can then select IronPython from the language dropdown. You can then use IronPython as the code-behind language for your ASP.net web application.  While I didn’t have an in depth look, I tried to add a Click event to a button, but found I had to do everything manually – but it works.

6) Books and Resources

Michael Foord’s book IronPython in Action can be purchased as a early access ebook – http://www.manning.com/foord

Michael Foord’s Blog – http://www.voidspace.org.uk/python/weblog/index.shtml

Codeplex site – IronPython – Home and lots more resources listed here http://www.codeplex.com/IronPython/Wiki/View.aspx?title=News%20Feeds&referringTitle=Home

If you know any other really good resources for IronPython and the DLR, then please leave them in the comments.

I hope you have found this post useful to get you up and running using IronPython.

Technorati Tags: ,,