Archive for the ‘Software Development’ Category

Jan
12

From Glue Logic to Program Logic

My first introduction to the Python computer language was in the late 90’s.  The project I was on was being moved to “an evil empire” in Oregon and I was tasked with transferring the entire contents of our CR database into “something” that they could easily parse and/or import.  At this time, Python was (as far as I was concerned) a great “glue logic” language.  You wouldn’t use it to deliver your application, but you could use it for quick one-off tasks like building your code base, doing large scale search-and-destroy (or modify) file operations, etc.  I used it to execute queries against the database and push the data into several output files.

Since then, I’ve worked on a few Python applications:

  • SNMP MIB Browser, delivered as a downloadable program for Windows
  • A tool to track requirement and test case changes in MS Word and handle traceability and user notification
  • A unit-test suite for the code base of a dialysis machine
  • A tool-chain for “building” the pattern/image sets that go onto an SD memory card in a pattern cutter.
  • A lint processing system; zillions of code files in, one simple ranked report out.

There were numerous other small projects scattered throughout.  All of these were 100% Python.  I’ve even seen a company where their entire server based sensor system (thousands of input readings in real-time) is built almost entirely on a python framework.  Python has evolved from being the “glue-logic” that helps the build process or the release process to the “program logic” that makes the application work.

IronPython

IronPython is an implementation of the Python language which runs on the .Net framework.  That is to say, it is a collection of .Net assemblies that allow you to compile and execute Python code as a .Net language such as C#, Visual Basic, etc.  As of the current release (2.6), you cannot use many of the existing Python (called cPython) libraries with it, as they are actually binary libraries (or rely on them), and cannot interface with IronPython.  On the other hand, you can access any compatible .Net assembly.  This gives you a (very) large space from which to draw capabilities that you use IronPython to glue together and solve the task at hand.

