Re.Mark

My Life As A Blog

Archive for the ‘c#’ Category

TF#I Friday #7 Part 2 : In which it still isn’t Friday

leave a comment »

Looking at how to call F# from C#, I thought I should share this link.  I have 2 projects in one solution (one in F# and one in C#.)  The C# project (a console application) references the F# project (a library) and that works.  Doesn’t do anything useful yet, but one step at a time…

Written by remark

June 29, 2011 at 3:09 pm

Posted in .NET, c#, Development, F#

TF#I Friday #6 : In which I write no F# at all

leave a comment »

I’ve been spending a little time recently writing a simple Windows Phone 7 app and I’ve been impressed with how easy it has been.  I’ve been thinking of writing an anagram solver as an exercise in F# – and it might make for another simple phone app – so I thought I’d start with a few simple steps.

The first step is to try (part of) a potential solution – given a list of words, group the words by the constituent letters (so that, for instance, horse and shore would be grouped together.)  That way, I won’t have to try every single combination of letters and compare against a list of words.  My intention this week was to write a simple program in C# to do this.  Here’s the method that does the work:


private Dictionary<string, List<string>> ProcessWordList(string[] words)
{
    Dictionary<string, List<string>> result = new Dictionary<string, List<string>>();

    foreach (string word in words)
    {
        IOrderedEnumerable<char> ordered = from char a in word.ToCharArray()
                                           orderby a
                                           select a;
        string orderedString = new string(ordered.ToArray());
        if (result.ContainsKey(orderedString))
        {
           result[orderedString].Add(word);
        }
        else
        {
            List<string> list = new List<string>();
            list.Add(word);
            result.Add(orderedString, list);
        }
    }

    return result;
}

Next exercise is either to extend the program towards solving anagrams or to write the method above in F#.

Written by remark

May 27, 2011 at 5:31 pm

Posted in .NET, c#, Development, F#, Languages

TF#I Friday #5 : In which I return to C# for a bit

leave a comment »

In my last post I counted some words using F#, which turned out to require a single, simple line of F#.  When I’d done the same thing before in C# i had iterated over the words and kept a count as I went, which is a typically imperative approach.  So, I wondered if you could apply the functional approach to C# – perhaps using LINQ.  Turns out you can.

Firstly, it’s helpful to have some words to count.  Here’s a simple approach:


string test = "The cat sat on the mat.";
string[] words = test.Split(' ');

