Jul 23, 2008

Where are the digital engineering notebooks?

I am a strong proponent of making everything in my life digital.  I own a tablet PC and spend most of my time working on engineering projects on the computer.  One thing that always drives me nuts is printing out stuff just to go into a engineering notebook.  First I'm waisting paper, second its not the write medium for an entire source code system.  Where are digital engineering notebooks? 

I propose that someone starts a company that is basically a limited access subversion repository ran by lawyers and IT specialist.  Client can use Tortoise SVN or the like to synch their directory changes to the server.  Someone can even have rules in place that require digital signatures before commits can happen.  If a patent dispute ever happens the company can show the directory in its state as of a certain disputed date.

What this gives you:

  • A way to backup your files
  • A clean medium for digital files like source code
  • Worry free notebook maintenance

The company could make a good deal of money charging for space and charging a lot of money for engineering audits. 

 

Well just a thought!

C# Code to cleanup Startmenu

I image my machine once a semester to clear off all the garbage I installed over the term. (I use partimage in case anyone cares).  Anyway I always have unsused shortcuts left in my startmenu since that lives on my d:\ drive which is where my data and profile are.  I wrote the code below to nuke bad shortcuts and empty shortcut directories.  It doesn't get all the uninstall files and readme links but its good enough!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using IWshRuntimeLibrary;

namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
string path = @"D:\Michael\Start Menu\";

WshShell shell = new WshShell();
string[] files = Directory.GetFiles(path, "*.lnk", SearchOption.AllDirectories);
Directory.CreateDirectory(@"C:\oldshortcuts");
List<string> directories = new List<string>();
foreach (string file in files)
{
IWshShortcut link = shell.CreateShortcut(file) as IWshShortcut;
if (!System.IO.File.Exists(link.TargetPath))
{
Console.Write("Broken Shortcut");
Console.WriteLine(file);
directories.Add(Path.GetDirectoryName(file));
try
{
System.IO.File.Copy(file, Path.Combine("C:\\oldshortcuts", Path.GetFileName(file)), true);
System.IO.File.Delete(file);
}
catch { Console.WriteLine("Error copying file and deleting file. Press any key to continue"); Console.ReadLine(); }
}
}
//now delete the old shortcuts
foreach (string dir in directories)
{
if (!Directory.Exists(dir)) { continue; }//we already deleted this!
DirectoryInfo info = new DirectoryInfo(dir);
if (info.GetFiles("*", SearchOption.AllDirectories).Length == 0)
{
try
{
info.Delete(true);
Console.Write("Deleting directory");
Console.WriteLine(info.FullName);
}
catch { Console.WriteLine("Error Removing Directory. Press any key to continue"); Console.ReadLine(); }
}
}


}
}
}

Jul 2, 2008

Debugging in IronPythonStudio with PythonEngine


I was charged by the company I work for to add a code editor to our IronPython Application scripting engine. Originally the code editor was supposed to just be a friendly IDE.. but I wanted break points too. PythonStudio seemed like a good choice since it ships as a standalone application and can do Python breakpoints via our friend "the red dot" for break points.



Here is how it all goes together. First I created a class library in PythonStudio. I set a break point in PythonStudio so that my source looks like this.




The next thing I did was build a simple PythonEngine host in C#.



static void Main(string[] args)
{
EngineOptions eo = new EngineOptions();
eo.ClrDebuggingEnabled = true; //this is important! this turns on debugging!
PythonEngine pe = new PythonEngine(eo);
//create the class def from our code base
pe.ExecuteFile(@"D:\Michael\My Documents\IronPython Studio\ClassLibrary1\ClassLibrary1\Class.py");
pe.Import("System");
pe.Import("System.Diagnostics");
Console.WriteLine("Please attach Python Studio if you want breakpoints :)");
Console.ReadLine();
pe.ExecuteToConsole("a=Class()");
}


You can see that I execute the python file from the directory that iron python studio placed its file. If you run this file in or out of debugging nothing happens. Why? Visual Studio doesn’t know about the breakpoints in your iron python studio project yet.



The final piece is attaching PythonStudio to the c# application where the python engine lives. To do this run the application without debugging.




Then in PythonStudio Attach PythonStudio to your c# application.




As you can see my project was called ConsoleApplication9.exe [Don’t we all love the visual studio generated project names]





Hit Enter in the command window once you have attached PythonStudio and watch your break point get hit!






Hit F5 and this will be your final output.




As you can see *most* of the work is done for you. All that is needed is delicate wiring of pieces and parts. Finally a way for us all to embed scripting that is more powerful than VBA and has a better editor than VBA.

IronPython Parser AST walker

I just thought I would post an example of an IronPython 1.1 parser for AST. You can override tons of methods on the ASTWalker class to get the job done.

Parser p=Parser.FromString(new IronPython.Runtime.SystemState(),new CompilerContext(),"a=5+10");
Statement e=p.ParseSingleStatement();
e.Walk(new ExpressionWalker());


public class ExpressionWalker : IronPython.Compiler.Ast.AstWalker
{
public override void PostWalk(NameExpression node)
{
base.PostWalk(node);
}
}