There are essentially three ways you can use IronPython.  The first is that you can use it to create whole applications by itself.  The second is to have a primary implementation in a .Net language (e.g. C#) as your main program logic and then use the IronPython engine (and Python code) to handle details that are dynamic.  The third is for quick “knock off” scripts used to do system operations or small easy tasks.

I strongly recommend staying away from the first option.  Before the flame wars begin and I’m branded as a heretic, you should recognize that while IronPython executes as a .Net language, the support for it in Visual Studio is minimal, at best.  An add-on for using it in VS, IronPython Studio, hasn’t been updated in 2 years and the remarks on it are mixed at best.  If you are working in .Net and you’ve worked in just about anything else for comparison, you know that intellisense, refactoring, and GUI development support in VS is pretty good.  Without it, you’re guessing the names of imports, classes, functions, manually adding callbacks, fiddling with code (and recompiling) to get the GUI just right;  that’s a hassle and more importantly, a waste of valuable time.

The second option, the one which this article focuses on, seems like a very good use of IronPython.  Consider the following scenarios:

  1. You are delivering a business application to your end users.  They each have a certain way they like the application configured.  Or they each have a custom processing need (e.g. business logic).  Your application is the engine that contains and performs the main operations on the data.  You can use IronPython to create custom scripts for each customer to handle their particular configuration and business logic.  When they need updates or modifications, you send them updated Python scripts.
  2. You have a server based application and users have a “client” on their desktop.  The client application allows them to pull/push data to/from the server.  You provide extra processing capabilities in the form of scripts that the users can either create or obtain from your organization.
  3. You want to write a video game.  Using a scripting engine in video games is very common.  All sorts of “operations” and “capabilities” (initialization, dialog, plot, victory condition, artificial intelligence, level instantiation, etc.) that are constantly changing during development not become scripted and can be handed off to non-programmers.  The syntax of Python is relatively easy and you don’t have to recompile the engine every time you change the code.

The third option is on an “as-needed” basis.  For these types of operations, I usually stick to cPython; the libraries are there and existing Python tools recognize the existing packages without any hassles.

Examples

The first thing we should do is ask ourselves how we think IronPython will be used.  I created a list of operations/situations that I wanted to create an example for so that I could understand how the limits (and syntax) of using IronPython:

  1. Define a class in C#, access it from Python.
  2. Define a class in Python, create an instance and call a member function from C#.
  3. Execute a Python function (not the same as a class).
  4. Define a Python variables as module level variables, access and manipulate them from C#.
  5. Pass a function pointer (delegate) from C# to Python.  Allow Python to call it with an argument.
  6. Execute a “yield” operation in Python.
  7. Trap a Python script compile error.
  8. Trap a Python script execution error.

I’m going to discuss some of these but most should be fairly obvious (and the comments in the code (download from here) should be sufficient to tie it all together.

Environment

  • The entire application code was written in Visual Studio 2008 Professional edition.  The code should run without an issue in the Express edition as well.
  • I used IronPython version 2.6, which can be found here.

Test Harness

In order to execute the tests, I created a new project as a simple C# Windows Forms application.  A screenshot of the finished product is below.

Executing IronPython Tests

Executing IronPython Tests

The program is little more than a form with buttons for executing tests, a “SimpleLogger” class for logging messages, a timer updated for updating the logger, and the code for executing the tests themselves.  The “Setup Python” button initializes the IronPython engine and scope (you can segment variables and data by “scope”).  I left it in so you could see that it takes a non-trivial amount of time to initialize the engine.

Because the entire code base can be downloaded, I’m going to be sparse on including code here unless it will be value added to do so.  I will walk through the first example, executing a C# class from Python, so that you get the feel for the flow of execution.

Infrastructure

The following statements include the IronPython and Microsoft Dynamic Language Runtime (DLR) namespaces.

using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;

I’ve also include the following declarations at the top of the main form class:

private ScriptEngine pyEngine = null;
private ScriptRuntime pyRuntime = null;
private ScriptScope pyScope = null;
private SimpleLogger _logger = new SimpleLogger();

The SimpleLogger class is included in the source code.  It is a thread-safe tool for creating log entries and works both from the C# and the Python side.

Initialization

The following bit of code initializes the engine and sets up the “scope”.  The ScriptScope is the container that holds all the variables, functions, and class definitions that you would like to “group” together.  When the engine executes, it executes against a scope.

            if (pyEngine == null)
            {
                pyEngine = Python.CreateEngine();
                pyScope = pyEngine.CreateScope();
                pyScope.SetVariable("log", _logger);
                _logger.AddInfo("Python Initialized");
            }
Note that the _logger object is passed into the Python Scope by pyScope.SetVariable(…).  Now Python has a reference to the SimpleLogger instance and can reference the methods it exposes.

I also created a simple function to take a string (Python code statements), compile them and execute them in the scope defined.

        private void CompileSourceAndExecute(String code)
        {
            ScriptSource source = pyEngine.CreateScriptSourceFromString(code, SourceCodeKind.Statements);
            CompiledCode compiled = source.Compile();
            // Executes in the scope of Python
            compiled.Execute(pyScope);
        }

Now all you have to do is construct the Python code using strings, hand it off to the function, and it executes automatically.

For the first test, we want to add an element to the _logger from Python.  First, we need to create the Python code.  We can do this by loading it from a file or by just passing in the string.  For this project, I’m going for a “self-contained” solution, so the Python code will be created by C# directly:

        private String CreatePythonScript1()
        {
            String result = "";
            string[] lines =
                {
                    @"def DoIt1(logObj):",
                    @"   logObj.AddInfo('Executed in a function call using log object input.')",
                    @"",
                };
            result = String.Join("\r", lines);
            return result;
        }

I’m being a bit anal and using the “@” for raw strings here.  In Python, a string can be either enclosed in single quotes or double quotes (and even triple single/double quotes), but I’m going to stick with single quotes so the editor doesn’t start coloring/un-coloring and confusing me.  This is a simple declaration of a Python function, “DoIt1(logObj)”, which takes an argument logObj.

When you hit the “Test 1″ button, the following code gets executed:

        private void btnTest1_Click(object sender, EventArgs e)
        {
            CompileSourceAndExecute(CreatePythonScript1());
            CompileSourceAndExecute("DoIt1(log)");
        }

The first line compiles the Python function.  The second passes in the _logger object, defined in the Python scope as “log”.  And the list on the GUI gets “magically” updated.

The rest of the code contains the remaining examples.  Unless you are looking for something fairly exotic, I think I’ve covered the majority of the examples for usage that I think are practical for working applications.  Feel free to leave feedback if you have any trouble or need clarification.

The Source Code can be downloaded from this link:  IronPythonTest2 Source Code

, , , , , ,

Jan
08

Coding is all about grammar, software is all about philosophy.
– Unknown

Programming can be fun, so can cryptography; however, they should not be combined.
–Charles Kreitzberg and Ben Shneiderman

The sooner you start to code, the longer the program will take.
–Roy Carls

Our developers never release code. Rather, it tends to escape, pillaging the countryside all around.
–The Enlightenment Project

Perl is the crystal meth of programming: it’s so incredibly useful when you need to do a large amount of work in a small amount of time that you tend to overlook the fact that it’s basically precipitating the implosion of your vital organs.
–Dan Martinez

Programmers are the tools for converting caffeine into code.
–Unknown

If you lie to the compiler, it will get its revenge.
–Henry Spencer

There are only two industries that refer to their customers as users.
–Edward Tufte

Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the software engineer.
–Fred Brooks, Jr.

To iterate is human, to recurse divine.
–L. Peter Deutsch

Good judgment comes from experience, and experience comes from bad judgment.
–Fred Brooks

There are two ways to write error-free programs; only the third works.
–Alan J. Perlis

When Leo Tolstoy wrote Anna Karenina, he could have been thinking about cubicles: “there are no conditions of life to which a man cannot get accustomed, especially if he sees them accepted by everyone around him.”
–Leo Tolstoy

The real value of tests is not that they detect bugs in the code but that they detect inadequacies in the methods, concentration, and skills of those who design and produce the code.
–C.A.R. Hoare

The most important single aspect of software development is to be clear about what you are trying to build.
–Bjarne Stroustrup

Most of you are familiar with the virtues of a programmer. There are three, of course: laziness, impatience, and hubris.
–Larry Wall

I did say something along the lines of C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows your whole leg off.
–Bjarne Stroustrup

There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies.
–C.A.R. Hoare

Any fool can use a computer. Many do.
–Ted Nelson

Trying to outsmart a compiler defeats much of the purpose of using one
–Brian W. Kernighan and P. J. Plauger

UNIX is simple. It just takes a genius to understand its simplicity.
–Dennis Ritchie

Putt’s Law: Technology is dominated by two types of people–those who understand what they do not manage and those who manage what they do not understand.

An organization that treats its programmers as morons will soon have programmers that are willing and able to act like morons only.
–Bjarne Stroustrup

Theoretically, software is the only component that can be perfect, and this should always be our starting point.
–Jesse Poore

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
–Damian Conway

Documentation is a love letter that you write to your future self.
–Damian Conway

If you think good architecture is expensive, try bad architecture.
–Brian Foote and Joseph Yoder

Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why.
–Unknown

If the code and the comments disagree, then both are probably wrong.
–Unknown

Those who want really reliable software will discover that they must find means of avoiding the majority of bugs to start with, and as a result, the programming process will become cheaper. If you want more effective programmers, you will discover that they should not waste their time debugging, they should not introduce the bugs to start with.
–Edsger Dijkstra

In theory there is no difference between theory and practice. In practice there is.
–Yogi Berra

For a successful technology, honesty must take precedence over public relations for nature cannot be fooled.
–Richard Feynman

One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs.
–Robert Firth

Hofstadter’s Law: It always takes longer than you expect, even when you take into account Hofstadter’s Law.

PHP is a minor evil perpetrated and created by incompetent amateurs, whereas Perl is a great and insidious evil, perpetrated by skilled but perverted professionals.
–Jon Ribbens

If you want a girlfriend, avoid working in the computer games industry like the plague. If you work seven days a week, 15 hours a day for almost two years, with barely enough time for a pint, you have no time whatsoever for relationships. Plus computer-games makers are regarded as being about as hip and cool as abattoir workers.
–Toby Gard

Your problem is another’s solution; your solution will be his problem.
–Unknown

Embedded lines of code are growing 26% annually but developers are increasing by 8%.
–Venture Development Corporation

Productivity can decrease by as much as 25% when workers put in 60+ hour weeks for a prolonged time. And, turnover is nearly three times higher among workers who work extended hours. Absenteeism among companies with extended hours is more than twice the national average.
–Reworded from Circadian Technologies Shiftware Practices 2005 survey

There’s a fine line between being on the leading edge and being in the lunatic fringe.
–Frank Armstrong

Two things are infinite: the universe and human stupidity; and I’m not sure about the universe.
–Albert Einstein

Some people, when confronted with a problem, think I know, I’ll use regular expressions. Now they have two problems.
–Jamie Zawinski

The most amazing achievement of the computer software industry is its continuing cancellation of the steady and staggering gains made by the computer hardware industry.
–Henry Petroski

One test is worth a thousand opinions.
–Unknown

If the lessons of history teach us anything it is that nobody learns the lessons that history teaches us.
–Unknown

The trouble with the world is that the stupid are cocksure and the intelligent are full of doubt.
–Bertrand Russell

Debugging is like alien abduction. Large blocks of time disappear, for which you have no explanation.
–Unknown

Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves.
–Alan Kay

If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilization.
–Gerald Weinberg.

Ugly programs are like ugly suspension bridges: they’re much more liable to collapse than pretty ones, because the way humans (especially engineer-humans) perceive beauty is intimately related to our ability to process and understand complexity. A language that makes it hard to write elegant code makes it hard to write good code.
–Eric S. Raymond

Let us change our traditional attitude to the construction of programs. Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do.
–Donald Knuth

The most unsuccessful three years in the education of cost estimators appears to be fifth-grade arithmetic.
–Norman R. Augustine

No engineer looks at a television remote control without wondering what it would take to turn it into a stun gun. No engineer can take a shower without wondering if some sort of Teflon coating would make showering unnecessary. To the engineer, the world is a toy box full of suboptimized and feature-poor toys.
–Scott Adams

I love deadlines. I like the whooshing sound they make as they fly by.
–Douglas Adams

In handling resources, strive to avoid disaster rather than to attain an optimum.
–Butler Lampson

There are various reasons that software tends to be unwieldy, but a primary one is what I like to call “brittleness”. Software breaks before it bends, so it demands perfection in a universe that prefers statistics.
–Jaron Lanier

People tend to overestimate what can be done in one year and to underestimate what can be done in five or ten years.
–Joseph Licklider

Code generation, like drinking alcohol, is good in moderation.

--Alex Lowe

Nothing makes the software work like the hardware.

– Unknown

To error is human, but announcing your brand new Twitter, YouTube, Blog, and Forum Page in the same worldwide e-mail
as an application update is just asking for Trouble.
– Unknown

Overall pedigree unknown; attributed to a post on embedded.com; read at your own risk.

, , ,

Jan
05

I always keep a few sticky notes on my desktop to remind me about the realities of the (seemingly endless) tasks at hand:

The Six Stages Of A Project
1. Enthusiasm (This project rocks!)
2. Disillusionment  (This project blows chunks!)
3. Panic (How is this my fault?!)
4. Search for the Guilty (Run Away!  Run Away!)
5. Punishment of the Innocent (Blame the new hires!)
6. Rewards for the Non-Participants (Can you say MIP?)
The Five Rules Of Plumbing
1. Hot on the left
2. Cold on the right
3. Waste flows downhill
4. When in doubt, add a vent
5. Payday is on Friday

Happy Tuesday

, , , ,