(In F# I populated a list of words directly, so there’s an extra line of C# here – largely because I started with my previous C# code.)  Right, now to the counting in one line:


var result = from word in words
             let strippedWord = StripPunctuation(word).ToLower()
             where strippedWord.Length > 0
             group word by strippedWord into grouped
             select new { Word = grouped.Key, Count = grouped.Count() };

You  may have noticed a call to StripPunctuation – a utility function I had in my previous C# code.  Here it is (declared static as I was running it in a console application:


private static string StripPunctuation(string word)
{
    string result = word;
    if (result.Length > 0)
    {
        if (char.IsPunctuation(result[0]))
        {
            result = result.TrimStart(result[0]);
        }
        if (result.Length > 0)
        {
            if (char.IsPunctuation(result[result.Length - 1]))
            {
                result = result.TrimEnd(result[result.Length - 1]);
            }
        }
    }
    return result;
}

And now, with a little sprinkling of dynamic capability, outputting the results to the console:


foreach (dynamic entry in result)
{
    Console.WriteLine("{0}\t{1}", entry.Word, entry.Count);
}

So it is possible to apply the more functional approach courtesy of LINQ, although there’s still more code than I had in F#.  The C# is doing a couple of extra things (it strips out punctuation and is case insensitive) – but the point isn’t really the comparison between the two examples so much as the fact that by grasping some functional concepts can result in a change to your C# – which is a good reason to learn some F#.

Written by remark

March 25, 2011 at 5:43 pm

Posted in .NET, c#, Development, F#, Languages

TF#I Friday #4 : In which I count some words

leave a comment »

For a recent MSDN Flash article I wrote some simple code to calculate word frequency in C#.  As I get to grips with F#, I’m learning that the most rewarding but also the most difficult aspect is to think in a more functional way.  To count words in an imperative style (as I did in my C# example) I would iterate through a collection of words and keep a running count.  And, of course, you could write code in F# to do that.  But what would be the point?  How about approaching it in a different fashion?  So, with those questions in mind, I fired up Visual Studio and went about trying to bend my brain into a more F# like shape.  One of the things I like about F# is F# Interactive – a REPL which makes trying out and learning F# (as well as prototyping) easy – so that was where I started.  First thing I needed to do was to create a list of words (since at this stage I’m concerned simply with calculating frequency and not reading files or strings.)  It’s fairly simple to do that in F#:

let words = ["the"; "cat"; "sat"; "on"; "the"; "mat"];;

(the double semicolons are signal to F# Interactive the completion of a statement.)  After reading a bit about processing sequences in F#, I spotted that there is a function to count elements in a list – it can easily be used against the whole list like this:

let count = words |> Seq.countBy(fun x -> x);;

The countBy function takes a function to generate a key – in this case we can use each individual word.  To see if that has worked, we can print out the contents of the result:

printfn "%A" count;;

And in this case, I got the following result:

seq [("the", 2); ("cat", 1); ("sat", 1); ("on", 1); …]
val it : unit = ()

Which means it worked as intended.  There’s work to be done to make it the equivalent of the C# code, but the core counting is implemented in one line of code.

Written by remark

March 4, 2011 at 5:09 pm

Posted in .NET, c#, Development, F#, Languages

Dynamic Football Stats

with one comment

A couple of days ago I noticed that The Guardian had made data about the England vs USA game that took place last week available.  I downloaded the data (which is in a Google Apps spreadsheet) and saved each sheet as a CSV file.

Originally, I intended to read the data with IronPython.  Reading CSV data with Python is very simple – there’s a built in CSV module.  However, this module is written in C, which means it’s not available in IronPython – see here for more info.  There is aproject called IronClad that allows Python modules written in C to be used from IronPython.  At the moment, it’s built against .NET 2, which means that I could get it to work in .NET 2, but I had plans to use .NET 4 and the dynamic support in C#.   Time for another approach.

Using the CsvReader class,it’s easy to access the data in a CSV file.  I started with the Player Summaries sheet.  To make this dynamic (and, therefore, useful for each of these sheets and, potentially, other as yet unknown sheets) I created a class to hold each row of data.  Here it is:


public class DynamicDataObject : DynamicObject
{
    private readonly Dictionary<string, dynamic> data;

    public DynamicDataObject(Dictionary<string, dynamic> data)
    {
        this.data = data;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = data[binder.Name];
        return (result != null);
    }
}

By inheriting from DynamicObject, it will be possible to call this class dynamically – meaning that I can use the names of the data fields as defined properties on the class.  Next I created a DataReader class that reads the data from the CSV file and stores it as an IEnumerable<DynamicDataObject>.  Here’s that class:


public class DataReader : IEnumerable<DynamicDataObject>
{
    private readonly List<DynamicDataObject> dataList;

    public DataReader(string filename)
    {
        this.dataList = new List<DynamicDataObject>();
        using (StreamReader streamReader = new StreamReader(filename))
        {
            using (CsvReader reader = new CsvReader(streamReader, true))
            {
                string[] headers = reader.GetFieldHeaders();
                Dictionary<string, string> cleanHeaders = CleanHeaders(headers);
                while (reader.ReadNextRecord())
                {
                    Dictionary<string, dynamic> data = new Dictionary<string, dynamic>();
                    foreach (string header in headers)
                    {
                        int result;
                        dynamic value;
                        if (int.TryParse(reader[header],  out result))
                        {
                            value = result;
                        }
                        else
                        {
                            value = reader[header];
                        }
                        data.Add(cleanHeaders[header], value);
                    }
                    this.dataList.Add(new DynamicDataObject(data));
                }
            }
        }
    }

    private Dictionary<string, string> CleanHeaders(string[] headers)
    {
        Dictionary<string, string> result = new Dictionary<string, string>();
        foreach (string header in headers)
        {
            string cleanheader = header.Replace(' ', '_');
            cleanheader = cleanheader.Split('(')[0];
            result.Add(header, cleanheader);
        }
        return result;
    }

    #region IEnumerable<DynamicDataObject> Members

    public IEnumerator<DynamicDataObject> GetEnumerator()
    {
        return this.dataList.GetEnumerator();
    }

    #endregion

    #region IEnumerable Members

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.dataList.GetEnumerator();
    }

    #endregion
}

There’s a couple of things worth pointing out.  The first is that I’ve cleaned up the field names so that they can be used in code (by replacing spaces with underscores and removing anything in brackets).  The second is that if a value is an integer, I’m storing it as an integer.  I’m storing these values of type dynamic, which will come in handy when we want to query the data.

Speaking of querying that data, I wanted to use LINQ.  Here’s some simple code I wrote in a console application to try it out:


static void Main(string[] args)
{
    DataReader reader = new DataReader(@"C:\Users\Mark\Downloads\Eng-USA Data\Player Summaries.csv");

    var result = from dynamic player in reader
                 where player.Goals > 0
                 select player;

    foreach (dynamic player in result)
    {
        Console.WriteLine(player.Player_Name + " - " + player.Goals + " goals");
    }

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

And here’s the output:

image

By using the dynamic support in C#, the LINQ query just works and I can reference properties dynamically without having to create a class specifically for each sheet of data.  It’s important in the LINQ query to declare the player of type dynamic – otherwise C# will revert to its statically typed ways and inform you that the Goals property doesn’t exist, which, given that it only exists at runtime, is correct.  Now I can analyse the data easily.  Doesn’t change the result though…

Written by remark

June 17, 2010 at 12:41 pm

Posted in .NET, c#, Python

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.

Written by remark

April 30, 2010 at 8:44 pm

Posted in .NET, c#, Python

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

Simple Configuration with IronPython and .NET 4

with 2 comments

Recently I posted a short article about how to do simple configuration with IronPython.  I figured that it would be easier with .NET 4.0 thanks to the dynamic support.  And it is.  Using Visual Studio 2010, create a new Console Application.  Add one file called Configuration.py and set the copy output property to Copy Always.  Here’s the Python code for that file:


configuration.Text = "Hello from IronPython"
configuration.Count = 4

And here’s the code for the Program class:


class Program
{
    static void Main(string[] args)
    {
        dynamic configuration = ConfigureFromIronPython();

        for (int i = 0; i < configuration.Count; i++)
        {
            Console.WriteLine(configuration.Text);
        }

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

    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;
    }
}

For that to work you need add references to IronPython and Microsoft.Scripting (there’s a CTP of IronPython for .NET 4.0 that you can get here.)  You’ll also need a few using statements:


using System.Dynamic;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

The two things that make this work are the ExpandoObject and the dynamic keyword.  The ExpandoObject is a dynamic property bag that allows us to set and get members at runtime as needed.  The dynamic keyword means that .NET will resolve the properties at runtime (rather than the traditional behaviour of checking at compile time.)  The result is (I think) more simple and elegant than the configuration code to which I’ve become accustomed.

Written by remark

September 28, 2009 at 12:53 pm

Posted in .NET, c#, Design, Python

Simple configuration using IronPython

leave a comment »

I’ve just posted an article on how to use IronPython to configure an application – read it here.

Written by remark

September 14, 2009 at 4:49 pm

Posted in .NET, c#, Design, Development, Python

Use .NET classes in IronPython

with one comment

It’s really simple to use a .NET class in IronPython.  The first thing to remember is that you’ll need to add a reference (just like you would in .NET).  To do that you use the AddReference method of the clr module.  In the case of an assembly you’ve written, IronPython needs to be able to find it, which means it needs to be in sys.path.  It turns out that you can add the path to your assembly by using the append method of sys.path.  Here’s a simple example.  First, let’s create a simple class called User in C# in a solution called SampleClasses:

namespace SampleClasses

{

    public class User

    {

        public string Name{ get; set;}

        public DateTime DateOfBirth { get; set; }

    }

}

For the sake of simplicity, let’s copy the dll to a folder called lib on the C drive.  OK.  Time to fire up IronPython (which I’m going to assume you’ve already installed.)  Open a command prompt and type “ipy”.  You should see something like the following:

image

Next, let’s ensure the SampleClasses assembly is available to IronPython:

>>>import sys
>>>sys.path.append(‘C:\\lib’)

Once we’ve done that we can add a reference:

>>>import clr
>>>clr.AddReference(‘SampleClasses’)

Now, we need to import the User name from the SampleClasses namespace:

>>>from SampleClasses import User

We’re all set.  Create an instance of user and set one of the properties:

>>>a.User()
>>>a.Name=’Bob’
>>>a.Name
‘Bob’

That’s all there is to it.  Now you can go and experiment with IronPython and classes you’ve already written in .NET.

Written by remark

June 4, 2009 at 2:49 pm

Posted in .NET, c#, Python, Software