Re.Mark

My Life As A Blog

Archive for April 2010

Dynamic Configuration with IronPython and .NET 4

leave a comment »

A while back I posted a simple way of using IronPython to configure a .NET application.  The I spotted Herman’s question about whether the application would react to a change in the Configuration.py file.  The way I wrote the original sample the configuration is read in once.  But it’s fairly simple to modify the code to react to changes in Configuration.py.  Here’s the modified Program class:


class Program
{
	static void Main(string[] args)
	{
		FileSystemWatcher watcher = new FileSystemWatcher(AppDomain.CurrentDomain.BaseDirectory, "*.py");
		watcher.EnableRaisingEvents = true;
		watcher.NotifyFilter = NotifyFilters.LastWrite;
		DateTime modified = DateTime.Now;

		Console.TreatControlCAsInput = true;
		
		dynamic configuration = ConfigureFromIronPython();
		DisplayConfiguration(configuration);

		watcher.Changed += (sender, eventArgs) =>
		{
			if (DateTime.Now - modified > TimeSpan.FromMilliseconds(100))
			{
				modified = DateTime.Now;
				configuration = ConfigureFromIronPython();
				DisplayConfiguration(configuration);
			}
		};


		Console.WriteLine("Press any key to exit...");
		Console.ReadKey();

		watcher.EnableRaisingEvents = false;
		watcher.Dispose();

		Console.WriteLine("Exiting...");
	}

	private static dynamic ConfigureFromIronPython()
	{
		dynamic configuration = new ExpandoObject();
		ScriptEngine engine = Python.CreateEngine();
		ScriptScope scope = engine.CreateScope();
		scope.SetVariable("configuration", configuration);
		engine.ExecuteFile("Configuration.py", scope);
		return configuration;
	}

	private static void DisplayConfiguration(dynamic configuration)
	{
		for (int i = 0; i < configuration.Count; i++)
		{
			Console.WriteLine(configuration.Text);
		}
	}
}

By adding a FileSystemWatcher, we can react to changes in Configuration.py and update the application configuration.  (If you’re wondering the check of when the last modification occurred is to prevent handling the same change twice which can occur when using the FileSystemWatcher.)  The only other change is a minor refactoring to extract a method that displays the configuration.

Advertisements

Written by remark

April 30, 2010 at 8:44 pm

Posted in .NET, c#, Python

Learning IronRuby 1.0

leave a comment »

As you may have noticed, IronRuby 1.0 was released earlier this week.  I downloaded the .NET 4.0 version and headed to the Samples folder – in there, I noticed, is a Tutorial folder.  There’s a file called tutorial.bat, so I opened a command line in that folder and ran the batch file.  And I was impressed to be presented with an IronRuby Tutorial:

IronRuby Tutorial

There are three tutorial options available: an IronRuby Hosting Tutorial; an IronRuby Tutorial and a Try Ruby Tutorial.

So, if you’ve been meaning to get to grips with IronRuby, these tutorials could be just what you’ve been waiting for.

Written by remark

April 16, 2010 at 5:11 pm

Posted in Ruby

TechDays 2010

leave a comment »

Yesterday I was at TechDays 2010, where I presented a session on what’s new in VB 10 and C# 4.0.   Before diving into the new features, I talked about the trends in language design and evolution – for those who are interested, I’d take a look at this session where Anders Hejlsberg goes into far more detail.  Next I picked out a few personal highlights of what’s new in .NET 4.0 – including Code Contracts, Tuples and In-Process Side-By-Side Execution and talked briefly about co-evolution.  The new VB features I talked about were Auto-implemented Properties, Implicit Line Continuation, Collection Initializers and Statement Lambdas.  The new C# features I discussed were Optional and Named Parameters and Dynamically Typed Objects – of course no discussion of Dynamically Typed Objects would be complete without mentioning the Dynamic Language Runtime and I also talked about ExpandoObject and DynamicObject and how both C# and VB can use this new dynamic capability.  The last two features are new to both C# and VB: Improved COM Interoperability and Co- and Contra-Variance.

And for those of you who couldn’t make the event (and, thinking about it, for those who could, too), I’d suggest taking a look at the Underbelly backstage videos of day one and day two.

Written by remark

April 14, 2010 at 5:39 pm