Click here to Skip to main content
15,893,487 members
Articles / Artificial Intelligence

Embedding IronPython in a C# Application

Rate me:
Please Sign up or sign in to vote.
4.61/5 (11 votes)
21 Jan 2010MIT7 min read 112.1K   3.9K   53  
Examples of embedding the Iron Python engine in a C# form application.
using System;
using System.Collections.Generic;
using System.Threading;

namespace IronPythonTest2
{
    public class SimpleLogger
    {
        private Mutex _mutex = new Mutex(false);
        private UInt32 _entryCount = 0;
        public class Entry
        {
            public enum EntryType
            {
                Info,
                Warning,
                Error,
                Fault
            }

            private EntryType _entryType;
            private DateTime _timestamp;
            private String _msg;
            private UInt32 _index;

            private Entry()
            {
            }

            public Entry(EntryType entryType, String msg, UInt32 index)
            {
                _msg = msg;
                _timestamp = DateTime.Now;
                _entryType = entryType;
                _index = index;
            }

            public String msg { get { return _msg; } }
            public DateTime timestamp { get { return _timestamp; } }
            public EntryType entryType { get { return _entryType; } }
            public UInt32 index { get { return _index; } }

            public override string ToString()
            {
                return String.Format("[{0}][{1}][{2}][{3}]", _timestamp,_index, _entryType, _msg);
            }
        }

        private List<Entry> _entries = new List<Entry>();

        public void Reset()
        {
            try
            {
                _mutex.WaitOne();
                _entries = new List<Entry>();
            }
            finally
            {
                _mutex.ReleaseMutex();                
            }
        }

        public Int32 Count
        {
            get
            {
                _mutex.WaitOne();
                Int32 result = _entries.Count; _mutex.ReleaseMutex(); return result;
            }
        }

        /// <summary>
        /// Gets the first entry in log and removes it from the log.
        /// Returns null if the log is empty.
        /// </summary>
        /// <returns></returns>
        public Entry GetFirst()
        {
            Entry result = null;
            try
            {
                _mutex.WaitOne();
                if (_entries.Count > 0)
                {
                    result = _entries[0];
                    _entries.RemoveAt(0);
                }

            }
            finally
            {
                _mutex.ReleaseMutex();                
            }
            return result;
        }

        /// <summary>
        /// Retrives all the entries from the log.  The log will be 
        /// empty after the operation has been executed.
        /// </summary>
        /// <returns></returns>
        public List<Entry> GetAll()
        {
            List<Entry> result = null;
            try
            {
                _mutex.WaitOne();
                result = _entries;
                _entries = new List<Entry>();
            }
            finally
            {
                _mutex.ReleaseMutex();
            }
            return result;
        }

        public void AddInfo(String msg)
        {
            try
            {
                _mutex.WaitOne();
                _entries.Add(new Entry(Entry.EntryType.Info, msg,_entryCount++));
            }
            finally
            {
                _mutex.ReleaseMutex();                
            }
        }

        public void AddWarning(String msg)
        {
            try
            {
                _mutex.WaitOne();
                _entries.Add(new Entry(Entry.EntryType.Warning, msg, _entryCount++));
            }
            finally
            {
                _mutex.ReleaseMutex();
            }
        }

        public void AddError(String msg)
        {
            try
            {
                _mutex.WaitOne();
                _entries.Add(new Entry(Entry.EntryType.Error, msg, _entryCount++));
            }
            finally
            {
                _mutex.ReleaseMutex();
            }
        }

        public void AddFault(String msg)
        {
            try
            {
                _mutex.WaitOne();
                _entries.Add(new Entry(Entry.EntryType.Fault, msg, _entryCount++));
            }
            finally
            {
                _mutex.ReleaseMutex();
            }
        }

        public void AddFault(Exception ex)
        {

            try
            {
                _mutex.WaitOne();
                String msg = ex.Message;
                if (ex.InnerException != null)
                    msg += " (+INNER): " + ex.InnerException.Message;
                _entries.Add(new Entry(Entry.EntryType.Fault, msg, _entryCount++));
            }
            finally
            {
                _mutex.ReleaseMutex();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
President Nonlinear Ideas Inc.
United States United States
BS EE - 1993
MS EE - 1995
Went over to dark side of software - Officially in 1999, but I've been writing code since I was about 14 (on a Commodore 128).
Company Founded - 2002
Things I do - Write software, solve problems, get teams of people to work together, design architectures, mentor, spin, chop, collate, etc.
Interests: Software Architecture, Artificial Intelligence, Video Games.

Comments and Discussions