Jun 30, 2008

Python Generation using the Code Dom

I was tasked with translating a meta data scripting engine from metadata to ironpython. Luckily, the CodeDom was the solution. I've gotta give those M$ credit here!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.CodeDom;
using IronPython.CodeDom;
using System.CodeDom.Compiler;


namespace TestApp
{
class Program
{
static void Main(string[] args)
{
PythonProvider prov = new PythonProvider();

//Create a test class
StringWriter sw=new StringWriter();
CodeTypeDeclaration targetClass = new CodeTypeDeclaration("testclass");
targetClass.IsClass = true;

System.CodeDom.CodeConstructor cont = new CodeConstructor();
cont.Parameters.Add(new CodeParameterDeclarationExpression("int", "test"));
cont.Name = "testclass";
targetClass.Members.Add(cont);

prov.GenerateCodeFromType(targetClass, sw, new CodeGeneratorOptions());
Console.Write(sw.ToString());

//create an assignment statement
sw = new StringWriter();
CodeAssignStatement as1 = new CodeAssignStatement(new CodeVariableReferenceExpression("i"), new CodePrimitiveExpression(10));
prov.GenerateCodeFromStatement(as1, sw, new CodeGeneratorOptions());
Console.Write(sw.ToString());
}
}
}


And the output is...

class testclass(object):
__slots__ = []

def __init__(self, test):
pass

i = 10
Press any key to continue . . .

May 27, 2008

Useful Powershell Script

Have you ever had a list of .ISO's that you needed to burn but were tired of setting each iso to burn individually. I often have this problem when I need to burn for instance the 5 iso's that vista shipped with. Below is a useful powershell script that will scan a directory or directories, find the ISO file, and ship it to ImgBurn. I have it setup so that it deletes the image. All you have to do is wait for the drive to eject your dvd.


function burn()
{
#get the files and ship them to burn-file
dir -recurse -include *.iso -path c:\,d:\,e:\ | foreach { burn-file $_.FullName }
}

function burn-file($filename)
{
#call img burn with the nessessary arguments
. "c:\Program Files\ImgBurn\ImgBurn.exe" /mode ISOWRITE /WAITFORMEDIA /start /close /DELETEIMAGE /EJECT /SRC $filename
#Wait for IMGBURN to finish (right now you can only have one open at a time)
while ( (get-process | where{$_.ProcessName -eq "ImgBurn"}) -ne $null)
{
#sleep for a bit to let the processor work on burning
Start-Sleep –s 10
#tell the user what file you are still working on
"waiting for $filename to complete"
}

}

#call the burn function
burn


kick it on DotNetKicks.com

May 21, 2008

Taking lessons from Web 1.0 and Web 2.0 to create web sync.

With the dawn of Web 2.0 web developers have enjoyed developing rich desktop like clients and speedy flashy websites that they could have never constructed before. Web 2.0 brought us the ability to maintain state while making requests to the server giving users the experience similar to desktop applications. What Web 2.0 didn't provide was a good model for taking results from those requests and operating on them appropriately. All the model controller code we wrote for Web 1.0 is now useless. While this doesn’t sound like a problem from the beginning it is a staggering problem considering all the controls available and that all of them have to be reimplemented in javascript. I'll admit that there are many new libraries out there but they can be difficult to extend.


In review :

Web 2.0

Good
Provides a way to make asynchronous requests
Provides a way of requesting simple lightweight data and translating that to the screen

Bad
All the web 1.0 models have to be rewritten
Some operations can be quite weighty on the client end
No clean model of what should happen on the server and what should happen on the client


I wont discuss the success of Web 1.0 but the problems were obvious. Small state changes to a page required the user to redownload the entire page. For some websites this is still a clean model but for most this causes bandwidth issues.

My solution to take the good from both and leave the bad is to build what I am dubbing as "Web Synch". Web synch is a way to take an existing proven web 1.0 architecture like cgi, php, asp, and asp.net and give it the advantages of ajax. This takes the models we built in web 1.0 and the asynchrounous technology we developed in web 2.0 and combines them in a powerful new way. This new way operates on the principal of using the web 1.0 models to render a page. Once rendered the server sorts out what has changed. The changes are then shipped to the client rather than the entire page.

Below is a suggested implementation of web synch.

Initial request comes to the server
Page is rendered
Pages rendering is shipped as the response
The response is cached on the server
Page is submitted
Submit is captured and cancelled via "onsubmit" event
Form is serialized

Ajax request made simulating Postback
Second request comes to the server
Page is rendered
rendering compared to the previous request
difference serialized to json (add,update,delete,move)

Page returns from ajax request
JSON is deserialized
changes are merged into the dom


There are a few issues with this idea. One, it is going to require some memory management to store the old rendered page on the server. Two, it will take some processing power to determine changes to the rendered page. Finally, the only way to know that the page has changed is to have well formed html (everything must have ID's).


My first iteration will be targeted for asp.net server side and prototype on the client side.

kick it on DotNetKicks.com

Followers